interrupt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/context_tracking.h>
  3. #include <linux/err.h>
  4. #include <linux/compat.h>
  5. #include <linux/sched/debug.h> /* for show_regs */
  6. #include <asm/kup.h>
  7. #include <asm/cputime.h>
  8. #include <asm/hw_irq.h>
  9. #include <asm/interrupt.h>
  10. #include <asm/kprobes.h>
  11. #include <asm/paca.h>
  12. #include <asm/ptrace.h>
  13. #include <asm/reg.h>
  14. #include <asm/signal.h>
  15. #include <asm/switch_to.h>
  16. #include <asm/syscall.h>
  17. #include <asm/time.h>
  18. #include <asm/tm.h>
  19. #include <asm/unistd.h>
  20. #if defined(CONFIG_PPC_ADV_DEBUG_REGS) && defined(CONFIG_PPC32)
  21. unsigned long global_dbcr0[NR_CPUS];
  22. #endif
  23. #ifdef CONFIG_PPC_BOOK3S_64
  24. DEFINE_STATIC_KEY_FALSE(interrupt_exit_not_reentrant);
  25. static inline bool exit_must_hard_disable(void)
  26. {
  27. return static_branch_unlikely(&interrupt_exit_not_reentrant);
  28. }
  29. #else
  30. static inline bool exit_must_hard_disable(void)
  31. {
  32. return true;
  33. }
  34. #endif
  35. /*
  36. * local irqs must be disabled. Returns false if the caller must re-enable
  37. * them, check for new work, and try again.
  38. *
  39. * This should be called with local irqs disabled, but if they were previously
  40. * enabled when the interrupt handler returns (indicating a process-context /
  41. * synchronous interrupt) then irqs_enabled should be true.
  42. *
  43. * restartable is true then EE/RI can be left on because interrupts are handled
  44. * with a restart sequence.
  45. */
  46. static notrace __always_inline bool prep_irq_for_enabled_exit(bool restartable)
  47. {
  48. bool must_hard_disable = (exit_must_hard_disable() || !restartable);
  49. /* This must be done with RI=1 because tracing may touch vmaps */
  50. trace_hardirqs_on();
  51. if (must_hard_disable)
  52. __hard_EE_RI_disable();
  53. #ifdef CONFIG_PPC64
  54. /* This pattern matches prep_irq_for_idle */
  55. if (unlikely(lazy_irq_pending_nocheck())) {
  56. if (must_hard_disable) {
  57. local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
  58. __hard_RI_enable();
  59. }
  60. trace_hardirqs_off();
  61. return false;
  62. }
  63. #endif
  64. return true;
  65. }
  66. static notrace void booke_load_dbcr0(void)
  67. {
  68. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  69. unsigned long dbcr0 = current->thread.debug.dbcr0;
  70. if (likely(!(dbcr0 & DBCR0_IDM)))
  71. return;
  72. /*
  73. * Check to see if the dbcr0 register is set up to debug.
  74. * Use the internal debug mode bit to do this.
  75. */
  76. mtmsr(mfmsr() & ~MSR_DE);
  77. if (IS_ENABLED(CONFIG_PPC32)) {
  78. isync();
  79. global_dbcr0[smp_processor_id()] = mfspr(SPRN_DBCR0);
  80. }
  81. mtspr(SPRN_DBCR0, dbcr0);
  82. mtspr(SPRN_DBSR, -1);
  83. #endif
  84. }
  85. static void check_return_regs_valid(struct pt_regs *regs)
  86. {
  87. #ifdef CONFIG_PPC_BOOK3S_64
  88. unsigned long trap, srr0, srr1;
  89. static bool warned;
  90. u8 *validp;
  91. char *h;
  92. if (trap_is_scv(regs))
  93. return;
  94. trap = TRAP(regs);
  95. // EE in HV mode sets HSRRs like 0xea0
  96. if (cpu_has_feature(CPU_FTR_HVMODE) && trap == INTERRUPT_EXTERNAL)
  97. trap = 0xea0;
  98. switch (trap) {
  99. case 0x980:
  100. case INTERRUPT_H_DATA_STORAGE:
  101. case 0xe20:
  102. case 0xe40:
  103. case INTERRUPT_HMI:
  104. case 0xe80:
  105. case 0xea0:
  106. case INTERRUPT_H_FAC_UNAVAIL:
  107. case 0x1200:
  108. case 0x1500:
  109. case 0x1600:
  110. case 0x1800:
  111. validp = &local_paca->hsrr_valid;
  112. if (!*validp)
  113. return;
  114. srr0 = mfspr(SPRN_HSRR0);
  115. srr1 = mfspr(SPRN_HSRR1);
  116. h = "H";
  117. break;
  118. default:
  119. validp = &local_paca->srr_valid;
  120. if (!*validp)
  121. return;
  122. srr0 = mfspr(SPRN_SRR0);
  123. srr1 = mfspr(SPRN_SRR1);
  124. h = "";
  125. break;
  126. }
  127. if (srr0 == regs->nip && srr1 == regs->msr)
  128. return;
  129. /*
  130. * A NMI / soft-NMI interrupt may have come in after we found
  131. * srr_valid and before the SRRs are loaded. The interrupt then
  132. * comes in and clobbers SRRs and clears srr_valid. Then we load
  133. * the SRRs here and test them above and find they don't match.
  134. *
  135. * Test validity again after that, to catch such false positives.
  136. *
  137. * This test in general will have some window for false negatives
  138. * and may not catch and fix all such cases if an NMI comes in
  139. * later and clobbers SRRs without clearing srr_valid, but hopefully
  140. * such things will get caught most of the time, statistically
  141. * enough to be able to get a warning out.
  142. */
  143. barrier();
  144. if (!*validp)
  145. return;
  146. if (!warned) {
  147. warned = true;
  148. printk("%sSRR0 was: %lx should be: %lx\n", h, srr0, regs->nip);
  149. printk("%sSRR1 was: %lx should be: %lx\n", h, srr1, regs->msr);
  150. show_regs(regs);
  151. }
  152. *validp = 0; /* fixup */
  153. #endif
  154. }
  155. static notrace unsigned long
  156. interrupt_exit_user_prepare_main(unsigned long ret, struct pt_regs *regs)
  157. {
  158. unsigned long ti_flags;
  159. again:
  160. ti_flags = read_thread_flags();
  161. while (unlikely(ti_flags & (_TIF_USER_WORK_MASK & ~_TIF_RESTORE_TM))) {
  162. local_irq_enable();
  163. if (ti_flags & _TIF_NEED_RESCHED) {
  164. schedule();
  165. } else {
  166. /*
  167. * SIGPENDING must restore signal handler function
  168. * argument GPRs, and some non-volatiles (e.g., r1).
  169. * Restore all for now. This could be made lighter.
  170. */
  171. if (ti_flags & _TIF_SIGPENDING)
  172. ret |= _TIF_RESTOREALL;
  173. do_notify_resume(regs, ti_flags);
  174. }
  175. local_irq_disable();
  176. ti_flags = read_thread_flags();
  177. }
  178. if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && IS_ENABLED(CONFIG_PPC_FPU)) {
  179. if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) &&
  180. unlikely((ti_flags & _TIF_RESTORE_TM))) {
  181. restore_tm_state(regs);
  182. } else {
  183. unsigned long mathflags = MSR_FP;
  184. if (cpu_has_feature(CPU_FTR_VSX))
  185. mathflags |= MSR_VEC | MSR_VSX;
  186. else if (cpu_has_feature(CPU_FTR_ALTIVEC))
  187. mathflags |= MSR_VEC;
  188. /*
  189. * If userspace MSR has all available FP bits set,
  190. * then they are live and no need to restore. If not,
  191. * it means the regs were given up and restore_math
  192. * may decide to restore them (to avoid taking an FP
  193. * fault).
  194. */
  195. if ((regs->msr & mathflags) != mathflags)
  196. restore_math(regs);
  197. }
  198. }
  199. check_return_regs_valid(regs);
  200. user_enter_irqoff();
  201. if (!prep_irq_for_enabled_exit(true)) {
  202. user_exit_irqoff();
  203. local_irq_enable();
  204. local_irq_disable();
  205. goto again;
  206. }
  207. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  208. local_paca->tm_scratch = regs->msr;
  209. #endif
  210. booke_load_dbcr0();
  211. account_cpu_user_exit();
  212. /* Restore user access locks last */
  213. kuap_user_restore(regs);
  214. return ret;
  215. }
  216. /*
  217. * This should be called after a syscall returns, with r3 the return value
  218. * from the syscall. If this function returns non-zero, the system call
  219. * exit assembly should additionally load all GPR registers and CTR and XER
  220. * from the interrupt frame.
  221. *
  222. * The function graph tracer can not trace the return side of this function,
  223. * because RI=0 and soft mask state is "unreconciled", so it is marked notrace.
  224. */
  225. notrace unsigned long syscall_exit_prepare(unsigned long r3,
  226. struct pt_regs *regs,
  227. long scv)
  228. {
  229. unsigned long ti_flags;
  230. unsigned long ret = 0;
  231. bool is_not_scv = !IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !scv;
  232. CT_WARN_ON(ct_state() == CONTEXT_USER);
  233. kuap_assert_locked();
  234. regs->result = r3;
  235. /* Check whether the syscall is issued inside a restartable sequence */
  236. rseq_syscall(regs);
  237. ti_flags = read_thread_flags();
  238. if (unlikely(r3 >= (unsigned long)-MAX_ERRNO) && is_not_scv) {
  239. if (likely(!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) {
  240. r3 = -r3;
  241. regs->ccr |= 0x10000000; /* Set SO bit in CR */
  242. }
  243. }
  244. if (unlikely(ti_flags & _TIF_PERSYSCALL_MASK)) {
  245. if (ti_flags & _TIF_RESTOREALL)
  246. ret = _TIF_RESTOREALL;
  247. else
  248. regs->gpr[3] = r3;
  249. clear_bits(_TIF_PERSYSCALL_MASK, &current_thread_info()->flags);
  250. } else {
  251. regs->gpr[3] = r3;
  252. }
  253. if (unlikely(ti_flags & _TIF_SYSCALL_DOTRACE)) {
  254. do_syscall_trace_leave(regs);
  255. ret |= _TIF_RESTOREALL;
  256. }
  257. local_irq_disable();
  258. ret = interrupt_exit_user_prepare_main(ret, regs);
  259. #ifdef CONFIG_PPC64
  260. regs->exit_result = ret;
  261. #endif
  262. return ret;
  263. }
  264. #ifdef CONFIG_PPC64
  265. notrace unsigned long syscall_exit_restart(unsigned long r3, struct pt_regs *regs)
  266. {
  267. /*
  268. * This is called when detecting a soft-pending interrupt as well as
  269. * an alternate-return interrupt. So we can't just have the alternate
  270. * return path clear SRR1[MSR] and set PACA_IRQ_HARD_DIS (unless
  271. * the soft-pending case were to fix things up as well). RI might be
  272. * disabled, in which case it gets re-enabled by __hard_irq_disable().
  273. */
  274. __hard_irq_disable();
  275. local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
  276. #ifdef CONFIG_PPC_BOOK3S_64
  277. set_kuap(AMR_KUAP_BLOCKED);
  278. #endif
  279. trace_hardirqs_off();
  280. user_exit_irqoff();
  281. account_cpu_user_entry();
  282. BUG_ON(!user_mode(regs));
  283. regs->exit_result = interrupt_exit_user_prepare_main(regs->exit_result, regs);
  284. return regs->exit_result;
  285. }
  286. #endif
  287. notrace unsigned long interrupt_exit_user_prepare(struct pt_regs *regs)
  288. {
  289. unsigned long ret;
  290. BUG_ON(regs_is_unrecoverable(regs));
  291. BUG_ON(arch_irq_disabled_regs(regs));
  292. CT_WARN_ON(ct_state() == CONTEXT_USER);
  293. /*
  294. * We don't need to restore AMR on the way back to userspace for KUAP.
  295. * AMR can only have been unlocked if we interrupted the kernel.
  296. */
  297. kuap_assert_locked();
  298. local_irq_disable();
  299. ret = interrupt_exit_user_prepare_main(0, regs);
  300. #ifdef CONFIG_PPC64
  301. regs->exit_result = ret;
  302. #endif
  303. return ret;
  304. }
  305. void preempt_schedule_irq(void);
  306. notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs)
  307. {
  308. unsigned long ret = 0;
  309. unsigned long kuap;
  310. bool stack_store = read_thread_flags() & _TIF_EMULATE_STACK_STORE;
  311. if (regs_is_unrecoverable(regs))
  312. unrecoverable_exception(regs);
  313. /*
  314. * CT_WARN_ON comes here via program_check_exception, so avoid
  315. * recursion.
  316. *
  317. * Skip the assertion on PMIs on 64e to work around a problem caused
  318. * by NMI PMIs incorrectly taking this interrupt return path, it's
  319. * possible for this to hit after interrupt exit to user switches
  320. * context to user. See also the comment in the performance monitor
  321. * handler in exceptions-64e.S
  322. */
  323. if (!IS_ENABLED(CONFIG_PPC_BOOK3E_64) &&
  324. TRAP(regs) != INTERRUPT_PROGRAM &&
  325. TRAP(regs) != INTERRUPT_PERFMON)
  326. CT_WARN_ON(ct_state() == CONTEXT_USER);
  327. kuap = kuap_get_and_assert_locked();
  328. local_irq_disable();
  329. if (!arch_irq_disabled_regs(regs)) {
  330. /* Returning to a kernel context with local irqs enabled. */
  331. WARN_ON_ONCE(!(regs->msr & MSR_EE));
  332. again:
  333. if (IS_ENABLED(CONFIG_PREEMPT)) {
  334. /* Return to preemptible kernel context */
  335. if (unlikely(read_thread_flags() & _TIF_NEED_RESCHED)) {
  336. if (preempt_count() == 0)
  337. preempt_schedule_irq();
  338. }
  339. }
  340. check_return_regs_valid(regs);
  341. /*
  342. * Stack store exit can't be restarted because the interrupt
  343. * stack frame might have been clobbered.
  344. */
  345. if (!prep_irq_for_enabled_exit(unlikely(stack_store))) {
  346. /*
  347. * Replay pending soft-masked interrupts now. Don't
  348. * just local_irq_enabe(); local_irq_disable(); because
  349. * if we are returning from an asynchronous interrupt
  350. * here, another one might hit after irqs are enabled,
  351. * and it would exit via this same path allowing
  352. * another to fire, and so on unbounded.
  353. */
  354. hard_irq_disable();
  355. replay_soft_interrupts();
  356. /* Took an interrupt, may have more exit work to do. */
  357. goto again;
  358. }
  359. #ifdef CONFIG_PPC64
  360. /*
  361. * An interrupt may clear MSR[EE] and set this concurrently,
  362. * but it will be marked pending and the exit will be retried.
  363. * This leaves a racy window where MSR[EE]=0 and HARD_DIS is
  364. * clear, until interrupt_exit_kernel_restart() calls
  365. * hard_irq_disable(), which will set HARD_DIS again.
  366. */
  367. local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
  368. } else {
  369. check_return_regs_valid(regs);
  370. if (unlikely(stack_store))
  371. __hard_EE_RI_disable();
  372. #endif /* CONFIG_PPC64 */
  373. }
  374. if (unlikely(stack_store)) {
  375. clear_bits(_TIF_EMULATE_STACK_STORE, &current_thread_info()->flags);
  376. ret = 1;
  377. }
  378. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  379. local_paca->tm_scratch = regs->msr;
  380. #endif
  381. /*
  382. * 64s does not want to mfspr(SPRN_AMR) here, because this comes after
  383. * mtmsr, which would cause Read-After-Write stalls. Hence, take the
  384. * AMR value from the check above.
  385. */
  386. kuap_kernel_restore(regs, kuap);
  387. return ret;
  388. }
  389. #ifdef CONFIG_PPC64
  390. notrace unsigned long interrupt_exit_user_restart(struct pt_regs *regs)
  391. {
  392. __hard_irq_disable();
  393. local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
  394. #ifdef CONFIG_PPC_BOOK3S_64
  395. set_kuap(AMR_KUAP_BLOCKED);
  396. #endif
  397. trace_hardirqs_off();
  398. user_exit_irqoff();
  399. account_cpu_user_entry();
  400. BUG_ON(!user_mode(regs));
  401. regs->exit_result |= interrupt_exit_user_prepare(regs);
  402. return regs->exit_result;
  403. }
  404. /*
  405. * No real need to return a value here because the stack store case does not
  406. * get restarted.
  407. */
  408. notrace unsigned long interrupt_exit_kernel_restart(struct pt_regs *regs)
  409. {
  410. __hard_irq_disable();
  411. local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
  412. #ifdef CONFIG_PPC_BOOK3S_64
  413. set_kuap(AMR_KUAP_BLOCKED);
  414. #endif
  415. if (regs->softe == IRQS_ENABLED)
  416. trace_hardirqs_off();
  417. BUG_ON(user_mode(regs));
  418. return interrupt_exit_kernel_prepare(regs);
  419. }
  420. #endif