From bda0952cf022de1068509c5993ae85b5e3c89cfc Mon Sep 17 00:00:00 2001 From: kudasai <47227786+kudasaixc@users.noreply.github.com> Date: Sun, 5 Jul 2026 15:14:15 +0200 Subject: [PATCH 1/3] feat(shell): add reboot and poweroff commands Adds two machine lifecycle commands. Until now the only way to leave the shell was crash, which just halts. - reboot waits for the 8042 input buffer to drain, then pulses the CPU reset line via outb(0x64, 0xFE). - poweroff triggers an ACPI shutdown through the ports QEMU (0x604) and older QEMU / Bochs (0xB004) expose, which cleanly powers the VM off. Adds a 16-bit outw helper to io.h for the ACPI writes. Both fall back to a halt if the hardware action does not fire. --- src/include/io.h | 4 ++++ src/kernel/shell.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/include/io.h b/src/include/io.h index 0fce831..75d5d97 100644 --- a/src/include/io.h +++ b/src/include/io.h @@ -7,6 +7,10 @@ static inline void outb(uint16_t port, uint8_t value) { __asm__ volatile ("outb %0, %1" : : "a"(value), "Nd"(port)); } +static inline void outw(uint16_t port, uint16_t value) { + __asm__ volatile ("outw %0, %1" : : "a"(value), "Nd"(port)); +} + static inline uint8_t inb(uint16_t port) { uint8_t ret; __asm__ volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port)); diff --git a/src/kernel/shell.c b/src/kernel/shell.c index 575ab67..1d76c0b 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -2,6 +2,7 @@ #include "../include/colors.h" #include "../include/fetch.h" #include "../include/integer.h" +#include "../include/io.h" #include "../include/log.h" #include "../include/memory.h" #include "../include/mm.h" @@ -94,6 +95,8 @@ static void shell_execute(char *cmd) { terminal_writestring("mmap - print current virtual memory mappings\n"); terminal_writestring("peek - read and print memory at a given hex address\n"); terminal_writestring("echo - repeats your input to the console\n"); + terminal_writestring("reboot - restart the machine\n"); + terminal_writestring("poweroff- shut the machine down (QEMU/Bochs)\n"); terminal_writestring("crash - make the machine freeze (fun cmd)\n\n"); } else if (strcmp(cmd_name, "clear") == 0) { @@ -144,6 +147,23 @@ static void shell_execute(char *cmd) { for (;;) asm volatile("hlt"); } + else if (strcmp(cmd_name, "reboot") == 0) { + terminal_writestring("Rebooting...\n"); + // Wait for the 8042 input buffer to drain, then pulse the CPU reset line. + while (inb(0x64) & 0x02) + ; + outb(0x64, 0xFE); + // If the reset did not fire, fall back to a halt. + asm volatile("cli; hlt"); + } + else if (strcmp(cmd_name, "poweroff") == 0) { + terminal_writestring("Powering off...\n"); + // ACPI shutdown as exposed by QEMU (0x604) and older QEMU / Bochs (0xB004). + outw(0x604, 0x2000); + outw(0xB004, 0x2000); + // No virtual power management available: halt instead. + asm volatile("cli; hlt"); + } else if (strcmp(cmd_name, "uptime") == 0) { uint32_t ticks = get_tick(); uint32_t total_seconds = ticks / 1000; From 27addcff1fb6abfd8e94b3062cb464031e79415a Mon Sep 17 00:00:00 2001 From: shyybi Date: Fri, 17 Jul 2026 22:01:57 +0200 Subject: [PATCH 2/3] feat(shell): update reboot and poweroff commands to exit functionality --- src/kernel/shell.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/kernel/shell.c b/src/kernel/shell.c index 1d76c0b..99b116a 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -149,19 +149,15 @@ static void shell_execute(char *cmd) { } else if (strcmp(cmd_name, "reboot") == 0) { terminal_writestring("Rebooting...\n"); - // Wait for the 8042 input buffer to drain, then pulse the CPU reset line. while (inb(0x64) & 0x02) ; outb(0x64, 0xFE); - // If the reset did not fire, fall back to a halt. asm volatile("cli; hlt"); } - else if (strcmp(cmd_name, "poweroff") == 0) { + else if (strcmp(cmd_name, "exit") == 0) { terminal_writestring("Powering off...\n"); - // ACPI shutdown as exposed by QEMU (0x604) and older QEMU / Bochs (0xB004). outw(0x604, 0x2000); outw(0xB004, 0x2000); - // No virtual power management available: halt instead. asm volatile("cli; hlt"); } else if (strcmp(cmd_name, "uptime") == 0) { From d796dd5cf7adab21cdac40bcf011482f6955b25a Mon Sep 17 00:00:00 2001 From: shyybi Date: Fri, 17 Jul 2026 22:02:51 +0200 Subject: [PATCH 3/3] feat(shell): update poweroff command description to exit functionality --- src/kernel/shell.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/shell.c b/src/kernel/shell.c index 99b116a..fd21df3 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -96,7 +96,7 @@ static void shell_execute(char *cmd) { terminal_writestring("peek - read and print memory at a given hex address\n"); terminal_writestring("echo - repeats your input to the console\n"); terminal_writestring("reboot - restart the machine\n"); - terminal_writestring("poweroff- shut the machine down (QEMU/Bochs)\n"); + terminal_writestring("exit. - shut the machine down (QEMU/Bochs)\n"); terminal_writestring("crash - make the machine freeze (fun cmd)\n\n"); } else if (strcmp(cmd_name, "clear") == 0) {