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