Browse Source

Fix the X201 boot and PCH LAN probe

The X201 still triple-faulted at the first CR0.PG write.  Its BIOS left
translation state behind after the legacy rootfs copy, and the high-pack
path's INT 15h AH=87h copy was the trigger.  Load the legacy rootfs into
the low gap at 0x60000 instead, sanitize CR4/EFER before enabling paging,
reserve a low LFB range, and fix the LFB PDE range wrap for bases above
0xFF000000.  The AP trampoline applies the same CR4/EFER policy.

The 82577LM probe stalled with the cable connected.  Disable PCI INTx
before enabling memory/bus master, put the function in D0 through the PM
capability, and stop sizing BAR0: the ME/AMT can leave the restored BAR
undecoded, after which every MMIO access wedges.  Add the NOWM diagnostic
build and the boot-console/netlog tracing used to locate both failures.
Johannes Zellner 2 weeks ago
parent
commit
fde9bb69cc
11 changed files with 330 additions and 196 deletions
  1. 45 28
      AGENTS.md
  2. 15 2
      Makefile
  3. 22 0
      ap_tramp.S
  4. 15 0
      bootcon.c
  5. 53 19
      e1000e.c
  6. 24 4
      kernel.c
  7. 3 0
      kernel.ld
  8. 17 0
      mm.c
  9. 81 2
      paging.c
  10. 5 0
      paging.h
  11. 50 141
      vesa_stage.S

+ 45 - 28
AGENTS.md

@@ -20,6 +20,7 @@ make uefi       # build BOOTX64.EFI and the dual-boot usb-efi.img
 make run-uefi   # boot usb-efi.img under OVMF (x86-64 UEFI; serial log in /tmp/opencode)
 make test-uefi  # headless OVMF boot test (greps the serial log for the boot line)
 make rootfs     # build cmds.img (the 8 MB rootfs partition image)
+make nowm       # diagnostic kernel with no WM; the boot console stays visible
 make dev        # serial kernel + rootfs reload (KXE2)
 make kernel-dev # serial kernel-only reload (keeps the running rootfs)
 make net-dev    # network kernel + rootfs reload (KXN2)
@@ -68,17 +69,18 @@ make clean
 1. `boot.S` — 512-byte boot sector (FAT16 BPB). Loads contiguous kernel sectors
    to `0x7E00` (CHS for floppy, INT 13h AH=42h LBA for hard disk), jumps there.
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
-   enables A20, reads the `NBROOT1` rootfs header at the boot sector's
-   `pack_lba` field and copies the gzip pack to the top of RAM below 16 MB
-   with INT 15h AH=87h (no mode switching), 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`,
-   re-points ESP to the current stack top, then `kernel_main`).  The stage
-   prints `Loading rootfs [d=<drive> lba=<hex> e=<0|1> m=<C|L>]...`, then a
-   dot after the header and one per 32 KB chunk, so a BIOS hang is
-   localised.  Drives below 0x80 use CHS (the same path the boot sector's
-   kernel load used); fixed disks use EDD LBA.
+   reads the `NBROOT1` rootfs header at the boot sector's `pack_lba` field
+   and loads the gzip pack into the low gap at `0x60000..0x9FBFF`
+   (16 KB staging chunks at `0x3000`, `rep movsb`; no protected-mode BIOS
+   call), 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`, enables A20, enters
+   protected mode and jumps to `kernel_entry` (`entry.S`, re-points ESP to
+   the current stack top, then `kernel_main`).  The stage prints
+   `Loading rootfs [d=<drive> lba=<hex> e=<0|1> m=<C|L>]...`, then a dot
+   after the header and one per 16 KB chunk, so a BIOS hang is localised.
+   Drives below 0x80 use CHS (the same path the boot sector's kernel load
+   used); fixed disks use EDD LBA.
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
    ACPI/MADT → I/O → idle → processes → timer (PIT; 1 kHz busy, 100 Hz idle + IDT) → SMP AP
    bring-up → mouse → VGA shadow buffer → terminal.
@@ -116,7 +118,8 @@ a Pentium 1 (and a Pentium II board without ACPI) on the UP path.
 | `0x6000`  | AP real/protected-mode trampoline (`ap_tramp.S`/`ap_tramp.ld`; SIPI vector 0x06, image ends below `0x6C00`) |
 | `0x6C00`  | AP trampoline real-mode stack top (grows down) |
 | `0x6F00`  | AP startup info block (CR3, stack top, C entry, CPU index) |
