caller-saved registers and callee-saved registers
文章目录
The introduction to caller-saved registers and callee-saved registers.
1. Prerequisite
2. Definition
2.1 Caller-saved registers
Caller-saved registers (AKA volatile registers, or call-clobbered) are used to hold temporary quantities that need not be preserved across calls.
For that reason, it is the caller’s responsibility to push these registers onto the stack or copy them somewhere else if it wants to restore this value after a procedure call.
It’s normal to let a call
destroy temporary values in these registers, though.
2.2 Callee-saved registers
Callee-saved registers (AKA non-volatile registers, or call-preserved) are used to hold long-lived values that should be preserved across calls.
When the caller makes a procedure call, it can expect that those registers will hold the same value after the callee returns, making it the responsibility of the callee to save them and restore them before returning to the caller. Or to not touch them.
3. Linux x86-64 function call
3.1 Callee-saved registers
r12
, r13
, r14
, r15
, rbx
, rsp
, rbp
are the callee-saved registers - they have a “Yes” in the “Preserved across function calls” column.
3.2 Caller-saved registers
4. Example
4.1 setjmp
https://gitlab.com/kvm-unit-tests/kvm-unit-tests/-/blob/master/lib/x86/setjmp64.S
1 | .globl setjmp |
4.2 arch_switch_to
https://github.com/projectacrn/acrn-hypervisor/blob/release_2.7/hypervisor/arch/x86/sched.S
1 | .text |
参考资料: