diff options
Diffstat (limited to 'kernel/src')
| -rw-r--r-- | kernel/src/kapi/cio.cpp | 36 | ||||
| -rw-r--r-- | kernel/src/kapi/memory.cpp | 94 | ||||
| -rw-r--r-- | kernel/src/kapi/system.cpp | 21 | ||||
| -rw-r--r-- | kernel/src/kstd/os.cpp | 16 | ||||
| -rw-r--r-- | kernel/src/kstd/print.cpp | 145 | ||||
| -rw-r--r-- | kernel/src/main.cpp | 6 |
6 files changed, 3 insertions, 315 deletions
diff --git a/kernel/src/kapi/cio.cpp b/kernel/src/kapi/cio.cpp deleted file mode 100644 index 01c6420..0000000 --- a/kernel/src/kapi/cio.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#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(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 teachos::cio diff --git a/kernel/src/kapi/memory.cpp b/kernel/src/kapi/memory.cpp deleted file mode 100644 index ebd4c15..0000000 --- a/kernel/src/kapi/memory.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#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_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 teachos::memory
\ No newline at end of file diff --git a/kernel/src/kapi/system.cpp b/kernel/src/kapi/system.cpp deleted file mode 100644 index cdde049..0000000 --- a/kernel/src/kapi/system.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "kapi/system.hpp" - -#include "kapi/cpu.hpp" - -#include <kstd/print> - -#include <source_location> -#include <string_view> - -namespace teachos::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 teachos::system diff --git a/kernel/src/kstd/os.cpp b/kernel/src/kstd/os.cpp deleted file mode 100644 index 5280f9c..0000000 --- a/kernel/src/kstd/os.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "kapi/system.hpp" - -#include <kstd/os/error.hpp> - -#include <source_location> -#include <string_view> - -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/kstd/print.cpp b/kernel/src/kstd/print.cpp deleted file mode 100644 index 2665b9a..0000000 --- a/kernel/src/kstd/print.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include "kapi/cio.hpp" - -#include <kstd/format> -#include <kstd/os/print.hpp> -#include <kstd/print> - -#include <array> -#include <cstddef> -#include <iterator> -#include <string_view> - -namespace kstd::os -{ - - namespace - { - struct write_buffer - { - using output_stream = teachos::cio::output_stream; - - constexpr auto static size = 128uz; - - write_buffer(write_buffer const &) = delete; - write_buffer(write_buffer &&) = delete; - auto operator=(write_buffer const &) -> write_buffer & = delete; - auto operator=(write_buffer &&) -> write_buffer & = delete; - - explicit write_buffer(output_stream stream) - : m_stream{stream} - {} - - ~write_buffer() noexcept - { - flush(); - } - - auto flush() noexcept -> void - { - if (m_position > 0) - { - std::string_view chunk{m_buffer.data(), m_position}; - teachos::cio::write(m_stream, chunk); - m_position = 0; - } - } - - auto static callback(void * object, std::string_view text) -> void - { - auto * self = static_cast<write_buffer *>(object); - for (char const character : text) - { - if (self->m_position >= size) - { - self->flush(); - } - self->m_buffer.at(self->m_position++) = character; - } - } - - private: - output_stream m_stream; - std::array<char, size> m_buffer{}; - std::size_t m_position{}; - }; - - } // namespace - - auto vprint(print_sink sink, std::string_view format, kstd::format_args args) -> void - { - auto writer = write_buffer{(sink == print_sink::stderr) ? teachos::cio::output_stream::stderr - : teachos::cio::output_stream::stdout}; - auto context = kstd::format_context{.writer = write_buffer::callback, .user_data = &writer}; - - auto current = format.begin(); - auto end = format.end(); - auto next_automatic_index = 0uz; - - while (current != end) - { - if (*current != '{') - { - auto start = current; - while (current != end && *current != '{') - { - std::advance(current, 1); - } - context.push(std::string_view(start, current - start)); - continue; - } - - if (std::next(current) != end && *(std::next(current)) == '{') - { - context.push('{'); - std::advance(current, 2); - continue; - } - - std::advance(current, 1); - - auto index = 0uz; - if (current != end && *current >= '0' && *current <= '9') - { - while (current != end && *current >= '0' && *current <= '9') - { - index = index * 10 + static_cast<std::size_t>(*current - '0'); - std::advance(current, 1); - } - } - else - { - index = next_automatic_index++; - } - - auto remaining_fmt = std::string_view{current, static_cast<std::size_t>(std::distance(current, end))}; - - auto const arg = args.get(index); - if (arg.format) - { - auto const after_specs = arg.format(arg.value, remaining_fmt, context); - auto const consumed = remaining_fmt.size() - after_specs.size(); - std::advance(current, consumed); - } - else - { - context.push("{?}"); - while (current != end && *current != '}') - std::advance(current, 1); - } - - if (current != end && *current == '}') - { - std::advance(current, 1); - } - else - { - context.push("{fmt-err}"); - while (current != end && *current != '}') - std::advance(current, 1); - if (current != end) - std::advance(current, 1); - } - } - } - -} // namespace kstd::os diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index f1e5dd0..8732fa2 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -6,11 +6,11 @@ auto main() -> int { - teachos::cio::init(); + kapi::cio::init(); kstd::println("[OS] IO subsystem initialized."); - teachos::memory::init(); + kapi::memory::init(); kstd::println("[OS] Memory subsystem initialized."); - teachos::system::panic("Returning from kernel main!"); + kapi::system::panic("Returning from kernel main!"); } |
