/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include #include #include #define USTACK_DEFAULT_SIZE 0x10000 #define P_STACK_SIZE 0x4000 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) { spinlock_acquire(&thread->mem_lock); page_map(thread->pgd, p, v, len, flags, &thread->memmap); spinlock_release(&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) { spinlock_acquire(&parent->mem_lock); spinlock_acquire(&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) { spinlock_release(&child->mem_lock); spinlock_release(&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: spinlock_release(&child->mem_lock); spinlock_release(&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; } spinlock_acquire(&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)); } spinlock_release(&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"); }