core.c 12 KB

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