Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/seq_file.h> |
| 13 | #include <linux/interrupt.h> |
Nicolai Stange | 447ae31 | 2018-07-29 12:15:33 +0200 | [diff] [blame] | 14 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kernel_stat.h> |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 16 | #include <linux/notifier.h> |
| 17 | #include <linux/cpu.h> |
| 18 | #include <linux/delay.h> |
Jaswinder Singh Rajput | 72ade5f | 2009-01-04 16:32:36 +0530 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 20 | #include <linux/percpu.h> |
Eric Dumazet | 5c1eb08 | 2010-10-28 16:40:54 +0200 | [diff] [blame] | 21 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Thomas Gleixner | e05d723 | 2007-02-16 01:27:58 -0800 | [diff] [blame] | 23 | #include <asm/apic.h> |
Andi Kleen | 7614e91 | 2018-01-11 21:46:33 +0000 | [diff] [blame] | 24 | #include <asm/nospec-branch.h> |
Thomas Gleixner | db1cc7a | 2021-02-10 00:40:53 +0100 | [diff] [blame] | 25 | #include <asm/softirq_stack.h> |
Thomas Gleixner | e05d723 | 2007-02-16 01:27:58 -0800 | [diff] [blame] | 26 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_DEBUG_STACKOVERFLOW |
Ingo Molnar | 53b5650 | 2011-12-05 12:25:44 +0100 | [diff] [blame] | 28 | |
| 29 | int sysctl_panic_on_stackoverflow __read_mostly; |
| 30 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 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(); |
Mitsuo Hayasaka | 55af779 | 2011-11-29 15:08:36 +0900 | [diff] [blame] | 46 | if (sysctl_panic_on_stackoverflow) |
| 47 | panic("low stack detected by irq handler - check messages\n"); |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 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 | |
Thomas Gleixner | a754fe2 | 2019-04-14 18:00:01 +0200 | [diff] [blame] | 55 | DEFINE_PER_CPU(struct irq_stack *, hardirq_stack_ptr); |
| 56 | DEFINE_PER_CPU(struct irq_stack *, softirq_stack_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 58 | static void call_on_stack(void *func, void *stack) |
| 59 | { |
| 60 | asm volatile("xchgl %%ebx,%%esp \n" |
Andi Kleen | 7614e91 | 2018-01-11 21:46:33 +0000 | [diff] [blame] | 61 | CALL_NOSPEC |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 62 | "movl %%ebx,%%esp \n" |
| 63 | : "=b" (stack) |
| 64 | : "0" (stack), |
Andi Kleen | 7614e91 | 2018-01-11 21:46:33 +0000 | [diff] [blame] | 65 | [thunk_target] "D"(func) |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 66 | : "memory", "cc", "edx", "ecx", "eax"); |
Andi Kleen | 04b361a | 2008-05-05 12:36:38 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 69 | static inline void *current_stack(void) |
| 70 | { |
Andrey Ryabinin | 196bd48 | 2017-09-29 17:15:36 +0300 | [diff] [blame] | 71 | return (void *)(current_stack_pointer & ~(THREAD_SIZE - 1)); |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 74 | static inline int execute_on_irq_stack(int overflow, struct irq_desc *desc) |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 75 | { |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 76 | struct irq_stack *curstk, *irqstk; |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 77 | u32 *isp, *prev_esp, arg1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 79 | curstk = (struct irq_stack *) current_stack(); |
Thomas Gleixner | a754fe2 | 2019-04-14 18:00:01 +0200 | [diff] [blame] | 80 | irqstk = __this_cpu_read(hardirq_stack_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * this is where we switch to the IRQ stack. However, if we are |
| 84 | * already using the IRQ stack (because we interrupted a hardirq |
| 85 | * handler) we can't do that and just have to keep using the |
| 86 | * current stack (which is the irq stack already after all) |
| 87 | */ |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 88 | if (unlikely(curstk == irqstk)) |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 89 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 91 | isp = (u32 *) ((char *)irqstk + sizeof(*irqstk)); |
| 92 | |
| 93 | /* Save the next esp at the bottom of the stack */ |
| 94 | prev_esp = (u32 *)irqstk; |
Andrey Ryabinin | 196bd48 | 2017-09-29 17:15:36 +0300 | [diff] [blame] | 95 | *prev_esp = current_stack_pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 97 | if (unlikely(overflow)) |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 98 | call_on_stack(print_stack_overflow, isp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 100 | asm volatile("xchgl %%ebx,%%esp \n" |
Andi Kleen | 7614e91 | 2018-01-11 21:46:33 +0000 | [diff] [blame] | 101 | CALL_NOSPEC |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 102 | "movl %%ebx,%%esp \n" |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 103 | : "=a" (arg1), "=b" (isp) |
| 104 | : "0" (desc), "1" (isp), |
Andi Kleen | 7614e91 | 2018-01-11 21:46:33 +0000 | [diff] [blame] | 105 | [thunk_target] "D" (desc->handle_irq) |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 106 | : "memory", "cc", "ecx"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return 1; |
| 108 | } |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* |
Thomas Gleixner | 66c7ceb | 2019-04-14 18:00:04 +0200 | [diff] [blame] | 111 | * Allocate per-cpu stacks for hardirq and softirq processing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | */ |
Thomas Gleixner | 66c7ceb | 2019-04-14 18:00:04 +0200 | [diff] [blame] | 113 | int irq_init_percpu_irqstack(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Thomas Gleixner | 66c7ceb | 2019-04-14 18:00:04 +0200 | [diff] [blame] | 115 | int node = cpu_to_node(cpu); |
| 116 | struct page *ph, *ps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Thomas Gleixner | a754fe2 | 2019-04-14 18:00:01 +0200 | [diff] [blame] | 118 | if (per_cpu(hardirq_stack_ptr, cpu)) |
Thomas Gleixner | 66c7ceb | 2019-04-14 18:00:04 +0200 | [diff] [blame] | 119 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Thomas Gleixner | 66c7ceb | 2019-04-14 18:00:04 +0200 | [diff] [blame] | 121 | ph = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER); |
| 122 | if (!ph) |
| 123 | return -ENOMEM; |
| 124 | ps = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER); |
| 125 | if (!ps) { |
| 126 | __free_pages(ph, THREAD_SIZE_ORDER); |
| 127 | return -ENOMEM; |
| 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Thomas Gleixner | 66c7ceb | 2019-04-14 18:00:04 +0200 | [diff] [blame] | 130 | per_cpu(hardirq_stack_ptr, cpu) = page_address(ph); |
| 131 | per_cpu(softirq_stack_ptr, cpu) = page_address(ps); |
| 132 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Thomas Gleixner | 441e903 | 2021-09-24 18:12:45 +0200 | [diff] [blame] | 135 | #ifndef CONFIG_PREEMPT_RT |
Frederic Weisbecker | 7d65f4a | 2013-09-05 15:49:45 +0200 | [diff] [blame] | 136 | void do_softirq_own_stack(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 138 | struct irq_stack *irqstk; |
Steven Rostedt | 0788aa6 | 2014-02-06 09:41:30 -0500 | [diff] [blame] | 139 | u32 *isp, *prev_esp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Thomas Gleixner | a754fe2 | 2019-04-14 18:00:01 +0200 | [diff] [blame] | 141 | irqstk = __this_cpu_read(softirq_stack_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Frederic Weisbecker | 7d65f4a | 2013-09-05 15:49:45 +0200 | [diff] [blame] | 143 | /* build the stack frame on the softirq stack */ |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 144 | isp = (u32 *) ((char *)irqstk + sizeof(*irqstk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Steven Rostedt | 0788aa6 | 2014-02-06 09:41:30 -0500 | [diff] [blame] | 146 | /* Push the previous esp onto the stack */ |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 147 | prev_esp = (u32 *)irqstk; |
Andrey Ryabinin | 196bd48 | 2017-09-29 17:15:36 +0300 | [diff] [blame] | 148 | *prev_esp = current_stack_pointer; |
Steven Rostedt | 0788aa6 | 2014-02-06 09:41:30 -0500 | [diff] [blame] | 149 | |
Frederic Weisbecker | 7d65f4a | 2013-09-05 15:49:45 +0200 | [diff] [blame] | 150 | call_on_stack(__do_softirq, isp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
Thomas Gleixner | 441e903 | 2021-09-24 18:12:45 +0200 | [diff] [blame] | 152 | #endif |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 153 | |
Thomas Gleixner | 7c2a573 | 2020-05-21 22:05:35 +0200 | [diff] [blame] | 154 | void __handle_irq(struct irq_desc *desc, struct pt_regs *regs) |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 155 | { |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 156 | int overflow = check_stack_overflow(); |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 157 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 158 | if (user_mode(regs) || !execute_on_irq_stack(overflow, desc)) { |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 159 | if (unlikely(overflow)) |
| 160 | print_stack_overflow(); |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 161 | generic_handle_irq_desc(desc); |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 162 | } |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 163 | } |