InterruptManagement.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/FixedArray.h>
  27. #include <AK/StringView.h>
  28. #include <Kernel/ACPI/MultiProcessorParser.h>
  29. #include <Kernel/Arch/i386/CPU.h>
  30. #include <Kernel/Interrupts/APIC.h>
  31. #include <Kernel/Interrupts/IOAPIC.h>
  32. #include <Kernel/Interrupts/InterruptManagement.h>
  33. #include <Kernel/Interrupts/PIC.h>
  34. #include <Kernel/Interrupts/SpuriousInterruptHandler.h>
  35. #include <Kernel/Interrupts/UnhandledInterruptHandler.h>
  36. #include <Kernel/Syscall.h>
  37. #include <Kernel/VM/MemoryManager.h>
  38. #include <LibBareMetal/IO.h>
  39. #define PCAT_COMPAT_FLAG 0x1
  40. namespace Kernel {
  41. static InterruptManagement* s_interrupt_management;
  42. bool InterruptManagement::initialized()
  43. {
  44. return (s_interrupt_management != nullptr);
  45. }
  46. InterruptManagement& InterruptManagement::the()
  47. {
  48. ASSERT(InterruptManagement::initialized());
  49. return *s_interrupt_management;
  50. }
  51. void InterruptManagement::initialize()
  52. {
  53. ASSERT(!InterruptManagement::initialized());
  54. s_interrupt_management = new InterruptManagement();
  55. }
  56. void InterruptManagement::enumerate_interrupt_handlers(Function<void(GenericInterruptHandler&)> callback)
  57. {
  58. for (int i = 0; i < GENERIC_INTERRUPT_HANDLERS_COUNT; i++) {
  59. auto& handler = get_interrupt_handler(i);
  60. if (handler.type() != HandlerType::UnhandledInterruptHandler)
  61. callback(handler);
  62. }
  63. }
  64. IRQController& InterruptManagement::get_interrupt_controller(int index)
  65. {
  66. ASSERT(index >= 0);
  67. ASSERT(!m_interrupt_controllers[index].is_null());
  68. return *m_interrupt_controllers[index];
  69. }
  70. Vector<RefPtr<ISAInterruptOverrideMetadata>> InterruptManagement::isa_overrides()
  71. {
  72. return m_isa_interrupt_overrides;
  73. }
  74. u8 InterruptManagement::acquire_mapped_interrupt_number(u8 original_irq)
  75. {
  76. if (!InterruptManagement::initialized()) {
  77. // This is necessary, because we install UnhandledInterruptHandlers before we actually initialize the Interrupt Management object...
  78. return original_irq;
  79. }
  80. return InterruptManagement::the().get_mapped_interrupt_vector(original_irq);
  81. }
  82. u8 InterruptManagement::acquire_irq_number(u8 mapped_interrupt_vector)
  83. {
  84. ASSERT(InterruptManagement::initialized());
  85. return InterruptManagement::the().get_irq_vector(mapped_interrupt_vector);
  86. }
  87. u8 InterruptManagement::get_mapped_interrupt_vector(u8 original_irq)
  88. {
  89. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  90. // FIXME: Find a better way to handle conflict with Syscall interrupt gate.
  91. ASSERT((original_irq + IRQ_VECTOR_BASE) != syscall_vector);
  92. return original_irq;
  93. }
  94. u8 InterruptManagement::get_irq_vector(u8 mapped_interrupt_vector)
  95. {
  96. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  97. return mapped_interrupt_vector;
  98. }
  99. RefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 interrupt_vector)
  100. {
  101. if (m_interrupt_controllers.size() == 1 && m_interrupt_controllers[0]->type() == IRQControllerType::i8259) {
  102. return m_interrupt_controllers[0];
  103. }
  104. for (auto irq_controller : m_interrupt_controllers) {
  105. if (irq_controller->gsi_base() <= interrupt_vector)
  106. if (!irq_controller->is_hard_disabled())
  107. return irq_controller;
  108. }
  109. ASSERT_NOT_REACHED();
  110. }
  111. PhysicalAddress InterruptManagement::search_for_madt()
  112. {
  113. dbg() << "Early access to ACPI tables for interrupt setup";
  114. auto rsdp = ACPI::StaticParsing::search_rsdp();
  115. if (rsdp.is_null())
  116. return {};
  117. return ACPI::StaticParsing::search_table(rsdp, "APIC");
  118. }
  119. InterruptManagement::InterruptManagement()
  120. : m_madt(search_for_madt())
  121. {
  122. if (m_madt.is_null()) {
  123. m_interrupt_controllers[0] = adopt(*new PIC());
  124. return;
  125. }
  126. // FIXME: Check what is the actual data size then map accordingly
  127. dbg() << "Interrupts: MADT @ P " << m_madt.as_ptr();
  128. locate_apic_data();
  129. }
  130. void InterruptManagement::switch_to_pic_mode()
  131. {
  132. klog() << "Interrupts: Switch to Legacy PIC mode";
  133. InterruptDisabler disabler;
  134. m_smp_enabled = false;
  135. SpuriousInterruptHandler::initialize(7);
  136. SpuriousInterruptHandler::initialize(15);
  137. for (auto& irq_controller : m_interrupt_controllers) {
  138. ASSERT(irq_controller);
  139. if (irq_controller->type() == IRQControllerType::i82093AA) {
  140. irq_controller->hard_disable();
  141. dbg() << "Interrupts: Detected " << irq_controller->model() << " - Disabled";
  142. } else {
  143. dbg() << "Interrupts: Detected " << irq_controller->model();
  144. }
  145. }
  146. }
  147. void InterruptManagement::switch_to_ioapic_mode()
  148. {
  149. klog() << "Interrupts: Switch to IOAPIC mode";
  150. InterruptDisabler disabler;
  151. m_smp_enabled = true;
  152. if (m_interrupt_controllers.size() == 1) {
  153. if (get_interrupt_controller(0).type() == IRQControllerType::i8259) {
  154. klog() << "Interrupts: NO IOAPIC detected, Reverting to PIC mode.";
  155. return;
  156. }
  157. }
  158. for (auto& irq_controller : m_interrupt_controllers) {
  159. ASSERT(irq_controller);
  160. if (irq_controller->type() == IRQControllerType::i8259) {
  161. irq_controller->hard_disable();
  162. dbg() << "Interrupts: Detected " << irq_controller->model() << " Disabled";
  163. } else {
  164. dbg() << "Interrupts: Detected " << irq_controller->model();
  165. }
  166. }
  167. APIC::init();
  168. APIC::enable_bsp();
  169. MultiProcessorParser::initialize();
  170. }
  171. void InterruptManagement::locate_apic_data()
  172. {
  173. ASSERT(!m_madt.is_null());
  174. auto region = MM.allocate_kernel_region(m_madt.page_base(), (PAGE_SIZE * 2), "Initializing Interrupts", Region::Access::Read);
  175. auto& madt = *(const ACPI::Structures::MADT*)region->vaddr().offset(m_madt.offset_in_page()).as_ptr();
  176. int irq_controller_count = 0;
  177. if (madt.flags & PCAT_COMPAT_FLAG) {
  178. m_interrupt_controllers[0] = adopt(*new PIC());
  179. irq_controller_count++;
  180. }
  181. size_t entry_index = 0;
  182. size_t entries_length = madt.h.length - sizeof(ACPI::Structures::MADT);
  183. auto* madt_entry = madt.entries;
  184. while (entries_length > 0) {
  185. size_t entry_length = madt_entry->length;
  186. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::IOAPIC) {
  187. auto* ioapic_entry = (const ACPI::Structures::MADTEntries::IOAPIC*)madt_entry;
  188. dbg() << "IOAPIC found @ MADT entry " << entry_index << ", MMIO Registers @ Px" << String::format("%x", ioapic_entry->ioapic_address);
  189. m_interrupt_controllers.resize(1 + irq_controller_count);
  190. m_interrupt_controllers[irq_controller_count] = adopt(*new IOAPIC(*(ioapic_mmio_regs*)ioapic_entry->ioapic_address, ioapic_entry->gsi_base));
  191. irq_controller_count++;
  192. }
  193. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::InterruptSourceOverride) {
  194. auto* interrupt_override_entry = (const ACPI::Structures::MADTEntries::InterruptSourceOverride*)madt_entry;
  195. m_isa_interrupt_overrides.append(adopt(*new ISAInterruptOverrideMetadata(
  196. interrupt_override_entry->bus,
  197. interrupt_override_entry->source,
  198. interrupt_override_entry->global_system_interrupt,
  199. interrupt_override_entry->flags)));
  200. dbg() << "Interrupts: Overriding INT 0x" << String::format("%x", interrupt_override_entry->source) << " with GSI " << interrupt_override_entry->global_system_interrupt << ", for bus 0x" << String::format("%x", interrupt_override_entry->bus);
  201. }
  202. madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress((u32)madt_entry).offset(entry_length).get());
  203. entries_length -= entry_length;
  204. entry_index++;
  205. }
  206. }
  207. void InterruptManagement::locate_pci_interrupt_overrides()
  208. {
  209. // FIXME: calling the MultiProcessorParser causes a pagefault.
  210. ASSERT_NOT_REACHED();
  211. m_pci_interrupt_overrides = MultiProcessorParser::the().get_pci_interrupt_redirections();
  212. }
  213. ISAInterruptOverrideMetadata::ISAInterruptOverrideMetadata(u8 bus, u8 source, u32 global_system_interrupt, u16 flags)
  214. : m_bus(bus)
  215. , m_source(source)
  216. , m_global_system_interrupt(global_system_interrupt)
  217. , m_flags(flags)
  218. {
  219. }
  220. u8 ISAInterruptOverrideMetadata::bus() const
  221. {
  222. return m_bus;
  223. }
  224. u8 ISAInterruptOverrideMetadata::source() const
  225. {
  226. return m_source;
  227. }
  228. u32 ISAInterruptOverrideMetadata::gsi() const
  229. {
  230. return m_global_system_interrupt;
  231. }
  232. u16 ISAInterruptOverrideMetadata::flags() const
  233. {
  234. return m_flags;
  235. }
  236. }