Sfoglia il codice sorgente

Raise RAM to 512 MB and the legacy pack cap to 1 MB

Move the physical ceiling into phys.h so the kernel, panic handler and UEFI loader share MM_MAX_PHYS = 512 MB.  The frame bitmap grows to 16 KB; total_ram_kb is clipped to the ceiling.  QEMU run and UEFI targets now pass -m $(QEMU_MEM), defaulting to 512M.

Raise PACK_MAX and CMDS_GZ_MAX to 1 MB; the existing INT 15h AH=87h high-memory copy is size-independent.  Fix the libdeflate invocation: the installed libdeflate-gzip does not accept gzip's -n flag, so use libdeflate-gzip -12 -c (gzip -9 -n -c remains the fallback).  The current pack is 351760 bytes compressed, leaving about 680 KiB of the new cap; the floppy image still fits at 1.44 MB.

Verified with make all, make test-uefi at 512 MB, make test-libc, make test-font, a QEMU meminfo showing the 512 MB ceiling, and a synthetic 659700-byte pack that boots through the high-memory loader and mounts 52 ramfs files.
Johannes Zellner 1 settimana fa
parent
commit
a4a50d1655
13 ha cambiato i file con 116 aggiunte e 84 eliminazioni
  1. 20 17
      AGENTS.md
  2. 37 33
      Makefile
  3. 3 3
      docs/boot.html
  4. 6 0
      docs/development.html
  5. 4 4
      docs/filesystems.html
  6. 1 1
      docs/index.html
  7. 17 16
      docs/memory.html
  8. 5 4
      efi/main.c
  9. 7 1
      mm.c
  10. 4 3
      mm.h
  11. 2 1
      panic.c
  12. 9 0
      phys.h
  13. 1 1
      vesa_stage.S

+ 20 - 17
AGENTS.md

@@ -83,6 +83,8 @@ make clean
 
 - Build flags (in `Makefile`): `-m32 -march=i586 -ffreestanding -fno-pie -nostdlib
   -fno-stack-protector -mno-sse -mno-sse2 -mno-mmx -O2`.
+- QEMU guests default to 512 MB (`QEMU_MEM`, e.g. `make run QEMU_MEM=1G`),
+  matching `MM_MAX_PHYS` in `phys.h`; all run and UEFI targets pass `-m`.
 - The command pack is the system rootfs: `cmds.pack` (NBPAK1), gzip-compressed
   to `cmds_pack_gz.bin`, wrapped by `mkrootfs` with a one-sector `NBROOT1`
   header into `rootfs.bin` (floppy append) and `cmds.img` (the 8 MB USB
@@ -127,7 +129,7 @@ make clean
    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 pack
-   never occupies the low gap, so the cap is `PACK_MAX` (512 KB) and the
+   never occupies the low gap, so the cap is `PACK_MAX` (1 MB) and the
    kernel BSS/boot stack may extend past `0x60000`.  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.
@@ -143,7 +145,7 @@ make clean
    picks a GOP mode, converts `GetMemoryMap` into the E820 map at `0x1000`,
    stages `kernel.bin` below 4 GB, fills `0x500` (boot info) and `0x600`
    (framebuffer), calls `ExitBootServices`, copies the embedded gz rootfs to
-   the top of RAM below 128 MB, copies a 192-byte trampoline to `0x800`,
+   the top of RAM below 512 MB, copies a 192-byte trampoline to `0x800`,
    leaves IA-32e mode (`CR0.PG`, `EFER.LME`, `CR4.PAE/PGE/PCIDE/SMEP/SMAP/
    LA57`), copies the kernel to `0x7E00` and jumps to `kernel_entry`.  The
    BIOS boot sector / VESA stage are not executed on this path; the legacy
@@ -184,7 +186,7 @@ later.
 
 The boot-time rootfs pack placement depends on the boot path: the legacy
 stage copies it directly to `0x900000`, the UEFI loader puts it at the top of
-RAM below 128 MB, and kexec leaves it in the staging buffer
+RAM below 512 MB, and kexec leaves it in the staging buffer
 (≥ `0x900000`).  `mm_init` reserves `[pack_addr, pack_addr+pack_size)` from
 boot info when it is above the low 1 MB, so the legacy pack is protected
 from the frame allocator too.  `ramfs_init` copies/inflates it into fresh
@@ -239,9 +241,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   trampoline at `0x6000`), and `spinlock.h` (the IRQ-safe spinlocks).
   `cpu_enable_sse()` runs on every AP: an AP starts with firmware CR4, and the
   SSE2 drawing fast paths #UD until OSFXSR/OSXMMEXCPT are set.
