extra stability for FreeRTOS, what about?

nobody wrote on Tuesday, August 29, 2006:

Hello,

I have idea to ensure additional "watchdog" for FreeRTOS. There is possibility to implement a little memory controller for our tasks. In tasks.c tskTCB structure includes pxTopOfStack, pxStack and usStackDepth. We can add into OS clock code (for example into vTaskIncrement) special function to control stack of every tasks and to generate exceptions…
/*if ((pxTopOfStack-pxStack > usStackDepth) || (pxStack > pxTopOfStack)) call_an_exteption()*/
It could be good for debug mode to estimate needed size of a task stack…

what do you think about?

best regards
Janusz

P.S. My FreeRTOS (currently 4.0.5) is every day more and more stable with lwIP (currently 1.1.1) cooperation. The PPP (with reduced bugs) almost works fine! There is a huge amount of learning…

nobody wrote on Wednesday, August 30, 2006:

I have used that technique lots of time ;-)  I have it compiled in for debug builds only.

nobody wrote on Thursday, August 31, 2006:

I was blind:

void vTaskList( portCHAR *pcWriteBuffer );

configUSE_TRACE_FACILITY, INCLUDE_vTaskDelete and INCLUDE_vTaskSuspend must all be defined as 1 for this function to be available. See the configuration section for more information.

NOTE: This function will disable interrupts for its duration. It is not intended for normal application runtime use but as a debug aid.

Lists all the current tasks, along with their current state and stack usage high water mark.

Tasks are reported as blocked (‘B’), ready (‘R’), deleted (‘D’) or suspended (‘S’).

and writetotracebuffer…

best rgs

nobody wrote on Friday, September 01, 2006:

it works:
tasks.c
/*-----------------------------------------------------------*/

#if 1
// — JU
extern void kprintfFromISR(const char *format,…);

static void prvListTaskWithinSingleListPrintf(xList *pxList, signed portCHAR cStatus )
{
    volatile tskTCB *pxNextTCB, *pxFirstTCB;
    unsigned portSHORT usStackRemaining;

    /* Write the details of all the TCB’s in pxList into the buffer. */
    listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
    do {
        listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
        usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned portCHAR * ) pxNextTCB->pxStack );
        kprintfFromISR("%s\t\t%c\t%u\t%u\t\t%u\r\n",
                    pxNextTCB->pcTaskName, cStatus,
                    ( unsigned int ) pxNextTCB->uxPriority,
                    usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
    } while( pxNextTCB != pxFirstTCB );
}
   
