diff options
14 files changed, 113 insertions, 63 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c895618..35cfd65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,8 +122,14 @@ add_library("teachos::exception" ALIAS "_exception") add_library("_context" OBJECT) add_library("teachos::context_switching" ALIAS "_context") +add_library("_interrupt_handling" OBJECT) +add_library("teachos::interrupt_handling" ALIAS "_interrupt_handling") +# https://forum.osdev.org/viewtopic.php?f=1&t=36712 +# https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-interrupt-function-attribute_002c-x86 +target_compile_options("_interrupt_handling" PRIVATE "-mgeneral-regs-only") + #[============================================================================[ -# The Context switching Library +# The Stub Standard Library #]============================================================================] add_library("_stl" OBJECT) @@ -145,6 +151,7 @@ target_link_libraries("_kernel" PRIVATE "teachos::exception" "teachos::stl" "teachos::context_switching" + "teachos::interrupt_handling" ) #[============================================================================[ diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index 5242f3d..5e87594 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -107,6 +107,14 @@ target_sources("_context" PRIVATE ) #[============================================================================[ +# The Interrupt Handlers +#]============================================================================] + +target_sources("_interrupt_handling" PRIVATE + "src/interrupt_handling/generic_interrupt_handler.cpp" +) + +#[============================================================================[ # The Bootable ISO Image #]============================================================================] 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 a652e0c..e677cbb 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 @@ -1,5 +1,5 @@ -#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP -#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP +#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP +#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP #include "arch/context_switching/interrupt_descriptor_table/idt_flags.hpp" #include "arch/context_switching/interrupt_descriptor_table/ist_offset.hpp" @@ -57,15 +57,14 @@ 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. Represents the address of the entry point of the - ///< Interrupt Service Routine. (0 - 15) + uint16_t _offset_1 = {}; ///< Lower 16 bits of handler function address (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. Represents the address of the entry point of - ///< the Interrupt Service Routine. (48 - 95) + uint16_t _offset_2 = {}; ///< Middle 16 bits of handler function address (48 - 63) + uint32_t _offset_3 = {}; ///< Upper 32 bits of handler function address (for x86_64) (64 - 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 -#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP +#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_GATE_DESCRIPTOR_HPP diff --git a/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp b/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp index dd55cd7..e2ec4c5 100644 --- a/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp +++ b/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp @@ -1,5 +1,5 @@ -#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP -#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP +#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP +#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP #include "arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp" @@ -15,4 +15,4 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table } // namespace teachos::arch::context_switching::interrupt_descriptor_table -#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP +#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_HPP diff --git a/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp b/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp index d853ff0..7fe933b 100644 --- a/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp +++ b/arch/x86_64/include/arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.hpp @@ -1,5 +1,5 @@ -#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP -#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP +#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP +#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP #include "arch/context_switching/interrupt_descriptor_table/gate_descriptor.hpp" #include "arch/stl/vector.hpp" @@ -23,7 +23,7 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table /** * @brief Constructor. */ - interrupt_descriptor_table_pointer(uint16_t table_length, interrupt_descriptor_table * address); + interrupt_descriptor_table_pointer(uint16_t table_length, gate_descriptor * address); /** * @brief Defaulted three-way comparsion operator. @@ -31,10 +31,10 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table auto operator<=>(interrupt_descriptor_table_pointer const & other) const -> std::strong_ordering = default; private: - uint16_t table_length = {}; ///< The amount of segment descriptor entries in the global descriptor table - 1. - interrupt_descriptor_table * address = {}; ///< Non-owning pointer to the IDT base address. + uint16_t table_length = {}; ///< The amount of segment descriptor entries in the global descriptor table - 1. + gate_descriptor * address = {}; ///< Non-owning pointer to the IDT base address. }; } // namespace teachos::arch::context_switching::interrupt_descriptor_table -#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP +#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_INTERRUPT_DESCRIPTOR_TABLE_INTERRUPT_DESCRIPTOR_TABLE_POINTER_HPP diff --git a/arch/x86_64/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp b/arch/x86_64/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp index 764aec5..8217bcb 100644 --- a/arch/x86_64/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp +++ b/arch/x86_64/include/arch/context_switching/segment_descriptor_table/gdt_flags.hpp @@ -18,22 +18,16 @@ namespace teachos::arch::context_switching::segment_descriptor_table */ enum bitset : uint8_t { + AVAILABLE = 1U << 0U, ///< Available for use by System software. For our purposes this is basically reserved. LONG_MODE = 1U << 1U, ///< Defines in IA-32e mode (64-bit code and 32-bit compatability mode) if the segment - ///< contains 64-bit code. Otherwise this bit should always be 0. Enable if instructions are - ///< executed in 64-bit code, otherwise they are executed in compatability 32-bit mode. If bis - ///< is set the DEFAULT_LENGTH bis needs to be 0 + ///< contains 64-bit code. Otherwise this bit should always be 0. Enable if instructions + ///< are executed in 64-bit code, otherwise they are executed in compatability 32-bit mode. + ///< If this is set the DEFAULT_OPERAND_SIZE/BIG bit needs to be clear (0). - // FIXME: Where does this come from, and is this value correct? - UPPER_BOUND = 1U << 1U, ///< Specifies the upper bound of the segment for expand down data segment. Enable for 5 - ///< GiB, 4 KiB otherwise. - - // FIXME: Where does this come from, and is this value correct? - STACK_POINTER_SIZE = 1U << 1U, ///< Specifies the size of the Stack Pointer (SP) for stack segments used for - ///< implicit stack operations. Enable for 32 bit, 16 bit otherwise. - - // FIXME: Where does this come from, and is this value correct? - DEFAULT_LENGTH = 1U << 1U, ///< Indicates the default length for code segments with effective addresses and - ///< operands. Enable for 32 bit, 16 bit otherwise. + DEFAULT_OPERATION_SIZE = + 1U << 2U, ///< If clear, this is a 16-bit code segment; if set, this is a 32-bit segment. + BIG = 1U << 2U, ///< If set, the maximum offset size for a data segment is increased to 32-bit + ///< 0xffffffff. Otherwise it's the 16-bit max 0x0000ffff. Essentially the same meaning as "D". GRANULARITY = 1U << 3U, ///< Indicates the size the Limit value in the segment descriptor is scaled by 1 Byte ///< blocks if the bit is not set or by 4 KiB blocks if the bit is set. diff --git a/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp b/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp new file mode 100644 index 0000000..9f33fa0 --- /dev/null +++ b/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp @@ -0,0 +1,28 @@ +#ifndef TEACHOS_ARCH_X86_64_INTERRUPT_HANDLING_GENERIC_INTERRUPT_HANDLER_HPP +#define TEACHOS_ARCH_X86_64_INTERRUPT_HANDLING_GENERIC_INTERRUPT_HANDLER_HPP + +#include <cstdint> + +namespace teachos::arch::interrupt_handling +{ + /** + * @brief This has been created in a rush. I think it is correct + * + * TODO: Create doxygen + * + */ + struct interrupt_frame + { + uint64_t ip; ///< Dummy + uint64_t cs; ///< Dummy + uint64_t flags; ///< Dummy + uint64_t sp; ///< Dummy + uint64_t ss; ///< Dummy + }; + + [[gnu::interrupt]] + auto generic_interrupt_handler(struct interrupt_frame * frame) -> void; + +} // namespace teachos::arch::interrupt_handling + +#endif // TEACHOS_ARCH_X86_64_INTERRUPT_HANDLING_GENERIC_INTERRUPT_HANDLER_HPP diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 108dbe5..38a8af4 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -197,10 +197,8 @@ _start: call enable_paging call enable_sse - cli /* Clears the interrupt flag during the GDT setup */ lgdt (global_descriptor_table_pointer) jmp $global_descriptor_table_code, $_transition_to_long_mode - /* The interrupt flag is set in cpp after setting up the GDT */ call halt @@ -369,14 +367,7 @@ reload_segment_register: ret _transition_to_long_mode: - //call reload_segment_register - - xor %rax, %rax - mov %rax, %ss - mov %rax, %ds - mov %rax, %es - mov %rax, %fs - mov %rax, %gs + call reload_segment_register movl $0xb8000, (vga_buffer_pointer) diff --git a/arch/x86_64/src/context_switching/interrupt_descriptor_table/gate_descriptor.cpp b/arch/x86_64/src/context_switching/interrupt_descriptor_table/gate_descriptor.cpp index 28f289c..d86c459 100644 --- a/arch/x86_64/src/context_switching/interrupt_descriptor_table/gate_descriptor.cpp +++ b/arch/x86_64/src/context_switching/interrupt_descriptor_table/gate_descriptor.cpp @@ -8,6 +8,7 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table , _ist(flags >> 32U) , _flags(flags >> 40U) , _offset_2(flags >> 48U) + , _offset_3(flags >> 64U) { // Nothing to do. } @@ -18,6 +19,7 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table , _ist(ist) , _flags(flags) , _offset_2(offset >> 16U) + , _offset_3(offset >> 48U) { // Nothing to do. } 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 ddc098e..1c1de68 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 @@ -1,20 +1,24 @@ #include "arch/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.hpp" #include "arch/exception_handling/assert.hpp" +#include "arch/interrupt_handling/generic_interrupt_handler.hpp" #include "arch/kernel/cpu/idtr.hpp" namespace teachos::arch::context_switching::interrupt_descriptor_table { namespace { + auto create_interrupt_descriptor_table() -> interrupt_descriptor_table { - uint64_t offset = 0U; + // @MTO: This address resolution is most certainly wrong -> numbers in dbg seem off (offset_3 = 0) + uint64_t offset = reinterpret_cast<uint64_t>(&interrupt_handling::generic_interrupt_handler); segment_selector selector{0U, segment_selector::REQUEST_LEVEL_KERNEL}; ist_offset ist{0U}; - idt_flags flags{idt_flags::DESCRIPTOR_LEVEL_KERNEL}; - gate_descriptor gate_descriptor{selector, ist, flags, offset}; - return interrupt_descriptor_table{gate_descriptor}; + idt_flags flags{idt_flags::DESCRIPTOR_LEVEL_KERNEL | idt_flags::PRESENT}; + gate_descriptor descriptor{selector, ist, flags, offset}; + + return interrupt_descriptor_table{descriptor}; } } // namespace @@ -24,7 +28,7 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table static auto idt = create_interrupt_descriptor_table(); interrupt_descriptor_table_pointer idt_pointer{static_cast<uint16_t>((idt.size() * sizeof(gate_descriptor)) - 1), - &idt}; + idt.data()}; 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/interrupt_descriptor_table/interrupt_descriptor_table_pointer.cpp b/arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.cpp index 981944d..7bcbae6 100644 --- a/arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.cpp +++ b/arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.cpp @@ -3,7 +3,7 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table { interrupt_descriptor_table_pointer::interrupt_descriptor_table_pointer(uint16_t table_length, - interrupt_descriptor_table * address) + gate_descriptor * address) : table_length(table_length) , address(address) { diff --git a/arch/x86_64/src/context_switching/main.cpp b/arch/x86_64/src/context_switching/main.cpp index a5bd3fb..f449a3a 100644 --- a/arch/x86_64/src/context_switching/main.cpp +++ b/arch/x86_64/src/context_switching/main.cpp @@ -10,24 +10,30 @@ namespace teachos::arch::context_switching { auto initialize_descriptor_tables() -> descriptor_tables { + kernel::cpu::clear_interrupt_flag(); decltype(auto) global_descriptor_table = segment_descriptor_table::initialize_global_descriptor_table(); decltype(auto) interrupt_descriptor_table = interrupt_descriptor_table::initialize_interrupt_descriptor_table(); - kernel::cpu::far_pointer pointer{&boot::reload_segment_register, 1 * sizeof(segment_descriptor_table::segment_descriptor)}; - asm volatile("rex64 lcall *%[far_function_pointer]" : : [far_function_pointer] "m" (pointer)); + kernel::cpu::far_pointer pointer{&boot::reload_segment_register, + 1 * sizeof(segment_descriptor_table::segment_descriptor)}; + asm volatile("rex64 lcall *%[far_function_pointer]" : : [far_function_pointer] "m"(pointer)); - // // Load task state segment descriptor from the last element in the global descriptor table, done by calculating - // // offset in bytes to the start of the segment descriptor (5 * 16) = 80 - // uint16_t const tss_selector = - // (global_descriptor_table.size() - 1) * sizeof(segment_descriptor_table::segment_descriptor); - // kernel::cpu::load_task_register(tss_selector); + // Load task state segment descriptor from the last element in the global descriptor table, done by calculating + // offset in bytes to the start of the segment descriptor (5 * 16) = 80 + uint16_t const tss_selector = + (global_descriptor_table.size() - 1) * sizeof(segment_descriptor_table::segment_descriptor); + kernel::cpu::load_task_register(tss_selector); - // auto const stored_task_register = kernel::cpu::store_task_register(); - // arch::exception_handling::assert(tss_selector == stored_task_register, - // "[Global Descriptor Table] Loaded TR value is not the same as the stored value."); + auto const stored_task_register = kernel::cpu::store_task_register(); + arch::exception_handling::assert(tss_selector == stored_task_register, + "[Global Descriptor Table] Loaded TR value is not the same as the stored value."); - // FIXME: We currently cannot enable interrupts, since for some reason, we will later run into what looks like a GP. Maybe because no IDT is loaded? Maybe our boot code segment is not set up correctly? - // kernel::cpu::set_interrupt_flag(); + // FIXME: We currently cannot enable interrupts, since for some reason, we will later run into what looks like a GP + // and triple fault. + + // @MTO: SOMETIMES i get past a breakpoint here???? seems to happen when i actually pause before (f.e. inside the + // idt). NEVER happened when stepping through quickly. Can you reproduce this? + kernel::cpu::set_interrupt_flag(); descriptor_tables tables = {global_descriptor_table, interrupt_descriptor_table}; return tables; 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 37ee778..d9ad91c 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 @@ -17,17 +17,14 @@ namespace teachos::arch::context_switching::segment_descriptor_table { uint8_t access_level_bits = access_byte::PRESENT | access_byte::CODE_OR_DATA_SEGMENT | *reinterpret_cast<uint8_t *>(&access_level); - uint8_t gdt_flags_bits = gdt_flags::GRANULARITY; + uint8_t gdt_flags_bits = gdt_flags::GRANULARITY | gdt_flags::LONG_MODE; if (segment_descriptor_type == segment_descriptor_type::CODE_SEGMENT) { - gdt_flags_bits |= gdt_flags::LONG_MODE; access_level_bits |= access_byte::CODE_SEGMENT | access_byte::READABLE; } else if (segment_descriptor_type == segment_descriptor_type::DATA_SEGMENT) { gdt_flags_bits |= 1 << 2; - // FIXME: Look at those bit flags, something seems off. - // gdt_flags_bits |= gdt_flags::UPPER_BOUND; access_level_bits |= access_byte::WRITABLE; } diff --git a/arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp b/arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp new file mode 100644 index 0000000..68b4568 --- /dev/null +++ b/arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp @@ -0,0 +1,14 @@ +#include "arch/interrupt_handling/generic_interrupt_handler.hpp" + +#include "arch/video/vga/text.hpp" + +namespace teachos::arch::interrupt_handling +{ + + [[gnu::interrupt]] + auto generic_interrupt_handler(interrupt_frame * frame) -> void + { + (void)frame; + video::vga::text::write("An Interrupt occurred.", video::vga::text::common_attributes::green_on_black); + } +} // namespace teachos::arch::interrupt_handling
\ No newline at end of file |
