ARMv7-M-ALT port missing CH_CFG_CONTEXT_SWITCH_HOOK Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
Post Reply
jst
Posts: 2
Joined: Thu Mar 20, 2025 1:54 pm
Been thanked: 1 time

ARMv7-M-ALT port missing CH_CFG_CONTEXT_SWITCH_HOOK

Post by jst »

Hi,

I think there may be a bug in the ARMv7-M-ALT core (and, it seems in ARMv8-M-ML-ALT, too) in the __port_schedule_next(void) function (both in 21.11.x and trunk). Although it now calls __trace_switch(ntp, otp) and __stats_ctxswc(ntp, otp), the call to the CH_CFG_CONTEXT_SWITCH_HOOK(ntp, otp) is ommitted (in contrast to what happens in the chSysSwitch(ntp, otp) macro). This breaks my thread-local support (which uses this hook to update the current thread pointer) as soon as any context switching happens via this function. Once I add the hook call, everything works as expected (like in the regular ARMv7-M port). Am I overlooking a reason why this hook is not called in the __port_schedule_next(void) function, or is this a bug?

Thanks for looking into this!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ARMv7-M-ALT port missing CH_CFG_CONTEXT_SWITCH_HOOK

Post by Giovanni »

Hi,

Please post your change so I can review it, probably it is just missing because it is a new port.

Giovanni
jst
Posts: 2
Joined: Thu Mar 20, 2025 1:54 pm
Been thanked: 1 time

Re: ARMv7-M-ALT port missing CH_CFG_CONTEXT_SWITCH_HOOK

Post by jst »

It's really just the already mentioned one-liner :)

Code: Select all

diff --git a/lib/ChibiOS/os/common/ports/ARMv7-M-ALT/chcore.c b/lib/ChibiOS/os/common/ports/ARMv7-M-ALT/chcore.c
index 03d8817eb..6c95d7fca 100644
--- a/lib/ChibiOS/os/common/ports/ARMv7-M-ALT/chcore.c
+++ b/lib/ChibiOS/os/common/ports/ARMv7-M-ALT/chcore.c
@@ -141,6 +141,7 @@ uint64_t __port_schedule_next(void) {
 
     __trace_switch(ntp, otp);
     __stats_ctxswc(ntp, otp);
+    CH_CFG_CONTEXT_SWITCH_HOOK(ntp, otp);
 
     return ((uint64_t)(uint32_t)otp << 32) | ((uint64_t)(uint32_t)ntp << 0);
   }
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ARMv7-M-ALT port missing CH_CFG_CONTEXT_SWITCH_HOOK

Post by Giovanni »

Hi,

Change committed, while looking at that code I also found a nice improvement to IRQ servicing, committed that too.

Giovanni
Post Reply