xref: /linux/arch/powerpc/kernel/interrupt.c (revision 5379ef2a60431232b9bb01c6d3580b875123d723)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/context_tracking.h>
4 #include <linux/err.h>
5 #include <linux/compat.h>
6 #include <linux/sched/debug.h> /* for show_regs */
7 
8 #include <asm/asm-prototypes.h>
9 #include <asm/kup.h>
10 #include <asm/cputime.h>
11 #include <asm/hw_irq.h>
12 #include <asm/interrupt.h>
13 #include <asm/kprobes.h>
14 #include <asm/paca.h>
15 #include <asm/ptrace.h>
16 #include <asm/reg.h>
17 #include <asm/signal.h>
18 #include <asm/switch_to.h>
19 #include <asm/syscall.h>
20 #include <asm/time.h>
21 #include <asm/tm.h>
22 #include <asm/unistd.h>
23 
24 #if defined(CONFIG_PPC_ADV_DEBUG_REGS) && defined(CONFIG_PPC32)
25 unsigned long global_dbcr0[NR_CPUS];
26 #endif
27 
28 typedef long (*syscall_fn)(long, long, long, long, long, long);
29 
30 #ifdef CONFIG_PPC_BOOK3S_64
31 DEFINE_STATIC_KEY_FALSE(interrupt_exit_not_reentrant);
32 static inline bool exit_must_hard_disable(void)
33 {
34 	return static_branch_unlikely(&interrupt_exit_not_reentrant);
35 }
36 #else
37 static inline bool exit_must_hard_disable(void)
38 {
39 	return true;
40 }
41 #endif
42 
43 /*
44  * local irqs must be disabled. Returns false if the caller must re-enable
45  * them, check for new work, and try again.
46  *
47  * This should be called with local irqs disabled, but if they were previously
48  * enabled when the interrupt handler returns (indicating a process-context /
49  * synchronous interrupt) then irqs_enabled should be true.
50  *
51  * restartable is true then EE/RI can be left on because interrupts are handled
52  * with a restart sequence.
53  */
54 static notrace __always_inline bool prep_irq_for_enabled_exit(bool restartable)
55 {
56 	/* This must be done with RI=1 because tracing may touch vmaps */
57 	trace_hardirqs_on();
58 
59 	if (exit_must_hard_disable() || !restartable)
60 		__hard_EE_RI_disable();
61 
62 #ifdef CONFIG_PPC64
63 	/* This pattern matches prep_irq_for_idle */
64 	if (unlikely(lazy_irq_pending_nocheck())) {
65 		if (exit_must_hard_disable() || !restartable) {
66 			local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
67 			__hard_RI_enable();
68 		}
69 		trace_hardirqs_off();
70 
71 		return false;
72 	}
73 #endif
74 	return true;
75 }
76 
77 /* Has to run notrace because it is entered not completely "reconciled" */
78 notrace long system_call_exception(long r3, long r4, long r5,
79 				   long r6, long r7, long r8,
80 				   unsigned long r0, struct pt_regs *regs)
81 {
82 	syscall_fn f;
83 
84 	kuep_lock();
85 
86 	regs->orig_gpr3 = r3;
87 
88 	if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
89 		BUG_ON(irq_soft_mask_return() != IRQS_ALL_DISABLED);
90 
91 	trace_hardirqs_off(); /* finish reconciling */
92 
93 	CT_WARN_ON(ct_state() == CONTEXT_KERNEL);
94 	user_exit_irqoff();
95 
96 	BUG_ON(regs_is_unrecoverable(regs));
97 	BUG_ON(!(regs->msr & MSR_PR));
98 	BUG_ON(arch_irq_disabled_regs(regs));
99 
100 #ifdef CONFIG_PPC_PKEY
101 	if (mmu_has_feature(MMU_FTR_PKEY)) {
102 		unsigned long amr, iamr;
103 		bool flush_needed = false;
104 		/*
105 		 * When entering from userspace we mostly have the AMR/IAMR
106 		 * different from kernel default values. Hence don't compare.
107 		 */
108 		amr = mfspr(SPRN_AMR);
109 		iamr = mfspr(SPRN_IAMR);
110 		regs->amr  = amr;
111 		regs->iamr = iamr;
112 		if (mmu_has_feature(MMU_FTR_BOOK3S_KUAP)) {
113 			mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
114 			flush_needed = true;
115 		}
116 		if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP)) {
117 			mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
118 			flush_needed = true;
119 		}
120 		if (flush_needed)
121 			isync();
122 	} else
123 #endif
124 		kuap_assert_locked();
125 
126 	booke_restore_dbcr0();
127 
128 	account_cpu_user_entry();
129 
130 	account_stolen_time();
131 
132 	/*
133 	 * This is not required for the syscall exit path, but makes the
134 	 * stack frame look nicer. If this was initialised in the first stack
135 	 * frame, or if the unwinder was taught the first stack frame always
136 	 * returns to user with IRQS_ENABLED, this store could be avoided!
137 	 */
138 	irq_soft_mask_regs_set_state(regs, IRQS_ENABLED);
139 
140 	/*
141 	 * If the system call was made with a transaction active, doom it and
142 	 * return without performing the system call. Unless it was an
143 	 * unsupported scv vector, in which case it's treated like an illegal
144 	 * instruction.
145 	 */
146 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
147 	if (unlikely(MSR_TM_TRANSACTIONAL(regs->msr)) &&
148 	    !trap_is_unsupported_scv(regs)) {
149 		/* Enable TM in the kernel, and disable EE (for scv) */
150 		hard_irq_disable();
151 		mtmsr(mfmsr() | MSR_TM);
152 
153 		/* tabort, this dooms the transaction, nothing else */
154 		asm volatile(".long 0x7c00071d | ((%0) << 16)"
155 				:: "r"(TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT));
156 
157 		/*
158 		 * Userspace will never see the return value. Execution will
159 		 * resume after the tbegin. of the aborted transaction with the
160 		 * checkpointed register state. A context switch could occur
161 		 * or signal delivered to the process before resuming the
162 		 * doomed transaction context, but that should all be handled
163 		 * as expected.
164 		 */
165 		return -ENOSYS;
166 	}
167 #endif // CONFIG_PPC_TRANSACTIONAL_MEM
168 
169 	local_irq_enable();
170 
171 	if (unlikely(current_thread_info()->flags & _TIF_SYSCALL_DOTRACE)) {
172 		if (unlikely(trap_is_unsupported_scv(regs))) {
173 			/* Unsupported scv vector */
174 			_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
175 			return regs->gpr[3];
176 		}
177 		/*
178 		 * We use the return value of do_syscall_trace_enter() as the
179 		 * syscall number. If the syscall was rejected for any reason
180 		 * do_syscall_trace_enter() returns an invalid syscall number
181 		 * and the test against NR_syscalls will fail and the return
182 		 * value to be used is in regs->gpr[3].
183 		 */
184 		r0 = do_syscall_trace_enter(regs);
185 		if (unlikely(r0 >= NR_syscalls))
186 			return regs->gpr[3];
187 		r3 = regs->gpr[3];
188 		r4 = regs->gpr[4];
189 		r5 = regs->gpr[5];
190 		r6 = regs->gpr[6];
191 		r7 = regs->gpr[7];
192 		r8 = regs->gpr[8];
193 
194 	} else if (unlikely(r0 >= NR_syscalls)) {
195 		if (unlikely(trap_is_unsupported_scv(regs))) {
196 			/* Unsupported scv vector */
197 			_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
198 			return regs->gpr[3];
199 		}
200 		return -ENOSYS;
201 	}
202 
203 	/* May be faster to do array_index_nospec? */
204 	barrier_nospec();
205 
206 	if (unlikely(is_compat_task())) {
207 		f = (void *)compat_sys_call_table[r0];
208 
209 		r3 &= 0x00000000ffffffffULL;
210 		r4 &= 0x00000000ffffffffULL;
211 		r5 &= 0x00000000ffffffffULL;
212 		r6 &= 0x00000000ffffffffULL;
213 		r7 &= 0x00000000ffffffffULL;
214 		r8 &= 0x00000000ffffffffULL;
215 
216 	} else {
217 		f = (void *)sys_call_table[r0];
218 	}
219 
220 	return f(r3, r4, r5, r6, r7, r8);
221 }
222 
223 static notrace void booke_load_dbcr0(void)
224 {
225 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
226 	unsigned long dbcr0 = current->thread.debug.dbcr0;
227 
228 	if (likely(!(dbcr0 & DBCR0_IDM)))
229 		return;
230 
231 	/*
232 	 * Check to see if the dbcr0 register is set up to debug.
233 	 * Use the internal debug mode bit to do this.
234 	 */
235 	mtmsr(mfmsr() & ~MSR_DE);
236 	if (IS_ENABLED(CONFIG_PPC32)) {
237 		isync();
238 		global_dbcr0[smp_processor_id()] = mfspr(SPRN_DBCR0);
239 	}
240 	mtspr(SPRN_DBCR0, dbcr0);
241 	mtspr(SPRN_DBSR, -1);
242 #endif
243 }
244 
245 static void check_return_regs_valid(struct pt_regs *regs)
246 {
247 #ifdef CONFIG_PPC_BOOK3S_64
248 	unsigned long trap, srr0, srr1;
249 	static bool warned;
250 	u8 *validp;
251 	char *h;
252 
253 	if (trap_is_scv(regs))
254 		return;
255 
256 	trap = regs->trap;
257 	// EE in HV mode sets HSRRs like 0xea0
258 	if (cpu_has_feature(CPU_FTR_HVMODE) && trap == INTERRUPT_EXTERNAL)
259 		trap = 0xea0;
260 
261 	switch (trap) {
262 	case 0x980:
263 	case INTERRUPT_H_DATA_STORAGE:
264 	case 0xe20:
265 	case 0xe40:
266 	case INTERRUPT_HMI:
267 	case 0xe80:
268 	case 0xea0:
269 	case INTERRUPT_H_FAC_UNAVAIL:
270 	case 0x1200:
271 	case 0x1500:
272 	case 0x1600:
273 	case 0x1800:
274 		validp = &local_paca->hsrr_valid;
275 		if (!*validp)
276 			return;
277 
278 		srr0 = mfspr(SPRN_HSRR0);
279 		srr1 = mfspr(SPRN_HSRR1);
280 		h = "H";
281 
282 		break;
283 	default:
284 		validp = &local_paca->srr_valid;
285 		if (!*validp)
286 			return;
287 
288 		srr0 = mfspr(SPRN_SRR0);
289 		srr1 = mfspr(SPRN_SRR1);
290 		h = "";
291 		break;
292 	}
293 
294 	if (srr0 == regs->nip && srr1 == regs->msr)
295 		return;
296 
297 	/*
298 	 * A NMI / soft-NMI interrupt may have come in after we found
299 	 * srr_valid and before the SRRs are loaded. The interrupt then
300 	 * comes in and clobbers SRRs and clears srr_valid. Then we load
301 	 * the SRRs here and test them above and find they don't match.
302 	 *
303 	 * Test validity again after that, to catch such false positives.
304 	 *
305 	 * This test in general will have some window for false negatives
306 	 * and may not catch and fix all such cases if an NMI comes in
307 	 * later and clobbers SRRs without clearing srr_valid, but hopefully
308 	 * such things will get caught most of the time, statistically
309 	 * enough to be able to get a warning out.
310 	 */
311 	barrier();
312 
313 	if (!*validp)
314 		return;
315 
316 	if (!warned) {
317 		warned = true;
318 		printk("%sSRR0 was: %lx should be: %lx\n", h, srr0, regs->nip);
319 		printk("%sSRR1 was: %lx should be: %lx\n", h, srr1, regs->msr);
320 		show_regs(regs);
321 	}
322 
323 	*validp = 0; /* fixup */
324 #endif
325 }
326 
327 static notrace unsigned long
328 interrupt_exit_user_prepare_main(unsigned long ret, struct pt_regs *regs)
329 {
330 	unsigned long ti_flags;
331 
332 again:
333 	ti_flags = READ_ONCE(current_thread_info()->flags);
334 	while (unlikely(ti_flags & (_TIF_USER_WORK_MASK & ~_TIF_RESTORE_TM))) {
335 		local_irq_enable();
336 		if (ti_flags & _TIF_NEED_RESCHED) {
337 			schedule();
338 		} else {
339 			/*
340 			 * SIGPENDING must restore signal handler function
341 			 * argument GPRs, and some non-volatiles (e.g., r1).
342 			 * Restore all for now. This could be made lighter.
343 			 */
344 			if (ti_flags & _TIF_SIGPENDING)
345 				ret |= _TIF_RESTOREALL;
346 			do_notify_resume(regs, ti_flags);
347 		}
348 		local_irq_disable();
349 		ti_flags = READ_ONCE(current_thread_info()->flags);
350 	}
351 
352 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && IS_ENABLED(CONFIG_PPC_FPU)) {
353 		if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) &&
354 				unlikely((ti_flags & _TIF_RESTORE_TM))) {
355 			restore_tm_state(regs);
356 		} else {
357 			unsigned long mathflags = MSR_FP;
358 
359 			if (cpu_has_feature(CPU_FTR_VSX))
360 				mathflags |= MSR_VEC | MSR_VSX;
361 			else if (cpu_has_feature(CPU_FTR_ALTIVEC))
362 				mathflags |= MSR_VEC;
363 
364 			/*
365 			 * If userspace MSR has all available FP bits set,
366 			 * then they are live and no need to restore. If not,
367 			 * it means the regs were given up and restore_math
368 			 * may decide to restore them (to avoid taking an FP
369 			 * fault).
370 			 */
371 			if ((regs->msr & mathflags) != mathflags)
372 				restore_math(regs);
373 		}
374 	}
375 
376 	check_return_regs_valid(regs);
377 
378 	user_enter_irqoff();
379 	if (!prep_irq_for_enabled_exit(true)) {
380 		user_exit_irqoff();
381 		local_irq_enable();
382 		local_irq_disable();
383 		goto again;
384 	}
385 
386 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
387 	local_paca->tm_scratch = regs->msr;
388 #endif
389 
390 	booke_load_dbcr0();
391 
392 	account_cpu_user_exit();
393 
394 	/* Restore user access locks last */
395 	kuap_user_restore(regs);
396 	kuep_unlock();
397 
398 	return ret;
399 }
400 
401 /*
402  * This should be called after a syscall returns, with r3 the return value
403  * from the syscall. If this function returns non-zero, the system call
404  * exit assembly should additionally load all GPR registers and CTR and XER
405  * from the interrupt frame.
406  *
407  * The function graph tracer can not trace the return side of this function,
408  * because RI=0 and soft mask state is "unreconciled", so it is marked notrace.
409  */
410 notrace unsigned long syscall_exit_prepare(unsigned long r3,
411 					   struct pt_regs *regs,
412 					   long scv)
413 {
414 	unsigned long ti_flags;
415 	unsigned long ret = 0;
416 	bool is_not_scv = !IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !scv;
417 
418 	CT_WARN_ON(ct_state() == CONTEXT_USER);
419 
420 	kuap_assert_locked();
421 
422 	regs->result = r3;
423 
424 	/* Check whether the syscall is issued inside a restartable sequence */
425 	rseq_syscall(regs);
426 
427 	ti_flags = current_thread_info()->flags;
428 
429 	if (unlikely(r3 >= (unsigned long)-MAX_ERRNO) && is_not_scv) {
430 		if (likely(!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) {
431 			r3 = -r3;
432 			regs->ccr |= 0x10000000; /* Set SO bit in CR */
433 		}
434 	}
435 
436 	if (unlikely(ti_flags & _TIF_PERSYSCALL_MASK)) {
437 		if (ti_flags & _TIF_RESTOREALL)
438 			ret = _TIF_RESTOREALL;
439 		else
440 			regs->gpr[3] = r3;
441 		clear_bits(_TIF_PERSYSCALL_MASK, &current_thread_info()->flags);
442 	} else {
443 		regs->gpr[3] = r3;
444 	}
445 
446 	if (unlikely(ti_flags & _TIF_SYSCALL_DOTRACE)) {
447 		do_syscall_trace_leave(regs);
448 		ret |= _TIF_RESTOREALL;
449 	}
450 
451 	local_irq_disable();
452 	ret = interrupt_exit_user_prepare_main(ret, regs);
453 
454 #ifdef CONFIG_PPC64
455 	regs->exit_result = ret;
456 #endif
457 
458 	return ret;
459 }
460 
461 #ifdef CONFIG_PPC64
462 notrace unsigned long syscall_exit_restart(unsigned long r3, struct pt_regs *regs)
463 {
464 	/*
465 	 * This is called when detecting a soft-pending interrupt as well as
466 	 * an alternate-return interrupt. So we can't just have the alternate
467 	 * return path clear SRR1[MSR] and set PACA_IRQ_HARD_DIS (unless
468 	 * the soft-pending case were to fix things up as well). RI might be
469 	 * disabled, in which case it gets re-enabled by __hard_irq_disable().
470 	 */
471 	__hard_irq_disable();
472 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
473 
474 #ifdef CONFIG_PPC_BOOK3S_64
475 	set_kuap(AMR_KUAP_BLOCKED);
476 #endif
477 
478 	trace_hardirqs_off();
479 	user_exit_irqoff();
480 	account_cpu_user_entry();
481 
482 	BUG_ON(!user_mode(regs));
483 
484 	regs->exit_result = interrupt_exit_user_prepare_main(regs->exit_result, regs);
485 
486 	return regs->exit_result;
487 }
488 #endif
489 
490 notrace unsigned long interrupt_exit_user_prepare(struct pt_regs *regs)
491 {
492 	unsigned long ret;
493 
494 	BUG_ON(regs_is_unrecoverable(regs));
495 	BUG_ON(arch_irq_disabled_regs(regs));
496 	CT_WARN_ON(ct_state() == CONTEXT_USER);
497 
498 	/*
499 	 * We don't need to restore AMR on the way back to userspace for KUAP.
500 	 * AMR can only have been unlocked if we interrupted the kernel.
501 	 */
502 	kuap_assert_locked();
503 
504 	local_irq_disable();
505 
506 	ret = interrupt_exit_user_prepare_main(0, regs);
507 
508 #ifdef CONFIG_PPC64
509 	regs->exit_result = ret;
510 #endif
511 
512 	return ret;
513 }
514 
515 void preempt_schedule_irq(void);
516 
517 notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs)
518 {
519 	unsigned long flags;
520 	unsigned long ret = 0;
521 	unsigned long kuap;
522 	bool stack_store = current_thread_info()->flags &
523 						_TIF_EMULATE_STACK_STORE;
524 
525 	if (regs_is_unrecoverable(regs))
526 		unrecoverable_exception(regs);
527 	/*
528 	 * CT_WARN_ON comes here via program_check_exception,
529 	 * so avoid recursion.
530 	 */
531 	if (TRAP(regs) != INTERRUPT_PROGRAM)
532 		CT_WARN_ON(ct_state() == CONTEXT_USER);
533 
534 	kuap = kuap_get_and_assert_locked();
535 
536 	local_irq_save(flags);
537 
538 	if (!arch_irq_disabled_regs(regs)) {
539 		/* Returning to a kernel context with local irqs enabled. */
540 		WARN_ON_ONCE(!(regs->msr & MSR_EE));
541 again:
542 		if (IS_ENABLED(CONFIG_PREEMPT)) {
543 			/* Return to preemptible kernel context */
544 			if (unlikely(current_thread_info()->flags & _TIF_NEED_RESCHED)) {
545 				if (preempt_count() == 0)
546 					preempt_schedule_irq();
547 			}
548 		}
549 
550 		check_return_regs_valid(regs);
551 
552 		/*
553 		 * Stack store exit can't be restarted because the interrupt
554 		 * stack frame might have been clobbered.
555 		 */
556 		if (!prep_irq_for_enabled_exit(unlikely(stack_store))) {
557 			/*
558 			 * Replay pending soft-masked interrupts now. Don't
559 			 * just local_irq_enabe(); local_irq_disable(); because
560 			 * if we are returning from an asynchronous interrupt
561 			 * here, another one might hit after irqs are enabled,
562 			 * and it would exit via this same path allowing
563 			 * another to fire, and so on unbounded.
564 			 */
565 			hard_irq_disable();
566 			replay_soft_interrupts();
567 			/* Took an interrupt, may have more exit work to do. */
568 			goto again;
569 		}
570 #ifdef CONFIG_PPC64
571 		/*
572 		 * An interrupt may clear MSR[EE] and set this concurrently,
573 		 * but it will be marked pending and the exit will be retried.
574 		 * This leaves a racy window where MSR[EE]=0 and HARD_DIS is
575 		 * clear, until interrupt_exit_kernel_restart() calls
576 		 * hard_irq_disable(), which will set HARD_DIS again.
577 		 */
578 		local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
579 
580 	} else {
581 		check_return_regs_valid(regs);
582 
583 		if (unlikely(stack_store))
584 			__hard_EE_RI_disable();
585 		/*
586 		 * Returning to a kernel context with local irqs disabled.
587 		 * Here, if EE was enabled in the interrupted context, enable
588 		 * it on return as well. A problem exists here where a soft
589 		 * masked interrupt may have cleared MSR[EE] and set HARD_DIS
590 		 * here, and it will still exist on return to the caller. This
591 		 * will be resolved by the masked interrupt firing again.
592 		 */
593 		if (regs->msr & MSR_EE)
594 			local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
595 #endif /* CONFIG_PPC64 */
596 	}
597 
598 	if (unlikely(stack_store)) {
599 		clear_bits(_TIF_EMULATE_STACK_STORE, &current_thread_info()->flags);
600 		ret = 1;
601 	}
602 
603 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
604 	local_paca->tm_scratch = regs->msr;
605 #endif
606 
607 	/*
608 	 * 64s does not want to mfspr(SPRN_AMR) here, because this comes after
609 	 * mtmsr, which would cause Read-After-Write stalls. Hence, take the
610 	 * AMR value from the check above.
611 	 */
612 	kuap_kernel_restore(regs, kuap);
613 
614 	return ret;
615 }
616 
617 #ifdef CONFIG_PPC64
618 notrace unsigned long interrupt_exit_user_restart(struct pt_regs *regs)
619 {
620 	__hard_irq_disable();
621 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
622 
623 #ifdef CONFIG_PPC_BOOK3S_64
624 	set_kuap(AMR_KUAP_BLOCKED);
625 #endif
626 
627 	trace_hardirqs_off();
628 	user_exit_irqoff();
629 	account_cpu_user_entry();
630 
631 	BUG_ON(!user_mode(regs));
632 
633 	regs->exit_result |= interrupt_exit_user_prepare(regs);
634 
635 	return regs->exit_result;
636 }
637 
638 /*
639  * No real need to return a value here because the stack store case does not
640  * get restarted.
641  */
642 notrace unsigned long interrupt_exit_kernel_restart(struct pt_regs *regs)
643 {
644 	__hard_irq_disable();
645 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
646 
647 #ifdef CONFIG_PPC_BOOK3S_64
648 	set_kuap(AMR_KUAP_BLOCKED);
649 #endif
650 
651 	if (regs->softe == IRQS_ENABLED)
652 		trace_hardirqs_off();
653 
654 	BUG_ON(user_mode(regs));
655 
656 	return interrupt_exit_kernel_prepare(regs);
657 }
658 #endif
659