Эх сурвалжийг харах

Decouple the low-memory layout from the kernel size

The kernel kept running into a wall at 0x49000: the protected-mode stack
at 0x49800 grew down into it, the fallback stack pool and the kexec
staging buffer filled the rest of the low 1 MB, and growing the image
meant moving the whole block (and keeping the map in sync across .S, .c,
linker scripts and the docs).

- The bootstrap/protected-mode stack is an 8 KB .stack NOLOAD section
  inside the image now (__boot_stack_top), used by entry.S, vesa_stage.S,
  tss.c and process.c.  The kexec trampoline runs on a private stack
  below the E820 map (top 0x1000), so the image copy can overwrite the
  stack it was called on.
- kexec staging is allocated on demand (contiguous frame-allocator pages
  sized to the incoming image) instead of sitting at 0x58000.  Both the
  serial and the netkexec receiver use it and free it on error.
- The fallback process-stack pool is a static BSS array, no fixed
  address.
- The legacy ring-0 `run` area moved to 0x80000 (API struct 0x7FF00,
  inside the reserved low 1 MB).  bootstrap.bin keeps its own linker
  script at the old 0x4A000, because an old build's `serial recv` writes
  it there.
- kernel.ld has a single wall now: `_end <= 0x7F000` (was 0x49000), so
  adding code no longer moves any address.

Verified on QEMU: boot, `run prog` (draws and exits on ESC), a serial
kexec reload and `make net-dev` over UDP both boot the new image, and
the USB cursor/keyboard tests pass.

AGENTS.md: the memory-map table and the paragraphs around it.
Johannes Zellner 1 сар өмнө
parent
commit
d446273fce
21 өөрчлөгдсөн 171 нэмэгдсэн , 66 устгасан
  1. 14 11
      AGENTS.md
  2. 2 2
      Makefile
  3. 17 0
      bootstrap.ld
  4. 2 1
      colors.c
  5. 5 6
      entry.S
  6. 12 1
      kernel.ld
  7. 49 13
      kexec.c
  8. 16 5
      kexec.h
  9. 8 1
      kexec_tramp.S
  10. 1 1
      netcmd.c
  11. 9 1
      netkexec.c
  12. 11 5
      process.c
  13. 2 3
      process.h
  14. 2 2
      radeon.c
  15. 4 4
      run.c
  16. 3 1
      run_api.h
  17. 3 3
      serial_recv.c
  18. 5 1
      tss.c
  19. 3 2
      uhci.c
  20. 2 2
      user.ld
  21. 1 1
      vesa_stage.S

+ 14 - 11
AGENTS.md

@@ -46,17 +46,20 @@ make clean
 | `0x7E00`  | Kernel load / VESA-stage entry (`kernel.ld`) |
 | `0x800`   | kexec trampoline (relocated at runtime; below the kernel) |
 | `0x900`   | kexec flat GDT + GDTR (built at runtime; below the kernel) |
-| `0x1000`  | E820 memory map (`struct e820_map`, written by `vesa_stage.S`, parsed by `mm.c`) |
+| `0x1000`  | E820 memory map (`struct e820_map`, written by `vesa_stage.S`, parsed by `mm.c`); also the kexec trampoline's private stack top (grows down, below the map) |
 | `0x3000`  | UHCI frame list (1024 × 4 B, 4 KB-aligned; `uhci.c`) |
 | `0x4000`  | ISA DMA buffer, one sector (`disk.c`) |
-| `0x49000` | Kernel upper bound (`kernel.ld` ASSERT) |
-| `0x49800` | Protected-mode stack top (`vesa_stage.S`, `kexec_tramp.S`) |
-| `0x49F00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
-| `0x4A000` | User program load address (`run.c`, `user.ld`, max 0x6000 to the pool) |
-| `0x50000` | Fallback process stack pool (8 × 4 KB; primary stacks come from the frame allocator, ≥ `0x900000`, see `process.c`/`mm.c`) |
-| `0x58000` | kexec staging buffer (received kernel image, up to 0x3C200 B; ends below the VGA window) |
+| `_end`    | Kernel end (linker symbol; `kernel.ld` ASSERTs it stays below `0x7F000`) |
+| (in-image) | Bootstrap/protected-mode stack: 8 KB `.stack` NOLOAD section, `__boot_stack_top`/`__boot_stack_bottom` (travels with the kernel) |
+| `0x7FF00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
+| `0x80000` | Legacy ring-0 program load area (`run.c`, `serial_recv.c`, `user.ld`; max 0x6000, inside the reserved low 1 MB) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
 
