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