当前位置:网站首页>User mode and kernel mode

User mode and kernel mode

2022-06-23 07:16:00 summer_ west_ fish

One 、C P U Instruction set permissions

Say User mode and kernel mode Before , It's necessary to say CPU Instruction set , The instruction set is CPU The medium of software directing hardware execution , Specifically, each assembly statement corresponds to a CPU Instructions , And a lot of CPU Instructions together , Can form a 、 Even multiple collections , The set of instructions is called CPU Instruction set .

meanwhile CPU Instruction set There is permission to grade , Let's imagine ,CPU Instruction set Can operate the hardware directly , If it's because of instructions No specification `, The mistakes that result will affect the whole Computer system Of . It's like you write a program , Because of Unfamiliar with hardware operation , Causes the operating system kernel to 、 And all the other running programs , It could be because Operational errors And suffer irreparable mistakes , Finally, you have to restart the computer .

And for The operation of hardware It's very complicated , Many parameters , Something is wrong The odds are pretty good , You have to be careful , For developers, it's a Arduous task , It will also increase the burden , At the same time, developers are also Not to be trusted , Therefore, the operating system kernel directly blocks the possibility of developers' hardware operation , I won't let you touch these CPU Instruction set .

For the above needs , Hardware vendors provide hardware level support directly , The way to do it is to  CPU Instruction set Set permissions , Different levels of permissions can be used  CPU Instruction set   It is limited. , With  Intel CPU  For example ,Inter hold  CPU Instruction set   The operation permission is divided from high to low as 4 level : 

  among ring 0 The highest authority , You can use all CPU Instruction set ,ring 3 Minimum authority , Only use regular CPU Instruction set , Out of commission Operating hardware resources CPU Instruction set , such as I O Reading and writing 、 Network card access 、 I can't even apply for memory ,Linux The system only uses ring 0 and ring 3 this 2 Permissions .

Two 、 User mode and kernel mode

It's customs clearance CPU Instruction set permissions , Now let's talk about it User mode and kernel mode It's very simple , The concept of user state and kernel state is CPU Instruction set permissions The difference between , Read and write in the process IO, It must be used ring 0 Grade CPU Instruction set , And then CPU The permission of instruction set operation of is only ring 3, In order to be operational ring 0 Grade CPU Instruction set , CPU The permission level of switching instruction set operation is ring 0,CPU And then perform the corresponding ring 0 Grade CPU Instruction set Kernel code ), Executive Kernel code Will use the kernel stack of the current process .

PS: Each process has two stacks , They are user stack and kernel stack , The use of user mode and kernel mode .

1、 The space between user state and kernel state  

Use of memory resources , Operating system pair User mode and kernel mode There are also restrictions , Each process creation is assigned 「 Virtual space address 」, With Linux32 For example, bit operating system , Its addressing space is 4G(2 Of 32 Power ), And the operating system will divide the virtual control address into two parts , Part of it is Kernel space , The other part is User space , High order 1G( From virtual address 0xC0000000 To 0xFFFFFFFF) Used by the kernel , And the low ones 3G( From virtual address 0x00000000 To 0xBFFFFFFF) Used by each process .

 

  • User mode : You can only operate 0-3G The low virtual space address of the range
  • Kernel mode :0-4G The virtual space address of the scope can be operated , Especially for 3-4G The high bit virtual space address of the range must be operated by the kernel state
  • Add :3G-4G Some of you are sharing ( The kernel state logical address of all processes is the shared memory address ), Is the address space of kernel state , The code stored in the whole kernel and all kernel modules , And the data maintained by the kernel .

Of each process  4G  Virtual space address , High position  1G  It's all the same , That's kernel space . Only the rest  3G  For the process to use , In other words , High position  1G  The kernel space of is shared by all processes !

Finally, make a summary , We distinguish between user mode and kernel mode through instruction set permissions , It also limits the use of memory resources , The operating system divides two pieces of memory space for user mode and kernel mode , Use... For their corresponding instruction set

2、 Switch between user mode and kernel mode  

I believe everyone has heard such words 「 Switching between user mode and kernel mode costs a lot 」, But it costs a lot of money ? To put it simply, there are the following points

  • Keep user mode on site ( Context 、 register 、 User stack etc. )
  • Copy user state parameters , Switch user stack to kernel stack , Go into kernel state
  • Additional checks ( Because kernel code doesn't trust users )
  • Execute kernel state code
  • Copy kernel state code execution results , Go back to user mode
  • Restore user mode ( Context 、 register 、 User stack, etc )

In fact, the operating system will be more complex than the above , It's just a rough idea , We can see that a switching experience 「 User mode -> Kernel mode -> User mode 」.

The user mode should be actively switched to the kernel mode , There has to be an entrance , In fact, kernel state provides a unified entrance , Here is Linux Overall architecture :

From the picture above, we can see that through system call take Linux The whole system is divided into user mode and kernel mode , In order for the application to access the resources of the kernel , Such as CPU、 Memory 、I/O, The kernel must provide a common set of access interfaces , These interfaces are called system call .

Library function It's to shield these complex underlying implementation details , Lighten the burden of programmers , So we pay more attention to the logic realization of the upper layer , It encapsulates system calls , Provide a simple basic interface for programmers .

Shell seeing the name of a thing one thinks of its function , It means shell , It's like a shell wrapped in a kernel , It's a special application , Commonly known as the command line .Shell It's also programmable , It has a standard Shell grammar , The text that conforms to its grammar is called Shell Script , A lot of people use Shell Script to achieve some common functions , Can improve work efficiency .

Finally, let's talk about , What will cause the user mode to switch to kernel mode

  • system call : User mode process actively switches to kernel mode , User mode processes apply for resources from the operating system through system calls to complete the work , for example : fork() It's a system call to create a new process , The core of the system call mechanism uses an interrupt that the operating system is especially open to users , Such as Linux Of int 80h interrupt , It can also be called soft interrupt

  • abnormal : When CPU When executing a user mode process , Something unexpected happened , At this time, the current running process will switch to the kernel related process that handles this exception , That is to say, it switches to kernel state , If missing page is abnormal

  • interrupt : When CPU When executing a user mode process , After the peripheral device completes the operation requested by the user , Will send to CPU Send out corresponding interrupt signal , At this time CPU Will pause the next instruction to be executed , Go to the handler corresponding to the interrupt signal to execute , That is to say, it switches to kernel state . Such as : Hard disk read / write operation completed , The system will switch to the interrupt processing program of hard disk reading and writing to perform the following operations .

 

原网站

版权声明
本文为[summer_ west_ fish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230623087878.html