From 4b8674bee6089aef1e2c6b9064c6109f1cd392da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 3 May 2025 07:24:55 +0000 Subject: Remove zomby code and fix 32-bit compability crash --- arch/x86_64/src/context_switching/main.cpp | 38 +++++++----------------------- arch/x86_64/src/kernel/cpu/call.cpp | 1 - 2 files changed, 8 insertions(+), 31 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/src/context_switching/main.cpp b/arch/x86_64/src/context_switching/main.cpp index 7449d84..06c0810 100644 --- a/arch/x86_64/src/context_switching/main.cpp +++ b/arch/x86_64/src/context_switching/main.cpp @@ -20,8 +20,6 @@ namespace teachos::arch::context_switching constexpr interrupt_descriptor_table::segment_selector KERNEL_CODE_SEGMENT_SELECTOR{ 1U, interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_KERNEL}; - constexpr interrupt_descriptor_table::segment_selector KERNEL_DATA_SEGMENT_SELECTOR{ - 2U, interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_KERNEL}; constexpr kernel::cpu::far_pointer KERNEL_CODE_POINTER{&kernel::cpu::reload_data_segment_registers, KERNEL_CODE_SEGMENT_SELECTOR}; constexpr context_switching::interrupt_descriptor_table::segment_selector USER_CODE_SEGMENT_SELECTOR{ @@ -29,19 +27,7 @@ namespace teachos::arch::context_switching constexpr context_switching::interrupt_descriptor_table::segment_selector USER_DATA_SEGMENT_SELECTOR{ 4U, context_switching::interrupt_descriptor_table::segment_selector::REQUEST_LEVEL_USER}; - auto reload_gdtr() -> void - { - /*asm volatile("pushq $0x8\n\t" // Push new CS - "lea 1f(%%rip), %%rax\n\t" // Get address of label 1 into RAX - "pushq %%rax\n\t" // Push return address - "lretq\n" // Far return (loads CS:RIP) - "1:\n\t" // Label to return to - : - : - : "rax", "memory");*/ - - kernel::cpu::call(KERNEL_CODE_POINTER); - } + auto reload_gdtr() -> void { kernel::cpu::call(KERNEL_CODE_POINTER); } auto user_mode_main() -> void { @@ -64,14 +50,11 @@ namespace teachos::arch::context_switching : "memory"); asm volatile("syscall"); - // TODO: Reading RAX value does not work because the read itself changes the RAX value?! - // asm volatile("mov %%rax, %[output]" : [output] "=r"(syscall_value)); - - // TODO: Causes a general protection fault after the sysreturn? - // If removed instead it will cause a general protection fault after leaving this main method to return to kernel - // mode. But CS and SS are still configured for User mode. - /*video::vga::text::write("Successfully made a SYSCALL and returned with SYSRETQ!", - video::vga::text::common_attributes::green_on_black);*/ + // 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 @@ -103,11 +86,7 @@ namespace teachos::arch::context_switching : [input] "r"(result) : "memory"); - // Use SYSRET instead of SYSRETQ, because the latter would add 0x10 to bits [48:63] of IA32_STAR_ADDRESS for the - // Code Segment. But only add 0x8 to bits [48:63] of IA32_STAR_ADDRESS for the Stack Segment, which means either - // the Stack Segment or Code Segment is wrong. Whereas the former does not add 0x10 for the Code Segment and - // therefore fixes the aformentioned issue. - asm volatile("sysret"); + asm volatile("sysretq"); } auto enable_systemcall() -> void @@ -117,8 +96,7 @@ namespace teachos::arch::context_switching kernel::cpu::write_msr(IA32_FMASK_ADDRESS, 0U); uint64_t const kernel_cs = KERNEL_CODE_SEGMENT_SELECTOR; - uint64_t const user_cs = USER_CODE_SEGMENT_SELECTOR; - uint64_t const star_value = (kernel_cs << 32) | (user_cs << 48); + 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); diff --git a/arch/x86_64/src/kernel/cpu/call.cpp b/arch/x86_64/src/kernel/cpu/call.cpp index 6564b76..98fa248 100644 --- a/arch/x86_64/src/kernel/cpu/call.cpp +++ b/arch/x86_64/src/kernel/cpu/call.cpp @@ -5,6 +5,5 @@ namespace teachos::arch::kernel::cpu auto call(far_pointer pointer) -> void { asm volatile("rex64 lcall *%[input]" : /* no output from call */ : [input] "m"(pointer)); - // asm volatile("ljmp *%0" : : "m"(pointer)); } } // namespace teachos::arch::kernel::cpu -- cgit v1.2.3