From 5a8c9d2f2e4a3d2810f81c35070c6ef0926cfdd1 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 3 May 2025 09:45:45 +0000 Subject: write wrapper function for syscall --- arch/x86_64/src/context_switching/syscall/main.cpp | 57 ++++++++++++++++++ .../context_switching/syscall/syscall_handler.cpp | 68 ++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 arch/x86_64/src/context_switching/syscall/main.cpp create mode 100644 arch/x86_64/src/context_switching/syscall/syscall_handler.cpp (limited to 'arch/x86_64/src/context_switching/syscall') 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 + +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(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(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(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 -- cgit v1.2.3 From 4b4ca98376cb4bdf97fd910b7e2ca78628b0a8ee Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 3 May 2025 09:51:15 +0000 Subject: rename syscall result variable --- arch/x86_64/src/context_switching/syscall/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/context_switching/syscall') diff --git a/arch/x86_64/src/context_switching/syscall/main.cpp b/arch/x86_64/src/context_switching/syscall/main.cpp index e90f503..9ac63ce 100644 --- a/arch/x86_64/src/context_switching/syscall/main.cpp +++ b/arch/x86_64/src/context_switching/syscall/main.cpp @@ -49,9 +49,9 @@ namespace teachos::arch::context_switching::syscall asm volatile("syscall"); - uint64_t result{}; - asm volatile("mov %%rax, %[output]" : [output] "=m"(result)); - return result; + uint64_t error{}; + asm volatile("mov %%rax, %[output]" : [output] "=m"(error)); + return error; } } // namespace teachos::arch::context_switching::syscall \ No newline at end of file -- cgit v1.2.3 From 4d4e23116284f41329ea809e2bda86feea1b325c Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 4 May 2025 10:51:12 +0000 Subject: fix returning from syscall --- arch/x86_64/src/context_switching/syscall/syscall_handler.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/src/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 index f6e1c9e..759a092 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -12,6 +12,7 @@ namespace teachos::arch::context_switching::syscall { video::vga::text::write(reinterpret_cast(buffer), video::vga::text::common_attributes::green_on_black); + video::vga::text::newline(); } } // namespace @@ -62,7 +63,8 @@ namespace teachos::arch::context_switching::syscall : [input] "m"(rflags) : "memory"); - asm volatile("sysretq"); + asm volatile("leave\n" + "sysretq"); } } // namespace teachos::arch::context_switching::syscall \ No newline at end of file -- cgit v1.2.3 From c1dff44858ebdb3cd5a49e84179796e44e7eb91c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 5 May 2025 06:41:31 +0000 Subject: Fix recursive include using extra file --- arch/x86_64/src/context_switching/syscall/main.cpp | 35 ++-------------------- .../context_switching/syscall/syscall_enable.cpp | 32 ++++++++++++++++++++ .../context_switching/syscall/syscall_handler.cpp | 13 +++++--- 3 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 arch/x86_64/src/context_switching/syscall/syscall_enable.cpp (limited to 'arch/x86_64/src/context_switching/syscall') diff --git a/arch/x86_64/src/context_switching/syscall/main.cpp b/arch/x86_64/src/context_switching/syscall/main.cpp index 9ac63ce..a226e23 100644 --- a/arch/x86_64/src/context_switching/syscall/main.cpp +++ b/arch/x86_64/src/context_switching/syscall/main.cpp @@ -1,39 +1,8 @@ #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 - 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(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 + auto syscall(type syscall_number, arguments args) -> error { asm volatile("mov %[input], %%rax" : /* no output from call */ @@ -49,7 +18,7 @@ namespace teachos::arch::context_switching::syscall asm volatile("syscall"); - uint64_t error{}; + error error{}; asm volatile("mov %%rax, %[output]" : [output] "=m"(error)); return error; } 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..e6265d3 --- /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 + { + 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(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 \ 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 index 759a092..fbfecc0 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -1,5 +1,6 @@ #include "arch/context_switching/syscall/syscall_handler.hpp" +#include "arch/context_switching/syscall/main.hpp" #include "arch/exception_handling/panic.hpp" #include "arch/video/vga/text.hpp" @@ -8,11 +9,12 @@ namespace teachos::arch::context_switching::syscall namespace { - auto write_to_vga_buffer(uint64_t buffer) + auto write_to_vga_buffer(uint64_t buffer) -> error { video::vga::text::write(reinterpret_cast(buffer), video::vga::text::common_attributes::green_on_black); video::vga::text::newline(); + return error::OK; } } // namespace @@ -38,17 +40,17 @@ namespace teachos::arch::context_switching::syscall // and now. asm volatile("mov %%rax, %[output]" : [output] "=m"(syscall_number)); + error result = error::OK; switch (static_cast(syscall_number)) { - case WRITE: - write_to_vga_buffer(arg_0); + case type::WRITE: + result = 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) @@ -63,6 +65,9 @@ namespace teachos::arch::context_switching::syscall : [input] "m"(rflags) : "memory"); + // Additionally call leave, because x86 allocates tack 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, becuase the RIP has not been restored from the previous stack frame. asm volatile("leave\n" "sysretq"); } -- cgit v1.2.3 From 27d4fb90ebbc754e98ff68ce5bc7839a44ed99c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 5 May 2025 09:22:28 +0000 Subject: Add comments to syscall components --- arch/x86_64/src/context_switching/syscall/main.cpp | 2 +- arch/x86_64/src/context_switching/syscall/syscall_enable.cpp | 4 ++-- arch/x86_64/src/context_switching/syscall/syscall_handler.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/context_switching/syscall') diff --git a/arch/x86_64/src/context_switching/syscall/main.cpp b/arch/x86_64/src/context_switching/syscall/main.cpp index a226e23..93fc613 100644 --- a/arch/x86_64/src/context_switching/syscall/main.cpp +++ b/arch/x86_64/src/context_switching/syscall/main.cpp @@ -23,4 +23,4 @@ namespace teachos::arch::context_switching::syscall return error; } -} // namespace teachos::arch::context_switching::syscall \ No newline at end of file +} // 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 index e6265d3..3c43336 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_enable.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_enable.cpp @@ -8,7 +8,7 @@ namespace teachos::arch::context_switching::syscall { namespace { - constexpr interrupt_descriptor_table::segment_selector KERNEL_CODE_SEGMENT_SELECTOR{ + 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; @@ -29,4 +29,4 @@ namespace teachos::arch::context_switching::syscall kernel::cpu::set_efer_bit(kernel::cpu::efer_flags::SCE); } -} // namespace teachos::arch::context_switching::syscall \ No newline at end of file +} // 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 index fbfecc0..da9eb1b 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -72,4 +72,4 @@ namespace teachos::arch::context_switching::syscall "sysretq"); } -} // namespace teachos::arch::context_switching::syscall \ No newline at end of file +} // namespace teachos::arch::context_switching::syscall -- cgit v1.2.3 From 833cd6446d9981a262959749c0e248e33b54c174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 11 May 2025 08:48:57 +0000 Subject: Adjust user heap allocator with expanding heap functionality --- arch/x86_64/src/context_switching/syscall/main.cpp | 17 +++++++++++++---- .../src/context_switching/syscall/syscall_handler.cpp | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/context_switching/syscall') diff --git a/arch/x86_64/src/context_switching/syscall/main.cpp b/arch/x86_64/src/context_switching/syscall/main.cpp index 93fc613..996d7fb 100644 --- a/arch/x86_64/src/context_switching/syscall/main.cpp +++ b/arch/x86_64/src/context_switching/syscall/main.cpp @@ -2,7 +2,7 @@ namespace teachos::arch::context_switching::syscall { - auto syscall(type syscall_number, arguments args) -> error + auto syscall(type syscall_number, arguments args) -> response { asm volatile("mov %[input], %%rax" : /* no output from call */ @@ -18,9 +18,18 @@ namespace teachos::arch::context_switching::syscall asm volatile("syscall"); - error error{}; - asm volatile("mov %%rax, %[output]" : [output] "=m"(error)); - return error; + error error_code{}; + asm volatile("mov %%rax, %[output]" : [output] "=m"(error_code)); + + 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)); + + return {error_code, values}; } } // 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 index da9eb1b..b88f273 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -16,6 +16,20 @@ namespace teachos::arch::context_switching::syscall video::vga::text::newline(); return error::OK; } + + auto expand_user_heap() -> error + { + arguments args{}; + 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"); + return error::OUT_OF_MEMORY; + } } // namespace auto syscall_handler() -> void @@ -46,6 +60,9 @@ namespace teachos::arch::context_switching::syscall case type::WRITE: result = write_to_vga_buffer(arg_0); break; + case type::EXPAND_HEAP: + result = expand_user_heap(); + break; default: teachos::arch::exception_handling::panic("[Syscall Handler] Invalid syscall number"); break; -- cgit v1.2.3 From ef156dd6430855434b54275b22cd43ee3cedcfdc Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 11 May 2025 09:29:56 +0000 Subject: make frame_allocator and active_page_table statically available --- arch/x86_64/src/context_switching/syscall/syscall_handler.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'arch/x86_64/src/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 index b88f273..9ca03d9 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -2,6 +2,8 @@ #include "arch/context_switching/syscall/main.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 @@ -19,6 +21,10 @@ namespace teachos::arch::context_switching::syscall auto expand_user_heap() -> error { + // TODO: use actual addresses instead of this constant! + memory::remap_heap(memory::heap::USER_HEAP_SIZE + memory::heap::USER_HEAP_SIZE, memory::heap::USER_HEAP_SIZE, + memory::paging::entry::USER_ACCESSIBLE); + arguments args{}; asm volatile("mov %[input], %%rdi" : /* no output from call */ -- cgit v1.2.3 From ee4c61f7313fedd23d01c69ea5036fa38ef6248a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 12 May 2025 08:50:12 +0000 Subject: Adjust user heap to lazy allocate heap --- arch/x86_64/src/context_switching/syscall/syscall_handler.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'arch/x86_64/src/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 index 9ca03d9..7272e9e 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -21,11 +21,10 @@ namespace teachos::arch::context_switching::syscall auto expand_user_heap() -> error { - // TODO: use actual addresses instead of this constant! - memory::remap_heap(memory::heap::USER_HEAP_SIZE + memory::heap::USER_HEAP_SIZE, memory::heap::USER_HEAP_SIZE, - memory::paging::entry::USER_ACCESSIBLE); + static auto current_heap_end = memory::heap::USER_HEAP_START; + memory::remap_heap(current_heap_end, memory::heap::USER_HEAP_SIZE, memory::paging::entry::USER_ACCESSIBLE); - arguments args{}; + arguments args{current_heap_end, memory::heap::USER_HEAP_SIZE}; asm volatile("mov %[input], %%rdi" : /* no output from call */ : [input] "m"(args.arg_0) @@ -34,7 +33,8 @@ namespace teachos::arch::context_switching::syscall : /* no output from call */ : [input] "m"(args.arg_1) : "memory"); - return error::OUT_OF_MEMORY; + current_heap_end += memory::heap::USER_HEAP_SIZE; + return error::OK; } } // namespace -- cgit v1.2.3 From 29e067867e7a437d12351b481024d4bab431b202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 12 May 2025 13:51:12 +0000 Subject: Fix crashes because of are frame allocator copy --- arch/x86_64/src/context_switching/syscall/main.cpp | 6 +-- .../context_switching/syscall/syscall_handler.cpp | 50 ++++++++++++++-------- 2 files changed, 36 insertions(+), 20 deletions(-) (limited to 'arch/x86_64/src/context_switching/syscall') diff --git a/arch/x86_64/src/context_switching/syscall/main.cpp b/arch/x86_64/src/context_switching/syscall/main.cpp index 996d7fb..e291c10 100644 --- a/arch/x86_64/src/context_switching/syscall/main.cpp +++ b/arch/x86_64/src/context_switching/syscall/main.cpp @@ -18,9 +18,6 @@ namespace teachos::arch::context_switching::syscall asm volatile("syscall"); - error error_code{}; - asm volatile("mov %%rax, %[output]" : [output] "=m"(error_code)); - arguments values{}; asm volatile("mov %%rdi, %[output]" : [output] "=m"(values.arg_0)); asm volatile("mov %%rsi, %[output]" : [output] "=m"(values.arg_1)); @@ -29,6 +26,9 @@ namespace teachos::arch::context_switching::syscall 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}; } diff --git a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp index 7272e9e..9cc6edf 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -11,30 +11,21 @@ namespace teachos::arch::context_switching::syscall namespace { - auto write_to_vga_buffer(uint64_t buffer) -> error + auto write_to_vga_buffer(uint64_t buffer) -> response { video::vga::text::write(reinterpret_cast(buffer), video::vga::text::common_attributes::green_on_black); video::vga::text::newline(); - return error::OK; + return {error::OK}; } - auto expand_user_heap() -> error + auto expand_user_heap() -> response { static auto current_heap_end = memory::heap::USER_HEAP_START; - memory::remap_heap(current_heap_end, memory::heap::USER_HEAP_SIZE, memory::paging::entry::USER_ACCESSIBLE); - - arguments args{current_heap_end, memory::heap::USER_HEAP_SIZE}; - 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"); + 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; + return {error::OK, {heap_start, memory::heap::USER_HEAP_SIZE}}; } } // namespace @@ -60,7 +51,7 @@ namespace teachos::arch::context_switching::syscall // and now. asm volatile("mov %%rax, %[output]" : [output] "=m"(syscall_number)); - error result = error::OK; + response result; switch (static_cast(syscall_number)) { case type::WRITE: @@ -76,7 +67,32 @@ namespace teachos::arch::context_switching::syscall asm volatile("mov %[input], %%rax" : /* no output from call */ - : [input] "m"(result) + : [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" -- cgit v1.2.3 From 8d39f3f67734bf39cada370c39243e6ef33bf4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 18 May 2025 14:45:05 +0000 Subject: Make new usable for both kernel and user calls --- arch/x86_64/src/context_switching/syscall/syscall_handler.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'arch/x86_64/src/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 index 9cc6edf..cd1c8a2 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -60,6 +60,12 @@ namespace teachos::arch::context_switching::syscall case type::EXPAND_HEAP: result = expand_user_heap(); break; + case type::ASSERT: + if (!arg_0) + { + teachos::arch::exception_handling::panic(reinterpret_cast(arg_1)); + } + break; default: teachos::arch::exception_handling::panic("[Syscall Handler] Invalid syscall number"); break; -- cgit v1.2.3 From 8a6a9a3a159ce1b960721eb921b8e8d81b15b718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 20 May 2025 12:29:09 +0000 Subject: Improve syscalls and user heap allocator --- arch/x86_64/src/context_switching/syscall/syscall_handler.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/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 index cd1c8a2..af6d911 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -1,6 +1,7 @@ #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" @@ -61,10 +62,7 @@ namespace teachos::arch::context_switching::syscall result = expand_user_heap(); break; case type::ASSERT: - if (!arg_0) - { - teachos::arch::exception_handling::panic(reinterpret_cast(arg_1)); - } + teachos::arch::exception_handling::assert(arg_0, reinterpret_cast(arg_1)); break; default: teachos::arch::exception_handling::panic("[Syscall Handler] Invalid syscall number"); -- cgit v1.2.3 From 3e597ede8079883b3b9d48faf94b8a7bec2a2118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Tue, 27 May 2025 12:41:50 +0000 Subject: Readd text kernels ection with explanation --- arch/x86_64/src/context_switching/syscall/syscall_handler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/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 index af6d911..84dbe5f 100644 --- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp +++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp @@ -108,9 +108,9 @@ namespace teachos::arch::context_switching::syscall : [input] "m"(rflags) : "memory"); - // Additionally call leave, because x86 allocates tack space for the internal variables. If we do not clean up this + // 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, becuase the RIP has not been restored from the previous stack frame. + // method that originally called it, because the RIP has not been restored from the previous stack frame. asm volatile("leave\n" "sysretq"); } -- cgit v1.2.3