Преглед изворни кода

Phase 4 WIP: ring-3 + syscalls + ELF loader (hello crash / kexec fail on real HW)

Johannes Zellner пре 1 месец
родитељ
комит
a7cafb54d3
19 измењених фајлова са 583 додато и 18 уклоњено
  1. 22 3
      Makefile
  2. 110 0
      elf.c
  3. 10 0
      elf.h
  4. 40 4
      hello.c
  5. 12 0
      hello.ld
  6. 27 0
      kernel.c
  7. 5 0
      kexec_tramp.S
  8. 3 3
      mm.h
  9. 52 2
      paging.c
  10. 17 0
      paging.h
  11. 100 2
      process.c
  12. 10 0
      process.h
  13. 9 2
      switch.S
  14. 38 0
      syscall.c
  15. 12 0
      syscall.h
  16. 20 0
      syscall_stub.S
  17. 10 2
      terminal.c
  18. 63 0
      tss.c
  19. 23 0
      tss.h

+ 22 - 3
Makefile

@@ -41,7 +41,7 @@ terminal.o: terminal.c terminal.h screen.h vfs.h fat.h blkdev.h io.h dbg.h idle.
 wm.o: wm.c wm.h terminal.h screen.h fbtext.h idle.h io.h mouse.h process.h rtc.h timer.h version.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-hello.o: hello.c io.h
+hello.o: hello.c
 	$(CC) $(CFLAGS) -o $@ $<
 
 reboot.o: reboot.c
@@ -209,10 +209,22 @@ kmalloc.o: kmalloc.c kmalloc.h mm.h
 paging.o: paging.c paging.h mm.h vga.h serial.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+elf.o: elf.c elf.h mm.h paging.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+tss.o: tss.c tss.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+syscall.o: syscall.c syscall.h idle.h io.h process.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+syscall_stub.o: syscall_stub.S
+	$(AS) $(ASFLAGS) -o $@ $<
+
 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 mem.o screen.o io.o rtc.o terminal.o wm.o hello.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 font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.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
+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 font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.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
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -287,6 +299,13 @@ space.elf: space.o
 space.bin: space.elf
 	$(OBJCOPY) -O binary $< $@
 
+hello.elf: hello.o
+	$(LD) -T hello.ld -nostdlib -m elf_i386 -o $@ $<
+	$(OBJCOPY) --strip-all $@ $@
+
+hello_embed.o: hello.elf
+	$(LD) -m elf_i386 -r -b binary -o hello_embed.o hello.elf
+
 bootstrap.o: bootstrap.c
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -361,4 +380,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 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 font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.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 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 font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.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

+ 110 - 0
elf.c

