From c46767abb634d600dc5cb1ef5922ec3459ae4cf8 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 17 Sep 2025 18:04:28 -0400 Subject: kern/amd64: proc: Add proc_kill() routine Add a new function to kill processes and clean them up. They will then be marked as EXITING and it is up to the parent to do the rest. Signed-off-by: Ian Moffett --- src/sys/arch/amd64/os/os_proc.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/sys/include/sys/proc.h | 28 ++++++++++++++++++++++++++++ src/sys/os/os_proc.c | 14 ++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/src/sys/arch/amd64/os/os_proc.c b/src/sys/arch/amd64/os/os_proc.c index 8c7b6f3..1064c10 100644 --- a/src/sys/arch/amd64/os/os_proc.c +++ b/src/sys/arch/amd64/os/os_proc.c @@ -39,6 +39,8 @@ #include #include +extern struct proc g_rootproc; + /* * Put the current process into userland for its first * run. The catch is that we have never been in userland @@ -212,3 +214,41 @@ done: lapic_eoi(); lapic_timer_oneshot_us(SCHED_QUANTUM); } + +/* + * Commit procedural murder + */ +int +md_proc_kill(struct proc *procp, int flags) +{ + struct proc *self; + struct pcore *core = this_core(); + struct md_pcb *pcbp; + + if (core == NULL) { + return -ENXIO; + } + + /* Default to ourself */ + if (procp == NULL) { + procp = core->curproc; + } + + /* Release the VAS */ + pcbp = &procp->pcb; + mmu_free_vas(&pcbp->vas); + + /* Sanity check */ + if ((self = core->curproc) == NULL) { + printf("kill: could not get self, using rootproc\n"); + self = &g_rootproc; + } + + /* If this is us, spin time */ + if (self->pid == procp->pid) { + core->curproc = NULL; + md_proc_yield(); + } + + return 0; +} diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h index 52afada..15b382d 100644 --- a/src/sys/include/sys/proc.h +++ b/src/sys/include/sys/proc.h @@ -32,6 +32,7 @@ #include #include +#include #include #include /* standard */ @@ -46,15 +47,19 @@ * on the system. * * @pid: Process ID + * @flags: State flags (see PROC_*) * @pcb: Process control block * @link: TAILQ link */ struct proc { pid_t pid; + uint32_t flags; struct md_pcb pcb; TAILQ_ENTRY(proc) link; }; +#define PROC_EXITING BIT(0) + /* * Initialize a process into a basic minimal * state @@ -67,6 +72,17 @@ struct proc { */ int proc_init(struct proc *procp, int flags); +/* + * Kill a process with a specific status code + * + * @procp: Process to kill + * @status: Status code to send + * + * Returns zero on success, otherwise a less than + * zero value is returned on failure. + */ +int proc_kill(struct proc *procp, int status); + /* * Initialize machine dependent state of a process * @@ -78,6 +94,18 @@ int proc_init(struct proc *procp, int flags); */ int md_proc_init(struct proc *procp, int flags); +/* + * Machine dependent kill routine which cleans up + * things that exist within the process control block + * + * @procp: Process to kill + * @flags: Optional flags to use + * + * Returns zero on success, otherwise less than zero + * value on failure. + */ +int md_proc_kill(struct proc *procp, int flags); + /* * Set the instruction pointer field of a specific * process diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c index 5348eaf..6880dda 100644 --- a/src/sys/os/os_proc.c +++ b/src/sys/os/os_proc.c @@ -52,3 +52,17 @@ proc_init(struct proc *procp, int flags) error = md_proc_init(procp, flags); return error; } + +/* + * Kill a specific process + */ +int +proc_kill(struct proc *procp, int status) +{ + if (procp == NULL) { + return -EINVAL; + } + + procp->flags |= PROC_EXITING; + return md_proc_kill(procp, 0); +} -- cgit v1.2.3