-- **Memory**: `mm.c/h` — INT 15h E820 detection (real-mode in `vesa_stage.S`,
-  map at `0x1000`) + a bitmap page-frame allocator (4 KB pages). Reserves low
-  1 MB and an 8 MB VGA-shadow region. `kmalloc.c/h` — a power-of-two heap
+- **Memory**: `phys.h` — the shared 512 MB physical ceiling (`MM_MAX_PHYS`),
+  also used by the UEFI loader. `mm.c/h` — INT 15h E820 detection (real-mode in
+  `vesa_stage.S`, map at `0x1000`) + a bitmap page-frame allocator (4 KB
+  pages). Reserves low 1 MB and an 8 MB VGA-shadow region. `kmalloc.c/h` — a power-of-two heap
   allocator on top of the frame allocator (used for PCBs and command context).
   Process stacks (≥ `0x900000`) come from the frame allocator, with an 8-stack
   fallback pool (a static BSS array; no fixed address).
@@ -846,14 +849,14 @@ The commands live in `cmds.pack` (`bin/<name>` entries, built by `mkpack`),
 compressed with `libdeflate-gzip -12` when it is installed (falling back to
 `gzip -9`) to `cmds_pack_gz.bin` and wrapped by `mkrootfs` with a
 one-sector `NBROOT1` header (magic, version, exact `pack_bytes`, checksum,
-gzip flag).  The build rejects a pack above the `PACK_MAX` cap (512 KB);
+gzip flag).  The build rejects a pack above the `PACK_MAX` cap (1 MB);
 `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 via an embedded copy at the
-top of RAM below 128 MB, kexec via its staging buffer — and `ramfs_init()`
+top of RAM 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 512 KB (known issue 8).  The UI
+the high-memory legacy path supports up to 1 MB (known issue 8).  The UI
 font subsets live in the pack under `fonts/`.
 
 `rootfs.bin` is the floppy append (header + pack); `cmds.img` is the 8 MB
@@ -1158,7 +1161,7 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
   structure whose fields are read before being written must be cleared on
   allocation.  `clear_proc()` (`process.c`) exists for exactly this: a stale
   `procs[i].cr3` made the boot-time MMIO mapping (`paging_map_mmio_cr3`) fault
-  on the board while QEMU was fine.  Anything above `MM_MAX_PHYS` (128 MB) is
+  on the board while QEMU was fine.  Anything above `MM_MAX_PHYS` (512 MB) is
   also *unmapped*, so a wild pointer there is an instant #PF.
 - **All drawing goes into the current render target** (the shadow buffer by
   default; `vga_target()`, see the graphics bullet); the main loop flushes the
@@ -1203,12 +1206,12 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    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` 512 KB (not the old ~255 KB low-gap limit).  The browser and
+   `PACK_MAX` 1 MB (not the old ~255 KB low-gap limit).  The browser and
    its ring-3 dependencies are compiled with function sections and linked
-   with `--gc-sections`; the current pack with the UI fonts is ~343 KiB
-   (351472 bytes), leaving ~169 KiB under the cap.  Command growth
-   still needs care because `libdeflate-gzip -12` is already required (plain
-   `gzip -9` is over the cap).
+   with `--gc-sections`; the current pack with the UI fonts is ~344 KiB
+   (351760 bytes), leaving ~680 KiB under the cap.  `libdeflate-gzip -12` is
+   still preferred for the extra headroom, though the 1 MB cap leaves room
+   for `gzip -9` as a fallback.
 9. 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).
@@ -1256,11 +1259,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     `websrv` while `ping`/`curl`/`netrecv` owns the NIC fails; kill the other
     command first.
 20. `/share` is RAM only: a reboot/kexec starts empty, and the 16 MB cap is
