From 073442ecc5a892004c4dfcb577ed61557106f640 Mon Sep 17 00:00:00 2001 From: Allan Stephens Date: Wed, 9 Nov 2016 07:46:56 -0600 Subject: [PATCH] kernel: Treat aborting by main() as a fatal system error An application-supplied main() routine is now considered to be essential to system operation. Thus, if main() experiences an error that aborts the main thread a fatal system error is raised. Note: If main() completes its work and does a standard return- to-caller the main thread terminates normally. Change-Id: Icc9499f13578299244a856a246ad2a7d34a72f54 Signed-off-by: Allan Stephens --- doc/kernel_v2/threads/system_threads.rst | 12 +++++------- kernel/unified/init.c | 5 +++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/doc/kernel_v2/threads/system_threads.rst b/doc/kernel_v2/threads/system_threads.rst index c6c64b8b84c..4de597273f3 100644 --- a/doc/kernel_v2/threads/system_threads.rst +++ b/doc/kernel_v2/threads/system_threads.rst @@ -17,9 +17,7 @@ The kernel spawns the following system threads. **Main thread** This thread performs kernel initialization, then calls the application's - :cpp:func:`main()` function. If the application does not supply a - :cpp:func:`main()` function, the main thread terminates once initialization - is complete. + :cpp:func:`main()` function (if one is defined). By default, the main thread uses the highest configured preemptible thread priority (i.e. 0). If the kernel is not configured to support preemptible @@ -27,10 +25,10 @@ The kernel spawns the following system threads. priority (i.e. -1). The main thread is an essential thread while it is performing kernel - initialization, which means a fatal system error is raised if the thread - aborts. Once initialization is complete the thread is considered - non-essential, so the termination or aborting of a :cpp:func:`main()` - function supplied by the application does not cause a fatal system error. + initialization or executing the application's :cpp:func:`main()` function; + this means a fatal system error is raised if the thread aborts. If + :cpp:func:`main()` is not defined, or if it executes and then does a normal + return, the main thread terminates normally and no error is raised. **Idle thread** This thread executes when there is no other work for the system to do. diff --git a/kernel/unified/init.c b/kernel/unified/init.c index e8458874d4c..29591daedb0 100644 --- a/kernel/unified/init.c +++ b/kernel/unified/init.c @@ -200,8 +200,6 @@ static void _main(void *unused1, void *unused2, void *unused3) _init_static_threads(); - _main_thread->flags &= ~K_ESSENTIAL; - #ifdef CONFIG_BOOT_TIME_MEASUREMENT /* record timestamp for kernel's _main() function */ extern uint64_t __main_tsc; @@ -219,6 +217,9 @@ static void _main(void *unused1, void *unused2, void *unused3) k_thread_priority_set(_main_thread, MDEF_MAIN_THREAD_PRIORITY); #endif main(); + + /* Terminate thread normally since it has no more work to do */ + _main_thread->flags &= ~K_ESSENTIAL; } void __weak main(void)