idt.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Interrupt descriptor table related code
  3. *
  4. * This file is licensed under the GPL V2
  5. */
  6. #include <linux/interrupt.h>
  7. #include <asm/traps.h>
  8. #include <asm/proto.h>
  9. #include <asm/desc.h>
  10. #include <asm/hw_irq.h>
  11. struct idt_data {
  12. unsigned int vector;
  13. unsigned int segment;
  14. struct idt_bits bits;
  15. const void *addr;
  16. };
  17. #define DPL0 0x0
  18. #define DPL3 0x3
  19. #define DEFAULT_STACK 0
  20. #define G(_vector, _addr, _ist, _type, _dpl, _segment) \
  21. { \
  22. .vector = _vector, \
  23. .bits.ist = _ist, \
  24. .bits.type = _type, \
  25. .bits.dpl = _dpl, \
  26. .bits.p = 1, \
  27. .addr = _addr, \
  28. .segment = _segment, \
  29. }
  30. /* Interrupt gate */
  31. #define INTG(_vector, _addr) \
  32. G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL0, __KERNEL_CS)
  33. /* System interrupt gate */
  34. #define SYSG(_vector, _addr) \
  35. G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL3, __KERNEL_CS)
  36. /* Interrupt gate with interrupt stack */
  37. #define ISTG(_vector, _addr, _ist) \
  38. G(_vector, _addr, _ist, GATE_INTERRUPT, DPL0, __KERNEL_CS)
  39. /* System interrupt gate with interrupt stack */
  40. #define SISTG(_vector, _addr, _ist) \
  41. G(_vector, _addr, _ist, GATE_INTERRUPT, DPL3, __KERNEL_CS)
  42. /* Task gate */
  43. #define TSKG(_vector, _gdt) \
  44. G(_vector, NULL, DEFAULT_STACK, GATE_TASK, DPL0, _gdt << 3)
  45. /*
  46. * Early traps running on the DEFAULT_STACK because the other interrupt
  47. * stacks work only after cpu_init().
  48. */
  49. static const __initconst struct idt_data early_idts[] = {
  50. INTG(X86_TRAP_DB, debug),
  51. SYSG(X86_TRAP_BP, int3),
  52. #ifdef CONFIG_X86_32
  53. INTG(X86_TRAP_PF, page_fault),
  54. #endif
  55. };
  56. /*
  57. * The default IDT entries which are set up in trap_init() before
  58. * cpu_init() is invoked. Interrupt stacks cannot be used at that point and
  59. * the traps which use them are reinitialized with IST after cpu_init() has
  60. * set up TSS.
  61. */
  62. static const __initconst struct idt_data def_idts[] = {
  63. INTG(X86_TRAP_DE, divide_error),
  64. INTG(X86_TRAP_NMI, nmi),
  65. INTG(X86_TRAP_BR, bounds),
  66. INTG(X86_TRAP_UD, invalid_op),
  67. INTG(X86_TRAP_NM, device_not_available),
  68. INTG(X86_TRAP_OLD_MF, coprocessor_segment_overrun),
  69. INTG(X86_TRAP_TS, invalid_TSS),
  70. INTG(X86_TRAP_NP, segment_not_present),
  71. INTG(X86_TRAP_SS, stack_segment),
  72. INTG(X86_TRAP_GP, general_protection),
  73. INTG(X86_TRAP_SPURIOUS, spurious_interrupt_bug),
  74. INTG(X86_TRAP_MF, coprocessor_error),
  75. INTG(X86_TRAP_AC, alignment_check),
  76. INTG(X86_TRAP_XF, simd_coprocessor_error),
  77. #ifdef CONFIG_X86_32
  78. TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS),
  79. #else
  80. INTG(X86_TRAP_DF, double_fault),
  81. #endif
  82. INTG(X86_TRAP_DB, debug),
  83. #ifdef CONFIG_X86_MCE
  84. INTG(X86_TRAP_MC, &machine_check),
  85. #endif
  86. SYSG(X86_TRAP_OF, overflow),
  87. #if defined(CONFIG_IA32_EMULATION)
  88. SYSG(IA32_SYSCALL_VECTOR, entry_INT80_compat),
  89. #elif defined(CONFIG_X86_32)
  90. SYSG(IA32_SYSCALL_VECTOR, entry_INT80_32),
  91. #endif
  92. };
  93. /*
  94. * The APIC and SMP idt entries
  95. */
  96. static const __initconst struct idt_data apic_idts[] = {
  97. #ifdef CONFIG_SMP
  98. INTG(RESCHEDULE_VECTOR, reschedule_interrupt),
  99. INTG(CALL_FUNCTION_VECTOR, call_function_interrupt),
  100. INTG(CALL_FUNCTION_SINGLE_VECTOR, call_function_single_interrupt),
  101. INTG(IRQ_MOVE_CLEANUP_VECTOR, irq_move_cleanup_interrupt),
  102. INTG(REBOOT_VECTOR, reboot_interrupt),
  103. #endif
  104. #ifdef CONFIG_X86_THERMAL_VECTOR
  105. INTG(THERMAL_APIC_VECTOR, thermal_interrupt),
  106. #endif
  107. #ifdef CONFIG_X86_MCE_THRESHOLD
  108. INTG(THRESHOLD_APIC_VECTOR, threshold_interrupt),
  109. #endif
  110. #ifdef CONFIG_X86_MCE_AMD
  111. INTG(DEFERRED_ERROR_VECTOR, deferred_error_interrupt),
  112. #endif
  113. #ifdef CONFIG_X86_LOCAL_APIC
  114. INTG(LOCAL_TIMER_VECTOR, apic_timer_interrupt),
  115. INTG(X86_PLATFORM_IPI_VECTOR, x86_platform_ipi),
  116. # ifdef CONFIG_HAVE_KVM
  117. INTG(POSTED_INTR_VECTOR, kvm_posted_intr_ipi),
  118. INTG(POSTED_INTR_WAKEUP_VECTOR, kvm_posted_intr_wakeup_ipi),
  119. INTG(POSTED_INTR_NESTED_VECTOR, kvm_posted_intr_nested_ipi),
  120. # endif
  121. # ifdef CONFIG_IRQ_WORK
  122. INTG(IRQ_WORK_VECTOR, irq_work_interrupt),
  123. # endif
  124. #ifdef CONFIG_X86_UV
  125. INTG(UV_BAU_MESSAGE, uv_bau_message_intr1),
  126. #endif
  127. INTG(SPURIOUS_APIC_VECTOR, spurious_interrupt),
  128. INTG(ERROR_APIC_VECTOR, error_interrupt),
  129. #endif
  130. };
  131. #ifdef CONFIG_X86_64
  132. /*
  133. * Early traps running on the DEFAULT_STACK because the other interrupt
  134. * stacks work only after cpu_init().
  135. */
  136. static const __initconst struct idt_data early_pf_idts[] = {
  137. INTG(X86_TRAP_PF, page_fault),
  138. };
  139. /*
  140. * Override for the debug_idt. Same as the default, but with interrupt
  141. * stack set to DEFAULT_STACK (0). Required for NMI trap handling.
  142. */
  143. static const __initconst struct idt_data dbg_idts[] = {
  144. INTG(X86_TRAP_DB, debug),
  145. };
  146. #endif
  147. /* Must be page-aligned because the real IDT is used in a fixmap. */
  148. gate_desc idt_table[IDT_ENTRIES] __page_aligned_bss;
  149. struct desc_ptr idt_descr __ro_after_init = {
  150. .size = (IDT_ENTRIES * 2 * sizeof(unsigned long)) - 1,
  151. .address = (unsigned long) idt_table,
  152. };
  153. #ifdef CONFIG_X86_64
  154. /* No need to be aligned, but done to keep all IDTs defined the same way. */
  155. gate_desc debug_idt_table[IDT_ENTRIES] __page_aligned_bss;
  156. /*
  157. * The exceptions which use Interrupt stacks. They are setup after
  158. * cpu_init() when the TSS has been initialized.
  159. */
  160. static const __initconst struct idt_data ist_idts[] = {
  161. ISTG(X86_TRAP_DB, debug, DEBUG_STACK),
  162. ISTG(X86_TRAP_NMI, nmi, NMI_STACK),
  163. ISTG(X86_TRAP_DF, double_fault, DOUBLEFAULT_STACK),
  164. #ifdef CONFIG_X86_MCE
  165. ISTG(X86_TRAP_MC, &machine_check, MCE_STACK),
  166. #endif
  167. };
  168. /*
  169. * Override for the debug_idt. Same as the default, but with interrupt
  170. * stack set to DEFAULT_STACK (0). Required for NMI trap handling.
  171. */
  172. const struct desc_ptr debug_idt_descr = {
  173. .size = IDT_ENTRIES * 16 - 1,
  174. .address = (unsigned long) debug_idt_table,
  175. };
  176. #endif
  177. static inline void idt_init_desc(gate_desc *gate, const struct idt_data *d)
  178. {
  179. unsigned long addr = (unsigned long) d->addr;
  180. gate->offset_low = (u16) addr;
  181. gate->segment = (u16) d->segment;
  182. gate->bits = d->bits;
  183. gate->offset_middle = (u16) (addr >> 16);
  184. #ifdef CONFIG_X86_64
  185. gate->offset_high = (u32) (addr >> 32);
  186. gate->reserved = 0;
  187. #endif
  188. }
  189. static void
  190. idt_setup_from_table(gate_desc *idt, const struct idt_data *t, int size, bool sys)
  191. {
  192. gate_desc desc;
  193. for (; size > 0; t++, size--) {
  194. idt_init_desc(&desc, t);
  195. write_idt_entry(idt, t->vector, &desc);
  196. if (sys)
  197. set_bit(t->vector, system_vectors);
  198. }
  199. }
  200. static void set_intr_gate(unsigned int n, const void *addr)
  201. {
  202. struct idt_data data;
  203. BUG_ON(n > 0xFF);
  204. memset(&data, 0, sizeof(data));
  205. data.vector = n;
  206. data.addr = addr;
  207. data.segment = __KERNEL_CS;
  208. data.bits.type = GATE_INTERRUPT;
  209. data.bits.p = 1;
  210. idt_setup_from_table(idt_table, &data, 1, false);
  211. }
  212. /**
  213. * idt_setup_early_traps - Initialize the idt table with early traps
  214. *
  215. * On X8664 these traps do not use interrupt stacks as they can't work
  216. * before cpu_init() is invoked and sets up TSS. The IST variants are
  217. * installed after that.
  218. */
  219. void __init idt_setup_early_traps(void)
  220. {
  221. idt_setup_from_table(idt_table, early_idts, ARRAY_SIZE(early_idts),
  222. true);
  223. load_idt(&idt_descr);
  224. }
  225. /**
  226. * idt_setup_traps - Initialize the idt table with default traps
  227. */
  228. void __init idt_setup_traps(void)
  229. {
  230. idt_setup_from_table(idt_table, def_idts, ARRAY_SIZE(def_idts), true);
  231. }
  232. #ifdef CONFIG_X86_64
  233. /**
  234. * idt_setup_early_pf - Initialize the idt table with early pagefault handler
  235. *
  236. * On X8664 this does not use interrupt stacks as they can't work before
  237. * cpu_init() is invoked and sets up TSS. The IST variant is installed
  238. * after that.
  239. *
  240. * FIXME: Why is 32bit and 64bit installing the PF handler at different
  241. * places in the early setup code?
  242. */
  243. void __init idt_setup_early_pf(void)
  244. {
  245. idt_setup_from_table(idt_table, early_pf_idts,
  246. ARRAY_SIZE(early_pf_idts), true);
  247. }
  248. /**
  249. * idt_setup_ist_traps - Initialize the idt table with traps using IST
  250. */
  251. void __init idt_setup_ist_traps(void)
  252. {
  253. idt_setup_from_table(idt_table, ist_idts, ARRAY_SIZE(ist_idts), true);
  254. }
  255. /**
  256. * idt_setup_debugidt_traps - Initialize the debug idt table with debug traps
  257. */
  258. void __init idt_setup_debugidt_traps(void)
  259. {
  260. memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
  261. idt_setup_from_table(debug_idt_table, dbg_idts, ARRAY_SIZE(dbg_idts), false);
  262. }
  263. #endif
  264. /**
  265. * idt_setup_apic_and_irq_gates - Setup APIC/SMP and normal interrupt gates
  266. */
  267. void __init idt_setup_apic_and_irq_gates(void)
  268. {
  269. int i = FIRST_EXTERNAL_VECTOR;
  270. void *entry;
  271. idt_setup_from_table(idt_table, apic_idts, ARRAY_SIZE(apic_idts), true);
  272. for_each_clear_bit_from(i, system_vectors, FIRST_SYSTEM_VECTOR) {
  273. entry = irq_entries_start + 8 * (i - FIRST_EXTERNAL_VECTOR);
  274. set_intr_gate(i, entry);
  275. }
  276. #ifdef CONFIG_X86_LOCAL_APIC
  277. for_each_clear_bit_from(i, system_vectors, NR_VECTORS) {
  278. /*
  279. * Don't set the non assigned system vectors in the
  280. * system_vectors bitmap. Otherwise they show up in
  281. * /proc/interrupts.
  282. */
  283. entry = spurious_entries_start + 8 * (i - FIRST_SYSTEM_VECTOR);
  284. set_intr_gate(i, entry);
  285. }
  286. #endif
  287. }
  288. /**
  289. * idt_setup_early_handler - Initializes the idt table with early handlers
  290. */
  291. void __init idt_setup_early_handler(void)
  292. {
  293. int i;
  294. for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
  295. set_intr_gate(i, early_idt_handler_array[i]);
  296. #ifdef CONFIG_X86_32
  297. for ( ; i < NR_VECTORS; i++)
  298. set_intr_gate(i, early_ignore_irq);
  299. #endif
  300. load_idt(&idt_descr);
  301. }
  302. /**
  303. * idt_invalidate - Invalidate interrupt descriptor table
  304. * @addr: The virtual address of the 'invalid' IDT
  305. */
  306. void idt_invalidate(void *addr)
  307. {
  308. struct desc_ptr idt = { .address = (unsigned long) addr, .size = 0 };
  309. load_idt(&idt);
  310. }
  311. void __init update_intr_gate(unsigned int n, const void *addr)
  312. {
  313. if (WARN_ON_ONCE(!test_bit(n, system_vectors)))
  314. return;
  315. set_intr_gate(n, addr);
  316. }
  317. void alloc_intr_gate(unsigned int n, const void *addr)
  318. {
  319. BUG_ON(n < FIRST_SYSTEM_VECTOR);
  320. if (!test_and_set_bit(n, system_vectors))
  321. set_intr_gate(n, addr);
  322. }