how to understand the function of “__swtich_ to” for contex-switch in the ARM linux

僤鯓⒐⒋嵵緔 提交于 2019-12-23 04:55:28

问题


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.

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

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).

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.


回答1:


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().

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.

* Assuming there is one, i.e. the calling context isn't a kernel thread.



来源:https://stackoverflow.com/questions/39140898/how-to-understand-the-function-of-swtich-to-for-contex-switch-in-the-arm-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!