Просмотр исходного кода

Grow the kernel region and add MMIO mapping plus debug tools

Johannes Zellner 1 месяц назад
Родитель
Сommit
c7fa38691a
27 измененных файлов с 500 добавлено и 80 удалено
  1. 5 2
      Makefile
  2. 6 6
      bootstrap.c
  3. 1 1
      colors.c
  4. 1 1
      disk.c
  5. 9 1
      kdbg.c
  6. 4 0
      kdbg.h
  7. 1 1
      kernel.ld
  8. 15 4
      kexec.c
  9. 10 0
      kexec.h
  10. 1 1
      kexec_tramp.S
  11. 202 25
      lspci.c
  12. 20 13
      mtrr.c
  13. 1 1
      netcmd.c
  14. 11 3
      paging.c
  15. 5 0
      paging.h
  16. 91 0
      peek.c
  17. 12 1
      process.c
  18. 8 1
      process.h
  19. 2 2
      radeon.c
  20. 3 3
      run.c
  21. 1 1
      run_api.h
  22. 2 2
      serial_recv.c
  23. 84 6
      terminal.c
  24. 1 1
      tss.c
  25. 1 1
      uhci.c
  26. 2 2
      user.ld
  27. 1 1
      vesa_stage.S

+ 5 - 2
Makefile

@@ -137,6 +137,9 @@ kdbg.o: kdbg.c kdbg.h font_8x8.h timer.h vga.h
 lspci.o: lspci.c io.h pci.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+peek.o: peek.c io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 ne2000.o: ne2000.c ne2000.h pci.h timer.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -233,7 +236,7 @@ syscall_stub.o: syscall_stub.S
 mem.o: mem.c mm.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o hello_embed.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o peek.o ne2000.o netcmd.o net.o hello_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -409,4 +412,4 @@ dev: kernel.bin kexec_send
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o kdbg.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o peek.o ne2000.o netcmd.o net.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send

+ 6 - 6
bootstrap.c

@@ -3,8 +3,8 @@
  * full hardware access: it switches COM1 to 115200, receives a new kernel image,
  * copies it over 0x7E00, and jumps to its kernel_main.
  *
- * Self-contained: no kernel symbols, no run_api dependency. We live at 0x31000
- * (outside the 0x7E00..0x30000 kernel region), so we can overwrite the kernel
+ * Self-contained: no kernel symbols, no run_api dependency. We live at 0x41000
+ * (outside the 0x7E00..0x40000 kernel region), so we can overwrite the kernel
  * directly from our own code without a relocated trampoline.
  *
  * Wire protocol (host -> us), after we emit 'R':
@@ -16,11 +16,11 @@
  */
 
 #define UART_BASE        0x3F8
-#define STAGE_ADDR       0x50000
+#define STAGE_ADDR       0x58000
 #define KERNEL_LOAD_ADDR 0x7E00
-#define BOOT_STACK       0x30800
+#define BOOT_STACK       0x40800
 #define MAGIC            0x4345584B /* "KXEC" little-endian */
-#define MAX_SIZE         0x28200
+#define MAX_SIZE         0x38200
 
 static inline void outb(unsigned short port, unsigned char v)
 {
@@ -148,7 +148,7 @@ void _start(void)
 	}
 
 	/* Copy staged kernel over 0x7E00, then jump to its kernel_main.
-	 * We live at 0x31000, outside the overwritten region, so this is safe. */
+	 * We live at 0x41000, outside the overwritten region, so this is safe. */
 	setup_flat_gdt();
 	for (i = 0; i < size; i++)
 		((volatile unsigned char *)KERNEL_LOAD_ADDR)[i] =

+ 1 - 1
colors.c

@@ -16,7 +16,7 @@
 /* Precomputed hue-to-RGB lookup table. Packed as 0x00BBGGRR for fast 32-bit load.
  * The hue cycle is RAINBOW_FULL entries, but the table is decimated by
  * LUT_STEP: a 4-hue step is ~1.5% of a color segment and invisible, and the
- * smaller table keeps the kernel image under the 0x30000 memory-bound. */
+ * smaller table keeps the kernel image under the 0x40000 memory-bound. */
 #define LUT_STEP      4
 #define LUT_SIZE      (RAINBOW_FULL / LUT_STEP)
 

+ 1 - 1
disk.c

@@ -7,7 +7,7 @@
 #define SECTOR_SIZE       512
 #define SECTORS_PER_TRACK 18
 #define HEADS             2
-#define DMA_BUF_ADDR      0x30000
+#define DMA_BUF_ADDR      0x4000   /* low RAM below the kernel; single sector (64 KB-safe) */
 
 /* FDC I/O ports */
 #define FDC_DOR   0x3F2

