From 5eb8d63a6ece530cb1d56217a046553b4b96245d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Wed, 30 Apr 2025 14:50:34 +0000 Subject: Note linux calling contract for implementation --- arch/x86_64/src/context_switching/main.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/context_switching/main.cpp') diff --git a/arch/x86_64/src/context_switching/main.cpp b/arch/x86_64/src/context_switching/main.cpp index 8d1c019..7db9583 100644 --- a/arch/x86_64/src/context_switching/main.cpp +++ b/arch/x86_64/src/context_switching/main.cpp @@ -35,6 +35,16 @@ namespace teachos::arch::context_switching { 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: + + // Syscall Number: RAX, Return Value: RAX (0 indicating no error, and -1 indicating an error, use as a boolean) + // Argument in this order (max 6. no argument on stack): RDI, RSI, RDX, R10, R8, R9 + // Not used registers: RBX, RSP, R12, R13, R14 + + // 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 new_value = 60U; asm volatile("mov %[input], %%rax" : /* no output from call */ @@ -70,7 +80,7 @@ namespace teachos::arch::context_switching { uint64_t const syscall_function = reinterpret_cast(syscall_handler); kernel::cpu::write_msr(IA32_LSTAR_ADDRESS, syscall_function); - kernel::cpu::write_msr(IA32_FMASK_ADDRESS, 1 << 9U); // Disable interrupt flag during syscall. + kernel::cpu::write_msr(IA32_FMASK_ADDRESS, 0U); uint64_t kernel_cs = KERNEL_CODE_SEGMENT_SELECTOR; // We want to provide the user code segment, but the instruction calculates + 0x10 to fill the -- cgit v1.2.3