From f92e88597de0a9725a9ec35233b3677b57ca9906 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 11 Feb 2025 21:11:49 -0500 Subject: [PATCH] sched: fix page faulting on process switching This is the first version of the kernel with fully isolated threads for user and kernel tasks, where process 0 is only used for scheduling and interrupt handling. User process have a "buddy" kernel process that handles its tasks in the kernel. This system allows for easy blocking of threads without much need for thought. Just `sswtch` in an infinite read loop, and your userspace process will be blocked. Signed-off-by: Michael Shaw --- dev/serial.c | 19 +++++++------ include/kernel/proc.h | 5 +++- init/main.c | 3 +- init/trap.c | 2 +- sched/entry.S | 62 +++++++++++++++++++++++++++++++++++------ sched/proc.c | 41 +++++++++++++++++++++++---- sched/syscall.c | 6 ---- usr.bin/shtest/shtest.c | 17 +++++++++++ 8 files changed, 125 insertions(+), 30 deletions(-) diff --git a/dev/serial.c b/dev/serial.c index 3ea5a72..57e6590 100644 --- a/dev/serial.c +++ b/dev/serial.c @@ -60,14 +60,19 @@ inline void serial_putchar(char c) outb(COM1, c); } -void serial_trap() +void serial_read_line() { - while ((inb(COM1 | 5) & 0x01) == 0) - ; + if ((inb(COM1 | 5) & 0x01) == 0) + return; + char c = inb(COM1); ringbuf_write(tty0->data, &c, 1); +} +void serial_trap() +{ + serial_read_line(); lapic_eoi(); } @@ -77,14 +82,12 @@ ssize_t serial_read(char *buf) char c; + serial_read_line(); + struct proc *proc = proc_find(getpid()); - serial_trap(); - while ((ret = ringbuf_read(tty0->data, &c, 1)) == 0) { - _save_context(); - sti(); - yield(); + sswtch(); } copy_to_user(proc, buf, &c, 1); diff --git a/include/kernel/proc.h b/include/kernel/proc.h index e8b04c5..2d7cfe3 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -53,13 +53,13 @@ struct procregs { struct proc { struct procregs regs; + uint8_t state; pid_t pid; pid_t ppid; uint64_t cr3; spinlock_t lock; bool is_kernel; - uint8_t state; struct rbtree page_map; spinlock_t page_map_lock; @@ -71,6 +71,9 @@ struct proc { struct rbtree children; struct proc *buddy_proc; + + uintptr_t stack_start; + uintptr_t stack_size; }; diff --git a/init/main.c b/init/main.c index 39a9235..38d888c 100644 --- a/init/main.c +++ b/init/main.c @@ -75,7 +75,8 @@ void panic() { kprintf(LOG_ERROR "KERNEL PANIC\n"); cli(); - yield(); + while (1) + yield(); } char *kcmdline_get_symbol(const char *sym) diff --git a/init/trap.c b/init/trap.c index 9d812b9..4fb2029 100644 --- a/init/trap.c +++ b/init/trap.c @@ -44,7 +44,7 @@ static void exception_page_fault(int err) { uint64_t cr2 = cr2_read(); - kprintf(LOG_ERROR "Page fault at %xh, error code %xh\n", cr2, err); + kprintf(LOG_ERROR "Page fault at %Xh, error code %xh\n", cr2, err); } void exception(int vector, int error) diff --git a/sched/entry.S b/sched/entry.S index 4b7fda2..6441dae 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -172,11 +172,6 @@ iretq .endm -.global _save_context -_save_context: - save_context - ret - .global sti sti: sti @@ -292,17 +287,24 @@ _gate_syscall_int80_entry_ret: .global sswtch sswtch: save_context - movw %ss, %di + xorq %rdi, %rdi + movw %ss, %di movq %rdi, 152(%rax) - pushfq /* save flags */ + pushfq popq %rdi movq %rsp, 144(%rax) movq %rdi, 136(%rax) + xorq %rdi, %rdi movw %cs, %di movq %rdi, 128(%rax) movq $_sswtch_ret, 120(%rax) + call getpid + movq %rax, %rdi + movq $1, %rsi /* PROC_STOPPED: allow to be scheduled */ + call proc_set_state + call schedule _sswtch_ret: ret @@ -341,7 +343,51 @@ _return_to_user: iretq +.global _save_context +_save_context: + pushq %rax + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi + pushq %r8 + pushq %r9 + pushq %r10 + pushq %r11 + + /* get current task registers */ + call proc_current_regs + + /* save current task registers */ + popq %rdi + movq %rdi, 80(%rax) /* r11 */ + popq %rdi + movq %rdi, 72(%rax) /* r10 */ + popq %rdi + movq %rdi, 64(%rax) /* r9 */ + popq %rdi + movq %rdi, 56(%rax) /* r8 */ + popq %rdi + movq %rdi, 40(%rax) /* rdi */ + popq %rdi + movq %rdi, 32(%rax) /* rsi */ + popq %rdi + movq %rdi, 24(%rax) /* rdx */ + popq %rdi + movq %rdi, 16(%rax) /* rcx */ + popq %rdi + movq %rdi, 0(%rax) /* rax */ + + /* save remaining registers */ + movq %rbx, 8(%rax) + movq %rbp, 48(%rax) + movq %r12, 88(%rax) + movq %r13, 96(%rax) + movq %r14, 104(%rax) + movq %r15, 112(%rax) + ret + .global yield yield: hlt - jmp yield + ret diff --git a/sched/proc.c b/sched/proc.c index d9264f4..e25dd2b 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -56,6 +56,14 @@ pid_t getupid() return 0; } +void proc_set_state(pid_t pid, uint8_t state) +{ + struct proc *proc = proc_find(pid); + spinlock_acquire(&proc->lock); + proc->state = state; + spinlock_release(&proc->lock); +} + struct proc *proc_find(pid_t pid) { if (pid == 0) @@ -134,6 +142,17 @@ void proc_term(pid_t pid) } } +void proc_yield() +{ + struct proc *proc = proc_find(getpid()); + proc_set_current(0); + spinlock_acquire(&proc->lock); + proc->state = PROC_STOPPED; + spinlock_release(&proc->lock); + sti(); + yield(); +} + void trap_sched() { lapic_eoi(); @@ -194,7 +213,9 @@ void proc_init_memory(struct proc *proc, uint64_t mem_flags) memcpy((void *)proc->cr3, (void *)(kcr3 | hhdm_start), 0x1000); proc->cr3 &= ~(hhdm_start); } + uintptr_t stackaddr = (uintptr_t)buddy_alloc(0x4000); + proc->stack_start = stackaddr; if (!proc->is_kernel) { stackaddr &= ~(hhdm_start); @@ -224,10 +245,14 @@ struct proc *proc_createv(int flags) kpid_counter++; proc->pid = -kpid_counter; proc->is_kernel = true; + proc->regs.cs = GDT_ACCESS_CODE_RING0 | 0; + proc->regs.ss = GDT_ACCESS_DATA_RING0 | 0; } else { pid_counter++; proc->pid = pid_counter; proc->is_kernel = false; + proc->regs.cs = GDT_ACCESS_CODE_RING3 | 3; + proc->regs.ss = GDT_ACCESS_DATA_RING3 | 3; } proc->state = PROC_RUNNING; /* prevent immediate scheduling */ @@ -263,10 +288,15 @@ void proc_enter_kernel(uint64_t save, void *ret) panic(); } - struct proc *kernel_proc = proc_createv(PT_KERN); + struct proc *kernel_proc; + if (proc->buddy_proc) { + kernel_proc = proc->buddy_proc; + } else { + kernel_proc = proc_createv(PT_KERN); - kernel_proc->is_kernel = true; - kernel_proc->buddy_proc = proc; + kernel_proc->is_kernel = true; + kernel_proc->buddy_proc = proc; + } proc_init_memory(kernel_proc, PAGE_PRESENT | PAGE_RW); @@ -293,7 +323,6 @@ uint64_t proc_leave_kernel(uint64_t ret, bool shouldret) } struct proc *uproc = proc->buddy_proc; - uproc->buddy_proc = NULL; if (shouldret) { uproc->regs.rax = ret; @@ -302,10 +331,12 @@ uint64_t proc_leave_kernel(uint64_t ret, bool shouldret) proc_set_current(uproc->pid); spinlock_acquire(&proc->lock); - proc->state = PROC_ZOMBIE; + proc->state = PROC_BLOCKED; spinlock_release(&proc->lock); + spinlock_acquire(&uproc->lock); uproc->state = PROC_RUNNING; + spinlock_release(&uproc->lock); return ret; } diff --git a/sched/syscall.c b/sched/syscall.c index 091830b..16c0fa2 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -65,8 +65,6 @@ ssize_t sys_read(int fd, void *buf, size_t count) ssize_t ret = vfs_read(file, tmp_buf, fdesc->pos, count); - proc->state = PROC_RUNNING; - if (ret > 0) memcpy(buf, tmp_buf, count); else @@ -147,10 +145,6 @@ void sys_exit(int status) if (proc == NULL) return; - spinlock_acquire(&proc->lock); - proc->state = PROC_STOPPED; - spinlock_release(&proc->lock); - proc_term(pid); kprintf("(syscall)Process %d exited with status 0x%x\n", pid, status); diff --git a/usr.bin/shtest/shtest.c b/usr.bin/shtest/shtest.c index 831298e..cfe1fba 100644 --- a/usr.bin/shtest/shtest.c +++ b/usr.bin/shtest/shtest.c @@ -27,6 +27,23 @@ void _start() char buf[256]; int n; + + /* fork test */ + pid_t pid = fork(); + if (pid < 0) { + write(fd, "fork failed\n", 12); + exit(1); + } + + if (pid == 0) { + write(fd, "child\n", 6); + while (1) + ; + } else { + write(fd, "parent\n", 7); + } + + /* rw test */ while ((n = read(fd, buf, 256)) > 0) { write(fd, buf, n); }