1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef NSI_COMMON_SRC_INCL_NSI_CPU_IF_H
8 #define NSI_COMMON_SRC_INCL_NSI_CPU_IF_H
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include "nsi_cpu_if_internal.h"
15 
16 /*
17  * Any symbol annotated by these macros will be visible outside of the
18  * embedded SW library, both by the native simulator runner,
19  * and other possible embedded CPU's SW.
20  */
21 #define NATIVE_SIMULATOR_IF_SECT(sect) __attribute__((visibility("default"))) \
22 	__attribute__((__section__(sect)))
23 #define NATIVE_SIMULATOR_IF NATIVE_SIMULATOR_IF_SECT(".native_sim_if")
24 #define NATIVE_SIMULATOR_IF_DATA NATIVE_SIMULATOR_IF_SECT(".native_sim_if.data")
25 #define NATIVE_SIMULATOR_IF_TEXT NATIVE_SIMULATOR_IF_SECT(".native_sim_if.text")
26 
27 /*
28  * Implementation note:
29  * The interface between the embedded SW and the native simulator is allocated in its
30  * own section to allow the embedded software developers to, using a linker script,
31  * direct the linker to keep those symbols even when doing its linking with garbage collection.
32  * It is also be possible for the embedded SW to require the linker to keep those
33  * symbols by requiring each of them to be kept explicitly by name (either by defining them
34  * as entry points, or as required in the output).
35  * It is also possible for the embedded SW developers to not use garbage collection
36  * during their SW linking.
37  */
38 
39 
40 /*
41  * Interfaces the Native Simulator _expects_ from the embedded CPUs:
42  */
43 
44 /*
45  * Called during the earliest initialization (before command line parsing)
46  *
47  * The embedded SW library may provide this function to perform any
48  * early initialization, including registering its own command line arguments
49  * in the runner.
50  */
51 NATIVE_SIMULATOR_IF void nsif_cpu0_pre_cmdline_hooks(void);
52 
53 /*
54  * Called during initialization (before the HW models are initialized)
55  *
56  * The embedded SW library may provide this function to perform any
57  * early initialization, after the command line arguments have been parsed.
58  */
59 NATIVE_SIMULATOR_IF void nsif_cpu0_pre_hw_init_hooks(void);
60 
61 /*
62  * Called by the runner to boot the CPU.
63  *
64  * The embedded SW library must provide this function.
65  * This function is expected to return after the embedded CPU
66  * has gone to sleep for the first time.
67  *
68  * The expectation is that the embedded CPU SW will spawn a
69  * new pthread while in this call, and run the embedded SW
70  * initialization in that pthread.
71  *
72  * It is recommended for the embedded SW to use the NCE (CPU start/stop emulation)
73  * component to achieve this.
74  */
75 NATIVE_SIMULATOR_IF void nsif_cpu0_boot(void);
76 
77 /*
78  * Called by the runner when the simulation is ending/exiting
79  *
80  * The embedded SW library may provide this function.
81  * to do any cleanup it needs.
82  */
83 NATIVE_SIMULATOR_IF int nsif_cpu0_cleanup(void);
84 
85 /*
86  * Called by the runner each time an interrupt is raised by the HW
87  *
88  * The embedded SW library must provide this function.
89  * This function is expected to return after the embedded CPU
90  * has gone back to sleep.
91  */
92 NATIVE_SIMULATOR_IF void nsif_cpu0_irq_raised(void);
93 
94 /*
95  * Called by the runner each time an interrupt is raised in SW context itself.
96  * That is, when a embedded SW action in the HW models, causes an immediate
97  * interrupt to be raised (while the execution is still in the
98  * context of the calling SW thread).
99  */
100 NATIVE_SIMULATOR_IF void nsif_cpu0_irq_raised_from_sw(void);
101 
102 /*
103  * Optional hook which may be used for test functionality.
104  * When the runner HW models use them and for what is up to those
105  * specific models.
106  */
107 NATIVE_SIMULATOR_IF int nsif_cpu0_test_hook(void *p);
108 
109 /* Provide prototypes for all n instances of these hooks */
110 F_TRAMP_LIST(NATIVE_SIMULATOR_IF void nsif_cpu, _pre_cmdline_hooks(void))
111 F_TRAMP_LIST(NATIVE_SIMULATOR_IF void nsif_cpu, _pre_hw_init_hooks(void))
112 F_TRAMP_LIST(NATIVE_SIMULATOR_IF void nsif_cpu, _boot(void))
113 F_TRAMP_LIST(NATIVE_SIMULATOR_IF int nsif_cpu, _cleanup(void))
114 F_TRAMP_LIST(NATIVE_SIMULATOR_IF void nsif_cpu, _irq_raised(void))
115 F_TRAMP_LIST(NATIVE_SIMULATOR_IF void nsif_cpu, _irq_raised_from_sw(void))
116 F_TRAMP_LIST(NATIVE_SIMULATOR_IF int nsif_cpu, _test_hook(void *p))
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif /* NSI_COMMON_SRC_INCL_NSI_CPU_IF_H */
123