Linux 内核--任务0的运行(切换到用户模式)mov

系统 1534 0

本文分析基于Linux 0.11内核,转载请标明出处, http://blog.csdn.net/yming0221/archive/2011/06/01/6459119.aspx

在main.c的程序中,当设备初始化完毕后,程序将从内核模式切换到用户模式,也就是所说的任务0,执行fork()函数(该函数使用内嵌的汇

编,防止函数调用弄乱堆栈,造成写时复制COPY_ON_WRITE)切换到用户堆栈并执行任务0使用iret指令,首先将有关的值压入堆栈,然后执

行iret指令。将刚刚压入对堆栈的数据根据标志位弹入相应的寄存器。

执行iret前的堆栈如下:

Linux 内核--任务0的运行(切换到用户模式)move_to_user_mode

一下这段话引用自 http://faydoc.tripod.com/cpu/iret.htm


In Protected Mode, the action of the IRET instruction depends on the settings of the NT (nested task) and VM flags in the EFLAGS register and the VM flag in the EFLAGS image stored on the current stack. Depending on the setting of these flags, the processor performs the following types of interrupt returns:

  • Return from virtual-8086 mode.
  • Return to virtual-8086 mode.
  • Intra-privilege level return.
  • Inter-privilege level return.
  • Return from nested task (task switch).

If the NT flag (EFLAGS register) is cleared, the IRET instruction performs a far return from the interrupt procedure, without a task switch. The code segment being returned to must be equally or less privileged than the interrupt handler routine (as indicated by the RPL field of the code segment selector popped from the stack). As with a real-address mode interrupt return, the IRET instruction pops the return instruction pointer, return code segment selector, and EFLAGS image from the stack to the EIP, CS, and EFLAGS registers, respectively, and then resumes execution of the interrupted program or procedure. If the return is to another privilege level, the IRET instruction also pops the stack pointer and SS from the stack, before resuming program execution. If the return is to virtual-8086 mode, the processor also pops the data segment registers from the stack.

-----------------------------------

由于在sched_init()中已经设置了标志寄存器中的vm标志为0,所以iret掉用后不会发生任务切换,而是继续执行EIP指向的指令故继续执行

1标号的代码,开始执行任务0,任务0的堆栈段选择符为0x17,在sched_init()中已设置了任务0 的任务描述符和局部描述符为INIT_TASK

Linux 内核--任务0的运行(切换到用户模式)move_to_user_mode


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论