irq_32.c 5.0 KB

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