+ 9 - 1
kdbg.c

@@ -19,9 +19,10 @@ static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
 static volatile unsigned char *fb_lfb;
 static int fb_ok;
 
-#define KDBG_INFO_MAX 10
+#define KDBG_INFO_MAX 14
 static char kdbg_info_buf[KDBG_INFO_MAX][40];
 static unsigned char kdbg_info_used[KDBG_INFO_MAX];
+static unsigned int kdbg_info_next;
 
 void kdbg_init(void)
 {
@@ -166,6 +167,13 @@ static void kdbg_redraw_info(void)
 		kdbg_draw_info_row(r);
 }
 
+int kdbg_info_alloc(void)
+{
+	if (kdbg_info_next >= KDBG_INFO_MAX)
+		return -1;
+	return (int)kdbg_info_next++;
+}
+
 void kdbg_status(void)
 {
 	char buf[96];

+ 4 - 0
kdbg.h

@@ -23,6 +23,10 @@ void kdbg_panic(const char *name, unsigned int eip, unsigned int cr2);
    framebuffer is up. */
 void kdbg_info(unsigned int row, const char *s);
 
+/* Allocate the next free info row (boot subsystems claim rows in init
+   order).  Returns -1 when every row is already taken. */
+int kdbg_info_alloc(void);
+
 /* Counters shared by the keyboard IRQ, the WM and the terminal. */
 extern volatile unsigned int kdbg_irq_count;   /* IRQ1 entries */
 extern volatile unsigned int kdbg_irq_bytes;   /* scancode bytes read */

+ 1 - 1
kernel.ld

@@ -17,4 +17,4 @@ SECTIONS
 	}
 	_end = .;
 }
-ASSERT(_end <= 0x30000, "Kernel too large would overwrite stack/DMA at 0x30000");
+ASSERT(_end <= 0x40000, "Kernel too large would overwrite stack/DMA at 0x40000");

+ 15 - 4
kexec.c

@@ -5,7 +5,7 @@
  * LFB are still valid) and jump straight to the new kernel's kernel_main,
  * which re-inits IDT/PIC/PIT/mouse/process/VGA-shadow/terminal from scratch.
  *
- * Because this code lives at 0x7E00..0x30000 (inside the region we overwrite),
+ * Because this code lives at 0x7E00..0x40000 (inside the region we overwrite),
  * the final "copy image to 0x7E00 and jump" is done by a small
  * position-independent trampoline relocated to 0x800 (below the kernel).
  *
@@ -26,10 +26,14 @@
 #include "serial.h"
 
 #define KEXEC_MAGIC 0x4345584B /* "KXEC" little-endian */
-#define KEXEC_STAGE 0x50000    /* staging buffer for the received image */
-#define KEXEC_LOAD  0x7E00     /* where the kernel must be placed */
+
+/* The staging buffer must hold a full kernel image and stay below the VGA
+   window (0xA0000). */
+#if KEXEC_STAGE + KEXEC_MAX > 0xA0000
+#error "kexec staging overlaps the VGA aperture"
+#endif
+
 #define KEXEC_TRAMP 0x800      /* relocated trampoline (below the kernel) */
-#define KEXEC_MAX   0x28200    /* max = kernel region 0x7E00..0x30000 */
 
 volatile int kexec_busy;
 volatile unsigned int kexec_progress;
@@ -148,6 +152,13 @@ void program_kexec_main(void)
 	io_puthex(entry);
 	io_putstr("\n");
 