-    charged against the 128 MB identity-mapped window.
+    charged against the 512 MB identity-mapped window.
 
 21. The TCP backlog's early-request buffers cost 8 KB of kernel BSS
     (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
-    pack is out of the kernel now, so `_end` keeps ~190 KB of headroom below
+    pack is out of the kernel now, so `_end` keeps ~170 KB of headroom below
     the `0x90000` wall; the remaining limit is kernel code/buffers only.
 
 22. Browser limits: no JavaScript, forms, pseudo-classes/elements,

+ 37 - 33
Makefile

@@ -56,7 +56,7 @@ all: floppy.img usb.img final.bin root.img
 boot.o: boot.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.o: kernel.c acpi.h cpu.h idle.h io.h lapic.h mouse.h panic.h process.h sched.h smp.h terminal.h timer.h serial.h tss.h usbhid.h version.h vga.h
+kernel.o: kernel.c acpi.h cpu.h idle.h io.h lapic.h mouse.h panic.h phys.h process.h sched.h smp.h terminal.h timer.h serial.h tss.h usbhid.h version.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 screen.o: screen.c screen.h
@@ -194,7 +194,7 @@ usbhid.o: usbhid.c usbhid.h io.h usb.h usb_hcd.h serial.h timer.h
 lsusb.o: lsusb.c io.h usb.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
-panic.o: panic.c panic.h bootinfo.h font_8x8.h process.h serial.h timer.h vga.h
+panic.o: panic.c panic.h bootinfo.h font_8x8.h phys.h process.h serial.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 bootcon.o: bootcon.c bootcon.h bootinfo.h font_8x8.h vga.h
@@ -314,13 +314,13 @@ ap_tramp_embed.o: ap_tramp.bin
 process.o: process.c process.h sched.h io.h kmalloc.h mm.h serial.h smp.h idle.h timer.h wm_abi.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-mm.o: mm.c mm.h
+mm.o: mm.c mm.h phys.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 kmalloc.o: kmalloc.c kmalloc.h mm.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-paging.o: paging.c paging.h mm.h mtrr.h vga.h serial.h
+paging.o: paging.c paging.h mm.h phys.h mtrr.h vga.h serial.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 elf.o: elf.c elf.h mm.h paging.h
@@ -469,7 +469,7 @@ efi/efi_entry.h: kernel.elf
 	if [ -z "$$entry" ]; then echo "efi: kernel_entry not found" >&2; exit 1; fi; \
 	printf '#define KERNEL_ENTRY 0x%s\n' "$$entry" > $@
 
-efi/main.o: efi/main.c efi/efi.h bootinfo.h vga.h efi/efi_entry.h
+efi/main.o: efi/main.c efi/efi.h bootinfo.h phys.h vga.h efi/efi_entry.h
 	$(EFI_CC) $(EFI_CFLAGS) -c -o $@ $<
 
 efi/payload.o: efi/payload.S kernel.bin cmds_pack_gz.bin efi/tramp.bin
@@ -515,11 +515,15 @@ uefi: usb-efi.img
 
 OVMF ?= $(firstword $(wildcard /usr/share/ovmf/x64/OVMF.4m.fd /usr/share/OVMF/OVMF.4m.fd))
 
+# Guest RAM for the QEMU targets.  The kernel ceiling is 512 MB (phys.h);
+# override with `make run QEMU_MEM=1G` for larger guests.
+QEMU_MEM ?= 512M
+
 # root.img is deliberately not attached: a running QEMU may hold its write
 # lock, and the UEFI boot test does not need a second filesystem.
 run-uefi: usb-efi.img
 	@test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
-	qemu-system-x86_64 -M pc -m 256 -smp 4 -bios $(OVMF) -snapshot \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -smp 4 -bios $(OVMF) -snapshot \
 	  -drive file=usb-efi.img,format=raw,if=ide \
 	  -device pci-ohci -device usb-mouse \
 	  -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
@@ -528,7 +532,7 @@ run-uefi: usb-efi.img
 test-uefi: usb-efi.img
 	@test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
 	@rm -f /tmp/opencode/nebos-uefi-serial.log
-	timeout 40 qemu-system-x86_64 -M pc -m 256 -smp 4 -bios $(OVMF) -snapshot \
+	timeout 40 qemu-system-x86_64 -M pc -m $(QEMU_MEM) -smp 4 -bios $(OVMF) -snapshot \
 	  -drive file=usb-efi.img,format=raw,if=ide \
 	  -device pci-ohci -device usb-mouse \
 	  -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
@@ -794,11 +798,11 @@ cmds.pack: $(addsuffix .elf,$(UCMDS)) hello.elf browser.elf gallery.elf \
 # The boot-time rootfs is gzip-compressed; the raw cmds.pack is still what
 # cmds-dev/net-cmds send for a runtime replacement.  Prefer libdeflate's
 # level 12 when it is installed: it saves about 8 KB over gzip -9 on the
-# current pack.  The high-memory rootfs loader supports up to 512 KB, which
-# leaves room for the font subsystem without touching the kernel image.
+# current pack.  The high-memory rootfs loader supports up to 1 MB; the
+# floppy image still has room for it after the kernel.
 CMDS_GZ     = $(shell command -v libdeflate-gzip >/dev/null 2>&1 && \
-		     echo "libdeflate-gzip -12 -n -c" || echo "gzip -9 -n -c")
-CMDS_GZ_MAX = 524288
+		     echo "libdeflate-gzip -12 -c" || echo "gzip -9 -n -c")
+CMDS_GZ_MAX = 1048576
 cmds_pack_gz.bin: cmds.pack
 	$(CMDS_GZ) $< > $@
 	@sz=$$(stat -c%s $@); \
@@ -853,8 +857,8 @@ run: image.bin root.img
 # Four logical CPUs (the Atom 330 topology).  UP is still the default so a
 # machine without ACPI/MADT boots exactly as before.
 run-smp: floppy.img root.img
-	qemu-system-i386 -M pc -smp 4 -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -smp 4 -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -smp 4 -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -smp 4 -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 floppy: floppy.img
 
@@ -870,34 +874,34 @@ flash: $(FLASH_IMG)
 	@./flash.sh "$(USB)" "$(FORCE)" "$(FLASH_IMG)"
 
 run-floppy: floppy.img root.img
