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