You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gu...@apache.org on 2021/04/30 19:58:06 UTC

[incubator-nuttx] branch releases/10.1 updated: arch/risc-v: Fix stack alignment according to calling convention

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

gustavonihei pushed a commit to branch releases/10.1
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/releases/10.1 by this push:
     new 112d709  arch/risc-v: Fix stack alignment according to calling convention
112d709 is described below

commit 112d709609e4916b21e435152679cd0b5f3177fb
Author: Alin Jerpelea <al...@sony.com>
AuthorDate: Thu Apr 29 12:39:37 2021 +0200

    arch/risc-v: Fix stack alignment according to calling convention
    
    The RISC-V Integer Calling Convention states that the stack pointer
    shall always be aligned to a 128-bit boundary upon procedure entry, both
    for RV32* and RV64* ISAs (exception to the RV32E ISA, which must follow a
    specific convention)
    
    cherrypick from
    https://github.com/apache/incubator-nuttx/pull/3632/commits/f0696f27bcf1e1d6be939c7ba46b78c3832e1f72
    
    Signed-off-by: Alin Jerpelea <al...@sony.com>
---
 arch/risc-v/src/bl602/bl602_head.S          |  8 ++++----
 arch/risc-v/src/c906/c906_head.S            |  8 ++++----
 arch/risc-v/src/common/riscv_createstack.c  | 15 ++++-----------
 arch/risc-v/src/common/riscv_stackframe.c   | 13 +++----------
 arch/risc-v/src/common/riscv_usestack.c     | 15 ++++-----------
 arch/risc-v/src/esp32c3/esp32c3_interrupt.S |  6 +++---
 arch/risc-v/src/fe310/fe310_head.S          |  8 ++++----
 arch/risc-v/src/k210/k210_head.S            | 12 ++++++------
 arch/risc-v/src/litex/litex_head.S          |  8 ++++----
 arch/risc-v/src/rv32im/riscv_vfork.c        |  4 ----
 10 files changed, 36 insertions(+), 61 deletions(-)

diff --git a/arch/risc-v/src/bl602/bl602_head.S b/arch/risc-v/src/bl602/bl602_head.S
index c43a946..4a76f7d 100644
--- a/arch/risc-v/src/bl602/bl602_head.S
+++ b/arch/risc-v/src/bl602/bl602_head.S
@@ -150,17 +150,17 @@ exception_common:
  *  Name: g_intstackalloc and g_intstackbase
  ************************************************************************************/
 
-#if CONFIG_ARCH_INTERRUPTSTACK > 3
+#if CONFIG_ARCH_INTERRUPTSTACK > 15
   .bss
-  .align  4
+  .balign 16
   .global g_intstackalloc
   .global g_intstackbase
   .type   g_intstackalloc, object
   .type   g_intstackbase, object
 g_intstackalloc:
-  .skip  ((CONFIG_ARCH_INTERRUPTSTACK & ~3))
+  .skip  ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
 g_intstackbase:
   .skip  4
   .size  g_intstackbase, 4
-  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~3)
+  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
 #endif
diff --git a/arch/risc-v/src/c906/c906_head.S b/arch/risc-v/src/c906/c906_head.S
index 7880e35..2b495d9 100644
--- a/arch/risc-v/src/c906/c906_head.S
+++ b/arch/risc-v/src/c906/c906_head.S
@@ -264,17 +264,17 @@ exception_common:
  *  Name: g_intstackalloc and g_intstackbase
  ************************************************************************************/
 
-#if CONFIG_ARCH_INTERRUPTSTACK > 7
+#if CONFIG_ARCH_INTERRUPTSTACK > 15
   .bss
-  .align  8
+  .balign 16
   .global g_intstackalloc
   .global g_intstackbase
   .type   g_intstackalloc, object
   .type   g_intstackbase, object
 g_intstackalloc:
-  .skip  (((CONFIG_ARCH_INTERRUPTSTACK * 2) & ~7))
+  .skip  ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
 g_intstackbase:
   .skip  8
   .size  g_intstackbase, 8
-  .size  g_intstackalloc, ((CONFIG_ARCH_INTERRUPTSTACK * 2) & ~7)
+  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
 #endif
diff --git a/arch/risc-v/src/common/riscv_createstack.c b/arch/risc-v/src/common/riscv_createstack.c
index bb96d66..fa5d0fa 100644
--- a/arch/risc-v/src/common/riscv_createstack.c
+++ b/arch/risc-v/src/common/riscv_createstack.c
@@ -42,20 +42,13 @@
  * Pre-processor Macros
  ****************************************************************************/
 
