aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
Diffstat (limited to 'arch')
-rw-r--r--arch/x86_64/arch/boot/boot.hpp4
-rw-r--r--arch/x86_64/arch/cpu/global_descriptor_table.hpp2
-rw-r--r--arch/x86_64/arch/cpu/initialization.cpp6
-rw-r--r--arch/x86_64/arch/cpu/interrupts.cpp14
-rw-r--r--arch/x86_64/arch/devices/init.cpp10
-rw-r--r--arch/x86_64/arch/drivers/cpu/lapic.cpp5
-rw-r--r--arch/x86_64/arch/memory/higher_half_mapper.cpp15
-rw-r--r--arch/x86_64/arch/memory/higher_half_mapper.hpp10
-rw-r--r--arch/x86_64/arch/memory/kernel_mapper.cpp3
-rw-r--r--arch/x86_64/arch/memory/kernel_mapper.hpp6
-rw-r--r--arch/x86_64/arch/memory/page_utilities.hpp7
-rw-r--r--arch/x86_64/arch/memory/region_allocator.hpp7
-rw-r--r--arch/x86_64/arch/vga/text/buffer.cpp6
-rw-r--r--arch/x86_64/arch/vga/text/buffer.hpp4
-rw-r--r--arch/x86_64/arch/vga/text/device.cpp10
-rw-r--r--arch/x86_64/kapi/memory.cpp12
16 files changed, 76 insertions, 45 deletions
diff --git a/arch/x86_64/arch/boot/boot.hpp b/arch/x86_64/arch/boot/boot.hpp
index c790152c..566d1486 100644
--- a/arch/x86_64/arch/boot/boot.hpp
+++ b/arch/x86_64/arch/boot/boot.hpp
@@ -33,6 +33,8 @@
#include <kapi/boot.hpp> // IWYU pragma: export
+#include <kstd/memory.hpp>
+
#include <multiboot2/information.hpp>
#include <cstddef>
@@ -43,7 +45,7 @@ namespace kapi::boot
struct information
{
//! A pointer to the loader provided Multiboot2 Information structure.
- multiboot2::information_view const * mbi;
+ kstd::observer_ptr<multiboot2::information_view const> mbi;
//! The index of the next character to be written in the VGA text buffer after handoff.
std::size_t vga_buffer_index;
diff --git a/arch/x86_64/arch/cpu/global_descriptor_table.hpp b/arch/x86_64/arch/cpu/global_descriptor_table.hpp
index 0bd7677d..34aac3e0 100644
--- a/arch/x86_64/arch/cpu/global_descriptor_table.hpp
+++ b/arch/x86_64/arch/cpu/global_descriptor_table.hpp
@@ -46,7 +46,7 @@ namespace arch::cpu
: m_descriptors{}
{
auto descriptor_data = std::array{
- std::pair{std::bit_cast<std::byte const *>(&descriptors), sizeof(descriptors)}
+ std::pair{reinterpret_cast<std::byte const *>(&descriptors), sizeof(descriptors)}
...
};
auto written_size = 0uz;
diff --git a/arch/x86_64/arch/cpu/initialization.cpp b/arch/x86_64/arch/cpu/initialization.cpp
index be1cc9f8..cae40d38 100644
--- a/arch/x86_64/arch/cpu/initialization.cpp
+++ b/arch/x86_64/arch/cpu/initialization.cpp
@@ -88,9 +88,9 @@ namespace arch::cpu
.base_high = 0,
};
- constexpr auto make_tss_descriptor(task_state_segment const * tss_ptr) -> system_segment_descriptor
+ constexpr auto make_tss_descriptor(task_state_segment const & tss) -> system_segment_descriptor
{
- auto const address = std::bit_cast<std::uintptr_t>(tss_ptr);
+ auto const address = std::bit_cast<std::uintptr_t>(&tss);
auto const limit = sizeof(task_state_segment) - 1;
return system_segment_descriptor{
@@ -134,7 +134,7 @@ namespace arch::cpu
auto initialize_descriptors() -> void
{
- auto static tss_descriptor = make_tss_descriptor(&tss);
+ auto static tss_descriptor = make_tss_descriptor(tss);
auto static gdt = global_descriptor_table{
gdt_null_descriptor, gdt_kernel_code_descriptor, gdt_kernel_data_descriptor,
diff --git a/arch/x86_64/arch/cpu/interrupts.cpp b/arch/x86_64/arch/cpu/interrupts.cpp
index f27c3a2e..2b166306 100644
--- a/arch/x86_64/arch/cpu/interrupts.cpp
+++ b/arch/x86_64/arch/cpu/interrupts.cpp
@@ -8,6 +8,7 @@
#include <kapi/memory.hpp>
#include <kapi/system.hpp>
+#include <kstd/memory.hpp>
#include <kstd/print.hpp>
#include <kstd/units.hpp>
@@ -100,7 +101,7 @@ namespace arch::cpu
}
}
- auto dispatch_exception(interrupt_frame * frame) -> bool
+ auto dispatch_exception(kstd::observer_ptr<interrupt_frame const> frame) -> bool
{
auto type = to_exception_type(static_cast<exception>(frame->interrupt.number));
auto fault_address = kapi::memory::linear_address{};
@@ -122,7 +123,7 @@ namespace arch::cpu
}
}
- auto acknowledge_pic_interrupt(interrupt_frame * frame) -> void
+ auto acknowledge_pic_interrupt(kstd::observer_ptr<interrupt_frame const> frame) -> void
{
if (frame->interrupt.number >= pic_slave_irq_start)
{
@@ -131,7 +132,7 @@ namespace arch::cpu
pic_master_control_port::write(pic_end_of_interrupt);
}
- auto handle_double_fault(interrupt_frame * frame) -> void
+ auto handle_double_fault(kstd::observer_ptr<interrupt_frame const> frame) -> void
{
auto const rsp = frame->cpu_saved.rsp;
auto const rip = frame->cpu_saved.rip;
@@ -162,17 +163,18 @@ namespace arch::cpu
{
extern std::uintptr_t const isr_stub_table[256];
+ // Note: this function receives a plain pointer because it is called from the assembly ISR stub.
auto interrupt_dispatch(interrupt_frame * frame) -> void
{
auto [number, code] = frame->interrupt;
if (number == static_cast<std::uint64_t>(exception::double_fault))
{
- handle_double_fault(frame);
+ handle_double_fault(kstd::make_observer(frame));
}
else if (number < number_of_exception_vectors)
{
- if (!dispatch_exception(frame))
+ if (!dispatch_exception(kstd::make_observer(frame)))
{
if (has_error_code(static_cast<exception>(number)))
{
@@ -194,7 +196,7 @@ namespace arch::cpu
kstd::println(kstd::print_sink::stderr, "[ARCH:CPU] Unhandled interrupt {:#04x} (IRQ{})", number, irq_number);
}
- acknowledge_pic_interrupt(frame);
+ acknowledge_pic_interrupt(kstd::make_observer(frame));
}
}
}
diff --git a/arch/x86_64/arch/devices/init.cpp b/arch/x86_64/arch/devices/init.cpp
index 639e8870..830c6d0a 100644
--- a/arch/x86_64/arch/devices/init.cpp
+++ b/arch/x86_64/arch/devices/init.cpp
@@ -12,6 +12,8 @@
#include <kstd/memory.hpp>
#include <kstd/print.hpp>
+#include <cstddef>
+
namespace arch::devices
{
@@ -21,21 +23,21 @@ namespace arch::devices
auto get_acpi_root_pointer() -> kstd::observer_ptr<::acpi::rsdp const>
{
auto const & mbi = kapi::boot::bootstrap_information.mbi;
- auto system_description_pointer = static_cast<::acpi::rsdp const *>(nullptr);
+ auto system_description_pointer = kstd::observer_ptr<std::byte const>(nullptr);
if (auto const & xsdp = mbi->maybe_acpi_xsdp())
{
auto data = xsdp->pointer().data();
- system_description_pointer = reinterpret_cast<::acpi::xsdp const *>(data);
+ system_description_pointer = kstd::make_observer(data);
}
else if (auto const & rsdp = mbi->maybe_acpi_rsdp())
{
auto data = rsdp->pointer().data();
- system_description_pointer = reinterpret_cast<::acpi::rsdp const *>(data);
+ system_description_pointer = kstd::make_observer(data);
}
- return kstd::make_observer(system_description_pointer);
+ return kstd::observer_ptr(reinterpret_cast<::acpi::rsdp const *>(system_description_pointer.get()));
}
} // namespace
diff --git a/arch/x86_64/arch/drivers/cpu/lapic.cpp b/arch/x86_64/arch/drivers/cpu/lapic.cpp
index 3b7c2d45..5cc62d4c 100644
--- a/arch/x86_64/arch/drivers/cpu/lapic.cpp
+++ b/arch/x86_64/arch/drivers/cpu/lapic.cpp
@@ -162,13 +162,14 @@ namespace arch::drivers::cpu
auto lapic::read_register(registers id) const -> std::uint32_t
{
- auto reg = static_cast<std::uint32_t volatile *>(m_mapped_region.first + std::to_underlying(id));
+ auto reg =
+ static_cast<kstd::observer_ptr<std::uint32_t volatile const>>(m_mapped_region.first + std::to_underlying(id));
return *reg;
}
auto lapic::write_register(registers id, std::uint32_t value) -> void
{
- auto reg = static_cast<std::uint32_t volatile *>(m_mapped_region.first + std::to_underlying(id));
+ auto reg = static_cast<kstd::observer_ptr<std::uint32_t volatile>>(m_mapped_region.first + std::to_underlying(id));
*reg = value;
}
diff --git a/arch/x86_64/arch/memory/higher_half_mapper.cpp b/arch/x86_64/arch/memory/higher_half_mapper.cpp
index 974b00a0..8a069c86 100644
--- a/arch/x86_64/arch/memory/higher_half_mapper.cpp
+++ b/arch/x86_64/arch/memory/higher_half_mapper.cpp
@@ -6,6 +6,8 @@
#include <kapi/memory.hpp>
#include <kapi/system.hpp>
+#include <kstd/memory.hpp>
+
#include <algorithm>
#include <array>
#include <cstddef>
@@ -16,11 +18,12 @@
namespace arch::memory
{
- higher_half_mapper::higher_half_mapper(page_table * root)
+ higher_half_mapper::higher_half_mapper(kstd::observer_ptr<page_table> root)
: m_root{root}
{}
- auto higher_half_mapper::map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> std::byte *
+ auto higher_half_mapper::map(kapi::memory::page page, kapi::memory::frame frame, flags flags)
+ -> kstd::observer_ptr<std::byte>
{
auto table = get_or_create_page_table(page);
if (!table)
@@ -38,7 +41,7 @@ namespace arch::memory
entry.frame(frame, to_table_flags(flags) | page_table::entry::flags::present);
- return static_cast<std::byte *>(page.start_address());
+ return static_cast<kstd::observer_ptr<std::byte>>(page.start_address());
}
auto higher_half_mapper::unmap(kapi::memory::page page) -> void
@@ -51,7 +54,7 @@ namespace arch::memory
auto higher_half_mapper::try_unmap(kapi::memory::page page) noexcept -> bool
{
- auto table_path = std::array<std::pair<page_table *, std::size_t>, 4>{};
+ 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)};
for (auto level = 4uz; level > 1uz; --level)
@@ -85,7 +88,7 @@ namespace arch::memory
return true;
}
- auto higher_half_mapper::get_or_create_page_table(kapi::memory::page page) noexcept -> page_table *
+ auto higher_half_mapper::get_or_create_page_table(kapi::memory::page page) noexcept -> kstd::observer_ptr<page_table>
{
auto table = m_root;
@@ -103,7 +106,7 @@ namespace arch::memory
}
auto new_table = to_higher_half_pointer<page_table>(table_frame->start_address());
- std::construct_at(new_table);
+ std::construct_at(new_table.get());
auto const flags = page_table::entry::flags::present | page_table::entry::flags::writable |
page_table::entry::flags::user_accessible;
diff --git a/arch/x86_64/arch/memory/higher_half_mapper.hpp b/arch/x86_64/arch/memory/higher_half_mapper.hpp
index 4f6574ff..45ff100a 100644
--- a/arch/x86_64/arch/memory/higher_half_mapper.hpp
+++ b/arch/x86_64/arch/memory/higher_half_mapper.hpp
@@ -5,6 +5,8 @@
#include <kapi/memory.hpp>
+#include <kstd/memory.hpp>
+
#include <cstddef>
namespace arch::memory
@@ -16,10 +18,10 @@ namespace arch::memory
//! Construct a new mapper for a hierarchy rooted in the given PML.
//!
//! @param root The root of the hierarchy to operate on.
- explicit higher_half_mapper(page_table * root);
+ explicit higher_half_mapper(kstd::observer_ptr<page_table> root);
//! @copydoc kapi::memory::page_mapper::map
- auto map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> std::byte * override;
+ 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;
@@ -34,9 +36,9 @@ namespace arch::memory
//!
//! @param page The page to get the PML1 for.
//! @return The PML1 that manages the given page, nullptr it the system runs out of memory.
- auto get_or_create_page_table(kapi::memory::page page) noexcept -> page_table *;
+ auto get_or_create_page_table(kapi::memory::page page) noexcept -> kstd::observer_ptr<page_table>;
- page_table * m_root;
+ kstd::observer_ptr<page_table> m_root;
};
} // namespace arch::memory
diff --git a/arch/x86_64/arch/memory/kernel_mapper.cpp b/arch/x86_64/arch/memory/kernel_mapper.cpp
index 070a7866..7d9d9bb5 100644
--- a/arch/x86_64/arch/memory/kernel_mapper.cpp
+++ b/arch/x86_64/arch/memory/kernel_mapper.cpp
@@ -8,6 +8,7 @@
#include <elf/format.hpp>
#include <elf/section_header.hpp>
+#include <kstd/memory.hpp>
#include <kstd/print.hpp>
#include <kstd/units.hpp>
@@ -40,7 +41,7 @@ namespace arch::memory
} // namespace
- kernel_mapper::kernel_mapper(multiboot2::information_view const * mbi)
+ kernel_mapper::kernel_mapper(kstd::observer_ptr<multiboot2::information_view const> mbi)
: m_mbi{std::move(mbi)}
, m_kernel_load_base{std::bit_cast<std::uintptr_t>(&arch::boot::TEACHOS_VMA)}
{}
diff --git a/arch/x86_64/arch/memory/kernel_mapper.hpp b/arch/x86_64/arch/memory/kernel_mapper.hpp
index aeb936dc..2adf8fd9 100644
--- a/arch/x86_64/arch/memory/kernel_mapper.hpp
+++ b/arch/x86_64/arch/memory/kernel_mapper.hpp
@@ -6,6 +6,8 @@
#include <elf/format.hpp>
#include <elf/section_header.hpp>
+#include <kstd/memory.hpp>
+
#include <multiboot2/information.hpp>
#include <cstdint>
@@ -18,7 +20,7 @@ namespace arch::memory
{
using section_header_type = elf::section_header<elf::format::elf64>;
- explicit kernel_mapper(multiboot2::information_view const * mbi);
+ explicit kernel_mapper(kstd::observer_ptr<multiboot2::information_view const> mbi);
auto remap_kernel(kapi::memory::page_mapper & mapper) -> void;
@@ -26,7 +28,7 @@ namespace arch::memory
auto map_section(section_header_type const & section, std::string_view name, kapi::memory::page_mapper & mapper)
-> void;
- multiboot2::information_view const * m_mbi;
+ kstd::observer_ptr<multiboot2::information_view const> m_mbi;
std::uintptr_t m_kernel_load_base;
};
diff --git a/arch/x86_64/arch/memory/page_utilities.hpp b/arch/x86_64/arch/memory/page_utilities.hpp
index 478f776d..cf334cf8 100644
--- a/arch/x86_64/arch/memory/page_utilities.hpp
+++ b/arch/x86_64/arch/memory/page_utilities.hpp
@@ -3,6 +3,8 @@
#include <kapi/memory.hpp>
+#include <kstd/memory.hpp>
+
#include <cstddef>
namespace arch::memory
@@ -17,11 +19,12 @@ namespace arch::memory
}
template<typename ValueType = void>
- [[nodiscard]] constexpr auto to_higher_half_pointer(kapi::memory::physical_address address) -> ValueType *
+ [[nodiscard]] constexpr auto to_higher_half_pointer(kapi::memory::physical_address address)
+ -> kstd::observer_ptr<ValueType>
{
using namespace kapi::memory;
auto const higher_half_address = higher_half_direct_map_base + address.raw();
- return static_cast<ValueType *>(higher_half_address);
+ return static_cast<kstd::observer_ptr<ValueType>>(higher_half_address);
}
} // namespace arch::memory
diff --git a/arch/x86_64/arch/memory/region_allocator.hpp b/arch/x86_64/arch/memory/region_allocator.hpp
index 61cc1d4d..be3c32bb 100644
--- a/arch/x86_64/arch/memory/region_allocator.hpp
+++ b/arch/x86_64/arch/memory/region_allocator.hpp
@@ -5,6 +5,8 @@
#include <kapi/memory/frame.hpp>
#include <kapi/memory/frame_allocator.hpp>
+#include <kstd/memory.hpp>
+
#include <multiboot2/information.hpp>
#include <cstddef>
@@ -46,7 +48,7 @@ namespace arch::memory
//! The loader supplied Multiboot2 information structure.
//!
//! This is used to query boot module ranges so these frames can be excluded from early allocations.
- multiboot2::information_view const * mbi;
+ kstd::observer_ptr<multiboot2::information_view const> mbi;
};
using region = multiboot2::memory_map::region;
@@ -85,7 +87,8 @@ namespace arch::memory
kapi::memory::frame m_kernel_end; //!< The end of the kernel image in physical memory.
kapi::memory::frame m_multiboot_start; //!< The start of the Multiboot2 information in physical memory.
kapi::memory::frame m_multiboot_end; //!< The end of the Multiboot2 information in physical memory.
- multiboot2::information_view const * m_multiboot_information; //!< Source of Multiboot2 module ranges.
+ kstd::observer_ptr<multiboot2::information_view const>
+ m_multiboot_information; //!< Source of Multiboot2 module ranges.
};
} // namespace arch::memory
diff --git a/arch/x86_64/arch/vga/text/buffer.cpp b/arch/x86_64/arch/vga/text/buffer.cpp
index 498b9a39..3557777b 100644
--- a/arch/x86_64/arch/vga/text/buffer.cpp
+++ b/arch/x86_64/arch/vga/text/buffer.cpp
@@ -2,6 +2,8 @@
#include <arch/vga/text/attribute.hpp>
+#include <kstd/memory.hpp>
+
#include <algorithm>
#include <bit>
#include <cstddef>
@@ -11,10 +13,10 @@
namespace arch::vga::text
{
- buffer::buffer(std::size_t width, std::size_t height, cell * start, std::size_t position)
+ buffer::buffer(std::size_t width, std::size_t height, kstd::observer_ptr<cell> start, std::size_t position)
: m_width{width}
, m_height{height}
- , m_buffer{start, m_width * m_height}
+ , m_buffer{start.get(), m_width * m_height}
, m_position{position}
{}
diff --git a/arch/x86_64/arch/vga/text/buffer.hpp b/arch/x86_64/arch/vga/text/buffer.hpp
index 7827b1af..15dbcb0f 100644
--- a/arch/x86_64/arch/vga/text/buffer.hpp
+++ b/arch/x86_64/arch/vga/text/buffer.hpp
@@ -5,6 +5,8 @@
#include <arch/vga/text/attribute.hpp>
+#include <kstd/memory.hpp>
+
#include <cstddef>
#include <span>
#include <string_view>
@@ -29,7 +31,7 @@ namespace arch::vga::text
//! @param height The height of the buffer
//! @param start A pointer to the first byte of the buffer.
//! @param position The starting position for the first write to the buffer
- buffer(std::size_t width, std::size_t height, cell * start, std::size_t position = 0);
+ buffer(std::size_t width, std::size_t height, kstd::observer_ptr<cell> start, std::size_t position = 0);
//! Clear the buffer.
//!
diff --git a/arch/x86_64/arch/vga/text/device.cpp b/arch/x86_64/arch/vga/text/device.cpp
index 84683585..e7140e43 100644
--- a/arch/x86_64/arch/vga/text/device.cpp
+++ b/arch/x86_64/arch/vga/text/device.cpp
@@ -5,6 +5,8 @@
#include <kapi/cio.hpp>
+#include <kstd/memory.hpp>
+
#include <bit>
#include <cstddef>
#include <cstdint>
@@ -22,10 +24,10 @@ namespace arch::vga::text
} // namespace
device::device()
- : m_buffer{
- default_buffer_width, default_buffer_height,
- std::bit_cast<buffer::cell *>(default_buffer_address + std::bit_cast<std::uintptr_t>(&boot::TEACHOS_VMA)),
- kapi::boot::bootstrap_information.vga_buffer_index}
+ : m_buffer{default_buffer_width, default_buffer_height,
+ kstd::observer_ptr{reinterpret_cast<buffer::cell *>(
+ default_buffer_address + std::bit_cast<std::uintptr_t>(&boot::TEACHOS_VMA))},
+ kapi::boot::bootstrap_information.vga_buffer_index}
{
clear();
}
diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp
index 03c5107c..307789cd 100644
--- a/arch/x86_64/kapi/memory.cpp
+++ b/arch/x86_64/kapi/memory.cpp
@@ -12,6 +12,7 @@
#include <kapi/boot.hpp>
#include <kapi/system.hpp>
+#include <kstd/memory.hpp>
#include <kstd/print.hpp>
#include <kstd/units.hpp>
@@ -49,7 +50,7 @@ namespace kapi::memory
}
auto const & mbi = boot::bootstrap_information.mbi;
- auto mbi_span = std::span{std::bit_cast<std::byte *>(mbi), static_cast<std::size_t>(mbi->size())};
+ auto mbi_span = std::span{reinterpret_cast<std::byte const *>(mbi.get()), static_cast<std::size_t>(mbi->size())};
auto image_span = std::span{&arch::boot::_start_physical, &arch::boot::_end_physical};
return arch::memory::region_allocator::memory_information{
@@ -62,6 +63,9 @@ namespace kapi::memory
auto establish_higher_half_direct_mapping() -> void
{
+ // We can't use an observer_ptr here, since this first frame allocation is very likely to return frame number
+ // zero, which has a starting address of zero. observer_ptr will interpret this as being a nullptr and raise a
+ // panic on dereference.
auto pml3_frame = kapi::memory::allocate_frame();
auto pml3 = static_cast<arch::memory::page_table *>(pml3_frame->start_address());
pml3->clear();
@@ -75,7 +79,7 @@ namespace kapi::memory
});
auto current_cr3 = arch::cpu::cr3::read();
- auto pml4 = static_cast<arch::memory::page_table *>(current_cr3.address());
+ auto pml4 = static_cast<kstd::observer_ptr<arch::memory::page_table>>(current_cr3.address());
(*pml4)[256].frame(*pml3_frame, arch::memory::page_table::entry::flags::present |
arch::memory::page_table::entry::flags::writable |
arch::memory::page_table::entry::flags::global);
@@ -218,7 +222,7 @@ namespace kapi::memory
system::panic("[ARCH:MEM] Failed to allocate new PML4!");
}
auto new_pml4 = arch::memory::to_higher_half_pointer<arch::memory::page_table>(new_pml4_frame->start_address());
- std::construct_at(new_pml4);
+ std::construct_at(new_pml4.get());
higher_half_mapper.emplace(new_pml4);
set_page_mapper(*higher_half_mapper);
@@ -229,7 +233,7 @@ namespace kapi::memory
remap_bootloader_modules(*higher_half_mapper);
auto current_cr3 = arch::cpu::cr3::read();
- auto old_pml4 = static_cast<arch::memory::page_table *>(current_cr3.address());
+ auto old_pml4 = static_cast<kstd::observer_ptr<arch::memory::page_table>>(current_cr3.address());
(*new_pml4)[256] = (*old_pml4)[256];
kstd::println("[ARCH:MEM] Switching to new paging hierarchy.");