-| `_end`    | Kernel end (linker symbol; `kernel.ld` ASSERTs it stays below `0x7F000`); the command pack is no longer in the image, so this has ~130 KB of headroom |
+| `0x60000` | Legacy rootfs pack (`vesa_stage.S`; compressed gzip data, up to `PACK_LOW_MAX` 0x3FBFF, ending below the EBDA at `0x9FC00`).  Loaded before the kernel; `ramfs_init` copies/inflates it, after which the `0x80000` run area may reuse the space |
+| `_end`    | Kernel end (linker symbol; `kernel.ld` ASSERTs `_end <= 0x60000` for the low rootfs pack and `<= 0x7F000` for the run area).  The low pack leaves only ~6 KB of growth margin |
 | (in-image) | Bootstrap/protected-mode stack: 8 KB `.stack` NOLOAD section, `__boot_stack_top`/`__boot_stack_bottom` (travels with the kernel) |
 | `0x7FF00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
 | `0x80000` | Legacy ring-0 program load area (`run.c`, `serial_recv.c`, `user.ld`; max 0x6000, inside the reserved low 1 MB) |
@@ -127,13 +130,14 @@ buffer before the kernel starts; the UHCI frame list (`0x3000`), ISA DMA
 buffer (`0x4000`) and AP trampoline (`0x6000`) reinitialize those addresses
 later.
 
-The boot-time rootfs pack is also dynamic: the legacy stage puts it at the
-top of RAM below 16 MB (the AH=87h 24-bit descriptor limit), the UEFI loader
-at the top of RAM below 128 MB, and kexec leaves it in the staging buffer
+The boot-time rootfs pack placement depends on the boot path: the legacy
+stage uses the fixed low gap `0x60000..0x9FBFF`, the UEFI loader puts it at
+the top of RAM below 128 MB, and kexec leaves it in the staging buffer
 (≥ `0x900000`).  `mm_init` reserves `[pack_addr, pack_addr+pack_size)` from
-boot info; `ramfs_init` copies/inflates it into fresh pages and
-`mm_free_range()` releases the boot region.  The placement is always above
-the 1..9 MB VGA shadow reserve.
+boot info when it is above the low 1 MB; the legacy low pack is already
+inside the reserved low 1 MB.  `ramfs_init` copies/inflates it into fresh
+pages and `mm_free_range()` releases the boot region when it is above the
+VGA shadow reserve.
 
 Everything else is dynamic: process stacks and the kexec staging buffer come
 from the frame allocator (≥ `0x900000`), and the fallback stack pool is a
@@ -201,7 +205,14 @@ run area), so adding code no longer moves any address.
   `mouse.c` forces the software cursor; `acpi.c` prefers the passed RSDP and
   maps its page before validating it.
 - **Paging**: `paging.c/h` — identity-maps RAM (4 MB pages via CR4.PSE) + the
-  LFB (uncacheable) and enables CR0.PG/WP. Each process gets a page directory
+  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
+  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
   cloned from the kernel identity map (kernel stacks are mapped everywhere, so
   CR3 can switch on the old stack); `paging_current_cr3()` reads CR3. The kexec
   trampoline (`kexec_tramp.S`) clears CR0.PG before the copy so it can overwrite
@@ -343,8 +354,10 @@ run area), so adding code no longer moves any address.
 - **Serial**: `serial.c`, `serial_ring_buf.c/h`, `bin_recv.c/h`, `serial_recv.c`,
   `serial_loop.c`, `send_serial.c` — 16550 UART, IRQ-driven ring buffer, XMODEM.
 - **Network**: `netdev.c/h` — NIC driver registry (`struct netdev`, first probe
-  wins; drivers: `r8168.c` RTL8111/8168 (PCIe, polled, C+ mode, 64-entry RX
-  ring) and `ne2000.c`
+  wins; drivers: `e1000e.c` Intel e1000/PCH LAN (82540EM emulated,
+  82577LM/82578/82579; polled, PCI INTx disabled before MMIO, PCH D0 and
+  no BAR sizing so the ME/AMT cannot leave the BAR undecoded), `r8168.c`
+  RTL8111/8168 (PCIe, polled, C+ mode, 64-entry RX ring) and `ne2000.c`
   RTL8029 (QEMU/ISA)). `net.c` — ARP + ICMP echo, IPv4/UDP send + parse helpers,
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
   decode, `net_checksum()`/`net_checksum_tcp()`. `dhcp.c` — minimal DHCP client
@@ -452,7 +465,7 @@ run area), so adding code no longer moves any address.
   `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 high RAM (legacy AH=87h / UEFI / kexec) and `ramfs_init`
+  loads it into RAM (legacy low gap / 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
@@ -695,11 +708,12 @@ re-applied on a full floppy/USB boot (C-light reuses the current LFB).
 The commands live in `cmds.pack` (`bin/<name>` entries, built by `mkpack`),
 gzip-compressed to `cmds_pack_gz.bin` and wrapped by `mkrootfs` with a
 one-sector `NBROOT1` header (magic, version, exact `pack_bytes`, checksum,
-gzip flag).  The boot path loads the gz pack into high RAM — the legacy stage
-via INT 15h AH=87h, the UEFI loader via an embedded copy, 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 can grow without touching `_end <= 0x7F000`.
+gzip flag).  The boot path loads the gz pack into RAM — the legacy stage into
+the low gap at `0x60000`, the UEFI loader via an embedded copy at the top of
+RAM below 128 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
+legacy low gap has its own size cap (known issue 9).
 
 `rootfs.bin` is the floppy append (header + pack); `cmds.img` is the 8 MB
 rootfs partition image written to USB partition 2.  `cmds-dev`/`net-cmds`
