signal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * FPU signal frame handling routines.
  3. */
  4. #include <linux/compat.h>
  5. #include <linux/cpu.h>
  6. #include <asm/fpu/internal.h>
  7. #include <asm/fpu/signal.h>
  8. #include <asm/fpu/regset.h>
  9. #include <asm/fpu/xstate.h>
  10. #include <asm/sigframe.h>
  11. #include <asm/trace/fpu.h>
  12. static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
  13. /*
  14. * Check for the presence of extended state information in the
  15. * user fpstate pointer in the sigcontext.
  16. */
  17. static inline int check_for_xstate(struct fxregs_state __user *buf,
  18. void __user *fpstate,
  19. struct _fpx_sw_bytes *fx_sw)
  20. {
  21. int min_xstate_size = sizeof(struct fxregs_state) +
  22. sizeof(struct xstate_header);
  23. unsigned int magic2;
  24. if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
  25. return -1;
  26. /* Check for the first magic field and other error scenarios. */
  27. if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
  28. fx_sw->xstate_size < min_xstate_size ||
  29. fx_sw->xstate_size > fpu_user_xstate_size ||
  30. fx_sw->xstate_size > fx_sw->extended_size)
  31. return -1;
  32. /*
  33. * Check for the presence of second magic word at the end of memory
  34. * layout. This detects the case where the user just copied the legacy
  35. * fpstate layout with out copying the extended state information
  36. * in the memory layout.
  37. */
  38. if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
  39. || magic2 != FP_XSTATE_MAGIC2)
  40. return -1;
  41. return 0;
  42. }
  43. /*
  44. * Signal frame handlers.
  45. */
  46. static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
  47. {
  48. if (use_fxsr()) {
  49. struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
  50. struct user_i387_ia32_struct env;
  51. struct _fpstate_32 __user *fp = buf;
  52. convert_from_fxsr(&env, tsk);
  53. if (__copy_to_user(buf, &env, sizeof(env)) ||
  54. __put_user(xsave->i387.swd, &fp->status) ||
  55. __put_user(X86_FXSR_MAGIC, &fp->magic))
  56. return -1;
  57. } else {
  58. struct fregs_state __user *fp = buf;
  59. u32 swd;
  60. if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
  66. {
  67. struct xregs_state __user *x = buf;
  68. struct _fpx_sw_bytes *sw_bytes;
  69. u32 xfeatures;
  70. int err;
  71. /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
  72. sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved;
  73. err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes));
  74. if (!use_xsave())
  75. return err;
  76. err |= __put_user(FP_XSTATE_MAGIC2,
  77. (__u32 *)(buf + fpu_user_xstate_size));
  78. /*
  79. * Read the xfeatures which we copied (directly from the cpu or
  80. * from the state in task struct) to the user buffers.
  81. */
  82. err |= __get_user(xfeatures, (__u32 *)&x->header.xfeatures);
  83. /*
  84. * For legacy compatible, we always set FP/SSE bits in the bit
  85. * vector while saving the state to the user context. This will
  86. * enable us capturing any changes(during sigreturn) to
  87. * the FP/SSE bits by the legacy applications which don't touch
  88. * xfeatures in the xsave header.
  89. *
  90. * xsave aware apps can change the xfeatures in the xsave
  91. * header as well as change any contents in the memory layout.
  92. * xrestore as part of sigreturn will capture all the changes.
  93. */
  94. xfeatures |= XFEATURE_MASK_FPSSE;
  95. err |= __put_user(xfeatures, (__u32 *)&x->header.xfeatures);
  96. return err;
  97. }
  98. static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
  99. {
  100. int err;
  101. if (use_xsave())
  102. err = copy_xregs_to_user(buf);
  103. else if (use_fxsr())
  104. err = copy_fxregs_to_user((struct fxregs_state __user *) buf);
  105. else
  106. err = copy_fregs_to_user((struct fregs_state __user *) buf);
  107. if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
  108. err = -EFAULT;
  109. return err;
  110. }
  111. /*
  112. * Save the fpu, extended register state to the user signal frame.
  113. *
  114. * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
  115. * state is copied.
  116. * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
  117. *
  118. * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
  119. * buf != buf_fx for 32-bit frames with fxstate.
  120. *
  121. * If the fpu, extended register state is live, save the state directly
  122. * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise,
  123. * copy the thread's fpu state to the user frame starting at 'buf_fx'.
  124. *
  125. * If this is a 32-bit frame with fxstate, put a fsave header before
  126. * the aligned state at 'buf_fx'.
  127. *
  128. * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
  129. * indicating the absence/presence of the extended state to the user.
  130. */
  131. int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
  132. {
  133. struct fpu *fpu = &current->thread.fpu;
  134. struct xregs_state *xsave = &fpu->state.xsave;
  135. struct task_struct *tsk = current;
  136. int ia32_fxstate = (buf != buf_fx);
  137. ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
  138. IS_ENABLED(CONFIG_IA32_EMULATION));
  139. if (!access_ok(VERIFY_WRITE, buf, size))
  140. return -EACCES;
  141. if (!static_cpu_has(X86_FEATURE_FPU))
  142. return fpregs_soft_get(current, NULL, 0,
  143. sizeof(struct user_i387_ia32_struct), NULL,
  144. (struct _fpstate_32 __user *) buf) ? -1 : 1;
  145. if (fpu->initialized || using_compacted_format()) {
  146. /* Save the live register state to the user directly. */
  147. if (copy_fpregs_to_sigframe(buf_fx))
  148. return -1;
  149. /* Update the thread's fxstate to save the fsave header. */
  150. if (ia32_fxstate)
  151. copy_fxregs_to_kernel(fpu);
  152. } else {
  153. /*
  154. * It is a *bug* if kernel uses compacted-format for xsave
  155. * area and we copy it out directly to a signal frame. It
  156. * should have been handled above by saving the registers
  157. * directly.
  158. */
  159. if (boot_cpu_has(X86_FEATURE_XSAVES)) {
  160. WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
  161. return -1;
  162. }
  163. fpstate_sanitize_xstate(fpu);
  164. if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
  165. return -1;
  166. }
  167. /* Save the fsave header for the 32-bit frames. */
  168. if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
  169. return -1;
  170. if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
  171. return -1;
  172. return 0;
  173. }
  174. static inline void
  175. sanitize_restored_xstate(struct task_struct *tsk,
  176. struct user_i387_ia32_struct *ia32_env,
  177. u64 xfeatures, int fx_only)
  178. {
  179. struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
  180. struct xstate_header *header = &xsave->header;
  181. if (use_xsave()) {
  182. /*
  183. * Note: we don't need to zero the reserved bits in the
  184. * xstate_header here because we either didn't copy them at all,
  185. * or we checked earlier that they aren't set.
  186. */
  187. /*
  188. * Init the state that is not present in the memory
  189. * layout and not enabled by the OS.
  190. */
  191. if (fx_only)
  192. header->xfeatures = XFEATURE_MASK_FPSSE;
  193. else
  194. header->xfeatures &= xfeatures;
  195. }
  196. if (use_fxsr()) {
  197. /*
  198. * mscsr reserved bits must be masked to zero for security
  199. * reasons.
  200. */
  201. xsave->i387.mxcsr &= mxcsr_feature_mask;
  202. convert_to_fxsr(tsk, ia32_env);
  203. }
  204. }
  205. /*
  206. * Restore the extended state if present. Otherwise, restore the FP/SSE state.
  207. */
  208. static inline int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
  209. {
  210. if (use_xsave()) {
  211. if ((unsigned long)buf % 64 || fx_only) {
  212. u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
  213. copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
  214. return copy_user_to_fxregs(buf);
  215. } else {
  216. u64 init_bv = xfeatures_mask & ~xbv;
  217. if (unlikely(init_bv))
  218. copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
  219. return copy_user_to_xregs(buf, xbv);
  220. }
  221. } else if (use_fxsr()) {
  222. return copy_user_to_fxregs(buf);
  223. } else
  224. return copy_user_to_fregs(buf);
  225. }
  226. static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
  227. {
  228. int ia32_fxstate = (buf != buf_fx);
  229. struct task_struct *tsk = current;
  230. struct fpu *fpu = &tsk->thread.fpu;
  231. int state_size = fpu_kernel_xstate_size;
  232. u64 xfeatures = 0;
  233. int fx_only = 0;
  234. ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
  235. IS_ENABLED(CONFIG_IA32_EMULATION));
  236. if (!buf) {
  237. fpu__clear(fpu);
  238. return 0;
  239. }
  240. if (!access_ok(VERIFY_READ, buf, size))
  241. return -EACCES;
  242. fpu__initialize(fpu);
  243. if (!static_cpu_has(X86_FEATURE_FPU))
  244. return fpregs_soft_set(current, NULL,
  245. 0, sizeof(struct user_i387_ia32_struct),
  246. NULL, buf) != 0;
  247. if (use_xsave()) {
  248. struct _fpx_sw_bytes fx_sw_user;
  249. if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
  250. /*
  251. * Couldn't find the extended state information in the
  252. * memory layout. Restore just the FP/SSE and init all
  253. * the other extended state.
  254. */
  255. state_size = sizeof(struct fxregs_state);
  256. fx_only = 1;
  257. trace_x86_fpu_xstate_check_failed(fpu);
  258. } else {
  259. state_size = fx_sw_user.xstate_size;
  260. xfeatures = fx_sw_user.xfeatures;
  261. }
  262. }
  263. if (ia32_fxstate) {
  264. /*
  265. * For 32-bit frames with fxstate, copy the user state to the
  266. * thread's fpu state, reconstruct fxstate from the fsave
  267. * header. Validate and sanitize the copied state.
  268. */
  269. struct fpu *fpu = &tsk->thread.fpu;
  270. struct user_i387_ia32_struct env;
  271. int err = 0;
  272. /*
  273. * Drop the current fpu which clears fpu->initialized. This ensures
  274. * that any context-switch during the copy of the new state,
  275. * avoids the intermediate state from getting restored/saved.
  276. * Thus avoiding the new restored state from getting corrupted.
  277. * We will be ready to restore/save the state only after
  278. * fpu->initialized is again set.
  279. */
  280. fpu__drop(fpu);
  281. if (using_compacted_format()) {
  282. err = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
  283. } else {
  284. err = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
  285. if (!err && state_size > offsetof(struct xregs_state, header))
  286. err = validate_xstate_header(&fpu->state.xsave.header);
  287. }
  288. if (err || __copy_from_user(&env, buf, sizeof(env))) {
  289. fpstate_init(&fpu->state);
  290. trace_x86_fpu_init_state(fpu);
  291. err = -1;
  292. } else {
  293. sanitize_restored_xstate(tsk, &env, xfeatures, fx_only);
  294. }
  295. fpu->initialized = 1;
  296. preempt_disable();
  297. fpu__restore(fpu);
  298. preempt_enable();
  299. return err;
  300. } else {
  301. /*
  302. * For 64-bit frames and 32-bit fsave frames, restore the user
  303. * state to the registers directly (with exceptions handled).
  304. */
  305. user_fpu_begin();
  306. if (copy_user_to_fpregs_zeroing(buf_fx, xfeatures, fx_only)) {
  307. fpu__clear(fpu);
  308. return -1;
  309. }
  310. }
  311. return 0;
  312. }
  313. static inline int xstate_sigframe_size(void)
  314. {
  315. return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
  316. fpu_user_xstate_size;
  317. }
  318. /*
  319. * Restore FPU state from a sigframe:
  320. */
  321. int fpu__restore_sig(void __user *buf, int ia32_frame)
  322. {
  323. void __user *buf_fx = buf;
  324. int size = xstate_sigframe_size();
  325. if (ia32_frame && use_fxsr()) {
  326. buf_fx = buf + sizeof(struct fregs_state);
  327. size += sizeof(struct fregs_state);
  328. }
  329. return __fpu__restore_sig(buf, buf_fx, size);
  330. }
  331. unsigned long
  332. fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
  333. unsigned long *buf_fx, unsigned long *size)
  334. {
  335. unsigned long frame_size = xstate_sigframe_size();
  336. *buf_fx = sp = round_down(sp - frame_size, 64);
  337. if (ia32_frame && use_fxsr()) {
  338. frame_size += sizeof(struct fregs_state);
  339. sp -= sizeof(struct fregs_state);
  340. }
  341. *size = frame_size;
  342. return sp;
  343. }
  344. /*
  345. * Prepare the SW reserved portion of the fxsave memory layout, indicating
  346. * the presence of the extended state information in the memory layout
  347. * pointed by the fpstate pointer in the sigcontext.
  348. * This will be saved when ever the FP and extended state context is
  349. * saved on the user stack during the signal handler delivery to the user.
  350. */
  351. void fpu__init_prepare_fx_sw_frame(void)
  352. {
  353. int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
  354. fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
  355. fx_sw_reserved.extended_size = size;
  356. fx_sw_reserved.xfeatures = xfeatures_mask;
  357. fx_sw_reserved.xstate_size = fpu_user_xstate_size;
  358. if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
  359. IS_ENABLED(CONFIG_X86_32)) {
  360. int fsave_header_size = sizeof(struct fregs_state);
  361. fx_sw_reserved_ia32 = fx_sw_reserved;
  362. fx_sw_reserved_ia32.extended_size = size + fsave_header_size;
  363. }
  364. }