feat: shell and signal demo as loadable user executable
[lunaix-os.git] / lunaix-os / kernel / process / process.c
index 1647e10dd8bc24765a4d9bc50283c3bc66416f50..23ff6a4dcc3e9a24ae115ebb532f4bed10de17bf 100644 (file)
@@ -1,6 +1,7 @@
 #include <klibc/string.h>
 #include <lunaix/clock.h>
 #include <lunaix/common.h>
+#include <lunaix/mm/mmap.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/region.h>
 #include <lunaix/mm/valloc.h>
@@ -134,6 +135,12 @@ __DEFINE_LXSYSCALL2(int, setpgid, pid_t, pid, pid_t, pgid)
     return 0;
 }
 
+void
+__stack_copied(struct mm_region* region)
+{
+    mm_index((void**)&region->proc_vms->stack, region);
+}
+
 void
 init_proc_user_space(struct proc_info* pcb)
 {
@@ -141,18 +148,22 @@ init_proc_user_space(struct proc_info* pcb)
 
     /*---  分配用户栈  ---*/
 
-    struct mm_region* stack_vm;
-
-    stack_vm = region_create_range(
-      USTACK_END, USTACK_SIZE, REGION_RW | REGION_RSHARED | REGION_ANON);
-    // 注册用户栈区域
-    region_add(&pcb->mm.regions, stack_vm);
-
-    // 预留地址空间,具体物理页将由Page Fault Handler按需分配。
-    for (uintptr_t i = PG_ALIGN(USTACK_END); i < USTACK_TOP; i += PG_SIZE) {
-        vmm_set_mapping(VMS_MOUNT_1, i, 0, PG_ALLOW_USER | PG_WRITE, VMAP_NULL);
+    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 };
+
+    int status = 0;
+    if ((status = mem_map(NULL, &mapped, USTACK_END, NULL, &param))) {
+        kprint_panic("fail to alloc user stack: %d", status);
     }
 
+    mapped->region_copied = __stack_copied;
+    mm_index((void**)&pcb->mm.stack, mapped);
+
     // TODO other uspace initialization stuff
 
     vmm_unmount_pd(VMS_MOUNT_1);
@@ -194,7 +205,6 @@ pid_t
 dup_proc()
 {
     struct proc_info* pcb = alloc_process();
-    pcb->mm.u_heap = __current->mm.u_heap;
     pcb->intr_ctx = __current->intr_ctx;
     pcb->parent = __current;
 
@@ -206,7 +216,7 @@ dup_proc()
     }
 
     __copy_fdtable(pcb);
-    region_copy(&__current->mm.regions, &pcb->mm.regions);
+    region_copy(&__current->mm, &pcb->mm);
 
     setup_proc_mem(pcb, VMS_SELF);