proc_create: init user stack

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-19 16:52:30 -04:00
commit 48ba3d4fa2
2 changed files with 46 additions and 14 deletions

View file

@ -33,7 +33,8 @@ struct proc;
struct thread { struct thread {
tid_t tid; tid_t tid;
vaddr_t stack_addr; uaddr_t stack_addr;
size_t stack_size;
u64 state; u64 state;
bool in_kernel; bool in_kernel;
@ -46,7 +47,7 @@ struct thread {
struct proc { struct proc {
pid_t pid; pid_t pid;
struct thread main_thread; struct thread *main_thread;
char *name; char *name;

View file

@ -1,8 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/page.h>
#include <kernel/proc.h> #include <kernel/proc.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <lib/string.h> #include <lib/string.h>
#define USTACK_DEFAULT_SIZE 0x10000
mem_cache_t *proc_slab; mem_cache_t *proc_slab;
mem_cache_t *thread_slab; mem_cache_t *thread_slab;
@ -18,6 +21,28 @@ void proc_mmap(struct proc *proc, paddr_t p, vaddr_t v, size_t len, u64 flags)
spinlock_release(&proc->mem_lock); spinlock_release(&proc->mem_lock);
} }
void enter_userspace(struct thread *thread)
{
}
void *proc_alloc(struct proc *proc, size_t size)
{
struct proc_alloc *a = kalloc(sizeof(struct proc_alloc), ALLOC_KERN);
if (!a)
return NULL;
void *ret = kalloc(size, ALLOC_USER);
if (!ret) {
kfree(a);
return NULL;
}
rbnode_t *node = rbt_insert(&proc->alloc_map, (uintptr_t)ret);
node->ptr = ret;
return ret;
}
static int copy_parent(struct proc *new, struct proc *parent) static int copy_parent(struct proc *new, struct proc *parent)
{ {
spinlock_acquire(&parent->mem_lock); spinlock_acquire(&parent->mem_lock);
@ -32,16 +57,7 @@ static int copy_parent(struct proc *new, struct proc *parent)
while (node) { while (node) {
struct vm_area *area = node->ptr; struct vm_area *area = node->ptr;
void *vm_alloc = kalloc(area->len, ALLOC_USER); void *vm_alloc = proc_alloc(new, area->len);
if (!vm_alloc)
break;
struct proc_alloc *a = kalloc(sizeof(struct proc_alloc), ALLOC_KERN);
if (!a) {
kfree(vm_alloc);
break;
}
proc_mmap(new, TO_PHYS(vm_alloc), area->start, area->len, area->flags); proc_mmap(new, TO_PHYS(vm_alloc), area->start, area->len, area->flags);
node = rbt_successor(node); node = rbt_successor(node);
@ -66,19 +82,34 @@ struct proc *proc_create(struct proc *parent)
return NULL; return NULL;
} }
ret->pgd = pgd_create_user(); r_mt->stack_size = USTACK_DEFAULT_SIZE;
if (!ret->pgd) { r_mt->stack_addr = 0x7FFFFFFFF000;
void *ustack = proc_alloc(ret, r_mt->stack_size);
if (!ustack) {
slab_free(proc_slab, ret); slab_free(proc_slab, ret);
slab_free(thread_slab, r_mt); slab_free(thread_slab, r_mt);
return NULL; return NULL;
} }
proc_mmap(ret, TO_PHYS(ustack), r_mt->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW);
ret->main_thread = r_mt;
ret->pgd = pgd_create_user();
if (!ret->pgd) {
slab_free(proc_slab, ret);
slab_free(thread_slab, r_mt);
kfree(ustack);
return NULL;
}
if (parent) { if (parent) {
int res = copy_parent(ret, parent); int res = copy_parent(ret, parent);
if (res) { if (res)
kerr("failed to copy parent memmap for new process: %s\n", strerror(-res)); kerr("failed to copy parent memmap for new process: %s\n", strerror(-res));
} }
}
return ret; return ret;
} }