@@ -1008,7 +1022,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 9. 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).
+   raised without a bigger partition).  The legacy low pack is additionally
+   capped at `PACK_LOW_MAX` (0x3FBFF, ~255 KB compressed) by the
+   `0x60000..0x9FBFF` gap; a larger pack needs the UEFI/kexec path or a
+   high-memory legacy copy.
 10. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
     board that exposes only MP tables stays UP (deliberate; there is no test
     hardware).

+ 15 - 2
Makefile

@@ -13,6 +13,13 @@ ASFLAGS  = --32 -I.
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 
+# NOWM=1 builds a diagnostic kernel that boots to the console without the
+# window manager (make nowm).
+NOWM ?= 0
+ifeq ($(NOWM),1)
+CFLAGS += -DNOWM
+endif
+
 # UEFI loader: a PE32+ x86-64 EFI application built with the mingw toolchain.
 # The kernel itself stays 32-bit; the loader leaves long mode before jumping.
 EFI_CC   ?= x86_64-w64-mingw32-gcc
@@ -325,8 +332,8 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h pr
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
-	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
+kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
+	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
 
@@ -713,6 +720,12 @@ rootfs.bin: cmds_pack_gz.bin mkrootfs
 
 rootfs: cmds.img
 
+# Diagnostic: no WM, boot console stays visible.  Rebuild normally with
+# `make -B ...` afterwards (the objects carry -DNOWM).
+nowm:
+	$(MAKE) NOWM=1 -B kernel.elf kernel.bin floppy.img usb.img usb-efi.img final.bin
+	@echo "nowm: WM disabled; flash usb.img or usb-efi.img."
+
 cmds.img: cmds_pack_gz.bin mkrootfs
 	./mkrootfs $@ cmds_pack_gz.bin $$(( $(ROOTFS_SECTORS) * 512 ))
 

+ 22 - 0
ap_tramp.S

@@ -55,10 +55,32 @@ ap_pm32:
 	movl  AP_INFO_CR3, %eax
 	movl  %eax, %cr3
 
+	/* Clear stale translation controls left by the firmware (PAE/PGE/
+	 * LA57/PCIDE/SMEP/SMAP) and enable PSE.  An AP that starts with
+	 * CR4.PAE set would otherwise walk the kernel page directory as a PAE
+	 * PDPT the moment CR0.PG is set. */
 	movl  %cr4, %eax
+	andl  $0xFFCDEF5F, %eax
 	orl   $0x10, %eax             /* CR4.PSE */
 	movl  %eax, %cr4
 
+	/* Clear EFER.LME too when long mode exists and the firmware left it
+	 * set; otherwise setting CR0.PG would enter long mode. */
+	movl  $0x80000000, %eax
+	cpuid
+	cmpl  $0x80000001, %eax
+	jb    3f
+	movl  $0x80000001, %eax
+	cpuid
+	testl $(1 << 29), %edx
+	jz    3f
+	movl  $0xC0000080, %ecx
+	rdmsr
+	testl $(1 << 8), %eax
+	jz    3f
+	andl  $~0x100, %eax
+	wrmsr
+3:
 	movl  %cr0, %eax
 	orl   $0x80010000, %eax       /* CR0.PG | CR0.WP */
 	movl  %eax, %cr0

+ 15 - 0
bootcon.c

@@ -8,6 +8,7 @@
 #include "bootcon.h"
 #include "bootinfo.h"
 #include "font_8x8.h"
+#include "serial.h"
 #include "vga.h"
 
 #define SCALE  2
@@ -220,4 +221,18 @@ void bootcon_fb_info(void)
 	bootcon_puts(" lfb 0x");
 	bootcon_hex(fb_base);
 	bootcon_puts("\n");
+
+	/* Mirror to COM1 (and the netlog console) so a machine without a
+	 * serial port can report the VBE mode before the WM repaints. */
+	serial_puts(SERIAL_COM1, "fb ");
+	serial_putdec(SERIAL_COM1, fb_w);
+	serial_puts(SERIAL_COM1, "x");
+	serial_putdec(SERIAL_COM1, fb_h);
+	serial_puts(SERIAL_COM1, "x");
+	serial_putdec(SERIAL_COM1, fb_bpp);
+	serial_puts(SERIAL_COM1, " pitch ");
+	serial_putdec(SERIAL_COM1, fb_pitch);
+	serial_puts(SERIAL_COM1, " lfb ");
+	serial_puthex(SERIAL_COM1, fb_base);
+	serial_puts(SERIAL_COM1, "\n");
 }

+ 53 - 19
e1000e.c

@@ -13,6 +13,7 @@
  * the user process.
  */
 #include "e1000e.h"
+#include "bootcon.h"
 #include "io.h"
 #include "mm.h"
 #include "netdev.h"
