]> git.corax.cc Git - corax/commitdiff
kernel/core: Remove pid argument from process_create()
authorMatthias Kruk <m@m10k.eu>
Wed, 6 May 2020 06:42:22 +0000 (15:42 +0900)
committerMatthias Kruk <m@m10k.eu>
Wed, 6 May 2020 06:42:22 +0000 (15:42 +0900)
kernel/core/main.c
kernel/core/process.c
kernel/include/process.h

index 4f94974186d9a71d4b23d1bf0b1d1b9de4f2da38..2b59083b233e3dced2e80c2b48e74c4a3b82292e 100644 (file)
@@ -245,13 +245,13 @@ int corax(void *mb_info, u32_t magic)
                           initfs.ifs_entries[err].ife_limit);
        }
 
-       err = process_create(&proc, PID_ANY, 0, (void*)_idle);
+       err = process_create(&proc, 0, (void*)_idle);
 
        if(err < 0) {
                PANIC("Could not spawn idle process\n");
        }
 
-       err = process_create(&proc, PID_ANY, 3, (void*)_init);
+       err = process_create(&proc, 3, (void*)_init);
 
        if(err < 0) {
                PANIC("Could not spawn init process\n");
index c22623bb4b7088a82fb14f56ba9d6374bd9cfcc6..5afdd8e63fcf669902eef714e4dee74042d13dcb 100644 (file)
@@ -258,7 +258,7 @@ skip:
        return(ret_val);
 }
 
-int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry)
+int process_create(process_t **dst, u32_t ppl, void *entry)
 {
        int ret_val;
        process_t *proc;
@@ -268,25 +268,8 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry)
        proc = NULL;
        ctask = task_get_current();
 
-       /* was a specific pid requested? */
-       if(pid != PID_ANY) {
-               /* may only request pids in [2..100] */
-
-               if(pid < 2 || pid >= 100) {
-                       ret_val = -EINVAL;
-                       goto cleanup;
-               }
-
-               /* check if pid is already taken */
-               if(process_lookup(pid)) {
-                       ret_val = -EALREADY;
-                       goto cleanup;
-               }
-       }
-
-       if(entry == PROC_ENTRY_FORK ||
-          entry == PROC_ENTRY_VFORK) {
-               /* inherit the ppl from the current task, in the case of fork and vfork */
+       if(entry == PROC_ENTRY_FORK || entry == PROC_ENTRY_VFORK) {
+               /* (v)forked processes inherit the parent's privilege level */
                ppl = ctask->t_privl;
        }
 
@@ -304,10 +287,7 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry)
                        goto cleanup;
                }
 
-               /*
-                * The new page directory contains only the kernel regions.
-                */
-
+               /* the new page directory contains only the kernel regions */
                pdbr = pg_dir_get_pdbr(proc->p_pgdir);
                kstack = pg_dir_get_kstack(proc->p_pgdir);
                ustack = pg_dir_get_ustack(proc->p_pgdir);
@@ -334,8 +314,8 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry)
                        proc->p_tasks[0]->t_rslice = ctask->t_tslice;
                } else {
                        /*
-                        * This only happens when process_create() is called before the
-                        * scheduler is invoked for the first time.
+                        * This only happens when process_create() is called
+                        * before the scheduler is invoked for the first time.
                         */
                        proc->p_uid = 0;
                        proc->p_gid = 0;
@@ -346,17 +326,12 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry)
                }
 
                /* set the default signal handlers */
-               memcpy(proc->p_sighandler, _sig_dfl, sizeof(proc->p_sighandler));
+               memcpy(proc->p_sighandler, _sig_dfl,
+                      sizeof(proc->p_sighandler));
 
                proc->p_privl = ppl;
                proc->p_sigpending = -1;
-
-               if(pid == PID_ANY) {
-                       /* no specific pid requested */
-                       proc->p_id = _next_pid++;
-               } else {
-                       proc->p_id = pid;
-               }
+               proc->p_id = _next_pid++;
 
                proc->p_tasks[0]->t_kstack = kstack;
                proc->p_tasks[0]->t_proc = proc;