@@ -0,0 +1,110 @@
+/* Minimal ELF32 loader: parse PT_LOAD segments, allocate physical pages,
+ * copy file bytes, zero BSS, and map them into a page directory as user pages. */
+
+#include "elf.h"
+#include "mm.h"
+#include "paging.h"
+
+#define ELF_MAGIC 0x464C457F   /* "\x7fELF" little-endian */
+#define PT_LOAD   1
+#define EM_386    3
+
+struct elf32_hdr {
+	unsigned char ident[16];
+	unsigned short type;
+	unsigned short machine;
+	unsigned int   version;
+	unsigned int   entry;
+	unsigned int   phoff;
+	unsigned int   shoff;
+	unsigned int   flags;
+	unsigned short ehsize;
+	unsigned short phentsize;
+	unsigned short phnum;
+	unsigned short shentsize;
+	unsigned short shnum;
+	unsigned short shstrndx;
+};
+
+struct elf32_phdr {
+	unsigned int type;
+	unsigned int offset;
+	unsigned int vaddr;
+	unsigned int paddr;
+	unsigned int filesz;
+	unsigned int memsz;
+	unsigned int flags;
+	unsigned int align;
+};
+
+static void memzero(void *dst, unsigned int n)
+{
+	unsigned char *p = (unsigned char *)dst;
+	while (n--)
+		*p++ = 0;
+}
+
+static void memcopy(void *dst, const void *src, unsigned int n)
+{
+	unsigned char *d = (unsigned char *)dst;
+	const unsigned char *s = (const unsigned char *)src;
+	while (n--)
+		*d++ = *s++;
+}
+
+unsigned int elf_load(unsigned int cr3, const void *image, unsigned int size)
+{
+	const struct elf32_hdr *h = (const struct elf32_hdr *)image;
+	const struct elf32_phdr *ph;
+	unsigned int i, p;
+
+	if (size < sizeof(*h))
+		return 0;
+	if (*(const unsigned int *)h->ident != ELF_MAGIC)
+		return 0;
+	if (h->machine != EM_386)
+		return 0;
+
+	ph = (const struct elf32_phdr *)((const char *)image + h->phoff);
+
+	for (i = 0; i < h->phnum; i++) {
+		unsigned int va, memsz, filesz, off, npages;
+
+		if (ph[i].type != PT_LOAD || ph[i].memsz == 0)
+			continue;
+
+		va = ph[i].vaddr;
+		memsz = ph[i].memsz;
+		filesz = ph[i].filesz;
+		off = ph[i].offset;
+		npages = (memsz + 0xFFF) >> 12;
+
+		for (p = 0; p < npages; p++) {
+			unsigned int phys = mm_alloc_page();
+			unsigned int src = p * 0x1000;
+			unsigned int n;
+
+			if (!phys)
+				return 0;
+			memzero((void *)phys, 0x1000);
+			if (src < filesz) {
+				n = filesz - src;
+				if (n > 0x1000)
+					n = 0x1000;
+				memcopy((void *)phys,
+					(const char *)image + off + src, n);
+			}
+			if (!paging_map_page(cr3, va + src, phys,
+					     PTE_USER | PTE_WRITABLE | PTE_PRESENT))
+				return 0;
+		}
+	}
+
+	/* Flush the caches so the freshly-copied code is coherent.  The Pentium
+	 * (P5) has split I/D caches with no hardware coherency for self-modifying
+	 * code, so the code we just wrote through the data cache is not visible to
+	 * the instruction cache until we wbinvd.  QEMU doesn't model this. */
+	__asm__ volatile ("wbinvd");
+
+	return h->entry;
+}

+ 10 - 0
elf.h

@@ -0,0 +1,10 @@
+/* Minimal ELF32 loader. */
+#ifndef ELF_H
+#define ELF_H
+
+/* Load an ELF32 image (already in memory) into page directory `cr3`, mapping
+ * its PT_LOAD segments as user pages (U/S=1, R/W=1).  Segments are assumed
+ * page-aligned.  Returns the entry point virtual address, or 0 on error. */
+unsigned int elf_load(unsigned int cr3, const void *image, unsigned int size);
+
+#endif

+ 40 - 4
hello.c

@@ -1,8 +1,44 @@
-/* Hello program: prints "Hello World" and exits. Uses io.h. */
+/* hello: a real ring-3 user process.
+ *
+ * Prints "Hello user this is a real process!" once per second (10 times),
+ * then "Goodbye", and exits — all through int 0x80 syscalls. */
+#define SYS_PUTSTR       1
+#define SYS_GET_TICKS_MS 2
+#define SYS_SLEEP_MS     3
+#define SYS_EXIT         4
 
-#include "io.h"
+static inline int syscall1(int num, int a1)
+{
+	int ret;
+	__asm__ volatile ("int $0x80"
+			  : "=a"(ret)
+			  : "a"(num), "b"(a1)
+			  : "memory");
+	return ret;
+}
+
+static inline int syscall0(int num)
+{
+	int ret;
+	__asm__ volatile ("int $0x80"
+			  : "=a"(ret)
+			  : "a"(num)
+			  : "memory");
+	return ret;
+}
+
+static void putstr(const char *s) { syscall1(SYS_PUTSTR, (int)s); }
+static void sleep_ms(unsigned int ms) { syscall1(SYS_SLEEP_MS, (int)ms); }
 
