i387.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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/fpu-internal.h>
  19. #include <asm/user.h>
  20. /*
  21. * Were we in an interrupt that interrupted kernel mode?
  22. *
  23. * For now, with eagerfpu we will return interrupted kernel FPU
  24. * state as not-idle. TBD: Ideally we can change the return value
  25. * to something like __thread_has_fpu(current). But we need to
  26. * be careful of doing __thread_clear_has_fpu() before saving
  27. * the FPU etc for supporting nested uses etc. For now, take
  28. * the simple route!
  29. *
  30. * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
  31. * pair does nothing at all: the thread must not have fpu (so
  32. * that we don't try to save the FPU state), and TS must
  33. * be set (so that the clts/stts pair does nothing that is
  34. * visible in the interrupted kernel thread).
  35. */
  36. static inline bool interrupted_kernel_fpu_idle(void)
  37. {
  38. if (use_eager_fpu())
  39. return 0;
  40. return !__thread_has_fpu(current) &&
  41. (read_cr0() & X86_CR0_TS);
  42. }
  43. /*
  44. * Were we in user mode (or vm86 mode) when we were
  45. * interrupted?
  46. *
  47. * Doing kernel_fpu_begin/end() is ok if we are running
  48. * in an interrupt context from user mode - we'll just
  49. * save the FPU state as required.
  50. */
  51. static inline bool interrupted_user_mode(void)
  52. {
  53. struct pt_regs *regs = get_irq_regs();
  54. return regs && user_mode_vm(regs);
  55. }
  56. /*
  57. * Can we use the FPU in kernel mode with the
  58. * whole "kernel_fpu_begin/end()" sequence?
  59. *
  60. * It's always ok in process context (ie "not interrupt")
  61. * but it is sometimes ok even from an irq.
  62. */
  63. bool irq_fpu_usable(void)
  64. {
  65. return !in_interrupt() ||
  66. interrupted_user_mode() ||
  67. interrupted_kernel_fpu_idle();
  68. }
  69. EXPORT_SYMBOL(irq_fpu_usable);
  70. void __kernel_fpu_begin(void)
  71. {
  72. struct task_struct *me = current;
  73. if (__thread_has_fpu(me)) {
  74. __save_init_fpu(me);
  75. __thread_clear_has_fpu(me);
  76. /* We do 'stts()' in __kernel_fpu_end() */
  77. } else if (!use_eager_fpu()) {
  78. this_cpu_write(fpu_owner_task, NULL);
  79. clts();
  80. }
  81. }
  82. EXPORT_SYMBOL(__kernel_fpu_begin);
  83. void __kernel_fpu_end(void)
  84. {
  85. if (use_eager_fpu())
  86. math_state_restore();
  87. else
  88. stts();
  89. }
  90. EXPORT_SYMBOL(__kernel_fpu_end);
  91. void unlazy_fpu(struct task_struct *tsk)
  92. {
  93. preempt_disable();
  94. if (__thread_has_fpu(tsk)) {
  95. __save_init_fpu(tsk);
  96. __thread_fpu_end(tsk);
  97. } else
  98. tsk->fpu_counter = 0;
  99. preempt_enable();
  100. }
  101. EXPORT_SYMBOL(unlazy_fpu);
  102. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  103. unsigned int xstate_size;
  104. EXPORT_SYMBOL_GPL(xstate_size);
  105. static struct i387_fxsave_struct fx_scratch __cpuinitdata;
  106. static void __cpuinit mxcsr_feature_mask_init(void)
  107. {
  108. unsigned long mask = 0;
  109. if (cpu_has_fxsr) {
  110. memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
  111. asm volatile("fxsave %0" : : "m" (fx_scratch));
  112. mask = fx_scratch.mxcsr_mask;
  113. if (mask == 0)
  114. mask = 0x0000ffbf;
  115. }
  116. mxcsr_feature_mask &= mask;
  117. }
  118. static void __cpuinit init_thread_xstate(void)
  119. {
  120. /*
  121. * Note that xstate_size might be overwriten later during
  122. * xsave_init().
  123. */
  124. if (!HAVE_HWFP) {
  125. /*
  126. * Disable xsave as we do not support it if i387
  127. * emulation is enabled.
  128. */
  129. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  130. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  131. xstate_size = sizeof(struct i387_soft_struct);
  132. return;
  133. }
  134. if (cpu_has_fxsr)
  135. xstate_size = sizeof(struct i387_fxsave_struct);
  136. else
  137. xstate_size = sizeof(struct i387_fsave_struct);
  138. }
  139. /*
  140. * Called at bootup to set up the initial FPU state that is later cloned
  141. * into all processes.
  142. */
  143. void __cpuinit fpu_init(void)
  144. {
  145. unsigned long cr0;
  146. unsigned long cr4_mask = 0;
  147. if (cpu_has_fxsr)
  148. cr4_mask |= X86_CR4_OSFXSR;
  149. if (cpu_has_xmm)
  150. cr4_mask |= X86_CR4_OSXMMEXCPT;
  151. if (cr4_mask)
  152. set_in_cr4(cr4_mask);
  153. cr0 = read_cr0();
  154. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  155. if (!HAVE_HWFP)
  156. cr0 |= X86_CR0_EM;
  157. write_cr0(cr0);
  158. /*
  159. * init_thread_xstate is only called once to avoid overriding
  160. * xstate_size during boot time or during CPU hotplug.
  161. */
  162. if (xstate_size == 0)
  163. init_thread_xstate();
  164. mxcsr_feature_mask_init();
  165. xsave_init();
  166. eager_fpu_init();
  167. }
  168. void fpu_finit(struct fpu *fpu)
  169. {
  170. if (!HAVE_HWFP) {
  171. finit_soft_fpu(&fpu->state->soft);
  172. return;
  173. }
  174. if (cpu_has_fxsr) {
  175. fx_finit(&fpu->state->fxsave);
  176. } else {
  177. struct i387_fsave_struct *fp = &fpu->state->fsave;
  178. memset(fp, 0, xstate_size);
  179. fp->cwd = 0xffff037fu;
  180. fp->swd = 0xffff0000u;
  181. fp->twd = 0xffffffffu;
  182. fp->fos = 0xffff0000u;
  183. }
  184. }
  185. EXPORT_SYMBOL_GPL(fpu_finit);
  186. /*
  187. * The _current_ task is using the FPU for the first time
  188. * so initialize it and set the mxcsr to its default
  189. * value at reset if we support XMM instructions and then
  190. * remember the current task has used the FPU.
  191. */
  192. int init_fpu(struct task_struct *tsk)
  193. {
  194. int ret;
  195. if (tsk_used_math(tsk)) {
  196. if (HAVE_HWFP && tsk == current)
  197. unlazy_fpu(tsk);
  198. tsk->thread.fpu.last_cpu = ~0;
  199. return 0;
  200. }
  201. /*
  202. * Memory allocation at the first usage of the FPU and other state.
  203. */
  204. ret = fpu_alloc(&tsk->thread.fpu);
  205. if (ret)
  206. return ret;
  207. fpu_finit(&tsk->thread.fpu);
  208. set_stopped_child_used_math(tsk);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(init_fpu);
  212. /*
  213. * The xstateregs_active() routine is the same as the fpregs_active() routine,
  214. * as the "regset->n" for the xstate regset will be updated based on the feature
  215. * capabilites supported by the xsave.
  216. */
  217. int fpregs_active(struct task_struct *target, const struct user_regset *regset)
  218. {
  219. return tsk_used_math(target) ? regset->n : 0;
  220. }
  221. int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
  222. {
  223. return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
  224. }
  225. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  226. unsigned int pos, unsigned int count,
  227. void *kbuf, void __user *ubuf)
  228. {
  229. int ret;
  230. if (!cpu_has_fxsr)
  231. return -ENODEV;
  232. ret = init_fpu(target);
  233. if (ret)
  234. return ret;
  235. sanitize_i387_state(target);
  236. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  237. &target->thread.fpu.state->fxsave, 0, -1);
  238. }
  239. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  240. unsigned int pos, unsigned int count,
  241. const void *kbuf, const void __user *ubuf)
  242. {
  243. int ret;
  244. if (!cpu_has_fxsr)
  245. return -ENODEV;
  246. ret = init_fpu(target);
  247. if (ret)
  248. return ret;
  249. sanitize_i387_state(target);
  250. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  251. &target->thread.fpu.state->fxsave, 0, -1);
  252. /*
  253. * mxcsr reserved bits must be masked to zero for security reasons.
  254. */
  255. target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  256. /*
  257. * update the header bits in the xsave header, indicating the
  258. * presence of FP and SSE state.
  259. */
  260. if (cpu_has_xsave)
  261. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
  262. return ret;
  263. }
  264. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  265. unsigned int pos, unsigned int count,
  266. void *kbuf, void __user *ubuf)
  267. {
  268. int ret;
  269. if (!cpu_has_xsave)
  270. return -ENODEV;
  271. ret = init_fpu(target);
  272. if (ret)
  273. return ret;
  274. /*
  275. * Copy the 48bytes defined by the software first into the xstate
  276. * memory layout in the thread struct, so that we can copy the entire
  277. * xstateregs to the user using one user_regset_copyout().
  278. */
  279. memcpy(&target->thread.fpu.state->fxsave.sw_reserved,
  280. xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
  281. /*
  282. * Copy the xstate memory layout.
  283. */
  284. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  285. &target->thread.fpu.state->xsave, 0, -1);
  286. return ret;
  287. }
  288. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  289. unsigned int pos, unsigned int count,
  290. const void *kbuf, const void __user *ubuf)
  291. {
  292. int ret;
  293. struct xsave_hdr_struct *xsave_hdr;
  294. if (!cpu_has_xsave)
  295. return -ENODEV;
  296. ret = init_fpu(target);
  297. if (ret)
  298. return ret;
  299. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  300. &target->thread.fpu.state->xsave, 0, -1);
  301. /*
  302. * mxcsr reserved bits must be masked to zero for security reasons.
  303. */
  304. target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  305. xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr;
  306. xsave_hdr->xstate_bv &= pcntxt_mask;
  307. /*
  308. * These bits must be zero.
  309. */
  310. xsave_hdr->reserved1[0] = xsave_hdr->reserved1[1] = 0;
  311. return ret;
  312. }
  313. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  314. /*
  315. * FPU tag word conversions.
  316. */
  317. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  318. {
  319. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  320. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  321. tmp = ~twd;
  322. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  323. /* and move the valid bits to the lower byte. */
  324. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  325. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  326. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  327. return tmp;
  328. }
  329. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  330. #define FP_EXP_TAG_VALID 0
  331. #define FP_EXP_TAG_ZERO 1
  332. #define FP_EXP_TAG_SPECIAL 2
  333. #define FP_EXP_TAG_EMPTY 3
  334. static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  335. {
  336. struct _fpxreg *st;
  337. u32 tos = (fxsave->swd >> 11) & 7;
  338. u32 twd = (unsigned long) fxsave->twd;
  339. u32 tag;
  340. u32 ret = 0xffff0000u;
  341. int i;
  342. for (i = 0; i < 8; i++, twd >>= 1) {
  343. if (twd & 0x1) {
  344. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  345. switch (st->exponent & 0x7fff) {
  346. case 0x7fff:
  347. tag = FP_EXP_TAG_SPECIAL;
  348. break;
  349. case 0x0000:
  350. if (!st->significand[0] &&
  351. !st->significand[1] &&
  352. !st->significand[2] &&
  353. !st->significand[3])
  354. tag = FP_EXP_TAG_ZERO;
  355. else
  356. tag = FP_EXP_TAG_SPECIAL;
  357. break;
  358. default:
  359. if (st->significand[3] & 0x8000)
  360. tag = FP_EXP_TAG_VALID;
  361. else
  362. tag = FP_EXP_TAG_SPECIAL;
  363. break;
  364. }
  365. } else {
  366. tag = FP_EXP_TAG_EMPTY;
  367. }
  368. ret |= tag << (2 * i);
  369. }
  370. return ret;
  371. }
  372. /*
  373. * FXSR floating point environment conversions.
  374. */
  375. void
  376. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  377. {
  378. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  379. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  380. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  381. int i;
  382. env->cwd = fxsave->cwd | 0xffff0000u;
  383. env->swd = fxsave->swd | 0xffff0000u;
  384. env->twd = twd_fxsr_to_i387(fxsave);
  385. #ifdef CONFIG_X86_64
  386. env->fip = fxsave->rip;
  387. env->foo = fxsave->rdp;
  388. /*
  389. * should be actually ds/cs at fpu exception time, but
  390. * that information is not available in 64bit mode.
  391. */
  392. env->fcs = task_pt_regs(tsk)->cs;
  393. if (tsk == current) {
  394. savesegment(ds, env->fos);
  395. } else {
  396. env->fos = tsk->thread.ds;
  397. }
  398. env->fos |= 0xffff0000;
  399. #else
  400. env->fip = fxsave->fip;
  401. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  402. env->foo = fxsave->foo;
  403. env->fos = fxsave->fos;
  404. #endif
  405. for (i = 0; i < 8; ++i)
  406. memcpy(&to[i], &from[i], sizeof(to[0]));
  407. }
  408. void convert_to_fxsr(struct task_struct *tsk,
  409. const struct user_i387_ia32_struct *env)
  410. {
  411. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  412. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  413. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  414. int i;
  415. fxsave->cwd = env->cwd;
  416. fxsave->swd = env->swd;
  417. fxsave->twd = twd_i387_to_fxsr(env->twd);
  418. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  419. #ifdef CONFIG_X86_64
  420. fxsave->rip = env->fip;
  421. fxsave->rdp = env->foo;
  422. /* cs and ds ignored */
  423. #else
  424. fxsave->fip = env->fip;
  425. fxsave->fcs = (env->fcs & 0xffff);
  426. fxsave->foo = env->foo;
  427. fxsave->fos = env->fos;
  428. #endif
  429. for (i = 0; i < 8; ++i)
  430. memcpy(&to[i], &from[i], sizeof(from[0]));
  431. }
  432. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  433. unsigned int pos, unsigned int count,
  434. void *kbuf, void __user *ubuf)
  435. {
  436. struct user_i387_ia32_struct env;
  437. int ret;
  438. ret = init_fpu(target);
  439. if (ret)
  440. return ret;
  441. if (!HAVE_HWFP)
  442. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  443. if (!cpu_has_fxsr) {
  444. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  445. &target->thread.fpu.state->fsave, 0,
  446. -1);
  447. }
  448. sanitize_i387_state(target);
  449. if (kbuf && pos == 0 && count == sizeof(env)) {
  450. convert_from_fxsr(kbuf, target);
  451. return 0;
  452. }
  453. convert_from_fxsr(&env, target);
  454. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  455. }
  456. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  457. unsigned int pos, unsigned int count,
  458. const void *kbuf, const void __user *ubuf)
  459. {
  460. struct user_i387_ia32_struct env;
  461. int ret;
  462. ret = init_fpu(target);
  463. if (ret)
  464. return ret;
  465. sanitize_i387_state(target);
  466. if (!HAVE_HWFP)
  467. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  468. if (!cpu_has_fxsr) {
  469. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  470. &target->thread.fpu.state->fsave, 0, -1);
  471. }
  472. if (pos > 0 || count < sizeof(env))
  473. convert_from_fxsr(&env, target);
  474. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  475. if (!ret)
  476. convert_to_fxsr(target, &env);
  477. /*
  478. * update the header bit in the xsave header, indicating the
  479. * presence of FP.
  480. */
  481. if (cpu_has_xsave)
  482. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
  483. return ret;
  484. }
  485. /*
  486. * FPU state for core dumps.
  487. * This is only used for a.out dumps now.
  488. * It is declared generically using elf_fpregset_t (which is
  489. * struct user_i387_struct) but is in fact only used for 32-bit
  490. * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
  491. */
  492. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
  493. {
  494. struct task_struct *tsk = current;
  495. int fpvalid;
  496. fpvalid = !!used_math();
  497. if (fpvalid)
  498. fpvalid = !fpregs_get(tsk, NULL,
  499. 0, sizeof(struct user_i387_ia32_struct),
  500. fpu, NULL);
  501. return fpvalid;
  502. }
  503. EXPORT_SYMBOL(dump_fpu);
  504. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */