2023-09-10 00:18:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2023 Ali Mashtizadeh
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
|
|
|
#include <sys/kassert.h>
|
|
|
|
#include <sys/kconfig.h>
|
|
|
|
#include <sys/kdebug.h>
|
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/ktime.h>
|
|
|
|
#include <sys/mp.h>
|
|
|
|
#include <sys/spinlock.h>
|
|
|
|
#include <sys/thread.h>
|
|
|
|
|
|
|
|
#include <machine/trap.h>
|
|
|
|
#include <machine/pmap.h>
|
|
|
|
|
|
|
|
extern Thread *curProc[MAX_CPUS];
|
|
|
|
|
|
|
|
// Process List
|
|
|
|
Spinlock procLock;
|
|
|
|
uint64_t nextProcessID;
|
|
|
|
ProcessQueue processList;
|
|
|
|
|
|
|
|
// Memory Pools
|
|
|
|
Slab processSlab;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process
|
|
|
|
*/
|
|
|
|
|
2023-09-09 22:15:41 +00:00
|
|
|
/**
|
|
|
|
* Process_Create --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Create a process.
|
2023-09-09 22:15:41 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* @param [in] parent Parent process.
|
|
|
|
* @param [in] title Process title.
|
|
|
|
*
|
|
|
|
* @return The newly created process.
|
2023-09-09 22:15:41 +00:00
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
Process *
|
|
|
|
Process_Create(Process *parent, const char *title)
|
|
|
|
{
|
|
|
|
Process *proc = (Process *)Slab_Alloc(&processSlab);
|
|
|
|
|
|
|
|
if (!proc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(proc, 0, sizeof(*proc));
|
|
|
|
|
|
|
|
proc->pid = nextProcessID++;
|
|
|
|
proc->threads = 0;
|
|
|
|
proc->refCount = 1;
|
2023-09-03 18:54:14 +00:00
|
|
|
proc->procState = PROC_STATE_NULL;
|
2023-08-21 22:38:51 +00:00
|
|
|
TAILQ_INIT(&proc->threadList);
|
|
|
|
|
|
|
|
if (title) {
|
|
|
|
strncpy((char *)&proc->title, title, PROCESS_TITLE_LENGTH);
|
|
|
|
} else {
|
|
|
|
proc->title[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
proc->space = PMap_NewAS();
|
|
|
|
if (proc->space == NULL) {
|
|
|
|
Slab_Free(&processSlab, proc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
proc->ustackNext = MEM_USERSPACE_STKBASE;
|
|
|
|
|
|
|
|
Spinlock_Init(&proc->lock, "Process Lock", SPINLOCK_TYPE_NORMAL);
|
|
|
|
|
|
|
|
Semaphore_Init(&proc->zombieSemaphore, 0, "Zombie Semaphore");
|
|
|
|
TAILQ_INIT(&proc->zombieQueue);
|
|
|
|
|
|
|
|
Handle_Init(proc);
|
|
|
|
|
|
|
|
proc->parent = parent;
|
|
|
|
if (parent) {
|
|
|
|
Spinlock_Lock(&parent->lock);
|
|
|
|
TAILQ_INSERT_TAIL(&parent->childrenList, proc, siblingList);
|
|
|
|
Spinlock_Unlock(&parent->lock);
|
|
|
|
}
|
|
|
|
TAILQ_INIT(&proc->childrenList);
|
|
|
|
TAILQ_INIT(&proc->zombieProc);
|
2023-09-02 21:51:10 +00:00
|
|
|
Mutex_Init(&proc->zombieProcLock, "Zombie Process Lock");
|
|
|
|
CV_Init(&proc->zombieProcCV, "Zombie Process CV");
|
2023-09-03 18:54:14 +00:00
|
|
|
CV_Init(&proc->zombieProcPCV, "Zombie Process PCV");
|
2023-08-21 22:38:51 +00:00
|
|
|
|
|
|
|
Spinlock_Lock(&procLock);
|
|
|
|
TAILQ_INSERT_TAIL(&processList, proc, processList);
|
|
|
|
Spinlock_Unlock(&procLock);
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2023-09-09 22:15:41 +00:00
|
|
|
/**
|
|
|
|
* Process_Destroy --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Destroy a process. This should not be called direct, but rather by
|
|
|
|
* Process_Release.
|
2023-09-09 22:15:41 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* @param [in] proc Process to destroy.
|
2023-09-09 22:15:41 +00:00
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
static void
|
|
|
|
Process_Destroy(Process *proc)
|
|
|
|
{
|
|
|
|
Handle_Destroy(proc);
|
|
|
|
|
|
|
|
Spinlock_Destroy(&proc->lock);
|
|
|
|
Semaphore_Destroy(&proc->zombieSemaphore);
|
2023-09-03 18:54:14 +00:00
|
|
|
CV_Destroy(&proc->zombieProcPCV);
|
2023-09-02 21:51:10 +00:00
|
|
|
CV_Destroy(&proc->zombieProcCV);
|
|
|
|
Mutex_Destroy(&proc->zombieProcLock);
|
2023-08-21 22:38:51 +00:00
|
|
|
PMap_DestroyAS(proc->space);
|
|
|
|
|
|
|
|
// XXX: We need to promote zombie processes to our parent
|
|
|
|
// XXX: Release the semaphore as well
|
|
|
|
|
|
|
|
Spinlock_Lock(&procLock);
|
|
|
|
TAILQ_REMOVE(&processList, proc, processList);
|
|
|
|
Spinlock_Unlock(&procLock);
|
|
|
|
|
|
|
|
Slab_Free(&processSlab, proc);
|
|
|
|
}
|
|
|
|
|
2023-09-09 22:15:41 +00:00
|
|
|
/**
|
2023-09-06 17:51:47 +00:00
|
|
|
* Process_Lookup --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Lookup a process by PID and increment its reference count.
|
|
|
|
*
|
|
|
|
* @param [in] pid Process ID to search for.
|
2023-09-09 22:15:41 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* @retval NULL No such process.
|
|
|
|
* @return Process that corresponds to the pid.
|
2023-09-06 17:51:47 +00:00
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
Process *
|
|
|
|
Process_Lookup(uint64_t pid)
|
|
|
|
{
|
|
|
|
Process *p;
|
|
|
|
Process *proc = NULL;
|
|
|
|
|
|
|
|
Spinlock_Lock(&procLock);
|
|
|
|
TAILQ_FOREACH(p, &processList, processList) {
|
|
|
|
if (p->pid == pid) {
|
|
|
|
Process_Retain(p);
|
|
|
|
proc = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Spinlock_Unlock(&procLock);
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2023-09-09 22:15:41 +00:00
|
|
|
/**
|
2023-09-06 17:51:47 +00:00
|
|
|
* Process_Retain --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Increment the reference count for a given process.
|
2023-09-09 22:15:41 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* @param proc Process to retain a reference to.
|
2023-09-06 17:51:47 +00:00
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
void
|
|
|
|
Process_Retain(Process *proc)
|
|
|
|
{
|
|
|
|
ASSERT(proc->refCount != 0);
|
|
|
|
__sync_fetch_and_add(&proc->refCount, 1);
|
|
|
|
}
|
|
|
|
|
2023-09-09 22:15:41 +00:00
|
|
|
/**
|
2023-09-06 17:51:47 +00:00
|
|
|
* Process_Release --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Decrement the reference count for a given process.
|
2023-09-09 22:15:41 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* @param proc Process to release a reference of.
|
2023-09-06 17:51:47 +00:00
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
void
|
|
|
|
Process_Release(Process *proc)
|
|
|
|
{
|
|
|
|
ASSERT(proc->refCount != 0);
|
|
|
|
if (__sync_fetch_and_sub(&proc->refCount, 1) == 1) {
|
|
|
|
Process_Destroy(proc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 22:15:41 +00:00
|
|
|
/**
|
2023-09-06 17:51:47 +00:00
|
|
|
* Process_Wait --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Wait for a process to exit and then cleanup it's references. If the pid ==
|
|
|
|
* 0, we wait for any process, otherwise we wait for a specific process.
|
|
|
|
*
|
|
|
|
* @param [in] proc Parent process.
|
|
|
|
* @param [in] pid Optionally specify the pid of the process to wait on.
|
2023-09-09 22:15:41 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* @retval ENOENT Process ID doesn't exist.
|
|
|
|
* @return Exit status of the process that exited or crashed.
|
2023-09-06 17:51:47 +00:00
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
uint64_t
|
|
|
|
Process_Wait(Process *proc, uint64_t pid)
|
|
|
|
{
|
|
|
|
Thread *thr;
|
|
|
|
Process *p = NULL;
|
|
|
|
uint64_t status;
|
|
|
|
|
|
|
|
// XXX: Need to verify pid exists!
|
|
|
|
|
2023-09-02 21:51:10 +00:00
|
|
|
Mutex_Lock(&proc->zombieProcLock);
|
2023-09-03 18:54:14 +00:00
|
|
|
if (pid == 0) {
|
|
|
|
while (1) {
|
|
|
|
p = TAILQ_FIRST(&proc->zombieProc);
|
|
|
|
if (p != NULL) {
|
|
|
|
TAILQ_REMOVE(&proc->zombieProc, p, siblingList);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_Wait(&proc->zombieProcCV, &proc->zombieProcLock);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = Process_Lookup(pid);
|
|
|
|
if (p == NULL) {
|
|
|
|
Mutex_Unlock(&proc->zombieProcLock);
|
|
|
|
return ENOENT;
|
2023-08-21 22:38:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 18:54:14 +00:00
|
|
|
while (p->procState != PROC_STATE_ZOMBIE) {
|
|
|
|
CV_Wait(&p->zombieProcPCV, &proc->zombieProcLock);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&proc->zombieProc, p, siblingList);
|
|
|
|
/*
|
|
|
|
* Release the reference from Process_Lookup but it's okay to continue
|
|
|
|
* using p because we're cleaning up the last reference as the parent
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
Process_Release(p);
|
2023-08-21 22:38:51 +00:00
|
|
|
}
|
2023-09-02 21:51:10 +00:00
|
|
|
Mutex_Unlock(&proc->zombieProcLock);
|
2023-08-21 22:38:51 +00:00
|
|
|
|
|
|
|
status = (p->pid << 16) | p->exitCode;
|
|
|
|
|
|
|
|
// Release threads
|
2023-09-06 02:18:10 +00:00
|
|
|
Spinlock_Lock(&proc->lock);
|
|
|
|
while (!TAILQ_EMPTY(&p->zombieQueue)) {
|
|
|
|
thr = TAILQ_FIRST(&p->zombieQueue);
|
|
|
|
TAILQ_REMOVE(&p->zombieQueue, thr, schedQueue);
|
|
|
|
Spinlock_Unlock(&proc->lock);
|
|
|
|
|
|
|
|
ASSERT(thr->proc->pid != 1);
|
2023-08-21 22:38:51 +00:00
|
|
|
Thread_Release(thr);
|
2023-09-06 02:18:10 +00:00
|
|
|
|
|
|
|
Spinlock_Lock(&proc->lock);
|
2023-08-21 22:38:51 +00:00
|
|
|
}
|
2023-09-06 02:18:10 +00:00
|
|
|
Spinlock_Unlock(&proc->lock);
|
2023-08-21 22:38:51 +00:00
|
|
|
|
|
|
|
// Release process
|
|
|
|
Process_Release(p);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Debugging
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
Process_Dump(Process *proc)
|
|
|
|
{
|
2023-09-03 18:54:14 +00:00
|
|
|
const char *stateStrings[] = {
|
|
|
|
"NULL",
|
|
|
|
"READY",
|
|
|
|
"ZOMBIE"
|
|
|
|
};
|
|
|
|
|
2023-08-21 22:38:51 +00:00
|
|
|
kprintf("title %s\n", proc->title);
|
|
|
|
kprintf("pid %llu\n", proc->pid);
|
2023-09-03 18:54:14 +00:00
|
|
|
kprintf("state %s\n", stateStrings[proc->procState]);
|
2023-08-21 22:38:51 +00:00
|
|
|
kprintf("space %016llx\n", proc->space);
|
|
|
|
kprintf("threads %llu\n", proc->threads);
|
|
|
|
kprintf("refCount %d\n", proc->refCount);
|
|
|
|
kprintf("nextFD %llu\n", proc->nextFD);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Debug_Processes(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
Process *proc;
|
|
|
|
|
2023-09-06 17:51:47 +00:00
|
|
|
/*
|
|
|
|
* We don't hold locks in case you the kernel debugger is entered while
|
|
|
|
* holding this lock.
|
|
|
|
*/
|
2023-08-21 22:38:51 +00:00
|
|
|
//Spinlock_Lock(&threadLock);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(proc, &processList, processList)
|
|
|
|
{
|
|
|
|
kprintf("Process: %d(%016llx)\n", proc->pid, proc);
|
|
|
|
Process_Dump(proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Spinlock_Unlock(&threadLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_DBGCMD(processes, "Display list of processes", Debug_Processes);
|
|
|
|
|
|
|
|
static void
|
|
|
|
Debug_ProcInfo(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
Thread *thr = curProc[CPU()];
|
|
|
|
|
|
|
|
kprintf("Current Process State:\n");
|
|
|
|
Process_Dump(thr->proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_DBGCMD(procinfo, "Display current process state", Debug_ProcInfo);
|
|
|
|
|