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.

fix(os): draw the console on the UEFI GOP framebuffer

James Tervit2586e1b43e91
diff --git a/02-baremetal-os/README.md b/02-baremetal-os/README.md
index ab9ec35..a2d9048 100644
--- a/02-baremetal-os/README.md
+++ b/02-baremetal-os/README.md
@@ -2,7 +2,7 @@
A standalone bootable x86-64 operating system that runs **Qwen/Qwen3-0.6B** directly on hardware or KVM with **no Linux, no Windows, no C, no Rust, and no libc**.
-- **25,404 bytes** stripped static Multiboot 1 kernel (`FAST_Q8=1`, default: `.text` = 18,151 B, `.rodata` = 2,900 B, `.bss` = 2.1 MiB `NOBITS`, zero dynamic dependencies), or **24,764 bytes** (`FAST_Q8=0`, legacy Q8_0xFP32).
+- **27,164 bytes** stripped static Multiboot 1 kernel (`FAST_Q8=1`, default: `.text` = 19,111 B, `.rodata` = 3,700 B, `.bss` = 2.1 MiB `NOBITS`, zero dynamic dependencies), or **26,524 bytes** (`FAST_Q8=0`, legacy Q8_0xFP32).
- **74–94 ms cold boot** to interactive `asm-llm>` multi-turn chat with persistent KV-cache across turns, all SMP cores online, and the `.asmbin` v2 (`614 MiB` FP16 scales) or v1 (`649 MiB` FP32 scales) model bound zero-copy from physical RAM on 2 MiB huge pages.
- **100% token-exact and byte-for-byte parity** with [Phase 1](../01-qwen3-inference/) across `1..32` vCPUs.
- **134.9 tok/s** decode on QEMU KVM (AMD EPYC 7642, 16 vCPUs; **23.7 tok/s** at 1 vCPU, **118.1 tok/s** at 8 vCPUs, **136.6 tok/s** peak) and **55.4 tok/s** live on a **0 GB disk Proxmox VE VM** (Intel Xeon E5-2670 v3, 12 cores).
@@ -42,7 +42,7 @@ make os-check
Expected output (`FAST_Q8=1`):
```text
--rwxr-xr-x 1 user user 25404 build/asm-llm-os.elf
+-rwxr-xr-x 1 user user 27164 build/asm-llm-os.elf
build/asm-llm-os.elf: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, stripped
There is no dynamic section in this file.
```
@@ -107,6 +107,8 @@ qemu-system-x86_64 -enable-kvm -cpu host -m 2G -smp 16 \
-cdrom build/asm-llm-os.iso -nographic
```
+UEFI has no VGA text mode. On a UEFI boot GRUB hands the kernel the firmware's GOP framebuffer (`gfxpayload=keep`), and `fbcon.asm` draws the same 80x25 colour console on it: 8x16 cells from the public-domain font8x8 glyphs, scaled 2x on screens of at least 1280x800, centred. 24 and 32 bpp direct-RGB modes below 4 GiB are supported. Other modes, or no framebuffer, fall back to the serial console. BIOS boots are unchanged and keep real VGA text mode. To see the screen under OVMF, drop `-nographic` and add `-serial stdio`. On real UEFI boards, PS/2 keyboard input depends on the firmware's legacy USB emulation. COM1 serial always works.
+
### Option C: Deploying on Proxmox VE (0 GB Disk VM)
Because the entire OS and model live inside the ISO and run out of RAM, the VM needs **zero hard disks**.
@@ -190,6 +192,7 @@ Phase 2 compiles the exact same `shared/math.asm`, `shared/matmul.asm`, `shared/
- **Bare-Metal SMP (`smp.asm`):** parses ACPI RSDP -> RSDT/XSDT -> MADT (`"APIC"`) with a Local APIC broadcast fallback, installs a position-independent 16->32->64-bit trampoline at `0x8000`, wakes all AP cores via Local APIC `INIT-SIPI-SIPI`, enables AVX2/FMA/MXCSR identically on every core, and dispatches both `matmul_q8_0` and `gqa_attn_range` (`FAST_Q8=1`) with a lock-free cache-line-isolated spin-barrier.
- **Hardware Drivers (`drivers.asm`):**
- **COM1 Serial (`0x3F8`, 115200 8N1):** raw UTF-8 TX and RX.
+ - **UEFI framebuffer console (`fbcon.asm`):** on UEFI boots, the 80x25 cells are kept in a shadow buffer and painted onto the GOP framebuffer passed by GRUB (Multiboot 1 video flag, 24/32 bpp). Only changed cells are repainted.
- **80x25 Color VGA (`0xB8000`):** colored boot banner, hardware scrolling, batched CRTC cursor updates, and UTF-8 decoding with Cyrillic-to-ASCII fallback on VGA while streaming raw UTF-8 to COM1.
- **PS/2 Keyboard (`0x60`/`0x64`):** scancode set 1 to ASCII (Shift, Backspace, Enter).
- **PIT Channel 2 + RDTSC:** 20 ms hardware PIT calibration for accurate nanosecond boot/prefill/decode profiling.
diff --git a/02-baremetal-os/boot.asm b/02-baremetal-os/boot.asm
index 9034434..0d7221a 100644
--- a/02-baremetal-os/boot.asm
+++ b/02-baremetal-os/boot.asm
@@ -19,6 +19,7 @@ extern _bss_end
extern kernel_main
extern serial_putc
extern console_write_cstr
+extern g_vga_base
extern write_hex64_raw
global _start
@@ -39,6 +40,9 @@ section .multiboot align=4
dd MB_HEADER_MAGIC
dd MB_HEADER_FLAGS
dd MB_HEADER_CHECKSUM
+ dd 0, 0, 0, 0, 0 ; address fields (unused: flags bit 16 clear, ELF used)
+ dd MB_VIDEO_EGA_TEXT ; mode_type: EGA text on BIOS, GOP framebuffer on UEFI
+ dd VGA_COLS, VGA_ROWS, 0 ; width, height, depth
; ============================================================================
; 32-bit Protected Mode Bootstrap
@@ -63,6 +67,9 @@ _start:
xor eax, eax
rep stosd
+ ; Console text cells start at VGA text memory; fb_init may swap in a shadow
+ mov dword [g_vga_base], VGA_MEM
+
; Set up temporary 32-bit stack at top of BSP stack
mov esp, bsp_stack_top
diff --git a/02-baremetal-os/drivers.asm b/02-baremetal-os/drivers.asm
index ecc32cc..43b4986 100644
--- a/02-baremetal-os/drivers.asm
+++ b/02-baremetal-os/drivers.asm
@@ -35,9 +35,13 @@ global machine_reboot
global check_reboot_key
global g_tsc_hz
global g_tsc_mhz
+global g_vga_base
extern g_boot_tsc
extern g_smp_apic_base
+extern fb_draw_cell
+extern fb_redraw
+extern g_fb_on
; ============================================================================
; 1. COM1 Serial Driver (0x3F8, 115200 8N1)
@@ -126,11 +130,12 @@ vga_clear:
mov edx, eax
shl eax, 16
or eax, edx ; 2 cells in eax
- mov rdi, VGA_MEM
+ mov rdi, [rel g_vga_base]
mov ecx, (VGA_COLS * VGA_ROWS) / 2
rep stosd
- call vga_update_cursor
- ret
+ cmp byte [rel g_fb_on], 0
+ je vga_update_cursor
+ jmp fb_redraw
; void vga_set_color(uint8 attr in dil)
vga_set_color:
@@ -139,6 +144,8 @@ vga_set_color:
; Internal: update hardware VGA cursor via CRTC 0x3D4/0x3D5
vga_update_cursor:
+ cmp byte [rel g_fb_on], 0
+ jne .ret
mov eax, [rel vga_row]
imul eax, eax, VGA_COLS
add eax, [rel vga_col]
@@ -157,6 +164,7 @@ vga_update_cursor:
mov dx, VGA_CRTC_DATA
mov al, ch
out dx, al
+.ret:
ret
; Internal: scroll VGA screen up by 1 line
@@ -164,8 +172,8 @@ vga_scroll:
push rdi
push rsi
push rcx
- mov rdi, VGA_MEM
- mov rsi, VGA_MEM + (VGA_COLS * 2)
+ mov rdi, [rel g_vga_base]
+ lea rsi, [rdi + VGA_COLS * 2]
mov ecx, ((VGA_ROWS - 1) * VGA_COLS) / 2
rep movsd
@@ -179,6 +187,10 @@ vga_scroll:
mov ecx, VGA_COLS / 2
rep stosd
mov dword [rel vga_row], VGA_ROWS - 1
+ cmp byte [rel g_fb_on], 0
+ je .no_fb
+ call fb_redraw
+.no_fb:
pop rcx
pop rsi
pop rdi
@@ -220,7 +232,7 @@ vga_word_wrap:
mov eax, [rel vga_row]
imul eax, eax, VGA_COLS
- mov rdi, VGA_MEM
+ mov rdi, [rel g_vga_base] ; VGA text memory or the fbcon shadow
lea rsi, [rdi + rax*2]
movzx edx, word [rsi + r8*2]
cmp dl, ' '
@@ -246,6 +258,10 @@ vga_word_wrap:
mov dl, ' '
mov dh, [rel vga_attr]
mov [rsi + rcx*2], dx
+ push rcx
+ add ecx, eax ; cell index = row * VGA_COLS + column
+ call vga_cell_touched ; repaint the blanked cell (fbcon only)
+ pop rcx
inc ebx
inc ecx
cmp ecx, r9d
@@ -313,8 +329,9 @@ vga_emit_ascii:
imul ecx, ecx, VGA_COLS
add ecx, [rel vga_col]
mov ah, [rel vga_attr]
- mov rdx, VGA_MEM
+ mov rdx, [rel g_vga_base]
mov [rdx + rcx*2], ax
+ call vga_cell_touched
mov eax, [rel vga_col]
inc eax
@@ -346,8 +363,9 @@ vga_emit_ascii:
add ecx, eax
mov ah, [rel vga_attr]
mov al, ' '
- mov rdx, VGA_MEM
+ mov rdx, [rel g_vga_base]
mov [rdx + rcx*2], ax
+ call vga_cell_touched
ret
.tab:
@@ -372,6 +390,14 @@ vga_emit_ascii:
.ret:
ret
+; Internal: redraw cell ecx on the framebuffer console (no-op in VGA text mode)
+vga_cell_touched:
+ cmp byte [rel g_fb_on], 0
+ je .ret
+ call fb_draw_cell
+.ret:
+ ret
+
; void vga_putc_utf8(uint8 byte in dil)
; Streams UTF-8 bytes cleanly onto VGA text mode:
; - ASCII passes straight through
@@ -1076,6 +1102,7 @@ section .bss
alignb 8
g_tsc_hz: resq 1
g_tsc_mhz: resq 1
+g_vga_base: resq 1 ; VGA_MEM (set in boot.asm) or fbcon shadow
vga_row: resd 1
vga_col: resd 1
utf8_cp: resd 1
diff --git a/02-baremetal-os/fbcon.asm b/02-baremetal-os/fbcon.asm
new file mode 100644
index 0000000..74afcf0
--- /dev/null
+++ b/02-baremetal-os/fbcon.asm
@@ -0,0 +1,322 @@
+; ============================================================================
+; os/fbcon.asm — Linear framebuffer text console (UEFI GOP via GRUB Multiboot 1)
+;
+; GRUB on UEFI has no VGA text mode, so the 80x25 console at 0xB8000 shows
+; nothing. The Multiboot header asks for EGA text first; GRUB-EFI then falls
+; back to a GOP framebuffer and reports it in the Multiboot info (flags bit 12).
+;
+; When a direct-RGB framebuffer is offered, fb_init points the VGA text
+; driver at an 80x25 cell shadow buffer in .bss and this module draws each cell
+; (24 or 32 bpp) with an 8x8 public-domain font, doubled vertically to 8x16, scaled 1x or 2x
+; and centred. Otherwise nothing changes: BIOS boots keep real VGA text mode.
+; ============================================================================
+
+%include "os_constants.inc"
+
+extern g_mb_magic
+extern g_mb_info
+extern g_vga_base
+
+global fb_init
+global fb_draw_cell
+global fb_redraw
+global g_fb_on
+
+section .text
+
+; void fb_init(void)
+; Enables framebuffer mode if GRUB passed a usable framebuffer. Preserves
+; callee-saved registers (SysV).
+fb_init:
+ push rbx
+ cmp dword [rel g_mb_magic], MB_BOOTLOADER_MAGIC
+ jne .done
+ mov esi, [rel g_mb_info]
+ test dword [rsi + MBI_FLAGS], MBI_FLAG_FRAMEBUFFER
+ jz .done
+ cmp byte [rsi + MBI_FB_TYPE], MBI_FB_TYPE_RGB
+ jne .done
+ movzx eax, byte [rsi + MBI_FB_BPP] ; 24 or 32 bpp (GOP modes vary)
+ cmp eax, 32
+ je .bpp_ok
+ cmp eax, 24
+ jne .done
+.bpp_ok:
+ shr eax, 3
+ mov [rel fb_bpp_bytes], eax
+ cmp byte [rsi + MBI_FB_RED_SIZE], 8
+ jne .done
+ cmp byte [rsi + MBI_FB_GREEN_SIZE], 8
+ jne .done
+ cmp byte [rsi + MBI_FB_BLUE_SIZE], 8
+ jne .done
+
+ mov r8d, [rsi + MBI_FB_WIDTH]
+ mov r9d, [rsi + MBI_FB_HEIGHT]
+ mov r10d, [rsi + MBI_FB_PITCH]
+ cmp r8d, FB_GRID_W
+ jb .done
+ cmp r9d, FB_GRID_H
+ jb .done
+ mov eax, r8d ; pitch must hold a full row of pixels
+ imul eax, [rel fb_bpp_bytes]
+ cmp r10d, eax
+ jb .done
+
+ ; Framebuffer must sit inside the 0..4 GiB identity map built in boot.asm
+ mov rdi, [rsi + MBI_FB_ADDR]
+ test rdi, rdi
+ jz .done
+ mov eax, r9d
+ imul rax, r10 ; rax = pitch * height (bytes)
+ mov rbx, rax
+ add rax, rdi
+ jc .done
+ mov rdx, FB_MAP_LIMIT
+ cmp rax, rdx
+ ja .done
+
+ ; scale = min(width / 640, height / 400), clamped to 1..FB_MAX_SCALE
+ xor edx, edx
+ mov eax, r8d
+ mov ecx, FB_GRID_W
+ div ecx
+ mov r11d, eax
+ xor edx, edx
+ mov eax, r9d
+ mov ecx, FB_GRID_H
+ div ecx
+ cmp eax, r11d
+ cmovb r11d, eax
+ cmp r11d, FB_MAX_SCALE
+ jbe .scale_ok
+ mov r11d, FB_MAX_SCALE
+.scale_ok:
+ mov [rel fb_scale], r11d
+ lea eax, [r11 * 2]
+ mov [rel fb_line_reps], eax ; each 8x8 glyph row covers 2*scale lines
+ mov [rel fb_pitch], r10
+ mov eax, r11d
+ shl eax, 3 ; 8 px * bytes per pixel * scale
+ imul eax, [rel fb_bpp_bytes]
+ mov [rel fb_col_bytes], rax
+ mov eax, r11d
+ shl eax, 4 ; 16 lines * scale
+ imul rax, r10
+ mov [rel fb_row_bytes], rax
+
+ ; origin = addr + ((h - 400*s) / 2) * pitch + ((w - 640*s) / 2) * Bpp
+ imul eax, r11d, FB_GRID_H
+ mov ecx, r9d
+ sub ecx, eax
+ shr ecx, 1
+ imul rcx, r10
+ imul eax, r11d, FB_GRID_W
+ mov edx, r8d
+ sub edx, eax
+ shr edx, 1
+ imul edx, [rel fb_bpp_bytes]
+ add rcx, rdx
+ add rcx, rdi
+ mov [rel fb_origin], rcx
+
+ ; Clear the whole framebuffer (GRUB leaves its menu there)
+ ; rdi = framebuffer base, rbx = pitch * height
+ mov rcx, rbx
+ shr rcx, 2
+ xor eax, eax
+ rep stosd
+
+ ; Build the 16-colour CGA palette in the framebuffer's pixel layout
+ movzx r8d, byte [rsi + MBI_FB_RED_POS]
+ movzx r9d, byte [rsi + MBI_FB_GREEN_POS]
+ movzx r10d, byte [rsi + MBI_FB_BLUE_POS]
+ lea rsi, [rel cga_rgb]
+ lea r11, [rel fb_palette]
+ xor edx, edx
+.pal:
+ movzx eax, byte [rsi]
+ mov ecx, r8d
+ shl eax, cl
+ movzx ebx, byte [rsi + 1]
+ mov ecx, r9d
+ shl ebx, cl
+ or eax, ebx
+ movzx ebx, byte [rsi + 2]
+ mov ecx, r10d
+ shl ebx, cl
+ or eax, ebx
+ mov [r11 + rdx * 4], eax
+ add rsi, 3
+ inc edx
+ cmp edx, 16
+ jb .pal
+
+ ; The cleared screen equals all-blank cells: record that so the first
+ ; redraw only paints cells that hold something.
+ lea rdi, [rel fb_drawn]
+ mov eax, FB_BLANK_CELL | (FB_BLANK_CELL << 16)
+ mov ecx, (VGA_COLS * VGA_ROWS) / 2
+ rep stosd
+
+ lea rax, [rel fb_shadow]
+ mov [rel g_vga_base], rax
+ mov byte [rel g_fb_on], 1
+.done:
+ pop rbx
+ ret
+
+; void fb_draw_cell(uint32 index in ecx)
+; Draws one 80x25 shadow cell. Preserves every general-purpose register.
+fb_draw_cell:
+ push rax
+ push rbx
+ push rcx
+ push rdx
+ push rsi
+ push rdi
+ push r8
+ push r9
+ push r10
+ push r11
+ push r12
+ push r13
+ push r14
+ push r15
+
+ mov r15d, [rel fb_bpp_bytes]
+ lea rsi, [rel fb_shadow]
+ movzx ebx, word [rsi + rcx * 2] ; bl = char, bh = attribute
+ lea rsi, [rel fb_drawn]
+ mov [rsi + rcx * 2], bx
+
+ mov eax, ecx
+ xor edx, edx
+ mov ecx, VGA_COLS
+ div ecx ; eax = row, edx = column
+ imul rax, [rel fb_row_bytes]
+ imul rdx, [rel fb_col_bytes]
+ mov rdi, [rel fb_origin]
+ add rdi, rax
+ add rdi, rdx
+
+ lea r11, [rel fb_palette]
+ movzx eax, bh
+ and eax, 0x0F
+ mov r8d, [r11 + rax * 4] ; foreground
+ movzx eax, bh
+ shr eax, 4
+ and eax, 0x0F
+ mov r9d, [r11 + rax * 4] ; background
+
+ movzx eax, bl
+ sub eax, 0x20
+ cmp eax, 0x7E - 0x20
+ jbe .glyph_ok
+ xor eax, eax ; anything else draws as a space
+.glyph_ok:
+ lea rsi, [rel fb_font]
+ lea rsi, [rsi + rax * 8]
+
+ mov r10d, 8 ; glyph rows
+.row:
+ movzx r11d, byte [rsi]
+ inc rsi
+ mov ebx, [rel fb_line_reps]
+.line:
+ mov rdx, rdi
+ mov eax, r11d
+ mov ecx, 8 ; pixels per glyph row
+.px:
+ mov r12d, r9d
+ test eax, 1 ; bit 0 is the leftmost pixel
+ cmovnz r12d, r8d
+ shr eax, 1
+ mov r14d, r12d
+ shr r14d, 16 ; third byte for 24 bpp
+ mov r13d, [rel fb_scale]
+.rep:
+ cmp r15d, 4
+ jne .rgb24
+ mov [rdx], r12d
+ jmp .next
+.rgb24:
+ mov [rdx], r12w
+ mov [rdx + 2], r14b
+.next:
+ add rdx, r15
+ dec r13d
+ jnz .rep
+ dec ecx
+ jnz .px
+ add rdi, [rel fb_pitch]
+ dec ebx
+ jnz .line
+ dec r10d
+ jnz .row
+
+ pop r15
+ pop r14
+ pop r13
+ pop r12
+ pop r11
+ pop r10
+ pop r9
+ pop r8
+ pop rdi
+ pop rsi
+ pop rdx
+ pop rcx
+ pop rbx
+ pop rax
+ ret
+
+; void fb_redraw(void) — repaint every cell whose content differs from what
+; is on screen (after clear or scroll). Preserves every GPR.
+fb_redraw:
+ push rax
+ push rcx
+ push rsi
+ push rdi
+ lea rsi, [rel fb_shadow]
+ lea rdi, [rel fb_drawn]
+ xor ecx, ecx
+.lp:
+ movzx eax, word [rsi + rcx * 2]
+ cmp ax, [rdi + rcx * 2]
+ je .next
+ call fb_draw_cell
+.next:
+ inc ecx
+ cmp ecx, VGA_COLS * VGA_ROWS
+ jb .lp
+ pop rdi
+ pop rsi
+ pop rcx
+ pop rax
+ ret
+
+section .rodata
+; CGA/VGA text palette as R, G, B bytes for attributes 0..15
+cga_rgb:
+ db 0x00,0x00,0x00, 0x00,0x00,0xAA, 0x00,0xAA,0x00, 0x00,0xAA,0xAA
+ db 0xAA,0x00,0x00, 0xAA,0x00,0xAA, 0xAA,0x55,0x00, 0xAA,0xAA,0xAA
+ db 0x55,0x55,0x55, 0x55,0x55,0xFF, 0x55,0xFF,0x55, 0x55,0xFF,0xFF
+ db 0xFF,0x55,0x55, 0xFF,0x55,0xFF, 0xFF,0xFF,0x55, 0xFF,0xFF,0xFF
+
+fb_font:
+%include "font8x8.inc"
+
+section .bss
+alignb 16
+fb_shadow: resw VGA_COLS * VGA_ROWS ; cells as the console wrote them
+fb_drawn: resw VGA_COLS * VGA_ROWS ; cells as currently painted
+fb_palette: resd 16
+fb_origin: resq 1
+fb_pitch: resq 1
+fb_row_bytes: resq 1
+fb_col_bytes: resq 1
+fb_scale: resd 1
+fb_bpp_bytes: resd 1
+fb_line_reps: resd 1
+g_fb_on: resb 1
diff --git a/02-baremetal-os/font8x8.inc b/02-baremetal-os/font8x8.inc
new file mode 100644
index 0000000..68d1b22
--- /dev/null
+++ b/02-baremetal-os/font8x8.inc
@@ -0,0 +1,101 @@
+; ============================================================================
+; os/font8x8.inc - 8x8 glyphs for printable ASCII 0x20..0x7E (95 x 8 bytes)
+; Source: font8x8_basic.h by Daniel Hepper (github.com/dhepper/font8x8),
+; Public Domain, derived from public-domain IBM VGA fonts.
+; Bit 0 of each byte is the leftmost pixel. Drawn as 8x16 by doubling rows.
+; ============================================================================
+ db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ; 0x20
+ db 0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00 ; 0x21 !
+ db 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ; 0x22 "
+ db 0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00 ; 0x23 #
+ db 0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00 ; 0x24 $
+ db 0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00 ; 0x25 %
+ db 0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00 ; 0x26 &
+ db 0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 ; 0x27
+ db 0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00 ; 0x28 (
+ db 0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00 ; 0x29 )
+ db 0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00 ; 0x2A *
+ db 0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00 ; 0x2B +
+ db 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06 ; 0x2C ,
+ db 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00 ; 0x2D -
+ db 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00 ; 0x2E .
+ db 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00 ; 0x2F /
+ db 0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00 ; 0x30 0
+ db 0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00 ; 0x31 1
+ db 0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00 ; 0x32 2
+ db 0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00 ; 0x33 3
+ db 0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00 ; 0x34 4
+ db 0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00 ; 0x35 5
+ db 0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00 ; 0x36 6
+ db 0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00 ; 0x37 7
+ db 0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00 ; 0x38 8
+ db 0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00 ; 0x39 9
+ db 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00 ; 0x3A :
+ db 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06 ; 0x3B ;
+ db 0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00 ; 0x3C <
+ db 0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00 ; 0x3D =
+ db 0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00 ; 0x3E >
+ db 0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00 ; 0x3F ?
+ db 0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00 ; 0x40 @
+ db 0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00 ; 0x41 A
+ db 0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00 ; 0x42 B
+ db 0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00 ; 0x43 C
+ db 0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00 ; 0x44 D
+ db 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00 ; 0x45 E
+ db 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00 ; 0x46 F
+ db 0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00 ; 0x47 G
+ db 0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00 ; 0x48 H
+ db 0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00 ; 0x49 I
+ db 0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00 ; 0x4A J
+ db 0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00 ; 0x4B K
+ db 0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00 ; 0x4C L
+ db 0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00 ; 0x4D M
+ db 0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00 ; 0x4E N
+ db 0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00 ; 0x4F O
+ db 0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00 ; 0x50 P
+ db 0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00 ; 0x51 Q
+ db 0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00 ; 0x52 R
+ db 0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00 ; 0x53 S
+ db 0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00 ; 0x54 T
+ db 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00 ; 0x55 U
+ db 0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00 ; 0x56 V
+ db 0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00 ; 0x57 W
+ db 0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00 ; 0x58 X
+ db 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00 ; 0x59 Y
+ db 0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00 ; 0x5A Z
+ db 0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00 ; 0x5B [
+ db 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00 ; 0x5C
+ db 0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00 ; 0x5D ]
+ db 0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00 ; 0x5E ^
+ db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF ; 0x5F _
+ db 0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00 ; 0x60 `
+ db 0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00 ; 0x61 a
+ db 0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00 ; 0x62 b
+ db 0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00 ; 0x63 c
+ db 0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00 ; 0x64 d
+ db 0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00 ; 0x65 e
+ db 0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00 ; 0x66 f
+ db 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F ; 0x67 g
+ db 0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00 ; 0x68 h
+ db 0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00 ; 0x69 i
+ db 0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E ; 0x6A j
+ db 0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00 ; 0x6B k
+ db 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00 ; 0x6C l
+ db 0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00 ; 0x6D m
+ db 0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00 ; 0x6E n
+ db 0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00 ; 0x6F o
+ db 0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F ; 0x70 p
+ db 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78 ; 0x71 q
+ db 0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00 ; 0x72 r
+ db 0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00 ; 0x73 s
+ db 0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00 ; 0x74 t
+ db 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00 ; 0x75 u
+ db 0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00 ; 0x76 v
+ db 0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00 ; 0x77 w
+ db 0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00 ; 0x78 x
+ db 0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F ; 0x79 y
+ db 0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00 ; 0x7A z
+ db 0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00 ; 0x7B {
+ db 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00 ; 0x7C |
+ db 0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00 ; 0x7D }
+ db 0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ; 0x7E ~
diff --git a/02-baremetal-os/kernel.asm b/02-baremetal-os/kernel.asm
index f64052b..1480c6c 100644
--- a/02-baremetal-os/kernel.asm
+++ b/02-baremetal-os/kernel.asm
@@ -28,6 +28,7 @@ extern g_smp_online_cpus
extern serial_init
extern serial_putc
extern vga_clear
+extern fb_init
extern vga_set_color
extern console_write
extern console_write_cstr
@@ -75,6 +76,7 @@ kernel_main:
; 1. Early hardware init: COM1 Serial + Color VGA + PIT/RDTSC Timer
call serial_init
+ call fb_init ; UEFI: draw the console on the GOP framebuffer
mov dil, VGA_ATTR_GREY
call vga_clear
diff --git a/02-baremetal-os/os_constants.inc b/02-baremetal-os/os_constants.inc
index 95d1fc1..f32da9d 100644
--- a/02-baremetal-os/os_constants.inc
+++ b/02-baremetal-os/os_constants.inc
@@ -5,9 +5,11 @@
; --- Multiboot 1 ---
%define MB_HEADER_MAGIC 0x1BADB002
-%define MB_HEADER_FLAGS 0x00000003 ; PAGE_ALIGN | MEMORY_INFO
+%define MB_HEADER_FLAGS 0x00000007 ; PAGE_ALIGN | MEMORY_INFO | VIDEO_MODE
%define MB_HEADER_CHECKSUM (-(MB_HEADER_MAGIC + MB_HEADER_FLAGS))
%define MB_BOOTLOADER_MAGIC 0x2BADB002
+%define MB_VIDEO_EGA_TEXT 1 ; header mode_type: prefer EGA text
+ ; (GRUB-EFI falls back to a framebuffer)
; Multiboot 1 Info struct offsets
%define MBI_FLAGS 0
@@ -19,6 +21,24 @@
%define MBI_MMAP_LENGTH 44
%define MBI_MMAP_ADDR 48
+; Multiboot 1 framebuffer info (valid when MBI flags bit 12 is set)
+%define MBI_FLAG_FRAMEBUFFER 0x00001000
+%define MBI_FB_ADDR 88 ; uint64
+%define MBI_FB_PITCH 96
+%define MBI_FB_WIDTH 100
+%define MBI_FB_HEIGHT 104
+%define MBI_FB_BPP 108 ; uint8
+%define MBI_FB_TYPE 109 ; uint8: 0 indexed, 1 RGB, 2 EGA text
+; color_info is a union holding a uint32, so GRUB 4-byte aligns it to 112
+; (the spec's offset table says 110; real loaders put it at 112).
+%define MBI_FB_RED_POS 112
+%define MBI_FB_RED_SIZE 113
+%define MBI_FB_GREEN_POS 114
+%define MBI_FB_GREEN_SIZE 115
+%define MBI_FB_BLUE_POS 116
+%define MBI_FB_BLUE_SIZE 117
+%define MBI_FB_TYPE_RGB 1
+
; Multiboot module struct (16 bytes)
%define MB_MOD_START 0
%define MB_MOD_END 4
@@ -39,6 +59,13 @@
%define VGA_CRTC_IDX 0x3D4
%define VGA_CRTC_DATA 0x3D5
+; --- Framebuffer console (fbcon.asm): 80x25 cells of 8x16 px ---
+%define FB_GRID_W (VGA_COLS * 8) ; 640 px at scale 1
+%define FB_GRID_H (VGA_ROWS * 16) ; 400 px at scale 1
+%define FB_MAX_SCALE 2 ; bounds full-redraw cost on 4K panels
+%define FB_MAP_LIMIT 0x100000000 ; boot.asm identity-maps 0..4 GiB
+%define FB_BLANK_CELL 0x0720 ; space on black: a cleared framebuffer
+
; VGA Color Attributes (bg<<4 | fg)
%define VGA_ATTR_GREY 0x07
%define VGA_ATTR_DARK 0x08
diff --git a/Makefile b/Makefile
index 76990a8..a2ae3b8 100644
--- a/Makefile
+++ b/Makefile
@@ -76,6 +76,7 @@ OS_LDFLAGS := -m elf_x86_64 -T $(P2_DIR)/linker.ld --gc-sections -s -z max-page-
OS_OBJS := \
$(OS_BUILD)/boot.o \
$(OS_BUILD)/drivers.o \
+ $(OS_BUILD)/fbcon.o \
$(OS_BUILD)/runtime.o \
$(OS_BUILD)/smp.o \
$(OS_BUILD)/kernel.o \
@@ -142,6 +143,8 @@ $(OS_BUILD):
$(OS_BUILD)/%.o: $(P2_DIR)/%.asm $(P2_DIR)/os_constants.inc $(SHARED_DIR)/constants.inc $(FAST_Q8_STAMP) | $(OS_BUILD)
$(NASM) $(OS_AFLAGS) $< -o $@
+$(OS_BUILD)/fbcon.o: $(P2_DIR)/font8x8.inc
+
$(OS_BUILD)/math.o: $(SHARED_DIR)/math.asm | $(OS_BUILD)
$(NASM) $(OS_AFLAGS) $< -o $@
diff --git a/README.md b/README.md
index 0970bd8..5457d39 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@
| Phase | Target | Size | Stack | Headline Result |
|---|---|---:|---|---|
| [`01`](01-qwen3-inference/) | [`01-qwen3-inference/`](01-qwen3-inference/) · `Qwen3-0.6B` static ELF | **15.1 KB** (`15,064 B`) | Pure x86-64 NASM | `126.8 tok/s` (40T), `+18%..+36%` vs `llama.cpp` CPU, `64/64` PyTorch parity |
-| [`02`](02-baremetal-os/) | [`02-baremetal-os/`](02-baremetal-os/) · Bootable Bare-Metal OS | **25.4 KB** (`25,404 B`) | Pure x86-64 NASM | `74–94 ms` boot, `134.9 tok/s` KVM (16 vCPU), Proxmox `0 GB` disk (`55.4 tok/s`) |
+| [`02`](02-baremetal-os/) | [`02-baremetal-os/`](02-baremetal-os/) · Bootable Bare-Metal OS | **27.2 KB** (`27,164 B`) | Pure x86-64 NASM | `74–94 ms` boot, `134.9 tok/s` KVM (16 vCPU), Proxmox `0 GB` disk (`55.4 tok/s`) |
| [`03`](03-minigpt-train/) | [`03-minigpt-train/`](03-minigpt-train/) · Mini-GPT Trainer + TUI | **33.9 KB** (`33,872 B`) | Pure x86-64 NASM | `27,662 tok/s`, `1.0000000000` grad cosine vs PyTorch, `80x24` Braille UI |
| [`04`](04-ring1-kernel-surgeon/) | [`04-ring1-kernel-surgeon/`](04-ring1-kernel-surgeon/) · Ring `-1` SVM Hypervisor | **75.4 KB** (`75,424 B`) | NASM + zero-libc C | MiniCPM5-2B live kernel crash patcher, `97.54%` pass@1 (`119/122`), `0%` dangerous |
| [`05`](05-ebpf-jev/) | [`05-ebpf-jev/`](05-ebpf-jev/) · Linux `6.12` eBPF LSM | **21.3 KB** (`21,280 B`) | eBPF C + INT8 RNN | `5.65 µs` median hook, `0` floats, `500/500` `0-LSB` parity, atomic hot-swap |
@@ -100,7 +100,7 @@ make all # Builds Phase 1 (asm-llm, test_math), Phase 2 (asm-llm-os.elf), Phas
```
- **01 · Qwen3 Inference (`15.1 KB`):** `python3 scripts/export_weights.py --model-dir models/qwen3-0.6b-bf16 --out models/qwen3-0.6b-q8_0.asmbin --scale-dtype fp16 && ./build/asm-llm -m models/qwen3-0.6b-q8_0.asmbin --chat -t 16` → [01-qwen3-inference/README.md](01-qwen3-inference/README.md)
-- **02 · Bare-Metal OS (`25.4 KB`):** `make os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16` *(or `make os-iso` for BIOS+UEFI ISO)* → [02-baremetal-os/README.md](02-baremetal-os/README.md)
+- **02 · Bare-Metal OS (`27.2 KB`):** `make os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16` *(or `make os-iso` for BIOS+UEFI ISO)* → [02-baremetal-os/README.md](02-baremetal-os/README.md)
- **03 · Mini-GPT Trainer (`33.9 KB`):** `./build/asm-gpt-train --data data/tinyshakespeare.txt --steps 400 --seq 128 -t 16 --save checkpoints/shakespeare.ckpt` → [03-minigpt-train/README.md](03-minigpt-train/README.md)
- **04 · Ring `-1` Kernel Surgeon (`75.4 KB`):** `cd 04-ring1-kernel-surgeon && make hv && python3 verifier/kvm_verifier.py --selftest` → [04-ring1-kernel-surgeon/README.md](04-ring1-kernel-surgeon/README.md)
- **05 · Linux `6.12` eBPF LSM (`21.3 KB`):** `cd 05-ebpf-jev && bash testbed/build.sh && testbed/run_vm.sh verify` → [05-ebpf-jev/README.md](05-ebpf-jev/README.md)
diff --git a/scripts/mkiso.sh b/scripts/mkiso.sh
index ce0d010..4d61211 100755
--- a/scripts/mkiso.sh
+++ b/scripts/mkiso.sh
@@ -34,7 +34,14 @@ mkdir -p "$ISO_ROOT/boot/grub"
cp "$KERNEL_ELF" "$ISO_ROOT/boot/asm-llm-os.elf"
# Note: quote cmdline carefully for GRUB2 multiboot command
+# On UEFI there is no VGA text mode: the kernel accepts a GOP framebuffer
+# instead (fbcon.asm). GRUB needs efi_gop loaded to pass one, and "keep" hands
+# over the firmware's current (native) mode rather than switching modes.
cat > "$ISO_ROOT/boot/grub/grub.cfg" <<EOF
+if [ "\$grub_platform" = "efi" ]; then
+ insmod efi_gop
+ set gfxpayload=keep
+fi
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
terminal_input console serial
terminal_output console serial
@@ -72,7 +79,7 @@ EOF
grub-mkstandalone \
-O x86_64-efi \
-o "$WORK_DIR/efi_tree/EFI/BOOT/BOOTX64.EFI" \
- --modules="part_gpt part_msdos iso9660 fat multiboot normal serial terminal search search_fs_file configfile" \
+ --modules="part_gpt part_msdos iso9660 fat multiboot normal serial terminal search search_fs_file configfile efi_gop" \
"boot/grub/grub.cfg=$WORK_DIR/embed.cfg"
# Create minimal FAT16 EFI System Partition image (zero root, zero mtools needed)