hasheddan / danielmangum.com

3 stars 2 forks source link

[question] entry.s doesn't work as expected #373

Open yf13 opened 9 months ago

yf13 commented 9 months ago

Thanks for sharing the blog: https://danielmangum.com/posts/risc-v-bytes-privilege-levels/

However, on Ubuntu Jammy (22.04) with stock qemu-system-riscv64, the entry.s, the first mret leads to m_trap instead of supervisor.

$ /usr/bin/qemu-system-riscv64 -version
QEMU emulator version 6.2.0 (Debian 1:6.2+dfsg-2ubuntu6.11)
$ gdb-multiarch ...
=> 0x80000038 <_start+56>:  mret
2: /x $mstatus = 0xa00000800
3: /x $mepc = 0x80000054
4: /x $sstatus = 0x0
5: /x $sepc = 0x0
6: /x $medeleg = 0x100
7: /x $mideleg = 0x0
(gdb) x/i $mepc
   0x80000054 <supervisor>: auipc   t0,0x0
(gdb) si

Breakpoint 3, m_trap () at entry.s:18
18      csrr    t0, mepc
1: x/i $pc
=> 0x8000003c <m_trap>: csrr    t0,mepc
2: /x $mstatus = 0xa00001800
3: /x $mepc = 0x80000038
4: /x $sstatus = 0x0
5: /x $sepc = 0x0
6: /x $medeleg = 0x100
7: /x $mideleg = 0x0
(gdb) i r mcause
mcause         0x2  2

It seems that the mret instruction at end of _start part is illegal instruction now. Is there something changed?

hasheddan commented 9 months ago

@yf13 thank you for reading the post and reporting the issue! This did change in newer versions of QEMU -- are you running QEMU with all the flags specified?

qemu-system-riscv64 -machine virt -cpu rv64,pmp=false -smp 1 -s -S -nographic -bios none -kernel entry

Specifically, the pmp=false is required in this case. This is mentioned in an update in the post:

Update (5/26/23): Physical Memory Protection (PMP) is now enabled by default in qemu-system-riscv64 (ref). We disable with the pmp=false flag to avoid our first mret instruction causing an exception and trapping to m_trap rather than returning to supervisor. Thanks to Kevin Xue for reporting this!

Is it still not working correctly for you with pmp=false set?

yf13 commented 9 months ago

@hasheddan No, the first mret still doesn't enter supervisor here.

$ ps -ef | fgrep 6011
yf          6011       1  0 09:57 pts/1    00:00:00 qemu-system-riscv64 -M virt -cpu rv64,pmp=false -smp 1 -nographic -bios none -kernel build/entry -s -S

BTW, why having PMP will cause first mret being illegal instruction?