aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2025-03-29 14:47:04 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2025-03-29 14:47:04 +0000
commitaba154ad01fc0e1e1274f2582b1493e78daa2559 (patch)
tree15aad40a97406a76e466191ef7afc5ed4ab10006 /arch/x86_64/src
parent98be1b722f17e77880e8c0b0f464a9ba31230fc4 (diff)
downloadteachos-aba154ad01fc0e1e1274f2582b1493e78daa2559.tar.xz
teachos-aba154ad01fc0e1e1274f2582b1493e78daa2559.zip
fix gdt segments, improve idt and trial&error for triple fault
Diffstat (limited to 'arch/x86_64/src')
-rw-r--r--arch/x86_64/src/boot/boot.s11
-rw-r--r--arch/x86_64/src/context_switching/interrupt_descriptor_table/gate_descriptor.cpp2
-rw-r--r--arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.cpp14
-rw-r--r--arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.cpp2
-rw-r--r--arch/x86_64/src/context_switching/main.cpp30
-rw-r--r--arch/x86_64/src/context_switching/segment_descriptor_table/global_descriptor_table.cpp5
-rw-r--r--arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp14
7 files changed, 46 insertions, 32 deletions
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