+	kexec_jump(entry, size);
+}
+
+/* Relocate the trampoline to 0x800 (it survives the kernel overwrite) and
+ * jump to it with the staging/load/size/entry arguments.  Never returns. */
+void kexec_jump(unsigned int entry, unsigned int size)
+{
 	/* Relocate the trampoline to 0x800 so it survives the kernel overwrite. */
 	{
 		const void *s = (const void *)kexec_trampoline;

+ 10 - 0
kexec.h

@@ -4,6 +4,16 @@
 
 void program_kexec_main(void);
 
+/* Staging buffer for a received kernel image, and the maximum image size
+ * (the kernel region 0x7E00..0x40000).  Below the VGA window; see AGENTS.md. */
+#define KEXEC_STAGE 0x58000
+#define KEXEC_LOAD  0x7E00
+#define KEXEC_MAX   0x38200
+
+/* Copy the staged image (KEXEC_STAGE, `size` bytes) over the kernel region,
+ * relocate the trampoline and jump to `entry`.  Never returns. */
+void kexec_jump(unsigned int entry, unsigned int size);
+
 /* Non-zero while a kexec receive is in progress (prevents double-trigger). */
 extern volatile int kexec_busy;
 

+ 1 - 1
kexec_tramp.S

@@ -66,7 +66,7 @@ kexec_trampoline:
 	 * this the jump below can execute stale instructions. */
 	wbinvd
 
-	movl  $0x30800, %esp
+	movl  $0x40800, %esp
 	jmp   *%edx
 
 .globl kexec_trampoline_end

+ 202 - 25
lspci.c

@@ -1,4 +1,15 @@
-/* lspci command: enumerate PCI devices (vendor:device, class, BAR0). */
+/* lspci command: enumerate PCI functions.
+ *
+ *   lspci            one line per function:
+ *                      bb:dd.f vvvv:dddd rNN class CCSS irq NN
+ *   lspci <filter>   additionally print BAR details for every function whose
+ *                    "vvvv:dddd" or "CCSS" contains <filter> (hex, case
+ *                    insensitive), e.g. `lspci 8168` or `lspci 0200`.
+ *
+ * BAR sizes are probed by writing all-ones and restoring the original value
+ * (the same trick lspci(8) uses).  Probing only happens for filter matches,
+ * so a plain `lspci` never touches config space.
+ */
 
 #include "io.h"
 #include "pci.h"
@@ -22,39 +33,205 @@ static void put_hex8(unsigned int v)
 	put_hex4(v & 0xFFFF);
 }
 
-void program_lspci_main(void)
+static void put_dec(unsigned int v)
 {
-	unsigned int bus, dev;
+	char b[12];
+	int i = 0;
+
+	if (v == 0) {
+		io_putchar('0');
+		return;
+	}
+	while (v) {
+		b[i++] = (char)('0' + (v % 10));
+		v /= 10;
+	}
+	while (i)
+		io_putchar(b[--i]);
+}
+
+static void put_size(unsigned int bytes)
+{
+	if (bytes == 0) {
+		io_putchar('?');
+	} else if (bytes >= 1024u * 1024u) {
+		put_dec(bytes / (1024u * 1024u));
+		io_putchar('M');
+	} else if (bytes >= 1024u) {
+		put_dec(bytes / 1024u);
+		io_putchar('K');
+	} else {
+		put_dec(bytes);
+		io_putchar('B');
+	}
+}
+
+static void hex_str(char *dst, unsigned int v, unsigned int digits)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	unsigned int i;
+
+	for (i = 0; i < digits; i++)
+		dst[i] = hex[(v >> (4 * (digits - 1 - i))) & 0xF];
+	dst[digits] = '\0';
+}
+
+static char lcc(char c)
+{
+	return (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c;
+}
+
+static int contains_ci(const char *hay, const char *needle)
+{
+	unsigned int i, j;
+
+	if (!needle[0])
+		return 1;
+	for (i = 0; hay[i]; i++) {
+		for (j = 0; needle[j] && hay[i + j] &&
+			    lcc(hay[i + j]) == lcc(needle[j]); j++)
+			;
+		if (!needle[j])
+			return 1;
+	}
+	return 0;
+}
+
+static const char *class_name(unsigned int cls)
+{
+	switch (cls) {
+	case 0x00: return "legacy";
+	case 0x01: return "storage";
+	case 0x02: return "net";
+	case 0x03: return "display";
+	case 0x04: return "multimedia";
+	case 0x06: return "bridge";
+	case 0x08: return "system";
+	case 0x0C: return "serial";
+	default:   return 0;
+	}
+}
+
+/* Print one BAR; returns the number of config slots consumed (2 for 64-bit). */
+static unsigned int print_bar(unsigned int bus, unsigned int dev,
+			      unsigned int func, unsigned int index)
+{
+	unsigned int reg = 0x10u + 4u * index;
+	unsigned int val = pci_cfg_read32(bus, dev, func, reg);
+	unsigned int is64, hi, mask;
+
+	if (val == 0 || val == 0xFFFFFFFFu)
+		return 1;                        /* not implemented */
+	if (!(val & 1u) && !(val & 0xFFFFFFF0u))
+		return 1;                        /* mem BAR without a base */
+
+	io_putstr("  bar");
+	put_dec(index);
+	io_putstr((val & 1u) ? " i/o " : " mem ");
+
+	if (val & 1u) {
+		pci_cfg_write32(bus, dev, func, reg, 0xFFFFFFFFu);
+		mask = pci_cfg_read32(bus, dev, func, reg) & 0xFFFFFFFCu;
+		pci_cfg_write32(bus, dev, func, reg, val);
+		put_hex4(val & 0xFFFCu);
+		io_putstr(" (");
+		put_size(mask ? (~mask + 1u) : 0);
+		io_putchar(')');
+		io_putchar('\n');
+		return 1;
+	}
+
+	is64 = (val & 4u) ? 1u : 0u;
+	hi = is64 ? pci_cfg_read32(bus, dev, func, reg + 4) : 0;
+	pci_cfg_write32(bus, dev, func, reg, 0xFFFFFFFFu);
+	if (is64)
+		pci_cfg_write32(bus, dev, func, reg + 4, 0xFFFFFFFFu);
+	mask = pci_cfg_read32(bus, dev, func, reg) & 0xFFFFFFF0u;
+	pci_cfg_write32(bus, dev, func, reg, val);
+	if (is64)
+		pci_cfg_write32(bus, dev, func, reg + 4, hi);
+
+	if (hi)
+		put_hex8(hi);
+	put_hex8(val & 0xFFFFFFF0u);
+	io_putstr(" (");
+	put_size(mask ? (~mask + 1u) : 0);
+	if (is64)
+		io_putstr(", 64b");
+	if (val & 8u)
+		io_putstr(", pf");
+	io_putchar(')');
+	io_putchar('\n');
+	return is64 ? 2u : 1u;
+}
+
+void program_lspci_main(const char *filter)
+{
+	unsigned int bus, dev, func, nfunc, matches = 0;
 
 	for (bus = 0; bus < 256; bus++) {
 		for (dev = 0; dev < 32; dev++) {
 			unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
-			unsigned int class_rev, bar0;
-			unsigned int vendor, device, cls, sub;
+			unsigned int htype;
 
 			if (id == 0xFFFFFFFFu || id == 0)
 				continue;
 
-			vendor = id & 0xFFFFu;
-			device = (id >> 16) & 0xFFFFu;
-			class_rev = pci_cfg_read32(bus, dev, 0, 0x08);
-			cls = (class_rev >> 24) & 0xFFu;
-			sub = (class_rev >> 16) & 0xFFu;
-			bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
-
-			put_hex2(bus);
-			io_putchar(':');
-			put_hex2(dev);
-			io_putstr("  ");
-			put_hex4(vendor);
-			io_putchar(':');
-			put_hex4(device);
-			io_putstr("  class=");
-			put_hex2(cls);
-			put_hex2(sub);
-			io_putstr("  bar0=");
-			put_hex8(bar0);
-			io_putchar('\n');
+			/* Multifunction if header type bit 7 is set. */
+			htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
+			nfunc = (htype & 0x80u) ? 8u : 1u;
+
+			for (func = 0; func < nfunc; func++) {
+				unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
+				unsigned int class_rev, cls, sub, irq, b;
+				const char *cn;
+				char vd[10], cs[5];
+
+				if (idf == 0xFFFFFFFFu || idf == 0)
+					continue;
+
+				class_rev = pci_cfg_read32(bus, dev, func, 0x08);
+				cls = (class_rev >> 24) & 0xFFu;
+				sub = (class_rev >> 16) & 0xFFu;
+				irq = pci_cfg_read32(bus, dev, func, 0x3C) & 0xFFu;
+
+				hex_str(vd, idf & 0xFFFFu, 4);
+				vd[4] = ':';
+				hex_str(vd + 5, (idf >> 16) & 0xFFFFu, 4);
+				hex_str(cs, (cls << 8) | sub, 4);
+
+				put_hex2(bus);
+				io_putchar(':');
+				put_hex2(dev);
+				io_putchar('.');
+				io_putchar((char)('0' + func));
+				io_putchar(' ');
+				io_putstr(vd);
+				io_putstr(" r");
+				put_hex2(class_rev & 0xFFu);
+				io_putstr(" class ");
+				io_putstr(cs);
+				cn = class_name(cls);
+				if (cn) {
+					io_putchar(' ');
+					io_putstr(cn);
+				}
+				io_putstr(" irq ");
+				put_dec(irq);
+				io_putchar('\n');
+
+				if (!contains_ci(vd, filter) && !contains_ci(cs, filter))
+					continue;
+				matches++;
+				for (b = 0; b < 6; )
+					b += print_bar(bus, dev, func, b);
+			}
 		}
 	}
+
+	if (filter[0] && matches == 0) {
+		io_putstr("lspci: no match for ");
+		io_putstr(filter);
+		io_putchar('\n');
+	}
 }

+ 20 - 13
mtrr.c

@@ -109,14 +109,14 @@ static const char *type_name(unsigned int t)
 	}
 }
 
