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

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