1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringView.h>
#include <Kernel/ACPI/MultiProcessorParser.h>
#include <Kernel/VM/MemoryManager.h>
#include <LibBareMetal/StdLib.h>
namespace Kernel {
static MultiProcessorParser* s_parser;
bool MultiProcessorParser::is_initialized()
{
return s_parser != nullptr;
}
void MultiProcessorParser::initialize()
{
if (!MultiProcessorParser::is_initialized())
s_parser = new MultiProcessorParser;
}
MultiProcessorParser::MultiProcessorParser()
: m_floating_pointer(search_floating_pointer())
, m_operable((m_floating_pointer != (FlatPtr) nullptr))
{
if (m_floating_pointer != (FlatPtr) nullptr) {
klog() << "MultiProcessor: Floating Pointer Structure @ " << PhysicalAddress(m_floating_pointer);
parse_floating_pointer_data();
parse_configuration_table();
} else {
klog() << "MultiProcessor: Can't Locate Floating Pointer Structure, disabled.";
}
}
void MultiProcessorParser::parse_floating_pointer_data()
{
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);
auto* floating_pointer = (MultiProcessor::FloatingPointer*)floating_pointer_region->vaddr().offset(offset_in_page((u32)m_floating_pointer)).as_ptr();
m_configuration_table = floating_pointer->physical_address_ptr;
m_specification_revision = floating_pointer->specification_revision;
dbg() << "Features " << floating_pointer->feature_info[0] << ", IMCR? " << (floating_pointer->feature_info[0] & (1 << 7));
}
size_t MultiProcessorParser::get_configuration_table_length()
{
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);
auto* config_table = (MultiProcessor::ConfigurationTableHeader*)config_table_region->vaddr().offset(offset_in_page((u32)m_configuration_table)).as_ptr();
return config_table->length;
}
void MultiProcessorParser::parse_configuration_table()
{
m_configuration_table_length = get_configuration_table_length();
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);
auto* config_table = (MultiProcessor::ConfigurationTableHeader*)config_table_region->vaddr().offset(offset_in_page((u32)m_configuration_table)).as_ptr();
size_t entry_count = config_table->entry_count;
auto* entry = config_table->entries;
auto* p_entry = reinterpret_cast<MultiProcessor::ConfigurationTableHeader*>(m_configuration_table)->entries;
while (entry_count > 0) {
dbg() << "MultiProcessor: Entry Type " << entry->entry_type << " detected.";
switch (entry->entry_type) {
case ((u8)MultiProcessor::ConfigurationTableEntryType::Processor):
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Processor;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Processor;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus):
m_bus_entries.append((FlatPtr)p_entry);
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::IOAPIC):
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IOAPIC;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IOAPIC;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment):
m_io_interrupt_redirection_entries.append((FlatPtr)p_entry);
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::Local_Interrupt_Assignment):
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Local_Interrupt_Assignment;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Local_Interrupt_Assignment;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::SystemAddressSpaceMapping):
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::SystemAddressSpaceMapping;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::SystemAddressSpaceMapping;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::BusHierarchyDescriptor):
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::BusHierarchyDescriptor;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::BusHierarchyDescriptor;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::CompatibilityBusAddressSpaceModifier):
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::CompatibilityBusAddressSpaceModifier;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::CompatibilityBusAddressSpaceModifier;
break;
ASSERT_NOT_REACHED();
}
entry_count--;
}
}
FlatPtr MultiProcessorParser::search_floating_pointer()
{
FlatPtr mp_floating_pointer = (FlatPtr) nullptr;
auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "MultiProcessor Parser Floating Pointer Structure Finding", Region::Access::Read);
u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e));
klog() << "MultiProcessor: Probing EBDA, Segment 0x" << String::format("%x", ebda_seg);
mp_floating_pointer = search_floating_pointer_in_ebda(ebda_seg);
if (mp_floating_pointer != (FlatPtr) nullptr)
return mp_floating_pointer;
return search_floating_pointer_in_bios_area();
}
FlatPtr MultiProcessorParser::search_floating_pointer_in_ebda(u16 ebda_segment)
{
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);
char* p_floating_pointer_str = (char*)(PhysicalAddress(ebda_segment << 4).as_ptr());
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) {
#ifdef MUTLIPROCESSOR_DEBUG
dbg() << "MultiProcessor: Looking for floating pointer structure in EBDA @ V0x " << String::format("%x", floating_pointer_str) << ", P0x" << String::format("%x", p_floating_pointer_str);
#endif
if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
return (FlatPtr)p_floating_pointer_str;
p_floating_pointer_str += 16;
}
return (FlatPtr) nullptr;
}
FlatPtr MultiProcessorParser::search_floating_pointer_in_bios_area()
{
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);
char* p_floating_pointer_str = (char*)(PhysicalAddress(0xE0000).as_ptr());
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) {
#ifdef MUTLIPROCESSOR_DEBUG
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);
#endif
if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
return (FlatPtr)p_floating_pointer_str;
p_floating_pointer_str += 16;
}
return (FlatPtr) nullptr;
}
Vector<unsigned> MultiProcessorParser::get_pci_bus_ids()
{
Vector<unsigned> pci_bus_ids;
for (auto entry : m_bus_entries) {
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);
auto* v_entry_ptr = (MultiProcessor::BusEntry*)entry_region->vaddr().offset(offset_in_page((u32)entry)).as_ptr();
if (!strncmp("PCI ", v_entry_ptr->bus_type, strlen("PCI ")))
pci_bus_ids.append(v_entry_ptr->bus_id);
}
return pci_bus_ids;
}
MultiProcessorParser& MultiProcessorParser::the()
{
ASSERT(!MultiProcessorParser::is_initialized());
return *s_parser;
}
Vector<RefPtr<PCIInterruptOverrideMetadata>> MultiProcessorParser::get_pci_interrupt_redirections()
{
dbg() << "MultiProcessor: Get PCI IOAPIC redirections";
Vector<RefPtr<PCIInterruptOverrideMetadata>> overrides;
Vector<unsigned> pci_bus_ids = get_pci_bus_ids();
for (auto entry : m_io_interrupt_redirection_entries) {
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);
auto* v_entry_ptr = (MultiProcessor::IOInterruptAssignmentEntry*)entry_region->vaddr().offset(offset_in_page((u32)entry)).as_ptr();
dbg() << "MultiProcessor: Parsing Entry P 0x" << String::format("%x", entry) << ", V " << v_entry_ptr;
for (auto id : pci_bus_ids) {
if (id == v_entry_ptr->source_bus_id) {
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;
overrides.append(adopt(*new PCIInterruptOverrideMetadata(
v_entry_ptr->source_bus_id,
v_entry_ptr->polarity,
v_entry_ptr->trigger_mode,
v_entry_ptr->source_bus_irq,
v_entry_ptr->destination_ioapic_id,
v_entry_ptr->destination_ioapic_intin_pin)));
}
}
}
for (auto override_metadata : overrides) {
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();
}
return overrides;
}
PCIInterruptOverrideMetadata::PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin)
: m_bus_id(bus_id)
, m_polarity(polarity)
, m_trigger_mode(trigger_mode)
, m_pci_interrupt_pin(source_irq & 0b11)
, m_pci_device_number((source_irq & 0b11111) >> 2)
, m_ioapic_id(ioapic_id)
, m_ioapic_interrupt_pin(ioapic_int_pin)
{
}
u8 PCIInterruptOverrideMetadata::bus() const
{
return m_bus_id;
}
u8 PCIInterruptOverrideMetadata::polarity() const
{
return m_polarity;
}
u8 PCIInterruptOverrideMetadata::trigger_mode() const
{
return m_trigger_mode;
}
u8 PCIInterruptOverrideMetadata::pci_interrupt_pin() const
{
return m_pci_interrupt_pin;
}
u8 PCIInterruptOverrideMetadata::pci_device_number() const
{
return m_pci_device_number;
}
u32 PCIInterruptOverrideMetadata::ioapic_id() const
{
return m_ioapic_id;
}
u16 PCIInterruptOverrideMetadata::ioapic_interrupt_pin() const
{
return m_ioapic_interrupt_pin;
}
}
|