182c73e0aSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2d8ed9d48SThomas Gleixner /* 3d8ed9d48SThomas Gleixner * Interrupt descriptor table related code 4d8ed9d48SThomas Gleixner */ 5d8ed9d48SThomas Gleixner #include <linux/interrupt.h> 6d8ed9d48SThomas Gleixner 700229a54SThomas Gleixner #include <asm/cpu_entry_area.h> 83e77abdaSThomas Gleixner #include <asm/set_memory.h> 93318e974SThomas Gleixner #include <asm/traps.h> 103318e974SThomas Gleixner #include <asm/proto.h> 11d8ed9d48SThomas Gleixner #include <asm/desc.h> 12447ae316SNicolai Stange #include <asm/hw_irq.h> 13d8ed9d48SThomas Gleixner 143318e974SThomas Gleixner #define DPL0 0x0 153318e974SThomas Gleixner #define DPL3 0x3 163318e974SThomas Gleixner 173318e974SThomas Gleixner #define DEFAULT_STACK 0 183318e974SThomas Gleixner 193318e974SThomas Gleixner #define G(_vector, _addr, _ist, _type, _dpl, _segment) \ 203318e974SThomas Gleixner { \ 213318e974SThomas Gleixner .vector = _vector, \ 223318e974SThomas Gleixner .bits.ist = _ist, \ 233318e974SThomas Gleixner .bits.type = _type, \ 243318e974SThomas Gleixner .bits.dpl = _dpl, \ 253318e974SThomas Gleixner .bits.p = 1, \ 263318e974SThomas Gleixner .addr = _addr, \ 273318e974SThomas Gleixner .segment = _segment, \ 283318e974SThomas Gleixner } 293318e974SThomas Gleixner 303318e974SThomas Gleixner /* Interrupt gate */ 313318e974SThomas Gleixner #define INTG(_vector, _addr) \ 323318e974SThomas Gleixner G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL0, __KERNEL_CS) 333318e974SThomas Gleixner 343318e974SThomas Gleixner /* System interrupt gate */ 353318e974SThomas Gleixner #define SYSG(_vector, _addr) \ 363318e974SThomas Gleixner G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL3, __KERNEL_CS) 373318e974SThomas Gleixner 38*1dcc917aSThomas Gleixner #ifdef CONFIG_X86_64 398f34c5b5SThomas Gleixner /* 408f34c5b5SThomas Gleixner * Interrupt gate with interrupt stack. The _ist index is the index in 418f34c5b5SThomas Gleixner * the tss.ist[] array, but for the descriptor it needs to start at 1. 428f34c5b5SThomas Gleixner */ 433318e974SThomas Gleixner #define ISTG(_vector, _addr, _ist) \ 448f34c5b5SThomas Gleixner G(_vector, _addr, _ist + 1, GATE_INTERRUPT, DPL0, __KERNEL_CS) 45*1dcc917aSThomas Gleixner #else 46*1dcc917aSThomas Gleixner #define ISTG(_vector, _addr, _ist) INTG(_vector, _addr) 47*1dcc917aSThomas Gleixner #endif 483318e974SThomas Gleixner 493318e974SThomas Gleixner /* Task gate */ 503318e974SThomas Gleixner #define TSKG(_vector, _gdt) \ 513318e974SThomas Gleixner G(_vector, NULL, DEFAULT_STACK, GATE_TASK, DPL0, _gdt << 3) 523318e974SThomas Gleixner 535a2bafcaSThomas Gleixner #define IDT_TABLE_SIZE (IDT_ENTRIES * sizeof(gate_desc)) 5406184325SVitaly Kuznetsov 5506184325SVitaly Kuznetsov static bool idt_setup_done __initdata; 5606184325SVitaly Kuznetsov 57433f8924SThomas Gleixner /* 58433f8924SThomas Gleixner * Early traps running on the DEFAULT_STACK because the other interrupt 59433f8924SThomas Gleixner * stacks work only after cpu_init(). 60433f8924SThomas Gleixner */ 61327867faSAndi Kleen static const __initconst struct idt_data early_idts[] = { 622bbc68f8SThomas Gleixner INTG(X86_TRAP_DB, asm_exc_debug), 638edd7e37SThomas Gleixner SYSG(X86_TRAP_BP, asm_exc_int3), 6494438af4SThomas Gleixner 65433f8924SThomas Gleixner #ifdef CONFIG_X86_32 6694438af4SThomas Gleixner /* 6794438af4SThomas Gleixner * Not possible on 64-bit. See idt_setup_early_pf() for details. 6894438af4SThomas Gleixner */ 6991eeafeaSThomas Gleixner INTG(X86_TRAP_PF, asm_exc_page_fault), 70433f8924SThomas Gleixner #endif 71433f8924SThomas Gleixner }; 72433f8924SThomas Gleixner 73b70543a0SThomas Gleixner /* 74b70543a0SThomas Gleixner * The default IDT entries which are set up in trap_init() before 75b70543a0SThomas Gleixner * cpu_init() is invoked. Interrupt stacks cannot be used at that point and 76b70543a0SThomas Gleixner * the traps which use them are reinitialized with IST after cpu_init() has 77b70543a0SThomas Gleixner * set up TSS. 78b70543a0SThomas Gleixner */ 79327867faSAndi Kleen static const __initconst struct idt_data def_idts[] = { 809d06c402SThomas Gleixner INTG(X86_TRAP_DE, asm_exc_divide_error), 81*1dcc917aSThomas Gleixner ISTG(X86_TRAP_NMI, asm_exc_nmi, IST_INDEX_NMI), 8258d9c81fSThomas Gleixner INTG(X86_TRAP_BR, asm_exc_bounds), 8349893c5cSThomas Gleixner INTG(X86_TRAP_UD, asm_exc_invalid_op), 84866ae2ccSThomas Gleixner INTG(X86_TRAP_NM, asm_exc_device_not_available), 85f95658fdSThomas Gleixner INTG(X86_TRAP_OLD_MF, asm_exc_coproc_segment_overrun), 8697b3d290SThomas Gleixner INTG(X86_TRAP_TS, asm_exc_invalid_tss), 8799a3fb8dSThomas Gleixner INTG(X86_TRAP_NP, asm_exc_segment_not_present), 88fd9689bfSThomas Gleixner INTG(X86_TRAP_SS, asm_exc_stack_segment), 89be4c11afSThomas Gleixner INTG(X86_TRAP_GP, asm_exc_general_protection), 90dad7106fSThomas Gleixner INTG(X86_TRAP_SPURIOUS, asm_exc_spurious_interrupt_bug), 9114a8bd2aSThomas Gleixner INTG(X86_TRAP_MF, asm_exc_coprocessor_error), 92436608bbSThomas Gleixner INTG(X86_TRAP_AC, asm_exc_alignment_check), 9348227e21SThomas Gleixner INTG(X86_TRAP_XF, asm_exc_simd_coprocessor_error), 94b70543a0SThomas Gleixner 95b70543a0SThomas Gleixner #ifdef CONFIG_X86_32 96b70543a0SThomas Gleixner TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS), 97b70543a0SThomas Gleixner #else 98*1dcc917aSThomas Gleixner ISTG(X86_TRAP_DF, asm_exc_double_fault, IST_INDEX_DF), 99b70543a0SThomas Gleixner #endif 100*1dcc917aSThomas Gleixner ISTG(X86_TRAP_DB, asm_exc_debug, IST_INDEX_DB), 101b70543a0SThomas Gleixner 102b70543a0SThomas Gleixner #ifdef CONFIG_X86_MCE 103*1dcc917aSThomas Gleixner ISTG(X86_TRAP_MC, asm_exc_machine_check, IST_INDEX_MCE), 104*1dcc917aSThomas Gleixner #endif 105*1dcc917aSThomas Gleixner 106*1dcc917aSThomas Gleixner #ifdef CONFIG_AMD_MEM_ENCRYPT 107*1dcc917aSThomas Gleixner ISTG(X86_TRAP_VC, asm_exc_vmm_communication, IST_INDEX_VC), 108b70543a0SThomas Gleixner #endif 109b70543a0SThomas Gleixner 1104b6b9111SThomas Gleixner SYSG(X86_TRAP_OF, asm_exc_overflow), 111b70543a0SThomas Gleixner #if defined(CONFIG_IA32_EMULATION) 112b70543a0SThomas Gleixner SYSG(IA32_SYSCALL_VECTOR, entry_INT80_compat), 113b70543a0SThomas Gleixner #elif defined(CONFIG_X86_32) 114b70543a0SThomas Gleixner SYSG(IA32_SYSCALL_VECTOR, entry_INT80_32), 115b70543a0SThomas Gleixner #endif 116b70543a0SThomas Gleixner }; 117b70543a0SThomas Gleixner 118636a7598SThomas Gleixner /* 119636a7598SThomas Gleixner * The APIC and SMP idt entries 120636a7598SThomas Gleixner */ 121327867faSAndi Kleen static const __initconst struct idt_data apic_idts[] = { 122636a7598SThomas Gleixner #ifdef CONFIG_SMP 12313cad985SThomas Gleixner INTG(RESCHEDULE_VECTOR, asm_sysvec_reschedule_ipi), 124582f9191SThomas Gleixner INTG(CALL_FUNCTION_VECTOR, asm_sysvec_call_function), 125582f9191SThomas Gleixner INTG(CALL_FUNCTION_SINGLE_VECTOR, asm_sysvec_call_function_single), 126582f9191SThomas Gleixner INTG(IRQ_MOVE_CLEANUP_VECTOR, asm_sysvec_irq_move_cleanup), 127582f9191SThomas Gleixner INTG(REBOOT_VECTOR, asm_sysvec_reboot), 128636a7598SThomas Gleixner #endif 129636a7598SThomas Gleixner 130636a7598SThomas Gleixner #ifdef CONFIG_X86_THERMAL_VECTOR 131720909a7SThomas Gleixner INTG(THERMAL_APIC_VECTOR, asm_sysvec_thermal), 132636a7598SThomas Gleixner #endif 133636a7598SThomas Gleixner 134636a7598SThomas Gleixner #ifdef CONFIG_X86_MCE_THRESHOLD 135720909a7SThomas Gleixner INTG(THRESHOLD_APIC_VECTOR, asm_sysvec_threshold), 136636a7598SThomas Gleixner #endif 137636a7598SThomas Gleixner 138636a7598SThomas Gleixner #ifdef CONFIG_X86_MCE_AMD 139720909a7SThomas Gleixner INTG(DEFERRED_ERROR_VECTOR, asm_sysvec_deferred_error), 140636a7598SThomas Gleixner #endif 141636a7598SThomas Gleixner 142636a7598SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC 143db0338eeSThomas Gleixner INTG(LOCAL_TIMER_VECTOR, asm_sysvec_apic_timer_interrupt), 144db0338eeSThomas Gleixner INTG(X86_PLATFORM_IPI_VECTOR, asm_sysvec_x86_platform_ipi), 145636a7598SThomas Gleixner # ifdef CONFIG_HAVE_KVM 1469c3b1f49SThomas Gleixner INTG(POSTED_INTR_VECTOR, asm_sysvec_kvm_posted_intr_ipi), 1479c3b1f49SThomas Gleixner INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi), 1489c3b1f49SThomas Gleixner INTG(POSTED_INTR_NESTED_VECTOR, asm_sysvec_kvm_posted_intr_nested_ipi), 149636a7598SThomas Gleixner # endif 150636a7598SThomas Gleixner # ifdef CONFIG_IRQ_WORK 151720909a7SThomas Gleixner INTG(IRQ_WORK_VECTOR, asm_sysvec_irq_work), 152636a7598SThomas Gleixner # endif 153db0338eeSThomas Gleixner INTG(SPURIOUS_APIC_VECTOR, asm_sysvec_spurious_apic_interrupt), 154db0338eeSThomas Gleixner INTG(ERROR_APIC_VECTOR, asm_sysvec_error_interrupt), 155636a7598SThomas Gleixner #endif 156636a7598SThomas Gleixner }; 157636a7598SThomas Gleixner 1583e77abdaSThomas Gleixner /* Must be page-aligned because the real IDT is used in the cpu entry area */ 1593e77abdaSThomas Gleixner static gate_desc idt_table[IDT_ENTRIES] __page_aligned_bss; 160d8ed9d48SThomas Gleixner 161286d966bSJason Andryuk static struct desc_ptr idt_descr __ro_after_init = { 1625a2bafcaSThomas Gleixner .size = IDT_TABLE_SIZE - 1, 16316bc18d8SThomas Gleixner .address = (unsigned long) idt_table, 16416bc18d8SThomas Gleixner }; 16516bc18d8SThomas Gleixner 1663e77abdaSThomas Gleixner void load_current_idt(void) 1673e77abdaSThomas Gleixner { 1683e77abdaSThomas Gleixner lockdep_assert_irqs_disabled(); 1693e77abdaSThomas Gleixner load_idt(&idt_descr); 1703e77abdaSThomas Gleixner } 1713e77abdaSThomas Gleixner 1723e77abdaSThomas Gleixner #ifdef CONFIG_X86_F00F_BUG 1733e77abdaSThomas Gleixner bool idt_is_f00f_address(unsigned long address) 1743e77abdaSThomas Gleixner { 1753e77abdaSThomas Gleixner return ((address - idt_descr.address) >> 3) == 6; 1763e77abdaSThomas Gleixner } 177d8ed9d48SThomas Gleixner #endif 178e802a51eSThomas Gleixner 179bdf5bde8SThomas Gleixner static __init void 180db18da78SThomas Gleixner idt_setup_from_table(gate_desc *idt, const struct idt_data *t, int size, bool sys) 1813318e974SThomas Gleixner { 1823318e974SThomas Gleixner gate_desc desc; 1833318e974SThomas Gleixner 1843318e974SThomas Gleixner for (; size > 0; t++, size--) { 1853318e974SThomas Gleixner idt_init_desc(&desc, t); 1863318e974SThomas Gleixner write_idt_entry(idt, t->vector, &desc); 187db18da78SThomas Gleixner if (sys) 1887854f822SThomas Gleixner set_bit(t->vector, system_vectors); 1893318e974SThomas Gleixner } 1903318e974SThomas Gleixner } 1913318e974SThomas Gleixner 192bdf5bde8SThomas Gleixner static __init void set_intr_gate(unsigned int n, const void *addr) 193facaa3e3SThomas Gleixner { 194facaa3e3SThomas Gleixner struct idt_data data; 195facaa3e3SThomas Gleixner 1964bed2266SJoerg Roedel init_idt_data(&data, n, addr); 197facaa3e3SThomas Gleixner 198facaa3e3SThomas Gleixner idt_setup_from_table(idt_table, &data, 1, false); 199facaa3e3SThomas Gleixner } 200facaa3e3SThomas Gleixner 201e802a51eSThomas Gleixner /** 202433f8924SThomas Gleixner * idt_setup_early_traps - Initialize the idt table with early traps 203433f8924SThomas Gleixner * 204433f8924SThomas Gleixner * On X8664 these traps do not use interrupt stacks as they can't work 205433f8924SThomas Gleixner * before cpu_init() is invoked and sets up TSS. The IST variants are 206433f8924SThomas Gleixner * installed after that. 207433f8924SThomas Gleixner */ 208433f8924SThomas Gleixner void __init idt_setup_early_traps(void) 209433f8924SThomas Gleixner { 210db18da78SThomas Gleixner idt_setup_from_table(idt_table, early_idts, ARRAY_SIZE(early_idts), 211db18da78SThomas Gleixner true); 212433f8924SThomas Gleixner load_idt(&idt_descr); 213433f8924SThomas Gleixner } 214433f8924SThomas Gleixner 215b70543a0SThomas Gleixner /** 216b70543a0SThomas Gleixner * idt_setup_traps - Initialize the idt table with default traps 217b70543a0SThomas Gleixner */ 218b70543a0SThomas Gleixner void __init idt_setup_traps(void) 219b70543a0SThomas Gleixner { 220db18da78SThomas Gleixner idt_setup_from_table(idt_table, def_idts, ARRAY_SIZE(def_idts), true); 221b70543a0SThomas Gleixner } 222b70543a0SThomas Gleixner 223433f8924SThomas Gleixner #ifdef CONFIG_X86_64 2243e77abdaSThomas Gleixner /* 2253e77abdaSThomas Gleixner * Early traps running on the DEFAULT_STACK because the other interrupt 2263e77abdaSThomas Gleixner * stacks work only after cpu_init(). 2273e77abdaSThomas Gleixner */ 2283e77abdaSThomas Gleixner static const __initconst struct idt_data early_pf_idts[] = { 2293e77abdaSThomas Gleixner INTG(X86_TRAP_PF, asm_exc_page_fault), 2303e77abdaSThomas Gleixner }; 2313e77abdaSThomas Gleixner 232433f8924SThomas Gleixner /** 233433f8924SThomas Gleixner * idt_setup_early_pf - Initialize the idt table with early pagefault handler 234433f8924SThomas Gleixner * 235433f8924SThomas Gleixner * On X8664 this does not use interrupt stacks as they can't work before 236433f8924SThomas Gleixner * cpu_init() is invoked and sets up TSS. The IST variant is installed 237433f8924SThomas Gleixner * after that. 238433f8924SThomas Gleixner * 23994438af4SThomas Gleixner * Note, that X86_64 cannot install the real #PF handler in 240d9f6e12fSIngo Molnar * idt_setup_early_traps() because the memory initialization needs the #PF 24194438af4SThomas Gleixner * handler from the early_idt_handler_array to initialize the early page 24294438af4SThomas Gleixner * tables. 243433f8924SThomas Gleixner */ 244433f8924SThomas Gleixner void __init idt_setup_early_pf(void) 245433f8924SThomas Gleixner { 246433f8924SThomas Gleixner idt_setup_from_table(idt_table, early_pf_idts, 247db18da78SThomas Gleixner ARRAY_SIZE(early_pf_idts), true); 248433f8924SThomas Gleixner } 249433f8924SThomas Gleixner #endif 250433f8924SThomas Gleixner 25100229a54SThomas Gleixner static void __init idt_map_in_cea(void) 25200229a54SThomas Gleixner { 25300229a54SThomas Gleixner /* 25400229a54SThomas Gleixner * Set the IDT descriptor to a fixed read-only location in the cpu 25500229a54SThomas Gleixner * entry area, so that the "sidt" instruction will not leak the 25600229a54SThomas Gleixner * location of the kernel, and to defend the IDT against arbitrary 25700229a54SThomas Gleixner * memory write vulnerabilities. 25800229a54SThomas Gleixner */ 25900229a54SThomas Gleixner cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table), 26000229a54SThomas Gleixner PAGE_KERNEL_RO); 26100229a54SThomas Gleixner idt_descr.address = CPU_ENTRY_AREA_RO_IDT; 26200229a54SThomas Gleixner } 26300229a54SThomas Gleixner 264433f8924SThomas Gleixner /** 265636a7598SThomas Gleixner * idt_setup_apic_and_irq_gates - Setup APIC/SMP and normal interrupt gates 266636a7598SThomas Gleixner */ 267636a7598SThomas Gleixner void __init idt_setup_apic_and_irq_gates(void) 268636a7598SThomas Gleixner { 269dc20b2d5SThomas Gleixner int i = FIRST_EXTERNAL_VECTOR; 270dc20b2d5SThomas Gleixner void *entry; 271dc20b2d5SThomas Gleixner 272db18da78SThomas Gleixner idt_setup_from_table(idt_table, apic_idts, ARRAY_SIZE(apic_idts), true); 273dc20b2d5SThomas Gleixner 2747854f822SThomas Gleixner for_each_clear_bit_from(i, system_vectors, FIRST_SYSTEM_VECTOR) { 275dc20b2d5SThomas Gleixner entry = irq_entries_start + 8 * (i - FIRST_EXTERNAL_VECTOR); 276dc20b2d5SThomas Gleixner set_intr_gate(i, entry); 277dc20b2d5SThomas Gleixner } 278dc20b2d5SThomas Gleixner 279dc20b2d5SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC 28033662812SDou Liyang for_each_clear_bit_from(i, system_vectors, NR_VECTORS) { 2811f1fbc70SVitaly Kuznetsov /* 2821f1fbc70SVitaly Kuznetsov * Don't set the non assigned system vectors in the 2831f1fbc70SVitaly Kuznetsov * system_vectors bitmap. Otherwise they show up in 2841f1fbc70SVitaly Kuznetsov * /proc/interrupts. 2851f1fbc70SVitaly Kuznetsov */ 286f8a8fe61SThomas Gleixner entry = spurious_entries_start + 8 * (i - FIRST_SYSTEM_VECTOR); 287f8a8fe61SThomas Gleixner set_intr_gate(i, entry); 288dc20b2d5SThomas Gleixner } 28933662812SDou Liyang #endif 29000229a54SThomas Gleixner /* Map IDT into CPU entry area and reload it. */ 29100229a54SThomas Gleixner idt_map_in_cea(); 29200229a54SThomas Gleixner load_idt(&idt_descr); 29300229a54SThomas Gleixner 2943e77abdaSThomas Gleixner /* Make the IDT table read only */ 2953e77abdaSThomas Gleixner set_memory_ro((unsigned long)&idt_table, 1); 2963e77abdaSThomas Gleixner 29706184325SVitaly Kuznetsov idt_setup_done = true; 298636a7598SThomas Gleixner } 299636a7598SThomas Gleixner 300636a7598SThomas Gleixner /** 301588787fdSThomas Gleixner * idt_setup_early_handler - Initializes the idt table with early handlers 302588787fdSThomas Gleixner */ 303588787fdSThomas Gleixner void __init idt_setup_early_handler(void) 304588787fdSThomas Gleixner { 305588787fdSThomas Gleixner int i; 306588787fdSThomas Gleixner 307588787fdSThomas Gleixner for (i = 0; i < NUM_EXCEPTION_VECTORS; i++) 308588787fdSThomas Gleixner set_intr_gate(i, early_idt_handler_array[i]); 30987e81786SThomas Gleixner #ifdef CONFIG_X86_32 31087e81786SThomas Gleixner for ( ; i < NR_VECTORS; i++) 31187e81786SThomas Gleixner set_intr_gate(i, early_ignore_irq); 31287e81786SThomas Gleixner #endif 313588787fdSThomas Gleixner load_idt(&idt_descr); 314588787fdSThomas Gleixner } 315588787fdSThomas Gleixner 316588787fdSThomas Gleixner /** 317e802a51eSThomas Gleixner * idt_invalidate - Invalidate interrupt descriptor table 318e802a51eSThomas Gleixner * @addr: The virtual address of the 'invalid' IDT 319e802a51eSThomas Gleixner */ 320e802a51eSThomas Gleixner void idt_invalidate(void *addr) 321e802a51eSThomas Gleixner { 322e802a51eSThomas Gleixner struct desc_ptr idt = { .address = (unsigned long) addr, .size = 0 }; 323e802a51eSThomas Gleixner 324e802a51eSThomas Gleixner load_idt(&idt); 325e802a51eSThomas Gleixner } 326db18da78SThomas Gleixner 32706184325SVitaly Kuznetsov void __init alloc_intr_gate(unsigned int n, const void *addr) 328db18da78SThomas Gleixner { 32906184325SVitaly Kuznetsov if (WARN_ON(n < FIRST_SYSTEM_VECTOR)) 33006184325SVitaly Kuznetsov return; 33106184325SVitaly Kuznetsov 33206184325SVitaly Kuznetsov if (WARN_ON(idt_setup_done)) 33306184325SVitaly Kuznetsov return; 33406184325SVitaly Kuznetsov 33506184325SVitaly Kuznetsov if (!WARN_ON(test_and_set_bit(n, system_vectors))) 336db18da78SThomas Gleixner set_intr_gate(n, addr); 337db18da78SThomas Gleixner } 338