ChibiOS  21.6.0
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  */
62 static 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.*/
112  ch_pqueue_init(&oip->rlist.pqueue);
113 
114 #if (CH_CFG_USE_REGISTRY == TRUE) && (CH_CFG_SMP_MODE == FALSE)
115  /* Registry initialization when SMP mode is disabled.*/
116  __reg_object_init(&oip->reglist);
117 #endif
118 
119 #if CH_CFG_SMP_MODE == FALSE
120  /* RFCU initialization when SMP mode is disabled.*/
121  __rfcu_object_init(&oip->rfcu);
122 #endif
123 
124  /* Virtual timers list initialization.*/
125  __vt_object_init(&oip->vtlist);
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  /* User instance initialization hook.*/
164 
165 #if CH_CFG_NO_IDLE_THREAD == FALSE
166  {
167  thread_descriptor_t idle_descriptor = {
168  .name = "idle",
169  .wbase = oicp->idlethread_base,
170  .wend = oicp->idlethread_end,
171  .prio = IDLEPRIO,
172  .funcp = __idle_thread,
173  .arg = NULL
174  };
175 
176  /* This thread has the lowest priority in the system, its role is just to
177  serve interrupts in its context while keeping the lowest energy saving
178  mode compatible with the system status.*/
179  (void) chThdCreateI(&idle_descriptor);
180  }
181 #endif
182 }
183 
184 /** @} */
IDLEPRIO
#define IDLEPRIO
Idle priority.
Definition: chschd.h:52
core_id_t
unsigned core_id_t
Type of a core identifier.
Definition: chearly.h:128
ch_os_instance::vtlist
virtual_timers_list_t vtlist
Virtual timers delta list header.
Definition: chobjects.h:402
ch_os_instance::trace_buffer
trace_buffer_t trace_buffer
Trace buffer.
Definition: chobjects.h:438
chDbgAssert
#define chDbgAssert(c, r)
Condition assertion.
Definition: chdebug.h:144
ch_ready_list::current
thread_t * current
The currently running thread.
Definition: chobjects.h:357
ch_os_instance::reglist
registry_t reglist
Registry header.
Definition: chobjects.h:409
ch_os_instance
System instance data structure.
Definition: chobjects.h:394
ch_os_instance::rfcu
rfcu_t rfcu
Runtime Faults Collection Unit for this instance.
Definition: chobjects.h:420
ch_os_instance::config
const os_instance_config_t * config
Pointer to the instance configuration data.
Definition: chobjects.h:425
chInstanceObjectInit
void chInstanceObjectInit(os_instance_t *oip, const os_instance_config_t *oicp)
Initializes a system instance.
Definition: chinstances.c:89
ch_os_instance::kernel_stats
kernel_stats_t kernel_stats
Global kernel statistics.
Definition: chobjects.h:444
chThdCreateI
thread_t * chThdCreateI(const thread_descriptor_t *tdp)
Creates a new thread into a static memory area.
Definition: chthreads.c:270
__dbg_object_init
static void __dbg_object_init(system_debug_t *sdp)
Debug support initialization.
Definition: chdebug.h:192
ch_system
Type of system data structure.
Definition: chobjects.h:457
ch_os_instance_config::mainthread_base
stkalign_t * mainthread_base
Lower limit of the main function thread stack.
Definition: chobjects.h:373
ch_os_instance_config::idlethread_base
stkalign_t * idlethread_base
Lower limit of the dedicated idle thread stack.
Definition: chobjects.h:383
ch_thread::wabase
stkalign_t * wabase
Working area base address.
Definition: chobjects.h:201
CH_STATE_CURRENT
#define CH_STATE_CURRENT
Currently running.
Definition: chschd.h:64
CH_CFG_IDLE_LOOP_HOOK
#define CH_CFG_IDLE_LOOP_HOOK()
Idle Loop hook.
Definition: rt/templates/chconf.h:771
__idle_thread
static void __idle_thread(void *p)
This function implements the idle thread infinite loop.
Definition: chinstances.c:62
ch_os_instance::mainthread
thread_t mainthread
Main thread descriptor.
Definition: chobjects.h:429
thread_descriptor_t
Type of a thread descriptor.
Definition: chthreads.h:57
__vt_object_init
static void __vt_object_init(virtual_timers_list_t *vtlp)
Virtual Timers instance initialization.
Definition: chvt.h:511
CH_CFG_OS_INSTANCE_INIT_HOOK
#define CH_CFG_OS_INSTANCE_INIT_HOOK(oip)
OS instance initialization hook.
Definition: rt/templates/chconf.h:688
ch_pqueue_init
static void ch_pqueue_init(ch_priority_queue_t *pqp)
Priority queue initialization.
Definition: chlists.h:336
__rfcu_object_init
static void __rfcu_object_init(rfcu_t *rfcup)
Runtime Faults Collection Unit initialization.
Definition: chrfcu.h:104
ch_os_instance::dbg
system_debug_t dbg
System debug.
Definition: chobjects.h:433
thread_descriptor_t::name
const char * name
Thread name.
Definition: chthreads.h:61
ch_thread::state
tstate_t state
Current thread state.
Definition: chobjects.h:206
__thd_object_init
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
__stats_object_init
static void __stats_object_init(kernel_stats_t *ksp)
Statistics initialization.
Definition: chstats.h:99
ch_os_instance::core_id
core_id_t core_id
Core associated to this instance.
Definition: chobjects.h:414
ch_os_instance_config
Type of an system instance configuration.
Definition: chobjects.h:363
port_wait_for_interrupt
static void port_wait_for_interrupt(void)
Enters an architecture-dependent IRQ-waiting mode.
Definition: chcore.h:411
__trace_object_init
void __trace_object_init(trace_buffer_t *tbp)
Circular trace buffer initialization.
Definition: chtrace.c:88
ch_system::instances
os_instance_t * instances[PORT_CORES_NUMBER]
Initialized OS instances or NULL.
Definition: chobjects.h:465
ch_os_instance::rlist
ready_list_t rlist
Ready list header.
Definition: chobjects.h:398
__reg_object_init
static void __reg_object_init(registry_t *rp)
Initializes a registry.
Definition: chregistry.h:134
ch_os_instance_config::idlethread_end
stkalign_t * idlethread_end
Upper limit of the dedicated idle thread stack.
Definition: chobjects.h:387
ch_ready_list::pqueue
ch_priority_queue_t pqueue
Threads ordered queues header.
Definition: chobjects.h:353
NORMALPRIO
#define NORMALPRIO
Normal priority.
Definition: chschd.h:54