-	qemu-system-i386 -M pc -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 run-usb: usb.img root.img
-	qemu-system-i386 -M pc -boot c -device pci-ohci -device usb-mouse -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -boot c -device pci-ohci -device usb-mouse -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -boot c -device pci-ohci -device usb-mouse -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -boot c -device pci-ohci -device usb-mouse -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
 
 # UHCI variant (machines with an Intel/VIA-style controller, and the
 # regression test for the UHCI backend of the USB core).
 run-uhci: floppy.img root.img
-	qemu-system-i386 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 # xHCI variant (Braswell/Intel PCH and other USB 3.0-only machines).
 run-xhci: floppy.img root.img
-	qemu-system-i386 -M pc -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 # EHCI variant (USB 2.0 high-speed root ports).  usb-storage is high-speed
 # and lands on EHCI; the mouse/keyboard are full-speed and use the UHCI
 # companion, exercising the multi-controller USB core.
 run-ehci: floppy.img root.img
-	qemu-system-i386 -M pc -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
 	  -device usb-storage,bus=ehci0.0,drive=usbstick \
 	  -drive if=none,id=usbstick,file=root.img,format=raw \
 	  -device usb-mouse,bus=uhci0.0 -device usb-kbd,bus=uhci0.0 \
 	  -drive file=floppy.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
 	  -device usb-storage,bus=ehci0.0,drive=usbstick \
 	  -drive if=none,id=usbstick,file=root.img,format=raw \
 	  -device usb-mouse,bus=uhci0.0 -device usb-kbd,bus=uhci0.0 \
@@ -909,29 +913,29 @@ run-ehci: floppy.img root.img
 # the only netdev.
 ASIX_HOST ?= vendorid=0x0b95,productid=0x7720
 run-ehci-asix: floppy.img root.img
-	qemu-system-i386 -M pc -nic none -device usb-ehci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -nic none -device usb-ehci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -nic none -device usb-ehci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -nic none -device usb-ehci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 run-xhci-asix: floppy.img root.img
-	qemu-system-i386 -M pc -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 # Network variant: a PCI NE2000 (RTL8029, the driver's QEMU match) on QEMU's
 # user-mode network (slirp).  DHCP/DNS come from slirp, the host is 10.0.2.2
 # (e.g. `python3 -m http.server` + `curl http://10.0.2.2:8000/` in a window).
 run-net: floppy.img root.img
-	qemu-system-i386 -M pc -device pci-ohci -device usb-mouse -device ne2k_pci,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -device pci-ohci -device usb-mouse -device ne2k_pci,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -device ne2k_pci,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -device ne2k_pci,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 # Intel e1000 variant: QEMU's 82540EM exercises the e1000e driver's MAC/DMA
 # path.  (The X201's 82577LM uses the same registers; only its PCH PHY is
 # different.)  usb-kbd is there for headless QMP `send-key` typing.
 run-e1000: floppy.img root.img
-	qemu-system-i386 -M pc -device pci-ohci -device usb-mouse -device usb-kbd \
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -device usb-kbd \
 	  -device e1000,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 \
 	  -drive file=floppy.img,format=raw,if=floppy \
 	  -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -device pci-ohci -device usb-mouse -device usb-kbd \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -device usb-kbd \
 	  -device e1000,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 \
 	  -drive file=floppy.img,format=raw,if=floppy \
 	  -drive file=root.img,format=raw,if=floppy
@@ -942,13 +946,13 @@ run-e1000: floppy.img root.img
 SATA_IMG ?= /tmp/opencode/nebos-sata.img
 run-ahci: floppy.img root.img
 	@test -f $(SATA_IMG) || truncate -s 512M $(SATA_IMG)
-	qemu-system-i386 -M pc -boot order=a -device pci-ohci -device usb-mouse \
+	qemu-system-i386 -M pc -m $(QEMU_MEM) -boot order=a -device pci-ohci -device usb-mouse \
 	  -drive file=floppy.img,format=raw,if=floppy \
 	  -drive file=root.img,format=raw,if=floppy \
 	  -device ich9-ahci,id=sata \
 	  -drive file=$(SATA_IMG),format=raw,if=none,id=satadisk \
 	  -device ide-hd,drive=satadisk,bus=sata.0 2>/dev/null || \
-	qemu-system-x86_64 -M pc -boot order=a -device pci-ohci -device usb-mouse \
+	qemu-system-x86_64 -M pc -m $(QEMU_MEM) -boot order=a -device pci-ohci -device usb-mouse \
 	  -drive file=floppy.img,format=raw,if=floppy \
 	  -drive file=root.img,format=raw,if=floppy \
 	  -device ich9-ahci,id=sata \

+ 3 - 3
docs/boot.html

