Intel architecture ia-32 User Manual

Page of 636
Vol. 3A 12-7
SSE, SSE2 AND SSE3 SYSTEM PROGRAMMING
In some cases, applications can only save the XMM and MXCSR registers in the following way:
Execute eight MOVDQ instructions to save the contents of the XMM0 through XMM7
registers to memory. 
Execute a STMXCSR instruction to save the state of the MXCSR register to memory.
In some cases, applications can only restore the XMM and MXCSR registers in the following
way:
Execute eight MOVDQ instructions to read the saved contents of XMM registers from
memory into the XMM0 through XMM7 registers.
Execute a LDMXCSR instruction to restore the state of the MXCSR register from memory.
12.4
SAVING THE SSE/SSE2/SSE3 STATE ON TASK
OR CONTEXT SWITCHES
When switching from one task or context to another, it is often necessary to save the
SSE/SSE2/SSE3 state. The FXSAVE and FXRSTOR instructions provide a simple method for
saving and restoring this state (as described in Section 12.3, “Saving and Restoring the
SSE/SSE2/SSE3 State”). 
These instructions offer the added benefit of saving the x87 FPU and
MMX state as well. Guidelines for writing such procedures are in Section 12.5, “Designing OS
Facilities for AUTOMATICALLY Saving x87 FPU, MMX, and SSE/SSE2/SSE3 state on Task
or Context Switches.”
12.5
DESIGNING OS FACILITIES FOR AUTOMATICALLY SAVING 
X87 FPU, MMX, AND SSE/SSE2/SSE3 STATE ON TASK OR 
CONTEXT SWITCHES
The x87 FPU/MMX/SSE/SSE2/SSE3 state consists of the state of the x87 FPU, MMX, XMM,
and MXCSR registers. The FXSAVE and FXRSTOR instructions provide a fast method of
saving ad restoring this state. If task or context switching facilities are already implemented in
an operating system or executive and they use FSAVE/FNSAVE and FRSTOR to save the x87
FPU and MMX state, these facilities can also be extended to save and restore the
SSE/SSE2/SSE3 state by substituting FXSAVE and FXRSTOR for FSAVE/FNSAVE and
FRSTOR. 
In cases where task or content switching facilities must be written from scratch, several
approaches can be taken for using the FXSAVE and FXRSTOR instructions to save and restore
the 87 FPU/MMX/SSE/SSE2/SSE3 state:
The operating system can require applications that are intended be run as tasks take
responsibility for saving the state of the x87 FPU, MMX, XXM, and MXCSR registers
prior to a task suspension during a task switch and for restoring the registers when the task
is resumed. This approach is appropriate for cooperative multitasking operating systems,
where the application has control over (or is able to determine) when a task switch is about
to occur and can save state prior to the task switch.