core.c 12 KB

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