"Switching from user mode to kernel mode" 是一个错误的概念
"Switching from user mode to kernel mode" is an incorrect concept
第一次学习"Operating System"。在我的书中,我发现了关于 "User Mode" 和 "Kernel Mode" 的这句话:
"Switch from user to kernel mode" instruction is executed only in kernel
mode
我认为这是一个不正确的句子,因为实际上没有 "switch of kernel"。事实上,当用户进程需要执行特权指令时,它只是要求内核为自己做一些事情。正确吗?
In fact, when a user process need to do a privileged instruction it simply ask the kernel to do something for itself.
但是这是怎么发生的呢?细节是处理器(即instruction set architecture) and OS specific (explained in ABI specifications relevant to your system, e.g. here), but that usually involves some machine code instruction like SYSENTER
or SYSCALL
(or SVC
on mainframes) capable of atomically changing the CPU mode(即以受控方式将其切换到内核模式)。系统调用的实际参数(甚至包括系统调用号)通常是在寄存器中传递(但细节是特定于 ABI 的)。
所以我觉得从用户模式切换到内核模式的概念是相关的,并且有意义(所以 "correct")。
顺便说一句,(硬件)禁止用户模式代码执行特权机器指令,例如与 IO 硬件设备交互的指令(阅读 protection rings). If you try, you get some hardware exception (a bit similar to interrupts)。因此你的代码(即使它是恶意的)必须进行系统调用,内核控制(它有很多与权限检查相关的代码),例如所有 IO.
另请阅读Operating Systems: Three Easy Pieces - freely downloadable. See also http://osdev.org/. Read system call wikipage & syscalls(2), and the Assembler HowTo。
在现实生活中,事情要复杂得多。了解 System Management Mode and about the (scary) Intel Management Engine。
第一次学习"Operating System"。在我的书中,我发现了关于 "User Mode" 和 "Kernel Mode" 的这句话:
"Switch from user to kernel mode" instruction is executed only in kernel mode
我认为这是一个不正确的句子,因为实际上没有 "switch of kernel"。事实上,当用户进程需要执行特权指令时,它只是要求内核为自己做一些事情。正确吗?
In fact, when a user process need to do a privileged instruction it simply ask the kernel to do something for itself.
但是这是怎么发生的呢?细节是处理器(即instruction set architecture) and OS specific (explained in ABI specifications relevant to your system, e.g. here), but that usually involves some machine code instruction like SYSENTER
or SYSCALL
(or SVC
on mainframes) capable of atomically changing the CPU mode(即以受控方式将其切换到内核模式)。系统调用的实际参数(甚至包括系统调用号)通常是在寄存器中传递(但细节是特定于 ABI 的)。
所以我觉得从用户模式切换到内核模式的概念是相关的,并且有意义(所以 "correct")。
顺便说一句,(硬件)禁止用户模式代码执行特权机器指令,例如与 IO 硬件设备交互的指令(阅读 protection rings). If you try, you get some hardware exception (a bit similar to interrupts)。因此你的代码(即使它是恶意的)必须进行系统调用,内核控制(它有很多与权限检查相关的代码),例如所有 IO.
另请阅读Operating Systems: Three Easy Pieces - freely downloadable. See also http://osdev.org/. Read system call wikipage & syscalls(2), and the Assembler HowTo。
在现实生活中,事情要复杂得多。了解 System Management Mode and about the (scary) Intel Management Engine。