Daily Notes of Learned Things 230813

X anonymous
4 min readAug 13, 2023

--

The first DNLT which talks about CPU’s job.

The news — Putting the “You” in CPU

In Putting the “You” in CPU , it explained in detail how your CPU operates.

The article covers some parts in both “Operating System Concepts” & “Computer Architecture”. Compared to university courses, this article explains many complex concepts in an easily understandable manner.

It is mainly divided into seven chapters, and I will provide my summary overview of each chapter in recent day’s DNLT. Please refer to the original content for detailed information.

The “Basics”

The function of a CPU is to execute the content described by "instructions." Each instruction is typically divided into two parts: opcode and data. The opcode represents the intent of the instruction, while the data stores certain information needed for that intent.

For the CPU, an "instruction" is actually a string of information composed of 0s and 1s, and this information includes the aforementioned parts, opcode and data. We refer to this syntax of information as binary. Assembly is a syntax that makes this string of 0s and 1s more comprehensible to people. When we need to convert between Assembly and Binary, we first need to write out the Machine code, which is, in fact, writing out those 0s and 1s in a hexadecimal manner.

All instructions that needed to be executed by the CPU must be stored in RAM. The CPU stores an "instruction pointer" in Register to record where the next instruction to be executed is located in RAM. Therefore, the basic task of the CPU is to read the location of the instruction recorded in the instruction pointer from RAM, then read the instruction at that location and execute it. This is known as the "fetch-execute cycle."

The operating system is a combination of software responsible for enabling many of the computer's basic functions to run. The Kernel is the core of the operating system and is the first program to be executed when the computer is booted. The Kernel has almost complete hardware access rights and is responsible for getting the programs installed on the computer to operate. The most well-known Kernel is Linux; others include macOS's XNU and Windows' NT Kernel.

The CPU can control which instructions are allowed to be executed through different modes (also known as privilege levels or rings), usually consisting of at least two types: Kernel mode and User mode.Kernel mode allows the execution of all supported instructions and can access any memory address, but user mode can only execute a subset of instructions, and both IO and memory access are restricted. Typically, the kernel and drivers will run in kernel mode, while other subsequently installed applications will run in user mode.

Running programs in user mode can prevent direct access to IO and memory, thereby avoiding damage, but programs still need to use IO and memory-related resources in some way. Therefore, the operating system kernel plays the role of a communication bridge, providing a "system call" API to user mode programs, allowing them to delegate access to restricted resources through the OS.

When the operating system starts, it creates a table (IVT) with IDs corresponding to instruction addresses. When an "interrupt" occurs, the system can look up where to begin executing based on the interrupt ID (INT). System calls are made through these "interrupts." For example, in Linux, the system call operation in kernel mode is taken over by using the instruction corresponding to the ID 0x80. After executing in kernel mode, the kernel will use the IRET instruction to inform the system to switch back to user mode, find the instruction prior to the interrupt, and continue execution from that point.

When the kernel executes the actual content of a system call, the method often varies due to hardware architecture or other factors. Therefore, system calls are implemented differently in various architectures. The operating system provides an abstract interface library for system calls, allowing programs to be developed without concern for how the system calls are actually implemented. This helps make program development more efficient, reusing the same methods across different platforms. Common examples of these libraries include Unix-like libc++ or Windows' ntdll.dll. When a program calls methods in these libraries, it does not directly cause a context switch, as this action is implemented through methods within the library.

That’s it! I hope this summary helps you understand chapter 1. I’ll continuously give my summary of the rest of chapters in my future posts.

--

--