sigcontext.h 9.7 KB

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