More work on x86_64 port of usermode
[tpg/acess2.git] / Kernel / arch / x86 / proc.c
1 /*
2  * AcessOS Microkernel Version
3  * proc.c
4  */
5 #include <acess.h>
6 #include <threads.h>
7 #include <proc.h>
8 #include <desctab.h>
9 #include <mm_virt.h>
10 #include <errno.h>
11 #if USE_MP
12 # include <mp.h>
13 #endif
14
15 // === FLAGS ===
16 #define DEBUG_TRACE_SWITCH      0
17 #define DEBUG_DISABLE_DOUBLEFAULT       1
18
19 // === CONSTANTS ===
20 #define SWITCH_MAGIC    0xFF5317C8      // FF SWITCH - There is no code in this area
21 // Base is 1193182
22 #define TIMER_BASE      1193182
23 #define TIMER_DIVISOR   11932   //~100Hz
24
25 // === TYPES ===
26 #if USE_MP
27 typedef struct sCPU
28 {
29         Uint8   APICID;
30         Uint8   State;  // 0: Unavaliable, 1: Idle, 2: Active
31         Uint16  Resvd;
32         tThread *Current;
33         tThread *IdleThread;
34 }       tCPU;
35 #endif
36
37 // === IMPORTS ===
38 extern tGDT     gGDT[];
39 extern tIDT     gIDT[];
40 extern void APWait(void);       // 16-bit AP pause code
41 extern void APStartup(void);    // 16-bit AP startup code
42 extern Uint     GetEIP(void);   // start.asm
43 extern int      GetCPUNum(void);        // start.asm
44 extern Uint32   gaInitPageDir[1024];    // start.asm
45 extern char     Kernel_Stack_Top[];
46 extern tShortSpinlock   glThreadListLock;
47 extern int      giNumCPUs;
48 extern int      giNextTID;
49 extern tThread  gThreadZero;
50 extern void     Isr8(void);     // Double Fault
51 extern void     Proc_ReturnToUser(tVAddr Handler, Uint Argument, tVAddr KernelStack);
52
53 // === PROTOTYPES ===
54 void    ArchThreads_Init(void);
55 #if USE_MP
56 void    MP_StartAP(int CPU);
57 void    MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
58 #endif
59 //void  Proc_Start(void);
60 //tThread       *Proc_GetCurThread(void);
61 void    Proc_ChangeStack(void);
62 // int  Proc_Clone(Uint *Err, Uint Flags);
63 Uint    Proc_MakeUserStack(void);
64 void    Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
65 void    Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP);
66  int    Proc_Demote(Uint *Err, int Dest, tRegs *Regs);
67 void    Proc_CallFaultHandler(tThread *Thread);
68 void    Proc_Scheduler(int CPU);
69
70 // === GLOBALS ===
71 // --- Multiprocessing ---
72 #if USE_MP
73 volatile int    giNumInitingCPUs = 0;
74 tMPInfo *gMPFloatPtr = NULL;
75 volatile Uint32 giMP_TimerCount;        // Start Count for Local APIC Timer
76 tAPIC   *gpMP_LocalAPIC = NULL;
77 Uint8   gaAPIC_to_CPU[256] = {0};
78 tCPU    gaCPUs[MAX_CPUS];
79 tTSS    gaTSSs[MAX_CPUS];       // TSS Array
80  int    giProc_BootProcessorID = 0;
81 #else
82 tThread *gCurrentThread = NULL;
83 tThread *gpIdleThread = NULL;
84 #endif
85 #if USE_PAE
86 Uint32  *gPML4s[4] = NULL;
87 #endif
88 tTSS    *gTSSs = NULL;  // Pointer to TSS array
89 tTSS    gTSS0 = {0};
90 // --- Error Recovery ---
91 char    gaDoubleFaultStack[1024] __attribute__ ((section(".padata")));
92 tTSS    gDoubleFault_TSS = {
93         .ESP0 = (Uint)&gaDoubleFaultStack[1024],
94         .SS0 = 0x10,
95         .CR3 = (Uint)gaInitPageDir - KERNEL_BASE,
96         .EIP = (Uint)Isr8,
97         .ESP = (Uint)&gaDoubleFaultStack[1024],
98         .CS = 0x08,     .SS = 0x10,
99         .DS = 0x10,     .ES = 0x10,
100         .FS = 0x10,     .GS = 0x10,
101 };
102
103 // === CODE ===
104 /**
105  * \fn void ArchThreads_Init(void)
106  * \brief Starts the process scheduler
107  */
108 void ArchThreads_Init(void)
109 {
110         Uint    pos = 0;
111         
112         #if USE_MP
113         tMPTable        *mptable;
114         
115         // Mark BSP as active
116         gaCPUs[0].State = 2;
117         
118         // -- Initialise Multiprocessing
119         // Find MP Floating Table
120         // - EBDA/Last 1Kib (640KiB)
121         for(pos = KERNEL_BASE|0x9F000; pos < (KERNEL_BASE|0xA0000); pos += 16) {
122                 if( *(Uint*)(pos) == MPPTR_IDENT ) {
123                         Log("Possible %p", pos);
124                         if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
125                         gMPFloatPtr = (void*)pos;
126                         break;
127                 }
128         }
129         // - Last KiB (512KiB base mem)
130         if(!gMPFloatPtr) {
131                 for(pos = KERNEL_BASE|0x7F000; pos < (KERNEL_BASE|0x80000); pos += 16) {
132                         if( *(Uint*)(pos) == MPPTR_IDENT ) {
133                                 Log("Possible %p", pos);
134                                 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
135                                 gMPFloatPtr = (void*)pos;
136                                 break;
137                         }
138                 }
139         }
140         // - BIOS ROM
141         if(!gMPFloatPtr) {
142                 for(pos = KERNEL_BASE|0xE0000; pos < (KERNEL_BASE|0x100000); pos += 16) {
143                         if( *(Uint*)(pos) == MPPTR_IDENT ) {
144                                 Log("Possible %p", pos);
145                                 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
146                                 gMPFloatPtr = (void*)pos;
147                                 break;
148                         }
149                 }
150         }
151         
152         // If the MP Table Exists, parse it
153         if(gMPFloatPtr)
154         {
155                  int    i;
156                 tMPTable_Ent    *ents;
157                 Log("gMPFloatPtr = %p", gMPFloatPtr);
158                 Log("*gMPFloatPtr = {");
159                 Log("\t.Sig = 0x%08x", gMPFloatPtr->Sig);
160                 Log("\t.MPConfig = 0x%08x", gMPFloatPtr->MPConfig);
161                 Log("\t.Length = 0x%02x", gMPFloatPtr->Length);
162                 Log("\t.Version = 0x%02x", gMPFloatPtr->Version);
163                 Log("\t.Checksum = 0x%02x", gMPFloatPtr->Checksum);
164                 Log("\t.Features = [0x%02x,0x%02x,0x%02x,0x%02x,0x%02x]",
165                         gMPFloatPtr->Features[0],       gMPFloatPtr->Features[1],
166                         gMPFloatPtr->Features[2],       gMPFloatPtr->Features[3],
167                         gMPFloatPtr->Features[4]
168                         );
169                 Log("}");
170                 
171                 mptable = (void*)( KERNEL_BASE|gMPFloatPtr->MPConfig );
172                 Log("mptable = %p", mptable);
173                 Log("*mptable = {");
174                 Log("\t.Sig = 0x%08x", mptable->Sig);
175                 Log("\t.BaseTableLength = 0x%04x", mptable->BaseTableLength);
176                 Log("\t.SpecRev = 0x%02x", mptable->SpecRev);
177                 Log("\t.Checksum = 0x%02x", mptable->Checksum);
178                 Log("\t.OEMID = '%8c'", mptable->OemID);
179                 Log("\t.ProductID = '%8c'", mptable->ProductID);
180                 Log("\t.OEMTablePtr = %p'", mptable->OEMTablePtr);
181                 Log("\t.OEMTableSize = 0x%04x", mptable->OEMTableSize);
182                 Log("\t.EntryCount = 0x%04x", mptable->EntryCount);
183                 Log("\t.LocalAPICMemMap = 0x%08x", mptable->LocalAPICMemMap);
184                 Log("\t.ExtendedTableLen = 0x%04x", mptable->ExtendedTableLen);
185                 Log("\t.ExtendedTableChecksum = 0x%02x", mptable->ExtendedTableChecksum);
186                 Log("}");
187                 
188                 gpMP_LocalAPIC = (void*)MM_MapHWPages(mptable->LocalAPICMemMap, 1);
189                 
190                 ents = mptable->Entries;
191                 giNumCPUs = 0;
192                 
193                 for( i = 0; i < mptable->EntryCount; i ++ )
194                 {
195                          int    entSize = 0;
196                         switch( ents->Type )
197                         {
198                         case 0: // Processor
199                                 entSize = 20;
200                                 Log("%i: Processor", i);
201                                 Log("\t.APICID = %i", ents->Proc.APICID);
202                                 Log("\t.APICVer = 0x%02x", ents->Proc.APICVer);
203                                 Log("\t.CPUFlags = 0x%02x", ents->Proc.CPUFlags);
204                                 Log("\t.CPUSignature = 0x%08x", ents->Proc.CPUSignature);
205                                 Log("\t.FeatureFlags = 0x%08x", ents->Proc.FeatureFlags);
206                                 
207                                 
208                                 if( !(ents->Proc.CPUFlags & 1) ) {
209                                         Log("DISABLED");
210                                         break;
211                                 }
212                                 
213                                 // Check if there is too many processors
214                                 if(giNumCPUs >= MAX_CPUS) {
215                                         giNumCPUs ++;   // If `giNumCPUs` > MAX_CPUS later, it will be clipped
216                                         break;
217                                 }
218                                 
219                                 // Initialise CPU Info
220                                 gaAPIC_to_CPU[ents->Proc.APICID] = giNumCPUs;
221                                 gaCPUs[giNumCPUs].APICID = ents->Proc.APICID;
222                                 gaCPUs[giNumCPUs].State = 0;
223                                 giNumCPUs ++;
224                                 
225                                 // Set BSP Variable
226                                 if( ents->Proc.CPUFlags & 2 ) {
227                                         giProc_BootProcessorID = giNumCPUs-1;
228                                 }
229                                 
230                                 break;
231                         
232                         #if DUMP_MP_TABLES
233                         case 1: // Bus
234                                 entSize = 8;
235                                 Log("%i: Bus", i);
236                                 Log("\t.ID = %i", ents->Bus.ID);
237                                 Log("\t.TypeString = '%6C'", ents->Bus.TypeString);
238                                 break;
239                         case 2: // I/O APIC
240                                 entSize = 8;
241                                 Log("%i: I/O APIC", i);
242                                 Log("\t.ID = %i", ents->IOAPIC.ID);
243                                 Log("\t.Version = 0x%02x", ents->IOAPIC.Version);
244                                 Log("\t.Flags = 0x%02x", ents->IOAPIC.Flags);
245                                 Log("\t.Addr = 0x%08x", ents->IOAPIC.Addr);
246                                 break;
247                         case 3: // I/O Interrupt Assignment
248                                 entSize = 8;
249                                 Log("%i: I/O Interrupt Assignment", i);
250                                 Log("\t.IntType = %i", ents->IOInt.IntType);
251                                 Log("\t.Flags = 0x%04x", ents->IOInt.Flags);
252                                 Log("\t.SourceBusID = 0x%02x", ents->IOInt.SourceBusID);
253                                 Log("\t.SourceBusIRQ = 0x%02x", ents->IOInt.SourceBusIRQ);
254                                 Log("\t.DestAPICID = 0x%02x", ents->IOInt.DestAPICID);
255                                 Log("\t.DestAPICIRQ = 0x%02x", ents->IOInt.DestAPICIRQ);
256                                 break;
257                         case 4: // Local Interrupt Assignment
258                                 entSize = 8;
259                                 Log("%i: Local Interrupt Assignment", i);
260                                 Log("\t.IntType = %i", ents->LocalInt.IntType);
261                                 Log("\t.Flags = 0x%04x", ents->LocalInt.Flags);
262                                 Log("\t.SourceBusID = 0x%02x", ents->LocalInt.SourceBusID);
263                                 Log("\t.SourceBusIRQ = 0x%02x", ents->LocalInt.SourceBusIRQ);
264                                 Log("\t.DestLocalAPICID = 0x%02x", ents->LocalInt.DestLocalAPICID);
265                                 Log("\t.DestLocalAPICIRQ = 0x%02x", ents->LocalInt.DestLocalAPICIRQ);
266                                 break;
267                         default:
268                                 Log("%i: Unknown (%i)", i, ents->Type);
269                                 break;
270                         #endif
271                         }
272                         ents = (void*)( (Uint)ents + entSize );
273                 }
274                 
275                 if( giNumCPUs > MAX_CPUS ) {
276                         Warning("Too many CPUs detected (%i), only using %i of them", giNumCPUs, MAX_CPUS);
277                         giNumCPUs = MAX_CPUS;
278                 }
279                 gTSSs = gaTSSs;
280         }
281         else {
282                 Log("No MP Table was found, assuming uniprocessor\n");
283                 giNumCPUs = 1;
284                 gTSSs = &gTSS0;
285         }
286         #else
287         giNumCPUs = 1;
288         gTSSs = &gTSS0;
289         MM_FinishVirtualInit();
290         #endif
291         
292         #if !DEBUG_DISABLE_DOUBLEFAULT
293         // Initialise Double Fault TSS
294         gGDT[5].BaseLow = (Uint)&gDoubleFault_TSS & 0xFFFF;
295         gGDT[5].BaseMid = (Uint)&gDoubleFault_TSS >> 16;
296         gGDT[5].BaseHi = (Uint)&gDoubleFault_TSS >> 24;
297         
298         // Set double fault IDT to use the new TSS
299         gIDT[8].OffsetLo = 0;
300         gIDT[8].CS = 5<<3;
301         gIDT[8].Flags = 0x8500;
302         gIDT[8].OffsetHi = 0;
303         #endif
304         
305         // Set timer frequency
306         outb(0x43, 0x34);       // Set Channel 0, Low/High, Rate Generator
307         outb(0x40, TIMER_DIVISOR&0xFF); // Low Byte of Divisor
308         outb(0x40, (TIMER_DIVISOR>>8)&0xFF);    // High Byte
309         
310         Log("Timer Frequency %i.%03i Hz",
311                 TIMER_BASE/TIMER_DIVISOR,
312                 ((Uint64)TIMER_BASE*1000/TIMER_DIVISOR)%1000
313                 );
314         
315         #if USE_MP
316         // Get the count setting for APIC timer
317         Log("Determining APIC Count");
318         __asm__ __volatile__ ("sti");
319         while( giMP_TimerCount == 0 )   __asm__ __volatile__ ("hlt");
320         __asm__ __volatile__ ("cli");
321         Log("APIC Count %i", giMP_TimerCount);
322         {
323                 Uint64  freq = giMP_TimerCount;
324                 freq /= TIMER_DIVISOR;
325                 freq *= TIMER_BASE;
326                 if( (freq /= 1000) < 2*1000)
327                         Log("Bus Frequency %i KHz", freq);
328                 else if( (freq /= 1000) < 2*1000)
329                         Log("Bus Frequency %i MHz", freq);
330                 else if( (freq /= 1000) < 2*1000)
331                         Log("Bus Frequency %i GHz", freq);
332                 else
333                         Log("Bus Frequency %i THz", freq);
334         }
335         
336         // Initialise Normal TSS(s)
337         for(pos=0;pos<giNumCPUs;pos++)
338         {
339         #else
340         pos = 0;
341         #endif
342                 gTSSs[pos].SS0 = 0x10;
343                 gTSSs[pos].ESP0 = 0;    // Set properly by scheduler
344                 gGDT[6+pos].BaseLow = ((Uint)(&gTSSs[pos])) & 0xFFFF;
345                 gGDT[6+pos].BaseMid = ((Uint)(&gTSSs[pos]) >> 16) & 0xFFFF;
346                 gGDT[6+pos].BaseHi = ((Uint)(&gTSSs[pos])) >> 24;
347         #if USE_MP
348         }
349         #endif
350         
351         // Load the BSP's TSS
352         __asm__ __volatile__ ("ltr %%ax"::"a"(0x30));
353         // Set Current Thread and CPU Number in DR0 and DR1
354         __asm__ __volatile__ ("mov %0, %%db0"::"r"(&gThreadZero));
355         __asm__ __volatile__ ("mov %0, %%db1"::"r"(0));
356         
357         #if USE_MP
358         gaCPUs[0].Current = &gThreadZero;
359         #else
360         gCurrentThread = &gThreadZero;
361         #endif
362         gThreadZero.CurCPU = 0;
363         
364         #if USE_PAE
365         gThreadZero.MemState.PDP[0] = 0;
366         gThreadZero.MemState.PDP[1] = 0;
367         gThreadZero.MemState.PDP[2] = 0;
368         #else
369         gThreadZero.MemState.CR3 = (Uint)gaInitPageDir - KERNEL_BASE;
370         #endif
371         
372         // Create Per-Process Data Block
373         if( !MM_Allocate(MM_PPD_CFG) )
374         {
375                 Panic("OOM - No space for initiali Per-Process Config");
376         }
377         
378         // Change Stacks
379         Proc_ChangeStack();
380 }
381
382 #if USE_MP
383 void MP_StartAP(int CPU)
384 {
385         Log("Starting AP %i (APIC %i)", CPU, gaCPUs[CPU].APICID);
386         
387         // Set location of AP startup code and mark for a warm restart
388         *(Uint16*)(KERNEL_BASE|0x467) = (Uint)&APWait - (KERNEL_BASE|0xFFFF0);
389         *(Uint16*)(KERNEL_BASE|0x469) = 0xFFFF;
390         outb(0x70, 0x0F);       outb(0x71, 0x0A);       // Set warm reset flag
391         MP_SendIPI(gaCPUs[CPU].APICID, 0, 5);   // Init IPI
392         
393         // Delay
394         inb(0x80); inb(0x80); inb(0x80); inb(0x80);
395         
396         // TODO: Use a better address, preferably registered with the MM
397         // - MM_AllocDMA mabye?
398         // Create a far jump
399         *(Uint8*)(KERNEL_BASE|0x11000) = 0xEA;  // Far JMP
400         *(Uint16*)(KERNEL_BASE|0x11001) = (Uint)&APStartup - (KERNEL_BASE|0xFFFF0);     // IP
401         *(Uint16*)(KERNEL_BASE|0x11003) = 0xFFFF;       // CS
402         // Send a Startup-IPI to make the CPU execute at 0x11000 (which we
403         // just filled)
404         MP_SendIPI(gaCPUs[CPU].APICID, 0x11, 6);        // StartupIPI
405         
406         giNumInitingCPUs ++;
407 }
408
409 /**
410  * \brief Send an Inter-Processor Interrupt
411  * \param APICID        Processor's Local APIC ID
412  * \param Vector        Argument of some kind
413  * \param DeliveryMode  Type of signal?
414  */
415 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode)
416 {
417         Uint32  val;
418         
419         // Hi
420         val = (Uint)APICID << 24;
421         Log("*%p = 0x%08x", &gpMP_LocalAPIC->ICR[1], val);
422         gpMP_LocalAPIC->ICR[1].Val = val;
423         // Low (and send)
424         val = ((DeliveryMode & 7) << 8) | (Vector & 0xFF);
425         Log("*%p = 0x%08x", &gpMP_LocalAPIC->ICR[0], val);
426         gpMP_LocalAPIC->ICR[0].Val = val;
427 }
428 #endif
429
430 /**
431  * \fn void Proc_Start(void)
432  * \brief Start process scheduler
433  */
434 void Proc_Start(void)
435 {
436         #if USE_MP
437          int    i;
438         #endif
439         
440         #if USE_MP
441         // Start APs
442         for( i = 0; i < giNumCPUs; i ++ )
443         {
444                  int    tid;
445                 if(i)   gaCPUs[i].Current = NULL;
446                 
447                 // Create Idle Task
448                 if( (tid = Proc_Clone(0, 0)) == 0)
449                 {
450                         for(;;) HALT(); // Just yeilds
451                 }
452                 gaCPUs[i].IdleThread = Threads_GetThread(tid);
453                 gaCPUs[i].IdleThread->ThreadName = (char*)"Idle Thread";
454                 Threads_SetPriority( gaCPUs[i].IdleThread, -1 );        // Never called randomly
455                 gaCPUs[i].IdleThread->Quantum = 1;      // 1 slice quantum
456                 
457                 
458                 // Start the AP
459                 if( i != giProc_BootProcessorID ) {
460                         MP_StartAP( i );
461                 }
462         }
463         
464         // BSP still should run the current task
465         gaCPUs[0].Current = &gThreadZero;
466         
467         // Start interrupts and wait for APs to come up
468         Log("Waiting for APs to come up\n");
469         __asm__ __volatile__ ("sti");
470         while( giNumInitingCPUs )       __asm__ __volatile__ ("hlt");
471         #else
472         // Create Idle Task
473         if(Proc_Clone(0, 0) == 0)
474         {
475                 gpIdleThread = Proc_GetCurThread();
476                 gpIdleThread->ThreadName = strdup("Idle Thread");
477                 Threads_SetPriority( gpIdleThread, -1 );        // Never called randomly
478                 gpIdleThread->Quantum = 1;      // 1 slice quantum
479                 for(;;) HALT(); // Just yeilds
480         }
481         
482         // Set current task
483         gCurrentThread = &gThreadZero;
484         
485         // Start Interrupts (and hence scheduler)
486         __asm__ __volatile__("sti");
487         #endif
488         MM_FinishVirtualInit();
489 }
490
491 /**
492  * \fn tThread *Proc_GetCurThread(void)
493  * \brief Gets the current thread
494  */
495 tThread *Proc_GetCurThread(void)
496 {
497         #if USE_MP
498         return gaCPUs[ GetCPUNum() ].Current;
499         #else
500         return gCurrentThread;
501         #endif
502 }
503
504 /**
505  * \fn void Proc_ChangeStack(void)
506  * \brief Swaps the current stack for a new one (in the proper stack reigon)
507  */
508 void Proc_ChangeStack(void)
509 {
510         Uint    esp, ebp;
511         Uint    tmpEbp, oldEsp;
512         Uint    curBase, newBase;
513
514         __asm__ __volatile__ ("mov %%esp, %0":"=r"(esp));
515         __asm__ __volatile__ ("mov %%ebp, %0":"=r"(ebp));
516
517         oldEsp = esp;
518
519         // Create new KStack
520         newBase = MM_NewKStack();
521         // Check for errors
522         if(newBase == 0) {
523                 Panic("What the?? Unable to allocate space for initial kernel stack");
524                 return;
525         }
526
527         curBase = (Uint)&Kernel_Stack_Top;
528         
529         LOG("curBase = 0x%x, newBase = 0x%x", curBase, newBase);
530
531         // Get ESP as a used size
532         esp = curBase - esp;
533         LOG("memcpy( %p, %p, 0x%x )", (void*)(newBase - esp), (void*)(curBase - esp), esp );
534         // Copy used stack
535         memcpy( (void*)(newBase - esp), (void*)(curBase - esp), esp );
536         // Get ESP as an offset in the new stack
537         esp = newBase - esp;
538         // Adjust EBP
539         ebp = newBase - (curBase - ebp);
540
541         // Repair EBPs & Stack Addresses
542         // Catches arguments also, but may trash stack-address-like values
543         for(tmpEbp = esp; tmpEbp < newBase; tmpEbp += 4)
544         {
545                 if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < curBase)
546                         *(Uint*)tmpEbp += newBase - curBase;
547         }
548         
549         Proc_GetCurThread()->KernelStack = newBase;
550         
551         __asm__ __volatile__ ("mov %0, %%esp"::"r"(esp));
552         __asm__ __volatile__ ("mov %0, %%ebp"::"r"(ebp));
553 }
554
555 /**
556  * \fn int Proc_Clone(Uint *Err, Uint Flags)
557  * \brief Clone the current process
558  */
559 int Proc_Clone(Uint *Err, Uint Flags)
560 {
561         tThread *newThread;
562         tThread *cur = Proc_GetCurThread();
563         Uint    eip, esp, ebp;
564         
565         __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
566         __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
567         
568         newThread = Threads_CloneTCB(Err, Flags);
569         if(!newThread)  return -1;
570         
571         // Initialise Memory Space (New Addr space or kernel stack)
572         if(Flags & CLONE_VM) {
573                 newThread->MemState.CR3 = MM_Clone();
574                 // Check for errors
575                 if(newThread->MemState.CR3 == 0) {
576                         Threads_Kill(newThread, -2);
577                         return -1;
578                 }
579                 newThread->KernelStack = cur->KernelStack;
580         } else {
581                 Uint    tmpEbp, oldEsp = esp;
582
583                 // Set CR3
584                 #if USE_PAE
585                 # warning "PAE Unimplemented"
586                 #else
587                 newThread->MemState.CR3 = cur->MemState.CR3;
588                 #endif
589
590                 // Create new KStack
591                 newThread->KernelStack = MM_NewKStack();
592                 // Check for errors
593                 if(newThread->KernelStack == 0) {
594                         Threads_Kill(newThread, -2);
595                         return -1;
596                 }
597
598                 // Get ESP as a used size
599                 esp = cur->KernelStack - esp;
600                 // Copy used stack
601                 memcpy( (void*)(newThread->KernelStack - esp), (void*)(cur->KernelStack - esp), esp );
602                 // Get ESP as an offset in the new stack
603                 esp = newThread->KernelStack - esp;
604                 // Adjust EBP
605                 ebp = newThread->KernelStack - (cur->KernelStack - ebp);
606
607                 // Repair EBPs & Stack Addresses
608                 // Catches arguments also, but may trash stack-address-like values
609                 for(tmpEbp = esp; tmpEbp < newThread->KernelStack; tmpEbp += 4)
610                 {
611                         if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < cur->KernelStack)
612                                 *(Uint*)tmpEbp += newThread->KernelStack - cur->KernelStack;
613                 }
614         }
615         
616         // Save core machine state
617         newThread->SavedState.ESP = esp;
618         newThread->SavedState.EBP = ebp;
619         eip = GetEIP();
620         if(eip == SWITCH_MAGIC) {
621                 __asm__ __volatile__ ("mov %0, %%db0" : : "r" (newThread) );
622                 #if USE_MP
623                 // ACK the interrupt
624                 if( GetCPUNum() )
625                         gpMP_LocalAPIC->EOI.Val = 0;
626                 else
627                 #endif
628                         outb(0x20, 0x20);       // ACK Timer and return as child
629                 __asm__ __volatile__ ("sti");   // Restart interrupts
630                 return 0;
631         }
632         
633         // Set EIP as parent
634         newThread->SavedState.EIP = eip;
635         
636         // Lock list and add to active
637         Threads_AddActive(newThread);
638         
639         return newThread->TID;
640 }
641
642 /**
643  * \fn int Proc_SpawnWorker(void)
644  * \brief Spawns a new worker thread
645  */
646 int Proc_SpawnWorker(void)
647 {
648         tThread *new, *cur;
649         Uint    eip, esp, ebp;
650         
651         cur = Proc_GetCurThread();
652         
653         // Create new thread
654         new = Threads_CloneThreadZero();
655         if(!new) {
656                 Warning("Proc_SpawnWorker - Out of heap space!\n");
657                 return -1;
658         }
659         // Create a new worker stack (in PID0's address space)
660         // - The stack is relocated by this function
661         new->KernelStack = MM_NewWorkerStack();
662
663         // Get ESP and EBP based in the new stack
664         __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
665         __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
666         esp = new->KernelStack - (cur->KernelStack - esp);
667         ebp = new->KernelStack - (cur->KernelStack - ebp);      
668         
669         // Save core machine state
670         new->SavedState.ESP = esp;
671         new->SavedState.EBP = ebp;
672         eip = GetEIP();
673         if(eip == SWITCH_MAGIC) {
674                 __asm__ __volatile__ ("mov %0, %%db0" : : "r"(new));
675                 #if USE_MP
676                 // ACK the interrupt
677                 if(GetCPUNum())
678                         gpMP_LocalAPIC->EOI.Val = 0;
679                 else
680                 #endif
681                         outb(0x20, 0x20);       // ACK Timer and return as child
682                 __asm__ __volatile__ ("sti");   // Restart interrupts
683                 return 0;
684         }
685         
686         // Set EIP as parent
687         new->SavedState.EIP = eip;
688         // Mark as active
689         Threads_AddActive( new );
690         
691         return new->TID;
692 }
693
694 /**
695  * \fn Uint Proc_MakeUserStack(void)
696  * \brief Creates a new user stack
697  */
698 Uint Proc_MakeUserStack(void)
699 {
700          int    i;
701         Uint    base = USER_STACK_TOP - USER_STACK_SZ;
702         
703         // Check Prospective Space
704         for( i = USER_STACK_SZ >> 12; i--; )
705                 if( MM_GetPhysAddr( base + (i<<12) ) != 0 )
706                         break;
707         
708         if(i != -1)     return 0;
709         
710         // Allocate Stack - Allocate incrementally to clean up MM_Dump output
711         for( i = 0; i < USER_STACK_SZ/0x1000; i++ )
712         {
713                 if( !MM_Allocate( base + (i<<12) ) )
714                 {
715                         Warning("OOM: Proc_MakeUserStack");
716                         return 0;
717                 }
718         }
719         
720         return base + USER_STACK_SZ;
721 }
722
723 /**
724  * \fn void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
725  * \brief Starts a user task
726  */
727 void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
728 {
729         Uint    *stack = (void*)Proc_MakeUserStack();
730          int    i;
731         Uint    delta;
732         Uint16  ss, cs;
733         
734         //Log("stack = %p", stack);
735         
736         // Copy Arguments
737         stack -= DataSize/sizeof(*stack);
738         memcpy( stack, ArgV, DataSize );
739         
740         //Log("stack = %p", stack);
741         
742         if( DataSize )
743         {
744                 // Adjust Arguments and environment
745                 delta = (Uint)stack - (Uint)ArgV;
746                 ArgV = (char**)stack;
747                 for( i = 0; ArgV[i]; i++ )
748                         ArgV[i] += delta;
749                 i ++;
750                 
751                 // Do we care about EnvP?
752                 if( EnvP ) {
753                         EnvP = &ArgV[i];
754                         for( i = 0; EnvP[i]; i++ )
755                                 EnvP[i] += delta;
756                 }
757         }
758         
759         // User Mode Segments
760         ss = 0x23;      cs = 0x1B;
761         
762         // Arguments
763         *--stack = (Uint)EnvP;
764         *--stack = (Uint)ArgV;
765         *--stack = (Uint)ArgC;
766         while(*Bases)
767                 *--stack = *Bases++;
768         *--stack = 0;   // Return Address
769         
770         Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
771 }
772
773 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
774 {
775         Uint    *stack = (void*)Stack;
776         *--stack = SS;          //Stack Segment
777         *--stack = Stack;       //Stack Pointer
778         *--stack = Flags;       //EFLAGS (Resvd (0x2) and IF (0x20))
779         *--stack = CS;          //Code Segment
780         *--stack = IP;  //EIP
781         //PUSHAD
782         *--stack = 0xAAAAAAAA;  // eax
783         *--stack = 0xCCCCCCCC;  // ecx
784         *--stack = 0xDDDDDDDD;  // edx
785         *--stack = 0xBBBBBBBB;  // ebx
786         *--stack = 0xD1D1D1D1;  // edi
787         *--stack = 0x54545454;  // esp - NOT POPED
788         *--stack = 0x51515151;  // esi
789         *--stack = 0xB4B4B4B4;  // ebp
790         //Individual PUSHs
791         *--stack = SS;  // ds
792         *--stack = SS;  // es
793         *--stack = SS;  // fs
794         *--stack = SS;  // gs
795         
796         __asm__ __volatile__ (
797         "mov %%eax,%%esp;\n\t"  // Set stack pointer
798         "pop %%gs;\n\t"
799         "pop %%fs;\n\t"
800         "pop %%es;\n\t"
801         "pop %%ds;\n\t"
802         "popa;\n\t"
803         "iret;\n\t" : : "a" (stack));
804         for(;;);
805 }
806
807 /**
808  * \fn int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
809  * \brief Demotes a process to a lower permission level
810  * \param Err   Pointer to user's errno
811  * \param Dest  New Permission Level
812  * \param Regs  Pointer to user's register structure
813  */
814 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
815 {
816          int    cpl = Regs->cs & 3;
817         // Sanity Check
818         if(Dest > 3 || Dest < 0) {
819                 *Err = -EINVAL;
820                 return -1;
821         }
822         
823         // Permission Check
824         if(cpl > Dest) {
825                 *Err = -EACCES;
826                 return -1;
827         }
828         
829         // Change the Segment Registers
830         Regs->cs = (((Dest+1)<<4) | Dest) - 8;
831         Regs->ss = ((Dest+1)<<4) | Dest;
832         // Check if the GP Segs are GDT, then change them
833         if(!(Regs->ds & 4))     Regs->ds = ((Dest+1)<<4) | Dest;
834         if(!(Regs->es & 4))     Regs->es = ((Dest+1)<<4) | Dest;
835         if(!(Regs->fs & 4))     Regs->fs = ((Dest+1)<<4) | Dest;
836         if(!(Regs->gs & 4))     Regs->gs = ((Dest+1)<<4) | Dest;
837         
838         return 0;
839 }
840
841 /**
842  * \brief Calls a signal handler in user mode
843  * \note Used for signals
844  */
845 void Proc_CallFaultHandler(tThread *Thread)
846 {
847         // Rewinds the stack and calls the user function
848         // Never returns
849         Proc_ReturnToUser( Thread->FaultHandler, Thread->CurFaultNum, Thread->KernelStack );
850         for(;;);
851 }
852
853 /**
854  * \fn void Proc_Scheduler(int CPU)
855  * \brief Swap current thread and clears dead threads
856  */
857 void Proc_Scheduler(int CPU)
858 {
859         Uint    esp, ebp, eip;
860         tThread *thread;
861         
862         // If the spinlock is set, let it complete
863         if(IS_LOCKED(&glThreadListLock))        return;
864         
865         // Get current thread
866         #if USE_MP
867         thread = gaCPUs[CPU].Current;
868         #else
869         thread = gCurrentThread;
870         #endif
871         
872         if( thread )
873         {
874                 // Reduce remaining quantum and continue timeslice if non-zero
875                 if( thread->Remaining-- )
876                         return;
877                 // Reset quantum for next call
878                 thread->Remaining = thread->Quantum;
879                 
880                 // Get machine state
881                 __asm__ __volatile__ ( "mov %%esp, %0" : "=r" (esp) );
882                 __asm__ __volatile__ ( "mov %%ebp, %0" : "=r" (ebp) );
883                 eip = GetEIP();
884                 if(eip == SWITCH_MAGIC) return; // Check if a switch happened
885                 
886                 // Save machine state
887                 thread->SavedState.ESP = esp;
888                 thread->SavedState.EBP = ebp;
889                 thread->SavedState.EIP = eip;
890         }
891         
892         // Get next thread to run
893         thread = Threads_GetNextToRun(CPU, thread);
894         
895         // No avaliable tasks, just go into low power mode (idle thread)
896         if(thread == NULL) {
897                 #if USE_MP
898                 thread = gaCPUs[CPU].IdleThread;
899                 Log("CPU %i Running Idle Thread", CPU);
900                 #else
901                 thread = gpIdleThread;
902                 #endif
903         }
904         
905         // Set current thread
906         #if USE_MP
907         gaCPUs[CPU].Current = thread;
908         #else
909         gCurrentThread = thread;
910         #endif
911         
912         #if DEBUG_TRACE_SWITCH
913         Log("Switching to task %i, CR3 = 0x%x, EIP = %p",
914                 thread->TID,
915                 thread->MemState.CR3,
916                 thread->SavedState.EIP
917                 );
918         #endif
919         
920         #if USE_MP      // MP Debug
921         Log("CPU = %i, Thread %p", CPU, thread);
922         #endif
923         
924         // Update Kernel Stack pointer
925         gTSSs[CPU].ESP0 = thread->KernelStack-4;
926         
927         #if 0
928         if(thread->SavedState.ESP > 0xC0000000
929         && thread->SavedState.ESP < thread->KernelStack-0x2000) {
930                 Log_Warning("Proc", "Possible bad ESP %p (PID %i)", thread->SavedState.ESP);
931         }
932         #endif
933         
934         #if USE_PAE
935         # error "Todo: Implement PAE Address space switching"
936         #else
937         // Switch threads
938         __asm__ __volatile__ (
939                 "mov %4, %%cr3\n\t"     // Set address space
940                 "mov %1, %%esp\n\t"     // Restore ESP
941                 "mov %2, %%ebp\n\t"     // and EBP
942                 "jmp *%3" : :   // And return to where we saved state (Proc_Clone or Proc_Scheduler)
943                 "a"(SWITCH_MAGIC), "b"(thread->SavedState.ESP),
944                 "d"(thread->SavedState.EBP), "c"(thread->SavedState.EIP),
945                 "r"(thread->MemState.CR3)
946                 );
947         #endif
948         for(;;);        // Shouldn't reach here
949 }
950
951 // === EXPORTS ===
952 EXPORT(Proc_SpawnWorker);

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