+Everything else is dynamic: process stacks and the kexec staging buffer come
+from the frame allocator (≥ `0x900000`), and the fallback stack pool is a
+static BSS array.  The kernel region itself only has one wall (`_end`
+below the run area), so adding code no longer moves any address.
+
 ## Key files
 
 - **Multitasking**: `process.c/h`, `switch.S` — ring-0 preemptive round-robin,
@@ -70,7 +73,7 @@ make clean
   1 MB and an 8 MB VGA-shadow region. `kmalloc.c/h` — a power-of-two heap
   allocator on top of the frame allocator (used for the process table). Process
   stacks (≥ `0x900000`) come from the frame allocator, with an 8-stack fallback
-  pool at `0x50000`.
+  pool (a static BSS array; no fixed address).
 - **Paging**: `paging.c/h` — identity-maps RAM (4 MB pages via CR4.PSE) + the
   LFB (uncacheable) and enables CR0.PG/WP. A single shared page directory for
   now (no per-process address spaces yet). The kexec trampoline
@@ -162,7 +165,7 @@ make clean
   scan generation counter.
 - **Power/idle**: `idle.c` — `mwait` C-states, `hlt` fallback.
 - **User programs**: `run.c`, `run_api.h`, `prog.c`, `space.c` — raw binaries at
