问题描述
我想了解基于 ARM 的 linux 上下文切换是如何工作的.
I am trying to understand how the context switch of linux works which is based on the ARM.
所以我想了解以下代码.
So i want to understand following codes.
进入(__switch_to)
ENTRY(__switch_to)
add ip, r1, #TI_CPU_SAVE
ldr r3, [r2, #TI_TP_VALUE]
stmia ip!, {r4 - sl, fp, sp, lr} @ Store most regs on stack
ldr r6, [r2, #TI_CPU_DOMAIN]
strex r5, r4, [ip] @ Clear exclusive monitor
mcr p15, 0, r3, c13, c0, 3 @ set TLS register
mov r4, #0xffff0fff
str r3, [r4, #-15] @ TLS val at 0xffff0ff0
mcr p15, 0, r6, c3, c0, 0 @ Set domain register
mov r5, r0
add r4, r2, #TI_CPU_SAVE
ldr r0, =thread_notify_head
mov r1, #THREAD_NOTIFY_SWITCH
bl atomic_notifier_call_chain
mov r0, r5
ldmia r4, {r4 - sl, fp, sp, pc} @ Load all regs saved previously
我知道这些代码用于存储当前进程的 cpu-context 并为将是当前进程的下一个进程恢复 cpu-context.但它不保存和恢复关于 ip, r1, r2, r3, r4,尤其是关于 cpsr(Current Program Status Register).
I understand that these codes are used for storing cpu-context for current process and restoring cpu-context for next process which will be current process. But it doesn’t save and restore about ip, r1, r2, r3, r4, especially about cpsr(Current Program Status Register).
我认为它应该保存和恢复上下文切换的cpsr寄存器.但是上面的代码中并没有保存cpsr.我不明白这一点.我在这个问题上挣扎了一个星期.但我找不到答案.如果有人能给我答案,我将不胜感激.
I think that it should save and restore the cpsr register for Context Switch. But it doesn’t save the cpsr in the above code. I don’t understand this. I am struggling with this question for a week. But I could not find the answer. It would be very grateful for somebody to give me the answer.
推荐答案
When context_switch()
调用 switch_to()
,它只是一个普通的函数调用.ABI 不需要 r0-r3、r12 或 CPSR 中的条件标志在函数调用时保留,因此它们不需要保存在调用任务的上下文中,因为它不会关心它们是什么是当它最终被重新安排并在从 switch_to()
返回时再次启动.
When context_switch()
calls switch_to()
, it's just a regular function call. The ABI doesn't require r0-r3, r12 or the condition flags in CPSR to be preserved over a function call, thus they don't need to be saved in the context of the calling task, because it won't care what they are when it eventually gets rescheduled and picks up again upon returning from switch_to()
.
重点是,thread_info
中的 cpu_context
,也就是在这里切换的内容,是最终调用 kernel 的状态代码>__schedule().实际(用户空间)进程状态,即r0-r15、SPSR等在任务的pt_regs
中——该状态被立即保存在进入内核时(参见例如 vector_swi
)并在退出时以您期望的方式恢复(ret_to_user
).
Point is, the cpu_context
in thread_info
, which is what's being switched here, is the kernel state of whatever ended up calling into __schedule()
. The actual (userspace) process state, i.e. r0-r15, SPSR, etc. is in the task's pt_regs
- that state is saved immediately upon entry to the kernel (see e.g. vector_swi
) and restored upon exit (ret_to_user
) in the manner you would expect.
这篇关于如何理解“__swtich_to"的功能用于 ARM linux 中的上下文切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!