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/memory/allocator/physical_frame.hpp | 105 ++++------------ .../arch/memory/multiboot/elf_symbols_section.hpp | 6 +- .../include/arch/memory/multiboot/memory_map.hpp | 2 +- .../include/arch/memory/paging/kernel_mapper.hpp | 25 ++-- arch/x86_64/include/arch/shared/container.hpp | 5 +- .../include/arch/shared/random_access_iterator.hpp | 138 ++++++++++++++------- 6 files changed, 134 insertions(+), 147 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp index 89e3cf8..43a22a2 100644 --- a/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp +++ b/arch/x86_64/include/arch/memory/allocator/physical_frame.hpp @@ -1,6 +1,9 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_PHYSICAL_FRAME_HPP #define TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_PHYSICAL_FRAME_HPP +#include "arch/shared/container.hpp" +#include "arch/shared/random_access_iterator.hpp" + #include #include #include @@ -48,68 +51,19 @@ namespace teachos::arch::memory::allocator */ auto start_address() const -> physical_address; - /** - * @brief Defaulted equals operator. - */ - auto operator==(const physical_frame & other) const -> bool = default; - - /** - * @brief Defaulted three-way comparsion operator. - */ - auto operator<=>(const physical_frame & other) const -> std::partial_ordering = default; - - std::size_t frame_number = - {}; ///< Index number of the current physical frame, used to distinguish it from other frames. - }; - - /** - * @brief Iterator for the frame number - */ - struct physical_frame_iterator - { - using iterator_category = std::random_access_iterator_tag; ///< Iterator category of this type. - using difference_type = std::ptrdiff_t; ///< Type when diving one instance of this iterator by another. - using value_type = physical_frame; ///< Underlying value pointed to by this iterator. - - /** - * @brief Defaulted constructor. - */ - physical_frame_iterator() = default; - - /** - * @brief Constructor. - * - * @param value Underlying value the iterator should point too - */ - explicit physical_frame_iterator(value_type value); - - /** - * @brief Dereferences the initally given pointer to its value. - * - * @return Reference to the value. - */ - auto operator*() -> value_type &; - - /** - * @brief Get underlying value, which is the intially passed pointer. - * - * @return Underlying value passed intially. - */ - auto operator->() -> value_type *; - /** * @brief Post increment operator. Returns a copy of the value. * * @return Copy of the incremented underlying address. */ - auto operator++(int) -> physical_frame_iterator; + auto operator++(int) -> physical_frame; /** * @brief Pre increment operator. Returns a reference to the changed value. * * @return Reference to the incremented underlying address. */ - auto operator++() -> physical_frame_iterator &; + auto operator++() -> physical_frame &; /** * @brief Addition assignment operator. Returns a reference to the changed value. @@ -117,7 +71,7 @@ namespace teachos::arch::memory::allocator * @param operand Value we want to add to the underlying address. * @return Reference to the changed underlying address. */ - auto operator+=(difference_type operand) -> physical_frame_iterator &; + auto operator+=(std::size_t operand) -> physical_frame &; /** * @brief Subtraction assignment operator. Returns a reference to the changed value. @@ -125,23 +79,21 @@ namespace teachos::arch::memory::allocator * @param operand Value we want to subtract from the underlying address. * @return Reference to the changed underlying address. */ - auto operator-=(difference_type operand) -> physical_frame_iterator &; + auto operator-=(std::size_t operand) -> physical_frame &; /** - * @brief Addition operator. Returns the changed value. + * @brief Post increment operator. Returns a copy of the value. * - * @param operand Value we want to add to a copy of the underlying address. - * @return Copy of underlying address incremented by the given value. + * @return Copy of the incremented underlying address. */ - auto operator+(difference_type operand) const -> physical_frame_iterator; + auto operator+(std::size_t operand) const -> physical_frame; /** - * @brief Subtraction operator. Returns the changed value. + * @brief Pre increment operator. Returns a reference to the changed value. * - * @param operand Value we want to subtrcat from a copy of the underlying address. - * @return Copy of underlying address decremented by the given value. + * @return Reference to the incremented underlying address. */ - auto operator-(difference_type operand) const -> physical_frame_iterator; + auto operator-(std::size_t operand) const -> physical_frame; /** * @brief Subtraction operator. Returns the size difference between two iterators. @@ -149,36 +101,23 @@ namespace teachos::arch::memory::allocator * @param other Other iterator we want to substract the underlying address with ours. * @return Size difference between the underlying address of this instance and the given iterator. */ - auto operator-(const physical_frame_iterator & other) const -> difference_type; - - /** - * @brief Index operator overload. Returns a reference to the value at the given index. Simply returns the - * dereferenced underlying pointer incremented by the given index. - * - * @param index Index we want to access and get the value from. - * @return Reference to the value at the given index. - */ - auto operator[](difference_type index) const -> value_type; + auto operator-(physical_frame const & other) const -> std::size_t; /** - * @brief Defaulted comparsion operator. Simply compares the memory address of both iterators. - * - * @param other Other iterator to compare to. - * @return Whether both iterators point to the same underlying address in memory. + * @brief Defaulted equals operator. */ - auto operator==(physical_frame_iterator const & other) const -> bool = default; + auto operator==(physical_frame const & other) const -> bool = default; /** - * @brief Defaulted threeway comparsion operator. Simply compares the memory address of both iterators. - * - * @param other Other iterator to compare to. - * @return Whether the given iterator is smaller or larger than this iterator. + * @brief Defaulted three-way comparsion operator. */ - auto operator<=>(physical_frame_iterator const & other) const -> std::strong_ordering = default; + auto operator<=>(physical_frame const & other) const -> std::partial_ordering = default; - private: - value_type value = {}; ///< Underlying address the iterator is currently pointing too. + std::size_t frame_number = + {}; ///< Index number of the current physical frame, used to distinguish it from other frames. }; + + typedef shared::container> frame_container; } // namespace teachos::arch::memory::allocator #endif // TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_PHYSICAL_FRAME_HPP diff --git a/arch/x86_64/include/arch/memory/multiboot/elf_symbols_section.hpp b/arch/x86_64/include/arch/memory/multiboot/elf_symbols_section.hpp index e29590d..6227fc9 100644 --- a/arch/x86_64/include/arch/memory/multiboot/elf_symbols_section.hpp +++ b/arch/x86_64/include/arch/memory/multiboot/elf_symbols_section.hpp @@ -122,8 +122,8 @@ namespace teachos::arch::memory::multiboot elf_section_flags flags; ///< 1-bit flgas that describe section attributes. uint64_t physical_address; ///< If section appears in memory image of a process, gives address at which the ///< sections first byte should reside, otherwise 0. - uint64_t file_offset; ///< Offset from the beginning of the file to the first byte in the section. SHT_NOBITS - ///< contains the conceptual placement instead (because it occupies no space in the file). + uint64_t file_offset; ///< Offset from the beginning of the file to the first byte in the section. SHT_NOBITS + ///< contains the conceptual placement instead (because it occupies no space in the file). uint64_t section_size; ///< Complete section size in bytes, SHT_NOBITS may have non-zero value but will always ///< occupy no space in the file. uint32_t other_section; ///< Section header table index link, behaviour varies on type @@ -162,7 +162,7 @@ namespace teachos::arch::memory::multiboot ///< contained in the section, to ensure byte alignment is actually 4 byte. }; - typedef shared::container> elf_section_header_container; + typedef shared::container> elf_section_header_container; } // namespace teachos::arch::memory::multiboot #endif // TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_ELF_SYBOLS_SECTION_HPP diff --git a/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp b/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp index 910eecb..ccd5cf0 100644 --- a/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp +++ b/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp @@ -46,7 +46,7 @@ namespace teachos::arch::memory::multiboot struct memory_area entries; ///< Specific memory regions. }; - typedef shared::container> memory_area_container; + typedef shared::container> memory_area_container; } // namespace teachos::arch::memory::multiboot #endif // TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_MEMORY_MAP_HPP diff --git a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp index 2dd0387..d301c91 100644 --- a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp +++ b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp @@ -9,11 +9,9 @@ namespace teachos::arch::memory::paging { namespace { - virtual_page UNUSED_VIRTUAL_PAGE = virtual_page::containing_address(0xCAFEBABE); + virtual_page constexpr UNUSED_VIRTUAL_PAGE(0xCAFEBABE); } // namespace - typedef shared::container frame_container; - /** * @brief Kernel mapper that allows to remap the kernel elf sections in C++. * @@ -103,20 +101,23 @@ namespace teachos::arch::memory::paging exception_handling::assert(!mem_info.sections.empty(), "[Kernel Mapper] Kernel elf sections empty"); for (auto const & section : mem_info.sections) { - if (!section.flags.contains_flags(multiboot::elf_section_flags::OCCUPIES_MEMORY)) + if (!section->flags.contains_flags(multiboot::elf_section_flags::OCCUPIES_MEMORY)) { continue; } - exception_handling::assert(section.physical_address % allocator::PAGE_FRAME_SIZE == 0U, + exception_handling::assert(section->physical_address % allocator::PAGE_FRAME_SIZE == 0U, "[Kernel Mapper] Section must be page aligned"); - auto const start_frame = allocator::physical_frame::containing_address(section.physical_address); - auto const end_frame = - allocator::physical_frame::containing_address(section.physical_address + section.section_size); + auto const start_frame = allocator::physical_frame::containing_address(section->physical_address); + // End address is exclusive, so that it is not part of the section anymore (one past the last frame of this + // section). But end frame would now point to the actual last frame and not one past the last frame, therefore + // we increment by one to get one past the last frame of this section. + auto end_frame = + ++(allocator::physical_frame::containing_address(section->physical_address + section->section_size - 1)); - allocator::physical_frame_iterator const begin{start_frame}; - allocator::physical_frame_iterator const end{end_frame}; - frame_container frames{begin, end}; - entry entry{section.flags}; + allocator::frame_container::iterator const begin{start_frame}; + allocator::frame_container::iterator const end{end_frame}; + allocator::frame_container frames{begin, end}; + entry entry{section->flags}; for (auto const & frame : frames) { diff --git a/arch/x86_64/include/arch/shared/container.hpp b/arch/x86_64/include/arch/shared/container.hpp index bc2a5f6..9b4192e 100644 --- a/arch/x86_64/include/arch/shared/container.hpp +++ b/arch/x86_64/include/arch/shared/container.hpp @@ -1,12 +1,13 @@ #ifndef TEACHOS_ARCH_X86_64_SHARED_CONTAINER_HPP #define TEACHOS_ARCH_X86_64_SHARED_CONTAINER_HPP -#include "arch/exception_handling/assert.hpp" - #include namespace teachos::arch::shared { + template + concept Iterator = std::contiguous_iterator; + /** * @brief Read-only container for given template type, that allow to easily use this container instance in C++20 * ranges calls. diff --git a/arch/x86_64/include/arch/shared/random_access_iterator.hpp b/arch/x86_64/include/arch/shared/random_access_iterator.hpp index b1a800c..69617f3 100644 --- a/arch/x86_64/include/arch/shared/random_access_iterator.hpp +++ b/arch/x86_64/include/arch/shared/random_access_iterator.hpp @@ -5,18 +5,25 @@ namespace teachos::arch::shared { + template + concept Iterable = std::incrementable && std::three_way_comparable && std::indirectly_readable; + /** - * @brief Generic random access iterator for given template type. Can be a nullptr, ensure to check when using this - * iterator. Allows to easily use this iterator instance in algorithm calls. + * @brief Generic random access iterator for given template type. Allows any value that Allows to easily use this + * iterator instance in algorithm calls. * * @tparam T Value the iterator points too. */ - template + template struct random_access_iterator { - using iterator_category = std::random_access_iterator_tag; ///< Iterator category of this type. - using difference_type = std::ptrdiff_t; ///< Type when diving one instance of this iterator by another. - using value_type = T; ///< Underlying value pointed to by this iterator. + using iterator_category = std::contiguous_iterator_tag; ///< Iterator category of this type. + using difference_type = std::ptrdiff_t; ///< Type when diving one instance of this iterator by another. + using value_type = T; ///< Underlying value pointed to by this iterator. + using reference_type = value_type &; ///< Reference to value returned by dereference * operation. + using pointer_type = value_type *; ///< Pointer to value returned by arrow -> operation. + using const_reference_type = value_type const &; ///< Reference to value returned by const dereference * operation. + using const_pointer_type = value_type const *; ///< Pointer to value returned by const arrow -> operation. /** * @brief Defaulted constructor. @@ -26,124 +33,162 @@ namespace teachos::arch::shared /** * @brief Constructor. * - * @param p Underlying address the iterator should point too. + * @param value Underlying value the iterator should point too and increment or decrement. */ - explicit random_access_iterator(value_type * p) - : ptr(p) + explicit random_access_iterator(value_type value) + : value(value) { // Nothing to do } /** - * @brief Dereferences the initally given pointer to its value. + * @brief Gets a reference to the underlying value. * * @return Reference to the value. */ - auto operator*() const -> value_type & { return *ptr; } + auto operator*() -> reference_type { return value; } /** - * @brief Get underlying value, which is the intially passed pointer. + * @brief Gets a pointer to the underlying value. * * @return Underlying value passed intially. */ - auto operator->() const -> value_type * { return ptr; } + auto operator->() -> pointer_type { return &value; } /** - * @brief Post increment operator. Returns a copy of the value. + * @brief Gets a const reference to the underlying value. * - * @return Copy of the incremented underlying address. + * @return Reference to the value. */ - auto operator++(int) -> random_access_iterator + auto operator*() const -> const_reference_type { return value; } + + /** + * @brief Gets a const pointer to the underlying value. + * + * @return Underlying value passed intially. + */ + auto operator->() const -> const_pointer_type { return &value; } + + /** + * @brief Pre decrement operator. Returns a reference to the changed value. + * + * @return Reference to the decrement underlying value. + */ + auto operator--() -> random_access_iterator & { - random_access_iterator const old_value = *this; - ++ptr; - return old_value; + --value; + return *this; } /** * @brief Pre increment operator. Returns a reference to the changed value. * - * @return Reference to the incremented underlying address. + * @return Reference to the incremented underlying value. */ auto operator++() -> random_access_iterator & { - ++ptr; + ++value; return *this; } + /** + * @brief Post decrement operator. Returns a copy of the value. + * + * @return Copy of the decremented underlying value. + */ + auto operator--(int) -> random_access_iterator + { + auto const old_value = *this; + --value; + return old_value; + } + + /** + * @brief Post increment operator. Returns a copy of the value. + * + * @return Copy of the incremented underlying value. + */ + auto operator++(int) -> random_access_iterator + { + auto const old_value = *this; + ++value; + return old_value; + } + /** * @brief Addition assignment operator. Returns a reference to the changed value. * - * @param value Value we want to add to the underlying address. - * @return Reference to the changed underlying address. + * @param operand Value we want to add to the underlying value the iterator is pointing too. + * @return Reference to the changed underlying value. */ - auto operator+=(difference_type value) -> random_access_iterator & + auto operator+=(difference_type operand) -> random_access_iterator & { - ptr += value; + value += operand; return *this; } /** * @brief Subtraction assignment operator. Returns a reference to the changed value. * - * @param value Value we want to subtract from the underlying address. - * @return Reference to the changed underlying address. + * @param operand Value we want to subtract from the underlying value the iterator is pointing too. + * @return Reference to the changed underlying value. */ - auto operator-=(difference_type value) -> random_access_iterator & + auto operator-=(difference_type operand) -> random_access_iterator & { - ptr -= value; + value -= operand; return *this; } /** * @brief Addition operator. Returns the changed value. * - * @param value Value we want to add to a copy of the underlying address. - * @return Copy of underlying address incremented by the given value. + * @param operand Value we want to add to a copy of the underlying value the iterator is pointing too. + * @return Copy of underlying value incremented by the given value. */ - auto operator+(difference_type value) const -> random_access_iterator + auto operator+(difference_type operand) const -> random_access_iterator { - return random_access_iterator{ptr + value}; + return random_access_iterator{value + operand}; } /** * @brief Subtraction operator. Returns the changed value. * - * @param value Value we want to subtrcat from a copy of the underlying address. - * @return Copy of underlying address decremented by the given value. + * @param operand Value we want to subtract from a copy of the underlying value the iterator is pointing too. + * @return Copy of underlying value decremented by the given value. */ - auto operator-(difference_type value) const -> random_access_iterator + auto operator-(difference_type operand) const -> random_access_iterator { - return random_access_iterator{ptr - value}; + return random_access_iterator{value - operand}; } /** * @brief Subtraction operator. Returns the size difference between two iterators. * - * @param other Other iterator we want to substract the underlying address with ours. - * @return Size difference between the underlying address of this instance and the given iterator. + * @param other Other iterator we want to substract the underlying value with ours. + * @return Size difference between the underlying value of this instance and the given iterator. */ - auto operator-(const random_access_iterator & other) const -> difference_type { return ptr - other.ptr; } + auto operator-(random_access_iterator const & other) const -> difference_type { return value - other.value; } /** * @brief Index operator overload. Returns a reference to the value at the given index. Simply returns the - * dereferenced underlying pointer incremented by the given index. + * underlying value the iterator is pointing too incremented by the given index. * * @param index Index we want to access and get the value from. * @return Reference to the value at the given index. */ - auto operator[](difference_type index) const -> value_type & { return *(ptr + index); } + auto operator[](difference_type index) const -> value_type const & { return value + index; } /** - * @brief Defaulted comparsion operator. Simply compares the memory address of both iterators. + * @brief Defaulted comparsion operator. Forwards to the comparsion operator of the given template type. * * @param other Other iterator to compare to. - * @return Whether both iterators point to the same underlying address in memory. + * @return Whether both iterators point to the same underlying value in memory. */ auto operator==(random_access_iterator const & other) const -> bool = default; /** - * @brief Defaulted threeway comparsion operator. Simply compares the memory address of both iterators. + * @brief Defaulted threeway comparsion operator. Forwards to the threeway comparsion operator of the given + * template type. * * @param other Other iterator to compare to. * @return Whether the given iterator is smaller or larger than this iterator. @@ -151,7 +196,8 @@ namespace teachos::arch::shared auto operator<=>(random_access_iterator const & other) const -> std::strong_ordering = default; private: - value_type * ptr = {}; ///< Underlying address the iterator is currently pointing too. + value_type value = + {}; ///< Underlying value the iterator is currently pointing too and should increment or decrement. }; } // namespace teachos::arch::shared -- cgit v1.2.3