ChibiOS/RT 7.0.6
chinstances.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file rt/src/chinstances.c
21 * @brief OS instances code.
22 *
23 * @addtogroup instances
24 * @details OS instances management.
25 * @{
26 */
27
28#include "ch.h"
29
30/*===========================================================================*/
31/* Module local definitions. */
32/*===========================================================================*/
33
34/*===========================================================================*/
35/* Module exported variables. */
36/*===========================================================================*/
37
38/*===========================================================================*/
39/* Module local types. */
40/*===========================================================================*/
41
42/*===========================================================================*/
43/* Module local variables. */
44/*===========================================================================*/
45
46/*===========================================================================*/
47/* Module local functions. */
48/*===========================================================================*/
49
50#if (CH_CFG_NO_IDLE_THREAD == FALSE) || defined(__DOXYGEN__)
51/**
52 * @brief This function implements the idle thread infinite loop.
53 * @details The function puts the processor in the lowest power mode capable
54 * to serve interrupts.<br>
55 * The priority is internally set to the minimum system value so
56 * that this thread is executed only if there are no other ready
57 * threads in the system.
58 *
59 * @param[in] p the thread parameter, unused in this scenario
60 */
61static void __idle_thread(void *p) {
62
63 (void)p;
64
65 while (true) {
66 /*lint -save -e522 [2.2] Apparently no side effects because it contains
67 an asm instruction.*/
68 port_wait_for_interrupt();
69 /*lint -restore*/
71 }
72}
73#endif /* CH_CFG_NO_IDLE_THREAD == FALSE */
74
75/*===========================================================================*/
76/* Module exported functions. */
77/*===========================================================================*/
78
79/**
80 * @brief Initializes a system instance.
81 * @note The system instance is in I-Lock state after initialization.
82 *
83 * @param[out] oip pointer to the @p os_instance_t structure
84 * @param[in] oicp pointer to the @p os_instance_config_t structure
85 *
86 * @special
87 */
89 const os_instance_config_t *oicp) {
90 core_id_t core_id;
91
92 /* Registering into the global system structure.*/
93#if CH_CFG_SMP_MODE == TRUE
94 core_id = port_get_core_id();
95#else
96 core_id = 0U;
97#endif
98 chDbgAssert(ch_system.instances[core_id] == NULL, "instance already registered");
99 ch_system.instances[core_id] = oip;
100
101 /* Core associated to this instance.*/
102 oip->core_id = core_id;
103
104 /* Keeping a reference to the configuration data.*/
105 oip->config = oicp;
106
107 /* Port initialization for the current instance.*/
108 port_init(oip);
109
110 /* Ready list initialization.*/
112
113#if (CH_CFG_USE_REGISTRY == TRUE) && (CH_CFG_SMP_MODE == FALSE)
114 /* Registry initialization when SMP mode is disabled.*/
116#endif
117
118#if CH_CFG_SMP_MODE == FALSE
119 /* RFCU initialization when SMP mode is disabled.*/
121#endif
122
123 /* Virtual timers list initialization.*/
125
126 /* Debug support initialization.*/
127 __dbg_object_init(&oip->dbg);
128
129#if CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED
130 /* Trace buffer initialization.*/
132#endif
133
134 /* Statistics initialization.*/
135#if CH_DBG_STATISTICS == TRUE
137#endif
138
139#if CH_CFG_NO_IDLE_THREAD == FALSE
140 /* Now this instructions flow becomes the main thread.*/
141#if CH_CFG_USE_REGISTRY == TRUE
142 oip->rlist.current = __thd_object_init(oip, &oip->mainthread,
143 (const char *)&ch_debug, NORMALPRIO);
144#else
145 oip->rlist.current = __thd_object_init(oip, &oip->mainthread,
146 "main", NORMALPRIO);
147#endif
148#else
149 /* Now this instructions flow becomes the idle thread.*/
150 oip->rlist.current = __thd_object_init(oip, &oip->mainthread,
151 "idle", IDLEPRIO);
152#endif
153
154#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE)
155 oip->rlist.current->wabase = oicp->mainthread_base;
156#endif
157
158 /* Setting up the caller as current thread.*/
160
161#if CH_DBG_STATISTICS == TRUE
162 /* Starting measurement for this thread.*/
164#endif
165
166 /* User instance initialization hook.*/
168
169#if CH_CFG_NO_IDLE_THREAD == FALSE
170 {
171 thread_descriptor_t idle_descriptor = {
172 .name = "idle",
173 .wbase = oicp->idlethread_base,
174 .wend = oicp->idlethread_end,
175 .prio = IDLEPRIO,
176 .funcp = __idle_thread,
177 .arg = NULL
178 };
179
180#if CH_DBG_FILL_THREADS == TRUE
181 __thd_stackfill((uint8_t *)idle_descriptor.wbase,
182 (uint8_t *)idle_descriptor.wend);
183#endif
184
185 /* This thread has the lowest priority in the system, its role is just to
186 serve interrupts in its context while keeping the lowest energy saving
187 mode compatible with the system status.*/
188 (void) chThdCreateI(&idle_descriptor);
189 }
190#endif
191}
192
193/** @} */
ChibiOS/RT main include file.
#define chDbgAssert(c, r)
Condition assertion.
Definition chdebug.h:143
static void __dbg_object_init(system_debug_t *sdp)
Debug support initialization.
Definition chdebug.h:191
#define CH_CFG_OS_INSTANCE_INIT_HOOK(oip)
OS instance initialization hook.
Definition chconf.h:687
#define CH_CFG_IDLE_LOOP_HOOK()
Idle Loop hook.
Definition chconf.h:770
static void __idle_thread(void *p)
This function implements the idle thread infinite loop.
Definition chinstances.c:61
void chInstanceObjectInit(os_instance_t *oip, const os_instance_config_t *oicp)
Initializes a system instance.
Definition chinstances.c:88
static void ch_pqueue_init(ch_priority_queue_t *pqp)
Priority queue initialization.
Definition chlists.h:335
unsigned core_id_t
Type of a core identifier.
Definition chearly.h:127
struct ch_os_instance os_instance_t
Type of an OS instance structure.
Definition chearly.h:137
struct ch_os_instance_config os_instance_config_t
Type of an system instance configuration.
ROMCONST chdebug_t ch_debug
Definition chregistry.c:76
static void __reg_object_init(registry_t *rp)
Initializes a registry.
Definition chregistry.h:150
static void __rfcu_object_init(rfcu_t *rfcup)
Runtime Faults Collection Unit initialization.
Definition chrfcu.h:103
#define CH_STATE_CURRENT
Currently running.
Definition chschd.h:63
#define IDLEPRIO
Idle priority.
Definition chschd.h:51
#define NORMALPRIO
Normal priority.
Definition chschd.h:53
static void __stats_object_init(kernel_stats_t *ksp)
Statistics initialization.
Definition chstats.h:98
thread_t * __thd_object_init(os_instance_t *oip, thread_t *tp, const char *name, tprio_t prio)
Initializes a thread structure.
Definition chthreads.c:88
thread_t * chThdCreateI(const thread_descriptor_t *tdp)
Creates a new thread.
Definition chthreads.c:263
void __thd_stackfill(uint8_t *startp, uint8_t *endp)
Stack fill utility.
Definition chthreads.c:139
NOINLINE void chTMStartMeasurementX(time_measurement_t *tmp)
Starts a measurement.
Definition chtm.c:95
static void __vt_object_init(virtual_timers_list_t *vtlp)
Virtual Timers instance initialization.
Definition chvt.h:508
void __trace_object_init(trace_buffer_t *tbp)
Circular trace buffer initialization.
Definition chtrace.c:87
stkalign_t * mainthread_base
Lower limit of the main function thread stack.
Definition chobjects.h:382
stkalign_t * idlethread_base
Lower limit of the dedicated idle thread stack.
Definition chobjects.h:392
stkalign_t * idlethread_end
Upper limit of the dedicated idle thread stack.
Definition chobjects.h:396
kernel_stats_t kernel_stats
Global kernel statistics.
Definition chobjects.h:453
registry_t reglist
Registry header.
Definition chobjects.h:418
rfcu_t rfcu
Runtime Faults Collection Unit for this instance.
Definition chobjects.h:429
const os_instance_config_t * config
Pointer to the instance configuration data.
Definition chobjects.h:434
thread_t mainthread
Main thread descriptor.
Definition chobjects.h:438
virtual_timers_list_t vtlist
Virtual timers delta list header.
Definition chobjects.h:411
core_id_t core_id
Core associated to this instance.
Definition chobjects.h:423
system_debug_t dbg
System debug.
Definition chobjects.h:442
ready_list_t rlist
Ready list header.
Definition chobjects.h:407
trace_buffer_t trace_buffer
Trace buffer.
Definition chobjects.h:447
thread_t * current
The currently running thread.
Definition chobjects.h:366
ch_priority_queue_t pqueue
Threads ordered queues header.
Definition chobjects.h:362
Type of system data structure.
Definition chobjects.h:466
os_instance_t * instances[PORT_CORES_NUMBER]
Initialized OS instances or NULL.
Definition chobjects.h:474
time_measurement_t stats
Thread statistics.
Definition chobjects.h:346
stkalign_t * wabase
Working area base address.
Definition chobjects.h:198
tstate_t state
Current thread state.
Definition chobjects.h:203
Type of a thread descriptor.
Definition chthreads.h:56
stkalign_t * wbase
Pointer to the working area base.
Definition chthreads.h:64
stkalign_t * wend
Pointer to the working area end.
Definition chthreads.h:68