From 3a2efa4ebc6b07a2304416262d5032a32dcddd8b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 14 Jul 2025 13:06:17 +0000 Subject: x86_64: fix syscall error code reading --- arch/x86_64/src/context_switching/syscall/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/context_switching/syscall/main.cpp b/arch/x86_64/src/context_switching/syscall/main.cpp index e291c10..b4ab468 100644 --- a/arch/x86_64/src/context_switching/syscall/main.cpp +++ b/arch/x86_64/src/context_switching/syscall/main.cpp @@ -27,7 +27,7 @@ namespace teachos::arch::context_switching::syscall asm volatile("mov %%r9, %[output]" : [output] "=m"(values.arg_5)); error error_code{}; - asm volatile("mov %%rax, %[output]" : [output] "=m"(error_code)); + asm volatile("mov %%al, %[output]" : [output] "=m"(error_code)); return {error_code, values}; } -- cgit v1.2.3 From e7eedd234954509f4f5ec52b2d62cbc4a1723936 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 14 Jul 2025 15:39:09 +0000 Subject: libs: begin extraction of kernel std --- .../src/memory/heap/linked_list_allocator.cpp | 2 +- arch/x86_64/src/memory/multiboot/reader.cpp | 250 +++++++++++---------- arch/x86_64/src/stl/mutex.cpp | 16 -- 3 files changed, 128 insertions(+), 140 deletions(-) delete mode 100644 arch/x86_64/src/stl/mutex.cpp (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp index 63a6111..00ca366 100644 --- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp +++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp @@ -9,7 +9,7 @@ namespace teachos::arch::memory::heap { linked_list_allocator::linked_list_allocator(std::size_t heap_start, std::size_t heap_end) : first(nullptr) - , mutex{stl::mutex{}} + , mutex{kstd::mutex{}} { auto const heap_size = heap_end - heap_start; exception_handling::assert( diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 2bf5b25..b05e6b3 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -2,130 +2,134 @@ #include "arch/boot/pointers.hpp" #include "arch/exception_handling/assert.hpp" -#include "arch/memory/multiboot/elf_symbols_section.hpp" -#include "arch/memory/multiboot/info.hpp" +#include "multiboot2/information.hpp" +// #include "arch/memory/multiboot/elf_symbols_section.hpp" +// #include "arch/memory/multiboot/info.hpp" #include #include -namespace teachos::arch::memory::multiboot -{ - namespace - { - template - requires std::is_pointer::value - auto align_to_8_byte_boundary(T ptr, uint32_t size) -> T - { - return reinterpret_cast(reinterpret_cast(ptr) + ((size + 7) & ~7)); - } - - auto process_memory_map(memory_map_header * mminfo) -> memory_area_container - { - auto const expected_entry_size = mminfo->entry_size; - auto constexpr actual_entry_size = sizeof(memory_area); - exception_handling::assert(expected_entry_size == actual_entry_size, - "[Multiboot Reader] Unexpected memory area entry size"); - - auto const total_size = mminfo->info.size; - auto const total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; - auto const number_of_entries = total_entries_size / actual_entry_size; - - auto const begin = memory_area_container::iterator{&mminfo->entries}; - auto const end = begin + number_of_entries; - return memory_area_container{begin, end}; - } - - auto process_elf_sections(elf_symbols_section_header * symbol, std::size_t & kernel_start, - std::size_t & kernel_end) -> elf_section_header_container - { - auto const expected_entry_size = symbol->entry_size; - auto constexpr actual_entry_size = sizeof(elf_section_header); - exception_handling::assert(expected_entry_size == actual_entry_size, - "[Multiboot Reader] Unexpected elf section header entry size"); - - auto const expected_total_size = symbol->info.size; - auto const actual_total_entry_size = actual_entry_size * symbol->number_of_sections; - auto constexpr actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); - auto const actual_total_size = actual_total_entry_size + actual_total_section_size; - exception_handling::assert(expected_total_size == actual_total_size, - "[Multiboot Reader] Unexpected elf symbols section header total size"); - - auto const begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; - auto const end = begin + symbol->number_of_sections; - exception_handling::assert(begin->is_null(), - "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); - - elf_section_header_container sections{begin, end}; - - auto allocated_sections = sections | std::views::filter([](auto const & section) { - return section.flags.contains_flags(elf_section_flags::OCCUPIES_MEMORY); - }); - - auto const elf_section_with_lowest_physical_address = std::ranges::min_element( - allocated_sections, [](auto const & a, auto const & b) { return a.physical_address < b.physical_address; }); - - auto const elf_section_with_highest_physical_address = - std::ranges::max_element(allocated_sections, [](auto const & a, auto const & b) { - auto a_physical_address_end = a.physical_address + a.section_size; - auto b_physical_address_end = b.physical_address + b.section_size; - return a_physical_address_end < b_physical_address_end; - }); - - auto const symbol_table_section_count = std::ranges::count_if(sections, [](auto const & section) { - return section.type == elf_section_type::DYNAMIC_SYMBOL_TABLE || section.type == elf_section_type::SYMBOL_TABLE; - }); - auto const dynamic_section_count = std::ranges::count_if( - sections, [](auto const & section) { return section.type == elf_section_type::DYNAMIC; }); - - exception_handling::assert( - symbol_table_section_count == 1U, - "[Multiboot Reader] ELF Specifications allows only (1) symbol table section, but got more"); - exception_handling::assert( - dynamic_section_count <= 1U, - "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); - - auto const lowest_elf_section = *elf_section_with_lowest_physical_address; - kernel_start = lowest_elf_section.physical_address; - - auto const highest_elf_section = *elf_section_with_highest_physical_address; - kernel_end = highest_elf_section.physical_address + highest_elf_section.section_size; - - return sections; - } - } // namespace - - auto read_multiboot2() -> memory_information - { - memory_information mem_info{UINT64_MAX, - 0U, - elf_section_header_container{}, - boot::multiboot_information_pointer, - 0U, - memory_area_container{}}; - - auto const multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); - auto const multiboot_tag = &multiboot_information_pointer->tags; - mem_info.multiboot_end = mem_info.multiboot_start + multiboot_information_pointer->total_size; - - for (auto tag = multiboot_tag; tag->type != tag_type::END; tag = align_to_8_byte_boundary(tag, tag->size)) - { - switch (tag->type) - { - case tag_type::ELF_SECTIONS: { - auto const symbol = reinterpret_cast(tag); - mem_info.sections = process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end); - break; - } - case tag_type::MEMORY_MAP: { - auto const mminfo = reinterpret_cast(tag); - mem_info.areas = process_memory_map(mminfo); - break; - } - default: - // All other cases are not important and can be ignored. - break; - } - } - return mem_info; - } -} // namespace teachos::arch::memory::multiboot +// namespace teachos::arch::memory::multiboot +// { +// namespace +// { +// template +// requires std::is_pointer::value +// auto align_to_8_byte_boundary(T ptr, uint32_t size) -> T +// { +// return reinterpret_cast(reinterpret_cast(ptr) + ((size + 7) & ~7)); +// } + +// auto process_memory_map(memory_map_header * mminfo) -> memory_area_container +// { +// auto const expected_entry_size = mminfo->entry_size; +// auto constexpr actual_entry_size = sizeof(memory_area); +// exception_handling::assert(expected_entry_size == actual_entry_size, +// "[Multiboot Reader] Unexpected memory area entry size"); + +// auto const total_size = mminfo->info.size; +// auto const total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; +// auto const number_of_entries = total_entries_size / actual_entry_size; + +// auto const begin = memory_area_container::iterator{&mminfo->entries}; +// auto const end = begin + number_of_entries; +// return memory_area_container{begin, end}; +// } + +// auto process_elf_sections(elf_symbols_section_header * symbol, std::size_t & kernel_start, std::size_t & +// kernel_end) +// -> elf_section_header_container +// { +// auto const expected_entry_size = symbol->entry_size; +// auto constexpr actual_entry_size = sizeof(elf_section_header); +// exception_handling::assert(expected_entry_size == actual_entry_size, +// "[Multiboot Reader] Unexpected elf section header entry size"); + +// auto const expected_total_size = symbol->info.size; +// auto const actual_total_entry_size = actual_entry_size * symbol->number_of_sections; +// auto constexpr actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); +// auto const actual_total_size = actual_total_entry_size + actual_total_section_size; +// exception_handling::assert(expected_total_size == actual_total_size, +// "[Multiboot Reader] Unexpected elf symbols section header total size"); + +// auto const begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; auto const end = begin + symbol->number_of_sections; +// exception_handling::assert(begin->is_null(), +// "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); + +// elf_section_header_container sections{begin, end}; + +// auto allocated_sections = sections | std::views::filter([](auto const & section) { +// return section.flags.contains_flags(elf_section_flags::OCCUPIES_MEMORY); +// }); + +// auto const elf_section_with_lowest_physical_address = std::ranges::min_element( +// allocated_sections, [](auto const & a, auto const & b) { return a.physical_address < b.physical_address; +// }); + +// auto const elf_section_with_highest_physical_address = +// std::ranges::max_element(allocated_sections, [](auto const & a, auto const & b) { +// auto a_physical_address_end = a.physical_address + a.section_size; +// auto b_physical_address_end = b.physical_address + b.section_size; +// return a_physical_address_end < b_physical_address_end; +// }); + +// auto const symbol_table_section_count = std::ranges::count_if(sections, [](auto const & section) { +// return section.type == elf_section_type::DYNAMIC_SYMBOL_TABLE || section.type == +// elf_section_type::SYMBOL_TABLE; +// }); +// auto const dynamic_section_count = std::ranges::count_if( +// sections, [](auto const & section) { return section.type == elf_section_type::DYNAMIC; }); + +// exception_handling::assert( +// symbol_table_section_count == 1U, +// "[Multiboot Reader] ELF Specifications allows only (1) symbol table section, but got more"); +// exception_handling::assert( +// dynamic_section_count <= 1U, +// "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); + +// auto const lowest_elf_section = *elf_section_with_lowest_physical_address; +// kernel_start = lowest_elf_section.physical_address; + +// auto const highest_elf_section = *elf_section_with_highest_physical_address; +// kernel_end = highest_elf_section.physical_address + highest_elf_section.section_size; + +// return sections; +// } +// } // namespace + +// auto read_multiboot2() -> memory_information +// { +// memory_information mem_info{UINT64_MAX, +// 0U, +// elf_section_header_container{}, +// boot::multiboot_information_pointer, +// 0U, +// memory_area_container{}}; + +// auto const multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); +// auto const multiboot_tag = &multiboot_information_pointer->tags; +// mem_info.multiboot_end = mem_info.multiboot_start + multiboot_information_pointer->total_size; + +// for (auto tag = multiboot_tag; tag->type != tag_type::END; tag = align_to_8_byte_boundary(tag, tag->size)) +// { +// switch (tag->type) +// { +// case tag_type::ELF_SECTIONS: { +// auto const symbol = reinterpret_cast(tag); +// mem_info.sections = process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end); +// break; +// } +// case tag_type::MEMORY_MAP: { +// auto const mminfo = reinterpret_cast(tag); +// mem_info.areas = process_memory_map(mminfo); +// break; +// } +// default: +// // All other cases are not important and can be ignored. +// break; +// } +// } +// return mem_info; +// } +// } // namespace teachos::arch::memory::multiboot diff --git a/arch/x86_64/src/stl/mutex.cpp b/arch/x86_64/src/stl/mutex.cpp deleted file mode 100644 index 232a11c..0000000 --- a/arch/x86_64/src/stl/mutex.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "arch/stl/mutex.hpp" - -namespace teachos::arch::stl -{ - auto mutex::lock() -> void - { - while (!try_lock()) - { - // Nothing to do - } - } - - auto mutex::try_lock() -> bool { return !locked.exchange(true, std::memory_order_acquire); } - - auto mutex::unlock() -> void { locked.store(false, std::memory_order_release); } -} // namespace teachos::arch::stl -- cgit v1.2.3 From ec572bff8150e2f8cd2dc99e053c5e8c8a0b99e3 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 14 Jul 2025 16:25:00 +0000 Subject: arch: prepare interfaces --- arch/x86_64/src/boot/boot.s | 2 +- arch/x86_64/src/io.cpp | 22 ++++++++++++++++ arch/x86_64/src/memory.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++ arch/x86_64/src/system.cpp | 12 +++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 arch/x86_64/src/io.cpp create mode 100644 arch/x86_64/src/memory.cpp create mode 100644 arch/x86_64/src/system.cpp (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 7932045..5488073 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -364,5 +364,5 @@ _transition_to_long_mode: call _init - call kernel_main + call main call halt diff --git a/arch/x86_64/src/io.cpp b/arch/x86_64/src/io.cpp new file mode 100644 index 0000000..8808dbb --- /dev/null +++ b/arch/x86_64/src/io.cpp @@ -0,0 +1,22 @@ +namespace teachos::arch::io +{ + + // using x86_64::vga::text_mode::attributes; + // using x86_64::vga::text_mode::color; + + // namespace + // { + // auto constexpr error_attributes = + // attributes{.foreground = color::light_gray, .bright = true, .background = color::red, .blink = true}; + // } // namespace + + auto init() -> void + { + // kernel::set_print_handler([](auto text) { return x86_64::vga::text_mode::print(text); }); + // kernel::set_println_handler([](auto text) { return x86_64::vga::text_mode::println(text); }); + // kernel::set_print_error_handler([](auto text) { return x86_64::vga::text_mode::print(text, error_attributes); }); + // kernel::set_println_error_handler( + // [](auto text) { return x86_64::vga::text_mode::println(text, error_attributes); }); + } + +} // namespace teachos::arch::io diff --git a/arch/x86_64/src/memory.cpp b/arch/x86_64/src/memory.cpp new file mode 100644 index 0000000..245d7bd --- /dev/null +++ b/arch/x86_64/src/memory.cpp @@ -0,0 +1,62 @@ +#include "arch/memory.hpp" + +// #include "noarch/error.hpp" +// #include "noarch/print.hpp" +// #include "x86_64/bootstrap/mutiboot.hpp" +// #include "x86_64/memory/frame_allocator.hpp" +// #include "x86_64/memory/mbi_frame_allocator.hpp" + +// #include +// #include +// #include +// #include + +namespace teachos::arch::memory +{ + + // namespace + // { + // /** + // * @brief Remap the kernel according to the ELF information. + // * + // * After initial bootup, a basic identity mapping of the lower 1GiB is set up. This mapping allows execution + // from, + // * as well as read and write access to, the mapped memory. In order to protect the kernel, remap it according to + // the + // * information supplied by the ELF file. This means remapping code sections as read-only and data sections as + // * no-execute (and read only for .rodata). + // * + // * @param sections Information about the sections in the loaded kernel binary. + // * @param allocator The frame allocator used to create new page mappings. + // */ + // auto remap_kernel(elf::section_header_table const & sections, x86_64::memory::frame_allocator & allocator) -> + // void + // { + // static_cast(sections); + // static_cast(allocator); + // } + // } // namespace + + auto init() -> void + { + // kernel::println("Initializing memory"); + + // if (!x86_64::bootstrap::multiboot_information_pointer->has()) + // { + // kernel::panic("Received no memory map from the boot loader!"); + // } + + // if (!x86_64::bootstrap::multiboot_information_pointer->has()) + // { + // kernel::panic("Received no ELF symbol information from the boot loader!"); + // } + + // auto memory_map = x86_64::bootstrap::multiboot_information_pointer->memory_map(); + // auto elf_symbols = x86_64::bootstrap::multiboot_information_pointer->elf_symbols(); + // auto section_header_table = elf::section_header_table{elf_symbols.data(), elf::file_class::bits_64}; + // auto allocator = x86_64::memory::mbi_frame_allocator{memory_map, section_header_table}; + + // remap_kernel(section_header_table, allocator); + } + +} // namespace teachos::arch::memory diff --git a/arch/x86_64/src/system.cpp b/arch/x86_64/src/system.cpp new file mode 100644 index 0000000..60ebf0e --- /dev/null +++ b/arch/x86_64/src/system.cpp @@ -0,0 +1,12 @@ +#include "arch/system.hpp" + +namespace teachos::arch::system +{ + + auto halt() -> void + { + asm volatile("1: hlt\njmp 1b"); + __builtin_unreachable(); + } + +} // namespace teachos::arch::system -- cgit v1.2.3 From 3a47a8bd0edcfa3aa03562d0a5c390ef85ad0c6b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 14 Jul 2025 21:08:02 +0000 Subject: x86_64: move basic text output implementation --- arch/x86_64/src/io.cpp | 28 ++++++++-------- arch/x86_64/src/vga/text.cpp | 66 ++++++++++++++++++++++++++++++++++++++ arch/x86_64/src/video/vga/text.cpp | 66 -------------------------------------- 3 files changed, 80 insertions(+), 80 deletions(-) create mode 100644 arch/x86_64/src/vga/text.cpp delete mode 100644 arch/x86_64/src/video/vga/text.cpp (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/io.cpp b/arch/x86_64/src/io.cpp index 8808dbb..5fb1c85 100644 --- a/arch/x86_64/src/io.cpp +++ b/arch/x86_64/src/io.cpp @@ -1,22 +1,22 @@ +#include "kern/print.hpp" +#include "x86_64/vga/text.hpp" + namespace teachos::arch::io { - // using x86_64::vga::text_mode::attributes; - // using x86_64::vga::text_mode::color; - - // namespace - // { - // auto constexpr error_attributes = - // attributes{.foreground = color::light_gray, .bright = true, .background = color::red, .blink = true}; - // } // namespace - auto init() -> void { - // kernel::set_print_handler([](auto text) { return x86_64::vga::text_mode::print(text); }); - // kernel::set_println_handler([](auto text) { return x86_64::vga::text_mode::println(text); }); - // kernel::set_print_error_handler([](auto text) { return x86_64::vga::text_mode::print(text, error_attributes); }); - // kernel::set_println_error_handler( - // [](auto text) { return x86_64::vga::text_mode::println(text, error_attributes); }); + teachos::set_print_handler( + [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::green_on_black); }); + teachos::set_println_handler( + [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::green_on_black); }); + + teachos::set_print_error_handler( + [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::red_on_black); }); + teachos::set_println_error_handler( + [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::red_on_black); }); + + teachos::println("[x86-64] Basic VGA text output initialized."); } } // namespace teachos::arch::io diff --git a/arch/x86_64/src/vga/text.cpp b/arch/x86_64/src/vga/text.cpp new file mode 100644 index 0000000..9b7946d --- /dev/null +++ b/arch/x86_64/src/vga/text.cpp @@ -0,0 +1,66 @@ +#include "x86_64/vga/text.hpp" + +#include "arch/asm_pointer.hpp" +#include "x86_64/vga/io.hpp" + +#include +#include +#include +#include + +extern "C" teachos::arch::asm_pointer> vga_buffer_pointer; + +namespace teachos::x86_64::vga::text +{ + namespace + { + // auto constexpr DEFAULT_VGA_TEXT_BUFFER_ADDRESS = 0xB8000; + + auto buffer_offset = std::ptrdiff_t{}; + + auto constexpr DEFAULT_TEXT_BUFFER_WIDTH = 80U; + auto constexpr DEFAULT_TEXT_BUFFER_HEIGHT = 25U; + } // namespace + + auto clear(attribute attribute) -> void + { + buffer_offset = 0; + std::ranges::fill_n(vga_buffer_pointer.get(), 2000, std::pair{' ', attribute}); + } + + auto cursor(bool enabled) -> void + { + auto cursor_disable_byte = std::byte{!enabled} << 5; + + io::crtc::address_port::write(io::crtc::registers::cursor_start); + io::crtc::data_port::write(io::crtc::data_port::read() | cursor_disable_byte); + } + + auto newline() -> void + { + auto current_line = buffer_offset / DEFAULT_TEXT_BUFFER_WIDTH; + auto next_line = current_line + 1; + + if (next_line >= DEFAULT_TEXT_BUFFER_HEIGHT) + { + auto begin = vga_buffer_pointer + DEFAULT_TEXT_BUFFER_WIDTH; + auto end = vga_buffer_pointer + DEFAULT_TEXT_BUFFER_WIDTH * DEFAULT_TEXT_BUFFER_HEIGHT; + std::ranges::move(begin, end, vga_buffer_pointer.get()); + buffer_offset = current_line * DEFAULT_TEXT_BUFFER_WIDTH; + } + else + { + buffer_offset = next_line * DEFAULT_TEXT_BUFFER_WIDTH; + } + } + + auto write_char(char code_point, attribute attribute) -> void + { + vga_buffer_pointer[buffer_offset++] = std::pair{code_point, attribute}; + }; + + auto write(std::string_view code_points, attribute attribute) -> void + { + std::ranges::for_each(code_points, [&](auto code_point) { write_char(code_point, attribute); }); + } +} // namespace teachos::x86_64::vga::text diff --git a/arch/x86_64/src/video/vga/text.cpp b/arch/x86_64/src/video/vga/text.cpp deleted file mode 100644 index b070a0a..0000000 --- a/arch/x86_64/src/video/vga/text.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "arch/video/vga/text.hpp" - -#include "arch/video/vga/io.hpp" -#include "memory/asm_pointer.hpp" - -#include -#include -#include - -extern "C" std::pair * vga_buffer_pointer; - -namespace teachos::arch::video::vga::text -{ - namespace - { - auto constexpr DEFAULT_TEXT_BUFFER_WIDTH = 80U; - auto constexpr DEFAULT_TEXT_BUFFER_HEIGHT = 25U; - - auto constinit text_buffer = teachos::memory::asm_pointer{vga_buffer_pointer}; - } // namespace - - auto clear(attribute attribute) -> void - { - *text_buffer = reinterpret_cast(DEFAULT_VGA_TEXT_BUFFER_ADDRESS); - std::ranges::fill_n(*text_buffer, 2000, std::pair{' ', attribute}); - } - - auto cursor(bool enabled) -> void - { - auto cursor_disable_byte = std::byte{!enabled} << 5; - - crtc::address_port::write(crtc::registers::cursor_start); - crtc::data_port::write(vga::crtc::data_port::read() | cursor_disable_byte); - } - - auto newline() -> void - { - auto base = reinterpret_cast(DEFAULT_VGA_TEXT_BUFFER_ADDRESS); - auto & raw_buffer = *text_buffer; - auto current_line = (raw_buffer - base) / DEFAULT_TEXT_BUFFER_WIDTH; - auto next_line = current_line + 1; - - if (next_line >= DEFAULT_TEXT_BUFFER_HEIGHT) - { - auto begin = base + DEFAULT_TEXT_BUFFER_WIDTH; - auto end = base + DEFAULT_TEXT_BUFFER_WIDTH * DEFAULT_TEXT_BUFFER_HEIGHT; - std::ranges::move(begin, end, base); - raw_buffer = base + current_line * DEFAULT_TEXT_BUFFER_WIDTH; - } - else - { - raw_buffer = base + next_line * DEFAULT_TEXT_BUFFER_WIDTH; - } - } - - auto write_char(char code_point, attribute attribute) -> void - { - auto & p = *text_buffer; - (*p++) = std::pair{code_point, attribute}; - }; - - auto write(std::string_view code_points, attribute attribute) -> void - { - std::ranges::for_each(code_points, [&](auto code_point) { write_char(code_point, attribute); }); - } -} // namespace teachos::arch::video::vga::text -- cgit v1.2.3 From 05ac8c2bdd000d27b38411db2223eabb649c318f Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 14 Jul 2025 21:29:10 +0000 Subject: build: reintroduce bootable ISO --- arch/x86_64/src/boot/boot.s | 2 +- arch/x86_64/src/io.cpp | 14 ++++++++++---- arch/x86_64/src/vga/text.cpp | 2 -- 3 files changed, 11 insertions(+), 7 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 5488073..728380d 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -105,7 +105,7 @@ mesage_long_mode_not_supported: * We need a pointer to our current position in the VGA text buffer. */ .global vga_buffer_pointer -vga_buffer_pointer: .long 0xb8000 +vga_buffer_pointer: .quad 0xb8000 /** * Code for the bootstrapping process. diff --git a/arch/x86_64/src/io.cpp b/arch/x86_64/src/io.cpp index 5fb1c85..8e9e411 100644 --- a/arch/x86_64/src/io.cpp +++ b/arch/x86_64/src/io.cpp @@ -6,15 +6,21 @@ namespace teachos::arch::io auto init() -> void { + x86_64::vga::text::cursor(false); + teachos::set_print_handler( [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::green_on_black); }); - teachos::set_println_handler( - [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::green_on_black); }); + teachos::set_println_handler([](auto text) { + x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::green_on_black); + x86_64::vga::text::newline(); + }); teachos::set_print_error_handler( [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::red_on_black); }); - teachos::set_println_error_handler( - [](auto text) { return x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::red_on_black); }); + teachos::set_println_error_handler([](auto text) { + x86_64::vga::text::write(text, x86_64::vga::text::common_attributes::red_on_black); + x86_64::vga::text::newline(); + }); teachos::println("[x86-64] Basic VGA text output initialized."); } diff --git a/arch/x86_64/src/vga/text.cpp b/arch/x86_64/src/vga/text.cpp index 9b7946d..16abf08 100644 --- a/arch/x86_64/src/vga/text.cpp +++ b/arch/x86_64/src/vga/text.cpp @@ -14,8 +14,6 @@ namespace teachos::x86_64::vga::text { namespace { - // auto constexpr DEFAULT_VGA_TEXT_BUFFER_ADDRESS = 0xB8000; - auto buffer_offset = std::ptrdiff_t{}; auto constexpr DEFAULT_TEXT_BUFFER_WIDTH = 80U; -- cgit v1.2.3 From a832505d9696ae66248b53602d41637bef4868aa Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 18 Jul 2025 10:49:03 +0000 Subject: kernel: turn into a PIE --- arch/x86_64/src/boot/boot.s | 112 ++++++++++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 30 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 728380d..ada6426 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -71,16 +71,7 @@ stack_top: global_descriptor_table: .quad 0 global_descriptor_table_code = . - global_descriptor_table .quad (1<<43) | (1<<44) | (1<<47) | (1<<53) - -/** - * We also need a pointer that we can load into the GDTR. - * - * The pointer consists of a word describing the size of the table minus 1 and - * the pointer to the actual table. - */ -global_descriptor_table_pointer: -.word . - global_descriptor_table - 1 -.quad global_descriptor_table +global_descriptor_table_end: /** * We are going to print some messages in case we panic during boot, so we are @@ -130,7 +121,12 @@ _panic: push %ebp mov %esp, %ebp - push message_prefix_panic + call .Lpanic_get_ip +.Lpanic_get_ip: + pop %eax + lea (message_prefix_panic - .Lpanic_get_ip)(%eax), %eax + + push %eax push $0x4f call _print add $8, %esp @@ -155,10 +151,17 @@ _print: push %ebx push %esi + push %edi + + call .Lprint_get_ip +.Lprint_get_ip: + pop %edi + lea (vga_buffer_pointer - .Lprint_get_ip)(%edi), %edi + mov 8(%ebp), %eax mov 12(%ebp), %ebx mov $0, %ecx - mov (vga_buffer_pointer), %esi + mov (%edi), %esi .Lprint_loop: mov (%ebx, %ecx), %dl @@ -171,9 +174,11 @@ _print: .Lupdate_vga_buffer_address: shl $1, %ecx - add %ecx, (vga_buffer_pointer) + add %ecx, %esi + mov %esi, (%edi) .Lprint_end: + pop %edi pop %esi pop %ebx mov %ebp, %esp @@ -187,7 +192,12 @@ _print: */ .global _start _start: - mov $stack_top, %esp + call .Lstart_get_ip +.Lstart_get_ip: + pop %esi + lea (stack_top - .Lstart_get_ip)(%esi), %ebx + + mov %ebx, %esp mov %esp, %ebp call assert_loaded_by_multiboot2_loader @@ -197,8 +207,19 @@ _start: call enable_paging call enable_sse - lgdt (global_descriptor_table_pointer) - jmp $global_descriptor_table_code, $_transition_to_long_mode + sub $10, %esp + lea (global_descriptor_table - .Lstart_get_ip)(%esi), %eax + movw $(global_descriptor_table_end - global_descriptor_table -1), (%esp) + mov %eax, 2(%esp) + movl $0, 6(%esp) + + lgdt (%esp) + add $10, %esp + + lea (_transition_to_long_mode - .Lstart_get_ip)(%esi), %eax + pushl $global_descriptor_table_code + pushl %eax + lret call halt @@ -217,7 +238,11 @@ assert_cpu_supports_long_mode: jz .Llong_mode_assertion_failed ret .Llong_mode_assertion_failed: - push $mesage_long_mode_not_supported + call .Lassert_cpu_supports_long_mode_fail_get_ip +.Lassert_cpu_supports_long_mode_fail_get_ip: + pop %eax + lea (mesage_long_mode_not_supported - .Lassert_cpu_supports_long_mode_fail_get_ip)(%eax), %eax + push %eax call _panic /** @@ -245,7 +270,11 @@ assert_cpuid_instruction_is_supported: je .Lcpuid_assertion_fail ret .Lcpuid_assertion_fail: - push $message_cpuid_instruction_no_supported + call .Lassert_cpuid_instruction_is_supported_fail_get_ip +.Lassert_cpuid_instruction_is_supported_fail_get_ip: + pop %eax + lea (message_cpuid_instruction_no_supported - .Lassert_cpuid_instruction_is_supported_fail_get_ip)(%eax), %eax + push %eax call _panic /** @@ -259,10 +288,18 @@ assert_loaded_by_multiboot2_loader: cmp $0x36d76289, %eax /* Check if we received the expected magic */ jne .Lmultiboot2_assertion_fail /* Panic otherwise */ - mov %ebx, multiboot_information_pointer /* Store the MBI pointer */ + call .Lassert_loaded_by_multiboot2_loader_get_ip +.Lassert_loaded_by_multiboot2_loader_get_ip: + pop %eax + lea (multiboot_information_pointer - .Lassert_loaded_by_multiboot2_loader_get_ip)(%eax), %eax + mov %ebx, (%eax) /* Store the MBI pointer */ ret .Lmultiboot2_assertion_fail: - push $message_not_loaded_by_multiboot2 + call .Lassert_loaded_by_multiboot2_loader_fail_get_ip +.Lassert_loaded_by_multiboot2_loader_fail_get_ip: + pop %eax + lea (message_not_loaded_by_multiboot2 - .Lassert_loaded_by_multiboot2_loader_fail_get_ip)(%eax), %eax + push %eax call _panic /** @@ -272,7 +309,10 @@ assert_loaded_by_multiboot2_loader: * set up for use. */ enable_paging: - mov $page_map_level_4, %eax + call .Lenable_paging_get_ip +.Lenable_paging_get_ip: + pop %eax +lea (page_map_level_4 - .Lenable_paging_get_ip)(%eax), %eax mov %eax, %cr3 /* Enable Physical Address Extension */ @@ -316,37 +356,46 @@ enable_sse: * page map entries. */ prepare_page_maps: + call .Lprepare_page_maps_get_ip +.Lprepare_page_maps_get_ip: + pop %edi /* Map the P4 table recursively */ - mov $page_map_level_4, %eax + lea (page_map_level_4 - .Lprepare_page_maps_get_ip)(%edi), %eax + mov %eax, %ebx or $0b11, %eax /* Write present + writable flags into eax register */ - mov %eax, (page_map_level_4 + 511 * 8) + mov %eax, (511 * 8)(%ebx) /* Add an entry to the PML4, pointing to the PML3 */ - mov $page_map_level_3, %eax + lea (page_map_level_3 - .Lprepare_page_maps_get_ip)(%edi), %eax + lea (page_map_level_4 - .Lprepare_page_maps_get_ip)(%edi), %ebx or $0x3, %eax - mov %eax, (page_map_level_4 + ((0x0000000000100000 >> 39) & 0x1ff) * 8) + mov %eax, (((0x0000000000100000 >> 39) & 0x1ff) * 8)(%ebx) /* Add an entry to the PML3, pointing to the PML2 */ - mov $page_map_level_2, %eax + lea (page_map_level_2 - .Lprepare_page_maps_get_ip)(%edi), %eax + lea (page_map_level_3 - .Lprepare_page_maps_get_ip)(%edi), %ebx or $0x3, %eax - mov %eax, (page_map_level_3 + ((0x0000000000100000 >> 30) & 0x1ff) * 8) + mov %eax, (((0x0000000000100000 >> 30) & 0x1ff) * 8)(%ebx) xor %ecx, %ecx - mov $_end_linear, %esi + push %esi + lea (_end_linear - .Lprepare_page_maps_get_ip)(%edi), %esi shr $21, %esi add $2, %esi .Lmap_pages: + lea (page_map_level_2 - .Lprepare_page_maps_get_ip)(%edi), %ebx mov $(1 << 21), %eax mul %ecx or $((1 << 0) | (1 << 1) | (1 << 7)), %eax - mov %eax, page_map_level_2(,%ecx,8) + mov %eax, (%ebx,%ecx,8) inc %ecx cmp %esi, %ecx jne .Lmap_pages + pop %esi ret .section .boot_text, "ax", @progbits @@ -360,7 +409,10 @@ _transition_to_long_mode: mov %rax, %fs mov %rax, %gs - movl $0xb8000, (vga_buffer_pointer) + leaq vga_buffer_pointer(%rip), %rax + movl $0xb8000, (%rax) + + xor %rax, %rax call _init -- cgit v1.2.3 From 14ed096fc5de6844cb116f3319c0d03043d26ea2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 17 Jul 2025 21:09:02 +0000 Subject: x86-64: prepare new architecture --- arch/x86_64/src/boot/boot.s | 2 +- arch/x86_64/src/memory.cpp | 90 ++++++++++---------- .../x86_64/src/memory/allocator/physical_frame.cpp | 24 ------ arch/x86_64/src/memory/region_allocator.cpp | 95 ++++++++++++++++++++++ 4 files changed, 137 insertions(+), 74 deletions(-) delete mode 100644 arch/x86_64/src/memory/allocator/physical_frame.cpp create mode 100644 arch/x86_64/src/memory/region_allocator.cpp (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index ada6426..e0cff7c 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -12,7 +12,7 @@ * Reserve some space for the Multiboot 2 information pointer. */ .global multiboot_information_pointer -multiboot_information_pointer: .skip 4 +multiboot_information_pointer: .skip 8 /** * Align page maps to 4 KiB or the assembler code, will cause crashes when attempting to enable paging. diff --git a/arch/x86_64/src/memory.cpp b/arch/x86_64/src/memory.cpp index 245d7bd..2b16beb 100644 --- a/arch/x86_64/src/memory.cpp +++ b/arch/x86_64/src/memory.cpp @@ -1,62 +1,54 @@ #include "arch/memory.hpp" -// #include "noarch/error.hpp" -// #include "noarch/print.hpp" -// #include "x86_64/bootstrap/mutiboot.hpp" -// #include "x86_64/memory/frame_allocator.hpp" -// #include "x86_64/memory/mbi_frame_allocator.hpp" +#include "kern/error.hpp" -// #include -// #include -// #include -// #include +#include "arch/asm_pointer.hpp" +#include "x86_64/memory/region_allocator.hpp" -namespace teachos::arch::memory -{ +#include - // namespace - // { - // /** - // * @brief Remap the kernel according to the ELF information. - // * - // * After initial bootup, a basic identity mapping of the lower 1GiB is set up. This mapping allows execution - // from, - // * as well as read and write access to, the mapped memory. In order to protect the kernel, remap it according to - // the - // * information supplied by the ELF file. This means remapping code sections as read-only and data sections as - // * no-execute (and read only for .rodata). - // * - // * @param sections Information about the sections in the loaded kernel binary. - // * @param allocator The frame allocator used to create new page mappings. - // */ - // auto remap_kernel(elf::section_header_table const & sections, x86_64::memory::frame_allocator & allocator) -> - // void - // { - // static_cast(sections); - // static_cast(allocator); - // } - // } // namespace +#include - auto init() -> void - { - // kernel::println("Initializing memory"); +extern "C" teachos::arch::asm_pointer multiboot_information_pointer; - // if (!x86_64::bootstrap::multiboot_information_pointer->has()) - // { - // kernel::panic("Received no memory map from the boot loader!"); - // } +namespace teachos::arch::memory +{ + namespace + { + auto constinit is_initialized = std::atomic_flag{}; - // if (!x86_64::bootstrap::multiboot_information_pointer->has()) - // { - // kernel::panic("Received no ELF symbol information from the boot loader!"); - // } + // auto create_memory_information() -> x86_64::memory::region_allocator::memory_information { - // auto memory_map = x86_64::bootstrap::multiboot_information_pointer->memory_map(); - // auto elf_symbols = x86_64::bootstrap::multiboot_information_pointer->elf_symbols(); - // auto section_header_table = elf::section_header_table{elf_symbols.data(), elf::file_class::bits_64}; - // auto allocator = x86_64::memory::mbi_frame_allocator{memory_map, section_header_table}; + // }; + } // namespace - // remap_kernel(section_header_table, allocator); + auto init() -> void + { + if (is_initialized.test_and_set()) + { + teachos::panic("[x86_64] Memory management has already been initialized."); + } + + auto memory_map = multiboot_information_pointer->maybe_memory_map(); + if (!memory_map) + { + teachos::panic("[x86_64] No memory map available."); + } + + // auto mem_info = x86_64::memory::region_allocator::memory_information{}; + // auto allocator = x86_64::memory::region_allocator{mem_info}; + + // kernel::cpu::set_cr0_bit(kernel::cpu::cr0_flags::WRITE_PROTECT); + // kernel::cpu::set_efer_bit(kernel::cpu::efer_flags::NXE); + + // paging::kernel_mapper kernel(allocator, memory_information); + // kernel.remap_kernel(); + // video::vga::text::write("Kernel remapping successful", video::vga::text::common_attributes::green_on_black); + // video::vga::text::newline(); + + // remap_heap(heap::KERNEL_HEAP_START, heap::KERNEL_HEAP_SIZE); + // video::vga::text::write("Heap remapping successful", video::vga::text::common_attributes::green_on_black); + // video::vga::text::newline(); } } // namespace teachos::arch::memory diff --git a/arch/x86_64/src/memory/allocator/physical_frame.cpp b/arch/x86_64/src/memory/allocator/physical_frame.cpp deleted file mode 100644 index ec387a1..0000000 --- a/arch/x86_64/src/memory/allocator/physical_frame.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "arch/memory/allocator/physical_frame.hpp" - -namespace teachos::arch::memory::allocator -{ - auto physical_frame::containing_address(physical_address address) -> physical_frame - { - return physical_frame{address / PAGE_FRAME_SIZE}; - } - - auto physical_frame::start_address() const -> physical_address { return frame_number * PAGE_FRAME_SIZE; } - - auto physical_frame::operator++(int) -> physical_frame - { - physical_frame const old_value = *this; - ++frame_number; - return old_value; - } - - auto physical_frame::operator++() -> physical_frame & - { - ++frame_number; - return *this; - } -} // namespace teachos::arch::memory::allocator diff --git a/arch/x86_64/src/memory/region_allocator.cpp b/arch/x86_64/src/memory/region_allocator.cpp new file mode 100644 index 0000000..c9a98b4 --- /dev/null +++ b/arch/x86_64/src/memory/region_allocator.cpp @@ -0,0 +1,95 @@ +// #include "arch/memory/allocator/region_allocator.hpp" + +// #include "arch/exception_handling/assert.hpp" + +// #include +// #include +// #include + +#include "x86_64/memory/region_allocator.hpp" + +#include "x86_64/memory/address.hpp" +#include "x86_64/memory/frame.hpp" + +#include + +#include +#include + +namespace teachos::x86_64::memory +{ + namespace + { + auto constexpr last_frame(multiboot2::memory_map::region const & region) + { + return frame::containing(physical_address{region.base + region.size_in_B - 1}); + } + } // namespace + + region_allocator::region_allocator(memory_information const & mem_info) + : m_next_frame{} + , m_current_region{} + , m_memory_map{} + , m_kernel_start(frame::containing(mem_info.image_range.first)) + , m_kernel_end(frame::containing(mem_info.image_range.second)) + , m_multiboot_start(frame::containing(mem_info.mbi_range.first)) + , m_multiboot_end(frame::containing(mem_info.mbi_range.second)) + { + choose_next_area(); + } + + auto region_allocator::choose_next_area() -> void + { + m_current_region.reset(); + auto next_area_with_free_frames = + m_memory_map | std::views::filter(&multiboot2::memory_map::region::available) | + std::views::filter([next = m_next_frame](auto const & region) { return last_frame(region) >= next; }); + + auto lowest_region_with_free_frames = + std::ranges::min_element(next_area_with_free_frames, [](auto lhs, auto rhs) { return lhs.base < rhs.base; }); + + if (lowest_region_with_free_frames != next_area_with_free_frames.end()) + { + m_current_region = *lowest_region_with_free_frames; + auto start_frame = frame::containing(physical_address{m_current_region->base}); + if (m_next_frame < start_frame) + { + m_next_frame = start_frame; + } + } + } + + auto region_allocator::allocate_frame() -> std::optional + { + if (!m_current_region) + { + return {}; + } + + auto const end_address = physical_address{m_current_region->base + m_current_region->size_in_B - 1}; + auto end_frame = frame::containing(end_address); + + if (m_next_frame > end_frame) + { + choose_next_area(); + } + else if (m_next_frame >= m_kernel_start && m_next_frame <= m_kernel_end) + { + m_next_frame = m_kernel_end + 1; + } + else if (m_next_frame >= m_multiboot_start && m_next_frame <= m_multiboot_end) + { + m_next_frame = m_multiboot_end + 1; + } + else + { + auto allocated = m_next_frame; + ++m_next_frame; + return allocated; + } + + return allocate_frame(); + } + + auto region_allocator::deallocate_frame(frame const &) -> void {} +} // namespace teachos::x86_64::memory -- cgit v1.2.3 From 4ae38294b0db1870f82cc402dc4a8bb38cea4a67 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 18 Jul 2025 11:16:43 +0000 Subject: x86_64: don't lose multi boot information pointer Since the transition to a PIE, more registers are required to perform the relative lookups of data references. As part of that change, a subtle mistake was introduced in _start, overwriting the multiboot information pointer that gets handed to kernel by the boot loader in %ebx. --- arch/x86_64/src/boot/boot.s | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index e0cff7c..ba5c6f0 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -195,9 +195,9 @@ _start: call .Lstart_get_ip .Lstart_get_ip: pop %esi - lea (stack_top - .Lstart_get_ip)(%esi), %ebx + lea (stack_top - .Lstart_get_ip)(%esi), %ecx - mov %ebx, %esp + mov %ecx, %esp mov %esp, %ebp call assert_loaded_by_multiboot2_loader -- cgit v1.2.3 From fd6282947bb13af4cfff8cf2209c442b568275f3 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 18 Jul 2025 11:35:58 +0000 Subject: x86_64: add data segment to boot GDT --- arch/x86_64/src/boot/boot.s | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index ba5c6f0..2decf26 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -58,19 +58,22 @@ stack_top: .section .boot_rodata, "a", @progbits /** - * A valid Global Descriptor Table is still required in long mode. However, we - * only need a single entry for the "code segment", so we will setup a single - * segment table below. + * A valid Global Descriptor Table is still required in long mode. * + * Bit 41: "RW" in the access byte => mark the segment as readable (code) or writable (data). * Bit 43: "E" in the access byte => mark the segment as executable. * Bit 44: "S" in the access byte => mark the segment as code or data. * Bit 47: "P" in the access byte => mark the segment as being present. * Bit 53: "L" in the flags byte => mark the segment as being for long mode */ -global_descriptor_table: .quad 0 +global_descriptor_table: +global_descriptor_table_null = . - global_descriptor_table +.quad 0 global_descriptor_table_code = . - global_descriptor_table -.quad (1<<43) | (1<<44) | (1<<47) | (1<<53) +.quad (1<<41) | (1<<43) | (1<<44) | (1<<47) | (1<<53) +global_descriptor_table_data = . - global_descriptor_table +.quad (1<<41) | (1<<44) | (1<<47) global_descriptor_table_end: /** @@ -402,7 +405,7 @@ prepare_page_maps: .code64 _transition_to_long_mode: - xor %rax, %rax + mov $global_descriptor_table_data, %rax mov %rax, %ss mov %rax, %ds mov %rax, %es -- cgit v1.2.3 From 3a67a3e1088508148002e7c20befa571fb0b72c0 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 18 Jul 2025 11:44:15 +0000 Subject: x86_64: set GDT entries as accessed. --- arch/x86_64/src/boot/boot.s | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 2decf26..f3d9585 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -60,6 +60,7 @@ stack_top: /** * A valid Global Descriptor Table is still required in long mode. * + * Bit 41: "A" in the access byte => mark the segment as accessed. * Bit 41: "RW" in the access byte => mark the segment as readable (code) or writable (data). * Bit 43: "E" in the access byte => mark the segment as executable. * Bit 44: "S" in the access byte => mark the segment as code or data. @@ -71,9 +72,9 @@ global_descriptor_table: global_descriptor_table_null = . - global_descriptor_table .quad 0 global_descriptor_table_code = . - global_descriptor_table -.quad (1<<41) | (1<<43) | (1<<44) | (1<<47) | (1<<53) +.quad (1<<40) | (1<<41) | (1<<43) | (1<<44) | (1<<47) | (1<<53) global_descriptor_table_data = . - global_descriptor_table -.quad (1<<41) | (1<<44) | (1<<47) +.quad (1<<40) | (1<<41) | (1<<44) | (1<<47) global_descriptor_table_end: /** -- cgit v1.2.3 From eb22cdcad4c27527a63a6e457e80c752f76821c6 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 21 Jul 2025 12:13:10 +0000 Subject: x86_64: clean up bootstrap code --- arch/x86_64/src/boot/boot.s | 512 +++++++++++++++++++++++--------------------- 1 file changed, 265 insertions(+), 247 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index f3d9585..7a46795 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -1,50 +1,22 @@ -.extern _end_physical -.extern _init -.extern kernel_main - - /** - * Uninitialized data for the bootstrapping process. + * @brief Uninitialized data for the bootstrapping process. */ .section .boot_bss, "aw", @nobits /** - * Reserve some space for the Multiboot 2 information pointer. + * @brief Storage for the multiboot2 information pointer. */ .global multiboot_information_pointer multiboot_information_pointer: .skip 8 -/** - * Align page maps to 4 KiB or the assembler code, will cause crashes when attempting to enable paging. - */ .align 4096 -/** - * Reserve space for the page maps we are going to use during startup. - * - * Note: We are going to use large pages to make the initial mapping code - * simpler. - * - * We need: - * - A single PML 4 (since we will only use 4-level paging) - * - 1 PML 3 - * - 1 PML 2 - */ - -.global page_map_level_4 page_map_level_4: .skip 512 * 8 - -.global page_map_level_3 page_map_level_3: .skip 512 * 8 - -.global page_map_level_2 page_map_level_2: .skip 512 * 8 /** - * Stack space for the bootstrapping process. - * - * Note: We are going to reserve 1 MiB for now. If/when the kernel requires - * more space to run, it will have to relocate the stack. + * @brief Storage for the bootstrap stack. */ .section .boot_stack, "aw", @nobits .align 16 @@ -53,210 +25,240 @@ stack_bottom: .skip 1 << 20 stack_top: /** - * Constants for the bootstrapping process. + * @brief Constants for the bootstrapping process. */ .section .boot_rodata, "a", @progbits /** - * A valid Global Descriptor Table is still required in long mode. - * - * Bit 41: "A" in the access byte => mark the segment as accessed. - * Bit 41: "RW" in the access byte => mark the segment as readable (code) or writable (data). - * Bit 43: "E" in the access byte => mark the segment as executable. - * Bit 44: "S" in the access byte => mark the segment as code or data. - * Bit 47: "P" in the access byte => mark the segment as being present. - * Bit 53: "L" in the flags byte => mark the segment as being for long mode + * @brief A basic GDT for long mode. */ - global_descriptor_table: +.set GDT_ACCESSED, 40 +.set GDT_READ_WRITE, 41 +.set GDT_EXECUTABLE, 43 +.set GDT_DESCRIPTOR_TYPE, 44 +.set GDT_PRESENT, 47 +.set GDT_LONG_MODE, 53 global_descriptor_table_null = . - global_descriptor_table .quad 0 global_descriptor_table_code = . - global_descriptor_table -.quad (1<<40) | (1<<41) | (1<<43) | (1<<44) | (1<<47) | (1<<53) +.quad (1 << GDT_ACCESSED) | (1 << GDT_READ_WRITE) | (1 << GDT_EXECUTABLE) | (1 << GDT_DESCRIPTOR_TYPE) | (1 << GDT_PRESENT) | (1 << GDT_LONG_MODE) global_descriptor_table_data = . - global_descriptor_table -.quad (1<<40) | (1<<41) | (1<<44) | (1<<47) +.quad (1 << GDT_ACCESSED) | (1 << GDT_READ_WRITE) | (1 << GDT_DESCRIPTOR_TYPE) | (1 << GDT_PRESENT) global_descriptor_table_end: -/** - * We are going to print some messages in case we panic during boot, so we are - * going to store them here as well - */ -.global message_prefix_panic -message_prefix_panic: -.string "TeachOS Panic: " -message_not_loaded_by_multiboot2: -.string "The operating system was not loaded by a Multiboot 2 loader." -message_cpuid_instruction_no_supported: -.string "The 'cpuid' instruction is not supported on this platform." -mesage_long_mode_not_supported: -.string "Long mode is not supported by this platform." +message_prefix_panic: .string "Panic: " +message_not_loaded_by_multiboot2: .string "The operating system was not loaded by a Multiboot 2 loader." +message_cpuid_instruction_no_supported: .string "The 'cpuid' instruction is not supported on this platform." +message_long_mode_not_supported: .string "Long mode is not supported by this platform." +message_return_from_kernel_main: .string "Execution returned from kernel main." /** - * Mutable data for the bootstrapping process. + * @brief Initialized data for the bootstrapping process. */ .section .boot_data, "aw", @progbits /** - * We need a pointer to our current position in the VGA text buffer. + * @brief A pointer to the current position within the VGA text buffer. */ .global vga_buffer_pointer vga_buffer_pointer: .quad 0xb8000 /** - * Code for the bootstrapping process. + * @brief Code for the bootstrapping process. */ .section .boot_text, "ax", @progbits .align 16 .code32 -.global halt -halt: -1: - hlt - jmp 1b +.macro pie_base + push %esi + call 0f + 0: + pop %esi +.endm + +.macro function_start + push %ebp + mov %esp, %ebp +.endm + +.macro function_end + leave + ret +.endm + +.macro pie_function_start + function_start + pie_base +.endm + +.macro pie_function_end + pop %esi + function_end +.endm /** - * Print a given panic message and then halt the machine. + * @brief Prepare the environment and start the kernel. * - * Parameters: - * - [stack - 0] message: the message to print + * This function performs all necessary checks to ensure the system was loaded + * by the expected loader and supports all features required to run the kernel. + * If successful, it prepares the system by setting up memory virtualization + * and then start the kernel proper. + * + * @param %eax The Multiboot 2 magic marker. + * @param %ebx The Multiboot 2 information pointer. + * @return void This function does not return. */ -_panic: - push %ebp +.global _start +_start: + call 0f +0: + pop %esi + + lea (stack_top - 0b)(%esi), %ecx + + mov %ecx, %esp mov %esp, %ebp - call .Lpanic_get_ip -.Lpanic_get_ip: - pop %eax - lea (message_prefix_panic - .Lpanic_get_ip)(%eax), %eax + call _assert_loaded_by_multiboot2_loader + call _save_multiboot_information_pointer - push %eax - push $0x4f - call _print - add $8, %esp + call _assert_cpuid_instruction_is_supported + call _assert_cpu_supports_long_mode - push 8(%ebp) - push 0x4f - call _print - add $8, %esp + call _prepare_page_maps + call _enable_paging + call _enable_sse + call _reload_gdt + + lea (_transition_to_long_mode - 0b)(%esi), %eax + pushl $global_descriptor_table_code + pushl %eax + lret + +/** + * @brief Halt the system. + * + * This function will instruct the CPU to halt. It will try to keep the CPU + * halted, even if interrupts occur. + * + * @return This function never returns. + */ +.global halt +_halt: + function_start - call halt +1: + hlt + jmp 1b + + function_end /** - * Print a message via the VGA buffer. + * @brief Print a message via the VGA text buffer. * - * Parameters: - * - [stack - 4] message: the message to print - * - [stack - 0] color: the color of the message + * @param ebp+12 The message to print. + * @param ebp+8 The color to print the message in. */ _print: - push %ebp - mov %esp, %ebp + pie_function_start - push %ebx - push %esi push %edi + push %ebx - call .Lprint_get_ip -.Lprint_get_ip: - pop %edi - lea (vga_buffer_pointer - .Lprint_get_ip)(%edi), %edi - - mov 8(%ebp), %eax - mov 12(%ebp), %ebx + mov 8(%ebp), %al + mov 12(%ebp), %edx mov $0, %ecx - mov (%edi), %esi - -.Lprint_loop: - mov (%ebx, %ecx), %dl - test %dl, %dl - je .Lupdate_vga_buffer_address - mov %dl, (%esi, %ecx, 2) - mov %al, 1(%esi, %ecx, 2) + lea (vga_buffer_pointer - 0b)(%esi), %edi + mov (%edi), %edi + +1: + mov (%edx, %ecx), %bl + test %bl, %bl + je 2f + mov %bl, (%edi, %ecx, 2) + mov %al, 1(%edi, %ecx, 2) inc %ecx - jmp .Lprint_loop + jmp 1b -.Lupdate_vga_buffer_address: +2: shl $1, %ecx - add %ecx, %esi - mov %esi, (%edi) + add %ecx, %edi + lea (vga_buffer_pointer - 0b)(%esi), %ecx + mov %edi, (%ecx) -.Lprint_end: - pop %edi - pop %esi pop %ebx - mov %ebp, %esp - pop %ebp - ret + pop %edi + + pie_function_end /** - * This is our entry point after being loaded by the bootloader. + * @brief Print a given panic message and then halt the machine as if by calling ::halt() * - * Having this in assembly makes it easier for us to keep things together. + * @param ebp+4 A message to print. + * @return This function does not return. */ -.global _start -_start: - call .Lstart_get_ip -.Lstart_get_ip: - pop %esi - lea (stack_top - .Lstart_get_ip)(%esi), %ecx - - mov %ecx, %esp - mov %esp, %ebp +_panic: + pie_function_start - call assert_loaded_by_multiboot2_loader - call assert_cpuid_instruction_is_supported - call assert_cpu_supports_long_mode - call prepare_page_maps - call enable_paging - call enable_sse + lea (message_prefix_panic - 0b)(%esi), %eax - sub $10, %esp - lea (global_descriptor_table - .Lstart_get_ip)(%esi), %eax - movw $(global_descriptor_table_end - global_descriptor_table -1), (%esp) - mov %eax, 2(%esp) - movl $0, 6(%esp) + push %eax + push $0x4f + call _print - lgdt (%esp) - add $10, %esp + mov 16(%ebp), %eax + mov %eax, 8(%ebp) + call _print + add $8, %esp - lea (_transition_to_long_mode - .Lstart_get_ip)(%esi), %eax - pushl $global_descriptor_table_code - pushl %eax - lret + call _halt - call halt + pie_function_end /** - * Assert that the CPU supports going into long mode. + * Assert that we were loaded by a Multiboot 2 compliant bootloader. + * + * This assertion will panic the system if the magic signature was not found. + * If we were loaded my an appropriate bootloader, this function also saves + * the provided MBI pointer to `multiboot_information_pointer`. */ -assert_cpu_supports_long_mode: - mov $0x80000000, %eax - cpuid - cmp $0x80000001, %eax - jb .Llong_mode_assertion_failed +_assert_loaded_by_multiboot2_loader: + pie_function_start - mov $0x80000001, %eax - cpuid - test $(1 << 29), %edx - jz .Llong_mode_assertion_failed - ret -.Llong_mode_assertion_failed: - call .Lassert_cpu_supports_long_mode_fail_get_ip -.Lassert_cpu_supports_long_mode_fail_get_ip: - pop %eax - lea (mesage_long_mode_not_supported - .Lassert_cpu_supports_long_mode_fail_get_ip)(%eax), %eax + .set MULTIBOOT2_MAGIC, 0x36d76289 + cmp $MULTIBOOT2_MAGIC, %eax + je 1f + lea (message_not_loaded_by_multiboot2 - 0b)(%esi), %eax push %eax call _panic +1: + pie_function_end /** - * Assert that the CPU supports the CPUID instruction. + * @brief Store the multiboot 2 information pointer in the global memory. + * + * @return void + */ +_save_multiboot_information_pointer: + pie_function_start + + lea (multiboot_information_pointer - 0b)(%esi), %eax + mov %ebx, (%eax) + + pie_function_end + +/** + * @brief Assert that the CPU supports the CPUID instruction. * * The primary way to check for support of the instruction is to flip the ID * bin in EFLAGS and then check if this changed was accepted. If so, the CPU * supports the CPUID instruction, otherwise it most-likely doesn't. */ -assert_cpuid_instruction_is_supported: +_assert_cpuid_instruction_is_supported: + pie_function_start + pushfl pop %eax mov %eax, %ecx @@ -271,52 +273,100 @@ assert_cpuid_instruction_is_supported: popfl cmp %ecx, %eax - je .Lcpuid_assertion_fail - ret -.Lcpuid_assertion_fail: - call .Lassert_cpuid_instruction_is_supported_fail_get_ip -.Lassert_cpuid_instruction_is_supported_fail_get_ip: - pop %eax - lea (message_cpuid_instruction_no_supported - .Lassert_cpuid_instruction_is_supported_fail_get_ip)(%eax), %eax + jne 1f + lea (message_cpuid_instruction_no_supported - 0b)(%esi), %eax push %eax call _panic +1: + pie_function_end + /** - * Assert that we were loaded by a Multiboot 2 compliant bootloader. - * - * This assertion will panic the system if the magic signature was not found. - * If we were loaded my an appropriate bootloader, this function also saves - * the provided MBI pointer to `multiboot_information_pointer`. + * @brief Assert that the CPU supports going into long mode. */ -assert_loaded_by_multiboot2_loader: - cmp $0x36d76289, %eax /* Check if we received the - expected magic */ - jne .Lmultiboot2_assertion_fail /* Panic otherwise */ - call .Lassert_loaded_by_multiboot2_loader_get_ip -.Lassert_loaded_by_multiboot2_loader_get_ip: - pop %eax - lea (multiboot_information_pointer - .Lassert_loaded_by_multiboot2_loader_get_ip)(%eax), %eax - mov %ebx, (%eax) /* Store the MBI pointer */ - ret -.Lmultiboot2_assertion_fail: - call .Lassert_loaded_by_multiboot2_loader_fail_get_ip -.Lassert_loaded_by_multiboot2_loader_fail_get_ip: - pop %eax - lea (message_not_loaded_by_multiboot2 - .Lassert_loaded_by_multiboot2_loader_fail_get_ip)(%eax), %eax +_assert_cpu_supports_long_mode: + pie_function_start + + mov $0x80000000, %eax + cpuid + cmp $0x80000001, %eax + jb 1f + + mov $0x80000001, %eax + cpuid + test $(1 << 29), %edx + jnz 2f +1: + lea (message_long_mode_not_supported - 0b)(%esi), %eax push %eax call _panic +2: + pie_function_end /** - * Enable paging. + * @brief Prepare a recursive page map hierarchy + * + * We map all physical memory we were loaded in plus one additional page. The + * mapping is done in terms of huge pages (2 MiB per page) to save on required + * page map entries. + * + * @return void + */ +_prepare_page_maps: + pie_function_start + push %ebx + + .set HUG