public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Questions and possible optimizations about sysv/linux/[x86_64|i386]/swapcontext.S
@ 2018-05-20 13:15 Remus Clearwater
  2018-05-20 13:36 ` Remus Clearwater
  0 siblings, 1 reply; 2+ messages in thread
From: Remus Clearwater @ 2018-05-20 13:15 UTC (permalink / raw)
  To: libc-help

Hi, I'm reading the sources of ucontext
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/swapcontext.S;h=1110c479fad27829f1b1e480cf7520cf77d795f7;hb=HEAD#l46>
.

According to https://en.wikipedia.org/wiki/X86_calling_conventions#cdecl:

Integer values and memory addresses are returned in the EAX register
> <https://en.wikipedia.org/wiki/Processor_register>, floating point values
> in the ST0 x87 <https://en.wikipedia.org/wiki/X87> register. Registers
> EAX, ECX, and EDX are caller-saved, and the rest are callee-saved. The x87
> <https://en.wikipedia.org/wiki/X87> floating point registers ST0 to ST7
> must be empty (popped or freed) when calling a new function, and ST1 to ST7
> must be empty on exiting a function. ST0 must also be empty when not used
> for returning a value.


And https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI
:

If the callee wishes to use registers RBX, RBP, and R12–R15, it must
> restore their original values before returning control to the caller. All
> other registers must be saved by the caller if it wishes to preserve their
> values.


Questions:

1. Since `swapcontext` is a function call, so it maybe not need to
save/restore
(fnstenv,stmxcsr/fldenv,ldmxcsr) the floating-point context. Is this
correct?
If it's wrong, where I could found the detailed calling convention about the
floating-point context ? Or a bug example after the (fnstenv,stmxcsr/fldenv,
ldmxcsr)
part is eliminated?

2. Could the save/restore (fnstenv,stmxcsr/fldenv,ldmxcsr) be eliminated if
all
the functions which uses fpu are leaf-node function (function which do not
call
any other function) ? If it is true, where I could found the detailed
reference?

3. Since `swapcontext` is a function call, so it is only necessary to
save/restore
RBX, RBP, R12–R15, RSP and RIP, the save/restore of RDI, RSI, RDX, RCX, R8
and R8 is quite useless and can be eliminated which could save at least
(6*8*2 = 96)
times memory read/writes in each swapcontext operation (Same optimization
also
exists in i386/swapcontext.S
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/setcontext.S;h=a604fca0aceaa17f19f536bac46a1fc305f30860;hb=HEAD#l77>).
Is this correct?

4. In i386/swapcontext.S
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/setcontext.S;h=a604fca0aceaa17f19f536bac46a1fc305f30860;hb=HEAD#l54>
:

54
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/setcontext.S;h=a604fca0aceaa17f19f536bac46a1fc305f30860;hb=HEAD#l54>
        /* Restore the FS segment register.  We don't touch the GS register
55
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/setcontext.S;h=a604fca0aceaa17f19f536bac46a1fc305f30860;hb=HEAD#l55>
           since it is used for threads.  */
56
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/setcontext.S;h=a604fca0aceaa17f19f536bac46a1fc305f30860;hb=HEAD#l56>
        movl    oFS(%eax), %ecx
57
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/setcontext.S;h=a604fca0aceaa17f19f536bac46a1fc305f30860;hb=HEAD#l57>
        movw    %cx, %fs

But there is no such save/restore about register fs in x86_64/swapcontext.S
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/x86_64/swapcontext.S;h=1110c479fad27829f1b1e480cf7520cf77d795f7;hb=HEAD#l46>
.
Is this a bug or it's no need to save/restore the register fs at all ? (The
save/restore of register fs in i386/swapcontext.S
<https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/setcontext.S;h=a604fca0aceaa17f19f536bac46a1fc305f30860;hb=HEAD#l54>
could
also be elimianted
if the later is correct)

Thanks a lot :)

Remus

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

end of thread, other threads:[~2018-05-20 13:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-20 13:15 Questions and possible optimizations about sysv/linux/[x86_64|i386]/swapcontext.S Remus Clearwater
2018-05-20 13:36 ` Remus Clearwater

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