Skip to content

Commit

Permalink
Pack arguments of proc_init()
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Jul 3, 2024
1 parent 1c7d4d0 commit 000bd7f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
11 changes: 9 additions & 2 deletions include/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions src/arch/riscv/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 7 additions & 1 deletion src/kmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 11 additions & 13 deletions src/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#include <mm/page_alloc.h>

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];

Expand Down Expand Up @@ -64,32 +63,31 @@ 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)
return -ENOMEM;

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);

Expand All @@ -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;
}

Expand Down
9 changes: 7 additions & 2 deletions src/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/syscalls/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 000bd7f

Please sign in to comment.