Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2010-2012, 2014-2015 Wind River Systems, Inc.
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /**
8 : : * @file
9 : : * @brief Architecture-independent private kernel APIs
10 : : *
11 : : * This file contains private kernel APIs that are not architecture-specific.
12 : : */
13 : :
14 : : #ifndef ZEPHYR_KERNEL_INCLUDE_KERNEL_INTERNAL_H_
15 : : #define ZEPHYR_KERNEL_INCLUDE_KERNEL_INTERNAL_H_
16 : :
17 : : #include <zephyr/kernel.h>
18 : : #include <kernel_arch_interface.h>
19 : : #include <kthread.h>
20 : : #include <string.h>
21 : :
22 : : #ifndef _ASMLANGUAGE
23 : :
24 : : #ifdef __cplusplus
25 : : extern "C" {
26 : : #endif
27 : :
28 : : /* Initialize per-CPU kernel data */
29 : : void z_init_cpu(int id);
30 : :
31 : : /* Initialize a thread */
32 : : void z_init_thread_base(struct _thread_base *thread_base, int priority,
33 : : uint32_t initial_state, unsigned int options);
34 : :
35 : :
36 : : FUNC_NORETURN void z_cstart(void);
37 : :
38 : : /*
39 : : * Lean kernel-internal init model.
40 : : *
41 : : * A subsystem registers a parameterless init function that the boot path runs
42 : : * at a fixed phase, without the SYS_INIT level/priority machinery (kernel init
43 : : * order is fixed at compile time). Each entry is emitted into the subsystem's
44 : : * own translation unit, so it -- and its init -- is linked only when the
45 : : * subsystem itself is linked, preserving the pay-per-use linkage that SYS_INIT
46 : : * provides. The entry holds a single function pointer (no device back-pointer
47 : : * and no priority), so it is half the size of a SYS_INIT init_entry.
48 : : */
49 : : struct k_kernel_init_pre_entry {
50 : : void (*init_fn)(void);
51 : : };
52 : :
53 : : struct k_kernel_init_post_entry {
54 : : void (*init_fn)(void);
55 : : };
56 : :
57 : : /* Register a kernel init function to run before multithreading starts (the
58 : : * PRE_KERNEL phase), ahead of PRE_KERNEL device init.
59 : : */
60 : : #define K_KERNEL_INIT_PRE(_fn) \
61 : : static const STRUCT_SECTION_ITERABLE(k_kernel_init_pre_entry, \
62 : : _kernel_init_pre_##_fn) = { \
63 : : .init_fn = (_fn), \
64 : : }
65 : :
66 : : /* Register a kernel init function to run after the kernel is up (the
67 : : * POST_KERNEL phase), before POST_KERNEL device init.
68 : : */
69 : : #define K_KERNEL_INIT_POST(_fn) \
70 : : static const STRUCT_SECTION_ITERABLE(k_kernel_init_post_entry, \
71 : : _kernel_init_post_##_fn) = { \
72 : : .init_fn = (_fn), \
73 : : }
74 : :
75 : : extern FUNC_NORETURN void z_thread_entry(k_thread_entry_t entry,
76 : : void *p1, void *p2, void *p3);
77 : :
78 : : extern char *z_setup_new_thread(struct k_thread *new_thread,
79 : : k_thread_stack_t *stack, size_t stack_size,
80 : : k_thread_entry_t entry,
81 : : void *p1, void *p2, void *p3,
82 : : int prio, uint32_t options, const char *name);
83 : :
84 : : /**
85 : : * @brief Allocate aligned memory from the current thread's resource pool
86 : : *
87 : : * Threads may be assigned a resource pool, which will be used to allocate
88 : : * memory on behalf of certain kernel and driver APIs. Memory reserved
89 : : * in this way should be freed with k_free().
90 : : *
91 : : * If called from an ISR, the k_malloc() system heap will be used if it exists.
92 : : *
93 : : * @param align Required memory alignment
94 : : * @param size Memory allocation size
95 : : * @return A pointer to the allocated memory, or NULL if there is insufficient
96 : : * RAM in the pool or there is no pool to draw memory from
97 : : */
98 : : void *z_thread_aligned_alloc(size_t align, size_t size);
99 : :
100 : : /**
101 : : * @brief Allocate some memory from the current thread's resource pool
102 : : *
103 : : * Threads may be assigned a resource pool, which will be used to allocate
104 : : * memory on behalf of certain kernel and driver APIs. Memory reserved
105 : : * in this way should be freed with k_free().
106 : : *
107 : : * If called from an ISR, the k_malloc() system heap will be used if it exists.
108 : : *
109 : : * @param size Memory allocation size
110 : : * @return A pointer to the allocated memory, or NULL if there is insufficient
111 : : * RAM in the pool or there is no pool to draw memory from
112 : : */
113 : : void *z_thread_malloc(size_t size);
114 : :
115 : :
116 : : #ifdef CONFIG_USE_SWITCH
117 : : /* This is a arch function traditionally, but when the switch-based
118 : : * z_swap() is in use it's a simple inline provided by the kernel.
119 : : */
120 : : static ALWAYS_INLINE void
121 : : arch_thread_return_value_set(struct k_thread *thread, unsigned int value)
122 : : {
123 : : thread->swap_retval = value;
124 : : }
125 : : #endif
126 : :
127 : : static ALWAYS_INLINE void
128 : 4 : z_thread_return_value_set_with_data(struct k_thread *thread,
129 : : unsigned int value,
130 : : void *data)
131 : : {
132 : 4 : arch_thread_return_value_set(thread, value);
133 : 4 : thread->base.swap_data = data;
134 : 4 : }
135 : :
136 : : #if defined(CONFIG_SMP) && defined(CONFIG_SYS_CLOCK_EXISTS)
137 : : extern void smp_timer_init(void);
138 : : #endif /* CONFIG_SMP && CONFIG_SYS_CLOCK_EXISTS */
139 : :
140 : : extern void z_early_rand_get(uint8_t *buf, size_t length);
141 : :
142 : : #if defined(CONFIG_STACK_POINTER_RANDOM) && (CONFIG_STACK_POINTER_RANDOM != 0)
143 : : extern int z_stack_adjust_initialized;
144 : : #endif /* CONFIG_STACK_POINTER_RANDOM */
145 : :
146 : : #ifdef CONFIG_MULTITHREADING
147 : : extern struct k_thread z_main_thread;
148 : : #endif /* CONFIG_MULTITHREADING */
149 : :
150 : :
151 : : K_KERNEL_STACK_ARRAY_DECLARE(z_interrupt_stacks, CONFIG_MP_MAX_NUM_CPUS,
152 : : CONFIG_ISR_STACK_SIZE);
153 : : K_THREAD_STACK_DECLARE(z_main_stack, CONFIG_MAIN_STACK_SIZE);
154 : :
155 : : #ifdef CONFIG_GEN_PRIV_STACKS
156 : : extern uint8_t *z_priv_stack_find(k_thread_stack_t *stack);
157 : : #endif /* CONFIG_GEN_PRIV_STACKS */
158 : :
159 : : /* Calculate stack usage. */
160 : : int z_stack_space_get(const uint8_t *stack_start, size_t size, size_t *unused_ptr);
161 : :
162 : : #ifdef CONFIG_USERSPACE
163 : : bool z_stack_is_user_capable(k_thread_stack_t *stack);
164 : :
165 : : /* Memory domain setup hook, called from z_setup_new_thread() */
166 : : void z_mem_domain_init_thread(struct k_thread *thread);
167 : :
168 : : /* Memory domain teardown hook, called from z_thread_abort() */
169 : : void z_mem_domain_exit_thread(struct k_thread *thread);
170 : :
171 : : /* This spinlock:
172 : : *
173 : : * - Protects the full set of active k_mem_domain objects and their contents
174 : : * - Serializes calls to arch_mem_domain_* APIs
175 : : *
176 : : * If architecture code needs to access k_mem_domain structures or the
177 : : * partitions they contain at any other point, this spinlock should be held.
178 : : * Uniprocessor systems can get away with just locking interrupts but this is
179 : : * not recommended.
180 : : */
181 : : extern struct k_spinlock z_mem_domain_lock;
182 : : #endif /* CONFIG_USERSPACE */
183 : :
184 : : #ifdef CONFIG_GDBSTUB
185 : : struct gdb_ctx;
186 : :
187 : : /* Should be called by the arch layer. This is the gdbstub main loop
188 : : * and synchronously communicate with gdb on host.
189 : : */
190 : : extern int z_gdb_main_loop(struct gdb_ctx *ctx);
191 : : #endif /* CONFIG_GDBSTUB */
192 : :
193 : : #ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
194 : : void z_thread_mark_switched_in(void);
195 : : void z_thread_mark_switched_out(void);
196 : : #else
197 : :
198 : : /**
199 : : * @brief Called after a thread has been selected to run
200 : : */
201 : : #define z_thread_mark_switched_in()
202 : :
203 : : /**
204 : : * @brief Called before a thread has been selected to run
205 : : */
206 : :
207 : : #define z_thread_mark_switched_out()
208 : :
209 : : #endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */
210 : :
211 : : /* Init hook for page frame management, invoked immediately upon entry of
212 : : * main thread, before POST_KERNEL tasks
213 : : */
214 : : void z_mem_manage_init(void);
215 : :
216 : : /**
217 : : * @brief Finalize page frame management at the end of boot process.
218 : : */
219 : : void z_mem_manage_boot_finish(void);
220 : :
221 : :
222 : : bool z_handle_obj_poll_events(sys_dlist_t *events, uint32_t state);
223 : :
224 : : #ifdef CONFIG_PM
225 : :
226 : : /* When the kernel is about to go idle, it calls this function to notify the
227 : : * power management subsystem, that the kernel is ready to enter the idle state.
228 : : *
229 : : * At this point, the kernel has disabled interrupts and computed the maximum
230 : : * time the system can remain idle. The function passes the time that the system
231 : : * can remain idle. The SOC interface performs power operations that can be done
232 : : * in the available time. The power management operations must halt execution of
233 : : * the CPU.
234 : : *
235 : : * This function assumes that a wake up event has already been set up by the
236 : : * application.
237 : : *
238 : : * This function is entered with the idle thread's interrupt lock held. If it
239 : : * enters a power state, it returns before the idle thread restores the saved
240 : : * interrupt key. SoC PM code may use architecture helpers around the
241 : : * low-power instruction, but normal IRQ dispatch must remain blocked until
242 : : * that final idle-thread restore.
243 : : *
244 : : * @return True if the system suspended, otherwise return false
245 : : */
246 : : bool pm_system_suspend(int32_t ticks);
247 : :
248 : : #endif /* CONFIG_PM */
249 : :
250 : : #ifdef CONFIG_DEMAND_PAGING_TIMING_HISTOGRAM
251 : : /**
252 : : * Initialize the timing histograms for demand paging.
253 : : */
254 : : void z_paging_histogram_init(void);
255 : :
256 : : /**
257 : : * Increment the counter in the timing histogram.
258 : : *
259 : : * @param hist The timing histogram to be updated.
260 : : * @param cycles Time spent in measured operation.
261 : : */
262 : : void z_paging_histogram_inc(struct k_mem_paging_histogram_t *hist,
263 : : uint32_t cycles);
264 : : #endif /* CONFIG_DEMAND_PAGING_TIMING_HISTOGRAM */
265 : :
266 : : #ifdef CONFIG_OBJ_CORE_STATS_THREAD
267 : : int z_thread_stats_raw(struct k_obj_core *obj_core, void *stats);
268 : : int z_thread_stats_query(struct k_obj_core *obj_core, void *stats);
269 : : int z_thread_stats_reset(struct k_obj_core *obj_core);
270 : : int z_thread_stats_disable(struct k_obj_core *obj_core);
271 : : int z_thread_stats_enable(struct k_obj_core *obj_core);
272 : : #endif /* CONFIG_OBJ_CORE_STATS_THREAD */
273 : :
274 : : #ifdef CONFIG_OBJ_CORE_STATS_SYSTEM
275 : : int z_cpu_stats_raw(struct k_obj_core *obj_core, void *stats);
276 : : int z_cpu_stats_query(struct k_obj_core *obj_core, void *stats);
277 : :
278 : : int z_kernel_stats_raw(struct k_obj_core *obj_core, void *stats);
279 : : int z_kernel_stats_query(struct k_obj_core *obj_core, void *stats);
280 : : #endif /* CONFIG_OBJ_CORE_STATS_SYSTEM */
281 : :
282 : : #if defined(CONFIG_THREAD_ABORT_NEED_CLEANUP)
283 : : /**
284 : : * Perform cleanup at the end of k_thread_abort().
285 : : *
286 : : * This performs additional cleanup steps at the end of k_thread_abort()
287 : : * where these steps require that the thread is no longer running.
288 : : * If the target thread is not the current running thread, the cleanup
289 : : * steps will be performed immediately. However, if the target thread is
290 : : * the current running thread (e.g. k_thread_abort(_current)), it defers
291 : : * the cleanup steps to later when the work will be finished in another
292 : : * context.
293 : : *
294 : : * @param thread Pointer to thread to be cleaned up.
295 : : */
296 : : void k_thread_abort_cleanup(struct k_thread *thread);
297 : :
298 : : /**
299 : : * Check if thread is the same as the one waiting for cleanup.
300 : : *
301 : : * This is used to guard against reusing the same thread object
302 : : * before the previous cleanup has finished. This will perform
303 : : * the necessary cleanups before the thread object can be
304 : : * reused. Should mainly be used during thread creation.
305 : : *
306 : : * @param thread Pointer to thread to be checked.
307 : : */
308 : : void k_thread_abort_cleanup_check_reuse(struct k_thread *thread);
309 : : #endif /* CONFIG_THREAD_ABORT_NEED_CLEANUP */
310 : :
311 : : #ifdef __cplusplus
312 : : }
313 : : #endif
314 : :
315 : : #endif /* _ASMLANGUAGE */
316 : :
317 : : #endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_INTERNAL_H_ */
|