Fixes to Libc, Doxygen Comments and VTerm layout
[tpg/acess2.git] / Kernel / arch / x86 / proc.c
index bb93bfc..5301401 100644 (file)
@@ -10,6 +10,9 @@
 # include <mp.h>
 #endif
 
+// === FLAGS ===
+#define DEBUG_TRACE_SWITCH     0
+
 // === CONSTANTS ===
 #define        SWITCH_MAGIC    0xFFFACE55      // There is no code in this area
 #define TIMER_DIVISOR  11931   //~100Hz
@@ -29,6 +32,9 @@ extern tThread        *gActiveThreads;
 extern tThread *gSleepingThreads;
 extern tThread *gDeleteThreads;
 extern tThread *Threads_GetNextToRun(int CPU);
+extern void    Threads_Dump();
+extern tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
+extern void    Isr7();
 
 // === PROTOTYPES ===
 void   ArchThreads_Init();
@@ -55,6 +61,13 @@ tTSS *gTSSs = NULL;
 #if !USE_MP
 tTSS   gTSS0 = {0};
 #endif
+// --- Error Recovery ---
+char   gaDoubleFaultStack[1024];
+tTSS   gDoubleFault_TSS = {
+       .ESP0 = (Uint)&gaDoubleFaultStack[1023],
+       .SS0 = 0x10,
+       .EIP = (Uint)Isr7
+};
 
 // === CODE ===
 /**
@@ -101,6 +114,15 @@ void ArchThreads_Init()
        #if USE_MP
        }
        
+       // Initialise Double Fault TSS
+       gGDT[5].LimitLow = sizeof(tTSS);
+       gGDT[5].LimitHi = 0;
+       gGDT[5].Access = 0x89;  // Type
+       gGDT[5].Flags = 0x4;
+       gGDT[5].BaseLow = (Uint)&gDoubleFault_TSS & 0xFFFF;
+       gGDT[5].BaseMid = (Uint)&gDoubleFault_TSS >> 16;
+       gGDT[5].BaseHi = (Uint)&gDoubleFault_TSS >> 24;
+       
        // Initialise TSS
        for(pos=0;pos<giNumCPUs;pos++)
        {
@@ -109,18 +131,18 @@ void ArchThreads_Init()
        #endif
                gTSSs[pos].SS0 = 0x10;
                gTSSs[pos].ESP0 = 0;    // Set properly by scheduler
-               gGDT[5+pos].LimitLow = sizeof(tTSS);
-               gGDT[5+pos].LimitHi = 0;
-               gGDT[5+pos].Access = 0x89;      // Type
-               gGDT[5+pos].Flags = 0x4;
-               gGDT[5+pos].BaseLow = (Uint)&gTSSs[pos] & 0xFFFF;
-               gGDT[5+pos].BaseMid = (Uint)&gTSSs[pos] >> 16;
-               gGDT[5+pos].BaseHi = (Uint)&gTSSs[pos] >> 24;
+               gGDT[6+pos].LimitLow = sizeof(tTSS);
+               gGDT[6+pos].LimitHi = 0;
+               gGDT[6+pos].Access = 0x89;      // Type
+               gGDT[6+pos].Flags = 0x4;
+               gGDT[6+pos].BaseLow = (Uint)&gTSSs[pos] & 0xFFFF;
+               gGDT[6+pos].BaseMid = (Uint)&gTSSs[pos] >> 16;
+               gGDT[6+pos].BaseHi = (Uint)&gTSSs[pos] >> 24;
        #if USE_MP
        }
        for(pos=0;pos<giNumCPUs;pos++) {
        #endif
-               __asm__ __volatile__ ("ltr %%ax"::"a"(0x28+pos*8));
+               __asm__ __volatile__ ("ltr %%ax"::"a"(0x30+pos*8));
        #if USE_MP
        }
        #endif
@@ -232,27 +254,25 @@ void Proc_ChangeStack()
 int Proc_Clone(Uint *Err, Uint Flags)
 {
        tThread *newThread;
+       tThread *cur = Proc_GetCurThread();
        Uint    eip, esp, ebp;
        
        __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
        __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
        
-       // Create new thread structure
-       newThread = malloc( sizeof(tThread) );
-       if(!newThread) {
-               Warning("Proc_Clone - Out of memory when creating thread\n");
-               *Err = -ENOMEM;
-               return -1;
-       }
-       // Base new thread on old
-       memcpy(newThread, gCurrentThread, sizeof(tThread));
+       newThread = Threads_CloneTCB(Err, Flags);
+       if(!newThread)  return -1;
+       
        // Initialise Memory Space (New Addr space or kernel stack)
        if(Flags & CLONE_VM) {
-               newThread->TGID = newThread->TID;
                newThread->MemState.CR3 = MM_Clone();
+               newThread->KernelStack = cur->KernelStack;
        } else {
                Uint    tmpEbp, oldEsp = esp;
 
+               // Set CR3
+               newThread->MemState.CR3 = cur->MemState.CR3;
+
                // Create new KStack
                newThread->KernelStack = MM_NewKStack();
                // Check for errors
@@ -262,35 +282,22 @@ int Proc_Clone(Uint *Err, Uint Flags)
                }
 
                // Get ESP as a used size
-               esp = gCurrentThread->KernelStack - esp;
+               esp = cur->KernelStack - esp;
                // Copy used stack
-               memcpy( (void*)(newThread->KernelStack - esp), (void*)(gCurrentThread->KernelStack - esp), esp );
+               memcpy( (void*)(newThread->KernelStack - esp), (void*)(cur->KernelStack - esp), esp );
                // Get ESP as an offset in the new stack
                esp = newThread->KernelStack - esp;
                // Adjust EBP
-               ebp = newThread->KernelStack - (gCurrentThread->KernelStack - ebp);
+               ebp = newThread->KernelStack - (cur->KernelStack - ebp);
 
                // Repair EBPs & Stack Addresses
                // Catches arguments also, but may trash stack-address-like values
                for(tmpEbp = esp; tmpEbp < newThread->KernelStack; tmpEbp += 4)
                {
-                       if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < gCurrentThread->KernelStack)
-                               *(Uint*)tmpEbp += newThread->KernelStack - gCurrentThread->KernelStack;
+                       if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < cur->KernelStack)
+                               *(Uint*)tmpEbp += newThread->KernelStack - cur->KernelStack;
                }
        }
-
-       // Set Pointer, Spinlock and TID
-       newThread->Next = NULL;
-       newThread->IsLocked = 0;
-       newThread->TID = giNextTID++;
-       newThread->PTID = gCurrentThread->TID;
-
-       // Clear message list (messages are not inherited)
-       newThread->Messages = NULL;
-       newThread->LastMessage = NULL;
-       
-       // Set remaining (sheduler expects remaining to be correct)
-       newThread->Remaining = newThread->Quantum;
        
        // Save core machine state
        newThread->SavedState.ESP = esp;
@@ -305,16 +312,55 @@ int Proc_Clone(Uint *Err, Uint Flags)
        newThread->SavedState.EIP = eip;
        
        // Lock list and add to active
-       LOCK( &giThreadListLock );
-       newThread->Next = gActiveThreads;
-       gActiveThreads = newThread;
-       giNumActiveThreads ++;
-       giTotalTickets += newThread->NumTickets;
-       RELEASE( &giThreadListLock );
+       Threads_AddActive(newThread);
        
        return newThread->TID;
 }
 
+/**
+ * \fn int Proc_SpawnWorker()
+ * \brief Spawns a new worker thread
+ */
+int Proc_SpawnWorker()
+{
+       tThread *new, *cur;
+       Uint    eip, esp, ebp;
+       
+       cur = Proc_GetCurThread();
+       
+       // Create new thread
+       new = malloc( sizeof(tThread) );
+       if(!new) {
+               Warning("Proc_SpawnWorker - Out of heap space!\n");
+               return -1;
+       }
+       memcpy(new, &gThreadZero, sizeof(tThread));
+       // Set Thread ID
+       new->TID = giNextTID++;
+       // Set kernel stack
+       new->KernelStack = MM_NewWorkerStack();
+
+       // Get ESP and EBP based in the new stack
+       __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
+       __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
+       esp = cur->KernelStack - (new->KernelStack - esp);
+       ebp = new->KernelStack - (cur->KernelStack - ebp);      
+       
+       // Save core machine state
+       new->SavedState.ESP = esp;
+       new->SavedState.EBP = ebp;
+       eip = GetEIP();
+       if(eip == SWITCH_MAGIC) {
+               outb(0x20, 0x20);       // ACK Timer and return as child
+               return 0;
+       }
+       
+       // Set EIP as parent
+       new->SavedState.EIP = eip;
+       
+       return new->TID;
+}
+
 /**
  * \fn Uint Proc_MakeUserStack()
  * \brief Creates a new user stack
@@ -340,7 +386,7 @@ Uint Proc_MakeUserStack()
 
 
 /**
- * \fn void Proc_StartUser(Uint Entrypoint, Uint Base, int ArgC, char **ArgV, char **EnvP, int DataSize)
+ * \fn void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
  * \brief Starts a user task
  */
 void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
@@ -411,6 +457,8 @@ void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **
  * \fn int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
  * \brief Demotes a process to a lower permission level
  * \param Err  Pointer to user's errno
+ * \param Dest New Permission Level
+ * \param Regs Pointer to user's register structure
  */
 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
 {
@@ -464,7 +512,7 @@ void Proc_Scheduler(int CPU)
        
        // Check if there is any tasks running
        if(giNumActiveThreads == 0) {
-               Log("No Active threads, sleeping\n");
+               Log("No Active threads, sleeping");
                __asm__ __volatile__ ("hlt");
                return;
        }
@@ -494,6 +542,14 @@ void Proc_Scheduler(int CPU)
                return;
        }
        
+       #if DEBUG_TRACE_SWITCH
+       Log("Switching to task %i, CR3 = 0x%x, EIP = %p",
+               thread->TID,
+               thread->MemState.CR3,
+               thread->SavedState.EIP
+               );
+       #endif
+       
        // Set current thread
        gCurrentThread = thread;
        

UCC git Repository :: git.ucc.asn.au