Explorar o código

Fix the X201 rootfs load with a protected-mode pack copy

The X201 BIOS's INT 15h AH=87h block move silently copies zeros to
PACK_HIGH_ADDR, so the kernel received an empty pack, took the raw-pack
path and wedged before the WM. Copy each 16 KB staging chunk with a
temporary flat protected-mode rep movsb instead; leave through a 16-bit
protected-mode code segment before clearing PE and returning to real
mode.

Update the boot-chain documentation in AGENTS.md and docs/boot.html.
Johannes Zellner hai 1 semana
pai
achega
094a42f397
Modificáronse 3 ficheiros con 74 adicións e 66 borrados
  1. 18 12
      AGENTS.md
  2. 8 6
      docs/boot.html
  3. 48 48
      vesa_stage.S

+ 18 - 12
AGENTS.md

@@ -140,9 +140,12 @@ make clean
    enables A20, reads the `NBROOT1` rootfs header at the boot sector's
    `pack_lba` field and copies the gzip pack directly to `PACK_HIGH_ADDR`
    (`0x900000`, above the 8 MB VGA-shadow reserve) in 16 KB chunks through
-   the `0x3000` staging buffer with INT 15h AH=87h.  The BIOS block move uses
-   24-bit descriptor bases; `paging_prepare_cpu()` clears the CR4/EFER state
-   the X201's BIOS leaves behind.  The stage then picks the best VBE mode
+   the `0x3000` staging buffer with a temporary flat protected-mode
+   `rep movsb` (`pm_copy`).  The X201 BIOS's INT 15h AH=87h block move
+   silently copies zeros to this range, which left the kernel with an empty
+   pack; the CPU copy is used instead.  `paging_prepare_cpu()` still clears
+   the CR4/EFER state a BIOS protected-mode call can leave behind.  The
+   stage then picks the best VBE mode
    (≥16 bpp, LFB, fits VRAM, ≤1280px), writes mode info to `0x600` and the
    12-dword `struct boot_info` (magic/checksum/rsdp/flags/pack range) to
    `0x500`, enters protected mode and jumps to `kernel_entry` (`entry.S`,
@@ -290,8 +293,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   LFB (uncacheable) and enables CR0.PG/WP. Before the switch it clears stale
   translation controls a BIOS protected-mode call can leave behind
   (`CR4.PAE/PGE/LA57/PCIDE/SMEP/SMAP` and `EFER.LME`); the X201's legacy
-  stage used to return from `INT 15h AH=87h` with `CR4` set, and the first
-  `CR0.PG` write then triple-faulted.  The LFB PDE range is computed from
+  stage historically returned from `INT 15h AH=87h` with `CR4` set, and the
+  first `CR0.PG` write then triple-faulted.  The LFB PDE range is computed from
   the page index (a base above `0xFF000000` used to wrap `base + 16 MB` and
   map nothing), and `mm_init` reserves a low LFB range so the boot console
   cannot overwrite the page directory.  Each process gets a page directory
@@ -912,8 +915,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `USER_BASE` (`0x40000000`), packed by `mkpack` into `cmds.pack` (as
   `bin/<name>`), gzipped to `cmds_pack_gz.bin` and wrapped by `mkrootfs`
   with a `NBROOT1` header.  The pack is the system rootfs: the boot path
-  loads it into RAM (legacy high-memory AH=87h copy / UEFI / kexec staging)
-  and `ramfs_init` takes a private page-allocated copy, mounted read-only at
+  loads it into RAM (legacy high-memory protected-mode copy / UEFI / kexec
+  staging) and `ramfs_init` takes a private page-allocated copy, mounted
+  read-only at
   `/ramfs`.  The
   shell finds them through PATH (`/ramfs/bin` by default), loads the whole
   ELF through the VFS and spawns it as a ring-3 process; `hello` is just
@@ -1319,8 +1323,9 @@ gzip flag).  The build rejects a pack above the `PACK_MAX` cap (2 MB,
 `CMDS_GZ_MAX` in the Makefile, `PACK_MAX` in `vesa_stage.S`);
 `libdeflate` buys several KB over `gzip -9` and is still preferred.  The boot
 path loads the gz pack into RAM — the legacy stage copies it directly to
-`0x900000` with INT 15h AH=87h, the UEFI loader into a loader-data buffer
-below 512 MB, kexec via its staging buffer — and `ramfs_init()`
+`0x900000` with its protected-mode `rep movsb`, the UEFI loader into a
+loader-data buffer below 512 MB, kexec via its staging buffer — and
+`ramfs_init()`
 inflates it into page-allocated memory and mounts it read-only at `/ramfs`.
 The kernel image no longer contains it, so the pack no longer moves `_end`;
 the high-memory legacy path supports up to 2 MB (known issue 8).  The UI
