kernel: add Z_STACK_PTR_ALIGN ARCH_STACK_PTR_ALIGN

This operation is formally defined as rounding down a potential
stack pointer value to meet CPU and ABI requirments.

This was previously defined ad-hoc as STACK_ROUND_DOWN().

A new architecture constant ARCH_STACK_PTR_ALIGN is added.
Z_STACK_PTR_ALIGN() is defined in terms of it. This used to
be inconsistently specified as STACK_ALIGN or STACK_PTR_ALIGN;
in the latter case, STACK_ALIGN meant something else, typically
a required alignment for the base of a stack buffer.

STACK_ROUND_UP() only used in practice by Risc-V, delete
elsewhere.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2020-04-19 15:06:31 -07:00 committed by Anas Nashif
commit 618426d6e7
30 changed files with 69 additions and 87 deletions

View file

@ -42,6 +42,8 @@
extern "C" {
#endif
#define ARCH_STACK_PTR_ALIGN 4
#if defined(CONFIG_MPU_STACK_GUARD) || defined(CONFIG_USERSPACE)
#if defined(CONFIG_ARC_CORE_MPU)
#if CONFIG_ARC_MPU_VER == 2

View file

@ -46,16 +46,16 @@ extern "C" {
#endif
/**
* @brief Declare the STACK_ALIGN_SIZE
* @brief Declare the ARCH_STACK_PTR_ALIGN
*
* Denotes the required alignment of the stack pointer on public API
* boundaries
*
*/
#ifdef CONFIG_STACK_ALIGN_DOUBLE_WORD
#define STACK_ALIGN_SIZE 8
#define ARCH_STACK_PTR_ALIGN 8
#else
#define STACK_ALIGN_SIZE 4
#define ARCH_STACK_PTR_ALIGN 4
#endif
/**
@ -70,7 +70,7 @@ extern "C" {
#if defined(CONFIG_USERSPACE)
#define Z_THREAD_MIN_STACK_ALIGN CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE
#else
#define Z_THREAD_MIN_STACK_ALIGN STACK_ALIGN_SIZE
#define Z_THREAD_MIN_STACK_ALIGN ARCH_STACK_PTR_ALIGN
#endif
/**
@ -97,7 +97,7 @@ extern "C" {
* | Some thread data | <---- Defined when thread is created
* | ... |
* |---------------------| <---- Actual initial stack ptr
* | Initial Stack Ptr | aligned to STACK_ALIGN_SIZE
* | Initial Stack Ptr | aligned to ARCH_STACK_PTR_ALIGN
* | ... |
* | ... |
* | ... |
@ -174,7 +174,7 @@ extern "C" {
* the MPU Stack Guard feature).
*/
#if defined(CONFIG_USERSPACE)
#define Z_PRIVILEGE_STACK_ALIGN MAX(STACK_ALIGN_SIZE, Z_MPU_GUARD_ALIGN)
#define Z_PRIVILEGE_STACK_ALIGN MAX(ARCH_STACK_PTR_ALIGN, Z_MPU_GUARD_ALIGN)
#endif
/**

View file

@ -35,14 +35,13 @@ extern "C" {
#endif
/**
* @brief Declare the STACK_ALIGN_SIZE
* @brief Declare the ARCH_STACK_PTR_ALIGN
*
* Denotes the required alignment of the stack pointer on public API
* boundaries
*
*/
#define STACK_ALIGN 16
#define STACK_ALIGN_SIZE STACK_ALIGN
#define ARCH_STACK_PTR_ALIGN 16
#ifdef __cplusplus
}

View file

@ -24,7 +24,7 @@
#include <arch/common/sys_io.h>
#include <arch/common/ffs.h>
#define STACK_ALIGN 4
#define ARCH_STACK_PTR_ALIGN 4
#ifndef _ASMLANGUAGE
#include <zephyr/types.h>

View file

@ -33,11 +33,9 @@ extern "C" {
#endif
#ifdef CONFIG_64BIT
#define STACK_ALIGN 8
#define STACK_ALIGN_SIZE 8
#define ARCH_STACK_PTR_ALIGN 8
#else
#define STACK_ALIGN 4
#define STACK_ALIGN_SIZE 4
#define ARCH_STACK_PTR_ALIGN 4
#endif
struct __esf {

View file

@ -26,7 +26,7 @@
#include <devicetree.h>
/* stacks, for RISCV architecture stack should be 16byte-aligned */
#define STACK_ALIGN 16
#define ARCH_STACK_PTR_ALIGN 16
#ifdef CONFIG_64BIT
#define RV_OP_LOADREG ld
@ -65,8 +65,7 @@
extern "C" {
#endif
#define STACK_ROUND_UP(x) ROUND_UP(x, STACK_ALIGN)
#define STACK_ROUND_DOWN(x) ROUND_DOWN(x, STACK_ALIGN)
#define STACK_ROUND_UP(x) ROUND_UP(x, ARCH_STACK_PTR_ALIGN)
/* macros convert value of its argument to a string */
#define DO_TOSTR(s) #s

View file

@ -13,7 +13,7 @@
#error "Check ISR stack configuration (CONFIG_ISR_*)"
#endif
#if CONFIG_ISR_SUBSTACK_SIZE % STACK_ALIGN
#if CONFIG_ISR_SUBSTACK_SIZE % ARCH_STACK_PTR_ALIGN
#error "CONFIG_ISR_SUBSTACK_SIZE must be a multiple of 16"
#endif

View file

@ -9,9 +9,9 @@
#include <arch/x86/mmustructs.h>
#ifdef CONFIG_X86_64
#define STACK_ALIGN 16UL
#define ARCH_STACK_PTR_ALIGN 16UL
#else
#define STACK_ALIGN 4UL
#define ARCH_STACK_PTR_ALIGN 4UL
#endif
#ifdef CONFIG_USERSPACE
@ -112,7 +112,7 @@
#if defined(CONFIG_HW_STACK_PROTECTION) || defined(CONFIG_USERSPACE)
#define Z_X86_STACK_BASE_ALIGN MMU_PAGE_SIZE
#else
#define Z_X86_STACK_BASE_ALIGN STACK_ALIGN
#define Z_X86_STACK_BASE_ALIGN ARCH_STACK_PTR_ALIGN
#endif
#ifdef CONFIG_USERSPACE
@ -122,7 +122,7 @@
*/
#define Z_X86_STACK_SIZE_ALIGN MMU_PAGE_SIZE
#else
#define Z_X86_STACK_SIZE_ALIGN STACK_ALIGN
#define Z_X86_STACK_SIZE_ALIGN ARCH_STACK_PTR_ALIGN
#endif
#ifndef _ASMLANGUAGE

View file

@ -27,7 +27,7 @@
#include <xtensa/config/core.h>
#include <arch/common/addr_types.h>
#define STACK_ALIGN 16
#define ARCH_STACK_PTR_ALIGN 16
/* Xtensa GPRs are often designated by two different names */
#define sys_define_gpr_with_alias(name1, name2) union { u32_t name1, name2; }

View file

@ -108,9 +108,16 @@ static inline u32_t arch_k_cycle_get_32(void);
* @see K_THREAD_STACK_RESERVED
*/
/**
* @def ARCH_STACK_PTR_ALIGN
*
* Required alignment of the CPU's stack pointer register value, dictated by
* hardware constraints and the ABI calling convention.
*
* @see Z_STACK_PTR_ALIGN
*/
/** @} */
/**
* @addtogroup arch-pm
* @{

View file

@ -15,6 +15,7 @@
#if !defined(_ASMLANGUAGE)
#include <arch/cpu.h>
#include <sys/util.h>
/* Using typedef deliberately here, this is quite intended to be an opaque
* type.
@ -42,6 +43,16 @@ struct __packed z_thread_stack_element {
* @see z_thread_stack_element
*/
/**
* @brief Properly align a CPU stack pointer value
*
* Take the provided value and round it down such that the value is aligned
* to the CPU and ABI requirements. This is not used for any memory protection
* hardware requirements.
*/
#define Z_STACK_PTR_ALIGN(ptr) ROUND_DOWN((ptr), ARCH_STACK_PTR_ALIGN)
/**
* @brief Obtain an extern reference to a stack
*
@ -74,8 +85,8 @@ static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
*
* This declares a region of memory suitable for use as a thread's stack.
*
* This is the generic, historical definition. Align to STACK_ALIGN and put in
* 'noinit' section so that it isn't zeroed at boot
* This is the generic, historical definition. Align to ARCH_STACK_PTR_ALIGN
* and put in 'noinit' section so that it isn't zeroed at boot
*
* The declared symbol will always be a k_thread_stack_t which can be passed to
* k_thread_create(), but should otherwise not be manipulated. If the buffer
@ -96,7 +107,8 @@ static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
* @param size Size of the stack memory region
*/
#define K_THREAD_STACK_DEFINE(sym, size) \
struct z_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
struct z_thread_stack_element __noinit \
__aligned(ARCH_STACK_PTR_ALIGN) sym[size]
/**
* @brief Calculate size of stacks to be allocated in a stack array
@ -116,8 +128,8 @@ static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
* Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
* definition for additional details and constraints.
*
* This is the generic, historical definition. Align to STACK_ALIGN and put in
* 'noinit' section so that it isn't zeroed at boot
* This is the generic, historical definition. Align to ARCH_STACK_PTR_ALIGN
* and put in 'noinit' section so that it isn't zeroed at boot
*
* @param sym Thread stack symbol name
* @param nmemb Number of stacks to declare
@ -125,7 +137,8 @@ static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
*/
#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
struct z_thread_stack_element __noinit \
__aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
__aligned(ARCH_STACK_PTR_ALIGN) \
sym[nmemb][K_THREAD_STACK_LEN(size)]
/**
* @brief Declare an embedded stack memory region
@ -140,7 +153,7 @@ static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
* @param size Size of the stack memory region
*/
#define K_THREAD_STACK_MEMBER(sym, size) \
struct z_thread_stack_element __aligned(STACK_ALIGN) sym[size]
struct z_thread_stack_element __aligned(ARCH_STACK_PTR_ALIGN) sym[size]
/**
* @brief Return the size in bytes of a stack memory region