-void program_hello_main(void)
+void _start(void)
 {
-	io_putstr("Hello World\n");
+	int i;
+	for (i = 0; i < 10; i++) {
+		sleep_ms(1000);
+		putstr("Hello user this is a real process!\n");
+	}
+	putstr("Goodbye\n");
+	syscall1(SYS_EXIT, 0);
+	for (;;)
+		;
 }

+ 12 - 0
hello.ld

@@ -0,0 +1,12 @@
+/* User program (ring-3): load at 0x40000000, the canonical user VA. */
+ENTRY(_start)
+
+SECTIONS
+{
+	. = 0x40000000;
+	.text : { *(.text .text.*) }
+	.rodata : { *(.rodata .rodata.*) }
+	.data : { *(.data .data.*) }
+	.bss : { *(.bss .bss.*) *(COMMON) }
+	_end = .;
+}

+ 27 - 0
kernel.c

@@ -8,6 +8,7 @@
 #include "paging.h"
 #include "process.h"
 #include "timer.h"
+#include "tss.h"
 #include "wm.h"
 #include "serial.h"
 #include "version.h"
@@ -41,6 +42,7 @@ extern void pit_irq_stub(void);
 extern void kbd_irq_stub(void);
 extern void fdc_irq_stub(void);
 extern void default_irq_stub(void);
+extern void syscall_stub(void);
 extern unsigned int exc_stub_table[];
 
 unsigned int get_ticks_ms(void)
@@ -83,6 +85,17 @@ static void set_idt_gate(int vec, unsigned int handler)
 	idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
 }
 
+/* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
+ * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
+static void set_idt_gate_user(int vec, unsigned int handler)
+{
+	idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
+	idt[vec].selector  = 0x08;
+	idt[vec].zero      = 0;
+	idt[vec].type      = 0xEF;
+	idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
+}
+
 static void timer_init(void)
 {
 	int i;
@@ -113,6 +126,9 @@ static void timer_init(void)
 	set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
 	set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
 
+	/* Syscall gate (ring-3 can int 0x80). */
+	set_idt_gate_user(0x80, (unsigned int)syscall_stub);
+
 	idtr.limit = (unsigned short)(sizeof(idt) - 1);
 	idtr.base  = (unsigned int)idt;
 	__asm__ volatile ("lidt %0" : : "m"(idtr));
@@ -140,13 +156,24 @@ void kernel_main(void)
 	mm_dump();
 	kmalloc_init();
 	paging_init();
+	serial_puts(SERIAL_COM1, "dbg: paging done\n");
 	io_init();
+	serial_puts(SERIAL_COM1, "dbg: io done\n");
 	idle_init();
+	serial_puts(SERIAL_COM1, "dbg: idle done\n");
 	timer_init();
+	serial_puts(SERIAL_COM1, "dbg: timer done\n");
 	mouse_init();
+	serial_puts(SERIAL_COM1, "dbg: mouse done\n");
 	process_init();
+	serial_puts(SERIAL_COM1, "dbg: process done\n");
+	gdt_tss_init();
+	serial_puts(SERIAL_COM1, "dbg: gdt_tss done\n");
 	vga_shadow_init();
+	serial_puts(SERIAL_COM1, "dbg: vga_shadow done\n");
 	vga_fb_flush();
+	serial_puts(SERIAL_COM1, "dbg: fb_flush done\n");
 	vfs_mount_all();
+	serial_puts(SERIAL_COM1, "dbg: vfs done\n");
 	wm_run();
 }

+ 5 - 0
kexec_tramp.S

@@ -61,6 +61,11 @@ kexec_trampoline:
 	cld
 	rep   movsb
 
+	/* Flush the caches so the freshly-copied kernel is coherent.  The Pentium
+	 * (P5) split I/D caches are not coherent for self-modifying code; without
+	 * this the jump below can execute stale instructions. */
+	wbinvd
+
 	movl  $0x30800, %esp
 	jmp   *%edx
 

+ 3 - 3
mm.h

