MultiProcessorParser.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #pragma once
  27. #include <AK/Types.h>
  28. #include <Kernel/VM/Region.h>
  29. #include <LibBareMetal/Memory/PhysicalAddress.h>
  30. #include <LibBareMetal/Memory/VirtualAddress.h>
  31. namespace Kernel {
  32. namespace MultiProcessor {
  33. struct [[gnu::packed]] FloatingPointer
  34. {
  35. char sig[4];
  36. u32 physical_address_ptr;
  37. u8 length;
  38. u8 specification_revision;
  39. u8 checksum;
  40. u8 feature_info[5];
  41. };
  42. struct [[gnu::packed]] EntryHeader
  43. {
  44. u8 entry_type;
  45. };
  46. struct [[gnu::packed]] ConfigurationTableHeader
  47. {
  48. char sig[4];
  49. u16 length;
  50. u8 specification_revision;
  51. u8 checksum;
  52. char oem_id[8];
  53. char product_id[12];
  54. u32 oem_table_ptr;
  55. u16 oem_table_size;
  56. u16 entry_count;
  57. u32 local_apic_address;
  58. u16 ext_table_length;
  59. u8 ext_table_checksum;
  60. u8 reserved;
  61. EntryHeader entries[];
  62. };
  63. enum class ConfigurationTableEntryType {
  64. Processor = 0,
  65. Bus = 1,
  66. IOAPIC = 2,
  67. IO_Interrupt_Assignment = 3,
  68. Local_Interrupt_Assignment = 4,
  69. SystemAddressSpaceMapping = 128,
  70. BusHierarchyDescriptor = 129,
  71. CompatibilityBusAddressSpaceModifier = 130
  72. };
  73. enum class ConfigurationTableEntryLength {
  74. Processor = 20,
  75. Bus = 8,
  76. IOAPIC = 8,
  77. IO_Interrupt_Assignment = 8,
  78. Local_Interrupt_Assignment = 8,
  79. SystemAddressSpaceMapping = 20,
  80. BusHierarchyDescriptor = 8,
  81. CompatibilityBusAddressSpaceModifier = 8
  82. };
  83. struct [[gnu::packed]] ExtEntryHeader
  84. {
  85. u8 entry_type;
  86. u8 entry_length;
  87. };
  88. struct [[gnu::packed]] ProcessorEntry
  89. {
  90. EntryHeader h;
  91. u8 local_apic_id;
  92. u8 local_apic_version;
  93. u8 cpu_flags;
  94. u32 cpu_signature;
  95. u32 feature_flags;
  96. u8 reserved[8];
  97. };
  98. struct [[gnu::packed]] BusEntry
  99. {
  100. EntryHeader h;
  101. u8 bus_id;
  102. char bus_type[6];
  103. };
  104. struct [[gnu::packed]] IOAPICEntry
  105. {
  106. EntryHeader h;
  107. u8 ioapic_id;
  108. u8 ioapic_version;
  109. u8 ioapic_flags;
  110. u32 ioapic_address;
  111. };
  112. enum class InterruptType {
  113. INT = 0,
  114. NMI = 1,
  115. SMI = 2,
  116. ExtINT = 3,
  117. };
  118. struct [[gnu::packed]] IOInterruptAssignmentEntry
  119. {
  120. EntryHeader h;
  121. u8 interrupt_type;
  122. u8 polarity;
  123. u8 trigger_mode;
  124. u8 source_bus_id;
  125. u8 source_bus_irq;
  126. u8 destination_ioapic_id;
  127. u8 destination_ioapic_intin_pin;
  128. };
  129. struct [[gnu::packed]] LocalInterruptAssignmentEntry
  130. {
  131. EntryHeader h;
  132. u8 interrupt_type;
  133. u8 polarity;
  134. u8 trigger_mode;
  135. u8 source_bus_id;
  136. u8 source_bus_irq;
  137. u8 destination_lapic_id;
  138. u8 destination_lapic_lintin_pin;
  139. };
  140. enum class SystemAddressType {
  141. IO = 0,
  142. Memory = 1,
  143. Prefetch = 2,
  144. };
  145. struct [[gnu::packed]] SystemAddressSpaceMappingEntry
  146. {
  147. ExtEntryHeader h;
  148. u8 bus_id;
  149. u8 address_type;
  150. u64 address_base;
  151. u64 length;
  152. };
  153. struct [[gnu::packed]] BusHierarchyDescriptorEntry
  154. {
  155. ExtEntryHeader h;
  156. u8 bus_id;
  157. u8 bus_info;
  158. u8 parent_bus;
  159. u8 reserved[3];
  160. };
  161. struct [[gnu::packed]] CompatibilityBusAddressSpaceModifierEntry
  162. {
  163. ExtEntryHeader h;
  164. u8 bus_id;
  165. u8 address_modifier;
  166. u32 predefined_range_list;
  167. };
  168. }
  169. class PCIInterruptOverrideMetadata : public RefCounted<PCIInterruptOverrideMetadata> {
  170. public:
  171. PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin);
  172. u8 bus() const;
  173. u8 polarity() const;
  174. u8 trigger_mode() const;
  175. u8 pci_interrupt_pin() const;
  176. u8 pci_device_number() const;
  177. u32 ioapic_id() const;
  178. u16 ioapic_interrupt_pin() const;
  179. private:
  180. u8 m_bus_id;
  181. u8 m_polarity;
  182. u8 m_trigger_mode;
  183. u8 m_pci_interrupt_pin;
  184. u8 m_pci_device_number;
  185. u32 m_ioapic_id;
  186. u16 m_ioapic_interrupt_pin;
  187. };
  188. class MultiProcessorParser {
  189. public:
  190. static MultiProcessorParser& the();
  191. static bool is_initialized();
  192. static void initialize();
  193. Vector<RefPtr<PCIInterruptOverrideMetadata>> get_pci_interrupt_redirections();
  194. protected:
  195. MultiProcessorParser();
  196. void parse_configuration_table();
  197. size_t get_configuration_table_length();
  198. void parse_floating_pointer_data();
  199. Vector<unsigned> get_pci_bus_ids();
  200. uintptr_t search_floating_pointer();
  201. uintptr_t search_floating_pointer_in_ebda(u16 ebda_segment);
  202. uintptr_t search_floating_pointer_in_bios_area();
  203. uintptr_t m_floating_pointer;
  204. uintptr_t m_configuration_table;
  205. Vector<uintptr_t> m_io_interrupt_redirection_entries;
  206. Vector<uintptr_t> m_bus_entries;
  207. bool m_operable;
  208. size_t m_configuration_table_length;
  209. u8 m_specification_revision;
  210. };
  211. }