vbos/kernel/thread/thread.c
Michael Shaw 2076f1dae1
lock: merge spinlock and mtx locks into one
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-13 14:19:10 -04:00

146 lines
3.1 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/page.h>
#include <kernel/thread.h>
#include <kernel/slab.h>
#include <kernel/atomic.h>
#include <kernel/driver.h>
#include <lib/string.h>
#define USTACK_DEFAULT_SIZE 0x10000
mem_cache_t *thread_slab;
static u64 _tid_counter = 1;
tid_t alloc_tid()
{
return atomic_xadd(&_tid_counter, 1);
}
void thread_mmap(struct thread *thread, paddr_t p, vaddr_t v, size_t len, u64 flags)
{
spin_lock(&thread->mem_lock);
page_map(thread->pgd, p, v, len, flags, &thread->memmap);
spin_unlock(&thread->mem_lock);
}
void *thread_alloc(struct thread *thread, size_t size)
{
return kalloc(size, ALLOC_USER);
}
static int copy_parent_memmap(struct thread *child, struct thread *parent)
{
spin_lock(&parent->mem_lock);
spin_lock(&child->mem_lock);
rbnode_t *node = parent->memmap.root;
if (!node)
goto done;
while (node->left)
node = node->left;
while (node) {
struct vm_area *area = node->ptr;
void *pages = kalloc(area->len, ALLOC_USER);
if (!pages) {
spin_unlock(&child->mem_lock);
spin_unlock(&parent->mem_lock);
return -ENOMEM;
}
memcpy(pages, (void *)area->start, area->len);
page_map(child->pgd, TO_PHYS(pages), area->start, area->len, area->flags, &child->memmap);
node = rbt_successor(node);
}
done:
spin_unlock(&child->mem_lock);
spin_unlock(&parent->mem_lock);
return 0;
}
struct thread *thread_create(void)
{
struct thread *thread = slab_alloc(thread_slab);
if (!thread)
return NULL;
memset(thread, 0, sizeof(struct thread));
thread->stack_size = USTACK_DEFAULT_SIZE;
thread->ustack_top = 0x7FFFFFFFF000;
thread->tid = alloc_tid();
thread->state = THREAD_STATE_DNS;
thread->group = thread->tid;
void *ustack = thread_alloc(thread, thread->stack_size);
if (!ustack) {
slab_free(thread_slab, thread);
return NULL;
}
thread->pgd = pgd_create_user();
if (!thread->pgd) {
slab_free(thread_slab, thread);
kfree(ustack);
return NULL;
}
thread_mmap(thread, TO_PHYS(ustack), thread->ustack_top - thread->stack_size, thread->stack_size, PAGE_RW | PAGE_USER);
return thread;
}
struct thread *thread_fork(struct thread *parent)
{
struct thread *child = slab_alloc(thread_slab);
if (!child)
return NULL;
memset(child, 0, sizeof(struct thread));
child->tid = alloc_tid();
child->stack_size = parent->stack_size;
child->ustack_top = parent->ustack_top;
child->state = THREAD_STATE_DNS;
child->pgd = pgd_create_user();
if (!child->pgd) {
slab_free(thread_slab, child);
return NULL;
}
if (copy_parent_memmap(child, parent) < 0) {
slab_free(thread_slab, child);
return NULL;
}
spin_lock(&parent->fd_lock);
if (parent->fd_array_size) {
child->fd_array_size = parent->fd_array_size;
child->fdt = kalloc(child->fd_array_size * sizeof(struct file), ALLOC_KERN);
if (child->fdt)
memcpy(child->fdt, parent->fdt, child->fd_array_size * sizeof(struct file));
}
spin_unlock(&parent->fd_lock);
child->group = child->tid;
return child;
}
void thread_free(struct thread *thread)
{
slab_free(thread_slab, thread);
}
void thread_init()
{
thread_slab = slab_create("thread_slab", sizeof(struct thread), 1 * MB, 8);
krdy("thread_init ready\n");
}