sigcontext_x86.h 9.5 KB

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