-/* RISC-V requires at least a 4-byte stack alignment.
- * For floating point use, however, the stack must be aligned to 8-byte
- * addresses.
- */
-
-#if defined(CONFIG_LIBC_FLOATINGPOINT) || defined (CONFIG_ARCH_RV64GC)
-#  define STACK_ALIGNMENT   8
-#else
-#  define STACK_ALIGNMENT   4
-#endif
+/* RISC-V requires a 16-byte stack alignment. */
+
+#define STACK_ALIGNMENT     16
 
 /* Stack alignment macros */
 
-#define STACK_ALIGN_MASK    (STACK_ALIGNMENT-1)
+#define STACK_ALIGN_MASK    (STACK_ALIGNMENT - 1)
 #define STACK_ALIGN_DOWN(a) ((a) & ~STACK_ALIGN_MASK)
 #define STACK_ALIGN_UP(a)   (((a) + STACK_ALIGN_MASK) & ~STACK_ALIGN_MASK)
 
diff --git a/arch/risc-v/src/common/riscv_stackframe.c b/arch/risc-v/src/common/riscv_stackframe.c
index 193adcb..906a638 100644
--- a/arch/risc-v/src/common/riscv_stackframe.c
+++ b/arch/risc-v/src/common/riscv_stackframe.c
@@ -37,20 +37,13 @@
  * Pre-processor Macros
  ****************************************************************************/
 
-/* RISC-V requires at least a 4-byte stack alignment.
- * For floating point use, however, the stack must be aligned to 8-byte
- * addresses.
- */
+/* RISC-V requires a 16-byte stack alignment. */
 
-#if defined(CONFIG_LIBC_FLOATINGPOINT) || defined (CONFIG_ARCH_RV64GC)
-#  define STACK_ALIGNMENT   8
-#else
-#  define STACK_ALIGNMENT   4
-#endif
+#define STACK_ALIGNMENT     16
 
 /* Stack alignment macros */
 
-#define STACK_ALIGN_MASK    (STACK_ALIGNMENT-1)
+#define STACK_ALIGN_MASK    (STACK_ALIGNMENT - 1)
 #define STACK_ALIGN_DOWN(a) ((a) & ~STACK_ALIGN_MASK)
 #define STACK_ALIGN_UP(a)   (((a) + STACK_ALIGN_MASK) & ~STACK_ALIGN_MASK)
 
diff --git a/arch/risc-v/src/common/riscv_usestack.c b/arch/risc-v/src/common/riscv_usestack.c
index ba294cc..fa02315 100644
--- a/arch/risc-v/src/common/riscv_usestack.c
+++ b/arch/risc-v/src/common/riscv_usestack.c
@@ -39,20 +39,13 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* RISC-V requires at least a 4-byte stack alignment.
- * For floating point use, however, the stack must be aligned to 8-byte
- * addresses.
- */
-
-#if defined(CONFIG_LIBC_FLOATINGPOINT) || defined (CONFIG_ARCH_RV64GC)
-#  define STACK_ALIGNMENT   8
-#else
-#  define STACK_ALIGNMENT   4
-#endif
+/* RISC-V requires a 16-byte stack alignment. */
+
+#define STACK_ALIGNMENT     16
 
 /* Stack alignment macros */
 
-#define STACK_ALIGN_MASK    (STACK_ALIGNMENT-1)
+#define STACK_ALIGN_MASK    (STACK_ALIGNMENT - 1)
 #define STACK_ALIGN_DOWN(a) ((a) & ~STACK_ALIGN_MASK)
 #define STACK_ALIGN_UP(a)   (((a) + STACK_ALIGN_MASK) & ~STACK_ALIGN_MASK)
 
diff --git a/arch/risc-v/src/esp32c3/esp32c3_interrupt.S b/arch/risc-v/src/esp32c3/esp32c3_interrupt.S
index 957f2de..db374af 100644
--- a/arch/risc-v/src/esp32c3/esp32c3_interrupt.S
+++ b/arch/risc-v/src/esp32c3/esp32c3_interrupt.S
@@ -43,13 +43,13 @@
   .section  .noinit
 
 #if CONFIG_ARCH_INTERRUPTSTACK > 15
-  .align    4
+  .balign   16
   .type     g_intstackalloc, @object
   .type     g_intstackbase,  @object
 g_intstackalloc:
-  .skip     CONFIG_ARCH_INTERRUPTSTACK
+  .skip     ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
 g_intstackbase:
