irq_32.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. DEFINE_PER_CPU(struct irq_stack *, hardirq_stack);
  47. DEFINE_PER_CPU(struct irq_stack *, softirq_stack);
  48. static void call_on_stack(void *func, void *stack)
  49. {
  50. asm volatile("xchgl %%ebx,%%esp \n"
  51. "call *%%edi \n"
  52. "movl %%ebx,%%esp \n"
  53. : "=b" (stack)
  54. : "0" (stack),
  55. "D"(func)
  56. : "memory", "cc", "edx", "ecx", "eax");
  57. }
  58. /* how to get the current stack pointer from C */
  59. #define current_stack_pointer ({ \
  60. unsigned long sp; \
  61. asm("mov %%esp,%0" : "=g" (sp)); \
  62. sp; \
  63. })
  64. static inline void *current_stack(void)
  65. {
  66. return (void *)(current_stack_pointer & ~(THREAD_SIZE - 1));
  67. }
  68. static inline int
  69. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
  70. {
  71. struct irq_stack *curstk, *irqstk;
  72. u32 *isp, *prev_esp, arg1, arg2;
  73. curstk = (struct irq_stack *) current_stack();
  74. irqstk = __this_cpu_read(hardirq_stack);
  75. /*
  76. * this is where we switch to the IRQ stack. However, if we are
  77. * already using the IRQ stack (because we interrupted a hardirq
  78. * handler) we can't do that and just have to keep using the
  79. * current stack (which is the irq stack already after all)
  80. */
  81. if (unlikely(curstk == irqstk))
  82. return 0;
  83. isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
  84. /* Save the next esp at the bottom of the stack */
  85. prev_esp = (u32 *)irqstk;
  86. *prev_esp = current_stack_pointer;
  87. if (unlikely(overflow))
  88. call_on_stack(print_stack_overflow, isp);
  89. asm volatile("xchgl %%ebx,%%esp \n"
  90. "call *%%edi \n"
  91. "movl %%ebx,%%esp \n"
  92. : "=a" (arg1), "=d" (arg2), "=b" (isp)
  93. : "0" (irq), "1" (desc), "2" (isp),
  94. "D" (desc->handle_irq)
  95. : "memory", "cc", "ecx");
  96. return 1;
  97. }
  98. /*
  99. * allocate per-cpu stacks for hardirq and for softirq processing
  100. */
  101. void irq_ctx_init(int cpu)
  102. {
  103. struct irq_stack *irqstk;
  104. if (per_cpu(hardirq_stack, cpu))
  105. return;
  106. irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
  107. THREADINFO_GFP,
  108. THREAD_SIZE_ORDER));
  109. per_cpu(hardirq_stack, cpu) = irqstk;
  110. irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
  111. THREADINFO_GFP,
  112. THREAD_SIZE_ORDER));
  113. per_cpu(softirq_stack, cpu) = irqstk;
  114. printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
  115. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  116. }
  117. void do_softirq_own_stack(void)
  118. {
  119. struct thread_info *curstk;
  120. struct irq_stack *irqstk;
  121. u32 *isp, *prev_esp;
  122. curstk = current_stack();
  123. irqstk = __this_cpu_read(softirq_stack);
  124. /* build the stack frame on the softirq stack */
  125. isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
  126. /* Push the previous esp onto the stack */
  127. prev_esp = (u32 *)irqstk;
  128. *prev_esp = current_stack_pointer;
  129. call_on_stack(__do_softirq, isp);
  130. }
  131. bool handle_irq(unsigned irq, struct pt_regs *regs)
  132. {
  133. struct irq_desc *desc;
  134. int overflow;
  135. overflow = check_stack_overflow();
  136. desc = irq_to_desc(irq);
  137. if (unlikely(!desc))
  138. return false;
  139. if (user_mode_vm(regs) || !execute_on_irq_stack(overflow, desc, irq)) {
  140. if (unlikely(overflow))
  141. print_stack_overflow();
  142. desc->handle_irq(irq, desc);
  143. }
  144. return true;
  145. }