kernel: Add function for calculating stack usage

Extracting stack usage calculation from k_thread_stack_space_get to
z_stack_space_get so it can be used also for interrupt stack.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2022-01-28 15:40:37 +01:00 committed by Anas Nashif
commit 1da97e1374
2 changed files with 14 additions and 7 deletions

View file

@ -160,6 +160,9 @@ K_KERNEL_PINNED_STACK_ARRAY_EXTERN(z_interrupt_stacks, CONFIG_MP_NUM_CPUS,
extern uint8_t *z_priv_stack_find(k_thread_stack_t *stack);
#endif
/* Calculate stack usage. */
int z_stack_space_get(const uint8_t *stack_start, size_t size, size_t *unused_ptr);
#ifdef CONFIG_USERSPACE
bool z_stack_is_user_capable(k_thread_stack_t *stack);

View file

@ -901,18 +901,15 @@ void irq_offload(irq_offload_routine_t routine, const void *parameter)
#error "Unsupported configuration for stack analysis"
#endif
int z_impl_k_thread_stack_space_get(const struct k_thread *thread,
size_t *unused_ptr)
int z_stack_space_get(const uint8_t *stack_start, size_t size, size_t *unused_ptr)
{
const uint8_t *start = (uint8_t *)thread->stack_info.start;
size_t size = thread->stack_info.size;
size_t unused = 0;
const uint8_t *checked_stack = start;
const uint8_t *checked_stack = stack_start;
/* Take the address of any local variable as a shallow bound for the
* stack pointer. Addresses above it are guaranteed to be
* accessible.
*/
const uint8_t *stack_pointer = (const uint8_t *)&start;
const uint8_t *stack_pointer = (const uint8_t *)&stack_start;
/* If we are currently running on the stack being analyzed, some
* memory management hardware will generate an exception if we
@ -921,7 +918,7 @@ int z_impl_k_thread_stack_space_get(const struct k_thread *thread,
* This never happens when invoked from user mode, as user mode
* will always run this function on the privilege elevation stack.
*/
if ((stack_pointer > start) && (stack_pointer <= (start + size)) &&
if ((stack_pointer > stack_start) && (stack_pointer <= (stack_start + size)) &&
IS_ENABLED(CONFIG_NO_UNUSED_STACK_INSPECTION)) {
/* TODO: We could add an arch_ API call to temporarily
* disable the stack checking in the CPU, but this would
@ -955,6 +952,13 @@ int z_impl_k_thread_stack_space_get(const struct k_thread *thread,
return 0;
}
int z_impl_k_thread_stack_space_get(const struct k_thread *thread,
size_t *unused_ptr)
{
return z_stack_space_get((const uint8_t *)thread->stack_info.start,
thread->stack_info.size, unused_ptr);
}
#ifdef CONFIG_USERSPACE
int z_vrfy_k_thread_stack_space_get(const struct k_thread *thread,
size_t *unused_ptr)