irq_32.c 4.1 KB

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