mit-pdos / xv6-riscv

Xv6 for RISC-V
Other
6.51k stars 2.36k forks source link

Clean shutdown from shell prompt #60

Open markem1 opened 3 years ago

markem1 commented 3 years ago

We added a "halt" command to xv6 for the x86. I was wondering if anyone has done so for the riscv version. Here are our changes for x86.

sysproc.c:

ifdef PDX_XV6

// shutdown QEMU int sys_halt(void) { do_shutdown(); // never returns return 0; }

endif // PDX_XV6

console.c:

ifdef PDX_XV6

void do_shutdown() { cprintf("\nShutting down ...\n"); outw( 0x604, 0x0 | 0x2000); // signal QEMU to shutdown return; // not reached }

endif // PDX_XV6

Else, I will track this down. We didn't add it to the official as it seemed that the authors were comfortable with just using c-a x. Is qemu mapped to the same range of addresses? I hope so.

dhouck commented 3 years ago

I don't know about QEMU shutdown commands that way, but I'm pretty sure QEMU supports SBI, so you can use the SBI shutdown command.

I could be wrong about that (I'm mostly looking at this repository because I want to learn about this stuff, not because I already know it) but it's the first thing I plan to check once I get things set up for experimentation.

markem1 commented 3 years ago

Thank you for the great pointer. I have been busy reading. It looks like qemu only supports openSBI, which doesn't include support for sbi_shutdown.

When I get some time, I will locate where the command interface to qemu is mapped in the risc-v xv6 kernel to see if the above command will still work, since it is a qemu command, not a risc-v instruction.

dhouck commented 3 years ago

Huh, it looks like the latest version of OpenSBI supports SRST, but I guess it might not be supported on QEMU or they might use an old version. I certainly can't get either the legacy EID #0x08 or the updated SRST to work correctly.

Hopefully you can get some variant of the above command to work.

joel-porquet commented 2 years ago

The virt QEMU board (https://github.com/qemu/qemu/blob/master/hw/riscv/virt.c#L1449) integrates a "test finisher" device (https://git.qemu.org/?p=qemu.git;a=blob;f=hw/misc/sifive_test.c).

If your shutdown syscall writes 0x55550000 to where the test finisher device is mapped, then QEMU should exit the simulation.

meighti commented 1 year ago

I added the function timerhalt() to start.c

Since writing to the test finisher device should be done in machine mode, a new flag is set by timerhalt() in supervisor mode. Later timervec() will check this flag, which is at the end of timer scratch area. If the halt flag is not zero, it will branch to halt. See pull request #176.

In shell kill init process (PID=1) to shutdown virt QEMU board, using this: kill 1