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 {
tid_t tid;
vaddr_t stack_addr;
uaddr_t stack_addr;
size_t stack_size;
u64 state;
bool in_kernel;
@ -46,7 +47,7 @@ struct thread {
struct proc {
pid_t pid;
struct thread main_thread;
struct thread *main_thread;
char *name;

View file

@ -1,8 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/page.h>
#include <kernel/proc.h>
#include <kernel/slab.h>
#include <lib/string.h>
#define USTACK_DEFAULT_SIZE 0x10000
mem_cache_t *proc_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);
}
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)
{
spinlock_acquire(&parent->mem_lock);
@ -32,16 +57,7 @@ static int copy_parent(struct proc *new, struct proc *parent)
while (node) {
struct vm_area *area = node->ptr;
void *vm_alloc = kalloc(area->len, ALLOC_USER);
if (!vm_alloc)
break;
struct proc_alloc *a = kalloc(sizeof(struct proc_alloc), ALLOC_KERN);
if (!a) {
kfree(vm_alloc);
break;
}
void *vm_alloc = proc_alloc(new, area->len);
proc_mmap(new, TO_PHYS(vm_alloc), area->start, area->len, area->flags);
node = rbt_successor(node);
@ -66,18 +82,33 @@ struct proc *proc_create(struct proc *parent)
return NULL;
}
ret->pgd = pgd_create_user();
if (!ret->pgd) {
r_mt->stack_size = USTACK_DEFAULT_SIZE;
r_mt->stack_addr = 0x7FFFFFFFF000;
void *ustack = proc_alloc(ret, r_mt->stack_size);
if (!ustack) {
slab_free(proc_slab, ret);
slab_free(thread_slab, r_mt);
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) {
int res = copy_parent(ret, parent);
if (res) {
if (res)
kerr("failed to copy parent memmap for new process: %s\n", strerror(-res));
}
}
return ret;