aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-01-16 13:36:38 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-01-16 13:36:38 +0100
commit7d6f0ed063790042a808f4bf07c50d308b3f2de4 (patch)
tree1a2e1c4ed7e2f3d8e6cdcfb012e554d1a4eb1e5a /kernel/kapi
parent9750405757396d006ab6992fb93baf414b3e2ae8 (diff)
downloadteachos-7d6f0ed063790042a808f4bf07c50d308b3f2de4.tar.xz
teachos-7d6f0ed063790042a808f4bf07c50d308b3f2de4.zip
chore: restructure namespaces
Diffstat (limited to 'kernel/kapi')
-rw-r--r--kernel/kapi/cio.cpp36
-rw-r--r--kernel/kapi/memory.cpp94
-rw-r--r--kernel/kapi/system.cpp21
3 files changed, 151 insertions, 0 deletions
diff --git a/kernel/kapi/cio.cpp b/kernel/kapi/cio.cpp
new file mode 100644
index 0000000..d447a6a
--- /dev/null
+++ b/kernel/kapi/cio.cpp
@@ -0,0 +1,36 @@
+#include "kapi/cio.hpp"
+
+#include <optional>
+#include <string_view>
+#include <utility>
+
+namespace kapi::cio
+{
+ namespace
+ {
+ struct null_device final : public output_device
+ {
+ null_device static instance;
+ auto write(output_stream, std::string_view) -> void override {}
+ };
+
+ constinit null_device null_device::instance;
+ } // namespace
+
+ constinit auto static active_device = static_cast<output_device *>(&null_device::instance);
+
+ auto set_output_device(output_device & device) -> std::optional<output_device *>
+ {
+ if (&device == active_device)
+ {
+ return {};
+ }
+ return std::exchange(active_device, &device);
+ }
+
+ auto write(output_stream stream, std::string_view text) -> void
+ {
+ active_device->write(stream, text);
+ }
+
+} // namespace kapi::cio
diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp
new file mode 100644
index 0000000..7c9b1da
--- /dev/null
+++ b/kernel/kapi/memory.cpp
@@ -0,0 +1,94 @@
+#include "kapi/memory.hpp"
+
+#include "kapi/system.hpp"
+
+#include <cstddef>
+#include <optional>
+#include <utility>
+
+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<std::pair<frame, std::size_t>> override
+ {
+ system::panic("Tried to allocate frames without an active allocator.");
+ }
+
+ auto release_many(std::pair<frame, std::size_t>) -> void override
+ {
+ system::panic("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) -> std::byte * override
+ {
+ system::panic("Tried to map a page without an active mapper.");
+ }
+
+ auto unmap(page) -> void override
+ {
+ system::panic("Tried to unmap a page without an active mapper.");
+ }
+
+ auto try_unmap(page) noexcept -> bool override
+ {
+ return false;
+ }
+ };
+
+ constinit bad_frame_allocator bad_frame_allocator::instance{};
+ constinit bad_page_mapper bad_page_mapper::instance{};
+ } // namespace
+
+ constinit auto static active_frame_allocator = static_cast<frame_allocator *>(&bad_frame_allocator::instance);
+ constinit auto static active_page_mapper = static_cast<page_mapper *>(&bad_page_mapper::instance);
+
+ auto set_frame_allocator(frame_allocator & allocator) -> std::optional<frame_allocator *>
+ {
+ if (&allocator == active_frame_allocator)
+ {
+ return {};
+ }
+ return std::exchange(active_frame_allocator, &allocator);
+ }
+
+ auto set_page_mapper(page_mapper & mapper) -> std::optional<page_mapper *>
+ {
+ if (&mapper == active_page_mapper)
+ {
+ return {};
+ }
+ return std::exchange(active_page_mapper, &mapper);
+ }
+
+ auto allocate_frame() -> std::optional<frame>
+ {
+ return active_frame_allocator->allocate();
+ }
+
+ auto allocate_many_frames(std::size_t count) -> std::optional<std::pair<frame, std::size_t>>
+ {
+ return active_frame_allocator->allocate_many(count);
+ }
+
+ auto map(page page, frame frame) -> std::byte *
+ {
+ return active_page_mapper->map(page, frame, page_mapper::flags::empty);
+ }
+
+ auto unmap(page page) -> void
+ {
+ return active_page_mapper->unmap(page);
+ }
+
+} // namespace kapi::memory \ No newline at end of file
diff --git a/kernel/kapi/system.cpp b/kernel/kapi/system.cpp
new file mode 100644
index 0000000..a17d9b9
--- /dev/null
+++ b/kernel/kapi/system.cpp
@@ -0,0 +1,21 @@
+#include "kapi/system.hpp"
+
+#include "kapi/cpu.hpp"
+
+#include <kstd/print>
+
+#include <source_location>
+#include <string_view>
+
+namespace kapi::system
+{
+
+ [[gnu::weak]]
+ auto panic(std::string_view message, std::source_location location) -> void
+ {
+ kstd::println(kstd::print_sink::stderr, "[PANIC] in {} : {} @ {}:{}", location.function_name(), message,
+ location.file_name(), location.line());
+ cpu::halt();
+ }
+
+} // namespace kapi::system