-/* Dump MTRRdefType and the variable entries to kdbg and serial.  Returns
- * the next free kdbg info row. */
-static unsigned int dump_mtrrs(unsigned int vcnt)
+/* Dump MTRRdefType and the variable entries to kdbg and serial.  Rows come
+ * from the kdbg info pool, so other subsystems keep their own. */
+static void dump_mtrrs(unsigned int vcnt)
 {
 	char buf[56];
 	char *p;
 	unsigned int i, lo, hi, mlo, mhi;
-	unsigned int row = 0;
+	int row;
 
 	rdmsr(MSR_MTRR_DEF_TYPE, &lo, &hi);
 	p = buf;
@@ -125,7 +125,9 @@ static unsigned int dump_mtrrs(unsigned int vcnt)
 	p = app(p, " vcnt ");
 	*p++ = (char)('0' + (vcnt > 9 ? 9 : vcnt));
 	*p = '\0';
-	kdbg_info(row++, buf);
+	row = kdbg_info_alloc();
+	if (row >= 0)
+		kdbg_info((unsigned int)row, buf);
 	serial_puts(SERIAL_COM1, buf);
 	serial_puts(SERIAL_COM1, "\n");
 
@@ -147,11 +149,12 @@ static unsigned int dump_mtrrs(unsigned int vcnt)
 			p = app(p, type_name(lo));
 		}
 		*p = '\0';
