idt.c 9.4 KB

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