idt.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Interrupt descriptor table related code
  4. */
  5. #include <linux/interrupt.h>
  6. #include <asm/cpu_entry_area.h>
  7. #include <asm/set_memory.h>
  8. #include <asm/traps.h>
  9. #include <asm/proto.h>
  10. #include <asm/desc.h>
  11. #include <asm/hw_irq.h>
  12. #include <asm/idtentry.h>
  13. #define DPL0 0x0
  14. #define DPL3 0x3
  15. #define DEFAULT_STACK 0
  16. #define G(_vector, _addr, _ist, _type, _dpl, _segment) \
  17. { \
  18. .vector = _vector, \
  19. .bits.ist = _ist, \
  20. .bits.type = _type, \
  21. .bits.dpl = _dpl, \
  22. .bits.p = 1, \
  23. .addr = _addr, \
  24. .segment = _segment, \
  25. }
  26. /* Interrupt gate */
  27. #define INTG(_vector, _addr) \
  28. G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL0, __KERNEL_CS)
  29. /* System interrupt gate */
  30. #define SYSG(_vector, _addr) \
  31. G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL3, __KERNEL_CS)
  32. #ifdef CONFIG_X86_64
  33. /*
  34. * Interrupt gate with interrupt stack. The _ist index is the index in
  35. * the tss.ist[] array, but for the descriptor it needs to start at 1.
  36. */
  37. #define ISTG(_vector, _addr, _ist) \
  38. G(_vector, _addr, _ist + 1, GATE_INTERRUPT, DPL0, __KERNEL_CS)
  39. #else
  40. #define ISTG(_vector, _addr, _ist) INTG(_vector, _addr)
  41. #endif
  42. /* Task gate */
  43. #define TSKG(_vector, _gdt) \
  44. G(_vector, NULL, DEFAULT_STACK, GATE_TASK, DPL0, _gdt << 3)
  45. #define IDT_TABLE_SIZE (IDT_ENTRIES * sizeof(gate_desc))
  46. static bool idt_setup_done __initdata;
  47. /*
  48. * Early traps running on the DEFAULT_STACK because the other interrupt
  49. * stacks work only after cpu_init().
  50. */
  51. static const __initconst struct idt_data early_idts[] = {
  52. INTG(X86_TRAP_DB, asm_exc_debug),
  53. SYSG(X86_TRAP_BP, asm_exc_int3),
  54. #ifdef CONFIG_X86_32
  55. /*
  56. * Not possible on 64-bit. See idt_setup_early_pf() for details.
  57. */
  58. INTG(X86_TRAP_PF, asm_exc_page_fault),
  59. #endif
  60. #ifdef CONFIG_INTEL_TDX_GUEST
  61. INTG(X86_TRAP_VE, asm_exc_virtualization_exception),
  62. #endif
  63. };
  64. /*
  65. * The default IDT entries which are set up in trap_init() before
  66. * cpu_init() is invoked. Interrupt stacks cannot be used at that point and
  67. * the traps which use them are reinitialized with IST after cpu_init() has
  68. * set up TSS.
  69. */
  70. static const __initconst struct idt_data def_idts[] = {
  71. INTG(X86_TRAP_DE, asm_exc_divide_error),
  72. ISTG(X86_TRAP_NMI, asm_exc_nmi, IST_INDEX_NMI),
  73. INTG(X86_TRAP_BR, asm_exc_bounds),
  74. INTG(X86_TRAP_UD, asm_exc_invalid_op),
  75. INTG(X86_TRAP_NM, asm_exc_device_not_available),
  76. INTG(X86_TRAP_OLD_MF, asm_exc_coproc_segment_overrun),
  77. INTG(X86_TRAP_TS, asm_exc_invalid_tss),
  78. INTG(X86_TRAP_NP, asm_exc_segment_not_present),
  79. INTG(X86_TRAP_SS, asm_exc_stack_segment),
  80. INTG(X86_TRAP_GP, asm_exc_general_protection),
  81. INTG(X86_TRAP_SPURIOUS, asm_exc_spurious_interrupt_bug),
  82. INTG(X86_TRAP_MF, asm_exc_coprocessor_error),
  83. INTG(X86_TRAP_AC, asm_exc_alignment_check),
  84. INTG(X86_TRAP_XF, asm_exc_simd_coprocessor_error),
  85. #ifdef CONFIG_X86_32
  86. TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS),
  87. #else
  88. ISTG(X86_TRAP_DF, asm_exc_double_fault, IST_INDEX_DF),
  89. #endif
  90. ISTG(X86_TRAP_DB, asm_exc_debug, IST_INDEX_DB),
  91. #ifdef CONFIG_X86_MCE
  92. ISTG(X86_TRAP_MC, asm_exc_machine_check, IST_INDEX_MCE),
  93. #endif
  94. #ifdef CONFIG_X86_KERNEL_IBT
  95. INTG(X86_TRAP_CP, asm_exc_control_protection),
  96. #endif
  97. #ifdef CONFIG_AMD_MEM_ENCRYPT
  98. ISTG(X86_TRAP_VC, asm_exc_vmm_communication, IST_INDEX_VC),
  99. #endif
  100. SYSG(X86_TRAP_OF, asm_exc_overflow),
  101. #if defined(CONFIG_IA32_EMULATION)
  102. SYSG(IA32_SYSCALL_VECTOR, asm_int80_emulation),
  103. #elif defined(CONFIG_X86_32)
  104. SYSG(IA32_SYSCALL_VECTOR, entry_INT80_32),
  105. #endif
  106. };
  107. /*
  108. * The APIC and SMP idt entries
  109. */
  110. static const __initconst struct idt_data apic_idts[] = {
  111. #ifdef CONFIG_SMP
  112. INTG(RESCHEDULE_VECTOR, asm_sysvec_reschedule_ipi),
  113. INTG(CALL_FUNCTION_VECTOR, asm_sysvec_call_function),
  114. INTG(CALL_FUNCTION_SINGLE_VECTOR, asm_sysvec_call_function_single),
  115. INTG(IRQ_MOVE_CLEANUP_VECTOR, asm_sysvec_irq_move_cleanup),
  116. INTG(REBOOT_VECTOR, asm_sysvec_reboot),
  117. #endif
  118. #ifdef CONFIG_X86_THERMAL_VECTOR
  119. INTG(THERMAL_APIC_VECTOR, asm_sysvec_thermal),
  120. #endif
  121. #ifdef CONFIG_X86_MCE_THRESHOLD
  122. INTG(THRESHOLD_APIC_VECTOR, asm_sysvec_threshold),
  123. #endif
  124. #ifdef CONFIG_X86_MCE_AMD
  125. INTG(DEFERRED_ERROR_VECTOR, asm_sysvec_deferred_error),
  126. #endif
  127. #ifdef CONFIG_X86_LOCAL_APIC
  128. INTG(LOCAL_TIMER_VECTOR, asm_sysvec_apic_timer_interrupt),
  129. INTG(X86_PLATFORM_IPI_VECTOR, asm_sysvec_x86_platform_ipi),
  130. # ifdef CONFIG_HAVE_KVM
  131. INTG(POSTED_INTR_VECTOR, asm_sysvec_kvm_posted_intr_ipi),
  132. INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi),
  133. INTG(POSTED_INTR_NESTED_VECTOR, asm_sysvec_kvm_posted_intr_nested_ipi),
  134. # endif
  135. # ifdef CONFIG_IRQ_WORK
  136. INTG(IRQ_WORK_VECTOR, asm_sysvec_irq_work),
  137. # endif
  138. INTG(SPURIOUS_APIC_VECTOR, asm_sysvec_spurious_apic_interrupt),
  139. INTG(ERROR_APIC_VECTOR, asm_sysvec_error_interrupt),
  140. #endif
  141. };
  142. /* Must be page-aligned because the real IDT is used in the cpu entry area */
  143. static gate_desc idt_table[IDT_ENTRIES] __page_aligned_bss;
  144. static struct desc_ptr idt_descr __ro_after_init = {
  145. .size = IDT_TABLE_SIZE - 1,
  146. .address = (unsigned long) idt_table,
  147. };
  148. void load_current_idt(void)
  149. {
  150. lockdep_assert_irqs_disabled();
  151. load_idt(&idt_descr);
  152. }
  153. #ifdef CONFIG_X86_F00F_BUG
  154. bool idt_is_f00f_address(unsigned long address)
  155. {
  156. return ((address - idt_descr.address) >> 3) == 6;
  157. }
  158. #endif
  159. static __init void
  160. idt_setup_from_table(gate_desc *idt, const struct idt_data *t, int size, bool sys)
  161. {
  162. gate_desc desc;
  163. for (; size > 0; t++, size--) {
  164. idt_init_desc(&desc, t);
  165. write_idt_entry(idt, t->vector, &desc);
  166. if (sys)
  167. set_bit(t->vector, system_vectors);
  168. }
  169. }
  170. static __init void set_intr_gate(unsigned int n, const void *addr)
  171. {
  172. struct idt_data data;
  173. init_idt_data(&data, n, addr);
  174. idt_setup_from_table(idt_table, &data, 1, false);
  175. }
  176. /**
  177. * idt_setup_early_traps - Initialize the idt table with early traps
  178. *
  179. * On X8664 these traps do not use interrupt stacks as they can't work
  180. * before cpu_init() is invoked and sets up TSS. The IST variants are
  181. * installed after that.
  182. */
  183. void __init idt_setup_early_traps(void)
  184. {
  185. idt_setup_from_table(idt_table, early_idts, ARRAY_SIZE(early_idts),
  186. true);
  187. load_idt(&idt_descr);
  188. }
  189. /**
  190. * idt_setup_traps - Initialize the idt table with default traps
  191. */
  192. void __init idt_setup_traps(void)
  193. {
  194. idt_setup_from_table(idt_table, def_idts, ARRAY_SIZE(def_idts), true);
  195. }
  196. #ifdef CONFIG_X86_64
  197. /*
  198. * Early traps running on the DEFAULT_STACK because the other interrupt
  199. * stacks work only after cpu_init().
  200. */
  201. static const __initconst struct idt_data early_pf_idts[] = {
  202. INTG(X86_TRAP_PF, asm_exc_page_fault),
  203. };
  204. /**
  205. * idt_setup_early_pf - Initialize the idt table with early pagefault handler
  206. *
  207. * On X8664 this does not use interrupt stacks as they can't work before
  208. * cpu_init() is invoked and sets up TSS. The IST variant is installed
  209. * after that.
  210. *
  211. * Note, that X86_64 cannot install the real #PF handler in
  212. * idt_setup_early_traps() because the memory initialization needs the #PF
  213. * handler from the early_idt_handler_array to initialize the early page
  214. * tables.
  215. */
  216. void __init idt_setup_early_pf(void)
  217. {
  218. idt_setup_from_table(idt_table, early_pf_idts,
  219. ARRAY_SIZE(early_pf_idts), true);
  220. }
  221. #endif
  222. static void __init idt_map_in_cea(void)
  223. {
  224. /*
  225. * Set the IDT descriptor to a fixed read-only location in the cpu
  226. * entry area, so that the "sidt" instruction will not leak the
  227. * location of the kernel, and to defend the IDT against arbitrary
  228. * memory write vulnerabilities.
  229. */
  230. cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table),
  231. PAGE_KERNEL_RO);
  232. idt_descr.address = CPU_ENTRY_AREA_RO_IDT;
  233. }
  234. /**
  235. * idt_setup_apic_and_irq_gates - Setup APIC/SMP and normal interrupt gates
  236. */
  237. void __init idt_setup_apic_and_irq_gates(void)
  238. {
  239. int i = FIRST_EXTERNAL_VECTOR;
  240. void *entry;
  241. idt_setup_from_table(idt_table, apic_idts, ARRAY_SIZE(apic_idts), true);
  242. for_each_clear_bit_from(i, system_vectors, FIRST_SYSTEM_VECTOR) {
  243. entry = irq_entries_start + IDT_ALIGN * (i - FIRST_EXTERNAL_VECTOR);
  244. set_intr_gate(i, entry);
  245. }
  246. #ifdef CONFIG_X86_LOCAL_APIC
  247. for_each_clear_bit_from(i, system_vectors, NR_VECTORS) {
  248. /*
  249. * Don't set the non assigned system vectors in the
  250. * system_vectors bitmap. Otherwise they show up in
  251. * /proc/interrupts.
  252. */
  253. entry = spurious_entries_start + IDT_ALIGN * (i - FIRST_SYSTEM_VECTOR);
  254. set_intr_gate(i, entry);
  255. }
  256. #endif
  257. /* Map IDT into CPU entry area and reload it. */
  258. idt_map_in_cea();
  259. load_idt(&idt_descr);
  260. /* Make the IDT table read only */
  261. set_memory_ro((unsigned long)&idt_table, 1);
  262. idt_setup_done = true;
  263. }
  264. /**
  265. * idt_setup_early_handler - Initializes the idt table with early handlers
  266. */
  267. void __init idt_setup_early_handler(void)
  268. {
  269. int i;
  270. for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
  271. set_intr_gate(i, early_idt_handler_array[i]);
  272. #ifdef CONFIG_X86_32
  273. for ( ; i < NR_VECTORS; i++)
  274. set_intr_gate(i, early_ignore_irq);
  275. #endif
  276. load_idt(&idt_descr);
  277. }
  278. /**
  279. * idt_invalidate - Invalidate interrupt descriptor table
  280. */
  281. void idt_invalidate(void)
  282. {
  283. static const struct desc_ptr idt = { .address = 0, .size = 0 };
  284. load_idt(&idt);
  285. }
  286. void __init alloc_intr_gate(unsigned int n, const void *addr)
  287. {
  288. if (WARN_ON(n < FIRST_SYSTEM_VECTOR))
  289. return;
  290. if (WARN_ON(idt_setup_done))
  291. return;
  292. if (!WARN_ON(test_and_set_bit(n, system_vectors)))
  293. set_intr_gate(n, addr);
  294. }