// arch/arm64/include/asm/processor.h:113 struct cpu_context { unsigned long x19; unsigned long x20; unsigned long x21; unsigned long x22; unsigned long x23; unsigned long x24; unsigned long x25; unsigned long x26; unsigned long x27; unsigned long x28; // 对应 x29 寄存器 unsigned long fp; unsigned long sp; // 对应 lr 寄存器 unsigned long pc; }; 这些值刚好与上述汇编片段的代码一一对应上,读者应该不需要太多汇编基础就可以分析出来。
上述汇编中,最后一行 msr sp_el0, x1,x1 寄存器中保存了 next 的指针,这样后续再调用 current 宏的时候,就指向了下一个指针:
// arch/arm64/include/asm/current.h:15 static struct task_struct *get_current(void) { unsigned long sp_el0; asm ("mrs %0, sp_el0" : "=r" (sp_el0)); return (struct task_struct *)sp_el0; } // current 宏,很多地方会使用到 #define current get_current() 进程上下文切换的核心逻辑到这里就结束了,最后我们做下小结。