@@ -169,7 +169,7 @@
 		</ol>
 		<p>
 			The pack no longer occupies the old <code>0x60000..0x9FBFF</code>
-			low gap, so the legacy cap is <code>PACK_MAX</code> (512 KB) and the
+			low gap, so the legacy cap is <code>PACK_MAX</code> (1 MB) and the
 			kernel BSS/boot stack may use the gap. The kernel image only has to
 			stay below <code>0x90000</code> so the top of low memory and the
 			EBDA remain untouched; <span class="file">kernel.ld</span> enforces
@@ -197,7 +197,7 @@
 			<li>Selects a GOP mode with BGRX or RGBX 32bpp, width at most 1280, <code>pitch × height</code> at most 8 MB, and a framebuffer below 4 GB.</li>
 			<li>Calls <code>GetMemoryMap</code> and converts the firmware map into the kernel's E820 format at <code>0x1000</code>.</li>
 			<li>Stages the kernel image below 4 GB and writes the boot-info and framebuffer blocks.</li>
-			<li>Calls <code>ExitBootServices</code>, copies the embedded rootfs pack to the top of RAM below 128 MB, and copies a 192-byte trampoline to <code>0x800</code>.</li>
+			<li>Calls <code>ExitBootServices</code>, copies the embedded rootfs pack to the top of RAM below 512 MB, and copies a 192-byte trampoline to <code>0x800</code>.</li>
 			<li>Leaves IA-32e mode (clears <code>CR0.PG</code>, <code>EFER.LME</code>, <code>CR4.PAE/PGE/PCIDE/SMEP/SMAP/LA57</code>), copies the kernel to <code>0x7E00</code>, and jumps to <code>kernel_entry</code>.</li>
 		</ol>
 		<p>
@@ -384,7 +384,7 @@
 		<h2>Failure modes to know</h2>
 		<ul>
 			<li><strong>No VBE mode.</strong> The legacy stage cannot produce a framebuffer; the kernel may start headless or the boot stops in the stage.</li>
-			<li><strong>Pack larger than the cap.</strong> The legacy and kexec paths share the 512 KB <code>PACK_MAX</code>; a larger pack needs the UEFI path or a bigger cap and loader change.</li>
+			<li><strong>Pack larger than the cap.</strong> The legacy BIOS path caps the compressed pack at 1 MB <code>PACK_MAX</code>; UEFI and kexec can carry larger packs up to the 8 MB ramfs cap.</li>
 			<li><strong>PIT not ticking.</strong> <code>kernel_main</code> prints <code>*** PIT NOT TICKING ***</code> and halts; no process scheduling is possible.</li>
 			<li><strong>UEFI Secure Boot.</strong> The unsigned <code>BOOTX64.EFI</code> is rejected before any NebOS code runs.</li>
 			<li><strong>Stale boot info.</strong> The checksum makes an invalid <code>0x500</code> block read as absent rather than as a bogus pack or RSDP.</li>

+ 6 - 0
docs/development.html

@@ -183,6 +183,12 @@
 				</tbody>
 			</table>
 		</div>
+		<p>
+			QEMU guests default to 512 MB, matching <code>MM_MAX_PHYS</code> in
+			<span class="file">phys.h</span>. Override the size with
+			<code>QEMU_MEM</code>, for example <code>make run QEMU_MEM=1G</code>;
+			all run and UEFI targets pass it through.
+		</p>
 	</section>
 
 	<section id="serial">

+ 4 - 4
docs/filesystems.html

@@ -236,7 +236,7 @@
 			<figcaption>
 				<strong>The pack is the root filesystem.</strong> The kernel no
 				longer embeds it, so command growth does not move <code>_end</code>.
-				The high-memory legacy loader caps the compressed pack at 512 KB;
+				The high-memory legacy loader caps the compressed pack at 1 MB;
 				the 8 MB rootfs partition and ramfs cap remain the larger limits.
 			</figcaption>
 		</figure>
@@ -333,7 +333,7 @@
 	<section id="capacity">
 		<h2>Capacity chart</h2>
 		<figure class="figure">
-			<div class="bars" role="img" aria-label="Capacity limits: /share 16 MB, ramfs decompressed pack 8 MB, USB rootfs image 8 MB, legacy compressed pack 512 KB. Bar widths are relative to 16 MB.">
+			<div class="bars" role="img" aria-label="Capacity limits: /share 16 MB, ramfs decompressed pack 8 MB, USB rootfs image 8 MB, legacy compressed pack 1 MB. Bar widths are relative to 16 MB.">
 				<div class="bar-row">
 					<span class="bar-label">/share</span>
 					<div class="bar-track"><span class="bar-fill green" style="--w:100%"></span></div>
@@ -351,8 +351,8 @@
 				</div>
 				<div class="bar-row">
 					<span class="bar-label">legacy pack</span>
