From 3b618741d5088062cc47f6b9894c039ac559a175 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 26 Apr 2024 22:36:40 -0400 Subject: kernel: sched: Expose sched_init_stack() Signed-off-by: Ian Moffett --- sys/include/sys/sched.h | 1 + sys/kern/kern_sched.c | 114 ++++++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/sys/include/sys/sched.h b/sys/include/sys/sched.h index 8245120..01a58fe 100644 --- a/sys/include/sys/sched.h +++ b/sys/include/sys/sched.h @@ -43,6 +43,7 @@ void sched_init(void); void sched_exit(void); void sched_context_switch(struct trapframe *tf); void sched_rest(void); +uintptr_t sched_init_stack(void *stack_top, struct exec_args args); __noreturn uint64_t sys_exit(struct syscall_args *args); diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index c362b98..669d255 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -138,63 +138,6 @@ sched_enter(void) __builtin_unreachable(); } -static uintptr_t -sched_init_stack(void *stack_top, struct exec_args args) -{ - uintptr_t *sp = stack_top; - uintptr_t old_sp = 0; - size_t argc, envc, len; - char **argvp = args.argp; - char **envp = args.envp; - struct auxval auxv = args.auxv; - - /* Copy strings */ - old_sp = (uintptr_t)sp; - for (argc = 0; argvp[argc] != NULL; ++argc) { - len = strlen(argvp[argc]) + 1; - sp = (void *)((char *)sp - len); - memcpy((char *)sp, argvp[argc], len); - } - for (envc = 0; envp[envc] != NULL; ++envc) { - len = strlen(envp[envc]) + 1; - sp = (void *)((char *)sp - len); - memcpy((char *)sp, envp[envc], len); - } - - /* Ensure the stack is aligned */ - sp = (void *)__ALIGN_DOWN((uintptr_t)sp, 16); - if (((argc + envc + 1) & 1) != 0) - --sp; - - AUXVAL(sp, AT_NULL, 0x0); - AUXVAL(sp, AT_SECURE, 0x0); - AUXVAL(sp, AT_ENTRY, auxv.at_entry); - AUXVAL(sp, AT_PHDR, auxv.at_phdr); - AUXVAL(sp, AT_PHNUM, auxv.at_phnum); - AUXVAL(sp, AT_PAGESIZE, vm_get_page_size()); - STACK_PUSH(sp, 0); - - /* Copy envp pointers */ - sp -= envc; - for (int i = 0; i < envc; ++i) { - len = strlen(envp[i]) + 1; - old_sp -= len; - sp[i] = KERN_TO_USER(old_sp); - } - - /* Copy argvp pointers */ - STACK_PUSH(sp, 0); - sp -= argc; - for (int i = 0; i < argc; ++i) { - len = strlen(argvp[i]) + 1; - old_sp -= len; - sp[i] = KERN_TO_USER(old_sp); - } - - STACK_PUSH(sp, argc); - return (uintptr_t)sp; -} - static uintptr_t sched_create_stack(struct vas vas, bool user, struct exec_args args, struct proc *td) @@ -345,6 +288,63 @@ sched_make_idletd(void) sched_enqueue_td(td); } +uintptr_t +sched_init_stack(void *stack_top, struct exec_args args) +{ + uintptr_t *sp = stack_top; + uintptr_t old_sp = 0; + size_t argc, envc, len; + char **argvp = args.argp; + char **envp = args.envp; + struct auxval auxv = args.auxv; + + /* Copy strings */ + old_sp = (uintptr_t)sp; + for (argc = 0; argvp[argc] != NULL; ++argc) { + len = strlen(argvp[argc]) + 1; + sp = (void *)((char *)sp - len); + memcpy((char *)sp, argvp[argc], len); + } + for (envc = 0; envp[envc] != NULL; ++envc) { + len = strlen(envp[envc]) + 1; + sp = (void *)((char *)sp - len); + memcpy((char *)sp, envp[envc], len); + } + + /* Ensure the stack is aligned */ + sp = (void *)__ALIGN_DOWN((uintptr_t)sp, 16); + if (((argc + envc + 1) & 1) != 0) + --sp; + + AUXVAL(sp, AT_NULL, 0x0); + AUXVAL(sp, AT_SECURE, 0x0); + AUXVAL(sp, AT_ENTRY, auxv.at_entry); + AUXVAL(sp, AT_PHDR, auxv.at_phdr); + AUXVAL(sp, AT_PHNUM, auxv.at_phnum); + AUXVAL(sp, AT_PAGESIZE, vm_get_page_size()); + STACK_PUSH(sp, 0); + + /* Copy envp pointers */ + sp -= envc; + for (int i = 0; i < envc; ++i) { + len = strlen(envp[i]) + 1; + old_sp -= len; + sp[i] = KERN_TO_USER(old_sp); + } + + /* Copy argvp pointers */ + STACK_PUSH(sp, 0); + sp -= argc; + for (int i = 0; i < argc; ++i) { + len = strlen(argvp[i]) + 1; + old_sp -= len; + sp[i] = KERN_TO_USER(old_sp); + } + + STACK_PUSH(sp, argc); + return (uintptr_t)sp; +} + /* * Cause an early preemption and lets * the next thread run. -- cgit v1.2.3