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

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