public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* curiosity question about swapcontext libc implementation on x86_64
@ 2024-02-01 15:23 Godmar Back
  2024-02-25 11:27 ` Florian Weimer
  0 siblings, 1 reply; 2+ messages in thread
From: Godmar Back @ 2024-02-01 15:23 UTC (permalink / raw)
  To: William Tambe via Libc-help

[-- Attachment #1: Type: text/plain, Size: 887 bytes --]

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

It also saves/restores the current signal mask.

Does anyone know why it saves this set of registers/signal mask? Why does
it save callee-saved registers, but not all of them?

 - Godmar


[1]
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/swapcontext.S;h=b2e0f19a9d57e5007b094037b4cf59868029fc36;hb=HEAD
[2]
https://gitlab.com/x86-psABIs/x86-64-ABI/-/jobs/artifacts/master/raw/x86-64-ABI/abi.pdf?job=build

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: curiosity question about swapcontext libc implementation on x86_64
  2024-02-01 15:23 curiosity question about swapcontext libc implementation on x86_64 Godmar Back
@ 2024-02-25 11:27 ` Florian Weimer
  0 siblings, 0 replies; 2+ messages in thread
From: Florian Weimer @ 2024-02-25 11:27 UTC (permalink / raw)
  To: Godmar Back via Libc-help; +Cc: Godmar Back

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-25 11:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-01 15:23 curiosity question about swapcontext libc implementation on x86_64 Godmar Back
2024-02-25 11:27 ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).