i387.c 14 KB

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