From b6a9ddd1d0f178e2e37636275608feafaedf4d45 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 21 Jul 2026 21:18:51 -0400 Subject: [PATCH] sched: add basic scheduler, fork syscall Signed-off-by: Michael Shaw --- amd64/arch/entry.S | 11 +++++++- amd64/kernel/proc.c | 40 +++++++++++++++++++++++++++ include/kernel/proc.h | 4 +++ include/kernel/sched.h | 9 +++++++ include/kernel/syscall.h | 2 ++ kernel/proc/Makefile | 1 + kernel/proc/proc.c | 16 ++++++----- kernel/proc/sched.c | 58 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 include/kernel/sched.h create mode 100644 kernel/proc/sched.c diff --git a/amd64/arch/entry.S b/amd64/arch/entry.S index aeab2d4..227ab1e 100644 --- a/amd64/arch/entry.S +++ b/amd64/arch/entry.S @@ -98,7 +98,16 @@ irq_common: 2: add rsp, 8 iretq - + +/* Called by switch_to's ret when a thread is first scheduled. + * RSP points at user_rsp (set up by sys_fork / sched_init). + * rcx and r11 already set from pop_all (rcx=entry, r11=rflags). + */ +.global sched_start +sched_start: + pop rsp + swapgs + sysretq .global _syscall_enter _syscall_enter: diff --git a/amd64/kernel/proc.c b/amd64/kernel/proc.c index e2ac5b6..be40ec3 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include "arch/msr.h" struct cpu_local *tls_get() @@ -47,3 +50,40 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry) void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top); _enter_userspace_init(entry, 0x202, thread->stack_addr); } + +extern void sched_start(void); + +pid_t sys_fork() +{ + struct thread *parent = get_cur_thread(); + struct proc *child_proc = proc_create(parent); + if (!child_proc) + return -ENOMEM; + + struct thread *child = child_proc->main_thread; + child_proc->pid = alloc_pid(); + child->tid = child_proc->pid; + + void *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN); + vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE; + child->kstack_top = ktop; + + struct pt_regs *parent_r = current_pt_regs(); + u64 *sp = (u64 *)(ktop - 128); + sp[0] = 0; + memcpy(&sp[1], &parent_r->rbx, 14 * sizeof(u64)); + *(u64 *)(ktop - 8) = (u64)sched_start; + *(u64 *)(ktop) = parent_r->rsp; + child->stack_addr = (uaddr_t)sp; + + child->state = PROC_STATE_RUNNING; + sched_add_thread(child); + + return child_proc->pid; +} + +static void arch_proc_init() +{ + syscall_insert(sys_fork, SYS_fork); +} +DRIVER_ENTRY(arch_proc_init); diff --git a/include/kernel/proc.h b/include/kernel/proc.h index 1dddfb4..64e0bbf 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -49,6 +49,7 @@ struct thread { struct proc *proc; struct thread *next; + uaddr_t kstack_top; }; struct proc { @@ -85,5 +86,8 @@ struct proc *proc_create(struct thread *parent); void proc_free(struct proc *proc); void switch_to(struct thread *from, struct thread *to); void proc_init(); +pid_t sys_fork(); +void schedule(); +pid_t alloc_pid(); #endif /* _PROC_H_ */ diff --git a/include/kernel/sched.h b/include/kernel/sched.h new file mode 100644 index 0000000..5447c38 --- /dev/null +++ b/include/kernel/sched.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _SCHED_H_ +#define _SCHED_H_ + +#include + +void sched_add_thread(struct thread *t); + +#endif /* _SCHED_H_ */ diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h index 6087ff3..342aa96 100644 --- a/include/kernel/syscall.h +++ b/include/kernel/syscall.h @@ -10,6 +10,8 @@ #define SYS_close 3 #define SYS_pread 4 #define SYS_pwrite 5 +#define SYS_fork 57 +#define SYS_exit 60 typedef ssize_t (*syscall_t)(u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6); diff --git a/kernel/proc/Makefile b/kernel/proc/Makefile index b94c85b..53b6c99 100644 --- a/kernel/proc/Makefile +++ b/kernel/proc/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += proc.o obj-y += load.o +obj-y += sched.o diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 2726b61..9e6b81c 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include -#include #include #include +#include #include #define USTACK_DEFAULT_SIZE 0x10000 @@ -10,6 +10,14 @@ mem_cache_t *proc_slab; mem_cache_t *thread_slab; +/* PID allocator */ +static u64 _pid_counter = 1; + +pid_t alloc_pid() +{ + return atomic_xadd(&_pid_counter, 1); +} + int proc_kill(struct proc *proc, int signal) { return -ENOSYS; @@ -114,12 +122,6 @@ struct proc *proc_create(struct thread *parent) return ret; } -pid_t sys_fork() -{ - struct thread *cur_thread = get_cur_thread(); - struct proc *ret = proc_create(cur_thread); -} - void proc_free(struct proc *proc) { slab_free(proc_slab, proc); diff --git a/kernel/proc/sched.c b/kernel/proc/sched.c new file mode 100644 index 0000000..47206d3 --- /dev/null +++ b/kernel/proc/sched.c @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include + +/* Run queue */ +static struct { + struct thread *head; + struct thread *tail; + spinlock_t lock; +} _run_queue; + +void sched_add_thread(struct thread *t) +{ + spinlock_acquire(&_run_queue.lock); + t->next = NULL; + if (_run_queue.tail) + _run_queue.tail->next = t; + _run_queue.tail = t; + if (!_run_queue.head) + _run_queue.head = t; + spinlock_release(&_run_queue.lock); +} + +static struct thread *dequeue_thread() +{ + spinlock_acquire(&_run_queue.lock); + struct thread *t = _run_queue.head; + if (t) { + _run_queue.head = t->next; + if (!_run_queue.head) + _run_queue.tail = NULL; + t->next = NULL; + } + spinlock_release(&_run_queue.lock); + return t; +} + +void schedule() +{ + struct thread *cur = get_cur_thread(); + struct thread *next = dequeue_thread(); + if (!next) + return; + + struct thread *prev = cur; + if (cur) + sched_add_thread(cur); + + struct cpu_local *tls = tls_get(); + tls->cur_thread = next; + tls->kernel_rsp = next->kstack_top; + tls->tss.rsp0 = next->kstack_top; + pgd_load(next->proc->pgd); + + switch_to(prev, next); +}