irq_32.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  22. int sysctl_panic_on_stackoverflow __read_mostly;
  23. /* Debugging check for stack overflow: is there less than 1KB free? */
  24. static int check_stack_overflow(void)
  25. {
  26. long sp;
  27. __asm__ __volatile__("andl %%esp,%0" :
  28. "=r" (sp) : "0" (THREAD_SIZE - 1));
  29. return sp < (sizeof(struct thread_info) + STACK_WARN);
  30. }
  31. static void print_stack_overflow(void)
  32. {
  33. printk(KERN_WARNING "low stack detected by irq handler\n");
  34. dump_stack();
  35. if (sysctl_panic_on_stackoverflow)
  36. panic("low stack detected by irq handler - check messages\n");
  37. }
  38. #else
  39. static inline int check_stack_overflow(void) { return 0; }
  40. static inline void print_stack_overflow(void) { }
  41. #endif
  42. DEFINE_PER_CPU(struct irq_stack *, hardirq_stack);
  43. DEFINE_PER_CPU(struct irq_stack *, softirq_stack);
  44. static void call_on_stack(void *func, void *stack)
  45. {
  46. asm volatile("xchgl %%ebx,%%esp \n"
  47. "call *%%edi \n"
  48. "movl %%ebx,%%esp \n"
  49. : "=b" (stack)
  50. : "0" (stack),
  51. "D"(func)
  52. : "memory", "cc", "edx", "ecx", "eax");
  53. }
  54. static inline void *current_stack(void)
  55. {
  56. return (void *)(current_stack_pointer() & ~(THREAD_SIZE - 1));
  57. }
  58. static inline int
  59. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
  60. {
  61. struct irq_stack *curstk, *irqstk;
  62. u32 *isp, *prev_esp, arg1, arg2;
  63. curstk = (struct irq_stack *) current_stack();
  64. irqstk = __this_cpu_read(hardirq_stack);
  65. /*
  66. * this is where we switch to the IRQ stack. However, if we are
  67. * already using the IRQ stack (because we interrupted a hardirq
  68. * handler) we can't do that and just have to keep using the
  69. * current stack (which is the irq stack already after all)
  70. */
  71. if (unlikely(curstk == irqstk))
  72. return 0;
  73. isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
  74. /* Save the next esp at the bottom of the stack */
  75. prev_esp = (u32 *)irqstk;
  76. *prev_esp = current_stack_pointer();
  77. if (unlikely(overflow))
  78. call_on_stack(print_stack_overflow, isp);
  79. asm volatile("xchgl %%ebx,%%esp \n"
  80. "call *%%edi \n"
  81. "movl %%ebx,%%esp \n"
  82. : "=a" (arg1), "=d" (arg2), "=b" (isp)
  83. : "0" (irq), "1" (desc), "2" (isp),
  84. "D" (desc->handle_irq)
  85. : "memory", "cc", "ecx");
  86. return 1;
  87. }
  88. /*
  89. * allocate per-cpu stacks for hardirq and for softirq processing
  90. */
  91. void irq_ctx_init(int cpu)
  92. {
  93. struct irq_stack *irqstk;
  94. if (per_cpu(hardirq_stack, cpu))
  95. return;
  96. irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
  97. THREADINFO_GFP,
  98. THREAD_SIZE_ORDER));
  99. per_cpu(hardirq_stack, cpu) = irqstk;
  100. irqstk = page_address(alloc_pages_node(cpu_to_node(cpu),
  101. THREADINFO_GFP,
  102. THREAD_SIZE_ORDER));
  103. per_cpu(softirq_stack, cpu) = irqstk;
  104. printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
  105. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  106. }
  107. void do_softirq_own_stack(void)
  108. {
  109. struct thread_info *curstk;
  110. struct irq_stack *irqstk;
  111. u32 *isp, *prev_esp;
  112. curstk = current_stack();
  113. irqstk = __this_cpu_read(softirq_stack);
  114. /* build the stack frame on the softirq stack */
  115. isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
  116. /* Push the previous esp onto the stack */
  117. prev_esp = (u32 *)irqstk;
  118. *prev_esp = current_stack_pointer();
  119. call_on_stack(__do_softirq, isp);
  120. }
  121. bool handle_irq(unsigned irq, struct pt_regs *regs)
  122. {
  123. struct irq_desc *desc;
  124. int overflow;
  125. overflow = check_stack_overflow();
  126. desc = irq_to_desc(irq);
  127. if (unlikely(!desc))
  128. return false;
  129. if (user_mode(regs) || !execute_on_irq_stack(overflow, desc, irq)) {
  130. if (unlikely(overflow))
  131. print_stack_overflow();
  132. desc->handle_irq(irq, desc);
  133. }
  134. return true;
  135. }