aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/context_switching/main.cpp
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2025-04-27 14:07:33 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2025-04-27 14:07:33 +0000
commit13dd2bd5a88ec7efeadf8586778f2c5a26d8cd9e (patch)
tree9dcf8c9efeaa74f136a796531caee323aceaae37 /arch/x86_64/src/context_switching/main.cpp
parent187eba4eca3ea46d8c26419168e525242338dae4 (diff)
downloadteachos-13dd2bd5a88ec7efeadf8586778f2c5a26d8cd9e.tar.xz
teachos-13dd2bd5a88ec7efeadf8586778f2c5a26d8cd9e.zip
Move not public methods into anonymous namespace
Diffstat (limited to 'arch/x86_64/src/context_switching/main.cpp')
-rw-r--r--arch/x86_64/src/context_switching/main.cpp59
1 files changed, 38 insertions, 21 deletions
diff --git a/arch/x86_64/src/context_switching/main.cpp b/arch/x86_64/src/context_switching/main.cpp
index 155d150..f73cb19 100644
--- a/arch/x86_64/src/context_switching/main.cpp
+++ b/arch/x86_64/src/context_switching/main.cpp
@@ -1,7 +1,6 @@
#include "arch/context_switching/main.hpp"
#include "arch/boot/pointers.hpp"
-#include "arch/context_switching/syscall_handler.hpp"
#include "arch/exception_handling/assert.hpp"
#include "arch/kernel/cpu/call.hpp"
#include "arch/kernel/cpu/control_register.hpp"
@@ -30,6 +29,43 @@ namespace teachos::arch::context_switching
auto reload_global_descriptor_table_register() -> void { kernel::cpu::call(KERNEL_CODE_POINTER); }
+ auto user_mode_main() -> void
+ {
+ kernel::cpu::validate_segment_registers(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR);
+
+ video::vga::text::write("Successfully entered user mode!", video::vga::text::common_attributes::green_on_black);
+
+ asm volatile("syscall");
+
+ kernel::cpu::validate_segment_registers(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR);
+
+ video::vga::text::write("Successfully made a SYSCALL and returned back with SYSRET!",
+ video::vga::text::common_attributes::green_on_black);
+ }
+
+ auto syscall_handler() -> void
+ {
+ uint64_t dummy{};
+ switch (dummy)
+ {
+ case 0:
+ break;
+ default:
+ break;
+ }
+
+ asm volatile("sysretq");
+ }
+
+ auto enable_systemcall() -> void
+ {
+ uint64_t const syscall_function = reinterpret_cast<uint64_t>(syscall_handler);
+ kernel::cpu::write_msr(IA32_LSTAR_ADDRESS, syscall_function);
+ kernel::cpu::write_msr(IA32_FMASK_ADDRESS, 0U);
+ kernel::cpu::write_msr(IA32_STAR_ADDRESS, KERNEL_CODE_SEGMENT_SELECTOR);
+ kernel::cpu::set_efer_bit(kernel::cpu::efer_flags::SCE);
+ }
+
} // namespace
auto initialize_descriptor_tables() -> descriptor_tables
@@ -55,17 +91,9 @@ namespace teachos::arch::context_switching
return tables;
}
- auto user_mode_main() -> void
- {
- kernel::cpu::validate_segment_registers(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR);
-
- asm volatile("SYSCALL");
-
- video::vga::text::write("Successfully entered user mode!", video::vga::text::common_attributes::green_on_black);
- }
-
auto switch_to_user_mode() -> void
{
+ enable_systemcall();
switch_context(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR, user_mode_main);
}
@@ -77,15 +105,4 @@ namespace teachos::arch::context_switching
kernel::cpu::set_code_segment_register(data_segment, code_segment, reinterpret_cast<uint64_t>(return_function));
}
- auto setup_syscall() -> void
- {
- uint64_t const syscall_function = reinterpret_cast<uint64_t>(syscall_handler);
- uint64_t const segment_selector = *reinterpret_cast<uint64_t const *>(&KERNEL_CODE_SEGMENT_SELECTOR);
-
- kernel::cpu::write_msr(IA32_LSTAR_ADDRESS, syscall_function);
- kernel::cpu::write_msr(IA32_FMASK_ADDRESS, 0U);
- kernel::cpu::write_msr(IA32_STAR_ADDRESS, segment_selector);
- kernel::cpu::set_efer_bit(kernel::cpu::efer_flags::SCE);
- }
-
} // namespace teachos::arch::context_switching