i387.c 17 KB

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