#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace kapi::memory { namespace { struct bad_frame_allocator final : public frame_allocator { bad_frame_allocator static instance; auto allocate_many(std::size_t) noexcept -> std::optional> override { system::panic("[OS:MEM] Tried to allocate frames without an active allocator."); } auto mark_used(frame) -> void override { system::panic("[OS:MEM] Tried to mark frame as used without an active allocator."); } auto release_many(std::pair) -> void override { system::panic("[OS:MEM] Tried to release frames without an active allocator."); } }; struct bad_page_mapper final : public page_mapper { bad_page_mapper static instance; auto map(page, frame, flags) -> kstd::observer_ptr override { system::panic("[OS:MEM] Tried to map a page without an active mapper."); } auto unmap(page) -> kapi::memory::frame override { system::panic("[OS:MEM] Tried to unmap a page without an active mapper."); } auto try_unmap(page) noexcept -> std::optional override { return std::nullopt; } }; constinit bad_frame_allocator bad_frame_allocator::instance{}; constinit bad_page_mapper bad_page_mapper::instance{}; auto constinit allocator = std::optional{}; auto constinit mmio_allocator = std::optional{}; constinit auto active_frame_allocator = static_cast(&bad_frame_allocator::instance); constinit auto active_page_mapper = static_cast(&bad_page_mapper::instance); auto unmap_range_if_mapped(page start, std::size_t count) -> void { for (auto page_index = 0uz; page_index < count; ++page_index) { auto const current_page = start + page_index; // NOTE: We ignore the returned frame, since it has never been allocated by any kernel code. Rather, this frame // corresponds to a frame which is "always present" since it is an abstraction of a memory mapped device, // provided by the firmware. Additionally, we use try unmap to avoid panicking if the page was not mapped. std::ignore = active_page_mapper->try_unmap(current_page); } } } // namespace auto get_frame_allocator() -> frame_allocator & { return *active_frame_allocator; } auto set_frame_allocator(frame_allocator & allocator) -> std::optional { if (&allocator == active_frame_allocator) { return {}; } return std::exchange(active_frame_allocator, &allocator); } auto set_page_mapper(page_mapper & mapper) -> std::optional { if (&mapper == active_page_mapper) { return {}; } return std::exchange(active_page_mapper, &mapper); } auto allocate_frame() -> std::optional { return get_frame_allocator().allocate(); } auto allocate_many_frames(std::size_t count) -> std::optional> { return get_frame_allocator().allocate_many(count); } auto map(page page, frame frame, page_mapper::flags flags) -> kstd::observer_ptr { return active_page_mapper->map(page, frame, flags); } auto unmap(page page) -> frame { return active_page_mapper->unmap(page); } auto init_pmm(std::size_t frame_count, void (&handoff_handler)(frame_allocator &)) -> void { using namespace kstd::units_literals; auto const bitmap_bytes = kstd::bytes{(frame_count + 7uz) / 8uz}; auto const bitmap_pages = (bitmap_bytes + page::size - 1_B) / page::size; auto const bitmap_frames = allocate_many_frames(bitmap_pages); if (!bitmap_frames) { system::panic("[OS:MEM] Not enough memory for bitmap allocator!"); } auto const flags = page_mapper::flags::writable | page_mapper::flags::supervisor_only | page_mapper::flags::global; auto bitmap_ptr = kstd::observer_ptr{nullptr}; std::ranges::for_each(std::views::iota(0uz, bitmap_pages), [&](auto index) { auto page = page::containing(pmm_metadata_base + index * page::size); auto frame = memory::frame(bitmap_frames->first.number() + index); auto mapped = active_page_mapper->map(page, frame, flags); if (!bitmap_ptr) { bitmap_ptr = reinterpret_pointer_cast(mapped); } }); auto bitmap = std::span{bitmap_ptr.get(), (bitmap_bytes + kstd::size_of() - 1_B) / kstd::size_of()}; allocator.emplace(bitmap, frame_count); handoff_handler(allocator.value()); set_frame_allocator(allocator.value()); kstd::println("[OS:MEM] Physical memory manager initialized."); } auto init_mmio(linear_address base, std::size_t page_count) -> void { mmio_allocator.emplace(base, page_count); } auto allocate_mmio_region(std::size_t page_count) -> kstd::result { if (!mmio_allocator) { system::panic("[OS:MEM] MMIO allocator not initialized!"); } return mmio_allocator->allocate(page_count).transform([&](auto base) -> mmio_region { return {base, page_count}; }); } auto map_mmio_region(mmio_region region, physical_address hw_base, page_mapper::flags flags) -> kstd::result> { // Ensure that the MMIO region is not mapped as executable. Asking for an executable mapping of an MMIO region is a // logical error, not a runtime issue. if ((flags & page_mapper::flags::executable) != page_mapper::flags::empty) { system::panic("[OS:MEM] Attempt to map MMIO region at {:#018x} as executable!", region.first); } auto [start, count] = region; auto start_page = page::containing(start); auto start_frame = frame::containing(hw_base); flags |= page_mapper::flags::uncached | page_mapper::flags::supervisor_only | page_mapper::flags::global; auto first_mapped_page = map(start_page, start_frame, flags); if (!first_mapped_page) { return kstd::failure(make_error_code(kstd::errc::not_enough_memory)); } for (auto page_index = 1uz; page_index < count; ++page_index) { auto page = start_page + page_index; auto frame = start_frame + page_index; auto mapped = map(page, frame, flags); if (!mapped) { unmap_range_if_mapped(start_page, page_index); return kstd::failure(make_error_code(kstd::errc::not_enough_memory)); } } return kstd::success(first_mapped_page); } auto release_mmio_region(mmio_region region) -> void { if (!mmio_allocator) { system::panic("[OS:MEM] MMIO allocator not initialized!"); } auto start_address = region.first; auto start_page = kapi::memory::page::containing(start_address); unmap_range_if_mapped(start_page, region.second); mmio_allocator->release(region.first); } } // namespace kapi::memory