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