From 061012a2683e56a05e8e443beda6d633964a02f9 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 18 Apr 2024 13:32:40 +0300 Subject: [PATCH] Kernel: Fix signal trampoline 32 bit did not even support it and 64 bit did not align stack --- kernel/arch/i686/Signal.S | 10 +++++++--- kernel/arch/x86_64/Signal.S | 10 ++++++++-- kernel/include/kernel/Thread.h | 2 +- kernel/kernel/Thread.cpp | 2 ++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/kernel/arch/i686/Signal.S b/kernel/arch/i686/Signal.S index c22d1c4ce1..633f01bc32 100644 --- a/kernel/arch/i686/Signal.S +++ b/kernel/arch/i686/Signal.S @@ -7,22 +7,26 @@ .global signal_trampoline signal_trampoline: - ud2 - pushl %ebp movl %esp, %ebp - subl $8, %esp pusha movl 40(%esp), %edi movl 36(%esp), %eax + // align stack to 16 bytes + movl %esp, %ebx + andl $0x0F, %ebx + subl %ebx, %esp + subl $12, %esp pushl %edi call *%eax addl $16, %esp + // restore stack + addl %ebx, %esp popa leave diff --git a/kernel/arch/x86_64/Signal.S b/kernel/arch/x86_64/Signal.S index afa7095af0..5cf227605a 100644 --- a/kernel/arch/x86_64/Signal.S +++ b/kernel/arch/x86_64/Signal.S @@ -23,12 +23,18 @@ signal_trampoline: pushq %r14 pushq %r15 - // This is 16 byte aligned - movq 128(%rsp), %rdi movq 120(%rsp), %rax + + // align stack to 16 bytes + movq %rsp, %rbx + andq $0x0F, %rbx + subq %rbx, %rsp + call *%rax + // restore stack + addq %rbx, %rsp popq %r15 popq %r14 popq %r13 diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index 8e79d2c273..9da9222211 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -94,7 +94,7 @@ namespace Kernel private: static constexpr size_t m_kernel_stack_size = PAGE_SIZE * 64; - static constexpr size_t m_userspace_stack_size = PAGE_SIZE * 4; + static constexpr size_t m_userspace_stack_size = PAGE_SIZE * 64; BAN::UniqPtr m_kernel_stack; BAN::UniqPtr m_userspace_stack; const pid_t m_tid { 0 }; diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index 443531edff..d98a13b60d 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -335,7 +335,9 @@ namespace Kernel else if (signal_handler != (vaddr_t)SIG_DFL) { // call userspace signal handlers +#if ARCH(x86_64) interrupt_stack.sp -= 128; // skip possible red-zone +#endif write_to_stack(interrupt_stack.sp, interrupt_stack.ip); write_to_stack(interrupt_stack.sp, signal); write_to_stack(interrupt_stack.sp, signal_handler);