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