tests: threads/thread_stack: extend tests for mapped stack

This extends the test for memory mapped stack, as the address of
memory mapped stack object would be different than the actual
stack object.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2024-03-14 12:49:45 -07:00 committed by Anas Nashif
parent 0b3a4f35d0
commit cfbbcac3f7

View file

@ -66,6 +66,10 @@ static inline int z_vrfy_check_perms(void *addr, size_t size, int write)
ZTEST_BMEM struct scenario_data { ZTEST_BMEM struct scenario_data {
k_thread_stack_t *stack; k_thread_stack_t *stack;
#ifdef CONFIG_THREAD_STACK_MEM_MAPPED
k_thread_stack_t *stack_mapped;
#endif
/* If this was declared with K_THREAD_STACK_DEFINE and not /* If this was declared with K_THREAD_STACK_DEFINE and not
* K_KERNEL_STACK_DEFINE * K_KERNEL_STACK_DEFINE
*/ */
@ -89,7 +93,7 @@ ZTEST_BMEM struct scenario_data {
void stack_buffer_scenarios(void) void stack_buffer_scenarios(void)
{ {
k_thread_stack_t *stack_obj = scenario_data.stack; k_thread_stack_t *stack_obj;
size_t obj_size = scenario_data.object_size; size_t obj_size = scenario_data.object_size;
size_t stack_size, unused, carveout, reserved, alignment, adjusted; size_t stack_size, unused, carveout, reserved, alignment, adjusted;
uint8_t val = 0; uint8_t val = 0;
@ -97,10 +101,18 @@ void stack_buffer_scenarios(void)
char *stack_buf; char *stack_buf;
volatile char *pos; volatile char *pos;
int ret, expected; int ret, expected;
uintptr_t base = (uintptr_t)stack_obj; uintptr_t base;
bool is_usermode; bool is_usermode;
long int end_space; long int end_space;
#ifdef CONFIG_THREAD_STACK_MEM_MAPPED
stack_obj = scenario_data.stack_mapped;
#else
stack_obj = scenario_data.stack;
#endif
base = (uintptr_t)stack_obj;
#ifdef CONFIG_USERSPACE #ifdef CONFIG_USERSPACE
is_usermode = arch_is_user_context(); is_usermode = arch_is_user_context();
#else #else
@ -211,8 +223,10 @@ void stack_buffer_scenarios(void)
stack_size); stack_size);
} }
#endif #endif
carveout = stack_start - stack_buf; carveout = stack_start - stack_buf;
printk(" - Carved-out space in buffer: %zu\n", carveout); printk(" - Carved-out space in buffer: %zu\n", carveout);
zassert_true(carveout < stack_size, zassert_true(carveout < stack_size,
"Suspicious carve-out space reported"); "Suspicious carve-out space reported");
/* 0 unless this is a stack array */ /* 0 unless this is a stack array */
@ -318,12 +332,28 @@ void stest_thread_launch(uint32_t flags, bool drop)
k_thread_create(&test_thread, scenario_data.stack, STEST_STACKSIZE, k_thread_create(&test_thread, scenario_data.stack, STEST_STACKSIZE,
stest_thread_entry, stest_thread_entry,
(void *)drop, NULL, NULL, (void *)drop, NULL, NULL,
-1, flags, K_NO_WAIT); -1, flags, K_FOREVER);
#ifdef CONFIG_THREAD_STACK_MEM_MAPPED
scenario_data.stack_mapped = test_thread.stack_info.mapped.addr;
printk(" - Memory mapped stack object %p\n", scenario_data.stack_mapped);
#endif /* CONFIG_THREAD_STACK_MEM_MAPPED */
k_thread_start(&test_thread);
k_thread_join(&test_thread, K_FOREVER); k_thread_join(&test_thread, K_FOREVER);
ret = k_thread_stack_space_get(&test_thread, &unused); ret = k_thread_stack_space_get(&test_thread, &unused);
zassert_equal(ret, 0, "failed to calculate unused stack space\n");
printk("target thread unused stack space: %zu\n", unused); #ifdef CONFIG_THREAD_STACK_MEM_MAPPED
if (ret == -EINVAL) {
printk("! cannot report unused stack space due to stack no longer mapped.\n");
} else
#endif /* CONFIG_THREAD_STACK_MEM_MAPPED */
{
zassert_equal(ret, 0, "failed to calculate unused stack space\n");
printk("target thread unused stack space: %zu\n", unused);
}
} }
void scenario_entry(void *stack_obj, size_t obj_size, size_t reported_size, void scenario_entry(void *stack_obj, size_t obj_size, size_t reported_size,