From a2fdcea0d7615f8933401e45e0c64a2f618bb730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 19 Oct 2024 13:15:45 +0000 Subject: Move compelte multiboot2 code into seperate files and behaviour into seperate static class --- .../src/memory/multiboot/elf_symbols_section.cpp | 13 +++ arch/x86_64/src/memory/multiboot/memory_map.cpp | 24 +++++ arch/x86_64/src/memory/multiboot/reader.cpp | 117 +++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp create mode 100644 arch/x86_64/src/memory/multiboot/memory_map.cpp create mode 100644 arch/x86_64/src/memory/multiboot/reader.cpp (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp b/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp new file mode 100644 index 0000000..78ab952 --- /dev/null +++ b/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp @@ -0,0 +1,13 @@ +#include "arch/memory/multiboot/elf_sybols_section.hpp" + +namespace teachos::arch::memory::multiboot +{ + auto elf_section_flags::contains_flags(std::bitset<64U> other) const -> bool { return (flags & other) == other; } + + auto elf_section_header::is_null() const -> bool + { + return name_table_index == 0U && type == elf_section_type::INACTIVE && flags == elf_section_flags(0U) && + virtual_address == 0U && file_offset == 0U && additional_information == 0U && address_alignment == 0U && + fixed_table_entry_size == 0U; + } +} // namespace teachos::arch::memory::multiboot diff --git a/arch/x86_64/src/memory/multiboot/memory_map.cpp b/arch/x86_64/src/memory/multiboot/memory_map.cpp new file mode 100644 index 0000000..6b1d1d4 --- /dev/null +++ b/arch/x86_64/src/memory/multiboot/memory_map.cpp @@ -0,0 +1,24 @@ +#include "arch/memory/multiboot/memory_map.hpp" + +namespace teachos::arch::memory::multiboot +{ + memory_area_iterator::memory_area_iterator(multiboot::memory_area * p) + : ptr(p) + { + // Nothing to do + } + + multiboot::memory_area & memory_area_iterator::operator*() const { return *ptr; } + + auto memory_area_iterator::operator++(int) -> memory_area_iterator + { + ++(*this); + return *this; + } + + auto memory_area_iterator::operator++() -> memory_area_iterator & + { + ++ptr; + return *this; + } +} // namespace teachos::arch::memory::multiboot diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp new file mode 100644 index 0000000..8741b44 --- /dev/null +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -0,0 +1,117 @@ +#include "arch/memory/multiboot/reader.hpp" + +#include "arch/boot/pointers.hpp" +#include "arch/exception_handling/assert.hpp" +#include "arch/memory/multiboot/info.hpp" + +namespace teachos::arch::memory::multiboot +{ + namespace + { + template + requires std::is_pointer::value + auto align_to_8_byte_boundary(T ptr, uint32_t size) -> T + { + return reinterpret_cast(reinterpret_cast(ptr) + ((size + 7) & ~7)); + } + + auto process_memory_map(memory_map_header * mminfo, memory_area *& memory_areas, uint8_t & area_count) -> void + { + auto expected_entry_size = mminfo->entry_size; + constexpr auto actual_entry_size = sizeof(memory_area); + arch::exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected memory_area entry size"); + + auto total_size = mminfo->info.size; + auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; + auto number_of_entries = total_entries_size / actual_entry_size; + + memory_areas = &mminfo->entries; + area_count = number_of_entries; + } + + auto process_elf_sections(elf_symbols_section_header * symbol, uint64_t & kernel_start, + uint64_t & kernel_end) -> void + { + auto expected_entry_size = symbol->entry_size; + constexpr auto actual_entry_size = sizeof(elf_section_header); + arch::exception_handling::assert(expected_entry_size == actual_entry_size, + "Unexpected elf_section_header entry size"); + + auto expected_total_size = symbol->info.size; + auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections; + constexpr auto actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); + auto actual_total_size = actual_total_entry_size + actual_total_section_size; + arch::exception_handling::assert(expected_total_size == actual_total_size, + "Unexpected elf_symbols_section_header total size"); + + auto begin = reinterpret_cast(&symbol->end); + auto end = begin + symbol->number_of_sections; + arch::exception_handling::assert(begin->is_null(), "Missing elf_section_header begin"); + + std::size_t symbol_table_section_count = 0U; + std::size_t dynamic_section_count = 0U; + + for (auto section = begin; section != end; ++section) + { + switch (section->type) + { + case elf_section_type::PROGRAMM: { + if (section->virtual_address < kernel_start) + { + kernel_start = section->virtual_address; + } + auto virtual_address_end = section->virtual_address + section->section_size; + if (virtual_address_end > kernel_end) + { + kernel_end = virtual_address_end; + } + break; + } + case elf_section_type::DYNAMIC_SYMBOL_TABLE: + case elf_section_type::SYMBOL_TABLE: + symbol_table_section_count++; + break; + case elf_section_type::DYNAMIC: + dynamic_section_count++; + break; + default: + // All other cases are not important and can be ignored + break; + } + } + + arch::exception_handling::assert(symbol_table_section_count == 1U, "Unexpected symbol_table_count value"); + arch::exception_handling::assert(dynamic_section_count <= 1U, "Unexpected dynamic_section_count value"); + } + } // namespace + + auto read_multiboot2() -> memory_information + { + memory_information mem_info{UINT64_MAX, 0U, boot::multiboot_information_pointer, 0U, nullptr, 0U}; + + auto * multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); + auto multiboot_tag = &(multiboot_information_pointer->tags); + mem_info.multiboot_end = mem_info.multiboot_start + multiboot_information_pointer->total_size; + + for (auto tag = multiboot_tag; tag->type != tag_type::END; tag = align_to_8_byte_boundary(tag, tag->size)) + { + switch (tag->type) + { + case tag_type::ELF_SECTIONS: { + auto symbol = reinterpret_cast(tag); + process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end); + break; + } + case tag_type::MEMORY_MAP: { + auto mminfo = reinterpret_cast(tag); + process_memory_map(mminfo, mem_info.memory_areas, mem_info.area_count); + break; + } + default: + // All other cases are not important and can be ignored + break; + } + } + return mem_info; + } +} // namespace teachos::arch::memory::multiboot -- cgit v1.2.3 From 7ca089125b8c5e55dd584648cd33612883cc004d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 06:26:58 +0000 Subject: Fix typo in file name --- arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp b/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp index 78ab952..953a57d 100644 --- a/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp +++ b/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp @@ -1,4 +1,4 @@ -#include "arch/memory/multiboot/elf_sybols_section.hpp" +#include "arch/memory/multiboot/elf_symbols_section.hpp" namespace teachos::arch::memory::multiboot { -- cgit v1.2.3 From d728052d62470799f73f6d9a2b8baa2b0b357383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 07:01:53 +0000 Subject: Add helper methods to phyisca frame --- arch/x86_64/src/memory/multiboot/reader.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 8741b44..156b437 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -19,7 +19,7 @@ namespace teachos::arch::memory::multiboot { auto expected_entry_size = mminfo->entry_size; constexpr auto actual_entry_size = sizeof(memory_area); - arch::exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected memory_area entry size"); + exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected memory_area entry size"); auto total_size = mminfo->info.size; auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; @@ -34,19 +34,18 @@ namespace teachos::arch::memory::multiboot { auto expected_entry_size = symbol->entry_size; constexpr auto actual_entry_size = sizeof(elf_section_header); - arch::exception_handling::assert(expected_entry_size == actual_entry_size, - "Unexpected elf_section_header entry size"); + exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected elf_section_header entry size"); auto expected_total_size = symbol->info.size; auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections; constexpr auto actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); auto actual_total_size = actual_total_entry_size + actual_total_section_size; - arch::exception_handling::assert(expected_total_size == actual_total_size, - "Unexpected elf_symbols_section_header total size"); + exception_handling::assert(expected_total_size == actual_total_size, + "Unexpected elf_symbols_section_header total size"); auto begin = reinterpret_cast(&symbol->end); auto end = begin + symbol->number_of_sections; - arch::exception_handling::assert(begin->is_null(), "Missing elf_section_header begin"); + exception_handling::assert(begin->is_null(), "Missing elf_section_header begin"); std::size_t symbol_table_section_count = 0U; std::size_t dynamic_section_count = 0U; @@ -80,8 +79,8 @@ namespace teachos::arch::memory::multiboot } } - arch::exception_handling::assert(symbol_table_section_count == 1U, "Unexpected symbol_table_count value"); - arch::exception_handling::assert(dynamic_section_count <= 1U, "Unexpected dynamic_section_count value"); + exception_handling::assert(symbol_table_section_count == 1U, "Unexpected symbol_table_count value"); + exception_handling::assert(dynamic_section_count <= 1U, "Unexpected dynamic_section_count value"); } } // namespace -- cgit v1.2.3 From f8928c877c48b5beba9bb42876f70213aba68f88 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 20 Oct 2024 07:46:51 +0000 Subject: translate --- arch/x86_64/src/memory/multiboot/reader.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 156b437..565f604 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -111,6 +111,7 @@ namespace teachos::arch::memory::multiboot break; } } + return mem_info; } } // namespace teachos::arch::memory::multiboot -- cgit v1.2.3 From da2341ec12128d3b4983a67d39aeaf76b1781fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:02:20 +0000 Subject: Add printf like behaviour to assert --- arch/x86_64/src/memory/multiboot/reader.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 565f604..4e78a06 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -19,7 +19,9 @@ namespace teachos::arch::memory::multiboot { auto expected_entry_size = mminfo->entry_size; constexpr auto actual_entry_size = sizeof(memory_area); - exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected memory_area entry size"); + exception_handling::assert(expected_entry_size == actual_entry_size, + "[Multiboot Reader] Expected memory area entry size (%u) but got (%u)", + expected_entry_size, actual_entry_size); auto total_size = mminfo->info.size; auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; @@ -34,18 +36,22 @@ namespace teachos::arch::memory::multiboot { auto expected_entry_size = symbol->entry_size; constexpr auto actual_entry_size = sizeof(elf_section_header); - exception_handling::assert(expected_entry_size == actual_entry_size, "Unexpected elf_section_header entry size"); + exception_handling::assert(expected_entry_size == actual_entry_size, + "[Multiboot Reader] Expected elf section header entry size (%u) but got (%u)", + expected_entry_size, actual_entry_size); auto expected_total_size = symbol->info.size; auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections; constexpr auto actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); auto actual_total_size = actual_total_entry_size + actual_total_section_size; exception_handling::assert(expected_total_size == actual_total_size, - "Unexpected elf_symbols_section_header total size"); + "[Multiboot Reader] Expected elf symbols section header total size (%u) but got (%u)", + expected_total_size, actual_total_size); auto begin = reinterpret_cast(&symbol->end); auto end = begin + symbol->number_of_sections; - exception_handling::assert(begin->is_null(), "Missing elf_section_header begin"); + exception_handling::assert(begin->is_null(), + "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); std::size_t symbol_table_section_count = 0U; std::size_t dynamic_section_count = 0U; @@ -79,8 +85,14 @@ namespace teachos::arch::memory::multiboot } } - exception_handling::assert(symbol_table_section_count == 1U, "Unexpected symbol_table_count value"); - exception_handling::assert(dynamic_section_count <= 1U, "Unexpected dynamic_section_count value"); + exception_handling::assert( + symbol_table_section_count == 1U, + "[Multiboot Reader] ELF Specifications allows only (1) symbol table section, but got (%u)", + symbol_table_section_count); + exception_handling::assert( + dynamic_section_count <= 1U, + "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got (%u)", + dynamic_section_count); } } // namespace -- cgit v1.2.3 From 2129bdb22bab7dc5a9d23a31c23f38e847511a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:17:44 +0000 Subject: =?UTF-8?q?Revert=20assert=20with=20printf=20functionality,=20requ?= =?UTF-8?q?ires=20malloc=20=F0=9F=98=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arch/x86_64/src/memory/multiboot/reader.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 4e78a06..545e517 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -20,8 +20,7 @@ namespace teachos::arch::memory::multiboot auto expected_entry_size = mminfo->entry_size; constexpr auto actual_entry_size = sizeof(memory_area); exception_handling::assert(expected_entry_size == actual_entry_size, - "[Multiboot Reader] Expected memory area entry size (%u) but got (%u)", - expected_entry_size, actual_entry_size); + "[Multiboot Reader] Unexpected memory area entry size"); auto total_size = mminfo->info.size; auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; @@ -37,16 +36,14 @@ namespace teachos::arch::memory::multiboot auto expected_entry_size = symbol->entry_size; constexpr auto actual_entry_size = sizeof(elf_section_header); exception_handling::assert(expected_entry_size == actual_entry_size, - "[Multiboot Reader] Expected elf section header entry size (%u) but got (%u)", - expected_entry_size, actual_entry_size); + "[Multiboot Reader] Unexpected elf section header entry size"); auto expected_total_size = symbol->info.size; auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections; constexpr auto actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); auto actual_total_size = actual_total_entry_size + actual_total_section_size; exception_handling::assert(expected_total_size == actual_total_size, - "[Multiboot Reader] Expected elf symbols section header total size (%u) but got (%u)", - expected_total_size, actual_total_size); + "[Multiboot Reader] Unexpected elf symbols section header total size"); auto begin = reinterpret_cast(&symbol->end); auto end = begin + symbol->number_of_sections; @@ -87,12 +84,10 @@ namespace teachos::arch::memory::multiboot exception_handling::assert( symbol_table_section_count == 1U, - "[Multiboot Reader] ELF Specifications allows only (1) symbol table section, but got (%u)", - symbol_table_section_count); + "[Multiboot Reader] ELF Specifications allows only (1) symbol table section, but got more"); exception_handling::assert( dynamic_section_count <= 1U, - "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got (%u)", - dynamic_section_count); + "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); } } // namespace -- cgit v1.2.3 From a29e823c6ead21fa7c8f6445411d52f57c4518fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 27 Oct 2024 09:21:25 +0000 Subject: Attempt to start using C++20 algorithm calls. --- arch/x86_64/src/memory/multiboot/memory_map.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/memory_map.cpp b/arch/x86_64/src/memory/multiboot/memory_map.cpp index 6b1d1d4..da7f05d 100644 --- a/arch/x86_64/src/memory/multiboot/memory_map.cpp +++ b/arch/x86_64/src/memory/multiboot/memory_map.cpp @@ -1,11 +1,13 @@ #include "arch/memory/multiboot/memory_map.hpp" +#include "arch/exception_handling/assert.hpp" + namespace teachos::arch::memory::multiboot { memory_area_iterator::memory_area_iterator(multiboot::memory_area * p) : ptr(p) { - // Nothing to do + exception_handling::assert(ptr, "[Memory Area] Attempted to pass nullptr as iterator"); } multiboot::memory_area & memory_area_iterator::operator*() const { return *ptr; } @@ -21,4 +23,15 @@ namespace teachos::arch::memory::multiboot ++ptr; return *this; } + + memory_area_container::memory_area_container(memory_area * begin, std::size_t size) + : area_begin(begin) + , area_end(begin + size) + { + // Nothing to do + } + + auto memory_area_container::begin() const -> multiboot::memory_area_iterator { return area_begin; } + + auto memory_area_container::end() const -> multiboot::memory_area_iterator { return area_end; } } // namespace teachos::arch::memory::multiboot -- cgit v1.2.3 From 3fb2780f665eb3514ef07d2d4a83820653e35b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 27 Oct 2024 11:20:46 +0000 Subject: Use container and C++20 range algorithms for allocator. --- arch/x86_64/src/memory/multiboot/memory_map.cpp | 44 ++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/memory_map.cpp b/arch/x86_64/src/memory/multiboot/memory_map.cpp index da7f05d..b5e2759 100644 --- a/arch/x86_64/src/memory/multiboot/memory_map.cpp +++ b/arch/x86_64/src/memory/multiboot/memory_map.cpp @@ -4,18 +4,21 @@ namespace teachos::arch::memory::multiboot { - memory_area_iterator::memory_area_iterator(multiboot::memory_area * p) + memory_area_iterator::memory_area_iterator(value_type * p) : ptr(p) { exception_handling::assert(ptr, "[Memory Area] Attempted to pass nullptr as iterator"); } - multiboot::memory_area & memory_area_iterator::operator*() const { return *ptr; } + auto memory_area_iterator::operator*() const -> value_type & { return *ptr; } + + auto memory_area_iterator::operator->() const -> value_type * { return ptr; } auto memory_area_iterator::operator++(int) -> memory_area_iterator { - ++(*this); - return *this; + memory_area_iterator old_value = *this; + ++ptr; + return old_value; } auto memory_area_iterator::operator++() -> memory_area_iterator & @@ -24,6 +27,35 @@ namespace teachos::arch::memory::multiboot return *this; } + auto memory_area_iterator::operator+=(difference_type value) -> memory_area_iterator & + { + ptr += value; + return *this; + } + + auto memory_area_iterator::operator-=(difference_type value) -> memory_area_iterator & + { + ptr -= value; + return *this; + } + + auto memory_area_iterator::operator+(difference_type value) const -> memory_area_iterator + { + return memory_area_iterator{ptr + value}; + } + + auto memory_area_iterator::operator-(difference_type value) const -> memory_area_iterator + { + return memory_area_iterator{ptr - value}; + } + + auto memory_area_iterator::operator-(const memory_area_iterator & other) const -> difference_type + { + return ptr - other.ptr; + } + + auto memory_area_iterator::operator[](difference_type index) const -> value_type & { return *(ptr + index); } + memory_area_container::memory_area_container(memory_area * begin, std::size_t size) : area_begin(begin) , area_end(begin + size) @@ -34,4 +66,8 @@ namespace teachos::arch::memory::multiboot auto memory_area_container::begin() const -> multiboot::memory_area_iterator { return area_begin; } auto memory_area_container::end() const -> multiboot::memory_area_iterator { return area_end; } + + auto memory_area_container::size() const -> size_type { return std::distance(begin(), end()); } + + auto memory_area_container::empty() const -> bool { return size() == 0; } } // namespace teachos::arch::memory::multiboot -- cgit v1.2.3 From d0f47ef0cd8cb2f5079808a261dd724b3eb1a3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 27 Oct 2024 14:12:35 +0000 Subject: Fix typos --- arch/x86_64/src/memory/multiboot/reader.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 545e517..c750ae1 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -55,20 +55,18 @@ namespace teachos::arch::memory::multiboot for (auto section = begin; section != end; ++section) { + if (section->virtual_address < kernel_start) + { + kernel_start = section->virtual_address; + } + auto virtual_address_end = section->virtual_address + section->section_size; + if (virtual_address_end > kernel_end) + { + kernel_end = virtual_address_end; + } + switch (section->type) { - case elf_section_type::PROGRAMM: { - if (section->virtual_address < kernel_start) - { - kernel_start = section->virtual_address; - } - auto virtual_address_end = section->virtual_address + section->section_size; - if (virtual_address_end > kernel_end) - { - kernel_end = virtual_address_end; - } - break; - } case elf_section_type::DYNAMIC_SYMBOL_TABLE: case elf_section_type::SYMBOL_TABLE: symbol_table_section_count++; -- cgit v1.2.3 From 04b0285d8329e45cea426aa1a8e69203685a4467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 10:15:18 +0000 Subject: Move iterator and container into generic template classes. Use algorithms instead of raw pointer for loops --- arch/x86_64/src/memory/multiboot/memory_map.cpp | 73 ----------------------- arch/x86_64/src/memory/multiboot/reader.cpp | 79 +++++++++++++------------ 2 files changed, 42 insertions(+), 110 deletions(-) delete mode 100644 arch/x86_64/src/memory/multiboot/memory_map.cpp (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/memory_map.cpp b/arch/x86_64/src/memory/multiboot/memory_map.cpp deleted file mode 100644 index b5e2759..0000000 --- a/arch/x86_64/src/memory/multiboot/memory_map.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "arch/memory/multiboot/memory_map.hpp" - -#include "arch/exception_handling/assert.hpp" - -namespace teachos::arch::memory::multiboot -{ - memory_area_iterator::memory_area_iterator(value_type * p) - : ptr(p) - { - exception_handling::assert(ptr, "[Memory Area] Attempted to pass nullptr as iterator"); - } - - auto memory_area_iterator::operator*() const -> value_type & { return *ptr; } - - auto memory_area_iterator::operator->() const -> value_type * { return ptr; } - - auto memory_area_iterator::operator++(int) -> memory_area_iterator - { - memory_area_iterator old_value = *this; - ++ptr; - return old_value; - } - - auto memory_area_iterator::operator++() -> memory_area_iterator & - { - ++ptr; - return *this; - } - - auto memory_area_iterator::operator+=(difference_type value) -> memory_area_iterator & - { - ptr += value; - return *this; - } - - auto memory_area_iterator::operator-=(difference_type value) -> memory_area_iterator & - { - ptr -= value; - return *this; - } - - auto memory_area_iterator::operator+(difference_type value) const -> memory_area_iterator - { - return memory_area_iterator{ptr + value}; - } - - auto memory_area_iterator::operator-(difference_type value) const -> memory_area_iterator - { - return memory_area_iterator{ptr - value}; - } - - auto memory_area_iterator::operator-(const memory_area_iterator & other) const -> difference_type - { - return ptr - other.ptr; - } - - auto memory_area_iterator::operator[](difference_type index) const -> value_type & { return *(ptr + index); } - - memory_area_container::memory_area_container(memory_area * begin, std::size_t size) - : area_begin(begin) - , area_end(begin + size) - { - // Nothing to do - } - - auto memory_area_container::begin() const -> multiboot::memory_area_iterator { return area_begin; } - - auto memory_area_container::end() const -> multiboot::memory_area_iterator { return area_end; } - - auto memory_area_container::size() const -> size_type { return std::distance(begin(), end()); } - - auto memory_area_container::empty() const -> bool { return size() == 0; } -} // namespace teachos::arch::memory::multiboot diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index c750ae1..228aa09 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -2,8 +2,11 @@ #include "arch/boot/pointers.hpp" #include "arch/exception_handling/assert.hpp" +#include "arch/memory/multiboot/elf_symbols_section.hpp" #include "arch/memory/multiboot/info.hpp" +#include + namespace teachos::arch::memory::multiboot { namespace @@ -15,7 +18,8 @@ namespace teachos::arch::memory::multiboot return reinterpret_cast(reinterpret_cast(ptr) + ((size + 7) & ~7)); } - auto process_memory_map(memory_map_header * mminfo, memory_area *& memory_areas, uint8_t & area_count) -> void + auto process_memory_map(memory_map_header * mminfo, memory_area_container::iterator & begin_area, + memory_area_container::iterator & end_area) -> void { auto expected_entry_size = mminfo->entry_size; constexpr auto actual_entry_size = sizeof(memory_area); @@ -26,8 +30,8 @@ namespace teachos::arch::memory::multiboot auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; auto number_of_entries = total_entries_size / actual_entry_size; - memory_areas = &mminfo->entries; - area_count = number_of_entries; + begin_area = memory_area_container::iterator{&mminfo->entries}; + end_area = begin_area + number_of_entries; } auto process_elf_sections(elf_symbols_section_header * symbol, uint64_t & kernel_start, @@ -45,40 +49,30 @@ namespace teachos::arch::memory::multiboot exception_handling::assert(expected_total_size == actual_total_size, "[Multiboot Reader] Unexpected elf symbols section header total size"); - auto begin = reinterpret_cast(&symbol->end); + auto begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; auto end = begin + symbol->number_of_sections; exception_handling::assert(begin->is_null(), "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); - std::size_t symbol_table_section_count = 0U; - std::size_t dynamic_section_count = 0U; + elf_section_header_container container{begin, end}; - for (auto section = begin; section != end; ++section) - { - if (section->virtual_address < kernel_start) - { - kernel_start = section->virtual_address; - } - auto virtual_address_end = section->virtual_address + section->section_size; - if (virtual_address_end > kernel_end) - { - kernel_end = virtual_address_end; - } + auto elf_section_with_lowest_virtual_address = + std::ranges::min_element(container, [](elf_section_header const & a, elf_section_header const & b) { + return a.virtual_address < b.virtual_address; + }); - switch (section->type) - { - case elf_section_type::DYNAMIC_SYMBOL_TABLE: - case elf_section_type::SYMBOL_TABLE: - symbol_table_section_count++; - break; - case elf_section_type::DYNAMIC: - dynamic_section_count++; - break; - default: - // All other cases are not important and can be ignored - break; - } - } + auto elf_section_with_highest_virtual_address = + std::ranges::max_element(container, [](elf_section_header const & a, elf_section_header const & b) { + auto a_virtual_address_end = a.virtual_address + a.section_size; + auto b_virtual_address_end = b.virtual_address + b.section_size; + return a_virtual_address_end < b_virtual_address_end; + }); + + auto symbol_table_section_count = std::ranges::count_if(container, [](elf_section_header const & section) { + return section.type == elf_section_type::DYNAMIC_SYMBOL_TABLE || section.type == elf_section_type::SYMBOL_TABLE; + }); + auto dynamic_section_count = std::ranges::count_if( + container, [](elf_section_header const & section) { return section.type == elf_section_type::DYNAMIC; }); exception_handling::assert( symbol_table_section_count == 1U, @@ -86,15 +80,26 @@ namespace teachos::arch::memory::multiboot exception_handling::assert( dynamic_section_count <= 1U, "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); + + auto lowest_elf_section = *elf_section_with_lowest_virtual_address; + kernel_start = lowest_elf_section.virtual_address; + + auto highest_elf_section = *elf_section_with_highest_virtual_address; + kernel_end = highest_elf_section.virtual_address + highest_elf_section.section_size; } } // namespace auto read_multiboot2() -> memory_information { - memory_information mem_info{UINT64_MAX, 0U, boot::multiboot_information_pointer, 0U, nullptr, 0U}; - - auto * multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); - auto multiboot_tag = &(multiboot_information_pointer->tags); + memory_information mem_info{UINT64_MAX, + 0U, + boot::multiboot_information_pointer, + 0U, + memory_area_container::iterator{}, + memory_area_container::iterator{}}; + + auto multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); + auto multiboot_tag = &multiboot_information_pointer->tags; mem_info.multiboot_end = mem_info.multiboot_start + multiboot_information_pointer->total_size; for (auto tag = multiboot_tag; tag->type != tag_type::END; tag = align_to_8_byte_boundary(tag, tag->size)) @@ -108,11 +113,11 @@ namespace teachos::arch::memory::multiboot } case tag_type::MEMORY_MAP: { auto mminfo = reinterpret_cast(tag); - process_memory_map(mminfo, mem_info.memory_areas, mem_info.area_count); + process_memory_map(mminfo, mem_info.begin_area, mem_info.end_area); break; } default: - // All other cases are not important and can be ignored + // All other cases are not important and can be ignored. break; } } -- cgit v1.2.3 From e5925df93411429340d2887594004aaa690d2ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 28 Oct 2024 13:32:09 +0000 Subject: Adjust constant and make all possible variables const --- arch/x86_64/src/memory/multiboot/reader.cpp | 47 ++++++++++++++--------------- 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 228aa09..c3a78df 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -21,14 +21,14 @@ namespace teachos::arch::memory::multiboot auto process_memory_map(memory_map_header * mminfo, memory_area_container::iterator & begin_area, memory_area_container::iterator & end_area) -> void { - auto expected_entry_size = mminfo->entry_size; - constexpr auto actual_entry_size = sizeof(memory_area); + auto const expected_entry_size = mminfo->entry_size; + auto constexpr actual_entry_size = sizeof(memory_area); exception_handling::assert(expected_entry_size == actual_entry_size, "[Multiboot Reader] Unexpected memory area entry size"); - auto total_size = mminfo->info.size; - auto total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; - auto number_of_entries = total_entries_size / actual_entry_size; + auto const total_size = mminfo->info.size; + auto const total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; + auto const number_of_entries = total_entries_size / actual_entry_size; begin_area = memory_area_container::iterator{&mminfo->entries}; end_area = begin_area + number_of_entries; @@ -37,41 +37,41 @@ namespace teachos::arch::memory::multiboot auto process_elf_sections(elf_symbols_section_header * symbol, uint64_t & kernel_start, uint64_t & kernel_end) -> void { - auto expected_entry_size = symbol->entry_size; - constexpr auto actual_entry_size = sizeof(elf_section_header); + auto const expected_entry_size = symbol->entry_size; + auto constexpr actual_entry_size = sizeof(elf_section_header); exception_handling::assert(expected_entry_size == actual_entry_size, "[Multiboot Reader] Unexpected elf section header entry size"); - auto expected_total_size = symbol->info.size; - auto actual_total_entry_size = actual_entry_size * symbol->number_of_sections; - constexpr auto actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); - auto actual_total_size = actual_total_entry_size + actual_total_section_size; + auto const expected_total_size = symbol->info.size; + auto const actual_total_entry_size = actual_entry_size * symbol->number_of_sections; + auto constexpr actual_total_section_size = sizeof(elf_symbols_section_header) - sizeof(uint32_t); + auto const actual_total_size = actual_total_entry_size + actual_total_section_size; exception_handling::assert(expected_total_size == actual_total_size, "[Multiboot Reader] Unexpected elf symbols section header total size"); - auto begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; - auto end = begin + symbol->number_of_sections; + auto const begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; + auto const end = begin + symbol->number_of_sections; exception_handling::assert(begin->is_null(), "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); elf_section_header_container container{begin, end}; - auto elf_section_with_lowest_virtual_address = + auto const elf_section_with_lowest_virtual_address = std::ranges::min_element(container, [](elf_section_header const & a, elf_section_header const & b) { return a.virtual_address < b.virtual_address; }); - auto elf_section_with_highest_virtual_address = + auto const elf_section_with_highest_virtual_address = std::ranges::max_element(container, [](elf_section_header const & a, elf_section_header const & b) { auto a_virtual_address_end = a.virtual_address + a.section_size; auto b_virtual_address_end = b.virtual_address + b.section_size; return a_virtual_address_end < b_virtual_address_end; }); - auto symbol_table_section_count = std::ranges::count_if(container, [](elf_section_header const & section) { + auto const symbol_table_section_count = std::ranges::count_if(container, [](elf_section_header const & section) { return section.type == elf_section_type::DYNAMIC_SYMBOL_TABLE || section.type == elf_section_type::SYMBOL_TABLE; }); - auto dynamic_section_count = std::ranges::count_if( + auto const dynamic_section_count = std::ranges::count_if( container, [](elf_section_header const & section) { return section.type == elf_section_type::DYNAMIC; }); exception_handling::assert( @@ -81,10 +81,10 @@ namespace teachos::arch::memory::multiboot dynamic_section_count <= 1U, "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); - auto lowest_elf_section = *elf_section_with_lowest_virtual_address; + auto const lowest_elf_section = *elf_section_with_lowest_virtual_address; kernel_start = lowest_elf_section.virtual_address; - auto highest_elf_section = *elf_section_with_highest_virtual_address; + auto const highest_elf_section = *elf_section_with_highest_virtual_address; kernel_end = highest_elf_section.virtual_address + highest_elf_section.section_size; } } // namespace @@ -98,8 +98,8 @@ namespace teachos::arch::memory::multiboot memory_area_container::iterator{}, memory_area_container::iterator{}}; - auto multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); - auto multiboot_tag = &multiboot_information_pointer->tags; + auto const multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); + auto const multiboot_tag = &multiboot_information_pointer->tags; mem_info.multiboot_end = mem_info.multiboot_start + multiboot_information_pointer->total_size; for (auto tag = multiboot_tag; tag->type != tag_type::END; tag = align_to_8_byte_boundary(tag, tag->size)) @@ -107,12 +107,12 @@ namespace teachos::arch::memory::multiboot switch (tag->type) { case tag_type::ELF_SECTIONS: { - auto symbol = reinterpret_cast(tag); + auto const symbol = reinterpret_cast(tag); process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end); break; } case tag_type::MEMORY_MAP: { - auto mminfo = reinterpret_cast(tag); + auto const mminfo = reinterpret_cast(tag); process_memory_map(mminfo, mem_info.begin_area, mem_info.end_area); break; } @@ -121,7 +121,6 @@ namespace teachos::arch::memory::multiboot break; } } - return mem_info; } } // namespace teachos::arch::memory::multiboot -- cgit v1.2.3 From 5ffe7d69545bf098efdd70f105a5df83304b211a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 2 Nov 2024 11:49:38 +0000 Subject: Add physical frame iterator and continue implementing kernel mapping. --- arch/x86_64/src/memory/multiboot/reader.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index c3a78df..e35baf3 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -34,8 +34,9 @@ namespace teachos::arch::memory::multiboot end_area = begin_area + number_of_entries; } - auto process_elf_sections(elf_symbols_section_header * symbol, uint64_t & kernel_start, - uint64_t & kernel_end) -> void + auto process_elf_sections(elf_symbols_section_header * symbol, std::size_t & kernel_start, std::size_t & kernel_end, + elf_section_header_container::iterator & begin_kernel, + elf_section_header_container::iterator & end_kernel) -> void { auto const expected_entry_size = symbol->entry_size; auto constexpr actual_entry_size = sizeof(elf_section_header); @@ -49,12 +50,12 @@ namespace teachos::arch::memory::multiboot exception_handling::assert(expected_total_size == actual_total_size, "[Multiboot Reader] Unexpected elf symbols section header total size"); - auto const begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; - auto const end = begin + symbol->number_of_sections; - exception_handling::assert(begin->is_null(), + begin_kernel = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; + end_kernel = begin_kernel + symbol->number_of_sections; + exception_handling::assert(begin_kernel->is_null(), "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); - elf_section_header_container container{begin, end}; + elf_section_header_container container{begin_kernel, end_kernel}; auto const elf_section_with_lowest_virtual_address = std::ranges::min_element(container, [](elf_section_header const & a, elf_section_header const & b) { @@ -93,6 +94,8 @@ namespace teachos::arch::memory::multiboot { memory_information mem_info{UINT64_MAX, 0U, + elf_section_header_container::iterator{}, + elf_section_header_container::iterator{}, boot::multiboot_information_pointer, 0U, memory_area_container::iterator{}, @@ -108,7 +111,8 @@ namespace teachos::arch::memory::multiboot { case tag_type::ELF_SECTIONS: { auto const symbol = reinterpret_cast(tag); - process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end); + process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end, mem_info.begin_kernel, + mem_info.end_kernel); break; } case tag_type::MEMORY_MAP: { -- cgit v1.2.3 From 5ade8e0d5f190f2e439f81232b38b90c49c0cd1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 2 Nov 2024 12:38:22 +0000 Subject: Add comments and improve multiboot information struct usability. --- arch/x86_64/src/memory/multiboot/reader.cpp | 42 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index e35baf3..7bdf48f 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -18,8 +18,7 @@ namespace teachos::arch::memory::multiboot return reinterpret_cast(reinterpret_cast(ptr) + ((size + 7) & ~7)); } - auto process_memory_map(memory_map_header * mminfo, memory_area_container::iterator & begin_area, - memory_area_container::iterator & end_area) -> void + auto process_memory_map(memory_map_header * mminfo) -> memory_area_container { auto const expected_entry_size = mminfo->entry_size; auto constexpr actual_entry_size = sizeof(memory_area); @@ -30,13 +29,13 @@ namespace teachos::arch::memory::multiboot auto const total_entries_size = total_size - sizeof(memory_map_header) + actual_entry_size; auto const number_of_entries = total_entries_size / actual_entry_size; - begin_area = memory_area_container::iterator{&mminfo->entries}; - end_area = begin_area + number_of_entries; + auto const begin = memory_area_container::iterator{&mminfo->entries}; + auto const end = begin + number_of_entries; + return memory_area_container{begin, end}; } - auto process_elf_sections(elf_symbols_section_header * symbol, std::size_t & kernel_start, std::size_t & kernel_end, - elf_section_header_container::iterator & begin_kernel, - elf_section_header_container::iterator & end_kernel) -> void + auto process_elf_sections(elf_symbols_section_header * symbol, std::size_t & kernel_start, + std::size_t & kernel_end) -> elf_section_header_container { auto const expected_entry_size = symbol->entry_size; auto constexpr actual_entry_size = sizeof(elf_section_header); @@ -50,30 +49,30 @@ namespace teachos::arch::memory::multiboot exception_handling::assert(expected_total_size == actual_total_size, "[Multiboot Reader] Unexpected elf symbols section header total size"); - begin_kernel = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; - end_kernel = begin_kernel + symbol->number_of_sections; - exception_handling::assert(begin_kernel->is_null(), + auto const begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; + auto const end = begin + symbol->number_of_sections; + exception_handling::assert(begin->is_null(), "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); - elf_section_header_container container{begin_kernel, end_kernel}; + elf_section_header_container sections{begin, end}; auto const elf_section_with_lowest_virtual_address = - std::ranges::min_element(container, [](elf_section_header const & a, elf_section_header const & b) { + std::ranges::min_element(sections, [](elf_section_header const & a, elf_section_header const & b) { return a.virtual_address < b.virtual_address; }); auto const elf_section_with_highest_virtual_address = - std::ranges::max_element(container, [](elf_section_header const & a, elf_section_header const & b) { + std::ranges::max_element(sections, [](elf_section_header const & a, elf_section_header const & b) { auto a_virtual_address_end = a.virtual_address + a.section_size; auto b_virtual_address_end = b.virtual_address + b.section_size; return a_virtual_address_end < b_virtual_address_end; }); - auto const symbol_table_section_count = std::ranges::count_if(container, [](elf_section_header const & section) { + auto const symbol_table_section_count = std::ranges::count_if(sections, [](elf_section_header const & section) { return section.type == elf_section_type::DYNAMIC_SYMBOL_TABLE || section.type == elf_section_type::SYMBOL_TABLE; }); auto const dynamic_section_count = std::ranges::count_if( - container, [](elf_section_header const & section) { return section.type == elf_section_type::DYNAMIC; }); + sections, [](elf_section_header const & section) { return section.type == elf_section_type::DYNAMIC; }); exception_handling::assert( symbol_table_section_count == 1U, @@ -87,6 +86,8 @@ namespace teachos::arch::memory::multiboot auto const highest_elf_section = *elf_section_with_highest_virtual_address; kernel_end = highest_elf_section.virtual_address + highest_elf_section.section_size; + + return sections; } } // namespace @@ -94,12 +95,10 @@ namespace teachos::arch::memory::multiboot { memory_information mem_info{UINT64_MAX, 0U, - elf_section_header_container::iterator{}, - elf_section_header_container::iterator{}, + elf_section_header_container{}, boot::multiboot_information_pointer, 0U, - memory_area_container::iterator{}, - memory_area_container::iterator{}}; + memory_area_container{}}; auto const multiboot_information_pointer = reinterpret_cast(boot::multiboot_information_pointer); auto const multiboot_tag = &multiboot_information_pointer->tags; @@ -111,13 +110,12 @@ namespace teachos::arch::memory::multiboot { case tag_type::ELF_SECTIONS: { auto const symbol = reinterpret_cast(tag); - process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end, mem_info.begin_kernel, - mem_info.end_kernel); + mem_info.sections = process_elf_sections(symbol, mem_info.kernel_start, mem_info.kernel_end); break; } case tag_type::MEMORY_MAP: { auto const mminfo = reinterpret_cast(tag); - process_memory_map(mminfo, mem_info.begin_area, mem_info.end_area); + mem_info.areas = process_memory_map(mminfo); break; } default: -- cgit v1.2.3 From 67be3c58bef94fece14d4e3a79f3559649e9a74c Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 3 Nov 2024 12:12:41 +0000 Subject: rename member and use correct address --- .../src/memory/multiboot/elf_symbols_section.cpp | 2 +- arch/x86_64/src/memory/multiboot/reader.cpp | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp b/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp index 953a57d..f5d126b 100644 --- a/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp +++ b/arch/x86_64/src/memory/multiboot/elf_symbols_section.cpp @@ -7,7 +7,7 @@ namespace teachos::arch::memory::multiboot auto elf_section_header::is_null() const -> bool { return name_table_index == 0U && type == elf_section_type::INACTIVE && flags == elf_section_flags(0U) && - virtual_address == 0U && file_offset == 0U && additional_information == 0U && address_alignment == 0U && + physical_address == 0U && file_offset == 0U && additional_information == 0U && address_alignment == 0U && fixed_table_entry_size == 0U; } } // namespace teachos::arch::memory::multiboot diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 7bdf48f..4576085 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -56,16 +56,16 @@ namespace teachos::arch::memory::multiboot elf_section_header_container sections{begin, end}; - auto const elf_section_with_lowest_virtual_address = + auto const elf_section_with_lowest_physical_address = std::ranges::min_element(sections, [](elf_section_header const & a, elf_section_header const & b) { - return a.virtual_address < b.virtual_address; + return a.physical_address < b.physical_address; }); - auto const elf_section_with_highest_virtual_address = + auto const elf_section_with_highest_physical_address = std::ranges::max_element(sections, [](elf_section_header const & a, elf_section_header const & b) { - auto a_virtual_address_end = a.virtual_address + a.section_size; - auto b_virtual_address_end = b.virtual_address + b.section_size; - return a_virtual_address_end < b_virtual_address_end; + auto a_physical_address_end = a.physical_address + a.section_size; + auto b_physical_address_end = b.physical_address + b.section_size; + return a_physical_address_end < b_physical_address_end; }); auto const symbol_table_section_count = std::ranges::count_if(sections, [](elf_section_header const & section) { @@ -81,11 +81,11 @@ namespace teachos::arch::memory::multiboot dynamic_section_count <= 1U, "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); - auto const lowest_elf_section = *elf_section_with_lowest_virtual_address; - kernel_start = lowest_elf_section.virtual_address; + auto const lowest_elf_section = *elf_section_with_lowest_physical_address; + kernel_start = lowest_elf_section.physical_address; - auto const highest_elf_section = *elf_section_with_highest_virtual_address; - kernel_end = highest_elf_section.virtual_address + highest_elf_section.section_size; + auto const highest_elf_section = *elf_section_with_highest_physical_address; + kernel_end = highest_elf_section.physical_address + highest_elf_section.section_size; return sections; } -- cgit v1.2.3 From 162bea11c7a4f1854cde53920b4c14b4eadf539d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 4 Nov 2024 12:13:44 +0000 Subject: WIP attempt to fix crashes --- arch/x86_64/src/memory/multiboot/reader.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 4576085..178cc45 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -51,28 +51,27 @@ namespace teachos::arch::memory::multiboot auto const begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; auto const end = begin + symbol->number_of_sections; - exception_handling::assert(begin->is_null(), + exception_handling::assert((*begin)->is_null(), "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); elf_section_header_container sections{begin, end}; - auto const elf_section_with_lowest_physical_address = - std::ranges::min_element(sections, [](elf_section_header const & a, elf_section_header const & b) { - return a.physical_address < b.physical_address; - }); + auto const elf_section_with_lowest_physical_address = std::ranges::min_element( + sections, [](auto const & a, auto const & b) { return a->physical_address < b->physical_address; }); auto const elf_section_with_highest_physical_address = - std::ranges::max_element(sections, [](elf_section_header const & a, elf_section_header const & b) { - auto a_physical_address_end = a.physical_address + a.section_size; - auto b_physical_address_end = b.physical_address + b.section_size; + std::ranges::max_element(sections, [](auto const & a, auto const & b) { + auto a_physical_address_end = a->physical_address + a->section_size; + auto b_physical_address_end = b->physical_address + b->section_size; return a_physical_address_end < b_physical_address_end; }); - auto const symbol_table_section_count = std::ranges::count_if(sections, [](elf_section_header const & section) { - return section.type == elf_section_type::DYNAMIC_SYMBOL_TABLE || section.type == elf_section_type::SYMBOL_TABLE; + auto const symbol_table_section_count = std::ranges::count_if(sections, [](auto const & section) { + return section->type == elf_section_type::DYNAMIC_SYMBOL_TABLE || + section->type == elf_section_type::SYMBOL_TABLE; }); auto const dynamic_section_count = std::ranges::count_if( - sections, [](elf_section_header const & section) { return section.type == elf_section_type::DYNAMIC; }); + sections, [](auto const & section) { return section->type == elf_section_type::DYNAMIC; }); exception_handling::assert( symbol_table_section_count == 1U, @@ -82,10 +81,10 @@ namespace teachos::arch::memory::multiboot "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); auto const lowest_elf_section = *elf_section_with_lowest_physical_address; - kernel_start = lowest_elf_section.physical_address; + kernel_start = lowest_elf_section->physical_address; auto const highest_elf_section = *elf_section_with_highest_physical_address; - kernel_end = highest_elf_section.physical_address + highest_elf_section.section_size; + kernel_end = highest_elf_section->physical_address + highest_elf_section->section_size; return sections; } -- cgit v1.2.3 From 30466aeb3da181c21bd451f32c1ff97e53a55dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 4 Nov 2024 13:29:03 +0000 Subject: Use more concepts and seperate iterator implementations --- arch/x86_64/src/memory/multiboot/reader.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'arch/x86_64/src/memory/multiboot') diff --git a/arch/x86_64/src/memory/multiboot/reader.cpp b/arch/x86_64/src/memory/multiboot/reader.cpp index 178cc45..1dd18ff 100644 --- a/arch/x86_64/src/memory/multiboot/reader.cpp +++ b/arch/x86_64/src/memory/multiboot/reader.cpp @@ -51,27 +51,26 @@ namespace teachos::arch::memory::multiboot auto const begin = elf_section_header_container::iterator{reinterpret_cast(&symbol->end)}; auto const end = begin + symbol->number_of_sections; - exception_handling::assert((*begin)->is_null(), + exception_handling::assert(begin->is_null(), "[Multiboot Reader] Elf symbols section not starting with SHT_NULL section"); elf_section_header_container sections{begin, end}; auto const elf_section_with_lowest_physical_address = std::ranges::min_element( - sections, [](auto const & a, auto const & b) { return a->physical_address < b->physical_address; }); + sections, [](auto const & a, auto const & b) { return a.physical_address < b.physical_address; }); auto const elf_section_with_highest_physical_address = std::ranges::max_element(sections, [](auto const & a, auto const & b) { - auto a_physical_address_end = a->physical_address + a->section_size; - auto b_physical_address_end = b->physical_address + b->section_size; + auto a_physical_address_end = a.physical_address + a.section_size; + auto b_physical_address_end = b.physical_address + b.section_size; return a_physical_address_end < b_physical_address_end; }); auto const symbol_table_section_count = std::ranges::count_if(sections, [](auto const & section) { - return section->type == elf_section_type::DYNAMIC_SYMBOL_TABLE || - section->type == elf_section_type::SYMBOL_TABLE; + return section.type == elf_section_type::DYNAMIC_SYMBOL_TABLE || section.type == elf_section_type::SYMBOL_TABLE; }); auto const dynamic_section_count = std::ranges::count_if( - sections, [](auto const & section) { return section->type == elf_section_type::DYNAMIC; }); + sections, [](auto const & section) { return section.type == elf_section_type::DYNAMIC; }); exception_handling::assert( symbol_table_section_count == 1U, @@ -81,10 +80,10 @@ namespace teachos::arch::memory::multiboot "[Multiboot Reader] ELF Specifications allows only (1) or less dynamic sections, but got more"); auto const lowest_elf_section = *elf_section_with_lowest_physical_address; - kernel_start = lowest_elf_section->physical_address; + kernel_start = lowest_elf_section.physical_address; auto const highest_elf_section = *elf_section_with_highest_physical_address; - ke