This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 4405b121558b5e3660d87ca53d7a379f1aa3345c
Author: raiden00pl <[email protected]>
AuthorDate: Mon Aug 10 12:19:27 2026 +0200

    arch/x86_64: restore the kernel stack when a signal handler returns
    
    SYS_signal_handler_return restored RSP from saved_rsp, which is not
    written when a task signals itself: synchronous dispatch skips
    up_schedule_sigaction(), so the kernel stack pointer was set to zero
    and the next push faulted. Save the kernel stack pointer at dispatch
    in xcp.kstkptr, as risc-v does, and restore that.
    
    Signed-off-by: raiden00pl <[email protected]>
    Assisted-by: Claude Code
---
 arch/x86_64/src/common/x86_64_syscall.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/arch/x86_64/src/common/x86_64_syscall.c 
b/arch/x86_64/src/common/x86_64_syscall.c
index 196453f5d6a..b9111b3033a 100644
--- a/arch/x86_64/src/common/x86_64_syscall.c
+++ b/arch/x86_64/src/common/x86_64_syscall.c
@@ -247,6 +247,12 @@ uint64_t *x86_64_syscall(uint64_t *regs)
             {
               uint64_t usp;
 
+              /* Save the kernel stack pointer to restore on handler
+               * return
+               */
+
+              rtcb->xcp.kstkptr = (uintptr_t *)regs[REG_RSP];
+
               /* Copy "info" into user stack */
 
               usp = rtcb->xcp.saved_ursp - 8;
@@ -290,12 +296,21 @@ uint64_t *x86_64_syscall(uint64_t *regs)
           DEBUGASSERT(rtcb->xcp.sigreturn != 0);
 
           regs[REG_RCX]       = rtcb->xcp.sigreturn;
-          regs[REG_RSP]       = rtcb->xcp.saved_rsp;
           rtcb->xcp.sigreturn = 0;
 
-          /* For kernel mode, we should be already on a correct kernel stack
-           * which was recovered in x86_64_syscall_entry.
-           */
+#ifdef CONFIG_ARCH_KERNEL_STACK
+          if (rtcb->xcp.kstack != NULL)
+            {
+              /* Return to the kernel stack saved at dispatch */
+
+              regs[REG_RSP]     = (uint64_t)rtcb->xcp.kstkptr;
+              rtcb->xcp.kstkptr = rtcb->xcp.ktopstk;
+            }
+          else
+#endif
+            {
+              regs[REG_RSP] = rtcb->xcp.saved_rsp;
+            }
 
           break;
         }

Reply via email to