i387.c 17 KB

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