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 bool check_stack_overflow(void) 33 { 34 unsigned long sp = current_stack_pointer & (THREAD_SIZE - 1); 35 36 return sp < (sizeof(struct thread_info) + STACK_WARN); 37 } 38 39 static void print_stack_overflow(void) 40 { 41 printk(KERN_WARNING "low stack detected by irq handler\n"); 42 dump_stack(); 43 if (sysctl_panic_on_stackoverflow) 44 panic("low stack detected by irq handler - check messages\n"); 45 } 46 47 #else 48 static inline bool check_stack_overflow(void) { return false; } 49 static inline void print_stack_overflow(void) { } 50 #endif 51 52 DEFINE_PER_CPU_CACHE_HOT(struct irq_stack *, softirq_stack_ptr); 53 54 static void call_on_stack(void *func, void *stack) 55 { 56 asm volatile("xchgl %[sp], %%esp\n" 57 CALL_NOSPEC 58 "movl %[sp], %%esp" 59 : [sp] "+b" (stack) 60 : [thunk_target] "D" (func) 61 : "memory", "cc", "edx", "ecx", "eax"); 62 } 63 64 static inline void *current_stack(void) 65 { 66 return (void *)(current_stack_pointer & ~(THREAD_SIZE - 1)); 67 } 68 69 static inline bool execute_on_irq_stack(bool overflow, struct irq_desc *desc) 70 { 71 struct irq_stack *curstk, *irqstk; 72 u32 *isp, *prev_esp; 73 74 curstk = (struct irq_stack *) current_stack(); 75 irqstk = __this_cpu_read(hardirq_stack_ptr); 76 77 /* 78 * this is where we switch to the IRQ stack. However, if we are 79 * already using the IRQ stack (because we interrupted a hardirq 80 * handler) we can't do that and just have to keep using the 81 * current stack (which is the irq stack already after all) 82 */ 83 if (unlikely(curstk == irqstk)) 84 return false; 85 86 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk)); 87 88 /* Save the next esp at the bottom of the stack */ 89 prev_esp = (u32 *)irqstk; 90 *prev_esp = current_stack_pointer; 91 92 if (unlikely(overflow)) 93 call_on_stack(print_stack_overflow, isp); 94 95 asm volatile("xchgl %[sp], %%esp\n" 96 CALL_NOSPEC 97 "movl %[sp], %%esp" 98 : "+a" (desc), [sp] "+b" (isp) 99 : [thunk_target] "D" (desc->handle_irq) 100 : "memory", "cc", "edx", "ecx"); 101 return true; 102 } 103 104 /* 105 * Allocate per-cpu stacks for hardirq and softirq processing 106 */ 107 int irq_init_percpu_irqstack(unsigned int cpu) 108 { 109 int node = cpu_to_node(cpu); 110 struct page *ph, *ps; 111 112 if (per_cpu(hardirq_stack_ptr, cpu)) 113 return 0; 114 115 ph = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER); 116 if (!ph) 117 return -ENOMEM; 118 ps = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER); 119 if (!ps) { 120 __free_pages(ph, THREAD_SIZE_ORDER); 121 return -ENOMEM; 122 } 123 124 per_cpu(hardirq_stack_ptr, cpu) = page_address(ph); 125 per_cpu(softirq_stack_ptr, cpu) = page_address(ps); 126 return 0; 127 } 128 129 #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK 130 void do_softirq_own_stack(void) 131 { 132 struct irq_stack *irqstk; 133 u32 *isp, *prev_esp; 134 135 irqstk = __this_cpu_read(softirq_stack_ptr); 136 137 /* build the stack frame on the softirq stack */ 138 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk)); 139 140 /* Push the previous esp onto the stack */ 141 prev_esp = (u32 *)irqstk; 142 *prev_esp = current_stack_pointer; 143 144 call_on_stack(__do_softirq, isp); 145 } 146 #endif 147 148 void __handle_irq(struct irq_desc *desc, struct pt_regs *regs) 149 { 150 bool overflow = check_stack_overflow(); 151 152 if (user_mode(regs) || !execute_on_irq_stack(overflow, desc)) { 153 if (unlikely(overflow)) 154 print_stack_overflow(); 155 generic_handle_irq_desc(desc); 156 } 157 } 158