@@ -137,6 +138,7 @@
 #define BMCR_ANRESTART 0x0200
 
 /* ---- PCI ---- */
+#define PCI_CMD_INT_DISABLE 0x0400u   /* command bit 10: INTx disabled */
 #define E1000E_VENDOR 0x8086u
 
 struct e1000e_rx_desc {
@@ -207,6 +209,12 @@ static void dbg(const char *s)
 	serial_puts(SERIAL_COM1, "e1000e: ");
 	serial_puts(SERIAL_COM1, s);
 	serial_puts(SERIAL_COM1, "\n");
+	/* Also show the trail on the boot console (NOWM diagnostics): if the
+	 * probe wedges, the last line says which step.  No-op once the WM has
+	 * taken over and disabled the console. */
+	bootcon_puts("e1000e: ");
+	bootcon_puts(s);
+	bootcon_puts("\n");
 }
 
 static void put_dec(unsigned int v)
@@ -311,32 +319,51 @@ static int probe_bar0(unsigned int bus, unsigned int dev, unsigned int func,
 		      unsigned int *base, unsigned int *size)
 {
 	unsigned int val = pci_cfg_read32(bus, dev, func, 0x10);
-	unsigned int hi = 0, mask, len;
 
 	if (!val || val == 0xFFFFFFFFu || (val & 1u))
 		return -1;
-	if (val & 4u) {
-		hi = pci_cfg_read32(bus, dev, func, 0x14);
-		pci_cfg_write32(bus, dev, func, 0x14, 0xFFFFFFFFu);
-	}
-	pci_cfg_write32(bus, dev, func, 0x10, 0xFFFFFFFFu);
-	mask = pci_cfg_read32(bus, dev, func, 0x10) & 0xFFFFFFF0u;
-	pci_cfg_write32(bus, dev, func, 0x10, val);
+	/* Do NOT size the BAR by writing 0xFFFFFFFF: on PCH LAN parts the
+	 * ME/AMT can leave the BAR restore incomplete, after which every MMIO
+	 * access goes to an undecoded address and stalls.  All supported
+	 * e1000/e1000e BARs are 128 KB, so trust the firmware-assigned base. */
 	if (val & 4u)
-		pci_cfg_write32(bus, dev, func, 0x14, hi);
-	if (!mask || mask == 0xFFFFFFF0u)
-		return -1;
-
-	len = ~mask + 1u;
-	if (len < 0x1000)
-		len = 0x1000;
-	if (len > 0x100000)
-		len = 0x100000;      /* sanity: all supported BARs are 128 KB */
+		return -1;               /* 64-bit BAR: not used by these parts */
 	*base = val & 0xFFFFFFF0u;
-	*size = len;
+	*size = 0x20000;
 	return 0;
 }
 
+/* Put the function in D0 before the first MMIO access.  The PCH LAN is
+ * often left in D3hot by the BIOS/ME; MMIO while suspended can return
+ * 0xFFFFFFFF or stall the PCIe root complex.  Walk the capability list for
+ * the PM capability (ID 1) and clear PMCSR.PS. */
+static void pci_set_d0(unsigned int bus, unsigned int dev, unsigned int func)
+{
+	unsigned int status = pci_cfg_read32(bus, dev, func, 0x04) >> 16;
+	unsigned int cap, guard = 0;
+
+	if (!(status & 0x10u))
+		return;                       /* no capability list */
+	cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xFCu;
+	while (cap && guard++ < 48) {
+		unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
+
+		if ((hdr & 0xFFu) == 0x01u) {
+			unsigned int pmcsr =
+				pci_cfg_read32(bus, dev, func, cap + 4);
+
+			if (pmcsr & 3u) {
+				pci_cfg_write32(bus, dev, func, cap + 4,
+						pmcsr & ~3u);
+				delay_ms(10);
+				dbg("PM state -> D0");
+			}
+			return;
+		}
+		cap = (hdr >> 8) & 0xFCu;
+	}
+}
+
 static int probe_pci(void)
 {
 	unsigned int bus, dev, func;
@@ -364,8 +391,15 @@ static int probe_pci(void)
 
 				/* Memory space + bus master. */
 				cmd = pci_cfg_read32(bus, dev, func, 0x04);
+				/* Polled driver: disable INTx before enabling memory
+				 * and bus master.  A link-change interrupt from
+				 * the PHY can otherwise fire before hw_stop()
+				 * masks the device, and the kernel has no NIC IRQ
+				 * handler (the X201 stalls with the cable in). */
 				pci_cfg_write32(bus, dev, func, 0x04,
-						cmd | 0x0006u);
+						cmd | 0x0006u |
+						PCI_CMD_INT_DISABLE);
+				pci_set_d0(bus, dev, func);
 
 				if (probe_bar0(bus, dev, func,
 					       &bar_base, &bar_size) != 0)

+ 24 - 4
kernel.c

