irq_32.c 4.0 KB

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