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)
This commit is contained in:
Gustavo Henrique Nihei 2021-04-27 10:48:00 -03:00 committed by Xiang Xiao
parent 91955be0e1
commit 7caebdd50f
10 changed files with 47 additions and 75 deletions

View File

@ -150,16 +150,16 @@ exception_common:
* Name: g_intstackalloc and g_intstacktop
************************************************************************************/
#if CONFIG_ARCH_INTERRUPTSTACK > 3
#if CONFIG_ARCH_INTERRUPTSTACK > 15
.bss
.align 4
.balign 16
.global g_intstackalloc
.global g_intstacktop
.type g_intstackalloc, object
.type g_intstacktop, object
g_intstackalloc:
.skip (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.skip ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
g_intstacktop:
.size g_intstacktop, 0
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
#endif

View File

@ -264,16 +264,16 @@ exception_common:
* Name: g_intstackalloc and g_intstacktop
************************************************************************************/
#if CONFIG_ARCH_INTERRUPTSTACK > 7
#if CONFIG_ARCH_INTERRUPTSTACK > 15
.bss
.align 8
.balign 16
.global g_intstackalloc
.global g_intstacktop
.type g_intstackalloc, object
.type g_intstacktop, object
g_intstackalloc:
.skip ((CONFIG_ARCH_INTERRUPTSTACK + 4) & ~7)
.skip ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
g_intstacktop:
.size g_intstacktop, 0
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~7)
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
#endif

View File

@ -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.
*/
/* 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)
@ -193,18 +186,16 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
uintptr_t top_of_stack;
size_t size_of_stack;
/* RISCV uses a push-down stack: the stack grows toward lower
* addresses in memory. The stack pointer register points to the
* lowest, valid working address (the "top" of the stack). Items on
* the stack are referenced as positive word offsets from sp.
/* RISC-V uses a push-down stack: the stack grows toward lower
* addresses in memory. The stack pointer register points to the
* lowest, valid working address (the "top" of the stack). Items on
* the stack are referenced as positive word offsets from SP.
*/
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
/* The RISC-V stack must be aligned at word (4 byte) boundaries; for
* floating point use, the stack must be aligned to 8-byte addresses.
* If necessary top_of_stack must be rounded down to the next
* boundary to meet these alignment requirements.
/* The RISC-V stack must be aligned at 128-bit (16-byte) boundaries.
* If necessary top_of_stack must be rounded down to the next boundary.
*/
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);

View File

@ -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)

View File

@ -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.
*/
/* 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)
@ -112,17 +105,16 @@ int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size)
tcb->stack_alloc_ptr = stack;
/* RISC-V uses a push-down stack: the stack grows toward loweraddresses in
* memory. The stack pointer register, points to the lowest, valid work
* address (the "top" of the stack). Items on the stack are referenced
* as positive word offsets from sp.
/* RISC-V uses a push-down stack: the stack grows toward lower addresses in
* memory. The stack pointer register, points to the lowest, valid work
* address (the "top" of the stack). Items on the stack are referenced
* as positive word offsets from SP.
*/
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
/* The RISC-V stack must be aligned at word (4 byte) or double word
* (8 byte) boundaries. If necessary top_of_stack must be rounded down to
* the next boundary.
/* The RISC-V stack must be aligned at 128-bit (16-byte) boundaries.
* If necessary top_of_stack must be rounded down to the next boundary.
*/
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);

View File

@ -42,14 +42,14 @@
.section .noinit
#if CONFIG_ARCH_INTERRUPTSTACK > 3
.align 4
#if CONFIG_ARCH_INTERRUPTSTACK > 15
.balign 16
.type g_intstackalloc, @object
.type g_intstacktop, @object
g_intstackalloc:
.skip (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.skip ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
g_intstacktop:
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
#endif
/****************************************************************************

View File

@ -201,16 +201,16 @@ exception_common:
* Name: g_intstackalloc and g_intstacktop
************************************************************************************/
#if CONFIG_ARCH_INTERRUPTSTACK > 3
#if CONFIG_ARCH_INTERRUPTSTACK > 15
.bss
.align 4
.balign 16
.global g_intstackalloc
.global g_intstacktop
.type g_intstackalloc, object
.type g_intstacktop, object
g_intstackalloc:
.skip (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.skip ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
g_intstacktop:
.size g_intstacktop, 0
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
#endif

View File

@ -242,24 +242,24 @@ normal_irq:
* Name: g_intstackalloc and g_intstacktop
************************************************************************************/
#if CONFIG_ARCH_INTERRUPTSTACK > 7
#if CONFIG_ARCH_INTERRUPTSTACK > 15
.bss
.align 8
.balign 16
.global g_intstackalloc
.global g_intstacktop
.type g_intstackalloc, object
.type g_intstacktop, 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_intstacktop:
.size g_intstacktop, 0
#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

View File

@ -188,16 +188,16 @@ exception_common:
* Name: g_intstackalloc and g_intstacktop
************************************************************************************/
#if CONFIG_ARCH_INTERRUPTSTACK > 3
#if CONFIG_ARCH_INTERRUPTSTACK > 15
.bss
.align 4
.balign 16
.global g_intstackalloc
.global g_intstacktop
.type g_intstackalloc, object
.type g_intstacktop, object
g_intstackalloc:
.skip (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.skip ((CONFIG_ARCH_INTERRUPTSTACK + 8) & ~15)
g_intstacktop:
.size g_intstacktop, 0
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~3)
.size g_intstackalloc, (CONFIG_ARCH_INTERRUPTSTACK & ~15)
#endif

View File

@ -41,10 +41,6 @@
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_STACK_ALIGNMENT
# define CONFIG_STACK_ALIGNMENT 4
#endif
/****************************************************************************
* Private Functions
****************************************************************************/