sched: add basic scheduler, fork syscall

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-21 21:18:51 -04:00
commit b6a9ddd1d0
8 changed files with 133 additions and 8 deletions

View file

@ -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:

View file

@ -2,6 +2,9 @@
#include <arch/proc.h>
#include <arch/page.h>
#include <kernel/proc.h>
#include <kernel/sched.h>
#include <kernel/syscall.h>
#include <lib/string.h>
#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);