-					<div class="bar-track"><span class="bar-fill" style="--w:3.125%"></span></div>
-					<span class="bar-value">512 KB</span>
+					<div class="bar-track"><span class="bar-fill" style="--w:6.25%"></span></div>
+					<span class="bar-value">1 MB</span>
 				</div>
 			</div>
 			<figcaption>

+ 1 - 1
docs/index.html

@@ -157,7 +157,7 @@
 			<li><strong>Freestanding C.</strong> The kernel has no libc, <code>malloc</code> or <code>printf</code>; ring-3 commands can link the in-tree <code>libc.a</code> (<code>malloc</code>, strings, stdlib). Formatting and allocation are in-tree either way.</li>
 			<li><strong>i586 baseline.</strong> Builds use <code>-march=i586 -mno-sse -mno-sse2 -mno-mmx</code>; SSE2 drawing paths are probed at runtime.</li>
 			<li><strong>Identity-mapped paging.</strong> Physical and virtual addresses are equal. There is no per-process address space or isolation.</li>
-			<li><strong>128 MB physical ceiling.</strong> The frame bitmap covers 128 MB; anything above that is unmapped.</li>
+			<li><strong>512 MB physical ceiling.</strong> The frame bitmap covers 512 MB; anything above that is unmapped.</li>
 			<li><strong>Hardcoded low memory.</strong> Boot info, the E820 map, the framebuffer block and the AP trampoline have fixed addresses that every boot path must honour.</li>
 			<li><strong>No firmware runtime.</strong> <code>reboot</code> and <code>poweroff</code> use legacy ports; UEFI runtime services are not called after <code>ExitBootServices</code>.</li>
 		</ul>

+ 17 - 16
docs/memory.html

@@ -41,7 +41,7 @@
 			window canvases, the ring-3 heap and the rootfs copy — is dynamic.
 		</p>
 		<div class="meta">
-			<span class="pill">128 MB physical ceiling</span>
+			<span class="pill">512 MB physical ceiling</span>
 			<span class="pill">4 KB frames</span>
 			<span class="pill">4 MB PSE pages</span>
 			<span class="pill">identity map</span>
@@ -76,28 +76,28 @@
 		<figure class="figure">
 			<svg viewBox="0 0 920 180" role="img" aria-labelledby="phys-title phys-desc">
 				<title id="phys-title">Physical memory composition</title>
-				<desc id="phys-desc">Of the 128 MB addressable window, the low 1 MB and an 8 MB VGA shadow reserve are unavailable, leaving about 119 MB for the frame allocator.</desc>
+				<desc id="phys-desc">Of the 512 MB addressable window, the low 1 MB and an 8 MB VGA shadow reserve are unavailable, leaving about 503 MB for the frame allocator.</desc>
 				<text class="svg-text" x="20" y="28">0x00000000</text>
-				<text class="svg-text" x="900" y="28" text-anchor="end">0x08000000 · 128 MB</text>
+				<text class="svg-text" x="900" y="28" text-anchor="end">0x20000000 · 512 MB</text>
 
-				<rect class="svg-box blue" x="40" y="44" width="6.5" height="34" rx="1"/>
-				<rect class="svg-box accent" x="46.5" y="44" width="52.5" height="34" rx="1"/>
-				<rect class="svg-box green" x="99" y="44" width="781" height="34" rx="2"/>
+				<rect class="svg-box blue" x="40" y="44" width="2" height="34" rx="1"/>
+				<rect class="svg-box accent" x="42" y="44" width="13.5" height="34" rx="1"/>
+				<rect class="svg-box green" x="55.5" y="44" width="844.5" height="34" rx="2"/>
 
 				<text class="svg-tiny" x="40" y="100">low 1 MB</text>
-				<text class="svg-tiny" x="105" y="100">VGA shadow · 8 MB</text>
-				<text class="svg-tiny" x="420" y="100" text-anchor="middle">frame allocator · ~119 MB</text>
+				<text class="svg-tiny" x="64" y="100">VGA shadow · 8 MB</text>
+				<text class="svg-tiny" x="477" y="100" text-anchor="middle">frame allocator · ~503 MB</text>
 
-				<line class="svg-line" x1="43" y1="78" x2="43" y2="88"/>
-				<line class="svg-line" x1="73" y1="78" x2="73" y2="88"/>
+				<line class="svg-line" x1="41" y1="78" x2="41" y2="88"/>
+				<line class="svg-line" x1="48" y1="78" x2="48" y2="88"/>
 				<line class="svg-line" x1="490" y1="78" x2="490" y2="88"/>
 
 				<text class="svg-tiny" x="40" y="130">boot structures · kernel image · BIOS/UMA · DMA buffers</text>
 				<text class="svg-tiny" x="40" y="150">The 8 MB reserve protects the shadow buffer; the allocator starts at 0x900000.</text>
 			</svg>
 			<figcaption>
