1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_POWERPC_INTERRUPT_H
3 #define _ASM_POWERPC_INTERRUPT_H
4 
5 /* BookE/4xx */
6 #define INTERRUPT_CRITICAL_INPUT  0x100
7 
8 /* BookE */
9 #define INTERRUPT_DEBUG           0xd00
10 #ifdef CONFIG_BOOKE
11 #define INTERRUPT_PERFMON         0x260
12 #define INTERRUPT_DOORBELL        0x280
13 #endif
14 
15 /* BookS/4xx/8xx */
16 #define INTERRUPT_MACHINE_CHECK   0x200
17 
18 /* BookS/8xx */
19 #define INTERRUPT_SYSTEM_RESET    0x100
20 
21 /* BookS */
22 #define INTERRUPT_DATA_SEGMENT    0x380
23 #define INTERRUPT_INST_SEGMENT    0x480
24 #define INTERRUPT_TRACE           0xd00
25 #define INTERRUPT_H_DATA_STORAGE  0xe00
26 #define INTERRUPT_HMI			0xe60
27 #define INTERRUPT_H_FAC_UNAVAIL   0xf80
28 #ifdef CONFIG_PPC_BOOK3S
29 #define INTERRUPT_DOORBELL        0xa00
30 #define INTERRUPT_PERFMON         0xf00
31 #define INTERRUPT_ALTIVEC_UNAVAIL	0xf20
32 #endif
33 
34 /* BookE/BookS/4xx/8xx */
35 #define INTERRUPT_DATA_STORAGE    0x300
36 #define INTERRUPT_INST_STORAGE    0x400
37 #define INTERRUPT_EXTERNAL		0x500
38 #define INTERRUPT_ALIGNMENT       0x600
39 #define INTERRUPT_PROGRAM         0x700
40 #define INTERRUPT_SYSCALL         0xc00
41 #define INTERRUPT_TRACE			0xd00
42 
43 /* BookE/BookS/44x */
44 #define INTERRUPT_FP_UNAVAIL      0x800
45 
46 /* BookE/BookS/44x/8xx */
47 #define INTERRUPT_DECREMENTER     0x900
48 
49 #ifndef INTERRUPT_PERFMON
50 #define INTERRUPT_PERFMON         0x0
51 #endif
52 
53 /* 8xx */
54 #define INTERRUPT_SOFT_EMU_8xx		0x1000
55 #define INTERRUPT_INST_TLB_MISS_8xx	0x1100
56 #define INTERRUPT_DATA_TLB_MISS_8xx	0x1200
57 #define INTERRUPT_INST_TLB_ERROR_8xx	0x1300
58 #define INTERRUPT_DATA_TLB_ERROR_8xx	0x1400
59 #define INTERRUPT_DATA_BREAKPOINT_8xx	0x1c00
60 #define INTERRUPT_INST_BREAKPOINT_8xx	0x1d00
61 
62 /* 603 */
63 #define INTERRUPT_INST_TLB_MISS_603		0x1000
64 #define INTERRUPT_DATA_LOAD_TLB_MISS_603	0x1100
65 #define INTERRUPT_DATA_STORE_TLB_MISS_603	0x1200
66 
67 #ifndef __ASSEMBLY__
68 
69 #include <linux/context_tracking.h>
70 #include <linux/hardirq.h>
71 #include <asm/cputime.h>
72 #include <asm/ftrace.h>
73 #include <asm/kprobes.h>
74 #include <asm/runlatch.h>
75 
76 static inline void nap_adjust_return(struct pt_regs *regs)
77 {
78 #ifdef CONFIG_PPC_970_NAP
79 	if (unlikely(test_thread_local_flags(_TLF_NAPPING))) {
80 		/* Can avoid a test-and-clear because NMIs do not call this */
81 		clear_thread_local_flags(_TLF_NAPPING);
82 		regs->nip = (unsigned long)power4_idle_nap_return;
83 	}
84 #endif
85 }
86 
87 struct interrupt_state {
88 };
89 
90 static inline void booke_restore_dbcr0(void)
91 {
92 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
93 	unsigned long dbcr0 = current->thread.debug.dbcr0;
94 
95 	if (IS_ENABLED(CONFIG_PPC32) && unlikely(dbcr0 & DBCR0_IDM)) {
96 		mtspr(SPRN_DBSR, -1);
97 		mtspr(SPRN_DBCR0, global_dbcr0[smp_processor_id()]);
98 	}
99 #endif
100 }
101 
102 static inline void interrupt_enter_prepare(struct pt_regs *regs, struct interrupt_state *state)
103 {
104 #ifdef CONFIG_PPC32
105 	if (!arch_irq_disabled_regs(regs))
106 		trace_hardirqs_off();
107 
108 	if (user_mode(regs)) {
109 		kuep_lock();
110 		account_cpu_user_entry();
111 	} else {
112 		kuap_save_and_lock(regs);
113 	}
114 #endif
115 
116 #ifdef CONFIG_PPC64
117 	if (irq_soft_mask_set_return(IRQS_ALL_DISABLED) == IRQS_ENABLED)
118 		trace_hardirqs_off();
119 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
120 
121 	if (user_mode(regs)) {
122 		CT_WARN_ON(ct_state() != CONTEXT_USER);
123 		user_exit_irqoff();
124 
125 		account_cpu_user_entry();
126 		account_stolen_time();
127 	} else {
128 		/*
129 		 * CT_WARN_ON comes here via program_check_exception,
130 		 * so avoid recursion.
131 		 */
132 		if (TRAP(regs) != INTERRUPT_PROGRAM)
133 			CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
134 	}
135 #endif
136 
137 	booke_restore_dbcr0();
138 }
139 
140 /*
141  * Care should be taken to note that interrupt_exit_prepare and
142  * interrupt_async_exit_prepare do not necessarily return immediately to
143  * regs context (e.g., if regs is usermode, we don't necessarily return to
144  * user mode). Other interrupts might be taken between here and return,
145  * context switch / preemption may occur in the exit path after this, or a
146  * signal may be delivered, etc.
147  *
148  * The real interrupt exit code is platform specific, e.g.,
149  * interrupt_exit_user_prepare / interrupt_exit_kernel_prepare for 64s.
150  *
151  * However interrupt_nmi_exit_prepare does return directly to regs, because
152  * NMIs do not do "exit work" or replay soft-masked interrupts.
153  */
154 static inline void interrupt_exit_prepare(struct pt_regs *regs, struct interrupt_state *state)
155 {
156 	if (user_mode(regs))
157 		kuep_unlock();
158 }
159 
160 static inline void interrupt_async_enter_prepare(struct pt_regs *regs, struct interrupt_state *state)
161 {
162 #ifdef CONFIG_PPC_BOOK3S_64
163 	if (cpu_has_feature(CPU_FTR_CTRL) &&
164 	    !test_thread_local_flags(_TLF_RUNLATCH))
165 		__ppc64_runlatch_on();
166 #endif
167 
168 	interrupt_enter_prepare(regs, state);
169 	irq_enter();
170 }
171 
172 static inline void interrupt_async_exit_prepare(struct pt_regs *regs, struct interrupt_state *state)
173 {
174 	/*
175 	 * Adjust at exit so the main handler sees the true NIA. This must
176 	 * come before irq_exit() because irq_exit can enable interrupts, and
177 	 * if another interrupt is taken before nap_adjust_return has run
178 	 * here, then that interrupt would return directly to idle nap return.
179 	 */
180 	nap_adjust_return(regs);
181 
182 	irq_exit();
183 	interrupt_exit_prepare(regs, state);
184 }
185 
186 struct interrupt_nmi_state {
187 #ifdef CONFIG_PPC64
188 	u8 irq_soft_mask;
189 	u8 irq_happened;
190 	u8 ftrace_enabled;
191 #endif
192 };
193 
194 static inline bool nmi_disables_ftrace(struct pt_regs *regs)
195 {
196 	/* Allow DEC and PMI to be traced when they are soft-NMI */
197 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) {
198 		if (TRAP(regs) == INTERRUPT_DECREMENTER)
199 		       return false;
200 		if (TRAP(regs) == INTERRUPT_PERFMON)
201 		       return false;
202 	}
203 	if (IS_ENABLED(CONFIG_PPC_BOOK3E)) {
204 		if (TRAP(regs) == INTERRUPT_PERFMON)
205 			return false;
206 	}
207 
208 	return true;
209 }
210 
211 static inline void interrupt_nmi_enter_prepare(struct pt_regs *regs, struct interrupt_nmi_state *state)
212 {
213 #ifdef CONFIG_PPC64
214 	state->irq_soft_mask = local_paca->irq_soft_mask;
215 	state->irq_happened = local_paca->irq_happened;
216 
217 	/*
218 	 * Set IRQS_ALL_DISABLED unconditionally so irqs_disabled() does
219 	 * the right thing, and set IRQ_HARD_DIS. We do not want to reconcile
220 	 * because that goes through irq tracing which we don't want in NMI.
221 	 */
222 	local_paca->irq_soft_mask = IRQS_ALL_DISABLED;
223 	local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
224 
225 	/* Don't do any per-CPU operations until interrupt state is fixed */
226 
227 	if (nmi_disables_ftrace(regs)) {
228 		state->ftrace_enabled = this_cpu_get_ftrace_enabled();
229 		this_cpu_set_ftrace_enabled(0);
230 	}
231 #endif
232 
233 	/*
234 	 * Do not use nmi_enter() for pseries hash guest taking a real-mode
235 	 * NMI because not everything it touches is within the RMA limit.
236 	 */
237 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) ||
238 			!firmware_has_feature(FW_FEATURE_LPAR) ||
239 			radix_enabled() || (mfmsr() & MSR_DR))
240 		nmi_enter();
241 }
242 
243 static inline void interrupt_nmi_exit_prepare(struct pt_regs *regs, struct interrupt_nmi_state *state)
244 {
245 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) ||
246 			!firmware_has_feature(FW_FEATURE_LPAR) ||
247 			radix_enabled() || (mfmsr() & MSR_DR))
248 		nmi_exit();
249 
250 	/*
251 	 * nmi does not call nap_adjust_return because nmi should not create
252 	 * new work to do (must use irq_work for that).
253 	 */
254 
255 #ifdef CONFIG_PPC64
256 	if (nmi_disables_ftrace(regs))
257 		this_cpu_set_ftrace_enabled(state->ftrace_enabled);
258 
259 	/* Check we didn't change the pending interrupt mask. */
260 	WARN_ON_ONCE((state->irq_happened | PACA_IRQ_HARD_DIS) != local_paca->irq_happened);
261 	local_paca->irq_happened = state->irq_happened;
262 	local_paca->irq_soft_mask = state->irq_soft_mask;
263 #endif
264 }
265 
266 /*
267  * Don't use noinstr here like x86, but rather add NOKPROBE_SYMBOL to each
268  * function definition. The reason for this is the noinstr section is placed
269  * after the main text section, i.e., very far away from the interrupt entry
270  * asm. That creates problems with fitting linker stubs when building large
271  * kernels.
272  */
273 #define interrupt_handler __visible noinline notrace __no_kcsan __no_sanitize_address
274 
275 /**
276  * DECLARE_INTERRUPT_HANDLER_RAW - Declare raw interrupt handler function
277  * @func:	Function name of the entry point
278  * @returns:	Returns a value back to asm caller
279  */
280 #define DECLARE_INTERRUPT_HANDLER_RAW(func)				\
281 	__visible long func(struct pt_regs *regs)
282 
283 /**
284  * DEFINE_INTERRUPT_HANDLER_RAW - Define raw interrupt handler function
285  * @func:	Function name of the entry point
286  * @returns:	Returns a value back to asm caller
287  *
288  * @func is called from ASM entry code.
289  *
290  * This is a plain function which does no tracing, reconciling, etc.
291  * The macro is written so it acts as function definition. Append the
292  * body with a pair of curly brackets.
293  *
294  * raw interrupt handlers must not enable or disable interrupts, or
295  * schedule, tracing and instrumentation (ftrace, lockdep, etc) would
296  * not be advisable either, although may be possible in a pinch, the
297  * trace will look odd at least.
298  *
299  * A raw handler may call one of the other interrupt handler functions
300  * to be converted into that interrupt context without these restrictions.
301  *
302  * On PPC64, _RAW handlers may return with fast_interrupt_return.
303  *
304  * Specific handlers may have additional restrictions.
305  */
306 #define DEFINE_INTERRUPT_HANDLER_RAW(func)				\
307 static __always_inline long ____##func(struct pt_regs *regs);		\
308 									\
309 interrupt_handler long func(struct pt_regs *regs)			\
310 {									\
311 	long ret;							\
312 									\
313 	ret = ____##func (regs);					\
314 									\
315 	return ret;							\
316 }									\
317 NOKPROBE_SYMBOL(func);							\
318 									\
319 static __always_inline long ____##func(struct pt_regs *regs)
320 
321 /**
322  * DECLARE_INTERRUPT_HANDLER - Declare synchronous interrupt handler function
323  * @func:	Function name of the entry point
324  */
325 #define DECLARE_INTERRUPT_HANDLER(func)					\
326 	__visible void func(struct pt_regs *regs)
327 
328 /**
329  * DEFINE_INTERRUPT_HANDLER - Define synchronous interrupt handler function
330  * @func:	Function name of the entry point
331  *
332  * @func is called from ASM entry code.
333  *
334  * The macro is written so it acts as function definition. Append the
335  * body with a pair of curly brackets.
336  */
337 #define DEFINE_INTERRUPT_HANDLER(func)					\
338 static __always_inline void ____##func(struct pt_regs *regs);		\
339 									\
340 interrupt_handler void func(struct pt_regs *regs)			\
341 {									\
342 	struct interrupt_state state;					\
343 									\
344 	interrupt_enter_prepare(regs, &state);				\
345 									\
346 	____##func (regs);						\
347 									\
348 	interrupt_exit_prepare(regs, &state);				\
349 }									\
350 NOKPROBE_SYMBOL(func);							\
351 									\
352 static __always_inline void ____##func(struct pt_regs *regs)
353 
354 /**
355  * DECLARE_INTERRUPT_HANDLER_RET - Declare synchronous interrupt handler function
356  * @func:	Function name of the entry point
357  * @returns:	Returns a value back to asm caller
358  */
359 #define DECLARE_INTERRUPT_HANDLER_RET(func)				\
360 	__visible long func(struct pt_regs *regs)
361 
362 /**
363  * DEFINE_INTERRUPT_HANDLER_RET - Define synchronous interrupt handler function
364  * @func:	Function name of the entry point
365  * @returns:	Returns a value back to asm caller
366  *
367  * @func is called from ASM entry code.
368  *
369  * The macro is written so it acts as function definition. Append the
370  * body with a pair of curly brackets.
371  */
372 #define DEFINE_INTERRUPT_HANDLER_RET(func)				\
373 static __always_inline long ____##func(struct pt_regs *regs);		\
374 									\
375 interrupt_handler long func(struct pt_regs *regs)			\
376 {									\
377 	struct interrupt_state state;					\
378 	long ret;							\
379 									\
380 	interrupt_enter_prepare(regs, &state);				\
381 									\
382 	ret = ____##func (regs);					\
383 									\
384 	interrupt_exit_prepare(regs, &state);				\
385 									\
386 	return ret;							\
387 }									\
388 NOKPROBE_SYMBOL(func);							\
389 									\
390 static __always_inline long ____##func(struct pt_regs *regs)
391 
392 /**
393  * DECLARE_INTERRUPT_HANDLER_ASYNC - Declare asynchronous interrupt handler function
394  * @func:	Function name of the entry point
395  */
396 #define DECLARE_INTERRUPT_HANDLER_ASYNC(func)				\
397 	__visible void func(struct pt_regs *regs)
398 
399 /**
400  * DEFINE_INTERRUPT_HANDLER_ASYNC - Define asynchronous interrupt handler function
401  * @func:	Function name of the entry point
402  *
403  * @func is called from ASM entry code.
404  *
405  * The macro is written so it acts as function definition. Append the
406  * body with a pair of curly brackets.
407  */
408 #define DEFINE_INTERRUPT_HANDLER_ASYNC(func)				\
409 static __always_inline void ____##func(struct pt_regs *regs);		\
410 									\
411 interrupt_handler void func(struct pt_regs *regs)			\
412 {									\
413 	struct interrupt_state state;					\
414 									\
415 	interrupt_async_enter_prepare(regs, &state);			\
416 									\
417 	____##func (regs);						\
418 									\
419 	interrupt_async_exit_prepare(regs, &state);			\
420 }									\
421 NOKPROBE_SYMBOL(func);							\
422 									\
423 static __always_inline void ____##func(struct pt_regs *regs)
424 
425 /**
426  * DECLARE_INTERRUPT_HANDLER_NMI - Declare NMI interrupt handler function
427  * @func:	Function name of the entry point
428  * @returns:	Returns a value back to asm caller
429  */
430 #define DECLARE_INTERRUPT_HANDLER_NMI(func)				\
431 	__visible long func(struct pt_regs *regs)
432 
433 /**
434  * DEFINE_INTERRUPT_HANDLER_NMI - Define NMI interrupt handler function
435  * @func:	Function name of the entry point
436  * @returns:	Returns a value back to asm caller
437  *
438  * @func is called from ASM entry code.
439  *
440  * The macro is written so it acts as function definition. Append the
441  * body with a pair of curly brackets.
442  */
443 #define DEFINE_INTERRUPT_HANDLER_NMI(func)				\
444 static __always_inline long ____##func(struct pt_regs *regs);		\
445 									\
446 interrupt_handler long func(struct pt_regs *regs)			\
447 {									\
448 	struct interrupt_nmi_state state;				\
449 	long ret;							\
450 									\
451 	interrupt_nmi_enter_prepare(regs, &state);			\
452 									\
453 	ret = ____##func (regs);					\
454 									\
455 	interrupt_nmi_exit_prepare(regs, &state);			\
456 									\
457 	return ret;							\
458 }									\
459 NOKPROBE_SYMBOL(func);							\
460 									\
461 static __always_inline long ____##func(struct pt_regs *regs)
462 
463 
464 /* Interrupt handlers */
465 /* kernel/traps.c */
466 DECLARE_INTERRUPT_HANDLER_NMI(system_reset_exception);
467 #ifdef CONFIG_PPC_BOOK3S_64
468 DECLARE_INTERRUPT_HANDLER_ASYNC(machine_check_exception);
469 #else
470 DECLARE_INTERRUPT_HANDLER_NMI(machine_check_exception);
471 #endif
472 DECLARE_INTERRUPT_HANDLER(SMIException);
473 DECLARE_INTERRUPT_HANDLER(handle_hmi_exception);
474 DECLARE_INTERRUPT_HANDLER(unknown_exception);
475 DECLARE_INTERRUPT_HANDLER_ASYNC(unknown_async_exception);
476 DECLARE_INTERRUPT_HANDLER_NMI(unknown_nmi_exception);
477 DECLARE_INTERRUPT_HANDLER(instruction_breakpoint_exception);
478 DECLARE_INTERRUPT_HANDLER(RunModeException);
479 DECLARE_INTERRUPT_HANDLER(single_step_exception);
480 DECLARE_INTERRUPT_HANDLER(program_check_exception);
481 DECLARE_INTERRUPT_HANDLER(emulation_assist_interrupt);
482 DECLARE_INTERRUPT_HANDLER(alignment_exception);
483 DECLARE_INTERRUPT_HANDLER(StackOverflow);
484 DECLARE_INTERRUPT_HANDLER(stack_overflow_exception);
485 DECLARE_INTERRUPT_HANDLER(kernel_fp_unavailable_exception);
486 DECLARE_INTERRUPT_HANDLER(altivec_unavailable_exception);
487 DECLARE_INTERRUPT_HANDLER(vsx_unavailable_exception);
488 DECLARE_INTERRUPT_HANDLER(facility_unavailable_exception);
489 DECLARE_INTERRUPT_HANDLER(fp_unavailable_tm);
490 DECLARE_INTERRUPT_HANDLER(altivec_unavailable_tm);
491 DECLARE_INTERRUPT_HANDLER(vsx_unavailable_tm);
492 DECLARE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi);
493 DECLARE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async);
494 DECLARE_INTERRUPT_HANDLER_RAW(performance_monitor_exception);
495 DECLARE_INTERRUPT_HANDLER(DebugException);
496 DECLARE_INTERRUPT_HANDLER(altivec_assist_exception);
497 DECLARE_INTERRUPT_HANDLER(CacheLockingException);
498 DECLARE_INTERRUPT_HANDLER(SPEFloatingPointException);
499 DECLARE_INTERRUPT_HANDLER(SPEFloatingPointRoundException);
500 DECLARE_INTERRUPT_HANDLER_NMI(WatchdogException);
501 DECLARE_INTERRUPT_HANDLER(kernel_bad_stack);
502 
503 /* slb.c */
504 DECLARE_INTERRUPT_HANDLER_RAW(do_slb_fault);
505 DECLARE_INTERRUPT_HANDLER(do_bad_slb_fault);
506 
507 /* hash_utils.c */
508 DECLARE_INTERRUPT_HANDLER_RAW(do_hash_fault);
509 
510 /* fault.c */
511 DECLARE_INTERRUPT_HANDLER(do_page_fault);
512 DECLARE_INTERRUPT_HANDLER(do_bad_page_fault_segv);
513 
514 /* process.c */
515 DECLARE_INTERRUPT_HANDLER(do_break);
516 
517 /* time.c */
518 DECLARE_INTERRUPT_HANDLER_ASYNC(timer_interrupt);
519 
520 /* mce.c */
521 DECLARE_INTERRUPT_HANDLER_NMI(machine_check_early);
522 DECLARE_INTERRUPT_HANDLER_NMI(hmi_exception_realmode);
523 
524 DECLARE_INTERRUPT_HANDLER_ASYNC(TAUException);
525 
526 void __noreturn unrecoverable_exception(struct pt_regs *regs);
527 
528 void replay_system_reset(void);
529 void replay_soft_interrupts(void);
530 
531 static inline void interrupt_cond_local_irq_enable(struct pt_regs *regs)
532 {
533 	if (!arch_irq_disabled_regs(regs))
534 		local_irq_enable();
535 }
536 
537 #endif /* __ASSEMBLY__ */
538 
539 #endif /* _ASM_POWERPC_INTERRUPT_H */
540