-		kdbg_info(row++, buf);
+		row = kdbg_info_alloc();
+		if (row >= 0)
+			kdbg_info((unsigned int)row, buf);
 		serial_puts(SERIAL_COM1, buf);
 		serial_puts(SERIAL_COM1, "\n");
 	}
-	return row;
 }
 
 void mtrr_set_wc(unsigned int base, unsigned int size)
@@ -161,7 +164,7 @@ void mtrr_set_wc(unsigned int base, unsigned int size)
 	unsigned int vcnt;
 	unsigned int i, use = ~0u;
 	unsigned int region_size, align, physmask;
-	unsigned int row;
+	int row;
 	char buf[56];
 	char *p;
 
@@ -175,9 +178,9 @@ void mtrr_set_wc(unsigned int base, unsigned int size)
 	if (vcnt == 0)
 		return;
 
-	/* Dump the first 8 at most (kdbg has 10 info rows); the search
-	 * below still covers every variable range. */
-	row = dump_mtrrs(vcnt > 8 ? 8 : vcnt);
+	/* Dump the first 8 at most (kdbg info rows are a shared pool); the
+	 * search below still covers every variable range. */
+	dump_mtrrs(vcnt > 8 ? 8 : vcnt);
 
 	/* Round size up to the next power of two (min 4 KB). */
 	region_size = 0x1000;
@@ -213,7 +216,9 @@ void mtrr_set_wc(unsigned int base, unsigned int size)
 	if (use == ~0u) {
 		p = app(p, "WC skipped: no free MTRR entry");
 		*p = '\0';
-		kdbg_info(row, buf);
+		row = kdbg_info_alloc();
+		if (row >= 0)
+			kdbg_info((unsigned int)row, buf);
 		serial_puts(SERIAL_COM1, "mtrr: no free variable entry; skipping WC\n");
 		return;
 	}
@@ -241,7 +246,9 @@ void mtrr_set_wc(unsigned int base, unsigned int size)
 	p = app(p, " s=");
 	p = hex8(p, region_size);
 	*p = '\0';
-	kdbg_info(row, buf);
+	row = kdbg_info_alloc();
+	if (row >= 0)
+		kdbg_info((unsigned int)row, buf);
 
 	serial_puts(SERIAL_COM1, "mtrr: LFB WC [");
 	serial_puthex(SERIAL_COM1, base);

+ 1 - 1
netcmd.c

@@ -7,7 +7,7 @@
 
 /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
    hold a full-size frame, and keeping them out of BSS-as-much-as-possible
-   avoids bloating the kernel past its 0x30000 limit). Max Ethernet frame is
+   avoids bloating the kernel past its 0x40000 limit). Max Ethernet frame is
    1518 bytes (14 header + 1500 payload + 4 VLAN). */
 #define NET_FRAME_MAX 1518
 static unsigned char net_rx_buf[NET_FRAME_MAX];

+ 11 - 3
paging.c

@@ -134,13 +134,21 @@ int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
 	return 1;
 }
 
