i387.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. * Quirk: we don't yet handle the XSAVES* instructions
  156. * correctly, as we don't correctly convert between
  157. * standard and compacted format when interfacing
  158. * with user-space - so disable it for now.
  159. *
  160. * The difference is small: with recent CPUs the
  161. * compacted format is only marginally smaller than
  162. * the standard FPU state format.
  163. *
  164. * ( This is easy to backport while we are fixing
  165. * XSAVES* support. )
  166. */
  167. setup_clear_cpu_cap(X86_FEATURE_XSAVES);
  168. }
  169. /*
  170. * Called at bootup to set up the initial FPU state that is later cloned
  171. * into all processes.
  172. */
  173. void fpu_init(void)
  174. {
  175. unsigned long cr0;
  176. unsigned long cr4_mask = 0;
  177. #ifndef CONFIG_MATH_EMULATION
  178. if (!cpu_has_fpu) {
  179. pr_emerg("No FPU found and no math emulation present\n");
  180. pr_emerg("Giving up\n");
  181. for (;;)
  182. asm volatile("hlt");
  183. }
  184. #endif
  185. if (cpu_has_fxsr)
  186. cr4_mask |= X86_CR4_OSFXSR;
  187. if (cpu_has_xmm)
  188. cr4_mask |= X86_CR4_OSXMMEXCPT;
  189. if (cr4_mask)
  190. cr4_set_bits(cr4_mask);
  191. cr0 = read_cr0();
  192. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  193. if (!cpu_has_fpu)
  194. cr0 |= X86_CR0_EM;
  195. write_cr0(cr0);
  196. /*
  197. * init_thread_xstate is only called once to avoid overriding
  198. * xstate_size during boot time or during CPU hotplug.
  199. */
  200. if (xstate_size == 0)
  201. init_thread_xstate();
  202. mxcsr_feature_mask_init();
  203. xsave_init();
  204. eager_fpu_init();
  205. }
  206. void fpu_finit(struct fpu *fpu)
  207. {
  208. if (!cpu_has_fpu) {
  209. finit_soft_fpu(&fpu->state->soft);
  210. return;
  211. }
  212. memset(fpu->state, 0, xstate_size);
  213. if (cpu_has_fxsr) {
  214. fx_finit(&fpu->state->fxsave);
  215. } else {
  216. struct i387_fsave_struct *fp = &fpu->state->fsave;
  217. fp->cwd = 0xffff037fu;
  218. fp->swd = 0xffff0000u;
  219. fp->twd = 0xffffffffu;
  220. fp->fos = 0xffff0000u;
  221. }
  222. }
  223. EXPORT_SYMBOL_GPL(fpu_finit);
  224. /*
  225. * The _current_ task is using the FPU for the first time
  226. * so initialize it and set the mxcsr to its default
  227. * value at reset if we support XMM instructions and then
  228. * remember the current task has used the FPU.
  229. */
  230. int init_fpu(struct task_struct *tsk)
  231. {
  232. int ret;
  233. if (tsk_used_math(tsk)) {
  234. if (cpu_has_fpu && tsk == current)
  235. unlazy_fpu(tsk);
  236. task_disable_lazy_fpu_restore(tsk);
  237. return 0;
  238. }
  239. /*
  240. * Memory allocation at the first usage of the FPU and other state.
  241. */
  242. ret = fpu_alloc(&tsk->thread.fpu);
  243. if (ret)
  244. return ret;
  245. fpu_finit(&tsk->thread.fpu);
  246. set_stopped_child_used_math(tsk);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(init_fpu);
  250. /*
  251. * The xstateregs_active() routine is the same as the fpregs_active() routine,
  252. * as the "regset->n" for the xstate regset will be updated based on the feature
  253. * capabilites supported by the xsave.
  254. */
  255. int fpregs_active(struct task_struct *target, const struct user_regset *regset)
  256. {
  257. return tsk_used_math(target) ? regset->n : 0;
  258. }
  259. int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
  260. {
  261. return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
  262. }
  263. int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
  264. unsigned int pos, unsigned int count,
  265. void *kbuf, 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. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  275. &target->thread.fpu.state->fxsave, 0, -1);
  276. }
  277. int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
  278. unsigned int pos, unsigned int count,
  279. const void *kbuf, const void __user *ubuf)
  280. {
  281. int ret;
  282. if (!cpu_has_fxsr)
  283. return -ENODEV;
  284. ret = init_fpu(target);
  285. if (ret)
  286. return ret;
  287. sanitize_i387_state(target);
  288. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  289. &target->thread.fpu.state->fxsave, 0, -1);
  290. /*
  291. * mxcsr reserved bits must be masked to zero for security reasons.
  292. */
  293. target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
  294. /*
  295. * update the header bits in the xsave header, indicating the
  296. * presence of FP and SSE state.
  297. */
  298. if (cpu_has_xsave)
  299. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
  300. return ret;
  301. }
  302. int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
  303. unsigned int pos, unsigned int count,
  304. void *kbuf, void __user *ubuf)
  305. {
  306. struct xsave_struct *xsave;
  307. int ret;
  308. if (!cpu_has_xsave)
  309. return -ENODEV;
  310. ret = init_fpu(target);
  311. if (ret)
  312. return ret;
  313. xsave = &target->thread.fpu.state->xsave;
  314. /*
  315. * Copy the 48bytes defined by the software first into the xstate
  316. * memory layout in the thread struct, so that we can copy the entire
  317. * xstateregs to the user using one user_regset_copyout().
  318. */
  319. memcpy(&xsave->i387.sw_reserved,
  320. xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
  321. /*
  322. * Copy the xstate memory layout.
  323. */
  324. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
  325. return ret;
  326. }
  327. int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
  328. unsigned int pos, unsigned int count,
  329. const void *kbuf, const void __user *ubuf)
  330. {
  331. struct xsave_struct *xsave;
  332. int ret;
  333. if (!cpu_has_xsave)
  334. return -ENODEV;
  335. ret = init_fpu(target);
  336. if (ret)
  337. return ret;
  338. xsave = &target->thread.fpu.state->xsave;
  339. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
  340. /*
  341. * mxcsr reserved bits must be masked to zero for security reasons.
  342. */
  343. xsave->i387.mxcsr &= mxcsr_feature_mask;
  344. xsave->xsave_hdr.xstate_bv &= pcntxt_mask;
  345. /*
  346. * These bits must be zero.
  347. */
  348. memset(&xsave->xsave_hdr.reserved, 0, 48);
  349. return ret;
  350. }
  351. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  352. /*
  353. * FPU tag word conversions.
  354. */
  355. static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
  356. {
  357. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  358. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  359. tmp = ~twd;
  360. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  361. /* and move the valid bits to the lower byte. */
  362. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  363. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  364. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  365. return tmp;
  366. }
  367. #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
  368. #define FP_EXP_TAG_VALID 0
  369. #define FP_EXP_TAG_ZERO 1
  370. #define FP_EXP_TAG_SPECIAL 2
  371. #define FP_EXP_TAG_EMPTY 3
  372. static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
  373. {
  374. struct _fpxreg *st;
  375. u32 tos = (fxsave->swd >> 11) & 7;
  376. u32 twd = (unsigned long) fxsave->twd;
  377. u32 tag;
  378. u32 ret = 0xffff0000u;
  379. int i;
  380. for (i = 0; i < 8; i++, twd >>= 1) {
  381. if (twd & 0x1) {
  382. st = FPREG_ADDR(fxsave, (i - tos) & 7);
  383. switch (st->exponent & 0x7fff) {
  384. case 0x7fff:
  385. tag = FP_EXP_TAG_SPECIAL;
  386. break;
  387. case 0x0000:
  388. if (!st->significand[0] &&
  389. !st->significand[1] &&
  390. !st->significand[2] &&
  391. !st->significand[3])
  392. tag = FP_EXP_TAG_ZERO;
  393. else
  394. tag = FP_EXP_TAG_SPECIAL;
  395. break;
  396. default:
  397. if (st->significand[3] & 0x8000)
  398. tag = FP_EXP_TAG_VALID;
  399. else
  400. tag = FP_EXP_TAG_SPECIAL;
  401. break;
  402. }
  403. } else {
  404. tag = FP_EXP_TAG_EMPTY;
  405. }
  406. ret |= tag << (2 * i);
  407. }
  408. return ret;
  409. }
  410. /*
  411. * FXSR floating point environment conversions.
  412. */
  413. void
  414. convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
  415. {
  416. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  417. struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
  418. struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
  419. int i;
  420. env->cwd = fxsave->cwd | 0xffff0000u;
  421. env->swd = fxsave->swd | 0xffff0000u;
  422. env->twd = twd_fxsr_to_i387(fxsave);
  423. #ifdef CONFIG_X86_64
  424. env->fip = fxsave->rip;
  425. env->foo = fxsave->rdp;
  426. /*
  427. * should be actually ds/cs at fpu exception time, but
  428. * that information is not available in 64bit mode.
  429. */
  430. env->fcs = task_pt_regs(tsk)->cs;
  431. if (tsk == current) {
  432. savesegment(ds, env->fos);
  433. } else {
  434. env->fos = tsk->thread.ds;
  435. }
  436. env->fos |= 0xffff0000;
  437. #else
  438. env->fip = fxsave->fip;
  439. env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
  440. env->foo = fxsave->foo;
  441. env->fos = fxsave->fos;
  442. #endif
  443. for (i = 0; i < 8; ++i)
  444. memcpy(&to[i], &from[i], sizeof(to[0]));
  445. }
  446. void convert_to_fxsr(struct task_struct *tsk,
  447. const struct user_i387_ia32_struct *env)
  448. {
  449. struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
  450. struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
  451. struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
  452. int i;
  453. fxsave->cwd = env->cwd;
  454. fxsave->swd = env->swd;
  455. fxsave->twd = twd_i387_to_fxsr(env->twd);
  456. fxsave->fop = (u16) ((u32) env->fcs >> 16);
  457. #ifdef CONFIG_X86_64
  458. fxsave->rip = env->fip;
  459. fxsave->rdp = env->foo;
  460. /* cs and ds ignored */
  461. #else
  462. fxsave->fip = env->fip;
  463. fxsave->fcs = (env->fcs & 0xffff);
  464. fxsave->foo = env->foo;
  465. fxsave->fos = env->fos;
  466. #endif
  467. for (i = 0; i < 8; ++i)
  468. memcpy(&to[i], &from[i], sizeof(from[0]));
  469. }
  470. int fpregs_get(struct task_struct *target, const struct user_regset *regset,
  471. unsigned int pos, unsigned int count,
  472. void *kbuf, void __user *ubuf)
  473. {
  474. struct user_i387_ia32_struct env;
  475. int ret;
  476. ret = init_fpu(target);
  477. if (ret)
  478. return ret;
  479. if (!static_cpu_has(X86_FEATURE_FPU))
  480. return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
  481. if (!cpu_has_fxsr)
  482. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  483. &target->thread.fpu.state->fsave, 0,
  484. -1);
  485. sanitize_i387_state(target);
  486. if (kbuf && pos == 0 && count == sizeof(env)) {
  487. convert_from_fxsr(kbuf, target);
  488. return 0;
  489. }
  490. convert_from_fxsr(&env, target);
  491. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  492. }
  493. int fpregs_set(struct task_struct *target, const struct user_regset *regset,
  494. unsigned int pos, unsigned int count,
  495. const void *kbuf, const void __user *ubuf)
  496. {
  497. struct user_i387_ia32_struct env;
  498. int ret;
  499. ret = init_fpu(target);
  500. if (ret)
  501. return ret;
  502. sanitize_i387_state(target);
  503. if (!static_cpu_has(X86_FEATURE_FPU))
  504. return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
  505. if (!cpu_has_fxsr)
  506. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  507. &target->thread.fpu.state->fsave, 0,
  508. -1);
  509. if (pos > 0 || count < sizeof(env))
  510. convert_from_fxsr(&env, target);
  511. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
  512. if (!ret)
  513. convert_to_fxsr(target, &env);
  514. /*
  515. * update the header bit in the xsave header, indicating the
  516. * presence of FP.
  517. */
  518. if (cpu_has_xsave)
  519. target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
  520. return ret;
  521. }
  522. /*
  523. * FPU state for core dumps.
  524. * This is only used for a.out dumps now.
  525. * It is declared generically using elf_fpregset_t (which is
  526. * struct user_i387_struct) but is in fact only used for 32-bit
  527. * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
  528. */
  529. int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
  530. {
  531. struct task_struct *tsk = current;
  532. int fpvalid;
  533. fpvalid = !!used_math();
  534. if (fpvalid)
  535. fpvalid = !fpregs_get(tsk, NULL,
  536. 0, sizeof(struct user_i387_ia32_struct),
  537. fpu, NULL);
  538. return fpvalid;
  539. }
  540. EXPORT_SYMBOL(dump_fpu);
  541. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
  542. static int __init no_387(char *s)
  543. {
  544. setup_clear_cpu_cap(X86_FEATURE_FPU);
  545. return 1;
  546. }
  547. __setup("no387", no_387);
  548. void fpu_detect(struct cpuinfo_x86 *c)
  549. {
  550. unsigned long cr0;
  551. u16 fsw, fcw;
  552. fsw = fcw = 0xffff;
  553. cr0 = read_cr0();
  554. cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
  555. write_cr0(cr0);
  556. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  557. : "+m" (fsw), "+m" (fcw));
  558. if (fsw == 0 && (fcw & 0x103f) == 0x003f)
  559. set_cpu_cap(c, X86_FEATURE_FPU);
  560. else
  561. clear_cpu_cap(c, X86_FEATURE_FPU);
  562. /* The final cr0 value is set in fpu_init() */
  563. }