signal.c 11 KB

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