diff options
| author | Fabian Imhof <fabian.imhof@ost.ch> | 2025-03-13 14:05:45 +0000 |
|---|---|---|
| committer | Fabian Imhof <fabian.imhof@ost.ch> | 2025-03-13 14:05:45 +0000 |
| commit | 2e4cbd473ff3bb7ac7371af39becf830b4fb753b (patch) | |
| tree | 3eb8cc5fcf255db3c308930b800c9dd32c6b295b | |
| parent | b8a0024ee71a64ec0e87a1e2d0c0c7280dc954e6 (diff) | |
| download | teachos-2e4cbd473ff3bb7ac7371af39becf830b4fb753b.tar.xz teachos-2e4cbd473ff3bb7ac7371af39becf830b4fb753b.zip | |
IN_PROGRESS implement gdt initialization
17 files changed, 219 insertions, 65 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index 1b8349a..9d59d87 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -7,6 +7,10 @@ mark_as_advanced(TEACHOS_KERNEL_LINKER_SCRIPT) target_sources("_kernel" PRIVATE "src/kernel/main.cpp" + "src/kernel/cpu/tlb.cpp" + "src/kernel/cpu/control_register.cpp" + "src/kernel/cpu/msr.cpp" + "src/kernel/cpu/ss.cpp" ) target_link_options("_kernel" PRIVATE @@ -53,9 +57,6 @@ target_sources("_memory" PRIVATE "src/memory/paging/virtual_page.cpp" "src/memory/paging/active_page_table.cpp" "src/memory/paging/inactive_page_table.cpp" - "src/memory/cpu/tlb.cpp" - "src/memory/cpu/control_register.cpp" - "src/memory/cpu/msr.cpp" "src/memory/heap/bump_allocator.cpp" "src/memory/heap/memory_block.cpp" "src/memory/heap/linked_list_allocator.cpp" @@ -86,10 +87,11 @@ target_sources("_exception" PRIVATE #]============================================================================] target_sources("_context" PRIVATE - "src/context_switching/descriptor_table/gdt_flags.cpp" "src/context_switching/descriptor_table/access_byte.cpp" - "src/context_switching/descriptor_table/type_field.cpp" + "src/context_switching/descriptor_table/gdt_flags.cpp" + "src/context_switching/descriptor_table/global_descriptor_table.cpp" "src/context_switching/descriptor_table/segment_descriptor.cpp" + "src/context_switching/descriptor_table/type_field.cpp" ) #[============================================================================[ diff --git a/arch/x86_64/include/arch/context_switching/descriptor_table/global_descriptor_table.hpp b/arch/x86_64/include/arch/context_switching/descriptor_table/global_descriptor_table.hpp index e69de29..daba1fe 100644 --- a/arch/x86_64/include/arch/context_switching/descriptor_table/global_descriptor_table.hpp +++ b/arch/x86_64/include/arch/context_switching/descriptor_table/global_descriptor_table.hpp @@ -0,0 +1,19 @@ +#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_HPP +#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_HPP + +#include "arch/context_switching/descriptor_table/global_descriptor_table_pointer.hpp" +#include "arch/context_switching/descriptor_table/segment_descriptor.hpp" +#include "arch/stl/vector.hpp" + +namespace teachos::arch::context_switching::descriptor_table +{ + typedef stl::vector<segment_descriptor> global_descriptor_table; + + auto create_global_descriptor_table() -> global_descriptor_table; + + auto load_global_descriptor_table(global_descriptor_table_pointer gdt_pointer) -> void; + + auto initialize_global_descriptor_table() -> global_descriptor_table; +} // namespace teachos::arch::context_switching::descriptor_table + +#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_HPP diff --git a/arch/x86_64/include/arch/context_switching/descriptor_table/global_descriptor_table_pointer.hpp b/arch/x86_64/include/arch/context_switching/descriptor_table/global_descriptor_table_pointer.hpp new file mode 100644 index 0000000..c2925fd --- /dev/null +++ b/arch/x86_64/include/arch/context_switching/descriptor_table/global_descriptor_table_pointer.hpp @@ -0,0 +1,23 @@ +#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_POINTER_HPP +#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_POINTER_HPP + +#include "arch/context_switching/descriptor_table/global_descriptor_table.hpp" + +#include <cstdint> + +namespace teachos::arch::context_switching::descriptor_table +{ + /** + * @brief Represents a pointer to the Global Descriptor Table (GDT). + * + * This structure is used to store the base address and length of the GDT. + * It is used when loading or modifying the GDT during context switching. + */ + struct global_descriptor_table_pointer + { + uint16_t table_length; ///< The size of the GDT in bytes. + global_descriptor_table * address; ///< Pointer to the GDT base address. + }; +} // namespace teachos::arch::context_switching::descriptor_table + +#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_DESCRIPTOR_TABLE_GLOBAL_DESCRIPTOR_TABLE_POINTER_HPP diff --git a/arch/x86_64/include/arch/memory/cpu/control_register.hpp b/arch/x86_64/include/arch/kernel/cpu/control_register.hpp index e11813d..27c7777 100644 --- a/arch/x86_64/include/arch/memory/cpu/control_register.hpp +++ b/arch/x86_64/include/arch/kernel/cpu/control_register.hpp @@ -1,5 +1,5 @@ -#ifndef TEACHOS_ARCH_X86_64_MEMORY_CPU_CR3_HPP -#define TEACHOS_ARCH_X86_64_MEMORY_CPU_CR3_HPP +#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_CR3_HPP +#define TEACHOS_ARCH_X86_64_KERNEL_CPU_CR3_HPP #include <cstdint> @@ -68,4 +68,4 @@ namespace teachos::arch::memory::cpu } // namespace teachos::arch::memory::cpu -#endif // TEACHOS_ARCH_X86_64_MEMORY_CPU_CR3_HPP +#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_CR3_HPP diff --git a/arch/x86_64/include/arch/memory/cpu/msr.hpp b/arch/x86_64/include/arch/kernel/cpu/msr.hpp index cda70e2..52d74bd 100644 --- a/arch/x86_64/include/arch/memory/cpu/msr.hpp +++ b/arch/x86_64/include/arch/kernel/cpu/msr.hpp @@ -1,5 +1,5 @@ -#ifndef TEACHOS_ARCH_X86_64_MEMORY_CPU_NXE_HPP -#define TEACHOS_ARCH_X86_64_MEMORY_CPU_NXE_HPP +#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_NXE_HPP +#define TEACHOS_ARCH_X86_64_KERNEL_CPU_NXE_HPP #include <bitset> #include <cstdint> @@ -61,4 +61,4 @@ namespace teachos::arch::memory::cpu } // namespace teachos::arch::memory::cpu -#endif // TEACHOS_ARCH_X86_64_MEMORY_CPU_NXE_HPP
\ No newline at end of file +#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_NXE_HPP
\ No newline at end of file diff --git a/arch/x86_64/include/arch/kernel/cpu/ss.hpp b/arch/x86_64/include/arch/kernel/cpu/ss.hpp new file mode 100644 index 0000000..2d3518e --- /dev/null +++ b/arch/x86_64/include/arch/kernel/cpu/ss.hpp @@ -0,0 +1,56 @@ +#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_SS_HPP +#define TEACHOS_ARCH_X86_64_KERNEL_CPU_SS_HPP + +#include <bitset> +#include <cstdint> + +namespace teachos::arch::memory::cpu +{ + /** + * @brief Represents a segment selector in the x86_64 architecture. + * + * A segment selector is a 16-bit identifier used to select a segment descriptor + * from the Global Descriptor Table (GDT) or the Local Descriptor Table (LDT). + * It contains an index, a table indicator (TI), and a requested privilege level (RPL). + */ + struct segment_selector + { + /** + * @brief Constructs a segment selector. + * + * @param index The index of the segment descriptor. + * @param table_indicator The table indicator (0 for GDT, 1 for LDT). + * @param requested_privilege_level The requested privilege level (0-3). + */ + segment_selector(uint16_t index, std::bitset<1U> table_indicator, std::bitset<2U> requested_privilege_level); + + /** + * @brief Converts the segment selector to a 16-bit value. + * + * @return uint16_t The 16-bit representation of the segment selector. + */ + auto to_uint16() const -> uint16_t; + + private: + uint16_t index; + std::bitset<1U> table_indicator; + std::bitset<2U> requested_privilege_level; + }; + + /** + * @brief Reads the current value of the stack segment (SS) register. + * + * @return uint16_t The current SS register value. + */ + auto read_ss() -> uint16_t; + + /** + * @brief Writes a new value to the stack segment (SS) register. + * + * @param selector The segment selector to be written to SS. + */ + auto write_ss(segment_selector selector) -> void; + +} // namespace teachos::arch::memory::cpu + +#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_SS_HPP diff --git a/arch/x86_64/include/arch/memory/cpu/tlb.hpp b/arch/x86_64/include/arch/kernel/cpu/tlb.hpp index 075d7bb..333cd58 100644 --- a/arch/x86_64/include/arch/memory/cpu/tlb.hpp +++ b/arch/x86_64/include/arch/kernel/cpu/tlb.hpp @@ -1,5 +1,5 @@ -#ifndef TEACHOS_ARCH_X86_64_MEMORY_CPU_TLB_HPP -#define TEACHOS_ARCH_X86_64_MEMORY_CPU_TLB_HPP +#ifndef TEACHOS_ARCH_X86_64_KERNEL_CPU_TLB_HPP +#define TEACHOS_ARCH_X86_64_KERNEL_CPU_TLB_HPP #include "arch/memory/paging/virtual_page.hpp" @@ -24,4 +24,4 @@ namespace teachos::arch::memory::cpu } // namespace teachos::arch::memory::cpu -#endif // TEACHOS_ARCH_X86_64_MEMORY_CPU_TLB_HPP +#endif // TEACHOS_ARCH_X86_64_KERNEL_CPU_TLB_HPP diff --git a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp b/arch/x86_64/include/arch/memory/paging/active_page_table.hpp index 1b2aaed..9846a21 100644 --- a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp +++ b/arch/x86_64/include/arch/memory/paging/active_page_table.hpp @@ -2,8 +2,8 @@ #define TEACHOS_ARCH_X86_64_MEMORY_PAGING_ACTIVE_PAGE_TABLE_HPP #include "arch/exception_handling/assert.hpp" +#include "arch/kernel/cpu/tlb.hpp" #include "arch/memory/allocator/concept.hpp" -#include "arch/memory/cpu/tlb.hpp" #include "arch/memory/paging/virtual_page.hpp" #include <array> @@ -75,8 +75,8 @@ namespace teachos::arch::memory::paging * @param flags A bitset of flags that configure the page table entry for this mapping. */ template<allocator::FrameAllocator T> - auto map_page_to_frame(T & allocator, virtual_page page, allocator::physical_frame frame, - std::bitset<64U> flags) -> void + auto map_page_to_frame(T & allocator, virtual_page page, allocator::physical_frame frame, std::bitset<64U> flags) + -> void { auto current_handle = active_handle; 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 74f1c14..b137736 100644 --- a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp +++ b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp @@ -1,7 +1,7 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_PAGING_KERNEL_MAPPER_HPP #define TEACHOS_ARCH_X86_64_MEMORY_PAGING_KERNEL_MAPPER_HPP -#include "arch/memory/cpu/control_register.hpp" +#include "arch/kernel/cpu/control_register.hpp" #include "arch/memory/paging/active_page_table.hpp" #include "arch/memory/paging/inactive_page_table.hpp" #include "arch/memory/paging/temporary_page.hpp" diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index dbea42a..39bfe33 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -199,7 +199,7 @@ _start: cli /* Clears the interrupt flag during the GDT setup */ lgdt (global_descriptor_table_pointer) - jmp $global_descriptor_table_code,$_transition_to_long_mode + jmp $global_descriptor_table_code, $_transition_to_long_mode /* The interrupt flag is set in cpp after setting up the GDT */ call halt diff --git a/arch/x86_64/src/context_switching/descriptor_table/global_descriptor_table.cpp b/arch/x86_64/src/context_switching/descriptor_table/global_descriptor_table.cpp new file mode 100644 index 0000000..1cba13c --- /dev/null +++ b/arch/x86_64/src/context_switching/descriptor_table/global_descriptor_table.cpp @@ -0,0 +1,57 @@ +#include "global_descriptor_table.hpp" + +#include "arch/stl/vector.hpp" + +#include "segment_descriptor.hpp" + +namespace teachos::arch::context_switching::descriptor_table +{ + auto create_global_descriptor_table() -> global_descriptor_table + { + segment_descriptor null_segment{0}; + + // Kernel space code segment + access_byte kernel_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL | + access_byte::CODE_OR_DATA_SEGMENT, + type_field::CODE_SEGMENT | type_field::READABLE}; + gdt_flags kernel_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH}; + segment_descriptor kernel_code_segment{kernel_code_access_byte, kernel_code_gdt_flags, 0, 0xFFFFF}; + + // Kernel space data segment + access_byte kernel_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL | + access_byte::CODE_OR_DATA_SEGMENT, + type_field::WRITABLE}; + gdt_flags kernel_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND}; + segment_descriptor kernel_data_segment{kernel_data_access_byte, kernel_data_gdt_flags, 0, 0xFFFFF}; + + // User space code segment + access_byte user_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER | + access_byte::CODE_OR_DATA_SEGMENT, + type_field::CODE_SEGMENT | type_field::READABLE}; + gdt_flags user_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH}; + segment_descriptor user_code_segment{user_code_access_byte, user_code_gdt_flags, 0, 0xFFFFF}; + + // User space data segment + access_byte user_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER | + access_byte::CODE_OR_DATA_SEGMENT, + type_field::WRITABLE}; + gdt_flags user_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND}; + segment_descriptor user_data_segment{user_data_access_byte, user_data_gdt_flags, 0, 0xFFFFF}; + + stl::vector<segment_descriptor> global_descriptor_table{null_segment, kernel_code_segment, kernel_data_segment, + user_code_segment, user_data_segment}; + return global_descriptor_table; + } + + auto load_global_descriptor_table(global_descriptor_table_pointer gdt_pointer) -> void + { + // + } + + auto initialize_global_descriptor_table() -> global_descriptor_table + { + global_descriptor_table gdt{create_global_descriptor_table()}; + global_descriptor_table_pointer gdt_pointer{gdt.size() - 1, &gdt}; + load_global_descriptor_table(gdt_pointer); + } +} // namespace teachos::arch::context_switching::descriptor_table
\ No newline at end of file diff --git a/arch/x86_64/src/memory/cpu/control_register.cpp b/arch/x86_64/src/kernel/cpu/control_register.cpp index 7ee88b5..3051bae 100644 --- a/arch/x86_64/src/memory/cpu/control_register.cpp +++ b/arch/x86_64/src/kernel/cpu/control_register.cpp @@ -1,4 +1,4 @@ -#include "arch/memory/cpu/control_register.hpp" +#include "arch/kernel/cpu/control_register.hpp" #include "arch/exception_handling/panic.hpp" diff --git a/arch/x86_64/src/memory/cpu/msr.cpp b/arch/x86_64/src/kernel/cpu/msr.cpp index b83f902..082bca9 100644 --- a/arch/x86_64/src/memory/cpu/msr.cpp +++ b/arch/x86_64/src/kernel/cpu/msr.cpp @@ -1,4 +1,4 @@ -#include "arch/memory/cpu/msr.hpp" +#include "arch/kernel/cpu/msr.hpp" namespace teachos::arch::memory::cpu { diff --git a/arch/x86_64/src/kernel/cpu/ss.cpp b/arch/x86_64/src/kernel/cpu/ss.cpp new file mode 100644 index 0000000..b7e52e1 --- /dev/null +++ b/arch/x86_64/src/kernel/cpu/ss.cpp @@ -0,0 +1,33 @@ +#include "arch/kernel/cpu/ss.hpp" + +namespace teachos::arch::memory::cpu +{ + segment_selector::segment_selector(uint16_t index, std::bitset<1U> table_indicator, + std::bitset<2U> requested_privilege_level) + : index(index) + , table_indicator(table_indicator) + , requested_privilege_level(requested_privilege_level) + { + // Nothing to do + } + + auto segment_selector::to_uint16() const -> uint16_t + { + return static_cast<uint16_t>((index << 3) | (table_indicator.to_ulong() << 2) | + requested_privilege_level.to_ulong()); + } + + auto read_ss() -> uint16_t + { + uint16_t ss; + __asm__("mov %%ss, %0" : "=r"(ss)); + return ss; + } + + auto write_ss(segment_selector selector) -> void + { + uint16_t ss = selector.to_uint16(); + __asm__("mov %0, %%ss" ::"r"(ss)); + } + +} // namespace teachos::arch::memory::cpu
\ No newline at end of file diff --git a/arch/x86_64/src/memory/cpu/tlb.cpp b/arch/x86_64/src/kernel/cpu/tlb.cpp index 591d9fc..e753c2c 100644 --- a/arch/x86_64/src/memory/cpu/tlb.cpp +++ b/arch/x86_64/src/kernel/cpu/tlb.cpp @@ -1,6 +1,6 @@ -#include "arch/memory/cpu/tlb.hpp" +#include "arch/kernel/cpu/tlb.hpp" -#include "arch/memory/cpu/control_register.hpp" +#include "arch/kernel/cpu/control_register.hpp" namespace teachos::arch::memory::cpu { diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index d3938ed..4db9599 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -1,6 +1,6 @@ #include "arch/kernel/main.hpp" -#include "arch/context_switching/descriptor_table/segment_descriptor.hpp" +#include "arch/context_switching/descriptor_table/global_descriptor_table.hpp" #include "arch/memory/heap/bump_allocator.hpp" #include "arch/memory/heap/global_heap_allocator.hpp" #include "arch/memory/main.hpp" @@ -60,50 +60,14 @@ namespace teachos::arch::kernel heap_test(); - using context_switching::descriptor_table::access_byte; - using context_switching::descriptor_table::gdt_flags; - using context_switching::descriptor_table::segment_descriptor; - using context_switching::descriptor_table::segment_descriptor_type; - using context_switching::descriptor_table::type_field; - - segment_descriptor null_segment{0}; - - // Kernel space code segment - access_byte kernel_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL | - access_byte::CODE_OR_DATA_SEGMENT, - type_field::CODE_SEGMENT | type_field::READABLE}; - gdt_flags kernel_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH}; - segment_descriptor kernel_code_segment{kernel_code_access_byte, kernel_code_gdt_flags, 0, 0xFFFFF}; - - // Kernel space data segment - access_byte kernel_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL | - access_byte::CODE_OR_DATA_SEGMENT, - type_field::WRITABLE}; - gdt_flags kernel_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND}; - segment_descriptor kernel_data_segment{kernel_data_access_byte, kernel_data_gdt_flags, 0, 0xFFFFF}; - - // User space code segment - access_byte user_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER | - access_byte::CODE_OR_DATA_SEGMENT, - type_field::CODE_SEGMENT | type_field::READABLE}; - gdt_flags user_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH}; - segment_descriptor user_code_segment{user_code_access_byte, user_code_gdt_flags, 0, 0xFFFFF}; - - // User space data segment - access_byte user_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER | - access_byte::CODE_OR_DATA_SEGMENT, - type_field::WRITABLE}; - gdt_flags user_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND}; - segment_descriptor user_data_segment{user_data_access_byte, user_data_gdt_flags, 0, 0xFFFFF}; - - stl::vector<segment_descriptor> global_descriptor_table{null_segment, kernel_code_segment, kernel_data_segment, - user_code_segment, user_data_segment}; + context_switching::descriptor_table::global_descriptor_table global_descriptor_table{ + context_switching::descriptor_table::initialize_global_descriptor_table()}; decltype(auto) x = global_descriptor_table.at(1); if (global_descriptor_table.size() == 0) { } - if (x.get_segment_type() == segment_descriptor_type::CODE_SEGMENT) + if (x.get_segment_type() == context_switching::descriptor_table::segment_descriptor_type::CODE_SEGMENT) { } video::vga::text::write("GDT FILLED", video::vga::text::common_attributes::green_on_black); diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp index a6f91d9..abc7431 100644 --- a/arch/x86_64/src/memory/main.cpp +++ b/arch/x86_64/src/memory/main.cpp @@ -1,9 +1,9 @@ #include "arch/memory/main.hpp" #include "arch/exception_handling/assert.hpp" +#include "arch/kernel/cpu/control_register.hpp" +#include "arch/kernel/cpu/msr.hpp" #include "arch/memory/allocator/area_frame_allocator.hpp" -#include "arch/memory/cpu/control_register.hpp" -#include "arch/memory/cpu/msr.hpp" #include "arch/memory/heap/heap_allocator.hpp" #include "arch/memory/paging/active_page_table.hpp" #include "arch/memory/paging/kernel_mapper.hpp" |
