MultiProcessorParser.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <Kernel/ACPI/MultiProcessorParser.h>
  27. #include <Kernel/VM/MemoryManager.h>
  28. #include <LibBareMetal/StdLib.h>
  29. namespace Kernel {
  30. static MultiProcessorParser* s_parser;
  31. bool MultiProcessorParser::is_initialized()
  32. {
  33. return s_parser != nullptr;
  34. }
  35. void MultiProcessorParser::initialize()
  36. {
  37. if (!MultiProcessorParser::is_initialized())
  38. s_parser = new MultiProcessorParser;
  39. }
  40. MultiProcessorParser::MultiProcessorParser()
  41. : m_floating_pointer(search_floating_pointer())
  42. , m_operable((m_floating_pointer != (FlatPtr) nullptr))
  43. {
  44. if (m_floating_pointer != (FlatPtr) nullptr) {
  45. klog() << "MultiProcessor: Floating Pointer Structure @ " << PhysicalAddress(m_floating_pointer);
  46. parse_floating_pointer_data();
  47. parse_configuration_table();
  48. } else {
  49. klog() << "MultiProcessor: Can't Locate Floating Pointer Structure, disabled.";
  50. }
  51. }
  52. void MultiProcessorParser::parse_floating_pointer_data()
  53. {
  54. auto floating_pointer_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)m_floating_pointer)), PAGE_SIZE * 2, "MultiProcessor Parser Parsing Floating Pointer Structure", Region::Access::Read, false, true);
  55. auto* floating_pointer = (MultiProcessor::FloatingPointer*)floating_pointer_region->vaddr().offset(offset_in_page((u32)m_floating_pointer)).as_ptr();
  56. m_configuration_table = floating_pointer->physical_address_ptr;
  57. m_specification_revision = floating_pointer->specification_revision;
  58. dbg() << "Features " << floating_pointer->feature_info[0] << ", IMCR? " << (floating_pointer->feature_info[0] & (1 << 7));
  59. }
  60. size_t MultiProcessorParser::get_configuration_table_length()
  61. {
  62. auto config_table_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)m_configuration_table)), PAGE_SIZE * 2, "MultiProcessor Parser Getting Configuration Table length", Region::Access::Read, false, true);
  63. auto* config_table = (MultiProcessor::ConfigurationTableHeader*)config_table_region->vaddr().offset(offset_in_page((u32)m_configuration_table)).as_ptr();
  64. return config_table->length;
  65. }
  66. void MultiProcessorParser::parse_configuration_table()
  67. {
  68. m_configuration_table_length = get_configuration_table_length();
  69. auto config_table_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)m_configuration_table)), PAGE_ROUND_UP(m_configuration_table_length), "MultiProcessor Parser Parsing Configuration Table", Region::Access::Read, false, true);
  70. auto* config_table = (MultiProcessor::ConfigurationTableHeader*)config_table_region->vaddr().offset(offset_in_page((u32)m_configuration_table)).as_ptr();
  71. size_t entry_count = config_table->entry_count;
  72. auto* entry = config_table->entries;
  73. auto* p_entry = reinterpret_cast<MultiProcessor::ConfigurationTableHeader*>(m_configuration_table)->entries;
  74. while (entry_count > 0) {
  75. dbg() << "MultiProcessor: Entry Type " << entry->entry_type << " detected.";
  76. switch (entry->entry_type) {
  77. case ((u8)MultiProcessor::ConfigurationTableEntryType::Processor):
  78. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Processor;
  79. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Processor;
  80. break;
  81. case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus):
  82. m_bus_entries.append((FlatPtr)p_entry);
  83. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
  84. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
  85. break;
  86. case ((u8)MultiProcessor::ConfigurationTableEntryType::IOAPIC):
  87. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IOAPIC;
  88. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IOAPIC;
  89. break;
  90. case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment):
  91. m_io_interrupt_redirection_entries.append((FlatPtr)p_entry);
  92. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
  93. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
  94. break;
  95. case ((u8)MultiProcessor::ConfigurationTableEntryType::Local_Interrupt_Assignment):
  96. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Local_Interrupt_Assignment;
  97. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Local_Interrupt_Assignment;
  98. break;
  99. case ((u8)MultiProcessor::ConfigurationTableEntryType::SystemAddressSpaceMapping):
  100. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::SystemAddressSpaceMapping;
  101. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::SystemAddressSpaceMapping;
  102. break;
  103. case ((u8)MultiProcessor::ConfigurationTableEntryType::BusHierarchyDescriptor):
  104. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::BusHierarchyDescriptor;
  105. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::BusHierarchyDescriptor;
  106. break;
  107. case ((u8)MultiProcessor::ConfigurationTableEntryType::CompatibilityBusAddressSpaceModifier):
  108. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::CompatibilityBusAddressSpaceModifier;
  109. p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::CompatibilityBusAddressSpaceModifier;
  110. break;
  111. ASSERT_NOT_REACHED();
  112. }
  113. entry_count--;
  114. }
  115. }
  116. FlatPtr MultiProcessorParser::search_floating_pointer()
  117. {
  118. FlatPtr mp_floating_pointer = (FlatPtr) nullptr;
  119. auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "MultiProcessor Parser Floating Pointer Structure Finding", Region::Access::Read);
  120. u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e));
  121. klog() << "MultiProcessor: Probing EBDA, Segment 0x" << String::format("%x", ebda_seg);
  122. mp_floating_pointer = search_floating_pointer_in_ebda(ebda_seg);
  123. if (mp_floating_pointer != (FlatPtr) nullptr)
  124. return mp_floating_pointer;
  125. return search_floating_pointer_in_bios_area();
  126. }
  127. FlatPtr MultiProcessorParser::search_floating_pointer_in_ebda(u16 ebda_segment)
  128. {
  129. 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);
  130. char* p_floating_pointer_str = (char*)(PhysicalAddress(ebda_segment << 4).as_ptr());
  131. 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) {
  132. #ifdef MUTLIPROCESSOR_DEBUG
  133. dbg() << "MultiProcessor: Looking for floating pointer structure in EBDA @ V0x " << String::format("%x", floating_pointer_str) << ", P0x" << String::format("%x", p_floating_pointer_str);
  134. #endif
  135. if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
  136. return (FlatPtr)p_floating_pointer_str;
  137. p_floating_pointer_str += 16;
  138. }
  139. return (FlatPtr) nullptr;
  140. }
  141. FlatPtr MultiProcessorParser::search_floating_pointer_in_bios_area()
  142. {
  143. 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);
  144. char* p_floating_pointer_str = (char*)(PhysicalAddress(0xE0000).as_ptr());
  145. 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) {
  146. #ifdef MUTLIPROCESSOR_DEBUG
  147. 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);
  148. #endif
  149. if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
  150. return (FlatPtr)p_floating_pointer_str;
  151. p_floating_pointer_str += 16;
  152. }
  153. return (FlatPtr) nullptr;
  154. }
  155. Vector<unsigned> MultiProcessorParser::get_pci_bus_ids()
  156. {
  157. Vector<unsigned> pci_bus_ids;
  158. for (auto entry : m_bus_entries) {
  159. auto entry_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)entry)), PAGE_ROUND_UP(m_configuration_table_length), "MultiProcessor Parser Parsing Bus Entry", Region::Access::Read, false, true);
  160. auto* v_entry_ptr = (MultiProcessor::BusEntry*)entry_region->vaddr().offset(offset_in_page((u32)entry)).as_ptr();
  161. if (!strncmp("PCI ", v_entry_ptr->bus_type, strlen("PCI ")))
  162. pci_bus_ids.append(v_entry_ptr->bus_id);
  163. }
  164. return pci_bus_ids;
  165. }
  166. MultiProcessorParser& MultiProcessorParser::the()
  167. {
  168. ASSERT(!MultiProcessorParser::is_initialized());
  169. return *s_parser;
  170. }
  171. Vector<RefPtr<PCIInterruptOverrideMetadata>> MultiProcessorParser::get_pci_interrupt_redirections()
  172. {
  173. dbg() << "MultiProcessor: Get PCI IOAPIC redirections";
  174. Vector<RefPtr<PCIInterruptOverrideMetadata>> overrides;
  175. Vector<unsigned> pci_bus_ids = get_pci_bus_ids();
  176. for (auto entry : m_io_interrupt_redirection_entries) {
  177. auto entry_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)entry)), PAGE_ROUND_UP(m_configuration_table_length), "MultiProcessor Parser Parsing Bus Entry", Region::Access::Read, false, true);
  178. auto* v_entry_ptr = (MultiProcessor::IOInterruptAssignmentEntry*)entry_region->vaddr().offset(offset_in_page((u32)entry)).as_ptr();
  179. dbg() << "MultiProcessor: Parsing Entry P 0x" << String::format("%x", entry) << ", V " << v_entry_ptr;
  180. for (auto id : pci_bus_ids) {
  181. if (id == v_entry_ptr->source_bus_id) {
  182. klog() << "Interrupts: Bus " << v_entry_ptr->source_bus_id << ", Polarity " << v_entry_ptr->polarity << ", Trigger Mode " << v_entry_ptr->trigger_mode << ", INT " << v_entry_ptr->source_bus_irq << ", IOAPIC " << v_entry_ptr->destination_ioapic_id << ", IOAPIC INTIN " << v_entry_ptr->destination_ioapic_intin_pin;
  183. overrides.append(adopt(*new PCIInterruptOverrideMetadata(
  184. v_entry_ptr->source_bus_id,
  185. v_entry_ptr->polarity,
  186. v_entry_ptr->trigger_mode,
  187. v_entry_ptr->source_bus_irq,
  188. v_entry_ptr->destination_ioapic_id,
  189. v_entry_ptr->destination_ioapic_intin_pin)));
  190. }
  191. }
  192. }
  193. for (auto override_metadata : overrides) {
  194. 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();
  195. }
  196. return overrides;
  197. }
  198. PCIInterruptOverrideMetadata::PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin)
  199. : m_bus_id(bus_id)
  200. , m_polarity(polarity)
  201. , m_trigger_mode(trigger_mode)
  202. , m_pci_interrupt_pin(source_irq & 0b11)
  203. , m_pci_device_number((source_irq & 0b11111) >> 2)
  204. , m_ioapic_id(ioapic_id)
  205. , m_ioapic_interrupt_pin(ioapic_int_pin)
  206. {
  207. }
  208. u8 PCIInterruptOverrideMetadata::bus() const
  209. {
  210. return m_bus_id;
  211. }
  212. u8 PCIInterruptOverrideMetadata::polarity() const
  213. {
  214. return m_polarity;
  215. }
  216. u8 PCIInterruptOverrideMetadata::trigger_mode() const
  217. {
  218. return m_trigger_mode;
  219. }
  220. u8 PCIInterruptOverrideMetadata::pci_interrupt_pin() const
  221. {
  222. return m_pci_interrupt_pin;
  223. }
  224. u8 PCIInterruptOverrideMetadata::pci_device_number() const
  225. {
  226. return m_pci_device_number;
  227. }
  228. u32 PCIInterruptOverrideMetadata::ioapic_id() const
  229. {
  230. return m_ioapic_id;
  231. }
  232. u16 PCIInterruptOverrideMetadata::ioapic_interrupt_pin() const
  233. {
  234. return m_ioapic_interrupt_pin;
  235. }
  236. }