lunix lab5:深入理解进程切换
两步走进程环境的切换:
1、进程调度算法从就绪队列中选择一个进程(pick_next_task)
2、切换进程上下文(context_switch)
进程共享cpu和寄存器,所以进程切换时要保存对应的寄存器值属于cpu上下文:
1、用户地址空间:程序代码、数据和用户堆栈等
2、控制信息:进程描述符(存在内核堆栈的低地址,方便通过栈顶指针取)、内核堆栈(内核栈栈顶寄存器sp)等
3、进程的cpu上下文:相关寄存器的值(指令指针寄存器:下一条要执行的指令地址)。
内核把被切换出去的进程的cpu上下文保存在结构体变量thread_struct和内核堆栈中
进程切换也需要两步:
1、切换全局目录:保证不同进程的虚拟地址转换为不同物理地址
2、切换内核堆栈和进程的cpu上下文
Linux 内核中的上下文切换函数 是content_switch:
在这里插入代码片
```static __always_inline struct rq *
context_switch(struct rq *rq, struct task_struct *prev,
struct task_struct *next, struct rq_flags *rf)
{
prepare_task_switch(rq, prev, next);
/*
* For paravirt, this is coupled with an exit in switch_to to
* combine the page table reload and the switch backend into
* one hypercall.
*/
arch_start_context_switch(prev);
/*
* kernel -> kernel lazy + transfer active
* user -> kernel lazy + mmgrab() active
*
* kernel -> user switch + mmdrop() active
* user -> user switch
*/
if (!next->mm) { // to kernel
enter_lazy_tlb(prev->active_mm, next);
next->active_mm = prev->active_mm;
if (prev->mm) // from user
mmgrab(prev->active_mm);
else
prev->active_mm = NULL;
} else { // to user
membarrier_switch_mm(rq, prev->active_mm, next->mm);
/*
* sys_membarrier() requires an smp_mb() between setting
* rq->curr / membarrier_switch_mm() and returning to userspace.
*
* The below provides this either through switch_mm(), or in
* case 'prev->active_mm == next->mm' through
* finish_task_switch()'s mmdrop().
*/
switch_mm_irqs_off(prev->active_mm, next->mm, next);
if (!prev->mm) { // from kernel
/* will mmdrop() in finish_task_switch(). */
rq->prev_mm = prev->active_mm;
prev->active_mm = NULL;
}
}
rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
prepare_lock_switch(rq, next, rf);
/* Here we just switch the register state and the stack. */
switch_to(prev, next, prev);
barrier();
return finish_task_switch(prev);
}
这段代码是一个用于进程切换的函数,它接收三个参数:当前的运行队列rq,当前正在运行的进程prev以及要被调度的下一个进程next。该函数将当前正在运行的进程prev切换为被调度的下一个进程next。
content_switch 函数的下一个重要函数就是 switch_to,它切换寄存器状态和栈。swtich_to 函数会进一步调用 __switch_to_asm,而 __switch_to_asm 的实现是和体系结构强相关的。 x86_64 的中__switch_to_asm的实现如下:
在这里插入代码片
UNWIND_HINT_FUNC
/*
* Save callee-saved registers
* This must match the order in inactive_task_frame
*/
pushq %rbp
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
/* switch stack */
movq %rsp, TASK_threadsp(%rdi) // 保存旧进程的栈顶
movq TASK_threadsp(%rsi), %rsp // 恢复新进程的栈顶
/* restore callee-saved registers */
popq %r15
popq %r14
popq %r13
popq %r12
popq %rbx
popq %rbp
jmp __switch_to
END(__switch_to_asm)
两条 movq 语句切换了新旧进程,随着内核栈顶的切换,内核栈空间也就切换到了新进程,之后只需要弹出栈中保存的各个寄存器的值即可恢复寄存器状态。
通过call指令和ret指令的配对使用,函数在__switch_to_asm和switch_to中进行调用和返回。call指令压栈RIP寄存器到进程切换前的prev进程内核堆栈,而ret指令出栈存入RIP寄存器的是进程切换之后的next进程的内核堆栈栈顶数据,所以 ret 恢复的就是 next 进程内核堆栈中的 rip 值,实现了 rip 值的保存与修改。
再看一下arm64架构下__switch_to_asm()的实现 ,调用过程:switch_to -> switch_to -> cpu_switch_to,具体的切换发生在cpu_switch_to:
mov x10, #THREAD_CPU_CONTEXT
add x8, x0, x10
mov x9, sp
stp x19, x20, [x8], #16
stp x21, x22, [x8], #16
stp x23, x24, [x8], #16
stp x25, x26, [x8], #16
stp x27, x28, [x8], #16
stp x29, x9, [x8], #16 //x29是frame pointer,x9是stack pointer,lr是pc值
str lr, [x8]
add x8, x1, x10
ldp x19, x20, [x8], #16
ldp x21, x22, [x8], #16
ldp x23, x24, [x8], #16
ldp x25, x26, [x8], #16
ldp x27, x28, [x8], #16
ldp x29, x9, [x8], #16
ldr lr, [x8]
mov sp, x9
msr sp_el0, x1
ret
ENDPROC(cpu_switch_to)
NOKPROBE(cpu_switch_to)