Key words: register, ring0, ring3, kernel state, user state

The register is an important part inside the CPU. The register is composed of N flip-flops inside, and each flip-flop can save 1 bit of binary number, so the 16-bit register can save 16 bits.

CPU generally has multiple registers of different types inside, and we need to use the CPU corresponding machine instructions to operate these registers, of course, like memory, disk and these are also operated by machine instructions.

The CPU, for example, the x86 CPU divides machine instructions into general instructions and privileged instructions for security purposes. For example, instructions to operate the disk are privileged instructions, and only the CPU can execute privileged instructions when it is in a special state.

The x86 CPU uses a special internal register to mark whether the CPU can execute privileged instructions at this time. This special register can store four states, ring0, ring1, ring2, ring3.

Windows and Linux operating systems use only two states, ring0 and ring3. If in ring0, it means that the CPU can execute all instructions, including privileged instructions, and if in ring3, it means that the CPU cannot execute privileged instructions. ring0 level is high and ring3 level is low.

Both the operating system and the software that runs on top of it are developed in high-level languages that eventually need to be translated into machine instructions.

So essentially, our own software developed in c or java, as long as it is translated into machine instructions, is also capable of manipulating registers directly and operating disks.

But we don't do that, and we certainly don't need every piece of software to implement such a low-level and generic function on its own, so we usually call operating system functions to manipulate the disk.

The operating system is the equivalent of an intermediate layer.

Also the operating system is designed with a kernel state and a user state in order to protect the system.

When our computer starts up, the CPU is in ring0 state, when all instructions can be executed, thus starting the bootloader and thus the operating system. When the operating system starts up, it will divide the memory and set aside a portion of memory that can only be used by the operating system itself, while the rest of the memory can be used by the application software.

After the operating system has finished booting, the CPU status changes to ring3 and starts running the application.

Since the cpu is in ring3 at this time, it is definitely not possible for the application software to run some special instructions.

When we call the function provided by the operating system, the operating system will come to execute the privileged instruction, but the operating system is not also the code written in c language, to execute the privileged instruction needs ring0, how to switch ring3 to ring0?

System interrupt, in fact, is an instruction, such as int 0x80.

When the system interrupts, the cpu will automatically cut back to the ring0 state and then execute the code set by the OS at system startup, and this code can continue to execute subsequent logic according to the code executed before the interruption, and at this time the cpu is already in the ring0 state and can execute normally.

The CPU in ring0 state is what we call the kernel state, and in ring3 state is what we call the user state.

To sum up, when we write our own program to operate the disk, because we want to execute privileged instructions, but the CPU is in ring3, can not directly execute special instructions, need to call the operating system functions, which will modify the CPU in ring0, and thus go into the kernel state.

In the user state, the CPU can only execute some common instructions, and in the kernel state, the CPU can execute all instructions.

This is all we have to talk about today. If there is something wrong, welcome to point it out.