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 73318e974SThomas Gleixner #include <asm/traps.h> 83318e974SThomas Gleixner #include <asm/proto.h> 9d8ed9d48SThomas Gleixner #include <asm/desc.h> 10447ae316SNicolai Stange #include <asm/hw_irq.h> 11d8ed9d48SThomas Gleixner 123318e974SThomas Gleixner struct idt_data { 133318e974SThomas Gleixner unsigned int vector; 143318e974SThomas Gleixner unsigned int segment; 153318e974SThomas Gleixner struct idt_bits bits; 163318e974SThomas Gleixner const void *addr; 173318e974SThomas Gleixner }; 183318e974SThomas Gleixner 193318e974SThomas Gleixner #define DPL0 0x0 203318e974SThomas Gleixner #define DPL3 0x3 213318e974SThomas Gleixner 223318e974SThomas Gleixner #define DEFAULT_STACK 0 233318e974SThomas Gleixner 243318e974SThomas Gleixner #define G(_vector, _addr, _ist, _type, _dpl, _segment) \ 253318e974SThomas Gleixner { \ 263318e974SThomas Gleixner .vector = _vector, \ 273318e974SThomas Gleixner .bits.ist = _ist, \ 283318e974SThomas Gleixner .bits.type = _type, \ 293318e974SThomas Gleixner .bits.dpl = _dpl, \ 303318e974SThomas Gleixner .bits.p = 1, \ 313318e974SThomas Gleixner .addr = _addr, \ 323318e974SThomas Gleixner .segment = _segment, \ 333318e974SThomas Gleixner } 343318e974SThomas Gleixner 353318e974SThomas Gleixner /* Interrupt gate */ 363318e974SThomas Gleixner #define INTG(_vector, _addr) \ 373318e974SThomas Gleixner G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL0, __KERNEL_CS) 383318e974SThomas Gleixner 393318e974SThomas Gleixner /* System interrupt gate */ 403318e974SThomas Gleixner #define SYSG(_vector, _addr) \ 413318e974SThomas Gleixner G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL3, __KERNEL_CS) 423318e974SThomas Gleixner 438f34c5b5SThomas Gleixner /* 448f34c5b5SThomas Gleixner * Interrupt gate with interrupt stack. The _ist index is the index in 458f34c5b5SThomas Gleixner * the tss.ist[] array, but for the descriptor it needs to start at 1. 468f34c5b5SThomas Gleixner */ 473318e974SThomas Gleixner #define ISTG(_vector, _addr, _ist) \ 488f34c5b5SThomas Gleixner G(_vector, _addr, _ist + 1, GATE_INTERRUPT, DPL0, __KERNEL_CS) 493318e974SThomas Gleixner 503318e974SThomas Gleixner /* Task gate */ 513318e974SThomas Gleixner #define TSKG(_vector, _gdt) \ 523318e974SThomas Gleixner G(_vector, NULL, DEFAULT_STACK, GATE_TASK, DPL0, _gdt << 3) 533318e974SThomas Gleixner 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), 64433f8924SThomas Gleixner #ifdef CONFIG_X86_32 6591eeafeaSThomas Gleixner INTG(X86_TRAP_PF, asm_exc_page_fault), 66433f8924SThomas Gleixner #endif 67433f8924SThomas Gleixner }; 68433f8924SThomas Gleixner 69b70543a0SThomas Gleixner /* 70b70543a0SThomas Gleixner * The default IDT entries which are set up in trap_init() before 71b70543a0SThomas Gleixner * cpu_init() is invoked. Interrupt stacks cannot be used at that point and 72b70543a0SThomas Gleixner * the traps which use them are reinitialized with IST after cpu_init() has 73b70543a0SThomas Gleixner * set up TSS. 74b70543a0SThomas Gleixner */ 75327867faSAndi Kleen static const __initconst struct idt_data def_idts[] = { 769d06c402SThomas Gleixner INTG(X86_TRAP_DE, asm_exc_divide_error), 776271fef0SThomas Gleixner INTG(X86_TRAP_NMI, asm_exc_nmi), 7858d9c81fSThomas Gleixner INTG(X86_TRAP_BR, asm_exc_bounds), 7949893c5cSThomas Gleixner INTG(X86_TRAP_UD, asm_exc_invalid_op), 80866ae2ccSThomas Gleixner INTG(X86_TRAP_NM, asm_exc_device_not_available), 81f95658fdSThomas Gleixner INTG(X86_TRAP_OLD_MF, asm_exc_coproc_segment_overrun), 8297b3d290SThomas Gleixner INTG(X86_TRAP_TS, asm_exc_invalid_tss), 8399a3fb8dSThomas Gleixner INTG(X86_TRAP_NP, asm_exc_segment_not_present), 84fd9689bfSThomas Gleixner INTG(X86_TRAP_SS, asm_exc_stack_segment), 85be4c11afSThomas Gleixner INTG(X86_TRAP_GP, asm_exc_general_protection), 86dad7106fSThomas Gleixner INTG(X86_TRAP_SPURIOUS, asm_exc_spurious_interrupt_bug), 8714a8bd2aSThomas Gleixner INTG(X86_TRAP_MF, asm_exc_coprocessor_error), 88436608bbSThomas Gleixner INTG(X86_TRAP_AC, asm_exc_alignment_check), 8948227e21SThomas Gleixner INTG(X86_TRAP_XF, asm_exc_simd_coprocessor_error), 90b70543a0SThomas Gleixner 91b70543a0SThomas Gleixner #ifdef CONFIG_X86_32 92b70543a0SThomas Gleixner TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS), 93b70543a0SThomas Gleixner #else 94c29c775aSThomas Gleixner INTG(X86_TRAP_DF, asm_exc_double_fault), 95b70543a0SThomas Gleixner #endif 962bbc68f8SThomas Gleixner INTG(X86_TRAP_DB, asm_exc_debug), 97b70543a0SThomas Gleixner 98b70543a0SThomas Gleixner #ifdef CONFIG_X86_MCE 998cd501c1SThomas Gleixner INTG(X86_TRAP_MC, asm_exc_machine_check), 100b70543a0SThomas Gleixner #endif 101b70543a0SThomas Gleixner 1024b6b9111SThomas Gleixner SYSG(X86_TRAP_OF, asm_exc_overflow), 103b70543a0SThomas Gleixner #if defined(CONFIG_IA32_EMULATION) 104b70543a0SThomas Gleixner SYSG(IA32_SYSCALL_VECTOR, entry_INT80_compat), 105b70543a0SThomas Gleixner #elif defined(CONFIG_X86_32) 106b70543a0SThomas Gleixner SYSG(IA32_SYSCALL_VECTOR, entry_INT80_32), 107b70543a0SThomas Gleixner #endif 108b70543a0SThomas Gleixner }; 109b70543a0SThomas Gleixner 110636a7598SThomas Gleixner /* 111636a7598SThomas Gleixner * The APIC and SMP idt entries 112636a7598SThomas Gleixner */ 113327867faSAndi Kleen static const __initconst struct idt_data apic_idts[] = { 114636a7598SThomas Gleixner #ifdef CONFIG_SMP 11513cad985SThomas Gleixner INTG(RESCHEDULE_VECTOR, asm_sysvec_reschedule_ipi), 116582f9191SThomas Gleixner INTG(CALL_FUNCTION_VECTOR, asm_sysvec_call_function), 117582f9191SThomas Gleixner INTG(CALL_FUNCTION_SINGLE_VECTOR, asm_sysvec_call_function_single), 118582f9191SThomas Gleixner INTG(IRQ_MOVE_CLEANUP_VECTOR, asm_sysvec_irq_move_cleanup), 119582f9191SThomas Gleixner INTG(REBOOT_VECTOR, asm_sysvec_reboot), 120636a7598SThomas Gleixner #endif 121636a7598SThomas Gleixner 122636a7598SThomas Gleixner #ifdef CONFIG_X86_THERMAL_VECTOR 123720909a7SThomas Gleixner INTG(THERMAL_APIC_VECTOR, asm_sysvec_thermal), 124636a7598SThomas Gleixner #endif 125636a7598SThomas Gleixner 126636a7598SThomas Gleixner #ifdef CONFIG_X86_MCE_THRESHOLD 127720909a7SThomas Gleixner INTG(THRESHOLD_APIC_VECTOR, asm_sysvec_threshold), 128636a7598SThomas Gleixner #endif 129636a7598SThomas Gleixner 130636a7598SThomas Gleixner #ifdef CONFIG_X86_MCE_AMD 131720909a7SThomas Gleixner INTG(DEFERRED_ERROR_VECTOR, asm_sysvec_deferred_error), 132636a7598SThomas Gleixner #endif 133636a7598SThomas Gleixner 134636a7598SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC 135db0338eeSThomas Gleixner INTG(LOCAL_TIMER_VECTOR, asm_sysvec_apic_timer_interrupt), 136db0338eeSThomas Gleixner INTG(X86_PLATFORM_IPI_VECTOR, asm_sysvec_x86_platform_ipi), 137636a7598SThomas Gleixner # ifdef CONFIG_HAVE_KVM 1389c3b1f49SThomas Gleixner INTG(POSTED_INTR_VECTOR, asm_sysvec_kvm_posted_intr_ipi), 1399c3b1f49SThomas Gleixner INTG(POSTED_INTR_WAKEUP_VECTOR, asm_sysvec_kvm_posted_intr_wakeup_ipi), 1409c3b1f49SThomas Gleixner INTG(POSTED_INTR_NESTED_VECTOR, asm_sysvec_kvm_posted_intr_nested_ipi), 141636a7598SThomas Gleixner # endif 142636a7598SThomas Gleixner # ifdef CONFIG_IRQ_WORK 143720909a7SThomas Gleixner INTG(IRQ_WORK_VECTOR, asm_sysvec_irq_work), 144636a7598SThomas Gleixner # endif 145151ad17fSAndrew Banman # ifdef CONFIG_X86_UV 146720909a7SThomas Gleixner INTG(UV_BAU_MESSAGE, asm_sysvec_uv_bau_message), 147151ad17fSAndrew Banman # endif 148db0338eeSThomas Gleixner INTG(SPURIOUS_APIC_VECTOR, asm_sysvec_spurious_apic_interrupt), 149db0338eeSThomas Gleixner INTG(ERROR_APIC_VECTOR, asm_sysvec_error_interrupt), 150636a7598SThomas Gleixner #endif 151636a7598SThomas Gleixner }; 152636a7598SThomas Gleixner 153433f8924SThomas Gleixner #ifdef CONFIG_X86_64 154433f8924SThomas Gleixner /* 155433f8924SThomas Gleixner * Early traps running on the DEFAULT_STACK because the other interrupt 156433f8924SThomas Gleixner * stacks work only after cpu_init(). 157433f8924SThomas Gleixner */ 158327867faSAndi Kleen static const __initconst struct idt_data early_pf_idts[] = { 15991eeafeaSThomas Gleixner INTG(X86_TRAP_PF, asm_exc_page_fault), 160433f8924SThomas Gleixner }; 161433f8924SThomas Gleixner #endif 162433f8924SThomas Gleixner 163d8ed9d48SThomas Gleixner /* Must be page-aligned because the real IDT is used in a fixmap. */ 164d8ed9d48SThomas Gleixner gate_desc idt_table[IDT_ENTRIES] __page_aligned_bss; 165d8ed9d48SThomas Gleixner 16616bc18d8SThomas Gleixner struct desc_ptr idt_descr __ro_after_init = { 16716bc18d8SThomas Gleixner .size = (IDT_ENTRIES * 2 * sizeof(unsigned long)) - 1, 16816bc18d8SThomas Gleixner .address = (unsigned long) idt_table, 16916bc18d8SThomas Gleixner }; 17016bc18d8SThomas Gleixner 171d8ed9d48SThomas Gleixner #ifdef CONFIG_X86_64 1720a30908bSThomas Gleixner /* 17390f6225fSThomas Gleixner * The exceptions which use Interrupt stacks. They are setup after 17490f6225fSThomas Gleixner * cpu_init() when the TSS has been initialized. 17590f6225fSThomas Gleixner */ 176327867faSAndi Kleen static const __initconst struct idt_data ist_idts[] = { 1772bbc68f8SThomas Gleixner ISTG(X86_TRAP_DB, asm_exc_debug, IST_INDEX_DB), 1786271fef0SThomas Gleixner ISTG(X86_TRAP_NMI, asm_exc_nmi, IST_INDEX_NMI), 179c29c775aSThomas Gleixner ISTG(X86_TRAP_DF, asm_exc_double_fault, IST_INDEX_DF), 18090f6225fSThomas Gleixner #ifdef CONFIG_X86_MCE 1818cd501c1SThomas Gleixner ISTG(X86_TRAP_MC, asm_exc_machine_check, IST_INDEX_MCE), 18290f6225fSThomas Gleixner #endif 18390f6225fSThomas Gleixner }; 184d8ed9d48SThomas Gleixner #endif 185e802a51eSThomas Gleixner 1863318e974SThomas Gleixner static inline void idt_init_desc(gate_desc *gate, const struct idt_data *d) 1873318e974SThomas Gleixner { 1883318e974SThomas Gleixner unsigned long addr = (unsigned long) d->addr; 1893318e974SThomas Gleixner 1903318e974SThomas Gleixner gate->offset_low = (u16) addr; 1913318e974SThomas Gleixner gate->segment = (u16) d->segment; 1923318e974SThomas Gleixner gate->bits = d->bits; 1933318e974SThomas Gleixner gate->offset_middle = (u16) (addr >> 16); 1943318e974SThomas Gleixner #ifdef CONFIG_X86_64 1953318e974SThomas Gleixner gate->offset_high = (u32) (addr >> 32); 1963318e974SThomas Gleixner gate->reserved = 0; 1973318e974SThomas Gleixner #endif 1983318e974SThomas Gleixner } 1993318e974SThomas Gleixner 200*bdf5bde8SThomas Gleixner static __init void 201db18da78SThomas Gleixner idt_setup_from_table(gate_desc *idt, const struct idt_data *t, int size, bool sys) 2023318e974SThomas Gleixner { 2033318e974SThomas Gleixner gate_desc desc; 2043318e974SThomas Gleixner 2053318e974SThomas Gleixner for (; size > 0; t++, size--) { 2063318e974SThomas Gleixner idt_init_desc(&desc, t); 2073318e974SThomas Gleixner write_idt_entry(idt, t->vector, &desc); 208db18da78SThomas Gleixner if (sys) 2097854f822SThomas Gleixner set_bit(t->vector, system_vectors); 2103318e974SThomas Gleixner } 2113318e974SThomas Gleixner } 2123318e974SThomas Gleixner 213*bdf5bde8SThomas Gleixner static __init void set_intr_gate(unsigned int n, const void *addr) 214facaa3e3SThomas Gleixner { 215facaa3e3SThomas Gleixner struct idt_data data; 216facaa3e3SThomas Gleixner 217facaa3e3SThomas Gleixner BUG_ON(n > 0xFF); 218facaa3e3SThomas Gleixner 219facaa3e3SThomas Gleixner memset(&data, 0, sizeof(data)); 220facaa3e3SThomas Gleixner data.vector = n; 221facaa3e3SThomas Gleixner data.addr = addr; 222facaa3e3SThomas Gleixner data.segment = __KERNEL_CS; 223facaa3e3SThomas Gleixner data.bits.type = GATE_INTERRUPT; 224facaa3e3SThomas Gleixner data.bits.p = 1; 225facaa3e3SThomas Gleixner 226facaa3e3SThomas Gleixner idt_setup_from_table(idt_table, &data, 1, false); 227facaa3e3SThomas Gleixner } 228facaa3e3SThomas Gleixner 229e802a51eSThomas Gleixner /** 230433f8924SThomas Gleixner * idt_setup_early_traps - Initialize the idt table with early traps 231433f8924SThomas Gleixner * 232433f8924SThomas Gleixner * On X8664 these traps do not use interrupt stacks as they can't work 233433f8924SThomas Gleixner * before cpu_init() is invoked and sets up TSS. The IST variants are 234433f8924SThomas Gleixner * installed after that. 235433f8924SThomas Gleixner */ 236433f8924SThomas Gleixner void __init idt_setup_early_traps(void) 237433f8924SThomas Gleixner { 238db18da78SThomas Gleixner idt_setup_from_table(idt_table, early_idts, ARRAY_SIZE(early_idts), 239db18da78SThomas Gleixner true); 240433f8924SThomas Gleixner load_idt(&idt_descr); 241433f8924SThomas Gleixner } 242433f8924SThomas Gleixner 243b70543a0SThomas Gleixner /** 244b70543a0SThomas Gleixner * idt_setup_traps - Initialize the idt table with default traps 245b70543a0SThomas Gleixner */ 246b70543a0SThomas Gleixner void __init idt_setup_traps(void) 247b70543a0SThomas Gleixner { 248db18da78SThomas Gleixner idt_setup_from_table(idt_table, def_idts, ARRAY_SIZE(def_idts), true); 249b70543a0SThomas Gleixner } 250b70543a0SThomas Gleixner 251433f8924SThomas Gleixner #ifdef CONFIG_X86_64 252433f8924SThomas Gleixner /** 253433f8924SThomas Gleixner * idt_setup_early_pf - Initialize the idt table with early pagefault handler 254433f8924SThomas Gleixner * 255433f8924SThomas Gleixner * On X8664 this does not use interrupt stacks as they can't work before 256433f8924SThomas Gleixner * cpu_init() is invoked and sets up TSS. The IST variant is installed 257433f8924SThomas Gleixner * after that. 258433f8924SThomas Gleixner * 259433f8924SThomas Gleixner * FIXME: Why is 32bit and 64bit installing the PF handler at different 260433f8924SThomas Gleixner * places in the early setup code? 261433f8924SThomas Gleixner */ 262433f8924SThomas Gleixner void __init idt_setup_early_pf(void) 263433f8924SThomas Gleixner { 264433f8924SThomas Gleixner idt_setup_from_table(idt_table, early_pf_idts, 265db18da78SThomas Gleixner ARRAY_SIZE(early_pf_idts), true); 266433f8924SThomas Gleixner } 2670a30908bSThomas Gleixner 2680a30908bSThomas Gleixner /** 26990f6225fSThomas Gleixner * idt_setup_ist_traps - Initialize the idt table with traps using IST 27090f6225fSThomas Gleixner */ 27190f6225fSThomas Gleixner void __init idt_setup_ist_traps(void) 27290f6225fSThomas Gleixner { 273db18da78SThomas Gleixner idt_setup_from_table(idt_table, ist_idts, ARRAY_SIZE(ist_idts), true); 27490f6225fSThomas Gleixner } 275433f8924SThomas Gleixner #endif 276433f8924SThomas Gleixner 277433f8924SThomas Gleixner /** 278636a7598SThomas Gleixner * idt_setup_apic_and_irq_gates - Setup APIC/SMP and normal interrupt gates 279636a7598SThomas Gleixner */ 280636a7598SThomas Gleixner void __init idt_setup_apic_and_irq_gates(void) 281636a7598SThomas Gleixner { 282dc20b2d5SThomas Gleixner int i = FIRST_EXTERNAL_VECTOR; 283dc20b2d5SThomas Gleixner void *entry; 284dc20b2d5SThomas Gleixner 285db18da78SThomas Gleixner idt_setup_from_table(idt_table, apic_idts, ARRAY_SIZE(apic_idts), true); 286dc20b2d5SThomas Gleixner 2877854f822SThomas Gleixner for_each_clear_bit_from(i, system_vectors, FIRST_SYSTEM_VECTOR) { 288dc20b2d5SThomas Gleixner entry = irq_entries_start + 8 * (i - FIRST_EXTERNAL_VECTOR); 289dc20b2d5SThomas Gleixner set_intr_gate(i, entry); 290dc20b2d5SThomas Gleixner } 291dc20b2d5SThomas Gleixner 292dc20b2d5SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC 29333662812SDou Liyang for_each_clear_bit_from(i, system_vectors, NR_VECTORS) { 2941f1fbc70SVitaly Kuznetsov /* 2951f1fbc70SVitaly Kuznetsov * Don't set the non assigned system vectors in the 2961f1fbc70SVitaly Kuznetsov * system_vectors bitmap. Otherwise they show up in 2971f1fbc70SVitaly Kuznetsov * /proc/interrupts. 2981f1fbc70SVitaly Kuznetsov */ 299f8a8fe61SThomas Gleixner entry = spurious_entries_start + 8 * (i - FIRST_SYSTEM_VECTOR); 300f8a8fe61SThomas Gleixner set_intr_gate(i, entry); 301dc20b2d5SThomas Gleixner } 30233662812SDou Liyang #endif 30306184325SVitaly Kuznetsov idt_setup_done = true; 304636a7598SThomas Gleixner } 305636a7598SThomas Gleixner 306636a7598SThomas Gleixner /** 307588787fdSThomas Gleixner * idt_setup_early_handler - Initializes the idt table with early handlers 308588787fdSThomas Gleixner */ 309588787fdSThomas Gleixner void __init idt_setup_early_handler(void) 310588787fdSThomas Gleixner { 311588787fdSThomas Gleixner int i; 312588787fdSThomas Gleixner 313588787fdSThomas Gleixner for (i = 0; i < NUM_EXCEPTION_VECTORS; i++) 314588787fdSThomas Gleixner set_intr_gate(i, early_idt_handler_array[i]); 31587e81786SThomas Gleixner #ifdef CONFIG_X86_32 31687e81786SThomas Gleixner for ( ; i < NR_VECTORS; i++) 31787e81786SThomas Gleixner set_intr_gate(i, early_ignore_irq); 31887e81786SThomas Gleixner #endif 319588787fdSThomas Gleixner load_idt(&idt_descr); 320588787fdSThomas Gleixner } 321588787fdSThomas Gleixner 322588787fdSThomas Gleixner /** 323e802a51eSThomas Gleixner * idt_invalidate - Invalidate interrupt descriptor table 324e802a51eSThomas Gleixner * @addr: The virtual address of the 'invalid' IDT 325e802a51eSThomas Gleixner */ 326e802a51eSThomas Gleixner void idt_invalidate(void *addr) 327e802a51eSThomas Gleixner { 328e802a51eSThomas Gleixner struct desc_ptr idt = { .address = (unsigned long) addr, .size = 0 }; 329e802a51eSThomas Gleixner 330e802a51eSThomas Gleixner load_idt(&idt); 331e802a51eSThomas Gleixner } 332db18da78SThomas Gleixner 33306184325SVitaly Kuznetsov void __init alloc_intr_gate(unsigned int n, const void *addr) 334db18da78SThomas Gleixner { 33506184325SVitaly Kuznetsov if (WARN_ON(n < FIRST_SYSTEM_VECTOR)) 33606184325SVitaly Kuznetsov return; 33706184325SVitaly Kuznetsov 33806184325SVitaly Kuznetsov if (WARN_ON(idt_setup_done)) 33906184325SVitaly Kuznetsov return; 34006184325SVitaly Kuznetsov 34106184325SVitaly Kuznetsov if (!WARN_ON(test_and_set_bit(n, system_vectors))) 342db18da78SThomas Gleixner set_intr_gate(n, addr); 343db18da78SThomas Gleixner } 344