diff options
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/src/kapi/cio.cpp | 57 | ||||
| -rw-r--r-- | kernel/src/kapi/memory.cpp | 91 | ||||
| -rw-r--r-- | kernel/src/kapi/system.cpp | 20 | ||||
| -rw-r--r-- | kernel/src/kstd.cpp | 13 | ||||
| -rw-r--r-- | kernel/src/main.cpp | 14 |
5 files changed, 195 insertions, 0 deletions
diff --git a/kernel/src/kapi/cio.cpp b/kernel/src/kapi/cio.cpp new file mode 100644 index 0000000..66493b6 --- /dev/null +++ b/kernel/src/kapi/cio.cpp @@ -0,0 +1,57 @@ +#include "kapi/cio.hpp" + +#include <optional> +#include <string_view> +#include <utility> + +namespace teachos::cio +{ + namespace + { + struct null_device final : public output_device + { + null_device static instance; + + auto write(std::string_view) -> void override {} + auto writeln(std::string_view) -> void override {} + + auto write_error(std::string_view) -> void override {} + auto writeln_error(std::string_view) -> void override {} + }; + + constinit null_device null_device::instance; + } // namespace + + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) + constinit auto 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 print(std::string_view text) -> void + { + active_device->write(text); + } + + auto println(std::string_view text) -> void + { + active_device->writeln(text); + } + + auto print_error(std::string_view text) -> void + { + active_device->write_error(text); + } + + auto println_error(std::string_view text) -> void + { + active_device->writeln_error(text); + } + +} // namespace teachos::cio
\ No newline at end of file diff --git a/kernel/src/kapi/memory.cpp b/kernel/src/kapi/memory.cpp new file mode 100644 index 0000000..d6c84e1 --- /dev/null +++ b/kernel/src/kapi/memory.cpp @@ -0,0 +1,91 @@ +#include "kapi/memory.hpp" + +#include "kapi/system.hpp" + +#include <cstddef> +#include <optional> +#include <utility> + +namespace teachos::memory +{ + + namespace + { + struct bad_frame_allocator final : public frame_allocator + { + bad_frame_allocator static instance; + + auto allocate() noexcept -> std::optional<frame> override + { + system::panic("Tried to allocate a frame without an active allocator."); + } + + auto release(frame) -> void override + { + system::panic("Tried to release a frame 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 + + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) + constinit auto active_frame_allocator = static_cast<frame_allocator *>(&bad_frame_allocator::instance); + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) + constinit auto 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 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 teachos::memory
\ No newline at end of file diff --git a/kernel/src/kapi/system.cpp b/kernel/src/kapi/system.cpp new file mode 100644 index 0000000..3ae3f29 --- /dev/null +++ b/kernel/src/kapi/system.cpp @@ -0,0 +1,20 @@ +#include "kapi/system.hpp" + +#include "kapi/cio.hpp" +#include "kapi/cpu.hpp" + +namespace teachos::system +{ + + [[gnu::weak]] + auto panic(std::string_view message, std::source_location location) -> void + { + cio::println_error("!!!Kernel Panic!!! "); + cio::println_error(message); + cio::println_error(location.file_name()); + cio::println_error(location.function_name()); + + cpu::halt(); + } + +} // namespace teachos::system diff --git a/kernel/src/kstd.cpp b/kernel/src/kstd.cpp new file mode 100644 index 0000000..2149c12 --- /dev/null +++ b/kernel/src/kstd.cpp @@ -0,0 +1,13 @@ +#include "kapi/system.hpp" + +#include <kstd/bits/os.hpp> + +namespace kstd::os +{ + + auto panic(std::string_view message, std::source_location location) -> void + { + teachos::system::panic(message, location); + } + +} // namespace kstd::os
\ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp new file mode 100644 index 0000000..3394275 --- /dev/null +++ b/kernel/src/main.cpp @@ -0,0 +1,14 @@ +#include "kapi/cio.hpp" +#include "kapi/memory.hpp" +#include "kapi/system.hpp" + +auto main() -> int +{ + teachos::cio::init(); + teachos::cio::println("[OS] IO subsystem initialized."); + + teachos::memory::init(); + teachos::cio::println("[OS] Memory subsystem initialized."); + + teachos::system::panic("Returning from kernel main!"); +} |
