xref: /linux/arch/x86/kernel/irq_32.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
4  *
5  * This file contains the lowest level x86-specific interrupt
6  * entry, irq-stacks and irq statistics code. All the remaining
7  * irq logic is done by the generic kernel/irq/ code and
8  * by the x86-specific irq controller code. (e.g. i8259.c and
9  * io_apic.c.)
10  */
11 
12 #include <linux/seq_file.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/notifier.h>
17 #include <linux/cpu.h>
18 #include <linux/delay.h>
19 #include <linux/uaccess.h>
20 #include <linux/percpu.h>
21 #include <linux/mm.h>
22 
23 #include <asm/apic.h>
24 #include <asm/nospec-branch.h>
25 #include <asm/softirq_stack.h>
26 
27 #ifdef CONFIG_DEBUG_STACKOVERFLOW
28 
29 int sysctl_panic_on_stackoverflow __read_mostly;
30 
31 /* Debugging check for stack overflow: is there less than 1KB free? */
32 static int check_stack_overflow(void)
33 {
34 	long sp;
35 
36 	__asm__ __volatile__("andl %%esp,%0" :
37 			     "=r" (sp) : "0" (THREAD_SIZE - 1));
38 
39 	return sp < (sizeof(struct thread_info) + STACK_WARN);
40 }
41 
42 static void print_stack_overflow(void)
43 {
44 	printk(KERN_WARNING "low stack detected by irq handler\n");
45 	dump_stack();
46 	if (sysctl_panic_on_stackoverflow)
47 		panic("low stack detected by irq handler - check messages\n");
48 }
49 
50 #else
51 static inline int check_stack_overflow(void) { return 0; }
52 static inline void print_stack_overflow(void) { }
53 #endif
54 
55 static void call_on_stack(void *func, void *stack)
56 {
57 	asm volatile("xchgl	%%ebx,%%esp	\n"
58 		     CALL_NOSPEC
59 		     "movl	%%ebx,%%esp	\n"
60 		     : "=b" (stack)
61 		     : "0" (stack),
62 		       [thunk_target] "D"(func)
63 		     : "memory", "cc", "edx", "ecx", "eax");
64 }
65 
66 static inline void *current_stack(void)
67 {
68 	return (void *)(current_stack_pointer & ~(THREAD_SIZE - 1));
69 }
70 
71 static inline int execute_on_irq_stack(int overflow, struct irq_desc *desc)
72 {
73 	struct irq_stack *curstk, *irqstk;
74 	u32 *isp, *prev_esp, arg1;
75 
76 	curstk = (struct irq_stack *) current_stack();
77 	irqstk = __this_cpu_read(pcpu_hot.hardirq_stack_ptr);
78 
79 	/*
80 	 * this is where we switch to the IRQ stack. However, if we are
81 	 * already using the IRQ stack (because we interrupted a hardirq
82 	 * handler) we can't do that and just have to keep using the
83 	 * current stack (which is the irq stack already after all)
84 	 */
85 	if (unlikely(curstk == irqstk))
86 		return 0;
87 
88 	isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
89 
90 	/* Save the next esp at the bottom of the stack */
91 	prev_esp = (u32 *)irqstk;
92 	*prev_esp = current_stack_pointer;
93 
94 	if (unlikely(overflow))
95 		call_on_stack(print_stack_overflow, isp);
96 
97 	asm volatile("xchgl	%%ebx,%%esp	\n"
98 		     CALL_NOSPEC
99 		     "movl	%%ebx,%%esp	\n"
100 		     : "=a" (arg1), "=b" (isp)
101 		     :  "0" (desc),   "1" (isp),
102 			[thunk_target] "D" (desc->handle_irq)
103 		     : "memory", "cc", "ecx");
104 	return 1;
105 }
106 
107 /*
108  * Allocate per-cpu stacks for hardirq and softirq processing
109  */
110 int irq_init_percpu_irqstack(unsigned int cpu)
111 {
112 	int node = cpu_to_node(cpu);
113 	struct page *ph, *ps;
114 
115 	if (per_cpu(pcpu_hot.hardirq_stack_ptr, cpu))
116 		return 0;
117 
118 	ph = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER);
119 	if (!ph)
120 		return -ENOMEM;
121 	ps = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER);
122 	if (!ps) {
123 		__free_pages(ph, THREAD_SIZE_ORDER);
124 		return -ENOMEM;
125 	}
126 
127 	per_cpu(pcpu_hot.hardirq_stack_ptr, cpu) = page_address(ph);
128 	per_cpu(pcpu_hot.softirq_stack_ptr, cpu) = page_address(ps);
129 	return 0;
130 }
131 
132 #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK
133 void do_softirq_own_stack(void)
134 {
135 	struct irq_stack *irqstk;
136 	u32 *isp, *prev_esp;
137 
138 	irqstk = __this_cpu_read(pcpu_hot.softirq_stack_ptr);
139 
140 	/* build the stack frame on the softirq stack */
141 	isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
142 
143 	/* Push the previous esp onto the stack */
144 	prev_esp = (u32 *)irqstk;
145 	*prev_esp = current_stack_pointer;
146 
147 	call_on_stack(__do_softirq, isp);
148 }
149 #endif
150 
151 void __handle_irq(struct irq_desc *desc, struct pt_regs *regs)
152 {
153 	int overflow = check_stack_overflow();
154 
155 	if (user_mode(regs) || !execute_on_irq_stack(overflow, desc)) {
156 		if (unlikely(overflow))
157 			print_stack_overflow();
158 		generic_handle_irq_desc(desc);
159 	}
160 }
161