i387.c 17 KB

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