-  `0x4A000`, API function-pointer struct at `0x49F00`.  `prog`/`space` are
+  `0x80000`, API function-pointer struct at `0x7FF00`.  `prog`/`space` are
   **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`, like
   `hello.elf`), so `make dev`/`make net-dev` update them too and a board with
   no readable filesystem can still run them: `run <name>` and the `prog`/
@@ -496,8 +499,8 @@ Caveats:
 
 - Pixels are written directly to the LFB; WM full redraws overwrite them but
   the 1 Hz refresh brings them back.
-- Info rows cost ~0.5 KB BSS; the kernel must stay under the `0x49000`
-  boundary (`kernel.ld` ASSERT).
+- Info rows cost ~0.5 KB BSS; keep the kernel under the `0x7F000` boundary
+  (`kernel.ld` ASSERT).
 
 ## Conventions & gotchas
 

+ 2 - 2
Makefile

@@ -383,8 +383,8 @@ space_embed.o: space.bin
 bootstrap.o: bootstrap.c
 	$(CC) $(CFLAGS) -o $@ $<
 
-bootstrap.elf: bootstrap.o
-	$(LD) -T user.ld -nostdlib -m elf_i386 -o $@ $<
+bootstrap.elf: bootstrap.o bootstrap.ld
+	$(LD) -T bootstrap.ld -nostdlib -m elf_i386 -o $@ $<
 
 bootstrap.bin: bootstrap.elf
 	$(OBJCOPY) -O binary $< $@

+ 17 - 0
bootstrap.ld

@@ -0,0 +1,17 @@
+/* Bootstrap installer: pinned at the OLD build's PROGRAM_LOAD_ADDR (0x4A000).
+ *
+ * bootstrap.bin is received by a still-running *old* kernel whose `serial recv`
+ * writes to its own fixed program address (0x4A000, pre-Stage-A builds), so it
+ * must be linked there to run.  New builds never need it: the serial/net kexec
+ * auto-detect reloads them.  See bootstrap.c and the AGENTS.md bootstrap flow. */
+ENTRY(_start)
+
+SECTIONS
+{
+	. = 0x4A000;
+	.text : { *(.text .text.*) }
+	.rodata : { *(.rodata .rodata.*) }
+	.data : { *(.data .data.*) }
+	.bss : { *(.bss .bss.*) *(COMMON) }
+	_end = .;
+}

+ 2 - 1
colors.c

@@ -16,7 +16,8 @@
 /* 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 0x49000 memory-bound. */
+ * smaller table keeps the kernel image under its link-time budget (see the
+ * ASSERT in kernel.ld). */
 #define LUT_STEP      4
 #define LUT_SIZE      (RAINBOW_FULL / LUT_STEP)
 

+ 5 - 6
entry.S

@@ -3,15 +3,14 @@
  * transfer entry, see the dev/net-dev targets).
  *
  * It exists so a kernel reload cannot inherit a stale stack: the jumping
- * kernel's trampoline (kexec_tramp.S) sets ESP from a constant that was baked
- * into the *old* build's memory map.  If the map moved in between (the kernel
- * region grew), that stack can sit inside the new image and the BSS clear
- * would wipe the stack it is running on.  Re-pointing ESP here, before any
- * stack use, makes every reload independent of the old layout. */
+ * kernel's trampoline (kexec_tramp.S) copies the new image over this one
+ * while running on its own private stack, then jumps here.  Re-pointing ESP
+ * before any stack use makes the reload independent of where either build
+ * put its stacks. */
 .code32
 .text
 
 .globl kernel_entry
 kernel_entry:
-	movl  $0x49800, %esp
+	movl  $__boot_stack_top, %esp
 	jmp   kernel_main

+ 12 - 1
kernel.ld

@@ -15,6 +15,17 @@ SECTIONS
 		*(.bss .bss.*) *(COMMON)
 		__bss_end = .;
 	}
+	/* Bootstrap/protected-mode stack: inside the image's RAM footprint
+	 * (NOLOAD, so it costs no file bytes), which means its address travels
+	 * with the kernel instead of being a fixed one that kernel growth has
+	 * to be scheduled around.  It must sit *after* __bss_end: the BSS clear
+	 * would otherwise wipe the stack kernel_main is running on. */
+	.stack (NOLOAD) : {
+		. = ALIGN(16);
+		__boot_stack_bottom = .;
+		. += 0x2000;             /* 8 KB */
+		__boot_stack_top = .;
+	}
 	_end = .;
 }
-ASSERT(_end <= 0x49000, "Kernel too large would overwrite stack/DMA at 0x49000");
+ASSERT(_end <= 0x7F000, "Kernel too large: would reach the run area at 0x80000");

+ 49 - 13
kexec.c

@@ -5,9 +5,9 @@
  * LFB are still valid) and jump straight to the new kernel's kernel_entry,
  * which re-inits IDT/PIC/PIT/mouse/process/VGA-shadow/terminal from scratch.
  *
- * Because this code lives at 0x7E00..0x49000 (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).
+ * Because this code lives inside the kernel 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).
  *
  * The terminal polls kexec_pending() on its idle loop, so `make dev` on the
  * host alone triggers the reload (no typing required). The manual `kexec`
@@ -23,15 +23,41 @@
 
 #include "kexec.h"
 #include "io.h"
+#include "mm.h"
 #include "serial.h"
 
 #define KEXEC_MAGIC 0x4345584B /* "KXEC" little-endian */
 
-/* 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
+/* Staging buffer for the received image: allocated from the frame allocator
+ * when the header arrives, so no fixed low-memory region is needed.  It sits
+ * above the VGA shadow reserve (>= 0x900000), far from the 0x7E00 load
+ * address, so the trampoline's forward copy can never overlap itself. */
+unsigned int kexec_stage;
+static unsigned int kexec_stage_pages;
+
+int kexec_stage_alloc(unsigned int size)
+{
+	unsigned int pages;
+
+	if (size == 0 || size > KEXEC_MAX)
+		return 0;
+	pages = (size + 0xFFF) >> 12;
+	kexec_stage = mm_alloc_pages(pages);
+	if (!kexec_stage)
+		return 0;
+	kexec_stage_pages = pages;
+	return 1;
+}
+
+void kexec_stage_free(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < kexec_stage_pages; i++)
+		mm_free_page(kexec_stage + i * 0x1000);
+	kexec_stage_pages = 0;
+	kexec_stage = 0;
+}
 
 #define KEXEC_TRAMP 0x800      /* relocated trampoline (below the kernel) */
 
@@ -86,9 +112,9 @@ static int kexec_fail(const char *msg)
 
 static int kexec_recv(unsigned int *entry, unsigned int *size)
 {
-	volatile unsigned char *dst = (volatile unsigned char *)KEXEC_STAGE;
 	unsigned int i, magic = 0, n;
 	unsigned char c, sum = 0, chk;
+	volatile unsigned char *dst;
 
 	for (i = 0; i < 4; i++) {
 		if (!serial_ring_wait(5000, &c))
@@ -113,12 +139,18 @@ static int kexec_recv(unsigned int *entry, unsigned int *size)
 	}
 	if (*size == 0 || *size > KEXEC_MAX)
 		return kexec_fail("kexec: bad size\n");
+	if (!kexec_stage_alloc(*size))
+		return kexec_fail("kexec: out of memory (staging)\n");
+	dst = (volatile unsigned char *)kexec_stage;
+
 	kexec_total = *size;
 	kexec_progress = 0;
 
 	for (n = 0; n < *size; n++) {
-		if (!serial_ring_wait(5000, &c))
+		if (!serial_ring_wait(5000, &c)) {
+			kexec_stage_free();
 			return kexec_fail("kexec: timeout (data)\n");
+		}
 		dst[n] = c;
 		sum += c;
 		kexec_progress = n + 1;
@@ -127,10 +159,14 @@ static int kexec_recv(unsigned int *entry, unsigned int *size)
 	}
 	io_putchar('\n');
 
-	if (!serial_ring_wait(5000, &chk))
+	if (!serial_ring_wait(5000, &chk)) {
+		kexec_stage_free();
 		return kexec_fail("kexec: timeout (checksum)\n");
-	if (chk != sum)
+	}
+	if (chk != sum) {
+		kexec_stage_free();
 		return kexec_fail("kexec: checksum mismatch\n");
+	}
 
 	return 0;
 }
@@ -179,6 +215,6 @@ void kexec_jump(unsigned int entry, unsigned int size)
 	__asm__ volatile (
 		"jmp *%4"
 		:
-		: "a"(KEXEC_STAGE), "b"(KEXEC_LOAD), "c"(size), "d"(entry), "r"(KEXEC_TRAMP)
+		: "a"(kexec_stage), "b"(KEXEC_LOAD), "c"(size), "d"(entry), "r"(KEXEC_TRAMP)
 		: "memory");
 }

+ 16 - 5
kexec.h

@@ -4,13 +4,24 @@
 
 void program_kexec_main(void);
 
-/* Staging buffer for a received kernel image, and the maximum image size
- * (the kernel region 0x7E00..0x49000).  Below the VGA window; see AGENTS.md. */
-#define KEXEC_STAGE 0x58000
+/* Kernel load address (the boot sector's) and a sanity cap for an incoming
+ * image.  The staging buffer itself is allocated on demand: it used to be a
+ * fixed region below the VGA window, which forced the low-memory map to move
+ * whenever the kernel grew. */
 #define KEXEC_LOAD  0x7E00
-#define KEXEC_MAX   0x3C200
+#define KEXEC_MAX   0x400000
 
-/* Copy the staged image (KEXEC_STAGE, `size` bytes) over the kernel region,
+/* Allocate the staging buffer for an image of `size` bytes (contiguous,
+ * page-aligned, identity-mapped).  Sets kexec_stage; returns 1 on success. */
+int kexec_stage_alloc(unsigned int size);
+
+/* Free a staging buffer allocated by kexec_stage_alloc(). */
+void kexec_stage_free(void);
+
+/* Physical base of the staging buffer holding the received image. */
+extern unsigned int kexec_stage;
+
+/* 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);
 

+ 8 - 1
kexec_tramp.S

@@ -18,6 +18,14 @@
 .globl kexec_trampoline
 kexec_trampoline:
 	cli
+
+	/* Private stack: the copy below overwrites the kernel image, including
+	 * whatever stack we were called on.  Use the free gap below the E820
+	 * map at 0x1000 (trampoline at 0x800, flat GDT at 0x900 -- the stack
+	 * only ever dips a few words).  entry.S re-points ESP once the new
+	 * image is in place. */
+	movl  $0x1000, %esp
+
 	pushl %eax
 	pushl %ebx
 	pushl %ecx
@@ -66,7 +74,6 @@ kexec_trampoline:
 	 * this the jump below can execute stale instructions. */
 	wbinvd
 
-	movl  $0x49800, %esp
 	jmp   *%edx
 
 .globl kexec_trampoline_end

+ 1 - 1
netcmd.c

@@ -11,7 +11,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 0x49000 limit). Max Ethernet frame is
+   avoids bloating the kernel image). 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];

+ 9 - 1
netkexec.c

@@ -175,7 +175,7 @@ static void send_ack(unsigned int next_offset, unsigned int status)
 
 static int netkexec_recv(void)
 {
-	volatile unsigned char *stage = (volatile unsigned char *)KEXEC_STAGE;
+	volatile unsigned char *stage = (volatile unsigned char *)kexec_stage;
 	unsigned int offset = 0, retries = 0, sum = 0;
 	unsigned int deadline = get_ticks_ms() + 3000;
 
@@ -247,8 +247,16 @@ void program_netkexec_main(void)
 	io_puthex(x_size);
 	io_putstr(" bytes over UDP...\n");
 
+	if (!kexec_stage_alloc(x_size)) {
+		io_putstr("netkexec: out of memory (staging)\n");
+		netdev_release();
+		kexec_busy = 0;
+		return;
+	}
+
 	if (netkexec_recv() != 0) {
 		io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
+		kexec_stage_free();
 		netdev_release();
 		kexec_busy = 0;
 		return;

+ 11 - 5
process.c

@@ -17,6 +17,9 @@ static unsigned int proc_cap;            /* current capacity of procs[] */
 static unsigned int next_pid_counter = 1; /* monotonic, wraps */
 static unsigned int fallback_stack_next;  /* 0..PROC_FALLBACK_STACK_MAX */
 
+/* Top of the in-image bootstrap stack (see kernel.ld). */
+extern char __boot_stack_top[];
+
 #define INITIAL_PROC_CAP 16
 
 /* ---- interrupt flag save/restore (for short critical sections) ---- */
@@ -32,6 +35,11 @@ static inline void irq_restore(unsigned int flags)
 	__asm__ volatile ("pushl %0; popfl" : : "r"(flags));
 }
 
+/* Fallback pool: a static array, so its address is not a fixed memory-map
+ * entry (same reasoning as the in-image stack in kernel.ld). */
+static unsigned int fallback_stacks[PROC_FALLBACK_STACK_MAX][PROC_STACK_SIZE / 4]
+	__attribute__((aligned(16)));
+
 /* Allocate a process stack and return its BASE address (page-aligned), or 0.
  * *owned is set to 1 if it came from the frame allocator (must be freed on
  * exit), 0 if it came from the fallback pool. */
@@ -43,12 +51,10 @@ static unsigned int stack_alloc(int *owned)
 		return base;
 	}
 	if (fallback_stack_next < PROC_FALLBACK_STACK_MAX) {
-		unsigned int top = PROC_FALLBACK_STACK_BASE
-			+ fallback_stack_next * PROC_STACK_SIZE
-			+ PROC_STACK_SIZE;
+		base = (unsigned int)&fallback_stacks[fallback_stack_next][0];
 		fallback_stack_next++;
 		*owned = 0;
-		return top - PROC_STACK_SIZE;
+		return base;
 	}
 	return 0;
 }
@@ -118,7 +124,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 = 0x49800;  /* bootstrap stack top */
+	procs[0].kstack_top = (unsigned int)__boot_stack_top;
 	name_copy(procs[0].name, "wm");
 	current_pid = 0;
 }

+ 2 - 3
process.h

@@ -5,9 +5,8 @@
 #define PROC_STACK_SIZE 0x1000  /* 4 KB per process (= 1 page) */
 
 /* 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 0x50000
+ * VGA shadow buffer.  If a page is unavailable we fall back to a small static
+ * pool in BSS so the system still boots with fewer processes. */
 #define PROC_FALLBACK_STACK_MAX  8
 
 #define PROC_UNUSED  0

+ 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 (0x49800..0x49000) and a
- * 16 KB stack allocation overflows it.
+ * [16 KB] array -- the kernel stack is 8 KB (the in-image .stack section,
+ * see kernel.ld) and a 16 KB stack allocation overflows it.
  */
 
 #include "radeon.h"

+ 4 - 4
run.c

@@ -1,4 +1,4 @@
-/* run command: load raw binary from a FAT filesystem to 0x4A000 and execute.
+/* run command: load raw binary from a FAT filesystem to 0x80000 and execute.
  * Usage: run /<device>/<file>. */
 
 #include "fat.h"
@@ -10,8 +10,8 @@
 #include "vga.h"
 #include "vfs.h"
 
-#define PROGRAM_LOAD_ADDR 0x4A000
-#define PROGRAM_LOAD_MAX  0x6000   /* up to 0x50000, below the stack pool */
+#define PROGRAM_LOAD_ADDR 0x80000
+#define PROGRAM_LOAD_MAX  0x6000   /* up to 0x86000, below the VGA window */
 
 /* User programs embedded in the kernel image (like hello.elf): they travel
  * with `make dev`/`make net-dev`, so a board with no readable filesystem can
@@ -147,7 +147,7 @@ void program_run_main(const char *filename)
 	((void (*)(void))PROGRAM_LOAD_ADDR)();
 }
 
-/* run serial: execute the binary already loaded at 0x4A000 via serial_recv. */
+/* run serial: execute the binary already loaded at 0x80000 via serial_recv. */
 
 void program_run_serial_main(const char *arg)
 {

+ 3 - 1
run_api.h

@@ -3,7 +3,9 @@
 #ifndef RUN_API_H
 #define RUN_API_H
 
-#define PROGRAM_API_ADDR 0x49F00
+/* Sits just below the program load area, inside the low 1 MB the frame
+ * allocator reserves; keep in sync with run.c and user.ld. */
+#define PROGRAM_API_ADDR 0x7FF00
 #define IO_KEY_ESC       0x1B
 #define IO_KEY_SPACE     0x20
 #define IO_KEY_LEFT      0x100

+ 3 - 3
serial_recv.c

@@ -1,11 +1,11 @@
-/* Serial receive command: receives a binary via serial into 0x4A000. */
+/* Serial receive command: receives a binary via serial into 0x80000. */
 
 #include "io.h"
 #include "serial.h"
 #include "bin_recv.h"
 
-#define PROGRAM_LOAD_ADDR 0x4A000
-#define PROGRAM_LOAD_MAX  0x6000   /* up to 0x50000, below the stack pool */
+#define PROGRAM_LOAD_ADDR 0x80000
+#define PROGRAM_LOAD_MAX  0x6000   /* up to 0x86000, below the VGA window */
 
 unsigned int serial_recv_size;
 

+ 5 - 1
tss.c

@@ -2,6 +2,10 @@
 
 #include "tss.h"
 
+/* Top of the in-image bootstrap stack (see kernel.ld): used until the first
+ * context switch replaces TSS.esp0 with the process's own kernel stack. */
+extern char __boot_stack_top[];
+
 struct tss tss;
 
 struct gdt_entry {
@@ -57,7 +61,7 @@ void gdt_tss_init(void)
 	__asm__ volatile ("lgdt %0" : : "m"(gdtp));
 
 	tss.ss0  = 0x10;    /* kernel data segment */
-	tss.esp0 = 0x49800; /* bootstrap stack; updated by switch.S per process */
+	tss.esp0 = (unsigned int)__boot_stack_top;
 	tss.iomap_base = 0xFFFF;  /* no I/O permission bitmap (deny ring-3 I/O) */
 	__asm__ volatile ("ltr %0" : : "r"((unsigned short)0x28));
 }

+ 3 - 2
uhci.c

@@ -72,8 +72,9 @@ 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 0x49000 linker budget. It must be
-   4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory map). */
+   bloating the kernel's BSS past its link-time budget (see kernel.ld). It
+   must be 4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory
+   map). */
 #define UHCI_FRAME_LIST_ADDR 0x3000u
 
 static struct uhci_td tds[UHCI_NUM_TD];

+ 2 - 2
user.ld

@@ -1,9 +1,9 @@
-/* User program: load at 0x4A000, same as run.c PROGRAM_LOAD_ADDR. */
+/* User program: load at 0x80000, same as run.c PROGRAM_LOAD_ADDR. */
 ENTRY(_start)
 
 SECTIONS
 {
-	. = 0x4A000;
+	. = 0x80000;
 	.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  $0x49800, %esp
+	movl  $__boot_stack_top, %esp
 	jmp   kernel_entry
 
 .align 4