@@ -230,15 +230,22 @@ static void system_housekeeping(void)
 {
 	process_reap_dead();
 	mouse_poll_update();
+#ifndef NOWM
 	wm_pump_keyboard();
 	wm_blink_cursor();
+#endif
 	netlog_flush();
+#ifndef NOWM
 	wm_handle_kexec();
 	wm_server_supervise();
 	if (wm_server_can_reap())
 		sys_gfx_collect();
 	if (!wm_server_frame_active())
 		vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
+#else
+	/* Diagnostic build: keep the direct-LFB boot console visible and do
+	 * not let the WM shadow flush overwrite it. */
+#endif
 }
 
 static void kworker_main(void)
@@ -350,6 +357,9 @@ void kernel_main(void)
 	boot_trace("mm");
 	boot_val("mm free KB", mm_free_pages() * 4);
 	kmalloc_init();
+	/* Report the VBE geometry before paging, so a boot that dies in the
+	 * translation switch still says which mode the BIOS picked. */
+	bootcon_fb_info();
 	paging_init();
 	boot_trace("paging");
 	/* paging_init only ORs in PSE; make sure the BSP's SSE state is on
@@ -398,12 +408,11 @@ void kernel_main(void)
 	vga_shadow_init();
 	vga_fb_flush();
 	boot_trace("vga");
-	bootcon_fb_info();
 
 	/* The command pack: read-only, mounted at /ramfs (see ramfs.c).  The
-	 * boot path copied it into high RAM (the legacy stage, the UEFI loader
-	 * or kexec); ramfs_init takes its own copy so the boot region can be
-	 * released.  The mount is created even without a pack, so a running
+	 * boot path copied it into RAM (the legacy stage's low gap, the UEFI
+	 * loader or kexec); ramfs_init takes its own copy so the boot region
+	 * can be released.  The mount is created even without a pack, so a running
 	 * board can be recovered with `make cmds-dev`/`make net-cmds`. */
 	{
 		unsigned int pack_addr = bootinfo_pack_addr();
@@ -436,6 +445,7 @@ void kernel_main(void)
 	boot_trace("ramfs");
 	boot_val("ramfs files", ramfs_count());
 
+#ifndef NOWM
 	/* Start the WM before touching disks: the first frame can be painted
 	 * while storage and the network are still being probed. */
 	wm_server_init();
@@ -445,6 +455,12 @@ void kernel_main(void)
 	bootcon_set_enabled(0);
 	wm_server_start();
 	boot_trace("wm");
+#else
+	/* Diagnostic build: boot to the console, no WM, no terminal. */
+	bootcon_puts("WM disabled (diagnostic build)\n");
+	serial_puts(SERIAL_COM1, "nowm: window manager disabled\n");
+	boot_trace("wm off");
+#endif
 
 	/* Probe the NIC (link negotiation is asynchronous now) before the
 	 * kworker starts, so its netlog flushes have a device to use. */
@@ -471,8 +487,12 @@ void kernel_main(void)
 	/* The first terminal is spawned only now: while the boot scan was
 	 * rebuilding the mount table, no user command could touch the disks.
 	 * The desktop itself was already painted from /ramfs. */
+#ifndef NOWM
 	wm_spawn_terminal();
 	boot_trace("terminal");
+#else
+	boot_trace("terminal off");
+#endif
 
 	/* With a NIC present, try DHCP; on failure the static default stays.
 	 * Deliberately after the WM: a missing cable or DHCP server must not

+ 3 - 0
kernel.ld

@@ -29,3 +29,6 @@ SECTIONS
 	_end = .;
 }
 ASSERT(_end <= 0x7F000, "Kernel too large: would reach the run area at 0x80000");
+/* The legacy stage loads the compressed rootfs at 0x60000; the BSS clear and
+ * the boot stack must stay below it. */
+ASSERT(_end <= 0x60000, "Kernel too large: would overlap the low rootfs pack");

+ 17 - 0
mm.c

@@ -3,6 +3,7 @@
 #include "mm.h"
 #include "bootinfo.h"
 #include "serial.h"
+#include "vga.h"
 
 /* One bit per 4 KB page; bit set = in use. */
 #define MM_BITMAP_BITS  (MM_MAX_PHYS / PAGE_SIZE)
@@ -110,6 +111,22 @@ void mm_init(void)
 	mark_used(0, MM_RESERVE_LOW);
 	mark_used(MM_SHADOW_BASE, MM_SHADOW_RESERVE);
 
+	/* The VBE LFB is normally high MMIO, but a BIOS may place it inside
+	 * low RAM.  Never hand framebuffer pages to the allocator: the boot
+	 * console draws into the LFB, so an allocated page directory there
+	 * would be overwritten by its own progress line. */
+	{
+		const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
+		unsigned int sz;
+
+		__asm__ volatile ("" : "+r"(fb));
+		sz = (unsigned int)fb->pitch * fb->height;
+		if (!sz)
+			sz = 0x1000000;
+		if (fb->valid && fb->lfb_base && fb->lfb_base < MM_MAX_PHYS)
+			mark_used(fb->lfb_base, sz);
+	}
+
 	/* The boot path copied the command-pack rootfs into high RAM before
 	 * the kernel ran.  Keep it away from the allocator until ramfs_init()
 	 * has taken its own copy. */

+ 81 - 2
paging.c

@@ -9,11 +9,29 @@
  */
 
 #include "paging.h"
+#include "cpu.h"
 #include "mm.h"
 #include "mtrr.h"
 #include "serial.h"
 #include "vga.h"
 
+/* CR4 bits that must not survive a BIOS protected-mode call.  A BIOS that
+ * returns with CR4.PAE or EFER.LME set makes the first CR0.PG enable either
+ * walk the 32-bit page directory as a PAE PDPT or enter long mode, which
+ * triple-faults before the kernel can print anything.  The X201's old
+ * INT 15h AH=87h rootfs copy was one source of this state; clear the bits
+ * defensively on every boot path before enabling paging. */
+#define CR4_PSE    (1u << 4)
+#define CR4_PAE    (1u << 5)
+#define CR4_PGE    (1u << 7)
+#define CR4_LA57   (1u << 12)
+#define CR4_PCIDE  (1u << 17)
+#define CR4_SMEP   (1u << 20)
+#define CR4_SMAP   (1u << 21)
+
+#define MSR_EFER   0xC0000080u
+#define EFER_LME   (1u << 8)
+
 /* Page-directory entry flags (4 MB page). */
 #define PDE_PRESENT  0x001
 #define PDE_WRITABLE 0x002
@@ -27,6 +45,59 @@
 #define LFB_MAP_SIZE 0x1000000
 
 static unsigned int *page_dir;
+
+static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
+{
+	__asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
+}
+
+static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
+{
+	__asm__ volatile ("wrmsr" : : "a"(lo), "d"(hi), "c"(msr));
+}
+
+static int cpu_has_long_mode(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+
+	if (!cpu_has_cpuid())
+		return 0;
+	eax = 0x80000000u;
+	__asm__ volatile ("cpuid"
+			  : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
+	if (eax < 0x80000001u)
+		return 0;
+	eax = 0x80000001u;
+	__asm__ volatile ("cpuid"
+			  : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
+	return (edx & (1u << 29)) != 0;   /* LM */
+}
+
+/* Put the CPU into the state this 32-bit kernel expects before it installs
+ * its own page directory.  A BIOS protected-mode call (notably the old
+ * high-pack INT 15h AH=87h path) may leave CR4.PAE/PGE or EFER.LME set;
+ * CR0.PG is off here, so clearing them is safe on every CPU (reserved bits
+ * are written as zero). */
+void paging_prepare_cpu(void)
+{
+	unsigned int cr4, lo, hi;
+
+	__asm__ volatile ("movl %%cr4, %0" : "=r"(cr4));
+	cr4 &= ~(CR4_PAE | CR4_PGE | CR4_LA57 | CR4_PCIDE |
+		 CR4_SMEP | CR4_SMAP);
+	cr4 |= CR4_PSE;
+	__asm__ volatile ("movl %0, %%cr4" : : "r"(cr4) : "memory");
+
+	if (cpu_has_long_mode()) {
+		rdmsr(MSR_EFER, &lo, &hi);
+		if (lo & EFER_LME) {
+			lo &= ~EFER_LME;
+			wrmsr(MSR_EFER, lo, hi);
+			serial_puts(SERIAL_COM1, "paging: cleared EFER.LME\n");
+		}
+	}
+}
+
 /* LFB page-type decision, kept so it can be re-applied after other MMIO
  * mappings (the GPU BARs cover the framebuffer address) have set PCD. */
 static unsigned int lfb_wc_base, lfb_wc_size;
@@ -44,8 +115,14 @@ static void lfb_apply_pages(unsigned int *pd)
 		return;
 	base = fb->lfb_base;
 	p0 = base >> 22;
-	p1 = (base + LFB_MAP_SIZE) >> 22;
-	for (i = p0; i < p1 && i < 1024; i++) {
+	/* Compute the end page from the page index, not from base+size: for a
+	 * base above 0xFF000000 the 32-bit addition wraps to a small number
+	 * and the loop would map nothing, faulting on the first LFB write
+	 * after CR0.PG comes on. */
+	p1 = p0 + (LFB_MAP_SIZE >> 22);
+	if (p1 > 1024)
+		p1 = 1024;
+	for (i = p0; i < p1; i++) {
 		unsigned int page = i << 22;
 		unsigned int flags = PDE_PS | PDE_WRITABLE | PDE_PRESENT;
 
@@ -88,6 +165,8 @@ void paging_init(void)
 {
 	unsigned int i;
 	const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
+
+	paging_prepare_cpu();
 	/* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
 	__asm__ volatile ("" : "+r"(fb));
 

+ 5 - 0
paging.h

@@ -3,6 +3,11 @@
 #define PAGING_H
 
 void paging_init(void);
+/* Clear translation controls a BIOS protected-mode call may have left set
+ * (CR4.PAE/PGE/LA57/PCIDE, EFER.LME) before this 32-bit kernel enables its
+ * own page directory.  Called by paging_init(); the AP trampoline applies
+ * the same policy inline. */
+void paging_prepare_cpu(void);
 /* Re-apply the framebuffer page types (after GPU BAR mappings that cover it). */
 void paging_lfb_refresh(void);
 /* 1 when the LFB is mapped for write-combining. */

+ 50 - 141
vesa_stage.S

@@ -2,11 +2,12 @@
  * VESA real-mode stage: runs at 0x7E00 before protected mode.
  *
  * Called by the boot sector in real mode (CS=0, IP=0x7E00).
- * Probes the E820 map, enables A20, copies the command-pack rootfs into
- * high memory (INT 15h AH=87h, so the CPU never leaves real mode), 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 enters protected mode and jumps to kernel_entry.
+ * Probes the E820 map, loads the compressed command-pack rootfs into the
+ * low gap at 0x60000..0x9FBFF (16 KB staging chunks at 0x3000, BIOS disk
+ * reads only), 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.
  *
  * If no VESA mode works, boots in text mode (valid=0 at 0x600).
  */
@@ -21,9 +22,8 @@
 
 /* Rootfs/boot-info constants (must match rootfs.h / bootinfo.h). */
 .equ ROOTFS_VERSION,   1
-.equ PACK_MAX,         0x400000      /* 4 MB gzip payload cap */
-.equ PACK_MIN_ADDR,    0x900000      /* just above the VGA shadow reserve */
-.equ PACK_TOP_ADDR,    0x1000000     /* INT 15h AH=87h has a 24-bit base */
+.equ PACK_LOW_ADDR,    0x60000       /* low gap behind the kernel */
+.equ PACK_LOW_MAX,     0x3FBFF       /* 0x60000..0x9FBFF */
 .equ BOOT_INFO_MAGIC,  0x4E425349
 .equ BOOT_INFO_VERSION, 2
 .equ BOOT_INFO_WORDS,  12
@@ -87,11 +87,11 @@ e820_loop:
 	jmp   e820_loop
 e820_done:
 
-	/* ---- Rootfs: copy the command pack into high RAM ----
+	/* ---- Rootfs: load the command pack into the low gap ----
 	 * The pack is a gzip blob behind a one-sector NBROOT1 header (rootfs.h).
-	 * The destination is the top of RAM below 16 MB (the AH=87h block move
-	 * uses 24-bit descriptor bases), well above the 1..9 MB shadow reserve. */
-	call  enable_a20
+	 * Keep the destination at 0x60000..0x9FBFF: above the kernel BSS/boot
+	 * stack, below the EBDA, and inside the low 1 MB the frame allocator
+	 * already reserves. */
 	xorw  %ax, %ax
 	movw  %ax, %ds
 	movw  %ax, %es
@@ -129,6 +129,12 @@ e820_done:
 	movw  $msg_end, %si
 	call  print_str
 
+	xorw  %ax, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+
+	/* Read and validate the header, then copy the payload in 16 KB
+	 * chunks (32 sectors) through the 0x3000 staging buffer. */
 	cmpl  $0, pack_lba
 	je    pack_fail
 
@@ -150,17 +156,12 @@ e820_done:
 	movl  12(%esi), %eax          /* pack_bytes */
 	testl %eax, %eax
 	jz    pack_fail
-	cmpl  $PACK_MAX, %eax
+	cmpl  $PACK_LOW_MAX, %eax
 	ja    pack_fail
 	movl  %eax, pack_bytes
 	movw  $msg_dot, %si
 	call  print_str
 
-	call  compute_pack_addr
-	testl %eax, %eax
-	jz    pack_noroom
-	movl  %eax, pack_addr
-
 	movl  $0, pack_off
 load_loop:
 	movl  pack_off, %eax
@@ -183,11 +184,10 @@ load_loop:
 	movl  chunk_sectors, %eax
 	shll  $9, %eax
 	movl  %eax, copy_len
-	movl  pack_addr, %eax
+	movl  $PACK_LOW_ADDR, %eax
 	addl  pack_off, %eax
 	movl  %eax, copy_dst
-	call  bios_copy
-	jc    pack_fail
+	call  copy_low
 	movl  chunk_sectors, %eax
 	shll  $9, %eax
 	addl  %eax, pack_off
@@ -196,15 +196,11 @@ load_loop:
 	jmp   load_loop
 
 load_ok:
+	movl  $PACK_LOW_ADDR, pack_addr
 	movw  $msg_ok, %si
 	call  print_str
 	jmp   pack_done
 
-pack_noroom:
-	movw  $msg_noroom, %si
-	call  print_str
-	jmp   pack_clear
-
 pack_fail:
 	movw  $msg_fail, %si
 	call  print_str
@@ -349,8 +345,8 @@ vesa_no_vbe:
 	movb  $0, (%bx)
 
 stage_finish:
-	/* Publish the boot info after the last BIOS call that could touch
-	 * 0x500, then enter protected mode. */
+	/* The pack is loaded; enable A20 and enter protected mode. */
+	call  enable_a20
 	call  write_bootinfo
 	jmp   enter_pm
 
@@ -451,92 +447,6 @@ read_ok:
 	clc
 	ret
 
-/* ---- Pick the pack destination from the E820 map -----------------------
- * The AH=87h block move uses 24-bit bases, so stay below 16 MB.
- * out: eax = 4 KB-aligned destination (0 when there is no room) */
-compute_pack_addr:
-	movl  pack_bytes, %ebx
-	addl  $0xFFF, %ebx
-	andl  $0xFFFFF000, %ebx
-	movl  $0, best_addr
-
-	cmpl  $E820_MAGIC, 0x1000
-	jne   pack_addr_fallback
-	movl  0x1004, %ecx
-	testl %ecx, %ecx
-	jz    pack_addr_fallback
-	movl  $0x1008, %esi
-
-pack_addr_loop:
-	cmpl  $1, 16(%esi)            /* E820_RAM */
-	jne   1f
-	movl  0(%esi), %eax           /* base low */
-	cmpl  $PACK_TOP_ADDR, %eax
-	jae   1f
-	movl  4(%esi), %edx           /* base high */
-	testl %edx, %edx
-	jnz   1f
-	addl  8(%esi), %eax           /* end = base + length low */
-	jc    2f
-	cmpl  $PACK_TOP_ADDR, %eax
-	jbe   3f
-2:	movl  $PACK_TOP_ADDR, %eax
-3:	/* Need [end - need, end) to fit inside the entry. */
-	movl  0(%esi), %edx
-	addl  %ebx, %edx
-	jc    1f
-	cmpl  %edx, %eax
-	jb    1f
-	subl  %ebx, %eax
-	andl  $0xFFFFF000, %eax
-	cmpl  $PACK_MIN_ADDR, %eax
-	jb    1f
-	cmpl  best_addr, %eax
-	jbe   1f
-	movl  %eax, best_addr
-1:	addl  $24, %esi
-	decl  %ecx
-	jnz   pack_addr_loop
-	movl  best_addr, %eax
-	ret
-
-pack_addr_fallback:
-	/* No usable E820 map: matches mm_init's 16 MB fallback. */
-	movl  $PACK_TOP_ADDR, %eax
-	cmpl  %ebx, %eax
-	jbe   1f
-	subl  %ebx, %eax
-	andl  $0xFFFFF000, %eax
-	cmpl  $PACK_MIN_ADDR, %eax
-	jb    1f
-	ret
-1:	xorl  %eax, %eax
-	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. */
-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 */
-	movl  copy_len, %ecx
-	shrl  $1, %ecx                  /* AH=87h counts words */
-	jz    bios_copy_done
-	xorw  %ax, %ax
-	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
-	ret
-
 /* ---- Print a NUL-terminated string with the BIOS teletype ------------- */
 print_str:
 	pushw %ax
@@ -552,6 +462,29 @@ print_str:
 	popw  %ax
 	ret
 
+/* ---- Copy the 16 KB staging chunk to the low pack destination --------- */
+copy_low:
+	/* Read the stage variables with DS=0 first; only then switch DS to the
+	 * staging segment.  Save both segment registers: the load loop and the
+	 * progress prints keep using DS=0/ES=0. */
+	movl  copy_dst, %eax
+	movl  copy_len, %ecx
+	movl  %eax, %ebx
+	shrl  $4, %eax
+	pushw %ds
+	pushw %es
+	movw  %ax, %es
+	andl  $0x0000000F, %ebx
+	movw  %bx, %di
+	movw  $0x0300, %ax
+	movw  %ax, %ds
+	xorw  %si, %si
+	cld
+	rep movsb
+	popw  %es
+	popw  %ds
+	ret
+
 /* ---- Hex print (EAX, 8 digits) ----------------------------------------- */
 print_hex32:
 	movl  $8, %ecx
@@ -568,10 +501,10 @@ print_hex:
 	addl  $55, %eax
 	jmp   3f
 2:	addl  $48, %eax
-3:	movb  $0x0E, %ah
-	movb  $0x00, %bh
-	pushl %ebx
+3:	pushl %ebx
 	pushl %ecx
+	movb  $0x0E, %ah
+	xorw  %bx, %bx
 	int   $0x10
 	popl  %ecx
 	popl  %ebx
@@ -643,28 +576,6 @@ 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
@@ -676,7 +587,6 @@ pack_off:       .long 0
 chunk_sectors:  .long 0
 copy_dst:       .long 0
 copy_len:       .long 0
-best_addr:      .long 0
 rd_lba:         .long 0
 rd_off:         .word 0
 rd_left:        .word 0
@@ -689,7 +599,6 @@ msg_m:          .asciz " m="
 msg_end:        .asciz "]... "
 msg_dot:        .asciz "."
 msg_ok:         .asciz " ok\r\n"
-msg_noroom:     .asciz " skipped (no room)\r\n"
 msg_fail:       .asciz " failed\r\n"
 
 .align 2