*/
#include <acess.h>
#include <proc.h>
+#include <desctab.h>
#include <mm_virt.h>
#include <errno.h>
#if USE_MP
// === CONSTANTS ===
#define SWITCH_MAGIC 0xFFFACE55 // There is no code in this area
+// Base is 1193182
#define TIMER_DIVISOR 11931 //~100Hz
// === IMPORTS ===
extern tGDT gGDT[];
+extern tIDT gIDT[];
extern void APStartup(); // 16-bit AP startup code
extern Uint GetEIP(); // start.asm
extern Uint32 gaInitPageDir[1024]; // start.asm
extern void Kernel_Stack_Top;
-extern volatile int giThreadListLock;
+extern tSpinlock glThreadListLock;
extern int giNumCPUs;
extern int giNextTID;
extern int giTotalTickets;
extern tThread *Threads_GetNextToRun(int CPU);
extern void Threads_Dump();
extern tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
-extern void Isr7();
+extern void Isr8(); // Double Fault
+extern void Proc_AlterUserReturnAddr();
// === PROTOTYPES ===
void ArchThreads_Init();
tThread *Proc_GetCurThread();
void Proc_ChangeStack();
int Proc_Clone(Uint *Err, Uint Flags);
+void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP);
+void Proc_CallFaultHandler(tThread *Thread);
void Proc_Scheduler();
// === GLOBALS ===
tTSS gDoubleFault_TSS = {
.ESP0 = (Uint)&gaDoubleFaultStack[1023],
.SS0 = 0x10,
- .EIP = (Uint)Isr7
+ .CR3 = (Uint)gaInitPageDir - KERNEL_BASE,
+ .EIP = (Uint)Isr8,
+ .ESP = (Uint)&gaDoubleFaultStack[1023],
+ .CS = 0x08, .SS = 0x10,
+ .DS = 0x10, .ES = 0x10,
+ .FS = 0x10, .GS = 0x10,
};
// === CODE ===
gGDT[5].BaseMid = (Uint)&gDoubleFault_TSS >> 16;
gGDT[5].BaseHi = (Uint)&gDoubleFault_TSS >> 24;
+ Log_Debug("Proc", "gIDT[8] = {OffsetLo:%04x, CS:%04x, Flags:%04x, OffsetHi:%04x}",
+ gIDT[8].OffsetLo, gIDT[8].CS, gIDT[8].Flags, gIDT[8].OffsetHi);
+ gIDT[8].OffsetLo = 0;
+ gIDT[8].CS = 5<<3;
+ gIDT[8].Flags = 0x8500;
+ gIDT[8].OffsetHi = 0;
+ Log_Debug("Proc", "gIDT[8] = {OffsetLo:%04x, CS:%04x, Flags:%04x, OffsetHi:%04x}",
+ gIDT[8].OffsetLo, gIDT[8].CS, gIDT[8].Flags, gIDT[8].OffsetHi);
+
+ //__asm__ __volatile__ ("xchg %bx, %bx");
+
#if USE_MP
// Initialise Normal TSS(s)
for(pos=0;pos<giNumCPUs;pos++)
while(*Bases)
*--stack = *Bases++;
*--stack = 0; // Return Address
- delta = (Uint)stack; // Reuse delta to save SP
- *--stack = ss; //Stack Segment
- *--stack = delta; //Stack Pointer
- *--stack = 0x0202; //EFLAGS (Resvd (0x2) and IF (0x20))
- *--stack = cs; //Code Segment
- *--stack = Entrypoint; //EIP
+ Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
+}
+
+void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
+{
+ Uint *stack = (void*)Stack;
+ *--stack = SS; //Stack Segment
+ *--stack = Stack; //Stack Pointer
+ *--stack = Flags; //EFLAGS (Resvd (0x2) and IF (0x20))
+ *--stack = CS; //Code Segment
+ *--stack = IP; //EIP
//PUSHAD
*--stack = 0xAAAAAAAA; // eax
*--stack = 0xCCCCCCCC; // ecx
*--stack = 0x51515151; // esi
*--stack = 0xB4B4B4B4; // ebp
//Individual PUSHs
- *--stack = ss; // ds
- *--stack = ss; // es
- *--stack = ss; // fs
- *--stack = ss; // gs
+ *--stack = SS; // ds
+ *--stack = SS; // es
+ *--stack = SS; // fs
+ *--stack = SS; // gs
__asm__ __volatile__ (
"mov %%eax,%%esp;\n\t" // Set stack pointer
return 0;
}
+/**
+ * \brief Calls a signal handler in user mode
+ * \note Used for signals
+ */
+void Proc_CallFaultHandler(tThread *Thread)
+{
+ // Rewinds the stack and calls the user function
+ // Never returns
+ __asm__ __volatile__ ("mov %0, %%ebp;\n\tcall Proc_AlterUserReturnAddr" :: "r"(Thread->FaultHandler));
+ for(;;);
+}
+
/**
* \fn void Proc_Scheduler(int CPU)
* \brief Swap current thread and clears dead threads
tThread *thread;
// If the spinlock is set, let it complete
- if(giThreadListLock) return;
+ if(IS_LOCKED(&glThreadListLock)) return;
// Clear Delete Queue
while(gDeleteThreads)
#else
__asm__ __volatile__ ("mov %0, %%cr3"::"a"(thread->MemState.CR3));
#endif
+
+ #if 0
+ if(thread->SavedState.ESP > 0xC0000000
+ && thread->SavedState.ESP < thread->KernelStack-0x2000) {
+ Log_Warning("Proc", "Possible bad ESP %p (PID %i)", thread->SavedState.ESP);
+ }
+ #endif
+
// Switch threads
__asm__ __volatile__ (
"mov %1, %%esp\n\t" // Restore ESP