Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

os: add /reboot command and Ctrl-R / serial BREAK hard reset

Kevin Griffin680ed8de88c7
diff --git a/02-baremetal-os/README.md b/02-baremetal-os/README.md
index 8887779..4a228ff 100644
--- a/02-baremetal-os/README.md
+++ b/02-baremetal-os/README.md
@@ -156,6 +156,8 @@ Both PS/2 keyboard (VGA) and COM1 serial (`115200 8N1`) are polled simultaneousl
| `/nothink` | Disable reasoning mode (direct answers, default) |
| `/bench` | Reset KV-cache and run built-in latency/throughput benchmark prompt |
| `/halt` or `/quit` | Trigger clean ACPI S5 poweroff |
+| `/reboot` | Hard reset: 8042 KBC pulse (`0x64 <- 0xFE`), then `0xCF9 <- 0x02, 0x06`, then triple fault |
+| `Ctrl-R` or serial BREAK | Same hard reset, works at any time over COM1, including mid-prefill/mid-generation (polled once per token) |
| `/help` | Show available console commands |
### Multiboot Kernel Cmdline Options
diff --git a/02-baremetal-os/drivers.asm b/02-baremetal-os/drivers.asm
index 4c88ab6..9f31b97 100644
--- a/02-baremetal-os/drivers.asm
+++ b/02-baremetal-os/drivers.asm
@@ -31,6 +31,8 @@ global pit_delay_us
global timer_init
global timer_ns
global qemu_shutdown
+global machine_reboot
+global check_reboot_key
global g_tsc_hz
global g_tsc_mhz
@@ -84,9 +86,12 @@ serial_putc:
ret
; int32 serial_try_getc(void) -> eax = byte (0..255) or -1 if empty
+; A received serial BREAK (LSR.BI) is reported as REBOOT_KEY.
serial_try_getc:
mov dx, COM1_LSR
in al, dx
+ test al, 0x10 ; BI (Break Interrupt)
+ jnz .brk
test al, 0x01 ; DR (Data Ready)
jz .none
mov dx, COM1_PORT
@@ -96,6 +101,11 @@ serial_try_getc:
.none:
mov eax, -1
ret
+.brk:
+ mov dx, COM1_PORT
+ in al, dx ; drain the NUL byte that goes with a BREAK
+ mov eax, REBOOT_KEY
+ ret
; ============================================================================
; 2. VGA Text Mode Driver (0xB8000, 80x25)
@@ -512,6 +522,8 @@ console_readline:
je .got_cr
cmp eax, 4 ; Ctrl-D (EOF)
je .got_eof
+ cmp eax, REBOOT_KEY ; Ctrl-R / serial BREAK -> hard reset
+ je machine_reboot
cmp eax, 8 ; Backspace
je .got_bs
cmp eax, 127 ; DEL (Backspace over serial)
@@ -719,11 +731,75 @@ qemu_shutdown:
hlt
jmp .halt
+; ============================================================================
+; 6. Hard Reset: 8042 KBC pulse -> 0xCF9 PCH reset -> triple fault
+; void machine_reboot(void) — never returns
+; ============================================================================
+machine_reboot:
+ cli
+ ; Let the UART finish sending whatever is queued (LSR.TEMT), bounded
+ mov ecx, 0x100000
+.flush:
+ mov dx, COM1_LSR
+ in al, dx
+ test al, 0x40
+ jnz .kbc
+ pause
+ dec ecx
+ jnz .flush
+
+.kbc:
+ ; 1. 8042 keyboard controller: wait for input buffer empty, pulse reset
+ mov ecx, 0x10000
+.kbc_wait:
+ in al, PS2_STATUS
+ test al, 0x02
+ jz .kbc_go
+ pause
+ dec ecx
+ jnz .kbc_wait
+.kbc_go:
+ mov al, KBC_CMD_RESET
+ out PS2_STATUS, al
+ mov edi, 50000 ; give it 50 ms
+ call pit_delay_us
+
+ ; 2. PCH Reset Control (0xCF9): SYS_RST, then SYS_RST|RST_CPU = hard reset
+ mov dx, RST_CNT_PORT
+ mov al, 0x02
+ out dx, al
+ mov edi, 10
+ call pit_delay_us
+ mov dx, RST_CNT_PORT
+ mov al, 0x06
+ out dx, al
+ mov edi, 50000
+ call pit_delay_us
+
+ ; 3. Triple fault: empty IDT, then any exception cannot be delivered
+ lidt [rel null_idt_ptr]
+ int3
+.hang:
+ hlt
+ jmp .hang
+
+; void check_reboot_key(void) — poll COM1 once; Ctrl-R/BREAK resets the box.
+; Other bytes typed while the model is busy are dropped. Clobbers eax, edx.
+check_reboot_key:
+ call serial_try_getc
+ cmp eax, REBOOT_KEY
+ je machine_reboot
+ ret
+
; ============================================================================
; Read-only Lookup Tables
; ============================================================================
section .rodata
+align 8
+null_idt_ptr: dw 0
+ dq 0
+
; Cyrillic U+0410..U+044F (А..Я, а..я) -> single-char ASCII translit for VGA
cyr_ascii_tbl:
; А Б В Г Д Е Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ъ Ы Ь Э Ю Я
diff --git a/02-baremetal-os/kernel.asm b/02-baremetal-os/kernel.asm
index a64362d..19264b0 100644
--- a/02-baremetal-os/kernel.asm
+++ b/02-baremetal-os/kernel.asm
@@ -35,6 +35,8 @@ extern console_readline
extern timer_init
extern timer_ns
extern qemu_shutdown
+extern machine_reboot
+extern check_reboot_key
extern mem_init
extern smp_init
@@ -284,6 +286,13 @@ kernel_main:
test eax, eax
jnz .do_halt
+ ; /reboot — hard reset (KBC -> 0xCF9 -> triple fault)
+ mov rdi, r12
+ lea rsi, [rel cmd_reboot]
+ call str_eq
+ test eax, eax
+ jnz .do_reboot
+
; /clear, /new, /reset — reset multi-turn KV-cache history
mov rdi, r12
lea rsi, [rel cmd_clear]
@@ -385,6 +394,14 @@ kernel_main:
call run_inference
jmp .repl_loop
+.do_reboot:
+ mov dil, VGA_ATTR_YELLOW
+ call vga_set_color
+ mov edi, 1
+ lea rsi, [rel msg_reboot]
+ call write_cstr
+ call machine_reboot
+
.do_halt:
mov dil, VGA_ATTR_YELLOW
call vga_set_color
@@ -491,6 +508,7 @@ run_inference:
xor ebx, ebx ; i = 0
mov r14, [rbp + CTX_BUF_PROMPT]
.prefill_lp:
+ call check_reboot_key ; Ctrl-R / BREAK works mid-prefill
cmp rbx, r12
jae .prefill_done
mov edi, [r14 + rbx*4]
@@ -527,6 +545,7 @@ run_inference:
xor r15d, r15d ; n_gen = 0
mov rbx, [rel opt_max_tok]
.decode_lp:
+ call check_reboot_key ; Ctrl-R / BREAK works mid-generation
cmp r15, rbx
jae .decode_done
cmp r14, [rbp + CTX_EOS_1]
@@ -1306,13 +1325,15 @@ ban_mod_at: db " MiB @ 0x", 0
ban_mod_ctx: db ", 28L, ctx=", 0
msg_prompt_lbl: db "Prompt: ", 0
-msg_repl_help: db "Interactive Bare-Metal Chat (/clear, /think, /nothink, /bench, /halt, /help)", 10, 0
+msg_repl_help: db "Interactive Bare-Metal Chat (/clear, /think, /nothink, /bench, /halt, /reboot, /help)", 10
+ db "Ctrl-R or a serial BREAK reboots at any time, even mid-generation", 10, 0
msg_repl_prompt: db 10, "asm-llm> ", 0
msg_chat_cleared: db "[chat] history cleared (ctx=0/", 0
msg_ctx_full: db "[chat] context full, resetting KV-cache", 10, 0
msg_think_on: db "[mode] think=1 (Qwen3 reasoning enabled)", 10, 0
msg_think_off: db "[mode] think=0 (direct answer mode)", 10, 0
msg_poweroff: db "Powering off bare-metal VM...", 10, 0
+msg_reboot: db "Rebooting...", 10, 0
msg_nl: db 10, 0
msg_comma: db ","
@@ -1335,6 +1356,7 @@ def_cpu_brand: db "x86-64 Processor", 0
bench_prompt: db "Explain how a CPU cache works in 3 concise sentences.", 0
cmd_halt: db "/halt", 0
+cmd_reboot: db "/reboot", 0
cmd_quit: db "/quit", 0
cmd_exit: db "exit", 0
cmd_clear: db "/clear", 0
diff --git a/02-baremetal-os/os_constants.inc b/02-baremetal-os/os_constants.inc
index 12f4a31..95d1fc1 100644
--- a/02-baremetal-os/os_constants.inc
+++ b/02-baremetal-os/os_constants.inc
@@ -50,6 +50,11 @@
%define VGA_ATTR_YELLOW 0x0E
%define VGA_ATTR_WHITE 0x0F
+; --- Reset / reboot ---
+%define REBOOT_KEY 0x12 ; Ctrl-R (also sent for a serial BREAK)
+%define KBC_CMD_RESET 0xFE ; 8042 pulse CPU reset line
+%define RST_CNT_PORT 0xCF9 ; Intel PCH Reset Control register
+
; --- PS/2 Keyboard ---
%define PS2_DATA 0x60
%define PS2_STATUS 0x64