aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/context_switching/syscall
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/src/context_switching/syscall')
-rw-r--r--arch/x86_64/src/context_switching/syscall/main.cpp35
-rw-r--r--arch/x86_64/src/context_switching/syscall/syscall_enable.cpp32
-rw-r--r--arch/x86_64/src/context_switching/syscall/syscall_handler.cpp118
3 files changed, 185 insertions, 0 deletions
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..e291c10
--- /dev/null
+++ b/arch/x86_64/src/context_switching/syscall/main.cpp
@@ -0,0 +1,35 @@
+#include "arch/context_switching/syscall/main.hpp"
+
+namespace teachos::arch::context_switching::syscall
+{
+ auto syscall(type syscall_number, arguments args) -> response
+ {
+ 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");
+
+ arguments values{};
+ asm volatile("mov %%rdi, %[output]" : [output] "=m"(values.arg_0));
+ asm volatile("mov %%rsi, %[output]" : [output] "=m"(values.arg_1));
+ asm volatile("mov %%rdx, %[output]" : [output] "=m"(values.arg_2));
+ asm volatile("mov %%r10, %[output]" : [output] "=m"(values.arg_3));
+ asm volatile("mov %%r8, %[output]" : [output] "=m"(values.arg_4));
+ asm volatile("mov %%r9, %[output]" : [output] "=m"(values.arg_5));
+
+ error error_code{};
+ asm volatile("mov %%rax, %[output]" : [output] "=m"(error_code));
+
+ return {error_code, values};
+ }
+
+} // namespace teachos::arch::context_switching::syscall
diff --git a/arch/x86_64/src/context_switching/syscall/syscall_enable.cpp b/arch/x86_64/src/context_switching/syscall/syscall_enable.cpp
new file mode 100644
index 0000000..3c43336
--- /dev/null
+++ b/arch/x86_64/src/context_switching/syscall/syscall_enable.cpp
@@ -0,0 +1,32 @@
+#include "arch/context_switching/syscall/syscall_enable.hpp"
+
+#include "arch/context_switching/interrupt_descriptor_table/segment_selector.hpp"
+#include "arch/context_switching/syscall/syscall_handler.hpp"
+#include "arch/kernel/cpu/msr.hpp"
+
+namespace teachos::arch::context_switching::syscall
+{
+ namespace
+ {
+ interrupt_descriptor_table::segment_selector constexpr 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);
+ }
+} // namespace teachos::arch::context_switching::syscall
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..84dbe5f
--- /dev/null
+++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp
@@ -0,0 +1,118 @@
+#include "arch/context_switching/syscall/syscall_handler.hpp"
+
+#include "arch/context_switching/syscall/main.hpp"
+#include "arch/exception_handling/assert.hpp"
+#include "arch/exception_handling/panic.hpp"
+#include "arch/memory/heap/global_heap_allocator.hpp"
+#include "arch/memory/main.hpp"
+#include "arch/video/vga/text.hpp"
+
+namespace teachos::arch::context_switching::syscall
+{
+
+ namespace
+ {
+ auto write_to_vga_buffer(uint64_t buffer) -> response
+ {
+ video::vga::text::write(reinterpret_cast<const char *>(buffer),
+ video::vga::text::common_attributes::green_on_black);
+ video::vga::text::newline();
+ return {error::OK};
+ }
+
+ auto expand_user_heap() -> response
+ {
+ static auto current_heap_end = memory::heap::USER_HEAP_START;
+ uint64_t const heap_start = current_heap_end;
+ memory::remap_heap(heap_start, memory::heap::USER_HEAP_SIZE, memory::paging::entry::USER_ACCESSIBLE);
+ current_heap_end += memory::heap::USER_HEAP_SIZE;
+ return {error::OK, {heap_start, memory::heap::USER_HEAP_SIZE}};
+ }
+ } // 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));
+
+ response result;
+ switch (static_cast<type>(syscall_number))
+ {
+ case type::WRITE:
+ result = write_to_vga_buffer(arg_0);
+ break;
+ case type::EXPAND_HEAP:
+ result = expand_user_heap();
+ break;
+ case type::ASSERT:
+ teachos::arch::exception_handling::assert(arg_0, reinterpret_cast<const char *>(arg_1));
+ break;
+ default:
+ teachos::arch::exception_handling::panic("[Syscall Handler] Invalid syscall number");
+ break;
+ }
+
+ asm volatile("mov %[input], %%rax"
+ : /* no output from call */
+ : [input] "m"(result.error_code)
+ : "memory");
+
+ asm volatile("mov %[input], %%rdi"
+ : /* no output from call */
+ : [input] "m"(result.values.arg_0)
+ : "memory");
+ asm volatile("mov %[input], %%rsi"
+ : /* no output from call */
+ : [input] "m"(result.values.arg_1)
+ : "memory");
+ asm volatile("mov %[input], %%rdx"
+ : /* no output from call */
+ : [input] "m"(result.values.arg_2)
+ : "memory");
+ asm volatile("mov %[input], %%r10"
+ : /* no output from call */
+ : [input] "m"(result.values.arg_3)
+ : "memory");
+ asm volatile("mov %[input], %%r8"
+ : /* no output from call */
+ : [input] "m"(result.values.arg_4)
+ : "memory");
+ asm volatile("mov %[input], %%r9"
+ : /* no output from call */
+ : [input] "m"(result.values.arg_5)
+ : "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");
+
+ // Additionally call leave, because x86 allocates stack space for the internal variables. If we do not clean up this
+ // newly created stack frame the syscall instruction that landed in this syscall_handler, will never return to the
+ // method that originally called it, because the RIP has not been restored from the previous stack frame.
+ asm volatile("leave\n"
+ "sysretq");
+ }
+
+} // namespace teachos::arch::context_switching::syscall