enter/leave_critical_section: In SMP configuration, may attempt to access the task lists before they have been initialized

This commit is contained in:
Gregory Nutt 2016-05-18 08:21:28 -06:00
parent 72de45b7cf
commit 32838fcc2c
3 changed files with 73 additions and 65 deletions

View File

@ -71,14 +71,15 @@ enum os_initstate_e
OSINIT_BOOT = 1, /* Basic boot up initialization is complete. OS
* services and hardware resources are not yet
* available. */
OSINIT_MEMORY = 2, /* The memory manager has been initialized */
OSINIT_HARDWARE = 3, /* MCU-specific hardware is initialized. Hardware
OSINIT_TASKLISTS = 2, /* Head of ready-to-run/assigned task lists valid */
OSINIT_MEMORY = 3, /* The memory manager has been initialized */
OSINIT_HARDWARE = 4, /* MCU-specific hardware is initialized. Hardware
* resources such as timers and device drivers are
* now avaiable. Low-level OS services sufficient
* to support the hardware are also available but
* the OS has not yet completed its full
* initialization. */
OSINIT_OSREADY = 4 /* The OS is fully initialized and multi-tasking is
OSINIT_OSREADY = 5 /* The OS is fully initialized and multi-tasking is
* active. */
};

View File

@ -530,6 +530,10 @@ void os_start(void)
up_initial_state(&g_idletcb[cpu].cmn);
}
/* Task lists are initialized */
g_os_initstate = OSINIT_TASKLISTS;
/* Initialize RTOS facilities *********************************************/
/* Initialize the semaphore facility. This has to be done very early
* because many subsystems depend upon fully functional semaphores.

View File

@ -41,6 +41,7 @@
#include <sys/types.h>
#include <nuttx/init.h>
#include <nuttx/spinlock.h>
#include <nuttx/sched_note.h>
#include <arch/irq.h>
@ -86,17 +87,12 @@ irqstate_t enter_critical_section(void)
{
FAR struct tcb_s *rtcb;
/* Do nothing if called from an interrupt handler */
if (up_interrupt_context())
{
/* The value returned does not matter. We assume only that it is a
* scalar here.
/* Check if we were called from an interrupt handler and that the tasks
* lists have been initialized.
*/
return (irqstate_t)0;
}
if (!up_interrupt_context() && g_os_initstate >= OSINIT_TASKLISTS)
{
/* Do we already have interrupts disabled? */
rtcb = this_task();
@ -135,6 +131,7 @@ irqstate_t enter_critical_section(void)
sched_note_csection(rtcb, true);
#endif
}
}
/* Then disable interrupts (they may already be disabled, be we need to
* return valid interrupt status in any event).
@ -145,9 +142,11 @@ irqstate_t enter_critical_section(void)
#else /* defined(CONFIG_SCHED_INSTRUMENTATION_CSECTION) */
irqstate_t enter_critical_section(void)
{
/* Check if we were called from an interrupt handler */
/* Check if we were called from an interrupt handler and that the tasks
* lists have been initialized.
*/
if (!up_interrupt_context())
if (!up_interrupt_context() && g_os_initstate >= OSINIT_TASKLISTS)
{
FAR struct tcb_s *rtcb = this_task();
DEBUGASSERT(rtcb != NULL);
@ -175,9 +174,11 @@ irqstate_t enter_critical_section(void)
#ifdef CONFIG_SMP
void leave_critical_section(irqstate_t flags)
{
/* Do nothing if called from an interrupt handler */
/* Check if we were called from an interrupt handler and that the tasks
* lists have been initialized.
*/
if (!up_interrupt_context())
if (!up_interrupt_context() && g_os_initstate >= OSINIT_TASKLISTS)
{
FAR struct tcb_s *rtcb = this_task();
DEBUGASSERT(rtcb != 0 && rtcb->irqcount > 0);
@ -229,6 +230,7 @@ void leave_critical_section(irqstate_t flags)
}
}
}
}
/* Restore the previous interrupt state which may still be interrupts
* disabled (but we don't have a mechanism to verify that now)
@ -236,13 +238,14 @@ void leave_critical_section(irqstate_t flags)
up_irq_restore(flags);
}
}
#else /* defined(CONFIG_SCHED_INSTRUMENTATION_CSECTION) */
void leave_critical_section(irqstate_t flags)
{
/* Check if we were called from an interrupt handler */
/* Check if we were called from an interrupt handler and that the tasks
* lists have been initialized.
*/
if (!up_interrupt_context())
if (!up_interrupt_context() && g_os_initstate >= OSINIT_TASKLISTS)
{
FAR struct tcb_s *rtcb = this_task();
DEBUGASSERT(rtcb != NULL);