Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010-2014 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
Allan Stephens | 9f09777 | 2016-10-24 12:41:43 -0500 | [diff] [blame] | 9 | * @brief Kernel thread support |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 10 | * |
Allan Stephens | 9f09777 | 2016-10-24 12:41:43 -0500 | [diff] [blame] | 11 | * This module provides general purpose thread support. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 12 | */ |
| 13 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 14 | #include <zephyr/kernel.h> |
| 15 | #include <zephyr/spinlock.h> |
| 16 | #include <zephyr/sys/math_extras.h> |
| 17 | #include <zephyr/sys_clock.h> |
Benjamin Walsh | b4b108d | 2016-10-13 10:31:48 -0400 | [diff] [blame] | 18 | #include <ksched.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 19 | #include <zephyr/wait_q.h> |
| 20 | #include <zephyr/syscall_handler.h> |
Andy Ross | 245b54e | 2018-02-08 09:10:46 -0800 | [diff] [blame] | 21 | #include <kernel_internal.h> |
Andy Ross | 9c62cc6 | 2018-01-25 15:24:15 -0800 | [diff] [blame] | 22 | #include <kswap.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 23 | #include <zephyr/init.h> |
| 24 | #include <zephyr/tracing/tracing.h> |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 25 | #include <string.h> |
Flavio Ceolin | 09e362e | 2018-12-17 12:34:05 -0800 | [diff] [blame] | 26 | #include <stdbool.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 27 | #include <zephyr/irq_offload.h> |
| 28 | #include <zephyr/sys/check.h> |
| 29 | #include <zephyr/random/rand32.h> |
| 30 | #include <zephyr/sys/atomic.h> |
| 31 | #include <zephyr/logging/log.h> |
Krzysztof Chruscinski | 3ed8083 | 2020-11-26 19:32:34 +0100 | [diff] [blame] | 32 | LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 33 | |
Andrew Boie | 6cf496f | 2020-02-14 10:52:49 -0800 | [diff] [blame] | 34 | #ifdef CONFIG_THREAD_MONITOR |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 35 | /* This lock protects the linked list of active threads; i.e. the |
| 36 | * initial _kernel.threads pointer and the linked list made up of |
| 37 | * thread->next_thread (until NULL) |
| 38 | */ |
| 39 | static struct k_spinlock z_thread_monitor_lock; |
| 40 | #endif /* CONFIG_THREAD_MONITOR */ |
Andy Ross | e456d0f | 2018-07-25 10:46:38 -0700 | [diff] [blame] | 41 | |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 42 | #define _FOREACH_STATIC_THREAD(thread_data) \ |
Fabio Baltieri | f88a420 | 2021-08-04 23:05:54 +0100 | [diff] [blame] | 43 | STRUCT_SECTION_FOREACH(_static_thread_data, thread_data) |
Peter Mitsis | 0ca7cea | 2016-09-28 19:18:09 -0400 | [diff] [blame] | 44 | |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 45 | void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data) |
| 46 | { |
Ramakrishna Pallala | e74d85d | 2018-07-18 13:50:52 +0530 | [diff] [blame] | 47 | #if defined(CONFIG_THREAD_MONITOR) |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 48 | struct k_thread *thread; |
Andy Ross | e456d0f | 2018-07-25 10:46:38 -0700 | [diff] [blame] | 49 | k_spinlock_key_t key; |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 50 | |
Flavio Ceolin | d8837c6 | 2018-09-18 12:40:54 -0700 | [diff] [blame] | 51 | __ASSERT(user_cb != NULL, "user_cb can not be NULL"); |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Lock is needed to make sure that the _kernel.threads is not being |
Paul Sokolovsky | 2df1829 | 2018-09-26 13:54:09 +0300 | [diff] [blame] | 55 | * modified by the user_cb either directly or indirectly. |
| 56 | * The indirect ways are through calling k_thread_create and |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 57 | * k_thread_abort from user_cb. |
| 58 | */ |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 59 | key = k_spin_lock(&z_thread_monitor_lock); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 60 | |
| 61 | SYS_PORT_TRACING_FUNC_ENTER(k_thread, foreach); |
| 62 | |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 63 | for (thread = _kernel.threads; thread; thread = thread->next_thread) { |
| 64 | user_cb(thread, user_data); |
| 65 | } |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 66 | |
| 67 | SYS_PORT_TRACING_FUNC_EXIT(k_thread, foreach); |
| 68 | |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 69 | k_spin_unlock(&z_thread_monitor_lock, key); |
Ramakrishna Pallala | 110b8e4 | 2018-04-27 12:55:43 +0530 | [diff] [blame] | 70 | #endif |
Ramakrishna Pallala | e74d85d | 2018-07-18 13:50:52 +0530 | [diff] [blame] | 71 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 72 | |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame] | 73 | void k_thread_foreach_unlocked(k_thread_user_cb_t user_cb, void *user_data) |
| 74 | { |
| 75 | #if defined(CONFIG_THREAD_MONITOR) |
| 76 | struct k_thread *thread; |
| 77 | k_spinlock_key_t key; |
| 78 | |
| 79 | __ASSERT(user_cb != NULL, "user_cb can not be NULL"); |
| 80 | |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 81 | key = k_spin_lock(&z_thread_monitor_lock); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 82 | |
| 83 | SYS_PORT_TRACING_FUNC_ENTER(k_thread, foreach_unlocked); |
| 84 | |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame] | 85 | for (thread = _kernel.threads; thread; thread = thread->next_thread) { |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 86 | k_spin_unlock(&z_thread_monitor_lock, key); |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame] | 87 | user_cb(thread, user_data); |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 88 | key = k_spin_lock(&z_thread_monitor_lock); |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame] | 89 | } |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 90 | |
| 91 | SYS_PORT_TRACING_FUNC_EXIT(k_thread, foreach_unlocked); |
| 92 | |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 93 | k_spin_unlock(&z_thread_monitor_lock, key); |
Radoslaw Koppel | 2c529ce | 2019-11-27 14:20:37 +0100 | [diff] [blame] | 94 | #endif |
| 95 | } |
| 96 | |
Flavio Ceolin | 6a4a86e | 2018-12-17 12:40:22 -0800 | [diff] [blame] | 97 | bool k_is_in_isr(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 98 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 99 | return arch_is_in_isr(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Allan Stephens | 9f09777 | 2016-10-24 12:41:43 -0500 | [diff] [blame] | 102 | /* |
| 103 | * This function tags the current thread as essential to system operation. |
| 104 | * Exceptions raised by this thread will be treated as a fatal system error. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 105 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 106 | void z_thread_essential_set(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 107 | { |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 108 | _current->base.user_options |= K_ESSENTIAL; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Allan Stephens | 9f09777 | 2016-10-24 12:41:43 -0500 | [diff] [blame] | 111 | /* |
| 112 | * This function tags the current thread as not essential to system operation. |
| 113 | * Exceptions raised by this thread may be recoverable. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 114 | * (This is the default tag for a thread.) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 115 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 116 | void z_thread_essential_clear(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 117 | { |
Benjamin Walsh | ed240f2 | 2017-01-22 13:05:08 -0500 | [diff] [blame] | 118 | _current->base.user_options &= ~K_ESSENTIAL; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Allan Stephens | 9f09777 | 2016-10-24 12:41:43 -0500 | [diff] [blame] | 121 | /* |
| 122 | * This routine indicates if the current thread is an essential system thread. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 123 | * |
Flavio Ceolin | 09e362e | 2018-12-17 12:34:05 -0800 | [diff] [blame] | 124 | * Returns true if current thread is essential, false if it is not. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 125 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 126 | bool z_is_thread_essential(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 127 | { |
Flavio Ceolin | 09e362e | 2018-12-17 12:34:05 -0800 | [diff] [blame] | 128 | return (_current->base.user_options & K_ESSENTIAL) == K_ESSENTIAL; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 131 | #ifdef CONFIG_THREAD_CUSTOM_DATA |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 132 | void z_impl_k_thread_custom_data_set(void *value) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 133 | { |
| 134 | _current->custom_data = value; |
| 135 | } |
| 136 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 137 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 138 | static inline void z_vrfy_k_thread_custom_data_set(void *data) |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 139 | { |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 140 | z_impl_k_thread_custom_data_set(data); |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 141 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 142 | #include <syscalls/k_thread_custom_data_set_mrsh.c> |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 143 | #endif |
| 144 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 145 | void *z_impl_k_thread_custom_data_get(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 146 | { |
| 147 | return _current->custom_data; |
| 148 | } |
| 149 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 151 | static inline void *z_vrfy_k_thread_custom_data_get(void) |
| 152 | { |
| 153 | return z_impl_k_thread_custom_data_get(); |
| 154 | } |
| 155 | #include <syscalls/k_thread_custom_data_get_mrsh.c> |
| 156 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 157 | #endif /* CONFIG_USERSPACE */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 158 | #endif /* CONFIG_THREAD_CUSTOM_DATA */ |
| 159 | |
| 160 | #if defined(CONFIG_THREAD_MONITOR) |
Allan Stephens | 92e7504 | 2016-10-25 09:52:39 -0500 | [diff] [blame] | 161 | /* |
| 162 | * Remove a thread from the kernel's list of active threads. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 163 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 164 | void z_thread_monitor_exit(struct k_thread *thread) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 165 | { |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 166 | k_spinlock_key_t key = k_spin_lock(&z_thread_monitor_lock); |
Allan Stephens | 1be7bca | 2016-10-25 10:57:52 -0500 | [diff] [blame] | 167 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 168 | if (thread == _kernel.threads) { |
| 169 | _kernel.threads = _kernel.threads->next_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 170 | } else { |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 171 | struct k_thread *prev_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 172 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 173 | prev_thread = _kernel.threads; |
Flavio Ceolin | c806ac3 | 2018-09-17 16:03:52 -0700 | [diff] [blame] | 174 | while ((prev_thread != NULL) && |
| 175 | (thread != prev_thread->next_thread)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 176 | prev_thread = prev_thread->next_thread; |
| 177 | } |
Adithya Baglody | 10db82b | 2018-01-23 15:33:11 +0530 | [diff] [blame] | 178 | if (prev_thread != NULL) { |
| 179 | prev_thread->next_thread = thread->next_thread; |
| 180 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 181 | } |
Allan Stephens | 1be7bca | 2016-10-25 10:57:52 -0500 | [diff] [blame] | 182 | |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 183 | k_spin_unlock(&z_thread_monitor_lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 184 | } |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 185 | #endif |
| 186 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 187 | int z_impl_k_thread_name_set(struct k_thread *thread, const char *value) |
| 188 | { |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 189 | #ifdef CONFIG_THREAD_NAME |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 190 | if (thread == NULL) { |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 191 | thread = _current; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 192 | } |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 193 | |
Dominik Ermel | 8db0e5a | 2022-03-09 17:08:21 +0000 | [diff] [blame] | 194 | strncpy(thread->name, value, CONFIG_THREAD_MAX_NAME_LEN - 1); |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 195 | thread->name[CONFIG_THREAD_MAX_NAME_LEN - 1] = '\0'; |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 196 | |
| 197 | SYS_PORT_TRACING_OBJ_FUNC(k_thread, name_set, thread, 0); |
| 198 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 199 | return 0; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 200 | #else |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 201 | ARG_UNUSED(thread); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 202 | ARG_UNUSED(value); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 203 | |
| 204 | SYS_PORT_TRACING_OBJ_FUNC(k_thread, name_set, thread, -ENOSYS); |
| 205 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 206 | return -ENOSYS; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 207 | #endif /* CONFIG_THREAD_NAME */ |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 208 | } |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 209 | |
| 210 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 211 | static inline int z_vrfy_k_thread_name_set(struct k_thread *thread, const char *str) |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 212 | { |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 213 | #ifdef CONFIG_THREAD_NAME |
James Harris | 33c9be9 | 2021-03-08 09:23:49 -0800 | [diff] [blame] | 214 | char name[CONFIG_THREAD_MAX_NAME_LEN]; |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 215 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 216 | if (thread != NULL) { |
| 217 | if (Z_SYSCALL_OBJ(thread, K_OBJ_THREAD) != 0) { |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 218 | return -EINVAL; |
| 219 | } |
| 220 | } |
| 221 | |
James Harris | 33c9be9 | 2021-03-08 09:23:49 -0800 | [diff] [blame] | 222 | /* In theory we could copy directly into thread->name, but |
| 223 | * the current z_vrfy / z_impl split does not provide a |
| 224 | * means of doing so. |
| 225 | */ |
| 226 | if (z_user_string_copy(name, (char *)str, sizeof(name)) != 0) { |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 227 | return -EFAULT; |
| 228 | } |
| 229 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 230 | return z_impl_k_thread_name_set(thread, name); |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 231 | #else |
| 232 | return -ENOSYS; |
| 233 | #endif /* CONFIG_THREAD_NAME */ |
| 234 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 235 | #include <syscalls/k_thread_name_set_mrsh.c> |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 236 | #endif /* CONFIG_USERSPACE */ |
| 237 | |
| 238 | const char *k_thread_name_get(struct k_thread *thread) |
| 239 | { |
| 240 | #ifdef CONFIG_THREAD_NAME |
| 241 | return (const char *)thread->name; |
| 242 | #else |
| 243 | ARG_UNUSED(thread); |
| 244 | return NULL; |
| 245 | #endif /* CONFIG_THREAD_NAME */ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 246 | } |
| 247 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 248 | int z_impl_k_thread_name_copy(k_tid_t thread, char *buf, size_t size) |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 249 | { |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 250 | #ifdef CONFIG_THREAD_NAME |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 251 | strncpy(buf, thread->name, size); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 252 | return 0; |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 253 | #else |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 254 | ARG_UNUSED(thread); |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 255 | ARG_UNUSED(buf); |
| 256 | ARG_UNUSED(size); |
| 257 | return -ENOSYS; |
| 258 | #endif /* CONFIG_THREAD_NAME */ |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 259 | } |
| 260 | |
Peter Mitsis | a30cf39 | 2022-04-11 19:54:23 -0400 | [diff] [blame] | 261 | static size_t copy_bytes(char *dest, size_t dest_size, const char *src, size_t src_size) |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 262 | { |
Peter Mitsis | a30cf39 | 2022-04-11 19:54:23 -0400 | [diff] [blame] | 263 | size_t bytes_to_copy; |
| 264 | |
| 265 | bytes_to_copy = MIN(dest_size, src_size); |
| 266 | memcpy(dest, src, bytes_to_copy); |
| 267 | |
| 268 | return bytes_to_copy; |
| 269 | } |
| 270 | |
| 271 | const char *k_thread_state_str(k_tid_t thread_id, char *buf, size_t buf_size) |
| 272 | { |
| 273 | size_t off = 0; |
| 274 | uint8_t bit; |
| 275 | uint8_t thread_state = thread_id->base.thread_state; |
| 276 | static const char *states_str[8] = {"dummy", "pending", "prestart", |
| 277 | "dead", "suspended", "aborting", |
| 278 | "", "queued"}; |
| 279 | static const size_t states_sz[8] = {5, 7, 8, 4, 9, 8, 0, 6}; |
| 280 | |
| 281 | if ((buf == NULL) || (buf_size == 0)) { |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 282 | return ""; |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 283 | } |
Peter Mitsis | a30cf39 | 2022-04-11 19:54:23 -0400 | [diff] [blame] | 284 | |
| 285 | buf_size--; /* Reserve 1 byte for end-of-string character */ |
| 286 | |
| 287 | /* |
| 288 | * Loop through each bit in the thread_state. Stop once all have |
| 289 | * been processed. If more than one thread_state bit is set, then |
| 290 | * separate the descriptive strings with a '+'. |
| 291 | */ |
| 292 | |
| 293 | for (uint8_t index = 0; thread_state != 0; index++) { |
| 294 | bit = BIT(index); |
| 295 | if ((thread_state & bit) == 0) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | off += copy_bytes(buf + off, buf_size - off, |
| 300 | states_str[index], states_sz[index]); |
| 301 | |
| 302 | thread_state &= ~bit; |
| 303 | |
| 304 | if (thread_state != 0) { |
| 305 | off += copy_bytes(buf + off, buf_size - off, "+", 1); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | buf[off] = '\0'; |
| 310 | |
| 311 | return (const char *)buf; |
Pavlo Hamov | 8076c80 | 2019-07-31 12:43:54 +0300 | [diff] [blame] | 312 | } |
| 313 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 314 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 315 | static inline int z_vrfy_k_thread_name_copy(k_tid_t thread, |
| 316 | char *buf, size_t size) |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 317 | { |
| 318 | #ifdef CONFIG_THREAD_NAME |
| 319 | size_t len; |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 320 | struct z_object *ko = z_object_find(thread); |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 321 | |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 322 | /* Special case: we allow reading the names of initialized threads |
| 323 | * even if we don't have permission on them |
| 324 | */ |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 325 | if (thread == NULL || ko->type != K_OBJ_THREAD || |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 326 | (ko->flags & K_OBJ_FLAG_INITIALIZED) == 0) { |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | if (Z_SYSCALL_MEMORY_WRITE(buf, size) != 0) { |
| 330 | return -EFAULT; |
| 331 | } |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 332 | len = strlen(thread->name); |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 333 | if (len + 1 > size) { |
| 334 | return -ENOSPC; |
| 335 | } |
| 336 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 337 | return z_user_to_copy((void *)buf, thread->name, len + 1); |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 338 | #else |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 339 | ARG_UNUSED(thread); |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 340 | ARG_UNUSED(buf); |
| 341 | ARG_UNUSED(size); |
| 342 | return -ENOSYS; |
| 343 | #endif /* CONFIG_THREAD_NAME */ |
| 344 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 345 | #include <syscalls/k_thread_name_copy_mrsh.c> |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 346 | #endif /* CONFIG_USERSPACE */ |
| 347 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 348 | |
Ioannis Glaropoulos | dd574f6 | 2021-05-28 14:17:37 +0200 | [diff] [blame] | 349 | #ifdef CONFIG_MULTITHREADING |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 350 | #ifdef CONFIG_STACK_SENTINEL |
| 351 | /* Check that the stack sentinel is still present |
| 352 | * |
| 353 | * The stack sentinel feature writes a magic value to the lowest 4 bytes of |
| 354 | * the thread's stack when the thread is initialized. This value gets checked |
| 355 | * in a few places: |
| 356 | * |
| 357 | * 1) In k_yield() if the current thread is not swapped out |
Andrew Boie | ae1a75b | 2017-06-07 09:33:16 -0700 | [diff] [blame] | 358 | * 2) After servicing a non-nested interrupt |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 359 | * 3) In z_swap(), check the sentinel in the outgoing thread |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 360 | * |
Andrew Boie | ae1a75b | 2017-06-07 09:33:16 -0700 | [diff] [blame] | 361 | * Item 2 requires support in arch/ code. |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 362 | * |
| 363 | * If the check fails, the thread will be terminated appropriately through |
| 364 | * the system fatal error handler. |
| 365 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 366 | void z_check_stack_sentinel(void) |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 367 | { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 368 | uint32_t *stack; |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 369 | |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 370 | if ((_current->base.thread_state & _THREAD_DUMMY) != 0) { |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 374 | stack = (uint32_t *)_current->stack_info.start; |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 375 | if (*stack != STACK_SENTINEL) { |
| 376 | /* Restore it so further checks don't trigger this same error */ |
| 377 | *stack = STACK_SENTINEL; |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 378 | z_except_reason(K_ERR_STACK_CHK_FAIL); |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
Ioannis Glaropoulos | dd574f6 | 2021-05-28 14:17:37 +0200 | [diff] [blame] | 381 | #endif /* CONFIG_STACK_SENTINEL */ |
Andrew Boie | 5dcb279 | 2017-05-11 13:29:15 -0700 | [diff] [blame] | 382 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 383 | void z_impl_k_thread_start(struct k_thread *thread) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 384 | { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 385 | SYS_PORT_TRACING_OBJ_FUNC(k_thread, start, thread); |
| 386 | |
Andy Ross | 96ccc46 | 2020-01-23 13:28:30 -0800 | [diff] [blame] | 387 | z_sched_start(thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 388 | } |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 389 | |
| 390 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 391 | static inline void z_vrfy_k_thread_start(struct k_thread *thread) |
| 392 | { |
| 393 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
| 394 | return z_impl_k_thread_start(thread); |
| 395 | } |
| 396 | #include <syscalls/k_thread_start_mrsh.c> |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 397 | #endif |
Benjamin Walsh | 096d8e9 | 2016-12-16 16:45:05 -0500 | [diff] [blame] | 398 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 399 | |
Benjamin Walsh | 096d8e9 | 2016-12-16 16:45:05 -0500 | [diff] [blame] | 400 | #ifdef CONFIG_MULTITHREADING |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 401 | static void schedule_new_thread(struct k_thread *thread, k_timeout_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 402 | { |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 403 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 404 | if (K_TIMEOUT_EQ(delay, K_NO_WAIT)) { |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 405 | k_thread_start(thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 406 | } else { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 407 | z_add_thread_timeout(thread, delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 408 | } |
| 409 | #else |
| 410 | ARG_UNUSED(delay); |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 411 | k_thread_start(thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 412 | #endif |
| 413 | } |
Benjamin Walsh | 096d8e9 | 2016-12-16 16:45:05 -0500 | [diff] [blame] | 414 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 415 | |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 416 | #if CONFIG_STACK_POINTER_RANDOM |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 417 | int z_stack_adjust_initialized; |
| 418 | |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 419 | static size_t random_offset(size_t stack_size) |
Leandro Pereira | 1ccd715 | 2018-04-03 09:47:41 -0700 | [diff] [blame] | 420 | { |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 421 | size_t random_val; |
| 422 | |
| 423 | if (!z_stack_adjust_initialized) { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 424 | z_early_boot_rand_get((uint8_t *)&random_val, sizeof(random_val)); |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 425 | } else { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 426 | sys_rand_get((uint8_t *)&random_val, sizeof(random_val)); |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Andrew Boie | 61901cc | 2019-09-21 15:36:52 -0700 | [diff] [blame] | 429 | /* Don't need to worry about alignment of the size here, |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 430 | * arch_new_thread() is required to do it. |
Andrew Boie | 83752c1 | 2018-03-02 07:54:13 -0800 | [diff] [blame] | 431 | * |
| 432 | * FIXME: Not the best way to get a random number in a range. |
| 433 | * See #6493 |
| 434 | */ |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 435 | const size_t fuzz = random_val % CONFIG_STACK_POINTER_RANDOM; |
Leandro Pereira | 1ccd715 | 2018-04-03 09:47:41 -0700 | [diff] [blame] | 436 | |
| 437 | if (unlikely(fuzz * 2 > stack_size)) { |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 438 | return 0; |
Leandro Pereira | 1ccd715 | 2018-04-03 09:47:41 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 441 | return fuzz; |
Leandro Pereira | 1ccd715 | 2018-04-03 09:47:41 -0700 | [diff] [blame] | 442 | } |
Leandro Pereira | 1ccd715 | 2018-04-03 09:47:41 -0700 | [diff] [blame] | 443 | #if defined(CONFIG_STACK_GROWS_UP) |
| 444 | /* This is so rare not bothering for now */ |
| 445 | #error "Stack pointer randomization not implemented for upward growing stacks" |
| 446 | #endif /* CONFIG_STACK_GROWS_UP */ |
Andrew Boie | 83752c1 | 2018-03-02 07:54:13 -0800 | [diff] [blame] | 447 | #endif /* CONFIG_STACK_POINTER_RANDOM */ |
Leandro Pereira | 1ccd715 | 2018-04-03 09:47:41 -0700 | [diff] [blame] | 448 | |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 449 | static char *setup_thread_stack(struct k_thread *new_thread, |
| 450 | k_thread_stack_t *stack, size_t stack_size) |
| 451 | { |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 452 | size_t stack_obj_size, stack_buf_size; |
| 453 | char *stack_ptr, *stack_buf_start; |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 454 | size_t delta = 0; |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 455 | |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 456 | #ifdef CONFIG_USERSPACE |
| 457 | if (z_stack_is_user_capable(stack)) { |
| 458 | stack_obj_size = Z_THREAD_STACK_SIZE_ADJUST(stack_size); |
| 459 | stack_buf_start = Z_THREAD_STACK_BUFFER(stack); |
| 460 | stack_buf_size = stack_obj_size - K_THREAD_STACK_RESERVED; |
| 461 | } else |
| 462 | #endif |
| 463 | { |
| 464 | /* Object cannot host a user mode thread */ |
| 465 | stack_obj_size = Z_KERNEL_STACK_SIZE_ADJUST(stack_size); |
| 466 | stack_buf_start = Z_KERNEL_STACK_BUFFER(stack); |
| 467 | stack_buf_size = stack_obj_size - K_KERNEL_STACK_RESERVED; |
| 468 | } |
| 469 | |
| 470 | /* Initial stack pointer at the high end of the stack object, may |
| 471 | * be reduced later in this function by TLS or random offset |
| 472 | */ |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 473 | stack_ptr = (char *)stack + stack_obj_size; |
| 474 | |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 475 | LOG_DBG("stack %p for thread %p: obj_size=%zu buf_start=%p " |
| 476 | " buf_size %zu stack_ptr=%p", |
Krzysztof Chruscinski | bf39f54 | 2022-10-12 11:49:41 +0200 | [diff] [blame] | 477 | stack, new_thread, stack_obj_size, (void *)stack_buf_start, |
| 478 | stack_buf_size, (void *)stack_ptr); |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 479 | |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 480 | #ifdef CONFIG_INIT_STACKS |
| 481 | memset(stack_buf_start, 0xaa, stack_buf_size); |
| 482 | #endif |
| 483 | #ifdef CONFIG_STACK_SENTINEL |
| 484 | /* Put the stack sentinel at the lowest 4 bytes of the stack area. |
| 485 | * We periodically check that it's still present and kill the thread |
| 486 | * if it isn't. |
| 487 | */ |
| 488 | *((uint32_t *)stack_buf_start) = STACK_SENTINEL; |
| 489 | #endif /* CONFIG_STACK_SENTINEL */ |
Daniel Leung | 02b2035 | 2020-09-28 11:27:11 -0700 | [diff] [blame] | 490 | #ifdef CONFIG_THREAD_LOCAL_STORAGE |
Andrew Boie | 0e30c6a | 2020-10-24 13:04:04 -0700 | [diff] [blame] | 491 | /* TLS is always last within the stack buffer */ |
| 492 | delta += arch_tls_stack_setup(new_thread, stack_ptr); |
Daniel Leung | 02b2035 | 2020-09-28 11:27:11 -0700 | [diff] [blame] | 493 | #endif /* CONFIG_THREAD_LOCAL_STORAGE */ |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 494 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
| 495 | size_t tls_size = sizeof(struct _thread_userspace_local_data); |
| 496 | |
| 497 | /* reserve space on highest memory of stack buffer for local data */ |
| 498 | delta += tls_size; |
| 499 | new_thread->userspace_local_data = |
| 500 | (struct _thread_userspace_local_data *)(stack_ptr - delta); |
| 501 | #endif |
| 502 | #if CONFIG_STACK_POINTER_RANDOM |
| 503 | delta += random_offset(stack_buf_size); |
| 504 | #endif |
| 505 | delta = ROUND_UP(delta, ARCH_STACK_PTR_ALIGN); |
| 506 | #ifdef CONFIG_THREAD_STACK_INFO |
| 507 | /* Initial values. Arches which implement MPU guards that "borrow" |
| 508 | * memory from the stack buffer (not tracked in K_THREAD_STACK_RESERVED) |
| 509 | * will need to appropriately update this. |
| 510 | * |
| 511 | * The bounds tracked here correspond to the area of the stack object |
| 512 | * that the thread can access, which includes TLS. |
| 513 | */ |
| 514 | new_thread->stack_info.start = (uintptr_t)stack_buf_start; |
| 515 | new_thread->stack_info.size = stack_buf_size; |
| 516 | new_thread->stack_info.delta = delta; |
| 517 | #endif |
| 518 | stack_ptr -= delta; |
| 519 | |
| 520 | return stack_ptr; |
| 521 | } |
| 522 | |
Ioannis Glaropoulos | d69c2f8 | 2019-03-08 13:02:37 +0100 | [diff] [blame] | 523 | /* |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 524 | * The provided stack_size value is presumed to be either the result of |
| 525 | * K_THREAD_STACK_SIZEOF(stack), or the size value passed to the instance |
| 526 | * of K_THREAD_STACK_DEFINE() which defined 'stack'. |
Ioannis Glaropoulos | d69c2f8 | 2019-03-08 13:02:37 +0100 | [diff] [blame] | 527 | */ |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 528 | char *z_setup_new_thread(struct k_thread *new_thread, |
| 529 | k_thread_stack_t *stack, size_t stack_size, |
| 530 | k_thread_entry_t entry, |
| 531 | void *p1, void *p2, void *p3, |
| 532 | int prio, uint32_t options, const char *name) |
Leandro Pereira | 1ccd715 | 2018-04-03 09:47:41 -0700 | [diff] [blame] | 533 | { |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 534 | char *stack_ptr; |
| 535 | |
Andrew Boie | 1f6f977 | 2020-04-19 14:31:27 -0700 | [diff] [blame] | 536 | Z_ASSERT_VALID_PRIO(prio, entry); |
| 537 | |
Andrew Boie | f281b74 | 2019-07-30 14:02:54 -0700 | [diff] [blame] | 538 | #ifdef CONFIG_USERSPACE |
Flavio Ceolin | f6f951c | 2021-04-02 23:06:00 -0700 | [diff] [blame] | 539 | __ASSERT((options & K_USER) == 0U || z_stack_is_user_capable(stack), |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 540 | "user thread %p with kernel-only stack %p", |
| 541 | new_thread, stack); |
Andrew Boie | f281b74 | 2019-07-30 14:02:54 -0700 | [diff] [blame] | 542 | z_object_init(new_thread); |
| 543 | z_object_init(stack); |
| 544 | new_thread->stack_obj = stack; |
Andrew Boie | 378024c | 2020-05-28 11:48:54 -0700 | [diff] [blame] | 545 | new_thread->syscall_frame = NULL; |
Andrew Boie | f281b74 | 2019-07-30 14:02:54 -0700 | [diff] [blame] | 546 | |
| 547 | /* Any given thread has access to itself */ |
| 548 | k_object_access_grant(new_thread, new_thread); |
| 549 | #endif |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 550 | z_waitq_init(&new_thread->join_queue); |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 551 | |
Andrew Boie | c0df99c | 2020-04-19 14:28:15 -0700 | [diff] [blame] | 552 | /* Initialize various struct k_thread members */ |
| 553 | z_init_thread_base(&new_thread->base, prio, _THREAD_PRESTART, options); |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 554 | stack_ptr = setup_thread_stack(new_thread, stack, stack_size); |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 555 | |
Anas Nashif | 39f632e | 2020-12-07 13:15:42 -0500 | [diff] [blame] | 556 | #ifdef CONFIG_KERNEL_COHERENCE |
Andy Ross | a8d5437 | 2020-10-06 11:39:48 -0700 | [diff] [blame] | 557 | /* Check that the thread object is safe, but that the stack is |
| 558 | * still cached! |
| 559 | */ |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 560 | __ASSERT_NO_MSG(arch_mem_coherent(new_thread)); |
| 561 | __ASSERT_NO_MSG(!arch_mem_coherent(stack)); |
| 562 | #endif |
| 563 | |
Andrew Boie | b0c155f | 2020-04-23 13:55:56 -0700 | [diff] [blame] | 564 | arch_new_thread(new_thread, stack, stack_ptr, entry, p1, p2, p3); |
| 565 | |
Andrew Boie | c0df99c | 2020-04-19 14:28:15 -0700 | [diff] [blame] | 566 | /* static threads overwrite it afterwards with real value */ |
| 567 | new_thread->init_data = NULL; |
Daniel Leung | fc18243 | 2018-08-16 15:42:28 -0700 | [diff] [blame] | 568 | |
Andy Ross | a2f6826 | 2020-02-18 12:23:53 -0800 | [diff] [blame] | 569 | #ifdef CONFIG_USE_SWITCH |
| 570 | /* switch_handle must be non-null except when inside z_swap() |
| 571 | * for synchronization reasons. Historically some notional |
| 572 | * USE_SWITCH architectures have actually ignored the field |
| 573 | */ |
| 574 | __ASSERT(new_thread->switch_handle != NULL, |
| 575 | "arch layer failed to initialize switch_handle"); |
Andy Ross | 3235451 | 2020-01-17 09:32:36 -0800 | [diff] [blame] | 576 | #endif |
Andrew Boie | c0df99c | 2020-04-19 14:28:15 -0700 | [diff] [blame] | 577 | #ifdef CONFIG_THREAD_CUSTOM_DATA |
| 578 | /* Initialize custom data field (value is opaque to kernel) */ |
| 579 | new_thread->custom_data = NULL; |
| 580 | #endif |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 581 | #ifdef CONFIG_THREAD_MONITOR |
| 582 | new_thread->entry.pEntry = entry; |
| 583 | new_thread->entry.parameter1 = p1; |
| 584 | new_thread->entry.parameter2 = p2; |
| 585 | new_thread->entry.parameter3 = p3; |
| 586 | |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 587 | k_spinlock_key_t key = k_spin_lock(&z_thread_monitor_lock); |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 588 | |
| 589 | new_thread->next_thread = _kernel.threads; |
| 590 | _kernel.threads = new_thread; |
Andrew Boie | c4fbc08 | 2020-02-14 10:11:35 -0800 | [diff] [blame] | 591 | k_spin_unlock(&z_thread_monitor_lock, key); |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 592 | #endif |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 593 | #ifdef CONFIG_THREAD_NAME |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 594 | if (name != NULL) { |
| 595 | strncpy(new_thread->name, name, |
| 596 | CONFIG_THREAD_MAX_NAME_LEN - 1); |
| 597 | /* Ensure NULL termination, truncate if longer */ |
| 598 | new_thread->name[CONFIG_THREAD_MAX_NAME_LEN - 1] = '\0'; |
Andrew Boie | c0df99c | 2020-04-19 14:28:15 -0700 | [diff] [blame] | 599 | } else { |
| 600 | new_thread->name[0] = '\0'; |
Andrew Boie | 38129ce | 2019-06-25 08:54:37 -0700 | [diff] [blame] | 601 | } |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 602 | #endif |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 603 | #ifdef CONFIG_SCHED_CPU_MASK |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 604 | if (IS_ENABLED(CONFIG_SCHED_CPU_MASK_PIN_ONLY)) { |
| 605 | new_thread->base.cpu_mask = 1; /* must specify only one cpu */ |
| 606 | } else { |
| 607 | new_thread->base.cpu_mask = -1; /* allow all cpus */ |
| 608 | } |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 609 | #endif |
Andrew Boie | a7fedb7 | 2017-11-13 14:12:23 -0800 | [diff] [blame] | 610 | #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN |
| 611 | /* _current may be null if the dummy thread is not used */ |
| 612 | if (!_current) { |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 613 | new_thread->resource_pool = NULL; |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 614 | return stack_ptr; |
Andrew Boie | a7fedb7 | 2017-11-13 14:12:23 -0800 | [diff] [blame] | 615 | } |
| 616 | #endif |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 617 | #ifdef CONFIG_USERSPACE |
Andrew Boie | b5a71f7 | 2020-10-06 13:39:29 -0700 | [diff] [blame] | 618 | z_mem_domain_init_thread(new_thread); |
Andrew Boie | 0bf9d33 | 2017-10-16 09:12:47 -0700 | [diff] [blame] | 619 | |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 620 | if ((options & K_INHERIT_PERMS) != 0U) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 621 | z_thread_perms_inherit(_current, new_thread); |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 622 | } |
Andrew Boie | 2acfcd6 | 2017-08-30 14:31:03 -0700 | [diff] [blame] | 623 | #endif |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 624 | #ifdef CONFIG_SCHED_DEADLINE |
| 625 | new_thread->base.prio_deadline = 0; |
| 626 | #endif |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 627 | new_thread->resource_pool = _current->resource_pool; |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 628 | |
Peter Mitsis | 0ebd6c7 | 2021-12-15 09:46:52 -0500 | [diff] [blame] | 629 | #ifdef CONFIG_SCHED_THREAD_USAGE |
| 630 | new_thread->base.usage = (struct k_cycle_stats) {}; |
| 631 | new_thread->base.usage.track_usage = |
| 632 | CONFIG_SCHED_THREAD_USAGE_AUTO_ENABLE; |
| 633 | #endif |
| 634 | |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 635 | SYS_PORT_TRACING_OBJ_FUNC(k_thread, create, new_thread); |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 636 | |
| 637 | return stack_ptr; |
Andrew Boie | 2acfcd6 | 2017-08-30 14:31:03 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | #ifdef CONFIG_MULTITHREADING |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 641 | k_tid_t z_impl_k_thread_create(struct k_thread *new_thread, |
Andrew Boie | c5c104f | 2017-10-16 14:46:34 -0700 | [diff] [blame] | 642 | k_thread_stack_t *stack, |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 643 | size_t stack_size, k_thread_entry_t entry, |
| 644 | void *p1, void *p2, void *p3, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 645 | int prio, uint32_t options, k_timeout_t delay) |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 646 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 647 | __ASSERT(!arch_is_in_isr(), "Threads may not be created in ISRs"); |
Anas Nashif | b6304e6 | 2018-07-04 08:03:03 -0500 | [diff] [blame] | 648 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 649 | z_setup_new_thread(new_thread, stack, stack_size, entry, p1, p2, p3, |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 650 | prio, options, NULL); |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 651 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 652 | if (!K_TIMEOUT_EQ(delay, K_FOREVER)) { |
Andrew Boie | 7d627c5 | 2017-08-30 11:01:56 -0700 | [diff] [blame] | 653 | schedule_new_thread(new_thread, delay); |
| 654 | } |
Anas Nashif | b6304e6 | 2018-07-04 08:03:03 -0500 | [diff] [blame] | 655 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 656 | return new_thread; |
| 657 | } |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 658 | |
| 659 | |
| 660 | #ifdef CONFIG_USERSPACE |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 661 | bool z_stack_is_user_capable(k_thread_stack_t *stack) |
| 662 | { |
| 663 | return z_object_find(stack) != NULL; |
| 664 | } |
| 665 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 666 | k_tid_t z_vrfy_k_thread_create(struct k_thread *new_thread, |
| 667 | k_thread_stack_t *stack, |
| 668 | size_t stack_size, k_thread_entry_t entry, |
| 669 | void *p1, void *p2, void *p3, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 670 | int prio, uint32_t options, k_timeout_t delay) |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 671 | { |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 672 | size_t total_size, stack_obj_size; |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 673 | struct z_object *stack_object; |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 674 | |
| 675 | /* The thread and stack objects *must* be in an uninitialized state */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 676 | Z_OOPS(Z_SYSCALL_OBJ_NEVER_INIT(new_thread, K_OBJ_THREAD)); |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 677 | |
| 678 | /* No need to check z_stack_is_user_capable(), it won't be in the |
| 679 | * object table if it isn't |
| 680 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 681 | stack_object = z_object_find(stack); |
| 682 | Z_OOPS(Z_SYSCALL_VERIFY_MSG(z_obj_validation_check(stack_object, stack, |
Andrew Boie | 4bad34e | 2020-03-11 06:56:58 -0700 | [diff] [blame] | 683 | K_OBJ_THREAD_STACK_ELEMENT, |
Flavio Ceolin | 92ea2f9 | 2018-09-20 16:14:57 -0700 | [diff] [blame] | 684 | _OBJ_INIT_FALSE) == 0, |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 685 | "bad stack object")); |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 686 | |
| 687 | /* Verify that the stack size passed in is OK by computing the total |
| 688 | * size and comparing it with the size value in the object metadata |
| 689 | */ |
Andrew Boie | b5c6810 | 2019-11-21 17:38:17 -0800 | [diff] [blame] | 690 | Z_OOPS(Z_SYSCALL_VERIFY_MSG(!size_add_overflow(K_THREAD_STACK_RESERVED, |
| 691 | stack_size, &total_size), |
| 692 | "stack size overflow (%zu+%zu)", |
| 693 | stack_size, |
Andrew Boie | d0035f9 | 2019-03-19 10:43:06 -0700 | [diff] [blame] | 694 | K_THREAD_STACK_RESERVED)); |
| 695 | |
Andrew Boie | f4631d5 | 2019-03-19 10:48:09 -0700 | [diff] [blame] | 696 | /* Testing less-than-or-equal since additional room may have been |
| 697 | * allocated for alignment constraints |
| 698 | */ |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 699 | #ifdef CONFIG_GEN_PRIV_STACKS |
| 700 | stack_obj_size = stack_object->data.stack_data->size; |
| 701 | #else |
| 702 | stack_obj_size = stack_object->data.stack_size; |
| 703 | #endif |
| 704 | Z_OOPS(Z_SYSCALL_VERIFY_MSG(total_size <= stack_obj_size, |
Andrew Boie | f2734ab | 2020-03-11 06:37:42 -0700 | [diff] [blame] | 705 | "stack size %zu is too big, max is %zu", |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 706 | total_size, stack_obj_size)); |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 707 | |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 708 | /* User threads may only create other user threads and they can't |
| 709 | * be marked as essential |
| 710 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 711 | Z_OOPS(Z_SYSCALL_VERIFY(options & K_USER)); |
| 712 | Z_OOPS(Z_SYSCALL_VERIFY(!(options & K_ESSENTIAL))); |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 713 | |
| 714 | /* Check validity of prio argument; must be the same or worse priority |
| 715 | * than the caller |
| 716 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 717 | Z_OOPS(Z_SYSCALL_VERIFY(_is_valid_prio(prio, NULL))); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 718 | Z_OOPS(Z_SYSCALL_VERIFY(z_is_prio_lower_or_equal(prio, |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 719 | _current->base.prio))); |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 720 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 721 | z_setup_new_thread(new_thread, stack, stack_size, |
| 722 | entry, p1, p2, p3, prio, options, NULL); |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 723 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 724 | if (!K_TIMEOUT_EQ(delay, K_FOREVER)) { |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 725 | schedule_new_thread(new_thread, delay); |
| 726 | } |
| 727 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 728 | return new_thread; |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 729 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 730 | #include <syscalls/k_thread_create_mrsh.c> |
Andrew Boie | 662c345 | 2017-10-02 10:51:18 -0700 | [diff] [blame] | 731 | #endif /* CONFIG_USERSPACE */ |
| 732 | #endif /* CONFIG_MULTITHREADING */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 733 | |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 734 | #ifdef CONFIG_MULTITHREADING |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 735 | #ifdef CONFIG_USERSPACE |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 736 | |
| 737 | static void grant_static_access(void) |
| 738 | { |
Fabio Baltieri | f88a420 | 2021-08-04 23:05:54 +0100 | [diff] [blame] | 739 | STRUCT_SECTION_FOREACH(z_object_assignment, pos) { |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 740 | for (int i = 0; pos->objects[i] != NULL; i++) { |
| 741 | k_object_access_grant(pos->objects[i], |
| 742 | pos->thread); |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | #endif /* CONFIG_USERSPACE */ |
| 747 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 748 | void z_init_static_threads(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 749 | { |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 750 | _FOREACH_STATIC_THREAD(thread_data) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 751 | z_setup_new_thread( |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 752 | thread_data->init_thread, |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 753 | thread_data->init_stack, |
| 754 | thread_data->init_stack_size, |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 755 | thread_data->init_entry, |
| 756 | thread_data->init_p1, |
| 757 | thread_data->init_p2, |
| 758 | thread_data->init_p3, |
| 759 | thread_data->init_prio, |
Anas Nashif | 5755405 | 2018-03-03 02:31:05 -0600 | [diff] [blame] | 760 | thread_data->init_options, |
| 761 | thread_data->init_name); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 762 | |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 763 | thread_data->init_thread->init_data = thread_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 764 | } |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 765 | |
Andrew Boie | 877f82e | 2017-10-17 11:20:22 -0700 | [diff] [blame] | 766 | #ifdef CONFIG_USERSPACE |
| 767 | grant_static_access(); |
| 768 | #endif |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 769 | |
| 770 | /* |
Andy Ross | e456d0f | 2018-07-25 10:46:38 -0700 | [diff] [blame] | 771 | * Non-legacy static threads may be started immediately or |
| 772 | * after a previously specified delay. Even though the |
| 773 | * scheduler is locked, ticks can still be delivered and |
| 774 | * processed. Take a sched lock to prevent them from running |
| 775 | * until they are all started. |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 776 | * |
| 777 | * Note that static threads defined using the legacy API have a |
| 778 | * delay of K_FOREVER. |
| 779 | */ |
Andy Ross | e456d0f | 2018-07-25 10:46:38 -0700 | [diff] [blame] | 780 | k_sched_lock(); |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 781 | _FOREACH_STATIC_THREAD(thread_data) { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 782 | if (thread_data->init_delay != K_TICKS_FOREVER) { |
Andrew Boie | d26cf2d | 2017-03-30 13:07:02 -0700 | [diff] [blame] | 783 | schedule_new_thread(thread_data->init_thread, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 784 | K_MSEC(thread_data->init_delay)); |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 785 | } |
| 786 | } |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 787 | k_sched_unlock(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 788 | } |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 789 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 790 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 791 | void z_init_thread_base(struct _thread_base *thread_base, int priority, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 792 | uint32_t initial_state, unsigned int options) |
Benjamin Walsh | 069fd36 | 2016-11-22 17:48:13 -0500 | [diff] [blame] | 793 | { |
| 794 | /* k_q_node is initialized upon first insertion in a list */ |
Andy Ross | 8e16012 | 2021-02-18 17:38:07 -0800 | [diff] [blame] | 795 | thread_base->pended_on = NULL; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 796 | thread_base->user_options = (uint8_t)options; |
| 797 | thread_base->thread_state = (uint8_t)initial_state; |
Benjamin Walsh | 069fd36 | 2016-11-22 17:48:13 -0500 | [diff] [blame] | 798 | |
| 799 | thread_base->prio = priority; |
| 800 | |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 801 | thread_base->sched_locked = 0U; |
Benjamin Walsh | 069fd36 | 2016-11-22 17:48:13 -0500 | [diff] [blame] | 802 | |
Andy Ross | 6c283ca | 2019-08-16 22:09:30 -0700 | [diff] [blame] | 803 | #ifdef CONFIG_SMP |
| 804 | thread_base->is_idle = 0; |
| 805 | #endif |
| 806 | |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 807 | #ifdef CONFIG_TIMESLICE_PER_THREAD |
| 808 | thread_base->slice_ticks = 0; |
| 809 | thread_base->slice_expired = NULL; |
| 810 | #endif |
| 811 | |
Benjamin Walsh | 069fd36 | 2016-11-22 17:48:13 -0500 | [diff] [blame] | 812 | /* swap_data does not need to be initialized */ |
| 813 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 814 | z_init_thread_timeout(thread_base); |
Benjamin Walsh | 069fd36 | 2016-11-22 17:48:13 -0500 | [diff] [blame] | 815 | } |
| 816 | |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 817 | FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, |
| 818 | void *p1, void *p2, void *p3) |
| 819 | { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 820 | SYS_PORT_TRACING_FUNC(k_thread, user_mode_enter); |
| 821 | |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 822 | _current->base.user_options |= K_USER; |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 823 | z_thread_essential_clear(); |
Andrew Boie | 2dd91ec | 2018-06-06 08:45:01 -0700 | [diff] [blame] | 824 | #ifdef CONFIG_THREAD_MONITOR |
| 825 | _current->entry.pEntry = entry; |
| 826 | _current->entry.parameter1 = p1; |
| 827 | _current->entry.parameter2 = p2; |
| 828 | _current->entry.parameter3 = p3; |
| 829 | #endif |
Andrew Boie | 93eb603 | 2017-09-29 04:42:30 -0700 | [diff] [blame] | 830 | #ifdef CONFIG_USERSPACE |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 831 | __ASSERT(z_stack_is_user_capable(_current->stack_obj), |
| 832 | "dropping to user mode with kernel-only stack object"); |
Daniel Leung | 62cf196 | 2020-10-05 14:54:45 -0700 | [diff] [blame] | 833 | #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA |
Andrew Boie | 743ff98 | 2020-05-08 14:52:12 -0700 | [diff] [blame] | 834 | memset(_current->userspace_local_data, 0, |
| 835 | sizeof(struct _thread_userspace_local_data)); |
Daniel Leung | 62cf196 | 2020-10-05 14:54:45 -0700 | [diff] [blame] | 836 | #endif |
Andrew Boie | 0e30c6a | 2020-10-24 13:04:04 -0700 | [diff] [blame] | 837 | #ifdef CONFIG_THREAD_LOCAL_STORAGE |
| 838 | arch_tls_stack_setup(_current, |
| 839 | (char *)(_current->stack_info.start + |
| 840 | _current->stack_info.size)); |
| 841 | #endif |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 842 | arch_user_mode_enter(entry, p1, p2, p3); |
Andrew Boie | 93eb603 | 2017-09-29 04:42:30 -0700 | [diff] [blame] | 843 | #else |
| 844 | /* XXX In this case we do not reset the stack */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 845 | z_thread_entry(entry, p1, p2, p3); |
Andrew Boie | 93eb603 | 2017-09-29 04:42:30 -0700 | [diff] [blame] | 846 | #endif |
Andrew Boie | 3f091b5 | 2017-08-30 14:34:14 -0700 | [diff] [blame] | 847 | } |
Andy Ross | 5aa7460 | 2019-02-05 09:35:57 -0800 | [diff] [blame] | 848 | |
| 849 | /* These spinlock assertion predicates are defined here because having |
| 850 | * them in spinlock.h is a giant header ordering headache. |
| 851 | */ |
Danny Oerndrup | c9d7840 | 2019-12-13 11:24:56 +0100 | [diff] [blame] | 852 | #ifdef CONFIG_SPIN_VALIDATE |
Flavio Ceolin | 625ac2e | 2019-03-14 11:41:21 -0700 | [diff] [blame] | 853 | bool z_spin_lock_valid(struct k_spinlock *l) |
Andy Ross | 5aa7460 | 2019-02-05 09:35:57 -0800 | [diff] [blame] | 854 | { |
Jim Shu | e124670 | 2019-09-17 13:39:17 +0800 | [diff] [blame] | 855 | uintptr_t thread_cpu = l->thread_cpu; |
| 856 | |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 857 | if (thread_cpu != 0U) { |
Aastha Grover | 83b9f69 | 2020-08-20 16:47:11 -0700 | [diff] [blame] | 858 | if ((thread_cpu & 3U) == _current_cpu->id) { |
Flavio Ceolin | 625ac2e | 2019-03-14 11:41:21 -0700 | [diff] [blame] | 859 | return false; |
Andy Ross | 5aa7460 | 2019-02-05 09:35:57 -0800 | [diff] [blame] | 860 | } |
| 861 | } |
Flavio Ceolin | 625ac2e | 2019-03-14 11:41:21 -0700 | [diff] [blame] | 862 | return true; |
Andy Ross | 5aa7460 | 2019-02-05 09:35:57 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Flavio Ceolin | 625ac2e | 2019-03-14 11:41:21 -0700 | [diff] [blame] | 865 | bool z_spin_unlock_valid(struct k_spinlock *l) |
Andy Ross | 5aa7460 | 2019-02-05 09:35:57 -0800 | [diff] [blame] | 866 | { |
Nicolas Pitre | 0b5d9f7 | 2019-05-20 23:41:27 -0400 | [diff] [blame] | 867 | if (l->thread_cpu != (_current_cpu->id | (uintptr_t)_current)) { |
Flavio Ceolin | 625ac2e | 2019-03-14 11:41:21 -0700 | [diff] [blame] | 868 | return false; |
Andy Ross | 5aa7460 | 2019-02-05 09:35:57 -0800 | [diff] [blame] | 869 | } |
| 870 | l->thread_cpu = 0; |
Flavio Ceolin | 625ac2e | 2019-03-14 11:41:21 -0700 | [diff] [blame] | 871 | return true; |
Andy Ross | 5aa7460 | 2019-02-05 09:35:57 -0800 | [diff] [blame] | 872 | } |
Andy Ross | f37e0c6 | 2019-02-20 10:11:24 -0800 | [diff] [blame] | 873 | |
| 874 | void z_spin_lock_set_owner(struct k_spinlock *l) |
| 875 | { |
Nicolas Pitre | 0b5d9f7 | 2019-05-20 23:41:27 -0400 | [diff] [blame] | 876 | l->thread_cpu = _current_cpu->id | (uintptr_t)_current; |
Andy Ross | f37e0c6 | 2019-02-20 10:11:24 -0800 | [diff] [blame] | 877 | } |
Daniel Leung | d1495e9 | 2021-02-02 16:35:15 -0800 | [diff] [blame] | 878 | |
| 879 | #ifdef CONFIG_KERNEL_COHERENCE |
| 880 | bool z_spin_lock_mem_coherent(struct k_spinlock *l) |
| 881 | { |
| 882 | return arch_mem_coherent((void *)l); |
| 883 | } |
| 884 | #endif /* CONFIG_KERNEL_COHERENCE */ |
| 885 | |
Danny Oerndrup | c9d7840 | 2019-12-13 11:24:56 +0100 | [diff] [blame] | 886 | #endif /* CONFIG_SPIN_VALIDATE */ |
Ioannis Glaropoulos | 0e67759 | 2019-07-10 05:25:39 +0200 | [diff] [blame] | 887 | |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 888 | int z_impl_k_float_disable(struct k_thread *thread) |
| 889 | { |
Stephanos Ioannidis | aaf9320 | 2020-05-03 18:03:19 +0900 | [diff] [blame] | 890 | #if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING) |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 891 | return arch_float_disable(thread); |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 892 | #else |
Katsuhiro Suzuki | 19db485 | 2021-03-24 01:54:15 +0900 | [diff] [blame] | 893 | return -ENOTSUP; |
Stephanos Ioannidis | aaf9320 | 2020-05-03 18:03:19 +0900 | [diff] [blame] | 894 | #endif /* CONFIG_FPU && CONFIG_FPU_SHARING */ |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Katsuhiro Suzuki | 59903e2 | 2021-02-01 15:16:53 +0900 | [diff] [blame] | 897 | int z_impl_k_float_enable(struct k_thread *thread, unsigned int options) |
| 898 | { |
| 899 | #if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING) |
| 900 | return arch_float_enable(thread, options); |
| 901 | #else |
| 902 | return -ENOTSUP; |
| 903 | #endif /* CONFIG_FPU && CONFIG_FPU_SHARING */ |
| 904 | } |
| 905 | |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 906 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 907 | static inline int z_vrfy_k_float_disable(struct k_thread *thread) |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 908 | { |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 909 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 910 | return z_impl_k_float_disable(thread); |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 911 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 912 | #include <syscalls/k_float_disable_mrsh.c> |
Ioannis Glaropoulos | a6cb8b0 | 2019-05-09 21:55:10 +0200 | [diff] [blame] | 913 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | e09a025 | 2019-11-06 14:17:17 -0800 | [diff] [blame] | 914 | |
| 915 | #ifdef CONFIG_IRQ_OFFLOAD |
Enjia Mai | 00d7f2c | 2020-12-09 11:08:34 +0800 | [diff] [blame] | 916 | /* Make offload_sem visible outside under testing, in order to release |
| 917 | * it outside when error happened. |
| 918 | */ |
| 919 | K_SEM_DEFINE(offload_sem, 1, 1); |
Andrew Boie | e09a025 | 2019-11-06 14:17:17 -0800 | [diff] [blame] | 920 | |
Tomasz Bursztyka | ae5a761 | 2020-07-10 11:38:48 +0200 | [diff] [blame] | 921 | void irq_offload(irq_offload_routine_t routine, const void *parameter) |
Andrew Boie | e09a025 | 2019-11-06 14:17:17 -0800 | [diff] [blame] | 922 | { |
Andy Ross | 73453a3 | 2022-02-14 14:30:34 -0800 | [diff] [blame] | 923 | #ifdef CONFIG_IRQ_OFFLOAD_NESTED |
| 924 | arch_irq_offload(routine, parameter); |
| 925 | #else |
Andrew Boie | e09a025 | 2019-11-06 14:17:17 -0800 | [diff] [blame] | 926 | k_sem_take(&offload_sem, K_FOREVER); |
| 927 | arch_irq_offload(routine, parameter); |
| 928 | k_sem_give(&offload_sem); |
Andy Ross | 73453a3 | 2022-02-14 14:30:34 -0800 | [diff] [blame] | 929 | #endif |
Andrew Boie | e09a025 | 2019-11-06 14:17:17 -0800 | [diff] [blame] | 930 | } |
| 931 | #endif |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 932 | |
| 933 | #if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO) |
| 934 | #ifdef CONFIG_STACK_GROWS_UP |
| 935 | #error "Unsupported configuration for stack analysis" |
| 936 | #endif |
| 937 | |
Krzysztof Chruscinski | 1da97e1 | 2022-01-28 15:40:37 +0100 | [diff] [blame] | 938 | int z_stack_space_get(const uint8_t *stack_start, size_t size, size_t *unused_ptr) |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 939 | { |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 940 | size_t unused = 0; |
Krzysztof Chruscinski | 1da97e1 | 2022-01-28 15:40:37 +0100 | [diff] [blame] | 941 | const uint8_t *checked_stack = stack_start; |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 942 | /* Take the address of any local variable as a shallow bound for the |
| 943 | * stack pointer. Addresses above it are guaranteed to be |
| 944 | * accessible. |
| 945 | */ |
Krzysztof Chruscinski | 1da97e1 | 2022-01-28 15:40:37 +0100 | [diff] [blame] | 946 | const uint8_t *stack_pointer = (const uint8_t *)&stack_start; |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 947 | |
| 948 | /* If we are currently running on the stack being analyzed, some |
| 949 | * memory management hardware will generate an exception if we |
| 950 | * read unused stack memory. |
| 951 | * |
| 952 | * This never happens when invoked from user mode, as user mode |
| 953 | * will always run this function on the privilege elevation stack. |
| 954 | */ |
Krzysztof Chruscinski | 1da97e1 | 2022-01-28 15:40:37 +0100 | [diff] [blame] | 955 | if ((stack_pointer > stack_start) && (stack_pointer <= (stack_start + size)) && |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 956 | IS_ENABLED(CONFIG_NO_UNUSED_STACK_INSPECTION)) { |
| 957 | /* TODO: We could add an arch_ API call to temporarily |
| 958 | * disable the stack checking in the CPU, but this would |
| 959 | * need to be properly managed wrt context switches/interrupts |
| 960 | */ |
| 961 | return -ENOTSUP; |
| 962 | } |
| 963 | |
| 964 | if (IS_ENABLED(CONFIG_STACK_SENTINEL)) { |
| 965 | /* First 4 bytes of the stack buffer reserved for the |
| 966 | * sentinel value, it won't be 0xAAAAAAAA for thread |
| 967 | * stacks. |
| 968 | * |
| 969 | * FIXME: thread->stack_info.start ought to reflect |
| 970 | * this! |
| 971 | */ |
| 972 | checked_stack += 4; |
| 973 | size -= 4; |
| 974 | } |
| 975 | |
| 976 | for (size_t i = 0; i < size; i++) { |
| 977 | if ((checked_stack[i]) == 0xaaU) { |
| 978 | unused++; |
| 979 | } else { |
| 980 | break; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | *unused_ptr = unused; |
| 985 | |
| 986 | return 0; |
| 987 | } |
| 988 | |
Krzysztof Chruscinski | 1da97e1 | 2022-01-28 15:40:37 +0100 | [diff] [blame] | 989 | int z_impl_k_thread_stack_space_get(const struct k_thread *thread, |
| 990 | size_t *unused_ptr) |
| 991 | { |
| 992 | return z_stack_space_get((const uint8_t *)thread->stack_info.start, |
| 993 | thread->stack_info.size, unused_ptr); |
| 994 | } |
| 995 | |
Andrew Boie | efc5fe0 | 2020-02-05 10:41:58 -0800 | [diff] [blame] | 996 | #ifdef CONFIG_USERSPACE |
| 997 | int z_vrfy_k_thread_stack_space_get(const struct k_thread *thread, |
| 998 | size_t *unused_ptr) |
| 999 | { |
| 1000 | size_t unused; |
| 1001 | int ret; |
| 1002 | |
| 1003 | ret = Z_SYSCALL_OBJ(thread, K_OBJ_THREAD); |
| 1004 | CHECKIF(ret != 0) { |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | ret = z_impl_k_thread_stack_space_get(thread, &unused); |
| 1009 | CHECKIF(ret != 0) { |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | ret = z_user_to_copy(unused_ptr, &unused, sizeof(size_t)); |
| 1014 | CHECKIF(ret != 0) { |
| 1015 | return ret; |
| 1016 | } |
| 1017 | |
| 1018 | return 0; |
| 1019 | } |
| 1020 | #include <syscalls/k_thread_stack_space_get_mrsh.c> |
| 1021 | #endif /* CONFIG_USERSPACE */ |
| 1022 | #endif /* CONFIG_INIT_STACKS && CONFIG_THREAD_STACK_INFO */ |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1023 | |
| 1024 | #ifdef CONFIG_USERSPACE |
| 1025 | static inline k_ticks_t z_vrfy_k_thread_timeout_remaining_ticks( |
Peter Bigot | 0ab314f | 2020-11-16 15:28:59 -0600 | [diff] [blame] | 1026 | const struct k_thread *t) |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1027 | { |
| 1028 | Z_OOPS(Z_SYSCALL_OBJ(t, K_OBJ_THREAD)); |
| 1029 | return z_impl_k_thread_timeout_remaining_ticks(t); |
| 1030 | } |
| 1031 | #include <syscalls/k_thread_timeout_remaining_ticks_mrsh.c> |
| 1032 | |
| 1033 | static inline k_ticks_t z_vrfy_k_thread_timeout_expires_ticks( |
Peter Bigot | 0ab314f | 2020-11-16 15:28:59 -0600 | [diff] [blame] | 1034 | const struct k_thread *t) |
Andy Ross | 5a5d3da | 2020-03-09 13:59:15 -0700 | [diff] [blame] | 1035 | { |
| 1036 | Z_OOPS(Z_SYSCALL_OBJ(t, K_OBJ_THREAD)); |
| 1037 | return z_impl_k_thread_timeout_expires_ticks(t); |
| 1038 | } |
| 1039 | #include <syscalls/k_thread_timeout_expires_ticks_mrsh.c> |
| 1040 | #endif |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1041 | |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 1042 | #ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1043 | void z_thread_mark_switched_in(void) |
| 1044 | { |
Andy Ross | 4ae3250 | 2021-09-28 07:59:42 -0700 | [diff] [blame] | 1045 | #if defined(CONFIG_SCHED_THREAD_USAGE) && !defined(CONFIG_USE_SWITCH) |
| 1046 | z_sched_usage_start(_current); |
| 1047 | #endif |
| 1048 | |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 1049 | #ifdef CONFIG_TRACING |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1050 | SYS_PORT_TRACING_FUNC(k_thread, switched_in); |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 1051 | #endif |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | void z_thread_mark_switched_out(void) |
| 1055 | { |
Andy Ross | 4ae3250 | 2021-09-28 07:59:42 -0700 | [diff] [blame] | 1056 | #if defined(CONFIG_SCHED_THREAD_USAGE) && !defined(CONFIG_USE_SWITCH) |
| 1057 | z_sched_usage_stop(); |
| 1058 | #endif |
| 1059 | |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 1060 | #ifdef CONFIG_TRACING |
Keith Packard | 95cec04 | 2022-10-18 15:38:21 -0700 | [diff] [blame] | 1061 | #ifdef CONFIG_THREAD_LOCAL_STORAGE |
| 1062 | /* Dummy thread won't have TLS set up to run arbitrary code */ |
| 1063 | if (!_current_cpu->current || |
| 1064 | (_current_cpu->current->base.thread_state & _THREAD_DUMMY) != 0) |
| 1065 | return; |
| 1066 | #endif |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1067 | SYS_PORT_TRACING_FUNC(k_thread, switched_out); |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 1068 | #endif |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1069 | } |
Andy Ross | f169c5b | 2021-09-28 10:01:06 -0700 | [diff] [blame] | 1070 | #endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */ |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1071 | |
| 1072 | int k_thread_runtime_stats_get(k_tid_t thread, |
| 1073 | k_thread_runtime_stats_t *stats) |
| 1074 | { |
| 1075 | if ((thread == NULL) || (stats == NULL)) { |
| 1076 | return -EINVAL; |
| 1077 | } |
| 1078 | |
Andy Ross | f169c5b | 2021-09-28 10:01:06 -0700 | [diff] [blame] | 1079 | #ifdef CONFIG_SCHED_THREAD_USAGE |
Peter Mitsis | 5deaffb | 2021-12-14 10:56:14 -0500 | [diff] [blame] | 1080 | z_sched_thread_usage(thread, stats); |
| 1081 | #else |
| 1082 | *stats = (k_thread_runtime_stats_t) {}; |
Andy Ross | f169c5b | 2021-09-28 10:01:06 -0700 | [diff] [blame] | 1083 | #endif |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats) |
| 1089 | { |
Peter Mitsis | 4eb1dd0 | 2021-12-14 22:26:22 -0500 | [diff] [blame] | 1090 | #ifdef CONFIG_SCHED_THREAD_USAGE_ALL |
| 1091 | k_thread_runtime_stats_t tmp_stats; |
| 1092 | #endif |
| 1093 | |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1094 | if (stats == NULL) { |
| 1095 | return -EINVAL; |
| 1096 | } |
| 1097 | |
Andy Ross | f169c5b | 2021-09-28 10:01:06 -0700 | [diff] [blame] | 1098 | *stats = (k_thread_runtime_stats_t) {}; |
| 1099 | |
| 1100 | #ifdef CONFIG_SCHED_THREAD_USAGE_ALL |
Peter Mitsis | 4eb1dd0 | 2021-12-14 22:26:22 -0500 | [diff] [blame] | 1101 | /* Retrieve the usage stats for each core and amalgamate them. */ |
| 1102 | |
Kumar Gala | a1195ae | 2022-10-18 09:45:13 -0500 | [diff] [blame] | 1103 | unsigned int num_cpus = arch_num_cpus(); |
| 1104 | |
| 1105 | for (uint8_t i = 0; i < num_cpus; i++) { |
Peter Mitsis | 4eb1dd0 | 2021-12-14 22:26:22 -0500 | [diff] [blame] | 1106 | z_sched_cpu_usage(i, &tmp_stats); |
| 1107 | |
| 1108 | stats->execution_cycles += tmp_stats.execution_cycles; |
| 1109 | stats->total_cycles += tmp_stats.total_cycles; |
| 1110 | #ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS |
Peter Mitsis | 976e408 | 2022-05-03 11:39:39 -0400 | [diff] [blame] | 1111 | stats->current_cycles += tmp_stats.current_cycles; |
Peter Mitsis | 4eb1dd0 | 2021-12-14 22:26:22 -0500 | [diff] [blame] | 1112 | stats->peak_cycles += tmp_stats.peak_cycles; |
| 1113 | stats->average_cycles += tmp_stats.average_cycles; |
| 1114 | #endif |
| 1115 | stats->idle_cycles += tmp_stats.idle_cycles; |
| 1116 | } |
Andy Ross | f169c5b | 2021-09-28 10:01:06 -0700 | [diff] [blame] | 1117 | #endif |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 1118 | |
| 1119 | return 0; |
| 1120 | } |