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

kexec: re-point the stack in a kernel_entry stub

The trampoline that performs a reload comes from the *running* kernel and
sets ESP from a constant baked into that old build.  After the kernel
region grew (0x49000), a board still running the old map jumped into the
new image with ESP at 0x44800 -- inside the new image -- so the new
kernel's BSS clear wiped the stack it was running on and the board took
a page fault at EIP=CR2=5A4BFFFF right after `make net-dev`.

entry.S adds kernel_entry: set ESP to the current stack top (0x49800)
before any stack use, then jump to kernel_main.  vesa_stage.S enters
here too, and the dev/net-dev targets extract this symbol for the
transfer header, so a reload is independent of the jumping kernel's
memory map and survives future map changes.

Verified on QEMU: booted the old build (bound 0x44000, trampoline
stack 0x44800), ran `make net-dev` into it (entry 0x7ffe) and the new
kernel came up -- two clean boot sequences on the serial log.

AGENTS.md: the entry stub, the boot chain and the kexec notes.
Johannes Zellner 1 месяц назад
Родитель
Сommit
d42b1b9862
6 измененных файлов с 38 добавлено и 13 удалено
  1. 8 3
      AGENTS.md
  2. 9 6
      Makefile
  3. 17 0
      entry.S
  4. 2 2
      kexec.c
  5. 1 1
      kexec_tramp.S
  6. 1 1
      vesa_stage.S

+ 8 - 3
AGENTS.md

@@ -31,7 +31,8 @@ make clean
    to `0x7E00` (CHS for floppy, INT 13h AH=42h LBA for hard disk), jumps there.
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
    picks best VBE mode (≥16 bpp, LFB, fits VRAM, ≤1280px), writes mode info to
-   `0x600`, enables A20, enters protected mode, jumps to `kernel_main`.
+   `0x600`, enables A20, enters protected mode, jumps to `kernel_entry`
+   (`entry.S`, re-points ESP to the current stack top, then `kernel_main`).
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → I/O → idle → timer
    (PIT 1 kHz + IDT) → mouse → processes → VGA shadow buffer → terminal.
 4. `mbr.S` — MBR (USB/HDD boot); loads partition boot sector at LBA 1.
@@ -131,9 +132,13 @@ make clean
   HTTP/1.0 request and a dumped status/headers/body (no TLS, no redirects,
   chunked decoded).  Static IP default `192.168.178.240`, mask/gateway learned
   from DHCP (off-subnet traffic goes via the router).
-- **Hot-reload (kexec)**: `kexec.c/h`, `kexec_tramp.S`, `bootstrap.c`, and the
+- **Hot-reload (kexec)**: `kexec.c/h`, `kexec_tramp.S`, `entry.S`, `bootstrap.c`, and the
   host-side `kexec_send.c` — reload the kernel over serial without rebooting
   (C-light: reuses the current framebuffer, re-runs `kernel_main`).
+  The transfer entry is `kernel_entry` (`entry.S`), which re-points ESP before
+  any stack use: the jumping kernel's trampoline stack address comes from the
+  *old* build's memory map, so a reload between different maps (e.g. after the
+  kernel region grew) would otherwise land the stack inside the new image.
   `netkexec.c/h` + host-side `netkexec_send.c` do the same over UDP
   (`make net-dev BOARD_IP=…`): the WM loop auto-detects the header, the board
   acks every datagram with the next expected offset, verifies the checksum and
@@ -217,7 +222,7 @@ Wire protocol (host → target, sent immediately; no handshake):
 
 **Every iteration:**
 ```sh
-make dev            # builds kernel.bin, computes kernel_main via nm, sends
+make dev            # builds kernel.bin, computes kernel_entry via nm, sends
 ```
 The reload is automatic. On boot the kernel prints `Nebs OS v<version> booted`
 to COM1, so a serial monitor can confirm the new build headlessly:

+ 9 - 6
Makefile

@@ -245,6 +245,9 @@ timer.o: timer.S
 switch.o: switch.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
+entry.o: entry.S
+	$(AS) $(ASFLAGS) -o $@ $<
+
 process.o: process.c process.h io.h kmalloc.h mm.h serial.h idle.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -272,7 +275,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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.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 r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o hello_embed.o prog_embed.o space_embed.o
+kernel.elf: vesa_stage.o entry.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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.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 r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o hello_embed.o prog_embed.o space_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -499,8 +502,8 @@ netmonitor: netmonitor.c
 bootstrap: bootstrap.bin
 
 dev: kernel.bin kexec_send
-	@entry=$$(nm kernel.elf | awk '/ T kernel_main$$/{print $$1}'); \
-	if [ -z "$$entry" ]; then echo "error: kernel_main symbol not found"; exit 1; fi; \
+	@entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
+	if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
 	echo "Sending kernel.bin (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
@@ -517,10 +520,10 @@ NETLOG_PORT ?= 19290
 NETKEXEC_PORT ?= 19288
 
 net-dev: kernel.bin netkexec_send
-	@entry=$$(nm kernel.elf | awk '/ T kernel_main$$/{print $$1}'); \
-	if [ -z "$$entry" ]; then echo "error: kernel_main symbol not found"; exit 1; fi; \
+	@entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
+	if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
 	echo "Sending kernel.bin (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.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 r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.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 prog_embed.o space_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.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 r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.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 prog_embed.o space_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 17 - 0
entry.S

@@ -0,0 +1,17 @@
+/* Canonical kernel entry: the first instruction of the kernel for every boot
+ * path (vesa_stage.S jumps here; the kexec paths use this symbol as the
+ * 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. */
+.code32
+.text
+
+.globl kernel_entry
+kernel_entry:
+	movl  $0x49800, %esp
+	jmp   kernel_main

+ 2 - 2
kexec.c

@@ -2,7 +2,7 @@
  *
  * C-light reload: we do NOT return to real mode or re-run the VESA stage.
  * Instead we reuse the current framebuffer (the boot FB info at 0x600 and the
- * LFB are still valid) and jump straight to the new kernel's kernel_main,
+ * 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),
@@ -15,7 +15,7 @@
  *
  * Wire protocol (host -> us):
  *   4 bytes: magic "KXEC" (LE)
- *   4 bytes: entry address (LE)   -- new kernel_main VMA
+ *   4 bytes: entry address (LE)   -- new kernel_entry VMA
  *   4 bytes: size (LE)            -- kernel.bin byte count
  *   N bytes: kernel.bin data
  *   1 byte:  checksum (sum of data bytes mod 256)

+ 1 - 1
kexec_tramp.S

@@ -5,7 +5,7 @@
  *   eax = source (staging buffer)
  *   ebx = dest   (0x7E00)
  *   ecx = byte count
- *   edx = entry  (new kernel_main)
+ *   edx = entry  (new kernel_entry, see entry.S)
  *
  * Also rebuilds a flat GDT at 0x900 and loads it, so the new kernel's segment
  * reloads (iret on the first context switch) don't read the old GDT that lives

+ 1 - 1
vesa_stage.S

@@ -224,7 +224,7 @@ stage2_pm:
 	movw  %ax, %gs
 	movw  %ax, %ss
 	movl  $0x49800, %esp
-	jmp   kernel_main
+	jmp   kernel_entry
 
 .align 4
 stage2_gdt: