[笔记] kernel 进程线程 - xieyunzi/xieyunzi.github.io GitHub Wiki

how to derive CPU registers information in each tasks in linux kernel?

ask_pt_regs(task) provides a pointer to the block of saved registers. They are always located at the top of the task's kernel stack area. The kernel stack area is essentially "empty" while the task is executing. When a system call is executed (or the kernel is entered for other reasons), the "system entry" code is executed (for x86 this is in arch/x86/kernel/entry_{32,64}.S). The pt_regs structure is constructed to match the order of register saves done in the entry code.

How to get saved registers of a process in Linux (ARM Architecture)

It depends on which set of registers you are interested in.

If you are interested in the user mode state, take a look on how ptrace does it. From a quick peek at the source code, task_pt_regs(task) is where you should look. Apparently, they are near the top of the kernel stack for the task (take a look at vector_swi for instance; it has a stmia sp, {r0 - r12} near its beginning, followed by a store of sp and lr).

If you are interested in the kernel mode state, it is saved by __switch_to into task->cpu_context (TI_CPU_SAVE is the offset of cpu_context within the struct thread_info). As another answer already noted, it doesn't save r0-r3 because it doesn't have to; the caller of switch_to assumes they will be clobbered by __switch_to, so their values don't matter.