thread: add kthreads

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-12 22:03:52 -04:00
commit 54803ed29b
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
6 changed files with 79 additions and 36 deletions

View file

@ -142,24 +142,16 @@ sched_restore_state:
pop_all
ret
.global thread_wait
thread_wait:
.global reschedule
reschedule:
push_all
mov rax, qword ptr gs:[24] /* cur_thread */
mov [rax + 8], rsp /* save rsp to kstack_ptr */
mov qword ptr [rax + 32], 1 /* state = PROC_STATE_WAITING */
call sched_add_thread /* re-add to run queue */
call schedule
/* not reached */
.global thread_block
thread_block:
push_all
mov rax, qword ptr gs:[24] /* cur_thread */
mov [rax + 8], rsp /* save rsp to kstack_ptr */
mov qword ptr [rax + 32], 3 /* state = PROC_STATE_BLOCKED */
call schedule
/* not reached */
mov rax, gs:[0]
mov [rax + 8], rsp
mov [rax + 40], rdi
cmp rdi, 3
je 1f
call sched_add_thread
1: call schedule
.global _syscall_enter
_syscall_enter:
@ -215,6 +207,14 @@ fork_trampoline:
swapgs
sysretq
/* kthread entry: rdi=arg, rdx=fn */
/* argument are pre-set before the thread execution begins */
.global kthread_entry
kthread_entry:
call rdx
mov edi, 4
call reschedule
/* unreachable */
/* exceptions */
exception 0x00

View file

@ -18,6 +18,7 @@ struct cpu_local {
vaddr_t irq_rsp;
struct tss tss;
u32 apic_id;
struct thread *idle_thread;
} PACKED;
struct pt_regs {
@ -45,7 +46,6 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry);
struct pt_regs *current_pt_regs();
u32 cpu_id();
void thread_wait();
void thread_block();
void reschedule(u64 state);
#endif /* _ARCH_PROC_H_ */

View file

@ -2,6 +2,7 @@
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/atomic.h>
#include <kernel/thread.h>
#include <arch/proc.h>
static uint64_t spinlock_attempt_acquire(spinlock_t *lock)
@ -29,7 +30,7 @@ void cleanup_spinlock(spinlock_t **lock_ptr)
void mtx_lock(mtx_t *l)
{
while (spinlock_attempt_acquire(l))
thread_wait();
reschedule(THREAD_STATE_WAITING);
}
void mtx_unlock(mtx_t *l)

View file

@ -28,16 +28,25 @@ u32 cpu_id()
return tls_get()->cpu_id;
}
int thread_set_entry(struct thread *thread, uaddr_t entry)
{
char *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
if (!kstack)
return -ENOMEM;
thread->kstack_top = (vaddr_t)kstack + KSTACK_SIZE;
thread->entry = entry;
return 0;
}
void fork_trampoline();
void enter_userspace_init(struct thread *thread, uaddr_t entry)
{
thread_set_entry(thread, entry);
struct cpu_local *tls = tls_get();
tls->cur_thread = thread;
void *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
thread->kstack_top = (vaddr_t)kstack + KSTACK_SIZE;
tls->kernel_rsp = thread->kstack_top;
tls->tss.rsp0 = thread->kstack_top;
@ -58,10 +67,9 @@ tid_t sys_fork()
if (!child)
return -ENOMEM;
char *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE;
child->kstack_top = ktop;
thread_set_entry(child, parent->entry);
vaddr_t ktop = child->kstack_top;
struct pt_regs *parent_r = current_pt_regs();
struct pt_regs *child_r = (struct pt_regs *)(ktop - sizeof(struct pt_regs));
memcpy(child_r, parent_r, sizeof(struct pt_regs));
@ -76,6 +84,44 @@ tid_t sys_fork()
return child->tid;
}
extern void kthread_entry();
struct thread *thread_kthread(void (*fn)(void *), void *arg, const char *name)
{
struct thread *thread = slab_alloc(thread_slab);
if (!thread)
return NULL;
memset(thread, 0, sizeof(struct thread));
thread->tid = alloc_tid();
thread->group = thread->tid;
thread->state = THREAD_STATE_DNS;
thread->pgd = kpgd;
char *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
if (!kstack) {
slab_free(thread_slab, thread);
return NULL;
}
vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE;
struct pt_regs *r = (struct pt_regs *)(ktop - sizeof(struct pt_regs) - 8);
memset(r, 0, sizeof(struct pt_regs));
r->rdi = (u64)arg;
r->rdx = (u64)fn;
*(u64 *)(ktop - 8) = (u64)kthread_entry;
thread->kstack_top = ktop;
thread->kstack_ptr = (uaddr_t)r;
if (name)
thread->name = strdup(name);
sched_add_thread(thread);
return thread;
}
static void arch_thread_init()
{
syscall_insert(sys_fork, SYS_fork);