i387.c 17 KB

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