i387.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 <asm/sigcontext.h>
  12. #include <asm/processor.h>
  13. #include <asm/math_emu.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/i387.h>
  17. #include <asm/user.h>
  18. #ifdef CONFIG_X86_64
  19. # include <asm/sigcontext32.h>
  20. # include <asm/user32.h>
  21. #else
  22. # define save_i387_xstate_ia32 save_i387_xstate
  23. # define restore_i387_xstate_ia32 restore_i387_xstate
  24. # define _fpstate_ia32 _fpstate
  25. # define _xstate_ia32 _xstate
  26. # define sig_xstate_ia32_size sig_xstate_size
  27. # define fx_sw_reserved_ia32 fx_sw_reserved
  28. # define user_i387_ia32_struct user_i387_struct
  29. # define user32_fxsr_struct user_fxsr_struct
  30. #endif
  31. #ifdef CONFIG_MATH_EMULATION
  32. # define HAVE_HWFP (boot_cpu_data.hard_math)
  33. #else
  34. # define HAVE_HWFP 1
  35. #endif
  36. static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  37. unsigned int xstate_size;
  38. unsigned int sig_xstate_ia32_size = sizeof(struct _fpstate_ia32);
  39. static struct i387_fxsave_struct fx_scratch __cpuinitdata;
  40. void __cpuinit mxcsr_feature_mask_init(void)
  41. {
  42. unsigned long mask = 0;
  43. clts();
  44. if (cpu_has_fxsr) {
  45. memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
  46. asm volatile("fxsave %0" : : "m" (fx_scratch));
  47. mask = fx_scratch.mxcsr_mask;
  48. if (mask == 0)
  49. mask = 0x0000ffbf;
  50. }
  51. mxcsr_feature_mask &= mask;
  52. stts();
  53. }
  54. void __cpuinit init_thread_xstate(void)
  55. {
  56. if (!HAVE_HWFP) {
  57. xstate_size = sizeof(struct i387_soft_struct);
  58. return;
  59. }
  60. if (cpu_has_xsave) {
  61. xsave_cntxt_init();
  62. return;
  63. }
  64. if (cpu_has_fxsr)
  65. xstate_size = sizeof(struct i387_fxsave_struct);
  66. #ifdef CONFIG_X86_32
  67. else
  68. xstate_size = sizeof(struct i387_fsave_struct);
  69. #endif
  70. }
  71. #ifdef CONFIG_X86_64
  72. /*
  73. * Called at bootup to set up the initial FPU state that is later cloned
  74. * into all processes.
  75. */
  76. void __cpuinit fpu_init(void)
  77. {
  78. unsigned long oldcr0 = read_cr0();
  79. set_in_cr4(X86_CR4_OSFXSR);
  80. set_in_cr4(X86_CR4_OSXMMEXCPT);
  81. write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
  82. /*
  83. * Boot processor to setup the FP and extended state context info.
  84. */
  85. if (!smp_processor_id())
  86. init_thread_xstate();
  87. xsave_init();
  88. mxcsr_feature_mask_init();
  89. /* clean state in init */
  90. if (cpu_has_xsave)
  91. current_thread_info()->status = TS_XSAVE;
  92. else
  93. current_thread_info()->status = 0;
  94. clear_used_math();
  95. }
  96. #endif /* CONFIG_X86_64 */
  97. /*
  98. * The _current_ task is using the FPU for the first time
  99. * so initialize it and set the mxcsr to its default
  100. * value at reset if we support XMM instructions and then
  101. * remeber the current task has used the FPU.
  102. */
  103. int init_fpu(struct task_struct *tsk)
  104. {
  105. if (tsk_used_math(tsk)) {
  106. if (HAVE_HWFP && tsk == current)
  107. unlazy_fpu(tsk);
  108. return 0;
  109. }
  110. /*
  111. * Memory allocation at the first usage of the FPU and other state.
  112. */
  113. if (!tsk->thread.xstate) {
  114. tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  115. GFP_KERNEL);
  116. if (!tsk->thread.xstate)
  117. return -ENOMEM;
  118. }
  119. #ifdef CONFIG_X86_32
  120. if (!HAVE_HWFP) {
  121. memset(tsk->thread.xstate, 0, xstate_size);
  122. finit_task(tsk);
  123. set_stopped_child_used_math(tsk);
  124. return 0;
  125. }
  126. #endif
  127. if (cpu_has_fxsr) {
  128. struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
  129. memset(fx, 0, xstate_size);
  130. fx->cwd = 0x37f;
  131. if (cpu_has_xmm)
  132. fx->mxcsr = MXCSR_DEFAULT;
  133. } else {
  134. struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
  135. memset(fp, 0, xstate_size);
  136. fp->cwd = 0xffff037fu;
  137. fp->swd = 0xffff0000u;
  138. fp->twd = 0xffffffffu;
  139. fp->fos = 0xffff0000u;
  140. }
  141. /*
  142. * Only the device not available exception or ptrace can call init_fpu.
  143. */
  144. set_stopped_child_used_math(tsk);
  145. return 0;
  146. }
  147. /*
  148. * The xstateregs_active() routine is the same as the fpregs_active() routine,
  149. * as the "regset->n" for the xstate regset will be updated based on the feature
  150. * capabilites supported by the xsave.
  151. */
  152. int fpregs_active(struct task_struct *target, const struct user_regset *regset)
  153. {
  154. return tsk_used_math(target) ? regset->n : 0;
  155. }
  156. int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
  157. {
  158. return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
  159. }
  160. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  161. unsigned int pos, unsigned int count,
  162. void *kbuf, void __user *ubuf)
  163. {
  164. int ret;
  165. if (!cpu_has_fxsr)
  166. return -ENODEV;
  167. ret = init_fpu(target);
  168. if (ret)
  169. return ret;
  170. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  171. &target->thread.xstate->fxsave, 0, -1);
  172. }
  173. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  174. unsigned int pos, unsigned int count,
  175. const void *kbuf, const void __user *ubuf)
  176. {
  177. int ret;
  178. if (!cpu_has_fxsr)
  179. return -ENODEV;
  180. ret = init_fpu(target);
  181. if (ret)
  182. return ret;
  183. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  184. &target->thread.xstate->fxsave, 0, -1);
  185. /*
  186. * mxcsr reserved bits must be masked to zero for security reasons.
  187. */
  188. target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
  189. /*
  190. * update the header bits in the xsave header, indicating the
  191. * presence of FP and SSE state.
  192. */
  193. if (cpu_has_xsave)
  194. target->thread.xstate->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
  195. return ret;
  196. }
  197. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  198. unsigned int pos, unsigned int count,
  199. void *kbuf, void __user *ubuf)
  200. {
  201. int ret;
  202. if (!cpu_has_xsave)
  203. return -ENODEV;
  204. ret = init_fpu(target);
  205. if (ret)
  206. return ret;
  207. /*
  208. * Copy the 48bytes defined by the software first into the xstate
  209. * memory layout in the thread struct, so that we can copy the entire
  210. * xstateregs to the user using one user_regset_copyout().
  211. */
  212. memcpy(&target->thread.xstate->fxsave.sw_reserved,
  213. xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
  214. /*
  215. * Copy the xstate memory layout.
  216. */
  217. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  218. &target->thread.xstate->xsave, 0, -1);
  219. return ret;
  220. }
  221. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  222. unsigned int pos, unsigned int count,
  223. const void *kbuf, const void __user *ubuf)
  224. {
  225. int ret;
  226. struct xsave_hdr_struct *xsave_hdr;
  227. if (!cpu_has_xsave)
  228. return -ENODEV;
  229. ret = init_fpu(target);
  230. if (ret)
  231. return ret;
  232. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  233. &target->thread.xstate->xsave, 0, -1);
  234. /*
  235. * mxcsr reserved bits must be masked to zero for security reasons.
  236. */
  237. target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
  238. xsave_hdr = &target->thread.xstate->xsave.xsave_hdr;
  239. xsave_hdr->xstate_bv &= pcntxt_mask;
  240. /*
  241. * These bits must be zero.
  242. */
  243. xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
  244. return ret;
  245. }
  246. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  247. /*
  248. * FPU tag word conversions.
  249. */
  250. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  251. {
  252. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  253. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  254. tmp = ~twd;
  255. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  256. /* and move the valid bits to the lower byte. */
  257. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  258. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  259. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  260. return tmp;
  261. }
  262. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
  263. #define FP_EXP_TAG_VALID 0
  264. #define FP_EXP_TAG_ZERO 1
  265. #define FP_EXP_TAG_SPECIAL 2
  266. #define FP_EXP_TAG_EMPTY 3
  267. static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  268. {
  269. struct _fpxreg *st;
  270. u32 tos = (fxsave->swd >> 11) & 7;
  271. u32 twd = (unsigned long) fxsave->twd;
  272. u32 tag;
  273. u32 ret = 0xffff0000u;
  274. int i;
  275. for (i = 0; i < 8; i++, twd >>= 1) {
  276. if (twd & 0x1) {
  277. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  278. switch (st->exponent & 0x7fff) {
  279. case 0x7fff:
  280. tag = FP_EXP_TAG_SPECIAL;
  281. break;
  282. case 0x0000:
  283. if (!st->significand[0] &&
  284. !st->significand[1] &&
  285. !st->significand[2] &&
  286. !st->significand[3])
  287. tag = FP_EXP_TAG_ZERO;
  288. else
  289. tag = FP_EXP_TAG_SPECIAL;
  290. break;
  291. default:
  292. if (st->significand[3] & 0x8000)
  293. tag = FP_EXP_TAG_VALID;
  294. else
  295. tag = FP_EXP_TAG_SPECIAL;
  296. break;
  297. }
  298. } else {
  299. tag = FP_EXP_TAG_EMPTY;
  300. }
  301. ret |= tag << (2 * i);
  302. }
  303. return ret;
  304. }
  305. /*
  306. * FXSR floating point environment conversions.
  307. */
  308. static void
  309. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  310. {
  311. struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
  312. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  313. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  314. int i;
  315. env->cwd = fxsave->cwd | 0xffff0000u;
  316. env->swd = fxsave->swd | 0xffff0000u;
  317. env->twd = twd_fxsr_to_i387(fxsave);
  318. #ifdef CONFIG_X86_64
  319. env->fip = fxsave->rip;
  320. env->foo = fxsave->rdp;
  321. if (tsk == current) {
  322. /*
  323. * should be actually ds/cs at fpu exception time, but
  324. * that information is not available in 64bit mode.
  325. */
  326. asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
  327. asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
  328. } else {
  329. struct pt_regs *regs = task_pt_regs(tsk);
  330. env->fos = 0xffff0000 | tsk->thread.ds;
  331. env->fcs = regs->cs;
  332. }
  333. #else
  334. env->fip = fxsave->fip;
  335. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  336. env->foo = fxsave->foo;
  337. env->fos = fxsave->fos;
  338. #endif
  339. for (i = 0; i < 8; ++i)
  340. memcpy(&to[i], &from[i], sizeof(to[0]));
  341. }
  342. static void convert_to_fxsr(struct task_struct *tsk,
  343. const struct user_i387_ia32_struct *env)
  344. {
  345. struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
  346. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  347. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  348. int i;
  349. fxsave->cwd = env->cwd;
  350. fxsave->swd = env->swd;
  351. fxsave->twd = twd_i387_to_fxsr(env->twd);
  352. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  353. #ifdef CONFIG_X86_64
  354. fxsave->rip = env->fip;
  355. fxsave->rdp = env->foo;
  356. /* cs and ds ignored */
  357. #else
  358. fxsave->fip = env->fip;
  359. fxsave->fcs = (env->fcs & 0xffff);
  360. fxsave->foo = env->foo;
  361. fxsave->fos = env->fos;
  362. #endif
  363. for (i = 0; i < 8; ++i)
  364. memcpy(&to[i], &from[i], sizeof(from[0]));
  365. }
  366. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  367. unsigned int pos, unsigned int count,
  368. void *kbuf, void __user *ubuf)
  369. {
  370. struct user_i387_ia32_struct env;
  371. int ret;
  372. ret = init_fpu(target);
  373. if (ret)
  374. return ret;
  375. if (!HAVE_HWFP)
  376. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  377. if (!cpu_has_fxsr) {
  378. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  379. &target->thread.xstate->fsave, 0,
  380. -1);
  381. }
  382. if (kbuf && pos == 0 && count == sizeof(env)) {
  383. convert_from_fxsr(kbuf, target);
  384. return 0;
  385. }
  386. convert_from_fxsr(&env, target);
  387. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  388. }
  389. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  390. unsigned int pos, unsigned int count,
  391. const void *kbuf, const void __user *ubuf)
  392. {
  393. struct user_i387_ia32_struct env;
  394. int ret;
  395. ret = init_fpu(target);
  396. if (ret)
  397. return ret;
  398. if (!HAVE_HWFP)
  399. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  400. if (!cpu_has_fxsr) {
  401. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  402. &target->thread.xstate->fsave, 0, -1);
  403. }
  404. if (pos > 0 || count < sizeof(env))
  405. convert_from_fxsr(&env, target);
  406. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  407. if (!ret)
  408. convert_to_fxsr(target, &env);
  409. /*
  410. * update the header bit in the xsave header, indicating the
  411. * presence of FP.
  412. */
  413. if (cpu_has_xsave)
  414. target->thread.xstate->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
  415. return ret;
  416. }
  417. /*
  418. * Signal frame handlers.
  419. */
  420. static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
  421. {
  422. struct task_struct *tsk = current;
  423. struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
  424. fp->status = fp->swd;
  425. if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
  426. return -1;
  427. return 1;
  428. }
  429. static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
  430. {
  431. struct task_struct *tsk = current;
  432. struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
  433. struct user_i387_ia32_struct env;
  434. int err = 0;
  435. convert_from_fxsr(&env, tsk);
  436. if (__copy_to_user(buf, &env, sizeof(env)))
  437. return -1;
  438. err |= __put_user(fx->swd, &buf->status);
  439. err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
  440. if (err)
  441. return -1;
  442. if (__copy_to_user(&buf->_fxsr_env[0], fx, xstate_size))
  443. return -1;
  444. return 1;
  445. }
  446. static int save_i387_xsave(void __user *buf)
  447. {
  448. struct task_struct *tsk = current;
  449. struct _fpstate_ia32 __user *fx = buf;
  450. int err = 0;
  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.xstate->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.xstate->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.xstate->fxsave, &buf->_fxsr_env[0],
  513. size);
  514. /* mxcsr reserved bits must be masked to zero for security reasons */
  515. tsk->thread.xstate->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.xstate->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 */