Merge branch 'master' of git://localhost/acess2
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / proc.c
index 2c59516..460c920 100644 (file)
@@ -1,7 +1,11 @@
 /*
- * AcessOS Microkernel Version
+ * Acess2 Kernel (x86)
+ * - By John Hodge (thePowersGang)
+ *
  * proc.c
+ * - Low level thread management
  */
+#define DEBUG  0
 #include <acess.h>
 #include <threads.h>
 #include <proc.h>
 #define DEBUG_DISABLE_DOUBLEFAULT      1
 #define DEBUG_VERY_SLOW_PERIOD 0
 #define DEBUG_NOPREEMPT        1
+#define DISABLE_PIT    0
 
 // === CONSTANTS ===
 // Base is 1193182
 #define TIMER_BASE      1193182
-#if DEBUG_VERY_SLOW_PERIOD
+#if DISABLE_PIT
+# define TIMER_DIVISOR 0xFFFF
+#elif DEBUG_VERY_SLOW_PERIOD
 # define TIMER_DIVISOR 1193    //~10Hz switch, with 10 quantum = 1s per thread
 #else
 # define TIMER_DIVISOR 11932   //~100Hz
@@ -206,7 +213,7 @@ void ArchThreads_Init(void)
        gProcessZero.MemState.CR3 = (Uint)gaInitPageDir - KERNEL_BASE;
        
        // Create Per-Process Data Block
-       if( !MM_Allocate(MM_PPD_CFG) )
+       if( MM_Allocate( (void*)MM_PPD_CFG ) == 0 )
        {
                Panic("OOM - No space for initial Per-Process Config");
        }
@@ -456,6 +463,14 @@ tTID Proc_NewKThread(void (*Fcn)(void*), void *Data)
        return newThread->TID;
 }
 
+#if 0
+tPID Proc_NewProcess(Uint Flags, void (*Fcn)(void*), size_t SaveSize, const void *Data)
+{
+       tThread *newThread = Threads_CloneTCB(CLONE_VM);
+       return 0;
+}
+#endif
+
 /**
  * \fn int Proc_Clone(Uint *Err, Uint Flags)
  * \brief Clone the current process
@@ -466,6 +481,7 @@ tPID Proc_Clone(Uint Flags)
        tThread *cur = Proc_GetCurThread();
        Uint    eip;
 
+       Log_Warning("Proc", "Proc_Clone is deprecated");
        // Sanity, please
        if( !(Flags & CLONE_VM) ) {
                Log_Error("Proc", "Proc_Clone: Don't leave CLONE_VM unset, use Proc_NewKThread instead");
@@ -475,6 +491,9 @@ tPID Proc_Clone(Uint Flags)
        // New thread
        newThread = Threads_CloneTCB(Flags);
        if(!newThread)  return -1;
+       ASSERT(newThread->Process);
+       //ASSERT(CheckMem(newThread->Process, sizeof(tProcess)));
+       //LOG("newThread->Process = %p", newThread->Process);
 
        newThread->KernelStack = cur->KernelStack;
 
@@ -483,6 +502,9 @@ tPID Proc_Clone(Uint Flags)
        if( eip == 0 ) {
                return 0;
        }
+       //ASSERT(newThread->Process);
+       //ASSERT(CheckMem(newThread->Process, sizeof(tProcess)));
+       //LOG("newThread->Process = %p", newThread->Process);
        newThread->SavedState.EIP = eip;
        newThread->SavedState.SSE = NULL;
        newThread->SavedState.bSSEModified = 0;
@@ -505,11 +527,12 @@ tPID Proc_Clone(Uint Flags)
  */
 tThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
 {
-       tThread *new;
        Uint    stack_contents[4];
+       LOG("(Fcn=%p,Data=%p)", Fcn, Data);
        
        // Create new thread
-       new = Threads_CloneThreadZero();
+       tThread *new = Threads_CloneThreadZero();
+       LOG("new=%p", new);
        if(!new) {
                Warning("Proc_SpawnWorker - Out of heap space!\n");
                return NULL;
@@ -523,6 +546,7 @@ tThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
        
        // Create a new worker stack (in PID0's address space)
        new->KernelStack = MM_NewWorkerStack(stack_contents, sizeof(stack_contents));
+       LOG("new->KernelStack = %p", new->KernelStack);
 
        // Save core machine state
        new->SavedState.ESP = new->KernelStack - sizeof(stack_contents);
@@ -533,6 +557,7 @@ tThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
        // Mark as active
        new->Status = THREAD_STAT_PREINIT;
        Threads_AddActive( new );
+       LOG("Added to active");
        
        return new;
 }
@@ -543,27 +568,28 @@ tThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
  */
 Uint Proc_MakeUserStack(void)
 {
-        int    i;
-       Uint    base = USER_STACK_TOP - USER_STACK_SZ;
+       tPage   *base = (void*)(USER_STACK_TOP - USER_STACK_SZ);
        
        // Check Prospective Space
-       for( i = USER_STACK_SZ >> 12; i--; )
-               if( MM_GetPhysAddr( (void*)(base + (i<<12)) ) != 0 )
-                       break;
-       
-       if(i != -1)     return 0;
-       
+       for( Uint i = USER_STACK_SZ/PAGE_SIZE; i--; )
+       {
+               if( MM_GetPhysAddr( base + i ) != 0 )
+               {
+                       Warning("Proc_MakeUserStack: Address %p in use", base + i);
+                       return 0;
+               }
+       }
        // Allocate Stack - Allocate incrementally to clean up MM_Dump output
-       for( i = 0; i < USER_STACK_SZ/0x1000; i++ )
+       for( Uint i = 0; i < USER_STACK_SZ/PAGE_SIZE; i++ )
        {
-               if( !MM_Allocate( base + (i<<12) ) )
+               if( MM_Allocate( base + i ) == 0 )
                {
                        Warning("OOM: Proc_MakeUserStack");
                        return 0;
                }
        }
        
-       return base + USER_STACK_SZ;
+       return (tVAddr)( base + USER_STACK_SZ/PAGE_SIZE );
 }
 
 void Proc_StartUser(Uint Entrypoint, Uint Base, int ArgC, const char **ArgV, int DataSize)
@@ -715,9 +741,12 @@ void Proc_DumpThreadCPUState(tThread *Thread)
                }
                
                Log("  at %04x:%08x [EAX:%x]", regs->cs, regs->eip, regs->eax);
+               Error_Backtrace(regs->eip, regs->ebp);
                return ;
        }
-       
+
+       Log(" Saved = %p (SP=%p)", Thread->SavedState.EIP, Thread->SavedState.ESP);     
+
        tVAddr  diffFromScheduler = Thread->SavedState.EIP - (tVAddr)SwitchTasks;
        tVAddr  diffFromClone = Thread->SavedState.EIP - (tVAddr)Proc_CloneInt;
        tVAddr  diffFromSpawn = Thread->SavedState.EIP - (tVAddr)NewTaskHeader;

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