This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS: hwi stack corrupts my stack



Tool/software: TI-RTOS





When I have the below class object created in  he main(),
I had issuing runtime errors, and when I debugged it I have seen that object is corrupted by
hwi ISR stack. And I observed both in
memory view and by putting a hw breakpoint to my stack top.For double checking, I have put a dummy array (stackProtectBuffer) into the class,
and I have seen that array is also courupted. Dummy array solved my runtime
issue by this way.

class SmConfigCls: public ActiveCls {
 
    public:
 
        InterCoreCls interCoreObj;
        SystemManagerCls systemManagerObj;
        SensorCls SensorsObj;
        MissionComputerCls missionComputerObj;
        MaintenanceCls maintenanceObj;
        BitCls bitObj;
        ViewCls viewObj;
        char stackProtectBuffer[1024];
 
 
 
 
    private:
        void initRelations();
 
    public:
        SmConfigCls() :
                interCoreObj(CoreEnu_systemManager),
                        ActiveCls(4)
        {
            memset(stackProtectBuffer, 0, sizeof(stackProtectBuffer));
            initRelations();
        }
 
        void* Run();
};



Int main(int cmdCount, char* commands[]) {
 
    SmConfigCls sm;
 
    BIOS_start(); /* does not return */
    return (0);
}



I think there may be a bug in the kernel. Have a look at the following lines
in the family/c64p/Hwi.c

    Hwi_module->isrStack = Hwi_getIsrStackAddress() - 8;

    Hwi_module->taskSP = (Char *)-1;/* signal that we're executing on the */
                                        /* ISR stack */

and here is the Hwi_getIsrStackAddress function in the same file.

Char *Hwi_getIsrStackAddress()
{
    __extern __FAR__ char _stack[8];
    __extern __FAR__ UInt8 __TI_STACK_SIZE;
    UInt32 isrStack;

    isrStack = (UInt32)_stack;
    isrStack += (UInt32)&__TI_STACK_SIZE;
    isrStack -= 0x1;

    isrStack &= ~0x7;   /* align to long word */

    return ((Char *)isrStack);
}


As I understand hwi stack is put just on Program.stack and I think there may be bug in above code?

my device is 6678 and bios version is 6.46.0.23. C6000 compiler verison is 8.1.1 but I also tried 7.4.11.

  • Did you try increasing the size of the stack. You could simply be using more than you realize.

    Todd
  • I mean hwi stack is put just above where porgram.stack begins(not to program.stack+ stack_size).
    And yes, I made it large enough(64 mb) which is much less than my app needs.
    I am also curious about the expression
    isrStack = (UInt32)_stack;
    isrStack += (UInt32)&__TI_STACK_SIZE;
    isrStack -= 0x1;

    napyonsen
  • Once you call BIOS_start(), any local variables in main() are implicitly "freed", by virtue of what you are observing - main() is operating on the Hwi stack (or, more correctly, on the C stack that is then taken over by Hwi).  Your C++ local var in main() can not be used after calling BIOS_start().  You should convert it to global scope.

    napyonsen said:
    I am also curious about the expression
    isrStack = (UInt32)_stack;
    isrStack += (UInt32)&__TI_STACK_SIZE;
    isrStack -= 0x1;

    This code is finding the address of the topmost byte on the Hwi stack (which grows down).

    Regards,

    - Rob

  • Thanks Rob. This answer also clarifies my other therads not answered well:
    e2e.ti.com/.../567988 and
    e2e.ti.com/.../569901

    All the problems was because of this issuse.

    I wish you had seen this threads before I deep into detailed debug. Thanks again.

    Are you going to change this behavior or should I use global creation work around?
    By the way, since I am obsessive with not using globals, I created SmConfigObj static inside the main,
    and this one worked,too. Do you confirm that? Static objects in the main() are like globals by the nature and
    use .data section but not the Program.stack?
  • napyonsen said:
    All the problems was because of this issuse.

    I wish you had seen this threads before I deep into detailed debug. Thanks again.

    You're welcome, it is my pleasure.

    We do a rotation support system and this is one of my weeks, so I wasn't paying attention to Forum issues during the time you posted the other threads.  But one of them did mention that non-static local vars in main() are wiped out upon calling BIOS_start().

    napyonsen said:
    Are you going to change this behavior or should I use global creation work around?

    There are no plans to change this C stack re-use behavior.

    napyonsen said:
    By the way, since I am obsessive with not using globals, I created SmConfigObj static inside the main,
    and this one worked,too. Do you confirm that? Static objects in the main() are like globals by the nature and
    use .data section but not the Program.stack?

    That is a wise approach about not using globals.

    I can confirm that using a static local in main() is a good solution, since they are not allocated on the stack.

    Regards,

    - Rob