@@ -17,9 +17,9 @@
 #define E820_MAX_ENTRIES 32
 
 /* Frame allocator covers physical RAM [0, MM_MAX_PHYS).  Bitmap: 1 bit/page.
- * 256 MB covers every first-gen Pentium chipset (430FX = 128 MB, 430VX = 256 MB)
- * and QEMU's default 128 MB; raise this only if larger machines are targeted. */
-#define MM_MAX_PHYS      (256u * 1024 * 1024)  /* 256 MB ceiling */
+ * 128 MB matches this machine and QEMU's default; raise it (and move the
+ * kernel upper bound, see kernel.ld) if larger machines are targeted. */
+#define MM_MAX_PHYS      (128u * 1024 * 1024)  /* 128 MB ceiling */
 
 /* Regions the kernel already owns (marked used at init): */
 #define MM_RESERVE_LOW     0x100000   /* 0 .. 1 MB: kernel/DMA/UHCI/UMA/boot */

+ 52 - 2
paging.c

@@ -16,6 +16,7 @@
 /* Page-directory entry flags (4 MB page). */
 #define PDE_PRESENT  0x001
 #define PDE_WRITABLE 0x002
+#define PDE_USER     0x004
 #define PDE_PWT      0x008
 #define PDE_PCD      0x010
 #define PDE_PS       0x080   /* 4 MB page */
@@ -24,7 +25,7 @@
  * (~5 MB) with margin, and is independent of the BIOS's VRAM report. */
 #define LFB_MAP_SIZE 0x1000000
 
-static unsigned int page_dir[1024] __attribute__((aligned(4096)));
+static unsigned int *page_dir;
 
 void paging_init(void)
 {
@@ -33,6 +34,13 @@ void paging_init(void)
 	/* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
 	__asm__ volatile ("" : "+r"(fb));
 
+	page_dir = (unsigned int *)mm_alloc_page();
+	if (!page_dir) {
+		serial_puts(SERIAL_COM1, "paging: out of memory\n");
+		for (;;)
+			__asm__ volatile ("cli; hlt");
+	}
+
 	/* Identity-map RAM [0, MM_MAX_PHYS) with 4 MB pages (write-back). */
 	for (i = 0; i < MM_MAX_PHYS / 0x400000; i++)
 		page_dir[i] = (i << 22) | PDE_PS | PDE_WRITABLE | PDE_PRESENT;
@@ -60,8 +68,50 @@ void paging_init(void)
 		"jmp  1f\n\t"
 		"1:\n\t"
 		:
-		: "r"((unsigned int)&page_dir[0])
+		: "r"((unsigned int)page_dir)
 		: "eax", "memory");
 
 	serial_puts(SERIAL_COM1, "paging: enabled (identity map)\n");
 }
+
+unsigned int paging_current_cr3(void)
+{
+	return (unsigned int)page_dir;
+}
+
+unsigned int paging_new_directory(void)
+{
+	unsigned int *pd = (unsigned int *)mm_alloc_page();
+	unsigned int i;
+
+	if (!pd)
+		return 0;
+	for (i = 0; i < 1024; i++)
+		pd[i] = page_dir[i];
+	return (unsigned int)pd;
+}
+
+int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
+		    unsigned int flags)
+{
+	unsigned int *pd = (unsigned int *)cr3;
+	unsigned int pde = va >> 22;
+	unsigned int pte = (va >> 12) & 0x3FF;
+	unsigned int *pt;
+
+	if (!(pd[pde] & PDE_PRESENT)) {
+		unsigned int pt_phys = mm_alloc_page();
+		unsigned int i;
+		if (!pt_phys)
+			return 0;
+		pt = (unsigned int *)pt_phys;
+		for (i = 0; i < 1024; i++)
+			pt[i] = 0;
+		pd[pde] = pt_phys | PDE_USER | PDE_WRITABLE | PDE_PRESENT;
+	} else {
+		pt = (unsigned int *)(pd[pde] & 0xFFFFF000);
+	}
+
+	pt[pte] = (phys & 0xFFFFF000) | flags;
+	return 1;
+}

+ 17 - 0
paging.h

@@ -4,4 +4,21 @@
 
 void paging_init(void);
 
