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