aboutsummaryrefslogtreecommitdiff
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
parent4b8674bee6089aef1e2c6b9064c6109f1cd392da (diff)
downloadteachos-5a8c9d2f2e4a3d2810f81c35070c6ef0926cfdd1.tar.xz
teachos-5a8c9d2f2e4a3d2810f81c35070c6ef0926cfdd1.zip
write wrapper function for syscall
-rw-r--r--arch/x86_64/CMakeLists.txt2
-rw-r--r--arch/x86_64/include/arch/context_switching/syscall/main.hpp28
-rw-r--r--arch/x86_64/include/arch/context_switching/syscall/syscall_handler.hpp23
-rw-r--r--arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp8
-rw-r--r--arch/x86_64/src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table.cpp5
-rw-r--r--arch/x86_64/src/context_switching/main.cpp71
-rw-r--r--arch/x86_64/src/context_switching/syscall/main.cpp57
-rw-r--r--arch/x86_64/src/context_switching/syscall/syscall_handler.cpp68
-rw-r--r--arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp26
-rw-r--r--arch/x86_64/src/kernel/cpu/control_register.cpp6
10 files changed, 186 insertions, 108 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 21dbddd..0b4eafe 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -100,6 +100,8 @@ target_sources("_context" PRIVATE
"src/context_switching/segment_descriptor_table/segment_descriptor_base.cpp"
"src/context_switching/segment_descriptor_table/segment_descriptor_extension.cpp"
"src/context_switching/main.cpp"
+ "src/context_switching/syscall/main.cpp"
+ "src/context_switching/syscall/syscall_handler.cpp"
"src/context_switching/interrupt_descriptor_table/gate_descriptor.cpp"
"src/context_switching/interrupt_descriptor_table/idt_flags.cpp"
"src/context_switching/interrupt_descriptor_table/interrupt_descriptor_table_pointer.cpp"
diff --git a/arch/x86_64/include/arch/context_switching/syscall/main.hpp b/arch/x86_64/include/arch/context_switching/syscall/main.hpp
new file mode 100644
index 0000000..c75268a
--- /dev/null
+++ b/arch/x86_64/include/arch/context_switching/syscall/main.hpp
@@ -0,0 +1,28 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_MAIN_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_MAIN_HPP
+
+#include "arch/context_switching/syscall/syscall_handler.hpp"
+
+#include <array>
+#include <cstdint>
+#include <optional>
+
+namespace teachos::arch::context_switching::syscall
+{
+ struct arguments
+ {
+ uint64_t arg_0{};
+ uint64_t arg_1{};
+ uint64_t arg_2{};
+ uint64_t arg_3{};
+ uint64_t arg_4{};
+ uint64_t arg_5{};
+ };
+
+ auto enable_syscall() -> void;
+
+ auto syscall(type syscall_number, arguments args = {}) -> uint64_t;
+
+} // namespace teachos::arch::context_switching::syscall
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_MAIN_HPP
diff --git a/arch/x86_64/include/arch/context_switching/syscall/syscall_handler.hpp b/arch/x86_64/include/arch/context_switching/syscall/syscall_handler.hpp
new file mode 100644
index 0000000..e076995
--- /dev/null
+++ b/arch/x86_64/include/arch/context_switching/syscall/syscall_handler.hpp
@@ -0,0 +1,23 @@
+#ifndef TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_HANDLER_HPP
+#define TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_HANDLER_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::context_switching::syscall
+{
+ enum type : uint64_t
+ {
+ WRITE = 1U
+ };
+
+ /**
+ * @brief Handler for SYSCALL instruction. Calls a specific implementation based
+ * on the register RAX.
+ *
+ * @return Returns with SYSRETQ
+ */
+ auto syscall_handler() -> void;
+
+} // namespace teachos::arch::context_switching::syscall
+
+#endif // TEACHOS_ARCH_X86_64_CONTEXT_SWITCHING_SYSCALL_SYSCALL_HANDLER_HPP
diff --git a/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp b/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp
index 866d15d..8091a58 100644
--- a/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp
+++ b/arch/x86_64/include/arch/interrupt_handling/generic_interrupt_handler.hpp
@@ -29,14 +29,6 @@ namespace teachos::arch::interrupt_handling
[[gnu::interrupt]] [[gnu::section(".interrupt_text")]]
auto generic_interrupt_handler(interrupt_frame * frame) -> void;
- /**
- * @brief Interrupt handler function for syscalls (INT 0x80).
- *
- * @param frame Pointer to the interrupt frame containing CPU state.
- */
- [[gnu::interrupt]] [[gnu::section(".interrupt_text")]]
- auto syscall_interrupt_handler(interrupt_frame * frame) -> void;
-
} // namespace teachos::arch::interrupt_handling
#endif // TEACHOS_ARCH_X86_64_INTERRUPT_HANDLING_GENERIC_INTERRUPT_HANDLER_HPP
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 c041198..7aa0859 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
@@ -26,11 +26,6 @@ namespace teachos::arch::context_switching::interrupt_descriptor_table
interrupt_descriptor_table.at(i) = {selector, ist, flags, offset};
}
- interrupt_descriptor_table.at(0x80) = {
- segment_selector{1U, segment_selector::REQUEST_LEVEL_KERNEL}, ist_offset{0U},
- idt_flags{idt_flags::DESCRIPTOR_LEVEL_USER | idt_flags::TRAP_GATE | idt_flags::PRESENT},
- uint64_t{reinterpret_cast<uint64_t>(interrupt_handling::syscall_interrupt_handler)}};
-
return interrupt_descriptor_table;
}
} // namespace
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);
}
diff --git a/arch/x86_64/src/context_switching/syscall/main.cpp b/arch/x86_64/src/context_switching/syscall/main.cpp
new file mode 100644
index 0000000..e90f503
--- /dev/null
+++ b/arch/x86_64/src/context_switching/syscall/main.cpp
@@ -0,0 +1,57 @@
+#include "arch/context_switching/syscall/main.hpp"
+
+#include "arch/context_switching/interrupt_descriptor_table/segment_selector.hpp"
+#include "arch/exception_handling/assert.hpp"
+#include "arch/exception_handling/panic.hpp"
+#include "arch/kernel/cpu/msr.hpp"
+
+#include <cstdint>
+
+namespace teachos::arch::context_switching::syscall
+{
+ namespace
+ {
+ constexpr interrupt_descriptor_table::segment_selector KERNEL_CODE_SEGMENT_SELECTOR{
+ 1U, interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_KERNEL};
+
+ auto constexpr IA32_STAR_ADDRESS = 0xC0000081;
+ auto constexpr IA32_LSTAR_ADDRESS = 0xC0000082;
+ auto constexpr IA32_FMASK_ADDRESS = 0xC0000084;
+
+ } // namespace
+
+ auto enable_syscall() -> 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);
+ }
+
+ auto syscall(type syscall_number, arguments args) -> uint64_t
+ {
+ asm volatile("mov %[input], %%rax"
+ : /* no output from call */
+ : [input] "m"(syscall_number)
+ : "memory");
+
+ asm volatile("mov %[input], %%rdi " : /* no output from call */ : [input] "m"(args.arg_0) : "memory");
+ asm volatile("mov %[input], %%rsi" : /* no output from call */ : [input] "m"(args.arg_1) : "memory");
+ asm volatile("mov %[input], %%rdx" : /* no output from call */ : [input] "m"(args.arg_2) : "memory");
+ asm volatile("mov %[input], %%r10" : /* no output from call */ : [input] "m"(args.arg_3) : "memory");
+ asm volatile("mov %[input], %%r8" : /* no output from call */ : [input] "m"(args.arg_4) : "memory");
+ asm volatile("mov %[input], %%r9" : /* no output from call */ : [input] "m"(args.arg_5) : "memory");
+
+ asm volatile("syscall");
+
+ uint64_t result{};
+ asm volatile("mov %%rax, %[output]" : [output] "=m"(result));
+ return result;
+ }
+
+} // namespace teachos::arch::context_switching::syscall \ No newline at end of file
diff --git a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp
new file mode 100644
index 0000000..f6e1c9e
--- /dev/null
+++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp
@@ -0,0 +1,68 @@
+#include "arch/context_switching/syscall/syscall_handler.hpp"
+
+#include "arch/exception_handling/panic.hpp"
+#include "arch/video/vga/text.hpp"
+
+namespace teachos::arch::context_switching::syscall
+{
+
+ namespace
+ {
+ auto write_to_vga_buffer(uint64_t buffer)
+ {
+ video::vga::text::write(reinterpret_cast<const char *>(buffer),
+ video::vga::text::common_attributes::green_on_black);
+ }
+ } // namespace
+
+ auto syscall_handler() -> void
+ {
+ // Saving state of rcx and r11 because it is required by sysretq to function.
+ // Calls to other functions potentially overwrite these registers, because of
+ // callee saved calling convention.
+ uint64_t return_instruction_pointer, rflags = {};
+ asm volatile("mov %%rcx, %[output]" : [output] "=m"(return_instruction_pointer));
+ asm volatile("mov %%r11, %[output]" : [output] "=m"(rflags));
+
+ uint64_t syscall_number, arg_0, arg_1, arg_2, arg_3, arg_4, arg_5 = {};
+ asm volatile("mov %%rdi, %[output]" : [output] "=m"(arg_0));
+ asm volatile("mov %%rsi, %[output]" : [output] "=m"(arg_1));
+ asm volatile("mov %%rdx, %[output]" : [output] "=m"(arg_2));
+ asm volatile("mov %%r10, %[output]" : [output] "=m"(arg_3));
+ asm volatile("mov %%r8, %[output]" : [output] "=m"(arg_4));
+ asm volatile("mov %%r9, %[output]" : [output] "=m"(arg_5));
+
+ // RAX is read last, because paired with our type enum, we can use it to check
+ // if the register has been written by the compiled code between executing the syscall
+ // and now.
+ asm volatile("mov %%rax, %[output]" : [output] "=m"(syscall_number));
+
+ switch (static_cast<type>(syscall_number))
+ {
+ case WRITE:
+ write_to_vga_buffer(arg_0);
+ break;
+ default:
+ teachos::arch::exception_handling::panic("[Syscall Handler] Invalid syscall number");
+ break;
+ }
+
+ uint64_t result = 0U;
+ asm volatile("mov %[input], %%rax"
+ : /* no output from call */
+ : [input] "m"(result)
+ : "memory");
+
+ asm volatile("mov %[input], %%rcx"
+ : /* no output from call */
+ : [input] "m"(return_instruction_pointer)
+ : "memory");
+ asm volatile("mov %[input], %%r11"
+ : /* no output from call */
+ : [input] "m"(rflags)
+ : "memory");
+
+ asm volatile("sysretq");
+ }
+
+} // namespace teachos::arch::context_switching::syscall \ No newline at end of file
diff --git a/arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp b/arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp
index 60fb50c..8e2c62d 100644
--- a/arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp
+++ b/arch/x86_64/src/interrupt_handling/generic_interrupt_handler.cpp
@@ -9,30 +9,4 @@ namespace teachos::arch::interrupt_handling
(void)frame;
video::vga::text::write("An Interrupt occurred.", video::vga::text::common_attributes::green_on_black);
}
-
- auto syscall_interrupt_handler(interrupt_frame * frame) -> void
- {
- // RDI, RSI, RDX, RCX, R8, R9
- // RDI -> SYSCALL number
- // Others are arguments
-
- // TODO: The registers are not available here. We need to set them up on the stack
- // and access them via argument.
-
- uint64_t syscall_number{};
- asm volatile("mov %%rdi, %0" : "=r"(syscall_number));
-
- // Handle system call based on the number
- switch (syscall_number)
- {
- case 1:
- video::vga::text::write("SYSCALL 1.", video::vga::text::common_attributes::green_on_black);
- break;
- default:
- // Handle unknown syscall
- break;
- }
-
- (void)frame;
- }
} // namespace teachos::arch::interrupt_handling
diff --git a/arch/x86_64/src/kernel/cpu/control_register.cpp b/arch/x86_64/src/kernel/cpu/control_register.cpp
index a39a360..41b8cd7 100644
--- a/arch/x86_64/src/kernel/cpu/control_register.cpp
+++ b/arch/x86_64/src/kernel/cpu/control_register.cpp
@@ -23,9 +23,6 @@ namespace teachos::arch::kernel::cpu
case control_register::CR4:
asm volatile("mov %%cr4, %[output]" : [output] "=r"(current_value));
break;
- default:
- exception_handling::panic("[Control Register] Attempted to read non-existent or reserved control register");
- break;
}
return current_value;
}
@@ -58,9 +55,6 @@ namespace teachos::arch::kernel::cpu
: [input] "r"(new_value)
: "memory");
break;
- default:
- exception_handling::panic("[Control Register] Attempted to write non-existent or reserved control register");
- break;
}
}