E-23
GUIDELINES FOR WRITING FPU EXCEPTIONS HANDLERS
A simple way to handle the case of exceptions arriving during FPU state swaps is to allow the
kernel to be one of the FPU owning threads. A reserved thread identifier is used to indicate ker-
nel ownership of the FPU. During an floating-point state swap, the FPU owner variable should
be set to indicate the kernel as the current owner. At the completion of the state swap, the vari-
able should be set to indicate the new owning thread. The numeric exception handler needs to
check the FPU owner and discard any numeric exceptions that occur while the kernel is the FPU
owner. A more general flow for a DNA exception handler that handles this case is shown in Fig-
ure E-5.
Numeric exceptions received while the kernel owns the FPU for a state swap must be discarded
in the kernel without being dispatched to a handler. A flow for a numeric exception dispatch rou-
tine is shown in Figure E-6.
It may at first glance seem that there is a possibility of floating-point exceptions being lost be-
cause of exceptions that are discarded during state swaps. This is not the case, as the exception
will be re-issued when the floating-point state is reloaded. Walking through state swaps both
with and without pending numeric exceptions will clarify the operation of these two handlers.
Case #1: FPU State Swap Without Numeric Exception
Assume two threads A and B, both using the floating-point unit. Let A be the thread to have most
recently executed a floating-point instruction, with no pending numeric exceptions. Let B be the
currently executing thread. CR0.TS was set when thread A was suspended. When B starts to ex-
ecute a floating-point instruction the instruction will fault with the DNA exception because TS
is set.
At this point the handler is entered, and eventually it finds that the current FPU Owner is not the
currently executing thread. To guard the FPU state swap from extraneous numeric exceptions,
the FPU Owner is set to be the kernel. The old owners FPU state is saved with FNSAVE, and
the current threads FPU state is restored with FRSTOR. Before exiting, the FPU owner is set to
thread B, and the TS bit is cleared.
On exit, thread B resumes execution of the faulting floating-point instruction and continues.
Case #2: FPU State Swap with Discarded Numeric Exception
Again, assume two threads A and B, both using the floating-point unit. Let A be the thread to
have most recently executed a floating-point instruction, but this time let there be a pending nu-
meric exception. Let B be the currently executing thread. When B starts to execute a floating-
point instruction the instruction will fault with the DNA exception and enter the DNA handler.
(If both numeric and DNA exceptions are pending, the DNA exception takes precedence, in or-
der to support handling the numeric exception in its own context.)