-void paging_map_mmio(unsigned int phys_base, unsigned int size)
+/* 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)
 {
+	unsigned int *pd = (unsigned int *)cr3;
 	unsigned int p0 = phys_base >> 22;
 	unsigned int p1 = (phys_base + size + 0x3FFFFFu) >> 22;
 	unsigned int i;
 
 	for (i = p0; i < p1 && i < 1024; i++)
-		page_dir[i] = (i << 22) | PDE_PS | PDE_PCD |
-			      PDE_WRITABLE | PDE_PRESENT;
+		pd[i] = (i << 22) | PDE_PS | PDE_PCD |
+			PDE_WRITABLE | PDE_PRESENT;
+}
+
+void paging_map_mmio(unsigned int phys_base, unsigned int size)
+{
+	paging_map_mmio_cr3((unsigned int)page_dir, phys_base, size);
 }

+ 5 - 0
paging.h

@@ -26,4 +26,9 @@ int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
  * 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

+ 91 - 0
peek.c

@@ -0,0 +1,91 @@
+/* peek command: hex dump of memory (identity-mapped, so this also covers
+ * MMIO registers, e.g. a driver's BAR once mapped).
+ *
+ *   peek <addr> [len]      default len 64, max 256
+ *
+ * Reading an unmapped address faults; the exception handler shows EIP/CR2.
+ */
+
+#include "io.h"
+
+static void put_hex2(unsigned int v)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	io_putchar(hex[(v >> 4) & 0xF]);
+	io_putchar(hex[v & 0xF]);
+}
+
+static void put_hex8(unsigned int v)
+{
+	put_hex2((v >> 24) & 0xFF);
+	put_hex2((v >> 16) & 0xFF);
+	put_hex2((v >> 8) & 0xFF);
+	put_hex2(v & 0xFF);
+}
+
+static int hexval(char c)
+{
+	if (c >= '0' && c <= '9')
+		return c - '0';
+	if (c >= 'a' && c <= 'f')
+		return c - 'a' + 10;
+	if (c >= 'A' && c <= 'F')
+		return c - 'A' + 10;
+	return -1;
+}
+
+static int parse_hex(const char *s, unsigned int *out, const char **endp)
+{
+	unsigned int v = 0;
+	int digits = 0, d;
+
+	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
+		s += 2;
+	while ((d = hexval(*s)) >= 0) {
+		v = (v << 4) | (unsigned int)d;
+		digits++;
+		s++;
+	}
+	if (!digits)
+		return -1;
+	*out = v;
+	*endp = s;
+	return 0;
+}
+
+void program_peek_main(const char *arg)
+{
+	const char *p = arg;
+	const volatile unsigned char *mem;
+	unsigned int addr, len = 64, i, j;
+
+	if (parse_hex(p, &addr, &p) != 0) {
+		io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
+		return;
+	}
+	while (*p == ' ')
+		p++;
+	if (*p) {
+		if (parse_hex(p, &len, &p) != 0 || len == 0 || len > 256) {
+			io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
+			return;
+		}
+	}
+	while (*p == ' ')
+		p++;
+	if (*p) {
+		io_putstr("peek: usage peek <hexaddr> [len<=256]\n");
+		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]);
+			io_putchar(' ');
+		}
+		io_putchar('\n');
+	}
+}

+ 12 - 1
process.c

@@ -118,7 +118,7 @@ void process_init(void)
 	procs[0].screen     = (void *)0;
 	procs[0].stack_base = 0;  /* bootstrap stack, never freed */
 	procs[0].cr3        = paging_current_cr3();
-	procs[0].kstack_top = 0x30800;  /* bootstrap stack top */
+	procs[0].kstack_top = 0x40800;  /* bootstrap stack top */
 	name_copy(procs[0].name, "wm");
 	current_pid = 0;
 }
@@ -356,6 +356,17 @@ void process_set_screen(struct text_screen *screen)
 	procs[current_pid].screen = screen;
 }
 