@@ -1746,9 +1751,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 8. The rootfs pack is inflated into one contiguous page-allocated run at
    boot, capped by `RAMFS_MAX_DECOMPRESSED` (8 MB); a larger pack fails to
    load (the USB rootfs partition is 8 MB, so this only bites if the cap is
-   raised without a bigger partition).  The legacy stage now copies the
-   compressed pack directly to `0x900000` with INT 15h AH=87h, so the cap is
-   `PACK_MAX` 2 MB (raised from 1 MB for the WiFi firmware).  The browser and
+   raised without a bigger partition).  The legacy stage copies the
+   compressed pack directly to `0x900000` with a protected-mode
+   `rep movsb`, so the cap is `PACK_MAX` 2 MB (raised from 1 MB for the WiFi
+   firmware).  The browser and
    its ring-3 dependencies are compiled with function sections and linked
    with `--gc-sections`; the current full pack with the UI fonts, the
    1,036,312-byte WiFi firmware and `install/kernel.bin` is ~1.26 MiB gzip

+ 8 - 6
docs/boot.html

@@ -163,7 +163,7 @@
 		<ol>
 			<li>Probe the <code>INT 15h</code> E820 memory map into <code>0x1000</code>.</li>
 			<li>Enable A20 and read the <code>NBROOT1</code> rootfs header at the boot sector's <code>pack_lba</code> field.</li>
-			<li>Copy the compressed pack directly to <code>0x900000</code> above the 8 MB VGA-shadow reserve, in 16 KB staging chunks at <code>0x3000</code>, using the BIOS block move <code>INT 15h AH=87h</code>.</li>
+			<li>Copy the compressed pack directly to <code>0x900000</code> above the 8 MB VGA-shadow reserve, in 16 KB staging chunks at <code>0x3000</code>, using a temporary flat protected-mode <code>rep movsb</code>. The X201 BIOS's <code>INT 15h AH=87h</code> block move silently copies zeros to this range, so the CPU copy is used instead.</li>
 			<li>Pick the best VBE mode: at least 16 bpp, a linear framebuffer, it must fit the card's VRAM, and the width must be at most 1280 pixels. Mode information is written to <code>0x600</code>; the 12-dword boot-info block goes to <code>0x500</code>.</li>
 			<li>Enter protected mode and jump to <code>kernel_entry</code>.</li>
 		</ol>
@@ -179,11 +179,13 @@
 
 		<div class="note">
 			<p><strong>BIOS state after the move.</strong> The X201's BIOS
-			leaves translation bits behind after <code>AH=87h</code>;
-			<span class="file">paging.c</span> clears CR4.PAE/PGE/LA57/PCIDE/
-			SMEP/SMAP and EFER.LME before it enables paging. The block move
-			keeps the pack safe until <span class="file">ramfs.c</span> takes a
-			private page-allocated copy and releases the boot region.</p>
+			historically left translation bits behind after
+			<code>AH=87h</code>; <span class="file">paging.c</span> still clears
+			CR4.PAE/PGE/LA57/PCIDE/SMEP/SMAP and EFER.LME before it enables
+			paging, because other BIOS protected-mode calls can do the same.
+			The protected-mode copy keeps the pack safe until
+			<span class="file">ramfs.c</span> takes a private page-allocated
+			copy and releases the boot region.</p>
 		</div>
 	</section>
 

+ 48 - 48
vesa_stage.S

@@ -4,8 +4,11 @@
  * Called by the boot sector in real mode (CS=0, IP=0x7E00).
  * Probes the E820 map, loads the compressed command-pack rootfs in 16 KB
  * chunks through the 0x3000 staging buffer and copies each chunk straight
