This is the NTH day of my participation in the August More Text Challenge. For details, see:August is more challenging

Section 2.2.1 Formation of user mode and kernel mode

With the advent of kernel mode, the power of the computer system is highly concentrated towards the operating system.Copy the code

The operating system is divided into kernel mode and user mode for different levels of permission management, so as to better adapt to the multi-user and multi-task concurrent working environment.

Source of user mode and kernel mode

In early single-process, single-user operating systems, there were no two user modes.

With the emergence of processes and users, computers faced an important problem of how to restrict the permissions of different processes. (Because the operating system cannot predict what future processes will occur)

Imagine the computer interior as an orderly factory, that must be between the manager and the management of the rules, the authority of each position must be determined.

So engineers, at the time, were prepared to allow some dangerous operations to be done only by operating system processes. If another user process wants to do this, it must first ask the operating system for advice. The operating system then does the work and passes the data to the inquiring user process. (such as I/O operations)

With the advent of kernel mode, operating system processes/threads officially override user threadsCopy the code

Permissions are essentially different things that can be done.Copy the code

The kernel mode and user mode of the CPU appear

If it is only the different permissions of the operating system to the process/thread, it is not called “state”. However, with the popularity of the permission restriction function of the operating system, CPU manufacturers also keep pace with The Times and began to restrict the permission at the hardware layer.

Operating system manufacturer: CPU factory, now we all need permission restriction function, can you do a design in the hardware layer. The software layer is not safe enough and the speed switch is slow.

CPU vendor: Ok, I designed a two-bit flag in the program status register to provide at least four states. Your operating system changes the status bit of this register, so my CPU knows what permission level is at this point.

Modern operating systems generally use only the highest ring 0 and lowest ring3, which are called kernel mode and user mode, respectively.Copy the code

The CPU restricts the set of instructions that can be executed from the hardware layer, depending on the status bit.

The operating system changes the value of registers on the CPU to switch permissions. This process is the switch between kernel mode and user mode. Visible kernel mode, user mode is for the entire computer system, rather than for a thread.Copy the code

Question 1: How do we define high rights?

If a thread can access and modify all the memory and registers in the computer system. Then we assume that this thread has the highest privileges.

This kind of highest permission thread, generally belongs only to the operating system.Copy the code

Question 2: Do all operating systems have two states?

No, early operating systems, such as MS-DOS, didn’t have two states. The two states were created because the operating system had to deal with the need for multiple user permissions and concurrent tasks.

Question 3: The relationship between user thread and kernel thread and kernel mode and user mode

It doesn’t matter. Thread switching must be decided by the high-privilege thread of the operating system (by means of interrupts), it is not possible for ordinary threads to do it themselves. So any thread switching must be done in kernel mode. The user thread is not a real thread at all, so its switch in user mode is only a pseudo-switch in essence.

The relationship between user thread and kernel thread is true or false. Kernel mode, user mode is the relationship between the level of permission.Copy the code

It is not recommended to say “a thread is in kernel mode” because kernel mode is a description of the permissions of the operating system at a given time. Better to say “the operating system is executing a thread in kernel mode at this time”.

Example example 1: When a thread requests an I/O operation from the operating system

  1. Thread A requests I/O operations from the operating system because the permission is insufficient
  2. The operating system agrees to interrupt thread A.
  3. The operating system modifies the CPU register status, causing the computer to enter the kernel mode.
  4. The operating system calls its own thread with high permission to perform I/O operations. (Note that you don’t call thread A to do it)
  5. Modifies the CPU register status to make the computer enter user mode.
  6. Send the data to thread A.
  7. Call thread A.
The appearance of kernel mode comes from the design of multi-permission function, and with the improvement of the HARDWARE level of CPU manufacturers, it is formally formed. Kernel mode not only limits permissions, but also the operating system generally only allows its own threads to work in kernel mode. The further centralization of the privileges of the computer into the operating system facilitated the formation of the computer architecture.Copy the code

Zobol by Computer Operating system Learning Notes