x86/fpu: Add placeholder for 'Processor Trace' XSAVE state
[cascardo/linux.git] / arch / x86 / include / asm / fpu / types.h
1 /*
2  * FPU data structures:
3  */
4 #ifndef _ASM_X86_FPU_H
5 #define _ASM_X86_FPU_H
6
7 /*
8  * The legacy x87 FPU state format, as saved by FSAVE and
9  * restored by the FRSTOR instructions:
10  */
11 struct fregs_state {
12         u32                     cwd;    /* FPU Control Word             */
13         u32                     swd;    /* FPU Status Word              */
14         u32                     twd;    /* FPU Tag Word                 */
15         u32                     fip;    /* FPU IP Offset                */
16         u32                     fcs;    /* FPU IP Selector              */
17         u32                     foo;    /* FPU Operand Pointer Offset   */
18         u32                     fos;    /* FPU Operand Pointer Selector */
19
20         /* 8*10 bytes for each FP-reg = 80 bytes:                       */
21         u32                     st_space[20];
22
23         /* Software status information [not touched by FSAVE]:          */
24         u32                     status;
25 };
26
27 /*
28  * The legacy fx SSE/MMX FPU state format, as saved by FXSAVE and
29  * restored by the FXRSTOR instructions. It's similar to the FSAVE
30  * format, but differs in some areas, plus has extensions at
31  * the end for the XMM registers.
32  */
33 struct fxregs_state {
34         u16                     cwd; /* Control Word                    */
35         u16                     swd; /* Status Word                     */
36         u16                     twd; /* Tag Word                        */
37         u16                     fop; /* Last Instruction Opcode         */
38         union {
39                 struct {
40                         u64     rip; /* Instruction Pointer             */
41                         u64     rdp; /* Data Pointer                    */
42                 };
43                 struct {
44                         u32     fip; /* FPU IP Offset                   */
45                         u32     fcs; /* FPU IP Selector                 */
46                         u32     foo; /* FPU Operand Offset              */
47                         u32     fos; /* FPU Operand Selector            */
48                 };
49         };
50         u32                     mxcsr;          /* MXCSR Register State */
51         u32                     mxcsr_mask;     /* MXCSR Mask           */
52
53         /* 8*16 bytes for each FP-reg = 128 bytes:                      */
54         u32                     st_space[32];
55
56         /* 16*16 bytes for each XMM-reg = 256 bytes:                    */
57         u32                     xmm_space[64];
58
59         u32                     padding[12];
60
61         union {
62                 u32             padding1[12];
63                 u32             sw_reserved[12];
64         };
65
66 } __attribute__((aligned(16)));
67
68 /* Default value for fxregs_state.mxcsr: */
69 #define MXCSR_DEFAULT           0x1f80
70
71 /*
72  * Software based FPU emulation state. This is arbitrary really,
73  * it matches the x87 format to make it easier to understand:
74  */
75 struct swregs_state {
76         u32                     cwd;
77         u32                     swd;
78         u32                     twd;
79         u32                     fip;
80         u32                     fcs;
81         u32                     foo;
82         u32                     fos;
83         /* 8*10 bytes for each FP-reg = 80 bytes: */
84         u32                     st_space[20];
85         u8                      ftop;
86         u8                      changed;
87         u8                      lookahead;
88         u8                      no_update;
89         u8                      rm;
90         u8                      alimit;
91         struct math_emu_info    *info;
92         u32                     entry_eip;
93 };
94
95 /*
96  * List of XSAVE features Linux knows about:
97  */
98 enum xfeature {
99         XFEATURE_FP,
100         XFEATURE_SSE,
101         /*
102          * Values above here are "legacy states".
103          * Those below are "extended states".
104          */
105         XFEATURE_YMM,
106         XFEATURE_BNDREGS,
107         XFEATURE_BNDCSR,
108         XFEATURE_OPMASK,
109         XFEATURE_ZMM_Hi256,
110         XFEATURE_Hi16_ZMM,
111         XFEATURE_PT_UNIMPLEMENTED_SO_FAR,
112
113         XFEATURE_MAX,
114 };
115
116 #define XFEATURE_MASK_FP                (1 << XFEATURE_FP)
117 #define XFEATURE_MASK_SSE               (1 << XFEATURE_SSE)
118 #define XFEATURE_MASK_YMM               (1 << XFEATURE_YMM)
119 #define XFEATURE_MASK_BNDREGS           (1 << XFEATURE_BNDREGS)
120 #define XFEATURE_MASK_BNDCSR            (1 << XFEATURE_BNDCSR)
121 #define XFEATURE_MASK_OPMASK            (1 << XFEATURE_OPMASK)
122 #define XFEATURE_MASK_ZMM_Hi256         (1 << XFEATURE_ZMM_Hi256)
123 #define XFEATURE_MASK_Hi16_ZMM          (1 << XFEATURE_Hi16_ZMM)
124
125 #define XFEATURE_MASK_FPSSE             (XFEATURE_MASK_FP | XFEATURE_MASK_SSE)
126 #define XFEATURE_MASK_AVX512            (XFEATURE_MASK_OPMASK \
127                                          | XFEATURE_MASK_ZMM_Hi256 \
128                                          | XFEATURE_MASK_Hi16_ZMM)
129
130 #define FIRST_EXTENDED_XFEATURE XFEATURE_YMM
131
132 struct reg_128_bit {
133         u8      regbytes[128/8];
134 };
135 struct reg_256_bit {
136         u8      regbytes[256/8];
137 };
138 struct reg_512_bit {
139         u8      regbytes[512/8];
140 };
141
142 /*
143  * State component 2:
144  *
145  * There are 16x 256-bit AVX registers named YMM0-YMM15.
146  * The low 128 bits are aliased to the 16 SSE registers (XMM0-XMM15)
147  * and are stored in 'struct fxregs_state::xmm_space[]' in the
148  * "legacy" area.
149  *
150  * The high 128 bits are stored here.
151  */
152 struct ymmh_struct {
153         struct reg_128_bit              hi_ymm[16];
154 } __packed;
155
156 /* Intel MPX support: */
157
158 struct mpx_bndreg {
159         u64                             lower_bound;
160         u64                             upper_bound;
161 } __packed;
162 /*
163  * State component 3 is used for the 4 128-bit bounds registers
164  */
165 struct mpx_bndreg_state {
166         struct mpx_bndreg               bndreg[4];
167 } __packed;
168
169 /*
170  * State component 4 is used for the 64-bit user-mode MPX
171  * configuration register BNDCFGU and the 64-bit MPX status
172  * register BNDSTATUS.  We call the pair "BNDCSR".
173  */
174 struct mpx_bndcsr {
175         u64                             bndcfgu;
176         u64                             bndstatus;
177 } __packed;
178
179 /*
180  * The BNDCSR state is padded out to be 64-bytes in size.
181  */
182 struct mpx_bndcsr_state {
183         union {
184                 struct mpx_bndcsr               bndcsr;
185                 u8                              pad_to_64_bytes[64];
186         };
187 } __packed;
188
189 /* AVX-512 Components: */
190
191 /*
192  * State component 5 is used for the 8 64-bit opmask registers
193  * k0-k7 (opmask state).
194  */
195 struct avx_512_opmask_state {
196         u64                             opmask_reg[8];
197 } __packed;
198
199 /*
200  * State component 6 is used for the upper 256 bits of the
201  * registers ZMM0-ZMM15. These 16 256-bit values are denoted
202  * ZMM0_H-ZMM15_H (ZMM_Hi256 state).
203  */
204 struct avx_512_zmm_uppers_state {
205         struct reg_256_bit              zmm_upper[16];
206 } __packed;
207
208 /*
209  * State component 7 is used for the 16 512-bit registers
210  * ZMM16-ZMM31 (Hi16_ZMM state).
211  */
212 struct avx_512_hi16_state {
213         struct reg_512_bit              hi16_zmm[16];
214 } __packed;
215
216 struct xstate_header {
217         u64                             xfeatures;
218         u64                             xcomp_bv;
219         u64                             reserved[6];
220 } __attribute__((packed));
221
222 /*
223  * This is our most modern FPU state format, as saved by the XSAVE
224  * and restored by the XRSTOR instructions.
225  *
226  * It consists of a legacy fxregs portion, an xstate header and
227  * subsequent areas as defined by the xstate header.  Not all CPUs
228  * support all the extensions, so the size of the extended area
229  * can vary quite a bit between CPUs.
230  */
231 struct xregs_state {
232         struct fxregs_state             i387;
233         struct xstate_header            header;
234         u8                              extended_state_area[0];
235 } __attribute__ ((packed, aligned (64)));
236
237 /*
238  * This is a union of all the possible FPU state formats
239  * put together, so that we can pick the right one runtime.
240  *
241  * The size of the structure is determined by the largest
242  * member - which is the xsave area.  The padding is there
243  * to ensure that statically-allocated task_structs (just
244  * the init_task today) have enough space.
245  */
246 union fpregs_state {
247         struct fregs_state              fsave;
248         struct fxregs_state             fxsave;
249         struct swregs_state             soft;
250         struct xregs_state              xsave;
251         u8 __padding[PAGE_SIZE];
252 };
253
254 /*
255  * Highest level per task FPU state data structure that
256  * contains the FPU register state plus various FPU
257  * state fields:
258  */
259 struct fpu {
260         /*
261          * @last_cpu:
262          *
263          * Records the last CPU on which this context was loaded into
264          * FPU registers. (In the lazy-restore case we might be
265          * able to reuse FPU registers across multiple context switches
266          * this way, if no intermediate task used the FPU.)
267          *
268          * A value of -1 is used to indicate that the FPU state in context
269          * memory is newer than the FPU state in registers, and that the
270          * FPU state should be reloaded next time the task is run.
271          */
272         unsigned int                    last_cpu;
273
274         /*
275          * @fpstate_active:
276          *
277          * This flag indicates whether this context is active: if the task
278          * is not running then we can restore from this context, if the task
279          * is running then we should save into this context.
280          */
281         unsigned char                   fpstate_active;
282
283         /*
284          * @fpregs_active:
285          *
286          * This flag determines whether a given context is actively
287          * loaded into the FPU's registers and that those registers
288          * represent the task's current FPU state.
289          *
290          * Note the interaction with fpstate_active:
291          *
292          *   # task does not use the FPU:
293          *   fpstate_active == 0
294          *
295          *   # task uses the FPU and regs are active:
296          *   fpstate_active == 1 && fpregs_active == 1
297          *
298          *   # the regs are inactive but still match fpstate:
299          *   fpstate_active == 1 && fpregs_active == 0 && fpregs_owner == fpu
300          *
301          * The third state is what we use for the lazy restore optimization
302          * on lazy-switching CPUs.
303          */
304         unsigned char                   fpregs_active;
305
306         /*
307          * @counter:
308          *
309          * This counter contains the number of consecutive context switches
310          * during which the FPU stays used. If this is over a threshold, the
311          * lazy FPU restore logic becomes eager, to save the trap overhead.
312          * This is an unsigned char so that after 256 iterations the counter
313          * wraps and the context switch behavior turns lazy again; this is to
314          * deal with bursty apps that only use the FPU for a short time:
315          */
316         unsigned char                   counter;
317         /*
318          * @state:
319          *
320          * In-memory copy of all FPU registers that we save/restore
321          * over context switches. If the task is using the FPU then
322          * the registers in the FPU are more recent than this state
323          * copy. If the task context-switches away then they get
324          * saved here and represent the FPU state.
325          *
326          * After context switches there may be a (short) time period
327          * during which the in-FPU hardware registers are unchanged
328          * and still perfectly match this state, if the tasks
329          * scheduled afterwards are not using the FPU.
330          *
331          * This is the 'lazy restore' window of optimization, which
332          * we track though 'fpu_fpregs_owner_ctx' and 'fpu->last_cpu'.
333          *
334          * We detect whether a subsequent task uses the FPU via setting
335          * CR0::TS to 1, which causes any FPU use to raise a #NM fault.
336          *
337          * During this window, if the task gets scheduled again, we
338          * might be able to skip having to do a restore from this
339          * memory buffer to the hardware registers - at the cost of
340          * incurring the overhead of #NM fault traps.
341          *
342          * Note that on modern CPUs that support the XSAVEOPT (or other
343          * optimized XSAVE instructions), we don't use #NM traps anymore,
344          * as the hardware can track whether FPU registers need saving
345          * or not. On such CPUs we activate the non-lazy ('eagerfpu')
346          * logic, which unconditionally saves/restores all FPU state
347          * across context switches. (if FPU state exists.)
348          */
349         union fpregs_state              state;
350         /*
351          * WARNING: 'state' is dynamically-sized.  Do not put
352          * anything after it here.
353          */
354 };
355
356 #endif /* _ASM_X86_FPU_H */