aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/context_switching/main.cpp
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2025-05-03 09:45:45 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2025-05-03 09:45:45 +0000
commit5a8c9d2f2e4a3d2810f81c35070c6ef0926cfdd1 (patch)
treef93079bf5d250e176abbd3db9902f5d22285f6a4 /arch/x86_64/src/context_switching/main.cpp
parent4b8674bee6089aef1e2c6b9064c6109f1cd392da (diff)
downloadteachos-5a8c9d2f2e4a3d2810f81c35070c6ef0926cfdd1.tar.xz
teachos-5a8c9d2f2e4a3d2810f81c35070c6ef0926cfdd1.zip
write wrapper function for syscall
Diffstat (limited to 'arch/x86_64/src/context_switching/main.cpp')
-rw-r--r--arch/x86_64/src/context_switching/main.cpp71
1 files changed, 8 insertions, 63 deletions
diff --git a/arch/x86_64/src/context_switching/main.cpp b/arch/x86_64/src/context_switching/main.cpp
index 06c0810..7fe159e 100644
--- a/arch/x86_64/src/context_switching/main.cpp
+++ b/arch/x86_64/src/context_switching/main.cpp
@@ -1,11 +1,11 @@
#include "arch/context_switching/main.hpp"
#include "arch/boot/pointers.hpp"
+#include "arch/context_switching/syscall/main.hpp"
#include "arch/exception_handling/assert.hpp"
#include "arch/kernel/cpu/call.hpp"
#include "arch/kernel/cpu/control_register.hpp"
#include "arch/kernel/cpu/if.hpp"
-#include "arch/kernel/cpu/msr.hpp"
#include "arch/kernel/cpu/segment_register.hpp"
#include "arch/kernel/cpu/tr.hpp"
#include "arch/video/vga/text.hpp"
@@ -14,10 +14,6 @@ namespace teachos::arch::context_switching
{
namespace
{
- auto constexpr IA32_STAR_ADDRESS = 0xC0000081;
- auto constexpr IA32_LSTAR_ADDRESS = 0xC0000082;
- auto constexpr IA32_FMASK_ADDRESS = 0xC0000084;
-
constexpr interrupt_descriptor_table::segment_selector KERNEL_CODE_SEGMENT_SELECTOR{
1U, interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_KERNEL};
constexpr kernel::cpu::far_pointer KERNEL_CODE_POINTER{&kernel::cpu::reload_data_segment_registers,
@@ -31,8 +27,6 @@ namespace teachos::arch::context_switching
auto user_mode_main() -> void
{
- video::vga::text::write("Successfully entered user mode!", video::vga::text::common_attributes::green_on_black);
-
// RFLAGS is saved into R11, RIP of the next instruction into RCX
// Required for SYSRETURN to know where to return too.
// Additional state needs to be saved by calling convention:
@@ -43,65 +37,16 @@ namespace teachos::arch::context_switching
// Actual Source: https://man7.org/linux/man-pages/man2/syscall.2.html More cleare documentation:
// https://sys.readthedocs.io/en/latest/doc/05_calling_system_calls.html
- uint64_t syscall_number = 60U;
- asm volatile("mov %[input], %%rax"
- : /* no output from call */
- : [input] "r"(syscall_number)
- : "memory");
- asm volatile("syscall");
-
- // Reading RAX decrements value by one in 32-bit compatability mode it also crashes vga write, therfore use
- // SYSRETQ instead of SYSRET so we do not return into 32-bit compatability mode.
- asm volatile("mov %%rax, %[output]" : [output] "=r"(syscall_number));
- video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!",
- video::vga::text::common_attributes::green_on_black);
- }
- auto syscall_handler() -> void
- {
- uint64_t syscall_number, arg_0, arg_1, arg_2, arg_3, arg_4, arg_5 = {};
- asm volatile("mov %%rax, %[output]" : [output] "=r"(syscall_number));
- asm volatile("mov %%rdi, %[output]" : [output] "=r"(arg_0));
- asm volatile("mov %%rsi, %[output]" : [output] "=r"(arg_1));
- asm volatile("mov %%rdx, %[output]" : [output] "=r"(arg_2));
- asm volatile("mov %%r10, %[output]" : [output] "=r"(arg_3));
- asm volatile("mov %%r8, %[output]" : [output] "=r"(arg_4));
- asm volatile("mov %%r9, %[output]" : [output] "=r"(arg_5));
-
- switch (syscall_number)
+ const char syscall_message[32] = "Successfully entered user mode!";
+ auto error = syscall::syscall(syscall::WRITE, {reinterpret_cast<uint64_t>(&syscall_message)});
+
+ if (!error)
{
- case 1:
- // Write VGA
- break;
- case 60U:
- // Exit
- break;
- default:
- break;
+ video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!",
+ video::vga::text::common_attributes::green_on_black);
}
-
- uint64_t result = 0U;
- asm volatile("mov %[input], %%rax"
- : /* no output from call */
- : [input] "r"(result)
- : "memory");
-
- 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);
-
- uint64_t const kernel_cs = KERNEL_CODE_SEGMENT_SELECTOR;
- uint64_t const star_value = (kernel_cs << 32) | (kernel_cs << 48);
- kernel::cpu::write_msr(IA32_STAR_ADDRESS, star_value);
-
- kernel::cpu::set_efer_bit(kernel::cpu::efer_flags::SCE);
- }
-
} // namespace
auto initialize_descriptor_tables() -> descriptor_tables
@@ -129,7 +74,7 @@ namespace teachos::arch::context_switching
auto switch_to_user_mode() -> void
{
- enable_systemcall();
+ syscall::enable_syscall();
switch_context(USER_DATA_SEGMENT_SELECTOR, USER_CODE_SEGMENT_SELECTOR, user_mode_main);
}