From 6288868ebd728720236d6a857df2658bff2d6547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 29 Sep 2024 07:02:25 +0000 Subject: Pass multiboot info to main function --- arch/x86_64/include/arch/kernel/main.hpp | 2 +- arch/x86_64/src/kernel/main.cpp | 7 ++++++- src/kernel/main.cpp | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 6961594..57b9b58 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -5,7 +5,7 @@ namespace teachos::arch::kernel { - auto main() -> void; + auto main(size_t multiboot_information_address) -> void; } #endif \ No newline at end of file diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 0e90264..01c14a5 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -2,14 +2,19 @@ #include "arch/video/vga/text.hpp" +#include "stdio.h" + namespace teachos::arch::kernel { - auto main() -> void + auto main(size_t multiboot_information_address) -> void { using namespace video::vga; text::clear(); text::cursor(false); text::write("TeachOS is starting up...", text::common_attributes::green_on_black); + char address[32U] = {}; + snprintf(address, sizeof(address), "Multiboot address: (%lu)", multiboot_information_address); + text::write(address, text::common_attributes::green_on_black); } } // namespace teachos::arch::kernel diff --git a/src/kernel/main.cpp b/src/kernel/main.cpp index 07a9955..a1ef159 100644 --- a/src/kernel/main.cpp +++ b/src/kernel/main.cpp @@ -1,3 +1,6 @@ #include "arch/kernel/main.hpp" -extern "C" auto kernel_main() -> void { teachos::arch::kernel::main(); } \ No newline at end of file +extern "C" auto kernel_main(size_t multiboot_information_address) -> void +{ + teachos::arch::kernel::main(multiboot_information_address); +} -- cgit v1.2.3 From 4e991f05b8beb7538cee6939777f36610f8b7bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 29 Sep 2024 07:56:50 +0000 Subject: Pass multiboot to main from edi register --- arch/x86_64/src/boot/boot.s | 2 ++ arch/x86_64/src/kernel/main.cpp | 9 ++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 7b4e193..d41981a 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -366,5 +366,7 @@ _transition_to_long_mode: call _init + mov multiboot_information_pointer, %ebx + mov %ebx, %edi call kernel_main hlt diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 01c14a5..cb5092c 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -2,8 +2,6 @@ #include "arch/video/vga/text.hpp" -#include "stdio.h" - namespace teachos::arch::kernel { auto main(size_t multiboot_information_address) -> void @@ -13,8 +11,9 @@ namespace teachos::arch::kernel text::clear(); text::cursor(false); text::write("TeachOS is starting up...", text::common_attributes::green_on_black); - char address[32U] = {}; - snprintf(address, sizeof(address), "Multiboot address: (%lu)", multiboot_information_address); - text::write(address, text::common_attributes::green_on_black); + if (multiboot_information_address > 5) + { + return; + } } } // namespace teachos::arch::kernel -- cgit v1.2.3 From eeee7967c17704fee443a3b5b02d53a580f18b73 Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Sun, 29 Sep 2024 08:52:28 +0000 Subject: use multiboot_information_pointer public variable --- arch/x86_64/include/arch/boot/multiboot.hpp | 14 ++++++++++++++ arch/x86_64/include/arch/boot/pointers.hpp | 2 +- arch/x86_64/include/arch/kernel/main.hpp | 2 +- arch/x86_64/src/boot/boot.s | 2 -- arch/x86_64/src/kernel/main.cpp | 16 +++++++++++----- src/kernel/main.cpp | 5 +---- 6 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 arch/x86_64/include/arch/boot/multiboot.hpp diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp new file mode 100644 index 0000000..c6fed55 --- /dev/null +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -0,0 +1,14 @@ +#include + +struct multiboot_tag +{ + uint32_t type; + uint32_t size; +}; + +struct multiboot_info +{ + uint32_t total_size; + uint32_t reserved; + struct multiboot_tag tags[1]; // TODO: Size 0 +}; \ No newline at end of file diff --git a/arch/x86_64/include/arch/boot/pointers.hpp b/arch/x86_64/include/arch/boot/pointers.hpp index dcd14fe..c7424e2 100644 --- a/arch/x86_64/include/arch/boot/pointers.hpp +++ b/arch/x86_64/include/arch/boot/pointers.hpp @@ -5,7 +5,7 @@ namespace teachos::arch::boot { - extern "C" std::byte const multiboot_information_pointer; + extern "C" size_t const multiboot_information_pointer; } // namespace teachos::arch::boot #endif \ No newline at end of file diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 57b9b58..6961594 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -5,7 +5,7 @@ namespace teachos::arch::kernel { - auto main(size_t multiboot_information_address) -> void; + auto main() -> void; } #endif \ No newline at end of file diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index d41981a..7b4e193 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -366,7 +366,5 @@ _transition_to_long_mode: call _init - mov multiboot_information_pointer, %ebx - mov %ebx, %edi call kernel_main hlt diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index cb5092c..e77818e 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -1,19 +1,25 @@ #include "arch/kernel/main.hpp" +#include "arch/boot/multiboot.hpp" +#include "arch/boot/pointers.hpp" #include "arch/video/vga/text.hpp" namespace teachos::arch::kernel { - auto main(size_t multiboot_information_address) -> void + auto main() -> void { using namespace video::vga; + auto t = arch::boot::multiboot_information_pointer; + // auto multiboot_tag = (struct multiboot_tag *) ((uint8_t) t + 8); + // for (auto tag = multiboot_tag; tag->type != ) + + if (t == 300) + { + } + text::clear(); text::cursor(false); text::write("TeachOS is starting up...", text::common_attributes::green_on_black); - if (multiboot_information_address > 5) - { - return; - } } } // namespace teachos::arch::kernel diff --git a/src/kernel/main.cpp b/src/kernel/main.cpp index a1ef159..4799b29 100644 --- a/src/kernel/main.cpp +++ b/src/kernel/main.cpp @@ -1,6 +1,3 @@ #include "arch/kernel/main.hpp" -extern "C" auto kernel_main(size_t multiboot_information_address) -> void -{ - teachos::arch::kernel::main(multiboot_information_address); -} +extern "C" auto kernel_main() -> void { teachos::arch::kernel::main(); } -- cgit v1.2.3 From 8f91d0ef50e01440f7e6e9f4afa5887f6afefea1 Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Sun, 29 Sep 2024 09:26:17 +0000 Subject: read basic mem info --- arch/x86_64/include/arch/boot/multiboot.hpp | 38 +++++++++++++++++++++++++++-- arch/x86_64/src/kernel/main.cpp | 34 ++++++++++++++++++++------ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index c6fed55..9a0757e 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -10,5 +10,39 @@ struct multiboot_info { uint32_t total_size; uint32_t reserved; - struct multiboot_tag tags[1]; // TODO: Size 0 -}; \ No newline at end of file + /* + * field "tags" is an array of multiboot_tags, however the array is never + * being accessed by index and using an array definition with size 0 produces a compiler + * error. + */ + struct multiboot_tag tags; +}; + +/* + * Define all multiboot tag types to ther respective values + * The gnu boot information format is defined here: + * https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Boot-information-format + */ +#define MULTIBOOT_TAG_ALIGN 8 +#define MULTIBOOT_TAG_TYPE_END 0 +#define MULTIBOOT_TAG_TYPE_CMDLINE 1 +#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 +#define MULTIBOOT_TAG_TYPE_MODULE 3 +#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 +#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 +#define MULTIBOOT_TAG_TYPE_MMAP 6 +#define MULTIBOOT_TAG_TYPE_VBE 7 +#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 +#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9 +#define MULTIBOOT_TAG_TYPE_APM 10 +#define MULTIBOOT_TAG_TYPE_EFI32 11 +#define MULTIBOOT_TAG_TYPE_EFI64 12 +#define MULTIBOOT_TAG_TYPE_SMBIOS 13 +#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14 +#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15 +#define MULTIBOOT_TAG_TYPE_NETWORK 16 +#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17 +#define MULTIBOOT_TAG_TYPE_EFI_BS 18 +#define MULTIBOOT_TAG_TYPE_EFI32_IH 19 +#define MULTIBOOT_TAG_TYPE_EFI64_IH 20 +#define MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR 21 diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index e77818e..8ca577a 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -10,16 +10,34 @@ namespace teachos::arch::kernel { using namespace video::vga; - auto t = arch::boot::multiboot_information_pointer; - // auto multiboot_tag = (struct multiboot_tag *) ((uint8_t) t + 8); - // for (auto tag = multiboot_tag; tag->type != ) - - if (t == 300) - { - } - text::clear(); text::cursor(false); text::write("TeachOS is starting up...", text::common_attributes::green_on_black); + + auto mip = arch::boot::multiboot_information_pointer; + + // Address of the first multiboot tag + auto multiboot_tag = (struct multiboot_tag *)((uint8_t *)mip + 8); + + /* + * Loop over the multiboot2 tags to access the information needed. + * Tags are defined in the header. + */ + for (auto tag = multiboot_tag; tag->type != MULTIBOOT_TAG_TYPE_END; + tag = (struct multiboot_tag *)((uint8_t *)tag + ((tag->size + 7) & ~7))) + { + switch (tag->type) + { + case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: + uint32_t size = tag->size; + uint32_t mem_lower = *(uint32_t *)(&size + 32); + uint32_t mem_upper = *(uint32_t *)(&size + 64); + if (mem_lower > mem_upper) + { + } + text::write("BUFFER IS HERE", text::common_attributes::green_on_black); + break; + } + } } } // namespace teachos::arch::kernel -- cgit v1.2.3 From d2e1c8ba686d7d4ab32eda91c2f532676e9b8acf Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Sun, 29 Sep 2024 14:03:39 +0000 Subject: create write_number function --- arch/x86_64/include/arch/video/vga/text.hpp | 50 +++++++++++++++++++++++++++++ arch/x86_64/src/kernel/main.cpp | 10 +++--- arch/x86_64/src/video/vga/text.cpp | 15 ++++----- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/arch/x86_64/include/arch/video/vga/text.hpp b/arch/x86_64/include/arch/video/vga/text.hpp index 1e584d6..97344da 100644 --- a/arch/x86_64/include/arch/video/vga/text.hpp +++ b/arch/x86_64/include/arch/video/vga/text.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace teachos::arch::video::vga::text { @@ -108,6 +109,55 @@ namespace teachos::arch::video::vga::text * @see vga::text::attribute */ auto write(std::string_view code_points, attribute attribute) -> void; + + /** + * @brief Write a single character to the VGA text buffer. + * + * @note This function also updates the text mode buffer pointer. + * + * @param code_point A code point to write to the VGA text mode buffer. + * @param attribute The attribute to apply to the written sequence of code points. + * @see vga::text::attribute + */ + auto write_char(char code_point, attribute attribute) -> void; + + // TODO: Move concepts to their own file/folder + template + concept Integral = std::is_integral_v; + + /** + * @brief Write a integral value to the VGA text buffer. + * + * @note This function also updates the text mode buffer pointer. + * + * @param code_points A integral value to write to the VGA text mode buffer. + * @param attribute The attribute to apply to the written sequence of code points. + * @see vga::text::attribute + */ + template + auto write_number(T value, attribute attribute) -> void + { + T current_value = value; + + T divisor = 1; + while (current_value > 9) + { + divisor *= 10; + current_value = current_value / 10; + } + + current_value = value; + while (divisor > 0) + { + uint8_t quotient = current_value / divisor; + char ascii_digit = quotient + '0'; + + write_char(ascii_digit, attribute); + current_value %= divisor; + + divisor /= 10; + } + } } // namespace teachos::arch::video::vga::text #endif \ No newline at end of file diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 8ca577a..a5ffbb4 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -32,10 +32,12 @@ namespace teachos::arch::kernel uint32_t size = tag->size; uint32_t mem_lower = *(uint32_t *)(&size + 32); uint32_t mem_upper = *(uint32_t *)(&size + 64); - if (mem_lower > mem_upper) - { - } - text::write("BUFFER IS HERE", text::common_attributes::green_on_black); + + text::write_number(mem_lower, text::common_attributes::green_on_black); + text::write("Lower memory bound", text::common_attributes::green_on_black); + text::write_number(mem_lower, text::common_attributes::green_on_black); + text::write("Upper memory bound", text::common_attributes::green_on_black); + text::write_number(mem_upper, text::common_attributes::green_on_black); break; } } diff --git a/arch/x86_64/src/video/vga/text.cpp b/arch/x86_64/src/video/vga/text.cpp index f1e7412..bf60410 100644 --- a/arch/x86_64/src/video/vga/text.cpp +++ b/arch/x86_64/src/video/vga/text.cpp @@ -17,12 +17,6 @@ namespace teachos::arch::video::vga::text extern "C" std::pair * vga_buffer_pointer; auto constinit text_buffer = teachos::memory::asm_pointer{vga_buffer_pointer}; - - auto write(char code_point, attribute attribute) -> void - { - auto & p = *text_buffer; - (*p++) = std::pair{code_point, attribute}; - }; } // namespace auto clear(attribute attribute) -> void @@ -39,9 +33,14 @@ namespace teachos::arch::video::vga::text crtc::data_port::write(vga::crtc::data_port::read() | cursor_disable_byte); } + 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(code_point, attribute); }); + 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 20a5e5377c0f8e0769d67a7928891597bc463e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 30 Sep 2024 11:32:56 +0000 Subject: Attempt to print memory map --- arch/x86_64/include/arch/boot/multiboot.hpp | 11 +++++- arch/x86_64/include/arch/boot/pointers.hpp | 2 +- arch/x86_64/include/arch/io/port_io.hpp | 2 +- arch/x86_64/include/arch/kernel/main.hpp | 2 +- arch/x86_64/include/arch/video/vga/io.hpp | 4 +-- arch/x86_64/include/arch/video/vga/text.hpp | 3 +- arch/x86_64/src/kernel/main.cpp | 53 ++++++++++++++++++++++++----- arch/x86_64/src/video/vga/text.cpp | 1 - 8 files changed, 59 insertions(+), 19 deletions(-) diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index 9a0757e..4182a18 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -2,7 +2,8 @@ struct multiboot_tag { - uint32_t type; + uint16_t type; + uint16_t flags; uint32_t size; }; @@ -18,6 +19,14 @@ struct multiboot_info struct multiboot_tag tags; }; +struct memory_map_entry +{ + uint64_t base_addr; + uint64_t length; + uint32_t type; + uint32_t reserved; +}; + /* * Define all multiboot tag types to ther respective values * The gnu boot information format is defined here: diff --git a/arch/x86_64/include/arch/boot/pointers.hpp b/arch/x86_64/include/arch/boot/pointers.hpp index c7424e2..25800f4 100644 --- a/arch/x86_64/include/arch/boot/pointers.hpp +++ b/arch/x86_64/include/arch/boot/pointers.hpp @@ -8,4 +8,4 @@ namespace teachos::arch::boot extern "C" size_t const multiboot_information_pointer; } // namespace teachos::arch::boot -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/io/port_io.hpp b/arch/x86_64/include/arch/io/port_io.hpp index 5b61f90..c0f1ef3 100644 --- a/arch/x86_64/include/arch/io/port_io.hpp +++ b/arch/x86_64/include/arch/io/port_io.hpp @@ -131,4 +131,4 @@ namespace teachos::arch::io } // namespace teachos::arch::io -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 6961594..6759eb2 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -8,4 +8,4 @@ namespace teachos::arch::kernel auto main() -> void; } -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/video/vga/io.hpp b/arch/x86_64/include/arch/video/vga/io.hpp index 9226c5c..da9375d 100644 --- a/arch/x86_64/include/arch/video/vga/io.hpp +++ b/arch/x86_64/include/arch/video/vga/io.hpp @@ -7,10 +7,8 @@ namespace teachos::arch::video::vga { - namespace crtc { - /** * @brief The address port of the CRT Controller */ @@ -38,4 +36,4 @@ namespace teachos::arch::video::vga } // namespace teachos::arch::video::vga -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/video/vga/text.hpp b/arch/x86_64/include/arch/video/vga/text.hpp index 97344da..3b484e5 100644 --- a/arch/x86_64/include/arch/video/vga/text.hpp +++ b/arch/x86_64/include/arch/video/vga/text.hpp @@ -138,8 +138,8 @@ namespace teachos::arch::video::vga::text auto write_number(T value, attribute attribute) -> void { T current_value = value; - T divisor = 1; + while (current_value > 9) { divisor *= 10; @@ -154,7 +154,6 @@ namespace teachos::arch::video::vga::text write_char(ascii_digit, attribute); current_value %= divisor; - divisor /= 10; } } diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index a5ffbb4..6eb8521 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -6,6 +6,46 @@ namespace teachos::arch::kernel { + + auto print_meminfo(multiboot_tag * tag) -> void + { + using namespace video::vga; + + uint32_t * pointer = &tag->size; + uint32_t mem_lower = *(++pointer); + uint32_t mem_upper = *(++pointer); + + text::write("Lower memory bound: ", text::common_attributes::green_on_black); + text::write_number(mem_lower, text::common_attributes::green_on_black); + text::write("Upper memory bound: ", text::common_attributes::green_on_black); + text::write_number(mem_upper, text::common_attributes::green_on_black); + } + + auto print_memory_map(multiboot_tag * tag) -> void + { + using namespace video::vga; + + uint32_t * pointer = &tag->size; + uint32_t entry_size = *(++pointer); + uint32_t entry_version = *(++pointer); + text::write("Version: ", text::common_attributes::green_on_black); + text::write_number(entry_version, text::common_attributes::green_on_black); + + auto begin = (struct memory_map_entry *)++pointer; + auto end = begin + entry_size; + for (auto itr = begin; itr < end; ++itr) + { + text::write("Base Address: ", text::common_attributes::green_on_black); + text::write_number(itr->base_addr, text::common_attributes::green_on_black); + text::write("Length: ", text::common_attributes::green_on_black); + text::write_number(itr->length, text::common_attributes::green_on_black); + text::write("Type: ", text::common_attributes::green_on_black); + text::write_number(itr->type, text::common_attributes::green_on_black); + text::write("Reserved: ", text::common_attributes::green_on_black); + text::write_number(itr->reserved, text::common_attributes::green_on_black); + } + } + auto main() -> void { using namespace video::vga; @@ -29,15 +69,10 @@ namespace teachos::arch::kernel switch (tag->type) { case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: - uint32_t size = tag->size; - uint32_t mem_lower = *(uint32_t *)(&size + 32); - uint32_t mem_upper = *(uint32_t *)(&size + 64); - - text::write_number(mem_lower, text::common_attributes::green_on_black); - text::write("Lower memory bound", text::common_attributes::green_on_black); - text::write_number(mem_lower, text::common_attributes::green_on_black); - text::write("Upper memory bound", text::common_attributes::green_on_black); - text::write_number(mem_upper, text::common_attributes::green_on_black); + print_meminfo(tag); + break; + case MULTIBOOT_TAG_TYPE_MMAP: + print_memory_map(tag); break; } } diff --git a/arch/x86_64/src/video/vga/text.cpp b/arch/x86_64/src/video/vga/text.cpp index bf60410..a613c3b 100644 --- a/arch/x86_64/src/video/vga/text.cpp +++ b/arch/x86_64/src/video/vga/text.cpp @@ -10,7 +10,6 @@ namespace teachos::arch::video::vga::text { - namespace { auto constexpr default_text_buffer_address = 0xb8000; -- cgit v1.2.3 From b8e58d2f64fbb171b8687b9dd104ddd22fe4fc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 1 Oct 2024 07:42:09 +0000 Subject: Adjust printing of memory map --- arch/x86_64/include/arch/boot/multiboot.hpp | 11 +++++-- arch/x86_64/src/kernel/main.cpp | 46 ++++++++++------------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index 4182a18..c39081a 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -2,8 +2,7 @@ struct multiboot_tag { - uint16_t type; - uint16_t flags; + uint32_t type; uint32_t size; }; @@ -27,6 +26,14 @@ struct memory_map_entry uint32_t reserved; }; +struct memory_map_info +{ + multiboot_tag tag; + uint32_t entry_size; + uint32_t entry_version; + struct memory_map_entry entries; +}; + /* * Define all multiboot tag types to ther respective values * The gnu boot information format is defined here: diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 6eb8521..7107a36 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -6,43 +6,32 @@ namespace teachos::arch::kernel { - - auto print_meminfo(multiboot_tag * tag) -> void + auto print_memory_map(memory_map_info * mminfo) -> void { using namespace video::vga; - uint32_t * pointer = &tag->size; - uint32_t mem_lower = *(++pointer); - uint32_t mem_upper = *(++pointer); - - text::write("Lower memory bound: ", text::common_attributes::green_on_black); - text::write_number(mem_lower, text::common_attributes::green_on_black); - text::write("Upper memory bound: ", text::common_attributes::green_on_black); - text::write_number(mem_upper, text::common_attributes::green_on_black); - } - - auto print_memory_map(multiboot_tag * tag) -> void - { - using namespace video::vga; - - uint32_t * pointer = &tag->size; - uint32_t entry_size = *(++pointer); - uint32_t entry_version = *(++pointer); + uint32_t const entry_size = mminfo->entry_size; + uint32_t const entry_version = mminfo->entry_version; + text::write("Entry Size: ", text::common_attributes::green_on_black); + text::write_number(entry_size, text::common_attributes::green_on_black); text::write("Version: ", text::common_attributes::green_on_black); text::write_number(entry_version, text::common_attributes::green_on_black); - auto begin = (struct memory_map_entry *)++pointer; - auto end = begin + entry_size; - for (auto itr = begin; itr < end; ++itr) + uint32_t const remaining_size = mminfo->tag.size - (4 * sizeof(uint32_t)); + uint32_t const entry_amount = remaining_size / entry_size; + + auto begin = &mminfo->entries; + auto end = begin + entry_amount; + for (auto entry = begin; entry != end; ++entry) { text::write("Base Address: ", text::common_attributes::green_on_black); - text::write_number(itr->base_addr, text::common_attributes::green_on_black); + text::write_number(entry->base_addr, text::common_attributes::green_on_black); text::write("Length: ", text::common_attributes::green_on_black); - text::write_number(itr->length, text::common_attributes::green_on_black); + text::write_number(entry->length, text::common_attributes::green_on_black); text::write("Type: ", text::common_attributes::green_on_black); - text::write_number(itr->type, text::common_attributes::green_on_black); + text::write_number(entry->type, text::common_attributes::green_on_black); text::write("Reserved: ", text::common_attributes::green_on_black); - text::write_number(itr->reserved, text::common_attributes::green_on_black); + text::write_number(entry->reserved, text::common_attributes::green_on_black); } } @@ -68,11 +57,8 @@ namespace teachos::arch::kernel { switch (tag->type) { - case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: - print_meminfo(tag); - break; case MULTIBOOT_TAG_TYPE_MMAP: - print_memory_map(tag); + print_memory_map((struct memory_map_info *)tag); break; } } -- cgit v1.2.3 From e90fcb84fa43773d1e48bd82ce08381c6549a9cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 1 Oct 2024 08:22:40 +0000 Subject: Added efl section print method --- arch/x86_64/include/arch/boot/multiboot.hpp | 40 +++++++++++++++++++-- arch/x86_64/src/kernel/main.cpp | 54 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index c39081a..dfb289d 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -11,9 +11,9 @@ struct multiboot_info uint32_t total_size; uint32_t reserved; /* - * field "tags" is an array of multiboot_tags, however the array is never - * being accessed by index and using an array definition with size 0 produces a compiler - * error. + * field "tags" is an array of multiboot_tag, however the array is never + * being accessed by index we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. */ struct multiboot_tag tags; }; @@ -22,6 +22,11 @@ struct memory_map_entry { uint64_t base_addr; uint64_t length; +#define MULTIBOOT_MEMORY_AVAILABLE 1 +#define MULTIBOOT_MEMORY_RESERVED 2 +#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 +#define MULTIBOOT_MEMORY_NVS 4 +#define MULTIBOOT_MEMORY_BADRAM 5 uint32_t type; uint32_t reserved; }; @@ -31,9 +36,38 @@ struct memory_map_info multiboot_tag tag; uint32_t entry_size; uint32_t entry_version; + /* + * field "entries" is an array of memory_map_entry, however the array is never + * being accessed by index, we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. + */ struct memory_map_entry entries; }; +struct elf64_section_header +{ + uint32_t sh_name; + uint32_t sh_type; + uint64_t sh_flags; + uint64_t sh_addr; + uint64_t sh_offset; + uint64_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint64_t sh_addralign; + uint64_t sh_entsize; +}; + +struct elf_symbols_section +{ + multiboot_tag tag; + uint16_t num; + uint16_t entsize; + uint16_t shndx; + uint16_t reserved; + struct elf64_section_header sections; +}; + /* * Define all multiboot tag types to ther respective values * The gnu boot information format is defined here: diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 7107a36..2040fe3 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -35,6 +35,57 @@ namespace teachos::arch::kernel } } + auto print_elf_sections(elf_symbols_section * symbol) -> void + { + using namespace video::vga; + + uint16_t const num = symbol->num; + text::write("Number of entries: ", text::common_attributes::green_on_black); + text::write_number(num, text::common_attributes::green_on_black); + uint16_t const entsize = symbol->entsize; + text::write("Entry Size: ", text::common_attributes::green_on_black); + text::write_number(entsize, text::common_attributes::green_on_black); + uint16_t const shndx = symbol->shndx; + text::write("Section index: ", text::common_attributes::green_on_black); + text::write_number(shndx, text::common_attributes::green_on_black); + + auto begin = &symbol->sections; + auto end = begin + num; + for (auto section = begin; section != end; ++section) + { + uint32_t const sh_name = section->sh_name; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_name, text::common_attributes::green_on_black); + uint32_t const sh_type = section->sh_type; + text::write("Section type: ", text::common_attributes::green_on_black); + text::write_number(sh_type, text::common_attributes::green_on_black); + uint64_t const sh_flags = section->sh_flags; + text::write("Section flags: ", text::common_attributes::green_on_black); + text::write_number(sh_flags, text::common_attributes::green_on_black); + uint64_t const sh_addr = section->sh_addr; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_addr, text::common_attributes::green_on_black); + uint64_t const sh_offset = section->sh_offset; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_offset, text::common_attributes::green_on_black); + uint64_t const sh_size = section->sh_size; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_size, text::common_attributes::green_on_black); + uint32_t const sh_link = section->sh_link; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_link, text::common_attributes::green_on_black); + uint32_t const sh_info = section->sh_info; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_info, text::common_attributes::green_on_black); + uint64_t const sh_addralign = section->sh_addralign; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_addralign, text::common_attributes::green_on_black); + uint64_t const sh_entsize = section->sh_entsize; + text::write("Section name: ", text::common_attributes::green_on_black); + text::write_number(sh_entsize, text::common_attributes::green_on_black); + } + } + auto main() -> void { using namespace video::vga; @@ -57,6 +108,9 @@ namespace teachos::arch::kernel { switch (tag->type) { + case MULTIBOOT_TAG_TYPE_ELF_SECTIONS: + print_elf_sections((struct elf_symbols_section *)tag); + break; case MULTIBOOT_TAG_TYPE_MMAP: print_memory_map((struct memory_map_info *)tag); break; -- cgit v1.2.3 From 4c60bad3150b07e973eb385613a90ebb8c94ecac Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Tue, 1 Oct 2024 11:59:40 +0000 Subject: add structs, clean mip code --- arch/x86_64/include/arch/boot/multiboot.hpp | 86 +++++++++++++++++++++++++---- arch/x86_64/src/kernel/main.cpp | 60 +++++++++++++++----- 2 files changed, 120 insertions(+), 26 deletions(-) diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index dfb289d..d3787f2 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -6,6 +6,14 @@ struct multiboot_tag uint32_t size; }; +struct basic_memory_info +{ + uint32_t type; + uint32_t size; + uint32_t mem_lower; + uint32_t mem_upper; +}; + struct multiboot_info { uint32_t total_size; @@ -44,18 +52,72 @@ struct memory_map_info struct memory_map_entry entries; }; -struct elf64_section_header +#define EI_NIDENT 16 + +/* ELF standard typedefs (yet more proof that was way overdue) */ +typedef uint16_t Elf64_Half; +typedef int16_t Elf64_SHalf; +typedef uint32_t Elf64_Word; +typedef int32_t Elf64_Sword; +typedef uint64_t Elf64_Xword; +typedef int64_t Elf64_Sxword; + +typedef uint64_t Elf64_Off; +typedef uint64_t Elf64_Addr; +typedef uint16_t Elf64_Section; + +struct executable_header +{ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ +}; + +struct program_header +{ + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ +}; + +struct elf_section_header +{ + Elf64_Word sh_name; /* Section name (string tbl index) */ + Elf64_Word sh_type; /* Section type */ + Elf64_Xword sh_flags; /* Section flags */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Section size in bytes */ + Elf64_Word sh_link; /* Link to another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ +}; + +struct elf_symbol { - uint32_t sh_name; - uint32_t sh_type; - uint64_t sh_flags; - uint64_t sh_addr; - uint64_t sh_offset; - uint64_t sh_size; - uint32_t sh_link; - uint32_t sh_info; - uint64_t sh_addralign; - uint64_t sh_entsize; + Elf64_Word st_name; /* Symbol name (string tbl index) */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf64_Section st_shndx; /* Section index */ + Elf64_Addr st_value; /* Symbol value */ + Elf64_Xword st_size; /* Symbol size */ }; struct elf_symbols_section @@ -65,7 +127,7 @@ struct elf_symbols_section uint16_t entsize; uint16_t shndx; uint16_t reserved; - struct elf64_section_header sections; + struct elf_section_header sections; }; /* diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 2040fe3..4deac6d 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -6,14 +6,28 @@ namespace teachos::arch::kernel { + auto print_mem_info(basic_memory_info * mem_info) -> void + { + using namespace video::vga; + + auto mem_lower = mem_info->mem_lower; + text::write("Lower memory (kB): ", text::common_attributes::green_on_black); + text::write_number(mem_lower, text::common_attributes::green_on_black); + + auto mem_upper = mem_info->mem_upper; + text::write("Upper memory (kB): ", text::common_attributes::green_on_black); + text::write_number(mem_upper, text::common_attributes::green_on_black); + } + auto print_memory_map(memory_map_info * mminfo) -> void { using namespace video::vga; uint32_t const entry_size = mminfo->entry_size; - uint32_t const entry_version = mminfo->entry_version; text::write("Entry Size: ", text::common_attributes::green_on_black); text::write_number(entry_size, text::common_attributes::green_on_black); + + uint32_t const entry_version = mminfo->entry_version; text::write("Version: ", text::common_attributes::green_on_black); text::write_number(entry_version, text::common_attributes::green_on_black); @@ -26,10 +40,13 @@ namespace teachos::arch::kernel { text::write("Base Address: ", text::common_attributes::green_on_black); text::write_number(entry->base_addr, text::common_attributes::green_on_black); + text::write("Length: ", text::common_attributes::green_on_black); text::write_number(entry->length, text::common_attributes::green_on_black); + text::write("Type: ", text::common_attributes::green_on_black); text::write_number(entry->type, text::common_attributes::green_on_black); + text::write("Reserved: ", text::common_attributes::green_on_black); text::write_number(entry->reserved, text::common_attributes::green_on_black); } @@ -42,9 +59,11 @@ namespace teachos::arch::kernel uint16_t const num = symbol->num; text::write("Number of entries: ", text::common_attributes::green_on_black); text::write_number(num, text::common_attributes::green_on_black); + uint16_t const entsize = symbol->entsize; text::write("Entry Size: ", text::common_attributes::green_on_black); text::write_number(entsize, text::common_attributes::green_on_black); + uint16_t const shndx = symbol->shndx; text::write("Section index: ", text::common_attributes::green_on_black); text::write_number(shndx, text::common_attributes::green_on_black); @@ -56,32 +75,41 @@ namespace teachos::arch::kernel uint32_t const sh_name = section->sh_name; text::write("Section name: ", text::common_attributes::green_on_black); text::write_number(sh_name, text::common_attributes::green_on_black); + uint32_t const sh_type = section->sh_type; text::write("Section type: ", text::common_attributes::green_on_black); text::write_number(sh_type, text::common_attributes::green_on_black); + uint64_t const sh_flags = section->sh_flags; text::write("Section flags: ", text::common_attributes::green_on_black); text::write_number(sh_flags, text::common_attributes::green_on_black); + uint64_t const sh_addr = section->sh_addr; - text::write("Section name: ", text::common_attributes::green_on_black); + text::write("Section address: ", text::common_attributes::green_on_black); text::write_number(sh_addr, text::common_attributes::green_on_black); + uint64_t const sh_offset = section->sh_offset; - text::write("Section name: ", text::common_attributes::green_on_black); + text::write("Section offset: ", text::common_attributes::green_on_black); text::write_number(sh_offset, text::common_attributes::green_on_black); + uint64_t const sh_size = section->sh_size; - text::write("Section name: ", text::common_attributes::green_on_black); + text::write("Section size: ", text::common_attributes::green_on_black); text::write_number(sh_size, text::common_attributes::green_on_black); + uint32_t const sh_link = section->sh_link; - text::write("Section name: ", text::common_attributes::green_on_black); + text::write("Section link: ", text::common_attributes::green_on_black); text::write_number(sh_link, text::common_attributes::green_on_black); + uint32_t const sh_info = section->sh_info; - text::write("Section name: ", text::common_attributes::green_on_black); + text::write("Section info: ", text::common_attributes::green_on_black); text::write_number(sh_info, text::common_attributes::green_on_black); + uint64_t const sh_addralign = section->sh_addralign; - text::write("Section name: ", text::common_attributes::green_on_black); + text::write("Section address align: ", text::common_attributes::green_on_black); text::write_number(sh_addralign, text::common_attributes::green_on_black); + uint64_t const sh_entsize = section->sh_entsize; - text::write("Section name: ", text::common_attributes::green_on_black); + text::write("Section entry size: ", text::common_attributes::green_on_black); text::write_number(sh_entsize, text::common_attributes::green_on_black); } } @@ -94,20 +122,24 @@ namespace teachos::arch::kernel text::cursor(false); text::write("TeachOS is starting up...", text::common_attributes::green_on_black); - auto mip = arch::boot::multiboot_information_pointer; - - // Address of the first multiboot tag - auto multiboot_tag = (struct multiboot_tag *)((uint8_t *)mip + 8); + multiboot_info * multiboot_information_pointer = (multiboot_info *)arch::boot::multiboot_information_pointer; + auto multiboot_tag = &(multiboot_information_pointer->tags); /* * Loop over the multiboot2 tags to access the information needed. - * Tags are defined in the header. + * Tags are defined in the header file and are padded so that each + * Tag starts at an 8-bytes aligned adress. + * + * The increment part aligns the size to an 8-byte address. */ for (auto tag = multiboot_tag; tag->type != MULTIBOOT_TAG_TYPE_END; - tag = (struct multiboot_tag *)((uint8_t *)tag + ((tag->size + 7) & ~7))) + tag = (struct multiboot_tag *)(((uint8_t *)tag) + ((tag->size + 7) & ~7))) { switch (tag->type) { + case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: + print_mem_info((struct basic_memory_info *)tag); + break; case MULTIBOOT_TAG_TYPE_ELF_SECTIONS: print_elf_sections((struct elf_symbols_section *)tag); break; -- cgit v1.2.3 From 32add45849744dc976c7af6ec24f985bbace47d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 2 Oct 2024 13:02:35 +0000 Subject: Creating base frame allocation code --- arch/x86_64/include/arch/kernel/main.hpp | 2 - .../x86_64/include/arch/memory/frame_allocator.hpp | 48 ++++++++++++++++++++++ arch/x86_64/src/kernel/main.cpp | 6 +++ arch/x86_64/src/memory/frame_allocator.cpp | 9 ++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 arch/x86_64/include/arch/memory/frame_allocator.hpp create mode 100644 arch/x86_64/src/memory/frame_allocator.cpp diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 6759eb2..8813b46 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -1,8 +1,6 @@ #ifndef TEACHOS_ARCH_X86_64_KERNEL_MAIN_HPP #define TEACHOS_ARCH_X86_64_KERNEL_MAIN_HPP -#include - namespace teachos::arch::kernel { auto main() -> void; diff --git a/arch/x86_64/include/arch/memory/frame_allocator.hpp b/arch/x86_64/include/arch/memory/frame_allocator.hpp new file mode 100644 index 0000000..bcb0882 --- /dev/null +++ b/arch/x86_64/include/arch/memory/frame_allocator.hpp @@ -0,0 +1,48 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP + +#include +#include + +namespace teachos::arch::memory +{ + namespace + { + const size_t PAGE_FRAME_SIZE = 4096U; + } + + struct Frame + { + size_t frame_number; + + Frame(size_t frame_number) + : frame_number(frame_number) + { + // Nothing to do + } + + auto containing_address(size_t address) -> Frame { return Frame{address / PAGE_FRAME_SIZE}; } + }; + + struct IFrameAllocator + { + virtual auto allocate_frame() -> std::optional = 0; + virtual auto deallocate_frame(Frame frame) -> void = 0; + }; + + struct AreaFrameAllocator : public IFrameAllocator + { + Frame next_free_frame; + // std::optional current_area; + // MemoryArea * areas; + Frame kernel_start; + Frame kernel_end; + Frame multiboot_start; + Frame multiboot_end; + + auto allocate_frame() -> std::optional override; + auto deallocate_frame(Frame frame) -> void override; + }; +} // namespace teachos::arch::memory + +#endif // TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 4deac6d..da496a6 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -125,6 +125,12 @@ namespace teachos::arch::kernel multiboot_info * multiboot_information_pointer = (multiboot_info *)arch::boot::multiboot_information_pointer; auto multiboot_tag = &(multiboot_information_pointer->tags); + text::write("Multiboot Start: ", text::common_attributes::green_on_black); + text::write_number(arch::boot::multiboot_information_pointer, text::common_attributes::green_on_black); + text::write("Multiboot End: ", text::common_attributes::green_on_black); + text::write_number(arch::boot::multiboot_information_pointer + multiboot_information_pointer->total_size, + text::common_attributes::green_on_black); + /* * Loop over the multiboot2 tags to access the information needed. * Tags are defined in the header file and are padded so that each diff --git a/arch/x86_64/src/memory/frame_allocator.cpp b/arch/x86_64/src/memory/frame_allocator.cpp new file mode 100644 index 0000000..425528a --- /dev/null +++ b/arch/x86_64/src/memory/frame_allocator.cpp @@ -0,0 +1,9 @@ +#include "arch/memory/frame_allocator.hpp" + +namespace teachos::arch::memory +{ + auto AreaFrameAllocator::allocate_frame() -> std::optional {} + + auto AreaFrameAllocator::deallocate_frame(Frame frame) -> void {} + +} // namespace teachos::arch::memory -- cgit v1.2.3 From c2c7ad21fdd42111180d317ef50b15b7404769d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 2 Oct 2024 13:32:28 +0000 Subject: Improve naming, add enums and move into namespace --- arch/x86_64/include/arch/boot/multiboot.hpp | 160 ----------------------- arch/x86_64/include/arch/memory/multiboot.hpp | 174 ++++++++++++++++++++++++++ arch/x86_64/src/kernel/main.cpp | 33 ++--- 3 files changed, 193 insertions(+), 174 deletions(-) delete mode 100644 arch/x86_64/include/arch/boot/multiboot.hpp create mode 100644 arch/x86_64/include/arch/memory/multiboot.hpp diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp deleted file mode 100644 index d3787f2..0000000 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ /dev/null @@ -1,160 +0,0 @@ -#include - -struct multiboot_tag -{ - uint32_t type; - uint32_t size; -}; - -struct basic_memory_info -{ - uint32_t type; - uint32_t size; - uint32_t mem_lower; - uint32_t mem_upper; -}; - -struct multiboot_info -{ - uint32_t total_size; - uint32_t reserved; - /* - * field "tags" is an array of multiboot_tag, however the array is never - * being accessed by index we don't know the real size at compile-time, - * and using an array definition with size 0 produces a compiler error. - */ - struct multiboot_tag tags; -}; - -struct memory_map_entry -{ - uint64_t base_addr; - uint64_t length; -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 - uint32_t type; - uint32_t reserved; -}; - -struct memory_map_info -{ - multiboot_tag tag; - uint32_t entry_size; - uint32_t entry_version; - /* - * field "entries" is an array of memory_map_entry, however the array is never - * being accessed by index, we don't know the real size at compile-time, - * and using an array definition with size 0 produces a compiler error. - */ - struct memory_map_entry entries; -}; - -#define EI_NIDENT 16 - -/* ELF standard typedefs (yet more proof that was way overdue) */ -typedef uint16_t Elf64_Half; -typedef int16_t Elf64_SHalf; -typedef uint32_t Elf64_Word; -typedef int32_t Elf64_Sword; -typedef uint64_t Elf64_Xword; -typedef int64_t Elf64_Sxword; - -typedef uint64_t Elf64_Off; -typedef uint64_t Elf64_Addr; -typedef uint16_t Elf64_Section; - -struct executable_header -{ - unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ - Elf64_Half e_type; /* Object file type */ - Elf64_Half e_machine; /* Architecture */ - Elf64_Word e_version; /* Object file version */ - Elf64_Addr e_entry; /* Entry point virtual address */ - Elf64_Off e_phoff; /* Program header table file offset */ - Elf64_Off e_shoff; /* Section header table file offset */ - Elf64_Word e_flags; /* Processor-specific flags */ - Elf64_Half e_ehsize; /* ELF header size in bytes */ - Elf64_Half e_phentsize; /* Program header table entry size */ - Elf64_Half e_phnum; /* Program header table entry count */ - Elf64_Half e_shentsize; /* Section header table entry size */ - Elf64_Half e_shnum; /* Section header table entry count */ - Elf64_Half e_shstrndx; /* Section header string table index */ -}; - -struct program_header -{ - Elf64_Word p_type; /* Segment type */ - Elf64_Word p_flags; /* Segment flags */ - Elf64_Off p_offset; /* Segment file offset */ - Elf64_Addr p_vaddr; /* Segment virtual address */ - Elf64_Addr p_paddr; /* Segment physical address */ - Elf64_Xword p_filesz; /* Segment size in file */ - Elf64_Xword p_memsz; /* Segment size in memory */ - Elf64_Xword p_align; /* Segment alignment */ -}; - -struct elf_section_header -{ - Elf64_Word sh_name; /* Section name (string tbl index) */ - Elf64_Word sh_type; /* Section type */ - Elf64_Xword sh_flags; /* Section flags */ - Elf64_Addr sh_addr; /* Section virtual addr at execution */ - Elf64_Off sh_offset; /* Section file offset */ - Elf64_Xword sh_size; /* Section size in bytes */ - Elf64_Word sh_link; /* Link to another section */ - Elf64_Word sh_info; /* Additional section information */ - Elf64_Xword sh_addralign; /* Section alignment */ - Elf64_Xword sh_entsize; /* Entry size if section holds table */ -}; - -struct elf_symbol -{ - Elf64_Word st_name; /* Symbol name (string tbl index) */ - unsigned char st_info; /* Symbol type and binding */ - unsigned char st_other; /* Symbol visibility */ - Elf64_Section st_shndx; /* Section index */ - Elf64_Addr st_value; /* Symbol value */ - Elf64_Xword st_size; /* Symbol size */ -}; - -struct elf_symbols_section -{ - multiboot_tag tag; - uint16_t num; - uint16_t entsize; - uint16_t shndx; - uint16_t reserved; - struct elf_section_header sections; -}; - -/* - * Define all multiboot tag types to ther respective values - * The gnu boot information format is defined here: - * https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Boot-information-format - */ -#define MULTIBOOT_TAG_ALIGN 8 -#define MULTIBOOT_TAG_TYPE_END 0 -#define MULTIBOOT_TAG_TYPE_CMDLINE 1 -#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 -#define MULTIBOOT_TAG_TYPE_MODULE 3 -#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 -#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 -#define MULTIBOOT_TAG_TYPE_MMAP 6 -#define MULTIBOOT_TAG_TYPE_VBE 7 -#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 -#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9 -#define MULTIBOOT_TAG_TYPE_APM 10 -#define MULTIBOOT_TAG_TYPE_EFI32 11 -#define MULTIBOOT_TAG_TYPE_EFI64 12 -#define MULTIBOOT_TAG_TYPE_SMBIOS 13 -#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14 -#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15 -#define MULTIBOOT_TAG_TYPE_NETWORK 16 -#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17 -#define MULTIBOOT_TAG_TYPE_EFI_BS 18 -#define MULTIBOOT_TAG_TYPE_EFI32_IH 19 -#define MULTIBOOT_TAG_TYPE_EFI64_IH 20 -#define MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR 21 diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp new file mode 100644 index 0000000..95e6404 --- /dev/null +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -0,0 +1,174 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP + +#include + +namespace teachos::arch::memory +{ + /* + * Define all multiboot tag types to ther respective values + * The gnu boot information format is defined here: + * https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Boot-information-format + */ + enum class MultibootTagType : uint32_t + { + END, + CMDLINE, + BOOT_LOADER_NAME, + MODULE, + BASIC_MEMORY_INFO, + BOOTDEV, + MEMORY_MAP, + VBE, + FRAMEBUFFER, + ELF_SECTIONS, + APM, + EFI32, + EFI64, + SMBIOS, + ACPI_OLD, + ACPI_NEW, + NETWORK, + EFI_MEMORY_MAP, + EFI_BS, + EFI32_IH, + EFI64_IH, + LOAD_BASE_ADDRESS + }; + + struct MultibootTag + { + MultibootTagType type; + uint32_t size; + }; + + struct MemoryInfo + { + uint32_t type; + uint32_t size; + uint32_t mem_lower; + uint32_t mem_upper; + }; + + struct MultibootInfo + { + uint32_t total_size; + uint32_t reserved; + /* + * field "tags" is an array of MultibootTag, however the array is never + * being accessed by index we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. + */ + struct MultibootTag tags; + }; + + enum class MemoryAreaType : uint32_t + { + AVAILABLE = 1, + RESERVED, + ACPI_AVAILABLE, + RESERVED_HIBERNATION, + DEFECTIVE + }; + + struct MemoryArea + { + uint64_t base_addr; + uint64_t length; + MemoryAreaType type; + uint32_t reserved; + }; + + struct MemoryMap + { + MultibootTag tag; + uint32_t entry_size; + uint32_t entry_version; + /* + * field "entries" is an array of MemoryArea, however the array is never + * being accessed by index, we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. + */ + struct MemoryArea entries; + }; + +#define EI_NIDENT 16 + + /* ELF standard typedefs (yet more proof that was way overdue) */ + typedef uint16_t Elf64_Half; + typedef int16_t Elf64_SHalf; + typedef uint32_t Elf64_Word; + typedef int32_t Elf64_Sword; + typedef uint64_t Elf64_Xword; + typedef int64_t Elf64_Sxword; + + typedef uint64_t Elf64_Off; + typedef uint64_t Elf64_Addr; + typedef uint16_t Elf64_Section; + + struct executable_header + { + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ + }; + + struct program_header + { + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ + }; + + struct elf_section_header + { + Elf64_Word sh_name; /* Section name (string tbl index) */ + Elf64_Word sh_type; /* Section type */ + Elf64_Xword sh_flags; /* Section flags */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Section size in bytes */ + Elf64_Word sh_link; /* Link to another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ + }; + + struct elf_symbol + { + Elf64_Word st_name; /* Symbol name (string tbl index) */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf64_Section st_shndx; /* Section index */ + Elf64_Addr st_value; /* Symbol value */ + Elf64_Xword st_size; /* Symbol size */ + }; + + struct elf_symbols_section + { + MultibootTag tag; + uint16_t num; + uint16_t entsize; + uint16_t shndx; + uint16_t reserved; + struct elf_section_header sections; + }; +} // namespace teachos::arch::memory + +#endif // TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index da496a6..cffbfb8 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -1,12 +1,12 @@ #include "arch/kernel/main.hpp" -#include "arch/boot/multiboot.hpp" #include "arch/boot/pointers.hpp" +#include "arch/memory/multiboot.hpp" #include "arch/video/vga/text.hpp" namespace teachos::arch::kernel { - auto print_mem_info(basic_memory_info * mem_info) -> void + auto print_mem_info(arch::memory::MemoryInfo * mem_info) -> void { using namespace video::vga; @@ -19,7 +19,7 @@ namespace teachos::arch::kernel text::write_number(mem_upper, text::common_attributes::green_on_black); } - auto print_memory_map(memory_map_info * mminfo) -> void + auto print_memory_map(arch::memory::MemoryMap * mminfo) -> void { using namespace video::vga; @@ -45,14 +45,15 @@ namespace teachos::arch::kernel text::write_number(entry->length, text::common_attributes::green_on_black); text::write("Type: ", text::common_attributes::green_on_black); - text::write_number(entry->type, text::common_attributes::green_on_black); + text::write_number(static_cast::type>(entry->type), + text::common_attributes::green_on_black); text::write("Reserved: ", text::common_attributes::green_on_black); text::write_number(entry->reserved, text::common_attributes::green_on_black); } } - auto print_elf_sections(elf_symbols_section * symbol) -> void + auto print_elf_sections(arch::memory::elf_symbols_section * symbol) -> void { using namespace video::vga; @@ -122,7 +123,8 @@ namespace teachos::arch::kernel text::cursor(false); text::write("TeachOS is starting up...", text::common_attributes::green_on_black); - multiboot_info * multiboot_information_pointer = (multiboot_info *)arch::boot::multiboot_information_pointer; + arch::memory::MultibootInfo * multiboot_information_pointer = + (arch::memory::MultibootInfo *)arch::boot::multiboot_information_pointer; auto multiboot_tag = &(multiboot_information_pointer->tags); text::write("Multiboot Start: ", text::common_attributes::green_on_black); @@ -138,19 +140,22 @@ namespace teachos::arch::kernel * * The increment part aligns the size to an 8-byte address. */ - for (auto tag = multiboot_tag; tag->type != MULTIBOOT_TAG_TYPE_END; - tag = (struct multiboot_tag *)(((uint8_t *)tag) + ((tag->size + 7) & ~7))) + for (auto tag = multiboot_tag; tag->type != arch::memory::MultibootTagType::END; + tag = (arch::memory::MultibootTag *)(((uint8_t *)tag) + ((tag->size + 7) & ~7))) { switch (tag->type) { - case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: - print_mem_info((struct basic_memory_info *)tag); + case arch::memory::MultibootTagType::BASIC_MEMORY_INFO: + print_mem_info((arch::memory::MemoryInfo *)tag); break; - case MULTIBOOT_TAG_TYPE_ELF_SECTIONS: - print_elf_sections((struct elf_symbols_section *)tag); + case arch::memory::MultibootTagType::ELF_SECTIONS: + print_elf_sections((arch::memory::elf_symbols_section *)tag); break; - case MULTIBOOT_TAG_TYPE_MMAP: - print_memory_map((struct memory_map_info *)tag); + case arch::memory::MultibootTagType::MEMORY_MAP: + print_memory_map((arch::memory::MemoryMap *)tag); + break; + default: + // All other cases are not important and can be ignored break; } } -- cgit v1.2.3 From 1ab9c3c09d32283b39ca1026a9e29ada5ff7fda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 07:34:30 +0000 Subject: Renaming scheme --- .../x86_64/include/arch/memory/frame_allocator.hpp | 34 +++++++++++----------- arch/x86_64/include/arch/memory/multiboot.hpp | 30 +++++++++---------- arch/x86_64/include/arch/video/vga/text.hpp | 2 +- arch/x86_64/src/kernel/main.cpp | 24 +++++++-------- arch/x86_64/src/memory/frame_allocator.cpp | 4 +-- 5 files changed, 47 insertions(+), 47 deletions(-) diff --git a/arch/x86_64/include/arch/memory/frame_allocator.hpp b/arch/x86_64/include/arch/memory/frame_allocator.hpp index bcb0882..8dee848 100644 --- a/arch/x86_64/include/arch/memory/frame_allocator.hpp +++ b/arch/x86_64/include/arch/memory/frame_allocator.hpp @@ -11,37 +11,37 @@ namespace teachos::arch::memory const size_t PAGE_FRAME_SIZE = 4096U; } - struct Frame + struct frame { size_t frame_number; - Frame(size_t frame_number) + frame(size_t frame_number) : frame_number(frame_number) { // Nothing to do } - auto containing_address(size_t address) -> Frame { return Frame{address / PAGE_FRAME_SIZE}; } + auto containing_address(size_t address) -> frame { return frame{address / PAGE_FRAME_SIZE}; } }; - struct IFrameAllocator + struct frame_allocator { - virtual auto allocate_frame() -> std::optional = 0; - virtual auto deallocate_frame(Frame frame) ->