+void process_map_mmio_all(unsigned int phys_base, unsigned int size)
+{
+	unsigned int i, kernel_cr3 = paging_current_cr3();
+
+	paging_map_mmio(phys_base, size);
+	for (i = 0; i < proc_cap; i++) {
+		if (procs[i].cr3 && procs[i].cr3 != kernel_cr3)
+			paging_map_mmio_cr3(procs[i].cr3, phys_base, size);
+	}
+}
+
 void process_exit(void)
 {
 	procs[current_pid].state = PROC_DEAD;

+ 8 - 1
process.h

@@ -7,7 +7,7 @@
 /* Process stacks are allocated from the frame allocator (mm.c), i.e. above the
  * VGA shadow buffer.  If that is unavailable (no E820 map) we fall back to a
  * small fixed pool below 1 MB so the system still boots with fewer processes. */
-#define PROC_FALLBACK_STACK_BASE 0x42000
+#define PROC_FALLBACK_STACK_BASE 0x50000
 #define PROC_FALLBACK_STACK_MAX  8
 
 #define PROC_UNUSED  0
@@ -52,6 +52,13 @@ int  process_create_ptr(const char *name, void (*entry)(void *), void *arg);
  * process.  Returns the pid, or -1 on error. */
 int  process_create_user(const char *name, const void *image, unsigned int size);
 void process_set_screen(struct text_screen *screen);
+
+/* 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);
 void process_exit(void);
 void schedule(void);
 void process_ps(void);

+ 2 - 2
radeon.c

@@ -17,8 +17,8 @@
  *  - Cursor mode is RADEON_CRTC_CUR_MODE_24BPP (2) in bits 22:20.
  *
  * NOTE: the bitmap is written straight into VRAM.  Do not use a local
- * [16 KB] array -- the kernel stack is only 2 KB (0x30800..0x30000) and a
- * 16 KB stack allocation overflows it, corrupting the ISA DMA buffer.
+ * [16 KB] array -- the kernel stack is only 2 KB (0x40800..0x40000) and a
+ * 16 KB stack allocation overflows it.
  */
 
 #include "radeon.h"

+ 3 - 3
run.c

@@ -1,4 +1,4 @@
-/* run command: load raw binary from a FAT filesystem to 0x31000 and execute.
+/* run command: load raw binary from a FAT filesystem to 0x41000 and execute.
  * Usage: run /<device>/<file>. */
 
 #include "fat.h"
@@ -10,7 +10,7 @@
 #include "vga.h"
 #include "vfs.h"
 
-#define PROGRAM_LOAD_ADDR 0x31000
+#define PROGRAM_LOAD_ADDR 0x41000
 #define PROGRAM_LOAD_MAX  0x10000
 
 void program_run_main(const char *filename)
@@ -70,7 +70,7 @@ void program_run_main(const char *filename)
 	((void (*)(void))PROGRAM_LOAD_ADDR)();
 }
 
