Skip to content

Commit

Permalink
kernel: userspace: Fix dynamic thread stack allocation for RISC-V
Browse files Browse the repository at this point in the history
RISC-V use the same logic for dynamic thread stacks allocation as ARM.
This fixup reuses ARM code.

Signed-off-by: Rick Tsao <[email protected]>
  • Loading branch information
Rick Tsao authored and carlescufi committed Sep 4, 2024
1 parent 8974c24 commit e9ff0dc
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions kernel/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ static struct k_spinlock lists_lock; /* kobj dlist */
static struct k_spinlock objfree_lock; /* k_object_free */

#ifdef CONFIG_GEN_PRIV_STACKS
/* On ARM & ARC MPU we may have two different alignment requirement
/* On ARM & ARC MPU & RISC-V PMP we may have two different alignment requirement
* when dynamically allocating thread stacks, one for the privileged
* stack and other for the user stack, so we need to account the
* worst alignment scenario and reserve space for that.
*/
#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU)
#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) || defined(CONFIG_RISCV_PMP)
#define STACK_ELEMENT_DATA_SIZE(size) \
(sizeof(struct z_stack_data) + CONFIG_PRIVILEGED_STACK_SIZE + \
Z_THREAD_STACK_OBJ_ALIGN(size) + K_THREAD_STACK_LEN(size))
#else
#define STACK_ELEMENT_DATA_SIZE(size) (sizeof(struct z_stack_data) + \
K_THREAD_STACK_LEN(size))
#endif /* CONFIG_ARM_MPU || CONFIG_ARC_MPU */
#endif /* CONFIG_ARM_MPU || CONFIG_ARC_MPU || CONFIG_RISCV_PMP */
#else
#define STACK_ELEMENT_DATA_SIZE(size) K_THREAD_STACK_LEN(size)
#endif /* CONFIG_GEN_PRIV_STACKS */
Expand Down Expand Up @@ -150,13 +150,13 @@ uint8_t *z_priv_stack_find(k_thread_stack_t *stack)
#endif /* ARCH_DYNAMIC_OBJ_K_THREAD_ALIGNMENT */

#ifdef CONFIG_DYNAMIC_THREAD_STACK_SIZE
#ifndef CONFIG_MPU_STACK_GUARD
#if defined(CONFIG_MPU_STACK_GUARD) || defined(CONFIG_PMP_STACK_GUARD)
#define DYN_OBJ_DATA_ALIGN_K_THREAD_STACK \
Z_THREAD_STACK_OBJ_ALIGN(CONFIG_PRIVILEGED_STACK_SIZE)
Z_THREAD_STACK_OBJ_ALIGN(CONFIG_DYNAMIC_THREAD_STACK_SIZE)
#else
#define DYN_OBJ_DATA_ALIGN_K_THREAD_STACK \
Z_THREAD_STACK_OBJ_ALIGN(CONFIG_DYNAMIC_THREAD_STACK_SIZE)
#endif /* !CONFIG_MPU_STACK_GUARD */
Z_THREAD_STACK_OBJ_ALIGN(CONFIG_PRIVILEGED_STACK_SIZE)
#endif /* CONFIG_MPU_STACK_GUARD || CONFIG_PMP_STACK_GUARD */
#else
#define DYN_OBJ_DATA_ALIGN_K_THREAD_STACK \
Z_THREAD_STACK_OBJ_ALIGN(ARCH_STACK_PTR_ALIGN)
Expand Down Expand Up @@ -349,13 +349,13 @@ static struct k_object *dynamic_object_create(enum k_objects otype, size_t align
stack_data->priv = (uint8_t *)dyn->data;
stack_data->size = adjusted_size;
dyn->kobj.data.stack_data = stack_data;
#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU)
#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) || defined(CONFIG_RISCV_PMP)
dyn->kobj.name = (void *)ROUND_UP(
((uint8_t *)dyn->data + CONFIG_PRIVILEGED_STACK_SIZE),
Z_THREAD_STACK_OBJ_ALIGN(size));
#else
dyn->kobj.name = dyn->data;
#endif /* CONFIG_ARM_MPU || CONFIG_ARC_MPU */
#endif /* CONFIG_ARM_MPU || CONFIG_ARC_MPU || CONFIG_RISCV_PMP */
#else
dyn->kobj.name = dyn->data;
dyn->kobj.data.stack_size = adjusted_size;
Expand Down

0 comments on commit e9ff0dc

Please sign in to comment.