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