-				<strong>128 MB is a hard ceiling.</strong> The frame bitmap covers
-				<code>[0, 0x08000000)</code>; addresses above it are not mapped by
+				<strong>512 MB is a hard ceiling.</strong> The frame bitmap covers
+				<code>[0, 0x20000000)</code>; addresses above it are not mapped by
 				the kernel page tables. The VGA shadow reserve is conservative: the
 				actual buffer is smaller, but the gap keeps a future resolution bump
 				from colliding with allocated pages.
@@ -180,7 +180,7 @@
 		<h2>Frame allocator</h2>
 		<p>
 			<span class="file">mm.c</span> keeps one bit per 4 KB page for the
-			whole 128 MB window. The bitmap is not IRQ-safe; callers save and
+			whole 512 MB window. The bitmap is not IRQ-safe; callers save and
 			disable interrupts around allocations. The allocator returns:
 		</p>
 		<ul>
@@ -190,7 +190,7 @@
 		</ul>
 		<p>
 			At boot, <code>mm_init</code> reserves the low 1 MB, the 8 MB VGA
-			shadow region, any LFB that happens to live below 128 MB, and the
+			shadow region, any LFB that happens to live below 512 MB, and the
 			boot-time rootfs pack. The pack reservation is released by
 			<code>ramfs_init</code> after it copies/inflates the pack into fresh
 			pages.
@@ -293,7 +293,7 @@
 	<section id="pack">
 		<h2>Rootfs pack lifetime</h2>
 		<ol>
-			<li>The boot path places the gzip pack in RAM: the legacy stage copies it directly to <code>0x900000</code> with <code>INT 15h AH=87h</code>, UEFI at the top of RAM below 128 MB, kexec in its staging buffer.</li>
+			<li>The boot path places the gzip pack in RAM: the legacy stage copies it directly to <code>0x900000</code> with <code>INT 15h AH=87h</code>, UEFI at the top of RAM below 512 MB, kexec in its staging buffer.</li>
 			<li><code>mm_init</code> reserves <code>[pack_addr, pack_addr + pack_size)</code> so the frame allocator cannot reuse it.</li>
 			<li><code>ramfs_init</code> takes a private page-allocated copy, validates the <code>NBROOT1</code> header and inflates the pack into it.</li>
 			<li><code>mm_free_range</code> releases the boot region when it is above the VGA shadow reserve. The private copy lives until <code>ramfs_replace</code> or reboot.</li>
@@ -308,7 +308,7 @@
 		<h2>Limits &amp; leaks</h2>
 		<ul>
 			<li><strong>No per-process address spaces.</strong> Paging is identity-mapped; there is no memory protection between processes.</li>
-			<li><strong>128 MB ceiling.</strong> Physical memory above <code>0x08000000</code> is unmapped. A wild pointer there is an instant <code>#PF</code>.</li>
+			<li><strong>512 MB ceiling.</strong> Physical memory above <code>0x20000000</code> is unmapped. A wild pointer there is an instant <code>#PF</code>.</li>
 			<li><strong>User page leak on reap.</strong> Killing a ring-3 process frees its kernel stack and page directory but not all user pages mapped into it; a few pages leak per command run.</li>
 			<li><strong>Kernel heap fragmentation.</strong> <code>kmalloc</code>'s power-of-two size classes make long-running mixed-size kernel allocation wasteful; ring-3 commands use the separate first-fit libc heap described above.</li>
 			<li><strong>No TLB shootdown.</strong> Cross-CPU mapping changes rely on not-present entries not being cached.</li>
@@ -319,6 +319,7 @@
 		<h2>Source map</h2>
 		<ul class="filelist">
 			<li><code>mm.c</code>, <code>mm.h</code> — E820 + bitmap allocator</li>
+			<li><code>phys.h</code> — shared 512 MB physical ceiling</li>
 			<li><code>kmalloc.c</code>, <code>kmalloc.h</code> — kernel heap</li>
 			<li><code>libc/malloc.c</code>, <code>libc/ucmd_mem.c</code> — ring-3 heap and <code>SYS_MEM_*</code> backend</li>
 			<li><code>libc/string.c</code>, <code>libc/stdlib.c</code> — ring-3 libc support</li>

+ 5 - 4
efi/main.c

@@ -13,10 +13,11 @@
 
 #include "efi.h"
 #include "bootinfo.h"
+#include "phys.h"
 #include "vga.h"
 #include "efi_entry.h"
 
-#define MAX_PHYS    0x08000000u          /* MM_MAX_PHYS in mm.h */
+#define MAX_PHYS    ((UINT32)MM_MAX_PHYS)
 #define PAGE_SIZE_  0x1000u
 #define MAX_PAGES   (MAX_PHYS / PAGE_SIZE_)
 #define E820_ADDR   0x1000u
