mit-pdos / xv6-riscv

Xv6 for RISC-V
Other
6.98k stars 2.58k forks source link

mret Exception : Illegal instruction #103

Closed magnate3 closed 2 years ago

magnate3 commented 2 years ago

qemu-system-riscv64 --version QEMU emulator version 6.1.0 Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers

make kernel/kernel qemu-system-riscv64 -M virt -kernel kernel/kernel -bios none -serial stdio -display none

there is no output.through my effort, I find mret exception happens

The issue turned out to be RISC-V's Physical Memory Protection (PMP). QEMU will raise an illegal instruction exception when executing an MRET instruction if no PMP rules have been defined. Adding a PMP entry resolved the issue.

so ,I add setup_pmp function in kernel/start.c

void setup_pmp(void)
{
  // Set up a PMP to permit access to all of memory.
  // Ignore the illegal-instruction trap if PMPs aren't supported.
  unsigned long pmpc = PMP_NAPOT | PMP_R | PMP_W | PMP_X;
  asm volatile ("la t0, 1f\n\t"
                "csrrw t0, mtvec, t0\n\t"
                "csrw pmpaddr0, %1\n\t"
                "csrw pmpcfg0, %0\n\t"
                ".align 2\n\t"
                "1: csrw mtvec, t0"
                : : "r" (pmpc), "r" (-1UL) : "t0");
}

// entry.S jumps here in machine mode on stack0.
void
start()
{
  setup_pmp();
........
}