- * above the 8 MB VGA-shadow reserve (PACK_HIGH_ADDR) with the BIOS block
- * move (INT 15h AH=87h), walks the VBE mode list and picks the best mode (highest resolution,
+ * above the 8 MB VGA-shadow reserve (PACK_HIGH_ADDR) with a temporary
+ * flat protected-mode rep movsb.  The X201 BIOS silently turns the
+ * equivalent INT 15h AH=87h block move into zeros for this range, which
+ * left the kernel with an empty pack.  The stage then walks the VBE mode
+ * list and picks the best mode (highest resolution,
  * >= 16 bpp, LFB, fits in VRAM), writes the mode info to 0x600 and the boot
  * info to 0x500, then enables A20, enters protected mode and jumps to
  * kernel_entry.  Because the pack never occupies the low gap behind the
@@ -194,8 +197,7 @@ load_loop:
 	movl  $PACK_HIGH_ADDR, %eax
 	addl  pack_off, %eax
 	movl  %eax, copy_dst
-	call  bios_copy
-	jc    pack_fail
+	call  pm_copy
 	movl  chunk_sectors, %eax
 	shll  $9, %eax
 	addl  %eax, pack_off
@@ -470,31 +472,48 @@ print_str:
 	popw  %ax
 	ret
 
-/* ---- Copy a chunk to high memory with INT 15h AH=87h ------------------
- * in: copy_dst (24-bit), copy_len; source is 0x3000.
- * Copies in 16-bit words; returns with CF set on failure.  The kernel's
- * paging_prepare_cpu() sanitizes the CR4/EFER state this BIOS call can
- * leave behind on the X201. */
-bios_copy:
-	movl  copy_dst, %eax
-	movw  %ax, bios_gdt + 0x1A      /* dest base 15:0 */
-	shrl  $16, %eax
-	movb  %al, bios_gdt + 0x1C      /* dest base 23:16 */
+/* ---- Flat protected-mode copy -----------------------------------------
+ * The X201 BIOS silently turns INT 15h AH=87h into zeros for this range.
+ * Enter temporary protected mode, copy with a flat rep movsb, then return
+ * to real mode.  SS/ESP are left untouched; only DS/ES are widened. */
+pm_copy:
+	cli
+	lgdt  stage2_gdtr
+	movl  %cr0, %eax
+	orl   $1, %eax
+	movl  %eax, %cr0
+	.byte 0x66, 0xea
+	.long pm_copy32
+	.word 0x08
+.code32
+pm_copy32:
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	cld
+	movl  $0x3000, %esi
+	movl  copy_dst, %edi
 	movl  copy_len, %ecx
-	addl  $1, %ecx                  /* round up for an odd final byte */
-	shrl  $1, %ecx                  /* AH=87h counts words */
-	jz    bios_copy_done
+	rep movsb
+	/* Leave through a 16-bit protected-mode code segment before
+	 * clearing PE, then return to real mode. */
+	.byte 0xea
+	.long pm_copy16pm
+	.word 0x18
+.code16
+pm_copy16pm:
+	movl  %cr0, %eax
+	andl  $0xfffffffe, %eax
+	movl  %eax, %cr0
+	.byte 0xea
+	.word pm_copy16
+	.word 0x0000
+pm_copy16:
 	xorw  %ax, %ax
+	movw  %ax, %ds
 	movw  %ax, %es
-	movw  $bios_gdt, %si
-	movw  $0x8700, %ax
-	int   $0x15
-	jc    bios_copy_fail
-bios_copy_done:
-	clc
-	ret
-bios_copy_fail:
-	stc
+	movw  %ax, %fs
+	movw  %ax, %gs
 	ret
 
 /* ---- Hex print (EAX, 8 digits) ----------------------------------------- */
@@ -583,33 +602,14 @@ stage2_gdt:
 	.byte 0x00, 0x9a, 0xcf, 0x00
 	.word 0xffff, 0x0000
 	.byte 0x00, 0x92, 0xcf, 0x00
+	/* 16-bit code: base 0, 4 GB limit, D=0 */
+	.word 0xffff, 0x0000
+	.byte 0x00, 0x9a, 0x8f, 0x00
 stage2_gdt_end:
 stage2_gdtr:
 	.word stage2_gdt_end - stage2_gdt - 1
 	.long stage2_gdt
 
-/* ---- INT 15h AH=87h descriptor table (48 bytes) -----------------------
- * 16 zero bytes, source descriptor at 0x10 (0x3000, 64 KB), destination
- * at 0x18 (patched per chunk), 16 zero bytes. */
-.align 16
-bios_gdt:
-	.quad 0
-	.quad 0
-	/* source: limit 0xFFFF, base 0x3000, access 0x93, 16-bit data */
-	.word 0xFFFF
-	.word 0x3000
-	.byte 0x00
-	.byte 0x93
-	.word 0x0000
-	/* destination: limit 0xFFFF, base patched, access 0x93 */
-	.word 0xFFFF
-	.word 0x0000
-	.byte 0x00
-	.byte 0x93
-	.word 0x0000
-	.quad 0
-	.quad 0
-
 /* ---- Stage variables --------------------------------------------------- */
 .align 4
 stage_drive:    .byte 0