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