Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022, Meta |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include "kernel_internal.h" |
| 8 | |
| 9 | #include <zephyr/kernel.h> |
Andrei Emeltchenko | 9d3a3e9 | 2023-12-13 13:30:18 +0200 | [diff] [blame] | 10 | #include <ksched.h> |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 11 | #include <zephyr/kernel/thread_stack.h> |
| 12 | #include <zephyr/logging/log.h> |
| 13 | #include <zephyr/sys/bitarray.h> |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 14 | #include <zephyr/sys/kobject.h> |
Anas Nashif | 4e39617 | 2023-09-26 22:46:01 +0000 | [diff] [blame] | 15 | #include <zephyr/internal/syscall_handler.h> |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 16 | |
| 17 | LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); |
| 18 | |
| 19 | #if CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0 |
| 20 | #define BA_SIZE CONFIG_DYNAMIC_THREAD_POOL_SIZE |
| 21 | #else |
| 22 | #define BA_SIZE 1 |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 23 | #endif /* CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0 */ |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 24 | |
| 25 | struct dyn_cb_data { |
| 26 | k_tid_t tid; |
| 27 | k_thread_stack_t *stack; |
| 28 | }; |
| 29 | |
| 30 | static K_THREAD_STACK_ARRAY_DEFINE(dynamic_stack, CONFIG_DYNAMIC_THREAD_POOL_SIZE, |
| 31 | CONFIG_DYNAMIC_THREAD_STACK_SIZE); |
| 32 | SYS_BITARRAY_DEFINE_STATIC(dynamic_ba, BA_SIZE); |
| 33 | |
| 34 | static k_thread_stack_t *z_thread_stack_alloc_dyn(size_t align, size_t size) |
| 35 | { |
| 36 | return z_thread_aligned_alloc(align, size); |
| 37 | } |
| 38 | |
| 39 | static k_thread_stack_t *z_thread_stack_alloc_pool(size_t size) |
| 40 | { |
| 41 | int rv; |
| 42 | size_t offset; |
| 43 | k_thread_stack_t *stack; |
| 44 | |
| 45 | if (size > CONFIG_DYNAMIC_THREAD_STACK_SIZE) { |
| 46 | LOG_DBG("stack size %zu is > pool stack size %d", size, |
| 47 | CONFIG_DYNAMIC_THREAD_STACK_SIZE); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | rv = sys_bitarray_alloc(&dynamic_ba, 1, &offset); |
| 52 | if (rv < 0) { |
| 53 | LOG_DBG("unable to allocate stack from pool"); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | __ASSERT_NO_MSG(offset < CONFIG_DYNAMIC_THREAD_POOL_SIZE); |
| 58 | |
| 59 | stack = (k_thread_stack_t *)&dynamic_stack[offset]; |
| 60 | |
| 61 | return stack; |
| 62 | } |
| 63 | |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 64 | static k_thread_stack_t *stack_alloc_dyn(size_t size, int flags) |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 65 | { |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 66 | if ((flags & K_USER) == K_USER) { |
| 67 | #ifdef CONFIG_DYNAMIC_OBJECTS |
| 68 | return k_object_alloc_size(K_OBJ_THREAD_STACK_ELEMENT, size); |
| 69 | #else |
| 70 | /* Dynamic user stack needs a kobject, so if this option is not |
| 71 | * enabled we can't proceed. |
| 72 | */ |
| 73 | return NULL; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 74 | #endif /* CONFIG_DYNAMIC_OBJECTS */ |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 77 | return z_thread_stack_alloc_dyn(Z_KERNEL_STACK_OBJ_ALIGN, |
Daniel Leung | 6cd7936 | 2024-03-22 14:03:37 -0700 | [diff] [blame] | 78 | K_KERNEL_STACK_LEN(size)); |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | k_thread_stack_t *z_impl_k_thread_stack_alloc(size_t size, int flags) |
| 82 | { |
| 83 | k_thread_stack_t *stack = NULL; |
| 84 | |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 85 | if (IS_ENABLED(CONFIG_DYNAMIC_THREAD_PREFER_ALLOC)) { |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 86 | stack = stack_alloc_dyn(size, flags); |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 87 | if (stack == NULL && CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0) { |
| 88 | stack = z_thread_stack_alloc_pool(size); |
| 89 | } |
Flavio Ceolin | 4feb182 | 2023-06-29 15:54:20 -0700 | [diff] [blame] | 90 | } else if (IS_ENABLED(CONFIG_DYNAMIC_THREAD_PREFER_POOL)) { |
| 91 | if (CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0) { |
| 92 | stack = z_thread_stack_alloc_pool(size); |
| 93 | } |
| 94 | |
| 95 | if ((stack == NULL) && IS_ENABLED(CONFIG_DYNAMIC_THREAD_ALLOC)) { |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 96 | stack = stack_alloc_dyn(size, flags); |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 97 | } |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return stack; |
| 101 | } |
| 102 | |
| 103 | #ifdef CONFIG_USERSPACE |
| 104 | static inline k_thread_stack_t *z_vrfy_k_thread_stack_alloc(size_t size, int flags) |
| 105 | { |
| 106 | return z_impl_k_thread_stack_alloc(size, flags); |
| 107 | } |
| 108 | #include <syscalls/k_thread_stack_alloc_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 109 | #endif /* CONFIG_USERSPACE */ |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 110 | |
| 111 | static void dyn_cb(const struct k_thread *thread, void *user_data) |
| 112 | { |
| 113 | struct dyn_cb_data *const data = (struct dyn_cb_data *)user_data; |
| 114 | |
| 115 | if (data->stack == (k_thread_stack_t *)thread->stack_info.start) { |
Fabio Baltieri | a477bdd | 2023-09-06 15:44:48 +0000 | [diff] [blame] | 116 | __ASSERT(data->tid == NULL, "stack %p is associated with more than one thread!", |
| 117 | (void *)thread->stack_info.start); |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 118 | data->tid = (k_tid_t)thread; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | int z_impl_k_thread_stack_free(k_thread_stack_t *stack) |
| 123 | { |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 124 | struct dyn_cb_data data = {.stack = stack}; |
| 125 | |
| 126 | /* Get a possible tid associated with stack */ |
| 127 | k_thread_foreach(dyn_cb, &data); |
| 128 | |
| 129 | if (data.tid != NULL) { |
Andrei Emeltchenko | 9d3a3e9 | 2023-12-13 13:30:18 +0200 | [diff] [blame] | 130 | if (!(z_is_thread_state_set(data.tid, _THREAD_DUMMY) || |
| 131 | z_is_thread_state_set(data.tid, _THREAD_DEAD))) { |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 132 | LOG_ERR("tid %p is in use!", data.tid); |
| 133 | return -EBUSY; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (CONFIG_DYNAMIC_THREAD_POOL_SIZE > 0) { |
| 138 | if (IS_ARRAY_ELEMENT(dynamic_stack, stack)) { |
| 139 | if (sys_bitarray_free(&dynamic_ba, 1, ARRAY_INDEX(dynamic_stack, stack))) { |
| 140 | LOG_ERR("stack %p is not allocated!", stack); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (IS_ENABLED(CONFIG_DYNAMIC_THREAD_ALLOC)) { |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 149 | #ifdef CONFIG_USERSPACE |
Anas Nashif | c25d080 | 2023-09-27 10:49:28 +0000 | [diff] [blame] | 150 | if (k_object_find(stack)) { |
Flavio Ceolin | fc6d9ee | 2023-06-22 06:31:45 +0000 | [diff] [blame] | 151 | k_object_free(stack); |
| 152 | } else { |
| 153 | k_free(stack); |
| 154 | } |
| 155 | #else |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 156 | k_free(stack); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 157 | #endif /* CONFIG_USERSPACE */ |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 158 | } else { |
Christopher Friedt | 9f3d777 | 2024-01-01 13:01:35 -0500 | [diff] [blame] | 159 | LOG_DBG("Invalid stack %p", stack); |
Christopher Friedt | 7b1b257 | 2022-06-27 23:43:32 -0400 | [diff] [blame] | 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | #ifdef CONFIG_USERSPACE |
| 167 | static inline int z_vrfy_k_thread_stack_free(k_thread_stack_t *stack) |
| 168 | { |
| 169 | return z_impl_k_thread_stack_free(stack); |
| 170 | } |
| 171 | #include <syscalls/k_thread_stack_free_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 172 | #endif /* CONFIG_USERSPACE */ |