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