1
0

27 Коммиты 828cba05ea ... 8373a22461

Автор SHA1 Сообщение Дата
  Johannes Zellner 8373a22461 fs: skip raw reads of a failed USB slot 5 дней назад
  Johannes Zellner 2ee880a4fc fs: keep raw block reads preemptible 5 дней назад
  Johannes Zellner 7f6dbd12ae wm: remap content canvases after the frame begins 5 дней назад
  Johannes Zellner 17fcbc512e usb: fix boot-time mass-storage stalls 5 дней назад
  Johannes Zellner 35d464536c browser: hover events and Bootstrap tooltips 5 дней назад
  Johannes Zellner 59bd6db11a browser: fix cloudron.io script errors 5 дней назад
  Johannes Zellner 4bc60ea887 browser: real getBoundingClientRect boxes for inline spans 5 дней назад
  Johannes Zellner 10f099b15c browser: web fonts with WOFF, weight/style variants and lazy loading 5 дней назад
  Johannes Zellner dd31c1cb3c browser: fix google.com's script errors and click navigation 5 дней назад
  Johannes Zellner c3dfecba91 wm: refine the start menu and bar buttons 6 дней назад
  Johannes Zellner b2fe5704ca membench: ring-3 speed check for CPU, memory and syscalls 6 дней назад
  Johannes Zellner e256ccbf31 cpu: initialise the x87 FPU on every CPU and switch it per process 6 дней назад
  Johannes Zellner 4e792643e5 wm: add a Windows-like start menu 6 дней назад
  Johannes Zellner 13e673edb3 sched: lock-free clock syscall and BKL contention statistics 6 дней назад
  Johannes Zellner 58236ab095 smp: enable the caches on the APs 6 дней назад
  Johannes Zellner 682513d8aa mm: do not hand out highmem that the MTRRs leave uncached 6 дней назад
  Johannes Zellner 03b547935b net: keep parallel downloads from overrunning a polled RX ring 6 дней назад
  Johannes Zellner 35c014d819 bootscan: tail the storage scan in a live window 6 дней назад
  Johannes Zellner e528274751 browser: fix flex layout and DOMPurify for the Cloudron login page 6 дней назад
  Johannes Zellner a218af1968 ehci: hand every non-high-speed device to the companion 6 дней назад
  Johannes Zellner 379fb20fd2 wm: add a Tasks launcher for the process monitor 6 дней назад
  Johannes Zellner c88469eec8 paging: map MMIO, firmware tables and the LFB through an ioremap window 6 дней назад
  Johannes Zellner d459322876 process: add a graphical task manager 6 дней назад
  Johannes Zellner a09b22571c chat: render link preview cards 6 дней назад
  Johannes Zellner bad87a48e8 mm: add a highmem zone and a kmap window for RAM up to 4 GB 6 дней назад
  Johannes Zellner a991e68498 wm: use Adwaita icons for the network and battery status 6 дней назад
  Johannes Zellner 69b271dc69 wm: make the resize border invisible and own the pointer during drags 6 дней назад
100 измененных файлов с 2702 добавлено и 661 удалено
  1. 465 136
      AGENTS.md
  2. 26 9
      Makefile
  3. 4 0
      assets/icons/adwaita/ac-adapter-symbolic.svg
  4. 1 0
      assets/icons/adwaita/battery-symbolic.svg
  5. 1 0
      assets/icons/adwaita/network-wired-disconnected-symbolic.svg
  6. 1 0
      assets/icons/adwaita/network-wired-symbolic.svg
  7. 1 0
      assets/icons/adwaita/network-wireless-offline-symbolic.svg
  8. 1 0
      assets/icons/adwaita/network-wireless-signal-excellent-symbolic.svg
  9. 1 0
      assets/icons/adwaita/network-wireless-signal-good-symbolic.svg
  10. 1 0
      assets/icons/adwaita/network-wireless-signal-none-symbolic.svg
  11. 1 0
      assets/icons/adwaita/network-wireless-signal-ok-symbolic.svg
  12. 1 0
      assets/icons/adwaita/network-wireless-signal-weak-symbolic.svg
  13. 1 0
      assets/icons/adwaita/phone-symbolic.svg
  14. 16 12
      boot/efi/main.c
  15. 7 7
      docs/boot.html
  16. 13 2
      docs/cpu.html
  17. 8 7
      docs/development.html
  18. 25 9
      docs/drivers.html
  19. 3 2
      docs/filesystems.html
  20. 78 27
      docs/graphics.html
  21. 2 2
      docs/index.html
  22. 39 25
      docs/memory.html
  23. 6 5
      docs/network.html
  24. 17 11
      docs/userland.html
  25. 2 1
      host/main.c
  26. 10 1
      host/window_headless.c
  27. 19 4
      include/icon_abi.h
  28. 25 6
      include/phys.h
  29. 29 2
      include/syscall.h
  30. 11 45
      include/wm_abi.h
  31. 25 36
      kernel/acpi.c
  32. 3 2
      kernel/ahci.c
  33. 9 0
      kernel/ap_tramp.S
  34. 14 6
      kernel/ati.c
  35. 9 4
      kernel/backlight.c
  36. 89 0
      kernel/bootcon.c
  37. 23 0
      kernel/bootcon.h
  38. 20 0
      kernel/cpu.c
  39. 2 0
      kernel/cpu.h
  40. 23 3
      kernel/dbg.c
  41. 1 1
      kernel/dhcp.c
  42. 2 2
      kernel/display.c
  43. 1 1
      kernel/dns.c
  44. 6 6
      kernel/e1000e.c
  45. 170 15
      kernel/ehci.c
  46. 12 5
      kernel/elf.c
  47. 6 4
      kernel/intel.c
  48. 3 2
      kernel/ioapic.c
  49. 5 4
      kernel/iwl_trans.c
  50. 20 9
      kernel/kernel.c
  51. 2 1
      kernel/lapic.c
  52. 126 24
      kernel/mm.c
  53. 15 3
      kernel/mm.h
  54. 39 0
      kernel/mtrr.c
  55. 4 0
      kernel/mtrr.h
  56. 3 3
      kernel/net.c
  57. 2 2
      kernel/netcore.c
  58. 39 0
      kernel/netdev.c
  59. 11 0
      kernel/netdev.h
  60. 4 4
      kernel/netkexec.c
  61. 1 1
      kernel/ntp.c
  62. 1 1
      kernel/nv50.c
  63. 23 13
      kernel/nvidia.c
  64. 4 1
      kernel/nvidia.h
  65. 4 3
      kernel/ohci.c
  66. 286 31
      kernel/paging.c
  67. 33 15
      kernel/paging.h
  68. 31 9
      kernel/peek.c
  69. 19 16
      kernel/process.c
  70. 8 6
      kernel/process.h
  71. 19 5
      kernel/r8168.c
  72. 5 5
      kernel/radeon.c
  73. 68 0
      kernel/sched.c
  74. 3 2
      kernel/sdhci.c
  75. 4 1
      kernel/serial.c
  76. 10 4
      kernel/smp.c
  77. 26 4
      kernel/sys_fs.c
  78. 3 0
      kernel/sys_info.c
  79. 4 2
      kernel/sys_mem.c
  80. 5 4
      kernel/sys_net.c
  81. 18 4
      kernel/sys_wm.c
  82. 1 0
      kernel/sys_wm.h
  83. 42 0
      kernel/syscall.c
  84. 30 3
      kernel/tcp.c
  85. 5 2
      kernel/touchpad.c
  86. 4 18
      kernel/tpec.c
  87. 1 0
      kernel/uhci.c
  88. 10 0
      kernel/usb.c
  89. 4 0
      kernel/usb.h
  90. 6 0
      kernel/usb_hcd.h
  91. 123 8
      kernel/usbmsc.c
  92. 15 0
      kernel/usbmsc.h
  93. 105 11
      kernel/vfs.c
  94. 8 2
      kernel/vfs.h
  95. 15 7
      kernel/vga.c
  96. 4 1
      kernel/vga.h
  97. 129 26
      kernel/wm.c
  98. 1 0
      kernel/wm.h
  99. 43 6
      kernel/xhci.c
  100. 78 0
      testdata/browser/compat/074-font-variants.html

Разница между файлами не показана из-за своего большого размера
+ 465 - 136
AGENTS.md


+ 26 - 9
Makefile

