当前位置:网站首页>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-3GThe low virtual space address of the range - Kernel mode :
0-4GThe virtual space address of the scope can be operated , Especially for3-4GThe high bit virtual space address of the range must be operated by the kernel state - Add :
3G-4GSome 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 .
边栏推荐
猜你喜欢

Deep learning series 47: Super sub model real esrgan

20220621 Three Conjugates of Dual Quaternions

TensorFlow中的数据类型

Endnote20 tutorial sharing (unfinished

Eureka

excel高级绘图技巧100讲(八)-Excel绘制WIFI图

U-Net: Convolutional Networks for Biomedical Image Segmentation

deeplab v3 代码结构图

启发式的搜索策略

407-栈与队列(232.用栈实现队列、225. 用队列实现栈)
随机推荐
Mysql数据库的几个特点
Endnote20 tutorial sharing (unfinished
How to achieve efficient network information dissemination
312. 戳气球
技术文章写作指南
Regular expression graph and text ultra detailed summary without rote memorization (Part 1)
How to verify date format in PHP (regular)
896. 单调数列
如何达到高效的网络信息传播
MySQL总结
295. 数据流的中位数
900. RLE 迭代器
900. RLE iterator
pspnet完整代码实现
Verilog syntax explanation
都是硬盘分区 C盘和D盘到底有什么区别?
318. maximum word length product
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
启发式的搜索策略
Quartz调度框架的学习使用