i387.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (C) 1994 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * General FPU state handling cleanups
  6. * Gareth Hughes <gareth@valinux.com>, May 2000
  7. */
  8. #include <linux/module.h>
  9. #include <linux/regset.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <asm/sigcontext.h>
  13. #include <asm/processor.h>
  14. #include <asm/math_emu.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/i387.h>
  18. #include <asm/user.h>
  19. #ifdef CONFIG_X86_64
  20. # include <asm/sigcontext32.h>
  21. # include <asm/user32.h>
  22. #else
  23. # define save_i387_xstate_ia32 save_i387_xstate
  24. # define restore_i387_xstate_ia32 restore_i387_xstate
  25. # define _fpstate_ia32 _fpstate
  26. # define _xstate_ia32 _xstate
  27. # define sig_xstate_ia32_size sig_xstate_size
  28. # define fx_sw_reserved_ia32 fx_sw_reserved
  29. # define user_i387_ia32_struct user_i387_struct
  30. # define user32_fxsr_struct user_fxsr_struct
  31. #endif
  32. /*
  33. * Were we in an interrupt that interrupted kernel mode?
  34. *
  35. * We can do a kernel_fpu_begin/end() pair *ONLY* if that
  36. * pair does nothing at all: the thread must not have fpu (so
  37. * that we don't try to save the FPU state), and TS must
  38. * be set (so that the clts/stts pair does nothing that is
  39. * visible in the interrupted kernel thread).
  40. */
  41. static inline bool interrupted_kernel_fpu_idle(void)
  42. {
  43. return !__thread_has_fpu(current) &&
  44. (read_cr0() & X86_CR0_TS);
  45. }
  46. /*
  47. * Were we in user mode (or vm86 mode) when we were
  48. * interrupted?
  49. *
  50. * Doing kernel_fpu_begin/end() is ok if we are running
  51. * in an interrupt context from user mode - we'll just
  52. * save the FPU state as required.
  53. */
  54. static inline bool interrupted_user_mode(void)
  55. {
  56. struct pt_regs *regs = get_irq_regs();
  57. return regs && user_mode_vm(regs);
  58. }
  59. /*
  60. * Can we use the FPU in kernel mode with the
  61. * whole "kernel_fpu_begin/end()" sequence?
  62. *
  63. * It's always ok in process context (ie "not interrupt")
  64. * but it is sometimes ok even from an irq.
  65. */
  66. bool irq_fpu_usable(void)
  67. {
  68. return !in_interrupt() ||
  69. interrupted_user_mode() ||
  70. interrupted_kernel_fpu_idle();
  71. }
  72. EXPORT_SYMBOL(irq_fpu_usable);
  73. void kernel_fpu_begin(void)
  74. {
  75. struct task_struct *me = current;
  76. WARN_ON_ONCE(!irq_fpu_usable());
  77. preempt_disable();
  78. if (__thread_has_fpu(me)) {
  79. __save_init_fpu(me);
  80. __thread_clear_has_fpu(me);
  81. /* We do 'stts()' in kernel_fpu_end() */
  82. } else {
  83. percpu_write(fpu_owner_task, NULL);
  84. clts();
  85. }
  86. }
  87. EXPORT_SYMBOL(kernel_fpu_begin);
  88. void kernel_fpu_end(void)
  89. {
  90. stts();
  91. preempt_enable();
  92. }
  93. EXPORT_SYMBOL(kernel_fpu_end);
  94. void unlazy_fpu(struct task_struct *tsk)
  95. {
  96. preempt_disable();
  97. if (__thread_has_fpu(tsk)) {
  98. __save_init_fpu(tsk);
  99. __thread_fpu_end(tsk);
  100. } else
  101. tsk->fpu_counter = 0;
  102. preempt_enable();
  103. }
  104. EXPORT_SYMBOL(unlazy_fpu);
  105. #ifdef CONFIG_MATH_EMULATION
  106. # define HAVE_HWFP (boot_cpu_data.hard_math)
  107. #else
  108. # define HAVE_HWFP 1
  109. #endif
  110. static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  111. unsigned int xstate_size;
  112. EXPORT_SYMBOL_GPL(xstate_size);
  113. unsigned int sig_xstate_ia32_size = sizeof(struct _fpstate_ia32);
  114. static struct i387_fxsave_struct fx_scratch __cpuinitdata;
  115. void __cpuinit mxcsr_feature_mask_init(void)
  116. {
  117. unsigned long mask = 0;
  118. clts();
  119. if (cpu_has_fxsr) {
  120. memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
  121. asm volatile("fxsave %0" : : "m" (fx_scratch));
  122. mask = fx_scratch.mxcsr_mask;
  123. if (mask == 0)
  124. mask = 0x0000ffbf;
  125. }
  126. mxcsr_feature_mask &= mask;
  127. stts();
  128. }
  129. static void __cpuinit init_thread_xstate(void)
  130. {
  131. /*
  132. * Note that xstate_size might be overwriten later during
  133. * xsave_init().
  134. */
  135. if (!HAVE_HWFP) {
  136. /*
  137. * Disable xsave as we do not support it if i387
  138. * emulation is enabled.
  139. */
  140. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  141. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  142. xstate_size = sizeof(struct i387_soft_struct);
  143. return;
  144. }
  145. if (cpu_has_fxsr)
  146. xstate_size = sizeof(struct i387_fxsave_struct);
  147. else
  148. xstate_size = sizeof(struct i387_fsave_struct);
  149. }
  150. /*
  151. * Called at bootup to set up the initial FPU state that is later cloned
  152. * into all processes.
  153. */
  154. void __cpuinit fpu_init(void)
  155. {
  156. unsigned long cr0;
  157. unsigned long cr4_mask = 0;
  158. if (cpu_has_fxsr)
  159. cr4_mask |= X86_CR4_OSFXSR;
  160. if (cpu_has_xmm)
  161. cr4_mask |= X86_CR4_OSXMMEXCPT;
  162. if (cr4_mask)
  163. set_in_cr4(cr4_mask);
  164. cr0 = read_cr0();
  165. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  166. if (!HAVE_HWFP)
  167. cr0 |= X86_CR0_EM;
  168. write_cr0(cr0);
  169. if (!smp_processor_id())
  170. init_thread_xstate();
  171. mxcsr_feature_mask_init();
  172. /* clean state in init */
  173. current_thread_info()->status = 0;
  174. clear_used_math();
  175. }
  176. void fpu_finit(struct fpu *fpu)
  177. {
  178. if (!HAVE_HWFP) {
  179. finit_soft_fpu(&fpu->state->soft);
  180. return;
  181. }
  182. if (cpu_has_fxsr) {
  183. struct i387_fxsave_struct *fx = &fpu->state->fxsave;
  184. memset(fx, 0, xstate_size);
  185. fx->cwd = 0x37f;
  186. if (cpu_has_xmm)
  187. fx->mxcsr = MXCSR_DEFAULT;
  188. } else {
  189. struct i387_fsave_struct *fp = &fpu->state->fsave;
  190. memset(fp, 0, xstate_size);
  191. fp->cwd = 0xffff037fu;
  192. fp->swd = 0xffff0000u;
  193. fp->twd = 0xffffffffu;
  194. fp->fos = 0xffff0000u;
  195. }
  196. }
  197. EXPORT_SYMBOL_GPL(fpu_finit);
  198. /*
  199. * The _current_ task is using the FPU for the first time
  200. * so initialize it and set the mxcsr to its default
  201. * value at reset if we support XMM instructions and then
  202. * remember the current task has used the FPU.
  203. */
  204. int init_fpu(struct task_struct *tsk)
  205. {
  206. int ret;
  207. if (tsk_used_math(tsk)) {
  208. if (HAVE_HWFP && tsk == current)
  209. unlazy_fpu(tsk);
  210. return 0;
  211. }
  212. /*
  213. * Memory allocation at the first usage of the FPU and other state.
  214. */
  215. ret = fpu_alloc(&tsk->thread.fpu);
  216. if (ret)
  217. return ret;
  218. fpu_finit(&tsk->thread.fpu);
  219. set_stopped_child_used_math(tsk);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(init_fpu);
  223. /*
  224. * The xstateregs_active() routine is the same as the fpregs_active() routine,
  225. * as the "regset->n" for the xstate regset will be updated based on the feature
  226. * capabilites supported by the xsave.
  227. */
  228. int fpregs_active(struct task_struct *target, const struct user_regset *regset)
  229. {
  230. return tsk_used_math(target) ? regset->n : 0;
  231. }
  232. int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
  233. {
  234. return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
  235. }
  236. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  237. unsigned int pos, unsigned int count,
  238. void *kbuf, void __user *ubuf)
  239. {
  240. int ret;
  241. if (!cpu_has_fxsr)
  242. return -ENODEV;
  243. ret = init_fpu(target);
  244. if (ret)
  245. return ret;
  246. sanitize_i387_state(target);
  247. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  248. &target->thread.fpu.state->fxsave, 0, -1);
  249. }
  250. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  251. unsigned int pos, unsigned int count,
  252. const void *kbuf, const void __user *ubuf)
  253. {
  254. int ret;
  255. if (!cpu_has_fxsr)
  256. return -ENODEV;
  257. ret = init_fpu(target);
  258. if (ret)
  259. return ret;
  260. sanitize_i387_state(target);
  261. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  262. &target->thread.fpu.state->fxsave, 0, -1);
  263. /*
  264. * mxcsr reserved bits must be masked to zero for security reasons.
  265. */
  266. target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  267. /*
  268. * update the header bits in the xsave header, indicating the
  269. * presence of FP and SSE state.
  270. */
  271. if (cpu_has_xsave)
  272. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
  273. return ret;
  274. }
  275. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  276. unsigned int pos, unsigned int count,
  277. void *kbuf, void __user *ubuf)
  278. {
  279. int ret;
  280. if (!cpu_has_xsave)
  281. return -ENODEV;
  282. ret = init_fpu(target);
  283. if (ret)
  284. return ret;
  285. /*
  286. * Copy the 48bytes defined by the software first into the xstate
  287. * memory layout in the thread struct, so that we can copy the entire
  288. * xstateregs to the user using one user_regset_copyout().
  289. */
  290. memcpy(&target->thread.fpu.state->fxsave.sw_reserved,
  291. xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
  292. /*
  293. * Copy the xstate memory layout.
  294. */
  295. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  296. &target->thread.fpu.state->xsave, 0, -1);
  297. return ret;
  298. }
  299. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  300. unsigned int pos, unsigned int count,
  301. const void *kbuf, const void __user *ubuf)
  302. {
  303. int ret;
  304. struct xsave_hdr_struct *xsave_hdr;
  305. if (!cpu_has_xsave)
  306. return -ENODEV;
  307. ret = init_fpu(target);
  308. if (ret)
  309. return ret;
  310. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  311. &target->thread.fpu.state->xsave, 0, -1);
  312. /*
  313. * mxcsr reserved bits must be masked to zero for security reasons.
  314. */
  315. target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  316. xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr;
  317. xsave_hdr->xstate_bv &= pcntxt_mask;
  318. /*
  319. * These bits must be zero.
  320. */
  321. xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
  322. return ret;
  323. }
  324. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  325. /*
  326. * FPU tag word conversions.
  327. */
  328. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  329. {
  330. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  331. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  332. tmp = ~twd;
  333. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  334. /* and move the valid bits to the lower byte. */
  335. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  336. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  337. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  338. return tmp;
  339. }
  340. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  341. #define FP_EXP_TAG_VALID 0
  342. #define FP_EXP_TAG_ZERO 1
  343. #define FP_EXP_TAG_SPECIAL 2
  344. #define FP_EXP_TAG_EMPTY 3
  345. static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  346. {
  347. struct _fpxreg *st;
  348. u32 tos = (fxsave->swd >> 11) & 7;
  349. u32 twd = (unsigned long) fxsave->twd;
  350. u32 tag;
  351. u32 ret = 0xffff0000u;
  352. int i;
  353. for (i = 0; i < 8; i++, twd >>= 1) {
  354. if (twd & 0x1) {
  355. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  356. switch (st->exponent & 0x7fff) {
  357. case 0x7fff:
  358. tag = FP_EXP_TAG_SPECIAL;
  359. break;
  360. case 0x0000:
  361. if (!st->significand[0] &&
  362. !st->significand[1] &&
  363. !st->significand[2] &&
  364. !st->significand[3])
  365. tag = FP_EXP_TAG_ZERO;
  366. else
  367. tag = FP_EXP_TAG_SPECIAL;
  368. break;
  369. default:
  370. if (st->significand[3] & 0x8000)
  371. tag = FP_EXP_TAG_VALID;
  372. else
  373. tag = FP_EXP_TAG_SPECIAL;
  374. break;
  375. }
  376. } else {
  377. tag = FP_EXP_TAG_EMPTY;
  378. }
  379. ret |= tag << (2 * i);
  380. }
  381. return ret;
  382. }
  383. /*
  384. * FXSR floating point environment conversions.
  385. */
  386. static void
  387. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  388. {
  389. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  390. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  391. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  392. int i;
  393. env->cwd = fxsave->cwd | 0xffff0000u;
  394. env->swd = fxsave->swd | 0xffff0000u;
  395. env->twd = twd_fxsr_to_i387(fxsave);
  396. #ifdef CONFIG_X86_64
  397. env->fip = fxsave->rip;
  398. env->foo = fxsave->rdp;
  399. /*
  400. * should be actually ds/cs at fpu exception time, but
  401. * that information is not available in 64bit mode.
  402. */
  403. env->fcs = task_pt_regs(tsk)->cs;
  404. if (tsk == current) {
  405. savesegment(ds, env->fos);
  406. } else {
  407. env->fos = tsk->thread.ds;
  408. }
  409. env->fos |= 0xffff0000;
  410. #else
  411. env->fip = fxsave->fip;
  412. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  413. env->foo = fxsave->foo;
  414. env->fos = fxsave->fos;
  415. #endif
  416. for (i = 0; i < 8; ++i)
  417. memcpy(&to[i], &from[i], sizeof(to[0]));
  418. }
  419. static void convert_to_fxsr(struct task_struct *tsk,
  420. const struct user_i387_ia32_struct *env)
  421. {
  422. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  423. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  424. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  425. int i;
  426. fxsave->cwd = env->cwd;
  427. fxsave->swd = env->swd;
  428. fxsave->twd = twd_i387_to_fxsr(env->twd);
  429. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  430. #ifdef CONFIG_X86_64
  431. fxsave->rip = env->fip;
  432. fxsave->rdp = env->foo;
  433. /* cs and ds ignored */
  434. #else
  435. fxsave->fip = env->fip;
  436. fxsave->fcs = (env->fcs & 0xffff);
  437. fxsave->foo = env->foo;
  438. fxsave->fos = env->fos;
  439. #endif
  440. for (i = 0; i < 8; ++i)
  441. memcpy(&to[i], &from[i], sizeof(from[0]));
  442. }
  443. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  444. unsigned int pos, unsigned int count,
  445. void *kbuf, void __user *ubuf)
  446. {
  447. struct user_i387_ia32_struct env;
  448. int ret;
  449. ret = init_fpu(target);
  450. if (ret)
  451. return ret;
  452. if (!HAVE_HWFP)
  453. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  454. if (!cpu_has_fxsr) {
  455. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  456. &target->thread.fpu.state->fsave, 0,
  457. -1);
  458. }
  459. sanitize_i387_state(target);
  460. if (kbuf && pos == 0 && count == sizeof(env)) {
  461. convert_from_fxsr(kbuf, target);
  462. return 0;
  463. }
  464. convert_from_fxsr(&env, target);
  465. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  466. }
  467. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  468. unsigned int pos, unsigned int count,
  469. const void *kbuf, const void __user *ubuf)
  470. {
  471. struct user_i387_ia32_struct env;
  472. int ret;
  473. ret = init_fpu(target);
  474. if (ret)
  475. return ret;
  476. sanitize_i387_state(target);
  477. if (!HAVE_HWFP)
  478. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  479. if (!cpu_has_fxsr) {
  480. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  481. &target->thread.fpu.state->fsave, 0, -1);
  482. }
  483. if (pos > 0 || count < sizeof(env))
  484. convert_from_fxsr(&env, target);
  485. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  486. if (!ret)
  487. convert_to_fxsr(target, &env);
  488. /*
  489. * update the header bit in the xsave header, indicating the
  490. * presence of FP.
  491. */
  492. if (cpu_has_xsave)
  493. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
  494. return ret;
  495. }
  496. /*
  497. * Signal frame handlers.
  498. */
  499. static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
  500. {
  501. struct task_struct *tsk = current;
  502. struct i387_fsave_struct *fp = &tsk->thread.fpu.state->fsave;
  503. fp->status = fp->swd;
  504. if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
  505. return -1;
  506. return 1;
  507. }
  508. static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
  509. {
  510. struct task_struct *tsk = current;
  511. struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
  512. struct user_i387_ia32_struct env;
  513. int err = 0;
  514. convert_from_fxsr(&env, tsk);
  515. if (__copy_to_user(buf, &env, sizeof(env)))
  516. return -1;
  517. err |= __put_user(fx->swd, &buf->status);
  518. err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
  519. if (err)
  520. return -1;
  521. if (__copy_to_user(&buf->_fxsr_env[0], fx, xstate_size))
  522. return -1;
  523. return 1;
  524. }
  525. static int save_i387_xsave(void __user *buf)
  526. {
  527. struct task_struct *tsk = current;
  528. struct _fpstate_ia32 __user *fx = buf;
  529. int err = 0;
  530. sanitize_i387_state(tsk);
  531. /*
  532. * For legacy compatible, we always set FP/SSE bits in the bit
  533. * vector while saving the state to the user context.
  534. * This will enable us capturing any changes(during sigreturn) to
  535. * the FP/SSE bits by the legacy applications which don't touch
  536. * xstate_bv in the xsave header.
  537. *
  538. * xsave aware applications can change the xstate_bv in the xsave
  539. * header as well as change any contents in the memory layout.
  540. * xrestore as part of sigreturn will capture all the changes.
  541. */
  542. tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
  543. if (save_i387_fxsave(fx) < 0)
  544. return -1;
  545. err = __copy_to_user(&fx->sw_reserved, &fx_sw_reserved_ia32,
  546. sizeof(struct _fpx_sw_bytes));
  547. err |= __put_user(FP_XSTATE_MAGIC2,
  548. (__u32 __user *) (buf + sig_xstate_ia32_size
  549. - FP_XSTATE_MAGIC2_SIZE));
  550. if (err)
  551. return -1;
  552. return 1;
  553. }
  554. int save_i387_xstate_ia32(void __user *buf)
  555. {
  556. struct _fpstate_ia32 __user *fp = (struct _fpstate_ia32 __user *) buf;
  557. struct task_struct *tsk = current;
  558. if (!used_math())
  559. return 0;
  560. if (!access_ok(VERIFY_WRITE, buf, sig_xstate_ia32_size))
  561. return -EACCES;
  562. /*
  563. * This will cause a "finit" to be triggered by the next
  564. * attempted FPU operation by the 'current' process.
  565. */
  566. clear_used_math();
  567. if (!HAVE_HWFP) {
  568. return fpregs_soft_get(current, NULL,
  569. 0, sizeof(struct user_i387_ia32_struct),
  570. NULL, fp) ? -1 : 1;
  571. }
  572. unlazy_fpu(tsk);
  573. if (cpu_has_xsave)
  574. return save_i387_xsave(fp);
  575. if (cpu_has_fxsr)
  576. return save_i387_fxsave(fp);
  577. else
  578. return save_i387_fsave(fp);
  579. }
  580. static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
  581. {
  582. struct task_struct *tsk = current;
  583. return __copy_from_user(&tsk->thread.fpu.state->fsave, buf,
  584. sizeof(struct i387_fsave_struct));
  585. }
  586. static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf,
  587. unsigned int size)
  588. {
  589. struct task_struct *tsk = current;
  590. struct user_i387_ia32_struct env;
  591. int err;
  592. err = __copy_from_user(&tsk->thread.fpu.state->fxsave, &buf->_fxsr_env[0],
  593. size);
  594. /* mxcsr reserved bits must be masked to zero for security reasons */
  595. tsk->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  596. if (err || __copy_from_user(&env, buf, sizeof(env)))
  597. return 1;
  598. convert_to_fxsr(tsk, &env);
  599. return 0;
  600. }
  601. static int restore_i387_xsave(void __user *buf)
  602. {
  603. struct _fpx_sw_bytes fx_sw_user;
  604. struct _fpstate_ia32 __user *fx_user =
  605. ((struct _fpstate_ia32 __user *) buf);
  606. struct i387_fxsave_struct __user *fx =
  607. (struct i387_fxsave_struct __user *) &fx_user->_fxsr_env[0];
  608. struct xsave_hdr_struct *xsave_hdr =
  609. &current->thread.fpu.state->xsave.xsave_hdr;
  610. u64 mask;
  611. int err;
  612. if (check_for_xstate(fx, buf, &fx_sw_user))
  613. goto fx_only;
  614. mask = fx_sw_user.xstate_bv;
  615. err = restore_i387_fxsave(buf, fx_sw_user.xstate_size);
  616. xsave_hdr->xstate_bv &= pcntxt_mask;
  617. /*
  618. * These bits must be zero.
  619. */
  620. xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
  621. /*
  622. * Init the state that is not present in the memory layout
  623. * and enabled by the OS.
  624. */
  625. mask = ~(pcntxt_mask & ~mask);
  626. xsave_hdr->xstate_bv &= mask;
  627. return err;
  628. fx_only:
  629. /*
  630. * Couldn't find the extended state information in the memory
  631. * layout. Restore the FP/SSE and init the other extended state
  632. * enabled by the OS.
  633. */
  634. xsave_hdr->xstate_bv = XSTATE_FPSSE;
  635. return restore_i387_fxsave(buf, sizeof(struct i387_fxsave_struct));
  636. }
  637. int restore_i387_xstate_ia32(void __user *buf)
  638. {
  639. int err;
  640. struct task_struct *tsk = current;
  641. struct _fpstate_ia32 __user *fp = (struct _fpstate_ia32 __user *) buf;
  642. if (HAVE_HWFP)
  643. clear_fpu(tsk);
  644. if (!buf) {
  645. if (used_math()) {
  646. clear_fpu(tsk);
  647. clear_used_math();
  648. }
  649. return 0;
  650. } else
  651. if (!access_ok(VERIFY_READ, buf, sig_xstate_ia32_size))
  652. return -EACCES;
  653. if (!used_math()) {
  654. err = init_fpu(tsk);
  655. if (err)
  656. return err;
  657. }
  658. if (HAVE_HWFP) {
  659. if (cpu_has_xsave)
  660. err = restore_i387_xsave(buf);
  661. else if (cpu_has_fxsr)
  662. err = restore_i387_fxsave(fp, sizeof(struct
  663. i387_fxsave_struct));
  664. else
  665. err = restore_i387_fsave(fp);
  666. } else {
  667. err = fpregs_soft_set(current, NULL,
  668. 0, sizeof(struct user_i387_ia32_struct),
  669. NULL, fp) != 0;
  670. }
  671. set_used_math();
  672. return err;
  673. }
  674. /*
  675. * FPU state for core dumps.
  676. * This is only used for a.out dumps now.
  677. * It is declared generically using elf_fpregset_t (which is
  678. * struct user_i387_struct) but is in fact only used for 32-bit
  679. * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
  680. */
  681. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
  682. {
  683. struct task_struct *tsk = current;
  684. int fpvalid;
  685. fpvalid = !!used_math();
  686. if (fpvalid)
  687. fpvalid = !fpregs_get(tsk, NULL,
  688. 0, sizeof(struct user_i387_ia32_struct),
  689. fpu, NULL);
  690. return fpvalid;
  691. }
  692. EXPORT_SYMBOL(dump_fpu);
  693. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */