i387.c 15 KB

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