Parser.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/StringView.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/Firmware/BIOS.h>
  11. #include <Kernel/Firmware/MultiProcessor/Parser.h>
  12. #include <Kernel/Interrupts/IOAPIC.h>
  13. #include <Kernel/Memory/TypedMapping.h>
  14. #include <Kernel/Sections.h>
  15. #include <Kernel/StdLib.h>
  16. namespace Kernel {
  17. UNMAP_AFTER_INIT OwnPtr<MultiProcessorParser> MultiProcessorParser::autodetect()
  18. {
  19. auto floating_pointer = find_floating_pointer();
  20. if (!floating_pointer.has_value())
  21. return {};
  22. auto parser = adopt_own_if_nonnull(new (nothrow) MultiProcessorParser(floating_pointer.value()));
  23. VERIFY(parser != nullptr);
  24. return parser;
  25. }
  26. UNMAP_AFTER_INIT MultiProcessorParser::MultiProcessorParser(PhysicalAddress floating_pointer)
  27. : m_floating_pointer(floating_pointer)
  28. {
  29. dbgln("MultiProcessor: Floating Pointer Structure @ {}", m_floating_pointer);
  30. parse_floating_pointer_data();
  31. parse_configuration_table();
  32. }
  33. UNMAP_AFTER_INIT void MultiProcessorParser::parse_floating_pointer_data()
  34. {
  35. auto floating_pointer = Memory::map_typed<MultiProcessor::FloatingPointer>(m_floating_pointer).release_value_but_fixme_should_propagate_errors();
  36. m_configuration_table = PhysicalAddress(floating_pointer->physical_address_ptr);
  37. dbgln("Features {}, IMCR? {}", floating_pointer->feature_info[0], (floating_pointer->feature_info[0] & (1 << 7)));
  38. }
  39. UNMAP_AFTER_INIT void MultiProcessorParser::parse_configuration_table()
  40. {
  41. auto configuration_table_length = Memory::map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table).release_value_but_fixme_should_propagate_errors()->length;
  42. auto config_table = Memory::map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table, configuration_table_length).release_value_but_fixme_should_propagate_errors();
  43. size_t entry_count = config_table->entry_count;
  44. auto* entry = config_table->entries;
  45. while (entry_count > 0) {
  46. dbgln_if(MULTIPROCESSOR_DEBUG, "MultiProcessor: Entry Type {} detected.", entry->entry_type);
  47. switch (entry->entry_type) {
  48. case ((u8)MultiProcessor::ConfigurationTableEntryType::Processor):
  49. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::ProcessorEntry);
  50. break;
  51. case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus):
  52. MUST(m_bus_entries.try_append(*(const MultiProcessor::BusEntry*)entry));
  53. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::BusEntry);
  54. break;
  55. case ((u8)MultiProcessor::ConfigurationTableEntryType::IOAPIC):
  56. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::IOAPICEntry);
  57. break;
  58. case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment):
  59. MUST(m_io_interrupt_assignment_entries.try_append(*(const MultiProcessor::IOInterruptAssignmentEntry*)entry));
  60. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::IOInterruptAssignmentEntry);
  61. break;
  62. case ((u8)MultiProcessor::ConfigurationTableEntryType::Local_Interrupt_Assignment):
  63. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::LocalInterruptAssignmentEntry);
  64. break;
  65. case ((u8)MultiProcessor::ConfigurationTableEntryType::SystemAddressSpaceMapping):
  66. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::SystemAddressSpaceMappingEntry);
  67. break;
  68. case ((u8)MultiProcessor::ConfigurationTableEntryType::BusHierarchyDescriptor):
  69. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::BusHierarchyDescriptorEntry);
  70. break;
  71. case ((u8)MultiProcessor::ConfigurationTableEntryType::CompatibilityBusAddressSpaceModifier):
  72. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::CompatibilityBusAddressSpaceModifierEntry);
  73. break;
  74. default:
  75. VERIFY_NOT_REACHED();
  76. }
  77. --entry_count;
  78. }
  79. }
  80. UNMAP_AFTER_INIT Optional<PhysicalAddress> MultiProcessorParser::find_floating_pointer()
  81. {
  82. constexpr auto signature = "_MP_"sv;
  83. auto ebda_or_error = map_ebda();
  84. if (!ebda_or_error.is_error()) {
  85. auto mp_floating_pointer = ebda_or_error.value().find_chunk_starting_with(signature, 16);
  86. if (mp_floating_pointer.has_value())
  87. return mp_floating_pointer;
  88. }
  89. auto bios_or_error = map_bios();
  90. if (bios_or_error.is_error())
  91. return {};
  92. return bios_or_error.value().find_chunk_starting_with(signature, 16);
  93. }
  94. UNMAP_AFTER_INIT Vector<u8> MultiProcessorParser::get_pci_bus_ids() const
  95. {
  96. Vector<u8> pci_bus_ids;
  97. for (auto& entry : m_bus_entries) {
  98. if (!strncmp("PCI ", entry.bus_type, strlen("PCI ")))
  99. pci_bus_ids.append(entry.bus_id);
  100. }
  101. return pci_bus_ids;
  102. }
  103. UNMAP_AFTER_INIT Vector<PCIInterruptOverrideMetadata> MultiProcessorParser::get_pci_interrupt_redirections()
  104. {
  105. dbgln("MultiProcessor: Get PCI IOAPIC redirections");
  106. Vector<PCIInterruptOverrideMetadata> overrides;
  107. auto pci_bus_ids = get_pci_bus_ids();
  108. for (auto& entry : m_io_interrupt_assignment_entries) {
  109. for (auto id : pci_bus_ids) {
  110. if (id == entry.source_bus_id) {
  111. dbgln("Interrupts: Bus {}, polarity {}, trigger mode {}, INT {}, IOAPIC {}, IOAPIC INTIN {}",
  112. entry.source_bus_id,
  113. entry.polarity,
  114. entry.trigger_mode,
  115. entry.source_bus_irq,
  116. entry.destination_ioapic_id,
  117. entry.destination_ioapic_intin_pin);
  118. MUST(overrides.try_empend(
  119. entry.source_bus_id,
  120. entry.polarity,
  121. entry.trigger_mode,
  122. entry.source_bus_irq,
  123. entry.destination_ioapic_id,
  124. entry.destination_ioapic_intin_pin));
  125. }
  126. }
  127. }
  128. for (auto& override_metadata : overrides) {
  129. dbgln("Interrupts: Bus {}, polarity {}, PCI device {}, trigger mode {}, INT {}, IOAPIC {}, IOAPIC INTIN {}",
  130. override_metadata.bus(),
  131. override_metadata.polarity(),
  132. override_metadata.pci_device_number(),
  133. override_metadata.trigger_mode(),
  134. override_metadata.pci_interrupt_pin(),
  135. override_metadata.ioapic_id(),
  136. override_metadata.ioapic_interrupt_pin());
  137. }
  138. return overrides;
  139. }
  140. UNMAP_AFTER_INIT PCIInterruptOverrideMetadata::PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin)
  141. : m_bus_id(bus_id)
  142. , m_polarity(polarity)
  143. , m_trigger_mode(trigger_mode)
  144. , m_pci_interrupt_pin(source_irq & 0b11)
  145. , m_pci_device_number((source_irq >> 2) & 0b11111)
  146. , m_ioapic_id(ioapic_id)
  147. , m_ioapic_interrupt_pin(ioapic_int_pin)
  148. {
  149. }
  150. }