Merge branch 'master' of git://cadel.mutabah.net/acess2
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / mm_virt.c
1 /*
2  * AcessOS Microkernel Version
3  * mm_virt.c
4  * 
5  * Memory Map
6  * 0xE0 - Kernel Base
7  * 0xF0 - Kernel Stacks
8  * 0xFD - Fractals
9  * 0xFE - Unused
10  * 0xFF - System Calls / Kernel's User Code
11  */
12 #define DEBUG   0
13 #define SANITY  1
14 #include <acess.h>
15 #include <mm_virt.h>
16 #include <mm_phys.h>
17 #include <proc.h>
18 #include <hal_proc.h>
19 #include <arch_int.h>
20 #include <semaphore.h>
21
22 #define TAB     22
23
24 #define WORKER_STACKS           0x00100000      // Thread0 Only!
25 #define WORKER_STACK_SIZE       MM_KERNEL_STACK_SIZE
26 #define WORKER_STACKS_END       0xB0000000
27 #define NUM_WORKER_STACKS       ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
28
29 #define PAE_PAGE_TABLE_ADDR     0xFC000000      // 16 MiB
30 #define PAE_PAGE_DIR_ADDR       0xFCFC0000      // 16 KiB
31 #define PAE_PAGE_PDPT_ADDR      0xFCFC3F00      // 32 bytes
32 #define PAE_TMP_PDPT_ADDR       0xFCFC3F20      // 32 bytes
33 #define PAE_TMP_DIR_ADDR        0xFCFE0000      // 16 KiB
34 #define PAE_TMP_TABLE_ADDR      0xFD000000      // 16 MiB
35
36 #define PAGE_TABLE_ADDR 0xFC000000
37 #define PAGE_DIR_ADDR   0xFC3F0000
38 #define PAGE_CR3_ADDR   0xFC3F0FC0
39 #define TMP_CR3_ADDR    0xFC3F0FC4      // Part of core instead of temp
40 #define TMP_DIR_ADDR    0xFC3F1000      // Same
41 #define TMP_TABLE_ADDR  0xFC400000
42
43 #define HW_MAP_ADDR             0xFE000000
44 #define HW_MAP_MAX              0xFFEF0000
45 #define NUM_HW_PAGES    ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
46 #define TEMP_MAP_ADDR   0xFFEF0000      // Allows 16 "temp" pages
47 #define NUM_TEMP_PAGES  16
48 #define LAST_BLOCK_ADDR 0xFFFF0000      // Free space for kernel provided user code/ *(-1) protection
49
50 #define PF_PRESENT      0x1
51 #define PF_WRITE        0x2
52 #define PF_USER         0x4
53 #define PF_GLOBAL       0x80
54 #define PF_COW          0x200
55 #define PF_NOPAGE       0x400
56
57 #define INVLPG(addr)    __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
58
59 #define GET_TEMP_MAPPING(cr3) do { \
60         __ASM__("cli"); \
61         __AtomicTestSetLoop( (Uint *)gpTmpCR3, cr3 | 3 ); \
62 } while(0)
63 #define REL_TEMP_MAPPING() do { \
64         *gpTmpCR3 = 0; \
65         __ASM__("sti"); \
66 } while(0)
67
68 typedef Uint32  tTabEnt;
69
70 // === IMPORTS ===
71 extern char     _UsertextEnd[], _UsertextBase[];
72 extern Uint32   gaInitPageDir[1024];
73 extern Uint32   gaInitPageTable[1024];
74 extern void     Threads_SegFault(tVAddr Addr);
75 extern void     Error_Backtrace(Uint eip, Uint ebp);
76
77 // === PROTOTYPES ===
78 void    MM_PreinitVirtual(void);
79 void    MM_InstallVirtual(void);
80 void    MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
81 //void  MM_DumpTables(tVAddr Start, tVAddr End);
82 //void  MM_ClearUser(void);
83 tPAddr  MM_DuplicatePage(tVAddr VAddr);
84
85 // === GLOBALS ===
86 #define gaPageTable     ((tTabEnt*)PAGE_TABLE_ADDR)
87 #define gaPageDir       ((tTabEnt*)PAGE_DIR_ADDR)
88 #define gaTmpTable      ((tTabEnt*)TMP_TABLE_ADDR)
89 #define gaTmpDir        ((tTabEnt*)TMP_DIR_ADDR)
90 #define gpPageCR3       ((tTabEnt*)PAGE_CR3_ADDR)
91 #define gpTmpCR3        ((tTabEnt*)TMP_CR3_ADDR)
92
93 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
94 #define gaPAE_PageDir   ((tTabEnt*)PAE_PAGE_DIR_ADDR)
95 #define gaPAE_MainPDPT  ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
96 #define gaPAE_TmpTable  ((tTabEnt*)PAE_TMP_DIR_ADDR)
97 #define gaPAE_TmpDir    ((tTabEnt*)PAE_TMP_DIR_ADDR)
98 #define gaPAE_TmpPDPT   ((tTabEnt*)PAE_TMP_PDPT_ADDR)
99  int    gbUsePAE = 0;
100 tMutex  glTempMappings;
101 tSemaphore      gTempMappingsSem;
102 tMutex  glTempFractal;
103 Uint32  gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
104  int    giLastUsedWorker = 0;
105 struct sPageInfo {
106         void    *Node;
107         tVAddr  Base;
108         Uint64  Offset;
109          int    Length;
110          int    Flags;
111 }       *gaMappedRegions;       // sizeof = 24 bytes
112
113 // === CODE ===
114 /**
115  * \fn void MM_PreinitVirtual(void)
116  * \brief Maps the fractal mappings
117  */
118 void MM_PreinitVirtual(void)
119 {
120         gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
121         INVLPG( PAGE_TABLE_ADDR );
122         
123         Semaphore_Init(&gTempMappingsSem, NUM_TEMP_PAGES, NUM_TEMP_PAGES, "MMVirt", "Temp Mappings");
124 }
125
126 /**
127  * \fn void MM_InstallVirtual(void)
128  * \brief Sets up the constant page mappings
129  */
130 void MM_InstallVirtual(void)
131 {
132          int    i;
133         
134         // --- Pre-Allocate kernel tables
135         for( i = KERNEL_BASE>>22; i < 1024; i ++ )
136         {
137                 if( gaPageDir[ i ] )    continue;
138                 // Skip stack tables, they are process unique
139                 if( i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) {
140                         gaPageDir[ i ] = 0;
141                         continue;
142                 }
143                 // Preallocate table
144                 gaPageDir[ i ] = MM_AllocPhys() | 3;
145                 INVLPG( &gaPageTable[i*1024] );
146                 memset( &gaPageTable[i*1024], 0, 0x1000 );
147         }
148         
149         // Unset kernel on the User Text pages
150         for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
151                 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
152         }
153         
154         *gpTmpCR3 = 0;
155 }
156
157 /**
158  * \brief Cleans up the SMP required mappings
159  */
160 void MM_FinishVirtualInit(void)
161 {
162         gaInitPageDir[ 0 ] = 0;
163 }
164
165 /**
166  * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
167  * \brief Called on a page fault
168  */
169 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
170 {
171         //ENTER("xAddr bErrorCode", Addr, ErrorCode);
172         
173         // -- Check for COW --
174         if( gaPageDir  [Addr>>22] & PF_PRESENT  && gaPageTable[Addr>>12] & PF_PRESENT
175          && gaPageTable[Addr>>12] & PF_COW )
176         {
177                 tPAddr  paddr;
178                 __asm__ __volatile__ ("sti");
179                 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
180                 {
181                         gaPageTable[Addr>>12] &= ~PF_COW;
182                         gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
183                 }
184                 else
185                 {
186                         //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
187                         paddr = MM_DuplicatePage( Addr );
188                         MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
189                         gaPageTable[Addr>>12] &= PF_USER;
190                         gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
191                 }
192                 
193 //              Log_Debug("MMVirt", "COW for %p (%P)", Addr, gaPageTable[Addr>>12]);
194                 
195                 INVLPG( Addr & ~0xFFF );
196                 return;
197         }
198
199         // Disable instruction tracing  
200         __ASM__("pushf; andw $0xFEFF, 0(%esp); popf");
201         Proc_GetCurThread()->bInstrTrace = 0;
202
203         // If it was a user, tell the thread handler
204         if(ErrorCode & 4) {
205                 __asm__ __volatile__ ("sti");
206                 Log_Warning("MMVirt", "User %s %s memory%s",
207                         (ErrorCode&2?"write to":"read from"),
208                         (ErrorCode&1?"bad/locked":"non-present"),
209                         (ErrorCode&16?" (Instruction Fetch)":"")
210                         );
211                 Log_Warning("MMVirt", "Instruction %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
212                 __ASM__("sti"); // Restart IRQs
213                 #if 1
214                 Error_Backtrace(Regs->eip, Regs->ebp);
215                 #endif
216                 Threads_SegFault(Addr);
217                 return ;
218         }
219         
220         Debug_KernelPanic();
221         
222         // -- Check Error Code --
223         if(ErrorCode & 8)
224                 Warning("Reserved Bits Trashed!");
225         else
226         {
227                 Warning("Kernel %s %s memory%s",
228                         (ErrorCode&2?"write to":"read from"),
229                         (ErrorCode&1?"bad/locked":"non-present"),
230                         (ErrorCode&16?" (Instruction Fetch)":"")
231                         );
232         }
233         
234         Log("CPU %i - Code at %p accessed %p", GetCPUNum(), Regs->eip, Addr);
235         // Print Stack Backtrace
236         Error_Backtrace(Regs->eip, Regs->ebp);
237
238         #if 0   
239         Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
240         if( gaPageDir[Addr>>22] & PF_PRESENT )
241                 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
242         #endif
243         //MM_DumpTables(0, -1); 
244         
245         // Register Dump
246         Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
247         Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
248         //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
249         Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
250         Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
251         {
252                 Uint    dr0, dr1;
253                 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
254                 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
255                 Log("DR0 %08x DR1 %08x", dr0, dr1);
256         }
257         
258         Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
259 }
260
261 /**
262  * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
263  * \brief Dumps the layout of the page tables
264  */
265 void MM_DumpTables(tVAddr Start, tVAddr End)
266 {
267         tVAddr  rangeStart = 0;
268         tPAddr  expected = 0;
269         void    *expected_node = NULL, *tmpnode = NULL;
270         tVAddr  curPos;
271         Uint    page;
272         const tPAddr    MASK = ~0xF78;
273         
274         Start >>= 12;   End >>= 12;
275         
276         #if 0
277         Log("Directory Entries:");
278         for(page = Start >> 10;
279                 page < (End >> 10)+1;
280                 page ++)
281         {
282                 if(gaPageDir[page])
283                 {
284                         Log(" 0x%08x-0x%08x :: 0x%08x",
285                                 page<<22, ((page+1)<<22)-1,
286                                 gaPageDir[page]&~0xFFF
287                                 );
288                 }
289         }
290         #endif
291         
292         Log("Table Entries:");
293         for(page = Start, curPos = Start<<12;
294                 page < End;
295                 curPos += 0x1000, page++)
296         {
297                 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
298                 ||  !(gaPageTable[page] & PF_PRESENT)
299                 ||  (gaPageTable[page] & MASK) != expected
300                 ||  (tmpnode=NULL,MM_GetPageNode(expected, &tmpnode), tmpnode != expected_node))
301                 {
302                         if(expected) {
303                                 tPAddr  orig = gaPageTable[rangeStart>>12];
304                                 Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
305                                         rangeStart,
306                                         orig & ~0xFFF,
307                                         curPos - rangeStart,
308                                         (orig & PF_NOPAGE ? "P" : "-"),
309                                         (orig & PF_COW ? "C" : "-"),
310                                         (orig & PF_GLOBAL ? "G" : "-"),
311                                         (orig & PF_USER ? "U" : "-"),
312                                         (orig & PF_WRITE ? "W" : "-"),
313                                         expected_node
314                                         );
315                                 expected = 0;
316                         }
317                         if( !(gaPageDir[curPos>>22] & PF_PRESENT) )     continue;
318                         if( !(gaPageTable[curPos>>12] & PF_PRESENT) )   continue;
319                         
320                         expected = (gaPageTable[page] & MASK);
321                         MM_GetPageNode(expected, &expected_node);
322                         rangeStart = curPos;
323                 }
324                 if(expected)    expected += 0x1000;
325         }
326         
327         if(expected) {
328                 tPAddr  orig = gaPageTable[rangeStart>>12];
329                 Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
330                         rangeStart,
331                         orig & ~0xFFF,
332                         curPos - rangeStart,
333                         (orig & PF_NOPAGE ? "p" : "-"),
334                         (orig & PF_COW ? "C" : "-"),
335                         (orig & PF_GLOBAL ? "G" : "-"),
336                         (orig & PF_USER ? "U" : "-"),
337                         (orig & PF_WRITE ? "W" : "-"),
338                         expected_node
339                         );
340                 expected = 0;
341         }
342 }
343
344 /**
345  * \fn tPAddr MM_Allocate(tVAddr VAddr)
346  */
347 tPAddr MM_Allocate(tVAddr VAddr)
348 {
349         tPAddr  paddr;
350         //ENTER("xVAddr", VAddr);
351         //__ASM__("xchg %bx,%bx");
352         // Check if the directory is mapped
353         if( gaPageDir[ VAddr >> 22 ] == 0 )
354         {
355                 // Allocate directory
356                 paddr = MM_AllocPhys();
357                 if( paddr == 0 ) {
358                         Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
359                         //LEAVE('i',0);
360                         return 0;
361                 }
362                 // Map and mark as user (if needed)
363                 gaPageDir[ VAddr >> 22 ] = paddr | 3;
364                 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
365                 
366                 INVLPG( &gaPageDir[ VAddr >> 22 ] );
367                 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
368         }
369         // Check if the page is already allocated
370         else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
371                 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
372                 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
373                 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
374         }
375         
376         // Allocate
377         paddr = MM_AllocPhys();
378         //LOG("paddr = 0x%llx", paddr);
379         if( paddr == 0 ) {
380                 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
381                         VAddr, __builtin_return_address(0));
382                 //LEAVE('i',0);
383                 return 0;
384         }
385         // Map
386         gaPageTable[ VAddr >> 12 ] = paddr | 3;
387         // Mark as user
388         if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
389         // Invalidate Cache for address
390         INVLPG( VAddr & ~0xFFF );
391         
392         //LEAVE('X', paddr);
393         return paddr;
394 }
395
396 /**
397  * \fn void MM_Deallocate(tVAddr VAddr)
398  */
399 void MM_Deallocate(tVAddr VAddr)
400 {
401         if( gaPageDir[ VAddr >> 22 ] == 0 ) {
402                 Warning("MM_Deallocate - Directory not mapped");
403                 return;
404         }
405         
406         if(gaPageTable[ VAddr >> 12 ] == 0) {
407                 Warning("MM_Deallocate - Page is not allocated");
408                 return;
409         }
410         
411         // Dereference page
412         MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
413         // Clear page
414         gaPageTable[ VAddr >> 12 ] = 0;
415 }
416
417 /**
418  * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
419  * \brief Checks if the passed address is accesable
420  */
421 tPAddr MM_GetPhysAddr(volatile const void *Addr)
422 {
423         tVAddr  addr = (tVAddr)Addr;
424         if( !(gaPageDir[addr >> 22] & 1) )
425                 return 0;
426         if( !(gaPageTable[addr >> 12] & 1) )
427                 return 0;
428         return (gaPageTable[addr >> 12] & ~0xFFF) | (addr & 0xFFF);
429 }
430
431 /**
432  * \fn void MM_SetCR3(Uint CR3)
433  * \brief Sets the current process space
434  */
435 void MM_SetCR3(Uint CR3)
436 {
437         __ASM__("mov %0, %%cr3"::"r"(CR3));
438 }
439
440 /**
441  * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
442  * \brief Map a physical page to a virtual one
443  */
444 int MM_Map(tVAddr VAddr, tPAddr PAddr)
445 {
446         //ENTER("xVAddr xPAddr", VAddr, PAddr);
447         // Sanity check
448         if( PAddr & 0xFFF || VAddr & 0xFFF ) {
449                 Log_Warning("MM_Virt", "MM_Map - Physical or Virtual Addresses are not aligned (0x%P and %p)",
450                         PAddr, VAddr);
451                 //LEAVE('i', 0);
452                 return 0;
453         }
454         
455         // Align addresses
456         PAddr &= ~0xFFF;        VAddr &= ~0xFFF;
457         
458         // Check if the directory is mapped
459         if( gaPageDir[ VAddr >> 22 ] == 0 )
460         {
461                 tPAddr  tmp = MM_AllocPhys();
462                 if( tmp == 0 )
463                         return 0;
464                 gaPageDir[ VAddr >> 22 ] = tmp | 3;
465                 
466                 // Mark as user
467                 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
468                 
469                 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
470                 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
471         }
472         // Check if the page is already allocated
473         else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
474                 Warning("MM_Map - Allocating to used address");
475                 //LEAVE('i', 0);
476                 return 0;
477         }
478         
479         // Map
480         gaPageTable[ VAddr >> 12 ] = PAddr | 3;
481         // Mark as user
482         if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
483         
484         //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
485         //      VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
486         
487         // Reference
488         MM_RefPhys( PAddr );
489         
490         //LOG("INVLPG( 0x%x )", VAddr);
491         INVLPG( VAddr );
492         
493         //LEAVE('i', 1);
494         return 1;
495 }
496
497 /**
498  * \brief Clear user's address space
499  */
500 void MM_ClearUser(void)
501 {
502         Uint    i, j;
503         
504         for( i = 0; i < (MM_USER_MAX>>22); i ++ )
505         {
506                 // Check if directory is not allocated
507                 if( !(gaPageDir[i] & PF_PRESENT) ) {
508                         gaPageDir[i] = 0;
509                         continue;
510                 }
511                 
512                 // Deallocate tables
513                 for( j = 0; j < 1024; j ++ )
514                 {
515                         if( gaPageTable[i*1024+j] & 1 )
516                                 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
517                         gaPageTable[i*1024+j] = 0;
518                 }
519                 
520                 // Deallocate directory
521                 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
522                 gaPageDir[i] = 0;
523                 INVLPG( &gaPageTable[i*1024] );
524         }
525         INVLPG( gaPageDir );
526 }
527
528 /**
529  * \brief Deallocate an address space
530  */
531 void MM_ClearSpace(Uint32 CR3)
532 {
533          int    i, j;
534         
535         if(CR3 == (*gpPageCR3 & ~0xFFF)) {
536                 Log_Error("MMVirt", "Can't clear current address space");
537                 return ;
538         }
539
540         if( MM_GetRefCount(CR3) > 1 ) {
541                 MM_DerefPhys(CR3);
542                 Log_Log("MMVirt", "CR3 %P is still referenced, not cleaning (but dereferenced)", CR3);
543                 return ;
544         }
545
546         Log_Debug("MMVirt", "Clearing out address space 0x%x from 0x%x", CR3, *gpPageCR3);
547         
548         GET_TEMP_MAPPING(CR3);
549         INVLPG( gaTmpDir );
550
551         for( i = 0; i < 1024; i ++ )
552         {
553                 Uint32  *table = &gaTmpTable[i*1024];
554                 if( !(gaTmpDir[i] & PF_PRESENT) )
555                         continue ;
556
557                 INVLPG( table );        
558
559                 if( i < 768 || (i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) )
560                 {
561                         for( j = 0; j < 1024; j ++ )
562                         {
563                                 if( !(table[j] & 1) )
564                                         continue;
565                                 MM_DerefPhys( table[j] & ~0xFFF );
566                         }
567                 }
568
569                 if( i != (PAGE_TABLE_ADDR >> 22) )
570                 {               
571                         MM_DerefPhys( gaTmpDir[i] & ~0xFFF );
572                 }
573         }
574
575
576         MM_DerefPhys( CR3 );
577
578         REL_TEMP_MAPPING();
579 }
580
581 /**
582  * \fn tPAddr MM_Clone(void)
583  * \brief Clone the current address space
584  */
585 tPAddr MM_Clone(int bNoUserCopy)
586 {
587         Uint    i, j;
588         tPAddr  ret;
589         Uint    page = 0;
590         tVAddr  kStackBase = Proc_GetCurThread()->KernelStack - MM_KERNEL_STACK_SIZE;
591         void    *tmp;
592         
593         // Create Directory Table
594         ret = MM_AllocPhys();
595         if( ret == 0 ) {
596                 return 0;
597         }
598         
599         // Map
600         GET_TEMP_MAPPING( ret );
601         INVLPG( gaTmpDir );
602         memsetd( gaTmpDir, 0, 1024 );
603         
604         if( Threads_GetPID() != 0 && !bNoUserCopy )
605         {       
606                 // Copy Tables
607                 for( i = 0; i < 768; i ++)
608                 {
609                         // Check if table is allocated
610                         if( !(gaPageDir[i] & PF_PRESENT) ) {
611                                 gaTmpDir[i] = 0;
612                                 page += 1024;
613                                 continue;
614                         }
615                         
616                         // Allocate new table
617                         gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
618                         INVLPG( &gaTmpTable[page] );
619                         // Fill
620                         for( j = 0; j < 1024; j ++, page++ )
621                         {
622                                 if( !(gaPageTable[page] & PF_PRESENT) ) {
623                                         gaTmpTable[page] = 0;
624                                         continue;
625                                 }
626                                 
627                                 // Refrence old page
628                                 MM_RefPhys( gaPageTable[page] & ~0xFFF );
629                                 // Add to new table
630                                 if(gaPageTable[page] & PF_WRITE) {
631                                         gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
632                                         gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
633                                         INVLPG( page << 12 );
634                                 }
635                                 else
636                                         gaTmpTable[page] = gaPageTable[page];
637                         }
638                 }
639         }
640         
641         // Map in kernel tables (and make fractal mapping)
642         for( i = 768; i < 1024; i ++ )
643         {
644                 // Fractal
645                 if( i == (PAGE_TABLE_ADDR >> 22) ) {
646                         gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
647                         continue;
648                 }
649                 if( i == (TMP_TABLE_ADDR >> 22) ) {
650                         gaTmpDir[ TMP_TABLE_ADDR >> 22 ] = 0;
651                         continue ;
652                 }
653                 
654                 if( gaPageDir[i] == 0 ) {
655                         gaTmpDir[i] = 0;
656                         continue;
657                 }
658                 
659                 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
660                 MM_RefPhys( gaPageDir[i] & ~0xFFF );
661                 gaTmpDir[i] = gaPageDir[i];
662         }
663         
664         // Allocate kernel stack
665         for(i = MM_KERNEL_STACKS >> 22; i < MM_KERNEL_STACKS_END >> 22; i ++ )
666         {
667                 // Check if directory is allocated
668                 if( (gaPageDir[i] & 1) == 0 ) {
669                         gaTmpDir[i] = 0;
670                         continue;
671                 }               
672                 
673                 // We don't care about other kernel stacks, just the current one
674                 if( i != kStackBase >> 22 ) {
675                         MM_DerefPhys( gaPageDir[i] & ~0xFFF );
676                         gaTmpDir[i] = 0;
677                         continue;
678                 }
679                 
680                 // Create a copy
681                 gaTmpDir[i] = MM_AllocPhys() | 3;
682                 INVLPG( &gaTmpTable[i*1024] );
683                 for( j = 0; j < 1024; j ++ )
684                 {
685                         // Is the page allocated? If not, skip
686                         if( !(gaPageTable[i*1024+j] & 1) ) {
687                                 gaTmpTable[i*1024+j] = 0;
688                                 continue;
689                         }
690                         
691                         // We don't care about other kernel stacks
692                         if( ((i*1024+j)*4096 & ~(MM_KERNEL_STACK_SIZE-1)) != kStackBase ) {
693                                 gaTmpTable[i*1024+j] = 0;
694                                 continue;
695                         }
696                         
697                         // Allocate page
698                         gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
699                         
700                         MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
701                         
702                         tmp = MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
703                         memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
704                         MM_FreeTemp( tmp );
705                 }
706         }
707         
708         REL_TEMP_MAPPING();
709         
710         //LEAVE('x', ret);
711         return ret;
712 }
713
714 /**
715  * \fn tVAddr MM_NewKStack(void)
716  * \brief Create a new kernel stack
717  */
718 tVAddr MM_NewKStack(void)
719 {
720         tVAddr  base;
721         Uint    i;
722         for(base = MM_KERNEL_STACKS; base < MM_KERNEL_STACKS_END; base += MM_KERNEL_STACK_SIZE)
723         {
724                 // Check if space is free
725                 if(MM_GetPhysAddr( (void*) base) != 0)
726                         continue;
727                 // Allocate
728                 //for(i = MM_KERNEL_STACK_SIZE; i -= 0x1000 ; )
729                 for(i = 0; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
730                 {
731                         if( MM_Allocate(base+i) == 0 )
732                         {
733                                 // On error, print a warning and return error
734                                 Warning("MM_NewKStack - Out of memory");
735                                 // - Clean up
736                                 //for( i += 0x1000 ; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
737                                 //      MM_Deallocate(base+i);
738                                 return 0;
739                         }
740                 }
741                 // Success
742 //              Log("MM_NewKStack - Allocated %p", base + MM_KERNEL_STACK_SIZE);
743                 return base+MM_KERNEL_STACK_SIZE;
744         }
745         // No stacks left
746         Log_Warning("MMVirt", "MM_NewKStack - No address space left");
747         return 0;
748 }
749
750 /**
751  * \fn tVAddr MM_NewWorkerStack()
752  * \brief Creates a new worker stack
753  */
754 tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize)
755 {
756         Uint    base, addr;
757         tVAddr  tmpPage;
758         tPAddr  page;
759         
760         // TODO: Thread safety
761         // Find a free worker stack address
762         for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
763         {
764                 // Used block
765                 if( gWorkerStacks[base/32] == -1 ) {
766                         base += 31;     base &= ~31;
767                         base --;        // Counteracted by the base++
768                         continue;
769                 }
770                 // Used stack
771                 if( gWorkerStacks[base/32] & (1 << base) ) {
772                         continue;
773                 }
774                 break;
775         }
776         if(base >= NUM_WORKER_STACKS) {
777                 Warning("Uh-oh! Out of worker stacks");
778                 return 0;
779         }
780         
781         // It's ours now!
782         gWorkerStacks[base/32] |= (1 << base);
783         // Make life easier for later calls
784         giLastUsedWorker = base;
785         // We have one
786         base = WORKER_STACKS + base * WORKER_STACK_SIZE;
787         //Log(" MM_NewWorkerStack: base = 0x%x", base);
788         
789         // Set the temp fractals to TID0's address space
790         GET_TEMP_MAPPING( ((Uint)gaInitPageDir - KERNEL_BASE) );
791         INVLPG( gaTmpDir );
792         
793         // Check if the directory is mapped (we are assuming that the stacks
794         // will fit neatly in a directory)
795         //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
796         if(gaTmpDir[ base >> 22 ] == 0) {
797                 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
798                 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
799         }
800         
801         // Mapping Time!
802         for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
803         {
804                 page = MM_AllocPhys();
805                 gaTmpTable[ (base + addr) >> 12 ] = page | 3;
806         }
807
808         // Release temporary fractal
809         REL_TEMP_MAPPING();
810
811         // NOTE: Max of 1 page
812         // `page` is the last allocated page from the previious for loop
813         tmpPage = (tVAddr)MM_MapTemp( page );
814         memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize);
815         MM_FreeTemp( (void*)tmpPage );
816         
817         //Log("MM_NewWorkerStack: RETURN 0x%x", base);
818         return base + WORKER_STACK_SIZE;
819 }
820
821 /**
822  * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
823  * \brief Sets the flags on a page
824  */
825 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
826 {
827         tTabEnt *ent;
828         if( !(gaPageDir[VAddr >> 22] & 1) )     return ;
829         if( !(gaPageTable[VAddr >> 12] & 1) )   return ;
830         
831         ent = &gaPageTable[VAddr >> 12];
832         
833         // Read-Only
834         if( Mask & MM_PFLAG_RO )
835         {
836                 if( Flags & MM_PFLAG_RO ) {
837                         *ent &= ~PF_WRITE;
838                 }
839                 else {
840                         gaPageDir[VAddr >> 22] |= PF_WRITE;
841                         *ent |= PF_WRITE;
842                 }
843         }
844         
845         // Kernel
846         if( Mask & MM_PFLAG_KERNEL )
847         {
848                 if( Flags & MM_PFLAG_KERNEL ) {
849                         *ent &= ~PF_USER;
850                 }
851                 else {
852                         gaPageDir[VAddr >> 22] |= PF_USER;
853                         *ent |= PF_USER;
854                 }
855         }
856         
857         // Copy-On-Write
858         if( Mask & MM_PFLAG_COW )
859         {
860                 if( Flags & MM_PFLAG_COW ) {
861                         *ent &= ~PF_WRITE;
862                         *ent |= PF_COW;
863                 }
864                 else {
865                         *ent &= ~PF_COW;
866                         *ent |= PF_WRITE;
867                 }
868         }
869         
870         //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
871         //      *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
872 }
873
874 /**
875  * \brief Get the flags on a page
876  */
877 Uint MM_GetFlags(tVAddr VAddr)
878 {
879         tTabEnt *ent;
880         Uint    ret = 0;
881         
882         // Validity Check
883         if( !(gaPageDir[VAddr >> 22] & 1) )     return 0;
884         if( !(gaPageTable[VAddr >> 12] & 1) )   return 0;
885         
886         ent = &gaPageTable[VAddr >> 12];
887         
888         // Read-Only
889         if( !(*ent & PF_WRITE) )        ret |= MM_PFLAG_RO;
890         // Kernel
891         if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
892         // Copy-On-Write
893         if( *ent & PF_COW )     ret |= MM_PFLAG_COW;
894         
895         return ret;
896 }
897
898 /**
899  * \brief Check if the provided buffer is valid
900  * \return Boolean valid
901  */
902 int MM_IsValidBuffer(tVAddr Addr, size_t Size)
903 {
904          int    bIsUser;
905          int    dir, tab;
906
907         Size += Addr & (PAGE_SIZE-1);
908         Addr &= ~(PAGE_SIZE-1);
909
910         dir = Addr >> 22;
911         tab = Addr >> 12;
912         
913 //      Debug("Addr = %p, Size = 0x%x, dir = %i, tab = %i", Addr, Size, dir, tab);
914
915         if( !(gaPageDir[dir] & 1) )     return 0;
916         if( !(gaPageTable[tab] & 1) )   return 0;
917         
918         bIsUser = !!(gaPageTable[tab] & PF_USER);
919
920         while( Size >= PAGE_SIZE )
921         {
922                 if( (tab & 1023) == 0 )
923                 {
924                         dir ++;
925                         if( !(gaPageDir[dir] & 1) )     return 0;
926                 }
927                 
928                 if( !(gaPageTable[tab] & 1) )   return 0;
929                 if( bIsUser && !(gaPageTable[tab] & PF_USER) )  return 0;
930
931                 tab ++;
932                 Size -= PAGE_SIZE;
933         }
934         return 1;
935 }
936
937 /**
938  * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
939  * \brief Duplicates a virtual page to a physical one
940  */
941 tPAddr MM_DuplicatePage(tVAddr VAddr)
942 {
943         tPAddr  ret;
944         void    *temp;
945          int    wasRO = 0;
946         
947         //ENTER("xVAddr", VAddr);
948         
949         // Check if mapped
950         if( !(gaPageDir  [VAddr >> 22] & PF_PRESENT) )  return 0;
951         if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) )  return 0;
952         
953         // Page Align
954         VAddr &= ~0xFFF;
955         
956         // Allocate new page
957         ret = MM_AllocPhys();
958         if( !ret ) {
959                 return 0;
960         }
961         
962         // Write-lock the page (to keep data constistent), saving its R/W state
963         wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
964         gaPageTable[VAddr >> 12] &= ~PF_WRITE;
965         INVLPG( VAddr );
966         
967         // Copy Data
968         temp = MM_MapTemp(ret);
969         memcpy( temp, (void*)VAddr, 0x1000 );
970         MM_FreeTemp(temp);
971         
972         // Restore Writeable status
973         if(!wasRO)      gaPageTable[VAddr >> 12] |= PF_WRITE;
974         INVLPG(VAddr);
975         
976         //LEAVE('X', ret);
977         return ret;
978 }
979
980 /**
981  * \fn Uint MM_MapTemp(tPAddr PAddr)
982  * \brief Create a temporary memory mapping
983  * \todo Show Luigi Barone (C Lecturer) and see what he thinks
984  */
985 void * MM_MapTemp(tPAddr PAddr)
986 {
987         //ENTER("XPAddr", PAddr);
988         
989         PAddr &= ~0xFFF;
990         
991         //LOG("glTempMappings = %i", glTempMappings);
992         
993         if( Semaphore_Wait(&gTempMappingsSem, 1) != 1 )
994                 return NULL;
995         Mutex_Acquire( &glTempMappings );
996         for( int i = 0; i < NUM_TEMP_PAGES; i ++ )
997         {
998                 // Check if page used
999                 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1)        continue;
1000                 // Mark as used
1001                 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
1002                 INVLPG( TEMP_MAP_ADDR + (i << 12) );
1003                 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
1004                 Mutex_Release( &glTempMappings );
1005                 return (void*)( TEMP_MAP_ADDR + (i << 12) );
1006         }
1007         Mutex_Release( &glTempMappings );
1008         Log_KernelPanic("MMVirt", "Semaphore suplied a mapping, but none are avaliable");
1009         return NULL;
1010 }
1011
1012 /**
1013  * \fn void MM_FreeTemp(tVAddr PAddr)
1014  * \brief Free's a temp mapping
1015  */
1016 void MM_FreeTemp(void *VAddr)
1017 {
1018          int    i = (tVAddr)VAddr >> 12;
1019         //ENTER("xVAddr", VAddr);
1020         
1021         if(i >= (TEMP_MAP_ADDR >> 12)) {
1022                 gaPageTable[ i ] = 0;
1023                 Semaphore_Signal(&gTempMappingsSem, 1);
1024         }
1025         
1026         //LEAVE('-');
1027 }
1028
1029 /**
1030  * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
1031  * \brief Allocates a contigous number of pages
1032  */
1033 void *MM_MapHWPages(tPAddr PAddr, Uint Number)
1034 {
1035          int    j;
1036         
1037         PAddr &= ~0xFFF;
1038
1039         if( PAddr < 1024*1024 && (1024*1024-PAddr) >= Number * PAGE_SIZE )
1040         {
1041                 return (void*)(KERNEL_BASE + PAddr);
1042         }
1043
1044         // Scan List
1045         for( int i = 0; i < NUM_HW_PAGES; i ++ )
1046         {               
1047                 // Check if addr used
1048                 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
1049                         continue;
1050                 
1051                 // Check possible region
1052                 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
1053                 {
1054                         // If there is an allocated page in the region we are testing, break
1055                         if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 )    break;
1056                 }
1057                 // Is it all free?
1058                 if( j == Number )
1059                 {
1060                         // Allocate
1061                         for( j = 0; j < Number; j++ ) {
1062                                 MM_RefPhys( PAddr + (j<<12) );
1063                                 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
1064                         }
1065                         return (void*)(HW_MAP_ADDR + (i<<12));
1066                 }
1067         }
1068         // If we don't find any, return NULL
1069         return 0;
1070 }
1071
1072 /**
1073  * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1074  * \brief Allocates DMA physical memory
1075  * \param Pages Number of pages required
1076  * \param MaxBits       Maximum number of bits the physical address can have
1077  * \param PhysAddr      Pointer to the location to place the physical address allocated
1078  * \return Virtual address allocate
1079  */
1080 void *MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1081 {
1082         tPAddr  phys;
1083         void    *ret;
1084         
1085         ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1086         
1087         if(MaxBits == -1)
1088                 MaxBits = PHYS_BITS;
1089         
1090         // Sanity Check
1091         if(MaxBits < 12) {
1092                 LEAVE('i', 0);
1093                 return 0;
1094         }
1095         
1096         // Fast Allocate
1097         if(Pages == 1 && MaxBits >= PHYS_BITS)
1098         {
1099                 phys = MM_AllocPhys();
1100                 if( PhysAddr )
1101                         *PhysAddr = phys;
1102                 if( !phys ) {
1103                         LEAVE_RET('i', 0);
1104                 }
1105                 ret = MM_MapHWPages(phys, 1);
1106                 if(ret == 0) {
1107                         MM_DerefPhys(phys);
1108                         LEAVE('i', 0);
1109                         return 0;
1110                 }
1111                 LEAVE('x', ret);
1112                 return (void*)ret;
1113         }
1114         
1115         // Slow Allocate
1116         phys = MM_AllocPhysRange(Pages, MaxBits);
1117         // - Was it allocated?
1118         if(phys == 0) {
1119                 LEAVE('i', 0);
1120                 return 0;
1121         }
1122         
1123         // Allocated successfully, now map
1124         ret = MM_MapHWPages(phys, Pages);
1125         if( ret == 0 ) {
1126                 // If it didn't map, free then return 0
1127                 for(;Pages--;phys+=0x1000)
1128                         MM_DerefPhys(phys);
1129                 LEAVE('i', 0);
1130                 return 0;
1131         }
1132         
1133         if( PhysAddr )
1134                 *PhysAddr = phys;
1135         LEAVE('x', ret);
1136         return (void*)ret;
1137 }
1138
1139 /**
1140  * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1141  * \brief Unmap a hardware page
1142  */
1143 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1144 {
1145          int    i, j;
1146         
1147         //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1148
1149         //
1150         if( KERNEL_BASE <= VAddr && VAddr < KERNEL_BASE + 1024*1024 )
1151                 return ;        
1152
1153         // Sanity Check
1154         if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX)     return;
1155         
1156         i = VAddr >> 12;
1157         
1158         Mutex_Acquire( &glTempMappings );       // Temp and HW share a directory, so they share a lock
1159         
1160         for( j = 0; j < Number; j++ )
1161         {
1162                 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1163                 gaPageTable[ i + j ] = 0;
1164                 INVLPG( (tVAddr)(i+j) << 12 );
1165         }
1166         
1167         Mutex_Release( &glTempMappings );
1168 }
1169

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