2014-07-24 01:07:07 +00:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-02-02 19:55:34 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
#include <sys/kassert.h>
|
|
|
|
#include <sys/kconfig.h>
|
|
|
|
#include <sys/kdebug.h>
|
|
|
|
#include <sys/kmem.h>
|
2015-01-16 23:25:36 +00:00
|
|
|
#include <sys/ktime.h>
|
2014-11-29 23:46:17 +00:00
|
|
|
#include <sys/mp.h>
|
2014-07-24 01:07:07 +00:00
|
|
|
#include <sys/spinlock.h>
|
|
|
|
#include <sys/thread.h>
|
|
|
|
|
|
|
|
#include <machine/trap.h>
|
|
|
|
#include <machine/pmap.h>
|
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
// Special Kernel Process
|
2015-01-22 18:08:09 +00:00
|
|
|
Process *kernelProcess;
|
2015-01-14 22:44:32 +00:00
|
|
|
|
|
|
|
// Scheduler Queues
|
2015-01-20 23:07:31 +00:00
|
|
|
Spinlock schedLock;
|
2015-01-20 01:04:14 +00:00
|
|
|
ThreadQueue waitQueue;
|
|
|
|
ThreadQueue runnableQueue;
|
2015-02-16 03:15:18 +00:00
|
|
|
Thread *curProc[MAX_CPUS];
|
2015-01-14 22:44:32 +00:00
|
|
|
|
|
|
|
// Process List
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock procLock;
|
|
|
|
uint64_t nextProcessID;
|
2015-01-20 01:04:14 +00:00
|
|
|
ProcessQueue processList;
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
// Memory Pools
|
2015-01-14 22:28:43 +00:00
|
|
|
Slab processSlab;
|
2014-09-05 06:26:02 +00:00
|
|
|
Slab threadSlab;
|
|
|
|
|
2014-10-14 23:24:47 +00:00
|
|
|
void Handle_GlobalInit();
|
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
void
|
|
|
|
Thread_Init()
|
|
|
|
{
|
2015-01-14 22:28:43 +00:00
|
|
|
nextProcessID = 1;
|
2014-08-08 00:58:41 +00:00
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
Slab_Init(&processSlab, "Process Objects", sizeof(Process), 16);
|
2014-09-05 06:26:02 +00:00
|
|
|
Slab_Init(&threadSlab, "Thread Objects", sizeof(Thread), 16);
|
|
|
|
|
2015-01-22 22:11:26 +00:00
|
|
|
Spinlock_Init(&procLock, "Process List Lock", SPINLOCK_TYPE_NORMAL);
|
|
|
|
Spinlock_Init(&schedLock, "Scheduler Lock", SPINLOCK_TYPE_RECURSIVE);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2015-01-14 22:44:32 +00:00
|
|
|
TAILQ_INIT(&waitQueue);
|
|
|
|
TAILQ_INIT(&runnableQueue);
|
|
|
|
TAILQ_INIT(&processList);
|
2014-09-05 06:26:02 +00:00
|
|
|
|
2014-10-14 23:24:47 +00:00
|
|
|
Handle_GlobalInit();
|
2015-01-14 22:44:32 +00:00
|
|
|
|
2015-01-22 18:08:09 +00:00
|
|
|
// Kernel Process
|
2015-01-22 22:11:26 +00:00
|
|
|
kernelProcess = Process_Create(NULL, "kernel");
|
2015-01-22 18:08:09 +00:00
|
|
|
|
2015-01-14 22:44:32 +00:00
|
|
|
// Create an thread object for current context
|
2015-01-22 22:11:26 +00:00
|
|
|
Process *proc = Process_Create(NULL, "init");
|
2015-02-16 03:15:18 +00:00
|
|
|
curProc[0] = Thread_Create(proc);
|
|
|
|
curProc[0]->schedState = SCHED_STATE_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Thread_InitAP()
|
|
|
|
{
|
|
|
|
Thread *apthr = Thread_Create(kernelProcess);
|
|
|
|
|
|
|
|
apthr->schedState = SCHED_STATE_RUNNING;
|
|
|
|
|
|
|
|
//PAlloc_Release((void *)thr->kstack);
|
|
|
|
//thr->kstack = 0;
|
|
|
|
|
|
|
|
curProc[CPU()] = apthr;
|
2014-07-24 01:07:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
/*
|
|
|
|
* Process
|
|
|
|
*/
|
|
|
|
|
|
|
|
Process *
|
2015-01-22 22:11:26 +00:00
|
|
|
Process_Create(Process *parent, const char *title)
|
2014-07-28 00:09:31 +00:00
|
|
|
{
|
2015-01-20 23:07:31 +00:00
|
|
|
Process *proc = (Process *)Slab_Alloc(&processSlab);
|
|
|
|
|
|
|
|
if (!proc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(proc, 0, sizeof(*proc));
|
|
|
|
|
|
|
|
proc->pid = nextProcessID++;
|
|
|
|
proc->threads = 0;
|
2015-01-22 17:46:48 +00:00
|
|
|
proc->refCount = 1;
|
2015-01-20 23:07:31 +00:00
|
|
|
TAILQ_INIT(&proc->threadList);
|
|
|
|
|
2015-01-22 18:08:09 +00:00
|
|
|
if (title) {
|
|
|
|
strncpy((char *)&proc->title, title, PROCESS_TITLE_LENGTH);
|
|
|
|
} else {
|
|
|
|
proc->title[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
proc->space = PMap_NewAS();
|
|
|
|
if (proc->space == NULL) {
|
|
|
|
Slab_Free(&processSlab, proc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
proc->ustackNext = MEM_USERSPACE_STKBASE;
|
|
|
|
|
2015-01-22 22:11:26 +00:00
|
|
|
Spinlock_Init(&proc->lock, "Process Lock", SPINLOCK_TYPE_NORMAL);
|
2015-01-22 21:04:43 +00:00
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
Semaphore_Init(&proc->zombieSemaphore, 0, "Zombie Semaphore");
|
|
|
|
TAILQ_INIT(&proc->zombieQueue);
|
|
|
|
|
|
|
|
Handle_Init(proc);
|
|
|
|
|
2015-01-22 22:11:26 +00:00
|
|
|
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);
|
|
|
|
Semaphore_Init(&proc->zombieProcSemaphore, 0, "Zombie Process Semaphore");
|
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Lock(&procLock);
|
2015-01-20 23:07:31 +00:00
|
|
|
TAILQ_INSERT_TAIL(&processList, proc, processList);
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Unlock(&procLock);
|
2015-01-20 23:07:31 +00:00
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:30:11 +00:00
|
|
|
static void
|
2015-01-20 23:07:31 +00:00
|
|
|
Process_Destroy(Process *proc)
|
|
|
|
{
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Destroy(&proc->lock);
|
|
|
|
Semaphore_Destroy(&proc->zombieSemaphore);
|
|
|
|
PMap_DestroyAS(proc->space);
|
|
|
|
|
|
|
|
// XXX: Close and destroy handles
|
|
|
|
|
2015-01-22 22:11:26 +00:00
|
|
|
// XXX: We need to promote zombie processes to our parent
|
|
|
|
// XXX: Release the semaphore as well
|
|
|
|
|
|
|
|
if (proc->parent) {
|
|
|
|
Spinlock_Lock(&proc->parent->lock);
|
|
|
|
TAILQ_REMOVE(&proc->parent->zombieProc, proc, siblingList);
|
|
|
|
Spinlock_Unlock(&proc->parent->lock);
|
|
|
|
}
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Lock(&procLock);
|
|
|
|
TAILQ_REMOVE(&processList, proc, processList);
|
|
|
|
Spinlock_Unlock(&procLock);
|
|
|
|
|
2015-01-22 18:08:09 +00:00
|
|
|
Slab_Free(&processSlab, proc);
|
2015-01-20 23:07:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:30:11 +00:00
|
|
|
Process *
|
|
|
|
Process_Lookup(uint64_t pid)
|
|
|
|
{
|
2015-01-22 21:04:43 +00:00
|
|
|
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;
|
2015-01-20 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Process_Retain(Process *proc)
|
|
|
|
{
|
2015-01-22 17:46:48 +00:00
|
|
|
ASSERT(proc->refCount != 0);
|
|
|
|
__sync_fetch_and_add(&proc->refCount, 1);
|
2015-01-20 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Process_Release(Process *proc)
|
|
|
|
{
|
2015-01-22 17:46:48 +00:00
|
|
|
ASSERT(proc->refCount != 0);
|
|
|
|
if (__sync_fetch_and_sub(&proc->refCount, 1) == 1) {
|
|
|
|
Process_Destroy(proc);
|
|
|
|
}
|
2015-01-20 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
uint64_t
|
2015-01-22 23:00:01 +00:00
|
|
|
Process_Wait(Process *proc, uint64_t pid)
|
2015-01-20 23:07:31 +00:00
|
|
|
{
|
2015-01-22 23:00:01 +00:00
|
|
|
Thread *thr;
|
|
|
|
Thread *thr_temp;
|
2015-01-22 22:11:26 +00:00
|
|
|
Process *p = NULL;
|
|
|
|
uint64_t status;
|
|
|
|
|
2015-01-22 23:00:01 +00:00
|
|
|
// XXX: Need to verify pid exists!
|
2015-01-22 22:11:26 +00:00
|
|
|
|
2015-01-22 23:00:01 +00:00
|
|
|
while (1) {
|
|
|
|
Semaphore_Acquire(&proc->zombieProcSemaphore);
|
|
|
|
// XXX: Forced exit check!
|
2015-01-22 22:11:26 +00:00
|
|
|
|
2015-01-22 23:00:01 +00:00
|
|
|
Spinlock_Lock(&proc->lock);
|
|
|
|
p = TAILQ_FIRST(&proc->zombieProc);
|
|
|
|
if (pid == 0 || p->pid == pid) {
|
|
|
|
TAILQ_REMOVE(&proc->zombieProc, p, siblingList);
|
|
|
|
Spinlock_Unlock(&proc->lock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Spinlock_Unlock(&proc->lock);
|
|
|
|
|
|
|
|
Semaphore_Release(&proc->zombieProcSemaphore);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = (p->pid << 16) | p->exitCode;
|
2015-01-22 22:11:26 +00:00
|
|
|
|
|
|
|
// Release threads
|
2015-01-22 23:00:01 +00:00
|
|
|
TAILQ_FOREACH_SAFE(thr, &p->zombieQueue, schedQueue, thr_temp) {
|
|
|
|
Thread_Release(thr);
|
|
|
|
}
|
2015-01-22 22:11:26 +00:00
|
|
|
|
|
|
|
// Release process
|
2015-01-22 23:00:01 +00:00
|
|
|
Process_Release(p);
|
2015-01-22 22:11:26 +00:00
|
|
|
|
|
|
|
return status;
|
2014-07-28 00:09:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
/*
|
|
|
|
* Thread
|
|
|
|
*/
|
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
Thread *
|
2015-01-14 22:28:43 +00:00
|
|
|
Thread_Create(Process *proc)
|
2014-07-24 01:07:07 +00:00
|
|
|
{
|
2014-09-05 06:26:02 +00:00
|
|
|
Thread *thr = (Thread *)Slab_Alloc(&threadSlab);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
|
|
|
if (!thr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(thr, 0, sizeof(*thr));
|
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
ASSERT(proc != NULL);
|
|
|
|
|
|
|
|
thr->tid = proc->nextThreadID++;
|
2014-07-24 01:07:07 +00:00
|
|
|
thr->kstack = (uintptr_t)PAlloc_AllocPage();
|
|
|
|
if (thr->kstack == 0) {
|
2014-12-09 06:37:40 +00:00
|
|
|
Slab_Free(&threadSlab, thr);
|
2014-07-24 01:07:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
Process_Retain(proc);
|
2015-01-17 01:57:21 +00:00
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Lock(&proc->lock);
|
|
|
|
thr->proc = proc;
|
|
|
|
proc->threads++;
|
|
|
|
TAILQ_INSERT_TAIL(&proc->threadList, thr, threadList);
|
|
|
|
thr->space = proc->space;
|
|
|
|
thr->ustack = proc->ustackNext;
|
|
|
|
proc->ustackNext += MEM_USERSPACE_STKLEN;
|
|
|
|
Spinlock_Unlock(&proc->lock);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
thr->schedState = SCHED_STATE_NULL;
|
2015-01-16 22:50:10 +00:00
|
|
|
thr->timerEvt = NULL;
|
2015-01-22 17:46:48 +00:00
|
|
|
thr->refCount = 1;
|
2014-08-08 20:55:12 +00:00
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
Thread_InitArch(thr);
|
|
|
|
// Initialize queue
|
|
|
|
|
|
|
|
return thr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread *
|
|
|
|
Thread_KThreadCreate(void (*f)(void *), void *arg)
|
|
|
|
{
|
2015-01-22 18:08:09 +00:00
|
|
|
Thread *thr = Thread_Create(kernelProcess);
|
2014-07-24 01:07:07 +00:00
|
|
|
if (!thr)
|
|
|
|
return NULL;
|
|
|
|
|
2015-01-17 01:57:21 +00:00
|
|
|
Thread_SetupKThread(thr, f, (uintptr_t)arg, 0, 0);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
|
|
|
return thr;
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
Thread *
|
2015-01-17 01:57:21 +00:00
|
|
|
Thread_UThreadCreate(Thread *oldThr, uint64_t rip, uint64_t arg)
|
2015-01-14 22:28:43 +00:00
|
|
|
{
|
|
|
|
Process *proc = oldThr->proc;
|
|
|
|
Thread *thr = (Thread *)Slab_Alloc(&threadSlab);
|
|
|
|
|
|
|
|
if (!thr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(thr, 0, sizeof(*thr));
|
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
thr->tid = proc->nextThreadID++;
|
2015-01-14 22:28:43 +00:00
|
|
|
thr->kstack = (uintptr_t)PAlloc_AllocPage();
|
|
|
|
if (thr->kstack == 0) {
|
|
|
|
Slab_Free(&threadSlab, thr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
thr->space = oldThr->space;
|
|
|
|
thr->schedState = SCHED_STATE_NULL;
|
2015-01-22 17:46:48 +00:00
|
|
|
thr->refCount = 1;
|
2015-01-14 22:28:43 +00:00
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Lock(&proc->lock);
|
2015-01-17 01:57:21 +00:00
|
|
|
thr->ustack = proc->ustackNext;
|
|
|
|
proc->ustackNext += MEM_USERSPACE_STKLEN;
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Unlock(&proc->lock);
|
|
|
|
|
2015-01-17 01:57:21 +00:00
|
|
|
PMap_AllocMap(thr->space, thr->ustack, MEM_USERSPACE_STKLEN, PTE_W);
|
2015-01-22 21:04:43 +00:00
|
|
|
// XXX: Check failure
|
2015-01-17 01:57:21 +00:00
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
Thread_InitArch(thr);
|
|
|
|
// Initialize queue
|
|
|
|
|
2015-01-17 01:57:21 +00:00
|
|
|
Thread_SetupUThread(thr, rip, arg);
|
2015-01-14 22:28:43 +00:00
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
Process_Retain(proc);
|
|
|
|
|
|
|
|
Spinlock_Lock(&proc->lock);
|
2015-01-14 22:28:43 +00:00
|
|
|
thr->proc = proc;
|
2015-01-22 21:04:43 +00:00
|
|
|
// XXX: Process lock
|
2015-01-14 22:28:43 +00:00
|
|
|
proc->threads++;
|
2015-01-17 21:37:32 +00:00
|
|
|
TAILQ_INSERT_TAIL(&proc->threadList, thr, threadList);
|
2015-01-22 21:04:43 +00:00
|
|
|
Spinlock_Unlock(&proc->lock);
|
2015-01-14 22:28:43 +00:00
|
|
|
|
|
|
|
return thr;
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:30:11 +00:00
|
|
|
static void
|
2014-07-24 01:07:07 +00:00
|
|
|
Thread_Destroy(Thread *thr)
|
|
|
|
{
|
2015-01-22 18:08:09 +00:00
|
|
|
Process *proc = thr->proc;
|
|
|
|
|
2015-01-17 03:41:27 +00:00
|
|
|
// Free userspace stack
|
|
|
|
|
2015-01-22 21:04:43 +00:00
|
|
|
proc->threads--;
|
|
|
|
TAILQ_REMOVE(&proc->threadList, thr, threadList);
|
2015-01-17 03:41:27 +00:00
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
// Free AS
|
2014-12-09 06:37:40 +00:00
|
|
|
PAlloc_Release((void *)thr->kstack);
|
2014-09-05 06:26:02 +00:00
|
|
|
Slab_Free(&threadSlab, thr);
|
2015-01-22 18:08:09 +00:00
|
|
|
|
|
|
|
// Release process handle
|
2015-01-22 21:04:43 +00:00
|
|
|
Process_Release(thr->proc);
|
2014-07-24 01:07:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:30:11 +00:00
|
|
|
Thread *
|
|
|
|
Thread_Lookup(Process *proc, uint64_t tid)
|
|
|
|
{
|
2015-01-22 21:04:43 +00:00
|
|
|
Thread *t;
|
|
|
|
Thread *thr = NULL;
|
|
|
|
|
|
|
|
Spinlock_Lock(&proc->lock);
|
|
|
|
TAILQ_FOREACH(t, &proc->threadList, threadList) {
|
|
|
|
if (t->tid == tid) {
|
|
|
|
Thread_Retain(t);
|
|
|
|
thr = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Spinlock_Unlock(&proc->lock);
|
|
|
|
|
|
|
|
return thr;
|
2015-01-20 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Thread_Retain(Thread *thr)
|
|
|
|
{
|
2015-01-22 17:46:48 +00:00
|
|
|
ASSERT(thr->refCount != 0);
|
|
|
|
__sync_fetch_and_add(&thr->refCount, 1);
|
2015-01-20 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Thread_Release(Thread *thr)
|
|
|
|
{
|
2015-01-22 17:46:48 +00:00
|
|
|
ASSERT(thr->refCount != 0);
|
|
|
|
if (__sync_fetch_and_sub(&thr->refCount, 1) == 1) {
|
|
|
|
Thread_Destroy(thr);
|
|
|
|
}
|
2015-01-20 23:30:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 03:41:27 +00:00
|
|
|
uint64_t
|
|
|
|
Thread_Wait(Thread *thr, uint64_t tid)
|
|
|
|
{
|
|
|
|
Thread *t;
|
|
|
|
uint64_t status;
|
|
|
|
|
|
|
|
ASSERT(thr->proc != NULL);
|
|
|
|
|
|
|
|
if (tid == 0) {
|
|
|
|
t = TAILQ_FIRST(&thr->proc->zombieQueue);
|
|
|
|
if (!t) {
|
2015-02-02 19:55:34 +00:00
|
|
|
return SYSCALL_PACK(EAGAIN, 0);
|
2015-01-17 03:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_REMOVE(&thr->proc->zombieQueue, t, schedQueue);
|
|
|
|
status = t->exitValue;
|
2015-01-22 17:46:48 +00:00
|
|
|
Thread_Release(t);
|
2015-02-02 19:55:34 +00:00
|
|
|
return SYSCALL_PACK(0, status);
|
2015-01-17 03:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_FOREACH(t, &thr->proc->zombieQueue, schedQueue) {
|
|
|
|
if (t->tid == tid) {
|
|
|
|
TAILQ_REMOVE(&thr->proc->zombieQueue, t, schedQueue);
|
|
|
|
status = t->exitValue;
|
2015-01-22 17:46:48 +00:00
|
|
|
Thread_Release(t);
|
2015-02-02 19:55:34 +00:00
|
|
|
return SYSCALL_PACK(0, status);
|
2015-01-17 03:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
/*
|
|
|
|
* Scheduler Functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
Thread *
|
|
|
|
Sched_Current()
|
|
|
|
{
|
2015-01-22 17:46:48 +00:00
|
|
|
Spinlock_Lock(&schedLock);
|
|
|
|
|
2015-02-16 03:15:18 +00:00
|
|
|
Thread *thr = curProc[CPU()];
|
2015-01-22 17:46:48 +00:00
|
|
|
Thread_Retain(thr);
|
|
|
|
|
|
|
|
Spinlock_Unlock(&schedLock);
|
|
|
|
|
|
|
|
return thr;
|
2015-01-20 23:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sched_SetRunnable(Thread *thr)
|
|
|
|
{
|
|
|
|
Spinlock_Lock(&schedLock);
|
|
|
|
|
|
|
|
if (thr->schedState == SCHED_STATE_WAITING) {
|
|
|
|
thr->waitTime += KTime_GetEpochNS() - thr->waitStart;
|
|
|
|
thr->waitStart = 0;
|
|
|
|
TAILQ_REMOVE(&waitQueue, thr, schedQueue);
|
|
|
|
}
|
|
|
|
thr->schedState = SCHED_STATE_RUNNABLE;
|
|
|
|
TAILQ_INSERT_TAIL(&runnableQueue, thr, schedQueue);
|
|
|
|
|
|
|
|
Spinlock_Unlock(&schedLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sched_SetWaiting(Thread *thr)
|
|
|
|
{
|
|
|
|
Spinlock_Lock(&schedLock);
|
|
|
|
|
|
|
|
thr->schedState = SCHED_STATE_WAITING;
|
|
|
|
TAILQ_INSERT_TAIL(&waitQueue, thr, schedQueue);
|
|
|
|
thr->waitStart = KTime_GetEpochNS();
|
|
|
|
|
|
|
|
Spinlock_Unlock(&schedLock);
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
void
|
2015-01-20 23:07:31 +00:00
|
|
|
Sched_SetZombie(Thread *thr)
|
|
|
|
{
|
2015-01-22 22:11:26 +00:00
|
|
|
Process *proc = thr->proc;
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
Spinlock_Lock(&schedLock);
|
|
|
|
|
|
|
|
thr->schedState = SCHED_STATE_ZOMBIE;
|
2015-01-22 22:11:26 +00:00
|
|
|
Spinlock_Lock(&proc->lock);
|
|
|
|
TAILQ_INSERT_TAIL(&proc->zombieQueue, thr, schedQueue);
|
|
|
|
Spinlock_Unlock(&proc->lock);
|
2015-01-22 21:04:43 +00:00
|
|
|
|
2015-01-22 22:11:26 +00:00
|
|
|
if (proc->threads == 1) {
|
|
|
|
// All processes have parents except 'init' and 'kernel'
|
|
|
|
ASSERT(proc->parent != NULL);
|
|
|
|
Spinlock_Lock(&proc->parent->lock);
|
|
|
|
TAILQ_REMOVE(&proc->parent->childrenList, proc, siblingList);
|
|
|
|
TAILQ_INSERT_TAIL(&proc->parent->zombieProc, proc, siblingList);
|
|
|
|
Semaphore_Release(&proc->parent->zombieProcSemaphore);
|
|
|
|
Spinlock_Unlock(&proc->parent->lock);
|
2015-01-20 23:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Spinlock_Unlock(&schedLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Sched_Switch(Thread *oldthr, Thread *newthr)
|
2014-07-24 01:07:07 +00:00
|
|
|
{
|
|
|
|
// Load AS
|
|
|
|
PMap_LoadAS(newthr->space);
|
|
|
|
|
|
|
|
Thread_SwitchArch(oldthr, newthr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-20 23:07:31 +00:00
|
|
|
Sched_Scheduler()
|
2014-07-24 01:07:07 +00:00
|
|
|
{
|
|
|
|
Thread *prev;
|
|
|
|
Thread *next;
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
Spinlock_Lock(&schedLock);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
|
|
|
// Select next thread
|
2015-01-14 22:44:32 +00:00
|
|
|
next = TAILQ_FIRST(&runnableQueue);
|
2015-01-16 05:24:15 +00:00
|
|
|
if (!next) {
|
|
|
|
/*
|
|
|
|
* There may be no other runnable processes on this core. This is a
|
2015-01-17 03:41:27 +00:00
|
|
|
* good opportunity to migrate threads. We should never hit this case
|
|
|
|
* once the OS is up and running because of the idle threads, but just
|
|
|
|
* in case we should assert that we never return to a zombie or waiting
|
|
|
|
* thread.
|
2015-01-16 05:24:15 +00:00
|
|
|
*/
|
2015-02-16 03:15:18 +00:00
|
|
|
ASSERT(curProc[CPU()]->schedState == SCHED_STATE_RUNNING);
|
2015-01-20 23:07:31 +00:00
|
|
|
Spinlock_Unlock(&schedLock);
|
2015-01-16 05:24:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-14 22:44:32 +00:00
|
|
|
TAILQ_REMOVE(&runnableQueue, next, schedQueue);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2015-02-16 03:15:18 +00:00
|
|
|
prev = curProc[CPU()];
|
|
|
|
curProc[CPU()] = next;
|
2014-07-24 01:07:07 +00:00
|
|
|
next->schedState = SCHED_STATE_RUNNING;
|
|
|
|
next->ctxSwitches++;
|
|
|
|
|
2014-08-08 00:58:41 +00:00
|
|
|
if (prev->schedState == SCHED_STATE_RUNNING) {
|
|
|
|
prev->schedState = SCHED_STATE_RUNNABLE;
|
2015-01-14 22:44:32 +00:00
|
|
|
TAILQ_INSERT_TAIL(&runnableQueue, prev, schedQueue);
|
2014-08-08 00:58:41 +00:00
|
|
|
}
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
Sched_Switch(prev, next);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
Spinlock_Unlock(&schedLock);
|
2014-07-24 01:07:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:46:17 +00:00
|
|
|
extern TaskStateSegment64 TSS[MAX_CPUS];
|
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
void
|
|
|
|
ThreadKThreadEntry(TrapFrame *tf)
|
|
|
|
{
|
2015-02-16 03:15:18 +00:00
|
|
|
TSS[CPU()].rsp0 = curProc[CPU()]->kstack + 4096;
|
2014-11-29 23:46:17 +00:00
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
Spinlock_Unlock(&schedLock);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
|
|
|
Trap_Pop(tf);
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:07:31 +00:00
|
|
|
/*
|
|
|
|
* Debugging
|
|
|
|
*/
|
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
void
|
2015-01-20 23:07:31 +00:00
|
|
|
Process_Dump(Process *proc)
|
2015-01-14 22:28:43 +00:00
|
|
|
{
|
2015-01-22 18:08:09 +00:00
|
|
|
kprintf("title %s\n", proc->title);
|
2015-01-14 22:28:43 +00:00
|
|
|
kprintf("pid %llu\n", proc->pid);
|
|
|
|
kprintf("space %016llx\n", proc->space);
|
|
|
|
kprintf("threads %llu\n", proc->threads);
|
2015-01-22 17:46:48 +00:00
|
|
|
kprintf("refCount %d\n", proc->refCount);
|
2015-01-14 22:28:43 +00:00
|
|
|
kprintf("nextFD %llu\n", proc->nextFD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Thread_Dump(Thread *thr)
|
|
|
|
{
|
|
|
|
// Thread_DumpArch(thr)
|
|
|
|
kprintf("space %016llx\n", thr->space);
|
|
|
|
kprintf("kstack %016llx\n", thr->kstack);
|
|
|
|
kprintf("tid %llu\n", thr->tid);
|
2015-01-22 17:46:48 +00:00
|
|
|
kprintf("refCount %d\n", thr->refCount);
|
2015-01-14 22:28:43 +00:00
|
|
|
kprintf("state %d\n", thr->schedState);
|
|
|
|
kprintf("ctxswtch %llu\n", thr->ctxSwitches);
|
|
|
|
kprintf("utime %llu\n", thr->userTime);
|
|
|
|
kprintf("ktime %llu\n", thr->kernTime);
|
|
|
|
kprintf("wtime %llu\n", thr->waitTime);
|
|
|
|
if (thr->proc) {
|
2015-01-20 23:07:31 +00:00
|
|
|
Process_Dump(thr->proc);
|
2015-01-14 22:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
void
|
|
|
|
Debug_Threads(int argc, const char *argv[])
|
|
|
|
{
|
2015-02-16 03:15:18 +00:00
|
|
|
int c = CPU();
|
2014-07-24 01:07:07 +00:00
|
|
|
Thread *thr;
|
|
|
|
|
2014-11-29 23:46:17 +00:00
|
|
|
//Spinlock_Lock(&threadLock);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2015-02-16 03:15:18 +00:00
|
|
|
kprintf("Current Thread: %d(%016llx) %d\n",
|
|
|
|
curProc[c]->tid, curProc[c], curProc[c]->ctxSwitches);
|
|
|
|
Thread_Dump(curProc[c]);
|
2015-01-14 22:44:32 +00:00
|
|
|
TAILQ_FOREACH(thr, &runnableQueue, schedQueue)
|
2014-07-24 01:07:07 +00:00
|
|
|
{
|
2015-01-14 22:44:32 +00:00
|
|
|
kprintf("Runnable Thread: %d(%016llx) %d\n", thr->tid, thr, thr->ctxSwitches);
|
2015-01-16 23:25:36 +00:00
|
|
|
Thread_Dump(thr);
|
2015-01-14 22:44:32 +00:00
|
|
|
}
|
|
|
|
TAILQ_FOREACH(thr, &waitQueue, schedQueue)
|
|
|
|
{
|
|
|
|
kprintf("Waiting Thread: %d(%016llx) %d\n", thr->tid, thr, thr->ctxSwitches);
|
2015-01-16 23:25:36 +00:00
|
|
|
Thread_Dump(thr);
|
2014-07-24 01:07:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:46:17 +00:00
|
|
|
//Spinlock_Unlock(&threadLock);
|
2014-07-24 01:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_DBGCMD(threads, "Display list of threads", Debug_Threads);
|
|
|
|
|
2015-01-14 22:44:32 +00:00
|
|
|
void
|
|
|
|
Debug_Processes(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
Process *proc;
|
|
|
|
|
|
|
|
//Spinlock_Lock(&threadLock);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(proc, &processList, processList)
|
|
|
|
{
|
|
|
|
kprintf("Process: %d(%016llx)\n", proc->pid, proc);
|
2015-01-22 17:46:48 +00:00
|
|
|
Process_Dump(proc);
|
2015-01-14 22:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Spinlock_Unlock(&threadLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_DBGCMD(processes, "Display list of processes", Debug_Processes);
|
|
|
|
|
2014-11-29 23:46:17 +00:00
|
|
|
void
|
|
|
|
Debug_ThreadInfo(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_DBGCMD(threadinfo, "Display thread state", Debug_ThreadInfo);
|
|
|
|
|