+/* Physical address of the bootstrap (current) page directory. */
+unsigned int paging_current_cr3(void);
+
+/* Allocate a fresh page directory that clones the kernel identity map and
+ * return its physical address (0 on out-of-memory). */
+unsigned int paging_new_directory(void);
+
+/* Page-table entry flags (4 KB pages). */
+#define PTE_PRESENT  0x001
+#define PTE_WRITABLE 0x002
+#define PTE_USER     0x004
+
+/* 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,
+		    unsigned int flags);
+
 #endif

+ 100 - 2
process.c

@@ -1,10 +1,12 @@
 /* Process management: create, exit, round-robin schedule, ps, kill. */
 
 #include "process.h"
+#include "elf.h"
 #include "idle.h"
 #include "io.h"
 #include "kmalloc.h"
 #include "mm.h"
+#include "paging.h"
 #include "serial.h"
 
 struct process *procs;               /* dynamically-sized process table */
@@ -114,6 +116,8 @@ void process_init(void)
 	procs[0].esp        = 0;
 	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 */
 	name_copy(procs[0].name, "wm");
 	current_pid = 0;
 }
@@ -139,7 +143,7 @@ static int setup_process(const char *name, unsigned int entry,
 			 int has_arg, const char *arg)
 {
 	int slot, owned;
-	unsigned int base, top, *sp, arg_ptr = 0;
+	unsigned int base, top, *sp, arg_ptr = 0, cr3;
 	unsigned int flags;
 
 	flags = irq_save_disable();
@@ -150,8 +154,15 @@ static int setup_process(const char *name, unsigned int entry,
 		return -1;
 	}
 
+	cr3 = paging_new_directory();
+	if (!cr3) {
+		irq_restore(flags);
+		return -1;
+	}
+
 	base = stack_alloc(&owned);
 	if (!base) {
+		mm_free_page(cr3);
 		irq_restore(flags);
 		return -1;
 	}
@@ -193,6 +204,8 @@ static int setup_process(const char *name, unsigned int entry,
 	procs[slot].state      = PROC_READY;
 	procs[slot].pid        = next_pid_counter++;
 	procs[slot].stack_base = owned ? base : 0;
+	procs[slot].cr3        = cr3;
+	procs[slot].kstack_top = top;
 	name_copy(procs[slot].name, name);
 	/* Child inherits the parent's output screen (command output lands in the
 	   terminal that spawned it). */
@@ -217,7 +230,7 @@ int process_create_arg(const char *name, void (*entry)(const char *),
 int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
 {
 	int slot, owned;
-	unsigned int base, top, *sp;
+	unsigned int base, top, *sp, cr3;
 	unsigned int flags;
 
 	flags = irq_save_disable();
@@ -228,8 +241,15 @@ int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
 		return -1;
 	}
 
+	cr3 = paging_new_directory();
+	if (!cr3) {
+		irq_restore(flags);
+		return -1;
+	}
+
 	base = stack_alloc(&owned);
 	if (!base) {
+		mm_free_page(cr3);
 		irq_restore(flags);
 		return -1;
 	}
@@ -248,6 +268,81 @@ int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
 	procs[slot].state      = PROC_READY;
 	procs[slot].pid        = next_pid_counter++;
 	procs[slot].stack_base = owned ? base : 0;
+	procs[slot].cr3        = cr3;
+	procs[slot].kstack_top = top;
+	name_copy(procs[slot].name, name);
+	procs[slot].screen = procs[current_pid].screen;
+
+	irq_restore(flags);
+	return (int)procs[slot].pid;
+}
+
+/* Load an ELF image into a fresh address space and run it as a ring-3 process. */
+int process_create_user(const char *name, const void *image, unsigned int size)
+{
+	int slot, owned;
+	unsigned int base, top, *sp, cr3, entry;
+	unsigned int flags;
+	int i;
+
+	flags = irq_save_disable();
+
+	slot = alloc_slot();
+	if (slot < 0) {
+		irq_restore(flags);
+		return -1;
+	}
+
+	cr3 = paging_new_directory();
+	if (!cr3) {
+		irq_restore(flags);
+		return -1;
+	}
+
+	entry = elf_load(cr3, image, size);
+	if (!entry) {
+		mm_free_page(cr3);
+		irq_restore(flags);
+		return -1;
+	}
+
+	/* Map the user stack just below USER_STACK_TOP. */
+	for (i = 0; i < USER_STACK_PAGES; i++) {
+		unsigned int phys = mm_alloc_page();
+		unsigned int va = USER_STACK_TOP - (unsigned int)(i + 1) * 0x1000;
+		if (!phys ||
+		    !paging_map_page(cr3, va, phys,
+				     PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
+			mm_free_page(cr3);
+			irq_restore(flags);
+			return -1;
+		}
+	}
+
+	base = stack_alloc(&owned);
+	if (!base) {
+		mm_free_page(cr3);
+		irq_restore(flags);
+		return -1;
+	}
+	top = base + PROC_STACK_SIZE;
+	sp = (unsigned int *)top;
+
+	/* Initial frame so that popa + iret drops us into ring-3 at `entry`. */
+	*--sp = 0x23;                 /* SS: user data */
+	*--sp = USER_STACK_TOP;       /* ESP: user stack */
+	*--sp = 0x202;                /* EFLAGS: IF=1 */
+	*--sp = 0x1B;                 /* CS: user code */
+	*--sp = entry;                /* EIP */
+	*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0;  /* EAX ECX EDX EBX */
+	*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0;  /* ESP EBP ESI EDI */
+
+	procs[slot].esp        = (unsigned int)sp;
+	procs[slot].state      = PROC_READY;
+	procs[slot].pid        = next_pid_counter++;
+	procs[slot].stack_base = owned ? base : 0;
+	procs[slot].cr3        = cr3;
+	procs[slot].kstack_top = top;
 	name_copy(procs[slot].name, name);
 	procs[slot].screen = procs[current_pid].screen;
 
@@ -285,7 +380,10 @@ void schedule(void)
 		if (procs[i].state == PROC_DEAD && i != current_pid) {
 			if (procs[i].stack_base)
 				mm_free_page(procs[i].stack_base);
+			if (procs[i].cr3 && procs[i].cr3 != paging_current_cr3())
+				mm_free_page(procs[i].cr3);
 			procs[i].stack_base = 0;
+			procs[i].cr3 = 0;
 			procs[i].state = PROC_UNUSED;
 		}
 	}

+ 10 - 0
process.h

@@ -28,8 +28,15 @@ struct process {
 	char         name[PROC_NAME_LEN];
 	struct text_screen *screen;  /* output target for io_putchar & co. */
 	unsigned int stack_base;     /* page to free on exit, or 0 (bootstrap/fallback) */
+	unsigned int cr3;            /* physical address of this process's page dir */
+	unsigned int kstack_top;     /* ring-0 stack top (for TSS esp0) */
 };
 
+/* User-mode virtual address layout (ring-3 processes). */
+#define USER_BASE        0x40000000   /* user program link/load address */
+#define USER_STACK_TOP   0x40200000   /* user stack top (grows down) */
+#define USER_STACK_PAGES 2            /* user stack size in 4 KB pages */
+
 /* Dynamically-sized process table (grown on demand; see process.c). */
 extern struct process *procs;
 extern volatile unsigned int current_pid;
@@ -39,6 +46,9 @@ int  process_create(const char *name, void (*entry)(void));
 int  process_create_arg(const char *name, void (*entry)(const char *),
 			const char *arg);
 int  process_create_ptr(const char *name, void (*entry)(void *), void *arg);
+/* Load an ELF image into a fresh address space and run it as a ring-3
+ * 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);
 void process_exit(void);
 void schedule(void);

+ 9 - 2
switch.S

@@ -21,10 +21,13 @@
 .extern schedule
 .extern vga_shadow_flush_pending
 .extern serial_poll
+.extern tss
 
 /* struct process offsets (must match process.h / process.c) */
 .equ PCB_ESP,   0
-.equ PCB_SIZE,  32      /* sizeof(struct process): 4+4+4+12+4+4 = 32 */
+.equ PCB_CR3,   32
+.equ PCB_KSTACK_TOP, 36
+.equ PCB_SIZE,  40      /* sizeof(struct process): 4+4+4+12+4+4+4+4 = 40 */
 
 .equ TIMESLICE, 20
 .equ FLUSH_INTERVAL, 33 /* 1000Hz PIT / 30Hz flush ≈ 33 ticks */
@@ -64,11 +67,15 @@ pit_irq_stub:
 	/* Let the C scheduler pick the next process. */
 	call  schedule
 
-	/* Load new ESP from procs[current_pid].esp */
+	/* Load new ESP, CR3, and TSS esp0 from procs[current_pid]. */
 	movl  current_pid, %eax
 	imull $PCB_SIZE, %eax
 	addl  procs, %eax
 	movl  PCB_ESP(%eax), %esp
+	movl  PCB_CR3(%eax), %ebx
+	movl  %ebx, %cr3
+	movl  PCB_KSTACK_TOP(%eax), %ebx
+	movl  %ebx, tss+4           /* TSS.esp0 = this process's ring-0 stack top */
 
 .no_switch:
 	popa

+ 38 - 0
syscall.c

@@ -0,0 +1,38 @@
+/* Syscall dispatcher: services int 0x80 requests from ring-3. */
+
+#include "syscall.h"
+#include "idle.h"
+#include "io.h"
+#include "process.h"
+#include "serial.h"
+#include "timer.h"
+
+int syscall_dispatch(int num, int a1, int a2, int a3)
+{
+	(void)a2;
+	(void)a3;
+
+	serial_puts(SERIAL_COM1, "syscall ");
+	serial_putdec(SERIAL_COM1, (unsigned int)num);
+	serial_puts(SERIAL_COM1, "\n");
+
+	switch (num) {
+	case SYS_PUTSTR:
+		io_putstr((const char *)a1);
+		return 0;
+	case SYS_GET_TICKS_MS:
+		return (int)get_ticks_ms();
+	case SYS_SLEEP_MS: {
+		unsigned int start = get_ticks_ms();
+		unsigned int ms = (unsigned int)a1;
+		while (get_ticks_ms() - start < ms)
+			cpu_idle();
+		return 0;
+	}
+	case SYS_EXIT:
+		process_exit();   /* never returns */
+		return 0;
+	default:
+		return -1;
+	}
+}

+ 12 - 0
syscall.h

@@ -0,0 +1,12 @@
+/* Syscall interface (int 0x80). */
+#ifndef SYSCALL_H
+#define SYSCALL_H
+
+#define SYS_PUTSTR       1
+#define SYS_GET_TICKS_MS 2
+#define SYS_SLEEP_MS     3
+#define SYS_EXIT         4
+
+int syscall_dispatch(int num, int a1, int a2, int a3);
+
+#endif

+ 20 - 0
syscall_stub.S

@@ -0,0 +1,20 @@
+/* int 0x80 syscall stub (ring-3 -> ring-0).
+ *
+ * On entry the CPU has pushed SS, ESP, EFLAGS, CS, EIP (from ring-3).
+ * Registers: eax = syscall number, ebx/ecx/edx = args 1..3.
+ * The dispatcher's return value is placed in eax before returning to user. */
+.code32
+.text
+
+.globl syscall_stub
+syscall_stub:
+	pusha
+	pushl %edx
+	pushl %ecx
+	pushl %ebx
+	pushl %eax
+	call  syscall_dispatch
+	addl  $16, %esp
+	movl  %eax, 28(%esp)   /* overwrite the saved eax with the return value */
+	popa
+	iret

+ 10 - 2
terminal.c

@@ -9,7 +9,8 @@
 #include "terminal.h"
 #include "timer.h"
 
-extern void program_hello_main(void);
+extern unsigned char _binary_hello_elf_start[];
+extern unsigned char _binary_hello_elf_end[];
 extern void program_reboot_main(void);
 extern void program_poweroff_main(void);
 extern void program_ls_main(const char *path);
@@ -968,7 +969,14 @@ void term_run(struct term *t)
 					history_push(t, t->line, t->line_len);
 					t->history_browse = 0;
 					if (is_cmd_hello(t->line, t->line_len)) {
-						term_spawn(t, "hello", program_hello_main);
+						int pid = process_create_user("hello",
+							_binary_hello_elf_start,
+							(unsigned int)(_binary_hello_elf_end -
+								_binary_hello_elf_start));
+						if (pid < 0)
+							io_putstr("error: too many processes\n");
+						else
+							t->fg_pid = pid;
 					} else if (is_cmd_reboot(t->line, t->line_len)) {
 						program_reboot_main();
 					} else if (is_cmd_poweroff(t->line, t->line_len)) {

+ 63 - 0
tss.c

@@ -0,0 +1,63 @@
+/* TSS + flat GDT (kernel code/data, user code/data, TSS). */
+
+#include "tss.h"
+
+struct tss tss;
+
+struct gdt_entry {
+	unsigned short limit_low;
+	unsigned short base_low;
+	unsigned char  base_mid;
+	unsigned char  access;
+	unsigned char  limit_high_flags;
+	unsigned char  base_high;
+} __attribute__((packed));
+
+struct gdt_ptr {
+	unsigned short limit;
+	unsigned int   base;
+} __attribute__((packed));
+
+static struct gdt_entry gdt[6];
+static struct gdt_ptr   gdtp;
+
+static void set_entry(int i, unsigned int base, unsigned int limit,
+		      unsigned char access, unsigned char flags)
+{
+	gdt[i].limit_low  = (unsigned short)(limit & 0xFFFF);
+	gdt[i].base_low   = (unsigned short)(base & 0xFFFF);
+	gdt[i].base_mid   = (unsigned char)((base >> 16) & 0xFF);
+	gdt[i].access     = access;
+	gdt[i].limit_high_flags =
+		(unsigned char)(((limit >> 16) & 0x0F) | (flags & 0xF0));
+	gdt[i].base_high  = (unsigned char)((base >> 24) & 0xFF);
+}
+
+void gdt_tss_init(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(gdt); i++)
+		((unsigned char *)gdt)[i] = 0;
+	for (i = 0; i < sizeof(tss); i++)
+		((unsigned char *)&tss)[i] = 0;
+
+	/* 0x00 null */
+	/* 0x08 kernel code, 0x10 kernel data */
+	set_entry(1, 0, 0xFFFFF, 0x9A, 0xCF);
+	set_entry(2, 0, 0xFFFFF, 0x92, 0xCF);
+	/* 0x18 user code, 0x20 user data (DPL=3) */
+	set_entry(3, 0, 0xFFFFF, 0xFA, 0xCF);
+	set_entry(4, 0, 0xFFFFF, 0xF2, 0xCF);
+	/* 0x28 TSS */
+	set_entry(5, (unsigned int)&tss, sizeof(tss) - 1, 0x89, 0x00);
+
+	gdtp.limit = (unsigned short)(sizeof(gdt) - 1);
+	gdtp.base  = (unsigned int)gdt;
+	__asm__ volatile ("lgdt %0" : : "m"(gdtp));
+
+	tss.ss0  = 0x10;    /* kernel data segment */
+	tss.esp0 = 0x30800; /* 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));
+}

+ 23 - 0
tss.h

@@ -0,0 +1,23 @@
+/* TSS + flat GDT (kernel/user segments + TSS descriptor). */
+#ifndef TSS_H
+#define TSS_H
+
+struct tss {
+	unsigned int prev_tss;
+	unsigned int esp0;
+	unsigned int ss0;
+	unsigned int esp1, ss1, esp2, ss2;
+	unsigned int cr3;
+	unsigned int eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
+	unsigned int es, cs, ss, ds, fs, gs;
+	unsigned int ldt;
+	unsigned short trap, iomap_base;
+} __attribute__((packed));
+
+extern struct tss tss;
+
+/* Build the 6-entry GDT (null / kernel code / kernel data / user code / user
+ * data / TSS), load it, and load the TSS.  Call once from kernel_main. */
+void gdt_tss_init(void);
+
+#endif