aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
Diffstat (limited to 'arch')
-rw-r--r--arch/x86_64/CMakeLists.txt1
-rw-r--r--arch/x86_64/arch/drivers/cpu/lapic.cpp22
-rw-r--r--arch/x86_64/arch/drivers/cpu/lapic.hpp4
-rw-r--r--arch/x86_64/arch/memory/higher_half_mapper.cpp56
-rw-r--r--arch/x86_64/arch/memory/higher_half_mapper.hpp5
-rw-r--r--arch/x86_64/arch/memory/mmu.hpp4
-rw-r--r--arch/x86_64/arch/memory/page_table.hpp34
-rw-r--r--arch/x86_64/kapi/devices/pci_express.cpp39
8 files changed, 127 insertions, 38 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 59df016e..b54fce95 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -19,6 +19,7 @@ target_sources("x86_64" PRIVATE
"kapi/cio.cpp"
"kapi/cpu.cpp"
"kapi/devices.cpp"
+ "kapi/devices/pci_express.cpp"
"kapi/interrupts.cpp"
"kapi/memory.cpp"
"kapi/system.cpp"
diff --git a/arch/x86_64/arch/drivers/cpu/lapic.cpp b/arch/x86_64/arch/drivers/cpu/lapic.cpp
index 5cc62d4c..996b99c5 100644
--- a/arch/x86_64/arch/drivers/cpu/lapic.cpp
+++ b/arch/x86_64/arch/drivers/cpu/lapic.cpp
@@ -118,19 +118,20 @@ namespace arch::drivers::cpu
auto guard = kstd::lock_guard{m_lock};
- if (!m_is_mapped)
+ if (!m_mmio_window)
{
- auto const page_count = (mmio->size + kapi::memory::page_size - 1_B) / kapi::memory::page_size;
- m_mapped_region = kapi::memory::allocate_mmio_region(page_count);
+ auto const mmio_size = mmio->size;
+ auto const mmio_base = kapi::memory::physical_address{mmio->start};
+ auto mmio_window =
+ kapi::memory::mmio_window::map(mmio_base, mmio_size, kapi::memory::mmio_window::access::writable);
- if (!kapi::memory::map_mmio_region(m_mapped_region, kapi::memory::physical_address{mmio->start},
- kapi::memory::page_mapper::flags::writable))
+ if (!mmio_window)
{
kstd::println(kstd::print_sink::stderr, "[ARCH:DRV] LAPIC {} MMIO mapping failed!", signature->hardware_id());
- return kstd::failure(make_error_code(kstd::errc::io_error));
+ return kstd::failure(mmio_window.error());
}
- m_is_mapped = true;
+ m_mmio_window = std::move(*mmio_window);
}
if (signature->is_bsp())
@@ -162,15 +163,12 @@ namespace arch::drivers::cpu
auto lapic::read_register(registers id) const -> std::uint32_t
{
- auto reg =
- static_cast<kstd::observer_ptr<std::uint32_t volatile const>>(m_mapped_region.first + std::to_underlying(id));
- return *reg;
+ return m_mmio_window->read<std::uint32_t>(kstd::bytes(std::to_underlying(id)));
}
auto lapic::write_register(registers id, std::uint32_t value) -> void
{
- auto reg = static_cast<kstd::observer_ptr<std::uint32_t volatile>>(m_mapped_region.first + std::to_underlying(id));
- *reg = value;
+ m_mmio_window->write<std::uint32_t>(kstd::bytes(std::to_underlying(id)), value);
}
auto lapic::query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void>
diff --git a/arch/x86_64/arch/drivers/cpu/lapic.hpp b/arch/x86_64/arch/drivers/cpu/lapic.hpp
index 52533d9a..e0b3bb71 100644
--- a/arch/x86_64/arch/drivers/cpu/lapic.hpp
+++ b/arch/x86_64/arch/drivers/cpu/lapic.hpp
@@ -13,6 +13,7 @@
#include <cstddef>
#include <cstdint>
+#include <optional>
#include <string_view>
namespace arch::drivers::cpu
@@ -37,8 +38,7 @@ namespace arch::drivers::cpu
auto write_register(registers id, std::uint32_t value) -> void;
mutable kapi::tracked_mutex m_lock{};
- kapi::memory::mmio_region m_mapped_region{};
- bool m_is_mapped{};
+ std::optional<kapi::memory::mmio_window> m_mmio_window{};
};
} // namespace arch::drivers::cpu
diff --git a/arch/x86_64/arch/memory/higher_half_mapper.cpp b/arch/x86_64/arch/memory/higher_half_mapper.cpp
index 8a069c86..cd0f943a 100644
--- a/arch/x86_64/arch/memory/higher_half_mapper.cpp
+++ b/arch/x86_64/arch/memory/higher_half_mapper.cpp
@@ -1,5 +1,6 @@
#include <arch/memory/higher_half_mapper.hpp>
+#include <arch/memory/mmu.hpp>
#include <arch/memory/page_table.hpp>
#include <arch/memory/page_utilities.hpp>
@@ -8,10 +9,10 @@
#include <kstd/memory.hpp>
-#include <algorithm>
#include <array>
#include <cstddef>
#include <memory>
+#include <optional>
#include <ranges>
#include <utility>
@@ -44,15 +45,17 @@ namespace arch::memory
return static_cast<kstd::observer_ptr<std::byte>>(page.start_address());
}
- auto higher_half_mapper::unmap(kapi::memory::page page) -> void
+ auto higher_half_mapper::unmap(kapi::memory::page page) -> kapi::memory::frame
{
- if (!try_unmap(page))
+ auto frame = try_unmap(page);
+ if (!frame)
{
kapi::system::panic("[ARCH:MEM] Tried to unmap a page that is not mapped!");
}
+ return *frame;
}
- auto higher_half_mapper::try_unmap(kapi::memory::page page) noexcept -> bool
+ auto higher_half_mapper::try_unmap(kapi::memory::page page) noexcept -> std::optional<kapi::memory::frame>
{
auto table_path = std::array<std::pair<kstd::observer_ptr<page_table>, std::size_t>, 4>{};
table_path[0] = std::pair{m_root, pml_index(4, page)};
@@ -64,15 +67,33 @@ namespace arch::memory
if (!entry.present())
{
- return false;
+ return std::nullopt;
+ }
+
+ if (entry.huge())
+ {
+ kapi::system::panic("[ARCH:MEM] Tried to unmap a huge page, which is not supported!");
}
auto next_table = to_higher_half_pointer<page_table>(entry.frame()->start_address());
- auto next_index = pml_index(4 - level - 1, page);
- table_path[4 - level - 1] = std::pair{next_table, next_index};
+ auto next_index = pml_index(level - 1, page);
+ table_path[4 - (level - 1)] = std::pair{next_table, next_index};
+ }
+
+ auto [leaf_table, leaf_index] = table_path.back();
+ if (!(*leaf_table)[leaf_index].present())
+ {
+ return std::nullopt;
}
- std::ranges::for_each(std::views::reverse(table_path), [previous_was_empty = true](auto & step) mutable {
+ // If execution reaches this point, all page table entries leading to, and including, the leaf entry are present. It
+ // is therefore safe to access their `frame` value without additional checks.
+ auto release_path = std::views::reverse(table_path);
+ auto previous_was_empty = true;
+ auto unmapped_frame = std::optional<kapi::memory::frame>{};
+
+ for (auto & step : release_path)
+ {
auto [table, index] = step;
auto & entry = (*table)[index];
auto frame = entry.frame();
@@ -81,11 +102,19 @@ namespace arch::memory
{
entry.clear();
previous_was_empty = table->empty();
- kapi::memory::get_frame_allocator().release(*frame);
+ if (!unmapped_frame)
+ {
+ unmapped_frame = *frame;
+ }
+ else
+ {
+ kapi::memory::get_frame_allocator().release(*frame);
+ }
}
- });
+ }
- return true;
+ tlb_flush(page.start_address());
+ return unmapped_frame;
}
auto higher_half_mapper::get_or_create_page_table(kapi::memory::page page) noexcept -> kstd::observer_ptr<page_table>
@@ -97,6 +126,11 @@ namespace arch::memory
auto index = pml_index(level, page);
auto & entry = (*table)[index];
+ if (entry.huge())
+ {
+ kapi::system::panic("[ARCH:MEM] Tried map an entry in a huge page, which is not supported!");
+ }
+
if (!entry.present())
{
auto table_frame = kapi::memory::allocate_frame();
diff --git a/arch/x86_64/arch/memory/higher_half_mapper.hpp b/arch/x86_64/arch/memory/higher_half_mapper.hpp
index 45ff100a..f46e6f3c 100644
--- a/arch/x86_64/arch/memory/higher_half_mapper.hpp
+++ b/arch/x86_64/arch/memory/higher_half_mapper.hpp
@@ -8,6 +8,7 @@
#include <kstd/memory.hpp>
#include <cstddef>
+#include <optional>
namespace arch::memory
{
@@ -24,10 +25,10 @@ namespace arch::memory
auto map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> kstd::observer_ptr<std::byte> override;
//! @copydoc kapi::memory::page_mapper::unmap
- auto unmap(kapi::memory::page page) -> void override;
+ auto unmap(kapi::memory::page page) -> kapi::memory::frame override;
//! @copydoc kapi::memory::page_mapper::try_unmap
- auto try_unmap(kapi::memory::page page) noexcept -> bool override;
+ auto try_unmap(kapi::memory::page page) noexcept -> std::optional<kapi::memory::frame> override;
private:
//! Try to retrieve the the PML1 responsible for mapping this page, creating one if necessary.
diff --git a/arch/x86_64/arch/memory/mmu.hpp b/arch/x86_64/arch/memory/mmu.hpp
index 9bd95b43..f824ba53 100644
--- a/arch/x86_64/arch/memory/mmu.hpp
+++ b/arch/x86_64/arch/memory/mmu.hpp
@@ -7,10 +7,12 @@ namespace arch::memory
{
//! Invalidate a translation lookaside buffer (TLB) entry for a given address
//!
- //! @param address The physical address for which to flush the TLB entries
+ //! @param address The linear address for which to flush the TLB entries
auto tlb_flush(kapi::memory::linear_address address) -> void;
//! Invalidate all translation lookaside buffer (TLB) entries
+ //!
+ //! @note Once CR4.PGE is set, this function will no longer flush TLB entries for global pages.
auto tlb_flush_all() -> void;
} // namespace arch::memory
diff --git a/arch/x86_64/arch/memory/page_table.hpp b/arch/x86_64/arch/memory/page_table.hpp
index 0c0f1a77..c34f72e0 100644
--- a/arch/x86_64/arch/memory/page_table.hpp
+++ b/arch/x86_64/arch/memory/page_table.hpp
@@ -40,16 +40,30 @@ namespace arch::memory
enum struct flags : std::uint64_t
{
empty = 0,
- present = 1uz << 0, //!< The page is mapped.
- writable = 1uz << 1, //!< The page is writable.
- user_accessible = 1uz << 2, //!< The page is accessible in user mode.
- write_through = 1uz << 3, //!< Any writes to the page must immediately hit memory.
- disable_cache = 1uz << 4, //!< Any writes to the page must never be cached.
- accessed = 1uz << 5, //!< The page was accessed.
- dirty = 1uz << 6, //!< The page was written to.
- huge_page = 1uz << 7, //!< The page is huge.
- global = 1uz << 8, //!< The TLB entry for this page must not be flushed on context switches.
- no_execute = 1uz << 63, //!< The data in this page must not be executed.
+ //! The page is mapped.
+ present = 1uz << 0,
+ //! The page is writable.
+ writable = 1uz << 1,
+ //! The page is accessible in user mode.
+ user_accessible = 1uz << 2,
+ //! Any writes to the page must immediately hit memory.
+ write_through = 1uz << 3,
+ //! Any writes to the page must never be cached.
+ disable_cache = 1uz << 4,
+ //! The page was accessed.
+ accessed = 1uz << 5,
+ //! The page was written to.
+ dirty = 1uz << 6,
+ //! The page is huge.
+ huge_page = 1uz << 7,
+ //! The TLB entry for this page must not be flushed on context switches.
+ //!
+ //! @note This flag is currently not honored by this implementation, since the CR4.PGE bit is not set. As a
+ //! consequence, any CR3 reload or a call to arch::memory::tlb_flush_all will still flush this entry on context
+ //! switches.
+ global = 1uz << 8,
+ //! The data in this page must not be executed.
+ no_execute = 1uz << 63,
};
//! Construct an empty entry.
diff --git a/arch/x86_64/kapi/devices/pci_express.cpp b/arch/x86_64/kapi/devices/pci_express.cpp
new file mode 100644
index 00000000..a88931b9
--- /dev/null
+++ b/arch/x86_64/kapi/devices/pci_express.cpp
@@ -0,0 +1,39 @@
+#include <kapi/devices/pci_express.hpp>
+
+#include <kapi/acpi.hpp>
+#include <kapi/memory.hpp>
+
+#include <acpi/acpi.hpp>
+
+#include <kstd/ranges.hpp>
+#include <kstd/vector.hpp>
+
+#include <cstddef>
+#include <cstdint>
+#include <ranges>
+
+namespace kapi::devices
+{
+
+ auto get_pci_express_segment_descriptors() -> kstd::vector<pcie_segment_descriptor>
+ {
+ auto mcfg_table = kapi::acpi::get_table<::acpi::table_signature_v<::acpi::mcfg>>();
+ if (!mcfg_table)
+ {
+ return {};
+ }
+
+ return kstd::vector{kstd::from_range, *mcfg_table | std::views::transform([](auto & entry) {
+ auto base_address = memory::physical_address{entry.base_address()};
+ auto bus_count =
+ static_cast<std::size_t>(entry.end_bus_number() - entry.start_bus_number() + 1);
+ auto group_number = static_cast<std::uint16_t>(entry.segment_group_number());
+
+ return pcie_segment_descriptor{.base_address = base_address,
+ .group_number = group_number,
+ .first_bus = entry.start_bus_number(),
+ .bus_count = bus_count};
+ })};
+ }
+
+} // namespace kapi::devices \ No newline at end of file