how I can make a switch context?

261 views
Skip to first unread message

JUAN JOSE ROMERO HERNANDEZ

unread,
Dec 1, 2021, 1:12:46 PM12/1/21
to RISC-V SW Dev
Hi all,

Could someone please explain to me how I can do a context switch?

I'm build a kernel, I've an interrupt of timer that it interrup every so often, when the interrupt occurs I want to do a switch context, because I need to work with several processes concurrently.

I'm storing the registers this way link_github from line 39 to 74 and I'am calling the fountion  this way link_github line 3 and line 31. But when a run my code, It throw me a exception number 7 (Access failure on Load). 

Could someone please tell me why it causes this problem and if I am doing it correctly or is there another way?

Thanks a lot 
Greetings


Tommy Murphy

unread,
Dec 1, 2021, 2:12:22 PM12/1/21
to JUAN JOSE ROMERO HERNANDEZ, RISC-V SW Dev
Why don't you look at existing examples for RISC-V?
E.g. FreeRTOS, Zephyr, Linux etc.

Frédéric Pétrot

unread,
Dec 1, 2021, 2:30:02 PM12/1/21
to Tommy Murphy, JUAN JOSE ROMERO HERNANDEZ, RISC-V SW Dev
Even simpler through deprecated, riscv-pk.
Frédéric 

Le mer. 1 déc. 2021 à 20:12, Tommy Murphy <tommy_...@hotmail.com> a écrit :
Why don't you look at existing examples for RISC-V?
E.g. FreeRTOS, Zephyr, Linux etc.

--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/LO2P265MB477687E9DA7D5E817EDAD5BEF9689%40LO2P265MB4776.GBRP265.PROD.OUTLOOK.COM.

JUAN JOSE ROMERO HERNANDEZ

unread,
Dec 1, 2021, 3:47:17 PM12/1/21
to RISC-V SW Dev, tommy_...@hotmail.com, JUAN JOSE ROMERO HERNANDEZ
Hi Tommy,

I have looked at existing examples and based my code them, but I have errors. For that reason I am asking for help.

Thanks for your suggestion

JUAN JOSE ROMERO HERNANDEZ

unread,
Dec 1, 2021, 3:56:42 PM12/1/21
to RISC-V SW Dev, frederi...@gmail.com, RISC-V SW Dev
Hi Frederic, 

I don't quite understand what you are suggesting. because I understand that riscv-pk only runs in user mode and for what I am doing I need to run in machine mode for interrupts (bare metal).

Bruce Hoult

unread,
Dec 1, 2021, 6:24:00 PM12/1/21
to JUAN JOSE ROMERO HERNANDEZ, RISC-V SW Dev, frederi...@gmail.com
The programs you run using PK run in user mode. PK itself runs in machine mode and is a min-OS kernel. It sets up the hardware including PMP, page tables, exception handlers, and loads the user program in from an ELF file and transfers control to it in user mode. It then handles system calls (which it passes on to the host OS) and exceptions.

PK does almost everything an OS does. Including context switched, though only between OK itself and one running application.

Robert Lipe

unread,
Dec 2, 2021, 12:28:09 AM12/2/21
to RISC-V SW Dev, jjose....@alumnos.udg.mx, tommy_...@hotmail.com
Agreed that there are scores (hundreds?) of worthy examples to be found on the internet at large and on GitHub specifally.

I'm on mobile and on the go, but one construct that catches my eye as a potential source of trouble is:
https://github.com/JuanJRom/risc-v/blob/533fd53fa504f496ab0912b85b894ea00bc724ef/interrupts/interrupt_1/src/kernelvec.s#L42

Potentially changing the stack pointer while you're using $sp as a pointer to your context context can be painful. It's possible this is right, but it's worthy of stepping through it in a debugger and analyzing what happens d during that if $sp changes.

It's also possible that it's totally fine.


Sebastian Huber

unread,
Dec 2, 2021, 6:51:52 AM12/2/21
to Tommy Murphy, JUAN JOSE ROMERO HERNANDEZ, RISC-V SW Dev
There is also RTEMS which is a real-time operating system supporting
SMP systems:

https://git.rtems.org/rtems/tree/cpukit/score/cpu/riscv/riscv-exception-handler.S

On Wed, Dec 1, 2021 at 8:12 PM Tommy Murphy <tommy_...@hotmail.com> wrote:
>
> Why don't you look at existing examples for RISC-V?
> E.g. FreeRTOS, Zephyr, Linux etc.
>
Reply all
Reply to author
Forward
0 new messages