xref: /linux/arch/x86/include/uapi/asm/sigcontext.h (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_ASM_X86_SIGCONTEXT_H
3 #define _UAPI_ASM_X86_SIGCONTEXT_H
4 
5 /*
6  * Linux signal context definitions. The sigcontext includes a complex
7  * hierarchy of CPU and FPU state, available to user-space (on the stack) when
8  * a signal handler is executed.
9  *
10  * As over the years this ABI grew from its very simple roots towards
11  * supporting more and more CPU state organically, some of the details (which
12  * were rather clever hacks back in the days) became a bit quirky by today.
13  *
14  * The current ABI includes flexible provisions for future extensions, so we
15  * won't have to grow new quirks for quite some time. Promise!
16  */
17 
18 #include <linux/compiler.h>
19 #include <linux/types.h>
20 
21 #define FP_XSTATE_MAGIC1		0x46505853U
22 #define FP_XSTATE_MAGIC2		0x46505845U
23 #define FP_XSTATE_MAGIC2_SIZE		sizeof(FP_XSTATE_MAGIC2)
24 
25 /*
26  * Bytes 464..511 in the current 512-byte layout of the FXSAVE/FXRSTOR frame
27  * are reserved for SW usage. On CPUs supporting XSAVE/XRSTOR, these bytes are
28  * used to extend the fpstate pointer in the sigcontext, which now includes the
29  * extended state information along with fpstate information.
30  *
31  * If sw_reserved.magic1 == FP_XSTATE_MAGIC1 then there's a
32  * sw_reserved.extended_size bytes large extended context area present. (The
33  * last 32-bit word of this extended area (at the
34  * fpstate+extended_size-FP_XSTATE_MAGIC2_SIZE address) is set to
35  * FP_XSTATE_MAGIC2 so that you can sanity check your size calculations.)
36  *
37  * The xstate_size field indicates the actual size of the xstate context
38  * (including the 512-byte FXSAVE area and the 64-byte XSAVE header struct
39  * _header). This size is used in conjunction with the pointer to the xstate
40  * context to locate FP_XSTATE_MAGIC2.
41  *
42  * In 64-bit signal frames, the fpstate pointer points directly to the xstate
43  * context. In 32-bit signal frames (including 32-bit compat tasks on 64-bit
44  * kernels), the fpstate pointer points to struct _fpstate_32, which contains
45  * the 112-byte legacy FPU state followed by the 512-byte FXSR state (and any
46  * extended xstate), so the xstate context starts at fpstate + 112.
47  *
48  * This makes the signal frame self-describing and portable across machines
49  * with different xstate features. See Documentation/arch/x86/xstate.rst
50  * for details on signal frame portability and its architectural constraints.
51  *
52  * This extended area typically grows with newer CPUs that have larger and
53  * larger XSAVE areas.
54  */
55 struct _fpx_sw_bytes {
56 	/*
57 	 * If set to FP_XSTATE_MAGIC1 then this is an xstate context.
58 	 * 0 if a legacy frame.
59 	 */
60 	__u32				magic1;
61 
62 	/*
63 	 * Total size of the fpstate area:
64 	 *
65 	 *  - if magic1 == 0 then it's sizeof(struct _fpstate)
66 	 *  - if magic1 == FP_XSTATE_MAGIC1 then it's sizeof(struct _xstate)
67 	 *    plus extensions (if any)
68 	 */
69 	__u32				extended_size;
70 
71 	/*
72 	 * Feature bit mask (including FP/SSE/extended state) that is present
73 	 * in the memory layout:
74 	 */
75 	__u64				xfeatures;
76 
77 	/*
78 	 * Actual XSAVE state size, based on the xfeatures saved in the layout.
79 	 * 'extended_size' is greater than 'xstate_size':
80 	 */
81 	__u32				xstate_size;
82 
83 	/* For future use: */
84 	__u32				padding[7];
85 };
86 
87 /*
88  * As documented in the iBCS2 standard:
89  *
90  * The first part of "struct _fpstate" is just the normal i387 hardware setup,
91  * the extra "status" word is used to save the coprocessor status word before
92  * entering the handler.
93  *
94  * The FPU state data structure has had to grow to accommodate the extended FPU
95  * state required by the Streaming SIMD Extensions.  There is no documented
96  * standard to accomplish this at the moment.
97  */
98 
99 /* 10-byte legacy floating point register: */
100 struct _fpreg {
101 	__u16				significand[4];
102 	__u16				exponent;
103 };
104 
105 /* 16-byte floating point register: */
106 struct _fpxreg {
107 	__u16				significand[4];
108 	__u16				exponent;
109 	__u16				padding[3];
110 };
111 
112 /* 16-byte XMM register: */
113 struct _xmmreg {
114 	__u32				element[4];
115 };
116 
117 #define X86_FXSR_MAGIC			0x0000
118 
119 /*
120  * The 32-bit FPU frame:
121  */
122 struct _fpstate_32 {
123 	/* Legacy FPU environment: */
124 	__u32				cw;
125 	__u32				sw;
126 	__u32				tag;
127 	__u32				ipoff;
128 	__u32				cssel;
129 	__u32				dataoff;
130 	__u32				datasel;
131 	struct _fpreg			_st[8];
132 	__u16				status;
133 	__u16				magic;		/* 0xffff: regular FPU data only */
134 							/* 0x0000: FXSR FPU data */
135 
136 	/* FXSR FPU environment */
137 	__u32				_fxsr_env[6];	/* FXSR FPU env is ignored */
138 	__u32				mxcsr;
139 	__u32				reserved;
140 	struct _fpxreg			_fxsr_st[8];	/* FXSR FPU reg data is ignored */
141 	struct _xmmreg			_xmm[8];	/* First 8 XMM registers */
142 	union {
143 		__u32			padding1[44];	/* Second 8 XMM registers plus padding */
144 		__u32			padding[44];	/* Alias name for old user-space */
145 	};
146 
147 	union {
148 		__u32			padding2[12];
149 		struct _fpx_sw_bytes	sw_reserved;	/* Potential extended state is encoded here */
150 	};
151 };
152 
153 /*
154  * The 64-bit FPU frame. (FXSAVE format and later)
155  *
156  * Note1: If sw_reserved.magic1 == FP_XSTATE_MAGIC1 then the structure is
157  *        larger: 'struct _xstate'. Note that 'struct _xstate' embeds
158  *        'struct _fpstate' so that you can always assume the _fpstate portion
159  *        exists so that you can check the magic value.
160  *
161  * Note2: Reserved fields may someday contain valuable data. Always
162  *	  save/restore them when you change signal frames.
163  */
164 struct _fpstate_64 {
165 	__u16				cwd;
166 	__u16				swd;
167 	/* Note this is not the same as the 32-bit/x87/FSAVE twd: */
168 	__u16				twd;
169 	__u16				fop;
170 	__u64				rip;
171 	__u64				rdp;
172 	__u32				mxcsr;
173 	__u32				mxcsr_mask;
174 	__u32				st_space[32];	/*  8x  FP registers, 16 bytes each */
175 	__u32				xmm_space[64];	/* 16x XMM registers, 16 bytes each */
176 	__u32				reserved2[12];
177 	union {
178 		__u32			reserved3[12];
179 		struct _fpx_sw_bytes	sw_reserved;	/* Potential extended state is encoded here */
180 	};
181 };
182 
183 #ifdef __i386__
184 # define _fpstate _fpstate_32
185 #else
186 # define _fpstate _fpstate_64
187 #endif
188 
189 struct _header {
190 	__u64				xfeatures;
191 	__u64				reserved1[2];
192 	__u64				reserved2[5];
193 };
194 
195 struct _ymmh_state {
196 	/* 16x YMM registers, 16 bytes each: */
197 	__u32				ymmh_space[64];
198 };
199 
200 /*
201  * Extended state pointed to by sigcontext::fpstate.
202  *
203  * In addition to the fpstate, information encoded in _xstate::xstate_hdr
204  * indicates the presence of other extended state information supported
205  * by the CPU and kernel:
206  */
207 struct _xstate {
208 	struct _fpstate			fpstate;
209 	struct _header			xstate_hdr;
210 	struct _ymmh_state		ymmh;
211 	/* New processor state extensions go here: */
212 };
213 
214 /*
215  * The 32-bit signal frame:
216  */
217 struct sigcontext_32 {
218 	__u16				gs, __gsh;
219 	__u16				fs, __fsh;
220 	__u16				es, __esh;
221 	__u16				ds, __dsh;
222 	__u32				di;
223 	__u32				si;
224 	__u32				bp;
225 	__u32				sp;
226 	__u32				bx;
227 	__u32				dx;
228 	__u32				cx;
229 	__u32				ax;
230 	__u32				trapno;
231 	__u32				err;
232 	__u32				ip;
233 	__u16				cs, __csh;
234 	__u32				flags;
235 	__u32				sp_at_signal;
236 	__u16				ss, __ssh;
237 
238 	/*
239 	 * fpstate is really (struct _fpstate *) or (struct _xstate *)
240 	 * depending on the FP_XSTATE_MAGIC1 encoded in the SW reserved
241 	 * bytes of (struct _fpstate) and FP_XSTATE_MAGIC2 present at the end
242 	 * of extended memory layout. See comments at the definition of
243 	 * (struct _fpx_sw_bytes)
244 	 */
245 	__u32				fpstate; /* Zero when no FPU/extended context */
246 	__u32				oldmask;
247 	__u32				cr2;
248 };
249 
250 /*
251  * The 64-bit signal frame:
252  */
253 struct sigcontext_64 {
254 	__u64				r8;
255 	__u64				r9;
256 	__u64				r10;
257 	__u64				r11;
258 	__u64				r12;
259 	__u64				r13;
260 	__u64				r14;
261 	__u64				r15;
262 	__u64				di;
263 	__u64				si;
264 	__u64				bp;
265 	__u64				bx;
266 	__u64				dx;
267 	__u64				ax;
268 	__u64				cx;
269 	__u64				sp;
270 	__u64				ip;
271 	__u64				flags;
272 	__u16				cs;
273 	__u16				gs;
274 	__u16				fs;
275 	__u16				ss;
276 	__u64				err;
277 	__u64				trapno;
278 	__u64				oldmask;
279 	__u64				cr2;
280 
281 	/*
282 	 * fpstate is really (struct _fpstate *) or (struct _xstate *)
283 	 * depending on the FP_XSTATE_MAGIC1 encoded in the SW reserved
284 	 * bytes of (struct _fpstate) and FP_XSTATE_MAGIC2 present at the end
285 	 * of extended memory layout. See comments at the definition of
286 	 * (struct _fpx_sw_bytes)
287 	 */
288 	__u64				fpstate; /* Zero when no FPU/extended context */
289 	__u64				reserved1[8];
290 };
291 
292 /*
293  * Create the real 'struct sigcontext' type:
294  */
295 #ifdef __KERNEL__
296 # ifdef __i386__
297 #  define sigcontext sigcontext_32
298 # else
299 #  define sigcontext sigcontext_64
300 # endif
301 #endif
302 
303 /*
304  * The old user-space sigcontext definition, just in case user-space still
305  * relies on it. The kernel definition (in asm/sigcontext.h) has unified
306  * field names but otherwise the same layout.
307  */
308 #ifndef __KERNEL__
309 
310 #define _fpstate_ia32			_fpstate_32
311 #define sigcontext_ia32			sigcontext_32
312 
313 
314 # ifdef __i386__
315 struct sigcontext {
316 	__u16				gs, __gsh;
317 	__u16				fs, __fsh;
318 	__u16				es, __esh;
319 	__u16				ds, __dsh;
320 	__u32				edi;
321 	__u32				esi;
322 	__u32				ebp;
323 	__u32				esp;
324 	__u32				ebx;
325 	__u32				edx;
326 	__u32				ecx;
327 	__u32				eax;
328 	__u32				trapno;
329 	__u32				err;
330 	__u32				eip;
331 	__u16				cs, __csh;
332 	__u32				eflags;
333 	__u32				esp_at_signal;
334 	__u16				ss, __ssh;
335 	struct _fpstate __user		*fpstate;
336 	__u32				oldmask;
337 	__u32				cr2;
338 };
339 # else /* __x86_64__: */
340 struct sigcontext {
341 	__u64				r8;
342 	__u64				r9;
343 	__u64				r10;
344 	__u64				r11;
345 	__u64				r12;
346 	__u64				r13;
347 	__u64				r14;
348 	__u64				r15;
349 	__u64				rdi;
350 	__u64				rsi;
351 	__u64				rbp;
352 	__u64				rbx;
353 	__u64				rdx;
354 	__u64				rax;
355 	__u64				rcx;
356 	__u64				rsp;
357 	__u64				rip;
358 	__u64				eflags;		/* RFLAGS */
359 	__u16				cs;
360 
361 	/*
362 	 * Prior to 2.5.64 ("[PATCH] x86-64 updates for 2.5.64-bk3"),
363 	 * Linux saved and restored fs and gs in these slots.  This
364 	 * was counterproductive, as fsbase and gsbase were never
365 	 * saved, so arch_prctl was presumably unreliable.
366 	 *
367 	 * These slots should never be reused without extreme caution:
368 	 *
369 	 *  - Some DOSEMU versions stash fs and gs in these slots manually,
370 	 *    thus overwriting anything the kernel expects to be preserved
371 	 *    in these slots.
372 	 *
373 	 *  - If these slots are ever needed for any other purpose,
374 	 *    there is some risk that very old 64-bit binaries could get
375 	 *    confused.  I doubt that many such binaries still work,
376 	 *    though, since the same patch in 2.5.64 also removed the
377 	 *    64-bit set_thread_area syscall, so it appears that there
378 	 *    is no TLS API beyond modify_ldt that works in both pre-
379 	 *    and post-2.5.64 kernels.
380 	 *
381 	 * If the kernel ever adds explicit fs, gs, fsbase, and gsbase
382 	 * save/restore, it will most likely need to be opt-in and use
383 	 * different context slots.
384 	 */
385 	__u16				gs;
386 	__u16				fs;
387 	union {
388 		__u16			ss;	/* If UC_SIGCONTEXT_SS */
389 		__u16			__pad0;	/* Alias name for old (!UC_SIGCONTEXT_SS) user-space */
390 	};
391 	__u64				err;
392 	__u64				trapno;
393 	__u64				oldmask;
394 	__u64				cr2;
395 	struct _fpstate __user		*fpstate;	/* Zero when no FPU context */
396 #  ifdef __ILP32__
397 	__u32				__fpstate_pad;
398 #  endif
399 	__u64				reserved1[8];
400 };
401 # endif /* __x86_64__ */
402 #endif /* !__KERNEL__ */
403 
404 #endif /* _UAPI_ASM_X86_SIGCONTEXT_H */
405