xv6-riscv icon indicating copy to clipboard operation
xv6-riscv copied to clipboard

Clean shutdown from shell prompt

Open markem1 opened this issue 3 years ago • 5 comments

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.

markem1 avatar Feb 11 '21 15:02 markem1

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.

dhouck avatar Apr 05 '21 05:04 dhouck

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.

markem1 avatar Apr 05 '21 15:04 markem1

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.

dhouck avatar Apr 06 '21 07:04 dhouck

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.

joel-porquet avatar Jun 07 '22 18:06 joel-porquet

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

meighti avatar Mar 30 '23 19:03 meighti