aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/context_switching
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/src/context_switching')
-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
4 files changed, 133 insertions, 68 deletions
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