i386: Execute stack overflow warning on interrupt stack v2
[cascardo/linux.git] / arch / x86 / kernel / irq_32.c
1 /*
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
11 #include <linux/module.h>
12 #include <linux/seq_file.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/notifier.h>
16 #include <linux/cpu.h>
17 #include <linux/delay.h>
18
19 #include <asm/apic.h>
20 #include <asm/uaccess.h>
21
22 DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
23 EXPORT_PER_CPU_SYMBOL(irq_stat);
24
25 DEFINE_PER_CPU(struct pt_regs *, irq_regs);
26 EXPORT_PER_CPU_SYMBOL(irq_regs);
27
28 /*
29  * 'what should we do if we get a hw irq event on an illegal vector'.
30  * each architecture has to answer this themselves.
31  */
32 void ack_bad_irq(unsigned int irq)
33 {
34         printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
35
36 #ifdef CONFIG_X86_LOCAL_APIC
37         /*
38          * Currently unexpected vectors happen only on SMP and APIC.
39          * We _must_ ack these because every local APIC has only N
40          * irq slots per priority level, and a 'hanging, unacked' IRQ
41          * holds up an irq slot - in excessive cases (when multiple
42          * unexpected vectors occur) that might lock up the APIC
43          * completely.
44          * But only ack when the APIC is enabled -AK
45          */
46         if (cpu_has_apic)
47                 ack_APIC_irq();
48 #endif
49 }
50
51 #ifdef CONFIG_4KSTACKS
52 /*
53  * per-CPU IRQ handling contexts (thread information and stack)
54  */
55 union irq_ctx {
56         struct thread_info      tinfo;
57         u32                     stack[THREAD_SIZE/sizeof(u32)];
58 };
59
60 static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
61 static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
62 #endif
63
64 static void stack_overflow(void)
65 {
66         printk("low stack detected by irq handler\n");
67         dump_stack();
68 }
69
70 static inline void call_on_stack2(void *func, void *stack,
71                            unsigned long arg1, unsigned long arg2)
72 {
73         unsigned long bx;
74         asm volatile(
75                         "       xchgl  %%ebx,%%esp    \n"
76                         "       call   *%%edi         \n"
77                         "       movl   %%ebx,%%esp    \n"
78                         : "=a" (arg1), "=d" (arg2), "=b" (bx)
79                         :  "0" (arg1),   "1" (arg2),  "2" (stack),
80                            "D" (func)
81                         : "memory", "cc", "ecx");
82 }
83
84 /*
85  * do_IRQ handles all normal device IRQ's (the special
86  * SMP cross-CPU interrupts have their own specific
87  * handlers).
88  */
89 unsigned int do_IRQ(struct pt_regs *regs)
90 {       
91         struct pt_regs *old_regs;
92         /* high bit used in ret_from_ code */
93         int irq = ~regs->orig_ax;
94         struct irq_desc *desc = irq_desc + irq;
95 #ifdef CONFIG_4KSTACKS
96         union irq_ctx *curctx, *irqctx;
97         u32 *isp;
98 #endif
99         int overflow = 0;
100
101         if (unlikely((unsigned)irq >= NR_IRQS)) {
102                 printk(KERN_EMERG "%s: cannot handle IRQ %d\n",
103                                         __func__, irq);
104                 BUG();
105         }
106
107         old_regs = set_irq_regs(regs);
108         irq_enter();
109 #ifdef CONFIG_DEBUG_STACKOVERFLOW
110         /* Debugging check for stack overflow: is there less than 1KB free? */
111         {
112                 long sp;
113
114                 __asm__ __volatile__("andl %%esp,%0" :
115                                         "=r" (sp) : "0" (THREAD_SIZE - 1));
116                 if (unlikely(sp < (sizeof(struct thread_info) + STACK_WARN)))
117                         overflow = 1;
118         }
119 #endif
120
121 #ifdef CONFIG_4KSTACKS
122
123         curctx = (union irq_ctx *) current_thread_info();
124         irqctx = hardirq_ctx[smp_processor_id()];
125
126         /*
127          * this is where we switch to the IRQ stack. However, if we are
128          * already using the IRQ stack (because we interrupted a hardirq
129          * handler) we can't do that and just have to keep using the
130          * current stack (which is the irq stack already after all)
131          */
132         if (curctx != irqctx) {
133                 /* build the stack frame on the IRQ stack */
134                 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
135                 irqctx->tinfo.task = curctx->tinfo.task;
136                 irqctx->tinfo.previous_esp = current_stack_pointer;
137
138                 /*
139                  * Copy the softirq bits in preempt_count so that the
140                  * softirq checks work in the hardirq context.
141                  */
142                 irqctx->tinfo.preempt_count =
143                         (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
144                         (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
145
146                 /* Execute warning on interrupt stack */
147                 if (unlikely(overflow))
148                         call_on_stack2(stack_overflow, isp, 0, 0);
149
150                 call_on_stack2(desc->handle_irq, isp, irq, (unsigned long)desc);
151         } else
152 #endif
153         {
154                 /* AK: Slightly bogus here */
155                 if (overflow)
156                         stack_overflow();
157                 desc->handle_irq(irq, desc);
158         }
159
160         irq_exit();
161         set_irq_regs(old_regs);
162         return 1;
163 }
164
165 #ifdef CONFIG_4KSTACKS
166
167 static char softirq_stack[NR_CPUS * THREAD_SIZE]
168                 __attribute__((__section__(".bss.page_aligned")));
169
170 static char hardirq_stack[NR_CPUS * THREAD_SIZE]
171                 __attribute__((__section__(".bss.page_aligned")));
172
173 /*
174  * allocate per-cpu stacks for hardirq and for softirq processing
175  */
176 void irq_ctx_init(int cpu)
177 {
178         union irq_ctx *irqctx;
179
180         if (hardirq_ctx[cpu])
181                 return;
182
183         irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
184         irqctx->tinfo.task              = NULL;
185         irqctx->tinfo.exec_domain       = NULL;
186         irqctx->tinfo.cpu               = cpu;
187         irqctx->tinfo.preempt_count     = HARDIRQ_OFFSET;
188         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
189
190         hardirq_ctx[cpu] = irqctx;
191
192         irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE];
193         irqctx->tinfo.task              = NULL;
194         irqctx->tinfo.exec_domain       = NULL;
195         irqctx->tinfo.cpu               = cpu;
196         irqctx->tinfo.preempt_count     = 0;
197         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
198
199         softirq_ctx[cpu] = irqctx;
200
201         printk("CPU %u irqstacks, hard=%p soft=%p\n",
202                 cpu,hardirq_ctx[cpu],softirq_ctx[cpu]);
203 }
204
205 void irq_ctx_exit(int cpu)
206 {
207         hardirq_ctx[cpu] = NULL;
208 }
209
210 asmlinkage void do_softirq(void)
211 {
212         unsigned long flags;
213         struct thread_info *curctx;
214         union irq_ctx *irqctx;
215         u32 *isp;
216
217         if (in_interrupt())
218                 return;
219
220         local_irq_save(flags);
221
222         if (local_softirq_pending()) {
223                 curctx = current_thread_info();
224                 irqctx = softirq_ctx[smp_processor_id()];
225                 irqctx->tinfo.task = curctx->task;
226                 irqctx->tinfo.previous_esp = current_stack_pointer;
227
228                 /* build the stack frame on the softirq stack */
229                 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
230
231                 asm volatile(
232                         "       xchgl   %%ebx,%%esp     \n"
233                         "       call    __do_softirq    \n"
234                         "       movl    %%ebx,%%esp     \n"
235                         : "=b"(isp)
236                         : "0"(isp)
237                         : "memory", "cc", "edx", "ecx", "eax"
238                 );
239                 /*
240                  * Shouldnt happen, we returned above if in_interrupt():
241                  */
242                 WARN_ON_ONCE(softirq_count());
243         }
244
245         local_irq_restore(flags);
246 }
247 #endif
248
249 /*
250  * Interrupt statistics:
251  */
252
253 atomic_t irq_err_count;
254
255 /*
256  * /proc/interrupts printing:
257  */
258
259 int show_interrupts(struct seq_file *p, void *v)
260 {
261         int i = *(loff_t *) v, j;
262         struct irqaction * action;
263         unsigned long flags;
264
265         if (i == 0) {
266                 seq_printf(p, "           ");
267                 for_each_online_cpu(j)
268                         seq_printf(p, "CPU%-8d",j);
269                 seq_putc(p, '\n');
270         }
271
272         if (i < NR_IRQS) {
273                 unsigned any_count = 0;
274
275                 spin_lock_irqsave(&irq_desc[i].lock, flags);
276 #ifndef CONFIG_SMP
277                 any_count = kstat_irqs(i);
278 #else
279                 for_each_online_cpu(j)
280                         any_count |= kstat_cpu(j).irqs[i];
281 #endif
282                 action = irq_desc[i].action;
283                 if (!action && !any_count)
284                         goto skip;
285                 seq_printf(p, "%3d: ",i);
286 #ifndef CONFIG_SMP
287                 seq_printf(p, "%10u ", kstat_irqs(i));
288 #else
289                 for_each_online_cpu(j)
290                         seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
291 #endif
292                 seq_printf(p, " %8s", irq_desc[i].chip->name);
293                 seq_printf(p, "-%-8s", irq_desc[i].name);
294
295                 if (action) {
296                         seq_printf(p, "  %s", action->name);
297                         while ((action = action->next) != NULL)
298                                 seq_printf(p, ", %s", action->name);
299                 }
300
301                 seq_putc(p, '\n');
302 skip:
303                 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
304         } else if (i == NR_IRQS) {
305                 seq_printf(p, "NMI: ");
306                 for_each_online_cpu(j)
307                         seq_printf(p, "%10u ", nmi_count(j));
308                 seq_printf(p, "  Non-maskable interrupts\n");
309 #ifdef CONFIG_X86_LOCAL_APIC
310                 seq_printf(p, "LOC: ");
311                 for_each_online_cpu(j)
312                         seq_printf(p, "%10u ",
313                                 per_cpu(irq_stat,j).apic_timer_irqs);
314                 seq_printf(p, "  Local timer interrupts\n");
315 #endif
316 #ifdef CONFIG_SMP
317                 seq_printf(p, "RES: ");
318                 for_each_online_cpu(j)
319                         seq_printf(p, "%10u ",
320                                 per_cpu(irq_stat,j).irq_resched_count);
321                 seq_printf(p, "  Rescheduling interrupts\n");
322                 seq_printf(p, "CAL: ");
323                 for_each_online_cpu(j)
324                         seq_printf(p, "%10u ",
325                                 per_cpu(irq_stat,j).irq_call_count);
326                 seq_printf(p, "  function call interrupts\n");
327                 seq_printf(p, "TLB: ");
328                 for_each_online_cpu(j)
329                         seq_printf(p, "%10u ",
330                                 per_cpu(irq_stat,j).irq_tlb_count);
331                 seq_printf(p, "  TLB shootdowns\n");
332 #endif
333                 seq_printf(p, "TRM: ");
334                 for_each_online_cpu(j)
335                         seq_printf(p, "%10u ",
336                                 per_cpu(irq_stat,j).irq_thermal_count);
337                 seq_printf(p, "  Thermal event interrupts\n");
338                 seq_printf(p, "SPU: ");
339                 for_each_online_cpu(j)
340                         seq_printf(p, "%10u ",
341                                 per_cpu(irq_stat,j).irq_spurious_count);
342                 seq_printf(p, "  Spurious interrupts\n");
343                 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
344 #if defined(CONFIG_X86_IO_APIC)
345                 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
346 #endif
347         }
348         return 0;
349 }
350
351 #ifdef CONFIG_HOTPLUG_CPU
352 #include <mach_apic.h>
353
354 void fixup_irqs(cpumask_t map)
355 {
356         unsigned int irq;
357         static int warned;
358
359         for (irq = 0; irq < NR_IRQS; irq++) {
360                 cpumask_t mask;
361                 if (irq == 2)
362                         continue;
363
364                 cpus_and(mask, irq_desc[irq].affinity, map);
365                 if (any_online_cpu(mask) == NR_CPUS) {
366                         printk("Breaking affinity for irq %i\n", irq);
367                         mask = map;
368                 }
369                 if (irq_desc[irq].chip->set_affinity)
370                         irq_desc[irq].chip->set_affinity(irq, mask);
371                 else if (irq_desc[irq].action && !(warned++))
372                         printk("Cannot set affinity for irq %i\n", irq);
373         }
374
375 #if 0
376         barrier();
377         /* Ingo Molnar says: "after the IO-APIC masks have been redirected
378            [note the nop - the interrupt-enable boundary on x86 is two
379            instructions from sti] - to flush out pending hardirqs and
380            IPIs. After this point nothing is supposed to reach this CPU." */
381         __asm__ __volatile__("sti; nop; cli");
382         barrier();
383 #else
384         /* That doesn't seem sufficient.  Give it 1ms. */
385         local_irq_enable();
386         mdelay(1);
387         local_irq_disable();
388 #endif
389 }
390 #endif
391