-  .size     g_intstackalloc, CONFIG_ARCH_INTERRUPTSTACK
+  .size     g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
 #endif
 
 /****************************************************************************
diff --git a/arch/risc-v/src/fe310/fe310_head.S b/arch/risc-v/src/fe310/fe310_head.S
index b3500d0..7b72575 100644
--- a/arch/risc-v/src/fe310/fe310_head.S
+++ b/arch/risc-v/src/fe310/fe310_head.S
@@ -188,17 +188,17 @@ exception_common:
  *  Name: g_intstackalloc and g_intstackbase
  ************************************************************************************/
 
-#if CONFIG_ARCH_INTERRUPTSTACK > 3
+#if CONFIG_ARCH_INTERRUPTSTACK > 15
   .bss
-  .align  4
+  .balign 16
   .global g_intstackalloc
   .global g_intstackbase
   .type   g_intstackalloc, object
   .type   g_intstackbase, object
 g_intstackalloc:
-  .skip  ((CONFIG_ARCH_INTERRUPTSTACK & ~3))
+  .skip  ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
 g_intstackbase:
   .skip  4
   .size  g_intstackbase, 4
-  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~3)
+  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
 #endif
diff --git a/arch/risc-v/src/k210/k210_head.S b/arch/risc-v/src/k210/k210_head.S
index aa5f06a..b7cced3 100644
--- a/arch/risc-v/src/k210/k210_head.S
+++ b/arch/risc-v/src/k210/k210_head.S
@@ -215,25 +215,25 @@ normal_irq:
  *  Name: g_intstackalloc and g_intstackbase
  ************************************************************************************/
 
-#if CONFIG_ARCH_INTERRUPTSTACK > 7
+#if CONFIG_ARCH_INTERRUPTSTACK > 15
   .bss
-  .align  8
+  .balign 16
   .global g_intstackalloc
   .global g_intstackbase
   .type   g_intstackalloc, object
   .type   g_intstackbase, object
 g_intstackalloc:
 #ifndef CONFIG_SMP
-  .skip  ((CONFIG_ARCH_INTERRUPTSTACK + 4) & ~7)
+  .skip  ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
 #else
-  .skip  (((CONFIG_ARCH_INTERRUPTSTACK * CONFIG_SMP_NCPUS) + 4) & ~7)
+  .skip  (((CONFIG_ARCH_INTERRUPTSTACK * CONFIG_SMP_NCPUS) + 8) & ~15)
 #endif
 g_intstackbase:
   .skip  8
   .size  g_intstackbase, 8
 #ifndef CONFIG_SMP
-  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~7)
+  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
 #else
-  .size  g_intstackalloc, ((CONFIG_ARCH_INTERRUPTSTACK * CONFIG_SMP_NCPUS) & ~7)
+  .size  g_intstackalloc, ((CONFIG_ARCH_INTERRUPTSTACK * CONFIG_SMP_NCPUS) & ~15)
 #endif
 #endif
diff --git a/arch/risc-v/src/litex/litex_head.S b/arch/risc-v/src/litex/litex_head.S
index bcfb0f8..0e58cf3 100644
--- a/arch/risc-v/src/litex/litex_head.S
+++ b/arch/risc-v/src/litex/litex_head.S
@@ -188,17 +188,17 @@ exception_common:
  *  Name: g_intstackalloc and g_intstackbase
  ************************************************************************************/
 
-#if CONFIG_ARCH_INTERRUPTSTACK > 3
+#if CONFIG_ARCH_INTERRUPTSTACK > 15
   .bss
-  .align  4
+  .balign 16
   .global g_intstackalloc
   .global g_intstackbase
   .type   g_intstackalloc, object
   .type   g_intstackbase, object
 g_intstackalloc:
-  .skip  ((CONFIG_ARCH_INTERRUPTSTACK & ~3))
+  .skip  ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
 g_intstackbase:
   .skip  4
   .size  g_intstackbase, 4
-  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~3)
+  .size  g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
 #endif
diff --git a/arch/risc-v/src/rv32im/riscv_vfork.c b/arch/risc-v/src/rv32im/riscv_vfork.c
index 3ed4b8d..894f6f5 100644
--- a/arch/risc-v/src/rv32im/riscv_vfork.c
+++ b/arch/risc-v/src/rv32im/riscv_vfork.c
@@ -41,10 +41,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#ifndef CONFIG_STACK_ALIGNMENT
-#  define CONFIG_STACK_ALIGNMENT 4
-#endif
-
 /****************************************************************************
  * Private Functions
  ****************************************************************************/