X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fthreads.c;h=8c51d9a6e99d2f694308cbee1b9e23d157c5c7d6;hb=eecce4b7a55315f6c385ad8be35c25dbb12d43d8;hp=fb14f4d7f07117cf226d189e7725a7301335cfdc;hpb=f119d0e5b18b7286d04fc536a94e0f96e3c51714;p=tpg%2Facess2.git diff --git a/Kernel/threads.c b/Kernel/threads.c index fb14f4d7..8c51d9a6 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -3,35 +3,49 @@ * threads.c * - Common Thread Control */ -#include +#include #include +#include // === CONSTANTS === #define DEFAULT_QUANTUM 10 #define DEFAULT_TICKETS 5 #define MAX_TICKETS 10 +const enum eConfigTypes cCONFIG_TYPES[] = { + CFGT_HEAPSTR, // CFG_VFS_CWD + CFGT_INT, // CFG_VFS_MAXFILES + CFGT_NULL +}; // === IMPORTS === extern void ArchThreads_Init(); +extern void Proc_Start(); extern tThread *Proc_GetCurThread(); extern int Proc_Clone(Uint *Err, Uint Flags); +extern void Proc_CallFaultHandler(tThread *Thread); // === PROTOTYPES === void Threads_Init(); -void Threads_SetName(char *NewName); + int Threads_SetName(char *NewName); +char *Threads_GetName(int ID); void Threads_SetTickets(int Num); +tThread *Threads_CloneTCB(Uint *Err, Uint Flags); int Threads_WaitTID(int TID, int *status); tThread *Threads_GetThread(Uint TID); +void Threads_AddToDelete(tThread *Thread); tThread *Threads_int_GetPrev(tThread **List, tThread *Thread); void Threads_Exit(int TID, int Status); void Threads_Kill(tThread *Thread, int Status); void Threads_Yield(); void Threads_Sleep(); void Threads_Wake(tThread *Thread); +void Threads_AddActive(tThread *Thread); int Threads_GetPID(); int Threads_GetTID(); - int Threads_GetUID(); - int Threads_GetGID(); +tUID Threads_GetUID(); + int Threads_SetUID(Uint *Errno, tUID ID); +tGID Threads_GetGID(); + int Threads_SetGID(Uint *Errno, tUID ID); void Threads_Dump(); // === GLOBALS === @@ -49,7 +63,7 @@ tThread gThreadZero = { {0}, // Saved State {0}, // VM State - 0, {0}, {0}, // Signal State + 0, 0, // Current Fault, Fault Handler NULL, NULL, // Messages, Last Message DEFAULT_QUANTUM, DEFAULT_QUANTUM, // Quantum, Remaining @@ -58,7 +72,7 @@ tThread gThreadZero = { }; // -- Processes -- // --- Locks --- -volatile int giThreadListLock = 0; ///\note NEVER use a heap function while locked +tSpinlock glThreadListLock = 0; ///\note NEVER use a heap function while locked // --- Current State --- volatile int giNumActiveThreads = 0; volatile int giTotalTickets = 0; @@ -91,22 +105,40 @@ void Threads_Init() cur->ThreadName = "Idle Thread"; Threads_SetTickets(0); // Never called randomly cur->Quantum = 1; // 1 slice quantum - for(;;) __asm__ __volatile__ ("hlt"); // Just yeilds + HALT(); + for(;;) { + HALT(); // Just yeilds + } } #endif + + Proc_Start(); } /** * \fn void Threads_SetName(char *NewName) * \brief Sets the current thread's name */ -void Threads_SetName(char *NewName) +int Threads_SetName(char *NewName) { tThread *cur = Proc_GetCurThread(); if( IsHeap(cur->ThreadName) ) free( cur->ThreadName ); cur->ThreadName = malloc(strlen(NewName)+1); strcpy(cur->ThreadName, NewName); + return 0; +} + +/** + * \fn char *Threads_GetName(int ID) + * \brief Gets a thread's name + */ +char *Threads_GetName(int ID) +{ + if(ID == -1) { + return Proc_GetCurThread()->ThreadName; + } + return NULL; } /** @@ -119,12 +151,90 @@ void Threads_SetTickets(int Num) if(Num < 0) return; if(Num > MAX_TICKETS) Num = MAX_TICKETS; - LOCK( &giThreadListLock ); + LOCK( &glThreadListLock ); giTotalTickets -= cur->NumTickets; cur->NumTickets = Num; giTotalTickets += Num; //LOG("giTotalTickets = %i", giTotalTickets); - RELEASE( &giThreadListLock ); + RELEASE( &glThreadListLock ); +} + +/** + * \fn tThread *Threads_CloneTCB(Uint *Err, Uint Flags) + */ +tThread *Threads_CloneTCB(Uint *Err, Uint Flags) +{ + tThread *cur, *new; + int i; + cur = Proc_GetCurThread(); + + new = malloc(sizeof(tThread)); + if(new == NULL) { + *Err = -ENOMEM; + return NULL; + } + + new->Next = NULL; + new->IsLocked = 0; + new->Status = THREAD_STAT_ACTIVE; + new->RetStatus = 0; + + // Get Thread ID + new->TID = giNextTID++; + new->PTID = cur->TID; + + // Clone Name + new->ThreadName = malloc(strlen(cur->ThreadName)+1); + strcpy(new->ThreadName, cur->ThreadName); + + // Set Thread Group ID (PID) + if(Flags & CLONE_VM) + new->TGID = new->TID; + else + new->TGID = cur->TGID; + + // Messages are not inherited + new->Messages = NULL; + new->LastMessage = NULL; + + // Set State + new->Remaining = new->Quantum = cur->Quantum; + new->NumTickets = cur->NumTickets; + + // Set Signal Handlers + new->CurFaultNum = 0; + new->FaultHandler = cur->FaultHandler; + + for( i = 0; i < NUM_CFG_ENTRIES; i ++ ) + { + switch(cCONFIG_TYPES[i]) + { + default: + new->Config[i] = cur->Config[i]; + break; + case CFGT_HEAPSTR: + if(cur->Config[i]) + new->Config[i] = (Uint) strdup( (void*)cur->Config[i] ); + else + new->Config[i] = 0; + break; + } + } + + return new; +} + +/** + * \fn Uint *Threads_GetCfgPtr(int Id) + */ +Uint *Threads_GetCfgPtr(int Id) +{ + if(Id < 0 || Id >= NUM_CFG_ENTRIES) { + Warning("Threads_GetCfgPtr: Index %i is out of bounds", Id); + return NULL; + } + + return &Proc_GetCurThread()->Config[Id]; } /** @@ -132,7 +242,7 @@ void Threads_SetTickets(int Num) * \brief Wait for a task to change state */ int Threads_WaitTID(int TID, int *status) -{ +{ // Any Child if(TID == -1) { @@ -155,16 +265,23 @@ int Threads_WaitTID(int TID, int *status) tThread *t = Threads_GetThread(TID); int initStatus = t->Status; int ret; - while(t->Status == initStatus) Threads_Yield(); + + if(initStatus != THREAD_STAT_ZOMBIE) { + while(t->Status == initStatus) { + Threads_Yield(); + } + } + ret = t->RetStatus; switch(t->Status) { case THREAD_STAT_ZOMBIE: t->Status = THREAD_STAT_DEAD; - *status = 0; + if(status) *status = 0; + Threads_AddToDelete( t ); break; default: - *status = -1; + if(status) *status = -1; break; } return ret; @@ -203,7 +320,23 @@ tThread *Threads_GetThread(Uint TID) } /** - * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread) + * \fn void Threads_AddToDelete(tThread *Thread) + * \brief Adds a thread to the delete queue + */ +void Threads_AddToDelete(tThread *Thread) +{ + // Add to delete queue + if(gDeleteThreads) { + Thread->Next = gDeleteThreads; + gDeleteThreads = Thread; + } else { + Thread->Next = NULL; + gDeleteThreads = Thread; + } +} + +/** + * \fn tThread *Threads_int_GetPrev(tThread **List, tThread *Thread) * \brief Gets the previous entry in a thead linked list */ tThread *Threads_int_GetPrev(tThread **List, tThread *Thread) @@ -231,13 +364,18 @@ tThread *Threads_int_GetPrev(tThread **List, tThread *Thread) */ void Threads_Exit(int TID, int Status) { - Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF ); + if( TID == 0 ) + Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF ); + else + Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF ); + for(;;) HALT(); // Just in case } /** * \fn void Threads_Kill(tThread *Thread, int Status) * \brief Kill a thread - * \param TID Thread ID (0 for current) + * \param Thread Thread to kill + * \param Status Status code to return to the parent */ void Threads_Kill(tThread *Thread, int Status) { @@ -252,7 +390,7 @@ void Threads_Kill(tThread *Thread, int Status) child; child = child->Next) { - if(child->PTID == gCurrentThread->TID) + if(child->PTID == Thread->TID) Threads_Kill(child, -1); } } @@ -264,7 +402,7 @@ void Threads_Kill(tThread *Thread, int Status) LOCK( &Thread->IsLocked ); // Lock thread list - LOCK( &giThreadListLock ); + LOCK( &glThreadListLock ); // Get previous thread on list prev = Threads_int_GetPrev( &gActiveThreads, Thread ); @@ -295,21 +433,17 @@ void Threads_Kill(tThread *Thread, int Status) if(Status == -1) { Thread->Status = THREAD_STAT_DEAD; - // Add to delete queue - if(gDeleteThreads) { - Thread->Next = gDeleteThreads; - gDeleteThreads = Thread; - } else { - Thread->Next = NULL; - gDeleteThreads = Thread; - } + Threads_AddToDelete( Thread ); } else { Thread->Status = THREAD_STAT_ZOMBIE; } // Release spinlocks RELEASE( &Thread->IsLocked ); // Released first so that it IS released - RELEASE( &giThreadListLock ); + RELEASE( &glThreadListLock ); + + //Log("Thread %i went *hurk*", Thread->TID); + if(Status != -1) HALT(); } @@ -319,7 +453,7 @@ void Threads_Kill(tThread *Thread, int Status) */ void Threads_Yield() { - Proc_GetCurThread()->Quantum = 0; + Proc_GetCurThread()->Remaining = 0; HALT(); } @@ -332,21 +466,22 @@ void Threads_Sleep() tThread *cur = Proc_GetCurThread(); tThread *thread; - //Log("Proc_Sleep: %i going to sleep", gCurrentThread->TID); + //Log_Log("Threads", "%i going to sleep", cur->TID); // Acquire Spinlock - LOCK( &giThreadListLock ); + LOCK( &glThreadListLock ); // Get thread before current thread thread = Threads_int_GetPrev( &gActiveThreads, cur ); if(!thread) { - Warning("Proc_Sleep - Current thread is not on the active queue"); + Warning("Threads_Sleep - Current thread is not on the active queue"); + Threads_Dump(); return; } // Don't sleep if there is a message waiting if( cur->Messages ) { - RELEASE( &giThreadListLock ); + RELEASE( &glThreadListLock ); return; } @@ -368,9 +503,9 @@ void Threads_Sleep() cur->Status = THREAD_STAT_SLEEPING; // Release Spinlock - RELEASE( &giThreadListLock ); + RELEASE( &glThreadListLock ); - HALT(); + while(cur->Status != THREAD_STAT_ACTIVE) HALT(); } @@ -385,13 +520,16 @@ void Threads_Wake(tThread *Thread) { case THREAD_STAT_ACTIVE: break; case THREAD_STAT_SLEEPING: - LOCK( &giThreadListLock ); + //Log_Log("Threads", "Waking %i (%p) from sleeping", Thread->TID, Thread); + LOCK( &glThreadListLock ); prev = Threads_int_GetPrev(&gSleepingThreads, Thread); prev->Next = Thread->Next; // Remove from sleeping queue Thread->Next = gActiveThreads; // Add to active queue gActiveThreads = Thread; + giNumActiveThreads ++; + giTotalTickets += Thread->NumTickets; Thread->Status = THREAD_STAT_ACTIVE; - RELEASE( &giThreadListLock ); + RELEASE( &glThreadListLock ); break; case THREAD_STAT_WAITING: Warning("Thread_Wake - Waiting threads are not currently supported"); @@ -405,24 +543,116 @@ void Threads_Wake(tThread *Thread) } } +void Threads_WakeTID(tTID Thread) +{ + Threads_Wake( Threads_GetThread(Thread) ); +} + +/** + * \fn void Threads_AddActive(tThread *Thread) + * \brief Adds a thread to the active queue + */ +void Threads_AddActive(tThread *Thread) +{ + LOCK( &glThreadListLock ); + Thread->Next = gActiveThreads; + gActiveThreads = Thread; + giNumActiveThreads ++; + giTotalTickets += Thread->NumTickets; + //Log("Threads_AddActive: giNumActiveThreads = %i, giTotalTickets = %i", + // giNumActiveThreads, giTotalTickets); + RELEASE( &glThreadListLock ); +} + +/** + * \fn void Threads_SetFaultHandler(Uint Handler) + * \brief Sets the signal handler for a signal + */ +void Threads_SetFaultHandler(Uint Handler) +{ + Log_Log("Threads", "Threads_SetFaultHandler: Handler = %p", Handler); + Proc_GetCurThread()->FaultHandler = Handler; +} + +/** + * \fn void Threads_Fault(int Num) + * \brief Calls a fault handler + */ +void Threads_Fault(int Num) +{ + tThread *thread = Proc_GetCurThread(); + + Log_Log("Threads", "Threads_Fault: thread = %p", thread); + + if(!thread) return ; + + Log_Log("Threads", "Threads_Fault: thread->FaultHandler = %p", thread->FaultHandler); + + switch(thread->FaultHandler) + { + case 0: // Panic? + Threads_Kill(thread, -1); + HALT(); + return ; + case 1: // Dump Core? + Threads_Kill(thread, -1); + HALT(); + return ; + } + + // Double Fault? Oh, F**k + if(thread->CurFaultNum != 0) { + Threads_Kill(thread, -1); // For now, just kill + HALT(); + } + + thread->CurFaultNum = Num; + + Proc_CallFaultHandler(thread); +} + // --- Process Structure Access Functions --- -int Threads_GetPID() +tPID Threads_GetPID() { return Proc_GetCurThread()->TGID; } -int Threads_GetTID() +tTID Threads_GetTID() { return Proc_GetCurThread()->TID; } -int Threads_GetUID() +tUID Threads_GetUID() { return Proc_GetCurThread()->UID; } -int Threads_GetGID() +tGID Threads_GetGID() { return Proc_GetCurThread()->GID; } +int Threads_SetUID(Uint *Errno, tUID ID) +{ + tThread *t = Proc_GetCurThread(); + if( t->UID != 0 ) { + *Errno = -EACCES; + return -1; + } + Log("Threads_SetUID - Setting User ID to %i", ID); + t->UID = ID; + return 0; +} + +int Threads_SetGID(Uint *Errno, tGID ID) +{ + tThread *t = Proc_GetCurThread(); + if( t->UID != 0 ) { + *Errno = -EACCES; + return -1; + } + Log("Threads_SetGID - Setting Group ID to %i", ID); + t->GID = ID; + return 0; +} + /** * \fn void Threads_Dump() * \brief Dums a list of currently running threads @@ -430,19 +660,80 @@ int Threads_GetGID() void Threads_Dump() { tThread *thread; + tThread *cur = Proc_GetCurThread(); Log("Active Threads:"); for(thread=gActiveThreads;thread;thread=thread->Next) { - Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName); + Log("%c%i (%i) - %s", + (thread==cur?'*':' '), + thread->TID, thread->TGID, thread->ThreadName); Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum); Log(" KStack 0x%x", thread->KernelStack); } Log("Sleeping Threads:"); for(thread=gSleepingThreads;thread;thread=thread->Next) { - Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName); + Log("%c%i (%i) - %s", + (thread==cur?'*':' '), + thread->TID, thread->TGID, thread->ThreadName); Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum); Log(" KStack 0x%x", thread->KernelStack); } } + +/** + * \fn tThread *Threads_GetNextToRun(int CPU) + * \brief Gets the next thread to run + */ +tThread *Threads_GetNextToRun(int CPU) +{ + tThread *thread; + int ticket; + int number; + + if(giNumActiveThreads == 0) { + return NULL; + } + + // Special case: 1 thread + if(giNumActiveThreads == 1) { + return gActiveThreads; + } + + // Get the ticket number + ticket = number = rand() % giTotalTickets; + + // Find the next thread + for(thread=gActiveThreads;thread;thread=thread->Next) + { + if(thread->NumTickets > number) break; + number -= thread->NumTickets; + } + + // Error Check + if(thread == NULL) + { + number = 0; + for(thread=gActiveThreads;thread;thread=thread->Next) + number += thread->NumTickets; + Panic("Bookeeping Failed - giTotalTicketCount (%i) != true count (%i)", + giTotalTickets, number); + } + + return thread; +} + +/** + * \fn void Threads_SegFault(tVAddr Addr) + * \brief Called when a Segment Fault occurs + */ +void Threads_SegFault(tVAddr Addr) +{ + Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); + Threads_Fault( 1 ); + //Threads_Exit( 0, -1 ); +} + +// === EXPORTS === +EXPORT(Threads_GetUID);