2015-04-10 16:44:37 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2015-10-06 11:00:37 -05:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-04-10 16:44:37 -07:00
|
|
|
*
|
2015-10-06 11:00:37 -05:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-10 16:44:37 -07:00
|
|
|
*
|
2015-10-06 11:00:37 -05:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
2015-12-04 10:09:39 -05:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Nanokernel initialization module
|
|
|
|
*
|
2015-10-20 09:42:33 -07:00
|
|
|
* This module contains routines that are used to initialize the nanokernel.
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-05-12 16:15:58 -04:00
|
|
|
#include <offsets.h>
|
|
|
|
#include <nanokernel.h>
|
|
|
|
#include <misc/printk.h>
|
|
|
|
#include <drivers/rand32.h>
|
2015-04-10 16:44:37 -07:00
|
|
|
#include <sections.h>
|
2015-05-12 16:15:58 -04:00
|
|
|
#include <toolchain.h>
|
2015-06-19 11:07:02 -04:00
|
|
|
#include <nano_private.h>
|
2015-06-24 08:22:56 -07:00
|
|
|
#include <device.h>
|
|
|
|
#include <init.h>
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-05-12 16:15:58 -04:00
|
|
|
/* kernel build timestamp items */
|
|
|
|
|
|
|
|
#define BUILD_TIMESTAMP "BUILD: " __DATE__ " " __TIME__
|
|
|
|
|
|
|
|
#ifdef CONFIG_BUILD_TIMESTAMP
|
|
|
|
const char * const build_timestamp = BUILD_TIMESTAMP;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* boot banner items */
|
|
|
|
|
2015-06-05 10:19:43 -04:00
|
|
|
#define BOOT_BANNER "****** BOOTING ZEPHYR OS ******"
|
2015-05-12 16:15:58 -04:00
|
|
|
|
|
|
|
#if !defined(CONFIG_BOOT_BANNER)
|
|
|
|
#define PRINT_BOOT_BANNER() do { } while (0)
|
|
|
|
#elif !defined(CONFIG_BUILD_TIMESTAMP)
|
|
|
|
#define PRINT_BOOT_BANNER() printk(BOOT_BANNER "\n")
|
|
|
|
#else
|
|
|
|
#define PRINT_BOOT_BANNER() printk(BOOT_BANNER " %s\n", build_timestamp)
|
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-06-19 10:42:12 -04:00
|
|
|
/* boot time measurement items */
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOOT_TIME_MEASUREMENT
|
|
|
|
uint64_t __noinit __start_tsc; /* timestamp when kernel starts */
|
2015-09-29 13:41:42 -04:00
|
|
|
uint64_t __noinit __main_tsc; /* timestamp when main task starts */
|
2015-06-19 10:42:12 -04:00
|
|
|
uint64_t __noinit __idle_tsc; /* timestamp when CPU goes idle */
|
|
|
|
#endif
|
|
|
|
|
2015-05-25 12:22:44 -04:00
|
|
|
/* random number generator items */
|
|
|
|
#if defined(CONFIG_TEST_RANDOM_GENERATOR) || \
|
|
|
|
defined(CONFIG_CUSTOM_RANDOM_GENERATOR)
|
|
|
|
#define RAND32_INIT() sys_rand32_init()
|
|
|
|
#else
|
|
|
|
#define RAND32_INIT()
|
|
|
|
#endif
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
/* stack space for the background (or idle) task */
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-05-22 11:37:03 -04:00
|
|
|
char __noinit __stack main_task_stack[CONFIG_MAIN_STACK_SIZE];
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
/*
|
2015-05-12 17:04:01 -04:00
|
|
|
* storage space for the interrupt stack
|
2015-04-10 16:44:37 -07:00
|
|
|
*
|
2015-05-12 17:04:01 -04:00
|
|
|
* Note: This area is used as the system stack during nanokernel initialization,
|
|
|
|
* since the nanokernel hasn't yet set up its own stack areas. The dual
|
|
|
|
* purposing of this area is safe since interrupts are disabled until the
|
|
|
|
* nanokernel context switches to the background (or idle) task.
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
2015-04-27 10:28:19 -05:00
|
|
|
char __noinit _interrupt_stack[CONFIG_ISR_STACK_SIZE];
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-06-14 14:19:10 -04:00
|
|
|
#ifdef CONFIG_NANO_TIMEOUTS
|
|
|
|
#include <misc/dlist.h>
|
idle: fix tasks waiting when NANO_TIMEOUTS is enabled
Fix an issue where, if a task is pending on a nano timeout, the duration
it wants to wait is not taken into account by the tickless idle code.
This could cause a system to wait forever, or to the limit of the timer
hardware (which is forever, for all intents and purposes).
This fix is to add one field in the nanokernel data structure for one
task to record the amount of ticks it will wait on a nano timeout. Only
one task has to be able to record this information, since, these waits
being looping busy waits, the task of highest priority is the only task
that can be actively waiting with a nano timeout. If a task of lower
priority was previously waiting, and a new task is now waiting, it means
that the wait of the original task has been interrupted, which will
cause said task to run the busy loop on the object again when it gets
scheduled, and the number of ticks it wants to wait has to be recomputed
and recorded again.
Change-Id: Ibcf0f288fc42d96897642cfee00ab7359716703f
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-01-26 13:47:03 -05:00
|
|
|
#define initialize_nano_timeouts() do { \
|
|
|
|
sys_dlist_init(&_nanokernel.timeout_q); \
|
|
|
|
_nanokernel.task_timeout = TICKS_UNLIMITED; \
|
|
|
|
} while ((0))
|
2015-06-14 14:19:10 -04:00
|
|
|
#else
|
|
|
|
#define initialize_nano_timeouts() do { } while ((0))
|
|
|
|
#endif
|
|
|
|
|
2015-09-29 13:41:42 -04:00
|
|
|
#ifdef CONFIG_NANOKERNEL
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
2015-06-24 08:22:56 -07:00
|
|
|
*
|
2015-09-29 13:41:42 -04:00
|
|
|
* @brief Mainline for nanokernel's background task
|
|
|
|
*
|
|
|
|
* This routine completes kernel initialization by invoking the remaining
|
|
|
|
* init functions, then invokes application's main() routine.
|
2015-06-24 08:22:56 -07:00
|
|
|
*
|
2015-09-29 13:41:42 -04:00
|
|
|
* @return N/A
|
2015-06-24 08:22:56 -07:00
|
|
|
*/
|
|
|
|
static void _main(void)
|
|
|
|
{
|
2015-12-21 11:25:23 -05:00
|
|
|
_sys_device_do_config_level(_SYS_INIT_LEVEL_SECONDARY);
|
2015-10-14 11:17:20 -04:00
|
|
|
_sys_device_do_config_level(_SYS_INIT_LEVEL_NANOKERNEL);
|
|
|
|
_sys_device_do_config_level(_SYS_INIT_LEVEL_APPLICATION);
|
2015-09-29 13:41:42 -04:00
|
|
|
|
2016-01-13 13:02:56 -05:00
|
|
|
#ifdef CONFIG_CPLUSPLUS
|
|
|
|
/* Process the .ctors and .init_array sections */
|
|
|
|
extern void __do_global_ctors_aux(void);
|
|
|
|
extern void __do_init_array_aux(void);
|
|
|
|
__do_global_ctors_aux();
|
|
|
|
__do_init_array_aux();
|
|
|
|
#endif
|
|
|
|
|
2015-09-29 13:41:42 -04:00
|
|
|
extern void main(void);
|
2015-06-24 08:22:56 -07:00
|
|
|
main();
|
|
|
|
}
|
|
|
|
#else
|
2015-09-29 13:41:42 -04:00
|
|
|
/* microkernel has its own implementation of _main() */
|
2015-06-24 08:22:56 -07:00
|
|
|
|
2015-09-29 13:41:42 -04:00
|
|
|
extern void _main(void);
|
2015-06-24 08:22:56 -07:00
|
|
|
#endif
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Initializes nanokernel data structures
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
|
|
|
* This routine initializes various nanokernel data structures, including
|
|
|
|
* the background (or idle) task and any architecture-specific initialization.
|
|
|
|
*
|
|
|
|
* Note that all fields of "_nanokernel" are set to zero on entry, which may
|
|
|
|
* be all the initialization many of them require.
|
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return N/A
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-08-20 11:04:01 -04:00
|
|
|
static void nano_init(struct tcs *dummyOutContext)
|
2015-04-10 16:44:37 -07:00
|
|
|
{
|
|
|
|
/*
|
2015-08-20 11:04:01 -04:00
|
|
|
* Initialize the current execution thread to permit a level of debugging
|
2015-05-12 17:04:01 -04:00
|
|
|
* output if an exception should happen during nanokernel initialization.
|
2015-08-20 11:04:01 -04:00
|
|
|
* However, don't waste effort initializing the fields of the dummy thread
|
|
|
|
* beyond those needed to identify it as a dummy thread.
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
2015-05-08 17:12:45 -05:00
|
|
|
_nanokernel.current = dummyOutContext;
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
/*
|
|
|
|
* Do not insert dummy execution context in the list of fibers, so that it
|
|
|
|
* does not get scheduled back in once context-switched out.
|
|
|
|
*/
|
|
|
|
dummyOutContext->link = (struct tcs *)NULL;
|
|
|
|
|
2015-04-10 16:44:37 -07:00
|
|
|
dummyOutContext->flags = FIBER | ESSENTIAL;
|
|
|
|
dummyOutContext->prio = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The interrupt library needs to be initialized early since a series of
|
|
|
|
* handlers are installed into the interrupt table to catch spurious
|
2015-05-12 17:04:01 -04:00
|
|
|
* interrupts. This must be performed before other nanokernel subsystems
|
2015-04-10 16:44:37 -07:00
|
|
|
* install bonafide handlers, or before hardware device drivers are
|
2015-07-14 10:28:51 -07:00
|
|
|
* initialized.
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
_IntLibInit();
|
|
|
|
|
|
|
|
/*
|
2015-09-29 13:41:42 -04:00
|
|
|
* Initialize the thread control block (TCS) for the main task (either
|
|
|
|
* background or idle task). The entry point for this thread is '_main'.
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
_nanokernel.task = (struct tcs *) main_task_stack;
|
2015-05-25 10:21:57 -04:00
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
_new_thread(main_task_stack, /* pStackMem */
|
2015-04-10 16:44:37 -07:00
|
|
|
CONFIG_MAIN_STACK_SIZE, /* stackSize */
|
2015-08-20 11:04:01 -04:00
|
|
|
(_thread_entry_t)_main, /* pEntry */
|
|
|
|
(_thread_arg_t)0, /* parameter1 */
|
|
|
|
(_thread_arg_t)0, /* parameter2 */
|
|
|
|
(_thread_arg_t)0, /* parameter3 */
|
2015-04-10 16:44:37 -07:00
|
|
|
-1, /* priority */
|
|
|
|
0 /* options */
|
|
|
|
);
|
|
|
|
|
2015-05-12 17:04:01 -04:00
|
|
|
/* indicate that failure of this task may be fatal to the entire system */
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-05-08 17:12:45 -05:00
|
|
|
_nanokernel.task->flags |= ESSENTIAL;
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-06-14 14:19:10 -04:00
|
|
|
initialize_nano_timeouts();
|
|
|
|
|
2015-05-12 17:04:01 -04:00
|
|
|
/* perform any architecture-specific initialization */
|
|
|
|
|
2015-04-10 16:44:37 -07:00
|
|
|
nanoArchInit();
|
|
|
|
}
|
2015-05-12 16:15:58 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_STACK_CANARIES
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
2015-05-12 16:15:58 -04:00
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Initialize the kernel's stack canary
|
2015-05-12 16:15:58 -04:00
|
|
|
*
|
2015-05-12 17:04:01 -04:00
|
|
|
* This macro initializes the kernel's stack canary global variable,
|
|
|
|
* __stack_chk_guard, with a random value.
|
2015-05-12 16:15:58 -04:00
|
|
|
*
|
|
|
|
* INTERNAL
|
2015-05-20 10:33:48 -04:00
|
|
|
* Depending upon the compiler, modifying __stack_chk_guard directly at runtime
|
|
|
|
* may generate a build error. In-line assembly is used as a workaround.
|
2015-05-12 16:15:58 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
extern void *__stack_chk_guard;
|
|
|
|
|
2015-10-09 06:20:52 -04:00
|
|
|
#if defined(CONFIG_X86)
|
2015-05-12 16:15:58 -04:00
|
|
|
#define _MOVE_INSTR "movl "
|
2015-06-05 22:27:08 -04:00
|
|
|
#elif defined(CONFIG_ARM)
|
2015-05-12 16:15:58 -04:00
|
|
|
#define _MOVE_INSTR "str "
|
|
|
|
#else
|
2015-06-05 22:33:49 -04:00
|
|
|
#error "Unknown Architecture type"
|
2015-10-09 06:20:52 -04:00
|
|
|
#endif /* CONFIG_X86 */
|
2015-05-12 16:15:58 -04:00
|
|
|
|
|
|
|
#define STACK_CANARY_INIT() \
|
|
|
|
do { \
|
|
|
|
register void *tmp; \
|
2015-05-25 12:22:44 -04:00
|
|
|
tmp = (void *)sys_rand32_get(); \
|
2015-05-12 16:15:58 -04:00
|
|
|
__asm__ volatile(_MOVE_INSTR "%1, %0;\n\t" \
|
|
|
|
: "=m"(__stack_chk_guard) \
|
|
|
|
: "r"(tmp)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#else /* !CONFIG_STACK_CANARIES */
|
|
|
|
#define STACK_CANARY_INIT()
|
|
|
|
#endif /* CONFIG_STACK_CANARIES */
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Initialize nanokernel
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-17 13:51:37 -04:00
|
|
|
* This routine is invoked when the system is ready to run C code. The
|
|
|
|
* processor must be running in 32-bit mode, and the BSS must have been
|
2015-07-01 17:22:39 -04:00
|
|
|
* cleared/zeroed.
|
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return Does not return
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-05-12 16:15:58 -04:00
|
|
|
FUNC_NORETURN void _Cstart(void)
|
|
|
|
{
|
|
|
|
/* floating point operations are NOT performed during nanokernel init */
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
char dummyTCS[__tTCS_NOFLOAT_SIZEOF];
|
2015-05-12 16:15:58 -04:00
|
|
|
|
|
|
|
/*
|
2015-05-12 17:04:01 -04:00
|
|
|
* Initialize nanokernel data structures. This step includes
|
|
|
|
* initializing the interrupt subsystem, which must be performed
|
|
|
|
* before the hardware initialization phase.
|
2015-05-12 16:15:58 -04:00
|
|
|
*/
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
nano_init((struct tcs *)&dummyTCS);
|
2015-05-12 16:15:58 -04:00
|
|
|
|
|
|
|
/* perform basic hardware initialization */
|
|
|
|
|
2015-10-14 11:17:20 -04:00
|
|
|
_sys_device_do_config_level(_SYS_INIT_LEVEL_PRIMARY);
|
2015-05-12 16:15:58 -04:00
|
|
|
|
2015-05-25 12:22:44 -04:00
|
|
|
/*
|
|
|
|
* Initialize random number generator
|
|
|
|
* As a platform may implement it in hardware, it has to be
|
|
|
|
* initialized after rest of hardware initialization and
|
|
|
|
* before stack canaries that use it
|
|
|
|
*/
|
|
|
|
|
|
|
|
RAND32_INIT();
|
|
|
|
|
2015-05-12 17:04:01 -04:00
|
|
|
/* initialize stack canaries */
|
|
|
|
|
2015-05-12 16:15:58 -04:00
|
|
|
STACK_CANARY_INIT();
|
|
|
|
|
|
|
|
/* display boot banner */
|
|
|
|
|
|
|
|
PRINT_BOOT_BANNER();
|
|
|
|
|
2015-09-29 13:41:42 -04:00
|
|
|
/* context switch to main task (entry function is _main()) */
|
2015-05-12 16:15:58 -04:00
|
|
|
|
|
|
|
_nano_fiber_swap();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compiler can't tell that the above routines won't return and issues
|
|
|
|
* a warning unless we explicitly tell it that control never gets this
|
|
|
|
* far.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CODE_UNREACHABLE;
|
|
|
|
}
|