proc: enter into userspace

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-19 19:25:38 -04:00
commit cd916f624b
12 changed files with 138 additions and 29 deletions

View file

@ -12,9 +12,9 @@
#define PT_DYNAMIC 2 /* Dynamic linking information */
#define PT_INTERP 3 /* Program interpreter */
#define PF_X (1 << 0) /* Segment is executable */
#define PF_W (1 << 1) /* Segment is writable */
#define PF_R (1 << 2) /* Segment is readable */
#define PF_X (1 << 0) /* Segment is executable */
#define PF_W (1 << 1) /* Segment is writable */
#define PF_R (1 << 2) /* Segment is readable */
#define EM_X86_64 62
@ -54,6 +54,6 @@ struct phdr {
Elf64_Xword p_align; /* Segment alignment */
} PACKED;
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs);
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry);
#endif /* _ELF_H_ */

View file

@ -11,7 +11,7 @@
#include "elf.h"
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs)
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry)
{
struct ehdr *ehdr = kalloc(sizeof(struct ehdr), ALLOC_KERN);
if (!ehdr)
@ -36,20 +36,27 @@ int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_
*out_ehdr = ehdr;
*out_phdrs = phdrs;
*entry = ehdr->e_entry;
return ehdr->e_phnum;
}
int proc_load_file(const char *file, struct proc *proc)
ssize_t proc_load_file(const char *file, struct proc *proc)
{
struct ehdr *ehdr;
struct phdr *phdrs;
int res = elf_get_headers(file, &ehdr, &phdrs);
uaddr_t entry = 0;
int res = elf_get_headers(file, &ehdr, &phdrs, &entry);
if (res < 0)
return res;
proc->ehdr = ehdr;
proc->phdrs = phdrs;
proc->n_phdrs = res;
proc->entry = entry;
for (int i = 0; i < res; i++) {
struct phdr *cur = phdrs + i;
if (cur->p_type != PT_LOAD)
@ -77,5 +84,5 @@ int proc_load_file(const char *file, struct proc *proc)
proc_mmap(proc, TO_PHYS(program), page_vaddr, map_size, flags);
}
return 0;
return entry;
}

View file

@ -21,10 +21,6 @@ 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);
@ -84,6 +80,8 @@ struct proc *proc_create(struct proc *parent)
r_mt->stack_size = USTACK_DEFAULT_SIZE;
r_mt->stack_addr = 0x7FFFFFFFF000;
r_mt->proc = ret;
r_mt->state = PROC_STATE_DNS;
void *ustack = proc_alloc(ret, r_mt->stack_size);
@ -93,10 +91,6 @@ struct proc *proc_create(struct proc *parent)
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);
@ -105,6 +99,10 @@ struct proc *proc_create(struct proc *parent)
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;
if (parent) {
int res = copy_parent(ret, parent);
if (res)