从用户空间切换到内核空间时如何使用 4gb(VM) 地址空间
我查看了很多关于如何将进程地址空间划分为进程/内核的在线线程/教程
: 我有一些 Helloworld 程序 因为我有调用 printf(反过来它使 write 系统调用进入内核空间)
我怀疑内核如何使用 Helloworld 程序堆栈。 你能告诉我整个执行过程是如何进行的吗...
./helloworld -> printf() ->;编写系统调用->显示驱动程序->从写入返回 ->回到你好世界
谢谢, 阿玛伦德
I looked at a lot if online thread/tutorials regarding how process address space is divided into process/kernel
Ex:
i have some Helloworld program
in that i have call as printf(in turn it makes write system call to enter into kernel space)
My doubt how Helloworld program stack used by kernel.
Can you tell me how whole execution goes on...
./helloworld -> printf() -> write system call -> display driver -> return from write -> back to helloworld
Thanks,
Amarender
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题的详细答案取决于具体的内核和架构。然而,一般的答案是,当用户空间想要调用内核时,它会执行一条陷阱指令,这会导致 CPU 更改特权级别并开始执行内核代码。作为特权级别更改的一部分,CPU 还将切换到内核堆栈。当内核完成后,它将执行一个从陷阱返回序列,该序列恢复用户空间堆栈并从中断处恢复执行。
The detailed answer to this question depends on the specific kernel and architecture. However, the general answer is that when userspace wants to call into the kernel, it executes a trap instruction, that causes the CPU to change privilege level and start executing kernel code. As part of the privilege level change, the CPU will also switch to a kernel stack. When the kernel is done, it will execute a return-from-trap sequence that restores the userspace stack and resumes execution where it left off.
简而言之:当进行 write 系统调用时,会生成
int $80
陷阱。处理程序将当前进程寄存器保存在内核堆栈上(存在于内核地址空间中)。然后更改段寄存器中的 CPL 以启用内核页表。然后内核查找系统调用表并找到所需例程的适当地址。然后执行跳转到例程,该例程又可以调用设备驱动程序代码。完成工作后,内核通过恢复段寄存器中的寄存器内容和 CPL 返回到用户模式。
In a nutshell: When the write system call is made,
int $80
trap is generated. The handler saves the current process registers on the Kernel stack (present in the kernel address space). Then CPL in segment registers are changed to enable the use of kernel page tables. Then the kernel looks up its table of system calls and finds the appropriate address of the desired routine. The execution then jumps to the routine which in turn may call the device driver code.After doing its work, the kernel returns to user mode by restoring the register content and CPL in the segment registers.