PMP registers and User Mode

I found the pmp.c file in the Freedom metal library along with it’s header file (pmp.h). I also found the pmp.c example code on configuring them. My project is focusing on using Rust programming with RISC-V, so I was wondering if it would be sound to just take the code from the metal library and convert it into Rust programming and make a crate out of it. Or would it be easier to configure the pmp with assembly? Also how is user/machine mode utilized. The privilege spec manual talks about vertical and horizontal traps in association with privilege modes, can anyone elaborate on this any? Do I explicitly define when I want to be in user mode versus machine mode, or is it implicit according to what I am doing?



I haven’t tried this myself, but have been looking around the metal sources and the privileged ISA as well. Specifically checkout https://github.com/sifive/freedom-metal/blob/8db2c3c1880e7d63fba7c1edb5e9fce5fe4a1afd/src/privilege.c#L18 for how freedom-metal transitions to U-mode.

AFAICT, the xRET instructions are the ones you use to change to a desired privilege level, and would require some asm to issue that instruction, but aside from that the rest is just register programming: program reg mepc to where you want the code to jump to, sp to a block of memory you want to use for the U-mode stack, and then mPP to the value for U-mode (00). Then when mRET is called it will “restore” the privilege mode to whats in mPP, and “return” to mepc.

Interrupts by default seem like they always are run in M-mode, but it sounds like there are ways to trap into other privilege levels, but on the FE310 I think this is moot since there is only M/U mode, and no U-mode interrupt extension implemented (aka, always M-mode when trapping).

Regarding PMP, I think that is entirely register programming, so whatever you’d be more comfortable with?

1 Like

Appreciate the reply. I was reading the privilege manual last night and found the pmp register’s addresses and format. That shouldnt be too bad. User/machine modes seem to be a bit harder to deal with. One thing at a time…once I get pmp configured I will come back to your comments on modes. Thanks.

There is a PMP baremetal example in case this interests you: https://github.com/sifive/example-pmp-baremetal

1 Like

Wow thats a lot of code for something I thought was simple :sweat: I appeciate the link though, I will go through it line by line. I’m sure I will find something useful.

Since my project is focusing on security, Once I get a PMP region setup and a small application running as U-mode in that region, what operations would require me to use machine mode? For example, to start off, I thought I would setup UART communications that would except some commands and display results. One command might be “pmp config” which would read the PMP configuration registers and print the results on the console. I would also like to have maybe an LED setup which indicates which privilege mode I’m currently in. I’m new to embedded systems programming, so I apologize if my questions are trivial. Also I’m new to interrupts, so links for me to understand when and where to use them would be great. thanks,
Daniel