Major build system changes
[tpg/acess2.git] / Kernel / arch / x86_64 / proc.c
1 /*
2  * Acess2 x86_64 port
3  * proc.c
4  */
5 #include <acess.h>
6 #include <proc.h>
7 #include <threads.h>
8 #include <threads_int.h>
9 #include <desctab.h>
10 #include <mm_virt.h>
11 #include <errno.h>
12 #if USE_MP
13 # include <mp.h>
14 #endif
15 #include <arch_config.h>
16 #include <hal_proc.h>
17
18 // === FLAGS ===
19 #define DEBUG_TRACE_SWITCH      0
20 #define BREAK_ON_SWITCH         0       // Break into bochs debugger on a task switch
21
22 // === CONSTANTS ===
23
24 // === TYPES ===
25 typedef struct sCPU
26 {
27         Uint8   APICID;
28         Uint8   State;  // 0: Unavaliable, 1: Idle, 2: Active
29         Uint16  Resvd;
30         tThread *Current;
31         tThread *IdleThread;
32 }       tCPU;
33
34 // === IMPORTS ===
35 extern tGDT     gGDT[];
36 extern void     APStartup(void);        // 16-bit AP startup code
37
38 extern Uint     GetRIP(void);   // start.asm
39 extern Uint     SaveState(Uint *RSP, Uint *Regs);
40 extern Uint     Proc_CloneInt(Uint *RSP, Uint *CR3);
41 extern void     NewTaskHeader(void);    // Actually takes cdecl args
42
43 extern Uint64   gInitialPML4[512];      // start.asm
44 extern tShortSpinlock   glThreadListLock;
45 extern int      giNumCPUs;
46 extern int      giNextTID;
47 extern int      giTotalTickets;
48 extern int      giNumActiveThreads;
49 extern tThread  gThreadZero;
50 extern void     Threads_Dump(void);
51 extern void     Proc_ReturnToUser(tVAddr Handler, tVAddr KStackTop, int Argument);
52 extern void     Time_UpdateTimestamp(void);
53 extern void     SwitchTasks(Uint NewSP, Uint *OldSP, Uint NewIP, Uint *OldIO, Uint CR3);
54
55 // === PROTOTYPES ===
56 //void  ArchThreads_Init(void);
57 #if USE_MP
58 void    MP_StartAP(int CPU);
59 void    MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
60 #endif
61 void    Proc_IdleTask(void *unused);
62 //void  Proc_Start(void);
63 //tThread       *Proc_GetCurThread(void);
64  int    Proc_NewKThread(void (*Fcn)(void*), void *Data);
65 // int  Proc_Clone(Uint *Err, Uint Flags);
66 // int  Proc_SpawnWorker(void);
67 Uint    Proc_MakeUserStack(void);
68 //void  Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
69 void    Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP) NORETURN;
70  int    Proc_Demote(Uint *Err, int Dest, tRegs *Regs);
71 //void  Proc_CallFaultHandler(tThread *Thread);
72 //void  Proc_DumpThreadCPUState(tThread *Thread);
73 //void  Proc_Reschedule(void);
74 void    Proc_Scheduler(int CPU, Uint RSP, Uint RIP);
75
76 // === GLOBALS ===
77 //!\brief Used by desctab.asm in SyscallStub
78 const int ci_offsetof_tThread_KernelStack = offsetof(tThread, KernelStack);
79 // --- Multiprocessing ---
80 #if USE_MP
81 volatile int    giNumInitingCPUs = 0;
82 tMPInfo *gMPFloatPtr = NULL;
83 tAPIC   *gpMP_LocalAPIC = NULL;
84 Uint8   gaAPIC_to_CPU[256] = {0};
85 #endif
86 tCPU    gaCPUs[MAX_CPUS];
87 tTSS    *gTSSs = NULL;
88 tTSS    gTSS0 = {0};
89 // --- Error Recovery ---
90 Uint32  gaDoubleFaultStack[1024];
91
92 // === CODE ===
93 /**
94  * \fn void ArchThreads_Init(void)
95  * \brief Starts the process scheduler
96  */
97 void ArchThreads_Init(void)
98 {
99         Uint    pos = 0;
100         
101         #if USE_MP
102         tMPTable        *mptable;
103         
104         // Mark BSP as active
105         gaCPUs[0].State = 2;
106         
107         // -- Initialise Multiprocessing
108         // Find MP Floating Table
109         // - EBDA/Last 1Kib (640KiB)
110         for(pos = KERNEL_BASE|0x9F000; pos < (KERNEL_BASE|0xA0000); pos += 16) {
111                 if( *(Uint*)(pos) == MPPTR_IDENT ) {
112                         Log("Possible %p", pos);
113                         if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
114                         gMPFloatPtr = (void*)pos;
115                         break;
116                 }
117         }
118         // - Last KiB (512KiB base mem)
119         if(!gMPFloatPtr) {
120                 for(pos = KERNEL_BASE|0x7F000; pos < (KERNEL_BASE|0x80000); pos += 16) {
121                         if( *(Uint*)(pos) == MPPTR_IDENT ) {
122                                 Log("Possible %p", pos);
123                                 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
124                                 gMPFloatPtr = (void*)pos;
125                                 break;
126                         }
127                 }
128         }
129         // - BIOS ROM
130         if(!gMPFloatPtr) {
131                 for(pos = KERNEL_BASE|0xE0000; pos < (KERNEL_BASE|0x100000); pos += 16) {
132                         if( *(Uint*)(pos) == MPPTR_IDENT ) {
133                                 Log("Possible %p", pos);
134                                 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
135                                 gMPFloatPtr = (void*)pos;
136                                 break;
137                         }
138                 }
139         }
140         
141         // If the MP Table Exists, parse it
142         if(gMPFloatPtr)
143         {
144                  int    i;
145                 tMPTable_Ent    *ents;
146                 Log("gMPFloatPtr = %p", gMPFloatPtr);
147                 Log("*gMPFloatPtr = {");
148                 Log("\t.Sig = 0x%08x", gMPFloatPtr->Sig);
149                 Log("\t.MPConfig = 0x%08x", gMPFloatPtr->MPConfig);
150                 Log("\t.Length = 0x%02x", gMPFloatPtr->Length);
151                 Log("\t.Version = 0x%02x", gMPFloatPtr->Version);
152                 Log("\t.Checksum = 0x%02x", gMPFloatPtr->Checksum);
153                 Log("\t.Features = [0x%02x,0x%02x,0x%02x,0x%02x,0x%02x]",
154                         gMPFloatPtr->Features[0],       gMPFloatPtr->Features[1],
155                         gMPFloatPtr->Features[2],       gMPFloatPtr->Features[3],
156                         gMPFloatPtr->Features[4]
157                         );
158                 Log("}");
159                 
160                 mptable = (void*)( KERNEL_BASE|gMPFloatPtr->MPConfig );
161                 Log("mptable = %p", mptable);
162                 Log("*mptable = {");
163                 Log("\t.Sig = 0x%08x", mptable->Sig);
164                 Log("\t.BaseTableLength = 0x%04x", mptable->BaseTableLength);
165                 Log("\t.SpecRev = 0x%02x", mptable->SpecRev);
166                 Log("\t.Checksum = 0x%02x", mptable->Checksum);
167                 Log("\t.OEMID = '%8c'", mptable->OemID);
168                 Log("\t.ProductID = '%8c'", mptable->ProductID);
169                 Log("\t.OEMTablePtr = %p'", mptable->OEMTablePtr);
170                 Log("\t.OEMTableSize = 0x%04x", mptable->OEMTableSize);
171                 Log("\t.EntryCount = 0x%04x", mptable->EntryCount);
172                 Log("\t.LocalAPICMemMap = 0x%08x", mptable->LocalAPICMemMap);
173                 Log("\t.ExtendedTableLen = 0x%04x", mptable->ExtendedTableLen);
174                 Log("\t.ExtendedTableChecksum = 0x%02x", mptable->ExtendedTableChecksum);
175                 Log("}");
176                 
177                 gpMP_LocalAPIC = (void*)MM_MapHWPage(mptable->LocalAPICMemMap, 1);
178                 
179                 ents = mptable->Entries;
180                 giNumCPUs = 0;
181                 
182                 for( i = 0; i < mptable->EntryCount; i ++ )
183                 {
184                          int    entSize = 0;
185                         switch( ents->Type )
186                         {
187                         case 0: // Processor
188                                 entSize = 20;
189                                 Log("%i: Processor", i);
190                                 Log("\t.APICID = %i", ents->Proc.APICID);
191                                 Log("\t.APICVer = 0x%02x", ents->Proc.APICVer);
192                                 Log("\t.CPUFlags = 0x%02x", ents->Proc.CPUFlags);
193                                 Log("\t.CPUSignature = 0x%08x", ents->Proc.CPUSignature);
194                                 Log("\t.FeatureFlags = 0x%08x", ents->Proc.FeatureFlags);
195                                 
196                                 
197                                 if( !(ents->Proc.CPUFlags & 1) ) {
198                                         Log("DISABLED");
199                                         break;
200                                 }
201                                 
202                                 // Check if there is too many processors
203                                 if(giNumCPUs >= MAX_CPUS) {
204                                         giNumCPUs ++;   // If `giNumCPUs` > MAX_CPUS later, it will be clipped
205                                         break;
206                                 }
207                                 
208                                 // Initialise CPU Info
209                                 gaAPIC_to_CPU[ents->Proc.APICID] = giNumCPUs;
210                                 gaCPUs[giNumCPUs].APICID = ents->Proc.APICID;
211                                 gaCPUs[giNumCPUs].State = 0;
212                                 giNumCPUs ++;
213                                 
214                                 // Send IPI
215                                 if( !(ents->Proc.CPUFlags & 2) )
216                                 {
217                                         MP_StartAP( giNumCPUs-1 );
218                                 }
219                                 
220                                 break;
221                         case 1: // Bus
222                                 entSize = 8;
223                                 Log("%i: Bus", i);
224                                 Log("\t.ID = %i", ents->Bus.ID);
225                                 Log("\t.TypeString = '%6c'", ents->Bus.TypeString);
226                                 break;
227                         case 2: // I/O APIC
228                                 entSize = 8;
229                                 Log("%i: I/O APIC", i);
230                                 Log("\t.ID = %i", ents->IOAPIC.ID);
231                                 Log("\t.Version = 0x%02x", ents->IOAPIC.Version);
232                                 Log("\t.Flags = 0x%02x", ents->IOAPIC.Flags);
233                                 Log("\t.Addr = 0x%08x", ents->IOAPIC.Addr);
234                                 break;
235                         case 3: // I/O Interrupt Assignment
236                                 entSize = 8;
237                                 Log("%i: I/O Interrupt Assignment", i);
238                                 Log("\t.IntType = %i", ents->IOInt.IntType);
239                                 Log("\t.Flags = 0x%04x", ents->IOInt.Flags);
240                                 Log("\t.SourceBusID = 0x%02x", ents->IOInt.SourceBusID);
241                                 Log("\t.SourceBusIRQ = 0x%02x", ents->IOInt.SourceBusIRQ);
242                                 Log("\t.DestAPICID = 0x%02x", ents->IOInt.DestAPICID);
243                                 Log("\t.DestAPICIRQ = 0x%02x", ents->IOInt.DestAPICIRQ);
244                                 break;
245                         case 4: // Local Interrupt Assignment
246                                 entSize = 8;
247                                 Log("%i: Local Interrupt Assignment", i);
248                                 Log("\t.IntType = %i", ents->LocalInt.IntType);
249                                 Log("\t.Flags = 0x%04x", ents->LocalInt.Flags);
250                                 Log("\t.SourceBusID = 0x%02x", ents->LocalInt.SourceBusID);
251                                 Log("\t.SourceBusIRQ = 0x%02x", ents->LocalInt.SourceBusIRQ);
252                                 Log("\t.DestLocalAPICID = 0x%02x", ents->LocalInt.DestLocalAPICID);
253                                 Log("\t.DestLocalAPICIRQ = 0x%02x", ents->LocalInt.DestLocalAPICIRQ);
254                                 break;
255                         default:
256                                 Log("%i: Unknown (%i)", i, ents->Type);
257                                 break;
258                         }
259                         ents = (void*)( (Uint)ents + entSize );
260                 }
261                 
262                 if( giNumCPUs > MAX_CPUS ) {
263                         Warning("Too many CPUs detected (%i), only using %i of them", giNumCPUs, MAX_CPUS);
264                         giNumCPUs = MAX_CPUS;
265                 }
266         
267                 while( giNumInitingCPUs )
268                         MM_FinishVirtualInit();
269                 
270                 Panic("Uh oh... MP Table Parsing is unimplemented\n");
271         }
272         else {
273                 Log("No MP Table was found, assuming uniprocessor\n");
274                 giNumCPUs = 1;
275                 gTSSs = &gTSS0;
276         }
277         #else
278         giNumCPUs = 1;
279         gTSSs = &gTSS0;
280         MM_FinishVirtualInit();
281         #endif
282         
283         #if USE_MP
284         // Initialise Normal TSS(s)
285         for(pos=0;pos<giNumCPUs;pos++)
286         {
287         #else
288         pos = 0;
289         #endif
290                 gTSSs[pos].RSP0 = 0;    // Set properly by scheduler
291                 gGDT[7+pos*2].LimitLow = sizeof(tTSS) & 0xFFFF;
292                 gGDT[7+pos*2].BaseLow = ((Uint)(&gTSSs[pos])) & 0xFFFF;
293                 gGDT[7+pos*2].BaseMid = ((Uint)(&gTSSs[pos])) >> 16;
294                 gGDT[7+pos*2].BaseHi = ((Uint)(&gTSSs[pos])) >> 24;
295                 gGDT[7+pos*2+1].DWord[0] = ((Uint)(&gTSSs[pos])) >> 32;
296         #if USE_MP
297         }
298         for(pos=0;pos<giNumCPUs;pos++) {
299                 __asm__ __volatile__ ("ltr %%ax"::"a"(0x38+pos*16));
300         }
301         #else
302         __asm__ __volatile__ ("ltr %%ax"::"a"(0x38));
303         #endif
304         
305         // Set Debug registers
306         __asm__ __volatile__ ("mov %0, %%db0" : : "r"(&gThreadZero));
307         __asm__ __volatile__ ("mov %%rax, %%db1" : : "a"(0));
308         
309         gaCPUs[0].Current = &gThreadZero;
310         
311         gThreadZero.MemState.CR3 = (Uint)gInitialPML4 - KERNEL_BASE;
312         gThreadZero.CurCPU = 0;
313         gThreadZero.KernelStack = 0xFFFFA00000000000 + KERNEL_STACK_SIZE;
314         
315         // Set timer frequency
316         outb(0x43, 0x34);       // Set Channel 0, Low/High, Rate Generator
317         outb(0x40, PIT_TIMER_DIVISOR&0xFF);     // Low Byte of Divisor
318         outb(0x40, (PIT_TIMER_DIVISOR>>8)&0xFF);        // High Byte
319         
320         // Create Per-Process Data Block
321         if( !MM_Allocate(MM_PPD_CFG) )
322         {
323                 Warning("Oh, hell, Unable to allocate PPD for Thread#0");
324         }
325
326         Log_Log("Proc", "Multithreading initialised");
327 }
328
329 #if USE_MP
330 void MP_StartAP(int CPU)
331 {
332         Log("Starting AP %i (APIC %i)", CPU, gaCPUs[CPU].APICID);
333         // Set location of AP startup code and mark for a warm restart
334         *(Uint16*)(KERNEL_BASE|0x467) = (Uint)&APStartup - (KERNEL_BASE|0xFFFF0);
335         *(Uint16*)(KERNEL_BASE|0x469) = 0xFFFF;
336         outb(0x70, 0x0F);       outb(0x71, 0x0A);       // Warm Reset
337         MP_SendIPI(gaCPUs[CPU].APICID, 0, 5);
338         giNumInitingCPUs ++;
339 }
340
341 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode)
342 {
343         Uint32  addr = (Uint)gpMP_LocalAPIC + 0x300;
344         Uint32  val;
345         
346         // High
347         val = (Uint)APICID << 24;
348         Log("*%p = 0x%08x", addr+0x10, val);
349         *(Uint32*)(addr+0x10) = val;
350         // Low (and send)
351         val = ((DeliveryMode & 7) << 8) | (Vector & 0xFF);
352         Log("*%p = 0x%08x", addr, val);
353         *(Uint32*)addr = val;
354 }
355 #endif
356
357 /**
358  * \brief Idle task
359  */
360 void Proc_IdleTask(void *ptr)
361 {
362         tCPU    *cpu = ptr;
363         cpu->IdleThread = Proc_GetCurThread();
364         cpu->IdleThread->ThreadName = (char*)"Idle Thread";
365         Threads_SetPriority( cpu->IdleThread, -1 );     // Never called randomly
366         cpu->IdleThread->Quantum = 1;   // 1 slice quantum
367         for(;;) {
368                 HALT(); // Just yeilds
369                 Threads_Yield();
370         }
371 }
372
373 /**
374  * \fn void Proc_Start(void)
375  * \brief Start process scheduler
376  */
377 void Proc_Start(void)
378 {
379         #if USE_MP
380          int    i;
381         #endif
382         
383         #if USE_MP
384         // Start APs
385         for( i = 0; i < giNumCPUs; i ++ )
386         {
387                  int    tid;
388                 if(i)   gaCPUs[i].Current = NULL;
389                 
390                 Proc_NewKThread(Proc_IdleTask, &gaCPUs[i]);             
391
392                 // Create Idle Task
393                 gaCPUs[i].IdleThread = Threads_GetThread(tid);
394                 
395                 
396                 // Start the AP
397                 if( i != giProc_BootProcessorID ) {
398                         MP_StartAP( i );
399                 }
400         }
401         
402         // BSP still should run the current task
403         gaCPUs[0].Current = &gThreadZero;
404         
405         // Start interrupts and wait for APs to come up
406         Log("Waiting for APs to come up\n");
407         __asm__ __volatile__ ("sti");
408         while( giNumInitingCPUs )       __asm__ __volatile__ ("hlt");
409         #else
410         Proc_NewKThread(Proc_IdleTask, &gaCPUs[0]);
411         
412         // Start Interrupts (and hence scheduler)
413         __asm__ __volatile__("sti");
414         #endif
415         MM_FinishVirtualInit();
416         Log_Log("Proc", "Multithreading started");
417 }
418
419 /**
420  * \fn tThread *Proc_GetCurThread(void)
421  * \brief Gets the current thread
422  */
423 tThread *Proc_GetCurThread(void)
424 {
425         #if USE_MP
426         return gaCPUs[ GetCPUNum() ].Current;
427         #else
428         return gaCPUs[ 0 ].Current;
429         #endif
430 }
431
432 int Proc_NewKThread(void (*Fcn)(void*), void *Data)
433 {
434         Uint    rsp;
435         tThread *newThread, *cur;
436         
437         cur = Proc_GetCurThread();
438         newThread = Threads_CloneTCB(NULL, 0);
439         if(!newThread)  return -1;
440         
441         // Set CR3
442         newThread->MemState.CR3 = cur->MemState.CR3;
443
444         // Create new KStack
445         newThread->KernelStack = MM_NewKStack();
446         // Check for errors
447         if(newThread->KernelStack == 0) {
448                 free(newThread);
449                 return -1;
450         }
451
452         rsp = newThread->KernelStack;
453         *(Uint*)(rsp-=8) = (Uint)Data;  // Data (shadowed)
454         *(Uint*)(rsp-=8) = 1;   // Number of params
455         *(Uint*)(rsp-=8) = (Uint)Fcn;   // Function to call
456         *(Uint*)(rsp-=8) = (Uint)newThread;     // Thread ID
457         
458         newThread->SavedState.RSP = rsp;
459         newThread->SavedState.RIP = (Uint)&NewTaskHeader;
460 //      Log("New (KThread) %p, rsp = %p\n", newThread->SavedState.RIP, newThread->SavedState.RSP);
461         
462 //      MAGIC_BREAK();  
463         Threads_AddActive(newThread);
464
465         return newThread->TID;
466 }
467
468 /**
469  * \fn int Proc_Clone(Uint Flags)
470  * \brief Clone the current process
471  */
472 int Proc_Clone(Uint Flags)
473 {
474         tThread *newThread, *cur = Proc_GetCurThread();
475         Uint    rip;
476
477         // Sanity check 
478         if( !(Flags & CLONE_VM) ) {
479                 Log_Error("Proc", "Proc_Clone: Don't leave CLONE_VM unset, use Proc_NewKThread instead");
480                 return -1;
481         }
482
483         // Create new TCB
484         newThread = Threads_CloneTCB(NULL, Flags);
485         if(!newThread)  return -1;
486         
487         // Save core machine state
488         rip = Proc_CloneInt(&newThread->SavedState.RSP, &newThread->MemState.CR3);
489         if(rip == 0)    return 0;       // Child
490         newThread->KernelStack = cur->KernelStack;
491         newThread->SavedState.RIP = rip;
492
493         // DEBUG
494         #if 0
495         Log("New (Clone) %p, rsp = %p, cr3 = %p", rip, newThread->SavedState.RSP, newThread->MemState.CR3);
496         {
497                 Uint cr3;
498                 __asm__ __volatile__ ("mov %%cr3, %0" : "=r" (cr3));
499                 Log("Current CR3 = 0x%x, PADDR(RSP) = 0x%x", cr3, MM_GetPhysAddr(newThread->SavedState.RSP));
500         }
501         #endif
502         // /DEBUG
503         
504         // Lock list and add to active
505         Threads_AddActive(newThread);
506         
507         return newThread->TID;
508 }
509
510 /**
511  * \fn int Proc_SpawnWorker(void)
512  * \brief Spawns a new worker thread
513  */
514 int Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
515 {
516         tThread *new, *cur;
517         Uint    stack_contents[4];
518
519         cur = Proc_GetCurThread();
520         
521         // Create new thread
522         new = malloc( sizeof(tThread) );
523         if(!new) {
524                 Warning("Proc_SpawnWorker - Out of heap space!\n");
525                 return -1;
526         }
527         memcpy(new, &gThreadZero, sizeof(tThread));
528         // Set Thread ID
529         new->TID = giNextTID++;
530
531         // Create the stack contents
532         stack_contents[3] = (Uint)Data;
533         stack_contents[2] = 1;
534         stack_contents[1] = (Uint)Fcn;
535         stack_contents[0] = (Uint)new;
536         
537         // Create a new worker stack (in PID0's address space)
538         // The stack is relocated by this code
539         new->KernelStack = MM_NewWorkerStack(stack_contents, sizeof(stack_contents));
540
541         new->SavedState.RSP = new->KernelStack - sizeof(stack_contents);
542         new->SavedState.RIP = (Uint)&NewTaskHeader;
543         
544 //      Log("New (Worker) %p, rsp = %p\n", new->SavedState.RIP, new->SavedState.RSP);
545         
546         // Mark as active
547         new->Status = THREAD_STAT_PREINIT;
548         Threads_AddActive( new );
549         
550         return new->TID;
551 }
552
553 /**
554  * \brief Creates a new user stack
555  */
556 Uint Proc_MakeUserStack(void)
557 {
558          int    i;
559         Uint    base = USER_STACK_TOP - USER_STACK_SZ;
560         
561         // Check Prospective Space
562         for( i = USER_STACK_SZ >> 12; i--; )
563         {
564                 if( MM_GetPhysAddr( base + (i<<12) ) != 0 )
565                         break;
566         }
567         
568         if(i != -1)     return 0;
569         
570         // Allocate Stack - Allocate incrementally to clean up MM_Dump output
571         for( i = 0; i < (USER_STACK_SZ-USER_STACK_PREALLOC)/0x1000; i++ )
572         {
573                 MM_AllocateZero( base + (i<<12) );
574         }
575         for( ; i < USER_STACK_SZ/0x1000; i++ )
576         {
577                 tPAddr  alloc = MM_Allocate( base + (i<<12) );
578                 if( !alloc )
579                 {
580                         // Error
581                         Log_Error("Proc", "Unable to allocate user stack (%i pages requested)", USER_STACK_SZ/0x1000);
582                         while( i -- )
583                                 MM_Deallocate( base + (i<<12) );
584                         return 0;
585                 }
586         }
587         
588         return base + USER_STACK_SZ;
589 }
590
591
592 /**
593  * \fn void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
594  * \brief Starts a user task
595  */
596 void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
597 {
598         Uint    *stack = (void*)Proc_MakeUserStack();
599          int    i;
600         Uint    delta;
601         Uint16  ss, cs;
602         
603         LOG("stack = 0x%x", stack);
604         
605         // Copy Arguments
606         stack = (void*)( (Uint)stack - DataSize );
607         memcpy( stack, ArgV, DataSize );
608         
609         // Adjust Arguments and environment
610         delta = (Uint)stack - (Uint)ArgV;
611         ArgV = (char**)stack;
612         for( i = 0; ArgV[i]; i++ )      ArgV[i] += delta;
613         i ++;
614         EnvP = &ArgV[i];
615         for( i = 0; EnvP[i]; i++ )      EnvP[i] += delta;
616         
617         // User Mode Segments
618         // 0x2B = 64-bit
619         ss = 0x23;      cs = 0x2B;
620         
621         // Arguments
622         *--stack = (Uint)EnvP;
623         *--stack = (Uint)ArgV;
624         *--stack = (Uint)ArgC;
625         while(*Bases)
626                 *--stack = *Bases++;
627         
628         Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
629 }
630
631 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
632 {
633         if( !(CS == 0x1B || CS == 0x2B) || SS != 0x23 ) {
634                 Log_Error("Proc", "Proc_StartProcess: CS / SS are not valid (%x, %x)",
635                         CS, SS);
636                 Threads_Exit(0, -1);
637         }
638 //      Log("Proc_StartProcess: (SS=%x, Stack=%p, Flags=%x, CS=%x, IP=%p)", SS, Stack, Flags, CS, IP);
639 //      MM_DumpTables(0, USER_MAX);
640         if(CS == 0x1B)
641         {
642                 // 32-bit return
643                 __asm__ __volatile__ (
644                         "mov %0, %%rsp;\n\t"    // Set stack pointer
645                         "mov %2, %%r11;\n\t"    // Set RFLAGS
646                         "sysret;\n\t"
647                         : : "r" (Stack), "c" (IP), "r" (Flags)
648                         );
649         }
650         else
651         {
652                 // 64-bit return
653                 __asm__ __volatile__ (
654                         "mov %0, %%rsp;\n\t"    // Set stack pointer
655                         "mov %2, %%r11;\n\t"    // Set RFLAGS
656                         "sysretq;\n\t"
657                         : : "r" (Stack), "c" (IP), "r" (Flags)
658                         );
659         }
660         for(;;);
661 }
662
663 /**
664  * \fn int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
665  * \brief Demotes a process to a lower permission level
666  * \param Err   Pointer to user's errno
667  * \param Dest  New Permission Level
668  * \param Regs  Pointer to user's register structure
669  */
670 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
671 {
672          int    cpl = Regs->CS & 3;
673         // Sanity Check
674         if(Dest > 3 || Dest < 0) {
675                 *Err = -EINVAL;
676                 return -1;
677         }
678         
679         // Permission Check
680         if(cpl > Dest) {
681                 *Err = -EACCES;
682                 return -1;
683         }
684         
685         // Change the Segment Registers
686         Regs->CS = (((Dest+1)<<4) | Dest) - 8;
687         Regs->SS = ((Dest+1)<<4) | Dest;
688         
689         return 0;
690 }
691
692 /**
693  * \brief Calls a signal handler in user mode
694  * \note Used for signals
695  */
696 void Proc_CallFaultHandler(tThread *Thread)
697 {
698         // Never returns
699         Proc_ReturnToUser(Thread->FaultHandler, Thread->KernelStack, Thread->CurFaultNum);
700         for(;;);
701 }
702
703 void Proc_DumpThreadCPUState(tThread *Thread)
704 {
705         Log("  At %04x:%016llx", Thread->SavedState.UserCS, Thread->SavedState.UserRIP);
706 }
707
708 void Proc_Reschedule(void)
709 {
710         tThread *nextthread, *curthread;
711          int    cpu = GetCPUNum();
712
713         // TODO: Wait for it?
714         if(IS_LOCKED(&glThreadListLock))        return;
715         
716         curthread = gaCPUs[cpu].Current;
717
718         nextthread = Threads_GetNextToRun(cpu, curthread);
719
720         if(nextthread == curthread)     return ;
721         if(!nextthread)
722                 nextthread = gaCPUs[cpu].IdleThread;
723         if(!nextthread)
724                 return ;
725
726         #if DEBUG_TRACE_SWITCH
727         LogF("\nSwitching to task CR3 = 0x%x, RIP = %p, RSP = %p - %i (%s)\n",
728                 nextthread->MemState.CR3,
729                 nextthread->SavedState.RIP,
730                 nextthread->SavedState.RSP,
731                 nextthread->TID,
732                 nextthread->ThreadName
733                 );
734         #endif
735
736         // Update CPU state
737         gaCPUs[cpu].Current = nextthread;
738         gTSSs[cpu].RSP0 = nextthread->KernelStack-4;
739         __asm__ __volatile__ ("mov %0, %%db0" : : "r" (nextthread));
740
741         SwitchTasks(
742                 nextthread->SavedState.RSP, &curthread->SavedState.RSP,
743                 nextthread->SavedState.RIP, &curthread->SavedState.RIP,
744                 nextthread->MemState.CR3
745                 );
746         return ;
747 }
748
749 /**
750  * \fn void Proc_Scheduler(int CPU)
751  * \brief Swap current thread and clears dead threads
752  */
753 void Proc_Scheduler(int CPU, Uint RSP, Uint RIP)
754 {
755 #if 0
756         {
757         tThread *thread;
758
759         // If the spinlock is set, let it complete
760         if(IS_LOCKED(&glThreadListLock))        return;
761         
762         // Get current thread
763         thread = gaCPUs[CPU].Current;
764
765         if( thread )
766         {
767                 tRegs   *regs;
768                 // Reduce remaining quantum and continue timeslice if non-zero
769                 if(thread->Remaining--) return;
770                 // Reset quantum for next call
771                 thread->Remaining = thread->Quantum;
772                 
773                 // TODO: Make this more stable somehow
774                 {
775                         regs = (tRegs*)(RSP+(1)*8);     // CurThread
776                         thread->SavedState.UserCS = regs->CS;
777                         thread->SavedState.UserRIP = regs->RIP;
778                 }
779         }
780
781         // ACK Timer here?
782
783         Proc_Reschedule();
784         }
785 #endif
786 }
787
788 // === EXPORTS ===
789 EXPORT(Proc_SpawnWorker);

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