Skip to content

Commit 575abc0

Browse files
Andrew Boieandrewboie
authored andcommitted
kernel: add K_THREAD_STACK_RESERVED
This is used to have each arch canonically state how much room in the stack object is reserved for non-thread use. Signed-off-by: Andrew Boie <[email protected]>
1 parent 02f64c6 commit 575abc0

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

include/arch/arc/arch.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ extern "C" {
8181

8282
#if defined(CONFIG_USERSPACE)
8383

84+
#define Z_ARCH_THREAD_STACK_RESERVED \
85+
(STACK_GUARD_SIZE + CONFIG_PRIVILEGED_STACK_SIZE)
86+
8487
#if CONFIG_ARC_MPU_VER == 2
8588

8689
#define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \
@@ -134,6 +137,8 @@ extern "C" {
134137

135138
#else /* CONFIG_USERSPACE */
136139

140+
#define Z_ARCH_THREAD_STACK_RESERVED STACK_GUARD_SIZE
141+
137142
#define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \
138143
struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) \
139144
sym[size + STACK_GUARD_SIZE]

include/arch/arm/arch.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ extern "C" {
120120
1 << (31 - __builtin_clz(x) + 1) : \
121121
1 << (31 - __builtin_clz(x)))
122122

123+
#if defined(CONFIG_USERSPACE) && \
124+
defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT)
125+
/* Guard is 'carved-out' of the thread stack region, and the supervisor
126+
* mode stack is allocated elsewhere by gen_priv_stack.py
127+
*/
128+
#define Z_ARCH_THREAD_STACK_RESERVED 0
129+
#else
130+
#define Z_ARCH_THREAD_STACK_RESERVED MPU_GUARD_ALIGN_AND_SIZE
131+
#endif
132+
123133
#if defined(CONFIG_USERSPACE) && \
124134
defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT)
125135
#define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \

include/arch/x86/arch.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,17 @@ extern struct task_state_segment _main_tss;
582582
* to the top of it.
583583
* All context switches will save/restore the esp0 field in the TSS.
584584
*/
585-
#define _STACK_GUARD_SIZE (MMU_PAGE_SIZE * 2)
585+
#define Z_ARCH_THREAD_STACK_RESERVED (MMU_PAGE_SIZE * 2)
586586
#define _STACK_BASE_ALIGN MMU_PAGE_SIZE
587587
#elif defined(CONFIG_HW_STACK_PROTECTION) || defined(CONFIG_USERSPACE)
588588
/* If only one of HW stack protection or userspace is enabled, then the
589589
* stack will be preceded by one page which is a guard page or a kernel mode
590590
* stack, respectively.
591591
*/
592-
#define _STACK_GUARD_SIZE MMU_PAGE_SIZE
592+
#define Z_ARCH_THREAD_STACK_RESERVED MMU_PAGE_SIZE
593593
#define _STACK_BASE_ALIGN MMU_PAGE_SIZE
594594
#else /* Neither feature */
595-
#define _STACK_GUARD_SIZE 0
595+
#define Z_ARCH_THREAD_STACK_RESERVED 0
596596
#define _STACK_BASE_ALIGN STACK_ALIGN
597597
#endif
598598

@@ -609,12 +609,12 @@ extern struct task_state_segment _main_tss;
609609
#define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \
610610
struct _k_thread_stack_element __noinit \
611611
__aligned(_STACK_BASE_ALIGN) \
612-
sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + _STACK_GUARD_SIZE]
612+
sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + Z_ARCH_THREAD_STACK_RESERVED]
613613

614614
#define Z_ARCH_THREAD_STACK_LEN(size) \
615615
(ROUND_UP((size), \
616616
MAX(_STACK_BASE_ALIGN, _STACK_SIZE_ALIGN)) + \
617-
_STACK_GUARD_SIZE)
617+
Z_ARCH_THREAD_STACK_RESERVED)
618618

619619
#define Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
620620
struct _k_thread_stack_element __noinit \
@@ -623,13 +623,13 @@ extern struct task_state_segment _main_tss;
623623

624624
#define Z_ARCH_THREAD_STACK_MEMBER(sym, size) \
625625
struct _k_thread_stack_element __aligned(_STACK_BASE_ALIGN) \
626-
sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + _STACK_GUARD_SIZE]
626+
sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + Z_ARCH_THREAD_STACK_RESERVED]
627627

628628
#define Z_ARCH_THREAD_STACK_SIZEOF(sym) \
629-
(sizeof(sym) - _STACK_GUARD_SIZE)
629+
(sizeof(sym) - Z_ARCH_THREAD_STACK_RESERVED)
630630

631631
#define Z_ARCH_THREAD_STACK_BUFFER(sym) \
632-
((char *)((sym) + _STACK_GUARD_SIZE))
632+
((char *)((sym) + Z_ARCH_THREAD_STACK_RESERVED))
633633

634634
#if CONFIG_X86_KERNEL_OOPS
635635
#define Z_ARCH_EXCEPT(reason_p) do { \

include/kernel.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4580,6 +4580,7 @@ extern void z_timer_expiration_handler(struct _timeout *t);
45804580
#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
45814581
#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
45824582
#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
4583+
#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
45834584
static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
45844585
{
45854586
return Z_ARCH_THREAD_STACK_BUFFER(sym);
@@ -4677,6 +4678,18 @@ static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
46774678
*/
46784679
#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
46794680

4681+
4682+
/**
4683+
* @brief Indicate how much additional memory is reserved for stack objects
4684+
*
4685+
* Any given stack declaration may have additional memory in it for guard
4686+
* areas or supervisor mode stacks. This macro indicates how much space
4687+
* is reserved for this. The memory reserved may not be contiguous within
4688+
* the stack object, and does not account for additional space used due to
4689+
* enforce alignment.
4690+
*/
4691+
#define K_THREAD_STACK_RESERVED 0
4692+
46804693
/**
46814694
* @brief Get a pointer to the physical stack buffer
46824695
*

0 commit comments

Comments
 (0)