i387.c 14 KB

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