You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2024/02/23 15:05:01 UTC

(nuttx) 02/04: arch/intel64: fix stack alignment

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

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

commit 16e47d6c8bab04848471971dcda6c68714806c12
Author: p-szafonimateusz <p-...@xiaomi.com>
AuthorDate: Wed Feb 7 15:00:42 2024 +0100

    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 <p-...@xiaomi.com>
    Signed-off-by: Bowen Wang <wa...@xiaomi.com>
---
 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;