diff --git a/include/proc.h b/include/proc.h index f3226bb..5498550 100644 --- a/include/proc.h +++ b/include/proc.h @@ -42,10 +42,17 @@ typedef struct proc { usize *sp; } proc_t; +typedef struct { + void *entry; + u32 flags; + proc_t *parent; + i32 argc; + char **argv; +} proc_config_t; + extern proc_t *proc_table[]; -i32 proc_init(proc_t *proc, void *entry, u32 flags, proc_t *parent, int argc, - char *argv[]); +i32 proc_init(proc_t *proc, const proc_config_t *config); void proc_deinit(proc_t *proc); void proc_ctx_sw(proc_t *old_proc, proc_t *new_proc); void proc_adopt(proc_t *new_parent, proc_t *proc); diff --git a/src/arch/riscv/proc.c b/src/arch/riscv/proc.c index 92aada4..c48e3ad 100644 --- a/src/arch/riscv/proc.c +++ b/src/arch/riscv/proc.c @@ -4,14 +4,13 @@ void proc_entry(void); -void proc_fill_stack(proc_t *proc, void *entry, usize *usp, int argc, - char *argv[]) { +void proc_fill_stack(proc_t *proc, const proc_config_t *config, usize *usp) { proc->sp -= 22; proc->sp[21] = (usize) proc; - proc->sp[20] = (usize) entry; + proc->sp[20] = (usize) config->entry; proc->sp[19] = (usize) usp; - proc->sp[18] = argc; - proc->sp[17] = (usize) argv; + proc->sp[18] = config->argc; + proc->sp[17] = (usize) config->argv; proc->sp[15] = (usize) proc_entry; } diff --git a/src/kmain.c b/src/kmain.c index 64f5cc7..556a4a5 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -8,7 +8,13 @@ static proc_t init_proc; [[noreturn]] void kmain(void) { init_modules(); - proc_init(&init_proc, init, 0, nullptr, 0, nullptr); + proc_config_t proc_config; + proc_config.entry = init; + proc_config.flags = 0; + proc_config.parent = nullptr; + proc_config.argc = 0; + proc_config.argv = nullptr; + proc_init(&init_proc, &proc_config); sched_update_state(&init_proc, PROC_STATE_READY); sched_start(); diff --git a/src/proc.c b/src/proc.c index d5106a3..28a66ad 100644 --- a/src/proc.c +++ b/src/proc.c @@ -6,8 +6,7 @@ #include void ctx_sw(usize **old_sp, usize **new_sp); -void proc_fill_stack(const proc_t *proc, void *entry, usize *usp, int argc, - char *argv[]); +void proc_fill_stack(proc_t *proc, const proc_config_t *config, usize *usp); proc_t *proc_table[PROC_TABLE_SIZE]; @@ -64,7 +63,7 @@ static i32 init_fds(proc_t *proc) { return 0; } -static i32 init_stacks(proc_t *proc, void *entry, int argc, char *argv[]) { +static i32 init_stacks(proc_t *proc, const proc_config_t *config) { proc->kern_stack = (usize *) page_alloc(); proc->user_stack = (usize *) page_alloc(); if (!proc->kern_stack || !proc->user_stack) @@ -72,24 +71,23 @@ static i32 init_stacks(proc_t *proc, void *entry, int argc, char *argv[]) { usize *usp = (usize *) ((usize) proc->user_stack + PAGE_SIZE); *--usp = 0; - for (i32 i = 0; i < argc; i++) { - *--usp = (usize) argv[argc - 1 - i]; + for (i32 i = 0; i < config->argc; i++) { + *--usp = (usize) config->argv[config->argc - 1 - i]; } proc->sp = (usize *) ((usize) proc->kern_stack + PAGE_SIZE); - proc_fill_stack(proc, entry, usp, argc, argv); + proc_fill_stack(proc, config, usp); return 0; } -i32 proc_init(proc_t *proc, void *entry, u32 flags, proc_t *parent, i32 argc, - char *argv[]) { +i32 proc_init(proc_t *proc, const proc_config_t *config) { proc->pid = new_pid(); if (proc->pid < 0) return proc->pid; - proc->flags = flags; + proc->flags = config->flags; - proc->parent = parent; + proc->parent = config->parent; list_init_head(&proc->children); list_init_head(&proc->zombie_children); @@ -100,13 +98,13 @@ i32 proc_init(proc_t *proc, void *entry, u32 flags, proc_t *parent, i32 argc, proc->state = PROC_STATE_INIT; timer_init(&proc->timer); - res = init_stacks(proc, entry, argc, argv); + res = init_stacks(proc, config); if (res < 0) return res; proc_table[proc->pid] = proc; - if (parent) - list_push_back(&parent->children, &proc->tree_node); + if (config->parent) + list_push_back(&config->parent->children, &proc->tree_node); return 0; } diff --git a/src/sched.c b/src/sched.c index 7a20dd4..0ccd480 100644 --- a/src/sched.c +++ b/src/sched.c @@ -20,8 +20,13 @@ static void idle_task() { } static i32 init(void) { - i32 res = - proc_init(&idle_proc, idle_task, PROC_FLAG_KERN, nullptr, 0, nullptr); + proc_config_t proc_config; + proc_config.entry = idle_task; + proc_config.flags = PROC_FLAG_KERN; + proc_config.parent = nullptr; + proc_config.argc = 0; + proc_config.argv = nullptr; + i32 res = proc_init(&idle_proc, &proc_config); if (res < 0) return res; sched_update_state(&idle_proc, PROC_STATE_READY); diff --git a/src/syscalls/proc.c b/src/syscalls/proc.c index 37e11d5..43729e5 100644 --- a/src/syscalls/proc.c +++ b/src/syscalls/proc.c @@ -18,7 +18,13 @@ isize sys_proc(const trapframe_t *frame) { proc_t *proc = (proc_t *) kmalloc(sizeof(proc_t)); if (!proc) return -ENOMEM; - i32 res = proc_init(proc, entry, 0, curr_proc, argc, argv); + proc_config_t proc_config; + proc_config.entry = entry; + proc_config.flags = 0; + proc_config.parent = curr_proc; + proc_config.argc = argc; + proc_config.argv = argv; + i32 res = proc_init(proc, &proc_config); if (res < 0) return res;