2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
23 #define WORKER_STACKS 0x00100000 // Thread0 Only!
24 #define WORKER_STACK_SIZE MM_KERNEL_STACK_SIZE
25 #define WORKER_STACKS_END 0xB0000000
26 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
28 #define PAE_PAGE_TABLE_ADDR 0xFC000000 // 16 MiB
29 #define PAE_PAGE_DIR_ADDR 0xFCFC0000 // 16 KiB
30 #define PAE_PAGE_PDPT_ADDR 0xFCFC3F00 // 32 bytes
31 #define PAE_TMP_PDPT_ADDR 0xFCFC3F20 // 32 bytes
32 #define PAE_TMP_DIR_ADDR 0xFCFE0000 // 16 KiB
33 #define PAE_TMP_TABLE_ADDR 0xFD000000 // 16 MiB
35 #define PAGE_TABLE_ADDR 0xFC000000
36 #define PAGE_DIR_ADDR 0xFC3F0000
37 #define PAGE_CR3_ADDR 0xFC3F0FC0
38 #define TMP_CR3_ADDR 0xFC3F0FC4 // Part of core instead of temp
39 #define TMP_DIR_ADDR 0xFC3F1000 // Same
40 #define TMP_TABLE_ADDR 0xFC400000
42 #define HW_MAP_ADDR 0xFE000000
43 #define HW_MAP_MAX 0xFFEF0000
44 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
45 #define TEMP_MAP_ADDR 0xFFEF0000 // Allows 16 "temp" pages
46 #define NUM_TEMP_PAGES 16
47 #define LAST_BLOCK_ADDR 0xFFFF0000 // Free space for kernel provided user code/ *(-1) protection
49 #define PF_PRESENT 0x1
52 #define PF_GLOBAL 0x80
54 #define PF_NOPAGE 0x400
56 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
58 #define GET_TEMP_MAPPING(cr3) do { \
60 __AtomicTestSetLoop( (Uint *)gpTmpCR3, cr3 | 3 ); \
62 #define REL_TEMP_MAPPING() do { \
67 typedef Uint32 tTabEnt;
70 extern char _UsertextEnd[], _UsertextBase[];
71 extern Uint32 gaInitPageDir[1024];
72 extern Uint32 gaInitPageTable[1024];
73 extern void Threads_SegFault(tVAddr Addr);
74 extern void Error_Backtrace(Uint eip, Uint ebp);
77 void MM_PreinitVirtual(void);
78 void MM_InstallVirtual(void);
79 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
80 //void MM_DumpTables(tVAddr Start, tVAddr End);
81 //void MM_ClearUser(void);
82 tPAddr MM_DuplicatePage(tVAddr VAddr);
85 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
86 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
87 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
88 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
89 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
90 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
92 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
93 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
94 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
95 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
96 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
97 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
99 tMutex glTempMappings;
100 tMutex glTempFractal;
101 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
102 int giLastUsedWorker = 0;
109 } *gaMappedRegions; // sizeof = 24 bytes
113 * \fn void MM_PreinitVirtual(void)
114 * \brief Maps the fractal mappings
116 void MM_PreinitVirtual(void)
118 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
119 INVLPG( PAGE_TABLE_ADDR );
123 * \fn void MM_InstallVirtual(void)
124 * \brief Sets up the constant page mappings
126 void MM_InstallVirtual(void)
130 // --- Pre-Allocate kernel tables
131 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
133 if( gaPageDir[ i ] ) continue;
134 // Skip stack tables, they are process unique
135 if( i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) {
140 gaPageDir[ i ] = MM_AllocPhys() | 3;
141 INVLPG( &gaPageTable[i*1024] );
142 memset( &gaPageTable[i*1024], 0, 0x1000 );
145 // Unset kernel on the User Text pages
146 for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
147 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
154 * \brief Cleans up the SMP required mappings
156 void MM_FinishVirtualInit(void)
158 gaInitPageDir[ 0 ] = 0;
162 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
163 * \brief Called on a page fault
165 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
167 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
169 // -- Check for COW --
170 if( gaPageDir [Addr>>22] & PF_PRESENT && gaPageTable[Addr>>12] & PF_PRESENT
171 && gaPageTable[Addr>>12] & PF_COW )
174 __asm__ __volatile__ ("sti");
175 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
177 gaPageTable[Addr>>12] &= ~PF_COW;
178 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
182 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
183 paddr = MM_DuplicatePage( Addr );
184 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
185 gaPageTable[Addr>>12] &= PF_USER;
186 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
189 // Log_Debug("MMVirt", "COW for %p (%P)", Addr, gaPageTable[Addr>>12]);
191 INVLPG( Addr & ~0xFFF );
195 // Disable instruction tracing
196 __ASM__("pushf; andw $0xFEFF, 0(%esp); popf");
197 Proc_GetCurThread()->bInstrTrace = 0;
199 // If it was a user, tell the thread handler
201 __asm__ __volatile__ ("sti");
202 Log_Warning("MMVirt", "User %s %s memory%s",
203 (ErrorCode&2?"write to":"read from"),
204 (ErrorCode&1?"bad/locked":"non-present"),
205 (ErrorCode&16?" (Instruction Fetch)":"")
207 Log_Warning("MMVirt", "Instruction %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
208 __ASM__("sti"); // Restart IRQs
210 Error_Backtrace(Regs->eip, Regs->ebp);
212 Threads_SegFault(Addr);
218 // -- Check Error Code --
220 Warning("Reserved Bits Trashed!");
223 Warning("Kernel %s %s memory%s",
224 (ErrorCode&2?"write to":"read from"),
225 (ErrorCode&1?"bad/locked":"non-present"),
226 (ErrorCode&16?" (Instruction Fetch)":"")
230 Log("CPU %i - Code at %p accessed %p", GetCPUNum(), Regs->eip, Addr);
231 // Print Stack Backtrace
232 Error_Backtrace(Regs->eip, Regs->ebp);
235 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
236 if( gaPageDir[Addr>>22] & PF_PRESENT )
237 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
239 //MM_DumpTables(0, -1);
242 Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
243 Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
244 //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
245 Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
246 Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
249 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
250 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
251 Log("DR0 %08x DR1 %08x", dr0, dr1);
254 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
258 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
259 * \brief Dumps the layout of the page tables
261 void MM_DumpTables(tVAddr Start, tVAddr End)
263 tVAddr rangeStart = 0;
265 void *expected_node = NULL, *tmpnode = NULL;
268 const tPAddr MASK = ~0xF78;
270 Start >>= 12; End >>= 12;
273 Log("Directory Entries:");
274 for(page = Start >> 10;
275 page < (End >> 10)+1;
280 Log(" 0x%08x-0x%08x :: 0x%08x",
281 page<<22, ((page+1)<<22)-1,
282 gaPageDir[page]&~0xFFF
288 Log("Table Entries:");
289 for(page = Start, curPos = Start<<12;
291 curPos += 0x1000, page++)
293 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
294 || !(gaPageTable[page] & PF_PRESENT)
295 || (gaPageTable[page] & MASK) != expected
296 || (tmpnode=NULL,MM_GetPageNode(expected, &tmpnode), tmpnode != expected_node))
299 tPAddr orig = gaPageTable[rangeStart>>12];
300 Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
304 (orig & PF_NOPAGE ? "P" : "-"),
305 (orig & PF_COW ? "C" : "-"),
306 (orig & PF_GLOBAL ? "G" : "-"),
307 (orig & PF_USER ? "U" : "-"),
308 (orig & PF_WRITE ? "W" : "-"),
313 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
314 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
316 expected = (gaPageTable[page] & MASK);
317 MM_GetPageNode(expected, &expected_node);
320 if(expected) expected += 0x1000;
324 tPAddr orig = gaPageTable[rangeStart>>12];
325 Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
329 (orig & PF_NOPAGE ? "p" : "-"),
330 (orig & PF_COW ? "C" : "-"),
331 (orig & PF_GLOBAL ? "G" : "-"),
332 (orig & PF_USER ? "U" : "-"),
333 (orig & PF_WRITE ? "W" : "-"),
341 * \fn tPAddr MM_Allocate(tVAddr VAddr)
343 tPAddr MM_Allocate(tVAddr VAddr)
346 //ENTER("xVAddr", VAddr);
347 //__ASM__("xchg %bx,%bx");
348 // Check if the directory is mapped
349 if( gaPageDir[ VAddr >> 22 ] == 0 )
351 // Allocate directory
352 paddr = MM_AllocPhys();
354 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
358 // Map and mark as user (if needed)
359 gaPageDir[ VAddr >> 22 ] = paddr | 3;
360 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
362 INVLPG( &gaPageDir[ VAddr >> 22 ] );
363 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
365 // Check if the page is already allocated
366 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
367 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
368 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
369 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
373 paddr = MM_AllocPhys();
374 //LOG("paddr = 0x%llx", paddr);
376 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
377 VAddr, __builtin_return_address(0));
382 gaPageTable[ VAddr >> 12 ] = paddr | 3;
384 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
385 // Invalidate Cache for address
386 INVLPG( VAddr & ~0xFFF );
393 * \fn void MM_Deallocate(tVAddr VAddr)
395 void MM_Deallocate(tVAddr VAddr)
397 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
398 Warning("MM_Deallocate - Directory not mapped");
402 if(gaPageTable[ VAddr >> 12 ] == 0) {
403 Warning("MM_Deallocate - Page is not allocated");
408 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
410 gaPageTable[ VAddr >> 12 ] = 0;
414 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
415 * \brief Checks if the passed address is accesable
417 tPAddr MM_GetPhysAddr(tVAddr Addr)
419 if( !(gaPageDir[Addr >> 22] & 1) )
421 if( !(gaPageTable[Addr >> 12] & 1) )
423 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
427 * \fn void MM_SetCR3(Uint CR3)
428 * \brief Sets the current process space
430 void MM_SetCR3(Uint CR3)
432 __ASM__("mov %0, %%cr3"::"r"(CR3));
436 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
437 * \brief Map a physical page to a virtual one
439 int MM_Map(tVAddr VAddr, tPAddr PAddr)
441 //ENTER("xVAddr xPAddr", VAddr, PAddr);
443 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
444 Log_Warning("MM_Virt", "MM_Map - Physical or Virtual Addresses are not aligned (0x%P and %p)",
451 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
453 // Check if the directory is mapped
454 if( gaPageDir[ VAddr >> 22 ] == 0 )
456 tPAddr tmp = MM_AllocPhys();
459 gaPageDir[ VAddr >> 22 ] = tmp | 3;
462 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
464 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
465 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
467 // Check if the page is already allocated
468 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
469 Warning("MM_Map - Allocating to used address");
475 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
477 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
479 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
480 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
485 //LOG("INVLPG( 0x%x )", VAddr);
493 * \brief Clear user's address space
495 void MM_ClearUser(void)
499 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
501 // Check if directory is not allocated
502 if( !(gaPageDir[i] & PF_PRESENT) ) {
508 for( j = 0; j < 1024; j ++ )
510 if( gaPageTable[i*1024+j] & 1 )
511 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
512 gaPageTable[i*1024+j] = 0;
515 // Deallocate directory
516 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
518 INVLPG( &gaPageTable[i*1024] );
524 * \brief Deallocate an address space
526 void MM_ClearSpace(Uint32 CR3)
530 if(CR3 == (*gpPageCR3 & ~0xFFF)) {
531 Log_Error("MMVirt", "Can't clear current address space");
535 if( MM_GetRefCount(CR3) > 1 ) {
537 Log_Log("MMVirt", "CR3 %P is still referenced, not cleaning (but dereferenced)", CR3);
541 Log_Debug("MMVirt", "Clearing out address space 0x%x from 0x%x", CR3, *gpPageCR3);
543 GET_TEMP_MAPPING(CR3);
546 for( i = 0; i < 1024; i ++ )
548 Uint32 *table = &gaTmpTable[i*1024];
549 if( !(gaTmpDir[i] & PF_PRESENT) )
554 if( i < 768 || (i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) )
556 for( j = 0; j < 1024; j ++ )
558 if( !(table[j] & 1) )
560 MM_DerefPhys( table[j] & ~0xFFF );
564 if( i != (PAGE_TABLE_ADDR >> 22) )
566 MM_DerefPhys( gaTmpDir[i] & ~0xFFF );
577 * \fn tPAddr MM_Clone(void)
578 * \brief Clone the current address space
580 tPAddr MM_Clone(int bNoUserCopy)
585 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - MM_KERNEL_STACK_SIZE;
588 // Create Directory Table
589 ret = MM_AllocPhys();
595 GET_TEMP_MAPPING( ret );
597 memsetd( gaTmpDir, 0, 1024 );
599 if( Threads_GetPID() != 0 && !bNoUserCopy )
602 for( i = 0; i < 768; i ++)
604 // Check if table is allocated
605 if( !(gaPageDir[i] & PF_PRESENT) ) {
611 // Allocate new table
612 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
613 INVLPG( &gaTmpTable[page] );
615 for( j = 0; j < 1024; j ++, page++ )
617 if( !(gaPageTable[page] & PF_PRESENT) ) {
618 gaTmpTable[page] = 0;
623 MM_RefPhys( gaPageTable[page] & ~0xFFF );
625 if(gaPageTable[page] & PF_WRITE) {
626 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
627 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
628 INVLPG( page << 12 );
631 gaTmpTable[page] = gaPageTable[page];
636 // Map in kernel tables (and make fractal mapping)
637 for( i = 768; i < 1024; i ++ )
640 if( i == (PAGE_TABLE_ADDR >> 22) ) {
641 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
644 if( i == (TMP_TABLE_ADDR >> 22) ) {
645 gaTmpDir[ TMP_TABLE_ADDR >> 22 ] = 0;
649 if( gaPageDir[i] == 0 ) {
654 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
655 MM_RefPhys( gaPageDir[i] & ~0xFFF );
656 gaTmpDir[i] = gaPageDir[i];
659 // Allocate kernel stack
660 for(i = MM_KERNEL_STACKS >> 22; i < MM_KERNEL_STACKS_END >> 22; i ++ )
662 // Check if directory is allocated
663 if( (gaPageDir[i] & 1) == 0 ) {
668 // We don't care about other kernel stacks, just the current one
669 if( i != kStackBase >> 22 ) {
670 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
676 gaTmpDir[i] = MM_AllocPhys() | 3;
677 INVLPG( &gaTmpTable[i*1024] );
678 for( j = 0; j < 1024; j ++ )
680 // Is the page allocated? If not, skip
681 if( !(gaPageTable[i*1024+j] & 1) ) {
682 gaTmpTable[i*1024+j] = 0;
686 // We don't care about other kernel stacks
687 if( ((i*1024+j)*4096 & ~(MM_KERNEL_STACK_SIZE-1)) != kStackBase ) {
688 gaTmpTable[i*1024+j] = 0;
693 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
695 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
697 tmp = MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
698 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
710 * \fn tVAddr MM_NewKStack(void)
711 * \brief Create a new kernel stack
713 tVAddr MM_NewKStack(void)
717 for(base = MM_KERNEL_STACKS; base < MM_KERNEL_STACKS_END; base += MM_KERNEL_STACK_SIZE)
719 // Check if space is free
720 if(MM_GetPhysAddr(base) != 0) continue;
722 //for(i = MM_KERNEL_STACK_SIZE; i -= 0x1000 ; )
723 for(i = 0; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
725 if( MM_Allocate(base+i) == 0 )
727 // On error, print a warning and return error
728 Warning("MM_NewKStack - Out of memory");
730 //for( i += 0x1000 ; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
731 // MM_Deallocate(base+i);
736 // Log("MM_NewKStack - Allocated %p", base + MM_KERNEL_STACK_SIZE);
737 return base+MM_KERNEL_STACK_SIZE;
740 Log_Warning("MMVirt", "MM_NewKStack - No address space left");
745 * \fn tVAddr MM_NewWorkerStack()
746 * \brief Creates a new worker stack
748 tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize)
754 // TODO: Thread safety
755 // Find a free worker stack address
756 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
759 if( gWorkerStacks[base/32] == -1 ) {
760 base += 31; base &= ~31;
761 base --; // Counteracted by the base++
765 if( gWorkerStacks[base/32] & (1 << base) ) {
770 if(base >= NUM_WORKER_STACKS) {
771 Warning("Uh-oh! Out of worker stacks");
776 gWorkerStacks[base/32] |= (1 << base);
777 // Make life easier for later calls
778 giLastUsedWorker = base;
780 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
781 //Log(" MM_NewWorkerStack: base = 0x%x", base);
783 // Set the temp fractals to TID0's address space
784 GET_TEMP_MAPPING( ((Uint)gaInitPageDir - KERNEL_BASE) );
787 // Check if the directory is mapped (we are assuming that the stacks
788 // will fit neatly in a directory)
789 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
790 if(gaTmpDir[ base >> 22 ] == 0) {
791 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
792 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
796 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
798 page = MM_AllocPhys();
799 gaTmpTable[ (base + addr) >> 12 ] = page | 3;
802 // Release temporary fractal
805 // NOTE: Max of 1 page
806 // `page` is the last allocated page from the previious for loop
807 tmpPage = (tVAddr)MM_MapTemp( page );
808 memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize);
809 MM_FreeTemp( (void*)tmpPage );
811 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
812 return base + WORKER_STACK_SIZE;
816 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
817 * \brief Sets the flags on a page
819 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
822 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
823 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
825 ent = &gaPageTable[VAddr >> 12];
828 if( Mask & MM_PFLAG_RO )
830 if( Flags & MM_PFLAG_RO ) {
834 gaPageDir[VAddr >> 22] |= PF_WRITE;
840 if( Mask & MM_PFLAG_KERNEL )
842 if( Flags & MM_PFLAG_KERNEL ) {
846 gaPageDir[VAddr >> 22] |= PF_USER;
852 if( Mask & MM_PFLAG_COW )
854 if( Flags & MM_PFLAG_COW ) {
864 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
865 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
869 * \brief Get the flags on a page
871 Uint MM_GetFlags(tVAddr VAddr)
877 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
878 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
880 ent = &gaPageTable[VAddr >> 12];
883 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
885 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
887 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
893 * \brief Check if the provided buffer is valid
894 * \return Boolean valid
896 int MM_IsValidBuffer(tVAddr Addr, size_t Size)
901 Size += Addr & (PAGE_SIZE-1);
902 Addr &= ~(PAGE_SIZE-1);
907 // Debug("Addr = %p, Size = 0x%x, dir = %i, tab = %i", Addr, Size, dir, tab);
909 if( !(gaPageDir[dir] & 1) ) return 0;
910 if( !(gaPageTable[tab] & 1) ) return 0;
912 bIsUser = !!(gaPageTable[tab] & PF_USER);
914 while( Size >= PAGE_SIZE )
916 if( (tab & 1023) == 0 )
919 if( !(gaPageDir[dir] & 1) ) return 0;
922 if( !(gaPageTable[tab] & 1) ) return 0;
923 if( bIsUser && !(gaPageTable[tab] & PF_USER) ) return 0;
932 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
933 * \brief Duplicates a virtual page to a physical one
935 tPAddr MM_DuplicatePage(tVAddr VAddr)
941 //ENTER("xVAddr", VAddr);
944 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
945 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
951 ret = MM_AllocPhys();
956 // Write-lock the page (to keep data constistent), saving its R/W state
957 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
958 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
962 temp = MM_MapTemp(ret);
963 memcpy( temp, (void*)VAddr, 0x1000 );
966 // Restore Writeable status
967 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
975 * \fn Uint MM_MapTemp(tPAddr PAddr)
976 * \brief Create a temporary memory mapping
977 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
979 void * MM_MapTemp(tPAddr PAddr)
983 //ENTER("XPAddr", PAddr);
987 //LOG("glTempMappings = %i", glTempMappings);
991 Mutex_Acquire( &glTempMappings );
993 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
995 // Check if page used
996 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
998 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
999 INVLPG( TEMP_MAP_ADDR + (i << 12) );
1000 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
1001 Mutex_Release( &glTempMappings );
1002 return (void*)( TEMP_MAP_ADDR + (i << 12) );
1004 Mutex_Release( &glTempMappings );
1005 Threads_Yield(); // TODO: Use a sleep queue here instead
1010 * \fn void MM_FreeTemp(tVAddr PAddr)
1011 * \brief Free's a temp mapping
1013 void MM_FreeTemp(void *VAddr)
1015 int i = (tVAddr)VAddr >> 12;
1016 //ENTER("xVAddr", VAddr);
1018 if(i >= (TEMP_MAP_ADDR >> 12))
1019 gaPageTable[ i ] = 0;
1025 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
1026 * \brief Allocates a contigous number of pages
1028 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
1035 for( i = 0; i < NUM_HW_PAGES; i ++ )
1037 // Check if addr used
1038 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
1041 // Check possible region
1042 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
1044 // If there is an allocated page in the region we are testing, break
1045 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
1051 for( j = 0; j < Number; j++ ) {
1052 MM_RefPhys( PAddr + (j<<12) );
1053 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
1055 return HW_MAP_ADDR + (i<<12);
1058 // If we don't find any, return NULL
1063 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1064 * \brief Allocates DMA physical memory
1065 * \param Pages Number of pages required
1066 * \param MaxBits Maximum number of bits the physical address can have
1067 * \param PhysAddr Pointer to the location to place the physical address allocated
1068 * \return Virtual address allocate
1070 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1072 tPAddr maxCheck = (1 << MaxBits);
1076 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1079 MaxBits = PHYS_BITS;
1082 if(MaxBits < 12 || !PhysAddr) {
1088 if(MaxBits >= PHYS_BITS) maxCheck = -1;
1091 if(Pages == 1 && MaxBits >= PHYS_BITS)
1093 phys = MM_AllocPhys();
1099 ret = MM_MapHWPages(phys, 1);
1110 phys = MM_AllocPhysRange(Pages, MaxBits);
1111 // - Was it allocated?
1117 // Allocated successfully, now map
1118 ret = MM_MapHWPages(phys, Pages);
1120 // If it didn't map, free then return 0
1121 for(;Pages--;phys+=0x1000)
1133 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1134 * \brief Unmap a hardware page
1136 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1140 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1143 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1147 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1149 for( j = 0; j < Number; j++ )
1151 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1152 gaPageTable[ i + j ] = 0;
1155 Mutex_Release( &glTempMappings );