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: make /reboot reach 0xCF9 even if the 8042 is missing or stuck

Kevin Griffina79f72543aac
diff --git a/02-baremetal-os/README.md b/02-baremetal-os/README.md
index 4a228ff..d2a5f09 100644
--- a/02-baremetal-os/README.md
+++ b/02-baremetal-os/README.md
@@ -156,7 +156,7 @@ 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 |
+| `/reboot` | Hard reset: `0xCF9 <- 0x02, 0x06, 0x0E` first, then 8042 pulse if present (skipped if port `0x64` reads `0xFF` or stays full), then triple fault. Serial markers: `rst:cf9`, `rst:kbc`, `rst:tf`. |
| `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 |
diff --git a/02-baremetal-os/drivers.asm b/02-baremetal-os/drivers.asm
index 9f31b97..39999a3 100644
--- a/02-baremetal-os/drivers.asm
+++ b/02-baremetal-os/drivers.asm
@@ -37,6 +37,7 @@ global g_tsc_hz
global g_tsc_mhz
extern g_boot_tsc
+extern g_smp_apic_base
; ============================================================================
; 1. COM1 Serial Driver (0x3F8, 115200 8N1)
@@ -732,57 +733,185 @@ qemu_shutdown:
jmp .halt
; ============================================================================
-; 6. Hard Reset: 8042 KBC pulse -> 0xCF9 PCH reset -> triple fault
+; 6. Hard Reset: 0xCF9 PCH first, then 8042 if present, then triple fault
; void machine_reboot(void) — never returns
+;
+; Every wait is iteration- and/or TSC-capped. Do not call pit_delay_us here:
+; pit_wait_ticks polls port 0x61 bit 5 with no timeout. Do not `out` to the
+; 8042 if its input buffer is still full — that I/O can stall the CPU forever.
; ============================================================================
machine_reboot:
cli
- ; Let the UART finish sending whatever is queued (LSR.TEMT), bounded
- mov ecx, 0x100000
+
+ ; Park APs with INIT (all excluding self). Bounded ICR wait; ignore failure.
+ mov rax, [rel g_smp_apic_base]
+ test rax, rax
+ jz .flush
+ mov ecx, 100000
+.icr_idle:
+ test dword [rax + APIC_REG_ICR_LO], 0x1000
+ jz .ipi
+ pause
+ dec ecx
+ jnz .icr_idle
+ jmp .flush ; ICR stuck; skip rather than hang
+.ipi:
+ mov dword [rax + APIC_REG_ICR_HI], 0
+ mov dword [rax + APIC_REG_ICR_LO], 0x000C4500
+
+ ; Drain UART TX (LSR.TEMT) so "Rebooting..." and the first marker get out
.flush:
+ mov ecx, 100000
+.flush_lp:
mov dx, COM1_LSR
in al, dx
test al, 0x40
- jnz .kbc
+ jnz .do_cf9
pause
dec ecx
- jnz .flush
+ jnz .flush_lp
-.kbc:
- ; 1. 8042 keyboard controller: wait for input buffer empty, pulse reset
- mov ecx, 0x10000
+.do_cf9:
+ lea rsi, [rel msg_rst_cf9]
+ call reboot_mark
+%ifndef REBOOT_TEST_SKIP_CF9
+ mov dx, RST_CNT_PORT
+ mov al, 0x02 ; SYS_RST
+ out dx, al
+ mov edi, 20
+ call reboot_delay_us
+ mov dx, RST_CNT_PORT
+ mov al, 0x06 ; SYS_RST | RST_CPU (hard reset)
+ out dx, al
+ mov edi, 50000
+ call reboot_delay_us
+ mov dx, RST_CNT_PORT
+ mov al, 0x0E ; FULL_RST | RST_CPU | SYS_RST
+ out dx, al
+ mov edi, 50000
+ call reboot_delay_us
+%endif
+
+ ; 8042 pulse only if a controller is present and accepting commands
+.do_kbc:
+ lea rsi, [rel msg_rst_kbc]
+ call reboot_mark
+%ifdef REBOOT_TEST_KBC_FF
+ mov al, 0xFF
+%elifdef REBOOT_TEST_KBC_STUCK
+ mov al, 0x02 ; IBF set, not 0xFF
+%else
+ in al, PS2_STATUS
+%endif
+ cmp al, 0xFF ; floating / missing 8042
+ je .do_tf
+ mov ecx, 100000
.kbc_wait:
+%ifdef REBOOT_TEST_KBC_STUCK
+ mov al, 0x02
+%else
in al, PS2_STATUS
- test al, 0x02
+%endif
+ test al, 0x02 ; IBF
jz .kbc_go
pause
dec ecx
jnz .kbc_wait
+ jmp .do_tf ; still full: do not `out` (can stall)
.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
+ call reboot_delay_us
- ; 3. Triple fault: empty IDT, then any exception cannot be delivered
+.do_tf:
+ lea rsi, [rel msg_rst_tf]
+ call reboot_mark
lidt [rel null_idt_ptr]
int3
.hang:
hlt
jmp .hang
+; void reboot_mark(const char *msg in rsi) — bounded COM1 line, then '\n'
+reboot_mark:
+.lp:
+ mov al, [rsi]
+ test al, al
+ jz .nl
+ inc rsi
+ call reboot_putc
+ jmp .lp
+.nl:
+ mov al, 10
+ call reboot_putc
+ ret
+
+; void reboot_putc(uint8 al) — like serial_putc but gives up after 100k polls
+reboot_putc:
+ mov r8b, al
+ mov ecx, 100000
+ mov dx, COM1_LSR
+.wait:
+ in al, dx
+ test al, 0x20 ; THRE
+ jnz .go
+ pause
+ dec ecx
+ jnz .wait
+ ret
+.go:
+ mov dx, COM1_PORT
+ mov al, r8b
+ out dx, al
+ ret
+
+; void reboot_delay_us(uint32 us in edi) — TSC delay with a hard iteration cap
+reboot_delay_us:
+ push rbx
+ mov rax, [rel g_tsc_hz]
+ test rax, rax
+ jz .spin
+ mov ecx, edi
+ test ecx, ecx
+ jnz .have_us
+ mov ecx, 1
+.have_us:
+ mul rcx ; rdx:rax = tsc_hz * us
+ mov rcx, 1000000
+ cmp rdx, rcx
+ jae .spin ; pathological; fall back
+ div rcx ; rax = ticks
+ mov rbx, rax
+ rdtsc
+ shl rdx, 32
+ or rax, rdx
+ add rbx, rax ; deadline
+ mov ecx, 20000000 ; hard cap (~tens of ms even if TSC is odd)
+.tsc_lp:
+ rdtsc
+ shl rdx, 32
+ or rax, rdx
+ cmp rax, rbx
+ jae .done
+ pause
+ dec ecx
+ jnz .tsc_lp
+ jmp .done
+.spin:
+ mov ecx, edi
+ shl ecx, 8 ; ~256 loops/us, conservative
+ test ecx, ecx
+ jnz .spin_lp
+ mov ecx, 256
+.spin_lp:
+ pause
+ dec ecx
+ jnz .spin_lp
+.done:
+ pop rbx
+ ret
+
; 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:
@@ -799,6 +928,9 @@ section .rodata
align 8
null_idt_ptr: dw 0
dq 0
+msg_rst_cf9: db "rst:cf9", 0
+msg_rst_kbc: db "rst:kbc", 0
+msg_rst_tf: db "rst:tf", 0
; Cyrillic U+0410..U+044F (А..Я, а..я) -> single-char ASCII translit for VGA
cyr_ascii_tbl: