From 83a0de114a9701b8a658c620cd9ee52d232e0e66 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Mon, 25 Nov 2019 14:06:12 +0900 Subject: [PATCH] Use a static-size array to keep track of the tasks in a process for now --- config.h | 1 + kernel/core/process.c | 76 ++++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/config.h b/config.h index 6447353..d5d63e1 100644 --- a/config.h +++ b/config.h @@ -29,6 +29,7 @@ #define CONFIG_KERNEL_STACK_BASE ((void*)0xffffe000) #endif /* !__ASSEMBLY_SOURCE */ #define CONFIG_USER_STACK_SIZE 4096 +#define CONFIG_PROC_MAXTASKS 8 #define CONFIG_APIC 0 diff --git a/kernel/core/process.c b/kernel/core/process.c index 09b40d9..8a14321 100644 --- a/kernel/core/process.c +++ b/kernel/core/process.c @@ -19,7 +19,7 @@ enum procstate { struct process { struct pagedir *p_pgdir; - struct task *p_tasks; + struct task *p_tasks[CONFIG_PROC_MAXTASKS]; u32_t p_privl; pid_t p_id; pid_t p_pid; @@ -236,9 +236,11 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) { int ret_val; process_t *proc; + task_t *ctask; ret_val = -ENOMEM; proc = NULL; + ctask = task_get_current(); /* was a specific pid requested? */ if(pid != PID_ANY) { @@ -258,10 +260,7 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) if(entry == PROC_ENTRY_FORK || entry == PROC_ENTRY_VFORK) { - task_t *ctask; - /* inherit the ppl from the current task, in the case of fork and vfork */ - ctask = task_get_current(); ppl = ctask->t_privl; } @@ -287,9 +286,9 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) kstack = pg_dir_get_kstack(proc->p_pgdir); ustack = pg_dir_get_ustack(proc->p_pgdir); - proc->p_tasks = kmalloc(sizeof(*(proc->p_tasks))); + proc->p_tasks[0] = kmalloc(sizeof(*(proc->p_tasks[0]))); - if(!proc->p_tasks) { + if(!proc->p_tasks[0]) { ret_val = -ENOMEM; goto cleanup; } @@ -305,8 +304,8 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) proc->p_pid = cproc->p_id; /* also inherit the scheduling settings */ - proc->p_tasks->t_tslice = cproc->p_tasks->t_tslice; - proc->p_tasks->t_rslice = proc->p_tasks->t_tslice; + proc->p_tasks[0]->t_tslice = ctask->t_tslice; + proc->p_tasks[0]->t_rslice = ctask->t_tslice; } else { /* * This only happens when process_create() is called before the @@ -316,8 +315,8 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) proc->p_gid = 0; proc->p_pid = 0; - proc->p_tasks->t_tslice = 50; - proc->p_tasks->t_rslice = 50; + proc->p_tasks[0]->t_tslice = 50; + proc->p_tasks[0]->t_rslice = 50; } /* set the default signal handlers */ @@ -332,18 +331,14 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) proc->p_id = pid; } - proc->p_tasks->t_kstack = kstack; - proc->p_tasks->t_proc = proc; - proc->p_tasks->t_pid = proc->p_id; + proc->p_tasks[0]->t_kstack = kstack; + proc->p_tasks[0]->t_proc = proc; + proc->p_tasks[0]->t_pid = proc->p_id; if(entry == PROC_ENTRY_FORK || entry == PROC_ENTRY_VFORK) { - task_t *ctask; - process_t *cproc; void *custack; - ctask = task_get_current(); - cproc = (process_t*)ctask->t_proc; custack = pg_dir_get_ustack(cproc->p_pgdir); /* @@ -367,8 +362,8 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) * otherwise prepare it to start execution through task_switch. */ - proc->p_tasks->t_state = TASK_STATE_FORKED; - proc->p_tasks->t_pgdir = (u32_t)pdbr; + 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, @@ -384,7 +379,7 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) * to user-space asap. */ - if(task_prepare_fork(proc->p_tasks) == 0xf00f00ff) { + if(task_prepare_fork(proc->p_tasks[0]) == 0xf00f00ff) { /* this is the forked process */ proc = NULL; ret_val = 0; @@ -394,7 +389,7 @@ 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, (u32_t)pdbr, (u32_t)entry, + 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); } @@ -407,7 +402,7 @@ int process_create(process_t **dst, pid_t pid, u32_t ppl, void *entry) proc->p_state = PROC_STATE_READY; - if(sched_enqueue(proc->p_tasks) != 0) { + if(sched_enqueue(proc->p_tasks[0]) != 0) { PANIC("sched_enqueue() failed for a new task\n"); } } @@ -421,11 +416,11 @@ cleanup: proc->p_pgdir = NULL; } - if(proc->p_tasks) { + if(proc->p_tasks[0]) { /* cleanup task */ - kfree(proc->p_tasks); - proc->p_tasks = NULL; + kfree(proc->p_tasks[0]); + proc->p_tasks[0] = NULL; } kfree(proc); @@ -441,7 +436,7 @@ int process_switch(process_t *proc) { int ret_val; - ret_val = task_switch(proc->p_tasks); + ret_val = task_switch(proc->p_tasks[0]); return(ret_val); } @@ -654,7 +649,7 @@ int process_signal(pid_t pid) void* process_get_tasks(process_t *proc) { - return(proc->p_tasks); + return(proc->p_tasks[0]); } pid_t process_get_id(process_t *proc) @@ -675,6 +670,7 @@ int process_exit(process_t *proc, int status) task_t *cur; task_t *t; int reschedule; + int tid; reschedule = 0; @@ -682,7 +678,8 @@ int process_exit(process_t *proc, int status) /* FIXME: Properly deal with the process's remaining tasks */ - for(t = proc->p_tasks; t; t = NULL /* t = t->next */) { + for(tid = 0; tid < CONFIG_PROC_MAXTASKS; tid++) { + t = proc->p_tasks[tid]; t->t_state = TASK_STATE_EXIT; /* @@ -784,6 +781,7 @@ int process_signal_deliver(process_t *proc, int signal) u32_t *vesp3; u32_t *pesp3; task_t *task; + int i; /* FIXME: Need to suspend the process in order to deliver the signal safely */ process_suspend(proc); @@ -793,7 +791,19 @@ int process_signal_deliver(process_t *proc, int signal) * implementation doesn't support more than one task per process, and we * can just pick the first (and only) one. */ - task = proc->p_tasks; + for(task = NULL, i = 0; i < CONFIG_PROC_MAXTASKS; i++) { + if(proc->p_tasks[i]) { + task = proc->p_tasks[i]; + break; + } + } + + if(!task) { + /* this should actually never happen */ + ret_val = -EBADFD; + goto cleanup; + } + kstk = (stack_frame_t*)task->t_sp; /* @@ -975,11 +985,17 @@ int process_set_pagedir(process_t *proc, pg_dir_t *pd) ret_val = -EINVAL; if(proc && pd) { + int i; + /* FIXME: make sure none of the tasks are running */ proc->p_pgdir = pd; /* FIXME: Set t_pgdir in all tasks */ - proc->p_tasks->t_pgdir = (u32_t)pg_dir_get_pdbr(pd); + for(i = 0; i < CONFIG_PROC_MAXTASKS; i++) { + if(proc->p_tasks[i]) { + proc->p_tasks[i]->t_pgdir = (u32_t)pg_dir_get_pdbr(pd); + } + } /* * TODO: The old page directory may still be referenced -- 2.47.3