i387.c 17 KB

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