core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 <asm/fpu/internal.h>
  9. #include <asm/fpu/regset.h>
  10. #include <asm/fpu/signal.h>
  11. #include <asm/fpu/types.h>
  12. #include <asm/fpu/xstate.h>
  13. #include <asm/traps.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/pkeys.h>
  16. #define CREATE_TRACE_POINTS
  17. #include <asm/trace/fpu.h>
  18. /*
  19. * Represents the initial FPU state. It's mostly (but not completely) zeroes,
  20. * depending on the FPU hardware format:
  21. */
  22. union fpregs_state init_fpstate __read_mostly;
  23. /*
  24. * Track whether the kernel is using the FPU state
  25. * currently.
  26. *
  27. * This flag is used:
  28. *
  29. * - by IRQ context code to potentially use the FPU
  30. * if it's unused.
  31. *
  32. * - to debug kernel_fpu_begin()/end() correctness
  33. */
  34. static DEFINE_PER_CPU(bool, in_kernel_fpu);
  35. /*
  36. * Track which context is using the FPU on the CPU:
  37. */
  38. DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
  39. static void kernel_fpu_disable(void)
  40. {
  41. WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
  42. this_cpu_write(in_kernel_fpu, true);
  43. }
  44. static void kernel_fpu_enable(void)
  45. {
  46. WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
  47. this_cpu_write(in_kernel_fpu, false);
  48. }
  49. static bool kernel_fpu_disabled(void)
  50. {
  51. return this_cpu_read(in_kernel_fpu);
  52. }
  53. static bool interrupted_kernel_fpu_idle(void)
  54. {
  55. return !kernel_fpu_disabled();
  56. }
  57. /*
  58. * Were we in user mode (or vm86 mode) when we were
  59. * interrupted?
  60. *
  61. * Doing kernel_fpu_begin/end() is ok if we are running
  62. * in an interrupt context from user mode - we'll just
  63. * save the FPU state as required.
  64. */
  65. static bool interrupted_user_mode(void)
  66. {
  67. struct pt_regs *regs = get_irq_regs();
  68. return regs && user_mode(regs);
  69. }
  70. /*
  71. * Can we use the FPU in kernel mode with the
  72. * whole "kernel_fpu_begin/end()" sequence?
  73. *
  74. * It's always ok in process context (ie "not interrupt")
  75. * but it is sometimes ok even from an irq.
  76. */
  77. bool irq_fpu_usable(void)
  78. {
  79. return !in_interrupt() ||
  80. interrupted_user_mode() ||
  81. interrupted_kernel_fpu_idle();
  82. }
  83. EXPORT_SYMBOL(irq_fpu_usable);
  84. void __kernel_fpu_begin(void)
  85. {
  86. struct fpu *fpu = &current->thread.fpu;
  87. WARN_ON_FPU(!irq_fpu_usable());
  88. kernel_fpu_disable();
  89. if (fpu->fpregs_active) {
  90. /*
  91. * Ignore return value -- we don't care if reg state
  92. * is clobbered.
  93. */
  94. copy_fpregs_to_fpstate(fpu);
  95. } else {
  96. __cpu_invalidate_fpregs_state();
  97. }
  98. }
  99. EXPORT_SYMBOL(__kernel_fpu_begin);
  100. void __kernel_fpu_end(void)
  101. {
  102. struct fpu *fpu = &current->thread.fpu;
  103. if (fpu->fpregs_active)
  104. copy_kernel_to_fpregs(&fpu->state);
  105. kernel_fpu_enable();
  106. }
  107. EXPORT_SYMBOL(__kernel_fpu_end);
  108. void kernel_fpu_begin(void)
  109. {
  110. preempt_disable();
  111. __kernel_fpu_begin();
  112. }
  113. EXPORT_SYMBOL_GPL(kernel_fpu_begin);
  114. void kernel_fpu_end(void)
  115. {
  116. __kernel_fpu_end();
  117. preempt_enable();
  118. }
  119. EXPORT_SYMBOL_GPL(kernel_fpu_end);
  120. /*
  121. * Save the FPU state (mark it for reload if necessary):
  122. *
  123. * This only ever gets called for the current task.
  124. */
  125. void fpu__save(struct fpu *fpu)
  126. {
  127. WARN_ON_FPU(fpu != &current->thread.fpu);
  128. preempt_disable();
  129. trace_x86_fpu_before_save(fpu);
  130. if (fpu->fpregs_active) {
  131. if (!copy_fpregs_to_fpstate(fpu)) {
  132. copy_kernel_to_fpregs(&fpu->state);
  133. }
  134. }
  135. trace_x86_fpu_after_save(fpu);
  136. preempt_enable();
  137. }
  138. EXPORT_SYMBOL_GPL(fpu__save);
  139. /*
  140. * Legacy x87 fpstate state init:
  141. */
  142. static inline void fpstate_init_fstate(struct fregs_state *fp)
  143. {
  144. fp->cwd = 0xffff037fu;
  145. fp->swd = 0xffff0000u;
  146. fp->twd = 0xffffffffu;
  147. fp->fos = 0xffff0000u;
  148. }
  149. void fpstate_init(union fpregs_state *state)
  150. {
  151. if (!static_cpu_has(X86_FEATURE_FPU)) {
  152. fpstate_init_soft(&state->soft);
  153. return;
  154. }
  155. memset(state, 0, fpu_kernel_xstate_size);
  156. /*
  157. * XRSTORS requires that this bit is set in xcomp_bv, or
  158. * it will #GP. Make sure it is replaced after the memset().
  159. */
  160. if (static_cpu_has(X86_FEATURE_XSAVES))
  161. state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
  162. xfeatures_mask;
  163. if (static_cpu_has(X86_FEATURE_FXSR))
  164. fpstate_init_fxstate(&state->fxsave);
  165. else
  166. fpstate_init_fstate(&state->fsave);
  167. }
  168. EXPORT_SYMBOL_GPL(fpstate_init);
  169. int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
  170. {
  171. dst_fpu->fpregs_active = 0;
  172. dst_fpu->last_cpu = -1;
  173. if (!src_fpu->fpstate_active || !static_cpu_has(X86_FEATURE_FPU))
  174. return 0;
  175. WARN_ON_FPU(src_fpu != &current->thread.fpu);
  176. /*
  177. * Don't let 'init optimized' areas of the XSAVE area
  178. * leak into the child task:
  179. */
  180. memset(&dst_fpu->state.xsave, 0, fpu_kernel_xstate_size);
  181. /*
  182. * Save current FPU registers directly into the child
  183. * FPU context, without any memory-to-memory copying.
  184. * In lazy mode, if the FPU context isn't loaded into
  185. * fpregs, CR0.TS will be set and do_device_not_available
  186. * will load the FPU context.
  187. *
  188. * We have to do all this with preemption disabled,
  189. * mostly because of the FNSAVE case, because in that
  190. * case we must not allow preemption in the window
  191. * between the FNSAVE and us marking the context lazy.
  192. *
  193. * It shouldn't be an issue as even FNSAVE is plenty
  194. * fast in terms of critical section length.
  195. */
  196. preempt_disable();
  197. if (!copy_fpregs_to_fpstate(dst_fpu)) {
  198. memcpy(&src_fpu->state, &dst_fpu->state,
  199. fpu_kernel_xstate_size);
  200. copy_kernel_to_fpregs(&src_fpu->state);
  201. }
  202. preempt_enable();
  203. trace_x86_fpu_copy_src(src_fpu);
  204. trace_x86_fpu_copy_dst(dst_fpu);
  205. return 0;
  206. }
  207. /*
  208. * Activate the current task's in-memory FPU context,
  209. * if it has not been used before:
  210. */
  211. void fpu__activate_curr(struct fpu *fpu)
  212. {
  213. WARN_ON_FPU(fpu != &current->thread.fpu);
  214. if (!fpu->fpstate_active) {
  215. fpstate_init(&fpu->state);
  216. trace_x86_fpu_init_state(fpu);
  217. trace_x86_fpu_activate_state(fpu);
  218. /* Safe to do for the current task: */
  219. fpu->fpstate_active = 1;
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(fpu__activate_curr);
  223. /*
  224. * This function must be called before we read a task's fpstate.
  225. *
  226. * If the task has not used the FPU before then initialize its
  227. * fpstate.
  228. *
  229. * If the task has used the FPU before then save it.
  230. */
  231. void fpu__activate_fpstate_read(struct fpu *fpu)
  232. {
  233. /*
  234. * If fpregs are active (in the current CPU), then
  235. * copy them to the fpstate:
  236. */
  237. if (fpu->fpregs_active) {
  238. fpu__save(fpu);
  239. } else {
  240. if (!fpu->fpstate_active) {
  241. fpstate_init(&fpu->state);
  242. trace_x86_fpu_init_state(fpu);
  243. trace_x86_fpu_activate_state(fpu);
  244. /* Safe to do for current and for stopped child tasks: */
  245. fpu->fpstate_active = 1;
  246. }
  247. }
  248. }
  249. /*
  250. * This function must be called before we write a task's fpstate.
  251. *
  252. * If the task has used the FPU before then unlazy it.
  253. * If the task has not used the FPU before then initialize its fpstate.
  254. *
  255. * After this function call, after registers in the fpstate are
  256. * modified and the child task has woken up, the child task will
  257. * restore the modified FPU state from the modified context. If we
  258. * didn't clear its lazy status here then the lazy in-registers
  259. * state pending on its former CPU could be restored, corrupting
  260. * the modifications.
  261. */
  262. void fpu__activate_fpstate_write(struct fpu *fpu)
  263. {
  264. /*
  265. * Only stopped child tasks can be used to modify the FPU
  266. * state in the fpstate buffer:
  267. */
  268. WARN_ON_FPU(fpu == &current->thread.fpu);
  269. if (fpu->fpstate_active) {
  270. /* Invalidate any lazy state: */
  271. __fpu_invalidate_fpregs_state(fpu);
  272. } else {
  273. fpstate_init(&fpu->state);
  274. trace_x86_fpu_init_state(fpu);
  275. trace_x86_fpu_activate_state(fpu);
  276. /* Safe to do for stopped child tasks: */
  277. fpu->fpstate_active = 1;
  278. }
  279. }
  280. /*
  281. * This function must be called before we write the current
  282. * task's fpstate.
  283. *
  284. * This call gets the current FPU register state and moves
  285. * it in to the 'fpstate'. Preemption is disabled so that
  286. * no writes to the 'fpstate' can occur from context
  287. * swiches.
  288. *
  289. * Must be followed by a fpu__current_fpstate_write_end().
  290. */
  291. void fpu__current_fpstate_write_begin(void)
  292. {
  293. struct fpu *fpu = &current->thread.fpu;
  294. /*
  295. * Ensure that the context-switching code does not write
  296. * over the fpstate while we are doing our update.
  297. */
  298. preempt_disable();
  299. /*
  300. * Move the fpregs in to the fpu's 'fpstate'.
  301. */
  302. fpu__activate_fpstate_read(fpu);
  303. /*
  304. * The caller is about to write to 'fpu'. Ensure that no
  305. * CPU thinks that its fpregs match the fpstate. This
  306. * ensures we will not be lazy and skip a XRSTOR in the
  307. * future.
  308. */
  309. __fpu_invalidate_fpregs_state(fpu);
  310. }
  311. /*
  312. * This function must be paired with fpu__current_fpstate_write_begin()
  313. *
  314. * This will ensure that the modified fpstate gets placed back in
  315. * the fpregs if necessary.
  316. *
  317. * Note: This function may be called whether or not an _actual_
  318. * write to the fpstate occurred.
  319. */
  320. void fpu__current_fpstate_write_end(void)
  321. {
  322. struct fpu *fpu = &current->thread.fpu;
  323. /*
  324. * 'fpu' now has an updated copy of the state, but the
  325. * registers may still be out of date. Update them with
  326. * an XRSTOR if they are active.
  327. */
  328. if (fpregs_active())
  329. copy_kernel_to_fpregs(&fpu->state);
  330. /*
  331. * Our update is done and the fpregs/fpstate are in sync
  332. * if necessary. Context switches can happen again.
  333. */
  334. preempt_enable();
  335. }
  336. /*
  337. * 'fpu__restore()' is called to copy FPU registers from
  338. * the FPU fpstate to the live hw registers and to activate
  339. * access to the hardware registers, so that FPU instructions
  340. * can be used afterwards.
  341. *
  342. * Must be called with kernel preemption disabled (for example
  343. * with local interrupts disabled, as it is in the case of
  344. * do_device_not_available()).
  345. */
  346. void fpu__restore(struct fpu *fpu)
  347. {
  348. fpu__activate_curr(fpu);
  349. /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
  350. kernel_fpu_disable();
  351. trace_x86_fpu_before_restore(fpu);
  352. fpregs_activate(fpu);
  353. copy_kernel_to_fpregs(&fpu->state);
  354. trace_x86_fpu_after_restore(fpu);
  355. kernel_fpu_enable();
  356. }
  357. EXPORT_SYMBOL_GPL(fpu__restore);
  358. /*
  359. * Drops current FPU state: deactivates the fpregs and
  360. * the fpstate. NOTE: it still leaves previous contents
  361. * in the fpregs in the eager-FPU case.
  362. *
  363. * This function can be used in cases where we know that
  364. * a state-restore is coming: either an explicit one,
  365. * or a reschedule.
  366. */
  367. void fpu__drop(struct fpu *fpu)
  368. {
  369. preempt_disable();
  370. if (fpu->fpregs_active) {
  371. /* Ignore delayed exceptions from user space */
  372. asm volatile("1: fwait\n"
  373. "2:\n"
  374. _ASM_EXTABLE(1b, 2b));
  375. fpregs_deactivate(fpu);
  376. }
  377. fpu->fpstate_active = 0;
  378. trace_x86_fpu_dropped(fpu);
  379. preempt_enable();
  380. }
  381. /*
  382. * Clear FPU registers by setting them up from
  383. * the init fpstate:
  384. */
  385. static inline void copy_init_fpstate_to_fpregs(void)
  386. {
  387. if (use_xsave())
  388. copy_kernel_to_xregs(&init_fpstate.xsave, -1);
  389. else if (static_cpu_has(X86_FEATURE_FXSR))
  390. copy_kernel_to_fxregs(&init_fpstate.fxsave);
  391. else
  392. copy_kernel_to_fregs(&init_fpstate.fsave);
  393. if (boot_cpu_has(X86_FEATURE_OSPKE))
  394. copy_init_pkru_to_fpregs();
  395. }
  396. /*
  397. * Clear the FPU state back to init state.
  398. *
  399. * Called by sys_execve(), by the signal handler code and by various
  400. * error paths.
  401. */
  402. void fpu__clear(struct fpu *fpu)
  403. {
  404. WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
  405. fpu__drop(fpu);
  406. /*
  407. * Make sure fpstate is cleared and initialized.
  408. */
  409. if (static_cpu_has(X86_FEATURE_FPU)) {
  410. fpu__activate_curr(fpu);
  411. user_fpu_begin();
  412. copy_init_fpstate_to_fpregs();
  413. }
  414. }
  415. /*
  416. * x87 math exception handling:
  417. */
  418. int fpu__exception_code(struct fpu *fpu, int trap_nr)
  419. {
  420. int err;
  421. if (trap_nr == X86_TRAP_MF) {
  422. unsigned short cwd, swd;
  423. /*
  424. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  425. * status. 0x3f is the exception bits in these regs, 0x200 is the
  426. * C1 reg you need in case of a stack fault, 0x040 is the stack
  427. * fault bit. We should only be taking one exception at a time,
  428. * so if this combination doesn't produce any single exception,
  429. * then we have a bad program that isn't synchronizing its FPU usage
  430. * and it will suffer the consequences since we won't be able to
  431. * fully reproduce the context of the exception.
  432. */
  433. if (boot_cpu_has(X86_FEATURE_FXSR)) {
  434. cwd = fpu->state.fxsave.cwd;
  435. swd = fpu->state.fxsave.swd;
  436. } else {
  437. cwd = (unsigned short)fpu->state.fsave.cwd;
  438. swd = (unsigned short)fpu->state.fsave.swd;
  439. }
  440. err = swd & ~cwd;
  441. } else {
  442. /*
  443. * The SIMD FPU exceptions are handled a little differently, as there
  444. * is only a single status/control register. Thus, to determine which
  445. * unmasked exception was caught we must mask the exception mask bits
  446. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  447. */
  448. unsigned short mxcsr = MXCSR_DEFAULT;
  449. if (boot_cpu_has(X86_FEATURE_XMM))
  450. mxcsr = fpu->state.fxsave.mxcsr;
  451. err = ~(mxcsr >> 7) & mxcsr;
  452. }
  453. if (err & 0x001) { /* Invalid op */
  454. /*
  455. * swd & 0x240 == 0x040: Stack Underflow
  456. * swd & 0x240 == 0x240: Stack Overflow
  457. * User must clear the SF bit (0x40) if set
  458. */
  459. return FPE_FLTINV;
  460. } else if (err & 0x004) { /* Divide by Zero */
  461. return FPE_FLTDIV;
  462. } else if (err & 0x008) { /* Overflow */
  463. return FPE_FLTOVF;
  464. } else if (err & 0x012) { /* Denormal, Underflow */
  465. return FPE_FLTUND;
  466. } else if (err & 0x020) { /* Precision */
  467. return FPE_FLTRES;
  468. }
  469. /*
  470. * If we're using IRQ 13, or supposedly even some trap
  471. * X86_TRAP_MF implementations, it's possible
  472. * we get a spurious trap, which is not an error.
  473. */
  474. return 0;
  475. }