From 6288868ebd728720236d6a857df2658bff2d6547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 29 Sep 2024 07:02:25 +0000 Subject: Pass multiboot info to main function --- arch/x86_64/include/arch/kernel/main.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 6961594..57b9b58 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -5,7 +5,7 @@ namespace teachos::arch::kernel { - auto main() -> void; + auto main(size_t multiboot_information_address) -> void; } #endif \ No newline at end of file -- cgit v1.2.3 From eeee7967c17704fee443a3b5b02d53a580f18b73 Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Sun, 29 Sep 2024 08:52:28 +0000 Subject: use multiboot_information_pointer public variable --- arch/x86_64/include/arch/boot/multiboot.hpp | 14 ++++++++++++++ arch/x86_64/include/arch/boot/pointers.hpp | 2 +- arch/x86_64/include/arch/kernel/main.hpp | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 arch/x86_64/include/arch/boot/multiboot.hpp (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp new file mode 100644 index 0000000..c6fed55 --- /dev/null +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -0,0 +1,14 @@ +#include + +struct multiboot_tag +{ + uint32_t type; + uint32_t size; +}; + +struct multiboot_info +{ + uint32_t total_size; + uint32_t reserved; + struct multiboot_tag tags[1]; // TODO: Size 0 +}; \ No newline at end of file diff --git a/arch/x86_64/include/arch/boot/pointers.hpp b/arch/x86_64/include/arch/boot/pointers.hpp index dcd14fe..c7424e2 100644 --- a/arch/x86_64/include/arch/boot/pointers.hpp +++ b/arch/x86_64/include/arch/boot/pointers.hpp @@ -5,7 +5,7 @@ namespace teachos::arch::boot { - extern "C" std::byte const multiboot_information_pointer; + extern "C" size_t const multiboot_information_pointer; } // namespace teachos::arch::boot #endif \ No newline at end of file diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 57b9b58..6961594 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -5,7 +5,7 @@ namespace teachos::arch::kernel { - auto main(size_t multiboot_information_address) -> void; + auto main() -> void; } #endif \ No newline at end of file -- cgit v1.2.3 From 8f91d0ef50e01440f7e6e9f4afa5887f6afefea1 Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Sun, 29 Sep 2024 09:26:17 +0000 Subject: read basic mem info --- arch/x86_64/include/arch/boot/multiboot.hpp | 38 +++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index c6fed55..9a0757e 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -10,5 +10,39 @@ struct multiboot_info { uint32_t total_size; uint32_t reserved; - struct multiboot_tag tags[1]; // TODO: Size 0 -}; \ No newline at end of file + /* + * field "tags" is an array of multiboot_tags, however the array is never + * being accessed by index and using an array definition with size 0 produces a compiler + * error. + */ + struct multiboot_tag tags; +}; + +/* + * Define all multiboot tag types to ther respective values + * The gnu boot information format is defined here: + * https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Boot-information-format + */ +#define MULTIBOOT_TAG_ALIGN 8 +#define MULTIBOOT_TAG_TYPE_END 0 +#define MULTIBOOT_TAG_TYPE_CMDLINE 1 +#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 +#define MULTIBOOT_TAG_TYPE_MODULE 3 +#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 +#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 +#define MULTIBOOT_TAG_TYPE_MMAP 6 +#define MULTIBOOT_TAG_TYPE_VBE 7 +#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 +#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9 +#define MULTIBOOT_TAG_TYPE_APM 10 +#define MULTIBOOT_TAG_TYPE_EFI32 11 +#define MULTIBOOT_TAG_TYPE_EFI64 12 +#define MULTIBOOT_TAG_TYPE_SMBIOS 13 +#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14 +#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15 +#define MULTIBOOT_TAG_TYPE_NETWORK 16 +#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17 +#define MULTIBOOT_TAG_TYPE_EFI_BS 18 +#define MULTIBOOT_TAG_TYPE_EFI32_IH 19 +#define MULTIBOOT_TAG_TYPE_EFI64_IH 20 +#define MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR 21 -- cgit v1.2.3 From d2e1c8ba686d7d4ab32eda91c2f532676e9b8acf Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Sun, 29 Sep 2024 14:03:39 +0000 Subject: create write_number function --- arch/x86_64/include/arch/video/vga/text.hpp | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/video/vga/text.hpp b/arch/x86_64/include/arch/video/vga/text.hpp index 1e584d6..97344da 100644 --- a/arch/x86_64/include/arch/video/vga/text.hpp +++ b/arch/x86_64/include/arch/video/vga/text.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace teachos::arch::video::vga::text { @@ -108,6 +109,55 @@ namespace teachos::arch::video::vga::text * @see vga::text::attribute */ auto write(std::string_view code_points, attribute attribute) -> void; + + /** + * @brief Write a single character to the VGA text buffer. + * + * @note This function also updates the text mode buffer pointer. + * + * @param code_point A code point to write to the VGA text mode buffer. + * @param attribute The attribute to apply to the written sequence of code points. + * @see vga::text::attribute + */ + auto write_char(char code_point, attribute attribute) -> void; + + // TODO: Move concepts to their own file/folder + template + concept Integral = std::is_integral_v; + + /** + * @brief Write a integral value to the VGA text buffer. + * + * @note This function also updates the text mode buffer pointer. + * + * @param code_points A integral value to write to the VGA text mode buffer. + * @param attribute The attribute to apply to the written sequence of code points. + * @see vga::text::attribute + */ + template + auto write_number(T value, attribute attribute) -> void + { + T current_value = value; + + T divisor = 1; + while (current_value > 9) + { + divisor *= 10; + current_value = current_value / 10; + } + + current_value = value; + while (divisor > 0) + { + uint8_t quotient = current_value / divisor; + char ascii_digit = quotient + '0'; + + write_char(ascii_digit, attribute); + current_value %= divisor; + + divisor /= 10; + } + } } // namespace teachos::arch::video::vga::text #endif \ No newline at end of file -- cgit v1.2.3 From 20a5e5377c0f8e0769d67a7928891597bc463e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 30 Sep 2024 11:32:56 +0000 Subject: Attempt to print memory map --- arch/x86_64/include/arch/boot/multiboot.hpp | 11 ++++++++++- arch/x86_64/include/arch/boot/pointers.hpp | 2 +- arch/x86_64/include/arch/io/port_io.hpp | 2 +- arch/x86_64/include/arch/kernel/main.hpp | 2 +- arch/x86_64/include/arch/video/vga/io.hpp | 4 +--- arch/x86_64/include/arch/video/vga/text.hpp | 3 +-- 6 files changed, 15 insertions(+), 9 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index 9a0757e..4182a18 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -2,7 +2,8 @@ struct multiboot_tag { - uint32_t type; + uint16_t type; + uint16_t flags; uint32_t size; }; @@ -18,6 +19,14 @@ struct multiboot_info struct multiboot_tag tags; }; +struct memory_map_entry +{ + uint64_t base_addr; + uint64_t length; + uint32_t type; + uint32_t reserved; +}; + /* * Define all multiboot tag types to ther respective values * The gnu boot information format is defined here: diff --git a/arch/x86_64/include/arch/boot/pointers.hpp b/arch/x86_64/include/arch/boot/pointers.hpp index c7424e2..25800f4 100644 --- a/arch/x86_64/include/arch/boot/pointers.hpp +++ b/arch/x86_64/include/arch/boot/pointers.hpp @@ -8,4 +8,4 @@ namespace teachos::arch::boot extern "C" size_t const multiboot_information_pointer; } // namespace teachos::arch::boot -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/io/port_io.hpp b/arch/x86_64/include/arch/io/port_io.hpp index 5b61f90..c0f1ef3 100644 --- a/arch/x86_64/include/arch/io/port_io.hpp +++ b/arch/x86_64/include/arch/io/port_io.hpp @@ -131,4 +131,4 @@ namespace teachos::arch::io } // namespace teachos::arch::io -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 6961594..6759eb2 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -8,4 +8,4 @@ namespace teachos::arch::kernel auto main() -> void; } -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/video/vga/io.hpp b/arch/x86_64/include/arch/video/vga/io.hpp index 9226c5c..da9375d 100644 --- a/arch/x86_64/include/arch/video/vga/io.hpp +++ b/arch/x86_64/include/arch/video/vga/io.hpp @@ -7,10 +7,8 @@ namespace teachos::arch::video::vga { - namespace crtc { - /** * @brief The address port of the CRT Controller */ @@ -38,4 +36,4 @@ namespace teachos::arch::video::vga } // namespace teachos::arch::video::vga -#endif \ No newline at end of file +#endif diff --git a/arch/x86_64/include/arch/video/vga/text.hpp b/arch/x86_64/include/arch/video/vga/text.hpp index 97344da..3b484e5 100644 --- a/arch/x86_64/include/arch/video/vga/text.hpp +++ b/arch/x86_64/include/arch/video/vga/text.hpp @@ -138,8 +138,8 @@ namespace teachos::arch::video::vga::text auto write_number(T value, attribute attribute) -> void { T current_value = value; - T divisor = 1; + while (current_value > 9) { divisor *= 10; @@ -154,7 +154,6 @@ namespace teachos::arch::video::vga::text write_char(ascii_digit, attribute); current_value %= divisor; - divisor /= 10; } } -- cgit v1.2.3 From b8e58d2f64fbb171b8687b9dd104ddd22fe4fc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 1 Oct 2024 07:42:09 +0000 Subject: Adjust printing of memory map --- arch/x86_64/include/arch/boot/multiboot.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index 4182a18..c39081a 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -2,8 +2,7 @@ struct multiboot_tag { - uint16_t type; - uint16_t flags; + uint32_t type; uint32_t size; }; @@ -27,6 +26,14 @@ struct memory_map_entry uint32_t reserved; }; +struct memory_map_info +{ + multiboot_tag tag; + uint32_t entry_size; + uint32_t entry_version; + struct memory_map_entry entries; +}; + /* * Define all multiboot tag types to ther respective values * The gnu boot information format is defined here: -- cgit v1.2.3 From e90fcb84fa43773d1e48bd82ce08381c6549a9cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 1 Oct 2024 08:22:40 +0000 Subject: Added efl section print method --- arch/x86_64/include/arch/boot/multiboot.hpp | 40 ++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index c39081a..dfb289d 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -11,9 +11,9 @@ struct multiboot_info uint32_t total_size; uint32_t reserved; /* - * field "tags" is an array of multiboot_tags, however the array is never - * being accessed by index and using an array definition with size 0 produces a compiler - * error. + * field "tags" is an array of multiboot_tag, however the array is never + * being accessed by index we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. */ struct multiboot_tag tags; }; @@ -22,6 +22,11 @@ struct memory_map_entry { uint64_t base_addr; uint64_t length; +#define MULTIBOOT_MEMORY_AVAILABLE 1 +#define MULTIBOOT_MEMORY_RESERVED 2 +#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 +#define MULTIBOOT_MEMORY_NVS 4 +#define MULTIBOOT_MEMORY_BADRAM 5 uint32_t type; uint32_t reserved; }; @@ -31,9 +36,38 @@ struct memory_map_info multiboot_tag tag; uint32_t entry_size; uint32_t entry_version; + /* + * field "entries" is an array of memory_map_entry, however the array is never + * being accessed by index, we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. + */ struct memory_map_entry entries; }; +struct elf64_section_header +{ + uint32_t sh_name; + uint32_t sh_type; + uint64_t sh_flags; + uint64_t sh_addr; + uint64_t sh_offset; + uint64_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint64_t sh_addralign; + uint64_t sh_entsize; +}; + +struct elf_symbols_section +{ + multiboot_tag tag; + uint16_t num; + uint16_t entsize; + uint16_t shndx; + uint16_t reserved; + struct elf64_section_header sections; +}; + /* * Define all multiboot tag types to ther respective values * The gnu boot information format is defined here: -- cgit v1.2.3 From 4c60bad3150b07e973eb385613a90ebb8c94ecac Mon Sep 17 00:00:00 2001 From: TheSoeren Date: Tue, 1 Oct 2024 11:59:40 +0000 Subject: add structs, clean mip code --- arch/x86_64/include/arch/boot/multiboot.hpp | 86 +++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 12 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp index dfb289d..d3787f2 100644 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ b/arch/x86_64/include/arch/boot/multiboot.hpp @@ -6,6 +6,14 @@ struct multiboot_tag uint32_t size; }; +struct basic_memory_info +{ + uint32_t type; + uint32_t size; + uint32_t mem_lower; + uint32_t mem_upper; +}; + struct multiboot_info { uint32_t total_size; @@ -44,18 +52,72 @@ struct memory_map_info struct memory_map_entry entries; }; -struct elf64_section_header +#define EI_NIDENT 16 + +/* ELF standard typedefs (yet more proof that was way overdue) */ +typedef uint16_t Elf64_Half; +typedef int16_t Elf64_SHalf; +typedef uint32_t Elf64_Word; +typedef int32_t Elf64_Sword; +typedef uint64_t Elf64_Xword; +typedef int64_t Elf64_Sxword; + +typedef uint64_t Elf64_Off; +typedef uint64_t Elf64_Addr; +typedef uint16_t Elf64_Section; + +struct executable_header +{ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ +}; + +struct program_header +{ + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ +}; + +struct elf_section_header +{ + Elf64_Word sh_name; /* Section name (string tbl index) */ + Elf64_Word sh_type; /* Section type */ + Elf64_Xword sh_flags; /* Section flags */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Section size in bytes */ + Elf64_Word sh_link; /* Link to another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ +}; + +struct elf_symbol { - uint32_t sh_name; - uint32_t sh_type; - uint64_t sh_flags; - uint64_t sh_addr; - uint64_t sh_offset; - uint64_t sh_size; - uint32_t sh_link; - uint32_t sh_info; - uint64_t sh_addralign; - uint64_t sh_entsize; + Elf64_Word st_name; /* Symbol name (string tbl index) */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf64_Section st_shndx; /* Section index */ + Elf64_Addr st_value; /* Symbol value */ + Elf64_Xword st_size; /* Symbol size */ }; struct elf_symbols_section @@ -65,7 +127,7 @@ struct elf_symbols_section uint16_t entsize; uint16_t shndx; uint16_t reserved; - struct elf64_section_header sections; + struct elf_section_header sections; }; /* -- cgit v1.2.3 From 32add45849744dc976c7af6ec24f985bbace47d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 2 Oct 2024 13:02:35 +0000 Subject: Creating base frame allocation code --- arch/x86_64/include/arch/kernel/main.hpp | 2 - .../x86_64/include/arch/memory/frame_allocator.hpp | 48 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 arch/x86_64/include/arch/memory/frame_allocator.hpp (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/kernel/main.hpp b/arch/x86_64/include/arch/kernel/main.hpp index 6759eb2..8813b46 100644 --- a/arch/x86_64/include/arch/kernel/main.hpp +++ b/arch/x86_64/include/arch/kernel/main.hpp @@ -1,8 +1,6 @@ #ifndef TEACHOS_ARCH_X86_64_KERNEL_MAIN_HPP #define TEACHOS_ARCH_X86_64_KERNEL_MAIN_HPP -#include - namespace teachos::arch::kernel { auto main() -> void; diff --git a/arch/x86_64/include/arch/memory/frame_allocator.hpp b/arch/x86_64/include/arch/memory/frame_allocator.hpp new file mode 100644 index 0000000..bcb0882 --- /dev/null +++ b/arch/x86_64/include/arch/memory/frame_allocator.hpp @@ -0,0 +1,48 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP + +#include +#include + +namespace teachos::arch::memory +{ + namespace + { + const size_t PAGE_FRAME_SIZE = 4096U; + } + + struct Frame + { + size_t frame_number; + + Frame(size_t frame_number) + : frame_number(frame_number) + { + // Nothing to do + } + + auto containing_address(size_t address) -> Frame { return Frame{address / PAGE_FRAME_SIZE}; } + }; + + struct IFrameAllocator + { + virtual auto allocate_frame() -> std::optional = 0; + virtual auto deallocate_frame(Frame frame) -> void = 0; + }; + + struct AreaFrameAllocator : public IFrameAllocator + { + Frame next_free_frame; + // std::optional current_area; + // MemoryArea * areas; + Frame kernel_start; + Frame kernel_end; + Frame multiboot_start; + Frame multiboot_end; + + auto allocate_frame() -> std::optional override; + auto deallocate_frame(Frame frame) -> void override; + }; +} // namespace teachos::arch::memory + +#endif // TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP -- cgit v1.2.3 From c2c7ad21fdd42111180d317ef50b15b7404769d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 2 Oct 2024 13:32:28 +0000 Subject: Improve naming, add enums and move into namespace --- arch/x86_64/include/arch/boot/multiboot.hpp | 160 ----------------------- arch/x86_64/include/arch/memory/multiboot.hpp | 174 ++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 160 deletions(-) delete mode 100644 arch/x86_64/include/arch/boot/multiboot.hpp create mode 100644 arch/x86_64/include/arch/memory/multiboot.hpp (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/boot/multiboot.hpp b/arch/x86_64/include/arch/boot/multiboot.hpp deleted file mode 100644 index d3787f2..0000000 --- a/arch/x86_64/include/arch/boot/multiboot.hpp +++ /dev/null @@ -1,160 +0,0 @@ -#include - -struct multiboot_tag -{ - uint32_t type; - uint32_t size; -}; - -struct basic_memory_info -{ - uint32_t type; - uint32_t size; - uint32_t mem_lower; - uint32_t mem_upper; -}; - -struct multiboot_info -{ - uint32_t total_size; - uint32_t reserved; - /* - * field "tags" is an array of multiboot_tag, however the array is never - * being accessed by index we don't know the real size at compile-time, - * and using an array definition with size 0 produces a compiler error. - */ - struct multiboot_tag tags; -}; - -struct memory_map_entry -{ - uint64_t base_addr; - uint64_t length; -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 - uint32_t type; - uint32_t reserved; -}; - -struct memory_map_info -{ - multiboot_tag tag; - uint32_t entry_size; - uint32_t entry_version; - /* - * field "entries" is an array of memory_map_entry, however the array is never - * being accessed by index, we don't know the real size at compile-time, - * and using an array definition with size 0 produces a compiler error. - */ - struct memory_map_entry entries; -}; - -#define EI_NIDENT 16 - -/* ELF standard typedefs (yet more proof that was way overdue) */ -typedef uint16_t Elf64_Half; -typedef int16_t Elf64_SHalf; -typedef uint32_t Elf64_Word; -typedef int32_t Elf64_Sword; -typedef uint64_t Elf64_Xword; -typedef int64_t Elf64_Sxword; - -typedef uint64_t Elf64_Off; -typedef uint64_t Elf64_Addr; -typedef uint16_t Elf64_Section; - -struct executable_header -{ - unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ - Elf64_Half e_type; /* Object file type */ - Elf64_Half e_machine; /* Architecture */ - Elf64_Word e_version; /* Object file version */ - Elf64_Addr e_entry; /* Entry point virtual address */ - Elf64_Off e_phoff; /* Program header table file offset */ - Elf64_Off e_shoff; /* Section header table file offset */ - Elf64_Word e_flags; /* Processor-specific flags */ - Elf64_Half e_ehsize; /* ELF header size in bytes */ - Elf64_Half e_phentsize; /* Program header table entry size */ - Elf64_Half e_phnum; /* Program header table entry count */ - Elf64_Half e_shentsize; /* Section header table entry size */ - Elf64_Half e_shnum; /* Section header table entry count */ - Elf64_Half e_shstrndx; /* Section header string table index */ -}; - -struct program_header -{ - Elf64_Word p_type; /* Segment type */ - Elf64_Word p_flags; /* Segment flags */ - Elf64_Off p_offset; /* Segment file offset */ - Elf64_Addr p_vaddr; /* Segment virtual address */ - Elf64_Addr p_paddr; /* Segment physical address */ - Elf64_Xword p_filesz; /* Segment size in file */ - Elf64_Xword p_memsz; /* Segment size in memory */ - Elf64_Xword p_align; /* Segment alignment */ -}; - -struct elf_section_header -{ - Elf64_Word sh_name; /* Section name (string tbl index) */ - Elf64_Word sh_type; /* Section type */ - Elf64_Xword sh_flags; /* Section flags */ - Elf64_Addr sh_addr; /* Section virtual addr at execution */ - Elf64_Off sh_offset; /* Section file offset */ - Elf64_Xword sh_size; /* Section size in bytes */ - Elf64_Word sh_link; /* Link to another section */ - Elf64_Word sh_info; /* Additional section information */ - Elf64_Xword sh_addralign; /* Section alignment */ - Elf64_Xword sh_entsize; /* Entry size if section holds table */ -}; - -struct elf_symbol -{ - Elf64_Word st_name; /* Symbol name (string tbl index) */ - unsigned char st_info; /* Symbol type and binding */ - unsigned char st_other; /* Symbol visibility */ - Elf64_Section st_shndx; /* Section index */ - Elf64_Addr st_value; /* Symbol value */ - Elf64_Xword st_size; /* Symbol size */ -}; - -struct elf_symbols_section -{ - multiboot_tag tag; - uint16_t num; - uint16_t entsize; - uint16_t shndx; - uint16_t reserved; - struct elf_section_header sections; -}; - -/* - * Define all multiboot tag types to ther respective values - * The gnu boot information format is defined here: - * https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Boot-information-format - */ -#define MULTIBOOT_TAG_ALIGN 8 -#define MULTIBOOT_TAG_TYPE_END 0 -#define MULTIBOOT_TAG_TYPE_CMDLINE 1 -#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 -#define MULTIBOOT_TAG_TYPE_MODULE 3 -#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 -#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 -#define MULTIBOOT_TAG_TYPE_MMAP 6 -#define MULTIBOOT_TAG_TYPE_VBE 7 -#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 -#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9 -#define MULTIBOOT_TAG_TYPE_APM 10 -#define MULTIBOOT_TAG_TYPE_EFI32 11 -#define MULTIBOOT_TAG_TYPE_EFI64 12 -#define MULTIBOOT_TAG_TYPE_SMBIOS 13 -#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14 -#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15 -#define MULTIBOOT_TAG_TYPE_NETWORK 16 -#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17 -#define MULTIBOOT_TAG_TYPE_EFI_BS 18 -#define MULTIBOOT_TAG_TYPE_EFI32_IH 19 -#define MULTIBOOT_TAG_TYPE_EFI64_IH 20 -#define MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR 21 diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp new file mode 100644 index 0000000..95e6404 --- /dev/null +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -0,0 +1,174 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP + +#include + +namespace teachos::arch::memory +{ + /* + * Define all multiboot tag types to ther respective values + * The gnu boot information format is defined here: + * https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Boot-information-format + */ + enum class MultibootTagType : uint32_t + { + END, + CMDLINE, + BOOT_LOADER_NAME, + MODULE, + BASIC_MEMORY_INFO, + BOOTDEV, + MEMORY_MAP, + VBE, + FRAMEBUFFER, + ELF_SECTIONS, + APM, + EFI32, + EFI64, + SMBIOS, + ACPI_OLD, + ACPI_NEW, + NETWORK, + EFI_MEMORY_MAP, + EFI_BS, + EFI32_IH, + EFI64_IH, + LOAD_BASE_ADDRESS + }; + + struct MultibootTag + { + MultibootTagType type; + uint32_t size; + }; + + struct MemoryInfo + { + uint32_t type; + uint32_t size; + uint32_t mem_lower; + uint32_t mem_upper; + }; + + struct MultibootInfo + { + uint32_t total_size; + uint32_t reserved; + /* + * field "tags" is an array of MultibootTag, however the array is never + * being accessed by index we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. + */ + struct MultibootTag tags; + }; + + enum class MemoryAreaType : uint32_t + { + AVAILABLE = 1, + RESERVED, + ACPI_AVAILABLE, + RESERVED_HIBERNATION, + DEFECTIVE + }; + + struct MemoryArea + { + uint64_t base_addr; + uint64_t length; + MemoryAreaType type; + uint32_t reserved; + }; + + struct MemoryMap + { + MultibootTag tag; + uint32_t entry_size; + uint32_t entry_version; + /* + * field "entries" is an array of MemoryArea, however the array is never + * being accessed by index, we don't know the real size at compile-time, + * and using an array definition with size 0 produces a compiler error. + */ + struct MemoryArea entries; + }; + +#define EI_NIDENT 16 + + /* ELF standard typedefs (yet more proof that was way overdue) */ + typedef uint16_t Elf64_Half; + typedef int16_t Elf64_SHalf; + typedef uint32_t Elf64_Word; + typedef int32_t Elf64_Sword; + typedef uint64_t Elf64_Xword; + typedef int64_t Elf64_Sxword; + + typedef uint64_t Elf64_Off; + typedef uint64_t Elf64_Addr; + typedef uint16_t Elf64_Section; + + struct executable_header + { + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ + }; + + struct program_header + { + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ + }; + + struct elf_section_header + { + Elf64_Word sh_name; /* Section name (string tbl index) */ + Elf64_Word sh_type; /* Section type */ + Elf64_Xword sh_flags; /* Section flags */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Section size in bytes */ + Elf64_Word sh_link; /* Link to another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ + }; + + struct elf_symbol + { + Elf64_Word st_name; /* Symbol name (string tbl index) */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf64_Section st_shndx; /* Section index */ + Elf64_Addr st_value; /* Symbol value */ + Elf64_Xword st_size; /* Symbol size */ + }; + + struct elf_symbols_section + { + MultibootTag tag; + uint16_t num; + uint16_t entsize; + uint16_t shndx; + uint16_t reserved; + struct elf_section_header sections; + }; +} // namespace teachos::arch::memory + +#endif // TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP -- cgit v1.2.3 From 1ab9c3c09d32283b39ca1026a9e29ada5ff7fda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 07:34:30 +0000 Subject: Renaming scheme --- .../x86_64/include/arch/memory/frame_allocator.hpp | 34 +++++++++++----------- arch/x86_64/include/arch/memory/multiboot.hpp | 30 +++++++++---------- arch/x86_64/include/arch/video/vga/text.hpp | 2 +- 3 files changed, 33 insertions(+), 33 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/frame_allocator.hpp b/arch/x86_64/include/arch/memory/frame_allocator.hpp index bcb0882..8dee848 100644 --- a/arch/x86_64/include/arch/memory/frame_allocator.hpp +++ b/arch/x86_64/include/arch/memory/frame_allocator.hpp @@ -11,37 +11,37 @@ namespace teachos::arch::memory const size_t PAGE_FRAME_SIZE = 4096U; } - struct Frame + struct frame { size_t frame_number; - Frame(size_t frame_number) + frame(size_t frame_number) : frame_number(frame_number) { // Nothing to do } - auto containing_address(size_t address) -> Frame { return Frame{address / PAGE_FRAME_SIZE}; } + auto containing_address(size_t address) -> frame { return frame{address / PAGE_FRAME_SIZE}; } }; - struct IFrameAllocator + struct frame_allocator { - virtual auto allocate_frame() -> std::optional = 0; - virtual auto deallocate_frame(Frame frame) -> void = 0; + virtual auto allocate_frame() -> std::optional = 0; + virtual auto deallocate_frame(frame frame) -> void = 0; }; - struct AreaFrameAllocator : public IFrameAllocator + struct area_frame_allocator : public frame_allocator { - Frame next_free_frame; - // std::optional current_area; - // MemoryArea * areas; - Frame kernel_start; - Frame kernel_end; - Frame multiboot_start; - Frame multiboot_end; - - auto allocate_frame() -> std::optional override; - auto deallocate_frame(Frame frame) -> void override; + frame next_free_frame; + // std::optional current_area; + // memory_area * areas; + frame kernel_start; + frame kernel_end; + frame multiboot_start; + frame multiboot_end; + + auto allocate_frame() -> std::optional override; + auto deallocate_frame(frame frame) -> void override; }; } // namespace teachos::arch::memory diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp index 95e6404..baad25b 100644 --- a/arch/x86_64/include/arch/memory/multiboot.hpp +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -10,7 +10,7 @@ namespace teachos::arch::memory * The gnu boot information format is defined here: * https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Boot-information-format */ - enum class MultibootTagType : uint32_t + enum class multi_boot_tag_type : uint32_t { END, CMDLINE, @@ -36,13 +36,13 @@ namespace teachos::arch::memory LOAD_BASE_ADDRESS }; - struct MultibootTag + struct multi_boot_tag { - MultibootTagType type; + multi_boot_tag_type type; uint32_t size; }; - struct MemoryInfo + struct memory_info { uint32_t type; uint32_t size; @@ -50,19 +50,19 @@ namespace teachos::arch::memory uint32_t mem_upper; }; - struct MultibootInfo + struct multi_boot_info { uint32_t total_size; uint32_t reserved; /* - * field "tags" is an array of MultibootTag, however the array is never + * field "tags" is an array of multi_boot_tag, however the array is never * being accessed by index we don't know the real size at compile-time, * and using an array definition with size 0 produces a compiler error. */ - struct MultibootTag tags; + struct multi_boot_tag tags; }; - enum class MemoryAreaType : uint32_t + enum class memory_area_type : uint32_t { AVAILABLE = 1, RESERVED, @@ -71,25 +71,25 @@ namespace teachos::arch::memory DEFECTIVE }; - struct MemoryArea + struct memory_area { uint64_t base_addr; uint64_t length; - MemoryAreaType type; + memory_area_type type; uint32_t reserved; }; - struct MemoryMap + struct memory_map { - MultibootTag tag; + multi_boot_tag tag; uint32_t entry_size; uint32_t entry_version; /* - * field "entries" is an array of MemoryArea, however the array is never + * field "entries" is an array of memory_area, however the array is never * being accessed by index, we don't know the real size at compile-time, * and using an array definition with size 0 produces a compiler error. */ - struct MemoryArea entries; + struct memory_area entries; }; #define EI_NIDENT 16 @@ -162,7 +162,7 @@ namespace teachos::arch::memory struct elf_symbols_section { - MultibootTag tag; + multi_boot_tag tag; uint16_t num; uint16_t entsize; uint16_t shndx; diff --git a/arch/x86_64/include/arch/video/vga/text.hpp b/arch/x86_64/include/arch/video/vga/text.hpp index 3b484e5..9c4c701 100644 --- a/arch/x86_64/include/arch/video/vga/text.hpp +++ b/arch/x86_64/include/arch/video/vga/text.hpp @@ -130,7 +130,7 @@ namespace teachos::arch::video::vga::text * * @note This function also updates the text mode buffer pointer. * - * @param code_points A integral value to write to the VGA text mode buffer. + * @param value A integral value to write to the VGA text mode buffer. * @param attribute The attribute to apply to the written sequence of code points. * @see vga::text::attribute */ -- cgit v1.2.3 From 241c17a52e3954a6c1cf8d38f402d240070a5818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 11:55:31 +0000 Subject: Use alignas instead of seperate variable in struct for padding --- arch/x86_64/include/arch/memory/multiboot.hpp | 41 ++------------------------- 1 file changed, 3 insertions(+), 38 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp index baad25b..a2d6e9d 100644 --- a/arch/x86_64/include/arch/memory/multiboot.hpp +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -53,13 +53,12 @@ namespace teachos::arch::memory struct multi_boot_info { uint32_t total_size; - uint32_t reserved; /* * field "tags" is an array of multi_boot_tag, however the array is never * being accessed by index we don't know the real size at compile-time, * and using an array definition with size 0 produces a compiler error. */ - struct multi_boot_tag tags; + alignas(8) struct multi_boot_tag tags; }; enum class memory_area_type : uint32_t @@ -75,8 +74,7 @@ namespace teachos::arch::memory { uint64_t base_addr; uint64_t length; - memory_area_type type; - uint32_t reserved; + alignas(8) memory_area_type type; }; struct memory_map @@ -92,8 +90,6 @@ namespace teachos::arch::memory struct memory_area entries; }; -#define EI_NIDENT 16 - /* ELF standard typedefs (yet more proof that was way overdue) */ typedef uint16_t Elf64_Half; typedef int16_t Elf64_SHalf; @@ -106,36 +102,6 @@ namespace teachos::arch::memory typedef uint64_t Elf64_Addr; typedef uint16_t Elf64_Section; - struct executable_header - { - unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ - Elf64_Half e_type; /* Object file type */ - Elf64_Half e_machine; /* Architecture */ - Elf64_Word e_version; /* Object file version */ - Elf64_Addr e_entry; /* Entry point virtual address */ - Elf64_Off e_phoff; /* Program header table file offset */ - Elf64_Off e_shoff; /* Section header table file offset */ - Elf64_Word e_flags; /* Processor-specific flags */ - Elf64_Half e_ehsize; /* ELF header size in bytes */ - Elf64_Half e_phentsize; /* Program header table entry size */ - Elf64_Half e_phnum; /* Program header table entry count */ - Elf64_Half e_shentsize; /* Section header table entry size */ - Elf64_Half e_shnum; /* Section header table entry count */ - Elf64_Half e_shstrndx; /* Section header string table index */ - }; - - struct program_header - { - Elf64_Word p_type; /* Segment type */ - Elf64_Word p_flags; /* Segment flags */ - Elf64_Off p_offset; /* Segment file offset */ - Elf64_Addr p_vaddr; /* Segment virtual address */ - Elf64_Addr p_paddr; /* Segment physical address */ - Elf64_Xword p_filesz; /* Segment size in file */ - Elf64_Xword p_memsz; /* Segment size in memory */ - Elf64_Xword p_align; /* Segment alignment */ - }; - struct elf_section_header { Elf64_Word sh_name; /* Section name (string tbl index) */ @@ -166,8 +132,7 @@ namespace teachos::arch::memory uint16_t num; uint16_t entsize; uint16_t shndx; - uint16_t reserved; - struct elf_section_header sections; + alignas(8) struct elf_section_header sections; }; } // namespace teachos::arch::memory -- 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/include/arch/memory/multiboot.hpp | 70 ++++++++++++++------------- 1 file changed, 37 insertions(+), 33 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp index a2d6e9d..c808213 100644 --- a/arch/x86_64/include/arch/memory/multiboot.hpp +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -1,6 +1,7 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP #define TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_HPP +#include #include namespace teachos::arch::memory @@ -90,48 +91,51 @@ namespace teachos::arch::memory struct memory_area entries; }; - /* ELF standard typedefs (yet more proof that was way overdue) */ - typedef uint16_t Elf64_Half; - typedef int16_t Elf64_SHalf; - typedef uint32_t Elf64_Word; - typedef int32_t Elf64_Sword; - typedef uint64_t Elf64_Xword; - typedef int64_t Elf64_Sxword; - - typedef uint64_t Elf64_Off; - typedef uint64_t Elf64_Addr; - typedef uint16_t Elf64_Section; - - struct elf_section_header + /** + * https://refspecs.linuxfoundation.org/LSB_2.1.0/LSB-Core-generic/LSB-Core-generic/elftypes.html + */ + enum class elf_section_type : uint32_t { - Elf64_Word sh_name; /* Section name (string tbl index) */ - Elf64_Word sh_type; /* Section type */ - Elf64_Xword sh_flags; /* Section flags */ - Elf64_Addr sh_addr; /* Section virtual addr at execution */ - Elf64_Off sh_offset; /* Section file offset */ - Elf64_Xword sh_size; /* Section size in bytes */ - Elf64_Word sh_link; /* Link to another section */ - Elf64_Word sh_info; /* Additional section information */ - Elf64_Xword sh_addralign; /* Section alignment */ - Elf64_Xword sh_entsize; /* Entry size if section holds table */ + INACTIVE, + PROGRAMM, + SYMBOL_TABLE, + STRING_TABLE, + RELOCATION_ENTRY_WITH_EXPLICIT_ADDENDS, + SYMBOL_HASH_TABLE, + DYNAMIC, + NOTE, + EMPTY, + RELOCATION_ENTRY_WITHOUT_EXPLICIT_ADDENDS, + UNSPECIFIED, + DYNAMIC_SYMBOL, + INITALIZATION_FUNCTION_ARRAY = 14, + TERMINATION_FUNCTION_ARRAY, + PRE_INITALIZATION_FUNCTION_ARRAY }; - struct elf_symbol + struct elf_section_header { - Elf64_Word st_name; /* Symbol name (string tbl index) */ - unsigned char st_info; /* Symbol type and binding */ - unsigned char st_other; /* Symbol visibility */ - Elf64_Section st_shndx; /* Section index */ - Elf64_Addr st_value; /* Symbol value */ - Elf64_Xword st_size; /* Symbol size */ + uint32_t sh_name; /* Section name (string tbl index) */ + elf_section_type sh_type; /* Section type */ + std::bitset<64U> sh_flags; /* Section flags */ + uint64_t sh_addr; /* Section virtual addr at execution */ + uint64_t sh_offset; /* Section file offset */ + uint64_t sh_size; /* Section size in bytes */ + uint32_t sh_link; /* Link to another section */ + uint32_t sh_info; /* Additional section information */ + uint64_t sh_addralign; /* Section alignment */ + uint64_t sh_entsize; /* Entry size if section holds table */ }; + /** + * https://gist.github.com/x0nu11byt3/bcb35c3de461e5fb66173071a2379779 + */ struct elf_symbols_section { multi_boot_tag tag; - uint16_t num; - uint16_t entsize; - uint16_t shndx; + uint32_t num; + uint32_t entsize; + uint32_t shndx; alignas(8) struct elf_section_header sections; }; } // namespace teachos::arch::memory -- cgit v1.2.3 From 951b314b655781dc59aee24cf952d03bf3b3ee83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 6 Oct 2024 13:32:14 +0000 Subject: Interpret files as bitset wrapper --- arch/x86_64/include/arch/memory/multiboot.hpp | 64 ++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp index c808213..f1f0613 100644 --- a/arch/x86_64/include/arch/memory/multiboot.hpp +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -113,18 +113,62 @@ namespace teachos::arch::memory PRE_INITALIZATION_FUNCTION_ARRAY }; + /** + * https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-94076/index.html + */ + class elf_section_flags + { + public: + elf_section_flags(uint64_t flags) + : flags(flags) + { + // Nothing to do + } + + auto writeable() -> bool { return is_bit_set(0); } + + auto occupies_memory() -> bool { return is_bit_set(1); } + + auto is_executable() -> bool { return is_bit_set(2); } + + auto contains_duplicate_data() -> bool { return is_bit_set(4); } + + auto contains_strings() -> bool { return is_bit_set(5); } + + auto section_header_info_is_section_header_table_index() -> bool { return is_bit_set(6); } + + auto preserve_ordering_after_combination() -> bool { return is_bit_set(7); } + + auto requires_special_os_processing() -> bool { return is_bit_set(8); } + + auto is_section_group_member() -> bool { return is_bit_set(9); } + + auto holds_thread_local_data() -> bool { return is_bit_set(10); } + + auto is_compressed() -> bool { return is_bit_set(11); } + + auto has_special_ordering_requirements() -> bool { return is_bit_set(30); } + + auto is_excluded_unless_referenced_or_allocated() -> bool { return is_bit_set(31); } + + private: + auto is_bit_set(uint8_t index) -> bool { return flags[index] == 1; } + + std::bitset<64U> flags; + }; + struct elf_section_header { - uint32_t sh_name; /* Section name (string tbl index) */ - elf_section_type sh_type; /* Section type */ - std::bitset<64U> sh_flags; /* Section flags */ - uint64_t sh_addr; /* Section virtual addr at execution */ - uint64_t sh_offset; /* Section file offset */ - uint64_t sh_size; /* Section size in bytes */ - uint32_t sh_link; /* Link to another section */ - uint32_t sh_info; /* Additional section information */ - uint64_t sh_addralign; /* Section alignment */ - uint64_t sh_entsize; /* Entry size if section holds table */ + uint32_t sh_name; /* Section name (string tbl index) */ + elf_section_type sh_type; /* Section type */ + elf_section_flags sh_flags; /* Section flags */ + uint64_t sh_addr; /* Section virtual addr at execution */ + uint64_t sh_offset; /* Section file offset */ + uint64_t sh_size; /* Section size in bytes */ + uint32_t sh_link; /* Link to another section */ + uint32_t sh_info; /* Additional section information */ + uint64_t sh_addralign; /* Section alignment */ + uint64_t sh_entsize; /* Entry size if section holds table */ }; /** -- 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/include/arch/memory/multiboot.hpp | 48 ++++++++++++++------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp index f1f0613..37b10f0 100644 --- a/arch/x86_64/include/arch/memory/multiboot.hpp +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -43,14 +43,6 @@ namespace teachos::arch::memory uint32_t size; }; - struct memory_info - { - uint32_t type; - uint32_t size; - uint32_t mem_lower; - uint32_t mem_upper; - }; - struct multi_boot_info { uint32_t total_size; @@ -62,6 +54,13 @@ namespace teachos::arch::memory alignas(8) struct multi_boot_tag tags; }; + struct memory_info + { + multi_boot_tag tag; + uint32_t mem_lower; + uint32_t mem_upper; + }; + enum class memory_area_type : uint32_t { AVAILABLE = 1, @@ -73,8 +72,8 @@ namespace teachos::arch::memory struct memory_area { - uint64_t base_addr; - uint64_t length; + uint64_t base_address; + uint64_t area_length; alignas(8) memory_area_type type; }; @@ -157,18 +156,21 @@ namespace teachos::arch::memory std::bitset<64U> flags; }; + /** + * https://docs.oracle.com/cd/E19455-01/806-3773/elf-2/index.html + */ struct elf_section_header { - uint32_t sh_name; /* Section name (string tbl index) */ - elf_section_type sh_type; /* Section type */ - elf_section_flags sh_flags; /* Section flags */ - uint64_t sh_addr; /* Section virtual addr at execution */ - uint64_t sh_offset; /* Section file offset */ - uint64_t sh_size; /* Section size in bytes */ - uint32_t sh_link; /* Link to another section */ - uint32_t sh_info; /* Additional section information */ - uint64_t sh_addralign; /* Section alignment */ - uint64_t sh_entsize; /* Entry size if section holds table */ + uint32_t name_table_index; + elf_section_type type; + elf_section_flags flags; + uint64_t virtual_address; + uint64_t file_offset; + uint64_t section_size; + uint32_t other_section; + uint32_t additional_information; + uint64_t address_alignment; + uint64_t fixed_table_entry_size; }; /** @@ -177,9 +179,9 @@ namespace teachos::arch::memory struct elf_symbols_section { multi_boot_tag tag; - uint32_t num; - uint32_t entsize; - uint32_t shndx; + uint32_t number_of_sections; + uint32_t entry_size; + uint32_t section_index; alignas(8) struct elf_section_header sections; }; } // namespace teachos::arch::memory -- 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/include/arch/memory/multiboot.hpp | 46 ++++++++++++++++++--------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/multiboot.hpp b/arch/x86_64/include/arch/memory/multiboot.hpp index 37b10f0..f6ff480 100644 --- a/arch/x86_64/include/arch/memory/multiboot.hpp +++ b/arch/x86_64/include/arch/memory/multiboot.hpp @@ -124,40 +124,43 @@ namespace teachos::arch::memory // Nothing to do } - auto writeable() -> bool { return is_bit_set(0); } + auto writeable() const -> bool { return is_bit_set(0); } - auto occupies_memory() -> bool { return is_bit_set(1); } + auto occupies_memory() const -> bool { return is_bit_set(1); } - auto is_executable() -> bool { return is_bit_set(2); } + auto is_executable() const -> bool { return is_bit_set(2); } - auto contains_duplicate_data() -> bool { return is_bit_set(4); } + auto contains_duplicate_data() const -> bool { return is_bit_set(4); } - auto contains_strings() -> bool { return is_bit_set(5); } + auto contains_strings() const -> bool { return is_bit_set(5); } - auto section_header_info_is_section_header_table_index() -> bool { return is_bit_set(6); } + auto section_header_info_is_section_header_table_index() const -> bool { return is_bit_set(6); } - auto preserve_ordering_after_combination() -> bool { return is_bit_set(7); } + auto preserve_ordering_after_combination() const -> bool { return is_bit_set(7); } - auto requires_special_os_processing() -> bool { return is_bit_set(8); } + auto requires_special_os_processing() const -> bool { return is_bit_set(8); } - auto is_section_group_member() -> bool { return is_bit_set(9); } + auto is_section_group_member() const -> bool { return is_bit_set(9); } - auto holds_thread_local_data() -> bool { return is_bit_set(10); } + auto holds_thread_local_data() const -> bool { return is_bit_set(10); } - auto is_compressed() -> bool { return is_bit_set(11); } + auto is_compressed() const -> bool { return is_bit_set(11); } - auto has_special_ordering_requirements() -> bool { return is_bit_set(30); } + auto has_special_ordering_requirements() const -> bool { return is_bit_set(30); } - auto is_excluded_unless_referenced_or_allocated() -> bool { return is_bit_set(31); } + auto is_excluded_unless_referenced_or_allocated() const -> bool { return is_bit_set(31); } + + auto operator==(elf_section_flags const & other) const -> bool { return flags == other.flags; } private: - auto is_bit_set(uint8_t index) -> bool { return flags[index] == 1; } + auto is_bit_set(uint8_t index) const -> bool { return flags[index] == 1; } std::bitset<64U> flags; }; /** - * https://docs.oracle.com/cd/E19455-01/806-3773/elf-2/index.html + * @brief Defines the data included in a section header, where each section has exactly one section header. + * See https://refspecs.linuxbase.org/elf/gabi4+/ch4.sheader.html for more information */ struct elf_section_header { @@ -171,6 +174,19 @@ namespace teachos::arch::memory uint32_t additional_information; uint64_t address_alignment; uint64_t fixed_table_entry_size; + + /** + * @brief Detect whether e section header is inactive or not, should always be the case for the first entry in the + * sections table + * @return Whether the current section header is actually null or not, requires all fields besides section_size and + * other_section to actually contain 0 + */ + auto is_null() const -> bool + { + return name_table_index == 0U && type == elf_section_type::UNSPECIFIED && flags == 0U && virtual_address == 0U && + file_offset == 0U && additional_information == 0U && address_alignment == 0U && + fixed_table_entry_size == 0U; + } }; /** -- 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 --- .../x86_64/include/arch/memory/frame_allocator.hpp | 72 +++++++++++++++++----- 1 file changed, 57 insertions(+), 15 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/frame_allocator.hpp b/arch/x86_64/include/arch/memory/frame_allocator.hpp index 8dee848..35d7360 100644 --- a/arch/x86_64/include/arch/memory/frame_allocator.hpp +++ b/arch/x86_64/include/arch/memory/frame_allocator.hpp @@ -1,6 +1,7 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP #define TEACHOS_ARCH_X86_64_MEMORY_FRAME_HPP +#include "multiboot.hpp" #include #include @@ -8,20 +9,26 @@ namespace teachos::arch::memory { namespace { - const size_t PAGE_FRAME_SIZE = 4096U; + const std::size_t PAGE_FRAME_SIZE = 4096U; } struct frame { - size_t frame_number; + std::size_t frame_number; - frame(size_t frame_number) + frame(std::size_t frame_number) : frame_number(frame_number) { // Nothing to do } - auto containing_address(size_t address) -> frame { return frame{address / PAGE_FRAME_SIZE}; } + static auto containing_address(std::size_t address) -> frame { return frame{address / PAGE_FRAME_SIZE}; } + + constexpr bool operator==(const frame & other) const noexcept { return frame_number == other.frame_number; } + constexpr bool operator>(const frame & other) const noexcept { return frame_number > other.frame_number; } + constexpr bool operator<(const frame & other) const noexcept { return frame_number < other.frame_number; } + constexpr bool operator>=(const frame & other) const noexcept { return frame_number >= other.frame_number; } + constexpr bool operator<=(const frame & other) const noexcept { return frame_number <= other.frame_number; } }; struct frame_allocator @@ -30,18 +37,53 @@ namespace teachos::arch::memory virtual auto deallocate_frame(frame frame) -> void = 0; }; - struct area_frame_allocator : public frame_allocator + // TODO: FIX CONCEPT USAGE + // template + // concept FrameAllocator = requires(T t) { + // { t.allocate_frame() } -> std::optional; + // { t.deallocate_frame() } -> void; + // }; + + // template + struct area_frame_allocator : frame_allocator { - frame next_free_frame; - // std::optional current_area; - // memory_area * areas; - frame kernel_start; - frame kernel_end; - frame multiboot_start; - frame multiboot_end; - - auto allocate_frame() -> std::optional override; - auto deallocate_frame(frame frame) -> void override; + frame next_free_frame{0}; //!< The frame after the last allocated one + std::optional current_area{std::nullopt}; //!< The current memory area + arch::memory::memory_area * areas; //!< A list of all memory areas + frame kernel_start; //!< The start address of the kernel code in memory + frame kernel_end; //!< The end address of the kernel code in memory + frame multiboot_start; //!< The start address of the multiboot code in memory + frame multiboot_end; //!< The end address of the multiboot code in memory + + area_frame_allocator(std::size_t kernel_start, std::size_t kernel_end, std::size_t multiboot_start, + std::size_t multiboot_end, arch::memory::memory_area * memory_areas) + : kernel_start(frame{kernel_start}) + , kernel_end(frame{kernel_end}) + , multiboot_start(frame{multiboot_start}) + , multiboot_end(frame{multiboot_end}) + , areas(memory_areas) + { + choose_next_area(); + } + + /** + * @brief Allocate memory be finding and returning a free frame + * + * The frame allocation executes multiple checks before returning + * the frame that is available to allocate. It must at least + * do the following: + * - check if the next_free_frame is within the current_area + * - check if the next_free_frame is actually free + * - update the next_free_frame after finding a free frame + */ + auto allocate_frame() -> std::optional; + auto deallocate_frame(frame frame) -> void; + + private: + /** + * @brief Find the next memory area and write it into current_area + */ + auto choose_next_area() -> void; }; } // 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/include/arch/io/port_io.hpp | 1 - .../x86_64/include/arch/memory/frame_allocator.hpp | 86 +++-- arch/x86_64/include/arch/memory/multiboot.hpp | 345 ++++++++++++++------- 3 files changed, 287 insertions(+), 145 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/io/port_io.hpp b/arch/x86_64/include/arch/io/port_io.hpp index c0f1ef3..1945261 100644 --- a/arch/x86_64/include/arch/io/port_io.hpp +++ b/arch/x86_64/include/arch/io/port_io.hpp @@ -8,7 +8,6 @@ namespace teachos::arch::io { - /** * @brief An I/O port of a given size at a given address. * diff --git a/arch/x86_64/include/arch/memory/frame_allocator.hpp b/arch/x86_64/include/arch/memory/frame_allocator.hpp index 35d7360..3989cdf 100644 --- a/arch/x86_64/include/arch/memory/frame_allocator.hpp +++ b/arch/x86_64/include/arch/memory/frame_allocator.hpp @@ -4,6 +4,7 @@ #include "multiboot.hpp" #include #include +#include namespace teachos::arch::memory { @@ -12,51 +13,74 @@ namespace teachos::arch::memory const std::size_t PAGE_FRAME_SIZE = 4096U; } + /** + * @brief Specific frame containing helper functions to determine if a specific address is in that frame or not + */ struct frame { - std::size_t frame_number; + std::size_t frame_number; ///< Index number of the current frame, used to distinguish it from other frames + /** + * @brief Constructor + * + * @param frame_number Index number that should be assigned to this frame + */ frame(std::size_t frame_number) : frame_number(frame_number) { // Nothing to do } + /** + * @brief Returns the frame the given address is contained in + * + * @param address Address we want to get the corresponding frame for + * @return Frame the given address is contained in + */ static auto containing_address(std::size_t address) -> frame { return frame{address / PAGE_FRAME_SIZE}; } - constexpr bool operator==(const frame & other) const noexcept { return frame_number == other.frame_number; } - constexpr bool operator>(const frame & other) const noexcept { return frame_number > other.frame_number; } - constexpr bool operator<(const frame & other) const noexcept { return frame_number < other.frame_number; } - constexpr bool operator>=(const frame & other) const noexcept { return frame_number >= other.frame_number; } - constexpr bool operator<=(const frame & other) const noexcept { return frame_number <= other.frame_number; } - }; + /** + * @brief Defaulted equals operator + */ + constexpr auto operator==(const frame & other) const -> bool = default; - struct frame_allocator - { - virtual auto allocate_frame() -> std::optional = 0; - virtual auto deallocate_frame(frame frame) -> void = 0; + /** + * @brief Defaulted three-way comparsion operator + */ + constexpr auto operator<=>(const frame & other) const -> std::partial_ordering = default; }; - // TODO: FIX CONCEPT USAGE - // template - // concept FrameAllocator = requires(T t) { - // { t.allocate_frame() } -> std::optional; - // { t.deallocate_frame() } -> void; - // }; + template + concept FrameAllocator = requires(T t) { + { t.allocate_frame() } -> std::optional; + { t.deallocate_frame() } -> void; + }; - // template - struct area_frame_allocator : frame_allocator + /** + * @brief Allocates memory using memory areas read from the multiboot2 information pointer + * + */ + struct area_frame_allocator { - frame next_free_frame{0}; //!< The frame after the last allocated one - std::optional current_area{std::nullopt}; //!< The current memory area - arch::memory::memory_area * areas; //!< A list of all memory areas - frame ker