the fundemental questions about context switch mechanism in ChibiOS

Discussions and support about ChibiOS/RT, the free embedded RTOS.
hello,tomorrow
Posts: 3
Joined: Fri May 03, 2024 1:10 am

the fundemental questions about context switch mechanism in ChibiOS

Postby hello,tomorrow » Fri May 03, 2024 1:39 am

hi, I'm new to ChibiOS and learning about the magic kernel core of ChibiOS, we know the very important part is about context switch mechanism.


But when I study into the API for chSemWait , I have some doubts and get confused, as I see the calling archetecture:
chSemWait-->chSemWaitS-->chSchGoSleepS-->chSysSwitch-->_port_switch
I found there seems not any intterrupt occured in this process, so my question is

1) in this process, context switch is made without any intterrput like PendSV participate? if it is, how is it possible?
or maybe I miss something here?
2) I'm a little bit familiar with ucos, I know it always uses pendSV of intterrput for context switch which is in contradictory here?
3)a little suggestion: since a lot people is familar with ucos , is there some tutorial explaining for the different mechanisms in the kernel of both to help undering of such noob like me :lol:


expected and very appreciated for your answer!

User avatar
Giovanni
Site Admin
Posts: 14462
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1078 times
Been thanked: 922 times
Contact:

Re: the fundemental questions about context switch mechanism in ChibiOS

Postby Giovanni » Fri May 03, 2024 8:46 am

Hi,

In that port the context switch is performed synchronously and without entering an exception state, look at the asm implementation of port_switch(). It just stacks/unstacks all the EABI function-preserved registers and swap stacks.

Note that there are 2 distinct ARMv7-M ports, the alternate port uses exceptions like you described and it is a bit slower at context switching (it has other advantages). The 2 ports are perfectly interchangeable by changing one line in the makefile.

ChibiOS supports both context switch methods, it is a port-level decision, the portable OS mandates nothing, it just calls port_switch() when needed.

Giovanni

hello,tomorrow
Posts: 3
Joined: Fri May 03, 2024 1:10 am

Re: the fundemental questions about context switch mechanism in ChibiOS

Postby hello,tomorrow » Sat May 04, 2024 4:30 am

Giovanni wrote:Hi,

In that port the context switch is performed synchronously and without entering an exception state, look at the asm implementation of port_switch(). It just stacks/unstacks all the EABI function-preserved registers and swap stacks.

Note that there are 2 distinct ARMv7-M ports, the alternate port uses exceptions like you described and it is a bit slower at context switching (it has other advantages). The 2 ports are perfectly interchangeable by changing one line in the makefile.

ChibiOS supports both context switch methods, it is a port-level decision, the portable OS mandates nothing, it just calls port_switch() when needed.

Giovanni



thanks for your reply, but I'm still confused for how the context switch is happening without change of the PC pointer(pointing to the other thread) , as I understand:
1)the asm of port_switch only swap the stacks for r4-r10,SP mainly
2)the call of port_switch just like the normal call of sub function, and return back where it was called
3)so ,there is no PC pointer changed to the other thread/task anyway, so the thread is not switched to the other side ?
where do I miss?

and the other problem: I only see one ARMv7-M port for port_switch call under directory of port/ARMCMx/compilers/GCC/chcoreasm_v7m.S, where is the alternative way ?where is the exact one line in the makefile?

User avatar
Giovanni
Site Admin
Posts: 14462
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1078 times
Been thanked: 922 times
Contact:

Re: the fundemental questions about context switch mechanism in ChibiOS

Postby Giovanni » Sat May 04, 2024 5:50 am

Code: Select all

__port_switch:
                push    {r4, r5, r6, r7, r8, r9, r10, r11, lr}
#if CORTEX_USE_FPU
                /* Saving FPU context.*/
                vpush   {s16-s31}
#endif

                str     sp, [r1, #CONTEXT_OFFSET]
#if (CORTEX_SIMPLIFIED_PRIORITY == FALSE) &&                                \
    ((CORTEX_MODEL == 3) || (CORTEX_MODEL == 4))
                /* Workaround for ARM errata 752419, only applied if
                   condition exists for it to be triggered.*/
                ldr     r3, [r0, #CONTEXT_OFFSET]
                mov     sp, r3
#else
                ldr     sp, [r0, #CONTEXT_OFFSET]
#endif

#if CORTEX_USE_FPU
                /* Restoring FPU context.*/
                vpop    {s16-s31}
#endif
                pop     {r4, r5, r6, r7, r8, r9, r10, r11, pc}


Note that the last instruction pops PC from the stack, this function is called by thread A and returns in another thread B.

Giovanni

hello,tomorrow
Posts: 3
Joined: Fri May 03, 2024 1:10 am

Re: the fundemental questions about context switch mechanism in ChibiOS

Postby hello,tomorrow » Sat May 04, 2024 8:49 am

Giovanni wrote:

Code: Select all

__port_switch:
                push    {r4, r5, r6, r7, r8, r9, r10, r11, lr}
#if CORTEX_USE_FPU
                /* Saving FPU context.*/
                vpush   {s16-s31}
#endif

                str     sp, [r1, #CONTEXT_OFFSET]
#if (CORTEX_SIMPLIFIED_PRIORITY == FALSE) &&                                \
    ((CORTEX_MODEL == 3) || (CORTEX_MODEL == 4))
                /* Workaround for ARM errata 752419, only applied if
                   condition exists for it to be triggered.*/
                ldr     r3, [r0, #CONTEXT_OFFSET]
                mov     sp, r3
#else
                ldr     sp, [r0, #CONTEXT_OFFSET]
#endif

#if CORTEX_USE_FPU
                /* Restoring FPU context.*/
                vpop    {s16-s31}
#endif
                pop     {r4, r5, r6, r7, r8, r9, r10, r11, pc}


Note that the last instruction pops PC from the stack, this function is called by thread A and returns in another thread B.

Giovanni


Thanks, Giovanni,I got it! your art of code is really decent and elegant :D ,can’t love it more!
bother you one more thing,You say that there are two distinct versions selectable for port_switch realization .but
I only see one ARMv7-M port for port_switch call under directory of port/ARMCMx/compilers/GCC/chcoreasm_v7m.S, where is the alternative way ?where is the exact one line in the makefile?

User avatar
Giovanni
Site Admin
Posts: 14462
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1078 times
Been thanked: 922 times
Contact:

Re: the fundemental questions about context switch mechanism in ChibiOS

Postby Giovanni » Sat May 04, 2024 10:02 am

Hi,

The alternate port is under /os/common/ports/ARMv7-M-ALT, just replace the port path inclusion in the makefile.

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 3 guests