RP2350 RISC-V: Tick interrupt does not trigger a context switch on a single core

Hi,

I’ve been testing FreeRTOS on the RP2350’s Hazard3 RISC-V cores and came across an issue with the tick interrupt handler in the RP2350 RISC-V port.

It appears that the return value from xTaskIncrementTick() is overwritten before it is checked, which prevents the scheduler from switching tasks when a tick makes a higher-priority task ready.

The relevant code is in freertos_risc_v_mtimer_interrupt_handler in GCC/RP2350_RISC-V/portASM.S:

call callTaskEnterCriticalFromISR
mv s1, a0
call xTaskIncrementTick
mv a0, s1
call callTaskExitCriticalFromISR
beqz a0, exit_without_context_switch

xTaskIncrementTick() returns whether a context switch is required in a0. However, this value is overwritten with the saved interrupt status before callTaskExitCriticalFromISR() is called.

Since callTaskExitCriticalFromISR() returns void, the subsequent beqz checks whatever value happens to remain in a0, rather than the result from xTaskIncrementTick().

With configNUMBER_OF_CORES == 1, the body of callTaskExitCriticalFromISR() is empty, leaving a0 at zero. The handler therefore skips vTaskSwitchContext() entirely.

In practice, this means a higher-priority task that becomes ready when its vTaskDelay() expires will not run until the currently running task yields or blocks.

Reproduction

  • RP2350 using the Hazard3 RISC-V cores.

  • configNUMBER_OF_CORES == 1

  • configUSE_PREEMPTION == 1

  • A low-priority task continuously loops without blocking or yielding.

  • A higher-priority task calls vTaskDelay(1000).

When the delay expires, the higher-priority task never runs again while the lower-priority task continues running.

I’ve also seen this in Thread-Metric’s basic, message and synchronisation tests, where the report thread stops printing.

Affected versions

I’ve found the same code in both repositories:

  • raspberrypi/FreeRTOS-Kernel, commit 4f7299d6e

  • FreeRTOS/FreeRTOS-Kernel-Community-Supported-Ports, commit bae4c7a

Possible fix

I tested a change that saves the return value from xTaskIncrementTick() on the stack across the call to callTaskExitCriticalFromISR(), then restores it before checking whether a context switch is required.

With this change, the single-core tests run as expected.

I’d be interested to know whether this is already a known issue, and whether the maintainers would prefer a pull request against one of these repositories or both.

Thanks!

Good catch and good debugging! Please change the code to the following and see if it works:

    call callTaskEnterCriticalFromISR
    mv s1, a0 /* s1 = callTaskEnterCriticalFromISR() */
    call xTaskIncrementTick
    mv s2, a0 /* s2 = xTaskIncrementTick() */
    mv a0, s1 /* Pass return value from callTaskEnterCriticalFromISR to callTaskExitCriticalFromISR
    call callTaskExitCriticalFromISR */
    beqz s2, exit_without_context_switch    /* Don't switch context if incrementing tick didn't unblock a task. */

Thanks for the suggestion! I’ve tested the change on my hardware.

It fixes the preemption issue. A priority 3 task delaying one tick at a time now runs 100 times while a priority 1 task spins for 100 ms. With the original handler, it didn’t run at all.

I also tried this with both configNUMBER_OF_CORES == 1 and configNUMBER_OF_CORES == 2. For the two-core test, I pinned all the test tasks to core 0 so I could focus on the tick handler. The original handler still failed to preempt, so it looks like the issue affects both configurations.

The problem with using s2 is that freertos_risc_v_interrupt_handler is already using it to hold the interrupted context’s meicontext while the tick handler runs. When the tick result is put into s2, that value gets written back to meicontext when the interrupt handler exits, clearing the NOIRQ bit.

I checked this on the board too. With the s2 change, portCHECK_IF_IN_ISR() incorrectly reports that a task is in an ISR. SDK lock ownership also stops working properly, which leads to mutex operations either asserting or causing an incorrect context switch.

I tested using s3 instead:

call callTaskEnterCriticalFromISR
mv s1, a0
call xTaskIncrementTick
mv s3, a0
mv a0, s1
call callTaskExitCriticalFromISR
beqz s3, exit_without_context_switch

I also checked that s3 wouldn’t cause the same kind of problem. It gets saved when an interrupt occurs and restored when the handler exits, so using it for the tick result shouldn’t affect the interrupted task. I tested this on the board by checking 23 registers while ticks were occurring and switching between tasks. All of them stayed intact with the s3 change.

As a check that the test would catch a problem, I also removed the instruction that restores s3. That caused the register check to fail 192 times in 192 ms, so it seems to be picking up exactly the sort of issue we’re trying to avoid.

The s3 version passed the preemption and mutex tests, as did the stack-based fix.

If the s3 approach sounds suitable, I’m happy to put together a PR - and where would the best repo be for this PR?

Thank you for verifying the change. Apologies that I did not notice the s2 usage here.

Please raise a PR to the raspberrypi repo first and once it is merged there, raise another one to the FreeRTOS repo.

Thank you once again for all the work!

Raised here:

github.com/raspberrypi/FreeRTOS-Kernel/pull/12

(I can’t post links as my account is too new)

Thanks @aggarg :slight_smile:

You should now be able to post links.