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