cpu_extended_state.h revision 1.1 1 /* $NetBSD: cpu_extended_state.h,v 1.1 2014/02/07 19:36:15 dsl Exp $ */
2
3 #ifndef _X86_CPU_EXTENDED_STATE_H_
4 #define _X86_CPU_EXTENDED_STATE_H_
5
6 /*
7 * This file contains definitions of structures that match the memory
8 * layouts used x86 processors to save floating point registers and other
9 * extended cpu state.
10 * This includes registers (etc) used by SSE/SSE2/SSE3/SSSE3/SSE4 and
11 * the later AVX instructions.
12 * The definitions are such that any future 'extended state' should
13 * be handled (provided the kernel doesn't need to know the actual contents.
14 *
15 * The actual structures the cpu accesses must be aligned to 16 for
16 * FXSAVE and 64 for XSAVE. The types aren't aligned because copies
17 * do not need extra alignment.
18 *
19 * The slightly different layout saved by the i387 fsave in also defined.
20 * This is only normally written by pre Pentium II type cpus that don't
21 * support the fxsave instruction.
22 *
23 * Associated save instructions:
24 * FNSAVE: Saves x87 state in 108 bytes (original i387 layout).
25 * Then reinitialies the fpu.
26 * FSAVE: Encodes to FWAIT followed by FNSAVE.
27 * FXSAVE: Saves the x87 state and XMM (aka SSE) registers to the
28 * first 448 (max) bytes of a 512 byte area.
29 * This layout does not match that written by FNSAVE.
30 * XSAVE: Uses the same layout for the x87 and XMM registers,
31 * followed by a 64byte header and separate save areas
32 * for additional extended cpu state.
33 * The x87 state is always saved, the others conditionally.
34 * XSAVEOPT: As XSAVE but (IIRC) only writes the registers blocks
35 * that have been modified.
36 */
37
38 #ifdef __lint__
39 /* Lint has different packing rules and doesn't understand __aligned() */
40 #define __CTASSERT_NOLINT(x) __CTASSERT(1)
41 #else
42 #define __CTASSERT_NOLINT(x) __CTASSERT(x)
43 #endif
44
45 /*
46 * Layout for code/data pointers relating to FP exceptions.
47 * Marked 'packed' because they aren't always 64bit aligned.
48 * Since the x86 cpu supports misaligned accesses it isn't
49 * worth avoiding the 'packed' attribute.
50 */
51 union fp_addr {
52 uint64_t fa_64; /* Linear address for 64bit systems */
53 struct {
54 uint32_t fa_off; /* linear address for 32 bit */
55 uint16_t fa_seg; /* code/data (etc) segment */
56 uint16_t fa_opcode; /* last opcode (sometimes) */
57 } fa_32;
58 } __packed;
59
60 /* The x87 registers are 80 bits */
61 struct fpacc87 {
62 uint64_t f87_mantissa; /* mantissa */
63 uint16_t f87_exp_sign; /* exponent and sign */
64 } __packed;
65
66 /* The x87 registers padded out to 16 bytes for fxsave */
67 struct fpaccfx {
68 struct fpacc87 r __aligned(16);
69 };
70
71 /* The SSE/SSE2 registers are 128 bits */
72 struct xmmreg {
73 uint8_t xmm_bytes[16];
74 };
75
76 /* The AVX registers are 256 bits, but the low bits are the xmmregs */
77 struct ymmreg {
78 uint8_t ymm_bytes[16];
79 };
80
81 /*
82 * Floating point unit registers (fsave instruction).
83 * The s87_ac[] and fx_87_ac[] are relative to the stack top.
84 * The 'tag word' contains 2 bits per register and refers to
85 * absolute register numbers.
86 * The cpu sets the tag values 0b01 (zero) and 0b10 (special) when a value
87 * is loaded. The software need only set 0b00 (used) and 0xb11 (unused).
88 * The fxsave 'Abridged tag word' in inverted.
89 */
90 struct save87 {
91 uint32_t s87_cw; /* control word (16bits) */
92 uint32_t s87_sw; /* status word (16bits) */
93 uint32_t s87_tw; /* tag word (16bits) */
94 union fp_addr s87_ip; /* floating point instruction pointer */
95 #define s87_opcode s87_ip.fa_32.fa_opcode /* opcode last executed (11bits) */
96 union fp_addr s87_dp; /* floating operand offset */
97 struct fpacc87 s87_ac[8]; /* accumulator contents, 0-7 */
98 };
99 __CTASSERT_NOLINT(sizeof (struct save87) == 108);
100
101 /* FPU/MMX/SSE/SSE2 context */
102 struct fxsave {
103 /*0*/ uint16_t fx_cw; /* FPU Control Word */
104 uint16_t fx_sw; /* FPU Status Word */
105 uint8_t fx_tw; /* FPU Tag Word (abridged) */
106 uint8_t fx_reserved1;
107 uint16_t fx_opcode; /* FPU Opcode */
108 union fp_addr fx_ip; /* FPU Instruction Pointer */
109 /*16*/ union fp_addr fx_dp; /* FPU Data pointer */
110 uint32_t fx_mxcsr; /* MXCSR Register State */
111 uint32_t fx_mxcsr_mask;
112 struct fpaccfx fx_87_ac[8]; /* 8 x87 registers */
113 struct xmmreg fx_xmmregs[16]; /* XMM regs (8 in 32bit modes) */
114 uint8_t fx_rsvd[48];
115 uint8_t fx_kernel[48]; /* Not written by the hardware */
116 } __aligned(16);
117 __CTASSERT_NOLINT(sizeof (struct fxsave) == 512);
118
119 /* The end of the fsave buffer can be used by the operating system */
120 struct fxsave_os {
121 uint8_t fxo_fxsave[offsetof(struct fxsave, fx_kernel)];
122 /* 48 bytes available */
123 };
124
125 union savefpu {
126 struct save87 sv_87;
127 struct fxsave sv_xmm;
128 struct fxsave_os sv_os;
129 };
130
131 /*
132 * For XSAVE a 64byte header follows the above.
133 * Currently it only contains one field of which only 3 bits are defined.
134 * Some other parts must be zero - zero it all.
135 *
136 * The xsh_xstate_bv bits match those of XCR0:
137 * XCR0_X87 0x00000001 x87 FPU/MMX state (always set)
138 * XCR0_SSE 0x00000002 SSE state
139 * XCR0_AVX 0x00000004 AVX state (ymmn registers)
140 *
141 * The offsets and sizes of any save areas can be found by reading
142 * the correct control registers.
143 */
144
145 struct xsave_header {
146 uint64_t xsh_xstate_bv; /* bitmap of saved sub structures */
147 uint64_t xsh_rsrvd[2]; /* must be zero */
148 uint64_t xsh_reserved[5];/* best if zero */
149 };
150 __CTASSERT(sizeof (struct xsave_header) == 64);
151
152 /*
153 * The ymm save area actually follows the xsave_header.
154 */
155 struct xsave_ymm {
156 struct ymmreg xs_ymm[16]; /* High bits of YMM registers */
157 };
158 __CTASSERT(sizeof (struct xsave_ymm) == 256);
159
160
161 /*
162 * 80387 control and status word bits
163 *
164 * The only reference to the 0x40 and 0x80 bits I can find is for
165 * the Weitek 1167/3167.
166 * I (dsl) can't find why the default word has 0x40 set.
167 */
168 /* Interrupt masks (set masks interrupt) and status bits */
169 #define EN_SW_INVOP 0x0001 /* Invalid operation */
170 #define EN_SW_DENORM 0x0002 /* Denormalized operand */
171 #define EN_SW_ZERODIV 0x0004 /* Divide by zero */
172 #define EN_SW_OVERFLOW 0x0008 /* Overflow */
173 #define EN_SW_UNDERFLOW 0x0010 /* Underflow */
174 #define EN_SW_PRECLOSS 0x0020 /* Loss of precision */
175 #define EN_SW_RSVD_40 0x0040 /* Reserverd for all x87 parts */
176 #define EN_SW_RSVD_80 0x0080 /* Reserverd for all x87 parts */
177 /* Control bits (badly named) */
178 #define EN_SW_CTL_PREC 0x0300 /* Precision control */
179 #define EN_SW_PREC_24 0x0000 /* Single precision */
180 #define EN_SW_PREC_53 0x0200 /* Double precision */
181 #define EN_SW_PREC_64 0x0300 /* Extended precision */
182 #define EN_SW_CTL_ROUND 0x0c00 /* Rounding control */
183 #define EN_SW_ROUND_EVEN 0x0000 /* Round to nearest even */
184 #define EN_SW_ROUND_DOWN 0x0400 /* Round towards minus infinity */
185 #define EN_SW_ROUND_UP 0x0800 /* Round towards plus infinity */
186 #define EN_SW_ROUND_ZERO 0x0c00 /* Round towards zero (truncates) */
187 #define EN_SW_CTL_INF 0x1000 /* Infinity control, not used */
188
189 /*
190 * The standard 0x87 control word from finit is 0x37F, giving:
191 * round to nearest
192 * 64-bit precision
193 * all exceptions masked.
194 *
195 * NetBSD used to select:
196 * round to nearest
197 * 53-bit precision
198 * all exceptions masked.
199 * Stating: 64-bit precision often gives bad results with high level
200 * languages because it makes the results of calculations depend on whether
201 * intermediate values are stored in memory or in FPU registers.
202 * Also some 'pathological divisions' give an error in the LSB because
203 * the value is first rounded up when the 64bit mantissa is generated,
204 * and then again when it is truncated to 53 bits.
205 *
206 * However the C language explicitly allows the extra precision.
207 *
208 * The iBCS control word has underflow, overflow, zero divide, and invalid
209 * operation exceptions unmasked. But that causes an unexpected exception
210 * in the test program 'paranoia' and makes denormals useless (DBL_MIN / 2
211 * underflows). It doesn't make a lot of sense to trap underflow without
212 * trapping denormals.
213 */
214 #define __INITIAL_NPXCW__ 0x037f
215 /* Modern NetBSD uses the default control word.. */
216 #define __NetBSD_NPXCW__ __INITIAL_NPXCW__
217 /* NetBSD before 6.99.26 forced IEEE double precision. */
218 #define __NetBSD_COMPAT_NPXCW__ 0x127f
219 /* FreeBSD leaves some exceptions unmasked as well. */
220 #define __FreeBSD_NPXCW__ 0x1272
221 /* iBCS2 goes a bit further and leaves the underflow exception unmasked. */
222 #define __iBCS2_NPXCW__ 0x0262
223 /* Linux just uses the default control word. */
224 #define __Linux_NPXCW__ __INITIAL_NPXCW__
225 /* SVR4 uses the same control word as iBCS2. */
226 #define __SVR4_NPXCW__ 0x0262
227
228 /*
229 * The default MXCSR value at reset is 0x1f80, IA-32 Instruction
230 * Set Reference, pg. 3-369.
231 *
232 * The low 6 bits of the mxcsr are the fp status bits (same order as x87).
233 * Bit 6 is 'denormals are zero' (speeds up calculations).
234 * Bits 7-16 are the interrupt mask bits (same order, 1 to mask).
235 * Bits 13 and 14 are rounding control.
236 * Bit 15 is 'flush to zero' - affects underflow.
237 * Bits 16-31 must be zero.
238 */
239 #define __INITIAL_MXCSR__ 0x1f80
240
241
242 #endif /* _X86_CPU_EXTENDED_STATE_H_ */
243