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