irq_32.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <linux/mm.h>
  20. #include <asm/apic.h>
  21. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  22. EXPORT_PER_CPU_SYMBOL(irq_stat);
  23. DEFINE_PER_CPU(struct pt_regs *, irq_regs);
  24. EXPORT_PER_CPU_SYMBOL(irq_regs);
  25. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  26. int sysctl_panic_on_stackoverflow __read_mostly;
  27. /* Debugging check for stack overflow: is there less than 1KB free? */
  28. static int check_stack_overflow(void)
  29. {
  30. long sp;
  31. __asm__ __volatile__("andl %%esp,%0" :
  32. "=r" (sp) : "0" (THREAD_SIZE - 1));
  33. return sp < (sizeof(struct thread_info) + STACK_WARN);
  34. }
  35. static void print_stack_overflow(void)
  36. {
  37. printk(KERN_WARNING "low stack detected by irq handler\n");
  38. dump_stack();
  39. if (sysctl_panic_on_stackoverflow)
  40. panic("low stack detected by irq handler - check messages\n");
  41. }
  42. #else
  43. static inline int check_stack_overflow(void) { return 0; }
  44. static inline void print_stack_overflow(void) { }
  45. #endif
  46. /*
  47. * per-CPU IRQ handling contexts (thread information and stack)
  48. */
  49. union irq_ctx {
  50. struct thread_info tinfo;
  51. u32 stack[THREAD_SIZE/sizeof(u32)];
  52. } __attribute__((aligned(THREAD_SIZE)));
  53. static DEFINE_PER_CPU(union irq_ctx *, hardirq_ctx);
  54. static DEFINE_PER_CPU(union irq_ctx *, softirq_ctx);
  55. static 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");
  64. }
  65. static inline int
  66. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
  67. {
  68. union irq_ctx *curctx, *irqctx;
  69. u32 *isp, arg1, arg2;
  70. curctx = (union irq_ctx *) current_thread_info();
  71. irqctx = __this_cpu_read(hardirq_ctx);
  72. /*
  73. * this is where we switch to the IRQ stack. However, if we are
  74. * already using the IRQ stack (because we interrupted a hardirq
  75. * handler) we can't do that and just have to keep using the
  76. * current stack (which is the irq stack already after all)
  77. */
  78. if (unlikely(curctx == irqctx))
  79. return 0;
  80. /* build the stack frame on the IRQ stack */
  81. isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
  82. irqctx->tinfo.task = curctx->tinfo.task;
  83. irqctx->tinfo.previous_esp = current_stack_pointer;
  84. /*
  85. * Copy the softirq bits in preempt_count so that the
  86. * softirq checks work in the hardirq context.
  87. */
  88. irqctx->tinfo.preempt_count =
  89. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  90. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  91. if (unlikely(overflow))
  92. call_on_stack(print_stack_overflow, isp);
  93. asm volatile("xchgl %%ebx,%%esp \n"
  94. "call *%%edi \n"
  95. "movl %%ebx,%%esp \n"
  96. : "=a" (arg1), "=d" (arg2), "=b" (isp)
  97. : "0" (irq), "1" (desc), "2" (isp),
  98. "D" (desc->handle_irq)
  99. : "memory", "cc", "ecx");
  100. return 1;
  101. }
  102. /*
  103. * allocate per-cpu stacks for hardirq and for softirq processing
  104. */
  105. void __cpuinit irq_ctx_init(int cpu)
  106. {
  107. union irq_ctx *irqctx;
  108. if (per_cpu(hardirq_ctx, cpu))
  109. return;
  110. irqctx = page_address(alloc_pages_node(cpu_to_node(cpu),
  111. THREAD_FLAGS,
  112. THREAD_ORDER));
  113. memset(&irqctx->tinfo, 0, sizeof(struct thread_info));
  114. irqctx->tinfo.cpu = cpu;
  115. irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
  116. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  117. per_cpu(hardirq_ctx, cpu) = irqctx;
  118. irqctx = page_address(alloc_pages_node(cpu_to_node(cpu),
  119. THREAD_FLAGS,
  120. THREAD_ORDER));
  121. memset(&irqctx->tinfo, 0, sizeof(struct thread_info));
  122. irqctx->tinfo.cpu = cpu;
  123. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  124. per_cpu(softirq_ctx, cpu) = irqctx;
  125. printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
  126. cpu, per_cpu(hardirq_ctx, cpu), per_cpu(softirq_ctx, cpu));
  127. }
  128. asmlinkage void do_softirq(void)
  129. {
  130. unsigned long flags;
  131. struct thread_info *curctx;
  132. union irq_ctx *irqctx;
  133. u32 *isp;
  134. if (in_interrupt())
  135. return;
  136. local_irq_save(flags);
  137. if (local_softirq_pending()) {
  138. curctx = current_thread_info();
  139. irqctx = __this_cpu_read(softirq_ctx);
  140. irqctx->tinfo.task = curctx->task;
  141. irqctx->tinfo.previous_esp = current_stack_pointer;
  142. /* build the stack frame on the softirq stack */
  143. isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
  144. call_on_stack(__do_softirq, isp);
  145. /*
  146. * Shouldn't happen, we returned above if in_interrupt():
  147. */
  148. WARN_ON_ONCE(softirq_count());
  149. }
  150. local_irq_restore(flags);
  151. }
  152. bool handle_irq(unsigned irq, struct pt_regs *regs)
  153. {
  154. struct irq_desc *desc;
  155. int overflow;
  156. overflow = check_stack_overflow();
  157. desc = irq_to_desc(irq);
  158. if (unlikely(!desc))
  159. return false;
  160. if (!execute_on_irq_stack(overflow, desc, irq)) {
  161. if (unlikely(overflow))
  162. print_stack_overflow();
  163. desc->handle_irq(irq, desc);
  164. }
  165. return true;
  166. }