-/* run serial: execute the binary already loaded at 0x31000 via serial_recv. */
+/* run serial: execute the binary already loaded at 0x41000 via serial_recv. */
 
 void program_run_serial_main(const char *arg)
 {

+ 1 - 1
run_api.h

@@ -3,7 +3,7 @@
 #ifndef RUN_API_H
 #define RUN_API_H
 
-#define PROGRAM_API_ADDR 0x30F00
+#define PROGRAM_API_ADDR 0x40F00
 #define IO_KEY_ESC       0x1B
 #define IO_KEY_SPACE     0x20
 #define IO_KEY_LEFT      0x100

+ 2 - 2
serial_recv.c

@@ -1,10 +1,10 @@
-/* Serial receive command: receives a binary via serial into 0x31000. */
+/* Serial receive command: receives a binary via serial into 0x41000. */
 
 #include "io.h"
 #include "serial.h"
 #include "bin_recv.h"
 
-#define PROGRAM_LOAD_ADDR 0x31000
+#define PROGRAM_LOAD_ADDR 0x41000
 #define PROGRAM_LOAD_MAX  0x10000
 
 unsigned int serial_recv_size;

+ 84 - 6
terminal.c

@@ -32,7 +32,8 @@ extern void program_lsusb_main(void);
 extern void program_storage_main(void);
 extern void program_mem_main(void);
 extern void program_format_main(const char *arg);
-extern void program_lspci_main(void);
+extern void program_lspci_main(const char *filter);
+extern void program_peek_main(const char *arg);
 extern void program_netinfo_main(void);
 extern void program_netrecv_main(void);
 extern void program_netsend_main(void);
@@ -247,9 +248,10 @@ static int is_cmd_lsusb(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
-static int is_cmd_lspci(const char *line, unsigned int line_len)
+static int is_cmd_lspci(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
 {
-	unsigned int i = 0;
+	unsigned int i = 0, j = 0;
+
 	while (i < line_len && line[i] == ' ')
 		i++;
 	if (i + 5 > line_len)
@@ -258,6 +260,47 @@ static int is_cmd_lspci(const char *line, unsigned int line_len)
 	    line[i+3] != 'c' || line[i+4] != 'i')
 		return 0;
 	i += 5;
+	if (i < line_len && line[i] != ' ')
+		return 0;  /* "lspci" must be a whole word */
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len) {
+		if (buf_size > 0)
+			arg_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		arg_buf[j++] = line[i++];
+	arg_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
+static int is_cmd_peek(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 'p' || line[i+1] != 'e' || line[i+2] != 'e' ||
+	    line[i+3] != 'k')
+		return 0;
+	i += 4;
+	if (i < line_len && line[i] != ' ')
+		return 0;  /* "peek" must be a whole word */
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len) {
+		if (buf_size > 0)
+			arg_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		arg_buf[j++] = line[i++];
+	arg_buf[j] = '\0';
 	while (i < line_len && line[i] == ' ')
 		i++;
 	return (i == line_len);
@@ -280,6 +323,36 @@ static int is_cmd_netinfo(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_netconsole(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 10 > line_len)
+		return 0;
+	if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
+	    line[i+3] != 'c' || line[i+4] != 'o' || line[i+5] != 'n' ||
+	    line[i+6] != 's' || line[i+7] != 'o' || line[i+8] != 'l' ||
+	    line[i+9] != 'e')
+		return 0;
+	i += 10;
+	if (i < line_len && line[i] != ' ')
+		return 0;  /* "netconsole" must be a whole word */
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len) {
+		if (buf_size > 0)
+			arg_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		arg_buf[j++] = line[i++];
+	arg_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_netrecv(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -964,6 +1037,8 @@ void term_run(struct term *t)
 					char ls_path[LS_PATH_MAX];
 					char format_dev[FORMAT_DEV_MAX];
 					char netip_arg[16];
+					char lspci_arg[16];
+					char peek_arg[32];
 					char resolved[VFS_PATH_MAX];
 					char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
 					unsigned int kill_pid;
@@ -994,8 +1069,10 @@ void term_run(struct term *t)
 						}
 					} else if (is_cmd_lsusb(t->line, t->line_len)) {
 						term_spawn(t, "lsusb", program_lsusb_main);
-					} else if (is_cmd_lspci(t->line, t->line_len)) {
-						term_spawn(t, "lspci", program_lspci_main);
+					} else if (is_cmd_lspci(t->line, t->line_len, lspci_arg, sizeof(lspci_arg))) {
+						term_spawn_arg(t, "lspci", program_lspci_main, lspci_arg);
+					} else if (is_cmd_peek(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
+						term_spawn_arg(t, "peek", program_peek_main, peek_arg);
 					} else if (is_cmd_netinfo(t->line, t->line_len)) {
 						term_spawn(t, "netinfo", program_netinfo_main);
 					} else if (is_cmd_netrecv(t->line, t->line_len)) {
@@ -1102,7 +1179,8 @@ void term_run(struct term *t)
 						io_putstr("storage\n");
 						io_putstr("mem\n");
 						io_putstr("format <device>\n");
-						io_putstr("lspci\n");
+						io_putstr("lspci [id]\n");
+						io_putstr("peek <addr> [len]\n");
 						io_putstr("netinfo\n");
 						io_putstr("netrecv\n");
 						io_putstr("netsend\n");

+ 1 - 1
tss.c

@@ -57,7 +57,7 @@ void gdt_tss_init(void)
 	__asm__ volatile ("lgdt %0" : : "m"(gdtp));
 
 	tss.ss0  = 0x10;    /* kernel data segment */
-	tss.esp0 = 0x30800; /* bootstrap stack; updated by switch.S per process */
+	tss.esp0 = 0x40800; /* bootstrap stack; updated by switch.S per process */
 	tss.iomap_base = 0xFFFF;  /* no I/O permission bitmap (deny ring-3 I/O) */
 	__asm__ volatile ("ltr %0" : : "r"((unsigned short)0x28));
 }

+ 1 - 1
uhci.c

@@ -63,7 +63,7 @@ struct uhci_qh {
 #define UHCI_NUM_TD 16
 
 /* Frame list lives in low memory (below the kernel at 0x7E00) to avoid
-   bloating the kernel's BSS past the 0x30000 linker budget. It must be
+   bloating the kernel's BSS past the 0x40000 linker budget. It must be
    4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory map). */
 #define UHCI_FRAME_LIST_ADDR 0x3000u
 

+ 2 - 2
user.ld

@@ -1,9 +1,9 @@
-/* User program: load at 0x31000, same as run.c PROGRAM_LOAD_ADDR. */
+/* User program: load at 0x41000, same as run.c PROGRAM_LOAD_ADDR. */
 ENTRY(_start)
 
 SECTIONS
 {
-	. = 0x31000;
+	. = 0x41000;
 	.text : { *(.text .text.*) }
 	.rodata : { *(.rodata .rodata.*) }
 	.data : { *(.data .data.*) }

+ 1 - 1
vesa_stage.S

@@ -223,7 +223,7 @@ stage2_pm:
 	movw  %ax, %fs
 	movw  %ax, %gs
 	movw  %ax, %ss
-	movl  $0x30800, %esp
+	movl  $0x40800, %esp
 	jmp   kernel_main
 
 .align 4