1/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2#ifndef _ASM_X86_SIGCONTEXT_H
3#define _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
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 */
40struct _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: */
85struct _fpreg {
86 __u16 significand[4];
87 __u16 exponent;
88};
89
90/* 16-byte floating point register: */
91struct _fpxreg {
92 __u16 significand[4];
93 __u16 exponent;
94 __u16 padding[3];
95};
96
97/* 16-byte XMM register: */
98struct _xmmreg {
99 __u32 element[4];
100};
101
102#define X86_FXSR_MAGIC 0x0000
103
104/*
105 * The 32-bit FPU frame:
106 */
107struct _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' embeds
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 */
149struct _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
174struct _header {
175 __u64 xfeatures;
176 __u64 reserved1[2];
177 __u64 reserved2[5];
178};
179
180struct _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 */
192struct _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 */
202struct 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 */
238struct 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
281/*
282 * The old user-space sigcontext definition, just in case user-space still
283 * relies on it. The kernel definition (in asm/sigcontext.h) has unified
284 * field names but otherwise the same layout.
285 */
286
287#define _fpstate_ia32 _fpstate_32
288#define sigcontext_ia32 sigcontext_32
289
290
291# ifdef __i386__
292struct sigcontext {
293 __u16 gs, __gsh;
294 __u16 fs, __fsh;
295 __u16 es, __esh;
296 __u16 ds, __dsh;
297 __u32 edi;
298 __u32 esi;
299 __u32 ebp;
300 __u32 esp;
301 __u32 ebx;
302 __u32 edx;
303 __u32 ecx;
304 __u32 eax;
305 __u32 trapno;
306 __u32 err;
307 __u32 eip;
308 __u16 cs, __csh;
309 __u32 eflags;
310 __u32 esp_at_signal;
311 __u16 ss, __ssh;
312 struct _fpstate *fpstate;
313 __u32 oldmask;
314 __u32 cr2;
315};
316# else /* __x86_64__: */
317struct sigcontext {
318 __u64 r8;
319 __u64 r9;
320 __u64 r10;
321 __u64 r11;
322 __u64 r12;
323 __u64 r13;
324 __u64 r14;
325 __u64 r15;
326 __u64 rdi;
327 __u64 rsi;
328 __u64 rbp;
329 __u64 rbx;
330 __u64 rdx;
331 __u64 rax;
332 __u64 rcx;
333 __u64 rsp;
334 __u64 rip;
335 __u64 eflags; /* RFLAGS */
336 __u16 cs;
337
338 /*
339 * Prior to 2.5.64 ("[PATCH] x86-64 updates for 2.5.64-bk3"),
340 * Linux saved and restored fs and gs in these slots. This
341 * was counterproductive, as fsbase and gsbase were never
342 * saved, so arch_prctl was presumably unreliable.
343 *
344 * These slots should never be reused without extreme caution:
345 *
346 * - Some DOSEMU versions stash fs and gs in these slots manually,
347 * thus overwriting anything the kernel expects to be preserved
348 * in these slots.
349 *
350 * - If these slots are ever needed for any other purpose,
351 * there is some risk that very old 64-bit binaries could get
352 * confused. I doubt that many such binaries still work,
353 * though, since the same patch in 2.5.64 also removed the
354 * 64-bit set_thread_area syscall, so it appears that there
355 * is no TLS API beyond modify_ldt that works in both pre-
356 * and post-2.5.64 kernels.
357 *
358 * If the kernel ever adds explicit fs, gs, fsbase, and gsbase
359 * save/restore, it will most likely need to be opt-in and use
360 * different context slots.
361 */
362 __u16 gs;
363 __u16 fs;
364 union {
365 __u16 ss; /* If UC_SIGCONTEXT_SS */
366 __u16 __pad0; /* Alias name for old (!UC_SIGCONTEXT_SS) user-space */
367 };
368 __u64 err;
369 __u64 trapno;
370 __u64 oldmask;
371 __u64 cr2;
372 struct _fpstate *fpstate; /* Zero when no FPU context */
373# ifdef __ILP32__
374 __u32 __fpstate_pad;
375# endif
376 __u64 reserved1[8];
377};
378# endif /* __x86_64__ */
379
380#endif /* _ASM_X86_SIGCONTEXT_H */
381

source code of include/x86_64-linux-gnu/asm/sigcontext.h