@@ -325,6 +325,10 @@ UFONT_OBJS = $(addprefix $(UL)/ufont/,ufont.o ufont_ttf.o ufont_draw.o ufont_fil
 $(UFONT_OBJS): OPT = -Os
 $(UFONT_OBJS): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
 $(UL)/ufont/ufont_ttf.o: OPT = -O2
+# WOFF 1.0 unwrapping needs inflate, so only the browser links it.
+UFONT_WOFF = $(UL)/ufont/ufont_woff.o
+$(UFONT_WOFF): OPT = -Os
+$(UFONT_WOFF): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
 
 # ngui: the userland immediate-mode GUI toolkit.  -Os: every GUI app links
 # the whole toolkit, and the rootfs pack has a cap.
@@ -379,14 +383,14 @@ $(UL)/wifi/wpa.o: XFLAGS = $(SECTION_CFLAGS)
 # single-file command needs nothing else; one that links libraries gets an
 # explicit .elf rule below.
 UCMDS = cpuinfo ls cat save rm mkdir rmdir \
-        meminfo mouseinfo storage top \
+        meminfo mouseinfo storage top process bootscan \
         clock colors edit wm term spin \
         netinfo netip netdns host dhcp \
         netsend netrecv ping pong netlog \
         netconsole netregs netloop netreset net100 netmac curl \
         files view sensors powerinfo mines solitaire \
         settime date gallery websrv browser chat \
-        wifi netmenu disks
+        wifi netmenu disks membench
 
 app = $(U)/apps/$(1)/ucmd_$(1)
 UCMD_ELFS = $(foreach a,$(UCMDS),$(call app,$(a)).elf)
@@ -409,6 +413,10 @@ $(call app,settime).o $(call app,solitaire).o $(U)/apps/gallery/gallery.o \
 $(U)/apps/hello/hello.o: XFLAGS =
 $(call app,settime).o $(call app,solitaire).o: OPT = -Os
 
+# membench times memcpy/memset over a libc heap buffer.
+$(call app,membench).elf: $(call app,membench).o $(UCMD_LD) $(LIBC_A)
+	$(ULINK)
+
 # websrv buffers its whole PUT/GET body on the libc heap.
 $(call app,websrv).elf: $(call app,websrv).o $(UCMD_LD) $(LIBC_A)
 	$(ULINK)
@@ -432,6 +440,15 @@ $(call app,term).elf: $(call app,term).o $(UWM) $(FONT16) $(UFONT_OBJS) $(UCMD_L
 $(call app,files).elf: $(call app,files).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
 	$(ULINK)
 
+# Task manager: process table with CPU, memory and network history graphs.
+$(call app,process).elf: $(call app,process).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
+	$(ULINK)
+
+# Boot scan window: live tail of the bootscan kernel thread's serial output.
+$(call app,bootscan).elf: $(call app,bootscan).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
+	$(ULINK)
+$(call app,bootscan).o: OPT = -Os
+
 $(call app,mines).elf: $(call app,mines).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
 	$(ULINK)
 
@@ -489,7 +506,7 @@ $(BROWSER_OBJS): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
 $(BR)/browser_html.o: XFLAGS = $(SECTION_CFLAGS)
 $(BR)/js_mu.o: XFLAGS = $(SECTION_CFLAGS) -Wno-unused-parameter $(LIBC_INC) -I$(QJS_DIR)
 
-browser.elf: $(BR)/browser.o $(BR)/browser_html.o $(BR)/svg_raster.o $(BR)/js_browser.o $(BR)/js_prelude.o $(UHTTP_OBJS) $(NGUI_APP) $(IMGLIB_OBJS) $(INFLATE) $(TLSLIB_OBJS) $(BR)/js_mu.o $(QJS_OBJS) $(UCMD_LD) $(LIBC_A)
+browser.elf: $(BR)/browser.o $(BR)/browser_html.o $(BR)/svg_raster.o $(BR)/js_browser.o $(BR)/js_prelude.o $(UHTTP_OBJS) $(NGUI_APP) $(UFONT_WOFF) $(IMGLIB_OBJS) $(INFLATE) $(TLSLIB_OBJS) $(BR)/js_mu.o $(QJS_OBJS) $(UCMD_LD) $(LIBC_A)
 	$(ULINK)
 
 # hello: the first ring-3 test program (its own linker script).
@@ -537,7 +554,7 @@ HOST_BROWSER_APP_SRCS = $(addprefix user/apps/browser/,browser.c \
                         $(HOST_UHTTP_SRCS) user/lib/ngui/ngui.c \
                         common/font_8x16.c \
                         $(addprefix user/lib/ufont/,ufont.c ufont_ttf.c \
-                        ufont_draw.c ufont_file.c) \
+                        ufont_draw.c ufont_file.c ufont_woff.c) \
                         $(HOST_IMGLIB_SRCS) common/inflate.c $(HOST_TLS_SRCS) \
                         user/apps/browser/js_mu.c user/lib/libc/malloc.c \
                         $(HOST_QUERY_SRCS)
@@ -580,15 +597,15 @@ host-curl: $(HOST_CURL_OBJS)
 
 # ---- Host tests (no kernel needed) -----------------------------------------
 
-UFONT_TEST_SRCS = $(addprefix user/lib/ufont/,ufont_test.c ufont.c ufont_ttf.c ufont_draw.c)
-ufont_test: $(UFONT_TEST_SRCS) user/lib/ufont/ufont.h user/lib/ufont/ufont_internal.h fonts/emoji.ttf
-	gcc -O2 -Wall -Wextra -Iinclude -o $@ $(UFONT_TEST_SRCS)
+UFONT_TEST_SRCS = $(addprefix user/lib/ufont/,ufont_test.c ufont.c ufont_ttf.c ufont_draw.c ufont_woff.c) common/inflate.c
+ufont_test: $(UFONT_TEST_SRCS) user/lib/ufont/ufont.h user/lib/ufont/ufont_internal.h fonts/emoji.ttf testdata/fonts/sans.woff
+	gcc -O2 -Wall -Wextra -Iinclude -Icommon -o $@ $(UFONT_TEST_SRCS)
 
 test-font: ufont_test
 	./ufont_test
 
-test-font-asan: $(UFONT_TEST_SRCS) user/lib/ufont/ufont.h user/lib/ufont/ufont_internal.h fonts/emoji.ttf
-	gcc -O1 -g -fsanitize=address,undefined -Iinclude -o ufont_test_asan $(UFONT_TEST_SRCS)
+test-font-asan: $(UFONT_TEST_SRCS) user/lib/ufont/ufont.h user/lib/ufont/ufont_internal.h fonts/emoji.ttf testdata/fonts/sans.woff
+	gcc -O1 -g -fsanitize=address,undefined -Iinclude -Icommon -o ufont_test_asan $(UFONT_TEST_SRCS)
 	./ufont_test_asan
 
 fonts:

+ 4 - 0
assets/icons/adwaita/ac-adapter-symbolic.svg

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg height="16px" viewBox="0 0 16 16" width="16px" xmlns="http://www.w3.org/2000/svg">
+    <path d="m 7 0 c -1 0 -1 1 -1 1 v 1 h -1 s -0.707031 -0.015625 -1.449219 0.355469 c -0.738281 0.371093 -1.550781 1.3125 -1.550781 2.644531 v 8 s -0.015625 0.707031 0.355469 1.449219 c 0.371093 0.738281 1.3125 1.550781 2.644531 1.550781 h 6 s 0.707031 0.015625 1.449219 -0.355469 c 0.738281 -0.371093 1.550781 -1.3125 1.550781 -2.644531 v -8 c 0 -1.332031 -0.8125 -2.273438 -1.550781 -2.644531 c -0.742188 -0.371094 -1.449219 -0.355469 -1.449219 -0.355469 h -1 v -1 c 0 -1 -1 -1 -1 -1 z m -2 4 h 6 c 0.554688 0 1 0.445312 1 1 v 8 c 0 0.554688 -0.445312 1 -1 1 h -6 c -0.554688 0 -1 -0.445312 -1 -1 v -8 c 0 -0.554688 0.445312 -1 1 -1 z m 3.875 0.992188 c -0.042969 0 -0.082031 0.003906 -0.125 0.007812 c -0.179688 0.050781 -0.34375 0.148438 -0.46875 0.28125 l -3.03125 3 c -0.234375 0.230469 -0.34375 0.582031 -0.28125 0.90625 v 0.8125 h 2.46875 l -1.1875 1.273438 c -0.210938 0.21875 -0.320312 0.539062 -0.28125 0.84375 v 0.09375 v 0.78125 h 1 c 0.265625 0.003906 0.53125 -0.097657 0.71875 -0.28125 l 3 -2.992188 c 0.1875 -0.191406 0.289062 -0.453125 0.28125 -0.71875 c 0.003906 -0.042969 0.003906 -0.082031 0 -0.125 v -0.875 h -2.5625 l 1.3125 -1.28125 c 0.214844 -0.21875 0.320312 -0.539062 0.28125 -0.84375 c 0 -0.03125 0 -0.0625 0 -0.09375 v -0.78125 h -0.9375 c -0.019531 0 -0.042969 0 -0.0625 0 c -0.042969 -0.003906 -0.082031 -0.007812 -0.125 -0.007812 z m 0 0" fill="#2e3434"/>
+</svg>

+ 1 - 0
assets/icons/adwaita/battery-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3434" d="M7 0C6 0 6 1 6 1v1H5s-.707031-.015625-1.449219.355469C2.8125 2.726562 2 3.667969 2 5v8s-.015625.707031.355469 1.449219C2.726562 15.1875 3.667969 16 5 16h6s.707031.015625 1.449219-.355469C13.1875 15.273438 14 14.332031 14 13V5c0-1.332031-.8125-2.273438-1.550781-2.644531C11.707031 1.984375 11 2 11 2h-1V1c0-1-1-1-1-1zM5 4h6c.554688 0 1 .445312 1 1v8c0 .554688-.445312 1-1 1H5c-.554688 0-1-.445312-1-1V5c0-.554688.445312-1 1-1m0 1v2h6V5zm0 3v2h6V8zm0 3v2h6v-2zm0 0"/></svg>

+ 1 - 0
assets/icons/adwaita/network-wired-disconnected-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><g fill="#2e3436"><path d="M15 10c.265625 0 .519531.105469.707031.292969.390625.390625.390625 1.023437 0 1.414062L14.414062 13l1.292969 1.292969c.390625.390625.390625 1.023437 0 1.414062s-1.023437.390625-1.414062 0L13 14.414062l-1.292969 1.292969c-.390625.390625-1.023437.390625-1.414062 0s-.390625-1.023437 0-1.414062L11.585938 13l-1.292969-1.292969c-.390625-.390625-.390625-1.023437 0-1.414062C10.480469 10.105469 10.734375 10 11 10s.519531.105469.707031.292969L13 11.585938l1.292969-1.292969C14.480469 10.105469 14.734375 10 15 10m0 0"/><path fill-opacity="0.34902" d="M6 0c-.554688 0-1 .445312-1 1v3c0 .554688.445312 1 1 1h1v2H0v2h2v2H1c-.554688 0-1 .445312-1 1v3c0 .554688.445312 1 1 1h4c.554688 0 1-.445312 1-1v-3c0-.554688-.445312-1-1-1H4V9h12V7H9V5h1c.554688 0 1-.445312 1-1V1c0-.554688-.445312-1-1-1zm0 0"/></g></svg>

+ 1 - 0
assets/icons/adwaita/network-wired-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" d="M6 .015625c-.554688 0-1 .445313-1 1v3c0 .554687.445312 1 1 1h1v2H0v2h2v2H1c-.554688 0-1 .445313-1 1v3c0 .554687.445312 1 1 1h4c.554688 0 1-.445313 1-1v-3c0-.554687-.445312-1-1-1H4v-2h8v2h-1c-.554688 0-1 .445313-1 1v3c0 .554687.445312 1 1 1h4c.554688 0 1-.445313 1-1v-3c0-.554687-.445312-1-1-1h-1v-2h2v-2H9v-2h1c.554688 0 1-.445313 1-1v-3c0-.554687-.445312-1-1-1zm0 0"/></svg>

+ 1 - 0
assets/icons/adwaita/network-wireless-offline-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><g fill="#2e3436"><path fill-opacity="0.34902" d="M8 1.992188c-2.617188 0-5.238281.933593-7.195312 2.808593l-.496094.480469c-.398438.378906-.410156 1.011719-.03125 1.410156.382812.398438 1.015625.410156 1.414062.03125l.5-.476562c3.085938-2.957032 8.53125-2.957032 11.617188 0l.5.476562c.398437.378906 1.03125.367188 1.414062-.03125.378906-.398437.367188-1.03125-.03125-1.410156l-.496094-.480469C13.238281 2.925781 10.617188 1.992188 8 1.992188M7.96875 6c-1.570312.011719-3.128906.628906-4.207031 1.8125l-.5.550781c-.375.40625-.347657 1.042969.0625 1.414063s1.042969.339844 1.414062-.070313l.5-.546875c1.242188-1.359375 3.992188-1.488281 5.398438-.125Q10.818359 9 11 9c.53125 0 1.039062.210938 1.414062.585938l.222657.222656c.011719-.011719.023437-.019532.035156-.03125.410156-.371094.441406-1.007813.066406-1.414063l-.5-.550781C11.113281 6.582031 9.535156 5.988281 7.96875 6M8 10c-.511719 0-1.023438.195312-1.414062.585938-.78125.78125-.78125 2.046874 0 2.828124s2.046874.78125 2.828124 0c.210938-.210937.359376-.453124.457032-.714843l-.285156-.285157c-.554688-.554687-.707032-1.367187-.46875-2.070312C8.78125 10.117188 8.390625 10 8 10m0 0"/><path d="M11 10c-.265625 0-.519531.105469-.707031.292969-.390625.390625-.390625 1.023437 0 1.414062L11.585938 13l-1.292969 1.292969c-.390625.390625-.390625 1.023437 0 1.414062s1.023437.390625 1.414062 0L13 14.414062l1.292969 1.292969c.390625.390625 1.023437.390625 1.414062 0s.390625-1.023437 0-1.414062L14.414062 13l1.292969-1.292969c.390625-.390625.390625-1.023437 0-1.414062C15.519531 10.105469 15.265625 10 15 10s-.519531.105469-.707031.292969L13 11.585938l-1.292969-1.292969C11.519531 10.105469 11.265625 10 11 10m0 0"/></g></svg>

+ 1 - 0
assets/icons/adwaita/network-wireless-signal-excellent-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" d="M8 1.992188c-2.617188 0-5.238281.933593-7.195312 2.808593l-.496094.480469c-.398438.378906-.410156 1.011719-.03125 1.410156.382812.398438 1.015625.410156 1.414062.03125l.5-.476562c3.085938-2.957032 8.53125-2.957032 11.617188 0l.5.476562c.398437.378906 1.03125.367188 1.414062-.03125.378906-.398437.367188-1.03125-.03125-1.410156l-.496094-.484375C13.238281 2.925781 10.617188 1.992188 8 1.992188M7.96875 6c-1.570312.011719-3.128906.628906-4.207031 1.8125l-.5.550781c-.179688.195313-.277344.453125-.261719.71875.011719.265625.128906.515625.328125.695313.195313.179687.453125.273437.71875.257812.265625-.011718.515625-.128906.695313-.328125l.496093-.546875c1.277344-1.402344 4.160157-1.496094 5.523438.003906l.5.542969c.175781.199219.425781.316407.691406.328125.265625.015625.523437-.078125.722656-.257812.195313-.179688.3125-.429688.324219-.695313.011719-.261719-.082031-.523437-.261719-.71875l-.5-.546875C11.117188 6.582031 9.535156 5.988281 7.96875 6M8 10c-.511719 0-1.023438.195312-1.414062.585938-.78125.78125-.78125 2.046874 0 2.828124s2.046874.78125 2.828124 0 .78125-2.046874 0-2.828124C9.023438 10.195312 8.511719 10 8 10m0 0"/></svg>

+ 1 - 0
assets/icons/adwaita/network-wireless-signal-good-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" d="M8 1.992188c-2.617188 0-5.238281.933593-7.195312 2.808593l-.496094.480469c-.398438.378906-.410156 1.011719-.03125 1.410156.382812.398438 1.015625.410156 1.414062.03125l.5-.476562c3.085938-2.957032 8.53125-2.957032 11.617188 0l.5.476562c.398437.378906 1.03125.367188 1.414062-.03125.378906-.398437.367188-1.03125-.03125-1.410156l-.496094-.484375C13.238281 2.925781 10.617188 1.992188 8 1.992188M7.96875 6c-1.570312.011719-3.128906.628906-4.207031 1.8125l-.5.550781c-.179688.195313-.277344.453125-.261719.71875.011719.265625.128906.515625.328125.695313.195313.179687.453125.273437.71875.257812.265625-.011718.515625-.128906.695313-.328125l.496093-.546875c1.277344-1.402344 4.160157-1.496094 5.523438.003906l.5.542969c.175781.199219.425781.316407.691406.328125.265625.015625.523437-.078125.722656-.257812.195313-.179688.3125-.429688.324219-.695313.011719-.261719-.082031-.523437-.261719-.71875l-.5-.546875C11.117188 6.582031 9.535156 5.988281 7.96875 6M8 10c-.511719 0-1.023438.195312-1.414062.585938-.78125.78125-.78125 2.046874 0 2.828124s2.046874.78125 2.828124 0 .78125-2.046874 0-2.828124C9.023438 10.195312 8.511719 10 8 10m0 0"/></svg>

+ 1 - 0
assets/icons/adwaita/network-wireless-signal-none-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" fill-opacity="0.34902" d="M8 1.992188c-2.617188 0-5.238281.933593-7.195312 2.808593l-.496094.480469c-.398438.378906-.410156 1.011719-.03125 1.410156.382812.398438 1.015625.410156 1.414062.03125l.5-.476562c3.085938-2.957032 8.53125-2.957032 11.617188 0l.5.476562c.398437.378906 1.03125.367188 1.414062-.03125.378906-.398437.367188-1.03125-.03125-1.410156l-.496094-.480469C13.238281 2.925781 10.617188 1.992188 8 1.992188M7.96875 6c-1.570312.011719-3.128906.628906-4.207031 1.8125l-.5.550781c-.375.40625-.347657 1.042969.0625 1.414063s1.042969.339844 1.414062-.070313l.5-.546875c1.277344-1.402344 4.160157-1.496094 5.523438 0l.5.546875c.371093.410157 1.003906.441407 1.414062.070313.40625-.371094.4375-1.007813.0625-1.414063l-.5-.550781C11.113281 6.582031 9.535156 5.988281 7.96875 6M8 10c-.511719 0-1.023438.195312-1.414062.585938-.78125.78125-.78125 2.046874 0 2.828124s2.046874.78125 2.828124 0 .78125-2.046874 0-2.828124C9.023438 10.195312 8.511719 10 8 10m0 0"/></svg>

+ 1 - 0
assets/icons/adwaita/network-wireless-signal-ok-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" d="M7.96875 6c-1.570312.011719-3.128906.628906-4.207031 1.8125l-.5.550781c-.375.40625-.347657 1.042969.066406 1.414063.40625.371094 1.039063.339844 1.414063-.070313l.496093-.546875c1.277344-1.402344 4.160157-1.496094 5.523438.003906l.5.542969c.371093.410157 1.003906.441407 1.414062.070313.40625-.371094.4375-1.007813.0625-1.414063l-.5-.546875C11.117188 6.582031 9.535156 5.988281 7.96875 6M8 10c-.511719 0-1.023438.195312-1.414062.585938-.78125.78125-.78125 2.046874 0 2.828124s2.046874.78125 2.828124 0 .78125-2.046874 0-2.828124C9.023438 10.195312 8.511719 10 8 10m0 0"/><path fill="#2e3434" fill-opacity="0.34902" d="M8 1.992188c-2.617188 0-5.238281.933593-7.195312 2.808593l-.496094.480469c-.398438.378906-.410156 1.011719-.03125 1.410156.382812.398438 1.015625.410156 1.414062.03125l.5-.476562c3.085938-2.957032 8.53125-2.957032 11.617188 0l.5.476562c.398437.378906 1.03125.367188 1.414062-.03125.378906-.398437.367188-1.03125-.03125-1.410156l-.496094-.484375C13.238281 2.925781 10.617188 1.992188 8 1.992188m0 0"/></svg>

+ 1 - 0
assets/icons/adwaita/network-wireless-signal-weak-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" d="M8 10c-.511719 0-1.023438.195312-1.414062.585938-.78125.78125-.78125 2.046874 0 2.828124s2.046874.78125 2.828124 0 .78125-2.046874 0-2.828124C9.023438 10.195312 8.511719 10 8 10m0 0"/><path fill="#2e3434" fill-opacity="0.34902" d="M8 1.992188c-2.617188 0-5.238281.933593-7.195312 2.808593l-.496094.480469c-.398438.378906-.410156 1.011719-.03125 1.410156.382812.398438 1.015625.410156 1.414062.03125l.5-.476562c3.085938-2.957032 8.53125-2.957032 11.617188 0l.5.476562c.398437.378906 1.03125.367188 1.414062-.03125.378906-.398437.367188-1.03125-.03125-1.410156l-.496094-.480469C13.238281 2.925781 10.617188 1.992188 8 1.992188M7.96875 6c-1.570312.011719-3.128906.628906-4.207031 1.8125l-.5.550781c-.375.40625-.347657 1.042969.0625 1.414063s1.042969.339844 1.414062-.070313l.5-.546875c1.277344-1.402344 4.160157-1.496094 5.523438 0l.5.546875c.371093.410157 1.003906.441407 1.414062.070313.40625-.371094.4375-1.007813.0625-1.414063l-.5-.550781C11.113281 6.582031 9.535156 5.988281 7.96875 6m0 0"/></svg>

+ 1 - 0
assets/icons/adwaita/phone-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" d="M5 0C3.355469 0 2 1.355469 2 3v10c0 1.644531 1.355469 3 3 3h6c1.644531 0 3-1.355469 3-3V3c0-1.644531-1.355469-3-3-3zm0 2h6c.570312 0 1 .429688 1 1v10c0 .554688-.445312 1-1 1H5c-.554688 0-1-.445312-1-1V3c0-.570312.429688-1 1-1m0 0"/><path fill="#222" fill-opacity="0.34902" d="M5.5 12c-.277344 0-.5.222656-.5.5s.222656.5.5.5h5c.277344 0 .5-.222656.5-.5s-.222656-.5-.5-.5zm0 0"/></svg>

+ 16 - 12
boot/efi/main.c

@@ -19,7 +19,11 @@
 
 #define MAX_PHYS    ((UINT32)MM_MAX_PHYS)
 #define PAGE_SIZE_  0x1000u
-#define MAX_PAGES   (MAX_PHYS / PAGE_SIZE_)
+/* The synthesized E820 map covers RAM up to 4 GB: the kernel identity-maps
+ * the first MAX_PHYS (lowmem) and uses the rest as highmem for ring-3
+ * pages.  The rootfs buffer must still be lowmem. */
+#define MAP_LIMIT   ((UINT64)MM_HIGH_LIMIT)
+#define MAP_PAGES   ((UINT32)(MAP_LIMIT / PAGE_SIZE_))
 #define E820_ADDR   0x1000u
 
 /* The kernel image is linked to live at 0x7E00.  Its file image must stay
@@ -44,11 +48,11 @@ 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 1 GB identity window.  Starts
+/* One bit per 4 KB page below 4 GB (128 KB).  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. */
-static UINT8 page_used[MAX_PAGES / 8];
+static UINT8 page_used[MAP_PAGES / 8];
 
 struct fb_saved {
 	int      valid;
@@ -343,11 +347,11 @@ static void free_range(UINT64 base, UINT64 len)
 	UINT64 end;
 	UINT32 p0, p1, p;
 
-	if (base >= MAX_PHYS)
+	if (base >= MAP_LIMIT)
 		return;
 	end = base + len;
-	if (end > MAX_PHYS)
-		end = MAX_PHYS;
+	if (end > MAP_LIMIT)
+		end = MAP_LIMIT;
 	p0 = (UINT32)((base + PAGE_SIZE_ - 1) / PAGE_SIZE_);
 	p1 = (UINT32)(end / PAGE_SIZE_);
 	for (p = p0; p < p1; p++)
@@ -360,11 +364,11 @@ static void use_range(UINT64 base, UINT64 len)
 	UINT64 end;
 	UINT32 p0, p1, p;
 
-	if (base >= MAX_PHYS)
+	if (base >= MAP_LIMIT)
 		return;
 	end = base + len;
-	if (end > MAX_PHYS)
-		end = MAX_PHYS;
+	if (end > MAP_LIMIT)
+		end = MAP_LIMIT;
 	p0 = (UINT32)(base / PAGE_SIZE_);
 	p1 = (UINT32)((end + PAGE_SIZE_ - 1) / PAGE_SIZE_);
 	for (p = p0; p < p1; p++)
@@ -399,11 +403,11 @@ 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 1 GB ceiling. */
+	 * even when its BAR happens to sit below 4 GB next to RAM. */
 	if (fb->valid)
 		use_range(fb->base, fb->size);
 
-	for (p = 0; p < MAX_PAGES; ) {
+	for (p = 0; p < MAP_PAGES; ) {
 		UINT32 start;
 
 		if (page_is_used(p)) {
@@ -411,7 +415,7 @@ static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
 			continue;
 		}
 		start = p;
-		while (p < MAX_PAGES && !page_is_used(p))
+		while (p < MAP_PAGES && !page_is_used(p))
 			p++;
 		if (count < 32) {
 			UINT64 len = (UINT64)(p - start) * PAGE_SIZE_;

+ 7 - 7
docs/boot.html

@@ -206,7 +206,7 @@
 		<ol>
 			<li>Selects a GOP mode with BGRX or RGBX 32bpp, width at most 1920, <code>pitch × height</code> at most 8 MB (1920×1080 fits at 8.29 MB), and a framebuffer below 4 GB. With 32bpp, that size limit also keeps a full-screen RGB565 canvas inside the 4 MB window slot; the VBE stage additionally checks 2 M pixels because a 24bpp 1920×1200 mode would otherwise pass the shadow check.</li>
 			<li>Calls <code>GetMemoryMap</code> and converts the firmware map into the kernel's E820 format at <code>0x1000</code>.</li>
-			<li>Locates the parent whole-disk Block I/O handle from the ESP device path, reads the P1 boot sector and P2 <code>NBROOT1</code> header, and stages the kernel below 4 GB and the gzip pack below the 1 GB identity window. The kernel/pack embedded in <code>BOOTX64.EFI</code> are a fallback only.</li>
+			<li>Locates the parent whole-disk Block I/O handle from the ESP device path, reads the P1 boot sector and P2 <code>NBROOT1</code> header, and stages the kernel below 4 GB and the gzip pack below the 1 GB identity window, and synthesizes the E820 map for all RAM below 4 GB. The kernel/pack embedded in <code>BOOTX64.EFI</code> are a fallback only.</li>
 			<li>Writes the boot-info and framebuffer blocks, calls <code>ExitBootServices</code>, then 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>
@@ -411,9 +411,9 @@
 			<li>Start APs through INIT-SIPI-SIPI (they idle until scheduling is enabled).</li>
 			<li>Initialise PS/2 (the I2C touchpad probe only on Airmont) and the VGA shadow buffer.</li>
 			<li>Inflate the command pack into <code>/ramfs</code> (table-driven whole-buffer inflate); start the WM and the first frame.</li>
-			<li>Publish <code>vfs_scan_busy</code> and call <code>wm_server_boot_ready()</code>; idle (1 s cap) until the first terminal has presented a frame.</li>
+			<li>Publish <code>vfs_scan_busy</code> and call <code>wm_server_boot_ready()</code>; the WM spawns the <code>bootscan</code> log window first and the first terminal after it, then idle (1 s cap) until the terminal presents.</li>
 			<li>Bring up USB (controllers, HID, USB NICs/WiFi/disks) non-preemptibly, as before the WM existed; probe the NIC, restore retained network config, start the kworker.</li>
-			<li>Start the <code>bootscan</code> kernel thread (storage probe + FAT mounts, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done); run DHCP if needed.</li>
+			<li>Start the <code>bootscan</code> kernel thread (non-USB storage probe + FAT mounts, then the deferred USB mounts after the scan window closes, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done); its COM1 output is captured into the <code>SYS_BOOTLOG</code> ring, which the <code>bootscan</code> window tails live. The deferred USB pass can wait seconds for a slow card reader without holding up the desktop, the scan window or the other volumes. Run DHCP if needed.</li>
 			<li>Enable scheduling on the APs and enter the kernel idle loop.</li>
 		</ol>
 		<p>
@@ -426,8 +426,8 @@
 			and the scan thread sleeps through its long probe timeouts.  The
 			end point of a boot measurement is the one-shot COM1 line
 			<code>wm: first window frame (term) at N ms</code> (uptime), written
-			by <span class="file">sys_wm.c</span> when the first application
-			window presents.
+			by <span class="file">sys_wm.c</span> when the first terminal
+			window presents (the bootscan window is skipped).
 		</p>
 		<p>
 			The boot trace lines written along the way (<code>boot_trace</code>,
@@ -444,7 +444,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 BIOS path caps the compressed pack at 2 MB <code>PACK_MAX</code> (<code>CMDS_GZ_MAX</code> in the Makefile); UEFI and kexec can carry larger packs up to the 8 MB ramfs cap. The WiFi firmware and the bundled install kernel make the full pack about 1.3 MB, and inflating it adds roughly half a second to the boot.</li>
+			<li><strong>Pack larger than the cap.</strong> The legacy BIOS path caps the compressed pack at 2 MB <code>PACK_MAX</code> (<code>CMDS_GZ_MAX</code> in the Makefile); UEFI and kexec can carry larger packs up to the 8 MB ramfs cap. The WiFi firmware, the bundled install kernel and the UI assets make the full pack about 1.98 MB, close to the cap, and inflating it adds roughly half a second to the boot.</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>
@@ -465,7 +465,7 @@
 			<li><code>kernel/kexec.c</code>, <code>kernel/kexec_tramp.S</code> — serial reload</li>
 			<li><code>kernel/netkexec.c</code> — network reload</li>
 			<li><code>kernel/kernel.c</code> — init order</li>
-			<li><code>kernel/bootcon.c</code>, <code>common/font_8x16.c</code> — direct-to-LFB boot console</li>
+			<li><code>kernel/bootcon.c</code>, <code>common/font_8x16.c</code> — direct-to-LFB boot console and the <code>bootscan</code> thread's serial ring (<code>SYS_BOOTLOG</code>)</li>
 			<li><code>kernel/install.c</code>, <code>kernel/install.h</code> — clone, self-update and net-install writer</li>
 			<li><code>include/rootfs.h</code> — installed image geometry</li>
 			<li><code>kernel/kernel.ld</code> — image limits</li>

+ 13 - 2
docs/cpu.html

@@ -162,6 +162,7 @@
 			<li>Allocate an AP idle PCB and kernel stack via <code>process_init_ap()</code>.</li>
 			<li>Fill the startup block at <code>0x6F00</code> with CR3, stack top, C entry and CPU index.</li>
 			<li>Send INIT, then SIPI-SIPI, with a second SIPI retry.</li>
+			<li>The trampoline clears <code>CR0.CD</code>/<code>CR0.NW</code>: INIT leaves an AP with caching disabled (<code>CR0 = 0x60000010</code>), and before this every ring-3 process on an AP ran about 100× slower on real hardware (QEMU does not model caches). The wake-up value is written back to <code>0x6F10</code> and logged as <code>smp: AP n start cr0 …</code>.</li>
 			<li>The AP enters <code>smp_ap_main</code>, initialises its LAPIC timer and idle loop, and waits.</li>
 			<li>After device/FS setup, <code>smp_enable_sched()</code> lets APs take user work.</li>
 		</ol>
@@ -169,7 +170,15 @@
 			Kernel threads stay pinned to the BSP; only ring-3 processes may run
 			on an AP. Each AP runs <code>cpu_enable_sse()</code> because it starts
 			with firmware <code>CR4</code>; without OSFXSR/OSXMMEXCPT the SSE2
-			drawing fast paths would <code>#UD</code>.
+			drawing fast paths would <code>#UD</code>. Every CPU also runs
+			<code>cpu_init_fpu()</code> (<code>CR0.NE</code>/<code>MP</code>,
+			<code>FNINIT</code>): an AP leaves INIT with <code>CR0.NE</code> clear
+			and an x87 control word of <code>0x0040</code>, so every inexact
+			floating-point result froze it until the next timer tick, and the
+			browser's JavaScript ran hundreds of times slower on an AP. Each
+			process owns its x87 state; <code>schedule()</code> saves it with
+			<code>FNSAVE</code> and restores it with <code>FRSTOR</code> on every
+			switch (108 bytes in <code>struct process</code>, no FXSR needed).
 		</p>
 		<div class="warn">
 			<p><strong>No IOAPIC driver.</strong> Device IRQs never reach an AP,
@@ -330,6 +339,8 @@
 			<li><strong>Not timer-preempted.</strong> A CPU running a BKL holder is skipped by the tick decision, so the owner can safely be in the middle of kernel data structures.</li>
 			<li><strong>Recursive.</strong> The owner may re-enter; the depth is restored across a block in the PCB's <code>bkl_saved</code> field.</li>
 			<li><strong>Sleep, not spin.</strong> A waiter blocks on the BKL wait queue and is woken when the owner releases.</li>
+			<li><strong>Lock-free clock.</strong> <code>SYS_GET_TICKS_MS</code> is answered before the lock is taken (one aligned counter read). It is the hottest syscall: QuickJS's interrupt check called it every ~10000 operations and slept on a contended lock each time, and the browser's interrupt hook now reads the clock only every 16th check.</li>
+			<li><strong>Contention statistics.</strong> <code>sched</code> prints how often a taker had to sleep, the total wait and the longest single hold with its holder, all since the previous <code>sched</code>.</li>
 			<li><strong>Idle uses trylock.</strong> The idle fallback never blocks on the BKL; kworker and kernel commands take it normally.</li>
 		</ul>
 		<div class="note">
@@ -355,7 +366,7 @@
 			<li><strong>Nehalem/Westmere P-states.</strong> <code>power_init</code> enables SpeedStep, raises the package C-state limit to C6 when unlocked, reads min/max ratios from <code>PLATFORM_INFO</code> and starts at the maximum non-turbo ratio. The target ratio goes in bits 15:8 of <code>IA32_PERF_CTL</code> with the low byte zero.</li>
 			<li><strong>Airmont/Braswell P-states.</strong> Family 6 model <code>0x4C</code> reads the Atom ratio/VID MSRs (<code>0x66A..0x66D</code>), <code>MSR_FSB_FREQ</code> and the FSB table. The N3060 reports pstates 6..20, BCLK 80 MHz (1600 MHz nominal), turbo pstate 31 (2480 MHz) and VID 0x2D..0x3C. <code>power_init</code> enters at max non-turbo and the governor drops to pstate 6 (480 MHz); <code>IA32_PERF_CTL</code> carries the pstate in 15:8 and the Linux-interpolated VID in 7:0. Turbo is detected but not requested. The probe deliberately avoids package/power-SKU MSRs; an unsupported <code>rdmsr</code> before the IDT triple-faults.</li>
 			<li><strong>Governor.</strong> A 100 ms window picks min/max/linear target pstates; short busy streaks jump to max immediately. Each CPU writes its own <code>IA32_PERF_CTL</code>; <code>cpufreq [auto|min|max|off|&lt;pstate&gt;]</code> overrides it, and <code>SYS_CPUFREQ</code> exposes the same auto/min/max/off mode to the control panel's Power mode tile.</li>
-			<li><strong>Telemetry.</strong> <code>power</code> prints effective MHz from APERF/MPERF, the current/requested pstate and, on Airmont, the FSB, nominal MHz, turbo pstate/VID, VID range and live <code>PERF_CTL</code>; <code>sensors</code> prints the CPU temperature, board sensors and both EC types; <code>powerinfo</code> prints the AC adapter and battery state, and the WM top bar shows the temperature plus a far-right AC plug or level-filled battery icon (percentage tooltip below it on hover) on ThinkPads and Chromebooks.</li>
+			<li><strong>Telemetry.</strong> <code>power</code> prints effective MHz from APERF/MPERF, the current/requested pstate and, on Airmont, the FSB, nominal MHz, turbo pstate/VID, VID range and live <code>PERF_CTL</code>; <code>sensors</code> prints the CPU temperature, board sensors and both EC types; <code>powerinfo</code> prints the AC adapter and battery state, and the WM top bar shows the temperature plus a far-right AC plug or level-filled battery icon on ThinkPads and Chromebooks.</li>
 			<li><strong>Thermal.</strong> On Intel, each CPU samples its DTS once a second from its scheduler tick; the hottest core is exposed to ring 3. TjMax comes from <code>IA32_TEMPERATURE_TARGET</code> on Nehalem/Westmere, the coretemp 90 °C table on Airmont, and the Bonnell table on older Atoms. On AMD family 15h models below <code>0x60</code> (Bulldozer/Piledriver/Steamroller), <span class="file">thermal.c</span> reads the package Tctl from <code>D18F3xA4</code> at PCI <code>0:18.3</code> (k10temp's <code>(raw × 125) m°C</code>, with the 49 °C range offset when bits 17:16 are both set) and publishes it to every CPU from the kworker under the BKL; PCI config space is deliberately not touched from the per-CPU timer interrupt.</li>
 			<li><strong>Limits.</strong> Package C-states/S0ix remain firmware-managed; Airmont reports no C3/C6 residency and does not request the turbo pstate. The explicit <code>idle c6</code> override is the Nehalem <code>0x20</code> hint; use <code>idle auto</code> for the Airmont C6N (<code>0x58</code>) path.</li>
 		</ul>

+ 8 - 7
docs/development.html

@@ -239,15 +239,16 @@
 					<tr><td><code>make test-chat</code></td><td>Host tests (ASan/UBSan) for the chat client's JSON parser, cookie jar and the OIDC login page/reply/config parsers.</td></tr>
 					<tr><td><code>make test-chat-e2e</code></td><td>Boots QEMU with an NE2000 on slirp, starts <code>tools/mock_chat.py</code> and drives <code>chat</code> through QMP: native login, send, live poll, read marker, older paging, browse/join, new DM, saved-session resume and sign-out.  <code>CHAT_E2E=--totp|--consent|--passkey|--wrong</code> runs a login variant.</td></tr>
 					<tr><td><code>make compat-serve</code></td><td>Serve the browser compatibility ladder from <code>testdata/browser/compat/</code> on port 8000 for the board or QEMU guest.</td></tr>
-					<tr><td><code>make browser-host</code></td><td>Build the Linux SDL3 development harness (<code>browser-host</code> and its <code>host-curl</code> helper). It implements the ring-3 <code>SYS_*</code> ABI with host files, BSD sockets, <code>fork/exec</code> and an SDL3 window, so pages render and accept input without QEMU. Run <code>./browser-host [--size WxH] [--root DIR] URL|FILE</code>; <code>--headless --wait 3000 --shot out.ppm</code> writes the canvas without a display, and <code>--keys 'clo{down}{enter}'</code> types into it (one key per 100 ms; <code>{enter}</code>, <code>{esc}</code>, <code>{tab}</code>, <code>{bs}</code>, <code>{del}</code>, arrows, <code>{ctrl-l}</code>, <code>{wait}</code>, and <code>{click:X,Y}</code> for a left click at canvas coordinates). Needs the 32-bit SDL3/Wayland libraries (<code>lib32-sdl3</code>, <code>lib32-wayland</code>, <code>lib32-libxkbcommon</code>).</td></tr>
+					<tr><td><code>make browser-host</code></td><td>Build the Linux SDL3 development harness (<code>browser-host</code> and its <code>host-curl</code> helper). It implements the ring-3 <code>SYS_*</code> ABI with host files, BSD sockets, <code>fork/exec</code> and an SDL3 window, so pages render and accept input without QEMU. Run <code>./browser-host [--size WxH] [--root DIR] URL|FILE</code>; <code>--headless --wait 3000 --shot out.ppm</code> writes the canvas without a display, and <code>--keys 'clo{down}{enter}'</code> types into it (one key per 100 ms; <code>{enter}</code>, <code>{esc}</code>, <code>{tab}</code>, <code>{bs}</code>, <code>{del}</code>, arrows, <code>{ctrl-l}</code>, <code>{wait}</code>, <code>{click:X,Y}</code> for a left click at canvas coordinates, and <code>{move:X,Y}</code> to only move the pointer, for hover). Needs the 32-bit SDL3/Wayland libraries (<code>lib32-sdl3</code>, <code>lib32-wayland</code>, <code>lib32-libxkbcommon</code>).</td></tr>
 					<tr><td><code>make fonts</code></td><td>Regenerate the checked-in Arimo/Liberation Mono subsets with <code>pyftsubset</code>.</td></tr>
 				</tbody>
 			</table>
 		</div>
 		<p>
-			QEMU guests default to 1 GB, 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=512M</code>;
+			QEMU guests default to 1 GB, the lowmem size (<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=3G</code> to
+			exercise highmem;
 			all run and UEFI targets pass it through.
 		</p>
 	</section>
@@ -356,14 +357,14 @@ make net-cmds                   # replace /ramfs only</code></pre>
 		<h2>Debugging</h2>
 		<ul>
 			<li><strong>Serial log.</strong> <code>make monitor</code> shows the boot trace and printf-style driver output. The <code>NebOS v… booted</code> line is written before the framebuffer is ready.</li>
-			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime).  On QEMU the USB image reaches it about 0.4 s after the kernel starts; USB, the NIC and the disks are probed after that (<code>[boot] usb/net/storage</code> lines).</li>
+			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime; the <code>bootscan</code> window presents just before it and is not counted).  On QEMU the USB image reaches it about 0.5 s after the kernel starts; USB, the NIC and the disks are probed after that (<code>[boot] usb/net/storage</code> lines).</li>
 			<li><strong>No serial port.</strong> The boot console is the early log. After the desktop, <code>bootlog</code> prints the captured text and <code>bootlog save /usb0p3/BOOTLOG.TXT</code> writes it to the live stick's FAT ESP (mount that partition on the host to read it). Once DHCP works, <code>make netmon</code> mirrors serial and terminal output to UDP 19290, so it becomes the serial console.</li>
 			<li><strong>Network console.</strong> <code>make netmon</code> listens to UDP 19290 for serial and terminal output; <code>netconsole host &lt;ip&gt;</code> narrows the target.</li>
 			<li><strong>dbg.</strong> The ring-0 serial monitor accepts <code>r addr len</code>, <code>w addr b0 …</code>, <code>i port</code>, <code>o port val</code>, <code>h</code> and <code>q</code>.</li>
 			<li><strong>Keys.</strong> <code>keytest</code> drains the raw scancode tap in <span class="file">io.c</span> and prints every byte with its E0-prefix state, make/break kind and decoded <code>IO_KEY_*</code>/ASCII value; Ctrl+C stops it.  It sees releases and keys the decoder ignores, so an unknown action-row key is easy to identify.</li>
 			<li><strong>Panic.</strong> A ring-0 fault dumps vector, error code, registers, CRs, EIP bytes and stack candidates to the LFB and COM1. Resolve addresses with <code>nm kernel.elf</code> / <code>addr2line -e kernel.elf</code>.</li>
 			<li><strong>Profiling.</strong> <code>gfxbench</code> times fill/copy/cell/canvas/flush; <code>top</code>, <code>sched</code>, <code>sensors</code>, <code>powerinfo</code>, <code>power</code> and <code>lspci</code>/<code>lsusb</code> expose live state.</li>
-			<li><strong>Memory.</strong> <code>peek addr len</code> hex-dumps identity-mapped memory (MMIO too); <code>poke addr val</code> writes one dword and prints the read-back.</li>
+			<li><strong>Memory.</strong> <code>peek addr len</code> hex-dumps physical memory, MMIO registers too (highmem through <code>kmap</code>, MMIO through its <code>ioremap()</code> mapping, created on demand); <code>poke addr val</code> writes one dword and prints the read-back.</li>
 		</ul>
 	</section>
 
@@ -421,7 +422,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 			<li><code>user/lib/libc/</code>, <code>libc.a</code> — optional ring-3 libc and its host test</li>
 			<li><code>third_party/quickjs/</code> (see its <code>VENDOR</code>), <code>user/apps/browser/js_mu.c/h</code>, <code>user/apps/browser/js_browser.c/h</code> — vendored JavaScript engine, its MuJS-style stack API and the browser bindings; <code>user/apps/browser/js_test.c</code> host tests</li>
 			<li><code>host/</code> — Linux SDL3 browser harness: <code>host/syscall.c</code> implements the <code>SYS_*</code> ABI, <code>host/window_sdl3.c</code> the Wayland window/input, <code>host/fs.c</code>/<code>host/net.c</code>/<code>host/proc.c</code> the host file, socket and <code>curl __serve</code> services, and <code>host/window_headless.c</code> the screenshot mode; <span class="file">ucmd_lib.h</span> selects the host prototypes with <code>UCMD_HOST</code></li>
-			<li><code>user/lib/ufont/ufont.c</code>, <code>user/lib/ufont/ufont_ttf.c</code>, <code>fonts/</code> — UI font engine and subsets; <code>user/lib/ufont/ufont_test.c</code> host test</li>
+			<li><code>user/lib/ufont/ufont.c</code>, <code>user/lib/ufont/ufont_ttf.c</code>, <code>fonts/</code> — UI font engine and subsets; <code>user/lib/ufont/ufont_test.c</code> host test (with the <code>testdata/fonts/sans.woff</code> round trip; <code>tools/mkwoff.py</code> rebuilds it)</li>
 			<li><code>kernel/kexec.c</code>, <code>tools/kexec_send.c</code> — serial reload</li>
 			<li><code>kernel/netkexec.c</code>, <code>tools/netkexec_send.c</code> — network reload</li>
 			<li><code>kernel/netlog.c</code>, <code>tools/netmonitor.c</code> — UDP console</li>

+ 25 - 9
docs/drivers.html

@@ -109,7 +109,10 @@
 			driven at address zero.
 		</p>
 		<p>
-			MMIO regions are mapped through <code>paging_map_mmio()</code>. PCI
+			MMIO regions are mapped through <code>ioremap()</code>, which returns
+			a kernel address in the shared window at <code>0xC0000000</code>
+			(see <a href="memory.html#paging">Memory → Paging</a>); drivers keep
+			the physical BAR only for logging. PCI
 			interrupts are disabled before touching MMIO, so every driver polls
 			its own status registers. <code>lspci</code> lists every function;
 			with a filter it prints BAR type, size and address details.
@@ -272,7 +275,9 @@
 			<figcaption>
 				<strong>Companions are not optional.</strong> EHCI is scanned first
 				so its port resets can hand non-high-speed devices back to a
-				companion. There is no transaction translator: a low/full-speed
+				companion: a K line state before the reset, or no port-enable
+				after it (whatever the line state reads), means the device is
+				not high-speed. There is no transaction translator: a low/full-speed
 				device behind a high-speed hub is invisible to EHCI.
 			</figcaption>
 		</figure>
@@ -285,7 +290,7 @@
 				<tbody>
 					<tr><td>UHCI</td><td>PCI I/O ports</td><td>1</td><td>Frame list at <code>0x3000</code>; control QH plus one QH per interrupt slot.</td></tr>
 					<tr><td>OHCI</td><td>PCI MMIO</td><td>up to 4</td><td>HCCA from the page allocator, static ED/TD sets in the uncacheable <code>ohci_dma_page</code>, TDs start at <code>CC = NotAccessed</code>. Before reset it requests SMM ownership (<code>HcCommandStatus.OCR</code>, clear <code>HcControl.IR</code>) and disables the AMD southbridge USB SMI on <code>1002:4385</code>.</td></tr>
-					<tr><td>EHCI</td><td>PCI MMIO</td><td>up to 4</td><td>Async QH/qTD schedule, page-allocated descriptors, polling only, no periodic list.</td></tr>
+					<tr><td>EHCI</td><td>PCI MMIO</td><td>up to 4</td><td>Async QH/qTD schedule, page-allocated descriptors, polling only, no periodic list. The async-advance doorbell (<code>IAAD</code>) is rung after an idle gap and before cancelling a stuck transfer; the MCP79 can otherwise keep a halted QH's old qTD cached and never fetch a USB disk's first read after enumeration.</td></tr>
 					<tr><td>xHCI</td><td>PCI MMIO</td><td>—</td><td>USB2 and USB3 root ports; direct SuperSpeed devices use a 512-byte EP0 and the port speed in the slot context (USB3 hubs are not decoded). Halts and waits for <code>HCH</code> around <code>HCRST</code>; EP0 max packet is the speed's fixed value (8/64/512), and a full-speed EP0 is corrected with Evaluate Context after the first descriptor read (the VIA VL805 on the 970A-DS3P babbled on a high-speed stick otherwise); a SuperSpeed port is not PED-disabled before its reset; failed control transfers are always logged with their completion code; EP0 stages are encoded as one TD per stage (EDK2 style) because chained control TDs stall on Braswell.</td></tr>
 				</tbody>
 			</table>
@@ -355,8 +360,20 @@
 			Bulk-Only Transport with LUN 0 and 512-byte blocks: READ(10),
 			WRITE(10), SYNCHRONIZE CACHE and a BOT reset/endpoint-halt recovery.
 			Short bulk-IN chunks are accumulated within a BOT data stage; a
-			failed READ(10) gets REQUEST SENSE, retries and a 1-sector
-			fallback. Present devices are registered as <code>usb0..</code>
+			READ(10) that completed with a non-zero CSW status gets REQUEST
+			SENSE, retries and a 1-sector fallback, while a transport timeout
+			goes straight to the reset.  A timed-out bulk-IN transfer is
+			cancelled through the HCD's <code>bulk_reset</code> (EHCI disarms
+			the qTD and clears the data toggle; xHCI stops the endpoint before
+			dropping its ring), and the BOT reset clears both host-side toggles
+			after the CLEAR_FEATURE requests to match the device's DATA0 state
+			— QEMU tolerates the mismatch, real EHCI hardware does not.  After
+			the first failed attempt the driver re-applies SET_CONFIGURATION
+			once, resetting the device's endpoints and toggles without a port
+			reset. The boot background scan sets <code>usbmsc_set_probe_only</code>:
+			one 5 s first read without the reset/retry chain, so a stuck device
+			cannot hold the boot's disk scan; a <code>storage</code> rescan uses
+			the full path. Present devices are registered as <code>usb0..</code>
 			(<code>BLK_USB</code>) by <code>blk_scan()</code>, so
 			<code>storage</code>, the VFS, <code>format</code> and
 			<code>install</code> can use them.
@@ -635,10 +652,9 @@
 			<code>power</code> prints ratios, effective MHz and C-state hints.
 			The WM top bar shows the CPU temperature and, on both ThinkPads
 			and Chromebooks, a status icon at the far right for the first
-			present battery: a plug while the AC adapter is online, a
-			level-filled battery on battery, with a percentage tooltip below
-			the icon on hover.  The tooltip is painted after the windows, so
-			it always stays on top.  Machines without an EC show neither.
+			present battery: a plug while the AC adapter is online and a
+			level-filled battery on battery.  Machines without an EC show
+			neither.
 		</p>
 	</section>
 

+ 3 - 2
docs/filesystems.html

@@ -166,7 +166,8 @@
 			<li><strong>Read/write.</strong> Read, write, delete, mkdir and rmdir are implemented; FAT32 allocation uses an FSInfo-seeded free-cluster hint.</li>
 			<li><strong>8.3 names.</strong> The on-disk name is the canonical one. The shell and <code>files</code> display the space-padded form; a long name cannot be created.</li>
 			<li><strong>Directory cache.</strong> The last FAT directory's first 64 entries are cached in <code>vfs.c</code>, so WebDAV's index sequence is one walk instead of N. Any write invalidates the cache.</li>
-			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops (including the SDHCI command and CMD1 power-up polls) call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process.  A USB disk read by the rescan claims the USB bus (<code>usb_bus_claim()</code>), so its host-controller waits yield too; <code>storage</code> no longer freezes the WM.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</li>
+			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops (including the SDHCI command and CMD1 power-up polls) call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process.  A USB disk read by the rescan claims the USB bus (<code>usb_bus_claim()</code>), so its host-controller waits yield too; <code>storage</code> no longer freezes the WM.  A raw-sector read (<code>SYS_BLK_READ</code>, the Disks partition view) takes a scan-owned but preemptible section: disk file operations wait, while the USB waits yield and a USB read is best-effort (one 5&nbsp;s attempt), so an unresponsive stick cannot freeze the WM. Once a USB slot has failed its probe, later raw reads return <code>EIO</code> immediately; a <code>storage</code> rescan clears the failure and retries.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</li>
+			<li><strong>Mount scan.</strong> <code>mount_device()</code> recognises the NebOS MBR layout (<code>mbr_is_nebos_layout()</code>: active type-0x06 P1 at LBA 63 of 1024 sectors, P2 at LBA 1088) and skips P1 without reading it, so the boot stick costs only its MBR read. A valid MBR whose partitions are all non-FAT is not probed as a superfloppy, and an unreadable partition boot sector is skipped rather than mounted as FAT. The boot scan calls <code>vfs_mount_all(1)</code>, which logs USB devices as <code>deferred</code>; <code>vfs_mount_usb()</code> appends them after the bootscan window has closed, so a card reader that NAKs its first read for seconds cannot delay the other mounts or the desktop. The background pass is best-effort: one 5 s first read without the reset/retry chain (<code>usbmsc_set_probe_only</code>). The <code>storage</code> command's rescan still does the full scan (<code>vfs_mount_all(0)</code>) with the normal 15 s first-read deadline and retries.</li>
 		</ul>
 		<p>
 			<span class="file">fatdetect.c</span> and <span class="file">fatformat.c</span>
@@ -325,7 +326,7 @@
 		<h2>Storage manager &amp; mount policies</h2>
 		<p>
 			<code>disks</code> (<span class="file">ucmd_disks.c</span>, also the
-			top-bar <em>Disks</em> launcher and the <em>Disks</em> button in
+			start menu's <em>Disks</em> entry and the <em>Disks</em> button in
 			<code>files</code>) is the ngui front end to
 			storage; <code>storage</code> stays the text view. It lists every
 			present block device (<code>SYS_BLKDEV</code>), reads sector 0 and

+ 78 - 27
docs/graphics.html

@@ -146,21 +146,34 @@
 		<h2>Window manager</h2>
 		<p>
 			The WM is a process that owns the desktop and a fixed array of eight
-			windows. It draws a system bar with the banner, launcher buttons,
+			windows. It draws a system bar with the NebOS start button, the
 			clock, the temperature, and, on ThinkPads and Chromebooks, an
-			AC/battery status icon at the far right (a plug on AC, a
-			level-filled battery on battery, with a percentage tooltip below
-			the bar on hover, painted above the windows), plus draggable
+			AC/battery status icon at the far right (the Adwaita
+			<code>ac-adapter</code> symbol on AC, and the <code>battery</code>
+			outline with our own level fill inside on battery), plus
+			draggable
 			terminal and canvas windows with a title bar and close button.
+			A normal window has no visible frame; an invisible resize band
+			(1 px inside, 3 px outside its outer edge) is the resize grab
+			area, and the active title bar uses the toolkit's highlight
+			colour.
 			Click-to-focus, drag-by-title-bar and close are routed through the
 			same pointer path. To the left of the rightmost AC/battery status
-			icon, a network status icon shows a
-			cable for wired Ethernet, signal bars for a connected WiFi link, a
-			USB mark for RNDIS tethering, or a red X when no link is up.
-			The whole right status area — temperature, date/time, network
-			and AC/battery items plus the gaps between them — is one button:
-			it opens the anchored <code>bin/netmenu</code> control panel (a
-			second click closes it).
+			icon, a network status icon shows the Adwaita
+			<code>network-wired</code> symbol for wired Ethernet, a
+			<code>network-wireless-signal-*</code> symbol for a connected WiFi
+			link, the <code>phone</code> symbol for RNDIS tethering, and
+			<code>network-wired-disconnected</code> in red when no NIC is
+			active (<code>network-wireless-offline</code> when a WiFi card is
+			present but not associated).
+			The whole right status cluster — temperature, date/time,
+			network and AC/battery items plus the gaps between them — is
+			one button with a single rounded hover pill; the empty bar
+			between the start button and the cluster is not clickable. Both
+			bar buttons use the same height and rounded style, stopping just
+			above the rainbow separator, and turn selection-blue while their
+			panel is open. It opens the anchored <code>bin/netmenu</code>
+			control panel (a second click closes it).
 			It opens collapsed: the battery/clock summary and Quick settings
 			pills are visible, while the network, power-mode and date/time
 			settings stay hidden until their pill is clicked.  Present NICs
@@ -168,6 +181,25 @@
 			joined, asking for the WPA2 passphrase first when the AP requires
 			one.
 		</p>
+		<p>
+			The NebOS banner in the top-left is the start button. Clicking it
+			opens a Windows-like start menu drawn by the WM as a flat overlay,
+			not a window, so it floats above every canvas without consuming a
+			window slot. The panel uses the control panel's theme: a flat
+			<code>theme.bg</code> surface with no rim, a dim
+			<code>Applications</code> caption as its first row, rounded hover
+			rows and a rounded Power tile. Every self-contained GUI
+			application in the pack gets a row with a coloured letter tile —
+			Terminal, Files, Web, Gallery, Chat, Clock, Text editor, Colors,
+			Mines, Cards, Disks, Tasks, Settings and Date &amp; Time — the
+			pointer highlights the hovered row, and an outside press dismisses
+			the panel. Row height shrinks to keep the whole list inside the
+			work area on short panels. The last row, Power, opens a submenu
+			with Reboot and Power off; both run the privileged kernel command
+			through <code>SYS_SPAWN_KCMD</code>. The bar notice and the
+			right-aligned status never draw left of
+			<code>WM_BAR_LEFT_END</code>.
+		</p>
 		<p>
 			A popover is a chromeless canvas window created with
 			<code>u_win_create_popover()</code> (<code>WIN_F_POPOVER</code>).
@@ -179,7 +211,7 @@
 			panel. The kernel uses the window origin for content-relative
 			pointer and scroll coordinates. An outside press dismisses the
 			popover and the click then continues normally; a second click on
-			any status target toggles it. The panel itself is drawn with the
+			the status cluster toggles it. The panel itself is drawn with the
 			ngui components: a battery/AC and clock summary, a brightness
 			slider when the panel PWM is present, and Quick settings tiles for
 			Network, the CPU power mode (<code>SYS_CPUFREQ</code>) and Date
@@ -277,7 +309,11 @@
 			counters, so the WM cannot lose one. <code>content_gen</code> comes
 			from a global counter and is unique per window creation, so a new
 			window that reuses a just-closed slot forces the WM to remap its
-			front canvas instead of reusing a stale mapping.
+			front canvas instead of reusing a stale mapping. The ring-3 WM
+			revalidates the generation after <code>u_wm_begin()</code>:
+			the kernel refuses client self-resizes while a frame is active,
+			so a resize between the pre-frame map refresh and the draw can no
+			longer unmap the canvas under the WM.
 		</p>
 		<p>
 			<code>SYS_WIN_CREATE</code> creates a generic canvas window.
@@ -330,8 +366,11 @@
 			which makes side-by-side placement exact. Windows can be dragged past the left, right and
 			bottom edges; <code>WM_DRAG_VISIBLE</code> (48 px) of the title bar
 			always stays on-screen for recovery, and the top bar is never
-			overlapped. The resize grip is clamped to the full work area and
-			snaps to it when dragged into the screen corner.
+			overlapped. An invisible resize band (1 px inside, 3 px outside)
+			runs along every edge and corner: the grabbed edges follow the
+			pointer while the opposite edges stay put, clamped to the work
+			area. While a drag or resize is active the WM owns the pointer,
+			so the window content does not see the held button.
 		</p>
 		<div class="note">
 			<p><strong>Buttons fire on the release edge.</strong> The toolkit keeps
@@ -356,8 +395,8 @@
 			clipped RGB565 primitives, buttons, menu items, text fields, the
 			Adwaita symbolic icons (see <a href="#icons">UI icons</a>), a theme palette, layout cut helpers and scrolling
 			panes. It is linked into <code>files</code>, <code>mines</code>,
-			<code>solitaire</code>, <code>chat</code>, the control panel and
-			the real <code>gallery.elf</code>.
+			<code>solitaire</code>, <code>process</code>, <code>chat</code>,
+			the control panel and the real <code>gallery.elf</code>.
 		</p>
 		<ul>
 			<li><strong>Text fields</strong> support drag-selection, double-click select-all, Ctrl+Left/Right word jumps, Ctrl+Delete/Backspace word deletion and Ctrl+A. They use the proportional face with measured caret, selection and horizontal scrolling; a field marked <code>disabled</code> ignores pointer and keyboard input, drops the focus and paints its value dimmed in the same proportional face. A field marked <code>secret</code> is measured and painted from a <code>*</code> mask (caret, selection and click positions follow the mask, Ctrl+Left/Right jump to the ends), and Tab returns <code>NGUI_INPUT_NEXT</code> together with <code>NGUI_INPUT_CANCEL</code> so forms can move the focus. Fields hold up to 511 characters (<code>NGUI_INPUT_MAX</code> 512). The terminal and editor keep the 8×16 cell grid.</li>
@@ -376,7 +415,13 @@
 			use real pixel sizes from 8 to 48 px; the browser maps CSS
 			<code>font-size</code>, <code>font-weight</code>,
 			<code>font-style</code> and <code>font-family: monospace</code> to
-			the faces. Missing fonts fall back to the 8×16 bitmap. The browser
+			the faces. Pages may bring their own faces with
+			<code>@font-face</code>: TrueType/OpenType (TrueType outlines) or
+			WOFF&nbsp;1.0, which <span class="file">ufont_woff.c</span>
+			unwraps by inflating each table; per family the browser keeps one
+			face per weight/style and picks the nearest one at layout. Its
+			arena is 3&nbsp;MB instead of the default 512&nbsp;KB so web fonts
+			fit beside the built-in set. Missing fonts fall back to the 8×16 bitmap. The browser
 			detaches the font arena before <code>heap_reset()</code> and
 			reattaches after <code>heap_start()</code>; terminal content and
 			the editor keep the bitmap cells.
@@ -400,17 +445,21 @@
 				<h3>files</h3>
 				<p>File browser on the VFS: opens text in <code>edit</code>, images in <code>view</code> and HTML in <code>browser</code>. The first ngui user.</p>
 			</div>
+			<div class="card">
+				<h3>process</h3>
+				<p>Graphical task manager (the start menu's <em>Tasks</em> entry or <code>process</code>, the ngui counterpart of <code>top</code>): 60 s CPU, physical-memory and NIC-throughput graphs fed by one sample per second, and a sortable process table (PID, state, CPU%, memory, CPU time, name). A selected row shows its details and <em>End task</em> terminates it after a second confirming click; kernel threads and the monitor itself are protected. Headers sort, PgUp/PgDn and the wheel scroll, Esc closes.</p>
+			</div>
 			<div class="card">
 				<h3>gallery</h3>
 				<p>Live showcase of every ngui control and primitive. Ships in the pack as <code>bin/gallery.app</code>; a launcher prefers <code>/share/GALLERY</code> for development.</p>
 			</div>
 			<div class="card">
 				<h3>chat</h3>
-				<p>Cloudron Chat client: native OIDC login, channel/DM sidebar with unread badges and presence dots, a wrapped timeline with avatars, day headers and reply snippets, a composer and browse/new-DM pickers. Media downloads use a warm <code>curl __serve</code> helper; message images are also decoded there (480&times;320 bounds), while avatars decode at 32&times;32 on the chat thread, so the window keeps drawing. Saved sessions resume from <code>/data</code>. The top-bar <em>Chat</em> button launches it.</p>
+				<p>Cloudron Chat client: native OIDC login, channel/DM sidebar with unread badges and presence dots, a wrapped timeline with avatars, day headers, reply snippets and link-preview cards (site, title, description and an optional thumbnail; clicking one opens the link in the browser), a composer and browse/new-DM pickers. Media downloads use a warm <code>curl __serve</code> helper; message images are also decoded there (480&times;320 bounds), while avatars decode at 32&times;32 on the chat thread, so the window keeps drawing. Saved sessions resume from <code>/data</code>. The start menu's <em>Chat</em> entry launches it.</p>
 			</div>
 			<div class="card">
 				<h3>disks</h3>
-				<p>Storage manager: devices, MBR partitions and mounts, per-volume mount name, read-only and automount, and Format / Install NebOS dialogs that show the kernel command's output. The top-bar <em>Disks</em> button and the <em>Disks</em> button in <code>files</code> launch it.</p>
+				<p>Storage manager: devices, MBR partitions and mounts, per-volume mount name, read-only and automount, and Format / Install NebOS dialogs that show the kernel command's output. The start menu's <em>Disks</em> entry and the <em>Disks</em> button in <code>files</code> launch it.</p>
 			</div>
 			<div class="card">
 				<h3>solitaire</h3>
@@ -454,7 +503,8 @@
 		<p>
 			ngui's icons (<code>ngui_icon()</code>, <code>ngui_icon_button()</code>:
 			back/forward, reload and home in the browser toolbar, folder/file/drive
-			in <code>files</code>) are GNOME Adwaita symbolic icons
+			in <code>files</code>) and the WM's top-bar network/battery
+			indicators are GNOME Adwaita symbolic icons
 			(adwaita-icon-theme 51, LGPL-3 or CC-BY-SA-3.0).  The unchanged
 			<code>*-symbolic.svg</code> files live in
 			<span class="file">assets/icons/adwaita/</span>; the host tool
@@ -463,13 +513,13 @@
 			8-bit coverage mask and writes one <code>NBICO1</code> bundle,
 			<code>/ramfs/icons/adwaita16.icn</code> (layout in
 			<span class="file">include/icon_abi.h</span>): a 16-byte header and
-			13 masks, ~3.3&nbsp;KB raw and ~0.8&nbsp;KB gzip, so both the USB
+			24 masks, ~6.2&nbsp;KB raw and ~1.3&nbsp;KB gzip, so both the USB
 			and the floppy pack carry it.
 		</p>
 		<ul>
-			<li><strong>Icon list.</strong> <code>ICON_LIST</code> is an X-macro of enum suffix, short name and theme file.  Its order is the icon id and the bundle order (append only); it generates <code>enum ngui_icon</code> and <code>ngui_icon_name()</code>, and the gallery shows every entry.</li>
-			<li><strong>Tinting.</strong> Symbolic icons are one colour, so only coverage is stored.  <code>ngui_icon()</code> blends the mask in the caller's colour (the theme text colour on buttons, <code>icon_folder</code>/<code>icon_disk</code>/<code>icon_file</code> in <code>files</code>), centred and clipped.</li>
-			<li><strong>Loading.</strong> Each process reads the bundle once into a BSS buffer, so the browser's <code>heap_reset()</code> cannot free it.  The read is capped at the ids the app was built with, so an older app still works with a newer bundle.  Without the bundle the icon is left blank; there is no line-drawn fallback, because it cost ~2&nbsp;KB of code in each app.</li>
+			<li><strong>Icon list.</strong> <code>ICON_LIST</code> is an X-macro of enum suffix, short name and theme file.  Its order is the icon id and the bundle order (append only); it generates <code>enum ngui_icon</code> and <code>ngui_icon_name()</code>, and the gallery shows every entry.  <code>uwm_lib.h</code> applies the same list to <code>enum uwm_icon</code>, so the ring-3 WM can use ids without linking ngui.</li>
+			<li><strong>Tinting.</strong> Symbolic icons are one colour, so only coverage is stored.  <code>ngui_icon()</code> blends the mask in the caller's colour (the theme text colour on buttons, <code>icon_folder</code>/<code>icon_disk</code>/<code>icon_file</code> in <code>files</code>), centred and clipped.  <code>usurf_icon()</code> does the same over the WM's 16/24/32 bpp shadow.  The bar's battery clears the striped <code>battery</code> mask's interior to a quarter-brightness shade and draws its level fill over the lower part, so the gauge reads at 16&nbsp;px.</li>
+			<li><strong>Loading.</strong> Each process (every ngui app and the WM) reads the bundle once into a BSS buffer, so the browser's <code>heap_reset()</code> cannot free it.  The read is capped at the ids the process was built with, so an older app still works with a newer bundle.  Without the bundle the icon is left blank; there is no line-drawn fallback, because it cost ~2&nbsp;KB of code in each app.</li>
 			<li><strong>Strict converter.</strong> mkicons reads only what symbolic icons use (<code>&lt;svg viewBox&gt;</code>, <code>&lt;g&gt;</code>, <code>&lt;path&gt;</code>, <code>&lt;rect&gt;</code>, <code>&lt;circle&gt;</code>, <code>&lt;ellipse&gt;</code>, fill, fill-rule, opacity) and fails the build on transforms, strokes, <code>style</code> or unknown elements instead of drawing a wrong icon.  <code>./mkicons assets/icons/adwaita 16 out.icn -v</code> prints an ASCII preview of every mask.</li>
 		</ul>
 	</section>
@@ -494,7 +544,7 @@
 		</p>
 		<ul>
 			<li><strong>Loading.</strong> <span class="file">cursor.c</span> copies the bundle out of the ramfs into its own pages after <code>ramfs_init()</code> and after every <code>ramfs_replace()</code> (the old pack is freed), then tells <span class="file">mouse.c</span> to re-fetch its image.</li>
-			<li><strong>Selection.</strong> On every <code>SYS_WM_POLL</code> the window server resolves the shape: the WM's override (<code>SYS_WM_SET_CURSOR</code>: resize arrows on the grip and while resizing, the move cross during a title-bar drag, a hand over launchers and the status area), else the cursor of the window holding the pointer capture, else the content cursor (<code>SYS_WIN_SET_CURSOR</code>) of the window under the pointer, else the arrow.</li>
+			<li><strong>Selection.</strong> On every <code>SYS_WM_POLL</code> the window server resolves the shape: the WM's override (<code>SYS_WM_SET_CURSOR</code>: resize arrows on the resize band and while resizing, the move cross during a title-bar drag, a hand over the start button, the start menu and the status cluster), else the cursor of the window holding the pointer capture, else the content cursor (<code>SYS_WIN_SET_CURSOR</code>) of the window under the pointer, else the arrow.</li>
 			<li><strong>Clients.</strong> ngui resets the cursor to the arrow for each redrawn frame, a hovered text field asks for the I-beam, <code>ngui_cursor_rect()</code>/<code>ngui_set_cursor()</code> serve apps, and <code>ngui_end_ex()</code> sends it when it changed.  The terminal uses the I-beam; the browser maps the inherited CSS <code>cursor</code> property and, for <code>auto</code>, a hand over links, an I-beam over text fields and the progress arrow while a page loads.  The gallery's Cursors section shows every shape.</li>
 			<li><strong>Drawing.</strong> <code>cur_x</code>/<code>cur_y</code> stay the hotspot; the image's top-left is the hotspot minus the image's hotspot and may be off-screen.  The software cursor saves up to 32&times;32 pixels under it and blends the premultiplied pixels over them.  Intel Ironlake and Radeon copy the ARGB image into their 64&times;64 planes (Intel uses CURPOS sign bits for negative positions and follows the matching primary plane's gamma enable, because the display palette is after the plane blender and a cursor-only gamma pass brightens its antialiased edges on a VBE mode whose primary bypasses the palette; Radeon uses <code>HORZ_VERT_OFF</code> plus a shifted <code>CUR_OFFSET</code>); Cirrus reduces it to its two colours and clamps the position at 0.  Animated frames advance from <code>mouse_poll_update()</code>.</li>
 		</ul>
@@ -546,8 +596,9 @@
 			<li><code>user/apps/browser/browser.c</code>, <code>user/apps/browser/browser_html.c</code> — HTML/CSS renderer and inline SVG</li>
 			<li><code>user/lib/ngui/ngui.c</code>, <code>user/lib/ngui/ngui.h</code> — toolkit</li>
 			<li><code>user/lib/ufont/ufont.c</code>, <code>user/lib/ufont/ufont_ttf.c</code>, <code>user/lib/ufont/ufont_draw.c</code> — fixed-point TrueType text</li>
+			<li><code>user/lib/ufont/ufont_woff.c</code> — WOFF 1.0 to sfnt (browser only)</li>
 			<li><code>fonts/*.ttf</code> — Arimo / Liberation Mono / Noto Emoji subsets in the pack</li>
-			<li><code>user/apps/files/ucmd_files.c</code>, <code>user/apps/gallery/gallery.c</code>, <code>user/apps/solitaire/ucmd_solitaire.c</code>, <code>user/apps/chat/ucmd_chat.c</code></li>
+			<li><code>user/apps/files/ucmd_files.c</code>, <code>user/apps/process/ucmd_process.c</code>, <code>user/apps/gallery/gallery.c</code>, <code>user/apps/solitaire/ucmd_solitaire.c</code>, <code>user/apps/chat/ucmd_chat.c</code></li>
 			<li><code>user/lib/img/img*.c</code> — image codecs</li>
 			<li><code>kernel/ati.c</code>, <code>kernel/nvidia.c</code>, <code>kernel/intel.c</code>, <code>kernel/nv50.c</code></li>
 			<li><code>kernel/backlight.c</code>, <code>kernel/backlight.h</code> — Cherryview/VLV panel PWM</li>

+ 2 - 2
docs/index.html

@@ -78,7 +78,7 @@
 
 				<rect class="svg-box accent" x="100" y="8" width="720" height="72" rx="9"/>
 				<text class="svg-text" x="124" y="35">Ring 3 · commands &amp; GUI apps</text>
-				<text class="svg-sub" x="124" y="55">ls · cat · edit · view · files · browser · chat · gallery · solitaire</text>
+				<text class="svg-sub" x="124" y="55">ls · cat · edit · view · files · process · bootscan · browser · chat · gallery · solitaire</text>
 				<text class="svg-tiny" x="796" y="35" text-anchor="end">0x40000000</text>
 
 				<path class="svg-line" d="M460 80v22"/>
@@ -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>1 GB physical ceiling.</strong> The identity map ends where ring-3 space starts (<code>0x40000000</code>); RAM above it is ignored.</li>
+			<li><strong>1 GB lowmem, 4 GB highmem.</strong> The identity map ends where ring-3 space starts (<code>0x40000000</code>); RAM up to 4 GB backs ring-3 pages through per-CPU <code>kmap</code> slots.</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>

+ 39 - 25
docs/memory.html

@@ -4,7 +4,7 @@
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>Memory | NebOS Architecture</title>
-<meta name="description" content="NebOS memory architecture: fixed low-memory addresses, E820 parsing, bitmap frame allocator, identity paging, MTRR write-combining, user arenas and boot-pack lifetime.">
+<meta name="description" content="NebOS memory architecture: fixed low-memory addresses, E820 parsing, bitmap frame allocator with lowmem/highmem zones, identity paging, kmap, MTRR write-combining, user arenas and boot-pack lifetime.">
 <link rel="icon" href="favicon.svg" type="image/svg+xml">
 <link rel="stylesheet" href="style.css">
 </head>
@@ -35,13 +35,15 @@
 		<p class="kicker">Memory &amp; paging</p>
 		<h1>A fixed ladder and a 4 KB frame allocator.</h1>
 		<p class="lede">
-			NebOS identity-maps physical RAM with 4 MB pages and hands out
-			4 KB frames from a bitmap. A small set of low-memory addresses is
+			NebOS identity-maps the first 1 GB of RAM (lowmem) with 4 MB pages
+			and hands out 4 KB frames from a bitmap; RAM from 1 GB up to 4 GB
+			(highmem) backs ring-3 pages and is reached by the kernel through
+			per-CPU <code>kmap</code> slots. A small set of low-memory addresses is
 			fixed by the boot path and the ABI; everything else — process stacks,
 			window canvases, the ring-3 heap and the rootfs copy — is dynamic.
 		</p>
 		<div class="meta">
-			<span class="pill">1 GB physical ceiling</span>
+			<span class="pill">1 GB lowmem · 4 GB highmem</span>
 			<span class="pill">4 KB frames</span>
 			<span class="pill">4 MB PSE pages</span>
 			<span class="pill">identity map</span>
@@ -96,12 +98,13 @@
 				<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>1 GB is a hard ceiling</strong> (<code>MM_MAX_PHYS</code>,
+				<strong>Lowmem ends at 1 GB</strong> (<code>MM_MAX_PHYS</code>,
 				<code>include/phys.h</code>). The identity map covers
 				<code>[0, 0x40000000)</code> and ring-3 space starts at
-				<code>USER_BASE</code> = <code>0x40000000</code>, so RAM above 1 GB is
-				ignored until a highmem zone exists; <code>paging.c</code>
-				statically asserts the two never overlap. The VGA shadow reserve is conservative: the
+				<code>USER_BASE</code> = <code>0x40000000</code>; <code>paging.c</code>
+				statically asserts the two never overlap. RAM above 1 GB (up to
+				<code>MM_HIGH_LIMIT</code>, 4 GB) is highmem: tracked by the same
+				bitmap, never identity-mapped, used only for ring-3 pages. Highmem that the firmware's variable MTRRs leave non-write-back (a BIOS short of MTRRs on a 3–4 GB board) is never handed out: <code>mm_init</code> asks <code>mtrr_mem_type()</code> for every MB of free RAM, withholds such highmem, logs <code>mm: RAM not write-back by MTRR</code>, and <code>meminfo</code> shows the counts. Ring-3 code whose heap sits in uncached RAM runs many times slower (a browser's JavaScript heap, for example). The VGA shadow reserve is conservative: the
 				actual buffer is smaller, but the gap keeps a future resolution bump
 				from colliding with allocated pages.
 			</figcaption>
@@ -138,10 +141,12 @@
 					<tr><td><code>0x900000</code></td><td>Boot pack (written directly by the legacy stage), then the frame bitmap right after it; dynamic stacks and kexec staging</td><td>The pack is freed after <code>ramfs_init</code> takes its copy; the bitmap stays</td></tr>
 					<tr><td><code>0x100000</code></td><td>VGA shadow buffer base</td><td><code>VGA_SHADOW_BASE</code>; 8 MB reserved</td></tr>
 					<tr><td><code>0x40000000</code></td><td>Ring-3 ELF link/load address</td><td><code>USER_BASE</code>, 4 MB window up to <code>0x40400000</code></td></tr>
+					<tr><td><code>0x4FC00000</code></td><td>kmap window (virtual)</td><td><code>MM_KMAP_VA</code>; supervisor page table shared by every directory, 4 slots per CPU</td></tr>
 					<tr><td><code>0x50000000</code></td><td>Ring-3 window canvas</td><td><code>USER_WIN_BASE</code>, 4 MB slot</td></tr>
 					<tr><td><code>0x58000000</code></td><td>WM shadow mapping</td><td><code>WM_SHADOW_VA</code>; ring-3 WM only</td></tr>
 					<tr><td><code>0x59000000</code></td><td>WM content slots</td><td><code>WM_CONTENT_VA_BASE</code> + index × 4 MB (<code>WM_CONTENT_SLOT</code>)</td></tr>
 					<tr><td><code>0x60000000</code></td><td>Ring-3 anonymous heap</td><td><code>USER_HEAP_BASE</code>, up to 64 MB</td></tr>
+					<tr><td><code>0xC0000000</code></td><td>ioremap window (virtual)</td><td><code>IOMAP_BASE</code> to 4 GB: MMIO, firmware tables, LFB; kernel-only, 4 KB pages</td></tr>
 				</tbody>
 			</table>
 		</div>
@@ -152,13 +157,16 @@
 		<p>
 			<span class="file">paging.c</span> builds an identity map of RAM with
 			4 MB pages through <code>CR4.PSE</code>, adds the framebuffer's LFB
-			range, and enables <code>CR0.PG</code> and <code>CR0.WP</code>. Before
+			range (in lowmem; otherwise through the ioremap window), and enables
+			<code>CR0.PG</code> and <code>CR0.WP</code>. Before
 			switching, it clears translation controls a BIOS protected-mode call
 			can leave behind: <code>CR4.PAE/PGE/LA57/PCIDE/SMEP/SMAP</code> and
 			<code>EFER.LME</code>.
 		</p>
 		<ul>
-			<li><strong>Identity map.</strong> Physical and virtual addresses are equal. Page-directory entries are shared with every process.</li>
+			<li><strong>Identity map.</strong> Physical and virtual addresses are equal below 1 GB (lowmem). Page-directory entries are shared with every process.</li>
+			<li><strong>kmap window.</strong> The 4 MB slot at <code>MM_KMAP_VA</code> (<code>0x4FC00000</code>, in the unused gap below <code>USER_WIN_BASE</code>) has one supervisor page table that every process directory shares. <code>kmap(phys)</code> returns the identity address for lowmem, and for a highmem frame takes this CPU's next slot (4 per CPU, nesting LIFO) with preemption disabled until <code>kunmap()</code>; only the local TLB entry is flushed, because no other CPU uses that slot. It is not the last lowmem slot, because firmware puts the ACPI tables at the top of RAM, and not the top of the address space, where a VBE LFB may sit.</li>
+			<li><strong>ioremap window.</strong> Nothing above lowmem is mapped at its physical address. Device registers, ACPI/SMBIOS tables and the LFB go through <code>ioremap(phys, size, type)</code>: lowmem returns its identity address (an MMIO range there gets uncacheable 4 MB pages), everything else gets 4 KB pages in the kernel-only window <code>[0xC0000000, 4 GB)</code> (<code>IOMAP_BASE</code>). Its page tables are allocated on first use and copied into every live process directory (<code>process_sync_kernel_pde</code>); a range already covered by a same-type mapping is reused, and one that runs past the newest mapping grows it in place. MMIO pages are uncached except the pages the LFB's WC MTRR covers, which are cacheable in every alias; <code>IOREMAP_RAM</code> pages are write-back. Drivers keep the physical BAR for logs and use the returned pointer. <code>peek</code>, <code>poke</code> and <code>dbg</code> reach any physical byte through <code>paging_debug_map()</code>.</li>
 			<li><strong>Kernel stacks mapped everywhere.</strong> Each process gets a page directory cloned from the kernel identity map, so CR3 can switch while still running on the old kernel stack.</li>
 			<li><strong>LFB is uncacheable by default.</strong> If <code>mtrr_set_wc()</code> successfully covers the framebuffer, the LFB pages are mapped cacheable so the shadow-buffer flush can write-combine.</li>
 			<li><strong>No process isolation.</strong> There is one address space with per-process page directories only for mappings; a wild ring-0 or ring-3 pointer can reach any mapped physical page.</li>
@@ -183,26 +191,30 @@
 		<h2>Frame allocator</h2>
 		<p>
 			<span class="file">mm.c</span> keeps one bit per 4 KB page for the
-			RAM below the 1 GB ceiling: the bitmap is sized to the highest E820 RAM
-			address (32 KB at 1 GB) and is not part of the kernel image.
-			<code>mm_init</code> places it in the first E820 RAM run at or above
+			RAM below 4 GB: the bitmap is sized to the highest E820 RAM
+			address (32 KB at 1 GB, 96 KB at 3 GB) and is not part of the kernel
+			image. <code>mm_init</code> places it in the first lowmem E820 RAM run at or above
 			<code>0x900000</code> that avoids the boot rootfs pack and the LFB, and
 			marks its own pages used (legacy boots put it right after the pack;
 			a 16 MB static fallback covers a missing E820 map).  The allocators
 			disable interrupts around the bitmap and hand out the lowest free
-			frames: a <code>first_free</code> word hint skips the full low part,
-			<code>mm_alloc_page()</code> finds the bit with <code>bsf</code>, and
+			frames of a zone: each zone has a first-free word hint that skips its
+			full low part, single pages are found with <code>bsf</code>, and
 			<code>mm_alloc_pages(n)</code> steps over full and empty words 32 pages
-			at a time. The allocator returns:
+			at a time. Everything the kernel dereferences by physical address
+			(page tables, DMA buffers, canvases, kernel stacks, <code>kmalloc</code>)
+			comes from lowmem; ring-3 program, stack and heap frames prefer
+			highmem. The allocator returns:
 		</p>
 		<ul>
-			<li><code>mm_alloc_page()</code> — one zero-address-ambiguous page (physical address 0 means failure).</li>
-			<li><code>mm_alloc_pages(n)</code> — <code>n</code> contiguous pages.</li>
+			<li><code>mm_alloc_page()</code> — one lowmem page (physical address 0 means failure).</li>
+			<li><code>mm_alloc_user_page()</code> — one frame for a ring-3 mapping: highmem first, lowmem when highmem is exhausted. Touch it only through <code>kmap()</code> or the user mapping (<span class="file">elf.c</span>, the user stack in <span class="file">process.c</span>, the heap in <span class="file">sys_mem.c</span>).</li>
+			<li><code>mm_alloc_pages(n)</code> — <code>n</code> contiguous lowmem pages.</li>
 			<li><code>mm_free_page()</code> / <code>mm_free_range()</code> — release a page or a page-aligned boot reservation.</li>
 		</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 1 GB, the bitmap itself and the
+			shadow region, any LFB that overlaps RAM, the bitmap itself 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.
@@ -279,9 +291,9 @@
 					<tr><th>Range</th><th>Size</th><th>Contents</th></tr>
 				</thead>
 				<tbody>
-					<tr><td><code>0x40000000</code>–<code>0x40400000</code></td><td>4 MB</td><td>ELF segments, user stack top at <code>0x40400000</code> (32 pages / 128 KB, grows down)</td></tr>
+					<tr><td><code>0x40000000</code>–<code>0x40400000</code></td><td>4 MB</td><td>ELF segments, user stack top at <code>0x40400000</code> (32 pages / 128 KB, grows down); highmem frames when available</td></tr>
 					<tr><td><code>0x50000000</code>–<code>0x50400000</code></td><td>4 MB</td><td>One window canvas mapping (RW, per window)</td></tr>
-					<tr><td><code>0x60000000</code>–<code>0x64000000</code></td><td>up to 64 MB</td><td>Anonymous heap; initial allocation zeroed, growth pages are not</td></tr>
+					<tr><td><code>0x60000000</code>–<code>0x64000000</code></td><td>up to 64 MB</td><td>Anonymous heap (highmem frames when available, zeroed through <code>kmap</code>); initial allocation zeroed, growth pages are not</td></tr>
 				</tbody>
 			</table>
 		</div>
@@ -307,7 +319,7 @@
 	<section id="pack">
 		<h2>Rootfs pack lifetime</h2>
 		<ol>
-			<li>The compressed pack is at most 2 MB (<code>PACK_MAX</code>), so the legacy copy ends below <code>0xB00000</code>; inflated it is about 2 MB with the WiFi firmware (the 8 MB <code>RAMFS_MAX_DECOMPRESSED</code> cap applies).</li>
+			<li>The compressed pack is at most 2 MB (<code>PACK_MAX</code>), so the legacy copy ends below <code>0xB00000</code>; inflated it is about 4.1 MB with the WiFi firmware (the 8 MB <code>RAMFS_MAX_DECOMPRESSED</code> cap applies).</li>
 			<li>The boot path places the gzip pack in RAM: the legacy stage copies it directly to <code>0x900000</code> with a protected-mode <code>rep movsb</code>, UEFI into a loader-data buffer below 1 GB, 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>
@@ -323,7 +335,9 @@
 		<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>1 GB ceiling.</strong> Physical memory above <code>0x40000000</code> is not used: that address range is ring-3 space, so the kernel has no mapping for frames above it (a highmem zone with temporary kernel mappings, or PAE for RAM above 4 GB, would be needed). A wild kernel pointer between the identity map and the user windows is an instant <code>#PF</code>.</li>
+			<li><strong>4 GB ceiling, 64 MB per process.</strong> RAM above 4 GB is ignored (it would need PAE). Highmem raises the system total, but one process still has a 64 MB heap and a 4 MB image/stack window; <code>[USER_BASE, IOMAP_BASE)</code> is free for demand paging. A wild kernel pointer between the identity map and the user windows is an instant <code>#PF</code>.</li>
+			<li><strong>ioremap is append-only.</strong> 1 GB of window, 96 mapping records and no unmap: a driver that remaps repeatedly would exhaust it (<code>ioremap</code> logs <code>window full</code> and returns 0).</li>
+			<li><strong>Highmem is user-only.</strong> Contiguous kernel buffers and every kernel structure stay in the 1 GB lowmem zone, so a machine with 4 GB can still run out of lowmem first.</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>
 		</ul>
@@ -333,11 +347,11 @@
 		<h2>Source map</h2>
 		<ul class="filelist">
 			<li><code>kernel/mm.c</code>, <code>kernel/mm.h</code> — E820 + bitmap allocator</li>
-			<li><code>include/phys.h</code> — shared 1 GB physical ceiling</li>
+			<li><code>include/phys.h</code> — lowmem ceiling, kmap window, highmem limit</li>
 			<li><code>kernel/kmalloc.c</code>, <code>kernel/kmalloc.h</code> — kernel heap</li>
 			<li><code>user/lib/libc/malloc.c</code>, <code>user/lib/libc/ucmd_mem.c</code> — ring-3 heap and <code>SYS_MEM_*</code> backend</li>
 			<li><code>user/lib/libc/string.c</code>, <code>user/lib/libc/stdlib.c</code> — ring-3 libc support</li>
-			<li><code>kernel/paging.c</code>, <code>kernel/paging.h</code> — identity map</li>
+			<li><code>kernel/paging.c</code>, <code>kernel/paging.h</code> — identity map, kmap window, ioremap window, debug physical access</li>
 			<li><code>kernel/mtrr.c</code> — write-combining MTRRs</li>
 			<li><code>kernel/process.c</code>, <code>kernel/process.h</code> — stacks and user layout</li>
 			<li><code>kernel/ramfs.c</code> — pack copy / inflate</li>

Разница между файлами не показана из-за своего большого размера
+ 6 - 5
docs/network.html


Разница между файлами не показана из-за своего большого размера
+ 17 - 11
docs/userland.html


+ 2 - 1
host/main.c

@@ -6,7 +6,8 @@
  * --keys types into a headless window, one key every 100 ms from 300 ms
  * after the first frame: plain characters, plus {enter} {esc} {tab} {bs}
  * {del} {up} {down} {left} {right} {ctrl-l}, {wait} (a 1 s pause) and
- * {click:X,Y} (a left click at canvas coordinates X,Y).
+ * {click:X,Y} (a left click at canvas coordinates X,Y) and {move:X,Y}
+ * (move the pointer there without a click, for hover).
  */
 #include <signal.h>
 #include <stdio.h>

+ 10 - 1
host/window_headless.c

@@ -109,7 +109,8 @@ void host_headless_pump(unsigned int timeout_ms)
 }
 
 /* --keys `{click:X,Y}`: the pointer moves there and a left press is
- * queued, its release on the next event poll. */
+ * queued, its release on the next event poll; `{move:X,Y}` only moves
+ * it (hover). */
 static int ptr_x, ptr_y;
 static unsigned int ptr_buttons;
 static int click_pending;       /* 1 press queued, 2 release queued */
@@ -176,6 +177,14 @@ int host_headless_key(void)
 	if (!*key_pos || now < key_due)
 		return 0;
 	code = (unsigned char)*key_pos++;
+	if (code == '{' && !strncmp(key_pos, "move:", 5)) {
+		const char *end = strchr(key_pos, '}');
+
+		sscanf(key_pos + 5, "%d,%d", &ptr_x, &ptr_y);
+		key_pos = end ? end + 1 : key_pos + strlen(key_pos);
+		key_due = now + 300;
+		return 0;
+	}
 	if (code == '{' && !strncmp(key_pos, "click:", 6)) {
 		const char *end = strchr(key_pos, '}');
 

+ 19 - 4
include/icon_abi.h

@@ -1,8 +1,9 @@
 /* UI icons and the NBICO1 icon bundle.
  *
- * Shared by ngui (user/lib/ngui: enum ngui_icon, ngui_icon()) and the host
- * tool tools/mkicons.c, which rasterizes the Adwaita symbolic SVGs in
- * assets/icons/adwaita/ into /ramfs/icons/adwaita16.icn.
+ * Shared by ngui (user/lib/ngui: enum ngui_icon, ngui_icon()), the WM's
+ * uwm_lib (enum uwm_icon, usurf_icon()) and the host tool tools/mkicons.c,
+ * which rasterizes the Adwaita symbolic SVGs in assets/icons/adwaita/ into
+ * /ramfs/icons/adwaita16.icn.
  *
  * ICON_LIST is an X-macro: X(ENUM_SUFFIX, "short name", "theme file").  The
  * order is the icon id and the order of the masks in the bundle, so append
@@ -23,7 +24,21 @@
 	X(STOP,          "stop",    "process-stop-symbolic")   \
 	X(CLOSE,         "close",   "window-close-symbolic")   \
 	X(SEARCH,        "search",  "system-search-symbolic")  \
-	X(MENU,          "menu",    "open-menu-symbolic")
+	X(MENU,          "menu",    "open-menu-symbolic")      \
+	X(BATTERY,       "battery", "battery-symbolic")         \
+	X(AC,            "ac",      "ac-adapter-symbolic")      \
+	X(NET_WIRED,     "wired",   "network-wired-symbolic")   \
+	X(NET_WIRED_OFF, "wired-off", "network-wired-disconnected-symbolic") \
+	X(NET_WIFI_0,    "wifi-0",  "network-wireless-signal-none-symbolic") \
+	X(NET_WIFI_1,    "wifi-1",  "network-wireless-signal-weak-symbolic") \
+	X(NET_WIFI_2,    "wifi-2",  "network-wireless-signal-ok-symbolic")   \
+	X(NET_WIFI_3,    "wifi-3",  "network-wireless-signal-good-symbolic") \
+	X(NET_WIFI_4,    "wifi-4",  "network-wireless-signal-excellent-symbolic") \
+	X(NET_WIFI_OFF,  "wifi-off", "network-wireless-offline-symbolic") \
+	X(NET_USB,       "usb",     "phone-symbolic")
+
+/* The NBICO1 bundle, read by ngui and the WM's uwm_lib. */
+#define ICON_BUNDLE_PATH "/ramfs/icons/adwaita16.icn"
 
 /* ---- NBICO1 bundle ------------------------------------------------------
  * [struct icon_file_hdr][count x size*size coverage bytes]

+ 25 - 6
include/phys.h

@@ -1,11 +1,30 @@
-/* Physical address ceiling shared by the 32-bit kernel and the UEFI loader. */
+/* Physical address limits shared by the 32-bit kernel and the UEFI loader. */
 #ifndef PHYS_H
 #define PHYS_H
 
-/* 1 GB: the kernel identity-maps [0, MM_MAX_PHYS) with 4 MB pages, and ring-3
- * address space starts at USER_BASE (0x40000000), so the identity map cannot
- * grow past 1 GB without a highmem zone.  The frame bitmap (32 KB at 1 GB)
- * is placed in RAM by mm_init(), not in the kernel's BSS. */
-#define MM_MAX_PHYS      (1024u * 1024 * 1024)
+/* Lowmem: the kernel identity-maps [0, MM_MAX_PHYS) with 4 MB pages and
+ * dereferences physical addresses there directly.  Ring-3 address space
+ * starts at USER_BASE (0x40000000), so lowmem ends at 1 GB.  Everything the
+ * kernel touches through a physical pointer (page tables, DMA buffers,
+ * canvases, kernel stacks, kmalloc) must be lowmem. */
+#define MM_MAX_PHYS      0x40000000u
+
+/* The kmap window: one 4 MB page-directory slot in the unused gap between
+ * the ring-3 program/stack window and USER_WIN_BASE (0x50000000).  Its
+ * supervisor page table holds per-CPU temporary mappings of arbitrary frames
+ * and is shared by every process directory.  Not the last lowmem slot
+ * (firmware puts the ACPI tables at the top of RAM) and not the top of the
+ * address space (a VBE LFB may sit at 0xFF000000). */
+#define MM_KMAP_VA       0x4FC00000u
+
+/* The ioremap window: kernel-only virtual space for MMIO, firmware tables
+ * and the LFB above lowmem.  Ring-3 space may use [USER_BASE, IOMAP_BASE). */
+#define IOMAP_BASE       0xC0000000u
+
+/* Highmem: RAM in [MM_MAX_PHYS, MM_HIGH_LIMIT) is tracked by the frame
+ * allocator but never identity-mapped; it only backs ring-3 pages, which
+ * the kernel reaches through the owning process's mapping or kmap().
+ * RAM above 4 GB would need PAE. */
+#define MM_HIGH_LIMIT    0x100000000ull
 
 #endif

+ 29 - 2
include/syscall.h

@@ -121,6 +121,8 @@
 				  cursor (enum cursor_shape, cursor_abi.h) */
 #define SYS_WM_SET_CURSOR 118 /* (int shape | CURSOR_NO_OVERRIDE) -> 0/-1;
 				  WM only: cursor for its chrome/drags */
+#define SYS_WM_MOUSE_CAPTURE 121 /* (int on) -> 0/-1; WM only: own the pointer
+				    while it drags or resizes a window */
 
 /* Storage management (see sys_fs.c, the `disks` app).  SYS_BLK_READ reads one
  * raw 512-byte sector of block device `index` (SYS_BLKDEV order) so ring 3
@@ -130,6 +132,23 @@
  * live in RAM; the boot scan loads SYS_MOUNT_CFG_PATH from /data. */
 #define SYS_BLK_READ      119 /* (index, lba, void *buf512) -> 0/<0 */
 #define SYS_MOUNT_CTL     120 /* (op, a2, a3) */
+
+/* Boot storage-scan log (the `bootscan` kernel thread's serial output,
+ * captured while it runs).  STAT fills struct sys_bootlog, READ copies from
+ * absolute byte offset a2 into the caller's SYS_BOOTLOG_CHUNK buffer at a3
+ * and returns the number of bytes (0 at the end, <0 on a bad range).  The
+ * producer is a ring: `start` is the oldest byte still available. */
+#define SYS_BOOTLOG       122
+#define SYS_BOOTLOG_STAT  0
+#define SYS_BOOTLOG_READ  1
+#define SYS_BOOTLOG_CHUNK 1024
+
+struct sys_bootlog {
+	unsigned int total;      /* bytes ever written */
+	unsigned int start;      /* absolute offset of the oldest available byte */
+	unsigned int capacity;   /* ring size in bytes (0: no log) */
+	unsigned int active;     /* the scan thread is still producing */
+};
 #define SYS_MOUNT_POLICY_SET 0 /* (const struct sys_mount_policy *) -> 0/<0 */
 #define SYS_MOUNT_POLICY_GET 1 /* (index, struct sys_mount_policy *out) -> 1/0 */
 #define SYS_BACKLIGHT     113 /* (int op, int value) -> percent / -1.
@@ -265,6 +284,8 @@ struct sys_netinfo {
 	char name[8];
 	char chip[16];
 	unsigned int link;       /* active netdev reports a usable link */
+	unsigned int rx_bytes;   /* frame bytes received since boot (wraps) */
+	unsigned int tx_bytes;   /* frame bytes sent since boot (wraps) */
 };
 
 /* One selectable NIC (see SYS_NET_DEVLIST). */
@@ -343,10 +364,15 @@ struct sys_meminfo {
 	unsigned int free_pages;
 	unsigned int page_size;
 	unsigned int e820_count;
+	unsigned int high_free_pages;   /* free frames above 1 GB */
+	unsigned int uncached_high_mb;  /* highmem the MTRRs leave non-WB
+					 * (withheld from the allocator) */
+	unsigned int uncached_low_mb;   /* lowmem RAM the MTRRs leave non-WB */
 };
 
-/* One live process, for `top` (PROC_NAME_LEN = 12).  cpu_ticks/life_ticks
- * are PIT ticks (1 kHz), so lifecycle percentages can be derived. */
+/* One live process, for `top`/`process` (PROC_NAME_LEN = 12).
+ * cpu_ticks/life_ticks are PIT ticks (1 kHz), so lifecycle percentages can be
+ * derived. */
 struct sys_procinfo {
 	unsigned int pid;
 	unsigned int state;       /* PROC_*: 1 ready, 2 running, 4 sleep, 5 block */
@@ -356,6 +382,7 @@ struct sys_procinfo {
 	unsigned int weight;      /* CFS weight, 1..SCHED_WEIGHT_MAX */
 	unsigned int cpu;         /* CPU id, or 0xFFFFFFFF when not running */
 	char name[12];
+	unsigned int kernel_thread; /* 1 = kernel thread (not a ring-3 program) */
 };
 
 /* Scheduler counters for `top` and the `sched` command. */

+ 11 - 45
include/wm_abi.h

@@ -8,53 +8,19 @@
 
 /* Shared chrome layout (the WM draws it, the kernel only stores geometry). */
 #define WM_BAR_H   30
-#define WM_BTN_X   132
-#define WM_BTN_Y   5
-#define WM_BTN_W   56
-#define WM_BTN_H   20
-/* The other top-bar buttons follow the terminal button. */
-#define WM_BTN_GAP     6
-#define WM_BTN_CLOCK_X (WM_BTN_X + WM_BTN_W + WM_BTN_GAP)
-#define WM_BTN_CLOCK_Y WM_BTN_Y
-#define WM_BTN_CLOCK_W 64
-#define WM_BTN_CLOCK_H WM_BTN_H
-#define WM_BTN_COLORS_X (WM_BTN_CLOCK_X + WM_BTN_CLOCK_W + WM_BTN_GAP)
-#define WM_BTN_COLORS_Y WM_BTN_Y
-#define WM_BTN_COLORS_W 72
-#define WM_BTN_COLORS_H WM_BTN_H
-#define WM_BTN_FILES_X (WM_BTN_COLORS_X + WM_BTN_COLORS_W + WM_BTN_GAP)
-#define WM_BTN_FILES_Y WM_BTN_Y
-#define WM_BTN_FILES_W 64
-#define WM_BTN_FILES_H WM_BTN_H
-#define WM_BTN_MINES_X (WM_BTN_FILES_X + WM_BTN_FILES_W + WM_BTN_GAP)
-#define WM_BTN_MINES_Y WM_BTN_Y
-#define WM_BTN_MINES_W 56
-#define WM_BTN_MINES_H WM_BTN_H
-#define WM_BTN_WEB_X (WM_BTN_MINES_X + WM_BTN_MINES_W + WM_BTN_GAP)
-#define WM_BTN_WEB_Y WM_BTN_Y
-#define WM_BTN_WEB_W 56
-#define WM_BTN_WEB_H WM_BTN_H
-#define WM_BTN_CARDS_X (WM_BTN_WEB_X + WM_BTN_WEB_W + WM_BTN_GAP)
-#define WM_BTN_CARDS_Y WM_BTN_Y
-#define WM_BTN_CARDS_W 56
-#define WM_BTN_CARDS_H WM_BTN_H
-#define WM_BTN_GALLERY_X (WM_BTN_CARDS_X + WM_BTN_CARDS_W + WM_BTN_GAP)
-#define WM_BTN_GALLERY_Y WM_BTN_Y
-#define WM_BTN_GALLERY_W 72
-#define WM_BTN_GALLERY_H WM_BTN_H
-#define WM_BTN_CHAT_X (WM_BTN_GALLERY_X + WM_BTN_GALLERY_W + WM_BTN_GAP)
-#define WM_BTN_CHAT_Y WM_BTN_Y
-#define WM_BTN_CHAT_W 56
-#define WM_BTN_CHAT_H WM_BTN_H
-#define WM_BTN_DISKS_X (WM_BTN_CHAT_X + WM_BTN_CHAT_W + WM_BTN_GAP)
-#define WM_BTN_DISKS_Y WM_BTN_Y
-#define WM_BTN_DISKS_W 64
-#define WM_BTN_DISKS_H WM_BTN_H
-/* Right edge of the launcher buttons: the notice text and the right-aligned
- * status (clock, sensors, power) never draw left of it. */
-#define WM_BTN_END_X (WM_BTN_DISKS_X + WM_BTN_DISKS_W)
+/* The WM's start button is the NebOS banner at the top left; it opens the
+ * start menu.  The bar notice and the right-aligned status area (clock,
+ * sensors, network, power) never draw left of this edge. */
+#define WM_BAR_LEFT_END 160
 #define WM_TITLE_H 22
 #define WM_CLOSE_W 16
+/* No visible frame: the title bar and content start at the window origin. */
+#define WM_BORDER_W 0
+/* Invisible resize band around a normal window: WM_RESIZE_IN px inside the
+ * outer edge and WM_RESIZE_OUT px outside it.  Presses in the inside part
+ * are not routed to the window content; the whole band starts a resize. */
+#define WM_RESIZE_IN  1
+#define WM_RESIZE_OUT 3
 
 /* Keep at least this much of the title bar on-screen when a window is dragged
  * past an edge, so it can always be grabbed and brought back. */

+ 25 - 36
kernel/acpi.c

@@ -1,8 +1,8 @@
 /* Minimal ACPI RSDP/RSDT/XSDT/MADT parser.  See acpi.h.
  *
- * ACPI tables are usually below the 128 MB identity map, but on larger
- * machines they can sit above it; every table page is mapped through
- * paging_map_ram() before it is read.  Anything malformed or unsupported is
+ * ACPI tables are usually in lowmem, but on larger machines they sit above
+ * it; every table is reached through ioremap() (the identity address in
+ * lowmem, the kernel window otherwise).  Anything malformed or unsupported is
  * treated as "no SMP", never as a fatal error.
  */
 
@@ -51,9 +51,10 @@ static int acpi_ok;
  * when the firmware points at a real CMOS register. */
 static unsigned int rtc_century_reg;
 
-static void *phys_ptr(unsigned int addr)
+/* Kernel address of a firmware table range (write-back RAM mapping). */
+static void *table_ptr(unsigned int addr, unsigned int len)
 {
-	return (void *)addr;
+	return ioremap(addr, len, IOREMAP_RAM);
 }
 
 static int range_ok(unsigned int addr, unsigned int len)
@@ -63,21 +64,6 @@ static int range_ok(unsigned int addr, unsigned int len)
 	return addr + len >= addr;
 }
 
-/* Map every 4 MB page touched by [addr, addr+len). */
-static void map_table(unsigned int addr, unsigned int len)
-{
-	unsigned int p = addr & 0xFFC00000u;
-	unsigned int last = (addr + len - 1u) & 0xFFC00000u;
-
-	for (;;) {
-		paging_map_ram(p, 0x400000u);
-		if (p >= last)
-			break;
-		if (p > 0xFFC00000u - 0x400000u)
-			break;
-		p += 0x400000u;
-	}
-}
 
 static int sum_ok(const unsigned char *p, unsigned int len)
 {
@@ -114,14 +100,13 @@ static const struct acpi_rsdp *find_rsdp(void)
 	unsigned int given = bootinfo_rsdp();
 
 	/* The UEFI loader passes the RSDP address from the system table.  UEFI
-	 * places ACPI tables outside the 128 MB identity map often enough that
-	 * the scan below would never see them, so map the page first. */
+	 * places ACPI tables outside lowmem often enough that the scan below
+	 * would never see them, so map it first (4 KB covers the largest
+	 * length rsdp_valid() accepts). */
 	if (given && given < 0xFFF00000u) {
-		const struct acpi_rsdp *r;
+		const struct acpi_rsdp *r = table_ptr(given, 0x1000);
 
-		paging_map_ram(given & 0xFFC00000u, 0x800000u);
-		r = (const struct acpi_rsdp *)given;
-		if (rsdp_valid(r)) {
+		if (r && rsdp_valid(r)) {
 			serial_puts(SERIAL_COM1, "acpi: EFI RSDP at ");
 			serial_puthex(SERIAL_COM1, given);
 			serial_puts(SERIAL_COM1, "\n");
@@ -169,13 +154,15 @@ static const struct acpi_sdt *find_madt(const struct acpi_rsdp *rsdp)
 	}
 	if (!addr || !range_ok(addr, sizeof(struct acpi_sdt)))
 		return 0;
-	map_table(addr, sizeof(struct acpi_sdt));
-	sdt = (const struct acpi_sdt *)phys_ptr(addr);
+	sdt = table_ptr(addr, sizeof(struct acpi_sdt));
+	if (!sdt)
+		return 0;
 	len = sdt->length;
-	if (len < sizeof(struct acpi_sdt) || len > 0x100000u)
+	if (len < sizeof(struct acpi_sdt) || len > 0x100000u ||
+	    !range_ok(addr, len))
 		return 0;
-	map_table(addr, len);
-	if (!sum_ok((const unsigned char *)sdt, len))
+	sdt = table_ptr(addr, len);
+	if (!sdt || !sum_ok((const unsigned char *)sdt, len))
 		return 0;
 
 	for (i = sizeof(struct acpi_sdt); i + step <= len; i += step) {
@@ -194,13 +181,15 @@ static const struct acpi_sdt *find_madt(const struct acpi_rsdp *rsdp)
 		}
 		if (!taddr || !range_ok(taddr, sizeof(struct acpi_sdt)))
 			continue;
-		map_table(taddr, sizeof(struct acpi_sdt));
-		t = (const struct acpi_sdt *)phys_ptr(taddr);
+		t = table_ptr(taddr, sizeof(struct acpi_sdt));
+		if (!t)
+			continue;
 		tlen = t->length;
-		if (tlen < sizeof(struct acpi_sdt) || tlen > 0x100000u)
+		if (tlen < sizeof(struct acpi_sdt) || tlen > 0x100000u ||
+		    !range_ok(taddr, tlen))
 			continue;
-		map_table(taddr, tlen);
-		if (!sum_ok((const unsigned char *)t, tlen))
+		t = table_ptr(taddr, tlen);
+		if (!t || !sum_ok((const unsigned char *)t, tlen))
 			continue;
 		if (t->signature[0] == 'F' && t->signature[1] == 'A' &&
 		    t->signature[2] == 'C' && t->signature[3] == 'P') {

+ 3 - 2
kernel/ahci.c

@@ -255,8 +255,9 @@ int ahci_init(void)
 	cmd = pci_cfg_read32(bus, dev, func, 0x04);
 	pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);  /* mem + bus master */
 
-	process_map_mmio_all(bar, 0x2000);
-	ahci.abar = bar;
+	ahci.abar = (unsigned int)ioremap(bar, 0x2000, IOREMAP_MMIO);
+	if (!ahci.abar)
+		return -1;
 
 	cap = rd(H_CAP);
 	if (cap == 0 || cap == 0xFFFFFFFFu) {

+ 9 - 0
kernel/ap_tramp.S

@@ -18,6 +18,7 @@
 .equ AP_INFO_STACK, AP_INFO + 4
 .equ AP_INFO_ENTRY, AP_INFO + 8
 .equ AP_INFO_CPU,  AP_INFO + 12
+.equ AP_INFO_CR0,  AP_INFO + 16   /* written back: CR0 as the AP woke up */
 
 .globl ap_tramp_start
 ap_tramp_start:
@@ -81,7 +82,15 @@ ap_pm32:
 	andl  $~0x100, %eax
 	wrmsr
 3:
+	/* INIT leaves an AP with CR0 = 0x60000010: CD and NW set, so its
+	 * caches are off until the OS clears them.  Nothing did, and every
+	 * ring-3 process the scheduler put on an AP (the browser, its TLS
+	 * helpers) ran from uncached memory, about 100x slower; QEMU does
+	 * not model caches, so only real hardware showed it.  The original
+	 * value goes back to the BSP for the boot log. */
 	movl  %cr0, %eax
+	movl  %eax, AP_INFO_CR0
+	andl  $~0x60000000, %eax      /* clear CR0.CD | CR0.NW */
 	orl   $0x80010000, %eax       /* CR0.PG | CR0.WP */
 	movl  %eax, %cr0
 	jmp   1f

+ 14 - 6
kernel/ati.c

@@ -16,6 +16,7 @@
  * the lower 16 bits match the PCI device ID.
  */
 
+#include "paging.h"
 #include "ati.h"
 #include "pci.h"
 #include "radeon.h"
@@ -44,6 +45,9 @@ static int ati_device_is_radeon(unsigned int device_id)
 	}
 }
 
+/* Map the 1 KB register block at physical `pa` and probe it. */
+static int try_mmio_at(unsigned int pa, unsigned int device_id);
+
 static int try_mmio(volatile unsigned int *mmio, unsigned int device_id)
 {
 	unsigned int chip_id = mmio[ATI_CONFIG_CHIP_ID_OFS / 4];
@@ -65,8 +69,7 @@ static void ati_mach64_fixup(unsigned int bus, unsigned int dev,
 
 	/* Method 1: BAR2 (separate MMIO aperture) */
 	if (!(bar2 & 1) && (bar2 & 0xFFFFFFF0)) {
-		if (try_mmio((volatile unsigned int *)(bar2 & 0xFFFFFFF0),
-			     device_id)) {
+		if (try_mmio_at(bar2 & 0xFFFFFFF0, device_id)) {
 			mmio_method = 1;
 			return;
 		}
@@ -75,13 +78,11 @@ static void ati_mach64_fixup(unsigned int bus, unsigned int dev,
 	/* Method 2-3: MMIO at end of LFB aperture (264VT/GT) */
 	if (!(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
 		unsigned int lfb = bar0 & 0xFFFFFFF0;
-		if (try_mmio((volatile unsigned int *)(lfb + 0x7FFC00),
-			     device_id)) {
+		if (try_mmio_at(lfb + 0x7FFC00, device_id)) {
 			mmio_method = 2;
 			return;
 		}
-		if (try_mmio((volatile unsigned int *)(lfb + 0x3FFC00),
-			     device_id)) {
+		if (try_mmio_at(lfb + 0x3FFC00, device_id)) {
 			mmio_method = 3;
 			return;
 		}
@@ -103,6 +104,13 @@ static void ati_mach64_fixup(unsigned int bus, unsigned int dev,
 	}
 }
 
+static int try_mmio_at(unsigned int pa, unsigned int device_id)
+{
+	volatile unsigned int *mmio = ioremap(pa, 0x400, IOREMAP_MMIO);
+
+	return mmio ? try_mmio(mmio, device_id) : 0;
+}
+
 void ati_init(unsigned int bus, unsigned int dev)
 {
 	unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);

+ 9 - 4
kernel/backlight.c

@@ -37,7 +37,8 @@
 #define BLC_STEP_PERCENT	10
 #define BLC_DEFAULT_PERIOD	6000u	/* 19.2 MHz / (200 Hz * 16) */
 
-static unsigned int blc_bar;
+static unsigned int blc_bar;      /* physical BAR (reported) */
+static unsigned int blc_regs;     /* its kernel mapping */
 static unsigned int blc_pipe;
 static unsigned int blc_period;
 static unsigned int blc_duty;
@@ -48,12 +49,12 @@ static unsigned int blc_seq;	/* bumped on every user-visible change */
 
 static unsigned int blc_read(unsigned int reg)
 {
-	return *(volatile unsigned int *)(blc_bar + reg);
+	return *(volatile unsigned int *)(blc_regs + reg);
 }
 
 static void blc_write(unsigned int reg, unsigned int val)
 {
-	*(volatile unsigned int *)(blc_bar + reg) = val;
+	*(volatile unsigned int *)(blc_regs + reg) = val;
 }
 
 void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id)
@@ -83,7 +84,11 @@ void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 
 	/* The backlight registers live below 2 MB in the display MMIO BAR. */
 	blc_bar = bar0 & 0xFFFFFFF0u;
-	paging_map_mmio(blc_bar, 0x200000);
+	blc_regs = (unsigned int)ioremap(blc_bar, 0x200000, IOREMAP_MMIO);
+	if (!blc_regs) {
+		blc_bar = 0;
+		return;
+	}
 
 	for (i = 0; i < 2; i++) {
 		ctl2[i] = blc_read(VLV_BLC_PWM_CTL2(i));

+ 89 - 0
kernel/bootcon.c

@@ -14,6 +14,7 @@
 #include "bootinfo.h"
 #include "font_8x16.h"
 #include "mm.h"
+#include "process.h"
 #include "serial.h"
 #include "vga.h"
 
@@ -41,6 +42,14 @@ static unsigned int log_cap;
 static unsigned int log_len;
 static int log_truncated;
 
+/* Serial output of the bootscan kernel thread (bootcon_scan_begin). */
+static char *scan_buf;
+static unsigned int scan_cap;
+static unsigned int scan_total;
+static unsigned int scan_start;
+static unsigned int scan_pid;
+static int scan_active;
+
 static void fb_px(unsigned int x, unsigned int y, unsigned int r,
 		  unsigned int g, unsigned int b)
 {
@@ -258,6 +267,80 @@ int bootcon_log_truncated(void)
 	return log_truncated;
 }
 
+/* ---- bootscan thread serial capture ---------------------------------- */
+
+void bootcon_scan_begin(void)
+{
+	unsigned int mem;
+
+	if (!scan_buf) {
+		mem = mm_alloc_pages(BOOTCON_SCAN_PAGES);
+		if (!mem) {
+			scan_cap = 0;
+			scan_active = 0;
+			return;
+		}
+		scan_buf = (char *)mem;
+		scan_cap = BOOTCON_SCAN_PAGES * PAGE_SIZE;
+	}
+	scan_pid = current_proc() ? current_proc()->pid : 0;
+	scan_total = 0;
+	scan_start = 0;
+	scan_active = 1;
+}
+
+void bootcon_scan_end(void)
+{
+	scan_active = 0;
+}
+
+void bootcon_scan_char(char c)
+{
+	if (!scan_active || !scan_cap)
+		return;
+	if (!current_proc() || current_proc()->pid != scan_pid)
+		return;
+	if (c == '\r')
+		return;                 /* serial_puts emits CR before every LF */
+	if (scan_total - scan_start >= scan_cap)
+		scan_start = scan_total - scan_cap + 1;
+	scan_buf[scan_total % scan_cap] = c;
+	scan_total++;
+}
+
+unsigned int bootcon_scan_total(void)
+{
+	return scan_total;
+}
+
+unsigned int bootcon_scan_start(void)
+{
+	return scan_start;
+}
+
+unsigned int bootcon_scan_capacity(void)
+{
+	return scan_cap;
+}
+
+int bootcon_scan_running(void)
+{
+	return scan_active;
+}
+
+unsigned int bootcon_scan_read(unsigned int off, char *dst, unsigned int max)
+{
+	unsigned int n = 0;
+
+	if (!scan_buf || !scan_cap)
+		return 0;
+	if (off < scan_start)
+		off = scan_start;
+	while (off < scan_total && n < max)
+		dst[n++] = scan_buf[off++ % scan_cap];
+	return n;
+}
+
 void bootcon_dec(unsigned int v)
 {
 	char buf[12];
@@ -320,3 +403,9 @@ void bootcon_fb_info(void)
 	serial_puthex(SERIAL_COM1, fb_base);
 	serial_puts(SERIAL_COM1, "\n");
 }
+
+void bootcon_set_lfb(void *va)
+{
+	if (va)
+		fb_lfb = (volatile unsigned char *)va;
+}

+ 23 - 0
kernel/bootcon.h

@@ -13,11 +13,17 @@
 #define BOOTCON_H
 
 #define BOOTCON_LOG_PAGES 8
+/* Ring for the storage scan thread's own serial output (see bootcon.c).  It
+ * is allocated on demand from the frame allocator, not BSS. */
+#define BOOTCON_SCAN_PAGES 4
 
 void bootcon_init(void);
 int  bootcon_ok(void);
 void bootcon_set_enabled(int on);
 void bootcon_puts(const char *s);
+/* Switch drawing to the LFB's kernel address once paging is on (the LFB may
+ * live in the ioremap window, not at its physical address); 0 is ignored. */
+void bootcon_set_lfb(void *va);
 void bootcon_dec(unsigned int v);
 void bootcon_hex(unsigned int v);
 /* One line with the cached mode geometry and LFB address. */
@@ -32,4 +38,21 @@ void bootcon_log_init(void);
 const char *bootcon_log(unsigned int *len);
 int bootcon_log_truncated(void);
 
+/* Capture the bootscan kernel thread's serial output.  begin() is called by
+ * the thread itself (it allocates the ring and records the thread pid), end()
+ * stops the capture when the thread returns.  serial_putc() feeds every COM1
+ * character through bootcon_scan_char(), which keeps only the owning
+ * thread's output.  The ring survives end() so the `bootscan` window can
+ * still tail the completed log. */
+void bootcon_scan_begin(void);
+void bootcon_scan_end(void);
+void bootcon_scan_char(char c);
+unsigned int bootcon_scan_total(void);
+unsigned int bootcon_scan_start(void);
+unsigned int bootcon_scan_capacity(void);
+int bootcon_scan_running(void);
+/* Copy up to max bytes from absolute offset off; off is clamped to the
+ * oldest byte still in the ring.  Returns the byte count. */
+unsigned int bootcon_scan_read(unsigned int off, char *dst, unsigned int max);
+
 #endif

+ 20 - 0
kernel/cpu.c

@@ -37,6 +37,26 @@ unsigned int cpu_family_model(void)
 	return (family << 8) | model;
 }
 
+/* An AP leaves INIT with CR0.NE clear and an x87 control word of 0x0040:
+ * every exception unmasked and 24-bit precision.  With NE clear an
+ * unmasked exception (an inexact result, i.e. most floating-point
+ * operations) freezes the CPU at the next FPU instruction until an external
+ * interrupt -- the next 1 ms timer tick.  QuickJS computes with doubles, so
+ * the browser's scripts ran several hundred times slower whenever it was
+ * scheduled on an AP, at 100% CPU, with single-precision results.  QEMU
+ * models neither the freeze nor the precision.  Native #MF reporting (NE),
+ * MP for WAIT, no emulation or task-switch trap, and FNINIT's 0x037F. */
+void cpu_init_fpu(void)
+{
+	unsigned int cr0;
+
+	__asm__ volatile ("mov %%cr0, %0" : "=r"(cr0));
+	cr0 |= (1u << 5) | (1u << 1);           /* NE | MP */
+	cr0 &= ~((1u << 2) | (1u << 3));        /* EM | TS */
+	__asm__ volatile ("mov %0, %%cr0" :: "r"(cr0) : "memory");
+	__asm__ volatile ("fninit");
+}
+
 void cpu_enable_sse(void)
 {
 	unsigned int eax, ebx, ecx, edx, cr4;

+ 2 - 0
kernel/cpu.h

@@ -21,5 +21,7 @@ int cpu_has_sse2(void);
  * does this lazily in cpu_has_sse2(); an AP starts with firmware CR4 and must
  * call this before it can run the SSE2 drawing fast paths. */
 void cpu_enable_sse(void);
+/* Native x87 error reporting and a clean FPU on this CPU (BSP and APs). */
+void cpu_init_fpu(void);
 
 #endif

+ 23 - 3
kernel/dbg.c

@@ -13,6 +13,7 @@
  */
 
 #include "dbg.h"
+#include "paging.h"
 #include "serial.h"
 
 #define DBG_LINE_MAX 64
@@ -131,8 +132,18 @@ static int dbg_command(const char *line)
 			serial_puthex(SERIAL_COM1, addr);
 			serial_putc(SERIAL_COM1, ':');
 			for (i = 0; i < len; i++) {
+				volatile unsigned char *b;
+				void *km;
+
 				serial_putc(SERIAL_COM1, ' ');
-				dbg_putbyte(((volatile unsigned char *)addr)[i]);
+				b = paging_debug_map(addr + i, &km);
+				if (!b) {
+					serial_puts(SERIAL_COM1, "??");
+					continue;
+				}
+				dbg_putbyte(*b);
+				if (km)
+					kunmap(km);
 			}
 			serial_putc(SERIAL_COM1, '\n');
 		}
@@ -144,8 +155,17 @@ static int dbg_command(const char *line)
 		} else {
 			unsigned int addr = args[0];
 			unsigned int i;
-			for (i = 1; i < nargs; i++)
-				((volatile unsigned char *)addr)[i - 1] = (unsigned char)args[i];
+			for (i = 1; i < nargs; i++) {
+				void *km;
+				volatile unsigned char *b =
+					paging_debug_map(addr + i - 1, &km);
+
+				if (!b)
+					break;
+				*b = (unsigned char)args[i];
+				if (km)
+					kunmap(km);
+			}
 			serial_puts(SERIAL_COM1, "ok\n");
 		}
 		return 0;

+ 1 - 1
kernel/dhcp.c

@@ -335,7 +335,7 @@ static int dhcp_wait(const struct netdev *nd, unsigned int xid,
 		unsigned char type;
 		unsigned char ryi[4], rmask[4], rgw[4], rdns[4], rserver[4];
 		unsigned int rlease;
-		int len = nd->rx(rxbuf, sizeof(rxbuf));
+		int len = netdev_rx_frame(nd, rxbuf, sizeof(rxbuf));
 
 		if (len < 0)
 			continue;

+ 2 - 2
kernel/display.c

@@ -71,7 +71,7 @@ void program_display_main(void)
 	put_hex((unsigned int)(unsigned long)vga_fb_ptr());
 	io_putchar('\n');
 	io_putstr("LFB phys ");
-	put_hex((unsigned int)vga_fb_lfb_addr());
+	put_hex(vga_fb_lfb_phys());
 	io_putchar('\n');
 	io_putstr("rendering: ");
 	io_putstr(vga_shadow_is_active() ? "shadow buffer" : "direct");
@@ -113,7 +113,7 @@ void program_display_main(void)
 		}
 	}
 	if (gpu_vendor() == GPU_VENDOR_NVIDIA) {
-		unsigned int lfb = (unsigned int)vga_fb_lfb_addr();
+		unsigned int lfb = vga_fb_lfb_phys();
 		unsigned int bar3 = nvidia_bar3();
 
 		io_putstr("NVIDIA ");

+ 1 - 1
kernel/dns.c

@@ -452,7 +452,7 @@ int dns_resolve(const char *name, unsigned char ip[4])
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
 			if (raw) {
-				int len = nd->rx(netdev_rx_scratch(),
+				int len = netdev_rx_frame(nd, netdev_rx_scratch(),
 						 NETDEV_FRAME_MAX);
 
 				if (len < 0)

+ 6 - 6
kernel/e1000e.c

@@ -7,10 +7,9 @@
  * MDIC.  Everything is polled: interrupts are masked and the ring status
  * bits are checked directly, like the other nebos NIC drivers.
  *
- * Mapped BAR0 (128 KB on the 82577LM) is above the 128 MB identity map, so
- * process_map_mmio_all() installs it in every process page directory: kernel
- * code runs with the current process's CR3 and the network syscalls follow
- * the user process.
+ * BAR0 (128 KB on the 82577LM) is mapped with ioremap(); the window is
+ * shared by every process directory, because kernel code runs with the
+ * current process's CR3 and the network syscalls follow the user process.
  */
 #include "e1000e.h"
 #include "bootcon.h"
@@ -1601,8 +1600,9 @@ static int e1000e_init(void)
 		return -1;
 	}
 
-	process_map_mmio_all(bar_base, bar_size);
-	mmio = (unsigned char *)(unsigned long)bar_base;
+	mmio = ioremap(bar_base, bar_size, IOREMAP_MMIO);
+	if (!mmio)
+		return -1;
 	if (r32(REG_STATUS) == 0xFFFFFFFFu) {
 		dbg("MMIO read failed");
 		mmio = 0;

+ 170 - 15
kernel/ehci.c

@@ -148,6 +148,7 @@ struct ehci_bulk {
 	unsigned int mps;
 	unsigned int buf_len;
 	unsigned int toggle;
+	unsigned int arm_ms;
 	int armed;
 	int sched;
 	struct ehci_ctrl *owner;
@@ -165,12 +166,14 @@ struct ehci_int {
 };
 
 struct ehci_ctrl {
-	unsigned int base;
+	unsigned int base;      /* kernel mapping of the register BAR */
+	unsigned int phys;      /* the BAR itself (reported by lsusb) */
 	unsigned int cap_len;
 	unsigned int bus, dev, func;
 	unsigned int n_ports;
 	int ppc;
 	int last_port;
+	unsigned int cc_ports;  /* ports handed to a companion (bitmask) */
 	unsigned int hcs;
 	unsigned int hcc;
 
@@ -202,6 +205,7 @@ struct ehci_ctrl {
 	int recovering;
 	int sched_on;
 	int sched_refs;
+	unsigned int last_async_ms;	/* last async transfer, for idle IAA */
 	unsigned char addr_port[128];
 };
 
@@ -245,8 +249,13 @@ static void ehci_sched_set(struct ehci_ctrl *c, int on)
 {
 	unsigned int v, t0;
 
-	if (on == c->sched_on)
-		return;
+	if (on == c->sched_on) {
+		/* The software flag can be stale if the controller lost ASE
+		 * (an error or an unexpected reset); re-check the hardware
+		 * before assuming the async schedule is running. */
+		if (!on || (rd32(op_reg(c, EHCI_OP_USBCMD)) & CMD_ASE))
+			return;
+	}
 	v = rd32(op_reg(c, EHCI_OP_USBCMD));
 	if (on) {
 		wr32(op_reg(c, EHCI_OP_USBCMD), v | CMD_ASE);
@@ -462,16 +471,53 @@ static void bulk_configure(struct ehci_bulk *b, unsigned int addr,
 	b->armed = 0;
 }
 
+/* Ask the controller to release the async-schedule state it has cached.
+ * NVIDIA/MCP79 can keep a halted QH's old qTD cached and never fetch the
+ * re-armed one, so the first transfer after an idle gap (a USB disk's first
+ * read after enumeration) stays ACTIVE and times out forever.  Linux and
+ * U-Boot work around this with the IAAD doorbell. */
+static void ehci_iaa_wait(struct ehci_ctrl *c)
+{
+	unsigned int v, t0;
+
+	if (rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_HALT) {
+		serial_puts(SERIAL_COM1, "ehci: controller halted\n");
+		return;
+	}
+	/* Drop a stale acknowledgement before ringing the doorbell. */
+	wr32(op_reg(c, EHCI_OP_USBSTS), STS_IAA);
+	v = rd32(op_reg(c, EHCI_OP_USBCMD));
+	wr32(op_reg(c, EHCI_OP_USBCMD), v | CMD_IAAD);
+	t0 = get_ticks_ms();
+	while (!(rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_IAA)) {
+		if (get_ticks_ms() - t0 > 10) {
+			serial_puts(SERIAL_COM1, "ehci: IAA timeout\n");
+			break;
+		}
+		usb_poll_relax(t0);
+	}
+	wr32(op_reg(c, EHCI_OP_USBSTS), STS_IAA);
+}
+
 static void bulk_arm(struct ehci_bulk *b)
 {
+	unsigned int now;
+
 	if (!b->sched && b->owner) {
 		ehci_sched_get(b->owner);
 		b->sched = 1;
 	}
+	now = get_ticks_ms();
+	if (b->owner && b->owner->last_async_ms &&
+	    now - b->owner->last_async_ms >= 100)
+		ehci_iaa_wait(b->owner);
+	if (b->owner)
+		b->owner->last_async_ms = now;
 	b->qh->token = (b->qh->token & QTD_PING) |
 		       (b->toggle ? QTD_TOGGLE : 0);
 	qh_arm(b->qh, b->qtd);
 	b->armed = 1;
+	b->arm_ms = now;
 }
 
 static int ehci_bulk_out(unsigned int ctrl, unsigned int addr,
@@ -514,7 +560,7 @@ static int ehci_bulk_out(unsigned int ctrl, unsigned int addr,
 	bulk_arm(b);
 
 	if (qtd_wait(last, qtd, 2000) < 0) {
-		bulk_stop(b);
+		bulk_reset(b);
 		return -1;
 	}
 
@@ -523,8 +569,12 @@ static int ehci_bulk_out(unsigned int ctrl, unsigned int addr,
 		ztoken = last->token;
 	bulk_stop(b);
 	if ((token & QTD_ACTIVE) || qtd_error(token) ||
-	    (zlp && ((ztoken & QTD_ACTIVE) || qtd_error(ztoken))))
+	    (zlp && ((ztoken & QTD_ACTIVE) || qtd_error(ztoken)))) {
+		/* Clear the data toggle too: a failed transfer may have
+		 * advanced it or left the endpoint out of sync. */
+		bulk_reset(b);
 		return -1;
+	}
 
 	{
 		unsigned int packets = (len + mps - 1) / mps;
@@ -573,7 +623,10 @@ static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
 
 	b->armed = 0;
 	if (qtd_error(token)) {
-		bulk_stop(b);
+		/* A qTD error also leaves the endpoint's data toggle
+		 * unreliable; drop the state so the next transfer starts at
+		 * DATA0 with a fresh QH/qTD. */
+		bulk_reset(b);
 		return -1;
 	}
 
@@ -596,6 +649,63 @@ static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
 	return (int)actual;
 }
 
+/* Cancel a bulk transfer and clear the endpoint state, including the data
+ * toggle.  A timed-out bulk-IN qTD otherwise stays armed forever and every
+ * later transfer sees it as busy; a BOT reset also leaves the device at
+ * DATA0 while the host would keep its old toggle. */
+static void ehci_bulk_reset(unsigned int ctrl, unsigned int addr,
+			    unsigned int ep, int dir_in)
+{
+	struct ehci_ctrl *c;
+	struct ehci_bulk *b;
+
+	if (ctrl >= n_ctrls)
+		return;
+	c = &ctrls[ctrl];
+	b = dir_in ? &c->bin : &c->bout;
+	if (b->addr == addr && b->ep == ep) {
+		unsigned int token = b->qtd->token;
+
+		if ((token & QTD_ACTIVE) &&
+		    get_ticks_ms() - b->arm_ms > 100) {
+			unsigned int port = b->addr < 128 ?
+				c->addr_port[b->addr] : 0xFF;
+			unsigned int ps = 0;
+
+			if (port < c->n_ports)
+				ps = rd32(port_reg(c, port));
+			serial_puts(SERIAL_COM1, "ehci: stuck bulk ");
+			serial_puts(SERIAL_COM1, dir_in ? "in" : "out");
+			serial_puts(SERIAL_COM1, " sts ");
+			serial_puthex(SERIAL_COM1,
+				      rd32(op_reg(c, EHCI_OP_USBSTS)));
+			serial_puts(SERIAL_COM1, " cmd ");
+			serial_puthex(SERIAL_COM1,
+				      rd32(op_reg(c, EHCI_OP_USBCMD)));
+			serial_puts(SERIAL_COM1, " qtd ");
+			serial_puthex(SERIAL_COM1, token);
+			serial_puts(SERIAL_COM1, " qh ");
+			serial_puthex(SERIAL_COM1, b->qh->token);
+			serial_puts(SERIAL_COM1, " cur ");
+			serial_puthex(SERIAL_COM1, b->qh->current);
+			serial_puts(SERIAL_COM1, " port ");
+			serial_putdec(SERIAL_COM1, port + 1);
+			serial_puts(SERIAL_COM1, " ps ");
+			serial_puthex(SERIAL_COM1, ps);
+			serial_puts(SERIAL_COM1, " addr ");
+			serial_putdec(SERIAL_COM1, b->addr);
+			serial_puts(SERIAL_COM1, " ep ");
+			serial_putdec(SERIAL_COM1, b->ep);
+			serial_puts(SERIAL_COM1, " tgl ");
+			serial_putdec(SERIAL_COM1, b->toggle);
+			serial_putc(SERIAL_COM1, '\n');
+		}
+		if (c->sched_on && (token & QTD_ACTIVE))
+			ehci_iaa_wait(c);
+		bulk_reset(b);
+	}
+}
+
 /* ---- interrupt-IN endpoints ------------------------------------------- */
 static void int_arm(struct ehci_int *in)
 {
@@ -726,6 +836,14 @@ static int ehci_control(struct ehci_ctrl *c, unsigned int addr,
 	ehci_sched_get(c);
 	c->ctl_busy = 1;
 
+	{
+		unsigned int now = get_ticks_ms();
+
+		if (c->last_async_ms && now - c->last_async_ms >= 100)
+			ehci_iaa_wait(c);
+		c->last_async_ms = now;
+	}
+
 	for (i = 0; i < 8; i++)
 		c->setup[i] = setup[i];
 
@@ -856,6 +974,27 @@ static int ehci_port_take_change(unsigned int ctrl, unsigned int port)
 	return 1;
 }
 
+/* HCSPARAMS N_CC: the number of companion controllers. */
+static unsigned int n_companions(const struct ehci_ctrl *c)
+{
+	return (c->hcs >> 12) & 0x0Fu;
+}
+
+/* Give a non-high-speed device to the companion controller. */
+static void port_to_companion(struct ehci_ctrl *c, unsigned int port,
+			      unsigned int ps)
+{
+	wr32(port_reg(c, port),
+	     (ps & ~(PORT_RWC | PORT_PE | PORT_RESET)) | PORT_OWNER |
+	     PORT_CSC | PORT_PEC | PORT_OCC);
+	if (!(c->cc_ports & (1u << port))) {
+		c->cc_ports |= 1u << port;
+		serial_puts(SERIAL_COM1, "ehci: port ");
+		serial_putdec(SERIAL_COM1, port + 1);
+		serial_puts(SERIAL_COM1, " not high-speed, handed to companion\n");
+	}
+}
+
 static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 {
 	struct ehci_ctrl *c;
@@ -894,8 +1033,11 @@ static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 	   low/full-speed device belongs there; a high-speed one can be taken
 	   over by clearing PORT_OWNER (the controller may need more than one
 	   write while the port is enabled). */
+	/* The hardware returns a port to the EHCI when its device goes away. */
+	if (!(ps & PORT_OWNER))
+		c->cc_ports &= ~(1u << port);
 	if (ps & PORT_OWNER) {
-		if (PORT_USB11(ps))
+		if (PORT_USB11(ps) || (c->cc_ports & (1u << port)))
 			return -1;
 		for (try = 0; try < 8 && (ps & PORT_OWNER); try++) {
 			wr32(port_reg(c, port),
@@ -909,6 +1051,13 @@ static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 			return -1;
 	}
 
+	/* A low-speed device (K state on an unenabled port) is never reset by
+	   the EHCI: hand it straight to the companion (EHCI 4.2.2). */
+	if (!(ps & PORT_PE) && PORT_USB11(ps) && n_companions(c)) {
+		port_to_companion(c, port, ps);
+		return -1;
+	}
+
 	port_endpoints_reset(c, port);
 	c->last_port = (int)port;
 
@@ -967,11 +1116,13 @@ static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 	if (!(ps & PORT_PE)) {
 		if (ps & PORT_OWNER)
 			return -1;
-		if (PORT_USB11(ps)) {
-			/* Low/full speed: make sure the companion owns it. */
-			wr32(port_reg(c, port),
-			     (ps & ~(PORT_RWC | PORT_PE)) | PORT_OWNER |
-			     PORT_CSC | PORT_PEC | PORT_OCC);
+		/* Not enabled after the reset: the device is not high-speed
+		   (EHCI 4.2.2), whatever the line state reads -- a full-speed
+		   receiver on the MCP79 reports SE0 here.  It belongs to the
+		   companion.  Only a controller without companions (Intel PCH
+		   rate-matching hubs) can have a genuinely stuck HS reset. */
+		if (n_companions(c) || PORT_USB11(ps)) {
+			port_to_companion(c, port, ps);
 			return -1;
 		}
 		serial_puts(SERIAL_COM1, "ehci: reset no PED ");
@@ -1197,12 +1348,15 @@ static int ehci_setup_one(unsigned int bus, unsigned int dev,
 		return -1;
 	c = &ctrls[n_ctrls];
 	zero_ctrl(c);
-	c->base = bar;
+	c->phys = bar;
 	c->bus = bus;
 	c->dev = dev;
 	c->func = func;
 
-	paging_map_mmio(bar, 0x1000);
+	bar = (unsigned int)ioremap(bar, 0x1000, IOREMAP_MMIO);
+	if (!bar)
+		return -1;
+	c->base = bar;
 
 	if (rd32(bar) == 0xFFFFFFFFu)
 		return -1;
@@ -1324,7 +1478,7 @@ static void ehci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
 	*bus = c->bus;
 	*dev = c->dev;
 	*func = c->func;
-	*base = c->base;
+	*base = c->phys;
 	*mmio = 1;
 }
 
@@ -1390,5 +1544,6 @@ const struct usb_hcd_ops usb_ehci_ops = {
 	ehci_int_in_stop,
 	ehci_bulk_out,
 	ehci_bulk_in,
+	ehci_bulk_reset,
 	ehci_print_ports,
 };

+ 12 - 5
kernel/elf.c

@@ -84,15 +84,21 @@ unsigned int elf_load(unsigned int cr3, const void *image, unsigned int size)
 			unsigned int pva = pbase + p * 0x1000;
 			unsigned int phys = paging_page_phys(cr3, pva);
 			unsigned int start, end, dst, src, n;
+			unsigned char *page;
 
 			if (!phys) {
-				phys = mm_alloc_page();
+				phys = mm_alloc_user_page();
 				if (!phys)
 					return 0;
-				memzero((void *)phys, 0x1000);
 				if (!paging_map_page(cr3, pva, phys,
-						     PTE_USER | PTE_WRITABLE | PTE_PRESENT))
+						     PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
+					mm_free_page(phys);
 					return 0;
+				}
+				page = kmap(phys);
+				memzero(page, 0x1000);
+			} else {
+				page = kmap(phys);
 			}
 
 			/* Copy the file-backed part that falls in this page. */
@@ -104,7 +110,7 @@ unsigned int elf_load(unsigned int cr3, const void *image, unsigned int size)
 				dst = start - pva;
 				src = off + (start - va);
 				n = end - start;
-				memcopy((void *)(phys + dst),
+				memcopy(page + dst,
 					(const char *)image + src, n);
 			}
 
@@ -116,8 +122,9 @@ unsigned int elf_load(unsigned int cr3, const void *image, unsigned int size)
 			if (end > pva + 0x1000)
 				end = pva + 0x1000;
 			if (end > start)
-				memzero((void *)(phys + (start - pva)),
+				memzero(page + (start - pva),
 					end - start);
+			kunmap(page);
 		}
 	}
 

+ 6 - 4
kernel/intel.c

@@ -62,7 +62,7 @@
 #define GTT_PTE_VALID    0x1u
 #define GTT_PTE_UNCACHED 0x0u
 
-static unsigned int intel_mmio;
+static unsigned int intel_mmio;   /* kernel mapping of BAR0 */
 static volatile unsigned int *intel_gtt;
 static unsigned int intel_gtt_entries;
 static unsigned int intel_gtt_limit;
@@ -272,8 +272,10 @@ void intel_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 	if (bar0_size > 0x1000000)
 		bar0_size = 0x1000000;
 
-	intel_mmio = intel_bar_base(bar0);
-	paging_map_mmio(intel_mmio, bar0_size);
+	intel_mmio = (unsigned int)ioremap(intel_bar_base(bar0), bar0_size,
+					   IOREMAP_MMIO);
+	if (!intel_mmio)
+		return;
 
 	pgetbl = mmio_read(INTEL_PGETBL_CTL);
 	if (pgetbl == 0xFFFFFFFFu) {
@@ -295,7 +297,7 @@ void intel_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 	intel_gtt_limit = intel_gtt_entries;
 	if (bar2_size >= 0x1000000 && bar2_size / 4096 < intel_gtt_limit)
 		intel_gtt_limit = bar2_size / 4096;
-	lfb = vga_fb_lfb_addr();
+	lfb = vga_fb_lfb_phys();
 	if (bar2_base && lfb >= bar2_base)
 		lfb_gtt = (lfb - bar2_base) & 0xFFFFF000u;
 

+ 3 - 2
kernel/ioapic.c

@@ -49,8 +49,9 @@ void ioapic_mask_all(void)
 
 		if (!addr)
 			continue;
-		paging_map_mmio(addr, 0x1000);
-		base = (volatile unsigned int *)addr;
+		base = ioremap(addr, 0x1000, IOREMAP_MMIO);
+		if (!base)
+			continue;
 
 		ver = ioapic_read(base, IOAPIC_VER);
 		if (ver == 0xFFFFFFFFu)

+ 5 - 4
kernel/iwl_trans.c

@@ -505,10 +505,11 @@ int iwl_probe(void)
 						(cmd & 0xffffu) | 0x0006u | 0x0400u);
 				/* PCI_CFG_RETRY_TIMEOUT (0x41) = 0, like Linux. */
 				pci_cfg_write8(bus, dev, func, 0x41, 0);
-				/* Map the 8 KB CSR window in every address space
-				 * (probe may run after processes exist). */
-				process_map_mmio_all(bar, 0x2000);
-				S->mmio = (volatile unsigned char *)bar;
+				/* The 8 KB CSR window (the ioremap window is
+				 * shared by every address space). */
+				S->mmio = ioremap(bar, 0x2000, IOREMAP_MMIO);
+				if (!S->mmio)
+					return -1;
 				iwl_wr(CSR_INT_MASK, 0);
 				S->hw_rev = iwl_rd(CSR_HW_REV);
 				serial_puts(SERIAL_COM1, "iwl: ");

+ 20 - 9
kernel/kernel.c

@@ -402,14 +402,23 @@ static void boot_wifi_autoconnect(void)
  * (floppy motor, ATA/AHCI timeouts) go through vfs_scan_relax() and the WM
  * and the first terminal run meanwhile.  Disk file operations wait for
  * vfs_scan_busy; /ramfs and /share do not.  The WiFi autoconnect needs
- * /data, so it runs here after the mount. */
+ * /data, so it runs here after the mount.  Every COM1 character this thread
+ * writes is also captured into the bootcon scan ring, which the ring-3
+ * `bootscan` window tails live (it is spawned before the first terminal). */
 static volatile int boot_dhcp_done;
 
 static void boot_storage_main(void)
 {
-	vfs_mount_all();
+	bootcon_scan_begin();
+	vfs_mount_all(1);
 	sys_mount_load_config();
 	boot_trace("storage");
+	/* USB storage can NAK its first read for seconds while a card reader or
+	 * stick initialises.  Close the live scan first, then mount it behind
+	 * the usable desktop; the boot window no longer waits for it. */
+	bootcon_scan_end();
+	vfs_mount_usb();
+	sys_mount_load_config();
 	/* As before, WiFi only starts after the boot DHCP attempt on the
 	 * wired NIC, so two DHCP clients never overlap. */
 	while (!boot_dhcp_done)
@@ -463,6 +472,7 @@ void kernel_main(void)
 	/* paging_init only ORs in PSE; make sure the BSP's SSE state is on
 	 * before any drawing path can select an SSE2 helper. */
 	cpu_enable_sse();
+	cpu_init_fpu();
 	/* ACPI needs paging for table access; a missing or unusable MADT simply
 	 * leaves the machine at one CPU. */
 	acpi_init();
@@ -589,8 +599,9 @@ void kernel_main(void)
 	/* Publish the storage scan before anything can run a command: disk
 	 * paths wait for vfs_scan_busy (owned by the idle context until the
 	 * bootscan thread's vfs_mount_all() takes it over), /ramfs and /share
-	 * do not.  Then let the WM spawn the first terminal at once; neither
-	 * it nor the WM needs the NIC probe or the disks below. */
+	 * do not.  Then let the WM spawn the bootscan window and the first
+	 * terminal at once; neither it nor the WM needs the NIC probe or the
+	 * disks below. */
 	vfs_scan_owner_pid = 0;
 	vfs_scan_busy = 1;
 #ifndef NOWM
@@ -601,10 +612,10 @@ void kernel_main(void)
 #endif
 
 #ifndef NOWM
-	/* Let the WM put the first terminal on screen before the USB bring-up
-	 * below: kernel_main is the idle context, so halting here hands the
-	 * CPU to the WM and the new terminal.  Bounded, in case no terminal
-	 * ever presents (missing /ramfs/bin/term). */
+	/* Let the WM put the bootscan log window and the first terminal on
+	 * screen before the USB bring-up below: kernel_main is the idle
+	 * context, so halting here hands the CPU to the WM and those windows.
+	 * Bounded, in case no terminal ever presents. */
 	{
 		unsigned int t0 = get_ticks_ms();
 
@@ -656,7 +667,7 @@ void kernel_main(void)
 	boot_trace("storage begin");
 	storage_inline = 0;
 	if (process_create("bootscan", boot_storage_main) <= 0) {
-		vfs_mount_all();
+		vfs_mount_all(0);
 		boot_trace("storage");
 		storage_inline = 1;
 	}

+ 2 - 1
kernel/lapic.c

@@ -1,6 +1,7 @@
 /* Local APIC driver.  The MMIO window must already be mapped by smp.c. */
 
 #include "lapic.h"
+#include "paging.h"
 #include "timer.h"
 
 #define LAPIC_ID        0x020
@@ -49,7 +50,7 @@ static inline void wrmsr(unsigned int msr, unsigned long long value)
 void lapic_set_base(unsigned int base)
 {
 	lapic_phys = base;
-	lapic_mmio = (volatile unsigned int *)base;
+	lapic_mmio = ioremap(base, 0x1000, IOREMAP_MMIO);
 }
 
 unsigned int lapic_base(void)

+ 126 - 24
kernel/mm.c

@@ -4,15 +4,22 @@
 #include "bootinfo.h"
 #include "serial.h"
 #include "vga.h"
+#include "mtrr.h"
 
 /* One bit per 4 KB page; bit set = in use.
  *
- * The bitmap covers [0, top of RAM) rounded up to 128 KB, at most
- * MM_MAX_PHYS.  It is not in BSS (1 GB needs 32 KB, and the kernel image
- * must end below 0x90000): mm_init() places it in the first E820 RAM run
- * above the VGA-shadow reserve that avoids the boot rootfs pack and the
- * LFB, then marks those pages used.  The small static array is only the
- * fallback when no E820 map exists (the 16 MB guess) or nothing fits. */
+ * The bitmap covers [0, top of RAM) rounded up to 128 KB, at most 4 GB
+ * (MM_HIGH_LIMIT).  It is not in BSS (4 GB needs 128 KB, and the kernel
+ * image must end below 0x90000): mm_init() places it in the first lowmem
+ * E820 RAM run above the VGA-shadow reserve that avoids the boot rootfs
+ * pack and the LFB, then marks those pages used.  The small static array is
+ * only the fallback when no E820 map exists (the 16 MB guess) or nothing
+ * fits.
+ *
+ * Two zones share the bitmap: lowmem [0, MM_MAX_PHYS) is identity-mapped
+ * and serves every kernel allocation; highmem above it is handed out only
+ * by mm_alloc_user_page() for ring-3 pages, which the kernel reaches
+ * through kmap() (paging.c).  Each zone keeps its own first-free hint. */
 #define MM_FALLBACK_PHYS   0x1000000u      /* 16 MB */
 #define MM_FALLBACK_WORDS  (MM_FALLBACK_PHYS / PAGE_SIZE / 32)
 #define MM_PLACE_MIN       (MM_SHADOW_BASE + MM_SHADOW_RESERVE)
@@ -22,7 +29,10 @@ static unsigned int *bitmap = fallback_bitmap;
 static unsigned int bitmap_words;   /* words covered by the bitmap */
 static unsigned int bitmap_bits;    /* bitmap_words * 32 */
 static unsigned int bitmap_bytes;   /* page-rounded size when placed in RAM */
-static unsigned int first_free;     /* lowest word that may hold a free bit */
+static unsigned int low_words;      /* bitmap words below MM_MAX_PHYS */
+static unsigned int first_free;     /* lowest lowmem word with a free bit */
+static unsigned int first_free_high; /* same for highmem (>= low_words) */
+unsigned int mm_uncached_high_mb, mm_uncached_low_mb;
 static unsigned int free_pages;
 static unsigned int total_ram_kb;
 
@@ -124,7 +134,7 @@ static void find_boot_regions(void)
 	sz = (unsigned int)fb->pitch * fb->height;
 	if (!sz)
 		sz = 0x1000000;
-	if (fb->valid && fb->lfb_base && fb->lfb_base < MM_MAX_PHYS) {
+	if (fb->valid && fb->lfb_base) {
 		lfb_lo = fb->lfb_base;
 		lfb_hi = lfb_lo + sz;
 	}
@@ -193,8 +203,8 @@ void mm_init(void)
 
 			if (e->type != E820_RAM || e->length == 0)
 				continue;
-			if (end > MM_MAX_PHYS)
-				end = MM_MAX_PHYS;
+			if (end > MM_HIGH_LIMIT)
+				end = MM_HIGH_LIMIT;
 			if (end > e->base) {
 				total_ram_kb +=
 					(unsigned int)((end - e->base) >> 10);
@@ -225,6 +235,9 @@ void mm_init(void)
 		}
 	}
 	bitmap_bits = bitmap_words * 32u;
+	low_words = MM_MAX_PHYS / (32u * PAGE_SIZE);
+	if (low_words > bitmap_words)
+		low_words = bitmap_words;
 
 	/* Everything starts in-use. */
 	for (i = 0; i < bitmap_words; i++)
@@ -267,6 +280,50 @@ void mm_init(void)
 	if (bitmap_bytes)
 		mark_used((unsigned int)bitmap, bitmap_bytes);
 
+	/* RAM the firmware's MTRRs leave uncached.  A BIOS with too few
+	 * variable ranges for a 3-4 GB board (the ION) can leave part of the
+	 * RAM above the PCI hole UC; ring-3 heaps come from highmem first, so
+	 * a page load's JavaScript then ran 100x slower.  Such highmem is not
+	 * handed out at all (lowmem still serves user pages); uncached lowmem
+	 * is only reported, the kernel cannot do without it. */
+	{
+		unsigned long long a, lim = bitmap_limit();
+		unsigned int high_uc = 0, low_uc = 0, w;
+
+		if (lim > MM_HIGH_LIMIT)
+			lim = MM_HIGH_LIMIT;
+		for (a = 0x100000ull; a < lim; a += 0x100000ull) {
+			unsigned int w0 = (unsigned int)(a >> 17);
+			int any = 0, t;
+
+			for (w = w0; w < w0 + 8 && w < bitmap_words; w++)
+				if (bitmap[w] != 0xFFFFFFFFu)
+					any = 1;
+			if (!any)
+				continue;       /* no free RAM in this MB */
+			t = mtrr_mem_type((unsigned int)a);
+			if (t < 0)
+				break;          /* no usable MTRRs */
+			if (t == 6)
+				continue;       /* write-back */
+			if (a >= MM_MAX_PHYS) {
+				mark_used(a, 0x100000ull);
+				high_uc++;
+			} else {
+				low_uc++;
+			}
+		}
+		if (high_uc || low_uc) {
+			serial_puts(SERIAL_COM1, "mm: RAM not write-back by MTRR: ");
+			serial_putdec(SERIAL_COM1, high_uc);
+			serial_puts(SERIAL_COM1, " MB highmem (not used), ");
+			serial_putdec(SERIAL_COM1, low_uc);
+			serial_puts(SERIAL_COM1, " MB lowmem\n");
+		}
+		mm_uncached_high_mb = high_uc;
+		mm_uncached_low_mb = low_uc;
+	}
+
 	/* Count free pages. */
 	free_pages = 0;
 	for (i = 0; i < bitmap_words; i++) {
@@ -278,6 +335,7 @@ void mm_init(void)
 		}
 	}
 	first_free = 0;
+	first_free_high = low_words;
 
 	serial_puts(SERIAL_COM1, "mm: bitmap ");
 	serial_putdec(SERIAL_COM1, bitmap_words * 4u);
@@ -285,17 +343,18 @@ void mm_init(void)
 	serial_putdec(SERIAL_COM1, bitmap_bits / 256u);
 	serial_puts(SERIAL_COM1, " MB at ");
 	serial_puthex(SERIAL_COM1, (unsigned int)bitmap);
-	serial_puts(SERIAL_COM1, "\n");
+	serial_puts(SERIAL_COM1, ", highmem free ");
+	serial_putdec(SERIAL_COM1, mm_free_high_pages() / 256u);
+	serial_puts(SERIAL_COM1, " MB\n");
 }
 
-/* Lowest free page.  Words below first_free are known to be full, so a
- * steady stream of allocations does not rescan the low, busy part. */
-unsigned int mm_alloc_page(void)
+/* Scan words [from, to) for the lowest free bit; claim it. */
+static unsigned int claim_bit(unsigned int from, unsigned int to,
+			      unsigned int *hint)
 {
 	unsigned int w;
-	unsigned int flags = irq_disable_save();
 
-	for (w = first_free; w < bitmap_words; w++) {
+	for (w = from; w < to; w++) {
 		unsigned int word = bitmap[w];
 
 		if (word != 0xFFFFFFFFu) {
@@ -303,17 +362,56 @@ unsigned int mm_alloc_page(void)
 
 			bitmap[w] = word | (1u << i);
 			free_pages--;
-			first_free = w;
-			irq_restore(flags);
+			*hint = w;
 			return (w * 32u + i) * PAGE_SIZE;
 		}
 	}
-	first_free = bitmap_words;
-	irq_restore(flags);
+	*hint = to;
 	return 0;
 }
 
-/* Lowest run of n free pages.  Full words break a run and empty words
+/* Lowest free page.  Words below first_free are known to be full, so a
+ * steady stream of allocations does not rescan the low, busy part. */
+unsigned int mm_alloc_page(void)
+{
+	unsigned int flags = irq_disable_save();
+	unsigned int pa = claim_bit(first_free, low_words, &first_free);
+
+	irq_restore(flags);
+	return pa;
+}
+
+/* A frame for a ring-3 mapping: highmem first, lowmem when highmem is
+ * exhausted (or absent).  The result may be above MM_MAX_PHYS, so the
+ * kernel must not dereference it directly; use kmap(). */
+unsigned int mm_alloc_user_page(void)
+{
+	unsigned int flags = irq_disable_save();
+	unsigned int pa = claim_bit(first_free_high, bitmap_words,
+				    &first_free_high);
+
+	if (!pa)
+		pa = claim_bit(first_free, low_words, &first_free);
+	irq_restore(flags);
+	return pa;
+}
+
+unsigned int mm_free_high_pages(void)
+{
+	unsigned int w, n = 0;
+
+	for (w = low_words; w < bitmap_words; w++) {
+		unsigned int x = ~bitmap[w];
+
+		while (x) {
+			x &= x - 1;
+			n++;
+		}
+	}
+	return n;
+}
+
+/* Lowest run of n free lowmem pages.  Full words break a run and empty words
  * extend it 32 pages at a time; only mixed words are walked bit by bit. */
 unsigned int mm_alloc_pages(unsigned int n)
 {
@@ -326,7 +424,7 @@ unsigned int mm_alloc_pages(unsigned int n)
 
 	flags = irq_disable_save();
 	p = first_free * 32u;
-	while (p < bitmap_bits) {
+	while (p < low_words * 32u) {
 		if ((p & 31u) == 0) {
 			unsigned int word = bitmap[p / 32u];
 
@@ -382,8 +480,12 @@ void mm_free_page(unsigned int addr)
 	if (bit_test(idx)) {
 		bit_clear(idx);
 		free_pages++;
-		if (idx / 32u < first_free)
-			first_free = idx / 32u;
+		if (idx / 32u < low_words) {
+			if (idx / 32u < first_free)
+				first_free = idx / 32u;
+		} else if (idx / 32u < first_free_high) {
+			first_free_high = idx / 32u;
+		}
 	}
 	irq_restore(flags);
 }

+ 15 - 3
kernel/mm.h

@@ -18,7 +18,8 @@
 #define E820_MAGIC       0xE820E820
 #define E820_MAX_ENTRIES 32
 
-/* Frame allocator covers physical RAM [0, MM_MAX_PHYS).  Bitmap: 1 bit/page.
+/* Frame allocator covers physical RAM [0, MM_HIGH_LIMIT): lowmem below
+ * MM_MAX_PHYS, highmem above it.  Bitmap: 1 bit/page.
  * The ceiling lives in phys.h so the UEFI loader cannot drift; see
  * kernel.ld for the loaded-image bound. */
 
@@ -47,14 +48,25 @@ struct e820_map {
 
 void mm_init(void);
 
-/* Return the base address of a free 4 KB page, or 0 if none. */
+/* Return the base address of a free 4 KB lowmem page (identity-mapped,
+ * below MM_MAX_PHYS), or 0 if none. */
 unsigned int mm_alloc_page(void);
+
+/* A frame for a ring-3 mapping, preferring highmem; 0 if none.  It may lie
+ * above MM_MAX_PHYS: only touch it through kmap() or the user mapping. */
+unsigned int mm_alloc_user_page(void);
+
+/* Free pages above MM_MAX_PHYS (a slow count, for reporting). */
+unsigned int mm_free_high_pages(void);
+/* RAM the firmware's MTRRs leave non-write-back, in MB (mm_init); the
+ * highmem part is never handed out. */
+extern unsigned int mm_uncached_high_mb, mm_uncached_low_mb;
 void mm_free_page(unsigned int addr);
 
 /* Free a page-aligned boot-time reservation (never low 1 MB / the shadow). */
 void mm_free_range(unsigned int base, unsigned int size);
 
-/* Return n contiguous pages (base address), or 0 if unavailable. */
+/* Return n contiguous lowmem pages (base address), or 0 if unavailable. */
 unsigned int mm_alloc_pages(unsigned int n);
 
 unsigned int mm_free_pages(void);

+ 39 - 0
kernel/mtrr.c

@@ -121,6 +121,45 @@ static const char *type_name(unsigned int t)
 	}
 }
 
+/* Effective variable-range MTRR memory type of one physical address below
+ * 4 GB (above the fixed ranges): UC wins over everything, WT over WB, and
+ * MTRRdefType applies where no entry matches.  -1 when the CPU has no MTRRs
+ * or they are disabled (then nothing can be learned from them). */
+int mtrr_mem_type(unsigned int phys)
+{
+	unsigned int lo, hi, mlo, mhi, def, vcnt, i;
+	int type = -1;
+
+	if (!cpu_has_mtrr())
+		return -1;
+	rdmsr(MSR_MTRR_DEF_TYPE, &def, &hi);
+	if (!(def & MTRR_VALID))
+		return -1;
+	rdmsr(MSR_MTRRCAP, &vcnt, &hi);
+	vcnt &= 0xFF;
+	if (vcnt > 16)
+		vcnt = 16;
+	for (i = 0; i < vcnt; i++) {
+		int t;
+
+		rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
+		rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
+		if (!(mlo & MTRR_VALID))
+			continue;
+		/* The address's bits 32+ are zero: a base with high bits
+		 * set under the mask never matches. */
+		if ((hi & mhi) != 0 ||
+		    ((phys ^ lo) & mlo & 0xFFFFF000u) != 0)
+			continue;
+		t = (int)(lo & 0xFF);
+		if (t == 0)
+			return 0;
+		if (type < 0 || (type == 6 && t == 4))
+			type = t;
+	}
+	return type < 0 ? (int)(def & 0xFF) : type;
+}
+
 /* Dump MTRRdefType and the variable entries to serial. */
 static void dump_mtrrs(unsigned int vcnt)
 {

+ 4 - 0
kernel/mtrr.h

@@ -11,4 +11,8 @@
 int mtrr_set_wc(unsigned int base, unsigned int size,
 		unsigned int *wc_base, unsigned int *wc_size);
 
+/* Effective MTRR type of a physical address (0 UC, 1 WC, 4 WT, 5 WP,
+ * 6 WB), or -1 without usable MTRRs. */
+int mtrr_mem_type(unsigned int phys);
+
 #endif

+ 3 - 3
kernel/net.c

@@ -294,7 +294,7 @@ int net_udp_send_from_dev(const struct netdev *nd,
 	for (i = 0; i < len; i++)
 		udp[8 + i] = p[i];
 
-	return nd->tx(udp_frame, 14 + total);
+	return netdev_tx_frame(nd, udp_frame, 14 + total);
 }
 
 int net_udp_send_from(const unsigned char *src_ip,
@@ -552,7 +552,7 @@ int net_arp_resolve(const unsigned char *dst_ip, unsigned char mac[6])
 		 * alive even though the syscall gate entered with IF=0. */
 		if (!raw)
 			flags = net_irq_enter();
-		tx = nd->tx(req, reqlen);
+		tx = netdev_tx_frame(nd, req, reqlen);
 		if (!raw)
 			net_irq_exit(flags);
 		if (tx != 0) {
@@ -564,7 +564,7 @@ int net_arp_resolve(const unsigned char *dst_ip, unsigned char mac[6])
 		while ((int)(get_ticks_ms() - deadline) < 0) {
 			if (raw) {
 				unsigned char spa[4], sha[6];
-				int len = nd->rx(netdev_rx_scratch(),
+				int len = netdev_rx_frame(nd, netdev_rx_scratch(),
 						 NETDEV_FRAME_MAX);
 
 				if (len < 0)

+ 2 - 2
kernel/netcore.c

@@ -106,7 +106,7 @@ int net_pump(unsigned int budget)
 	flags = net_irq_enter();
 
 	for (n = 0; n < budget; n++) {
-		int len = nd->rx(rx, NETDEV_FRAME_MAX);
+		int len = netdev_rx_frame(nd, rx, NETDEV_FRAME_MAX);
 
 		if (len < 0)
 			break;
@@ -124,7 +124,7 @@ int net_pump(unsigned int budget)
 						   reply_buf, sizeof(reply_buf));
 
 			if (rlen > 0)
-				nd->tx(reply_buf, (unsigned int)rlen);
+				netdev_tx_frame(nd, reply_buf, (unsigned int)rlen);
 		}
 	}
 	net_irq_exit(flags);

+ 39 - 0
kernel/netdev.c

@@ -35,11 +35,50 @@ static unsigned int owner_pid;   /* process that took the NIC (busy > 0) */
  * command via netdev_acquire()). */
 static unsigned char rx_scratch[NETDEV_FRAME_MAX];
 
+/* Kernel-wide frame byte counters behind SYS_NET_INFO; every driver feeds
+ * them.  Only the Ethernet header and payload are counted, a failed send adds
+ * nothing and a zero-length receive leaves them alone. */
+static unsigned int rx_total, tx_total;
+
 unsigned char *netdev_rx_scratch(void)
 {
 	return rx_scratch;
 }
 
+int netdev_rx_frame(const struct netdev *nd, unsigned char *buf,
+		    unsigned int maxlen)
+{
+	int len;
+
+	if (!nd || !nd->rx)
+		return -1;
+	len = nd->rx(buf, maxlen);
+	if (len > 0)
+		rx_total += (unsigned int)len;
+	return len;
+}
+
+int netdev_tx_frame(const struct netdev *nd, const unsigned char *buf,
+		    unsigned int len)
+{
+	int ret;
+
+	if (!nd || !nd->tx)
+		return -1;
+	ret = nd->tx(buf, len);
+	if (ret == 0)
+		tx_total += len;
+	return ret;
+}
+
+void netdev_byte_counters(unsigned int *rx, unsigned int *tx)
+{
+	if (rx)
+		*rx = rx_total;
+	if (tx)
+		*tx = tx_total;
+}
+
 void netdev_acquire(void)
 {
 	if (busy == 0 && current_proc())

+ 11 - 0
kernel/netdev.h

@@ -89,6 +89,17 @@ const char *netdev_chip(const struct netdev *nd);
 /* Fill the active NIC's MAC; -1 (and a zero MAC) when there is no NIC. */
 int netdev_get_mac(unsigned char mac[6]);
 
+/* Frame I/O with the kernel-wide byte counters.  Every receive and send goes
+ * through these wrappers instead of calling nd->rx()/nd->tx() directly, so
+ * SYS_NET_INFO can report the totals to the process monitor.  The counters
+ * cover every NIC (including the management link), include the Ethernet
+ * header and wrap at 32 bits. */
+int netdev_rx_frame(const struct netdev *nd, unsigned char *buf,
+		    unsigned int maxlen);
+int netdev_tx_frame(const struct netdev *nd, const unsigned char *buf,
+		    unsigned int len);
+void netdev_byte_counters(unsigned int *rx, unsigned int *tx);
+
 /* One shared frame-receive scratch buffer (1518 bytes) for protocol code that
  * just needs somewhere for nd->rx() to land.  Only valid while the caller
  * owns the NIC, and only until the next rx().  Shared so the DNS/TCP/ARP

+ 4 - 4
kernel/netkexec.c

@@ -154,7 +154,7 @@ static int netkexec_poll_nd(const struct netdev *nd, int capture)
 	unsigned int n;
 
 	for (n = 0; n < 8; n++) {
-		int len = nd->rx(rxbuf, sizeof(rxbuf));
+		int len = netdev_rx_frame(nd, rxbuf, sizeof(rxbuf));
 
 		if (len < 0)
 			break;
@@ -176,7 +176,7 @@ static int netkexec_poll_nd(const struct netdev *nd, int capture)
 						       replybuf,
 						       sizeof(replybuf));
 			if (rlen > 0)
-				nd->tx(replybuf, (unsigned int)rlen);
+				netdev_tx_frame(nd, replybuf, (unsigned int)rlen);
 		}
 	}
 	return 0;
@@ -326,7 +326,7 @@ static int netkexec_recv(unsigned int stage_base)
 			continue;
 		}
 
-		len = nd->rx(rxbuf, sizeof(rxbuf));
+		len = netdev_rx_frame(nd, rxbuf, sizeof(rxbuf));
 		if (len < 0) {
 			/* Yield so the WM and terminal keep running while we
 			 * wait for the next datagram; this used to busy-spin
@@ -352,7 +352,7 @@ static int netkexec_recv(unsigned int stage_base)
 							       replybuf,
 							       sizeof(replybuf));
 				if (rlen > 0)
-					nd->tx(replybuf, (unsigned int)rlen);
+					netdev_tx_frame(nd, replybuf, (unsigned int)rlen);
 			}
 			/* Yield after a frame that was not ours (ARP/ICMP or
 			 * a stray datagram), so a broadcast storm cannot keep

+ 1 - 1
kernel/ntp.c

@@ -214,7 +214,7 @@ int ntp_sync(const char *server, unsigned int timeout_ms,
 			return SYS_NTP_ETX;
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
-			int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
+			int len = netdev_rx_frame(nd, rxbuf, NETDEV_FRAME_MAX);
 
 			if (len < 0)
 				continue;

+ 1 - 1
kernel/nv50.c

@@ -82,7 +82,7 @@ void nv50_probe(void)
 
 	if (!nvidia_available())
 		return;
-	nv_mmio = nvidia_bar0();
+	nv_mmio = nvidia_regs();
 
 	log_hex("nv50: pdobj ", mmio_rd(NV_PDISP_OBJECTS));
 	log_hex(" cursor2 ", mmio_rd(NV_PDISP_CURSOR_CTRL2(0)));

+ 23 - 13
kernel/nvidia.c

@@ -32,6 +32,7 @@ static unsigned int nv_present;      /* BAR0 mapped + PMC_BOOT_0 sane */
 static unsigned int nv_boot0;
 static unsigned int nv_chipset;
 static unsigned int nv_bar0;
+static unsigned int nv_regs;   /* kernel mapping of BAR0 */
 static unsigned int nv_bar1;
 static unsigned int nv_bar1_size;
 static unsigned int nv_bar3;
@@ -115,12 +116,10 @@ void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 	bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
 	if (!(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
 		nv_bar0 = bar0 & 0xFFFFFFF0;
-		/* Map into every process page directory: gpu_init() runs
-		 * before the first process exists, but drivers may run in a
-		 * terminal context later (same reason r8168 does this). */
-		process_map_mmio_all(nv_bar0, NV_BAR0_SIZE);
+		nv_regs = (unsigned int)ioremap(nv_bar0, NV_BAR0_SIZE,
+						IOREMAP_MMIO);
 
-		boot0 = *(volatile unsigned int *)nv_bar0;
+		boot0 = nv_regs ? *(volatile unsigned int *)nv_regs : 0;
 		if (boot0 != 0 && boot0 != 0xFFFFFFFF) {
 			nv_boot0 = boot0;
 			nv_chipset = (boot0 >> 20) & 0xFF;
@@ -139,9 +138,9 @@ void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 			nv_bar1 = b1 & 0xFFFFFFF0;
 		if (nv_bar1) {
 			nv_bar1_size = nv_bar_size(bus, dev, 0x14, b1 & 4);
-			if (nv_bar1_size > 0x10000000u)   /* cap the mapping */
+			if (nv_bar1_size > 0x10000000u)   /* cap the size */
 				nv_bar1_size = 0x10000000u;
-			process_map_mmio_all(nv_bar1, nv_bar1_size);
+			/* Not mapped: nothing dereferences BAR1. */
 		}
 
 		b3 = pci_cfg_read32(bus, dev, 0, 0x1C);
@@ -152,7 +151,8 @@ void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 			nv_bar3_size = nv_bar_size(bus, dev, 0x1C, b3 & 4);
 			if (nv_bar3_size > 0x04000000u)
 				nv_bar3_size = 0x04000000u;
-			process_map_mmio_all(nv_bar3, nv_bar3_size);
+			/* Not mapped: the VBE LFB inside it has its own
+			 * mapping (paging_lfb_va()). */
 		}
 	}
 
@@ -162,8 +162,8 @@ void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 	 * the display engine's VRAM address for the LFB. */
 	if (nv_present && (nv_chipset == 0xaa || nv_chipset == 0xac ||
 			   nv_chipset == 0xaf)) {
-		unsigned int vb = *(volatile unsigned int *)(nv_bar0 + 0x100E10);
-		unsigned int vs = *(volatile unsigned int *)(nv_bar0 + 0x100E14);
+		unsigned int vb = *(volatile unsigned int *)(nv_regs + 0x100E10);
+		unsigned int vs = *(volatile unsigned int *)(nv_regs + 0x100E14);
 
 		nv_vram_base = vb << 12;
 		nv_vram_size = vs << 12;
@@ -225,6 +225,11 @@ unsigned int nvidia_bar0(void)
 	return nv_bar0;
 }
 
+unsigned int nvidia_regs(void)
+{
+	return nv_regs;
+}
+
 unsigned int nvidia_bar1(void)
 {
 	return nv_bar1;
@@ -266,17 +271,22 @@ unsigned int nvidia_lfb_in_vram(void)
 	static const unsigned int mark[4] = { 0xA5C3F00Du, 0x5A3C0FF0u,
 					      0xDEADBEEFu, 0x12345678u };
 	unsigned int vram = nv_vram_base, vsize = nv_vram_size;
-	unsigned int lfb = (unsigned int)vga_fb_lfb_addr();
+	unsigned int lfb_phys = vga_fb_lfb_phys();
+	unsigned int lfb = (unsigned int)vga_fb_lfb_addr();   /* mapped */
 	unsigned int off, i, hit = 0;
 
 	if (!vram || vsize < 0x01000000u || !nv_bar3 || !nv_bar3_size)
 		return 0;
-	if (lfb < nv_bar3 || lfb > nv_bar3 + nv_bar3_size - 0x1000u)
+	if (lfb_phys < nv_bar3 || lfb_phys > nv_bar3 + nv_bar3_size - 0x1000u)
 		return 0;          /* the VBE LFB is not in the VRAM aperture */
 	if (nv_bar3_size < 0x00801000u)
 		return 0;          /* no room for a marker past the window */
 
-	paging_map_ram(vram, vsize);
+	/* The stolen VRAM is DRAM: map it write-back (the WC MTRR the caller
+	 * adds over it makes the effective type WC). */
+	vram = (unsigned int)ioremap(vram, vsize, IOREMAP_RAM);
+	if (!vram)
+		return 0;
 
 	/* Marker at LFB + 8 MB: past any 1920x1080x32 frame. */
 	{

+ 4 - 1
kernel/nvidia.h

@@ -25,6 +25,8 @@ unsigned int nvidia_chipset(void);
 /* BAR0 register aperture / BAR1 VRAM window / BAR3 VBE window bases
  * (0 = absent) and their probed sizes. */
 unsigned int nvidia_bar0(void);
+/* Kernel mapping of BAR0 (the register aperture), 0 when absent. */
+unsigned int nvidia_regs(void);
 unsigned int nvidia_bar1(void);
 unsigned int nvidia_bar1_size(void);
 unsigned int nvidia_bar3(void);
@@ -40,7 +42,8 @@ unsigned int nvidia_vram_size(void);
  * therefore much faster to write than the PCIe aperture.  Locates the
  * framebuffer in the stolen VRAM by writing a marker through the LFB and
  * scanning the (now cacheably mapped) VRAM at page granularity.  Returns the
- * framebuffer's address in the stolen VRAM, or 0 when not applicable. */
+ * kernel (ioremap) address of the framebuffer in the stolen VRAM, or 0 when
+ * not applicable. */
 unsigned int nvidia_lfb_in_vram(void);
 
 #endif

+ 4 - 3
kernel/ohci.c

@@ -297,14 +297,14 @@ static int ohci_setup_one(unsigned int bus, unsigned int dev, unsigned int func)
 	pci_cfg_write16(bus, dev, func, 0xC0, 0);
 
 	c = &dma->ctrls[n_ctrls];
-	c->regs = (volatile unsigned int *)bar;
+	c->regs = ioremap(bar, 0x1000, IOREMAP_MMIO);
+	if (!c->regs)
+		return -1;
 	c->bus = bus;
 	c->dev = dev;
 	c->func = func;
 	c->bar = bar;
 
-	paging_map_mmio(bar, 0x1000);
-
 	if (rd(c, R_REVISION) == 0xFFFFFFFFu || rd(c, R_REVISION) == 0) {
 		serial_puts(SERIAL_COM1, "ohci: no register response (rev ");
 		serial_puthex(SERIAL_COM1, rd(c, R_REVISION));
@@ -880,4 +880,5 @@ const struct usb_hcd_ops usb_ohci_ops = {
 	0,
 	0,
 	0,
+	0,
 };

+ 286 - 31
kernel/paging.c

@@ -14,6 +14,7 @@
 #include "mm.h"
 #include "mtrr.h"
 #include "process.h"
+#include "sched.h"
 #include "serial.h"
 #include "vga.h"
 #include "wm_abi.h"
@@ -26,6 +27,37 @@ _Static_assert(MM_MAX_PHYS <= USER_WIN_BASE &&
 	       MM_MAX_PHYS <= WM_CONTENT_VA_BASE &&
 	       MM_MAX_PHYS <= USER_HEAP_BASE,
 	       "identity map overlaps a ring-3 window");
+_Static_assert(MM_KMAP_VA >= USER_STACK_TOP &&
+	       MM_KMAP_VA + 0x400000u <= USER_WIN_BASE &&
+	       (MM_KMAP_VA & 0x3FFFFFu) == 0,
+	       "kmap window must be a free 4 MB slot below USER_WIN_BASE");
+
+/* kmap: KMAP_SLOTS nested mappings per CPU in the shared window page table. */
+#define KMAP_PDE    (MM_KMAP_VA >> 22)
+#define KMAP_SLOTS  4
+static unsigned int *kmap_pt;
+static unsigned int kmap_depth[MAX_CPUS];
+
+/* ioremap window: [IOMAP_BASE, 4 GB), kernel-only, 4 KB pages.  Its page
+ * tables are allocated on first use and their PDEs copied into every live
+ * process directory (process_sync_kernel_pde), so nothing is mapped at its
+ * physical address above lowmem and ring-3 space is free up to IOMAP_BASE. */
+_Static_assert(IOMAP_BASE >= USER_HEAP_BASE + USER_HEAP_MAX &&
+	       IOMAP_BASE >= WM_CONTENT_VA_BASE +
+			     WM_CONTENT_SLOT * WM_MAX_WINDOWS &&
+	       (IOMAP_BASE & 0x3FFFFFu) == 0,
+	       "ioremap window overlaps ring-3 space");
+#define IOMAP_MAX   96
+struct iomap {
+	unsigned int phys;   /* page-aligned */
+	unsigned int size;   /* page multiple */
+	unsigned int va;
+	int type;
+};
+static struct iomap iomaps[IOMAP_MAX];
+static unsigned int n_iomaps;
+static unsigned int iomap_next = IOMAP_BASE;  /* bump pointer; 0 = full */
+static void *lfb_va;
 
 /* 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
@@ -117,8 +149,9 @@ void paging_prepare_cpu(void)
 static unsigned int lfb_wc_base, lfb_wc_size;
 static int lfb_wc_ok;
 
-/* Write the LFB PDEs: WC-covered pages cacheable (effective type WC), the
- * rest uncacheable. */
+/* Write the LFB PDEs for a framebuffer in lowmem (stolen system RAM below
+ * 1 GB): WC-covered pages cacheable (effective type WC), the rest
+ * uncacheable.  An LFB above lowmem goes through ioremap() instead. */
 static void lfb_apply_pages(unsigned int *pd)
 {
 	const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
@@ -147,32 +180,15 @@ static void lfb_apply_pages(unsigned int *pd)
 	}
 }
 
-/* Re-apply the framebuffer mapping; call after any MMIO mapping that may have
- * covered the same addresses (the NVIDIA BAR3 aperture includes the VBE LFB). */
-void paging_lfb_refresh(void)
-{
-	if (!lfb_wc_ok || !page_dir)
-		return;
-	lfb_apply_pages(page_dir);
-	serial_puts(SERIAL_COM1, "paging: LFB write-combining re-applied\n");
-}
-
 /* 1 when the LFB is mapped for write-combining (WC MTRR + cacheable pages). */
 int paging_lfb_wc(void)
 {
 	return lfb_wc_ok;
 }
 
-void paging_map_ram(unsigned int base, unsigned int size)
+void *paging_lfb_va(void)
 {
-	unsigned int p0 = base >> 22;
-	unsigned int p1 = (base + size + 0x3FFFFFu) >> 22;
-	unsigned int i;
-
-	if (!page_dir)
-		return;
-	for (i = p0; i < p1 && i < 1024; i++)
-		page_dir[i] = (i << 22) | PDE_PS | PDE_WRITABLE | PDE_PRESENT;
+	return lfb_va;
 }
 
 void paging_init(void)
@@ -204,6 +220,18 @@ void paging_init(void)
 	for (i = 0; i < MM_MAX_PHYS / 0x400000; i++)
 		page_dir[i] = (i << 22) | PDE_PS | PDE_WRITABLE | PDE_PRESENT;
 
+	/* The kmap window: a supervisor page table every process directory
+	 * shares (paging_new_directory copies the PDE). */
+	kmap_pt = (unsigned int *)mm_alloc_page();
+	if (!kmap_pt) {
+		serial_puts(SERIAL_COM1, "paging: out of memory (kmap)\n");
+		for (;;)
+			__asm__ volatile ("cli; hlt");
+	}
+	for (i = 0; i < 1024; i++)
+		kmap_pt[i] = 0;
+	page_dir[KMAP_PDE] = (unsigned int)kmap_pt | PDE_WRITABLE | PDE_PRESENT;
+
 	/* The OHCI ED/TD/interrupt-buffer page is DMA'd by the SB950's legacy
 	 * USB controller, which does not snoop dirty CPU cache lines.  Split
 	 * the containing 4 MB page now, before paging is enabled, so every
@@ -232,7 +260,18 @@ void paging_init(void)
 		lfb_wc_ok = mtrr_set_wc(fb->lfb_base, want,
 					&lfb_wc_base, &lfb_wc_size);
 		bootcon_puts("paging: map\n");
-		lfb_apply_pages(page_dir);
+		if ((unsigned long long)fb->lfb_base + LFB_MAP_SIZE <= MM_MAX_PHYS) {
+			lfb_apply_pages(page_dir);
+			lfb_va = (void *)fb->lfb_base;
+		} else {
+			unsigned long long end = (unsigned long long)fb->lfb_base +
+						 LFB_MAP_SIZE;
+			unsigned int sz = end > 0x100000000ull
+				? (unsigned int)(0x100000000ull - fb->lfb_base)
+				: LFB_MAP_SIZE;
+
+			lfb_va = ioremap(fb->lfb_base, sz, IOREMAP_MMIO);
+		}
 		/* Tell the truth about VRAM writes: with cacheable pages the WC
 		 * MTRR applies (fast bursts); otherwise every dword is a separate
 		 * uncached write. */
@@ -258,6 +297,8 @@ void paging_init(void)
 		: "r"((unsigned int)page_dir)
 		: "eax", "memory");
 
+	/* The boot console drew through the physical LFB until now. */
+	bootcon_set_lfb(lfb_va);
 	bootcon_puts("paging: on\n");
 	serial_puts(SERIAL_COM1, "paging: enabled (identity map)\n");
 }
@@ -416,21 +457,159 @@ void paging_release_user_range(unsigned int cr3, unsigned int va,
 
 /* Identity-map an MMIO range uncacheable into one page directory (4 MB pages;
  * the range is rounded up to a whole number of them). */
-void paging_map_mmio_cr3(unsigned int cr3, unsigned int phys_base, unsigned int size)
+/* MMIO inside lowmem (a BAR the firmware put below 1 GB): make the 4 MB
+ * identity pages uncacheable, as the identity mapper always did, in the
+ * kernel directory and every process copy. */
+static void lowmem_uncached(unsigned int phys, unsigned int size)
 {
-	unsigned int *pd = (unsigned int *)cr3;
-	unsigned int p0 = phys_base >> 22;
-	unsigned int p1 = (phys_base + size + 0x3FFFFFu) >> 22;
+	unsigned int p0 = phys >> 22;
+	unsigned int p1 = (unsigned int)(((unsigned long long)phys + size +
+					  0x3FFFFFu) >> 22);
 	unsigned int i;
 
-	for (i = p0; i < p1 && i < 1024; i++)
-		pd[i] = (i << 22) | PDE_PS | PDE_PCD |
-			PDE_WRITABLE | PDE_PRESENT;
+	for (i = p0; i < p1 && i < MM_MAX_PHYS / 0x400000u; i++) {
+		if (!(page_dir[i] & PDE_PS))
+			continue;   /* split PDE 0 keeps its own page types */
+		page_dir[i] = (i << 22) | PDE_PS | PDE_PCD | PDE_WRITABLE |
+			      PDE_PRESENT;
+		process_sync_kernel_pde(i);
+	}
+}
+
+/* Page type for one 4 KB window page: RAM is write-back; MMIO is uncached,
+ * except pages the LFB's WC MTRR covers, which are cacheable (effective
+ * type WC) in every alias, so a GPU BAR that contains the framebuffer and
+ * the framebuffer mapping itself never disagree. */
+static unsigned int iomap_pte_flags(unsigned int phys, int type)
+{
+	unsigned int f = PTE_PRESENT | PTE_WRITABLE;
+
+	if (type == IOREMAP_RAM)
+		return f;
+	if (lfb_wc_ok && phys >= lfb_wc_base &&
+	    (unsigned long long)phys + PAGE_SIZE <=
+	    (unsigned long long)lfb_wc_base + lfb_wc_size)
+		return f;
+	return f | PDE_PCD;
+}
+
+static int iomap_set(unsigned int va, unsigned int phys, unsigned int flags)
+{
+	unsigned int pde = va >> 22;
+	unsigned int *pt;
+
+	if (!(page_dir[pde] & PDE_PRESENT)) {
+		unsigned int t = mm_alloc_page();
+		unsigned int i;
+
+		if (!t)
+			return 0;
+		pt = (unsigned int *)t;
+		for (i = 0; i < 1024; i++)
+			pt[i] = 0;
+		page_dir[pde] = t | PDE_WRITABLE | PDE_PRESENT;
+		process_sync_kernel_pde(pde);
+	} else {
+		pt = (unsigned int *)(page_dir[pde] & 0xFFFFF000u);
+	}
+	pt[(va >> 12) & 0x3FFu] = (phys & 0xFFFFF000u) | flags;
+	return 1;
+}
+
+void *ioremap(unsigned int phys, unsigned int size, int type)
+{
+	unsigned long long end;
+	unsigned int p0, bytes, va, i;
+
+	if (size == 0)
+		size = 1;
+	end = (unsigned long long)phys + size;
+	if (end > 0x100000000ull || !page_dir)
+		return 0;
+
+	/* Lowmem is identity-mapped already. */
+	if (end <= MM_MAX_PHYS) {
+		if (type == IOREMAP_MMIO)
+			lowmem_uncached(phys, size);
+		return (void *)phys;
+	}
+
+	/* Reuse a mapping of the same type that already covers the range. */
+	for (i = 0; i < n_iomaps; i++) {
+		const struct iomap *m = &iomaps[i];
+
+		if (m->type == type && phys >= m->phys &&
+		    end <= (unsigned long long)m->phys + m->size)
+			return (void *)(m->va + (phys - m->phys));
+	}
+
+	/* Grow the newest mapping in place when the request starts inside it
+	 * and only runs past its end (a table header mapped first, then the
+	 * whole table): its pages are the last ones in the window. */
+	if (n_iomaps && iomap_next) {
+		struct iomap *m = &iomaps[n_iomaps - 1];
+		unsigned long long mend = (unsigned long long)m->phys + m->size;
+		unsigned long long need = (end + PAGE_SIZE - 1) &
+					  ~(unsigned long long)(PAGE_SIZE - 1);
+
+		if (m->type == type && phys >= m->phys && phys < mend &&
+		    m->va + m->size == iomap_next &&
+		    (unsigned long long)iomap_next + (need - mend) <=
+		    0x100000000ull) {
+			unsigned int extra = (unsigned int)(need - mend);
+
+			for (i = 0; i < extra; i += PAGE_SIZE)
+				if (!iomap_set(iomap_next + i,
+					       (unsigned int)mend + i,
+					       iomap_pte_flags((unsigned int)mend + i,
+							       type)))
+					return 0;
+			m->size += extra;
+			iomap_next = ((unsigned long long)iomap_next + extra >=
+				      0x100000000ull) ? 0 : iomap_next + extra;
+			return (void *)(m->va + (phys - m->phys));
+		}
+	}
+
+	p0 = phys & 0xFFFFF000u;
+	bytes = (unsigned int)(((end + PAGE_SIZE - 1) &
+				~(unsigned long long)(PAGE_SIZE - 1)) - p0);
+	if (n_iomaps >= IOMAP_MAX || !iomap_next ||
+	    (unsigned long long)iomap_next + bytes > 0x100000000ull) {
+		serial_puts(SERIAL_COM1, "paging: ioremap window full for ");
+		serial_puthex(SERIAL_COM1, phys);
+		serial_puts(SERIAL_COM1, "\n");
+		return 0;
+	}
+	va = iomap_next;
+	for (i = 0; i < bytes; i += PAGE_SIZE)
+		if (!iomap_set(va + i, p0 + i, iomap_pte_flags(p0 + i, type))) {
+			serial_puts(SERIAL_COM1, "paging: ioremap out of memory\n");
+			return 0;
+		}
+	iomap_next = ((unsigned long long)va + bytes >= 0x100000000ull)
+		     ? 0 : va + bytes;
+	iomaps[n_iomaps].phys = p0;
+	iomaps[n_iomaps].size = bytes;
+	iomaps[n_iomaps].va = va;
+	iomaps[n_iomaps].type = type;
+	n_iomaps++;
+	return (void *)(va + (phys - p0));
 }
 
-void paging_map_mmio(unsigned int phys_base, unsigned int size)
+/* The window address of `phys` if some ioremap() covers it, else 0. */
+void *ioremap_lookup(unsigned int phys)
 {
-	paging_map_mmio_cr3((unsigned int)page_dir, phys_base, size);
+	unsigned int i;
+
+	for (i = 0; i < n_iomaps; i++) {
+		const struct iomap *m = &iomaps[i];
+
+		if (phys >= m->phys &&
+		    (unsigned long long)phys < (unsigned long long)m->phys + m->size)
+			return (void *)(m->va + (phys - m->phys));
+	}
+	return 0;
 }
 
 /* Split the 4 MB page containing `va` in one directory and set PCD on that
@@ -464,3 +643,79 @@ static void uc_page_cr3(unsigned int cr3, unsigned int va)
 	}
 	pt[pte] |= PDE_PCD;
 }
+
+/* Temporary kernel mapping of one frame.  Lowmem is returned through the
+ * identity map; a highmem frame gets this CPU's next window slot with
+ * preemption disabled, so the task cannot migrate or be switched out while
+ * the slot is live.  Nothing may sleep or yield before kunmap(). */
+void *kmap(unsigned int phys)
+{
+	struct cpu *c;
+	unsigned int slot, va;
+
+	phys &= 0xFFFFF000u;
+	if (phys < MM_MAX_PHYS)
+		return (void *)phys;
+	preempt_disable();
+	c = this_cpu();
+	if (c->id >= MAX_CPUS || kmap_depth[c->id] >= KMAP_SLOTS) {
+		serial_puts(SERIAL_COM1, "paging: kmap slots exhausted\n");
+		for (;;)
+			__asm__ volatile ("cli; hlt");
+	}
+	slot = c->id * KMAP_SLOTS + kmap_depth[c->id]++;
+	va = MM_KMAP_VA + slot * PAGE_SIZE;
+	kmap_pt[slot] = phys | PTE_WRITABLE | PTE_PRESENT;
+	__asm__ volatile ("invlpg (%0)" : : "r"(va) : "memory");
+	return (void *)va;
+}
+
+/* Undo the most recent kmap() on this CPU (mappings nest LIFO). */
+void kunmap(void *addr)
+{
+	unsigned int va = (unsigned int)addr & 0xFFFFF000u;
+	struct cpu *c;
+	unsigned int slot;
+
+	if (va < MM_MAX_PHYS)
+		return;
+	c = this_cpu();
+	slot = (va - MM_KMAP_VA) / PAGE_SIZE;
+	kmap_pt[slot] = 0;
+	__asm__ volatile ("invlpg (%0)" : : "r"(va) : "memory");
+	if (kmap_depth[c->id])
+		kmap_depth[c->id]--;
+	preempt_enable();
+}
+
+static int pa_is_ram(unsigned int pa)
+{
+	unsigned int i, n = mm_e820_count();
+
+	for (i = 0; i < n; i++) {
+		const struct e820_entry *e = mm_e820_entry(i);
+
+		if (e->type == E820_RAM && pa >= e->base &&
+		    (unsigned long long)pa < e->base + e->length)
+			return 1;
+	}
+	return 0;
+}
+
+volatile unsigned char *paging_debug_map(unsigned int pa, void **km)
+{
+	unsigned char *v;
+
+	*km = 0;
+	if (pa < MM_MAX_PHYS)
+		return (volatile unsigned char *)pa;
+	if (pa_is_ram(pa)) {
+		*km = kmap(pa & 0xFFFFF000u);
+		return (volatile unsigned char *)*km + (pa & 0xFFFu);
+	}
+	v = ioremap_lookup(pa);          /* includes the offset */
+	if (v)
+		return v;
+	v = ioremap(pa & 0xFFFFF000u, 0x1000, IOREMAP_MMIO);
+	return v ? v + (pa & 0xFFFu) : 0;
+}

+ 33 - 15
kernel/paging.h

@@ -8,15 +8,34 @@ void paging_init(void);
  * 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. */
 int paging_lfb_wc(void);
 
-/* Map a physical window identity-mapped with caching enabled (4 MB pages).
- * Used for the stolen-VRAM alias, which is ordinary DRAM; the effective type
- * is whatever the MTRRs say (write-combining when we added an entry). */
-void paging_map_ram(unsigned int base, unsigned int size);
+/* Kernel address of the boot framebuffer (the identity address when it is
+ * in lowmem, else its ioremap() window address); 0 without an LFB. */
+void *paging_lfb_va(void);
+
+/* Map physical [phys, phys+size) for kernel access and return the address
+ * of `phys`.  Lowmem (below MM_MAX_PHYS) is identity-mapped and returned
+ * as-is (an IOREMAP_MMIO range there has its 4 MB pages made uncacheable);
+ * anything else gets 4 KB pages in the kernel-only window at IOMAP_BASE,
+ * shared by every process directory.  IOREMAP_MMIO pages are uncached
+ * except where the LFB's WC MTRR covers them; IOREMAP_RAM pages (firmware
+ * tables, the stolen-VRAM alias) are write-back.  A range already covered
+ * by a mapping of the same type reuses it.  Mappings are never torn down.
+ * Returns 0 when the window is full or out of page tables. */
+#define IOREMAP_MMIO 0
+#define IOREMAP_RAM  1
+void *ioremap(unsigned int phys, unsigned int size, int type);
+
+/* Window address of `phys` if an ioremap() covers it, else 0 (peek/poke). */
+void *ioremap_lookup(unsigned int phys);
+
+/* Debug access (peek/poke/dbg) to physical byte `pa`: lowmem directly,
+ * highmem RAM through a kmap (*km is set; kunmap(*km) when done), MMIO
+ * through its ioremap() mapping, created on demand (4 KB, uncached).
+ * 0 when it cannot be mapped. */
+volatile unsigned char *paging_debug_map(unsigned int pa, void **km);
 
 
 
@@ -37,6 +56,14 @@ unsigned int paging_new_directory(void);
 #define PTE_WRITABLE 0x002
 #define PTE_USER     0x004
 
+/* Temporary kernel access to any frame, including highmem (above
+ * MM_MAX_PHYS).  Lowmem returns the identity address; highmem takes a
+ * per-CPU slot in the MM_KMAP_VA window with preemption disabled until the
+ * matching kunmap().  Mappings nest (LIFO, 4 per CPU); do not sleep, yield
+ * or take the BKL in between. */
+void *kmap(unsigned int phys);
+void kunmap(void *addr);
+
 /* Map one 4 KB page (va -> phys) in page directory `cr3`, allocating a page
  * table if needed.  Returns 1 on success, 0 on out-of-memory. */
 int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
@@ -63,14 +90,5 @@ void paging_release_user_range(unsigned int cr3, unsigned int va,
 int paging_user_range_ok(unsigned int cr3, unsigned int va,
 			 unsigned int size, int write);
 
-/* Identity-map an MMIO range (e.g. a GPU register aperture) uncacheable into
- * the current page directory.  Uses 4 MB pages; the range is rounded up to a
- * whole number of 4 MB pages. */
-void paging_map_mmio(unsigned int phys_base, unsigned int size);
-
-/* Same, but into an explicit page directory (used to update per-process
- * directories; see process_map_mmio_all()). */
-void paging_map_mmio_cr3(unsigned int cr3, unsigned int phys_base,
-			 unsigned int size);
 
 #endif

+ 31 - 9
kernel/peek.c

@@ -1,14 +1,17 @@
-/* peek command: hex dump of memory (identity-mapped, so this also covers
- * MMIO registers, e.g. a driver's BAR once mapped).
+/* peek command: hex dump of physical memory, including MMIO registers.
  *
  *   peek <addr> [len]      default len 64, max 256
  *   poke <addr> <val>      write one dword (e.g. an MMIO register), print
  *                          the read-back
  *
- * Reading an unmapped address faults; the exception handler shows EIP/CR2.
+ * Addresses are physical.  Lowmem is read through the identity map, RAM
+ * above it (highmem) through a temporary kmap, and anything else (MMIO)
+ * through its ioremap() window mapping, created on demand (4 KB,
+ * uncached) when no driver mapped it yet.
  */
 
 #include "io.h"
+#include "paging.h"
 
 static void put_hex2(unsigned int v)
 {
@@ -80,12 +83,20 @@ void program_peek_main(const char *arg)
 		return;
 	}
 
-	mem = (const volatile unsigned char *)addr;
 	for (i = 0; i < len; i += 16) {
 		put_hex8(addr + i);
 		io_putstr(": ");
 		for (j = 0; j < 16 && i + j < len; j++) {
-			put_hex2(mem[i + j]);
+			void *km;
+
+			mem = paging_debug_map(addr + i + j, &km);
+			if (!mem) {
+				io_putstr("?? ");
+				continue;
+			}
+			put_hex2(*mem);
+			if (km)
+				kunmap(km);
 			io_putchar(' ');
 		}
 		io_putchar('\n');
@@ -99,7 +110,7 @@ void program_poke_main(const char *arg)
 {
 	const char *p = arg;
 	volatile unsigned int *mem;
-	unsigned int addr, val;
+	unsigned int addr, val, back;
 
 	if (parse_hex(p, &addr, &p) != 0) {
 		io_putstr("poke: usage poke <hexaddr> <hexval>\n");
@@ -118,14 +129,25 @@ void program_poke_main(const char *arg)
 		return;
 	}
 
-	mem = (volatile unsigned int *)(addr & ~3u);
-	*mem = val;
+	{
+		void *km;
+
+		mem = (volatile unsigned int *)paging_debug_map(addr & ~3u, &km);
+		if (!mem) {
+			io_putstr("poke: cannot map that address\n");
+			return;
+		}
+		*mem = val;
+		back = *mem;
+		if (km)
+			kunmap(km);
+	}
 
 	io_putstr("poke ");
 	put_hex8(addr & ~3u);
 	io_putstr(" <- ");
 	put_hex8(val);
 	io_putstr(" reads ");
-	put_hex8(*mem);
+	put_hex8(back);
 	io_putchar('\n');
 }

+ 19 - 16
kernel/process.c

@@ -446,14 +446,14 @@ int process_create_user_tty(const char *name, const void *image, unsigned int si
 	}
 
 	/* Map the user stack just below USER_STACK_TOP.  Keep the top page's
-	 * physical address: the argument string is written through it (identity
-	 * mapped) after it is mapped in `cr3`. */
+	 * physical address: the argument string is written through a kmap of
+	 * it (the frame may be highmem) after it is mapped in `cr3`. */
 	{
 		unsigned int top_phys = 0;
 		unsigned int user_esp = USER_STACK_TOP;
 
 		for (i = 0; i < USER_STACK_PAGES; i++) {
-			unsigned int phys = mm_alloc_page();
+			unsigned int phys = mm_alloc_user_page();
 			unsigned int va = USER_STACK_TOP - (unsigned int)(i + 1) * 0x1000;
 			if (!phys ||
 			    !paging_map_page(cr3, va, phys,
@@ -470,11 +470,12 @@ int process_create_user_tty(const char *name, const void *image, unsigned int si
 		/* One C string argument at the top of the user stack with a fake
 		 * return address below it, so `_start(const char *arg)` reads it
 		 * as its first parameter without needing a crt0.  The virtual
-		 * addresses go into the frame; the writes go through the identity
-		 * map at the same offset from the physical page top. */
+		 * addresses go into the frame; the writes go through a kmap of the
+		 * top stack page at the same offset from its end. */
 		{
 			unsigned int va_top = USER_STACK_TOP;
-			unsigned char *base_phys = (unsigned char *)(top_phys + 0x1000);
+			unsigned char *top_map = kmap(top_phys);
+			unsigned char *base_phys = top_map + 0x1000;
 			unsigned char *str_phys;
 			unsigned int *frame_phys;
 			unsigned int len = 0;
@@ -484,7 +485,10 @@ int process_create_user_tty(const char *name, const void *image, unsigned int si
 				while (arg[len])
 					len++;
 			va = (va_top - (len + 1)) & ~15u;
-			if (va - 8 < USER_STACK_TOP - USER_STACK_PAGES * 0x1000) {
+			/* The string and frame are written through one kmap'd
+			 * page, so they must fit the top stack page. */
+			if (va - 8 < USER_STACK_TOP - 0x1000) {
+				kunmap(top_map);
 				mm_free_page(cr3);
 				kfree(p);
 				irq_restore(flags);
@@ -497,6 +501,7 @@ int process_create_user_tty(const char *name, const void *image, unsigned int si
 			frame_phys[0] = 0;   /* fake return address */
 			frame_phys[1] = va;  /* the argument pointer (user VA) */
 			user_esp = va - 8;
+			kunmap(top_map);
 		}
 		base = stack_alloc(&owned);
 		if (!base) {
@@ -555,23 +560,20 @@ void process_set_render_target(struct surface *s)
 		p->target = s;
 }
 
-void process_map_mmio_all(unsigned int phys_base, unsigned int size)
+void process_sync_kernel_pde(unsigned int index)
 {
+	const unsigned int *kpd = (const unsigned int *)paging_kernel_cr3();
 	unsigned int i, flags;
 
-	/* Map the kernel directory (the one new process directories clone) and
-	 * every live process copy.  paging_current_cr3() is the *current* process
-	 * directory, which may be one of these; mapping it twice is harmless.
-	 * The reaper frees PCBs, so hold the process lock while iterating. */
+	/* The reaper frees PCBs, so hold the process lock while iterating. */
 	flags = sched_lock_acquire();
-	paging_map_mmio(phys_base, size);
 	for (i = 0; i < PROC_MAX; i++) {
 		struct process *p = procs[i];
 
-		if (!p || p->state == PROC_DEAD)
+		if (!p || p->state == PROC_DEAD || !p->cr3 ||
+		    p->cr3 == (unsigned int)kpd)
 			continue;
-		if (p->cr3)
-			paging_map_mmio_cr3(p->cr3, phys_base, size);
+		((unsigned int *)p->cr3)[index] = kpd[index];
 	}
 	sched_lock_release(flags);
 }
@@ -813,6 +815,7 @@ int process_procinfo(unsigned int index, struct sys_procinfo *out)
 		out->pages      = 2 + count_user_pages(p->cr3) + p->heap_pages;
 		out->weight     = p->weight;
 		out->cpu        = p->on_cpu;
+		out->kernel_thread = p->kernel_thread ? 1 : 0;
 		name_copy(out->name, p->name);
 		sched_lock_release(flags);
 		return 1;

+ 8 - 6
kernel/process.h

@@ -78,6 +78,10 @@ struct process {
 	unsigned int kernel_thread;
 	/* BKL depth saved when the process blocks and must reacquire it. */
 	unsigned int bkl_saved;
+	/* x87 state (FNSAVE image) while switched out; fpu_valid is 0 until
+	 * the first switch-out, and such a process starts from FNINIT. */
+	unsigned char fpu[108];
+	unsigned int fpu_valid;
 };
 
 /* User-mode virtual address layout (ring-3 processes). */
@@ -117,12 +121,10 @@ int process_create_user_tty(const char *name, const void *image,
 struct surface *process_render_target(void);
 void process_set_render_target(struct surface *s);
 
-/* Map an MMIO range into the kernel's page directory and every live process
- * copy (and thus every process created later, which clones the kernel map).
- * Drivers that map their register window at command time must use this: a
- * process runs with its own page-directory copy, so paging_map_mmio() alone
- * would only be visible to the kernel bootstrap context. */
-void process_map_mmio_all(unsigned int phys_base, unsigned int size);
+/* Copy kernel page-directory entry `index` into every live process
+ * directory.  paging.c calls it when ioremap() adds a window page table or
+ * makes a lowmem PDE uncacheable; processes created later clone it. */
+void process_sync_kernel_pde(unsigned int index);
 void process_exit(void);
 /* Called by the kworker: free switched-out dead processes. */
 void process_reap_dead(void);

+ 19 - 5
kernel/r8168.c

@@ -361,6 +361,13 @@ static void rx_restart(void)
 	rx_restarts++;
 }
 
+/* Set by an RX overflow / descriptors-unavailable event: restart the RX
+ * engine, but only once the frames already in the ring were handed out.
+ * Restarting at once re-armed all 64 descriptors and threw away up to a
+ * ring of good frames per overflow, so parallel TCP downloads lost bursts,
+ * waited for the peers' retransmission timeouts and stalled. */
+static int rx_restart_pending;
+
 /* Read + acknowledge latched events; recover from an RX stall. */
 static void isr_poll(void)
 {
@@ -372,11 +379,11 @@ static void isr_poll(void)
 	isr_last = st;
 	if (st & ISR_ROVW) {
 		isr_rovw++;
-		rx_restart();
+		rx_restart_pending = 1;
 	}
 	if (st & ISR_FOV) {
 		isr_fov++;
-		rx_restart();
+		rx_restart_pending = 1;
 	}
 	if (st & ISR_TDU) {
 		isr_tdu++;
@@ -640,8 +647,9 @@ int r8168_init(void)
 	buf[i] = '\0';
 	dbg(buf);
 
-	process_map_mmio_all(bar_base, bar_size);
-	mmio = (unsigned char *)bar_base;
+	mmio = ioremap(bar_base, bar_size, IOREMAP_MMIO);
+	if (!mmio)
+		return -1;
 
 	/* Master abort / unmapped window reads back all-ones. */
 	if (r32(REG_TxConfig) == 0xFFFFFFFFu) {
@@ -818,8 +826,14 @@ int r8168_rx(unsigned char *buf, unsigned int maxlen)
 		unsigned int status = d->opts1;
 		unsigned int len;
 
-		if (status & DescOwn)
+		if (status & DescOwn) {
+			/* Drained: a pending restart loses nothing now. */
+			if (rx_restart_pending) {
+				rx_restart_pending = 0;
+				rx_restart();
+			}
 			return -1;              /* nothing pending */
+		}
 
 		dma_barrier();
 

+ 5 - 5
kernel/radeon.c

@@ -28,7 +28,7 @@
 #include "serial.h"
 #include "vga.h"
 
-static unsigned int radeon_mmio;   /* BAR2 register aperture (1:1 mapped) */
+static unsigned int radeon_mmio;   /* kernel mapping of the BAR2 registers */
 static int radeon_present;
 
 #define RADEON_MM_INDEX              0x0000
@@ -98,9 +98,9 @@ void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 	if (bar2_size < 0x10000)
 		return;
 
-	paging_map_mmio(bar2_base, bar2_size);
-
-	radeon_mmio = bar2_base;
+	radeon_mmio = (unsigned int)ioremap(bar2_base, bar2_size, IOREMAP_MMIO);
+	if (!radeon_mmio)
+		return;
 
 	/* Validate the aperture: a master abort reads back all-ones. */
 	mmio_write(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
@@ -111,7 +111,7 @@ void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 
 	radeon_present = 1;
 	serial_puts(SERIAL_COM1, "radeon: cursor MMIO ");
-	serial_puthex(SERIAL_COM1, radeon_mmio);
+	serial_puthex(SERIAL_COM1, bar2_base);
 	serial_puts(SERIAL_COM1, "\n");
 }
 

+ 68 - 0
kernel/sched.c

@@ -50,8 +50,31 @@ static struct {
 	struct process *owner;
 	unsigned int depth;
 	struct wait_queue wait;
+	unsigned int since;     /* get_ticks_ms() when the owner took it */
 } bkl;
 
+/* BKL contention, for `sched`: how often a taker had to sleep, the total
+ * wait, and the longest single hold with its holder (reset when printed).
+ * Every syscall takes the lock, so a long holder stalls ring-3 work on the
+ * other CPUs. */
+static struct {
+	unsigned int contended, wait_ms, hold_max_ms;
+	char hold_max_name[PROC_NAME_LEN];
+} bkl_stat;
+
+static void bkl_note_release(struct process *p)
+{
+	unsigned int held = get_ticks_ms() - bkl.since;
+	unsigned int i;
+
+	if (held <= bkl_stat.hold_max_ms)
+		return;
+	bkl_stat.hold_max_ms = held;
+	for (i = 0; i + 1 < PROC_NAME_LEN && p->name[i]; i++)
+		bkl_stat.hold_max_name[i] = p->name[i];
+	bkl_stat.hold_max_name[i] = '\0';
+}
+
 unsigned int sched_lock_acquire(void)
 {
 	unsigned int flags = spin_irq_save_disable();
@@ -389,6 +412,8 @@ void bkl_lock(void)
 
 	if (!p)
 		return;
+	unsigned int t0 = 0;
+
 	if (bkl.owner == p) {
 		bkl.depth++;
 		return;
@@ -398,9 +423,16 @@ void bkl_lock(void)
 		if (!bkl.owner) {
 			bkl.owner = p;
 			bkl.depth = 1;
+			bkl.since = get_ticks_ms();
+			if (t0)
+				bkl_stat.wait_ms += bkl.since - t0;
 			sched_lock_release(flags);
 			return;
 		}
+		if (!t0) {
+			t0 = get_ticks_ms();
+			bkl_stat.contended++;
+		}
 		sched_block_on_locked(&bkl.wait);
 		sched_lock_release(flags);
 		sched_yield();
@@ -419,6 +451,7 @@ void bkl_unlock(void)
 		if (bkl.depth > 1) {
 			bkl.depth--;
 		} else {
+			bkl_note_release(p);
 			bkl.owner = 0;
 			bkl.depth = 0;
 			wake_one_locked(&bkl.wait);
@@ -442,6 +475,7 @@ int bkl_trylock(void)
 	if (!bkl.owner) {
 		bkl.owner = p;
 		bkl.depth = 1;
+		bkl.since = get_ticks_ms();
 		sched_lock_release(flags);
 		return 1;
 	}
@@ -462,6 +496,7 @@ static void bkl_yield_release(void)
 	flags = sched_lock_acquire();
 	if (bkl.owner == p) {
 		p->bkl_saved = bkl.depth;
+		bkl_note_release(p);
 		bkl.owner = 0;
 		bkl.depth = 0;
 		wake_one_locked(&bkl.wait);
@@ -544,6 +579,24 @@ void sched_init(void)
 	}
 }
 
+/* Each process owns its x87 state; the CPU's registers are not shared
+ * between them any more (a preempted floating-point computation used to
+ * continue with another process's stack and control word).  Eager and
+ * cheap enough at 1 kHz: FNSAVE/FRSTOR move 108 bytes and need no FXSR,
+ * so they work back to the Pentium.  The kernel uses no x87 itself; its
+ * SSE fill helpers touch only XMM registers, which ring 3 never uses. */
+static void fpu_switch(struct process *cur, struct process *next)
+{
+	if (cur) {
+		__asm__ volatile ("fnsave %0" : "=m"(cur->fpu));
+		cur->fpu_valid = 1;
+	}
+	if (next->fpu_valid)
+		__asm__ volatile ("frstor %0" :: "m"(next->fpu));
+	else
+		__asm__ volatile ("fninit");
+}
+
 static void sched_switch_to(struct process *next)
 {
 	struct cpu *c = this_cpu();
@@ -637,6 +690,7 @@ void schedule(void)
 		if (cur)
 			cur->on_cpu = PROC_NO_CPU;
 		c->switches++;
+		fpu_switch(cur, next);
 	}
 	runq_del_locked(next);
 	next->state = PROC_RUNNING;
@@ -907,4 +961,18 @@ void sched_print(void)
 	io_putchar('.');
 	io_putchar((char)('0' + idle_pm % 10));
 	io_putstr("%\n");
+	/* Since the last `sched`. */
+	io_putstr("bkl: contended ");
+	sched_put_dec(bkl_stat.contended);
+	io_putstr("  waited ");
+	sched_put_dec(bkl_stat.wait_ms);
+	io_putstr(" ms  longest hold ");
+	sched_put_dec(bkl_stat.hold_max_ms);
+	io_putstr(" ms (");
+	io_putstr(bkl_stat.hold_max_name[0] ? bkl_stat.hold_max_name : "-");
+	io_putstr(")\n");
+	bkl_stat.contended = 0;
+	bkl_stat.wait_ms = 0;
+	bkl_stat.hold_max_ms = 0;
+	bkl_stat.hold_max_name[0] = '\0';
 }

+ 3 - 2
kernel/sdhci.c

@@ -398,10 +398,11 @@ static int sdhci_init(void)
 	serial_puthex(SERIAL_COM1, bar);
 	serial_puts(SERIAL_COM1, "\n");
 
-	process_map_mmio_all(bar, 0x1000);
 	cmd = pci_cfg_read32(bus, dev, func, 0x04);
 	pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);
-	mmc.bar = bar;
+	mmc.bar = (unsigned int)ioremap(bar, 0x1000, IOREMAP_MMIO);
+	if (!mmc.bar)
+		return -1;
 	mmc.state = 0;
 
 	caps = rd32(SDHCI_CAPABILITIES);

+ 4 - 1
kernel/serial.c

@@ -1,4 +1,5 @@
 #include "serial.h"
+#include "bootcon.h"
 #include "serial_ring_buf.h"
 #include "netlog.h"
 #include "pci.h"
@@ -28,8 +29,10 @@ void serial_putc(unsigned int port, char c)
 	 * the mirror is enabled and the NIC is up).  This must happen even when
 	 * COM1 itself is unusable -- boards without a serial port are exactly
 	 * the ones that use netlog. */
-	if (port == SERIAL_COM1)
+	if (port == SERIAL_COM1) {
+		bootcon_scan_char(c);
 		netlog_char(c);
+	}
 
 	/* A machine without a serial port can leave LSR.THRE clear forever
 	 * (the port is decoded but the UART is absent or clocked off).  Never

+ 10 - 4
kernel/smp.c

@@ -61,6 +61,7 @@ static void start_one_ap(unsigned int cpu, unsigned int top)
 	info[1] = top;
 	info[2] = (unsigned int)smp_ap_main;
 	info[3] = cpu;
+	info[4] = 0;
 	__sync_synchronize();
 
 	ap_started[cpu] = 0;
@@ -104,10 +105,6 @@ void smp_init(void)
 		return;
 
 	lapic_set_base(acpi_lapic_base());
-	paging_map_mmio(acpi_lapic_base(), 0x1000);
-	/* The 4 MB MMIO page could share a PDE with the framebuffer; restore
-	 * the LFB's WC/PCD choice if so. */
-	paging_lfb_refresh();
 	if (!lapic_init_bsp()) {
 		serial_puts(SERIAL_COM1, "smp: BSP LAPIC init failed\n");
 		return;
@@ -134,6 +131,14 @@ void smp_init(void)
 		}
 		online_aps++;
 		cpu_count = online_aps + 1;
+		/* CR0 as the AP woke up (its CD/NW are cleared by the
+		 * trampoline): 0x60000011 means it would have run uncached. */
+		serial_puts(SERIAL_COM1, "smp: AP ");
+		serial_putdec(SERIAL_COM1, i);
+		serial_puts(SERIAL_COM1, " start cr0 ");
+		serial_puthex(SERIAL_COM1,
+			      ((volatile unsigned int *)AP_INFO_ADDR)[4]);
+		serial_puts(SERIAL_COM1, "\n");
 	}
 
 	serial_puts(SERIAL_COM1, "smp: ");
@@ -155,6 +160,7 @@ void smp_ap_main(unsigned int cpu)
 	/* An AP starts with the firmware's CR4; enable the SSE state before it
 	 * can reach the drawing fast paths (the BSP does this lazily). */
 	cpu_enable_sse();
+	cpu_init_fpu();
 	cpus[cpu].id = cpu;
 	cpus[cpu].lapic_id = lapic_id();
 	idle = cpus[cpu].idle;

+ 26 - 4
kernel/sys_fs.c

@@ -22,6 +22,7 @@
 #include "kmalloc.h"
 #include "paging.h"
 #include "process.h"
+#include "usbmsc.h"
 #include "vfs.h"
 
 /* Wait for a destructive format that owns the disks while its FAT is
@@ -97,7 +98,7 @@ void sys_fs_scan(void)
 	fs_format_wait();
 	fs_scan_wait();
 	__asm__ volatile ("sti");
-	vfs_mount_all();
+	vfs_mount_all(0);
 	__asm__ volatile ("cli");
 }
 
@@ -326,16 +327,37 @@ int sys_blk_read(unsigned int index, unsigned int lba, void *buf)
 	if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)buf,
 				  sizeof(sec), 1))
 		return SYS_EINVAL;
-	sys_fs_enter();
+	fs_format_wait();
+	fs_scan_wait();
 	if (index >= blk_dev_count) {
-		sys_fs_exit();
+		__asm__ volatile ("cli");
 		return SYS_ENODEV;
 	}
 	d = &blk_devs[index];
+	if (d->kind == BLK_USB && usbmsc_slot_failed(d->unit)) {
+		/* The boot probe (or an earlier raw read) already failed on
+		 * this device; do not time out on it again just to show the
+		 * partition view.  `storage` clears the mark and retries. */
+		__asm__ volatile ("cli");
+		return SYS_EIO;
+	}
+	/* A raw read does not touch the FAT driver's static buffers, and a USB
+	 * disk can make its HCD wait sleep.  Publish it like a scan (disk file
+	 * operations wait for it) but keep preemption enabled, so
+	 * usb_can_sleep()/vfs_scan_poll() can yield instead of holding the BKL
+	 * and preemption through a multi-second device timeout. */
+	vfs_scan_owner_pid = current_proc()->pid;
+	vfs_scan_busy = 1;
+	__asm__ volatile ("sti");
+	if (d->kind == BLK_USB)
+		usbmsc_set_probe_only(1);
 	r = SYS_EIO;
 	if (d->present && lba < d->total_sectors && blk_read(d, lba, sec, 1) == 0)
 		r = 0;
-	sys_fs_exit();
+	if (d->kind == BLK_USB)
+		usbmsc_set_probe_only(0);
+	__asm__ volatile ("cli");
+	vfs_scan_busy = 0;
 	if (r == 0)
 		for (i = 0; i < sizeof(sec); i++)
 			((unsigned char *)buf)[i] = sec[i];

+ 3 - 0
kernel/sys_info.c

@@ -45,6 +45,9 @@ int sys_meminfo(struct sys_meminfo *out)
 	out->free_pages = mm_free_pages();
 	out->page_size = PAGE_SIZE;
 	out->e820_count = mm_e820_count();
+	out->high_free_pages = mm_free_high_pages();
+	out->uncached_high_mb = mm_uncached_high_mb;
+	out->uncached_low_mb = mm_uncached_low_mb;
 	return 0;
 }
 

+ 4 - 2
kernel/sys_mem.c

@@ -10,13 +10,15 @@ static void invlpg_va(unsigned int va)
 	__asm__ volatile ("invlpg (%0)" : : "r"(va) : "memory");
 }
 
+/* Heap frames may be highmem, so clear them through a kmap slot. */
 static void zero_page(unsigned int phys)
 {
-	unsigned int *p = (unsigned int *)phys;
+	unsigned int *p = (unsigned int *)kmap(phys);
 	unsigned int i;
 
 	for (i = 0; i < PAGE_SIZE / 4; i++)
 		p[i] = 0;
+	kunmap(p);
 }
 
 /* Map new pages [from, to) of the arena.  On failure release only the pages
@@ -27,7 +29,7 @@ static int map_pages(struct process *p, unsigned int from, unsigned int to,
 	unsigned int i;
 
 	for (i = from; i < to; i++) {
-		unsigned int phys = mm_alloc_page();
+		unsigned int phys = mm_alloc_user_page();
 		unsigned int va = USER_HEAP_BASE + i * PAGE_SIZE;
 
 		if (!phys)

+ 5 - 4
kernel/sys_net.c

@@ -146,6 +146,7 @@ int sys_net_info(struct sys_netinfo *out)
 			out->chip[i] = chip[i];
 	}
 	out->link = (unsigned int)netdev_link(nd);
+	netdev_byte_counters(&out->rx_bytes, &out->tx_bytes);
 	return 1;
 }
 
@@ -264,7 +265,7 @@ int sys_net_tx(const unsigned char *buf, unsigned int len)
 
 	if (!nd || !owns_nic())
 		return -1;
-	return nd->tx(buf, len);
+	return netdev_tx_frame(nd, buf, len);
 }
 
 int sys_net_rx(unsigned char *buf, unsigned int max)
@@ -273,7 +274,7 @@ int sys_net_rx(unsigned char *buf, unsigned int max)
 
 	if (!nd || !owns_nic())
 		return -1;
-	return nd->rx(buf, max);
+	return netdev_rx_frame(nd, buf, max);
 }
 
 void sys_net_decode(const unsigned char *buf, unsigned int len)
@@ -485,7 +486,7 @@ int sys_tcp_accept(void)
 
 	net_enter();
 	for (i = 0; i < 16; i++) {
-		len = nd->rx(rx, NETDEV_FRAME_MAX);
+		len = netdev_rx_frame(nd, rx, NETDEV_FRAME_MAX);
 		if (len < 0)
 			break;
 		if (tcp_srv_input(rx, (unsigned int)len))
@@ -499,7 +500,7 @@ int sys_tcp_accept(void)
 						   srv_reply,
 						   sizeof(srv_reply));
 			if (rlen > 0)
-				nd->tx(srv_reply, (unsigned int)rlen);
+				netdev_tx_frame(nd, srv_reply, (unsigned int)rlen);
 		}
 	}
 	c = tcp_srv_accept();

+ 18 - 4
kernel/sys_wm.c

@@ -166,16 +166,25 @@ int sys_win_create(const struct win_create *in, struct win_info *out)
 	return r;
 }
 
-/* One serial line when the first application window (normally the boot
- * terminal) presents a frame: the end point of the boot-time measurement
- * ("[boot] ..." lines are relative to the PIT start, this is uptime). */
+/* One serial line when the first terminal presents a frame: the end point of
+ * the boot-time measurement ("[boot] ..." lines are relative to the PIT
+ * start, this is uptime).  The bootscan log window presents before it and is
+ * deliberately not counted here. */
 volatile int wm_first_window_presented;
 
+static int is_term_proc(void)
+{
+	const char *n = current_proc() ? current_proc()->name : "";
+
+	return n[0] == 't' && n[1] == 'e' && n[2] == 'r' && n[3] == 'm' &&
+	       n[4] == '\0';
+}
+
 static void boot_first_present(void)
 {
 	static int done;
 
-	if (done)
+	if (done || !is_term_proc())
 		return;
 	done = 1;
 	wm_first_window_presented = 1;
@@ -295,3 +304,8 @@ int sys_wm_set_cursor(int shape)
 {
 	return wm_server_set_cursor(shape);
 }
+
+int sys_wm_mouse_capture(int on)
+{
+	return wm_server_set_pointer_capture(on);
+}

+ 1 - 0
kernel/sys_wm.h

@@ -31,6 +31,7 @@ int sys_win_key_wait(unsigned int timeout_ms);
 int sys_win_resize_self(unsigned int w, unsigned int h);
 int sys_win_set_cursor(int shape);
 int sys_wm_set_cursor(int shape);
+int sys_wm_mouse_capture(int on);
 
 /* Set once the first application window (the boot terminal) has presented
  * a frame; kernel_main waits for it before the USB bring-up. */

+ 42 - 0
kernel/syscall.c

@@ -2,6 +2,7 @@
 
 #include "syscall.h"
 #include "backlight.h"
+#include "bootcon.h"
 #include "io.h"
 #include "paging.h"
 #include "power.h"
@@ -358,6 +359,8 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return sys_win_set_cursor((int)a1);
 	case SYS_WM_SET_CURSOR:
 		return sys_wm_set_cursor((int)a1);
+	case SYS_WM_MOUSE_CAPTURE:
+		return sys_wm_mouse_capture((int)a1);
 	case SYS_SCREEN_INFO: {
 		struct sys_screen *out = (struct sys_screen *)a1;
 
@@ -479,6 +482,38 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return process_is_alive((unsigned int)a1);
 	case SYS_KILL:
 		return process_kill((unsigned int)a1);
+	case SYS_BOOTLOG:
+		if (a1 == SYS_BOOTLOG_STAT) {
+			struct sys_bootlog *out = (struct sys_bootlog *)a2;
+
+			if (!paging_user_range_ok(current_proc()->cr3,
+						  (unsigned int)out,
+						  sizeof(*out), 1))
+				return -1;
+			out->total = bootcon_scan_total();
+			out->start = bootcon_scan_start();
+			out->capacity = bootcon_scan_capacity();
+			out->active = (unsigned int)bootcon_scan_running();
+			return 0;
+		}
+		if (a1 == SYS_BOOTLOG_READ) {
+			unsigned int off = (unsigned int)a2;
+			unsigned int total = bootcon_scan_total();
+			unsigned int n;
+
+			if (off < bootcon_scan_start())
+				return -1;      /* ring wrapped; re-read the info */
+			if (off >= total)
+				return 0;
+			n = total - off;
+			if (n > SYS_BOOTLOG_CHUNK)
+				n = SYS_BOOTLOG_CHUNK;
+			if (!paging_user_range_ok(current_proc()->cr3,
+						  (unsigned int)a3, n, 1))
+				return -1;
+			return (int)bootcon_scan_read(off, (char *)a3, n);
+		}
+		return -1;
 	case SYS_CHDIR: {
 		char arg[VFS_PATH_MAX], abs[VFS_PATH_MAX];
 		unsigned int size;
@@ -518,6 +553,13 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 	if (p && p->state == PROC_DEAD)
 		process_exit();
 
+	/* The clock is one aligned 32-bit read and needs no lock.  It is the
+	 * hottest syscall (QuickJS's interrupt check, every poll loop), and
+	 * under the BKL a busy kernel holder made each call sleep for a
+	 * scheduler tick: the ION's browser ran its scripts 50x slower. */
+	if (num == SYS_GET_TICKS_MS)
+		return (int)get_ticks_ms();
+
 	bkl_lock();
 	r = syscall_dispatch_locked(num, a1, a2, a3);
 	bkl_unlock();

+ 30 - 3
kernel/tcp.c

@@ -30,6 +30,19 @@
 #define TCP_RXQ_PAGES   16
 #define TCP_SRV_RXQ_SIZE 4096
 
+/* What all client connections together may have in flight towards us.
+ * The NICs are polled, and a small RX ring (64 descriptors on the
+ * RTL8111/8168) overflows when several fast senders each fill a 64 KB
+ * window at once -- the browser's four parallel module downloads on the
+ * ION.  Every overflow costs the peer a retransmission timeout, because
+ * out-of-order segments are not kept, so the loads stalled for minutes.
+ * The budget (~32 full frames) is split evenly between the open client
+ * connections, with a floor of four segments each; a single download
+ * keeps ~45 KB. */
+#define TCP_RX_BUDGET   (32u * TCP_MSS_OURS)
+#define TCP_RX_FLOOR    (4u * TCP_MSS_OURS)
+static unsigned int tcp_rx_share(void);
+
 /* Listening port for the server side (0 = not listening).  Declared here
  * because the shared receive loop feeds non-matching frames to the listener. */
 static unsigned int srv_port;
@@ -167,7 +180,7 @@ static int tcp_send_segment_wnd(struct tcp_conn *c, unsigned char flags,
 	cs = net_checksum_tcp(my_ip, c->dst_ip, tcp, 20 + optlen + len);
 	wr16(tcp + 16, cs);
 
-	return nd->tx(tx_frame, 14 + total);
+	return netdev_tx_frame(nd, tx_frame, 14 + total);
 }
 
 static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
@@ -180,7 +193,10 @@ static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
 	 * is at most 64 KB. */
 	unsigned int wnd = with_mss ? (c->rxq_mask ? c->rxq_mask : TCP_WINDOW)
 				    : rxq_space(c);
+	unsigned int share = tcp_rx_share();
 
+	if (wnd > share)
+		wnd = share;
 	return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
 				    with_mss, wnd);
 }
@@ -317,6 +333,17 @@ struct tcp_client {
 };
 
 static struct tcp_client clients[TCP_CLIENT_MAX];
+
+static unsigned int tcp_rx_share(void)
+{
+	unsigned int i, n = 0, share;
+
+	for (i = 0; i < TCP_CLIENT_MAX; i++)
+		if (clients[i].used)
+			n++;
+	share = TCP_RX_BUDGET / (n ? n : 1);
+	return share < TCP_RX_FLOOR ? TCP_RX_FLOOR : share;
+}
 static unsigned int next_sport = 0xC000;
 
 static struct tcp_client *client_find(unsigned int pid)
@@ -735,7 +762,7 @@ int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
 			return -1;
 
 		{
-			int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
+			int len = netdev_rx_frame(nd, rxbuf, NETDEV_FRAME_MAX);
 
 			if (len < 0)
 				continue;
@@ -1214,7 +1241,7 @@ int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len)
 
 			while ((int)(get_ticks_ms() - deadline) < 0) {
 				struct tcp_seg s;
-				int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
+				int rlen = netdev_rx_frame(nd, rxbuf, NETDEV_FRAME_MAX);
 
 				if (rlen < 0)
 					continue;

+ 5 - 2
kernel/touchpad.c

@@ -352,9 +352,12 @@ int touchpad_init(void)
 	for (r = 0; r < 2; r++) {
 		for (k = 0; k < TP_CTRL_MAX; k++) {
 			unsigned int i = (k + 7) % TP_CTRL_MAX;
-			unsigned int base = ranges[r] + i * TP_CTRL_STEP;
+			unsigned int base = (unsigned int)ioremap(
+				ranges[r] + i * TP_CTRL_STEP, 0x1000,
+				IOREMAP_MMIO);
 
-			paging_map_mmio(base, 0x1000);
+			if (!base)
+				continue;
 			/* A block left in functional reset keeps its
 			 * register file but never clocks the I2C FSM. */
 			i2c_dw_reset_release(base);

+ 4 - 18
kernel/tpec.c

@@ -89,22 +89,6 @@ static void copy_trim(char *dst, unsigned int size, const unsigned char *src)
 	dst[i] = '\0';
 }
 
-/* Map every 4 MB page touched by [addr, addr+len); SMBIOS tables can sit
- * above the 128 MB identity map on larger machines (same approach as
- * acpi.c). */
-static void map_range(unsigned int addr, unsigned int len)
-{
-	unsigned int p = addr & 0xFFC00000u;
-	unsigned int last = (addr + len - 1u) & 0xFFC00000u;
-
-	for (;;) {
-		paging_map_ram(p, 0x400000u);
-		if (p >= last || p > 0xFFC00000u - 0x400000u)
-			break;
-		p += 0x400000u;
-	}
-}
-
 /* The n-th (1-based) string that follows a formatted SMBIOS structure. */
 static const unsigned char *smbios_string(const unsigned char *p,
 					  const unsigned char *end,
@@ -163,8 +147,10 @@ static int smbios_type1(char vendor[16], char product[32], char version[32])
 	    table_addr + table_len < table_addr)
 		return -1;
 
-	map_range(table_addr, table_len);
-	table = (const unsigned char *)table_addr;
+	/* SMBIOS tables can sit above lowmem on larger machines. */
+	table = ioremap(table_addr, table_len, IOREMAP_RAM);
+	if (!table)
+		return -1;
 	end = table + table_len;
 
 	for (p = table; p + 4 <= end; ) {

+ 1 - 0
kernel/uhci.c

@@ -551,4 +551,5 @@ const struct usb_hcd_ops usb_uhci_ops = {
 	0,
 	0,
 	0,
+	0,
 };

+ 10 - 0
kernel/usb.c

@@ -881,6 +881,16 @@ int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 					    mps, buf, len);
 }
 
+void usb_bulk_reset(unsigned int ctrl, unsigned int addr, unsigned int ep,
+		    int dir_in)
+{
+	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_reset ||
+	    usb_scan_foreign())
+		return;
+	usb_ctrls[ctrl].ops->bulk_reset(usb_ctrls[ctrl].local, addr, ep,
+					dir_in);
+}
+
 int usb_bulk_supported(unsigned int ctrl)
 {
 	if (ctrl >= n_usb_ctrls)

+ 4 - 0
kernel/usb.h

@@ -76,6 +76,10 @@ int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		 unsigned int len);
 int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		unsigned int mps, unsigned char *buf, unsigned int len);
+/* Cancel a bulk transfer and reset the endpoint's host-side state (toggle,
+ * armed qTD/ring) after a timeout or a BOT reset. */
+void usb_bulk_reset(unsigned int ctrl, unsigned int addr, unsigned int ep,
+		    int dir_in);
 
 /* 1 when the controller behind `ctrl` implements both bulk directions.
  * UHCI/OHCI do not, so device drivers that need bulk must check this. */

+ 6 - 0
kernel/usb_hcd.h

@@ -90,6 +90,12 @@ struct usb_hcd_ops {
 		       unsigned int ep, unsigned int mps,
 		       unsigned char *buf, unsigned int len);
 
+	/* Abort a bulk transfer and reset the host-side endpoint state
+	 * (data toggle, armed qTD/ring), so the next bulk_in/out starts
+	 * clean after a timeout or a BOT reset.  May be NULL. */
+	void (*bulk_reset)(unsigned int ctrl, unsigned int addr,
+			   unsigned int ep, int dir_in);
+
 	/* Optional: print raw root-port status during `lsusb` (print mode
 	 * only).  Used by EHCI to show PORTSC while debugging routing. */
 	void (*print_ports)(unsigned int ctrl);

+ 123 - 8
kernel/usbmsc.c

@@ -23,6 +23,13 @@
 #define MSC_CHUNK_SECTORS	62u
 #define MSC_BULK_CHUNK		2048u	/* EHCI's fixed bulk-IN buffer */
 #define MSC_TIMEOUT_MS		3000u
+/* The first media access after enumeration can take seconds while the card
+ * or stick finishes its internal initialisation.  It is better to wait on
+ * the outstanding READ(10) than to reset the pipe and restart that work. */
+#define MSC_FIRST_READ_MS	15000u
+/* Best-effort boot probe: one bounded attempt, then leave the device for a
+ * later `storage` rescan. */
+#define MSC_PROBE_MS		5000u
 #define MSC_CBW_SIG		0x43425355u	/* "USBC" LE */
 #define MSC_CSW_SIG		0x53425355u	/* "USBS" LE */
 
@@ -33,6 +40,10 @@ struct usb_msc {
 	unsigned int ep_in, ep_in_mps;	/* endpoint numbers (1..15) */
 	unsigned int ep_out, ep_out_mps;
 	unsigned int iface;
+	unsigned int config;		/* configuration value, for recovery */
+	unsigned int timeout_ms;	/* bulk-IN deadline */
+	unsigned int first_read;	/* media not read since the probe */
+	int failed;			/* last media read failed */
 	unsigned int sectors;
 	unsigned int tag;
 	unsigned int last_status, last_residue;
@@ -43,6 +54,28 @@ struct usb_msc {
 static struct usb_msc msc[USB_MSC_MAX];
 static char msc_names[USB_MSC_MAX][6];
 static unsigned int msc_generation;
+static int msc_probe_only;
+
+void usbmsc_set_probe_only(int on)
+{
+	msc_probe_only = on;
+}
+
+void usbmsc_begin_full_scan(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < USB_MSC_MAX; i++)
+		if (msc[i].present) {
+			msc[i].first_read = 1;
+			msc[i].failed = 0;
+		}
+}
+
+int usbmsc_slot_failed(unsigned int unit)
+{
+	return unit < USB_MSC_MAX && msc[unit].present && msc[unit].failed;
+}
 
 static unsigned int rd32le(const unsigned char *p)
 {
@@ -108,7 +141,11 @@ static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
 			 * byte count is reached. */
 			got += (unsigned int)n;
 			t0 = get_ticks_ms();
-		} else if (get_ticks_ms() - t0 > MSC_TIMEOUT_MS) {
+		} else if (get_ticks_ms() - t0 > m->timeout_ms) {
+			/* A timed-out bulk-IN qTD stays armed in the HCD
+			 * schedule; cancel it or every later transfer sees
+			 * the endpoint as permanently busy. */
+			usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1);
 			return -1;
 		} else {
 			usb_poll_relax(t0);
@@ -120,8 +157,12 @@ static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
 static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf,
 			unsigned int len)
 {
-	return usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
-			    buf, len);
+	if (usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
+			 buf, len) != 0) {
+		usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0);
+		return -1;
+	}
+	return 0;
 }
 
 /* BOT reset + clear both bulk endpoint halts. */
@@ -133,9 +174,28 @@ static void msc_reset(struct usb_msc *m)
 			0x02, 0x01, 0, (unsigned short)(0x80u | m->ep_in));
 	usb_control_out(m->ctrl, m->addr, m->mps, m->low,
 			0x02, 0x01, 0, (unsigned short)m->ep_out);
+	/* CLEAR_FEATURE resets the device's endpoint toggle to DATA0; the
+	 * host side must do the same or the next CBW/data stage is sent with
+	 * the wrong toggle (real EHCI hardware rejects it, QEMU does not). */
+	usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1);
+	usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0);
 	m->tag++;
 }
 
+/* Last-resort recovery for a device whose BOT reset did not restore the
+ * pipes: re-apply the configuration.  That resets the device's endpoints
+ * (and their data toggles) to DATA0 without a port reset, so the address
+ * and the driver's endpoint numbers stay valid. */
+static void msc_reconfigure(struct usb_msc *m)
+{
+	if (!m->config)
+		return;
+	usb_control_out(m->ctrl, m->addr, m->mps, m->low,
+			0x00, 0x09, (unsigned short)m->config, 0);
+	usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1);
+	usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0);
+}
+
 static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
 		   unsigned int cb_len, void *data, unsigned int data_len,
 		   int dir_in)
@@ -174,8 +234,13 @@ static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
 	}
 	m->last_status = csw[12];
 	m->last_residue = rd32le(csw + 8);
-	if (csw[12] != 0)
-		return -1;
+	if (csw[12] != 0) {
+		/* A command that completed with CHECK CONDITION (or another
+		 * SCSI status) has valid sense data; a transport failure does
+		 * not.  -2 tells the recovery path to ask for the sense
+		 * before resetting the pipe. */
+		return -2;
+	}
 	if (m->last_residue != 0)
 		return -1;
 	return 0;
@@ -239,15 +304,55 @@ static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
 static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
 			    unsigned int count, void *buf)
 {
-	unsigned int i;
+	unsigned int i, t0 = get_ticks_ms();
+	int probe_only = msc_probe_only;
 
 	for (i = 0; i < 3; i++) {
-		if (msc_read10(m, lba, count, buf) == 0)
+		int r;
+
+		if (i == 0 && (m->first_read || probe_only))
+			m->timeout_ms = probe_only ? MSC_PROBE_MS :
+						    MSC_FIRST_READ_MS;
+		r = msc_read10(m, lba, count, buf);
+		if (i == 0 && m->first_read) {
+			unsigned int elapsed = get_ticks_ms() - t0;
+
+			m->first_read = 0;
+			m->timeout_ms = MSC_TIMEOUT_MS;
+			if (r == 0 && elapsed > 1000) {
+				serial_puts(SERIAL_COM1,
+					    "usbmsc: first read ");
+				serial_putdec(SERIAL_COM1, elapsed);
+				serial_puts(SERIAL_COM1, " ms\n");
+			}
+		}
+		if (r == 0) {
+			if (probe_only)
+				m->timeout_ms = MSC_TIMEOUT_MS;
 			return 0;
-		msc_sense(m);
+		}
+		if (probe_only) {
+			/* Best-effort probe: do not spend the reset and retry
+			 * chain on a device that did not answer.  `storage`
+			 * will try the full path. */
+			m->timeout_ms = MSC_TIMEOUT_MS;
+			return -1;
+		}
+		/* Only a completed command with a non-zero CSW status has
+		 * sense data.  After a timeout the endpoint is in an
+		 * unknown state, and a REQUEST SENSE would just add another
+		 * multi-second timeout before the reset. */
+		if (r == -2)
+			msc_sense(m);
 		msc_reset(m);
+		/* If the BOT reset alone did not restore the pipes, reset the
+		 * device configuration once before the next retry. */
+		if (i == 0)
+			msc_reconfigure(m);
 		msc_delay(10);
 	}
+	if (probe_only)
+		m->timeout_ms = MSC_TIMEOUT_MS;
 	return -1;
 }
 
@@ -333,6 +438,7 @@ static int msc_config(struct usb_msc *m)
 		return -1;
 
 	m->iface = iface;
+	m->config = config;
 	m->ep_in = ep_in;
 	m->ep_in_mps = ep_in_mps ? ep_in_mps : 64;
 	m->ep_out = ep_out;
@@ -402,6 +508,10 @@ static int device_probe(unsigned int ctrl, unsigned int addr,
 	m->port = port;
 	m->tag = 0;
 	m->iface = 0;
+	m->config = 0;
+	m->timeout_ms = MSC_TIMEOUT_MS;
+	m->first_read = 0;
+	m->failed = 0;
 
 	if (msc_config(m) != 0)
 		return 0;
@@ -434,6 +544,8 @@ static int device_probe(unsigned int ctrl, unsigned int addr,
 
 	msc_set_model(m, inquiry);
 	m->sectors = sectors;
+	m->timeout_ms = MSC_TIMEOUT_MS;
+	m->first_read = 1;
 	m->present = 1;
 
 	serial_puts(SERIAL_COM1, "usbmsc: ");
@@ -526,6 +638,7 @@ static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
 							  (m->sense_asc << 8) |
 							  m->sense_ascq);
 						io_putchar('\n');
+						m->failed = 1;
 						return -1;
 					}
 			} else {
@@ -535,6 +648,7 @@ static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
 				io_puthex((m->sense_key << 16) |
 					  (m->sense_asc << 8) | m->sense_ascq);
 				io_putchar('\n');
+				m->failed = 1;
 				return -1;
 			}
 		}
@@ -542,6 +656,7 @@ static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
 		p += n * 512u;
 		count -= n;
 	}
+	m->failed = 0;
 	return 0;
 }
 

+ 15 - 0
kernel/usbmsc.h

@@ -15,6 +15,21 @@ void usbmsc_register(void);
  * the end of blk_scan(); the USB bus scan itself runs earlier at boot. */
 void usbmsc_blk_scan(void);
 
+/* Best-effort boot mounting: the first media read gets one bounded attempt
+ * instead of the normal retry chain, so a stuck device cannot hold
+ * vfs_scan_busy behind the boot scan.  A later `storage` rescan uses the
+ * full recovery path again. */
+void usbmsc_set_probe_only(int on);
+
+/* A full storage rescan gives every present device its long first-media-read
+ * deadline again (the boot background probe consumes it once). */
+void usbmsc_begin_full_scan(void);
+
+/* 1 when the last media read on this MSC slot failed.  The raw-sector view
+ * uses it to return immediately instead of timing out on a stuck device
+ * again; a full scan clears it. */
+int usbmsc_slot_failed(unsigned int unit);
+
 /* Block I/O for BLK_USB devices (unit = usbmsc slot index). */
 int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
 		unsigned int count);

+ 105 - 11
kernel/vfs.c

@@ -14,6 +14,7 @@
 #include "share.h"
 #include "syscall.h"
 #include "timer.h"
+#include "usbmsc.h"
 
 vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 unsigned int vfs_mount_count;
@@ -607,17 +608,46 @@ static int name_eq(const char *a, const char *b)
 	return a[i] == '\0' && b[i] == '\0';
 }
 
+static unsigned int mbr_dword(const unsigned char *p)
+{
+	return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
+	       ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
+}
+
+/* The NebOS MBR (boot/mbr.S) has a fixed layout: an active, FAT16-looking
+ * P1 at LBA 63 with 1024 sectors (boot sector + kernel) and P2 at LBA 1088
+ * (the rootfs partition, or the dummy entry that keeps the AT3IONT-I BIOS
+ * from classifying the stick as USB-ZIP).  Recognising that layout lets the
+ * boot stick skip the P1 content read, which is one USB round trip on every
+ * boot.  Anything that does not match exactly still goes through the
+ * content check below. */
+static int mbr_is_nebos_layout(const unsigned char *mbr)
+{
+	const unsigned char *p1 = mbr + 0x1BE;
+	const unsigned char *p2 = p1 + 16;
+
+	if (mbr[0] != 0xEB || mbr[1] != 0x42 || mbr[2] != 0x90)
+		return 0;
+	if (p1[0] != 0x80 || p1[4] != 0x06)
+		return 0;
+	if (mbr_dword(p1 + 8) != 63 || mbr_dword(p1 + 12) != 1024)
+		return 0;
+	return mbr_dword(p2 + 8) == 1088;
+}
+
 /* The P1 boot sector keeps only the NebOS header fields; a real FAT volume
  * never carries these values.  Older installed media still has the original
  * FAT-looking BPB (before boot.S was changed), so skip it explicitly instead
- * of mounting the raw kernel partition as a fake filesystem. */
+ * of mounting the raw kernel partition as a fake filesystem.
+ * Returns 1 = NebOS boot partition, 0 = not, -1 = the sector could not be
+ * read (skip it either way; a failed probe must not turn into a mount). */
 static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba)
 {
 	unsigned char s[512];
 	unsigned int i;
 
 	if (blk_read(d, start_lba, s, 1) != 0)
-		return 0;
+		return -1;
 	if (s[510] != 0x55 || s[511] != 0xAA)
 		return 0;
 	if (s[3] != 'N' || s[4] != 'E' || s[5] != 'B' || s[6] != 'O' ||
@@ -627,7 +657,7 @@ static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba)
 		return 0;
 	i = s[0x42] | (s[0x43] << 8) | (s[0x44] << 16) |
 	    ((unsigned int)s[0x45] << 24);
-	return i == ROOTFS_LBA;
+	return i == ROOTFS_LBA ? 1 : 0;
 }
 
 /* ---- mount policies ---- */
@@ -823,7 +853,8 @@ static void part_name(char *out, const char *base, unsigned int idx)
 static void mount_device(blk_dev_t *d, unsigned char *buf)
 {
 	mbr_part_t parts[4];
-	int n, j;
+	fat_info_t fi;
+	int n, j, neb_mbr;
 	int any_fat = 0;
 
 	if (!d->present)
@@ -833,24 +864,35 @@ static void mount_device(blk_dev_t *d, unsigned char *buf)
 		return;
 	}
 
-	/* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
+	/* IDE/SATA/USB: MBR -> FAT partitions, else superfloppy. */
 	if (blk_read(d, 0, buf, 1) != 0)
 		return;
 	n = mbr_parse(buf, parts);
+	neb_mbr = mbr_is_nebos_layout(buf);
 	for (j = 0; j < n; j++) {
 		char pname[16];
+		int neb;
+
 		if (!parts[j].is_fat || parts[j].start_lba == 0 ||
 		    parts[j].sectors == 0)
 			continue;
-		/* The active NebOS partition holds the raw boot sector and
-		 * kernel, not a filesystem (old images still look like FAT). */
-		if (is_nebos_boot_partition(d, parts[j].start_lba))
+		/* The NebOS boot stick's P1 is the raw boot sector + kernel,
+		 * not a filesystem; the layout check above avoids a USB read
+		 * of it on every boot, the content check is the fallback. */
+		if (neb_mbr && parts[j].index == 0)
+			continue;
+		neb = is_nebos_boot_partition(d, parts[j].start_lba);
+		if (neb != 0)		/* NebOS or unreadable: not FAT */
 			continue;
 		any_fat = 1;
 		part_name(pname, d->name, parts[j].index);
 		mount_one(d, parts[j].start_lba, pname);
 	}
-	if (!any_fat)
+	/* A valid MBR whose partitions are all non-FAT (the NebOS boot stick)
+	 * is not a superfloppy; only probe one when the sector has no MBR
+	 * entries, or still looks like a FAT boot sector with stray partition
+	 * bytes (a formatted superfloppy). */
+	if (!any_fat && (n == 0 || fat_detect(buf, &fi) == 0))
 		mount_one(d, 0, d->name);
 }
 
@@ -876,7 +918,7 @@ static void unmount_device(const char *dev)
 	}
 }
 
-void vfs_mount_all(void)
+void vfs_mount_all(int skip_usb)
 {
 	static unsigned char buf[512];
 	unsigned int i;
@@ -896,9 +938,23 @@ void vfs_mount_all(void)
 
 	blk_scan();
 
+	if (!skip_usb) {
+		/* A user-initiated `storage` rescan gets the full USB recovery
+		 * path, including the long first-read deadline. */
+		usbmsc_set_probe_only(0);
+		usbmsc_begin_full_scan();
+	}
+
 	for (i = 0; i < blk_dev_count; i++) {
-		unsigned int t0 = get_ticks_ms();
+		unsigned int t0;
 
+		if (skip_usb && blk_devs[i].kind == BLK_USB) {
+			serial_puts(SERIAL_COM1, "vfs: ");
+			serial_puts(SERIAL_COM1, blk_devs[i].name);
+			serial_puts(SERIAL_COM1, " deferred\n");
+			continue;
+		}
+		t0 = get_ticks_ms();
 		mount_device(&blk_devs[i], buf);
 		serial_puts(SERIAL_COM1, "vfs: ");
 		serial_puts(SERIAL_COM1, blk_devs[i].name);
@@ -924,6 +980,44 @@ static int device_is_mounted(const char *dev)
 	return 0;
 }
 
+/* Mount the USB disks the boot scan deferred.  A card reader or a stick
+ * can NAK its first media read for several seconds while it initialises, so
+ * this runs after the bootscan window has closed: the desktop, /ramfs,
+ * /share and the network are already usable while it waits. */
+void vfs_mount_usb(void)
+{
+	static unsigned char buf[512];
+	unsigned int i;
+	int any = 0;
+
+	vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0;
+	vfs_scan_busy = 1;
+	usbmsc_set_probe_only(1);
+
+	for (i = 0; i < blk_dev_count; i++) {
+		blk_dev_t *d = &blk_devs[i];
+		unsigned int t0;
+
+		if (d->kind != BLK_USB || !d->present ||
+		    device_is_mounted(d->name))
+			continue;
+		t0 = get_ticks_ms();
+		mount_device(d, buf);
+		serial_puts(SERIAL_COM1, "vfs: ");
+		serial_puts(SERIAL_COM1, d->name);
+		serial_puts(SERIAL_COM1, " mount ");
+		serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
+		serial_puts(SERIAL_COM1, " ms\n");
+		any = 1;
+	}
+
+	usbmsc_set_probe_only(0);
+	if (any)
+		serial_puts(SERIAL_COM1, "vfs: usb storage scanned\n");
+
+	vfs_scan_busy = 0;
+}
+
 static void remount(blk_dev_t *d)
 {
 	unsigned char buf[512];

+ 8 - 2
kernel/vfs.h

@@ -78,8 +78,14 @@ extern unsigned int vfs_mount_count;
 
 /* Scan devices and mount all detected FAT filesystems.  The ramfs/share
  * mounts are kept available throughout the scan; file syscalls that run on
- * another CPU wait until it completes (see vfs_scan_busy). */
-void vfs_mount_all(void);
+ * another CPU wait until it completes (see vfs_scan_busy).  skip_usb defers
+ * BLK_USB devices to vfs_mount_usb(), so a slow card reader cannot hold up
+ * the boot scan or the other volumes. */
+void vfs_mount_all(int skip_usb);
+/* Mount the USB block devices registered by the boot scan, appending to the
+ * existing table.  Runs after the boot scan window has closed; safe to call
+ * more than once (already-mounted devices are skipped). */
+void vfs_mount_usb(void);
 /* Re-mount only one block device after it was formatted. */
 void vfs_remount_device(blk_dev_t *d);
 

+ 15 - 7
kernel/vga.c

@@ -16,7 +16,8 @@
 /* Cached boot framebuffer info (set once when vga_fb_* first used). */
 static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
 static unsigned int vga_fb_vram_kb_val;
-static volatile unsigned char *vga_fb_lfb;
+static volatile unsigned char *vga_fb_lfb;   /* kernel address */
+static unsigned int vga_fb_lfb_phys_val;      /* physical LFB */
 static int vga_fb_cached;
 /* UEFI GOP can hand us RGBX instead of the VBE/BGRX byte order.  The shadow
  * buffer is always BGRX, so only the shadow->LFB flush has to swizzle. */
@@ -251,6 +252,7 @@ static void vga_cache_boot_fb(void)
 	if (!info->valid) {
 		vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
 		vga_fb_lfb = 0;
+		vga_fb_lfb_phys_val = 0;
 		gpu_init(0);
 		return;
 	}
@@ -270,7 +272,12 @@ static void vga_cache_boot_fb(void)
 		if (vga_fb_pitch_val < min_pitch)
 			vga_fb_pitch_val = min_pitch;
 	}
-	vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
+	/* The LFB is reached through its paging_init() mapping (the ioremap
+	 * window when it is above lowmem), not its physical address. */
+	vga_fb_lfb_phys_val = info->lfb_base;
+	vga_fb_lfb = (volatile unsigned char *)paging_lfb_va();
+	if (!vga_fb_lfb)
+		vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
 	vga_fb_vram_kb_val = (unsigned int)info->vram_64k * 64;
 	/* Clamp height to what fits in VRAM (prevents wrap on small VRAM cards) */
 	if (vga_fb_vram_kb_val > 0 && vga_fb_pitch_val > 0) {
@@ -339,6 +346,12 @@ unsigned long vga_fb_lfb_addr(void)
 	return (unsigned long)vga_fb_lfb;
 }
 
+unsigned int vga_fb_lfb_phys(void)
+{
+	vga_cache_boot_fb();
+	return vga_fb_lfb_phys_val;
+}
+
 int vga_has_vesa(void)
 {
 	vga_cache_boot_fb();
@@ -627,11 +640,6 @@ void vga_shadow_init(void)
 	if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0)
 		return;
 
-	/* The GPU probe (inside vga_cache_boot_fb) maps its BARs -- including the
-	 * NVIDIA VRAM aperture, which covers the VBE LFB -- uncacheable, so the
-	 * framebuffer mapping is re-applied once everything is up. */
-	paging_lfb_refresh();
-
 	/* On ION-class hardware the framebuffer is stolen DRAM and the VBE LFB
 	 * is just a BAR3 window into it.  The BIOS leaves a 512 MB uncacheable
 	 * MTRR over the PCIe apertures (and the lowest-numbered matching entry

+ 4 - 1
kernel/vga.h

@@ -31,8 +31,11 @@ unsigned int vga_fb_bpp(void);
 unsigned int vga_fb_vram_kb(void);
 unsigned int vga_fb_pci_id(void);
 void *vga_fb_ptr(void);
-/* Physical address of the linear framebuffer (for off-screen VRAM access). */
+/* Kernel address of the linear framebuffer (for off-screen VRAM access);
+ * the LFB's ioremap() window address when it is above lowmem. */
 unsigned long vga_fb_lfb_addr(void);
+/* Physical address of the linear framebuffer (for BAR comparisons). */
+unsigned int vga_fb_lfb_phys(void);
 int vga_has_vesa(void);
 
 /* A drawing surface: the screen's shadow buffer, a window layer or a canvas.

+ 129 - 26
kernel/wm.c

@@ -34,9 +34,12 @@ static unsigned int wm_next_content_gen(void)
  * called by the ring-3 WM every loop, so it is the cheapest place to turn the
  * sampled mouse state into press/release edges. */
 static unsigned int wm_ptr_buttons;
-/* SYS_WM_SET_CURSOR: the WM's shape for its own chrome (resize grip,
+/* SYS_WM_SET_CURSOR: the WM's shape for its own chrome (resize border,
  * title-bar drag, launchers), or CURSOR_NO_OVERRIDE. */
 static int wm_cursor_override = CURSOR_NO_OVERRIDE;
+/* The WM owns the pointer while it drags or resizes a window: no pointer
+ * events reach canvas clients and SYS_WIN_POINTER reports no buttons. */
+static int wm_pointer_owned;
 static int wm_ptr_capture = -1;
 
 /* Ring-3 WM lifecycle.  The kernel only admits the process it spawned; a
@@ -75,17 +78,19 @@ static void wm_close_window(unsigned int idx);
 static void wm_clamp_window(struct window *w, int nx, int ny);
 static void wm_server_unmap_index(unsigned int idx);
 static int wm_window_at(int px, int py);
+static int wm_resize_target(int px, int py, int *edges);
 
-/* Content origin: a normal window has a 1 px border and title bar, a popover
- * is a flat chromeless panel whose canvas fills the window. */
+/* Content origin: a normal window has no visible frame, so its content
+ * starts one title bar below the window origin; a popover is a flat
+ * chromeless panel whose canvas fills the window. */
 static int win_content_x(const struct window *w)
 {
-	return w->x + (w->popover ? 0 : 1);
+	return w->x + (w->popover ? 0 : WM_BORDER_W);
 }
 
 static int win_content_y(const struct window *w)
 {
-	return w->y + (w->popover ? 0 : 1 + WM_TITLE_H);
+	return w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H);
 }
 
 void wm_request_redraw(void)
@@ -208,8 +213,8 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 	unsigned int i, j, pages, cw, ch, base;
 	unsigned int flags;
 	int popover = (in && (in->flags & WIN_F_POPOVER)) ? 1 : 0;
-	unsigned int pad = popover ? 0 : 1;
-	unsigned int top = popover ? 0 : WM_TITLE_H + 1;
+	unsigned int pad = popover ? 0 : WM_BORDER_W;
+	unsigned int top = popover ? 0 : WM_TITLE_H + WM_BORDER_W;
 
 	if (!in || !out)
 		return -1;
@@ -486,11 +491,17 @@ int wm_server_win_pointer(struct win_pointer *out)
 	cy = (int)my - win_content_y(w);
 	out->x = cx;
 	out->y = cy;
-	out->buttons = mouse_buttons_state();
-	out->inside = (cx >= 0 && cy >= 0 &&
-		       cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
-		       (w->focused || wm_window_at((int)mx, (int)my) == idx)) ?
-		      1 : 0;
+	if (wm_pointer_owned) {
+		out->buttons = 0;
+		out->inside = 0;
+	} else {
+		out->buttons = mouse_buttons_state();
+		out->inside = (cx >= 0 && cy >= 0 &&
+			       cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
+			       (w->focused ||
+				wm_window_at((int)mx, (int)my) == idx)) ?
+			       1 : 0;
+	}
 	out->focused = w->focused ? 1 : 0;
 	return 0;
 }
@@ -536,8 +547,8 @@ static int wm_resize_window(unsigned int index, unsigned int cw, unsigned int ch
 	w = &windows[index];
 	if (!w->canvas_window || !process_is_alive(w->pid))
 		return -1;
-	pad = w->popover ? 0 : 1;
-	top = w->popover ? 0 : WM_TITLE_H + 1;
+	pad = w->popover ? 0 : WM_BORDER_W;
+	top = w->popover ? 0 : WM_TITLE_H + WM_BORDER_W;
 	if (cw < 160 || ch < 80 || cw > WM_WIN_MAX_W || ch > WM_WIN_MAX_H)
 		return -1;
 	if (cw * ch * 2 > WM_CONTENT_SLOT || cw * ch * 2 > USER_WIN_RANGE)
@@ -617,24 +628,55 @@ int wm_server_set_cursor(int shape)
 	return 0;
 }
 
+/* Take/release the pointer for a WM drag or resize.  While owned, a held
+ * button is invisible to the canvas clients, so a press that started on a
+ * window edge cannot also drag a scrollbar inside the window. */
+int wm_server_set_pointer_capture(int on)
+{
+	if (!wm_server_is_wm())
+		return -1;
+	wm_pointer_owned = on ? 1 : 0;
+	if (on) {
+		wm_ptr_capture = -1;
+		wm_ptr_buttons = mouse_buttons_state();
+	}
+	return 0;
+}
+
+/* Invisible resize border inside a normal window's outer edge.  Presses here
+ * belong to the ring-3 WM's resize path, not to the content client.  Only the
+ * WM_RESIZE_IN part of the band lies inside the window; the rest is outside
+ * the rectangle and never reaches the content anyway. */
+static int win_in_resize_border(const struct window *w, unsigned int mx,
+				unsigned int my)
+{
+	if (w->popover)
+		return 0;
+	return (int)mx < w->x + WM_RESIZE_IN ||
+	       (int)mx >= w->x + (int)w->pw - WM_RESIZE_IN ||
+	       (int)my < w->y + WM_RESIZE_IN ||
+	       (int)my >= w->y + (int)w->ph - WM_RESIZE_IN;
+}
+
 /* The shape for the pointer at (mx,my): the WM's override, else the
  * content cursor of the window that holds the pointer capture (a drag that
  * started in the content keeps its cursor), else of the window whose
- * content is under the pointer (not on a normal window's resize grip),
+ * content is under the pointer (not on a normal window's resize border),
  * else the default arrow (title bars, the desktop, the top bar). */
 static int wm_cursor_at(unsigned int mx, unsigned int my)
 {
 	int idx;
 	struct window *w;
 	int cx, cy;
+	int edges = 0;
 
 	if (wm_cursor_override != CURSOR_NO_OVERRIDE)
 		return wm_cursor_override;
 	if (wm_ptr_capture >= 0 && windows[wm_ptr_capture].used &&
 	    windows[wm_ptr_capture].canvas_window)
 		return windows[wm_ptr_capture].cursor;
-	idx = wm_window_at((int)mx, (int)my);
-	if (idx < 0)
+	idx = wm_resize_target((int)mx, (int)my, &edges);
+	if (idx < 0 || edges)
 		return CURSOR_DEFAULT;
 	w = &windows[idx];
 	if (!w->canvas_window)
@@ -644,8 +686,7 @@ static int wm_cursor_at(unsigned int mx, unsigned int my)
 	if (cx < 0 || cy < 0 || cx >= (int)w->canvas.w ||
 	    cy >= (int)w->canvas.h)
 		return CURSOR_DEFAULT;
-	if (!w->popover && (int)mx >= w->x + (int)w->pw - 16 &&
-	    (int)my >= w->y + (int)w->ph - 16)
+	if (win_in_resize_border(w, mx, my))
 		return CURSOR_DEFAULT;
 	return w->cursor;
 }
@@ -962,10 +1003,16 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 	unsigned int press = buttons & ~wm_ptr_buttons;
 	unsigned int release = wm_ptr_buttons & ~buttons;
 
+	if (wm_pointer_owned) {
+		wm_ptr_buttons = buttons;
+		return;
+	}
+
 	if (press & 1) {
-		int idx = wm_window_at((int)mx, (int)my);
+		int edges = 0;
+		int idx = wm_resize_target((int)mx, (int)my, &edges);
 
-		if (idx >= 0) {
+		if (idx >= 0 && !edges) {
 			struct window *w = &windows[idx];
 			int cx = (int)mx - win_content_x(w);
 			int cy = (int)my - win_content_y(w);
@@ -973,8 +1020,7 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 			if (w->canvas_window && cx >= 0 && cy >= 0 &&
 			    cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
 			    (w->popover ||
-			     !((int)mx >= w->x + (int)w->pw - 16 &&
-			       (int)my >= w->y + (int)w->ph - 16))) {
+			     !win_in_resize_border(w, mx, my))) {
 				wm_queue_ptr_event(w, WIN_EV_POINTER_DOWN,
 						   cx, cy, buttons);
 				wm_ptr_capture = idx;
@@ -1001,12 +1047,15 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 static void wm_route_scroll(unsigned int mx, unsigned int my)
 {
 	int sdx, sdy;
-	int idx = wm_window_at((int)mx, (int)my);
+	int edges = 0;
+	int idx = wm_resize_target((int)mx, (int)my, &edges);
 	struct window *w;
 	int cx, cy;
 
 	mouse_scroll_take(&sdx, &sdy);
-	if ((!sdx && !sdy) || idx < 0)
+	if (wm_pointer_owned)
+		return;
+	if (edges || (!sdx && !sdy) || idx < 0)
 		return;
 	w = &windows[idx];
 	if (!w->canvas_window)
@@ -1134,6 +1183,7 @@ static void wm_server_wm_died(void)
 	wm_ptr_buttons = 0;
 	wm_ptr_capture = -1;
 	wm_cursor_override = CURSOR_NO_OVERRIDE;
+	wm_pointer_owned = 0;
 	mouse_set_shape(CURSOR_DEFAULT);
 	for (i = 0; i < MAX_WINDOWS; i++) {
 		windows[i].mapped_kind = WM_KIND_NONE;
@@ -1344,6 +1394,59 @@ static int wm_window_at(int px, int py)
 	return -1;
 }
 
+/* Resize band of a normal window: WM_RESIZE_IN px inside and WM_RESIZE_OUT
+ * px outside every edge.  Mirrors the ring-3 WM's resize_edges_at(). */
+static int wm_in_resize_band(const struct window *w, int mx, int my)
+{
+	int l = w->x, t = w->y;
+	int r = l + (int)w->pw, b = t + (int)w->ph;
+	int near_x, near_y;
+
+	if (w->popover || my < WM_BAR_H)
+		return 0;
+	near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
+	near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
+	return (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN) ||
+	       (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT) ||
+	       (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN) ||
+	       (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT);
+}
+
+static int wm_hit_one(struct window *w, int mx, int my, int *edges)
+{
+	*edges = 0;
+	if (!w->used)
+		return 0;
+	if (wm_in_resize_band(w, mx, my)) {
+		*edges = 1;
+		return 1;
+	}
+	return wm_point_in_rect(mx, my, w->x, w->y, w->pw, w->ph);
+}
+
+/* Topmost window for a pointer position, in the WM's painting order.  A
+ * resize band wins over a plain rectangle hit, so a front window's outside
+ * band is not stolen by a window behind it.  *edges is 1 for a band hit. */
+static int wm_resize_target(int px, int py, int *edges)
+{
+	unsigned int i;
+
+	*edges = 0;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].popover &&
+		    wm_hit_one(&windows[i], px, py, edges))
+			return (int)i;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].focused && !windows[i].popover &&
+		    wm_hit_one(&windows[i], px, py, edges))
+			return (int)i;
+	for (i = MAX_WINDOWS; i-- > 0;)
+		if (!windows[i].focused && !windows[i].popover &&
+		    wm_hit_one(&windows[i], px, py, edges))
+			return (int)i;
+	return -1;
+}
+
 static void wm_clamp_window(struct window *w, int nx, int ny)
 {
 	int fbw = (int)vga_fb_width();
@@ -1354,7 +1457,7 @@ static void wm_clamp_window(struct window *w, int nx, int ny)
 	int minx = WM_DRAG_VISIBLE - (int)w->pw;
 	int maxx = fbw - WM_DRAG_VISIBLE;
 	int miny = WM_BAR_H;
-	int maxy = fbh - WM_TITLE_H;
+	int maxy = fbh - WM_BORDER_W - WM_TITLE_H;
 
 	if (w->popover) {
 		/* A popover is an overlay panel: keep all of it on screen,

+ 1 - 0
kernel/wm.h

@@ -92,6 +92,7 @@ int  wm_server_win_scroll(struct win_scroll *out);
 int  wm_server_win_resize_self(unsigned int cw, unsigned int ch);
 int  wm_server_win_set_cursor(int shape);
 int  wm_server_set_cursor(int shape);
+int  wm_server_set_pointer_capture(int on);
 void wm_server_boot_ready(void);
 struct window *wm_focused_window(void);
 int  wm_is_focused(const struct window *w);

+ 43 - 6
kernel/xhci.c

@@ -105,6 +105,7 @@ static unsigned int portsc_neutral(unsigned int ps)
 #define TRB_ADDR_DEV		11
 #define TRB_CONFIG_EP		12
 #define TRB_EVAL_CTX		13
+#define TRB_STOP_EP		15
 #define TRB_TRANSFER		32
 #define TRB_COMPLETION		33
 
@@ -186,7 +187,8 @@ struct xhci_bulk {
 	int error;
 };
 
-static unsigned int mmio_base;
+static unsigned int mmio_base;   /* kernel mapping of BAR0 */
+static unsigned int mmio_phys;   /* BAR0 itself (logged, reported) */
 static unsigned int cap_len;
 static unsigned int rt_off;
 static unsigned int db_off;
@@ -1246,6 +1248,38 @@ static int xhci_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 	return 0;
 }
 
+/* Drop a bulk transfer after a timeout or a BOT reset.  Stop the endpoint
+ * first so the xHC no longer references the old ring page, then clear the
+ * software state; the next bulk call configures a fresh ring. */
+static void xhci_bulk_reset(unsigned int ctrl, unsigned int addr,
+			    unsigned int ep, int dir_in)
+{
+	struct xhci_slot *s = slot_by_addr(addr);
+	struct xhci_bulk *b = dir_in ? &bulk_in : &bulk_out;
+	unsigned int flags, slot, ep_id;
+
+	(void)ctrl;
+	if (!found || !s)
+		return;
+
+	spin_lock_irqsave(&xhci_lock, &flags);
+	if (!b->active || b->slot != s->slot_id || b->ep != ep) {
+		spin_unlock_irqrestore(&xhci_lock, flags);
+		return;
+	}
+	slot = b->slot;
+	ep_id = b->ep_id;
+	spin_unlock_irqrestore(&xhci_lock, flags);
+
+	xhci_cmd(0, 0, TRB_TYPE(TRB_STOP_EP) | (slot << 24) | (ep_id << 16),
+		 0);
+
+	spin_lock_irqsave(&xhci_lock, &flags);
+	if (b->active && b->slot == slot && b->ep == ep)
+		bulk_reset(b);
+	spin_unlock_irqrestore(&xhci_lock, flags);
+}
+
 static int xhci_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		    unsigned int mps, const unsigned char *buf,
 		    unsigned int len)
@@ -1583,8 +1617,10 @@ found_xhci:
 	cmd = pci_cfg_read32(pci_bus, pci_dev, pci_func, 0x04);
 	pci_cfg_write32(pci_bus, pci_dev, pci_func, 0x04, cmd | 0x0006);
 
-	mmio_base = bar;
-	paging_map_mmio(mmio_base, 0x10000);
+	mmio_phys = bar;
+	mmio_base = (unsigned int)ioremap(bar, 0x10000, IOREMAP_MMIO);
+	if (!mmio_base)
+		return -1;
 
 	pci_id = pci_cfg_read32(pci_bus, pci_dev, pci_func, 0x00);
 	cap_len = mmio_rd(0) & 0xFF;
@@ -1608,7 +1644,7 @@ found_xhci:
 	ctx_size = (hcc_params & 0x0004u) ? 64 : 32;
 
 	dbg("xhci: found\n");
-	dbg_hex("xhci: mmio ", mmio_base);
+	dbg_hex("xhci: mmio ", mmio_phys);
 	dbg_dec("xhci: slots ", max_slots);
 	dbg_dec("xhci: ports ", max_ports);
 	dbg_dec("xhci: cap len ", cap_len);
@@ -1761,7 +1797,7 @@ found_xhci:
 	scan_protocols();
 	xlog_hex("xhci: ", pci_id);
 	xlog_hex(" hci ", hci_version);
-	xlog_hex(" mmio ", mmio_base);
+	xlog_hex(" mmio ", mmio_phys);
 	xlog_hex(" ports ", port_mask);
 	xlog_hex(" csz ", ctx_size);
 	xlog_hex(" scratch ", n);
@@ -1797,7 +1833,7 @@ static void xhci_info(unsigned int ctrl, unsigned int *bus,
 	*bus = pci_bus;
 	*dev = pci_dev;
 	*func = pci_func;
-	*base = mmio_base;
+	*base = mmio_phys;
 	*mmio = 1;
 }
 
@@ -1823,5 +1859,6 @@ const struct usb_hcd_ops usb_xhci_ops = {
 	stop_int,
 	xhci_bulk_out,
 	xhci_bulk_in,
+	xhci_bulk_reset,
 	0,
 };

+ 78 - 0
testdata/browser/compat/074-font-variants.html

@@ -0,0 +1,78 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>074 font-face variants</title>
+<style>
+/* The regular face lists WOFF2 first (unsupported, skipped) and a WOFF 1.0
+ * copy of the built-in mono face second. */
+@font-face {
+  font-family: "Ladder Var";
+  src: local("Ladder Var"), url("ladder-mono.woff2") format("woff2"),
+       url("ladder-mono.woff") format("woff");
+}
+/* The bold face of the same family is the built-in sans bold. */
+@font-face {
+  font-family: "Ladder Var";
+  font-weight: 700;
+  src: url("ladder-sans-b.ttf") format("truetype");
+}
+/* A non-Latin subset with the same family/weight/style must not replace
+ * the regular face (Google Fonts lists latin last). */
+@font-face {
+  font-family: "Ladder Var";
+  unicode-range: U+0400-045F, U+0490-0491;
+  src: url("does-not-exist-cyrillic.ttf") format("truetype");
+}
+/* Never referenced: must not be fetched (check the [font] log). */
+@font-face {
+  font-family: "Never Used";
+  src: url("never-used.ttf") format("truetype");
+}
+html, body { margin: 0; padding: 0; background: #ffffff; color: #202020; }
+body { font-family: sans-serif; font-size: 13px; padding: 10px; }
+h1 { font-size: 20px; margin: 0 0 6px 0; }
+p { margin: 0 0 10px 0; }
+.pane { float: left; width: 440px; margin-left: 10px; }
+.label { background: #eeeeee; padding: 4px 6px; margin-bottom: 4px; }
+.box { width: 400px; background: #f4f4f4; border: 1px solid #999999; padding: 6px; margin-bottom: 8px; }
+.var { font-family: "Ladder Var", sans-serif; font-size: 15px; }
+.var-b { font-family: "Ladder Var", sans-serif; font-size: 15px; font-weight: bold; }
+.short { font: 15px "Ladder Var", sans-serif; }
+.mono { font-family: monospace; font-size: 15px; }
+.sans-b { font-family: sans-serif; font-size: 15px; font-weight: bold; }
+#result { clear: both; margin-top: 14px; padding: 8px; background: #eeeeee; color: #000000; font-family: monospace; font-weight: bold; }
+</style>
+</head>
+<body>
+<h1>074 &middot; @font-face variants</h1>
+<p>One family with a WOFF regular face and a TTF bold face; the font shorthand names the family too.  Each A row must look like its B row.</p>
+<div class="pane">
+  <div class="label">A &middot; "Ladder Var"</div>
+  <div class="box"><span class="var">Regular: the quick brown fox 0123</span></div>
+  <div class="box"><span class="var-b">Bold: the quick brown fox 0123</span></div>
+  <div class="box"><span class="short">Shorthand: the quick brown fox 0123</span></div>
+</div>
+<div class="pane">
+  <div class="label">B &middot; reference</div>
+  <div class="box"><span class="mono">Regular: the quick brown fox 0123</span></div>
+  <div class="box"><span class="sans-b">Bold: the quick brown fox 0123</span></div>
+  <div class="box"><span class="mono">Shorthand: the quick brown fox 0123</span></div>
+</div>
+<div id="result">checking...</div>
+<script>
+/* The font rendering itself is the A/B comparison above (A must look like
+ * B row by row).  The script checks the CSS Font Loading API surface. */
+setTimeout(function () {
+  var ok = typeof FontFace === 'function' &&
+           new FontFace('X', 'url(x.ttf)').status === 'loaded' &&
+           document.fonts.size === 0 &&
+           document.fonts.check('15px "Ladder Var"') &&
+           typeof document.fonts.ready.then === 'function';
+  var out = document.getElementById('result');
+  out.textContent = (ok ? 'PASS' : 'FAIL') + ': FontFace / document.fonts API';
+  out.style.background = ok ? '#ccffcc' : '#ffcccc';
+}, 300);
+</script>
+</body>
+</html>

Некоторые файлы не были показаны из-за большого количества измененных файлов