core.c 14 KB

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