1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/****************************************************************************
* sched/os_start.c
*
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/types.h>
#include <debug.h>
#include <string.h>
#include <nuttx/arch.h>
#include <nuttx/compiler.h>
#include <nuttx/fs.h>
#include <nuttx/net.h>
#include <nuttx/lib.h>
#include <nuttx/mm.h>
#include <nuttx/init.h>
#include "os_internal.h"
#include "sig_internal.h"
#include "wd_internal.h"
#include "sem_internal.h"
#ifndef CONFIG_DISABLE_MQUEUE
# include "mq_internal.h"
#endif
#ifndef CONFIG_DISABLE_PTHREAD
# include "pthread_internal.h"
#endif
#include "clock_internal.h"
#include "timer_internal.h"
#include "irq_internal.h"
#ifdef CONFIG_SCHED_WORKQUEUE
# include "work_internal.h"
#endif
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Global Variables
****************************************************************************/
/* Task Lists ***************************************************************/
/* The state of a task is indicated both by the task_state field
* of the TCB and by a series of task lists. All of these
* tasks lists are declared below. Although it is not always
* necessary, most of these lists are prioritized so that common
* list handling logic can be used (only the g_readytorun,
* the g_pendingtasks, and the g_waitingforsemaphore lists need
* to be prioritized).
*/
/* This is the list of all tasks that are ready to run. The head
* of this list is the currently active task; the tail of this
* list is always the idle task.
*/
volatile dq_queue_t g_readytorun;
/* This is the list of all tasks that are ready-to-run, but
* cannot be placed in the g_readytorun list because: (1) They
* are higher priority than the currently active task at the head
* of the g_readytorun list, and (2) the currenly active task has
* disabled pre-emption.
*/
volatile dq_queue_t g_pendingtasks;
/* This is the list of all tasks that are blocked waiting for a semaphore */
volatile dq_queue_t g_waitingforsemaphore;
/* This is the list of all tasks that are blocked waiting for a signal */
#ifndef CONFIG_DISABLE_SIGNALS
volatile dq_queue_t g_waitingforsignal;
#endif
/* This is the list of all tasks that are blocked waiting for a message
* queue to become non-empty.
*/
#ifndef CONFIG_DISABLE_MQUEUE
volatile dq_queue_t g_waitingformqnotempty;
#endif
/* This is the list of all tasks that are blocked waiting for a message
* queue to become non-full.
*/
#ifndef CONFIG_DISABLE_MQUEUE
volatile dq_queue_t g_waitingformqnotfull;
#endif
/* This the list of all tasks that have been initialized, but not yet
* activated. NOTE: This is the only list that is not prioritized.
*/
volatile dq_queue_t g_inactivetasks;
/* This is the list of dayed memory deallocations that need to be handled
* within the IDLE loop. These deallocations get queued by sched_free()
* if the OS attempts to deallocate memory while it is within an interrupt
* handler.
*/
volatile sq_queue_t g_delayeddeallocations;
/* This is the value of the last process ID assigned to a task */
volatile pid_t g_lastpid;
/* The following hash table is used for two things:
*
* 1. This hash table greatly speeds the determination of
* a new unique process ID for a task, and
* 2. Is used to quickly map a process ID into a TCB.
* It has the side effects of using more memory and limiting
*
* the number of tasks to CONFIG_MAX_TASKS.
*/
pidhash_t g_pidhash[CONFIG_MAX_TASKS];
/* The task ID of the worker thread */
#ifdef CONFIG_SCHED_WORKQUEUE
pid_t g_worker;
#endif
/* This is a table of task lists. This table is indexed by
* the task state enumeration type (tstate_t) and provides
* a pointer to the associated static task list (if there
* is one) as well as a boolean indication as to if the list
* is an ordered list or not.
*/
const tasklist_t g_tasklisttable[NUM_TASK_STATES] =
{
{ NULL, FALSE }, /* TSTATE_TASK_INVALID */
{ &g_pendingtasks, TRUE }, /* TSTATE_TASK_PENDING */
{ &g_readytorun, TRUE }, /* TSTATE_TASK_READYTORUN */
{ &g_readytorun, TRUE }, /* TSTATE_TASK_RUNNING */
{ &g_inactivetasks, FALSE }, /* TSTATE_TASK_INACTIVE */
{ &g_waitingforsemaphore, TRUE }, /* TSTATE_WAIT_SEM */
#ifndef CONFIG_DISABLE_SIGNALS
{ &g_waitingforsignal, FALSE }, /* TSTATE_WAIT_SIG */
#endif
#ifndef CONFIG_DISABLE_MQUEUE
{ &g_waitingformqnotempty, TRUE }, /* TSTATE_WAIT_MQNOTEMPTY */
{ &g_waitingformqnotfull, TRUE } /* TSTATE_WAIT_MQNOTFULL */
#endif
};
/****************************************************************************
* Private Variables
****************************************************************************/
/* This is the task control block for this thread of execution.
* This thread of execution is the idle task. NOTE: the
* system boots into the idle task. The idle task spawns
* the user init task and the user init task is responsible
* for bringing up the rest of the system
*/
static FAR _TCB g_idletcb;
/* This is the name of the idle task */
static FAR char g_idlename[] = "Idle Task";
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: os_start
* Description: This function is called to initialize the
* operating system and to spawn the user init thread of
* execution
****************************************************************************/
void os_start(void)
{
int init_taskid;
int i;
slldbg("Entry\n");
/* Initialize all task lists */
dq_init(&g_readytorun);
dq_init(&g_pendingtasks);
dq_init(&g_waitingforsemaphore);
#ifndef CONFIG_DISABLE_SIGNALS
dq_init(&g_waitingforsignal);
#endif
#ifndef CONFIG_DISABLE_MQUEUE
dq_init(&g_waitingformqnotfull);
dq_init(&g_waitingformqnotempty);
#endif
dq_init(&g_inactivetasks);
sq_init(&g_delayeddeallocations);
/* Initialize the logic that determine unique process IDs. */
g_lastpid = 0;
for (i = 0; i < CONFIG_MAX_TASKS; i++)
{
g_pidhash[i].tcb = NULL;
g_pidhash[i].pid = INVALID_PROCESS_ID;
}
/* Assign the process ID of ZERO to the idle task */
g_pidhash[ PIDHASH(0)].tcb = &g_idletcb;
g_pidhash[ PIDHASH(0)].pid = 0;
/* Initialize a TCB for this thread of execution. NOTE: The default
* value for most components of the g_idletcb are zero. The entire
* structure is set to zero. Then only the (potentially) non-zero
* elements are initialized. NOTE: The idle task is the only task in
* that has pid == 0 and sched_priority == 0.
*/
bzero((void*)&g_idletcb, sizeof(_TCB));
g_idletcb.task_state = TSTATE_TASK_RUNNING;
g_idletcb.entry.main = (main_t)os_start;
#if CONFIG_TASK_NAME_SIZE > 0
strncpy(g_idletcb.name, g_idlename, CONFIG_TASK_NAME_SIZE-1);
g_idletcb.argv[0] = g_idletcb.name;
#else
g_idletcb.argv[0] = g_idlename;
#endif /* CONFIG_TASK_NAME_SIZE */
/* Then add the idle task's TCB to the head of the ready to run list */
dq_addfirst((FAR dq_entry_t*)&g_idletcb, (FAR dq_queue_t*)&g_readytorun);
/* Initialize the processor-specific portion of the TCB */
up_initial_state(&g_idletcb);
/* Initialize the memory manager */
#ifndef CONFIG_HEAP_BASE
{
FAR void *heap_start;
size_t heap_size;
up_allocate_heap(&heap_start, &heap_size);
mm_initialize(heap_start, heap_size);
}
#else
mm_initialize((void*)CONFIG_HEAP_BASE, CONFIG_HEAP_SIZE);
#endif
/* Initialize the interrupt handling subsystem (if included) */
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (irq_initialize != NULL)
#endif
{
irq_initialize();
}
/* Provide an access point to initialize any user-specific logic very
* early in the initialization sequence. Note that user_ininitialize()
* is called only if it is provided in the link.
*/
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (user_initialize != NULL)
#endif
{
user_initialize();
}
/* Initialize the watchdog facility (if included in the link) */
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (wd_initialize != NULL)
#endif
{
wd_initialize();
}
/* Initialize the POSIX timer facility (if included in the link) */
#ifndef CONFIG_DISABLE_CLOCK
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (clock_initialize != NULL)
#endif
{
clock_initialize();
}
#endif
#ifndef CONFIG_DISABLE_POSIX_TIMERS
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (timer_initialize != NULL)
#endif
{
timer_initialize();
}
#endif
/* Initialize the signal facility (if in link) */
#ifndef CONFIG_DISABLE_SIGNALS
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (sig_initialize != NULL)
#endif
{
sig_initialize();
}
#endif
/* Initialize the semaphore facility. (if in link) */
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (sem_initialize != NULL)
#endif
{
sem_initialize();
}
/* Initialize the named message queue facility (if in link) */
#ifndef CONFIG_DISABLE_MQUEUE
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (mq_initialize != NULL)
#endif
{
mq_initialize();
}
#endif
/* Initialize the thread-specific data facility (if in link) */
#ifndef CONFIG_DISABLE_PTHREAD
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (pthread_initialize != NULL)
#endif
{
pthread_initialize();
}
#endif
/* Initialize the file system (needed to support device drivers) */
#if CONFIG_NFILE_DESCRIPTORS > 0
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (fs_initialize != NULL)
#endif
{
fs_initialize();
}
#endif
/* Initialize the network system */
#ifdef CONFIG_NET
#if 0
if (net_initialize != NULL)
#endif
{
net_initialize();
}
#endif
/* The processor specific details of running the operating system
* will be handled here. Such things as setting up interrupt
* service routines and starting the clock are some of the things
* that are different for each processor and hardware platform.
*/
up_initialize();
/* Initialize the C libraries (if included in the link). This
* is done last because the libraries may depend on the above.
*/
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
if (lib_initialize != NULL)
#endif
{
lib_initialize();
}
/* Create stdout, stderr, stdin */
(void)sched_setupidlefiles(&g_idletcb);
/* Start the worker thread that will perform misc garbage clean-up */
#ifdef CONFIG_SCHED_WORKQUEUE
g_worker = task_create("work", CONFIG_SCHED_WORKPRIORITY,
CONFIG_SCHED_WORKSTACKSIZE,
(main_t)work_thread, (const char **)NULL);
#endif
ASSERT(g_worker != ERROR);
/* Once the operating system has been initialized, the system must be
* started by spawning the user init thread of execution.
*/
sdbg("Starting init thread\n");
#ifndef CONFIG_CUSTOM_STACK
init_taskid = task_create("init", SCHED_PRIORITY_DEFAULT,
CONFIG_USERMAIN_STACKSIZE,
(main_t)user_start, (const char **)NULL);
#else
init_taskid = task_create("init", SCHED_PRIORITY_DEFAULT,
(main_t)user_start, (const char **)NULL);
#endif
ASSERT(init_taskid != ERROR);
/* When control is return to this point, the system is idle. */
sdbg("Beginning Idle Loop\n");
for (;;)
{
/* Peform garbage collection (if it is not being done by the worker
* thread. This cleans-up memory de-allocations that was queued
* because it could not be freed in that execution context (for
* example, if the memory was freed from an interrupt handler).
*/
#ifndef CONFIG_SCHED_WORKQEUE
sched_garbagecollection();
#endif
/* Perform any processor-specific idle state operations */
up_idle();
}
}
|