Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Jan 2002 11:55:27 +1100 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Nate Williams <nate@yogotech.com>
Cc:        Daniel Eischen <eischen@pcnet1.pcnet.com>, Dan Eischen <eischen@vigrid.com>, Peter Wemm <peter@wemm.org>, Archie Cobbs <archie@dellroad.org>, Alfred Perlstein <bright@mu.org>, <arch@FreeBSD.ORG>
Subject:   Re: Request for review: getcontext, setcontext, etc
Message-ID:  <20020108111521.D2910-100000@gamplex.bde.org>
In-Reply-To: <15418.4935.657413.312252@caddis.yogotech.com>

next in thread | previous in thread | raw e-mail | index | archive | help

On Mon, 7 Jan 2002, Nate Williams wrote:

> Also, forgive my ignorance, but why are we using fnsave instead of
> fsave?  From my reading of the x86 manuals, it would seem that there is

Because we aren't broken.

> the possibility of getting bogus FPU results with fnsave (vs. fsave)
> since by using fsave, we give the FPU a chance to finish up the current
> FPU operations before we save the state?  I could see where they might
> be a race where we might end up storing intermediate results of the FPU
> in some instances.

This is only for 8086/8087 systems, to work around a bug in the 8086.
See Morse, Isaaacson and Albert's _The 80386/387 Architechure_ p.150.
The 8086 doesn't wait for the 8087 to become idle before before
proceeding past an 8087 instruction.  The synchronization must be done
in software by putting a "wait" instruction before every 8087 instruction,
even though this breaks correct operation of control instructions like
fnsave (see below).  The bug was fixed on the 80286/80287.

Manuals (up to at least the 486 one) also say that "wait" instruction
is required _after_ FPU instructions if the memory written to by the
FPU instruction is read by the CPU.  E.g., the wait is required for
"fnsave mem; movl mem,%eax", and "fistpl mem; movl mem,%eax" but not
for "fnsave mem; frstor mem" or "fstpl mem; fldl mem".  I've never
seen this rule followed.  The gcc maintainers knew about it and decided
not to follow it long ago, since it would be a significant pessimization
for normal FPU code.  However, it would only be an insignificant
pessimization after fnsave.

> Or, is there something else I'm missing?  (The reason I ask is that the
> JDK uses fsave to be 'safe', but maybe it's not buying us anything other
> than warm fuzzies. :)

This is negatively safe.  fsave is just an abbreviation for "wait; fnsave".
Using "wait" before an FP control instruction is usually wrong because
it prevents the "no-wait" feature of FP control operations from working.
It causes any unmasked exceptions to be signaled immediately.  It is
especially wrong for fnsave in context switches because it causes the
signals in the context of the context switcher instead of in the context
of the thread that caused the exception.  It normally works OK in
-current and RELENG_4 (but not in RELENG_3) only because it has no
significant effects (all exceptions are masked by default).

Bruce


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020108111521.D2910-100000>