MultiProcessorParser.cpp 14 KB

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