@@ -43,7 +44,7 @@ extern unsigned char tramp_blob_end[];
  * generous and keeps the retry loop simple. */
 static UINT8 memmap_buf[65536] __attribute__((aligned(8)));
 
-/* One bit per 4 KB page over the kernel's 128 MB identity window.  Starts
+/* One bit per 4 KB page over the kernel's 512 MB identity window.  Starts
  * all-used; only EfiConventional/BootServices memory is freed.  EfiLoader*
  * (this image and the staging buffer) stays reserved, which is safe and
  * costs only a few hundred KB. */
@@ -398,7 +399,7 @@ static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
 	}
 
 	/* The scanout is MMIO, not RAM; make sure it can never be handed out
-	 * even when its BAR happens to sit below the 128 MB ceiling. */
+	 * even when its BAR happens to sit below the 512 MB ceiling. */
 	if (fb->valid)
 		use_range(fb->base, fb->size);
 
@@ -436,7 +437,7 @@ static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
 
 /* ---- boot structures --------------------------------------------------- */
 
-/* Copy the embedded rootfs pack to the top of RAM (below the 128 MB
+/* Copy the embedded rootfs pack to the top of RAM (below the 512 MB
  * identity window and above the 1..9 MB VGA shadow reserve), matching the
  * legacy stage's placement.  Returns the address, or 0 when it does not fit.
  * Runs after ExitBootServices, so only plain memory accesses. */

+ 7 - 1
mm.c

@@ -95,8 +95,14 @@ void mm_init(void)
 		for (i = 0; i < n; i++) {
 			const struct e820_entry *e = &map->entry[i];
 			if (e->type == E820_RAM && e->length != 0) {
+				unsigned long long end = e->base + e->length;
+
 				mark_free(e->base, e->length);
-				total_ram_kb += (unsigned int)(e->length >> 10);
+				if (end > MM_MAX_PHYS)
+					end = MM_MAX_PHYS;
+				if (end > e->base)
+					total_ram_kb +=
+						(unsigned int)((end - e->base) >> 10);
 			}
 		}
 	} else {

+ 4 - 3
mm.h

@@ -9,6 +9,8 @@
 #ifndef MM_H
 #define MM_H
 
+#include "phys.h"
+
 #define PAGE_SIZE        0x1000
 
 /* E820 map storage (written by vesa_stage.S in real mode). */
@@ -17,9 +19,8 @@
 #define E820_MAX_ENTRIES 32
 
 /* Frame allocator covers physical RAM [0, MM_MAX_PHYS).  Bitmap: 1 bit/page.
- * 128 MB matches this machine and QEMU's default; raise it (and move the
- * kernel upper bound, see kernel.ld) if larger machines are targeted. */
-#define MM_MAX_PHYS      (128u * 1024 * 1024)  /* 128 MB ceiling */
+ * The ceiling lives in phys.h so the UEFI loader cannot drift; see
+ * kernel.ld for the loaded-image bound. */
 
 /* Regions the kernel already owns (marked used at init): */
 #define MM_RESERVE_LOW     0x100000   /* 0 .. 1 MB: kernel/DMA/UHCI/UMA/boot */

+ 2 - 1
panic.c

@@ -3,6 +3,7 @@
 #include "panic.h"
 #include "bootinfo.h"
 #include "font_8x8.h"
+#include "phys.h"
 #include "process.h"
 #include "serial.h"
 #include "timer.h"
@@ -10,7 +11,7 @@
 
 #define LINE_H      10
 #define PANIC_LINES 12
-#define RAM_LIMIT   0x08000000u   /* MM_MAX_PHYS: identity-mapped RAM */
+#define RAM_LIMIT   ((unsigned int)MM_MAX_PHYS)
 #define KERNEL_BASE 0x7E00u       /* kernel load address (kernel.ld) */
 
 static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;

+ 9 - 0
phys.h

@@ -0,0 +1,9 @@
+/* Physical address ceiling shared by the 32-bit kernel and the UEFI loader. */
+#ifndef PHYS_H
+#define PHYS_H
+
+/* 512 MB: enough for MuJS plus the browser, while the 4 KB frame bitmap
+ * stays 16 KB and the 4 MB identity map needs only 128 PDEs. */
+#define MM_MAX_PHYS      (512u * 1024 * 1024)
+
+#endif

+ 1 - 1
vesa_stage.S

@@ -25,7 +25,7 @@
 
 /* Rootfs/boot-info constants (must match rootfs.h / bootinfo.h). */
 .equ ROOTFS_VERSION,   1
-.equ PACK_MAX,         0x80000       /* 512 KB compressed rootfs cap */
+.equ PACK_MAX,         0x100000      /* 1 MB compressed rootfs cap */
 .equ PACK_HIGH_ADDR,   0x900000      /* above the 8 MB VGA-shadow reserve */
 .equ BOOT_INFO_MAGIC,  0x4E425349
 .equ BOOT_INFO_VERSION, 2