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

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