Moment For Technology

What is the difference between user mode and kernel mode

Posted on Nov. 27, 2023, 9:21 p.m. by James Knight-Boon
Category: The back-end Tag: The back-end The operating system

The depth of this article is not too deep, the key is to understand the difference between user mode and kernel mode.

For those of you who don't know about kernel-mode and user-mode, when will we mention these two concepts?

For example, our application needs to read data from a file from disk. Instead of loading data directly from disk into application memory, we need to:

  • Copy data from disk to kernel Buffer
  • Copy the data from the kernel Buffer to the user Buffer

So that's the concept of user mode and kernel mode. First of all, let's define it. These two states are the runlevels of the operating system.

And then we know that the programs that we write will actually be compiled and interpreted to be executed by the CPU, one by one.

Both user-mode and kernel-mode instructions are executed by the CPU, so we can say that this state actually represents the current STATE of the CPU. So since these instructions are ultimately executed by the CPU, what's the reason for the distinction?

That's because CPU instructions have different permissions, depending on how important they are. Some commands fail innocently, while others crash the entire operating system and require a reboot. Leaving these instructions open to applications at random increases the probability that the entire system will crash.

Let's take a similar example. Let's design a class that has some very important variables that you probably won't declare public, right? You should declare them private and develop a few special methods to modify them, doing a series of validations on the values passed in before setting them.

For example, Intel X86 divides CPU instructions into four levels:

The level of authority between them can be identified by this diagram:

The IA in the figure above refers to Intel Architecture

So you can see that the closer the core is, the higher the permissions are. In other words, the permission ranges from Ring0 Ring1 Ring2 Ring3

In Linux, there are only Ring0 and Ring3 level instructions, so we can give a more detailed description of the user state and the kernel state: running Ring0 level instructions is called the kernel state, running Ring3 level instructions is called the user state.

Knowing the concept of instruction set permissions, we can correct the above description again: what state actually represents what level of instruction the CPU is currently executing

Now that we know the difference between user and kernel, and why, we can see when to switch from user to kernel.

The answer is when a system call occurs

So what is a system call? Look at this picture

When a user - mode program needs to apply for services with higher permissions from the operating system, it sends a request to the kernel through system call.

The kernel also provides many interfaces to call, such as requesting dynamic memory space. But do you have to consider freeing memory? If this memory management is handed over to the application, the complexity of management can become a development burden.

So the library function is used to mask the internal complicated details, our application can call the interface provided by the kernel through the library function, and the library function will initiate the system call, after the system call, the user state will switch to the kernel state to execute the corresponding kernel method.

In addition to system calls, there are two other ways to cause state transitions: exceptions and interruptions.

Welcome to wechat search and follow [SH full stack Notes]. If you think this article is helpful to you, please click a like, close a note, share a share and leave a message.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.