Explorar o código

Fix the ring-3 execution for our first actual userspace programm

Johannes Zellner hai 1 mes
pai
achega
7bbe4281f9
Modificáronse 7 ficheiros con 88 adicións e 21 borrados
  1. 25 14
      kernel.c
  2. 7 0
      paging.c
  3. 1 2
      process.c
  4. 10 0
      switch.S
  5. 1 4
      syscall.c
  6. 18 0
      syscall_stub.S
  7. 26 1
      timer.S

+ 25 - 14
kernel.c

@@ -50,9 +50,10 @@ unsigned int get_ticks_ms(void)
 	return ticks;
 }
 
-void exc_handler(unsigned int vector);
-void exc_handler(unsigned int vector)
+void exc_handler(unsigned int eip, unsigned int vector);
+void exc_handler(unsigned int eip, unsigned int vector)
 {
+	unsigned int cr2 = 0;
 	static const char * const names[] = {
 		"#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
 		"#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
@@ -62,6 +63,21 @@ void exc_handler(unsigned int vector)
 		"#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
 		"#XM SIMD Error",
 	};
+	__asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
+	/* Print the fault to serial FIRST, using only minimal/no-stack code, so it
+	 * survives even if the screen buffer or kernel stack is the problem. */
+	serial_puts(SERIAL_COM1, "\n*** EXC vec ");
+	serial_puthex(SERIAL_COM1, vector);
+	serial_puts(SERIAL_COM1, " eip=");
+	serial_puthex(SERIAL_COM1, eip);
+	serial_puts(SERIAL_COM1, " cr2=");
+	serial_puthex(SERIAL_COM1, cr2);
+	serial_puts(SERIAL_COM1, " ");
+	if (vector < 20)
+		serial_puts(SERIAL_COM1, names[vector]);
+	serial_puts(SERIAL_COM1, " ***\n");
+
+	/* Then attempt the screen output (may fault if the screen is broken). */
 	io_putstr("\n*** EXCEPTION: ");
 	if (vector < 20)
 		io_putstr(names[vector]);
@@ -156,24 +172,19 @@ 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 + gdt_tss_init must run BEFORE timer_init: once the PIT is
+	 * started (sti in timer_init) the context-switch stub runs on every tick
+	 * and reads procs[].cr3 / procs[].kstack_top / the TSS, so those have to be
+	 * valid first (otherwise the first switch loads a garbage CR3 and faults). */
 	process_init();
-	serial_puts(SERIAL_COM1, "dbg: process done\n");
 	gdt_tss_init();
-	serial_puts(SERIAL_COM1, "dbg: gdt_tss done\n");
+	timer_init();
+	mouse_init();
 	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();
 }

+ 7 - 0
paging.c

@@ -41,6 +41,13 @@ void paging_init(void)
 			__asm__ volatile ("cli; hlt");
 	}
 
+	/* Zero the whole page directory.  mm_alloc_page does NOT clear memory, so
+	 * on real hardware the high PDEs (indices 32..1023, incl. the user region
+	 * at 0x100) would otherwise be garbage.  QEMU zeroes guest RAM, which is
+	 * why this only bites on real silicon. */
+	for (i = 0; i < 1024; i++)
+		page_dir[i] = 0;
+
 	/* 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;

+ 1 - 2
process.c

@@ -327,8 +327,6 @@ int process_create_user(const char *name, const void *image, unsigned int size)
 	}
 	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 */
@@ -360,6 +358,7 @@ void process_set_screen(struct text_screen *screen)
 void process_exit(void)
 {
 	procs[current_pid].state = PROC_DEAD;
+	__asm__ volatile ("sti");  /* re-enable IRQs so the PIT can switch us out */
 	for (;;)
 		__asm__ volatile ("hlt");
 }

+ 10 - 0
switch.S

@@ -36,6 +36,16 @@
 pit_irq_stub:
 	pusha
 
+	/* Load the kernel data segment.  The PIT can fire while a ring-3 process
+	 * is running, and in that case the CPU has switched CS/SS but left DS
+	 * null (IRET reset it when it returned to ring-3); the data accesses
+	 * below (ticks, counters, procs, tss) go through DS and would #GP. */
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  %ax, %gs
+
 	/* ticks++ and send EOI -- always, even when we don't switch. */
 	incl  ticks
 	movb  $0x20, %al

+ 1 - 4
syscall.c

@@ -12,10 +12,6 @@ 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);
@@ -25,6 +21,7 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 	case SYS_SLEEP_MS: {
 		unsigned int start = get_ticks_ms();
 		unsigned int ms = (unsigned int)a1;
+		__asm__ volatile ("sti");  /* re-enable IRQs so hlt wakes on the PIT */
 		while (get_ticks_ms() - start < ms)
 			cpu_idle();
 		return 0;

+ 18 - 0
syscall_stub.S

@@ -8,7 +8,25 @@
 
 .globl syscall_stub
 syscall_stub:
+	/* Disable interrupts for the duration of the syscall: the trap gate keeps
+	 * IF=1, so otherwise the PIT could fire and the context switch would switch
+	 * the stack out from under us mid-syscall.  Blocking syscalls (sleep/exit)
+	 * re-enable interrupts themselves. */
+	cli
+
+	/* Load the kernel data segment into DS/ES/FS/GS.  int 0x80 only switches
+	 * CS and SS; IRET reset DS (etc.) to null when it returned to ring-3, so
+	 * DS is null here and the C dispatcher's data accesses would #GP. */
+	pushl %eax
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  %ax, %gs
+	popl  %eax
+
 	pusha
+
 	pushl %edx
 	pushl %ecx
 	pushl %ebx

+ 26 - 1
timer.S

@@ -6,6 +6,13 @@
 .globl kbd_irq_stub
 kbd_irq_stub:
 	pusha
+	/* Reload the kernel data segment: this IRQ can fire while a ring-3
+	 * process runs, in which case DS is null (IRET reset it). */
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  %ax, %gs
 	call  kbd_irq
 	popa
 	iret
@@ -13,6 +20,11 @@ kbd_irq_stub:
 .globl fdc_irq_stub
 fdc_irq_stub:
 	pusha
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  %ax, %gs
 	call  fdc_irq
 	popa
 	iret
@@ -77,7 +89,20 @@ exc_no_err 30
 exc_no_err 31
 
 exc_common:
-	call  exc_handler
+	/* Load the kernel data segment: exceptions only switch CS, and DS may be
+	 * null (IRET reset it to null when returning to ring-3), which would make
+	 * the C handler's string/data accesses fault. */
+	pushl %eax
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  %ax, %gs
+	popl  %eax
+
+	pushl 4(%esp)      /* faulting EIP -> 1st arg */
+	call  exc_handler  /* exc_handler(eip, vector) */
+	addl  $4, %esp
 	cli
 1:	hlt
 	jmp   1b