1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef _ASM_PPC_ENTRY_COMMON_H
4 #define _ASM_PPC_ENTRY_COMMON_H
5
6 #include <asm/cputime.h>
7 #include <asm/interrupt.h>
8 #include <asm/runlatch.h>
9 #include <asm/stacktrace.h>
10 #include <asm/switch_to.h>
11 #include <asm/tm.h>
12
13 #ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
14 /*
15 * WARN/BUG is handled with a program interrupt so minimise checks here to
16 * avoid recursion and maximise the chance of getting the first oops handled.
17 */
18 #define INT_SOFT_MASK_BUG_ON(regs, cond) \
19 do { \
20 if ((user_mode(regs) || (TRAP(regs) != INTERRUPT_PROGRAM))) \
21 BUG_ON(cond); \
22 } while (0)
23 #else
24 #define INT_SOFT_MASK_BUG_ON(regs, cond)
25 #endif
26
27 #ifdef CONFIG_PPC_BOOK3S_64
28 extern char __end_soft_masked[];
29 bool search_kernel_soft_mask_table(unsigned long addr);
30 unsigned long search_kernel_restart_table(unsigned long addr);
31
32 DECLARE_STATIC_KEY_FALSE(interrupt_exit_not_reentrant);
33
is_implicit_soft_masked(struct pt_regs * regs)34 static inline bool is_implicit_soft_masked(struct pt_regs *regs)
35 {
36 if (user_mode(regs))
37 return false;
38
39 if (regs->nip >= (unsigned long)__end_soft_masked)
40 return false;
41
42 return search_kernel_soft_mask_table(regs->nip);
43 }
44
srr_regs_clobbered(void)45 static inline void srr_regs_clobbered(void)
46 {
47 local_paca->srr_valid = 0;
48 local_paca->hsrr_valid = 0;
49 }
50 #else
search_kernel_restart_table(unsigned long addr)51 static inline unsigned long search_kernel_restart_table(unsigned long addr)
52 {
53 return 0;
54 }
55
is_implicit_soft_masked(struct pt_regs * regs)56 static inline bool is_implicit_soft_masked(struct pt_regs *regs)
57 {
58 return false;
59 }
60
srr_regs_clobbered(void)61 static inline void srr_regs_clobbered(void)
62 {
63 }
64 #endif
65
nap_adjust_return(struct pt_regs * regs)66 static inline void nap_adjust_return(struct pt_regs *regs)
67 {
68 #ifdef CONFIG_PPC_970_NAP
69 /*
70 * Adjust the nap return address before irq_exit_rcu(). irq_exit_rcu()
71 * may invoke softirqs with interrupts re-enabled, allowing a nested
72 * async interrupt to arrive. If _TLF_NAPPING is still set at that
73 * point, the nested interrupt would erroneously redirect its own
74 * return address to power4_idle_nap_return, corrupting the stack.
75 */
76 if (unlikely(test_thread_local_flags(_TLF_NAPPING))) {
77 /* Can avoid a test-and-clear because NMIs do not call this */
78 clear_thread_local_flags(_TLF_NAPPING);
79 regs_set_return_ip(regs, (unsigned long)power4_idle_nap_return);
80 }
81 #endif
82 }
83
booke_load_dbcr0(void)84 static __always_inline void booke_load_dbcr0(void)
85 {
86 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
87 unsigned long dbcr0 = current->thread.debug.dbcr0;
88
89 if (likely(!(dbcr0 & DBCR0_IDM)))
90 return;
91
92 /*
93 * Check to see if the dbcr0 register is set up to debug.
94 * Use the internal debug mode bit to do this.
95 */
96 mtmsr(mfmsr() & ~MSR_DE);
97 if (IS_ENABLED(CONFIG_PPC32)) {
98 isync();
99 global_dbcr0[smp_processor_id()] = mfspr(SPRN_DBCR0);
100 }
101 mtspr(SPRN_DBCR0, dbcr0);
102 mtspr(SPRN_DBSR, -1);
103 #endif
104 }
105
booke_restore_dbcr0(void)106 static inline void booke_restore_dbcr0(void)
107 {
108 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
109 unsigned long dbcr0 = current->thread.debug.dbcr0;
110
111 if (IS_ENABLED(CONFIG_PPC32) && unlikely(dbcr0 & DBCR0_IDM)) {
112 mtspr(SPRN_DBSR, -1);
113 mtspr(SPRN_DBCR0, global_dbcr0[smp_processor_id()]);
114 }
115 #endif
116 }
117
check_return_regs_valid(struct pt_regs * regs)118 static inline void check_return_regs_valid(struct pt_regs *regs)
119 {
120 #ifdef CONFIG_PPC_BOOK3S_64
121 unsigned long trap, srr0, srr1;
122 static bool warned;
123 u8 *validp;
124 char *h;
125
126 if (trap_is_scv(regs))
127 return;
128
129 trap = TRAP(regs);
130 // EE in HV mode sets HSRRs like 0xea0
131 if (cpu_has_feature(CPU_FTR_HVMODE) && trap == INTERRUPT_EXTERNAL)
132 trap = 0xea0;
133
134 switch (trap) {
135 case 0x980:
136 case INTERRUPT_H_DATA_STORAGE:
137 case 0xe20:
138 case 0xe40:
139 case INTERRUPT_HMI:
140 case 0xe80:
141 case 0xea0:
142 case INTERRUPT_H_FAC_UNAVAIL:
143 case 0x1200:
144 case 0x1500:
145 case 0x1600:
146 case 0x1800:
147 validp = &local_paca->hsrr_valid;
148 if (!READ_ONCE(*validp))
149 return;
150
151 srr0 = mfspr(SPRN_HSRR0);
152 srr1 = mfspr(SPRN_HSRR1);
153 h = "H";
154
155 break;
156 default:
157 validp = &local_paca->srr_valid;
158 if (!READ_ONCE(*validp))
159 return;
160
161 srr0 = mfspr(SPRN_SRR0);
162 srr1 = mfspr(SPRN_SRR1);
163 h = "";
164 break;
165 }
166
167 if (srr0 == regs->nip && srr1 == regs->msr)
168 return;
169
170 /*
171 * A NMI / soft-NMI interrupt may have come in after we found
172 * srr_valid and before the SRRs are loaded. The interrupt then
173 * comes in and clobbers SRRs and clears srr_valid. Then we load
174 * the SRRs here and test them above and find they don't match.
175 *
176 * Test validity again after that, to catch such false positives.
177 *
178 * This test in general will have some window for false negatives
179 * and may not catch and fix all such cases if an NMI comes in
180 * later and clobbers SRRs without clearing srr_valid, but hopefully
181 * such things will get caught most of the time, statistically
182 * enough to be able to get a warning out.
183 */
184 if (!READ_ONCE(*validp))
185 return;
186
187 if (!data_race(warned)) {
188 data_race(warned = true);
189 pr_warn("%sSRR0 was: %lx should be: %lx\n", h, srr0, regs->nip);
190 pr_warn("%sSRR1 was: %lx should be: %lx\n", h, srr1, regs->msr);
191 show_regs(regs);
192 }
193
194 WRITE_ONCE(*validp, 0); /* fixup */
195 #endif
196 }
197
arch_interrupt_enter_prepare(struct pt_regs * regs)198 static inline void arch_interrupt_enter_prepare(struct pt_regs *regs)
199 {
200 #ifdef CONFIG_PPC64
201 irq_soft_mask_set(IRQS_ALL_DISABLED);
202
203 /*
204 * If the interrupt was taken with HARD_DIS clear, then enable MSR[EE].
205 * Asynchronous interrupts get here with HARD_DIS set (see below), so
206 * this enables MSR[EE] for synchronous interrupts. IRQs remain
207 * soft-masked. The interrupt handler may later call
208 * interrupt_cond_local_irq_enable() to achieve a regular process
209 * context.
210 */
211 if (!(local_paca->irq_happened & PACA_IRQ_HARD_DIS)) {
212 INT_SOFT_MASK_BUG_ON(regs, !(regs->msr & MSR_EE));
213 __hard_irq_enable();
214 } else {
215 __hard_RI_enable();
216 }
217 /* Enable MSR[RI] early, to support kernel SLB and hash faults */
218 #endif
219
220 if (!regs_irqs_disabled(regs))
221 trace_hardirqs_off();
222
223 if (user_mode(regs)) {
224 kuap_lock();
225 account_cpu_user_entry();
226 account_stolen_time();
227 } else {
228 kuap_save_and_lock(regs);
229 /*
230 * CT_WARN_ON comes here via program_check_exception,
231 * so avoid recursion.
232 */
233 if (TRAP(regs) != INTERRUPT_PROGRAM)
234 CT_WARN_ON(ct_state() != CT_STATE_KERNEL &&
235 ct_state() != CT_STATE_IDLE);
236 INT_SOFT_MASK_BUG_ON(regs, is_implicit_soft_masked(regs));
237 INT_SOFT_MASK_BUG_ON(regs, regs_irqs_disabled(regs) &&
238 search_kernel_restart_table(regs->nip));
239 }
240 INT_SOFT_MASK_BUG_ON(regs, !regs_irqs_disabled(regs) &&
241 !(regs->msr & MSR_EE));
242
243 booke_restore_dbcr0();
244 }
245
246 /*
247 * Care should be taken to note that arch_interrupt_exit_prepare and
248 * arch_interrupt_async_exit_prepare do not necessarily return immediately to
249 * regs context (e.g., if regs is usermode, we don't necessarily return to
250 * user mode). Other interrupts might be taken between here and return,
251 * context switch / preemption may occur in the exit path after this, or a
252 * signal may be delivered, etc.
253 *
254 * The real interrupt exit code is platform specific, e.g.,
255 * interrupt_exit_user_prepare / interrupt_exit_kernel_prepare for 64s.
256 *
257 * However arch_interrupt_nmi_exit_prepare does return directly to regs, because
258 * NMIs do not do "exit work" or replay soft-masked interrupts.
259 */
arch_interrupt_exit_prepare(struct pt_regs * regs)260 static inline void arch_interrupt_exit_prepare(struct pt_regs *regs)
261 {
262 if (user_mode(regs)) {
263 BUG_ON(regs_is_unrecoverable(regs));
264 BUG_ON(regs_irqs_disabled(regs));
265 /*
266 * We don't need to restore AMR on the way back to userspace for KUAP.
267 * AMR can only have been unlocked if we interrupted the kernel.
268 */
269 kuap_assert_locked();
270 }
271
272 /* irqentry_exit expects to be called with interrupts disabled */
273 local_irq_disable();
274 }
275
arch_interrupt_async_enter_prepare(struct pt_regs * regs)276 static inline void arch_interrupt_async_enter_prepare(struct pt_regs *regs)
277 {
278 #ifdef CONFIG_PPC64
279 /* Ensure arch_interrupt_enter_prepare does not enable MSR[EE] */
280 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
281 #endif
282 arch_interrupt_enter_prepare(regs);
283 #ifdef CONFIG_PPC_BOOK3S_64
284 /*
285 * RI=1 is set by arch_interrupt_enter_prepare, so this thread flags access
286 * has to come afterward (it can cause SLB faults).
287 */
288 if (cpu_has_feature(CPU_FTR_CTRL) &&
289 !test_thread_local_flags(_TLF_RUNLATCH))
290 __ppc64_runlatch_on();
291 #endif
292 }
293
arch_interrupt_async_exit_prepare(struct pt_regs * regs)294 static inline void arch_interrupt_async_exit_prepare(struct pt_regs *regs)
295 {
296 arch_interrupt_exit_prepare(regs);
297 }
298
299 struct interrupt_nmi_state {
300 #ifdef CONFIG_PPC64
301 u8 irq_soft_mask;
302 u8 irq_happened;
303 u8 ftrace_enabled;
304 u64 softe;
305 #endif
306 };
307
nmi_disables_ftrace(struct pt_regs * regs)308 static inline bool nmi_disables_ftrace(struct pt_regs *regs)
309 {
310 /* Allow DEC and PMI to be traced when they are soft-NMI */
311 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) {
312 if (TRAP(regs) == INTERRUPT_DECREMENTER)
313 return false;
314 if (TRAP(regs) == INTERRUPT_PERFMON)
315 return false;
316 }
317 if (IS_ENABLED(CONFIG_PPC_BOOK3E_64)) {
318 if (TRAP(regs) == INTERRUPT_PERFMON)
319 return false;
320 }
321
322 return true;
323 }
324
arch_interrupt_nmi_enter_prepare(struct pt_regs * regs,struct interrupt_nmi_state * state)325 static inline void arch_interrupt_nmi_enter_prepare(struct pt_regs *regs,
326 struct interrupt_nmi_state *state)
327 {
328 #ifdef CONFIG_PPC64
329 state->irq_soft_mask = local_paca->irq_soft_mask;
330 state->irq_happened = local_paca->irq_happened;
331 state->softe = regs->softe;
332
333 /*
334 * Set IRQS_ALL_DISABLED unconditionally so irqs_disabled() does
335 * the right thing, and set IRQ_HARD_DIS. We do not want to reconcile
336 * because that goes through irq tracing which we don't want in NMI.
337 */
338 local_paca->irq_soft_mask = IRQS_ALL_DISABLED;
339 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
340
341 if (!(regs->msr & MSR_EE) || is_implicit_soft_masked(regs)) {
342 /*
343 * Adjust regs->softe to be soft-masked if it had not been
344 * reconcied (e.g., interrupt entry with MSR[EE]=0 but softe
345 * not yet set disabled), or if it was in an implicit soft
346 * masked state. This makes regs_irqs_disabled(regs)
347 * behave as expected.
348 */
349 regs->softe = IRQS_ALL_DISABLED;
350 }
351
352 __hard_RI_enable();
353
354 /* Don't do any per-CPU operations until interrupt state is fixed */
355
356 if (nmi_disables_ftrace(regs)) {
357 state->ftrace_enabled = this_cpu_get_ftrace_enabled();
358 this_cpu_set_ftrace_enabled(0);
359 }
360 #endif
361 }
362
arch_interrupt_nmi_exit_prepare(struct pt_regs * regs,struct interrupt_nmi_state * state)363 static inline void arch_interrupt_nmi_exit_prepare(struct pt_regs *regs,
364 struct interrupt_nmi_state *state)
365 {
366 /*
367 * nmi does not call nap_adjust_return because nmi should not create
368 * new work to do (must use irq_work for that).
369 */
370
371 #ifdef CONFIG_PPC64
372 #ifdef CONFIG_PPC_BOOK3S
373 if (regs_irqs_disabled(regs)) {
374 unsigned long rst = search_kernel_restart_table(regs->nip);
375
376 if (rst)
377 regs_set_return_ip(regs, rst);
378 }
379 #endif
380
381 if (nmi_disables_ftrace(regs))
382 this_cpu_set_ftrace_enabled(state->ftrace_enabled);
383
384 /* Check we didn't change the pending interrupt mask. */
385 WARN_ON_ONCE((state->irq_happened | PACA_IRQ_HARD_DIS) != local_paca->irq_happened);
386 regs->softe = state->softe;
387 local_paca->irq_happened = state->irq_happened;
388 local_paca->irq_soft_mask = state->irq_soft_mask;
389 #endif
390 }
391
arch_enter_from_user_mode(struct pt_regs * regs)392 static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs)
393 {
394 kuap_lock();
395
396 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
397 BUG_ON(irq_soft_mask_return() != IRQS_ALL_DISABLED);
398
399 BUG_ON(regs_is_unrecoverable(regs));
400 BUG_ON(!user_mode(regs));
401 BUG_ON(regs_irqs_disabled(regs));
402
403 #ifdef CONFIG_PPC_PKEY
404 if (mmu_has_feature(MMU_FTR_PKEY) && trap_is_syscall(regs)) {
405 unsigned long amr, iamr;
406 bool flush_needed = false;
407 /*
408 * When entering from userspace we mostly have the AMR/IAMR
409 * different from kernel default values. Hence don't compare.
410 */
411 amr = mfspr(SPRN_AMR);
412 iamr = mfspr(SPRN_IAMR);
413 regs->amr = amr;
414 regs->iamr = iamr;
415 if (mmu_has_feature(MMU_FTR_KUAP)) {
416 mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
417 flush_needed = true;
418 }
419 if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP)) {
420 mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
421 flush_needed = true;
422 }
423 if (flush_needed)
424 isync();
425 }
426 #endif
427 kuap_assert_locked();
428 booke_restore_dbcr0();
429 account_cpu_user_entry();
430 account_stolen_time();
431
432 /*
433 * This is not required for the syscall exit path, but makes the
434 * stack frame look nicer. If this was initialised in the first stack
435 * frame, or if the unwinder was taught the first stack frame always
436 * returns to user with IRQS_ENABLED, this store could be avoided!
437 */
438 irq_soft_mask_regs_set_state(regs, IRQS_ENABLED);
439
440 /*
441 * If system call is called with TM active, set _TIF_RESTOREALL to
442 * prevent RFSCV being used to return to userspace, because POWER9
443 * TM implementation has problems with this instruction returning to
444 * transactional state. Final register values are not relevant because
445 * the transaction will be aborted upon return anyway. Or in the case
446 * of unsupported_scv SIGILL fault, the return state does not much
447 * matter because it's an edge case.
448 */
449 if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) &&
450 unlikely(MSR_TM_TRANSACTIONAL(regs->msr)))
451 set_bits(_TIF_RESTOREALL, ¤t_thread_info()->flags);
452
453 /*
454 * If the system call was made with a transaction active, doom it and
455 * return without performing the system call. Unless it was an
456 * unsupported scv vector, in which case it's treated like an illegal
457 * instruction.
458 */
459 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
460 if (unlikely(MSR_TM_TRANSACTIONAL(regs->msr)) &&
461 !trap_is_unsupported_scv(regs)) {
462 /* Enable TM in the kernel, and disable EE (for scv) */
463 hard_irq_disable();
464 mtmsr(mfmsr() | MSR_TM);
465
466 /* tabort, this dooms the transaction, nothing else */
467 asm volatile(".long 0x7c00071d | ((%0) << 16)"
468 :: "r"(TM_CAUSE_SYSCALL | TM_CAUSE_PERSISTENT));
469
470 /*
471 * Userspace will never see the return value. Execution will
472 * resume after the tbegin. of the aborted transaction with the
473 * checkpointed register state. A context switch could occur
474 * or signal delivered to the process before resuming the
475 * doomed transaction context, but that should all be handled
476 * as expected.
477 */
478 return;
479 }
480 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
481 }
482
483 #define arch_enter_from_user_mode arch_enter_from_user_mode
484
arch_exit_to_user_mode_prepare(struct pt_regs * regs,unsigned long ti_work)485 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
486 unsigned long ti_work)
487 {
488 unsigned long mathflags;
489
490 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && IS_ENABLED(CONFIG_PPC_FPU)) {
491 if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) &&
492 unlikely((ti_work & _TIF_RESTORE_TM))) {
493 restore_tm_state(regs);
494 } else {
495 mathflags = MSR_FP;
496
497 if (cpu_has_feature(CPU_FTR_VSX))
498 mathflags |= MSR_VEC | MSR_VSX;
499 else if (cpu_has_feature(CPU_FTR_ALTIVEC))
500 mathflags |= MSR_VEC;
501
502 /*
503 * If userspace MSR has all available FP bits set,
504 * then they are live and no need to restore. If not,
505 * it means the regs were given up and restore_math
506 * may decide to restore them (to avoid taking an FP
507 * fault).
508 */
509 if ((regs->msr & mathflags) != mathflags)
510 restore_math(regs);
511 }
512 }
513
514 check_return_regs_valid(regs);
515 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
516 local_paca->tm_scratch = regs->msr;
517 #endif
518 /* Restore user access locks last */
519 kuap_user_restore(regs);
520 }
521
522 #define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare
523
arch_exit_to_user_mode(void)524 static __always_inline void arch_exit_to_user_mode(void)
525 {
526 booke_load_dbcr0();
527
528 account_cpu_user_exit();
529 }
530
531 #define arch_exit_to_user_mode arch_exit_to_user_mode
532
533 #endif /* _ASM_PPC_ENTRY_COMMON_H */
534