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 | |
Andrew Boie | 73abd32 | 2017-04-04 13:19:13 -0700 | [diff] [blame] | 17 | #include <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 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 29 | void z_bss_zero(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 30 | #ifdef CONFIG_XIP |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 31 | void z_data_copy(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 32 | #else |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 33 | static inline void z_data_copy(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 34 | { |
| 35 | /* Do nothing */ |
| 36 | } |
| 37 | #endif |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 38 | FUNC_NORETURN void z_cstart(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 39 | |
Peter Bigot | 1cadd8b | 2021-02-02 10:07:18 -0600 | [diff] [blame] | 40 | void z_device_state_init(void); |
| 41 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 42 | extern FUNC_NORETURN void z_thread_entry(k_thread_entry_t entry, |
Andrew Boie | 1e06ffc | 2017-09-11 09:30:04 -0700 | [diff] [blame] | 43 | void *p1, void *p2, void *p3); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 44 | |
Andrew Boie | e4cc84a | 2020-04-24 11:29:47 -0700 | [diff] [blame] | 45 | extern char *z_setup_new_thread(struct k_thread *new_thread, |
| 46 | k_thread_stack_t *stack, size_t stack_size, |
| 47 | k_thread_entry_t entry, |
| 48 | void *p1, void *p2, void *p3, |
| 49 | int prio, uint32_t options, const char *name); |
Andrew Boie | 2acfcd6 | 2017-08-30 14:31:03 -0700 | [diff] [blame] | 50 | |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 51 | /** |
Daniel Leung | 0c9f969 | 2020-12-15 13:37:11 -0800 | [diff] [blame] | 52 | * @brief Allocate aligned memory from the current thread's resource pool |
| 53 | * |
| 54 | * Threads may be assigned a resource pool, which will be used to allocate |
| 55 | * memory on behalf of certain kernel and driver APIs. Memory reserved |
| 56 | * in this way should be freed with k_free(). |
| 57 | * |
| 58 | * If called from an ISR, the k_malloc() system heap will be used if it exists. |
| 59 | * |
| 60 | * @param align Required memory alignment |
| 61 | * @param size Memory allocation size |
| 62 | * @return A pointer to the allocated memory, or NULL if there is insufficient |
| 63 | * RAM in the pool or there is no pool to draw memory from |
| 64 | */ |
| 65 | void *z_thread_aligned_alloc(size_t align, size_t size); |
| 66 | |
| 67 | /** |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 68 | * @brief Allocate some memory from the current thread's resource pool |
| 69 | * |
| 70 | * Threads may be assigned a resource pool, which will be used to allocate |
| 71 | * memory on behalf of certain kernel and driver APIs. Memory reserved |
| 72 | * in this way should be freed with k_free(). |
| 73 | * |
Andrew Boie | 6f654bb | 2019-05-22 10:38:43 -0700 | [diff] [blame] | 74 | * If called from an ISR, the k_malloc() system heap will be used if it exists. |
| 75 | * |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 76 | * @param size Memory allocation size |
| 77 | * @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] | 78 | * 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] | 79 | */ |
Daniel Leung | 0c9f969 | 2020-12-15 13:37:11 -0800 | [diff] [blame] | 80 | static inline void *z_thread_malloc(size_t size) |
| 81 | { |
| 82 | return z_thread_aligned_alloc(0, size); |
| 83 | } |
Andrew Boie | 92e5bd7 | 2018-04-12 17:12:15 -0700 | [diff] [blame] | 84 | |
Anas Nashif | 780324b | 2017-10-29 07:10:22 -0400 | [diff] [blame] | 85 | /* set and clear essential thread flag */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 86 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 87 | extern void z_thread_essential_set(void); |
| 88 | extern void z_thread_essential_clear(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 89 | |
| 90 | /* clean up when a thread is aborted */ |
| 91 | |
| 92 | #if defined(CONFIG_THREAD_MONITOR) |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 93 | extern void z_thread_monitor_exit(struct k_thread *thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 94 | #else |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 95 | #define z_thread_monitor_exit(thread) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 96 | do {/* nothing */ \ |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 97 | } while (false) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 98 | #endif /* CONFIG_THREAD_MONITOR */ |
| 99 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 100 | #ifdef CONFIG_USE_SWITCH |
| 101 | /* This is a arch function traditionally, but when the switch-based |
| 102 | * z_swap() is in use it's a simple inline provided by the kernel. |
| 103 | */ |
| 104 | static ALWAYS_INLINE void |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 105 | arch_thread_return_value_set(struct k_thread *thread, unsigned int value) |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 106 | { |
| 107 | thread->swap_retval = value; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | static ALWAYS_INLINE void |
| 112 | z_thread_return_value_set_with_data(struct k_thread *thread, |
| 113 | unsigned int value, |
| 114 | void *data) |
| 115 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 116 | arch_thread_return_value_set(thread, value); |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 117 | thread->base.swap_data = data; |
| 118 | } |
| 119 | |
Andy Ross | a12f2d6 | 2019-06-05 08:58:42 -0700 | [diff] [blame] | 120 | extern void z_smp_init(void); |
Andy Ross | bdcd18a7 | 2018-01-17 11:34:50 -0800 | [diff] [blame] | 121 | |
Andy Ross | 564f590 | 2018-01-26 12:30:21 -0800 | [diff] [blame] | 122 | extern void smp_timer_init(void); |
| 123 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 124 | 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] | 125 | |
| 126 | #if CONFIG_STACK_POINTER_RANDOM |
| 127 | extern int z_stack_adjust_initialized; |
| 128 | #endif |
| 129 | |
Andrew Boie | e665410 | 2019-09-21 16:55:55 -0700 | [diff] [blame] | 130 | #ifdef CONFIG_BOOT_TIME_MEASUREMENT |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 131 | extern uint32_t z_timestamp_main; /* timestamp when main task starts */ |
| 132 | extern uint32_t z_timestamp_idle; /* timestamp when CPU goes idle */ |
Andrew Boie | e665410 | 2019-09-21 16:55:55 -0700 | [diff] [blame] | 133 | #endif |
| 134 | |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 135 | extern struct k_thread z_main_thread; |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 136 | |
| 137 | |
| 138 | #ifdef CONFIG_MULTITHREADING |
| 139 | extern struct k_thread z_idle_threads[CONFIG_MP_NUM_CPUS]; |
| 140 | #endif |
Andrew Boie | 8b4b0d6 | 2020-04-25 15:19:23 -0700 | [diff] [blame] | 141 | extern K_KERNEL_STACK_ARRAY_DEFINE(z_interrupt_stacks, CONFIG_MP_NUM_CPUS, |
Andrew Boie | 80a0d9d | 2020-03-12 15:37:29 -0700 | [diff] [blame] | 142 | CONFIG_ISR_STACK_SIZE); |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 143 | |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 144 | #ifdef CONFIG_GEN_PRIV_STACKS |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 145 | extern uint8_t *z_priv_stack_find(k_thread_stack_t *stack); |
Andrew Boie | 28be793 | 2020-03-11 10:56:19 -0700 | [diff] [blame] | 146 | #endif |
| 147 | |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 148 | #ifdef CONFIG_USERSPACE |
| 149 | bool z_stack_is_user_capable(k_thread_stack_t *stack); |
Andrew Boie | b5a71f7 | 2020-10-06 13:39:29 -0700 | [diff] [blame] | 150 | |
| 151 | /* Memory domain setup hook, called from z_setup_new_thread() */ |
| 152 | void z_mem_domain_init_thread(struct k_thread *thread); |
| 153 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 154 | /* Memory domain teardown hook, called from z_thread_abort() */ |
Andrew Boie | b5a71f7 | 2020-10-06 13:39:29 -0700 | [diff] [blame] | 155 | void z_mem_domain_exit_thread(struct k_thread *thread); |
Andrew Boie | 348a0fd | 2020-10-06 15:53:43 -0700 | [diff] [blame] | 156 | |
| 157 | /* This spinlock: |
| 158 | * |
| 159 | * - Protects the full set of active k_mem_domain objects and their contents |
| 160 | * - Serializes calls to arch_mem_domain_* APIs |
| 161 | * |
| 162 | * If architecture code needs to access k_mem_domain structures or the |
| 163 | * partitions they contain at any other point, this spinlock should be held. |
| 164 | * Uniprocessor systems can get away with just locking interrupts but this is |
| 165 | * not recommended. |
| 166 | */ |
| 167 | extern struct k_spinlock z_mem_domain_lock; |
Andrew Boie | 8ce260d | 2020-04-24 16:24:46 -0700 | [diff] [blame] | 168 | #endif /* CONFIG_USERSPACE */ |
| 169 | |
Flavio Ceolin | 5408f31 | 2020-05-21 16:55:28 -0700 | [diff] [blame] | 170 | #ifdef CONFIG_GDBSTUB |
| 171 | struct gdb_ctx; |
| 172 | |
| 173 | /* Should be called by the arch layer. This is the gdbstub main loop |
| 174 | * and synchronously communicate with gdb on host. |
| 175 | */ |
| 176 | extern int z_gdb_main_loop(struct gdb_ctx *ctx, bool start); |
| 177 | #endif |
| 178 | |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 179 | #ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 180 | void z_thread_mark_switched_in(void); |
| 181 | void z_thread_mark_switched_out(void); |
| 182 | #else |
Daniel Leung | fc577c4 | 2020-08-27 13:54:14 -0700 | [diff] [blame] | 183 | |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 184 | /** |
| 185 | * @brief Called after a thread has been selected to run |
| 186 | */ |
| 187 | #define z_thread_mark_switched_in() |
| 188 | |
| 189 | /** |
| 190 | * @brief Called before a thread has been selected to run |
| 191 | */ |
| 192 | |
| 193 | #define z_thread_mark_switched_out() |
| 194 | |
| 195 | #endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */ |
Flavio Ceolin | 5408f31 | 2020-05-21 16:55:28 -0700 | [diff] [blame] | 196 | |
Andrew Boie | e35f179 | 2020-12-09 12:18:40 -0800 | [diff] [blame] | 197 | /* Init hook for page frame management, invoked immediately upon entry of |
| 198 | * main thread, before POST_KERNEL tasks |
| 199 | */ |
| 200 | void z_mem_manage_init(void); |
| 201 | |
Andrew Boie | ecb25fe | 2020-12-18 15:18:04 -0800 | [diff] [blame] | 202 | /* Workaround for build-time page table mapping of the kernel */ |
| 203 | void z_kernel_map_fixup(void); |
| 204 | |
Andrei Emeltchenko | 377456c | 2021-02-19 16:57:02 +0200 | [diff] [blame] | 205 | #define LOCKED(lck) for (k_spinlock_key_t __i = {}, \ |
| 206 | __key = k_spin_lock(lck); \ |
| 207 | !__i.key; \ |
| 208 | k_spin_unlock(lck, __key), __i.key = 1) |
| 209 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 210 | #ifdef __cplusplus |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | #endif /* _ASMLANGUAGE */ |
| 215 | |
Flavio Ceolin | a7fffa9 | 2018-09-13 15:06:35 -0700 | [diff] [blame] | 216 | #endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_INTERNAL_H_ */ |