blob: 99c88607a790229e1e71d72ea34a388f282f27ce (
plain)
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
|
#include <kernel/acpi/manager.hpp>
#include <kapi/memory.hpp>
#include <kapi/system.hpp>
#include <acpi/acpi.hpp>
#include <kstd/memory>
#include <kstd/print>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <ranges>
#include <string_view>
namespace kernel::acpi
{
manager::manager(::acpi::rsdp const & sdp)
: m_sdp{&sdp}
{
if (m_sdp->signature() != "RSD PTR ")
{
kapi::system::panic("[OS:ACPI] Invalid RSDP signature!");
}
if (m_sdp->revision() >= 2)
{
auto const xsdp = static_cast<::acpi::xsdp const *>(m_sdp);
if (!xsdp->validate())
{
kapi::system::panic("[OS:ACPI] Invalid XSDP signature!");
}
auto physical_extended_table_address = kapi::memory::physical_address{xsdp->table_address()};
auto linear_extended_table_address = kapi::memory::hhdm_to_linear(physical_extended_table_address);
m_rsdt = static_cast<::acpi::xsdt const *>(linear_extended_table_address);
m_extended = true;
}
else
{
if (!m_sdp->validate())
{
kapi::system::panic("[OS:ACPI] Invalid RSDP checksum!");
}
auto physical_root_table_address = kapi::memory::physical_address{m_sdp->table_address()};
auto linear_root_table_address = kapi::memory::hhdm_to_linear(physical_root_table_address);
m_rsdt = static_cast<::acpi::rsdt const *>(linear_root_table_address);
}
}
auto manager::load_tables() -> bool
{
if (!::acpi::validate_checksum({reinterpret_cast<std::byte const *>(m_rsdt), m_rsdt->length().value}))
{
kapi::system::panic("[OS:ACPI] Invalid RSDT checksum!");
}
auto check_and_register_table = [&](auto table_address) -> void {
auto physical_table_address = kapi::memory::physical_address{reinterpret_cast<std::uintptr_t>(table_address)};
auto mapped_table = kapi::memory::hhdm_to_linear(physical_table_address);
auto table = static_cast<::acpi::table_header const *>(mapped_table);
if (!::acpi::validate_checksum({static_cast<std::byte const *>(mapped_table), table->length().value}))
{
kstd::println(kstd::print_sink::stderr, "[OS:ACPI] Invalid table checksum!");
}
else
{
kstd::println("[OS:ACPI] Found '{}' ACPI table", table->signature());
m_tables.emplace(table->signature(), table);
}
};
if (m_extended)
{
auto xsdt = static_cast<::acpi::xsdt const *>(m_rsdt);
std::ranges::for_each(*xsdt | std::views::transform([](auto const & entry) { return entry.address(); }),
check_and_register_table);
}
else
{
auto rsdt = static_cast<::acpi::rsdt const *>(m_rsdt);
std::ranges::for_each(*rsdt | std::views::transform([](auto const & entry) { return entry.address(); }),
check_and_register_table);
}
return !m_tables.empty();
}
auto manager::get_table(std::string_view signature) -> kstd::observer_ptr<::acpi::table_header const>
{
if (m_tables.contains(signature))
{
return kstd::make_observer(m_tables.at(signature));
}
return nullptr;
}
} // namespace kernel::acpi
|