idt.c 9.4 KB

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