blob: c80cf66996785bd5309eaf8c1831ee0fcf7ec9b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/seq_file.h>
13#include <linux/interrupt.h>
14#include <linux/kernel_stat.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070015#include <linux/notifier.h>
16#include <linux/cpu.h>
17#include <linux/delay.h>
Jaswinder Singh Rajput72ade5f2009-01-04 16:32:36 +053018#include <linux/uaccess.h>
Lai Jiangshan42f8fae2009-02-17 11:46:42 +080019#include <linux/percpu.h>
Eric Dumazet5c1eb082010-10-28 16:40:54 +020020#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Thomas Gleixnere05d7232007-02-16 01:27:58 -080022#include <asm/apic.h>
Thomas Gleixnere05d7232007-02-16 01:27:58 -080023
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020024#ifdef CONFIG_DEBUG_STACKOVERFLOW
Ingo Molnar53b56502011-12-05 12:25:44 +010025
26int sysctl_panic_on_stackoverflow __read_mostly;
27
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020028/* Debugging check for stack overflow: is there less than 1KB free? */
29static int check_stack_overflow(void)
30{
31 long sp;
32
33 __asm__ __volatile__("andl %%esp,%0" :
34 "=r" (sp) : "0" (THREAD_SIZE - 1));
35
36 return sp < (sizeof(struct thread_info) + STACK_WARN);
37}
38
39static void print_stack_overflow(void)
40{
41 printk(KERN_WARNING "low stack detected by irq handler\n");
42 dump_stack();
Mitsuo Hayasaka55af7792011-11-29 15:08:36 +090043 if (sysctl_panic_on_stackoverflow)
44 panic("low stack detected by irq handler - check messages\n");
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020045}
46
47#else
48static inline int check_stack_overflow(void) { return 0; }
49static inline void print_stack_overflow(void) { }
50#endif
51
Steven Rostedt198d2082014-02-06 09:41:31 -050052DEFINE_PER_CPU(struct irq_stack *, hardirq_stack);
53DEFINE_PER_CPU(struct irq_stack *, softirq_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020055static void call_on_stack(void *func, void *stack)
56{
57 asm volatile("xchgl %%ebx,%%esp \n"
58 "call *%%edi \n"
59 "movl %%ebx,%%esp \n"
60 : "=b" (stack)
61 : "0" (stack),
62 "D"(func)
63 : "memory", "cc", "edx", "ecx", "eax");
Andi Kleen04b361a2008-05-05 12:36:38 +020064}
65
Steven Rostedt198d2082014-02-06 09:41:31 -050066static inline void *current_stack(void)
67{
Andy Lutomirski83653c12014-11-13 15:57:07 -080068 return (void *)(current_stack_pointer() & ~(THREAD_SIZE - 1));
Steven Rostedt198d2082014-02-06 09:41:31 -050069}
70
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020071static inline int
72execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
73{
Steven Rostedt198d2082014-02-06 09:41:31 -050074 struct irq_stack *curstk, *irqstk;
Steven Rostedt0788aa62014-02-06 09:41:30 -050075 u32 *isp, *prev_esp, arg1, arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Steven Rostedt198d2082014-02-06 09:41:31 -050077 curstk = (struct irq_stack *) current_stack();
78 irqstk = __this_cpu_read(hardirq_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 /*
81 * this is where we switch to the IRQ stack. However, if we are
82 * already using the IRQ stack (because we interrupted a hardirq
83 * handler) we can't do that and just have to keep using the
84 * current stack (which is the irq stack already after all)
85 */
Steven Rostedt198d2082014-02-06 09:41:31 -050086 if (unlikely(curstk == irqstk))
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Steven Rostedt198d2082014-02-06 09:41:31 -050089 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
90
91 /* Save the next esp at the bottom of the stack */
92 prev_esp = (u32 *)irqstk;
Andy Lutomirski83653c12014-11-13 15:57:07 -080093 *prev_esp = current_stack_pointer();
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020095 if (unlikely(overflow))
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020096 call_on_stack(print_stack_overflow, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020098 asm volatile("xchgl %%ebx,%%esp \n"
99 "call *%%edi \n"
100 "movl %%ebx,%%esp \n"
101 : "=a" (arg1), "=d" (arg2), "=b" (isp)
102 : "0" (irq), "1" (desc), "2" (isp),
103 "D" (desc->handle_irq)
104 : "memory", "cc", "ecx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 1;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * allocate per-cpu stacks for hardirq and for softirq processing
110 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400111void irq_ctx_init(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Steven Rostedt198d2082014-02-06 09:41:31 -0500113 struct irq_stack *irqstk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Steven Rostedt198d2082014-02-06 09:41:31 -0500115 if (per_cpu(hardirq_stack, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return;
117
Steven Rostedt198d2082014-02-06 09:41:31 -0500118 irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
Thomas Gleixner38e7c572012-05-05 15:05:42 +0000119 THREADINFO_GFP,
120 THREAD_SIZE_ORDER));
Steven Rostedt198d2082014-02-06 09:41:31 -0500121 per_cpu(hardirq_stack, cpu) = irqstk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Steven Rostedt198d2082014-02-06 09:41:31 -0500123 irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
Thomas Gleixner38e7c572012-05-05 15:05:42 +0000124 THREADINFO_GFP,
125 THREAD_SIZE_ORDER));
Steven Rostedt198d2082014-02-06 09:41:31 -0500126 per_cpu(softirq_stack, cpu) = irqstk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200128 printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
Steven Rostedt198d2082014-02-06 09:41:31 -0500129 cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200132void do_softirq_own_stack(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Steven Rostedt198d2082014-02-06 09:41:31 -0500134 struct thread_info *curstk;
135 struct irq_stack *irqstk;
Steven Rostedt0788aa62014-02-06 09:41:30 -0500136 u32 *isp, *prev_esp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Steven Rostedt198d2082014-02-06 09:41:31 -0500138 curstk = current_stack();
139 irqstk = __this_cpu_read(softirq_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200141 /* build the stack frame on the softirq stack */
Steven Rostedt198d2082014-02-06 09:41:31 -0500142 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Steven Rostedt0788aa62014-02-06 09:41:30 -0500144 /* Push the previous esp onto the stack */
Steven Rostedt198d2082014-02-06 09:41:31 -0500145 prev_esp = (u32 *)irqstk;
Andy Lutomirski83653c12014-11-13 15:57:07 -0800146 *prev_esp = current_stack_pointer();
Steven Rostedt0788aa62014-02-06 09:41:30 -0500147
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200148 call_on_stack(__do_softirq, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200150
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000151bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800152{
Thomas Gleixnera47d4572015-08-28 10:30:15 +0200153 unsigned int irq;
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800154 int overflow;
155
156 overflow = check_stack_overflow();
157
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000158 if (IS_ERR_OR_NULL(desc))
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800159 return false;
160
Thomas Gleixnera47d4572015-08-28 10:30:15 +0200161 irq = irq_desc_get_irq(desc);
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700162 if (user_mode(regs) || !execute_on_irq_stack(overflow, desc, irq)) {
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800163 if (unlikely(overflow))
164 print_stack_overflow();
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000165 generic_handle_irq_desc(irq, desc);
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800166 }
167
168 return true;
169}