fork: properly set up child process

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-22 00:06:57 -04:00
commit b926d50365
6 changed files with 73 additions and 25 deletions

View file

@ -121,6 +121,7 @@ _syscall_enter:
swapgs swapgs
sysretq sysretq
/* called from the scheduler to switch processes */
.global switch_to .global switch_to
switch_to: switch_to:
push_all push_all
@ -129,6 +130,7 @@ switch_to:
pop_all pop_all
ret ret
/* return path after a process was execed */
.global _enter_userspace_init .global _enter_userspace_init
_enter_userspace_init: _enter_userspace_init:
mov rcx, rdi mov rcx, rdi
@ -153,6 +155,14 @@ _enter_userspace_init:
sysretq sysretq
/* set as entrypoint for a new process that was just forked */
.global fork_trampoline
fork_trampoline:
mov rsp, rdx
swapgs
sysretq
/* exceptions */ /* exceptions */
exception 0x00 exception 0x00
exception 0x01 exception 0x01

View file

@ -52,7 +52,7 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry)
_enter_userspace_init(entry, 0x202, thread->ustack_top); _enter_userspace_init(entry, 0x202, thread->ustack_top);
} }
void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top); void fork_trampoline();
pid_t sys_fork() pid_t sys_fork()
{ {
@ -69,18 +69,17 @@ pid_t sys_fork()
vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE; vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE;
child->kstack_top = ktop; child->kstack_top = ktop;
/* switch_to's pop_all restores rax..r15, then ret returns. /* Copy parent's register state, then:
* Set up so child resumes via _enter_userspace_init(entry, flags, stack_top) * rax = 0 (child fork return value)
* with rax=0 (fork return value). * rdx = parent_r->rsp (user stack for fork_trampoline's mov rsp, rdx)
* ret_addr = fork_trampoline (does mov rsp, rdx; swapgs; sysretq)
*/ */
struct pt_regs *parent_r = current_pt_regs(); struct pt_regs *parent_r = current_pt_regs();
struct pt_regs *child_r = (struct pt_regs *)(ktop - sizeof(struct pt_regs)); struct pt_regs *child_r = (struct pt_regs *)(ktop - sizeof(struct pt_regs));
memset(child_r, 0, 15 * sizeof(u64)); memcpy(child_r, parent_r, sizeof(struct pt_regs) - sizeof(u64));
child_r->rdx = parent_r->rsp; /* rdx = user stack */ child_r->rax = 0;
child_r->rdi = parent_r->rcx; /* rdi = return addr */ child_r->rdx = parent_r->rsp;
child_r->rsi = 0x202; /* rsi = flags */ *(u64 *)(ktop - 8) = (u64)fork_trampoline;
/* last address on the stack, *(ktop) is not actually mapped */
*(u64 *)(ktop - 8) = (u64)_enter_userspace_init;
child->kstack_ptr = (uaddr_t)child_r; child->kstack_ptr = (uaddr_t)child_r;
child->state = PROC_STATE_RUNNING; child->state = PROC_STATE_RUNNING;

View file

@ -21,6 +21,12 @@ struct timespec get_realtime()
return ret; return ret;
} }
time_t unix_time()
{
u64 sec = (rdtsc() - timekeeper.boot_tsc) / timekeeper.tsc_hz;
return timekeeper.boot_unix_seconds + sec;
}
void time_init() void time_init()
{ {
timekeeper.tsc_hz = tsc_calibrate(); timekeeper.tsc_hz = tsc_calibrate();

View file

@ -5,11 +5,12 @@
#include <kernel/types.h> #include <kernel/types.h>
struct timespec { struct timespec {
i64 tv_sec; time_t tv_sec;
i64 tv_nsec; i64 tv_nsec;
}; };
time_t arch_get_real_time(); time_t arch_get_real_time();
time_t unix_time();
u64 tsc_calibrate(); u64 tsc_calibrate();
u64 rdtsc(); u64 rdtsc();
struct timespec get_realtime(); struct timespec get_realtime();

View file

@ -71,6 +71,13 @@ static int copy_parent(struct proc *new, struct proc *parent)
spinlock_release(&parent->mem_lock); spinlock_release(&parent->mem_lock);
if (parent->fd_array_size) {
spinlock_acquire(&parent->fd_lock);
new->fd_array_size = parent->fd_array_size;
new->fdt = kalloc(new->fd_array_size * sizeof(struct file), ALLOC_KERN);
spinlock_release(&parent->fd_lock);
}
return 0; return 0;
} }

View file

@ -9,31 +9,56 @@ static struct {
struct thread *head; struct thread *head;
struct thread *tail; struct thread *tail;
spinlock_t lock; spinlock_t lock;
} _run_queue; } sched_queue;
void sched_add_thread(struct thread *t) void sched_add_thread(struct thread *t)
{ {
spinlock_acquire(&_run_queue.lock); spinlock_acquire(&sched_queue.lock);
t->next = NULL; t->next = NULL;
if (_run_queue.tail) if (sched_queue.tail)
_run_queue.tail->next = t; sched_queue.tail->next = t;
_run_queue.tail = t; sched_queue.tail = t;
if (!_run_queue.head) if (!sched_queue.head)
_run_queue.head = t; sched_queue.head = t;
spinlock_release(&_run_queue.lock); spinlock_release(&sched_queue.lock);
}
void sched_purge_proc(struct proc *proc)
{
spinlock_acquire(&sched_queue.lock);
struct thread *t = sched_queue.head;
struct thread *p = NULL;
while (t) {
if (t->proc == proc) {
if (p)
p->next = t->next;
else
sched_queue.head = t->next;
if (!t->next)
sched_queue.tail = p;
t->next = NULL;
} else {
p = t;
}
t = t->next;
}
spinlock_release(&sched_queue.lock);
} }
static struct thread *dequeue_thread() static struct thread *dequeue_thread()
{ {
spinlock_acquire(&_run_queue.lock); spinlock_acquire(&sched_queue.lock);
struct thread *t = _run_queue.head; struct thread *t = sched_queue.head;
if (t) { if (t) {
_run_queue.head = t->next; sched_queue.head = t->next;
if (!_run_queue.head) if (!sched_queue.head)
_run_queue.tail = NULL; sched_queue.tail = NULL;
t->next = NULL; t->next = NULL;
} }
spinlock_release(&_run_queue.lock); spinlock_release(&sched_queue.lock);
return t; return t;
} }