sched: improve thread handling on system calls

This commit is contained in:
Michael Shaw 2025-02-11 18:58:00 -05:00
commit 72827c1e00
9 changed files with 157 additions and 180 deletions

View file

@ -79,8 +79,12 @@ ssize_t serial_read(char *buf)
struct proc *proc = proc_find(getpid());
while((ret = ringbuf_read(tty0->data, &c, 1)) == 0) {
kthread_yield();
serial_trap();
while ((ret = ringbuf_read(tty0->data, &c, 1)) == 0) {
_save_context();
sti();
yield();
}
copy_to_user(proc, buf, &c, 1);

View file

@ -36,6 +36,7 @@ void panic();
void cli();
void sti();
void _save_context();
void load_stack_and_jump(uintptr_t rsp, uintptr_t rbp, void *func, void *arg);
typedef uintptr_t paddr_t;
typedef uint32_t paddr32_t;

View file

@ -13,6 +13,8 @@
#define PROC_RUNNING 0
#define PROC_STOPPED 1
#define PROC_BLOCKED 2
#define PROC_YIELD 3
#define PROC_ZOMBIE 4
#define PT_USER 0
#define PT_KERN 1
@ -49,13 +51,6 @@ struct procregs {
} PACKED;
typedef struct _kthread {
struct proc *proc;
uintptr_t stack;
bool done;
uint64_t retval;
} kthread_t;
struct proc {
struct procregs regs;
pid_t pid;
@ -75,15 +70,18 @@ struct proc {
struct proc *parent;
struct rbtree children;
kthread_t *waiting_on;
struct proc *buddy_proc;
};
struct procregs *proc_current_regs();
struct proc *proc_create();
struct proc *proc_createv(int flags);
void proc_init_memory(struct proc *proc, uint64_t mem_flags);
struct proc *proc_fork(struct proc *parent);
struct proc *proc_get(pid_t pid);
pid_t getpid();
pid_t getupid();
void proc_term(pid_t pid);
struct proc *proc_find(pid_t pid);
void proc_save_state();
@ -93,11 +91,6 @@ void trap_sched();
void schedule();
void syscall_block();
kthread_t *kthread_create();
void kthread_call(kthread_t *thread, uint64_t entry, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
uint64_t kthread_join(kthread_t *thread);
void kthread_yield();
kthread_t *kthread_current();
void sswtch();
#endif /* _PROC_H_ */

View file

@ -51,22 +51,19 @@ void exception(int vector, int error)
{
pid_t pid = getpid();
kprintf("Process %d terminated: %s(%xh)\n", pid, exception_names[vector], error);
struct proc *proc = proc_find(pid);
if (proc->is_kernel) {
kprintf(LOG_ERROR "Kernel process %d crashed: %s(%xh)\n", pid, exception_names[vector], error);
if (exception_handlers[vector])
exception_handlers[vector](error);
if(proc->pid == 0)
panic();
} else {
proc_term(pid);
kprintf("Process %d terminated: %s(%xh)\n", pid, exception_names[vector], error);
if (exception_handlers[vector])
exception_handlers[vector](error);
proc_set_current(0);
schedule();
}
proc_set_current(0);
proc_term(pid);
schedule();
}
void exception_init()

View file

@ -256,6 +256,13 @@ gate_syscall_int80:
save_context
save_iretq_frame
/* switch to a kernel process */
movq %rax, %rdi
movq $_gate_syscall_int80_entry_ret, %rsi
call proc_enter_kernel
_gate_syscall_int80_entry_ret:
movq %rdi, %rax
/* restore argument registers */
movq 40(%rax), %rdi
movq 32(%rax), %rsi
@ -269,6 +276,12 @@ gate_syscall_int80:
call syscall
/* switch back to user process */
movq %rax, %rdi
movq $1, %rsi
cli
call proc_leave_kernel
restore_iretq_frame
pushq %rax
restore_context
@ -332,8 +345,3 @@ _return_to_user:
yield:
hlt
jmp yield
.global _kthread_exit
_kthread_exit:
movq %rax, %rdi
call kthread_exit

View file

@ -1,4 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "kernel/mem.h"
#include <kernel/common.h>
#include <kernel/proc.h>
#include <kernel/rbtree.h>
@ -45,6 +46,16 @@ pid_t getpid()
return proc_current[lapic_idno()];
}
pid_t getupid()
{
struct proc *proc = proc_find(getpid());
if (proc && proc->buddy_proc)
return proc->buddy_proc->pid;
else
return 0;
}
struct proc *proc_find(pid_t pid)
{
if (pid == 0)
@ -85,7 +96,6 @@ void proc_term(pid_t pid)
{
struct rbnode *proc_node = rbt_search(proc_tree, pid);
if (proc_node) {
struct proc *proc = (void *)proc_node->value;
spinlock_acquire(&proc->lock);
@ -119,7 +129,7 @@ void proc_term(pid_t pid)
slab_free(proc_slab, proc);
} else {
kprintf(LOG_ERROR "proc: proc_term: proc %u not found\n", pid);
kprintf(LOG_ERROR "proc: proc_term: proc %d not found\n", pid);
panic();
}
}
@ -145,20 +155,24 @@ void schedule()
size_t num_procs = proc_tree->num_nodes;
size_t counter = 0;
size_t limit = num_procs * 2;
while (counter++ < limit) {
while (counter < limit) {
/* next process */
proc = proc_next(proc);
if (proc->pid == 0)
continue;
spinlock_acquire(&proc->lock);
switch (proc->state) {
case PROC_STOPPED:
proc->state = PROC_RUNNING;
proc_set_current(proc->pid);
spinlock_release(&proc->lock);
_return_to_user(&proc->regs, proc->cr3);
break;
case PROC_ZOMBIE:
case PROC_BLOCKED:
case PROC_RUNNING:
spinlock_release(&proc->lock);
@ -166,11 +180,39 @@ void schedule()
}
}
proc_set_current(0);
/* no process to run */
sti();
yield();
}
void proc_init_memory(struct proc *proc, uint64_t mem_flags)
{
if (proc->is_kernel && proc->buddy_proc) {
proc->cr3 = proc->buddy_proc->cr3;
} else {
proc->cr3 = (uintptr_t)buddy_alloc(0x1000);
memcpy((void *)proc->cr3, (void *)(kcr3 | hhdm_start), 0x1000);
proc->cr3 &= ~(hhdm_start);
}
uintptr_t stackaddr = (uintptr_t)buddy_alloc(0x4000);
if (!proc->is_kernel) {
stackaddr &= ~(hhdm_start);
proc_mmap(proc, stackaddr | hhdm_start, stackaddr, 0x4000, PAGE_XD | PAGE_RW | PAGE_PRESENT);
proc->regs.cs = GDT_ACCESS_CODE_RING3 | 3;
proc->regs.ss = GDT_ACCESS_DATA_RING3 | 3;
} else {
proc->regs.cs = GDT_ACCESS_CODE_RING0 | 0;
proc->regs.ss = GDT_ACCESS_DATA_RING0 | 0;
}
proc->regs.rflags = 0x246;
stackaddr += 0x3FF0;
proc->regs.rsp = stackaddr;
proc->regs.rbp = stackaddr;
}
struct proc *proc_createv(int flags)
{
struct proc *proc = slab_alloc(proc_slab);
@ -178,20 +220,17 @@ struct proc *proc_createv(int flags)
spinlock_acquire(&proc->lock);
if(flags & PT_KERN) {
if (flags & PT_KERN) {
kpid_counter++;
proc->pid = -kpid_counter;
proc->state = PROC_STOPPED;
proc->is_kernel = true;
} else {
pid_counter++;
proc->pid = pid_counter;
proc->state = PROC_RUNNING; /* prevent immediate scheduling */
proc->is_kernel = false;
}
proc->state = PROC_RUNNING; /* prevent immediate scheduling */
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
proc_node->value = (uintptr_t)proc;
@ -215,6 +254,62 @@ struct proc *proc_get(pid_t pid)
return NULL;
}
void proc_enter_kernel(uint64_t save, void *ret)
{
struct proc *proc = proc_find(getpid());
if (proc->is_kernel) {
kprintf(LOG_ERROR "proc: proc_enter_kernel: process %d is already a kernel process\n", getpid());
panic();
}
struct proc *kernel_proc = proc_createv(PT_KERN);
kernel_proc->is_kernel = true;
kernel_proc->buddy_proc = proc;
proc_init_memory(kernel_proc, PAGE_PRESENT | PAGE_RW);
spinlock_acquire(&proc->lock);
proc->state = PROC_BLOCKED;
spinlock_release(&proc->lock);
proc->buddy_proc = kernel_proc;
kernel_proc->buddy_proc = proc;
/* move the save value into RAX */
kernel_proc->regs.rax = save;
proc_set_current(kernel_proc->pid);
load_stack_and_jump(kernel_proc->regs.rsp, kernel_proc->regs.rbp, ret, (void *)save);
}
uint64_t proc_leave_kernel(uint64_t ret, bool shouldret)
{
struct proc *proc = proc_find(getpid());
if (!proc->is_kernel) {
kprintf(LOG_ERROR "proc: proc_leave_kernel: process %d is not a kernel process\n", getpid());
panic();
}
struct proc *uproc = proc->buddy_proc;
uproc->buddy_proc = NULL;
if (shouldret) {
uproc->regs.rax = ret;
}
proc_set_current(uproc->pid);
spinlock_acquire(&proc->lock);
proc->state = PROC_ZOMBIE;
spinlock_release(&proc->lock);
uproc->state = PROC_RUNNING;
return ret;
}
void proc_init(unsigned num_cpus)
{
/* allocate current process structs */

View file

@ -12,7 +12,7 @@
#define SYSCALL_MAX 0x100
typedef void *(*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
typedef uint64_t (*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
syscall_t syscall_table[SYSCALL_MAX];
slab_t *fd_slab;
@ -21,28 +21,18 @@ void gate_syscall();
uint64_t syscall(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t syscall_no)
{
syscall_t syscall = (void *)syscall_table[syscall_no];
syscall_t sc_sel = (void *)syscall_table[syscall_no];
if (syscall == NULL)
if (sc_sel == NULL)
return -ENOSYS;
kprintf("syscall %d\n", syscall_no);
struct proc *proc = proc_find(getpid());
if (proc == NULL)
return -1;
kthread_t *sc_thread = kthread_create();
if (sc_thread == NULL)
return -ENOMEM;
uint64_t ret = sc_sel(arg1, arg2, arg3, arg4, arg5);
sc_thread->proc = proc;
proc->waiting_on = sc_thread;
proc->state = PROC_BLOCKED;
kthread_call(sc_thread, (uint64_t)syscall, arg1, arg2, arg3, arg4, arg5);
return kthread_join(sc_thread);
return ret;
}
void sys_set_return(struct proc *proc, uint64_t ret)
@ -58,7 +48,7 @@ ssize_t sys_read(int fd, void *buf, size_t count)
if ((uintptr_t)buf >= hhdm_start)
return -EFAULT;
struct proc *proc = proc_find(getpid());
struct proc *proc = proc_find(getupid());
if (proc == NULL)
return -1;
@ -93,7 +83,7 @@ ssize_t sys_write(int fd, void *buf, size_t count)
if ((uintptr_t)buf >= hhdm_start)
return -EFAULT;
struct proc *proc = proc_find(getpid());
struct proc *proc = proc_find(getupid());
if (proc == NULL)
return -1;
@ -123,7 +113,7 @@ int sys_open(const char *pathname, int flags)
if ((uintptr_t)pathname >= hhdm_start)
return -EFAULT;
struct proc *proc = proc_find(getpid());
struct proc *proc = proc_find(getupid());
if (proc == NULL)
return -1;
@ -152,7 +142,8 @@ int sys_open(const char *pathname, int flags)
void sys_exit(int status)
{
struct proc *proc = proc_find(getpid());
pid_t pid = getupid();
struct proc *proc = proc_find(pid);
if (proc == NULL)
return;
@ -160,9 +151,9 @@ void sys_exit(int status)
proc->state = PROC_STOPPED;
spinlock_release(&proc->lock);
proc_term(getpid());
proc_term(pid);
kprintf("(syscall)Process %d exited with status 0x%x\n", getpid(), status);
kprintf("(syscall)Process %d exited with status 0x%x\n", pid, status);
proc_set_current(0);
schedule();
@ -170,7 +161,7 @@ void sys_exit(int status)
pid_t sys_fork()
{
struct proc *parent = proc_find(getpid());
struct proc *parent = proc_find(getupid());
struct proc *proc = proc_create();
proc_clone_mmap(parent, proc);

View file

@ -1,94 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <kernel/common.h>
#include <kernel/proc.h>
#include <kernel/gdt.h>
#include <kernel/slab.h>
void _kthread_exit();
kthread_t *kthread_create()
{
struct proc *proc = proc_create();
if (proc == NULL)
return NULL;
kthread_t *thread = kzalloc(sizeof(kthread_t), ALLOC_KERN);
if (thread == NULL) {
kfree(proc);
return NULL;
}
thread->proc = proc;
return thread;
}
kthread_t *kthread_current()
{
struct proc *proc = proc_find(getpid());
if (proc == NULL)
return NULL;
return proc->waiting_on;
}
void kthread_yield()
{
sswtch();
}
void kthread_exit(uint64_t retval)
{
kthread_t *thread = kthread_current();
if (thread == NULL)
panic();
thread->proc->state = PROC_BLOCKED;
thread->done = true;
thread->retval = retval;
sswtch();
}
uint64_t kthread_join(kthread_t *thread)
{
if (thread == NULL)
return -1;
while (!thread->done)
sswtch();
uint64_t retval = thread->retval;
proc_term(thread->proc->pid);
buddy_free((void *)thread->stack);
kfree(thread);
return retval;
}
void kthread_call(kthread_t *thread, uint64_t entry, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
{
thread->stack = (uintptr_t)buddy_alloc(KSTACK_SIZE);
thread->stack += KSTACK_SIZE - 8;
thread->proc->regs.rip = entry;
thread->proc->regs.rdi = arg1;
thread->proc->regs.rsi = arg2;
thread->proc->regs.rdx = arg3;
thread->proc->regs.rcx = arg4;
thread->proc->regs.r8 = arg5;
thread->proc->regs.cs = GDT_SEGMENT_CODE_RING0;
thread->proc->regs.ss = GDT_SEGMENT_DATA_RING0;
thread->proc->regs.rflags = 0x246;
thread->proc->regs.rax = 0;
thread->proc->cr3 = kcr3;
thread->proc->regs.rsp = thread->stack;
thread->proc->regs.rbp = thread->stack;
thread->proc->state = PROC_STOPPED; /* allow scheduling */
*(uint64_t *)thread->stack = (uint64_t)_kthread_exit;
}

View file

@ -25,27 +25,9 @@ void _start()
promptfd(fd);
char c[2];
ssize_t ret;
while ((ret = read(fd, c, 1)) > 0) {
if (c[0] == '\r')
c[0] = '\n';
write(fd, c, 1);
if (c[0] == '\n') {
write(fd, "attempting to fork\n", 19);
pid_t pid = fork();
if (pid == 0) {
write(fd, "child\n", 6);
exit(0);
} else {
write(fd, "parent\n", 7);
}
}
char buf[256];
int n;
while ((n = read(fd, buf, 256)) > 0) {
write(fd, buf, n);
}
exit(0x1234);
}