From 8dd1f460055d2180e5e31c8aac7523b6bdc63016 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 16 May 2025 09:35:49 -0400 Subject: kernel: spawn: Add proper spawn impl + SYS_spawn This commit introduces a more complete spawn implementation as well as the SYS_spawn syscall and a libc interface. Signed-off-by: Ian Moffett --- sys/arch/amd64/isa/i8042.c | 2 +- sys/include/sys/proc.h | 2 + sys/include/sys/spawn.h | 38 ++++++++++++++ sys/include/sys/syscall.h | 1 + sys/kern/init_main.c | 2 +- sys/kern/kern_spawn.c | 126 +++++++++++++++++++++++++++++++++++++++++++-- sys/kern/kern_syscall.c | 1 + 7 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 sys/include/sys/spawn.h (limited to 'sys') diff --git a/sys/arch/amd64/isa/i8042.c b/sys/arch/amd64/isa/i8042.c index 46a8d19..beacc87 100644 --- a/sys/arch/amd64/isa/i8042.c +++ b/sys/arch/amd64/isa/i8042.c @@ -436,7 +436,7 @@ i8042_init(void) quirks |= I8042_HOSTILE; pr_trace("lenovo device, assuming hostile\n"); pr_trace("disabling irq 1, polling as fallback\n"); - spawn(&polltd, 0, i8042_sync_loop, NULL); + spawn(&polltd, i8042_sync_loop, NULL, 0, NULL); } if (!ISSET(quirks, I8042_HOSTILE)) { diff --git a/sys/include/sys/proc.h b/sys/include/sys/proc.h index 4024879..1b59de9 100644 --- a/sys/include/sys/proc.h +++ b/sys/include/sys/proc.h @@ -64,6 +64,7 @@ struct proc { struct trapframe tf; struct pcb pcb; struct proc *parent; + void *spawn_data; size_t priority; int exit_status; bool rested; @@ -84,6 +85,7 @@ struct proc { #define PROC_WAITED BIT(4) /* Being waited on by parent */ struct proc *this_td(void); +struct proc *get_child(struct proc *cur, pid_t pid); int md_spawn(struct proc *p, struct proc *parent, uintptr_t ip); scret_t sys_spawn(struct syscall_args *scargs); diff --git a/sys/include/sys/spawn.h b/sys/include/sys/spawn.h new file mode 100644 index 0000000..3828d5c --- /dev/null +++ b/sys/include/sys/spawn.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_SPAWN_H_ +#define _SYS_SPAWN_H_ + +#include + +#if !defined(_KERNEL) +pid_t spawn(const char *pathname, int flags); +#endif /* _KERNEL */ +#endif /* !_SYS_SPAWN_H_ */ diff --git a/sys/include/sys/syscall.h b/sys/include/sys/syscall.h index 2b98df7..16c4b17 100644 --- a/sys/include/sys/syscall.h +++ b/sys/include/sys/syscall.h @@ -46,6 +46,7 @@ #define SYS_stat 5 #define SYS_sysctl 6 #define SYS_write 7 +#define SYS_spawn 8 #if defined(_KERNEL) /* Syscall return value and arg type */ diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 911785a..670d68c 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -101,7 +101,7 @@ main(void) /* Startup pid 1 */ memset(&proc0, 0, sizeof(proc0.tf)); - spawn(&proc0, 0, start_init, NULL); + spawn(&proc0, start_init, NULL, 0, NULL); /* Load all drivers */ DRIVERS_INIT(); diff --git a/sys/kern/kern_spawn.c b/sys/kern/kern_spawn.c index 6adf726..2cddd84 100644 --- a/sys/kern/kern_spawn.c +++ b/sys/kern/kern_spawn.c @@ -28,11 +28,15 @@ */ #include +#include #include +#include #include #include +#include #include #include +#include #include #include #include @@ -42,16 +46,62 @@ static volatile size_t nthreads = 0; +struct spawn_args { + char path[PATH_MAX]; +}; + +static inline void +try_free_data(void *p) +{ + if (p != NULL) { + dynfree(p); + } +} + +static void +spawn_thunk(void) +{ + const char *path; + char pathbuf[PATH_MAX]; + struct proc *cur; + struct execve_args execve_args; + struct spawn_args *args; + char *argv[] = { NULL, NULL }; + char *envp[] = { NULL }; + + cur = this_td(); + args = cur->spawn_data; + path = args->path; + memcpy(pathbuf, path, strlen(path)); + + argv[0] = (char *)pathbuf; + execve_args.pathname = argv[0]; + execve_args.argv = argv; + execve_args.envp = envp; + + path = NULL; + dynfree(args); + execve(cur, &execve_args); + __builtin_unreachable(); +} + /* * Spawn a new process * * @cur: Parent (current) process. + * @func: Address of start code. + * @p: Data to pass to new process (used for user procs) * @flags: Spawn flags. - * @ip: Location for process to start * @newprocp: If not NULL, will contain the new process. + * + * Returns the PID of the child on success, otherwise an + * errno value that is less than zero. + * + * XXX: `p` is only used by sys_spawn and should be set + * to NULL if called in the kernel. */ -int -spawn(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) +pid_t +spawn(struct proc *cur, void(*func)(void), void *p, int flags, struct proc **newprocp) { struct proc *newproc; struct mmap_lgdr *mlgdr; @@ -60,21 +110,24 @@ spawn(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) newproc = dynalloc(sizeof(*newproc)); if (newproc == NULL) { pr_error("could not alloc proc (-ENOMEM)\n"); + try_free_data(p); return -ENOMEM; } mlgdr = dynalloc(sizeof(*mlgdr)); if (mlgdr == NULL) { dynfree(newproc); + try_free_data(p); pr_error("could not alloc proc mlgdr (-ENOMEM)\n"); return -ENOMEM; } memset(newproc, 0, sizeof(*newproc)); - error = md_spawn(newproc, cur, (uintptr_t)ip); + error = md_spawn(newproc, cur, (uintptr_t)func); if (error < 0) { dynfree(newproc); dynfree(mlgdr); + try_free_data(p); pr_error("error initializing proc\n"); return error; } @@ -84,6 +137,17 @@ spawn(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) *newprocp = newproc; } + if (!ISSET(cur->flags, PROC_LEAFQ)) { + TAILQ_INIT(&cur->leafq); + cur->flags |= PROC_LEAFQ; + } + + /* Add to parent leafq */ + TAILQ_INSERT_TAIL(&cur->leafq, newproc, leaf_link); + atomic_inc_int(&cur->nleaves); + newproc->parent = cur; + newproc->spawn_data = p; + /* Initialize the mmap ledger */ mlgdr->nbytes = 0; RBT_INIT(lgdr_entries, &mlgdr->hd); @@ -92,5 +156,57 @@ spawn(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) newproc->pid = ++nthreads; signals_init(newproc); sched_enqueue_td(newproc); - return 0; + return newproc->pid; +} + +/* + * Get the child of a process by PID. + * + * @cur: Parent process. + * @pid: Child PID. + * + * Returns NULL if no child was found. + */ +struct proc * +get_child(struct proc *cur, pid_t pid) +{ + struct proc *procp; + + TAILQ_FOREACH(procp, &cur->leafq, leaf_link) { + if (procp->pid == pid) { + return procp; + } + } + + return NULL; +} + +/* + * arg0: The file /path/to/executable + * arg1: Optional flags (`flags') + */ +scret_t +sys_spawn(struct syscall_args *scargs) +{ + struct spawn_args *args; + const char *u_path; + struct proc *td; + int flags, error; + + td = this_td(); + flags = scargs->arg1; + u_path = (const char *)scargs->arg0; + + args = dynalloc(sizeof(*args)); + if (args == NULL) { + return -ENOMEM; + } + + error = copyinstr(u_path, args->path, sizeof(args->path)); + if (error < 0) { + dynfree(args); + return error; + } + + return spawn(td, spawn_thunk, args, flags, NULL); } diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c index 986d82a..bc61fa6 100644 --- a/sys/kern/kern_syscall.c +++ b/sys/kern/kern_syscall.c @@ -42,6 +42,7 @@ scret_t(*g_sctab[])(struct syscall_args *) = { sys_stat, /* SYS_stat */ sys_sysctl, /* SYS_sysctl */ sys_write, /* SYS_write */ + sys_spawn, /* SYS_spawn */ }; const size_t MAX_SYSCALLS = NELEM(g_sctab); -- cgit v1.2.3