i387.c 15 KB

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