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/src/kernel/main.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src') 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 -- 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(-) (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 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/src/boot/boot.s | 2 -- arch/x86_64/src/kernel/main.cpp | 16 +++++++++++----- 2 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 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 -- 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/src/kernel/main.cpp | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/src') 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/src/kernel/main.cpp | 10 ++++++---- arch/x86_64/src/video/vga/text.cpp | 15 +++++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'arch/x86_64/src') 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/src/kernel/main.cpp | 53 +++++++++++++++++++++++++++++++------- arch/x86_64/src/video/vga/text.cpp | 1 - 2 files changed, 44 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/src') 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/src/kernel/main.cpp | 46 ++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 30 deletions(-) (limited to 'arch/x86_64/src') 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/src/kernel/main.cpp | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'arch/x86_64/src') 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/src/kernel/main.cpp | 60 +++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 14 deletions(-) (limited to 'arch/x86_64/src') 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/src/kernel/main.cpp | 6 ++++++ arch/x86_64/src/memory/frame_allocator.cpp | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 arch/x86_64/src/memory/frame_allocator.cpp (limited to 'arch/x86_64/src') 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/src/kernel/main.cpp | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'arch/x86_64/src') 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 --- arch/x86_64/src/kernel/main.cpp | 24 ++++++++++++------------ arch/x86_64/src/memory/frame_allocator.cpp | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index cffbfb8..4d6296a 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -6,7 +6,7 @@ namespace teachos::arch::kernel { - auto print_mem_info(arch::memory::MemoryInfo * mem_info) -> void + auto print_mem_info(arch::memory::memory_info * 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(arch::memory::MemoryMap * mminfo) -> void + auto print_memory_map(arch::memory::memory_map * mminfo) -> void { using namespace video::vga; @@ -45,7 +45,7 @@ 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(static_cast::type>(entry->type), + text::write_number(static_cast::type>(entry->type), text::common_attributes::green_on_black); text::write("Reserved: ", text::common_attributes::green_on_black); @@ -123,8 +123,8 @@ namespace teachos::arch::kernel text::cursor(false); text::write("TeachOS is starting up...", text::common_attributes::green_on_black); - arch::memory::MultibootInfo * multiboot_information_pointer = - (arch::memory::MultibootInfo *)arch::boot::multiboot_information_pointer; + arch::memory::multi_boot_info * multiboot_information_pointer = + (arch::memory::multi_boot_info *)arch::boot::multiboot_information_pointer; auto multiboot_tag = &(multiboot_information_pointer->tags); text::write("Multiboot Start: ", text::common_attributes::green_on_black); @@ -140,19 +140,19 @@ namespace teachos::arch::kernel * * The increment part aligns the size to an 8-byte address. */ - for (auto tag = multiboot_tag; tag->type != arch::memory::MultibootTagType::END; - tag = (arch::memory::MultibootTag *)(((uint8_t *)tag) + ((tag->size + 7) & ~7))) + for (auto tag = multiboot_tag; tag->type != arch::memory::multi_boot_tag_type::END; + tag = (arch::memory::multi_boot_tag *)(((uint8_t *)tag) + ((tag->size + 7) & ~7))) { switch (tag->type) { - case arch::memory::MultibootTagType::BASIC_MEMORY_INFO: - print_mem_info((arch::memory::MemoryInfo *)tag); + case arch::memory::multi_boot_tag_type::BASIC_MEMORY_INFO: + print_mem_info((arch::memory::memory_info *)tag); break; - case arch::memory::MultibootTagType::ELF_SECTIONS: + case arch::memory::multi_boot_tag_type::ELF_SECTIONS: print_elf_sections((arch::memory::elf_symbols_section *)tag); break; - case arch::memory::MultibootTagType::MEMORY_MAP: - print_memory_map((arch::memory::MemoryMap *)tag); + case arch::memory::multi_boot_tag_type::MEMORY_MAP: + print_memory_map((arch::memory::memory_map *)tag); break; default: // All other cases are not important and can be ignored diff --git a/arch/x86_64/src/memory/frame_allocator.cpp b/arch/x86_64/src/memory/frame_allocator.cpp index 425528a..b9d26a5 100644 --- a/arch/x86_64/src/memory/frame_allocator.cpp +++ b/arch/x86_64/src/memory/frame_allocator.cpp @@ -2,8 +2,8 @@ namespace teachos::arch::memory { - auto AreaFrameAllocator::allocate_frame() -> std::optional {} + auto area_frame_allocator::allocate_frame() -> std::optional {} - auto AreaFrameAllocator::deallocate_frame(Frame frame) -> void {} + auto area_frame_allocator::deallocate_frame(frame frame) -> void {} } // namespace teachos::arch::memory -- cgit v1.2.3 From f7df7167f0c54bd8da79dbf2d48bda5d7491fd32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 08:45:04 +0000 Subject: Remove high memory kernel and needless prints --- arch/x86_64/src/boot/boot.s | 24 +------- arch/x86_64/src/kernel/main.cpp | 120 ++++++++++++---------------------------- 2 files changed, 38 insertions(+), 106 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 7b4e193..0c21c66 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -17,8 +17,8 @@ * * We need: * - A single PML 4 (since we will only use 4-level paging) - * - 2 PML 3s (since we need to map high (-2GiB) and low (1+MiB) memory) - * - 2 PML 2s (since we need to map high (-2GiB) and low (1+MiB) memory) + * - 2 PML 3s (since we need to map low (1+MiB) memory) + * - 2 PML 2s (since we need to map low (1+MiB) memory) */ .global page_map_level_4 @@ -26,13 +26,9 @@ page_map_level_4: .skip 512 * 8 .global page_map_level_3_low page_map_level_3_low: .skip 512 * 8 -.global page_map_level_3_high -page_map_level_3_high: .skip 512 * 8 .global page_map_level_2_low page_map_level_2_low: .skip 512 * 8 -.global page_map_level_2_high -page_map_level_2_high: .skip 512 * 8 /** * Reserve some space for the Multiboot 2 information pointer. @@ -306,10 +302,7 @@ enable_sse: * * 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. Note that we also map memory both in the low and high - * virtual address ranges, giving us two ways of accessing it. We need to do - * this, because the bootstrapping code lives in low memory, while the rest of - * the kernel will reside on the high end. + * page map entries. */ prepare_page_maps: /* Add an entry to the PML4, pointing to the low PML3 */ @@ -317,21 +310,11 @@ prepare_page_maps: or $0x3, %eax mov %eax, (page_map_level_4 + ((0x0000000000100000 >> 39) & 0x1ff) * 8) - /* Add an entry to the PML4, pointing to the high PML3 */ - mov $page_map_level_3_high, %eax - or $0x3, %eax - mov %eax, (page_map_level_4 + ((0xffffffff80100000 >> 39) & 0x1ff) * 8) - /* Add an entry to the low PML3, pointing to the low PML2 */ mov $page_map_level_2_low, %eax or $0x3, %eax mov %eax, (page_map_level_3_low + ((0x0000000000100000 >> 30) & 0x1ff) * 8) - /* Add an entry to the high PML3, pointing to the high PML2 */ - mov $page_map_level_2_high, %eax - or $0x3, %eax - mov %eax, (page_map_level_3_high + ((0xffffffff80100000 >> 30) & 0x1ff) * 8) - xor %ecx, %ecx mov $_end_linear, %esi @@ -343,7 +326,6 @@ prepare_page_maps: mul %ecx or $((1 << 0) | (1 << 1) | (1 << 7)), %eax mov %eax, page_map_level_2_low(,%ecx,8) - mov %eax, page_map_level_2_high(,%ecx,8) inc %ecx cmp %esi, %ecx diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 4d6296a..e867bad 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -9,47 +9,28 @@ namespace teachos::arch::kernel auto print_mem_info(arch::memory::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(arch::memory::memory_map * mminfo) -> void { using namespace video::vga; - uint32_t const entry_size = mminfo->entry_size; - 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); - - uint32_t const remaining_size = mminfo->tag.size - (4 * sizeof(uint32_t)); - uint32_t const entry_amount = remaining_size / entry_size; + auto entry_size = mminfo->entry_size; + auto entry_version = mminfo->entry_version; + auto remaining_size = mminfo->tag.size - (4 * sizeof(uint32_t)); + auto entry_amount = remaining_size / entry_size; auto begin = &mminfo->entries; auto end = begin + entry_amount; - for (auto entry = begin; entry != end; ++entry) + for (auto entry = begin; entry != end; + entry = (teachos::arch::memory::memory_area *)(((uint8_t *)entry) + ((entry_size + 7) & ~7))) { - 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(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 base_addr = entry->base_addr; + auto length = entry->length; + auto type = entry->type; + auto reserved = entry->reserved; } } @@ -57,61 +38,33 @@ namespace teachos::arch::kernel { 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 num = symbol->num; + auto entsize = symbol->entsize; + auto shndx = symbol->shndx; 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 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 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 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 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 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 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 entry size: ", text::common_attributes::green_on_black); - text::write_number(sh_entsize, text::common_attributes::green_on_black); + /* + * Loop over the elf section to access the information needed. + * Sections are defined in the header file and are padded so that each + * Section starts at an 8-bytes aligned adress. + * + * The increment part aligns the size to an 8-byte address. + */ + for (auto section = begin; section != end; + section = (teachos::arch::memory::elf_section_header *)(((uint8_t *)section) + ((entsize + 7) & ~7))) + { + auto sh_name = section->sh_name; + auto sh_type = section->sh_type; + auto sh_flags = section->sh_flags; + auto sh_addr = section->sh_addr; + auto sh_offset = section->sh_offset; + auto sh_size = section->sh_size; + auto sh_link = section->sh_link; + auto sh_info = section->sh_info; + auto sh_addralign = section->sh_addralign; + auto sh_entsize = section->sh_entsize; } } @@ -127,11 +80,8 @@ namespace teachos::arch::kernel (arch::memory::multi_boot_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); + auto multiboot_start = arch::boot::multiboot_information_pointer; + auto multiboot_end = arch::boot::multiboot_information_pointer + multiboot_information_pointer->total_size; /* * Loop over the multiboot2 tags to access the information needed. -- cgit v1.2.3 From f044a1740f49d632d1436df949fa2285918e5937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 08:53:21 +0000 Subject: Remove unused variable declarations --- arch/x86_64/src/kernel/main.cpp | 42 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index e867bad..3436127 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -8,42 +8,28 @@ namespace teachos::arch::kernel { auto print_mem_info(arch::memory::memory_info * mem_info) -> void { - using namespace video::vga; - auto mem_lower = mem_info->mem_lower; - auto mem_upper = mem_info->mem_upper; + video::vga::text::write("Memory info low: ", video::vga::text::common_attributes::green_on_black); + video::vga::text::write_number(mem_info->mem_lower, video::vga::text::common_attributes::green_on_black); } auto print_memory_map(arch::memory::memory_map * mminfo) -> void { - using namespace video::vga; - - auto entry_size = mminfo->entry_size; - auto entry_version = mminfo->entry_version; auto remaining_size = mminfo->tag.size - (4 * sizeof(uint32_t)); - auto entry_amount = remaining_size / entry_size; + auto entry_amount = remaining_size / mminfo->entry_size; auto begin = &mminfo->entries; auto end = begin + entry_amount; for (auto entry = begin; entry != end; - entry = (teachos::arch::memory::memory_area *)(((uint8_t *)entry) + ((entry_size + 7) & ~7))) + entry = (teachos::arch::memory::memory_area *)(((uint8_t *)entry) + ((mminfo->entry_size + 7) & ~7))) { - auto base_addr = entry->base_addr; - auto length = entry->length; - auto type = entry->type; - auto reserved = entry->reserved; + video::vga::text::write("Looping Memory area", video::vga::text::common_attributes::green_on_black); } } auto print_elf_sections(arch::memory::elf_symbols_section * symbol) -> void { - using namespace video::vga; - - auto num = symbol->num; - auto entsize = symbol->entsize; - auto shndx = symbol->shndx; - auto begin = &symbol->sections; - auto end = begin + num; + auto end = begin + symbol->num; /* * Loop over the elf section to access the information needed. @@ -53,18 +39,9 @@ namespace teachos::arch::kernel * The increment part aligns the size to an 8-byte address. */ for (auto section = begin; section != end; - section = (teachos::arch::memory::elf_section_header *)(((uint8_t *)section) + ((entsize + 7) & ~7))) + section = (teachos::arch::memory::elf_section_header *)(((uint8_t *)section) + ((symbol->entsize + 7) & ~7))) { - auto sh_name = section->sh_name; - auto sh_type = section->sh_type; - auto sh_flags = section->sh_flags; - auto sh_addr = section->sh_addr; - auto sh_offset = section->sh_offset; - auto sh_size = section->sh_size; - auto sh_link = section->sh_link; - auto sh_info = section->sh_info; - auto sh_addralign = section->sh_addralign; - auto sh_entsize = section->sh_entsize; + video::vga::text::write("Looping Code section", video::vga::text::common_attributes::green_on_black); } } @@ -80,9 +57,6 @@ namespace teachos::arch::kernel (arch::memory::multi_boot_info *)arch::boot::multiboot_information_pointer; auto multiboot_tag = &(multiboot_information_pointer->tags); - auto multiboot_start = arch::boot::multiboot_information_pointer; - auto multiboot_end = arch::boot::multiboot_information_pointer + multiboot_information_pointer->total_size; - /* * Loop over the multiboot2 tags to access the information needed. * Tags are defined in the header file and are padded so that each -- cgit v1.2.3 From 8e3c15daad14dfba76444d83bc4133acd00eaf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 09:12:40 +0000 Subject: Revert 8 byte aligning --- arch/x86_64/src/kernel/main.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 3436127..d94cbba 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -19,8 +19,7 @@ namespace teachos::arch::kernel auto begin = &mminfo->entries; auto end = begin + entry_amount; - for (auto entry = begin; entry != end; - entry = (teachos::arch::memory::memory_area *)(((uint8_t *)entry) + ((mminfo->entry_size + 7) & ~7))) + for (auto entry = begin; entry != end; ++entry) { video::vga::text::write("Looping Memory area", video::vga::text::common_attributes::green_on_black); } @@ -30,16 +29,7 @@ namespace teachos::arch::kernel { auto begin = &symbol->sections; auto end = begin + symbol->num; - - /* - * Loop over the elf section to access the information needed. - * Sections are defined in the header file and are padded so that each - * Section starts at an 8-bytes aligned adress. - * - * The increment part aligns the size to an 8-byte address. - */ - for (auto section = begin; section != end; - section = (teachos::arch::memory::elf_section_header *)(((uint8_t *)section) + ((symbol->entsize + 7) & ~7))) + for (auto section = begin; section != end; ++section) { video::vga::text::write("Looping Code section", video::vga::text::common_attributes::green_on_black); } -- cgit v1.2.3 From 1b33fc5746be4d23b1c4adacc02263fbcf994524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 09:58:17 +0000 Subject: Add assert method --- arch/x86_64/src/kernel/main.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index d94cbba..fa1e464 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -6,6 +6,15 @@ namespace teachos::arch::kernel { + auto assert(bool condition) -> void + { + video::vga::text::write("Assert failed", video::vga::text::common_attributes::green_on_black); + while (!condition) + { + ; + } + } + auto print_mem_info(arch::memory::memory_info * mem_info) -> void { video::vga::text::write("Memory info low: ", video::vga::text::common_attributes::green_on_black); @@ -14,6 +23,10 @@ namespace teachos::arch::kernel auto print_memory_map(arch::memory::memory_map * mminfo) -> void { + auto expected_entry_size = mminfo->entry_size; + constexpr auto actual_size = sizeof(arch::memory::memory_area); + assert(expected_entry_size == actual_size); + auto remaining_size = mminfo->tag.size - (4 * sizeof(uint32_t)); auto entry_amount = remaining_size / mminfo->entry_size; -- cgit v1.2.3 From b9c5aea495653bb9fc347fa6ba5976b42510af53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 12:40:33 +0000 Subject: Added elf section type enum --- arch/x86_64/src/kernel/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index fa1e464..481264c 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -24,8 +24,8 @@ namespace teachos::arch::kernel auto print_memory_map(arch::memory::memory_map * mminfo) -> void { auto expected_entry_size = mminfo->entry_size; - constexpr auto actual_size = sizeof(arch::memory::memory_area); - assert(expected_entry_size == actual_size); + constexpr auto actual_entry_size = sizeof(arch::memory::memory_area); + assert(expected_entry_size == actual_entry_size); auto remaining_size = mminfo->tag.size - (4 * sizeof(uint32_t)); auto entry_amount = remaining_size / mminfo->entry_size; @@ -40,6 +40,10 @@ namespace teachos::arch::kernel auto print_elf_sections(arch::memory::elf_symbols_section * symbol) -> void { + auto expected_entry_size = symbol->entsize; + constexpr auto actual_entry_size = sizeof(arch::memory::elf_section_header); + assert(expected_entry_size == actual_entry_size); + auto begin = &symbol->sections; auto end = begin + symbol->num; for (auto section = begin; section != end; ++section) -- cgit v1.2.3 From 78153377d8a964d6b7290d966e5c1d30369abc2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 13:40:24 +0000 Subject: Improve naming --- arch/x86_64/src/kernel/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 481264c..636ddf2 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -40,12 +40,12 @@ namespace teachos::arch::kernel auto print_elf_sections(arch::memory::elf_symbols_section * symbol) -> void { - auto expected_entry_size = symbol->entsize; + auto expected_entry_size = symbol->entry_size; constexpr auto actual_entry_size = sizeof(arch::memory::elf_section_header); assert(expected_entry_size == actual_entry_size); auto begin = &symbol->sections; - auto end = begin + symbol->num; + auto end = begin + symbol->number_of_sections; for (auto section = begin; section != end; ++section) { video::vga::text::write("Looping Code section", video::vga::text::common_attributes::green_on_black); -- cgit v1.2.3 From a6018f84cc8971859d90109740fbada8d77ff5a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 8 Oct 2024 07:58:53 +0000 Subject: Add more asserts for elf sections --- arch/x86_64/src/kernel/main.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 636ddf2..2ba4fe2 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -11,7 +11,12 @@ namespace teachos::arch::kernel video::vga::text::write("Assert failed", video::vga::text::common_attributes::green_on_black); while (!condition) { - ; + // Trick the compiler into thinking the variable is changes at run time, + // to prevent the while loop being optimized away + // See + // https://stackoverflow.com/questions/9495856/how-to-prevent-g-from-optimizing-out-a-loop-controlled-by-a-variable-that-can + // for mroe information. + asm volatile("" : "+g"(condition)); } } @@ -44,8 +49,20 @@ namespace teachos::arch::kernel constexpr auto actual_entry_size = sizeof(arch::memory::elf_section_header); assert(expected_entry_size == actual_entry_size); + auto expected_total_size = symbol->tag.size; + auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections; + constexpr auto actual_total_section_size = + sizeof(arch::memory::elf_symbols_section) - actual_entry_size - sizeof(uint32_t); + auto actual_total_size = actual_total_entry_size + actual_total_section_size; + assert(expected_total_size == actual_total_size); + auto begin = &symbol->sections; auto end = begin + symbol->number_of_sections; + // TODO: Last value is completly wrong, should show 0 but shows huge value for size of entries in the table of the + // SHT_NULL elf section entry. Memory around value also make no sense and look even worse? But according to api + // value should be zero :( + // assert(begin->is_null()); + for (auto section = begin; section != end; ++section) { video::vga::text::write("Looping Code section", video::vga::text::common_attributes::green_on_black); -- cgit v1.2.3 From 8773024d1b4e756fe5d3494479247c64c7ad8491 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 8 Oct 2024 09:51:26 +0000 Subject: begin implementing frame allocator --- arch/x86_64/src/memory/frame_allocator.cpp | 70 +++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/memory/frame_allocator.cpp b/arch/x86_64/src/memory/frame_allocator.cpp index b9d26a5..b8f53be 100644 --- a/arch/x86_64/src/memory/frame_allocator.cpp +++ b/arch/x86_64/src/memory/frame_allocator.cpp @@ -2,8 +2,76 @@ namespace teachos::arch::memory { - auto area_frame_allocator::allocate_frame() -> std::optional {} + auto area_frame_allocator::allocate_frame() -> std::optional + { + /* + * Only try to allocate memory if current_area is not null, because + * the current_area is null if there is no more available memory. + */ + if (current_area) + { + frame frame{next_free_frame.frame_number}; + + struct frame current_area_last_frame = { + frame::containing_address(current_area->base_address + current_area->area_length - 1)}; + + if (next_free_frame > current_area_last_frame) + { + // All frames of current area are used, switch to next area + choose_next_area(); + } + else if (frame >= multiboot_start && frame <= kernel_end) + { + // `frame` is used by the kernel or multiboot information structure + next_free_frame = arch::memory::frame{kernel_end.frame_number + 1}; + } + else + { + // Frame is unused, increment `next_free_frame` and return it + next_free_frame.frame_number += 1; + return frame; + } + + // `frame` was not valid, try it again with the updated `next_free_frame` + return allocate_frame(); + } + + // no free frames left + return std::nullopt; + } auto area_frame_allocator::deallocate_frame(frame frame) -> void {} + namespace + { + auto area_frame_allocator::choose_next_area() -> void + { + current_area = std::nullopt; + + // TODO: Fix this loop as soon as you've created an area iterator + for (const auto & area : areas) + { + std::size_t address = area.base_addr + area.length - 1; + if (frame::containing_address(address) >= next_free_frame) + { + // The `next_free_frame` address is smaller than the last address of the current area + if (!current_area || area.base_addr < current_area->base_address) + { + current_area = area; + } + } + } + + if (current_area) + { + // Update the `next_free_frame` according to the new memory area + frame start_frame = frame::containing_address(current_area->base_address); + if (next_free_frame < start_frame) + { + next_free_frame = start_frame; + } + } + } + + } // namespace } // namespace teachos::arch::memory -- cgit v1.2.3 From 7edd03e9a14a3025b4d2b2ff51d838d20b79b2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 8 Oct 2024 11:30:01 +0000 Subject: Added doxygen comments to all fields and structs --- arch/x86_64/src/kernel/main.cpp | 3 +++ arch/x86_64/src/memory/multiboot.cpp | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 arch/x86_64/src/memory/multiboot.cpp (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 2ba4fe2..6486b7c 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -63,6 +63,9 @@ namespace teachos::arch::kernel // value should be zero :( // assert(begin->is_null()); + // TODO: Check if only contains one DYNSYM or SYMTAB but not both! + // TODO: Check if only contains one dynamic section + for (auto section = begin; section != end; ++section) { video::vga::text::write("Looping Code section", video::vga::text::common_attributes::green_on_black); diff --git a/arch/x86_64/src/memory/multiboot.cpp b/arch/x86_64/src/memory/multiboot.cpp new file mode 100644 index 0000000..91e7550 --- /dev/null +++ b/arch/x86_64/src/memory/multiboot.cpp @@ -0,0 +1,41 @@ +#include "multiboot.hpp" + +namespace teachos::arch::me