i387.c 15 KB

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