内核如何知道CPU处于用户模式还是内核模式?
由于CPU运行在用户/内核模式下,我想知道这是如何由内核决定的。我的意思是,如果调用 sys 调用,内核会代表进程执行它,但是内核如何知道它正在内核模式下执行?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
由于CPU运行在用户/内核模式下,我想知道这是如何由内核决定的。我的意思是,如果调用 sys 调用,内核会代表进程执行它,但是内核如何知道它正在内核模式下执行?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
您可以通过代码段寄存器 (CS) 中设置的权限级别来判断您是处于用户模式还是内核模式。从 RIP 或 EIP 寄存器(指令指针寄存器分别取决于您是 x86_64 还是 x86)指向的内存加载到 CPU 的每条指令都将从当前代码的全局描述符表 (GDT) 中描述的段中读取- 段描述符。代码段描述符的低两位将确定代码正在执行的当前特权级别。当进行系统调用时(通常通过软件中断完成),CPU 将检查当前特权级别,如果处于用户模式,则将当前代码段描述符交换为内核级描述符,具体如下所示:系统调用的软件中断门描述符,以及进行堆栈切换并将当前标志、用户级 CS 值和 RIP 值保存在这个新的内核级堆栈上。当系统调用完成时,用户模式 CS 值、标志和指令指针(EIP 或 RIP)值将从内核堆栈中恢复,并且堆栈切换回当前执行进程的堆栈。
You can tell if you're in user-mode or kernel-mode from the privilege level set in the code-segment register (CS). Every instruction loaded into the CPU from the memory pointed to by the RIP or EIP register (the instruction pointer register depending on if you are x86_64 or x86 respectively) will read from the segment described in the global descriptor table (GDT) by the current code-segment descriptor. The lower two-bits of the code segment descriptor will determine the current privilege level that the code is executing at. When a syscall is made, which is typically done through a software interrupt, the CPU will check the current privilege-level, and if it's in user-mode, will exchange the current code-segment descriptor for a kernel-level one as determined by the syscall's software interrupt gate descriptor, as well as make a stack-switch and save the current flags, the user-level CS value and RIP value on this new kernel-level stack. When the syscall is complete, the user-mode CS value, flags, and instruction pointer (EIP or RIP) value are restored from the kernel-stack, and a stack-switch is made back to the current executing processes' stack.
一般来说,如果它正在运行内核代码,那么它就处于内核模式。从用户空间到内核模式的转换(例如系统调用)会导致发生上下文切换。作为上下文切换的一部分,CPU 模式会发生变化。
Broadly if it's running kernel code it's in kernel mode. The transition from user-space to kernel mode (say for a system call) causes a context switch to occur. As part of this context switch the CPU mode is changed.
内核代码仅在内核模式下执行。没办法,内核代码可以在用户态执行。当应用程序调用系统调用时,会产生陷阱(软件中断),并且模式会切换到内核模式,并执行系统调用的内核实现。一旦完成,内核将切换回用户模式,用户应用程序将继续在用户模式下处理。
Kernel code only executes in kernel mode. There is no way, kernel code can execute in user mode. When application calls system call, it will generate a trap (software interrupt) and the mode will be switch to kernel mode and kernel implementation of system call will executed. Once it is done, kernel will switch back to user mode and user application will continue processing in user mode.
该术语称为“Supervisor Mode”,它也适用于 x86/ARM 和许多其他处理器。
阅读此内容(仅适用于 x86 CPU):
http://en.wikipedia.org/ wiki/Ring_(computer_security)
Ring 0 到 3 是 x86 CPU 的不同权限级别。通常只使用 Ring0 和 3(内核和用户),但现在 Ring 1 找到了用途(例如,VMWare 使用它来模拟 guest 虚拟机对 Ring 0 的执行)。只有Ring 0有完全的权限来运行一些特权指令(如lgdt或lidt),因此在汇编级别的一个好的测试当然是执行这些指令,看看你的程序是否遇到任何异常。
阅读本文以真正确定您当前的权限级别(查找 CPL,这是 Jason 答案的图示):
http://duartes.org/gustavo/blog/post/cpu-rings-privilege-and-protection
The term is called "Superviser Mode", which applies to x86/ARM and many other processor as well.
Read this (which applies only to x86 CPU):
http://en.wikipedia.org/wiki/Ring_(computer_security)
Ring 0 to 3 are the different privileges level of x86 CPU. Normally only Ring0 and 3 are used (kernel and user), but nowadays Ring 1 find usages (eg, VMWare used it to emulate guest's execution of ring 0). Only Ring 0 has the full privilege to run some privileged instructions (like lgdt, or lidt), and so a good test at the assembly level is of course to execute these instruction, and see if your program encounters any exception or not.
Read this to really identify your current privilege level (look for CPL, which is a pictorialization of Jason's answer):
http://duartes.org/gustavo/blog/post/cpu-rings-privilege-and-protection
这是一个简单的问题,不需要上面提供的任何专家评论。
问题是CPU如何知道它是内核模式还是用户模式。
答案是“模式位”……
它是CPU寄存器组状态寄存器中的一个位。
当“模式位=0”时,,,它被认为是内核模式(也称为,监视模式,特权模式,保护模式......以及许多其他......)
当“模式位=1”时,它被认为是用户模式......并且用户现在可以执行其个人应用程序而无需任何特殊的内核中断。
这么简单……不是吗?
It is a simple question and does not need any expert comment as provided above..
The question is how does a cpu come to know whether it is kernel mode or its a user mode.
The answer is "mode bit"....
It is a bit in Status register of cpu's registers set.
When "mode bit=0",,,it is considered as kernel mode(also called,monitor mode,privileged mode,protected mode...and many other...)
When "mode bit=1",,it is considered as User mode...and user can now perform its personal applications without any special kernel interruption.
so simple...isn't it??