Redesign userspace memory management
All checks were successful
Build documentation / build-and-deploy (push) Successful in 44s

This commit is contained in:
2026-01-27 17:04:08 +01:00
parent 600886a7ee
commit b388b30b24
23 changed files with 195 additions and 484 deletions

View File

@@ -20,7 +20,6 @@ static atomic_int pids = 1;
struct proc* proc_from_elf (uint8_t* elf_contents) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
int rid;
struct proc* proc = malloc (sizeof (*proc));
if (proc == NULL)
@@ -39,32 +38,11 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
}
procgroup_attach (proc->procgroup, proc);
rid = procgroup_get_sys_rid (proc->procgroup);
struct proc_resource* kstk_r =
proc_create_resource_mem (proc->procgroup, rid, KSTACK_SIZE / PAGE_SIZE, 0, false);
if (kstk_r == NULL) {
procgroup_detach (proc->procgroup, proc);
free (proc);
return NULL;
}
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
proc->pdata.kernel_stack = kstk_r->u.mem.paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
rid = procgroup_get_sys_rid (proc->procgroup);
struct proc_resource* ustk_r =
proc_create_resource_mem (proc->procgroup, rid, USTACK_SIZE / PAGE_SIZE, 0, false);
if (ustk_r == NULL) {
kstk_r->ops.cleanup (kstk_r);
free (kstk_r);
procgroup_detach (proc->procgroup, proc);
free (proc);
return NULL;
}
proc->pdata.user_stack = ustk_r->u.mem.paddr;
proc_map (proc, proc->pdata.user_stack, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW);
procgroup_map (proc->procgroup, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW, NULL);
proc->flags |= PROC_USTK_PREALLOC;
@@ -83,7 +61,6 @@ struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, size_t stack_
uintptr_t entry) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
spin_lock_ctx_t ctxprt;
int rid;
struct proc* proc = malloc (sizeof (*proc));
if (proc == NULL)
@@ -102,27 +79,8 @@ struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, size_t stack_
spin_unlock (&proto->lock, &ctxprt);
uintptr_t vstack_bottom = vstack_top - stack_size;
uintptr_t pstack_bottom = mm_v2p (&proc->procgroup->pd, vstack_bottom, MM_PD_LOCK);
if (pstack_bottom == 0) {
procgroup_detach (proc->procgroup, proc);
free (proc);
return NULL;
}
rid = procgroup_get_sys_rid (proc->procgroup);
struct proc_resource* kstk_r =
proc_create_resource_mem (proc->procgroup, rid, KSTACK_SIZE / PAGE_SIZE, 0, false);
if (kstk_r == NULL) {
procgroup_detach (proc->procgroup, proc);
free (proc);
return NULL;
}
proc->pdata.kernel_stack = kstk_r->u.mem.paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
proc->pdata.user_stack = pstack_bottom + stack_size;
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
proc->pdata.regs.ss = GDT_UDATA | 0x03;
proc->pdata.regs.rsp = (uint64_t)vstack_top;
@@ -161,24 +119,7 @@ void proc_cleanup (struct proc* proc) {
procgroup_detach (proc->procgroup, proc);
/* clean virtual address space */
/* if (atomic_fetch_sub (&proc->pd->refs, 1) == 1) { */
/* DEBUG ("PID %d Free virtual address space\n", proc->pid); */
/* struct list_node_link *mapping_link, *mapping_link_tmp; */
/* spin_lock (&proc->pd->lock, &ctxprpd); */
/* list_foreach (proc->mappings, mapping_link, mapping_link_tmp) { */
/* struct proc_mapping* mapping = */
/* list_entry (mapping_link, struct proc_mapping, proc_mappings_link); */
/* list_remove (proc->mappings, mapping_link); */
/* free (mapping); */
/* } */
/* pmm_free (proc->pd->cr3_paddr, 1); */
/* spin_unlock (&proc->pd->lock, &ctxprpd); */
/* free (proc->pd); */
/* } */
pmm_free (proc->pdata.kernel_stack, KSTACK_SIZE / PAGE_SIZE);
/* clean the process */
free (proc);