X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/986ce23ace2f7875a1a561bd947f435a7594146c..34f6af4f61e0eec9c96113e07f140b609b4113c8:/lunaix-os/kernel/process/process.c diff --git a/lunaix-os/kernel/process/process.c b/lunaix-os/kernel/process/process.c index 1b22219..bb5008d 100644 --- a/lunaix-os/kernel/process/process.c +++ b/lunaix-os/kernel/process/process.c @@ -1,100 +1,21 @@ -#include #include #include -#include #include -#include +#include #include #include -#include #include #include #include #include #include +#include +#include -LOG_MODULE("PROC") - -void* -__dup_pagetable(pid_t pid, uintptr_t mount_point) -{ - void* ptd_pp = pmm_alloc_page(pid, PP_FGPERSIST); - vmm_set_mapping(VMS_SELF, PG_MOUNT_1, ptd_pp, PG_PREM_RW, VMAP_NULL); - - x86_page_table* ptd = PG_MOUNT_1; - x86_page_table* pptd = (x86_page_table*)(mount_point | (0x3FF << 12)); - - size_t kspace_l1inx = L1_INDEX(KERNEL_MM_BASE); - - for (size_t i = 0; i < PG_MAX_ENTRIES - 1; i++) { - - x86_pte_t ptde = pptd->entry[i]; - // 空或者是未在内存中的L1页表项直接照搬过去。 - // 内核地址空间直接共享过去。 - if (!ptde || i >= kspace_l1inx || !(ptde & PG_PRESENT)) { - ptd->entry[i] = ptde; - continue; - } - - // 复制L2页表 - void* pt_pp = pmm_alloc_page(pid, PP_FGPERSIST); - vmm_set_mapping(VMS_SELF, PG_MOUNT_2, pt_pp, PG_PREM_RW, VMAP_NULL); - - x86_page_table* ppt = (x86_page_table*)(mount_point | (i << 12)); - x86_page_table* pt = PG_MOUNT_2; - - for (size_t j = 0; j < PG_MAX_ENTRIES; j++) { - x86_pte_t pte = ppt->entry[j]; - pmm_ref_page(pid, PG_ENTRY_ADDR(pte)); - pt->entry[j] = pte; - } - - ptd->entry[i] = (uintptr_t)pt_pp | PG_ENTRY_FLAGS(ptde); - } - - ptd->entry[PG_MAX_ENTRIES - 1] = NEW_L1_ENTRY(T_SELF_REF_PERM, ptd_pp); +#include +#include - return ptd_pp; -} - -void -__del_pagetable(pid_t pid, uintptr_t mount_point) -{ - x86_page_table* pptd = (x86_page_table*)(mount_point | (0x3FF << 12)); - - // only remove user address space - for (size_t i = 0; i < L1_INDEX(KERNEL_MM_BASE); i++) { - x86_pte_t ptde = pptd->entry[i]; - if (!ptde || !(ptde & PG_PRESENT)) { - continue; - } - - x86_page_table* ppt = (x86_page_table*)(mount_point | (i << 12)); - - for (size_t j = 0; j < PG_MAX_ENTRIES; j++) { - x86_pte_t pte = ppt->entry[j]; - // free the 4KB data page - if ((pte & PG_PRESENT)) { - pmm_free_page(pid, PG_ENTRY_ADDR(pte)); - } - } - // free the L2 page table - pmm_free_page(pid, PG_ENTRY_ADDR(ptde)); - } - // free the L1 directory - pmm_free_page(pid, PG_ENTRY_ADDR(pptd->entry[PG_MAX_ENTRIES - 1])); -} - -void* -vmm_dup_vmspace(pid_t pid) -{ - return __dup_pagetable(pid, VMS_SELF); -} - -__DEFINE_LXSYSCALL(pid_t, fork) -{ - return dup_proc(); -} +LOG_MODULE("PROC") __DEFINE_LXSYSCALL(pid_t, getpid) { @@ -116,7 +37,7 @@ __DEFINE_LXSYSCALL2(int, setpgid, pid_t, pid, pid_t, pgid) struct proc_info* proc = pid ? get_process(pid) : __current; if (!proc) { - __current->k_status = EINVAL; + syscall_result(EINVAL); return -1; } @@ -125,7 +46,7 @@ __DEFINE_LXSYSCALL2(int, setpgid, pid_t, pid, pid_t, pgid) struct proc_info* gruppenfuhrer = get_process(pgid); if (!gruppenfuhrer || proc->pgid == gruppenfuhrer->pid) { - __current->k_status = EINVAL; + syscall_result(EINVAL); return -1; } @@ -136,150 +57,77 @@ __DEFINE_LXSYSCALL2(int, setpgid, pid_t, pid, pid_t, pgid) return 0; } -void -__stack_copied(struct mm_region* region) -{ - mm_index((void**)®ion->proc_vms->stack, region); -} - -void -init_proc_user_space(struct proc_info* pcb) +int +spawn_process(struct thread** created, ptr_t entry, bool with_ustack) { - vmm_mount_pd(VMS_MOUNT_1, pcb->page_table); + struct proc_info* kproc = alloc_process(); + struct proc_mm* mm = vmspace(kproc); - /*--- 分配用户栈 ---*/ + procvm_initvms_mount(mm); - struct mm_region* mapped; - struct mmap_param param = { .vms_mnt = VMS_MOUNT_1, - .pvms = &pcb->mm, - .mlen = USTACK_SIZE, - .proct = PROT_READ | PROT_WRITE, - .flags = MAP_ANON | MAP_PRIVATE | MAP_FIXED, - .type = REGION_TYPE_STACK }; + struct thread* kthread = create_thread(kproc, with_ustack); - int status = 0; - if ((status = mem_map(NULL, &mapped, USTACK_END, NULL, ¶m))) { - kprint_panic("fail to alloc user stack: %d", status); + if (!kthread) { + procvm_unmount(mm); + delete_process(kproc); + return -1; } - mapped->region_copied = __stack_copied; - mm_index((void**)&pcb->mm.stack, mapped); - - // TODO other uspace initialization stuff + commit_process(kproc); + start_thread(kthread, entry); - vmm_unmount_pd(VMS_MOUNT_1); -} + procvm_unmount(mm); -void -__mark_region(uintptr_t start_vpn, uintptr_t end_vpn, int attr) -{ - for (size_t i = start_vpn; i <= end_vpn; i++) { - x86_pte_t* curproc = &PTE_MOUNTED(VMS_SELF, i); - x86_pte_t* newproc = &PTE_MOUNTED(VMS_MOUNT_1, i); - cpu_invplg(newproc); - - if ((attr & REGION_MODE_MASK) == REGION_RSHARED) { - // 如果读共享,则将两者的都标注为只读,那么任何写入都将会应用COW策略。 - cpu_invplg(curproc); - cpu_invplg(i << 12); - *curproc = *curproc & ~PG_WRITE; - *newproc = *newproc & ~PG_WRITE; - } else { - // 如果是私有页,则将该页从新进程中移除。 - *newproc = 0; - } + if (created) { + *created = kthread; } -} -void -__copy_fdtable(struct proc_info* pcb) -{ - for (size_t i = 0; i < VFS_MAX_FD; i++) { - struct v_fd* fd = __current->fdtable->fds[i]; - if (!fd) - continue; - vfs_dup_fd(fd, &pcb->fdtable->fds[i]); - } + return 0; } -pid_t -dup_proc() +int +spawn_process_usr(struct thread** created, char* path, + const char** argv, const char** envp) { - struct proc_info* pcb = alloc_process(); - pcb->intr_ctx = __current->intr_ctx; - pcb->parent = __current; - - memcpy(pcb->fxstate, __current->fxstate, 512); + // FIXME remote injection of user stack not yet implemented - if (__current->cwd) { - pcb->cwd = __current->cwd; - vfs_ref_dnode(pcb->cwd); - } + struct proc_info* proc = alloc_process(); + struct proc_mm* mm = vmspace(proc); + + assert(!kernel_process(proc)); - __copy_fdtable(pcb); - region_copy(&__current->mm, &pcb->mm); + procvm_initvms_mount(mm); - setup_proc_mem(pcb, VMS_SELF); - - // 根据 mm_region 进一步配置页表 - - struct mm_region *pos, *n; - llist_for_each(pos, n, &pcb->mm.regions, head) - { - // 如果写共享,则不作处理。 - if ((pos->attr & REGION_WSHARED)) { - continue; - } + int errno = 0; + struct thread* main_thread; + if (!(main_thread = create_thread(proc, true))) { + errno = ENOMEM; + goto fail; + } - uintptr_t start_vpn = pos->start >> 12; - uintptr_t end_vpn = pos->end >> 12; - __mark_region(start_vpn, end_vpn, pos->attr); + struct exec_host container; + exec_init_container(&container, main_thread, VMS_MOUNT_1, argv, envp); + if ((errno = exec_load_byname(&container, path))) { + goto fail; } - vmm_unmount_pd(VMS_MOUNT_1); + commit_process(proc); + start_thread(main_thread, container.exe.entry); - // 正如同fork,返回两次。 - store_retval_to(pcb, 0); + if (created) { + *created = main_thread; + } - commit_process(pcb); + procvm_unmount(mm); + return 0; - return pcb->pid; +fail: + procvm_unmount(mm); + delete_process(proc); + return errno; } -extern void __kernel_end; - -void -setup_proc_mem(struct proc_info* proc, uintptr_t usedMnt) -{ - // copy the entire kernel page table - pid_t pid = proc->pid; - void* pt_copy = __dup_pagetable(pid, usedMnt); - - vmm_mount_pd(VMS_MOUNT_1, pt_copy); // 将新进程的页表挂载到挂载点#2 - - // copy the kernel stack - for (size_t i = KSTACK_START >> 12; i <= KSTACK_TOP >> 12; i++) { - volatile x86_pte_t* ppte = &PTE_MOUNTED(VMS_MOUNT_1, i); - - /* - This is a fucking nightmare, the TLB caching keep the rewrite to PTE - from updating. Even the Nightmare Moon the Evil is far less nasty - than this. It took me hours of debugging to figure this out. - - In the name of Celestia our glorious goddess, I will fucking HATE - the TLB for the rest of my LIFE! - */ - cpu_invplg(ppte); - - x86_pte_t p = *ppte; - void* ppa = vmm_dup_page(pid, PG_ENTRY_ADDR(p)); - pmm_free_page(pid, PG_ENTRY_ADDR(p)); - *ppte = (p & 0xfff) | (uintptr_t)ppa; - } - - // 我们不需要分配内核的区域,因为所有的内核代码和数据段只能通过系统调用来访问,任何非法的访问 - // 都会导致eip落在区域外面,从而segmentation fault. - // 至于其他的区域我们暂时没有办法知道,因为那需要知道用户程序的信息。我们留到之后在处理。 - proc->page_table = pt_copy; +ptr_t proc_vmroot() { + return __current->mm->vmroot; } \ No newline at end of file