irq_32.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. bool handle_irq(unsigned irq, struct pt_regs *regs)
  159. {
  160. struct irq_desc *desc;
  161. int overflow;
  162. overflow = check_stack_overflow();
  163. desc = irq_to_desc(irq);
  164. if (unlikely(!desc))
  165. return false;
  166. if (!execute_on_irq_stack(overflow, desc, irq)) {
  167. if (unlikely(overflow))
  168. print_stack_overflow();
  169. desc->handle_irq(irq, desc);
  170. }
  171. return true;
  172. }
  173. #ifdef CONFIG_HOTPLUG_CPU
  174. /* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
  175. void fixup_irqs(void)
  176. {
  177. unsigned int irq;
  178. static int warned;
  179. struct irq_desc *desc;
  180. for_each_irq_desc(irq, desc) {
  181. const struct cpumask *affinity;
  182. if (!desc)
  183. continue;
  184. if (irq == 2)
  185. continue;
  186. affinity = desc->affinity;
  187. if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) {
  188. printk("Breaking affinity for irq %i\n", irq);
  189. affinity = cpu_all_mask;
  190. }
  191. if (desc->chip->set_affinity)
  192. desc->chip->set_affinity(irq, affinity);
  193. else if (desc->action && !(warned++))
  194. printk("Cannot set affinity for irq %i\n", irq);
  195. }
  196. #if 0
  197. barrier();
  198. /* Ingo Molnar says: "after the IO-APIC masks have been redirected
  199. [note the nop - the interrupt-enable boundary on x86 is two
  200. instructions from sti] - to flush out pending hardirqs and
  201. IPIs. After this point nothing is supposed to reach this CPU." */
  202. __asm__ __volatile__("sti; nop; cli");
  203. barrier();
  204. #else
  205. /* That doesn't seem sufficient. Give it 1ms. */
  206. local_irq_enable();
  207. mdelay(1);
  208. local_irq_disable();
  209. #endif
  210. }
  211. #endif