1 /*
2  * Copyright (c) 2019 Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @file
8  * @brief Private kernel definitions (ARM)
9  *
10  * This file contains private kernel function definitions and various
11  * other definitions for the 32-bit ARM Cortex-A/R processor architecture
12  * family.
13  *
14  * This file is also included by assembly language files which must #define
15  * _ASMLANGUAGE before including this header file.  Note that kernel
16  * assembly source files obtains structure offset values via "absolute symbols"
17  * in the offsets.o module.
18  */
19 
20 #ifndef ZEPHYR_ARCH_ARM_INCLUDE_CORTEX_A_R_KERNEL_ARCH_FUNC_H_
21 #define ZEPHYR_ARCH_ARM_INCLUDE_CORTEX_A_R_KERNEL_ARCH_FUNC_H_
22 
23 #include <zephyr/platform/hooks.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #ifndef _ASMLANGUAGE
30 
arch_kernel_init(void)31 static ALWAYS_INLINE void arch_kernel_init(void)
32 {
33 #ifdef CONFIG_SOC_PER_CORE_INIT_HOOK
34 	soc_per_core_init_hook();
35 #endif /* CONFIG_SOC_PER_CORE_INIT_HOOK */
36 }
37 
38 #ifndef CONFIG_USE_SWITCH
39 
arch_swap(unsigned int key)40 static ALWAYS_INLINE int arch_swap(unsigned int key)
41 {
42 	/* store off key and return value */
43 	arch_current_thread()->arch.basepri = key;
44 	arch_current_thread()->arch.swap_return_value = -EAGAIN;
45 
46 	z_arm_cortex_r_svc();
47 	irq_unlock(key);
48 
49 	/* Context switch is performed here. Returning implies the
50 	 * thread has been context-switched-in again.
51 	 */
52 	return arch_current_thread()->arch.swap_return_value;
53 }
54 
55 static ALWAYS_INLINE void
arch_thread_return_value_set(struct k_thread * thread,unsigned int value)56 arch_thread_return_value_set(struct k_thread *thread, unsigned int value)
57 {
58 	thread->arch.swap_return_value = value;
59 }
60 
61 #else
62 
arch_switch(void * switch_to,void ** switched_from)63 static ALWAYS_INLINE void arch_switch(void *switch_to, void **switched_from)
64 {
65 	extern void z_arm_context_switch(struct k_thread *new,
66 					struct k_thread *old);
67 
68 	struct k_thread *new = switch_to;
69 	struct k_thread *old = CONTAINER_OF(switched_from, struct k_thread,
70 					    switch_handle);
71 
72 	z_arm_context_switch(new, old);
73 }
74 
75 #endif
76 
77 extern FUNC_NORETURN void z_arm_userspace_enter(k_thread_entry_t user_entry,
78 					       void *p1, void *p2, void *p3,
79 					       uint32_t stack_end,
80 					       uint32_t stack_start);
81 
82 extern void z_arm_fatal_error(unsigned int reason, const struct arch_esf *esf);
83 
84 #endif /* _ASMLANGUAGE */
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #endif /* ZEPHYR_ARCH_ARM_INCLUDE_CORTEX_A_R_KERNEL_ARCH_FUNC_H_ */
91