#include <acess.h>
#include <proc.h>
#include <threads.h>
+#include <threads_int.h>
#include <desctab.h>
#include <mm_virt.h>
#include <errno.h>
extern Uint GetRIP(void); // start.asm
extern Uint64 gInitialPML4[512]; // start.asm
extern char gInitialKernelStack[];
-extern tSpinlock glThreadListLock;
+extern tShortSpinlock glThreadListLock;
extern int giNumCPUs;
extern int giNextTID;
extern int giTotalTickets;
extern int giNumActiveThreads;
extern tThread gThreadZero;
-//extern tThread *Threads_GetNextToRun(int CPU);
extern void Threads_Dump(void);
-extern tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
extern void Proc_ReturnToUser(void);
extern int GetCPUNum(void);
void MP_StartAP(int CPU);
void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
#endif
-void Proc_Start(void);
-tThread *Proc_GetCurThread(void);
+//void Proc_Start(void);
+//tThread *Proc_GetCurThread(void);
void Proc_ChangeStack(void);
- int Proc_Clone(Uint *Err, Uint Flags);
+// int Proc_Clone(Uint *Err, Uint Flags);
+// int Proc_SpawnWorker(void);
+Uint Proc_MakeUserStack(void);
+void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP);
+ int Proc_Demote(Uint *Err, int Dest, tRegs *Regs);
void Proc_CallFaultHandler(tThread *Thread);
+void Proc_DumpThreadCPUState(tThread *Thread);
void Proc_Scheduler(int CPU);
// === GLOBALS ===
}
#endif
+ // Set Debug registers
+ __asm__ __volatile__ ("mov %0, %%db0" : : "r"(&gThreadZero));
+ __asm__ __volatile__ ("mov %%rax, %%db1" : : "a"(0));
+
gaCPUs[0].Current = &gThreadZero;
gThreadZero.MemState.CR3 = (Uint)gInitialPML4 - KERNEL_BASE;
+ gThreadZero.CurCPU = 0;
// Set timer frequency
outb(0x43, 0x34); // Set Channel 0, Low/High, Rate Generator
outb(0x40, (TIMER_DIVISOR>>8)&0xFF); // High Byte
// Create Per-Process Data Block
- MM_Allocate(MM_PPD_CFG);
+ if( !MM_Allocate(MM_PPD_CFG) )
+ {
+ Warning("Oh, hell, Unable to allocate PPD for Thread#0");
+ }
// Change Stacks
Proc_ChangeStack();
+
+ Log("Multithreading initialised");
}
#if USE_MP
if(Proc_Clone(0, 0) == 0)
{
gaCPUs[0].IdleThread = Proc_GetCurThread();
- gaCPUs[0].IdleThread->ThreadName = "Idle Thread";
- gaCPUs[0].IdleThread->NumTickets = 0; // Never called randomly
+ gaCPUs[0].IdleThread->ThreadName = (char*)"Idle Thread";
+ Threads_SetPriority( gaCPUs[0].IdleThread, -1 ); // Never called randomly
gaCPUs[0].IdleThread->Quantum = 1; // 1 slice quantum
for(;;) HALT(); // Just yeilds
}
// Set current task
gaCPUs[0].Current = &gThreadZero;
+ gaCPUs[0].Current->CurCPU = 0;
// Start Interrupts (and hence scheduler)
__asm__ __volatile__("sti");
#endif
MM_FinishVirtualInit();
+ Log("Multithreading started");
}
/**
if(i != -1) return 0;
// Allocate Stack - Allocate incrementally to clean up MM_Dump output
- for( i = 0; i < USER_STACK_SZ/4069; i++ )
- MM_Allocate( base + (i<<12) );
+ for( i = 0; i < USER_STACK_SZ/0x1000; i++ )
+ {
+ if( !MM_Allocate( base + (i<<12) ) )
+ {
+ // Error
+ Log_Error("Proc", "Unable to allocate user stack (%i pages requested)", USER_STACK_SZ/0x1000);
+ while( i -- )
+ MM_Deallocate( base + (i<<12) );
+ return 0;
+ }
+ }
return base + USER_STACK_SZ;
}
for(;;);
}
+void Proc_DumpThreadCPUState(tThread *Thread)
+{
+}
+
/**
* \fn void Proc_Scheduler(int CPU)
* \brief Swap current thread and clears dead threads
// If the spinlock is set, let it complete
if(IS_LOCKED(&glThreadListLock)) return;
-
+
// Get current thread
thread = gaCPUs[CPU].Current;
// Error Check
if(thread == NULL) {
thread = gaCPUs[CPU].IdleThread;
- Warning("Hmm... Threads_GetNextToRun returned NULL, I don't think this should happen.\n");
+ //Warning("Hmm... Threads_GetNextToRun returned NULL, I don't think this should happen.\n");
+ //LogF("Zzzzz.\n");
return;
}
#if DEBUG_TRACE_SWITCH
- Log("Switching to task %i, CR3 = 0x%x, RIP = %p",
+ LogF("Switching to task %i, CR3 = 0x%x, RIP = %p",
thread->TID,
thread->MemState.CR3,
thread->SavedState.RIP
);
#endif
+
+ if(CPU > MAX_CPUS)
+ LogF("CPU = %i", CPU);
// Set current thread
gaCPUs[CPU].Current = thread;