@@ -369,41 +344,49 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry)
                        custack = pg_dir_get_ustack(cproc->p_pgdir);
 
                        /*
-                        * The address space of the new process doesn't contain any memory segments
-                        * but those relevant for the kernel. If the process is a fork, clone all non-kernel
-                        * memory segments that are present in the parent's page directory. If it is a vfork,
-                        * directly map the segments into this address space.
+                        * The address space of the new process doesn't contain
+                        * any memory segments except for those relevant to the
+                        * kernel. If the new process is a fork, clone all
+                        * non-kernel memory segments that are present in the
+                        * parent's page directory. If it is a vfork, directly
+                        * map the segments into this address space.
                         */
                        pg_dir_foreach_region(cproc->p_pgdir,
-                                                                 entry == PROC_ENTRY_FORK ? _fork_region : _vfork_region,
-                                                                 proc->p_pgdir);
+                                             entry == PROC_ENTRY_FORK ?
+                                             _fork_region : _vfork_region,
+                                             proc->p_pgdir);
 
                        /*
-                        * In the case of fork() and vfork() we have to get the address that the
-                        * interrupt is supposed to return to. The stack that the address was pushed
-                        * onto is located at ctask->t_kstack. Instead of pulling the address from
-                        * where we believe it should be on the stack, we'll just copy the entire stack
-                        * and make the child process return to userspace the same way we do.
-                        * The task_prepare_fork function will actually do all of this work: it will
-                        * adjust the stack pointers for the new task, copy the kernel stack, and
-                        * otherwise prepare it to start execution through task_switch.
+                        * In the case of fork() and vfork() we have to get the
+                        * address that the interrupt is supposed to return to.
+                        * The stack that the address was pushed onto is located
+                        * at ctask->t_kstack. Instead of pulling the address
+                        * from where we believe it should be on the stack,
+                        * we'll just copy the entire stack and make the child
+                        * process return to userspace the same way we do.
+                        * The task_prepare_fork function will actually do all
+                        * of this work: it will adjust the stack pointers for
+                        * the new task, copy the kernel stack, and otherwise
+                        * prepare it to start execution through task_switch.
                         */
 
                        proc->p_tasks[0]->t_state = TASK_STATE_FORKED;
                        proc->p_tasks[0]->t_pgdir = (u32_t)pdbr;
 
-                       process_memcpy_ptop(proc, ustack,
-                                                               cproc, custack,
-                                                               CONFIG_USER_STACK_SIZE);
+                       process_memcpy_ptop(proc, ustack, cproc, custack,
+                                           CONFIG_USER_STACK_SIZE);
 
                        /*
-                        * The child process will start with the instruction that is following the
-                        * task_prepare_fork call. In the case of the parent, task_prepare_fork will
-                        * return zero, in case of the child, the return value will be 0xf00f00ff, but
-                        * the function that the child will be returning from is actually task_switch.
-                        * Since any registers besides esp, ebp, and eax will be in an undefined state
-                        * for the child process, we should not touch anything else and just return
-                        * to user-space asap.
+                        * The child process will start with the instruction
+                        * that is following the task_prepare_fork call. In the
+                        * case of the parent, task_prepare_fork will return
+                        * zero, in case of the child, the return value will be
+                        * 0xf00f00ff, but the function that the child will be
+                        * returning from is actually task_switch. Since any
+                        * registers except for esp, ebp, and eax will be in an
+                        * undefined state for the child process, we should not
+                        * touch anything else and just return to user-space
+                        * as soon as possible.
                         */
 
                        if(task_prepare_fork(proc->p_tasks[0]) == 0xf00f00ff) {
@@ -416,9 +399,11 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry)
                        /* the parent continues as usual */
 
                } else {
-                       task_prepare(proc->p_tasks[0], (u32_t)pdbr, (u32_t)entry,
-                                                (u32_t)kstack + CONFIG_KERNEL_STACK_SIZE,
-                                                (u32_t)ustack + CONFIG_USER_STACK_SIZE, ppl);
+                       task_prepare(proc->p_tasks[0], (u32_t)pdbr,
+                                    (u32_t)entry,
+                                    (u32_t)kstack + CONFIG_KERNEL_STACK_SIZE,
+                                    (u32_t)ustack + CONFIG_USER_STACK_SIZE,
+                                    ppl);
                }
 
                ret_val = NQ(proc);
@@ -691,7 +676,8 @@ int process_fork(int v)
        if(proc) {
                process_t *nproc;
 
-               ret_val = process_create(&nproc, PID_ANY, proc->p_privl, PROC_ENTRY_FORK);
+               ret_val = process_create(&nproc, proc->p_privl,
+                                        PROC_ENTRY_FORK);
 
                if(!ret_val && nproc) {
                        /* this is the parent */
index e2fda64ba73f6362e01d7f839e90f894e9e54781..9198cb30fff1dd3901a78e2e8886233a54399ad4 100644 (file)
@@ -29,7 +29,7 @@
 
 typedef struct process process_t;
 
-int process_create(process_t**, pid_t, u32_t, void*);
+int process_create(process_t**, u32_t, void*);
 int process_switch(process_t*);
 
 void process_lock(process_t*);