core.c 14 KB

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