Linux内核switch_to宏实现进程切换的原理

本文探讨了Linux内核中switch_to宏如何执行进程切换。通过将当前进程堆栈信息保存到task_struct,然后设置新进程的栈顶指针实现堆栈切换。接着,通过将next进程的ip压栈并跳转到__switch_to函数,当函数返回时,CPU将执行新进程的指令流,完成进程切换。在内核态切换中,仅保存和恢复特定寄存器,而在用户态到内核态切换时,通用寄存器的保存是为了确保上下文的一致性。
摘要由CSDN通过智能技术生成

switch_to宏的实现:

27 /*
 28  * Saving eflags is important. It switches not only IOPL between tasks,
 29  * it also protects other tasks from NT leaking through sysenter etc.
 30  */
 31 #define switch_to(prev, next, last)                                     \
 32 do {
                                                                       \
 33         /*                                                              \
 34          * Context-switching clobbers all registers, so we clobber      \
 35          * them explicitly, via unused output variables.                \
 36          * (EAX and EBP is not listed because EBP is saved/restored     \
 37          * explicitly for wchan access and EAX is the return value of   \
 38          * __switch_to())                                               \
 39          */                                                             \
 40         unsigned long ebx, ecx, edx, esi, edi;                          \
 41                                                                         \
 42         asm volatile("pushfl\n\t"               /* save    flags */     \
 43                      "pushl 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值