Răsfoiți Sursa

Ring-3: keep user data segments valid and kill faulting commands

A general protection fault on the board (EIP 0x40000040, the first store
through a pointer in `cpuinfo`) was the CPU's own behaviour, not the
command: an IRET back to ring 3 nulls DS/ES/FS/GS whose DPL is below
CPL 3, and every stub loads 0x10 (DPL 0) to run kernel code.  QEMU does
not model the nulling, so it only showed on real hardware.

- irqret.inc adds IRQ_RETURN_DS <cs_offset>: before the final popa/iret,
  load 0x23 (user data) for a ring-3 context or 0x10 for ring-0.  Used by
  the PIT switch (switch.S), the keyboard and floppy stubs (timer.S) and
  the syscall return (syscall_stub.S); default_irq_stub never touches the
  segment registers, so it needs nothing.
- exc_handler() now gets the faulting CS: a fault from ring 3 kills that
  process (a line in its terminal and on serial) and the kernel keeps
  running; kernel faults still print the kdbg banner and halt.

Verified on QEMU by reading the selectors inside a command at entry,
after a syscall, after timer preemption and after sleep (all 0x23), and
by deliberately faulting a command: it was killed and the following
commands ran normally.  On the board, `cpuinfo` now runs.

AGENTS.md: the exception-banner note and a new "ring-3 data segments"
convention.
Johannes Zellner 1 lună în urmă
părinte
comite
a760be4599
7 a modificat fișierele cu 87 adăugiri și 10 ștergeri
  1. 11 1
      AGENTS.md
  2. 3 3
      Makefile
  3. 31 0
      irqret.inc
  4. 18 3
      kernel.c
  5. 7 0
      switch.S
  6. 8 0
      syscall_stub.S
  7. 9 3
      timer.S

+ 11 - 1
AGENTS.md

@@ -478,7 +478,9 @@ SMM) breaks otherwise.
 
 **Exception banner**: `exc_handler()` (`kernel.c`) calls `kdbg_panic()`, which
 draws a red banner with vector name, EIP and CR2 at the top of the screen
-(visible without serial) before the CPU halts.
+(visible without serial) before the CPU halts.  Faults from ring 3 do not halt:
+the process is killed (with a short message in its terminal and on serial) and
+the kernel keeps running.
 
 API / wiring:
 
@@ -517,6 +519,14 @@ Caveats:
   grown (new cluster allocation for directories is not implemented).
 - **Keep the memory map in sync** — hardcoded addresses appear in `.S`, `.c`, and
   linker scripts; changing one without the others breaks boot/loading.
+- **Ring-3 data segments** — when an IRET returns to ring 3, the CPU nulls
+  DS/ES/FS/GS whose DPL is below CPL 3.  Every stub that loads 0x10 to run
+  kernel code must therefore restore the user data selector (0x23) before
+  returning to a ring-3 context: use `IRQ_RETURN_DS <cs_offset>`
+  (`irqret.inc`), as `switch.S`, `timer.S` and `syscall_stub.S` do.  QEMU does
+  not model this nulling, so a missing restore only fails on real hardware
+  (symptom: ring-3 #GP on its first data access, e.g. a store through a
+  pointer).
 
 ## Known issues (candidates for future tasks)
 

+ 3 - 3
Makefile

@@ -9,7 +9,7 @@ OBJCOPY = objcopy
 CFLAGS   = -ffreestanding -fno-pie -m32 -march=i586 -nostdlib -fno-stack-protector \
            -fno-asynchronous-unwind-tables -mno-sse -mno-sse2 -mno-mmx \
            -Wall -Wextra -O2 -c
-ASFLAGS  = --32
+ASFLAGS  = --32 -I.
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 
@@ -239,10 +239,10 @@ boot.elf: boot.o
 vesa_stage.o: vesa_stage.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-timer.o: timer.S
+timer.o: timer.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-switch.o: switch.S
+switch.o: switch.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
 entry.o: entry.S

+ 31 - 0
irqret.inc

@@ -0,0 +1,31 @@
+/* IRQ_RETURN_DS <cs_offset>
+ *
+ * Load DS/ES/FS/GS for the context we are about to IRET to, in a stub that
+ * switched them to the kernel data selector (0x10) to run C code.
+ *
+ * Why: an IRET to a less privileged level nulls DS/ES/FS/GS whose DPL is
+ * lower than the new CPL.  Coming back to ring 3 with 0x10 (DPL 0) therefore
+ * leaves the process with a *null* DS, and user code #GPs on its first data
+ * access (even a simple stack-buffer store through a pointer).  QEMU does not
+ * model this nulling, so it only shows up on real hardware.
+ *
+ * So: ring-3 contexts get the user data selector (0x23), ring-0 contexts keep
+ * 0x10.  <cs_offset> is the byte offset of the saved CS in the current frame
+ * (pusha is 32 bytes, then the CPU's EIP/CS/EFLAGS: offset 36).  Clobbers %cx,
+ * which the caller restores via popa.
+ */
+.macro IRQ_RETURN_DS cs_offset
+	movw  \cs_offset(%esp), %cx
+	andw  $3, %cx
+	cmpw  $3, %cx
+	jne   1f
+	movw  $0x23, %cx
+	jmp   2f
+1:
+	movw  $0x10, %cx
+2:
+	movw  %cx, %ds
+	movw  %cx, %es
+	movw  %cx, %fs
+	movw  %cx, %gs
+.endm

