From b926d50365bedd6731d652a5c38dfd6c78fbb0bd Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 22 Jul 2026 00:06:57 -0400 Subject: [PATCH] fork: properly set up child process Signed-off-by: Michael Shaw --- amd64/arch/entry.S | 10 ++++++++ amd64/kernel/proc.c | 19 ++++++++-------- amd64/kernel/time.c | 6 +++++ include/kernel/time.h | 3 ++- kernel/proc/proc.c | 7 ++++++ kernel/proc/sched.c | 53 +++++++++++++++++++++++++++++++------------ 6 files changed, 73 insertions(+), 25 deletions(-) diff --git a/amd64/arch/entry.S b/amd64/arch/entry.S index 4e33b9f..a407e13 100644 --- a/amd64/arch/entry.S +++ b/amd64/arch/entry.S @@ -121,6 +121,7 @@ _syscall_enter: swapgs sysretq +/* called from the scheduler to switch processes */ .global switch_to switch_to: push_all @@ -129,6 +130,7 @@ switch_to: pop_all ret +/* return path after a process was execed */ .global _enter_userspace_init _enter_userspace_init: mov rcx, rdi @@ -153,6 +155,14 @@ _enter_userspace_init: sysretq +/* set as entrypoint for a new process that was just forked */ +.global fork_trampoline +fork_trampoline: + mov rsp, rdx + swapgs + sysretq + + /* exceptions */ exception 0x00 exception 0x01 diff --git a/amd64/kernel/proc.c b/amd64/kernel/proc.c index 14e7f87..b3f2e27 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -52,7 +52,7 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry) _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() { @@ -69,18 +69,17 @@ pid_t sys_fork() vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE; child->kstack_top = ktop; - /* switch_to's pop_all restores rax..r15, then ret returns. - * Set up so child resumes via _enter_userspace_init(entry, flags, stack_top) - * with rax=0 (fork return value). + /* Copy parent's register state, then: + * rax = 0 (child 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 *child_r = (struct pt_regs *)(ktop - sizeof(struct pt_regs)); - memset(child_r, 0, 15 * sizeof(u64)); - child_r->rdx = parent_r->rsp; /* rdx = user stack */ - child_r->rdi = parent_r->rcx; /* rdi = return addr */ - child_r->rsi = 0x202; /* rsi = flags */ - /* last address on the stack, *(ktop) is not actually mapped */ - *(u64 *)(ktop - 8) = (u64)_enter_userspace_init; + memcpy(child_r, parent_r, sizeof(struct pt_regs) - sizeof(u64)); + child_r->rax = 0; + child_r->rdx = parent_r->rsp; + *(u64 *)(ktop - 8) = (u64)fork_trampoline; child->kstack_ptr = (uaddr_t)child_r; child->state = PROC_STATE_RUNNING; diff --git a/amd64/kernel/time.c b/amd64/kernel/time.c index 61bd90d..7374a4f 100644 --- a/amd64/kernel/time.c +++ b/amd64/kernel/time.c @@ -21,6 +21,12 @@ struct timespec get_realtime() return ret; } +time_t unix_time() +{ + u64 sec = (rdtsc() - timekeeper.boot_tsc) / timekeeper.tsc_hz; + return timekeeper.boot_unix_seconds + sec; +} + void time_init() { timekeeper.tsc_hz = tsc_calibrate(); diff --git a/include/kernel/time.h b/include/kernel/time.h index afd3bb4..8db9b42 100644 --- a/include/kernel/time.h +++ b/include/kernel/time.h @@ -5,11 +5,12 @@ #include struct timespec { - i64 tv_sec; + time_t tv_sec; i64 tv_nsec; }; time_t arch_get_real_time(); +time_t unix_time(); u64 tsc_calibrate(); u64 rdtsc(); struct timespec get_realtime(); diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 87d87dd..1fb3422 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -71,6 +71,13 @@ static int copy_parent(struct proc *new, struct proc *parent) 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; } diff --git a/kernel/proc/sched.c b/kernel/proc/sched.c index 47206d3..a5837a5 100644 --- a/kernel/proc/sched.c +++ b/kernel/proc/sched.c @@ -9,31 +9,56 @@ static struct { struct thread *head; struct thread *tail; spinlock_t lock; -} _run_queue; +} sched_queue; void sched_add_thread(struct thread *t) { - spinlock_acquire(&_run_queue.lock); + spinlock_acquire(&sched_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); + if (sched_queue.tail) + sched_queue.tail->next = t; + sched_queue.tail = t; + if (!sched_queue.head) + sched_queue.head = t; + 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() { - spinlock_acquire(&_run_queue.lock); - struct thread *t = _run_queue.head; + spinlock_acquire(&sched_queue.lock); + struct thread *t = sched_queue.head; if (t) { - _run_queue.head = t->next; - if (!_run_queue.head) - _run_queue.tail = NULL; + sched_queue.head = t->next; + if (!sched_queue.head) + sched_queue.tail = NULL; t->next = NULL; } - spinlock_release(&_run_queue.lock); + spinlock_release(&sched_queue.lock); return t; }