signal.c 12 KB

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