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