i387.c 17 KB

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