void vTaskPrintCurrentFromISR()
{
    unsigned portBASE_TYPE uxQueue;        // vTaskList
   
    if ( pxCurrentTCB == NULL ) {
        kprintfFromISR(“there is no tasks!\n”);
        return;
    }

    kprintfFromISR("current task %s, number 0x%x, priority %d\n",
                    pxCurrentTCB->pcTaskName,
                    pxCurrentTCB->uxTCBNumber,
                    pxCurrentTCB->uxPriority);
    kprintfFromISR("top of stack 0x%x, start of stack 0x%x, depth 0x%x\n",
                    pxCurrentTCB->pxTopOfStack,
                    pxCurrentTCB->pxStack,
                    pxCurrentTCB->usStackDepth);
                   
    // only for ARM7
    kprintfFromISR("R15(PC) = 0x%08x+instr, R14(LR) = 0x%08x\n", *pxCurrentTCB->pxTopOfStack, *(pxCurrentTCB->pxTopOfStack-1));
    kprintfFromISR("R13(SP) = 0x%08x,       R12     = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack-2), *(pxCurrentTCB->pxTopOfStack-3));
    kprintfFromISR(" R11 = 0x%08x, R10 = 0x%08x, ", *(pxCurrentTCB->pxTopOfStack-4), *(pxCurrentTCB->pxTopOfStack-5));
    kprintfFromISR(" R9  = 0x%08x, R8  = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack-6), *(pxCurrentTCB->pxTopOfStack-7));
    kprintfFromISR(" R7  = 0x%08x, R6  = 0x%08x, ", *(pxCurrentTCB->pxTopOfStack-8), *(pxCurrentTCB->pxTopOfStack-9));
    kprintfFromISR(" R5  = 0x%08x, R4  = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack-10), *(pxCurrentTCB->pxTopOfStack-11));       
    kprintfFromISR(" R3  = 0x%08x, R2  = 0x%08x, ", *(pxCurrentTCB->pxTopOfStack-12), *(pxCurrentTCB->pxTopOfStack-13));       
    kprintfFromISR(" R1  = 0x%08x, R0  = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack-14), *(pxCurrentTCB->pxTopOfStack-15));
    kprintfFromISR("SPSR = 0x%08x, critical nesting = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack-16), *(pxCurrentTCB->pxTopOfStack-17));       
   
    // vTaskList
    /* This is a VERY costly function that should be used for debug only.
    It leaves interrupts disabled for a LONG time. */

        /* Run through all the lists that could potentially contain a TCB and
        report the task name, state and stack high water mark. */
       
    kprintfFromISR("\ntask name\tstatus\tprior\tstack remaining\ttask number\n");   

    uxQueue = uxTopUsedPriority + 1;

    do {
        uxQueue–;

        if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
        {
            prvListTaskWithinSingleListPrintf(( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );           
        }
    } while( uxQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );

    if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
    {
        prvListTaskWithinSingleListPrintf(( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
    }

    if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
    {
        prvListTaskWithinSingleListPrintf(( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
    }

    if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
    {
        prvListTaskWithinSingleListPrintf(( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
    }

    if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
    {
        prvListTaskWithinSingleListPrintf(( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
    }
}
#endif

void vTaskSwitchContext( void )
{
#if 1
    // JU - debug
    if ((pxCurrentTCB->pxTopOfStack - pxCurrentTCB->pxStack > pxCurrentTCB->usStackDepth) ||
        (pxCurrentTCB->pxStack > pxCurrentTCB->pxTopOfStack)) {
        portENTER_CRITICAL();
        kprintfFromISR("\nKernel panic: stack overflow\n");
        vTaskPrintCurrentFromISR();
        while(1);    // halt
    }
#endif

    if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
    {
        /* The scheduler is currently suspended - do not allow a context
        switch. */
        xMissedYield = pdTRUE;
        return;
    }

    /* Find the highest priority queue that contains ready tasks. */
    while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
    {
        --uxTopReadyPriority;
    }

    /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
    same priority get an equal share of the processor time. */
    listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );
    vWriteTraceToBuffer();
}
/*-----------------------------------------------------------*/

portISR.c
//---------------------------------------------------------------------------------- interrupt exceptions (JU)  -------------------------------------------------------------------------
void Undef_Handler( void ) __attribute__((naked));
void PAbt_Handler( void ) __attribute__((naked));
void DAbt_Handler( void ) __attribute__((naked));

void Undef_Handler( void ) {
    portENTER_CRITICAL();
    kprintfFromISR("\nKernel panic: Undef handler\n");
    vTaskPrintCurrentFromISR();
    while(1);        // halt
}

void PAbt_Handler( void ) {
    portENTER_CRITICAL();
    kprintfFromISR("\nKernel panic: Program Abort handler\n");
    vTaskPrintCurrentFromISR();
    while(1);        // halt
}

void DAbt_Handler( void ) {
    portENTER_CRITICAL();
    kprintfFromISR("\nKernel panic: Data Abort handler\n");
    vTaskPrintCurrentFromISR();
    while(1);        // halt
}

maybe it will be useful for you

Janusz

nobody wrote on Friday, September 01, 2006:

there is mistake, sorry:
void vTaskPrintCurrentFromISR()
{
    unsigned portBASE_TYPE uxQueue;        // vTaskList
    #define RAM_MAX        0x210000
   
    if ( pxCurrentTCB == NULL ) {
        kprintfFromISR("there is no tasks!\n");
        return;
    }
    if ( pxCurrentTCB > RAM_MAX) {        // bad pointer protection
        kprintfFromISR("pxCurrentTCB failure!\n");
        return;
    }

    kprintfFromISR("current task %s, number 0x%x, priority %d\n",
                    pxCurrentTCB->pcTaskName,
                    pxCurrentTCB->uxTCBNumber,
                    pxCurrentTCB->uxPriority);
    kprintfFromISR("top of stack 0x%x, start of stack 0x%x, depth 0x%x\n",
                    pxCurrentTCB->pxTopOfStack,
                    pxCurrentTCB->pxStack,
                    pxCurrentTCB->usStackDepth);
                   
    if ( pxCurrentTCB->pxTopOfStack+17 > RAM_MAX) {        // bad pointer protection
        kprintfFromISR("pxTopOfStack failure!\n");
        return;
    }
                   
    // only for ARM7
    kprintfFromISR("critical nesting = 0x%08x, SPSR = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack+0), *(pxCurrentTCB->pxTopOfStack+1));       
    kprintfFromISR(" R0  = 0x%08x, R1  = 0x%08x, ", *(pxCurrentTCB->pxTopOfStack+2), *(pxCurrentTCB->pxTopOfStack+3));
    kprintfFromISR(" R2  = 0x%08x, R3  = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack+4), *(pxCurrentTCB->pxTopOfStack+5));       
    kprintfFromISR(" R4  = 0x%08x, R5  = 0x%08x, ", *(pxCurrentTCB->pxTopOfStack+6), *(pxCurrentTCB->pxTopOfStack+7));       
    kprintfFromISR(" R6  = 0x%08x, R7  = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack+8), *(pxCurrentTCB->pxTopOfStack+9));
    kprintfFromISR(" R8  = 0x%08x, R9  = 0x%08x, ", *(pxCurrentTCB->pxTopOfStack+10), *(pxCurrentTCB->pxTopOfStack+11));
    kprintfFromISR(" R10 = 0x%08x, R11 = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack+12), *(pxCurrentTCB->pxTopOfStack+13));
    kprintfFromISR(" R12    = 0x%08x,       R13(SP) = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack+14), *(pxCurrentTCB->pxTopOfStack+15));       
    kprintfFromISR("R14(LR) = 0x%08x+instr, R15(PC) = 0x%08x\n", *(pxCurrentTCB->pxTopOfStack+16), *(pxCurrentTCB->pxTopOfStack+17));
   
    // vTaskList

Janusz

nobody wrote on Friday, September 01, 2006:

In addtition portENTER_CRITICAL(); is negligible.