From 494b615dc21125bd6757d7a6084595fd4e29ae51 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Tue, 31 Mar 2026 20:06:25 +0200 Subject: [PATCH] Fix x64 ABI compliance by aligning stack and reserving shadow space --- xtoskrnl/ar/amd64/archsup.S | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/xtoskrnl/ar/amd64/archsup.S b/xtoskrnl/ar/amd64/archsup.S index 557a7a2..9a62cf9 100644 --- a/xtoskrnl/ar/amd64/archsup.S +++ b/xtoskrnl/ar/amd64/archsup.S @@ -115,22 +115,26 @@ Ar\Type\Vector: mov %cs, %ax and $3, %al mov %al, TrapPreviousMode(%rbp) - jz KernelMode\Type\Vector - swapgs - jmp UserMode\Type\Vector -KernelMode\Type\Vector: - /* Save kernel stack pointer (SS:RSP) */ - movl %ss, %eax - mov %eax, TrapSegSs(%rbp) - lea TRAP_FRAME_SIZE(%rbp), %rax - mov %rax, TrapRsp(%rbp) + /* Skip swapgs as the interrupt originated from kernel mode */ + jz UserMode\Type\Vector + + swapgs UserMode\Type\Vector: - /* Push Frame Pointer and clear direction flag */ + /* Set up trap frame pointer for the dispatcher and clear the direction flag */ mov %rsp, %rcx cld + /* Preserve the original stack pointer */ + mov %rsp, %rbx + + /* Force stack alignment */ + and $-16, %rsp + + /* Allocate 32 bytes of shadow space */ + sub $32, %rsp + .ifc \Type,Trap /* Pass to the trap dispatcher */ call ArDispatchTrap @@ -139,6 +143,9 @@ UserMode\Type\Vector: call ArDispatchTrap .endif + /* Restore the original trap frame stack pointer */ + mov %rbx, %rsp + /* Test previous mode and swapgs if needed */ testb $1, TrapPreviousMode(%rbp) jz KernelModeReturn\Type\Vector