aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/kernel/main.cpp
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2025-04-05 15:27:20 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2025-04-05 15:27:20 +0000
commita8852f91967a7e55e62e30f5cc07d076092b8b78 (patch)
tree12708befd57fc0b89dbadc2e856c935b71c88807 /arch/x86_64/src/kernel/main.cpp
parentc01d080bdc6bd843e840e4834424fe587286b274 (diff)
downloadteachos-a8852f91967a7e55e62e30f5cc07d076092b8b78.tar.xz
teachos-a8852f91967a7e55e62e30f5cc07d076092b8b78.zip
add wip context switch to user mode
Diffstat (limited to 'arch/x86_64/src/kernel/main.cpp')
-rw-r--r--arch/x86_64/src/kernel/main.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp
index 7782d30..7d4173e 100644
--- a/arch/x86_64/src/kernel/main.cpp
+++ b/arch/x86_64/src/kernel/main.cpp
@@ -1,9 +1,11 @@
#include "arch/kernel/main.hpp"
#include "arch/boot/pointers.hpp"
+#include "arch/context_switching/interrupt_descriptor_table/segment_selector.hpp"
#include "arch/context_switching/main.hpp"
#include "arch/kernel/cpu/if.hpp"
#include "arch/kernel/cpu/jmp.hpp"
+#include "arch/kernel/cpu/segment_register.hpp"
#include "arch/memory/heap/bump_allocator.hpp"
#include "arch/memory/heap/global_heap_allocator.hpp"
#include "arch/memory/main.hpp"
@@ -49,6 +51,23 @@ namespace teachos::arch::kernel
delete test9;
}
+ [[gnu::naked]]
+ auto push_code_segment(context_switching::interrupt_descriptor_table::segment_selector segment_selector) -> void
+ {
+ asm volatile("push %%rbp\n"
+ "push %[input]"
+ : /* No output from call */
+ : [input] "m"(segment_selector));
+ }
+
+ [[gnu::naked]]
+ auto iret() -> void
+ {
+ asm volatile("iret"
+ : /* No output from call */
+ : /* No input to call */);
+ }
+
auto main() -> void
{
video::vga::text::clear();
@@ -64,6 +83,22 @@ namespace teachos::arch::kernel
heap_test();
decltype(auto) descriptor_tables = context_switching::initialize_descriptor_tables();
+
+ // - Clear NT flag in EFLAGS register (for far return)
+
+ // - Push return instruction pointer
+ // - Push return code segment selector
+ context_switching::interrupt_descriptor_table::segment_selector user_code_segment_selector{
+ 3U, context_switching::interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_USER};
+ push_code_segment(user_code_segment_selector);
+
+ context_switching::interrupt_descriptor_table::segment_selector user_data_segment_selector{
+ 4U, context_switching::interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_USER};
+ kernel::cpu::set_segment_registers(user_data_segment_selector);
+
+ // IRET
+ iret();
+
(void)descriptor_tables;
}
} // namespace teachos::arch::kernel