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

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