]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/irq_32.c
x86/cpu/AMD: Fix erratum 1076 (CPB bit)
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / irq_32.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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
1da177e4
LT
11#include <linux/seq_file.h>
12#include <linux/interrupt.h>
13#include <linux/kernel_stat.h>
f3705136
ZM
14#include <linux/notifier.h>
15#include <linux/cpu.h>
16#include <linux/delay.h>
72ade5f9 17#include <linux/uaccess.h>
42f8faec 18#include <linux/percpu.h>
5c1eb089 19#include <linux/mm.h>
1da177e4 20
e05d723f 21#include <asm/apic.h>
aaed9aa7 22#include <asm/nospec-branch.h>
e05d723f 23
de9b10af 24#ifdef CONFIG_DEBUG_STACKOVERFLOW
53b56502
IM
25
26int sysctl_panic_on_stackoverflow __read_mostly;
27
de9b10af
TG
28/* 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();
55af7796
MH
43 if (sysctl_panic_on_stackoverflow)
44 panic("low stack detected by irq handler - check messages\n");
de9b10af
TG
45}
46
47#else
48static inline int check_stack_overflow(void) { return 0; }
49static inline void print_stack_overflow(void) { }
50#endif
51
198d208d
SR
52DEFINE_PER_CPU(struct irq_stack *, hardirq_stack);
53DEFINE_PER_CPU(struct irq_stack *, softirq_stack);
1da177e4 54
403d8efc 55static void call_on_stack(void *func, void *stack)
04b361ab 56{
403d8efc 57 asm volatile("xchgl %%ebx,%%esp \n"
aaed9aa7 58 CALL_NOSPEC
403d8efc
TG
59 "movl %%ebx,%%esp \n"
60 : "=b" (stack)
61 : "0" (stack),
aaed9aa7 62 [thunk_target] "D"(func)
403d8efc 63 : "memory", "cc", "edx", "ecx", "eax");
04b361ab 64}
1da177e4 65
198d208d
SR
66static inline void *current_stack(void)
67{
83653c16 68 return (void *)(current_stack_pointer() & ~(THREAD_SIZE - 1));
198d208d
SR
69}
70
bd0b9ac4 71static inline int execute_on_irq_stack(int overflow, struct irq_desc *desc)
de9b10af 72{
198d208d 73 struct irq_stack *curstk, *irqstk;
bd0b9ac4 74 u32 *isp, *prev_esp, arg1;
1da177e4 75
198d208d
SR
76 curstk = (struct irq_stack *) current_stack();
77 irqstk = __this_cpu_read(hardirq_stack);
1da177e4
LT
78
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 */
198d208d 85 if (unlikely(curstk == irqstk))
de9b10af 86 return 0;
1da177e4 87
198d208d
SR
88 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
89
90 /* Save the next esp at the bottom of the stack */
91 prev_esp = (u32 *)irqstk;
83653c16 92 *prev_esp = current_stack_pointer();
1da177e4 93
de9b10af 94 if (unlikely(overflow))
403d8efc
TG
95 call_on_stack(print_stack_overflow, isp);
96
97 asm volatile("xchgl %%ebx,%%esp \n"
aaed9aa7 98 CALL_NOSPEC
403d8efc 99 "movl %%ebx,%%esp \n"
bd0b9ac4
TG
100 : "=a" (arg1), "=b" (isp)
101 : "0" (desc), "1" (isp),
aaed9aa7 102 [thunk_target] "D" (desc->handle_irq)
403d8efc 103 : "memory", "cc", "ecx");
1da177e4
LT
104 return 1;
105}
106
1da177e4
LT
107/*
108 * allocate per-cpu stacks for hardirq and for softirq processing
109 */
148f9bb8 110void irq_ctx_init(int cpu)
1da177e4 111{
198d208d 112 struct irq_stack *irqstk;
1da177e4 113
198d208d 114 if (per_cpu(hardirq_stack, cpu))
1da177e4
LT
115 return;
116
198d208d 117 irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
38e7c572
TG
118 THREADINFO_GFP,
119 THREAD_SIZE_ORDER));
198d208d 120 per_cpu(hardirq_stack, cpu) = irqstk;
1da177e4 121
198d208d 122 irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
38e7c572
TG
123 THREADINFO_GFP,
124 THREAD_SIZE_ORDER));
198d208d 125 per_cpu(softirq_stack, cpu) = irqstk;
1da177e4 126
403d8efc 127 printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
198d208d 128 cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
1da177e4
LT
129}
130
7d65f4a6 131void do_softirq_own_stack(void)
1da177e4 132{
198d208d 133 struct irq_stack *irqstk;
0788aa6a 134 u32 *isp, *prev_esp;
1da177e4 135
198d208d 136 irqstk = __this_cpu_read(softirq_stack);
1da177e4 137
7d65f4a6 138 /* build the stack frame on the softirq stack */
198d208d 139 isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
1da177e4 140
0788aa6a 141 /* Push the previous esp onto the stack */
198d208d 142 prev_esp = (u32 *)irqstk;
83653c16 143 *prev_esp = current_stack_pointer();
0788aa6a 144
7d65f4a6 145 call_on_stack(__do_softirq, isp);
1da177e4 146}
403d8efc 147
a782a7e4 148bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
9b2b76a3 149{
bd0b9ac4 150 int overflow = check_stack_overflow();
9b2b76a3 151
a782a7e4 152 if (IS_ERR_OR_NULL(desc))
9b2b76a3
JF
153 return false;
154
bd0b9ac4 155 if (user_mode(regs) || !execute_on_irq_stack(overflow, desc)) {
9b2b76a3
JF
156 if (unlikely(overflow))
157 print_stack_overflow();
bd0b9ac4 158 generic_handle_irq_desc(desc);
9b2b76a3
JF
159 }
160
161 return true;
162}