irq_32.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include <linux/module.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/notifier.h>
  15. #include <linux/cpu.h>
  16. #include <linux/delay.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/percpu.h>
  19. #include <asm/apic.h>
  20. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  21. EXPORT_PER_CPU_SYMBOL(irq_stat);
  22. DEFINE_PER_CPU(struct pt_regs *, irq_regs);
  23. EXPORT_PER_CPU_SYMBOL(irq_regs);
  24. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  25. /* Debugging check for stack overflow: is there less than 1KB free? */
  26. static int check_stack_overflow(void)
  27. {
  28. long sp;
  29. __asm__ __volatile__("andl %%esp,%0" :
  30. "=r" (sp) : "0" (THREAD_SIZE - 1));
  31. return sp < (sizeof(struct thread_info) + STACK_WARN);
  32. }
  33. static void print_stack_overflow(void)
  34. {
  35. printk(KERN_WARNING "low stack detected by irq handler\n");
  36. dump_stack();
  37. }
  38. #else
  39. static inline int check_stack_overflow(void) { return 0; }
  40. static inline void print_stack_overflow(void) { }
  41. #endif
  42. #ifdef CONFIG_4KSTACKS
  43. /*
  44. * per-CPU IRQ handling contexts (thread information and stack)
  45. */
  46. union irq_ctx {
  47. struct thread_info tinfo;
  48. u32 stack[THREAD_SIZE/sizeof(u32)];
  49. } __attribute__((aligned(PAGE_SIZE)));
  50. static DEFINE_PER_CPU(union irq_ctx *, hardirq_ctx);
  51. static DEFINE_PER_CPU(union irq_ctx *, softirq_ctx);
  52. static DEFINE_PER_CPU_PAGE_ALIGNED(union irq_ctx, hardirq_stack);
  53. static DEFINE_PER_CPU_PAGE_ALIGNED(union irq_ctx, softirq_stack);
  54. static void call_on_stack(void *func, void *stack)
  55. {
  56. asm volatile("xchgl %%ebx,%%esp \n"
  57. "call *%%edi \n"
  58. "movl %%ebx,%%esp \n"
  59. : "=b" (stack)
  60. : "0" (stack),
  61. "D"(func)
  62. : "memory", "cc", "edx", "ecx", "eax");
  63. }
  64. static inline int
  65. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
  66. {
  67. union irq_ctx *curctx, *irqctx;
  68. u32 *isp, arg1, arg2;
  69. curctx = (union irq_ctx *) current_thread_info();
  70. irqctx = __get_cpu_var(hardirq_ctx);
  71. /*
  72. * this is where we switch to the IRQ stack. However, if we are
  73. * already using the IRQ stack (because we interrupted a hardirq
  74. * handler) we can't do that and just have to keep using the
  75. * current stack (which is the irq stack already after all)
  76. */
  77. if (unlikely(curctx == irqctx))
  78. return 0;
  79. /* build the stack frame on the IRQ stack */
  80. isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
  81. irqctx->tinfo.task = curctx->tinfo.task;
  82. irqctx->tinfo.previous_esp = current_stack_pointer;
  83. /*
  84. * Copy the softirq bits in preempt_count so that the
  85. * softirq checks work in the hardirq context.
  86. */
  87. irqctx->tinfo.preempt_count =
  88. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  89. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  90. if (unlikely(overflow))
  91. call_on_stack(print_stack_overflow, isp);
  92. asm volatile("xchgl %%ebx,%%esp \n"
  93. "call *%%edi \n"
  94. "movl %%ebx,%%esp \n"
  95. : "=a" (arg1), "=d" (arg2), "=b" (isp)
  96. : "0" (irq), "1" (desc), "2" (isp),
  97. "D" (desc->handle_irq)
  98. : "memory", "cc", "ecx");
  99. return 1;
  100. }
  101. /*
  102. * allocate per-cpu stacks for hardirq and for softirq processing
  103. */
  104. void __cpuinit irq_ctx_init(int cpu)
  105. {
  106. union irq_ctx *irqctx;
  107. if (per_cpu(hardirq_ctx, cpu))
  108. return;
  109. irqctx = &per_cpu(hardirq_stack, cpu);
  110. irqctx->tinfo.task = NULL;
  111. irqctx->tinfo.exec_domain = NULL;
  112. irqctx->tinfo.cpu = cpu;
  113. irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
  114. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  115. per_cpu(hardirq_ctx, cpu) = irqctx;
  116. irqctx = &per_cpu(softirq_stack, cpu);
  117. irqctx->tinfo.task = NULL;
  118. irqctx->tinfo.exec_domain = NULL;
  119. irqctx->tinfo.cpu = cpu;
  120. irqctx->tinfo.preempt_count = 0;
  121. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  122. per_cpu(softirq_ctx, cpu) = irqctx;
  123. printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
  124. cpu, per_cpu(hardirq_ctx, cpu), per_cpu(softirq_ctx, cpu));
  125. }
  126. void irq_ctx_exit(int cpu)
  127. {
  128. per_cpu(hardirq_ctx, cpu) = NULL;
  129. }
  130. asmlinkage void do_softirq(void)
  131. {
  132. unsigned long flags;
  133. struct thread_info *curctx;
  134. union irq_ctx *irqctx;
  135. u32 *isp;
  136. if (in_interrupt())
  137. return;
  138. local_irq_save(flags);
  139. if (local_softirq_pending()) {
  140. curctx = current_thread_info();
  141. irqctx = __get_cpu_var(softirq_ctx);
  142. irqctx->tinfo.task = curctx->task;
  143. irqctx->tinfo.previous_esp = current_stack_pointer;
  144. /* build the stack frame on the softirq stack */
  145. isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
  146. call_on_stack(__do_softirq, isp);
  147. /*
  148. * Shouldnt happen, we returned above if in_interrupt():
  149. */
  150. WARN_ON_ONCE(softirq_count());
  151. }
  152. local_irq_restore(flags);
  153. }
  154. #else
  155. static inline int
  156. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) { return 0; }
  157. #endif
  158. /*
  159. * do_IRQ handles all normal device IRQ's (the special
  160. * SMP cross-CPU interrupts have their own specific
  161. * handlers).
  162. */
  163. unsigned int do_IRQ(struct pt_regs *regs)
  164. {
  165. struct pt_regs *old_regs;
  166. /* high bit used in ret_from_ code */
  167. int overflow;
  168. unsigned vector = ~regs->orig_ax;
  169. struct irq_desc *desc;
  170. unsigned irq;
  171. old_regs = set_irq_regs(regs);
  172. irq_enter();
  173. irq = __get_cpu_var(vector_irq)[vector];
  174. overflow = check_stack_overflow();
  175. desc = irq_to_desc(irq);
  176. if (unlikely(!desc)) {
  177. printk(KERN_EMERG "%s: cannot handle IRQ %d vector %#x cpu %d\n",
  178. __func__, irq, vector, smp_processor_id());
  179. BUG();
  180. }
  181. if (!execute_on_irq_stack(overflow, desc, irq)) {
  182. if (unlikely(overflow))
  183. print_stack_overflow();
  184. desc->handle_irq(irq, desc);
  185. }
  186. irq_exit();
  187. set_irq_regs(old_regs);
  188. return 1;
  189. }
  190. #ifdef CONFIG_HOTPLUG_CPU
  191. #include <mach_apic.h>
  192. /* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
  193. void fixup_irqs(void)
  194. {
  195. unsigned int irq;
  196. static int warned;
  197. struct irq_desc *desc;
  198. for_each_irq_desc(irq, desc) {
  199. const struct cpumask *affinity;
  200. if (!desc)
  201. continue;
  202. if (irq == 2)
  203. continue;
  204. affinity = desc->affinity;
  205. if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) {
  206. printk("Breaking affinity for irq %i\n", irq);
  207. affinity = cpu_all_mask;
  208. }
  209. if (desc->chip->set_affinity)
  210. desc->chip->set_affinity(irq, affinity);
  211. else if (desc->action && !(warned++))
  212. printk("Cannot set affinity for irq %i\n", irq);
  213. }
  214. #if 0
  215. barrier();
  216. /* Ingo Molnar says: "after the IO-APIC masks have been redirected
  217. [note the nop - the interrupt-enable boundary on x86 is two
  218. instructions from sti] - to flush out pending hardirqs and
  219. IPIs. After this point nothing is supposed to reach this CPU." */
  220. __asm__ __volatile__("sti; nop; cli");
  221. barrier();
  222. #else
  223. /* That doesn't seem sufficient. Give it 1ms. */
  224. local_irq_enable();
  225. mdelay(1);
  226. local_irq_disable();
  227. #endif
  228. }
  229. #endif