MultiProcessorParser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/StringView.h>
  28. #include <Kernel/ACPI/MultiProcessorParser.h>
  29. #include <Kernel/Interrupts/IOAPIC.h>
  30. #include <Kernel/StdLib.h>
  31. #include <Kernel/VM/MemoryManager.h>
  32. #include <Kernel/VM/TypedMapping.h>
  33. //#define MULTIPROCESSOR_DEBUG
  34. namespace Kernel {
  35. static MultiProcessorParser* s_parser;
  36. bool MultiProcessorParser::is_initialized()
  37. {
  38. return s_parser != nullptr;
  39. }
  40. void MultiProcessorParser::initialize()
  41. {
  42. ASSERT(!is_initialized());
  43. s_parser = new MultiProcessorParser;
  44. }
  45. MultiProcessorParser::MultiProcessorParser()
  46. : m_floating_pointer(search_floating_pointer())
  47. {
  48. if (!m_floating_pointer.is_null()) {
  49. klog() << "MultiProcessor: Floating Pointer Structure @ " << PhysicalAddress(m_floating_pointer);
  50. parse_floating_pointer_data();
  51. parse_configuration_table();
  52. } else {
  53. klog() << "MultiProcessor: Can't Locate Floating Pointer Structure, disabled.";
  54. }
  55. }
  56. void MultiProcessorParser::parse_floating_pointer_data()
  57. {
  58. auto floating_pointer = map_typed<MultiProcessor::FloatingPointer>(m_floating_pointer);
  59. m_configuration_table = PhysicalAddress(floating_pointer->physical_address_ptr);
  60. dbg() << "Features " << floating_pointer->feature_info[0] << ", IMCR? " << (floating_pointer->feature_info[0] & (1 << 7));
  61. }
  62. void MultiProcessorParser::parse_configuration_table()
  63. {
  64. auto configuration_table_length = map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table)->length;
  65. auto config_table = map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table, configuration_table_length);
  66. size_t entry_count = config_table->entry_count;
  67. auto* entry = config_table->entries;
  68. while (entry_count > 0) {
  69. #ifdef MULTIPROCESSOR_DEBUG
  70. dbg() << "MultiProcessor: Entry Type " << entry->entry_type << " detected.";
  71. #endif
  72. switch (entry->entry_type) {
  73. case ((u8)MultiProcessor::ConfigurationTableEntryType::Processor):
  74. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Processor;
  75. break;
  76. case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus):
  77. m_bus_entries.append(*(const MultiProcessor::BusEntry*)entry);
  78. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
  79. break;
  80. case ((u8)MultiProcessor::ConfigurationTableEntryType::IOAPIC):
  81. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IOAPIC;
  82. break;
  83. case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment):
  84. m_io_interrupt_assignment_entries.append(*(const MultiProcessor::IOInterruptAssignmentEntry*)entry);
  85. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
  86. break;
  87. case ((u8)MultiProcessor::ConfigurationTableEntryType::Local_Interrupt_Assignment):
  88. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Local_Interrupt_Assignment;
  89. break;
  90. case ((u8)MultiProcessor::ConfigurationTableEntryType::SystemAddressSpaceMapping):
  91. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::SystemAddressSpaceMapping;
  92. break;
  93. case ((u8)MultiProcessor::ConfigurationTableEntryType::BusHierarchyDescriptor):
  94. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::BusHierarchyDescriptor;
  95. break;
  96. case ((u8)MultiProcessor::ConfigurationTableEntryType::CompatibilityBusAddressSpaceModifier):
  97. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::CompatibilityBusAddressSpaceModifier;
  98. break;
  99. default:
  100. ASSERT_NOT_REACHED();
  101. }
  102. --entry_count;
  103. }
  104. }
  105. PhysicalAddress MultiProcessorParser::search_floating_pointer()
  106. {
  107. PhysicalAddress mp_floating_pointer;
  108. auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "MultiProcessor Parser Floating Pointer Structure Finding", Region::Access::Read);
  109. u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e));
  110. klog() << "MultiProcessor: Probing EBDA, Segment 0x" << String::format("%x", ebda_seg);
  111. mp_floating_pointer = search_floating_pointer_in_ebda(ebda_seg);
  112. if (!mp_floating_pointer.is_null())
  113. return mp_floating_pointer;
  114. return search_floating_pointer_in_bios_area();
  115. }
  116. PhysicalAddress MultiProcessorParser::search_floating_pointer_in_ebda(u16 ebda_segment)
  117. {
  118. auto floating_pointer_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)(ebda_segment << 4))), PAGE_ROUND_UP(1024), "MultiProcessor Parser floating_pointer Finding #1", Region::Access::Read, false, true);
  119. char* p_floating_pointer_str = (char*)(PhysicalAddress(ebda_segment << 4).as_ptr());
  120. for (char* floating_pointer_str = (char*)floating_pointer_region->vaddr().offset(offset_in_page((u32)(ebda_segment << 4))).as_ptr(); floating_pointer_str < (char*)(floating_pointer_region->vaddr().offset(offset_in_page((u32)(ebda_segment << 4))).get() + 1024); floating_pointer_str += 16) {
  121. #ifdef MULTIPROCESSOR_DEBUG
  122. //dbg() << "MultiProcessor: Looking for floating pointer structure in EBDA @ V0x " << String::format("%x", floating_pointer_str) << ", P0x" << String::format("%x", p_floating_pointer_str);
  123. #endif
  124. if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
  125. return PhysicalAddress((FlatPtr)p_floating_pointer_str);
  126. p_floating_pointer_str += 16;
  127. }
  128. return {};
  129. }
  130. PhysicalAddress MultiProcessorParser::search_floating_pointer_in_bios_area()
  131. {
  132. auto floating_pointer_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)0xE0000)), PAGE_ROUND_UP(0xFFFFF - 0xE0000), "MultiProcessor Parser floating_pointer Finding #2", Region::Access::Read, false, true);
  133. char* p_floating_pointer_str = (char*)(PhysicalAddress(0xE0000).as_ptr());
  134. for (char* floating_pointer_str = (char*)floating_pointer_region->vaddr().offset(offset_in_page((u32)(0xE0000))).as_ptr(); floating_pointer_str < (char*)(floating_pointer_region->vaddr().offset(offset_in_page((u32)(0xE0000))).get() + (0xFFFFF - 0xE0000)); floating_pointer_str += 16) {
  135. #ifdef MULTIPROCESSOR_DEBUG
  136. //dbg() << "MultiProcessor: Looking for floating pointer structure in BIOS area @ V0x " << String::format("%x", floating_pointer_str) << ", P0x" << String::format("%x", p_floating_pointer_str);
  137. #endif
  138. if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
  139. return PhysicalAddress((FlatPtr)p_floating_pointer_str);
  140. p_floating_pointer_str += 16;
  141. }
  142. return {};
  143. }
  144. Vector<u8> MultiProcessorParser::get_pci_bus_ids() const
  145. {
  146. Vector<u8> pci_bus_ids;
  147. for (auto& entry : m_bus_entries) {
  148. if (!strncmp("PCI ", entry.bus_type, strlen("PCI ")))
  149. pci_bus_ids.append(entry.bus_id);
  150. }
  151. return pci_bus_ids;
  152. }
  153. MultiProcessorParser& MultiProcessorParser::the()
  154. {
  155. ASSERT(is_initialized());
  156. return *s_parser;
  157. }
  158. Vector<PCIInterruptOverrideMetadata> MultiProcessorParser::get_pci_interrupt_redirections()
  159. {
  160. dbg() << "MultiProcessor: Get PCI IOAPIC redirections";
  161. Vector<PCIInterruptOverrideMetadata> overrides;
  162. auto pci_bus_ids = get_pci_bus_ids();
  163. for (auto& entry : m_io_interrupt_assignment_entries) {
  164. for (auto id : pci_bus_ids) {
  165. if (id == entry.source_bus_id) {
  166. klog() << "Interrupts: Bus " << entry.source_bus_id << ", Polarity " << entry.polarity << ", Trigger Mode " << entry.trigger_mode << ", INT " << entry.source_bus_irq << ", IOAPIC " << entry.destination_ioapic_id << ", IOAPIC INTIN " << entry.destination_ioapic_intin_pin;
  167. overrides.empend(
  168. entry.source_bus_id,
  169. entry.polarity,
  170. entry.trigger_mode,
  171. entry.source_bus_irq,
  172. entry.destination_ioapic_id,
  173. entry.destination_ioapic_intin_pin);
  174. }
  175. }
  176. }
  177. for (auto& override_metadata : overrides) {
  178. klog() << "Interrupts: Bus " << override_metadata.bus() << ", Polarity " << override_metadata.polarity() << ", PCI Device " << override_metadata.pci_device_number() << ", Trigger Mode " << override_metadata.trigger_mode() << ", INT " << override_metadata.pci_interrupt_pin() << ", IOAPIC " << override_metadata.ioapic_id() << ", IOAPIC INTIN " << override_metadata.ioapic_interrupt_pin();
  179. }
  180. return overrides;
  181. }
  182. PCIInterruptOverrideMetadata::PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin)
  183. : m_bus_id(bus_id)
  184. , m_polarity(polarity)
  185. , m_trigger_mode(trigger_mode)
  186. , m_pci_interrupt_pin(source_irq & 0b11)
  187. , m_pci_device_number((source_irq >> 2) & 0b11111)
  188. , m_ioapic_id(ioapic_id)
  189. , m_ioapic_interrupt_pin(ioapic_int_pin)
  190. {
  191. }
  192. }