2022-06-28 11:43:32 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Meta
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel_internal.h"
|
|
|
|
|
|
|
|
#include <zephyr/kernel.h>
|
2023-12-13 19:30:18 +08:00
|
|
|
#include <ksched.h>
|
2022-06-28 11:43:32 +08:00
|
|
|
#include <zephyr/kernel/thread_stack.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
#include <zephyr/sys/bitarray.h>
|
2023-06-22 14:31:45 +08:00
|
|
|
#include <zephyr/sys/kobject.h>
|
2023-09-27 06:46:01 +08:00
|
|
|
#include <zephyr/internal/syscall_handler.h>
|
2022-06-28 11:43:32 +08:00
|
|
|
|
|
|
|
LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
|
|
|
|
|
|
|
|
#if CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0
|
|
|
|
#define BA_SIZE CONFIG_DYNAMIC_THREAD_POOL_SIZE
|
|
|
|
#else
|
|
|
|
#define BA_SIZE 1
|
2024-03-08 19:00:10 +08:00
|
|
|
#endif /* CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0 */
|
2022-06-28 11:43:32 +08:00
|
|
|
|
|
|
|
struct dyn_cb_data {
|
|
|
|
k_tid_t tid;
|
|
|
|
k_thread_stack_t *stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
static K_THREAD_STACK_ARRAY_DEFINE(dynamic_stack, CONFIG_DYNAMIC_THREAD_POOL_SIZE,
|
|
|
|
CONFIG_DYNAMIC_THREAD_STACK_SIZE);
|
|
|
|
SYS_BITARRAY_DEFINE_STATIC(dynamic_ba, BA_SIZE);
|
|
|
|
|
|
|
|
static k_thread_stack_t *z_thread_stack_alloc_dyn(size_t align, size_t size)
|
|
|
|
{
|
|
|
|
return z_thread_aligned_alloc(align, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static k_thread_stack_t *z_thread_stack_alloc_pool(size_t size)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
size_t offset;
|
|
|
|
k_thread_stack_t *stack;
|
|
|
|
|
|
|
|
if (size > CONFIG_DYNAMIC_THREAD_STACK_SIZE) {
|
|
|
|
LOG_DBG("stack size %zu is > pool stack size %d", size,
|
|
|
|
CONFIG_DYNAMIC_THREAD_STACK_SIZE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = sys_bitarray_alloc(&dynamic_ba, 1, &offset);
|
|
|
|
if (rv < 0) {
|
|
|
|
LOG_DBG("unable to allocate stack from pool");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(offset < CONFIG_DYNAMIC_THREAD_POOL_SIZE);
|
|
|
|
|
|
|
|
stack = (k_thread_stack_t *)&dynamic_stack[offset];
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2023-06-22 14:31:45 +08:00
|
|
|
static k_thread_stack_t *stack_alloc_dyn(size_t size, int flags)
|
2022-06-28 11:43:32 +08:00
|
|
|
{
|
2023-06-22 14:31:45 +08:00
|
|
|
if ((flags & K_USER) == K_USER) {
|
|
|
|
#ifdef CONFIG_DYNAMIC_OBJECTS
|
|
|
|
return k_object_alloc_size(K_OBJ_THREAD_STACK_ELEMENT, size);
|
|
|
|
#else
|
|
|
|
/* Dynamic user stack needs a kobject, so if this option is not
|
|
|
|
* enabled we can't proceed.
|
|
|
|
*/
|
|
|
|
return NULL;
|
2024-03-08 19:00:10 +08:00
|
|
|
#endif /* CONFIG_DYNAMIC_OBJECTS */
|
2022-06-28 11:43:32 +08:00
|
|
|
}
|
|
|
|
|
2023-06-22 14:31:45 +08:00
|
|
|
return z_thread_stack_alloc_dyn(Z_KERNEL_STACK_OBJ_ALIGN,
|
2024-03-23 05:03:37 +08:00
|
|
|
K_KERNEL_STACK_LEN(size));
|
2023-06-22 14:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
k_thread_stack_t *z_impl_k_thread_stack_alloc(size_t size, int flags)
|
|
|
|
{
|
|
|
|
k_thread_stack_t *stack = NULL;
|
|
|
|
|
2022-06-28 11:43:32 +08:00
|
|
|
if (IS_ENABLED(CONFIG_DYNAMIC_THREAD_PREFER_ALLOC)) {
|
2023-06-22 14:31:45 +08:00
|
|
|
stack = stack_alloc_dyn(size, flags);
|
2022-06-28 11:43:32 +08:00
|
|
|
if (stack == NULL && CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0) {
|
|
|
|
stack = z_thread_stack_alloc_pool(size);
|
|
|
|
}
|
2023-06-30 06:54:20 +08:00
|
|
|
} else if (IS_ENABLED(CONFIG_DYNAMIC_THREAD_PREFER_POOL)) {
|
|
|
|
if (CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0) {
|
|
|
|
stack = z_thread_stack_alloc_pool(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((stack == NULL) && IS_ENABLED(CONFIG_DYNAMIC_THREAD_ALLOC)) {
|
2023-06-22 14:31:45 +08:00
|
|
|
stack = stack_alloc_dyn(size, flags);
|
2022-06-28 11:43:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
static inline k_thread_stack_t *z_vrfy_k_thread_stack_alloc(size_t size, int flags)
|
|
|
|
{
|
|
|
|
return z_impl_k_thread_stack_alloc(size, flags);
|
|
|
|
}
|
|
|
|
#include <syscalls/k_thread_stack_alloc_mrsh.c>
|
2024-03-08 19:00:10 +08:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
2022-06-28 11:43:32 +08:00
|
|
|
|
|
|
|
static void dyn_cb(const struct k_thread *thread, void *user_data)
|
|
|
|
{
|
|
|
|
struct dyn_cb_data *const data = (struct dyn_cb_data *)user_data;
|
|
|
|
|
|
|
|
if (data->stack == (k_thread_stack_t *)thread->stack_info.start) {
|
2023-09-06 23:44:48 +08:00
|
|
|
__ASSERT(data->tid == NULL, "stack %p is associated with more than one thread!",
|
|
|
|
(void *)thread->stack_info.start);
|
2022-06-28 11:43:32 +08:00
|
|
|
data->tid = (k_tid_t)thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int z_impl_k_thread_stack_free(k_thread_stack_t *stack)
|
|
|
|
{
|
|
|
|
struct dyn_cb_data data = {.stack = stack};
|
|
|
|
|
|
|
|
/* Get a possible tid associated with stack */
|
|
|
|
k_thread_foreach(dyn_cb, &data);
|
|
|
|
|
|
|
|
if (data.tid != NULL) {
|
2023-12-13 19:30:18 +08:00
|
|
|
if (!(z_is_thread_state_set(data.tid, _THREAD_DUMMY) ||
|
|
|
|
z_is_thread_state_set(data.tid, _THREAD_DEAD))) {
|
2022-06-28 11:43:32 +08:00
|
|
|
LOG_ERR("tid %p is in use!", data.tid);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0) {
|
|
|
|
if (IS_ARRAY_ELEMENT(dynamic_stack, stack)) {
|
|
|
|
if (sys_bitarray_free(&dynamic_ba, 1, ARRAY_INDEX(dynamic_stack, stack))) {
|
|
|
|
LOG_ERR("stack %p is not allocated!", stack);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_DYNAMIC_THREAD_ALLOC)) {
|
2023-06-22 14:31:45 +08:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2023-09-27 18:49:28 +08:00
|
|
|
if (k_object_find(stack)) {
|
2023-06-22 14:31:45 +08:00
|
|
|
k_object_free(stack);
|
|
|
|
} else {
|
|
|
|
k_free(stack);
|
|
|
|
}
|
|
|
|
#else
|
2022-06-28 11:43:32 +08:00
|
|
|
k_free(stack);
|
2024-03-08 19:00:10 +08:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
2022-06-28 11:43:32 +08:00
|
|
|
} else {
|
2024-01-02 02:01:35 +08:00
|
|
|
LOG_DBG("Invalid stack %p", stack);
|
2022-06-28 11:43:32 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
static inline int z_vrfy_k_thread_stack_free(k_thread_stack_t *stack)
|
|
|
|
{
|
|
|
|
return z_impl_k_thread_stack_free(stack);
|
|
|
|
}
|
|
|
|
#include <syscalls/k_thread_stack_free_mrsh.c>
|
2024-03-08 19:00:10 +08:00
|
|
|
#endif /* CONFIG_USERSPACE */
|