public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Re: why don't setjmp save MXCSR register and x87 control word?
       [not found] <CAF6YOcOnWpqxsp+tbu-29HHaw7+sj=4jR-ZPcmEq=po6F5u-yg@mail.gmail.com>
@ 2021-03-17 18:59 ` Adhemerval Zanella
  2021-03-17 21:08   ` Joseph Myers
  0 siblings, 1 reply; 2+ messages in thread
From: Adhemerval Zanella @ 2021-03-17 18:59 UTC (permalink / raw)
  To: Fengkai Sun, GNU C Library



On 16/03/2021 11:39, Fengkai Sun via Libc-help wrote:
> Hi list,
> 
> To the best of my knowledge, setjmp have to save all the callee-saved
> registers.
> 
> According to Sys V ABI:
>>  The control bits of the MXCSR register are callee-saved (preserved
> across calls), while the status bits are caller-saved (not preserved).
>>  The x87 status word register is caller-saved, whereas the x87 control
> word is callee-saved.
> 
> But in sysdeps/x86_64/setjmp.S, __sigsetjmp only saves rbx, rbp, r12-r15,
> rsp.
> 
> I also found an example in FreeBSD, which makes more sense to me:
> https://svnweb.freebsd.org/base/head/lib/libc/amd64/gen/_setjmp.S?view=markup
> 
> I don't know if there will be any problem if MXCSR and x87 control word are
> not saved and get clobbered .

Because afaik the C standard specify that any state of floating-point status 
flag should *not* be saved:

  7.13 Nonlocal jumps <setjmp.h>
  [...]
  It does not include the state of the floating-point status flags, of open files, 
  or of any other component of the abstract machine.

And the FreeBSD implementation seems to deviate from standard deliberately,
the commit 64c2e4665060b5f4 states:

  Note that standards don't require longjmp to restore either control
  word, and none of Linux, MacOS X 10.3 and earlier, NetBSD, OpenBSD,
  or Solaris do it. However, it is historical FreeBSD behavior, and
  bde points out that it is needed to make longjmping out of a signal
  handler work properly, given the way FreeBSD clobbers the FPU state
  on signal handler entry.

And I am not sure, but skimming through kernel sources Linux does not
clobber the FPU state on signal handler entry.



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

* Re: why don't setjmp save MXCSR register and x87 control word?
  2021-03-17 18:59 ` why don't setjmp save MXCSR register and x87 control word? Adhemerval Zanella
@ 2021-03-17 21:08   ` Joseph Myers
  0 siblings, 0 replies; 2+ messages in thread
From: Joseph Myers @ 2021-03-17 21:08 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: Fengkai Sun, GNU C Library

On Wed, 17 Mar 2021, Adhemerval Zanella via Libc-alpha wrote:

> Because afaik the C standard specify that any state of floating-point status 
> flag should *not* be saved:
> 
>   7.13 Nonlocal jumps <setjmp.h>
>   [...]
>   It does not include the state of the floating-point status flags, of open files, 
>   or of any other component of the abstract machine.

Indeed, the floating-point exception flags, rounding mode and any other 
such state are essentially thread-local variables, and should be handled 
just like such variables.  setjmp and longjmp don't save and restore 
thread-local or global variables, so they shouldn't save and restore 
floating-point flags or control modes either.

(In psABI terms, we invented the term "limited-access bits" to describe 
this state in the 32-bit Power Architecture ABI.)

-- 
Joseph S. Myers
joseph@codesourcery.com

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

end of thread, other threads:[~2021-03-17 21:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAF6YOcOnWpqxsp+tbu-29HHaw7+sj=4jR-ZPcmEq=po6F5u-yg@mail.gmail.com>
2021-03-17 18:59 ` why don't setjmp save MXCSR register and x87 control word? Adhemerval Zanella
2021-03-17 21:08   ` Joseph Myers

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