irq_32.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <asm/softirq_stack.h>
  24. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  25. int sysctl_panic_on_stackoverflow __read_mostly;
  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. if (sysctl_panic_on_stackoverflow)
  39. panic("low stack detected by irq handler - check messages\n");
  40. }
  41. #else
  42. static inline int check_stack_overflow(void) { return 0; }
  43. static inline void print_stack_overflow(void) { }
  44. #endif
  45. static void call_on_stack(void *func, void *stack)
  46. {
  47. asm volatile("xchgl %%ebx,%%esp \n"
  48. CALL_NOSPEC
  49. "movl %%ebx,%%esp \n"
  50. : "=b" (stack)
  51. : "0" (stack),
  52. [thunk_target] "D"(func)
  53. : "memory", "cc", "edx", "ecx", "eax");
  54. }
  55. static inline void *current_stack(void)
  56. {
  57. return (void *)(current_stack_pointer & ~(THREAD_SIZE - 1));
  58. }
  59. static inline int execute_on_irq_stack(int overflow, struct irq_desc *desc)
  60. {
  61. struct irq_stack *curstk, *irqstk;
  62. u32 *isp, *prev_esp, arg1;
  63. curstk = (struct irq_stack *) current_stack();
  64. irqstk = __this_cpu_read(pcpu_hot.hardirq_stack_ptr);
  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_NOSPEC
  81. "movl %%ebx,%%esp \n"
  82. : "=a" (arg1), "=b" (isp)
  83. : "0" (desc), "1" (isp),
  84. [thunk_target] "D" (desc->handle_irq)
  85. : "memory", "cc", "ecx");
  86. return 1;
  87. }
  88. /*
  89. * Allocate per-cpu stacks for hardirq and softirq processing
  90. */
  91. int irq_init_percpu_irqstack(unsigned int cpu)
  92. {
  93. int node = cpu_to_node(cpu);
  94. struct page *ph, *ps;
  95. if (per_cpu(pcpu_hot.hardirq_stack_ptr, cpu))
  96. return 0;
  97. ph = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER);
  98. if (!ph)
  99. return -ENOMEM;
  100. ps = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER);
  101. if (!ps) {
  102. __free_pages(ph, THREAD_SIZE_ORDER);
  103. return -ENOMEM;
  104. }
  105. per_cpu(pcpu_hot.hardirq_stack_ptr, cpu) = page_address(ph);
  106. per_cpu(pcpu_hot.softirq_stack_ptr, cpu) = page_address(ps);
  107. return 0;
  108. }
  109. #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK
  110. void do_softirq_own_stack(void)
  111. {
  112. struct irq_stack *irqstk;
  113. u32 *isp, *prev_esp;
  114. irqstk = __this_cpu_read(pcpu_hot.softirq_stack_ptr);
  115. /* build the stack frame on the softirq stack */
  116. isp = (u32 *) ((char *)irqstk + sizeof(*irqstk));
  117. /* Push the previous esp onto the stack */
  118. prev_esp = (u32 *)irqstk;
  119. *prev_esp = current_stack_pointer;
  120. call_on_stack(__do_softirq, isp);
  121. }
  122. #endif
  123. void __handle_irq(struct irq_desc *desc, struct pt_regs *regs)
  124. {
  125. int overflow = check_stack_overflow();
  126. if (user_mode(regs) || !execute_on_irq_stack(overflow, desc)) {
  127. if (unlikely(overflow))
  128. print_stack_overflow();
  129. generic_handle_irq_desc(desc);
  130. }
  131. }