curiosity question about swapcontext libc implementation on x86_64

Florian Weimer fw@deneb.enyo.de
Sun Feb 25 11:27:25 GMT 2024


* Godmar Back via Libc-help:

> The implementation of the obsolescent makecontext/getcontext/swapcontext
> family of functions on x86_64 [1] appears to save a selection of registers.
>
> These include callee-saved regs like rbx, rbp, r12-r15, but also
> caller-saved regs like rdi, rsi, rdx, rcx. On the floating point side, it
> saves mxcsr, the x87 FPU state via fnstenv, but not the xmm SSE register.
> All of these floating point registers are callee-saved based on the AMD64
> ABI [2].

%rdi, %rsi, %rdx, %rcx are argument registers and need to be restored
for makecontext to work correctly.  As far as I can see, getcontext
wouldn't need to save them, but they need to be restored.

The handling of what is now considered per-thread state (such as
floating point control words or signal masks) is inconsistent.  I
think this is just due to the age of these interfaces, and not
something that was designed explicitly.


More information about the Libc-help mailing list