blob: 38da8f29a9c81f075d4b91fc6982e28ae2f28eee [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 Gleixnerbd0b9ac2015-09-14 10:42:37 +020071static inline int execute_on_irq_stack(int overflow, struct irq_desc *desc)
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020072{
Steven Rostedt198d2082014-02-06 09:41:31 -050073 struct irq_stack *curstk, *irqstk;
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +020074 u32 *isp, *prev_esp, arg1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Steven Rostedt198d2082014-02-06 09:41:31 -050076 curstk = (struct irq_stack *) current_stack();
77 irqstk = __this_cpu_read(hardirq_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /*
80 * this is where we switch to the IRQ stack. However, if we are
81 * already using the IRQ stack (because we interrupted a hardirq
82 * handler) we can't do that and just have to keep using the
83 * current stack (which is the irq stack already after all)
84 */
Steven Rostedt198d2082014-02-06 09:41:31 -050085 if (unlikely(curstk == irqstk))
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Steven Rostedt198d2082014-02-06 09:41:31 -050088 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
89
90 /* Save the next esp at the bottom of the stack */
91 prev_esp = (u32 *)irqstk;
Andy Lutomirski83653c12014-11-13 15:57:07 -080092 *prev_esp = current_stack_pointer();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020094 if (unlikely(overflow))
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020095 call_on_stack(print_stack_overflow, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020097 asm volatile("xchgl %%ebx,%%esp \n"
98 "call *%%edi \n"
99 "movl %%ebx,%%esp \n"
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200100 : "=a" (arg1), "=b" (isp)
101 : "0" (desc), "1" (isp),
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200102 "D" (desc->handle_irq)
103 : "memory", "cc", "ecx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 1;
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * allocate per-cpu stacks for hardirq and for softirq processing
109 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400110void irq_ctx_init(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Steven Rostedt198d2082014-02-06 09:41:31 -0500112 struct irq_stack *irqstk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Steven Rostedt198d2082014-02-06 09:41:31 -0500114 if (per_cpu(hardirq_stack, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return;
116
Steven Rostedt198d2082014-02-06 09:41:31 -0500117 irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
Thomas Gleixner38e7c572012-05-05 15:05:42 +0000118 THREADINFO_GFP,
119 THREAD_SIZE_ORDER));
Steven Rostedt198d2082014-02-06 09:41:31 -0500120 per_cpu(hardirq_stack, cpu) = irqstk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Steven Rostedt198d2082014-02-06 09:41:31 -0500122 irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
Thomas Gleixner38e7c572012-05-05 15:05:42 +0000123 THREADINFO_GFP,
124 THREAD_SIZE_ORDER));
Steven Rostedt198d2082014-02-06 09:41:31 -0500125 per_cpu(softirq_stack, cpu) = irqstk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200127 printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
Steven Rostedt198d2082014-02-06 09:41:31 -0500128 cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200131void do_softirq_own_stack(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Steven Rostedt198d2082014-02-06 09:41:31 -0500133 struct thread_info *curstk;
134 struct irq_stack *irqstk;
Steven Rostedt0788aa62014-02-06 09:41:30 -0500135 u32 *isp, *prev_esp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Steven Rostedt198d2082014-02-06 09:41:31 -0500137 curstk = current_stack();
138 irqstk = __this_cpu_read(softirq_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200140 /* build the stack frame on the softirq stack */
Steven Rostedt198d2082014-02-06 09:41:31 -0500141 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Steven Rostedt0788aa62014-02-06 09:41:30 -0500143 /* Push the previous esp onto the stack */
Steven Rostedt198d2082014-02-06 09:41:31 -0500144 prev_esp = (u32 *)irqstk;
Andy Lutomirski83653c12014-11-13 15:57:07 -0800145 *prev_esp = current_stack_pointer();
Steven Rostedt0788aa62014-02-06 09:41:30 -0500146
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200147 call_on_stack(__do_softirq, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200149
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000150bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800151{
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200152 int overflow = check_stack_overflow();
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800153
Thomas Gleixnera782a7e2015-08-02 20:38:27 +0000154 if (IS_ERR_OR_NULL(desc))
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800155 return false;
156
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200157 if (user_mode(regs) || !execute_on_irq_stack(overflow, desc)) {
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800158 if (unlikely(overflow))
159 print_stack_overflow();
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200160 generic_handle_irq_desc(desc);
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800161 }
162
163 return true;
164}