From 96711d45ad7fb5b96cfd2b4fffda8756fe68fcd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 26 Mar 2025 09:33:44 +0000 Subject: Fixing pointer values and adding basic idt value --- .../interrupt_descriptor_table/gate_descriptor.hpp | 6 ++++-- .../interrupt_descriptor_table.cpp | 20 ++++++++++++++++++-- .../global_descriptor_table.cpp | 5 ++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp b/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp index 196430f..a652e0c 100644 --- a/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp +++ b/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp @@ -57,11 +57,13 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table private: // The order in private variables starts for the first variable being the rightmost bit. - uint16_t _offset_1 = {}; ///< First part of the offset field (0 - 15) + uint16_t _offset_1 = {}; ///< First part of the offset field. Represents the address of the entry point of the + ///< Interrupt Service Routine. (0 - 15) segment_selector _selector = {}; ///< Segment selector (16 - 31) ist_offset _ist = {}; ///< Interrupt Stack Table offset (32 - 39) idt_flags _flags = {}; ///< Gate Type and Flags (40 - 47) - uint64_t _offset_2 : 48 = {}; ///< Second part of the offset field (48 - 95) + uint64_t _offset_2 : 48 = {}; ///< Second part of the offset field. Represents the address of the entry point of + ///< the Interrupt Service Routine. (48 - 95) uint32_t : 32; ///< Reserved field used to ensure this struct is 128 bits big (96 - 127) }; } // namespace teachos::arch::context_switching::interrupt_descriptor_table diff --git a/arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.cpp b/arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.cpp index 7346248..8bcce65 100644 --- a/arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.cpp +++ b/arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.cpp @@ -7,7 +7,22 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table { namespace { - auto create_interrupt_descriptor_table() -> interrupt_descriptor_table { return interrupt_descriptor_table{}; } + auto create_interrupt_descriptor_table() -> interrupt_descriptor_table + { + // TODO: Fix offset and ist and selector index + uint64_t const offset = 0x0; + segment_selector const selector{0U, segment_selector::REQUEST_LEVEL_KERNEL}; + ist_offset const ist{0U}; + idt_flags const flags{idt_flags::DESCRIPTOR_LEVEL_KERNEL | idt_flags::INTERRUPT_GATE}; + gate_descriptor const interrupt_gate{selector, ist, flags, offset}; + + segment_selector const selector{0U, segment_selector::REQUEST_LEVEL_KERNEL}; + ist_offset const ist{0U}; + idt_flags const flags{idt_flags::DESCRIPTOR_LEVEL_KERNEL | idt_flags::TRAP_GATE}; + gate_descriptor const trap_gate{selector, ist, flags, offset}; + + return interrupt_descriptor_table{interrupt_gate, trap_gate}; + } } // namespace auto initialize_interrupt_descriptor_table() -> interrupt_descriptor_table & @@ -15,7 +30,8 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table // Interrupt Descriptor Table needs to be kept alive static auto idt = create_interrupt_descriptor_table(); - interrupt_descriptor_table_pointer idt_pointer{static_cast(idt.size() - 1), &idt}; + interrupt_descriptor_table_pointer idt_pointer{static_cast((idt.size() * sizeof(gate_descriptor)) - 1), + &idt}; kernel::cpu::load_interrupt_descriptor_table(idt_pointer); auto const stored_gdt_pointer = kernel::cpu::store_interrupt_descriptor_table(); diff --git a/arch/x86_64/src/context_switching/segment_descriptor_table/global_descriptor_table.cpp b/arch/x86_64/src/context_switching/segment_descriptor_table/global_descriptor_table.cpp index 69cce19..e6a489c 100644 --- a/arch/x86_64/src/context_switching/segment_descriptor_table/global_descriptor_table.cpp +++ b/arch/x86_64/src/context_switching/segment_descriptor_table/global_descriptor_table.cpp @@ -75,7 +75,10 @@ namespace teachos::arch::context_switching::segment_descriptor_table // Global Descriptor Table needs to be kept alive static auto gdt = create_global_descriptor_table(); - global_descriptor_table_pointer gdt_pointer{static_cast(gdt.size() - 1), &gdt}; + // Calculate the size of the gdt in bytes - 1. This subtraction occurs because the maximum value of Size is 65535, + // while the GDT can be up to 65536 bytes in length (8192 entries). Further, no GDT can have a size of 0 bytes. + global_descriptor_table_pointer gdt_pointer{static_cast((gdt.size() * sizeof(segment_descriptor)) - 1), + &gdt}; kernel::cpu::load_global_descriptor_table(gdt_pointer); auto const stored_gdt_pointer = kernel::cpu::store_global_descriptor_table(); -- cgit v1.2.3