threadx icon indicating copy to clipboard operation
threadx copied to clipboard

Cortex-M7 : Is _tx_thread_context_save/_tx_thread_context_restore required in interrupts?

Open maxkunes opened this issue 1 year ago • 7 comments

The ThreadX docs under Chapter 3 - Functional Components: ISR Template suggest that context saving and restoring must be done in assembly before executing any ThreadX code (i.e. notifying semaphores) or calling user code.

Is this required on Cortex-M7? Or is this only needed on specific architectures?

maxkunes avatar Apr 02 '24 16:04 maxkunes

Not manually, For Armv7-M ISA based CPU like Cortex-M7, caller save registers (r0-r3,r12,LR(r14),PC(r15), xPSR) would be saved into stack by hardware, and r4-r9, r10,r11,SP would be saved by callee functions, save and restore instructions would be generated by compilers, so for C programmers, no manual procedure is required to save/restore contexts.

rayc345 avatar May 16 '24 02:05 rayc345

Hi @rayc345 Threadx context save/restore not only saves register, but also manages the option that while the interrupt happened something that requires context switch. For this case stuff needed to be stored on the stack and context restore returns from the interrupt to the scheduler instead of to the original point where the interrupt was called from. And hence I think the answer to @maxkunes is that indeed it should be called from assembly code.

amgross avatar May 16 '24 08:05 amgross

@amgross Thank you for the reply.

Honestly, I've been working with ThreadX for 3 years now without doing this, and I've yet to see anything that is obviously wrong. Can you explain what behavior might be seen in theory without these calls? Maybe I have some intermittent bugs due to this that I haven't connected to anything else.

maxkunes avatar May 16 '24 15:05 maxkunes

Hi @maxkunes , By 'without doing it' you mean you call it from C code or not calling it at all?

amgross avatar May 19 '24 05:05 amgross

I do not call it at all.

On Sat, May 18, 2024 at 10:21 PM amgross @.***> wrote:

Hi @maxkunes https://github.com/maxkunes , By 'without doing it' you mean you call it from C code or not calling it at all?

— Reply to this email directly, view it on GitHub https://github.com/eclipse-threadx/threadx/issues/381#issuecomment-2119105921, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZP7FF5RTLLH6BSHYQGAODZDAZFLAVCNFSM6AAAAABFTVTCPGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJZGEYDKOJSGE . You are receiving this because you were mentioned.Message ID: @.***>

maxkunes avatar May 19 '24 09:05 maxkunes

Surprisingly (for me), it seems like I was mistaken and there is no need for cortex M to use save/restore

https://github.com/eclipse-threadx/threadx/blob/485a02faec6edccef14812ddce6844af1d7d2eef/ports/cortex_m7/ac5/readme_threadx.txt#L161-L173

So @rayc345 is correct

amgross avatar May 19 '24 09:05 amgross

But that seems to depend on the compiler used?

See: https://github.com/eclipse-threadx/threadx/blob/485a02faec6edccef14812ddce6844af1d7d2eef/ports/cortex_m7/gnu/readme_threadx.txt

Which says on GNU you have to use assembly to save r0 and lr and then you can bl to your C function.

Im using arm-none-eabi-gcc, which presumably is AC5 or 6? If so, that explains why I don't have issues.

On Sun, May 19, 2024 at 2:33 AM amgross @.***> wrote:

Surprisingly (for me), it seems like I was mistaken and there is no need for cortex M to use save/restore

https://github.com/eclipse-threadx/threadx/blob/485a02faec6edccef14812ddce6844af1d7d2eef/ports/cortex_m7/ac5/readme_threadx.txt#L161-L173

So @rayc345 https://github.com/rayc345 is correct

— Reply to this email directly, view it on GitHub https://github.com/eclipse-threadx/threadx/issues/381#issuecomment-2119169327, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZP7FCLY7PRHJKSIQERW5DZDBWYFAVCNFSM6AAAAABFTVTCPGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJZGE3DSMZSG4 . You are receiving this because you were mentioned.Message ID: @.***>

maxkunes avatar May 19 '24 10:05 maxkunes

Closing this issue due to inactivity. Feel free to reopen or create a new one.

fdesbiens avatar Feb 27 '25 16:02 fdesbiens