Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar |
| 3 | * |
| 4 | * This file contains the lowest level x86-specific interrupt |
| 5 | * entry, irq-stacks and irq statistics code. All the remaining |
| 6 | * irq logic is done by the generic kernel/irq/ code and |
| 7 | * by the x86-specific irq controller code. (e.g. i8259.c and |
| 8 | * io_apic.c.) |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/kernel_stat.h> |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 15 | #include <linux/notifier.h> |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/delay.h> |
Jaswinder Singh Rajput | 72ade5f | 2009-01-04 16:32:36 +0530 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 19 | #include <linux/percpu.h> |
Eric Dumazet | 5c1eb08 | 2010-10-28 16:40:54 +0200 | [diff] [blame] | 20 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Thomas Gleixner | e05d723 | 2007-02-16 01:27:58 -0800 | [diff] [blame] | 22 | #include <asm/apic.h> |
Thomas Gleixner | e05d723 | 2007-02-16 01:27:58 -0800 | [diff] [blame] | 23 | |
Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 24 | DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | EXPORT_PER_CPU_SYMBOL(irq_stat); |
| 26 | |
Jeremy Fitzhardinge | 7c3576d | 2007-05-02 19:27:16 +0200 | [diff] [blame] | 27 | DEFINE_PER_CPU(struct pt_regs *, irq_regs); |
| 28 | EXPORT_PER_CPU_SYMBOL(irq_regs); |
| 29 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 30 | #ifdef CONFIG_DEBUG_STACKOVERFLOW |
Ingo Molnar | 53b5650 | 2011-12-05 12:25:44 +0100 | [diff] [blame] | 31 | |
| 32 | int sysctl_panic_on_stackoverflow __read_mostly; |
| 33 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 34 | /* Debugging check for stack overflow: is there less than 1KB free? */ |
| 35 | static int check_stack_overflow(void) |
| 36 | { |
| 37 | long sp; |
| 38 | |
| 39 | __asm__ __volatile__("andl %%esp,%0" : |
| 40 | "=r" (sp) : "0" (THREAD_SIZE - 1)); |
| 41 | |
| 42 | return sp < (sizeof(struct thread_info) + STACK_WARN); |
| 43 | } |
| 44 | |
| 45 | static void print_stack_overflow(void) |
| 46 | { |
| 47 | printk(KERN_WARNING "low stack detected by irq handler\n"); |
| 48 | dump_stack(); |
Mitsuo Hayasaka | 55af779 | 2011-11-29 15:08:36 +0900 | [diff] [blame] | 49 | if (sysctl_panic_on_stackoverflow) |
| 50 | panic("low stack detected by irq handler - check messages\n"); |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | #else |
| 54 | static inline int check_stack_overflow(void) { return 0; } |
| 55 | static inline void print_stack_overflow(void) { } |
| 56 | #endif |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
| 59 | * per-CPU IRQ handling contexts (thread information and stack) |
| 60 | */ |
| 61 | union irq_ctx { |
| 62 | struct thread_info tinfo; |
| 63 | u32 stack[THREAD_SIZE/sizeof(u32)]; |
Christoph Hellwig | 2589737 | 2010-07-27 14:13:13 +0200 | [diff] [blame] | 64 | } __attribute__((aligned(THREAD_SIZE))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 66 | static DEFINE_PER_CPU(union irq_ctx *, hardirq_ctx); |
| 67 | static DEFINE_PER_CPU(union irq_ctx *, softirq_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 69 | static void call_on_stack(void *func, void *stack) |
| 70 | { |
| 71 | asm volatile("xchgl %%ebx,%%esp \n" |
| 72 | "call *%%edi \n" |
| 73 | "movl %%ebx,%%esp \n" |
| 74 | : "=b" (stack) |
| 75 | : "0" (stack), |
| 76 | "D"(func) |
| 77 | : "memory", "cc", "edx", "ecx", "eax"); |
Andi Kleen | 04b361a | 2008-05-05 12:36:38 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 80 | static inline int |
| 81 | execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) |
| 82 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | union irq_ctx *curctx, *irqctx; |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 84 | u32 *isp, arg1, arg2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | curctx = (union irq_ctx *) current_thread_info(); |
Tejun Heo | 0a3aee0 | 2010-12-18 16:28:55 +0100 | [diff] [blame] | 87 | irqctx = __this_cpu_read(hardirq_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * this is where we switch to the IRQ stack. However, if we are |
| 91 | * already using the IRQ stack (because we interrupted a hardirq |
| 92 | * handler) we can't do that and just have to keep using the |
| 93 | * current stack (which is the irq stack already after all) |
| 94 | */ |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 95 | if (unlikely(curctx == irqctx)) |
| 96 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 98 | /* build the stack frame on the IRQ stack */ |
Jaswinder Singh Rajput | 72ade5f | 2009-01-04 16:32:36 +0530 | [diff] [blame] | 99 | isp = (u32 *) ((char *)irqctx + sizeof(*irqctx)); |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 100 | irqctx->tinfo.task = curctx->tinfo.task; |
| 101 | irqctx->tinfo.previous_esp = current_stack_pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 103 | if (unlikely(overflow)) |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 104 | call_on_stack(print_stack_overflow, isp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 106 | asm volatile("xchgl %%ebx,%%esp \n" |
| 107 | "call *%%edi \n" |
| 108 | "movl %%ebx,%%esp \n" |
| 109 | : "=a" (arg1), "=d" (arg2), "=b" (isp) |
| 110 | : "0" (irq), "1" (desc), "2" (isp), |
| 111 | "D" (desc->handle_irq) |
| 112 | : "memory", "cc", "ecx"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return 1; |
| 114 | } |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* |
| 117 | * allocate per-cpu stacks for hardirq and for softirq processing |
| 118 | */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 119 | void irq_ctx_init(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | union irq_ctx *irqctx; |
| 122 | |
Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 123 | if (per_cpu(hardirq_ctx, cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return; |
| 125 | |
Eric Dumazet | 5c1eb08 | 2010-10-28 16:40:54 +0200 | [diff] [blame] | 126 | irqctx = page_address(alloc_pages_node(cpu_to_node(cpu), |
Thomas Gleixner | 38e7c57 | 2012-05-05 15:05:42 +0000 | [diff] [blame] | 127 | THREADINFO_GFP, |
| 128 | THREAD_SIZE_ORDER)); |
Brian Gerst | 7b698ea | 2011-01-17 07:32:10 -0500 | [diff] [blame] | 129 | memset(&irqctx->tinfo, 0, sizeof(struct thread_info)); |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 130 | irqctx->tinfo.cpu = cpu; |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 131 | irqctx->tinfo.addr_limit = MAKE_MM_SEG(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 133 | per_cpu(hardirq_ctx, cpu) = irqctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Eric Dumazet | 5c1eb08 | 2010-10-28 16:40:54 +0200 | [diff] [blame] | 135 | irqctx = page_address(alloc_pages_node(cpu_to_node(cpu), |
Thomas Gleixner | 38e7c57 | 2012-05-05 15:05:42 +0000 | [diff] [blame] | 136 | THREADINFO_GFP, |
| 137 | THREAD_SIZE_ORDER)); |
Brian Gerst | 7b698ea | 2011-01-17 07:32:10 -0500 | [diff] [blame] | 138 | memset(&irqctx->tinfo, 0, sizeof(struct thread_info)); |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 139 | irqctx->tinfo.cpu = cpu; |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 140 | irqctx->tinfo.addr_limit = MAKE_MM_SEG(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 142 | per_cpu(softirq_ctx, cpu) = irqctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 144 | printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n", |
Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 145 | cpu, per_cpu(hardirq_ctx, cpu), per_cpu(softirq_ctx, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Frederic Weisbecker | 7d65f4a | 2013-09-05 15:49:45 +0200 | [diff] [blame] | 148 | void do_softirq_own_stack(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | struct thread_info *curctx; |
| 151 | union irq_ctx *irqctx; |
| 152 | u32 *isp; |
| 153 | |
Frederic Weisbecker | 7d65f4a | 2013-09-05 15:49:45 +0200 | [diff] [blame] | 154 | curctx = current_thread_info(); |
| 155 | irqctx = __this_cpu_read(softirq_ctx); |
| 156 | irqctx->tinfo.task = curctx->task; |
| 157 | irqctx->tinfo.previous_esp = current_stack_pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Frederic Weisbecker | 7d65f4a | 2013-09-05 15:49:45 +0200 | [diff] [blame] | 159 | /* build the stack frame on the softirq stack */ |
| 160 | isp = (u32 *) ((char *)irqctx + sizeof(*irqctx)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Frederic Weisbecker | 7d65f4a | 2013-09-05 15:49:45 +0200 | [diff] [blame] | 162 | call_on_stack(__do_softirq, isp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 164 | |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 165 | bool handle_irq(unsigned irq, struct pt_regs *regs) |
| 166 | { |
| 167 | struct irq_desc *desc; |
| 168 | int overflow; |
| 169 | |
| 170 | overflow = check_stack_overflow(); |
| 171 | |
| 172 | desc = irq_to_desc(irq); |
| 173 | if (unlikely(!desc)) |
| 174 | return false; |
| 175 | |
Linus Torvalds | 986cb48 | 2012-02-19 11:46:36 -0800 | [diff] [blame] | 176 | if (user_mode_vm(regs) || !execute_on_irq_stack(overflow, desc, irq)) { |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 177 | if (unlikely(overflow)) |
| 178 | print_stack_overflow(); |
| 179 | desc->handle_irq(irq, desc); |
| 180 | } |
| 181 | |
| 182 | return true; |
| 183 | } |