From 16e47d6c8bab04848471971dcda6c68714806c12 Mon Sep 17 00:00:00 2001 From: p-szafonimateusz Date: Wed, 7 Feb 2024 15:00:42 +0100 Subject: [PATCH] arch/intel64: fix stack alignment The stack pointer must be aligned to 16 bytes, otherwise the system crash on the first unaligned data access with vector instruction. The problem is only observable with optimization enabled, when vector instructions are generated. Signed-off-by: p-szafonimateusz Signed-off-by: Bowen Wang --- arch/x86_64/src/intel64/intel64_createstack.c | 5 +++-- arch/x86_64/src/intel64/intel64_stackframe.c | 2 +- arch/x86_64/src/intel64/intel64_usestack.c | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/x86_64/src/intel64/intel64_createstack.c b/arch/x86_64/src/intel64/intel64_createstack.c index f95086f5ff..f1c05da1fd 100644 --- a/arch/x86_64/src/intel64/intel64_createstack.c +++ b/arch/x86_64/src/intel64/intel64_createstack.c @@ -192,8 +192,9 @@ int up_create_stack(struct tcb_s *tcb, size_t stack_size, uint8_t ttype) /* The intel64 stack must be aligned at word (16 byte) boundaries. If * necessary top_of_stack must be rounded down to the next boundary. - * We intentionally align at 8 byte boundary, because at task_start, - * only frame pointer will be pushed, not instruction pointer. + * We intentionally align at 8 byte boundary (look at up_stack_frame()) + * , because at task_start, only frame pointer will be pushed, not + * instruction pointer. */ top_of_stack &= ~0x0f; diff --git a/arch/x86_64/src/intel64/intel64_stackframe.c b/arch/x86_64/src/intel64/intel64_stackframe.c index 2381aeef2f..a0c6909799 100644 --- a/arch/x86_64/src/intel64/intel64_stackframe.c +++ b/arch/x86_64/src/intel64/intel64_stackframe.c @@ -90,7 +90,7 @@ void *up_stack_frame(struct tcb_s *tcb, size_t frame_size) /* Save the adjusted stack values in the struct tcb_s */ tcb->stack_base_ptr = (uint8_t *)tcb->stack_base_ptr + frame_size; - tcb->adj_stack_size -= frame_size; + tcb->adj_stack_size -= frame_size + 8; /* And return the pointer to the allocated region */ diff --git a/arch/x86_64/src/intel64/intel64_usestack.c b/arch/x86_64/src/intel64/intel64_usestack.c index d05d89d1fd..386d7c77cd 100644 --- a/arch/x86_64/src/intel64/intel64_usestack.c +++ b/arch/x86_64/src/intel64/intel64_usestack.c @@ -110,8 +110,9 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size) /* The intel64 stack must be aligned at word (16 byte) boundaries. If * necessary top_of_stack must be rounded down to the next boundary. - * We intentionally align at 8 byte boundary, because at task_start, only - * frame pointer will be pushed, not instruction pointer. + * We intentionally align at 8 byte boundary (look at up_stack_frame()), + * because at task_start, only frame pointer will be pushed, not + * instruction pointer. */ top_of_stack &= ~0x0f;