+ 18 - 3
kernel.c

@@ -55,8 +55,8 @@ unsigned int get_ticks_ms(void)
 	return ticks;
 }
 
-void exc_handler(unsigned int eip, unsigned int vector);
-void exc_handler(unsigned int eip, unsigned int vector)
+void exc_handler(unsigned int eip, unsigned int vector, unsigned int cs);
+void exc_handler(unsigned int eip, unsigned int vector, unsigned int cs)
 {
 	unsigned int cr2 = 0;
 	static const char * const names[] = {
@@ -69,6 +69,20 @@ void exc_handler(unsigned int eip, unsigned int vector)
 		"#XM SIMD Error",
 	};
 	__asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
+
+	/* A fault from ring 3 is a bug in a user command (or its ELF): kill that
+	 * process and keep the kernel running.  Kernel faults still halt. */
+	if ((cs & 3) == 3) {
+		serial_puts(SERIAL_COM1, "\n*** user fault vec ");
+		serial_puthex(SERIAL_COM1, vector);
+		serial_puts(SERIAL_COM1, " eip=");
+		serial_puthex(SERIAL_COM1, eip);
+		serial_puts(SERIAL_COM1, " -- process killed\n");
+		io_putstr("\n*** fault: ");
+		io_putstr(vector < 20 ? names[vector] : "unknown vector");
+		io_putstr(" -- command killed\n");
+		process_exit();     /* marks us dead and waits to be switched out */
+	}
 	/* Draw the fault info directly into the framebuffer FIRST: with no
 	 * serial console attached, a fault was previously invisible (the text
 	 * went to the current process's screen buffer, which the WM never got
@@ -143,7 +157,8 @@ static void timer_init(void)
 	for (i = 0; i < 256; i++)
 		set_idt_gate(i, (unsigned int)default_irq_stub);
 
-	/* Exception handlers 0-31: print the fault name and halt. */
+	/* Exception handlers 0-31: ring-3 faults kill the process, kernel
+	   faults print the panic banner and halt (see exc_handler). */
 	for (i = 0; i < 32; i++)
 		set_idt_gate(i, exc_stub_table[i]);
 

+ 7 - 0
switch.S

@@ -24,6 +24,8 @@
 .extern kdbg_pit_tick
 .extern tss
 
+.include "irqret.inc"
+
 /* struct process offsets (must match process.h / process.c) */
 .equ PCB_ESP,   0
 .equ PCB_CR3,   32
@@ -93,6 +95,11 @@ pit_irq_stub:
 	movl  %ebx, tss+4           /* TSS.esp0 = this process's ring-0 stack top */
 
 .no_switch:
+	/* Both paths (return to the interrupted process, or to one just switched
+	 * in) must leave a usable data segment selector for the context being
+	 * resumed; ring-3 needs 0x23 (see irqret.inc). */
+	IRQ_RETURN_DS 36
+
 	popa
 	iret
 

+ 8 - 0
syscall_stub.S

@@ -6,6 +6,8 @@
 .code32
 .text
 
+.include "irqret.inc"
+
 .globl syscall_stub
 syscall_stub:
 	/* Disable interrupts for the duration of the syscall: the trap gate keeps
@@ -34,5 +36,11 @@ syscall_stub:
 	call  syscall_dispatch
 	addl  $16, %esp
 	movl  %eax, 28(%esp)   /* overwrite the saved eax with the return value */
+
+	/* Returning to the ring-3 caller: leave it a usable data selector (the
+	 * IRET would null 0x10, and user code #GPs on its first data access).
+	 * See irqret.inc for the why; this gate is ring-3-only. */
+	IRQ_RETURN_DS 36
+
 	popa
 	iret

+ 9 - 3
timer.S

@@ -3,6 +3,8 @@
 .code32
 .text
 
+.include "irqret.inc"
+
 .globl kbd_irq_stub
 kbd_irq_stub:
 	pusha
@@ -14,6 +16,7 @@ kbd_irq_stub:
 	movw  %ax, %fs
 	movw  %ax, %gs
 	call  kbd_irq
+	IRQ_RETURN_DS 36
 	popa
 	iret
 
@@ -26,6 +29,7 @@ fdc_irq_stub:
 	movw  %ax, %fs
 	movw  %ax, %gs
 	call  fdc_irq
+	IRQ_RETURN_DS 36
 	popa
 	iret
 
@@ -100,9 +104,11 @@ exc_common:
 	movw  %ax, %gs
 	popl  %eax
 
-	pushl 4(%esp)      /* faulting EIP -> 1st arg */
-	call  exc_handler  /* exc_handler(eip, vector) */
-	addl  $4, %esp
+	pushl 8(%esp)      /* CS  -> 3rd arg */
+	pushl 4(%esp)      /* vector -> 2nd arg */
+	pushl 12(%esp)     /* faulting EIP -> 1st arg */
+	call  exc_handler  /* exc_handler(eip, vector, cs) */
+	addl  $12, %esp
 	cli
 1:	hlt
 	jmp   1b