bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH gnumach] Align the user stack correctly for 64 bit programs.


From: Flavio Cruz
Subject: [PATCH gnumach] Align the user stack correctly for 64 bit programs.
Date: Mon, 20 Mar 2023 00:59:56 -0400

* i386/i386/thread.h: Define STACK_ALIGN which is 16-byte for 64 bit
  programs as recommended by the System V AMD64 guidelines.
* i386/i386/pcb.c: Use STACK_ALIGN to align the bootstrap arguments and
  ultimately the stack where the program starts on.
* kern/bootstrap.c: Do not align arg_len here since it will be aligned
  in set_user_regs.
---
 i386/i386/pcb.c    |  6 ++++--
 i386/i386/thread.h | 11 +++++++++++
 kern/bootstrap.c   |  3 +--
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/i386/i386/pcb.c b/i386/i386/pcb.c
index 9210656b..998db673 100644
--- a/i386/i386/pcb.c
+++ b/i386/i386/pcb.c
@@ -893,11 +893,13 @@ set_user_regs(vm_offset_t stack_base, /* low address */
        vm_offset_t     arg_addr;
        struct i386_saved_state *saved_state;
 
-       arg_size = (arg_size + sizeof(int) - 1) & ~(sizeof(int)-1);
+       assert(P2ALIGNED(stack_size, STACK_ALIGN));
+       assert(P2ALIGNED(stack_base, STACK_ALIGN));
+       arg_size = P2ROUND(arg_size, STACK_ALIGN);
        arg_addr = stack_base + stack_size - arg_size;
 
        saved_state = USER_REGS(current_thread());
-       saved_state->uesp = (long)arg_addr;
+       saved_state->uesp = (rpc_vm_offset_t)arg_addr;
        saved_state->eip = exec_info->entry;
 
        return (arg_addr);
diff --git a/i386/i386/thread.h b/i386/i386/thread.h
index cb317bee..c5da7522 100644
--- a/i386/i386/thread.h
+++ b/i386/i386/thread.h
@@ -225,6 +225,17 @@ typedef struct pcb {
 #define STACK_IEL(stack)       \
        ((struct i386_exception_link *)STACK_IKS(stack) - 1)
 
+#ifdef __x86_64__
+#ifdef USER32
+#define STACK_ALIGN 4
+#else
+/* Follow System V AMD64 ABI guidelines. */
+#define STACK_ALIGN 16
+#endif
+#else
+#define STACK_ALIGN 4
+#endif  /* __x86_64__ */
+
 #define USER_REGS(thread)      (&(thread)->pcb->iss)
 
 
diff --git a/kern/bootstrap.c b/kern/bootstrap.c
index 8f66a4b5..49358ac6 100644
--- a/kern/bootstrap.c
+++ b/kern/bootstrap.c
@@ -610,17 +610,16 @@ build_args_and_stack(struct exec_info *boot_exec_info,
         *      trailing 0 pointer
         *      pointers to environment variables
         *      trailing 0 pointer
-        *      and align to integer boundary
         */
        arg_len += (sizeof(rpc_vm_offset_t)
                    + (arg_count + 1 + envc + 1) * sizeof(rpc_vm_offset_t));
-       arg_len = (arg_len + sizeof(integer_t) - 1) & ~(sizeof(integer_t)-1);
 
        /*
         * Allocate the stack.
         */
        stack_size = round_page(STACK_SIZE);
        stack_base = user_stack_low(stack_size);
+
        (void) vm_allocate(current_task()->map,
                        &stack_base,
                        stack_size,
-- 
2.39.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]