Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010-2012, 2014-2015 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 |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 9 | * @brief Architecture-independent private kernel APIs |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 10 | * |
Anas Nashif | dc3d73b | 2016-12-19 20:25:56 -0500 | [diff] [blame] | 11 | * This file contains private kernel APIs that are not architecture-specific. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 12 | */ |
| 13 | |
Flavio Ceolin | a7fffa9 | 2018-09-13 15:06:35 -0700 | [diff] [blame] | 14 | #ifndef ZEPHYR_KERNEL_INCLUDE_KERNEL_INTERNAL_H_ |
| 15 | #define ZEPHYR_KERNEL_INCLUDE_KERNEL_INTERNAL_H_ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 16 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 17 | #include <zephyr/kernel.h> |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 18 | #include <kernel_arch_interface.h> |
| 19 | #include <string.h> |
Benjamin Walsh | 358a53c | 2016-11-18 15:35:05 -0500 | [diff] [blame] | 20 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 21 | #ifndef _ASMLANGUAGE |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | /* Early boot functions */ |
| 28 | |
Nicolas Pitre | 678b76e | 2022-02-10 13:54:49 -0500 | [diff] [blame] | 29 | void z_early_memset(void *dst, int c, size_t n); |
| 30 | void z_early_memcpy(void *dst, const void *src, size_t n); |
| 31 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 32 | void z_bss_zero(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 33 | #ifdef CONFIG_XIP |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 34 | void z_data_copy(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 35 | #else |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 36 | static inline void z_data_copy(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 37 | { |
| 38 | /* Do nothing */ |
| 39 | } |
| 40 | #endif |
Daniel Leung | d812728 | 2021-02-24 10:18:34 -0800 | [diff] [blame] | 41 | |
| 42 | #ifdef CONFIG_LINKER_USE_BOOT_SECTION |
| 43 | void z_bss_zero_boot(void); |
| 44 | #else |
| 45 | static inline void z_bss_zero_boot(void) |
| 46 | { |
| 47 | /* Do nothing */ |
| 48 | } |
| 49 | #endif |
| 50 | |
Daniel Leung | 1310ad6 | 2021-02-23 13:33:38 -0800 | [diff] [blame] | 51 | #ifdef CONFIG_LINKER_USE_PINNED_SECTION |
| 52 | void z_bss_zero_pinned(void); |
| 53 | #else |
| 54 | static inline void z_bss_zero_pinned(void) |
| 55 | { |
| 56 | /* Do nothing */ |
| 57 | } |
| 58 | #endif |
| 59 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 60 | FUNC_NORETURN void z_cstart(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 61 | |
Peter Bigot | 1cadd8b | 2021-02-02 10:07:18 -0600 | [diff] [blame] | 62 | void z_device_state_init(void); |
| 63 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 64 | extern FUNC_NORETURN void z_thread_entry(k_thread_entry_t entry, |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 65 | void *p1, void *p2, void *p3); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 66 | |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 67 | extern char *z_setup_new_thread(struct k_thread *new_thread, |
| 68 | k_thread_stack_t *stack, size_t stack_size, |
| 69 | k_thread_entry_t entry, |
| 70 | void *p1, void *p2, void *p3, |
| 71 | int prio, uint32_t options, const char *name); |
Andrew Boie | 2acfcd6 | 2017-08-30 14:31:03 -0700 | [diff] [blame] | 72 | |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 73 | /** |
Daniel Leung | 0c9f969 | 2020-12-15 13:37:11 -0800 | [diff] [blame] | 74 | * @brief Allocate aligned memory from the current thread's resource pool |
| 75 | * |
| 76 | * Threads may be assigned a resource pool, which will be used to allocate |
| 77 | * memory on behalf of certain kernel and driver APIs. Memory reserved |
| 78 | * in this way should be freed with k_free(). |
| 79 | * |
| 80 | * If called from an ISR, the k_malloc() system heap will be used if it exists. |
| 81 | * |
| 82 | * @param align Required memory alignment |
| 83 | * @param size Memory allocation size |
| 84 | * @return A pointer to the allocated memory, or NULL if there is insufficient |
| 85 | * RAM in the pool or there is no pool to draw memory from |
| 86 | */ |
| 87 | void *z_thread_aligned_alloc(size_t align, size_t size); |
| 88 | |
| 89 | /** |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 90 | * @brief Allocate some memory from the current thread's resource pool |
| 91 | * |
| 92 | * Threads may be assigned a resource pool, which will be used to allocate |
| 93 | * memory on behalf of certain kernel and driver APIs. Memory reserved |
| 94 | * in this way should be freed with k_free(). |
| 95 | * |
Andrew Boie | 6f654bb | 2019-05-22 10:38:43 -0700 | [diff] [blame] | 96 | * If called from an ISR, the k_malloc() system heap will be used if it exists. |
| 97 | * |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 98 | * @param size Memory allocation size |
| 99 | * @return A pointer to the allocated memory, or NULL if there is insufficient |
Andrew Boie | 6f654bb | 2019-05-22 10:38:43 -0700 | [diff] [blame] | 100 | * RAM in the pool or there is no pool to draw memory from |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 101 | */ |
Daniel Leung | 0c9f969 | 2020-12-15 13:37:11 -0800 | [diff] [blame] | 102 | static inline void *z_thread_malloc(size_t size) |
| 103 | { |
| 104 | return z_thread_aligned_alloc(0, size); |
| 105 | } |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 106 | |
Anas Nashif | 780324b | 2017-10-29 07:10:22 -0400 | [diff] [blame] | 107 | /* set and clear essential thread flag */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 108 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 109 | extern void z_thread_essential_set(void); |
| 110 | extern void z_thread_essential_clear(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 111 | |
| 112 | /* clean up when a thread is aborted */ |
| 113 | |
| 114 | #if defined(CONFIG_THREAD_MONITOR) |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 115 | extern void z_thread_monitor_exit(struct k_thread *thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 116 | #else |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 117 | #define z_thread_monitor_exit(thread) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 118 | do {/* nothing */ \ |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 119 | } while (false) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 120 | #endif /* CONFIG_THREAD_MONITOR */ |
| 121 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 122 | #ifdef CONFIG_USE_SWITCH |
| 123 | /* This is a arch function traditionally, but when the switch-based |
| 124 | * z_swap() is in use it's a simple inline provided by the kernel. |
| 125 | */ |
| 126 | static ALWAYS_INLINE void |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 127 | arch_thread_return_value_set(struct k_thread *thread, unsigned int value) |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 128 | { |
| 129 | thread->swap_retval = value; |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | static ALWAYS_INLINE void |
| 134 | z_thread_return_value_set_with_data(struct k_thread *thread, |
| 135 | unsigned int value, |
| 136 | void *data) |
| 137 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 138 | arch_thread_return_value_set(thread, value); |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 139 | thread->base.swap_data = data; |
| 140 | } |
| 141 | |
Guennadi Liakhovetski | 45b70e1 | 2021-07-15 09:36:45 +0200 | [diff] [blame] | 142 | #ifdef CONFIG_SMP |
Andy Ross | a12f2d6 | 2019-06-05 08:58:42 -0700 | [diff] [blame] | 143 | extern void z_smp_init(void); |
Andy Ross | 564f590 | 2018-01-26 12:30:21 -0800 | [diff] [blame] | 144 | extern void smp_timer_init(void); |
Guennadi Liakhovetski | 45b70e1 | 2021-07-15 09:36:45 +0200 | [diff] [blame] | 145 | #endif |
Andy Ross | 564f590 | 2018-01-26 12:30:21 -0800 | [diff] [blame] | 146 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 147 | extern void z_early_boot_rand_get(uint8_t *buf, size_t length); |
Andrew Boie | 538754c | 2018-05-23 15:25:23 -0700 | [diff] [blame] | 148 | |
| 149 | #if CONFIG_STACK_POINTER_RANDOM |
| 150 | extern int z_stack_adjust_initialized; |
| 151 | #endif |
| 152 | |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 153 | extern struct k_thread z_main_thread; |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 154 | |
| 155 | |
| 156 | #ifdef CONFIG_MULTITHREADING |
| 157 | extern struct k_thread z_idle_threads[CONFIG_MP_NUM_CPUS]; |
| 158 | #endif |
Daniel Leung | dbc0be4 | 2021-06-02 14:40:15 -0700 | [diff] [blame] | 159 | K_KERNEL_PINNED_STACK_ARRAY_EXTERN(z_interrupt_stacks, CONFIG_MP_NUM_CPUS, |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 160 | CONFIG_ISR_STACK_SIZE); |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 161 | |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 162 | #ifdef CONFIG_GEN_PRIV_STACKS |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 163 | extern uint8_t *z_priv_stack_find(k_thread_stack_t *stack); |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 164 | #endif |
| 165 | |
Krzysztof Chruscinski | 1da97e1 | 2022-01-28 15:40:37 +0100 | [diff] [blame] | 166 | /* Calculate stack usage. */ |
| 167 | int z_stack_space_get(const uint8_t *stack_start, size_t size, size_t *unused_ptr); |
| 168 | |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 169 | #ifdef CONFIG_USERSPACE |
| 170 | bool z_stack_is_user_capable(k_thread_stack_t *stack); |
Andrew Boie | b5a71f7 | 2020-10-06 13:39:29 -0700 | [diff] [blame] | 171 | |
| 172 | /* Memory domain setup hook, called from z_setup_new_thread() */ |
| 173 | void z_mem_domain_init_thread(struct k_thread *thread); |
| 174 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 175 | /* Memory domain teardown hook, called from z_thread_abort() */ |
Andrew Boie | b5a71f7 | 2020-10-06 13:39:29 -0700 | [diff] [blame] | 176 | void z_mem_domain_exit_thread(struct k_thread *thread); |
Andrew Boie | 348a0fd | 2020-10-06 15:53:43 -0700 | [diff] [blame] | 177 | |
| 178 | /* This spinlock: |
| 179 | * |
| 180 | * - Protects the full set of active k_mem_domain objects and their contents |
| 181 | * - Serializes calls to arch_mem_domain_* APIs |
| 182 | * |
| 183 | * If architecture code needs to access k_mem_domain structures or the |
| 184 | * partitions they contain at any other point, this spinlock should be held. |
| 185 | * Uniprocessor systems can get away with just locking interrupts but this is |
| 186 | * not recommended. |
| 187 | */ |
| 188 | extern struct k_spinlock z_mem_domain_lock; |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 189 | #endif /* CONFIG_USERSPACE */ |
| 190 | |
Flavio Ceolin | 5408f31 | 2020-05-21 16:55:28 -0700 | [diff] [blame] | 191 | #ifdef CONFIG_GDBSTUB |
| 192 | struct gdb_ctx; |
| 193 | |
| 194 | /* Should be called by the arch layer. This is the gdbstub main loop |
| 195 | * and synchronously communicate with gdb on host. |
| 196 | */ |
Daniel Leung | 650a629 | 2021-10-28 14:53:28 -0700 | [diff] [blame] | 197 | extern int z_gdb_main_loop(struct gdb_ctx *ctx); |
Flavio Ceolin | 5408f31 | 2020-05-21 16:55:28 -0700 | [diff] [blame] | 198 | #endif |
| 199 | |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 200 | #ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 201 | void z_thread_mark_switched_in(void); |
| 202 | void z_thread_mark_switched_out(void); |
| 203 | #else |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 204 | |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 205 | /** |
| 206 | * @brief Called after a thread has been selected to run |
| 207 | */ |
| 208 | #define z_thread_mark_switched_in() |
| 209 | |
| 210 | /** |
| 211 | * @brief Called before a thread has been selected to run |
| 212 | */ |
| 213 | |
| 214 | #define z_thread_mark_switched_out() |
| 215 | |
| 216 | #endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */ |
Flavio Ceolin | 5408f31 | 2020-05-21 16:55:28 -0700 | [diff] [blame] | 217 | |
Andrew Boie | e35f179 | 2020-12-09 12:18:40 -0800 | [diff] [blame] | 218 | /* Init hook for page frame management, invoked immediately upon entry of |
| 219 | * main thread, before POST_KERNEL tasks |
| 220 | */ |
| 221 | void z_mem_manage_init(void); |
| 222 | |
Daniel Leung | e88afd2 | 2021-07-15 13:15:29 -0700 | [diff] [blame] | 223 | /** |
| 224 | * @brief Finalize page frame management at the end of boot process. |
| 225 | */ |
| 226 | void z_mem_manage_boot_finish(void); |
| 227 | |
Andrei Emeltchenko | 377456c | 2021-02-19 16:57:02 +0200 | [diff] [blame] | 228 | #define LOCKED(lck) for (k_spinlock_key_t __i = {}, \ |
| 229 | __key = k_spin_lock(lck); \ |
| 230 | !__i.key; \ |
| 231 | k_spin_unlock(lck, __key), __i.key = 1) |
| 232 | |
Flavio Ceolin | 10f2935 | 2021-02-23 08:59:28 -0800 | [diff] [blame] | 233 | #ifdef CONFIG_PM |
| 234 | |
| 235 | /* When the kernel is about to go idle, it calls this function to notify the |
| 236 | * power management subsystem, that the kernel is ready to enter the idle state. |
| 237 | * |
| 238 | * At this point, the kernel has disabled interrupts and computed the maximum |
| 239 | * time the system can remain idle. The function passes the time that the system |
| 240 | * can remain idle. The SOC interface performs power operations that can be done |
| 241 | * in the available time. The power management operations must halt execution of |
| 242 | * the CPU. |
| 243 | * |
| 244 | * This function assumes that a wake up event has already been set up by the |
| 245 | * application. |
| 246 | * |
| 247 | * This function is entered with interrupts disabled. It should re-enable |
| 248 | * interrupts if it had entered a power state. |
Flavio Ceolin | 9444480 | 2021-10-30 23:13:08 -0700 | [diff] [blame] | 249 | * |
| 250 | * @return True if the system suspended, otherwise return false |
Flavio Ceolin | 10f2935 | 2021-02-23 08:59:28 -0800 | [diff] [blame] | 251 | */ |
Flavio Ceolin | 9444480 | 2021-10-30 23:13:08 -0700 | [diff] [blame] | 252 | bool pm_system_suspend(int32_t ticks); |
Flavio Ceolin | 10f2935 | 2021-02-23 08:59:28 -0800 | [diff] [blame] | 253 | |
Flavio Ceolin | 9b246ab | 2021-02-24 15:53:46 -0800 | [diff] [blame] | 254 | /** |
| 255 | * Notify exit from kernel idling after PM operations |
| 256 | * |
| 257 | * This function would notify exit from kernel idling if a corresponding |
| 258 | * pm_system_suspend() notification was handled and did not return |
| 259 | * PM_STATE_ACTIVE. |
| 260 | * |
| 261 | * This function would be called from the ISR context of the event |
| 262 | * that caused the exit from kernel idling. This will be called immediately |
| 263 | * after interrupts are enabled. This is called to give a chance to do |
| 264 | * any operations before the kernel would switch tasks or processes nested |
| 265 | * interrupts. This is required for cpu low power states that would require |
| 266 | * interrupts to be enabled while entering low power states. e.g. C1 in x86. In |
| 267 | * those cases, the ISR would be invoked immediately after the event wakes up |
| 268 | * the CPU, before code following the CPU wait, gets a chance to execute. This |
| 269 | * can be ignored if no operation needs to be done at the wake event |
Flavio Ceolin | 623ed5a | 2021-11-19 18:20:46 -0800 | [diff] [blame] | 270 | * notification. |
Flavio Ceolin | 9b246ab | 2021-02-24 15:53:46 -0800 | [diff] [blame] | 271 | */ |
| 272 | void pm_system_resume(void); |
| 273 | |
Flavio Ceolin | 10f2935 | 2021-02-23 08:59:28 -0800 | [diff] [blame] | 274 | #endif |
| 275 | |
Daniel Leung | 8eea511 | 2021-03-30 14:38:00 -0700 | [diff] [blame] | 276 | #ifdef CONFIG_DEMAND_PAGING_TIMING_HISTOGRAM |
| 277 | /** |
| 278 | * Initialize the timing histograms for demand paging. |
| 279 | */ |
| 280 | void z_paging_histogram_init(void); |
| 281 | |
| 282 | /** |
| 283 | * Increment the counter in the timing histogram. |
| 284 | * |
| 285 | * @param hist The timing histogram to be updated. |
| 286 | * @param cycles Time spent in measured operation. |
| 287 | */ |
| 288 | void z_paging_histogram_inc(struct k_mem_paging_histogram_t *hist, |
| 289 | uint32_t cycles); |
| 290 | #endif /* CONFIG_DEMAND_PAGING_TIMING_HISTOGRAM */ |
| 291 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 292 | #ifdef __cplusplus |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | #endif /* _ASMLANGUAGE */ |
| 297 | |
Flavio Ceolin | a7fffa9 | 2018-09-13 15:06:35 -0700 | [diff] [blame] | 298 | #endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_INTERNAL_H_ */ |