eclipse-threadx / threadx

Eclipse ThreadX is an advanced real-time operating system (RTOS) designed specifically for deeply embedded applications.
https://github.com/eclipse-threadx/rtos-docs/blob/main/rtos-docs/threadx/index.md
MIT License
2.89k stars 782 forks source link

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

Open maxkunes opened 6 months ago

maxkunes commented 6 months ago

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?

rayc345 commented 4 months ago

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.

amgross commented 4 months ago

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.

maxkunes commented 4 months ago

@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.

amgross commented 4 months ago

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

maxkunes commented 4 months ago

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: @.***>

amgross commented 4 months ago

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

maxkunes commented 4 months ago

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: @.***>