2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
24 #define KERNEL_STACKS 0xF0000000
25 #define KERNEL_STACK_SIZE 0x00008000
26 #define KERNEL_STACKS_END 0xFC000000
27 #define WORKER_STACKS 0x00100000 // Thread0 Only!
28 #define WORKER_STACK_SIZE KERNEL_STACK_SIZE
29 #define WORKER_STACKS_END 0xB0000000
30 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
32 #define PAE_PAGE_TABLE_ADDR 0xFC000000 // 16 MiB
33 #define PAE_PAGE_DIR_ADDR 0xFCFC0000 // 16 KiB
34 #define PAE_PAGE_PDPT_ADDR 0xFCFC3F00 // 32 bytes
35 #define PAE_TMP_PDPT_ADDR 0xFCFC3F20 // 32 bytes
36 #define PAE_TMP_DIR_ADDR 0xFCFE0000 // 16 KiB
37 #define PAE_TMP_TABLE_ADDR 0xFD000000 // 16 MiB
39 #define PAGE_TABLE_ADDR 0xFC000000
40 #define PAGE_DIR_ADDR 0xFC3F0000
41 #define PAGE_CR3_ADDR 0xFC3F0FC0
42 #define TMP_CR3_ADDR 0xFC3F0FC4 // Part of core instead of temp
43 #define TMP_DIR_ADDR 0xFC3F1000 // Same
44 #define TMP_TABLE_ADDR 0xFC400000
46 #define HW_MAP_ADDR 0xFE000000
47 #define HW_MAP_MAX 0xFFEF0000
48 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
49 #define TEMP_MAP_ADDR 0xFFEF0000 // Allows 16 "temp" pages
50 #define NUM_TEMP_PAGES 16
51 #define LAST_BLOCK_ADDR 0xFFFF0000 // Free space for kernel provided user code/ *(-1) protection
53 #define PF_PRESENT 0x1
57 #define PF_PAGED 0x400
59 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
62 typedef Uint64 tTabEnt;
64 typedef Uint32 tTabEnt;
68 extern Uint32 gaInitPageDir[1024];
69 extern Uint32 gaInitPageTable[1024];
70 extern void Threads_SegFault(tVAddr Addr);
71 extern void Error_Backtrace(Uint eip, Uint ebp);
74 void MM_PreinitVirtual();
75 void MM_InstallVirtual();
76 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
77 void MM_DumpTables(tVAddr Start, tVAddr End);
78 tPAddr MM_DuplicatePage(tVAddr VAddr);
81 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
82 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
83 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
84 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
85 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
86 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
88 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
89 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
90 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
91 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
92 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
93 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
95 int gilTempMappings = 0;
96 int gilTempFractal = 0;
97 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
98 int giLastUsedWorker = 0;
102 * \fn void MM_PreinitVirtual()
103 * \brief Maps the fractal mappings
105 void MM_PreinitVirtual()
109 gaInitPageDir[ ((PAGE_TABLE_ADDR >> TAB)-3*512+3)*2 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
111 gaInitPageDir[ 0 ] = 0;
112 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
114 INVLPG( PAGE_TABLE_ADDR );
118 * \fn void MM_InstallVirtual()
119 * \brief Sets up the constant page mappings
121 void MM_InstallVirtual()
126 // --- Pre-Allocate kernel tables
127 for( i = KERNEL_BASE >> TAB; i < 1024*4; i ++ )
129 if( gaPAE_PageDir[ i ] ) continue;
131 // Skip stack tables, they are process unique
132 if( i > KERNEL_STACKS >> TAB && i < KERNEL_STACKS_END >> TAB) {
133 gaPAE_PageDir[ i ] = 0;
137 gaPAE_PageDir[ i ] = MM_AllocPhys() | 3;
138 INVLPG( &gaPAE_PageTable[i*512] );
139 memset( &gaPAE_PageTable[i*512], 0, 0x1000 );
142 // --- Pre-Allocate kernel tables
143 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
145 if( gaPageDir[ i ] ) continue;
146 // Skip stack tables, they are process unique
147 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
152 gaPageDir[ i ] = MM_AllocPhys() | 3;
153 INVLPG( &gaPageTable[i*1024] );
154 memset( &gaPageTable[i*1024], 0, 0x1000 );
160 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
161 * \brief Called on a page fault
163 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
165 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
167 // -- Check for COW --
168 if( gaPageDir [Addr>>22] & PF_PRESENT
169 && gaPageTable[Addr>>12] & PF_PRESENT
170 && gaPageTable[Addr>>12] & PF_COW )
173 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
175 gaPageTable[Addr>>12] &= ~PF_COW;
176 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
180 paddr = MM_DuplicatePage( Addr );
181 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
182 gaPageTable[Addr>>12] &= PF_USER;
183 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
186 INVLPG( Addr & ~0xFFF );
191 // If it was a user, tell the thread handler
193 Warning("%s %s %s memory%s",
194 (ErrorCode&4?"User":"Kernel"),
195 (ErrorCode&2?"write to":"read from"),
196 (ErrorCode&1?"bad/locked":"non-present"),
197 (ErrorCode&16?" (Instruction Fetch)":"")
199 Warning("User Pagefault: Instruction at %p accessed %p", Regs->eip, Addr);
200 __asm__ __volatile__ ("sti"); // Restart IRQs
201 Threads_SegFault(Addr);
205 // -- Check Error Code --
207 Warning("Reserved Bits Trashed!");
210 Warning("%s %s %s memory%s",
211 (ErrorCode&4?"User":"Kernel"),
212 (ErrorCode&2?"write to":"read from"),
213 (ErrorCode&1?"bad/locked":"non-present"),
214 (ErrorCode&16?" (Instruction Fetch)":"")
218 Log("Code at %p accessed %p", Regs->eip, Addr);
219 // Print Stack Backtrace
220 Error_Backtrace(Regs->eip, Regs->ebp);
222 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
223 if( gaPageDir[Addr>>22] & PF_PRESENT )
224 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
226 //MM_DumpTables(0, -1);
228 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
232 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
233 * \brief Dumps the layout of the page tables
235 void MM_DumpTables(tVAddr Start, tVAddr End)
237 tVAddr rangeStart = 0;
241 const tPAddr MASK = ~0xF98;
243 Start >>= 12; End >>= 12;
246 Log("Directory Entries:");
247 for(page = Start >> 10;
248 page < (End >> 10)+1;
253 Log(" 0x%08x-0x%08x :: 0x%08x",
254 page<<22, ((page+1)<<22)-1,
255 gaPageDir[page]&~0xFFF
261 Log("Table Entries:");
262 for(page = Start, curPos = Start<<12;
264 curPos += 0x1000, page++)
266 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
267 || !(gaPageTable[page] & PF_PRESENT)
268 || (gaPageTable[page] & MASK) != expected)
271 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
272 rangeStart, curPos - 1,
273 gaPageTable[rangeStart>>12] & ~0xFFF,
274 (expected & ~0xFFF) - 1,
275 (expected & PF_PAGED ? "p" : "-"),
276 (expected & PF_COW ? "C" : "-"),
277 (expected & PF_USER ? "U" : "-"),
278 (expected & PF_WRITE ? "W" : "-")
282 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
283 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
285 expected = (gaPageTable[page] & MASK);
288 if(expected) expected += 0x1000;
292 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
293 rangeStart, curPos - 1,
294 gaPageTable[rangeStart>>12] & ~0xFFF,
295 (expected & ~0xFFF) - 1,
296 (expected & PF_PAGED ? "p" : "-"),
297 (expected & PF_COW ? "C" : "-"),
298 (expected & PF_USER ? "U" : "-"),
299 (expected & PF_WRITE ? "W" : "-")
306 * \fn tPAddr MM_Allocate(tVAddr VAddr)
308 tPAddr MM_Allocate(tVAddr VAddr)
311 //ENTER("xVAddr", VAddr);
312 //__asm__ __volatile__ ("xchg %bx,%bx");
313 // Check if the directory is mapped
314 if( gaPageDir[ VAddr >> 22 ] == 0 )
316 // Allocate directory
317 paddr = MM_AllocPhys();
318 //LOG("paddr = 0x%llx (new table)", paddr);
320 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
325 gaPageDir[ VAddr >> 22 ] = paddr | 3;
327 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
329 INVLPG( &gaPageDir[ VAddr >> 22 ] );
330 //LOG("Clearing new table");
331 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
333 // Check if the page is already allocated
334 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
335 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
336 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
337 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
341 paddr = MM_AllocPhys();
342 //LOG("paddr = 0x%llx", paddr);
344 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
345 VAddr, __builtin_return_address(0));
350 gaPageTable[ VAddr >> 12 ] = paddr | 3;
352 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
353 // Invalidate Cache for address
354 INVLPG( VAddr & ~0xFFF );
361 * \fn void MM_Deallocate(tVAddr VAddr)
363 void MM_Deallocate(tVAddr VAddr)
365 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
366 Warning("MM_Deallocate - Directory not mapped");
370 if(gaPageTable[ VAddr >> 12 ] == 0) {
371 Warning("MM_Deallocate - Page is not allocated");
376 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
378 gaPageTable[ VAddr >> 12 ] = 0;
382 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
383 * \brief Checks if the passed address is accesable
385 tPAddr MM_GetPhysAddr(tVAddr Addr)
387 if( !(gaPageDir[Addr >> 22] & 1) )
389 if( !(gaPageTable[Addr >> 12] & 1) )
391 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
396 * \fn int MM_IsUser(tVAddr VAddr)
397 * \brief Checks if a page is user accessable
399 int MM_IsUser(tVAddr VAddr)
401 if( !(gaPageDir[VAddr >> 22] & 1) )
403 if( !(gaPageTable[VAddr >> 12] & 1) )
405 if( !(gaPageTable[VAddr >> 12] & PF_USER) )
411 * \fn void MM_SetCR3(tPAddr CR3)
412 * \brief Sets the current process space
414 void MM_SetCR3(tPAddr CR3)
416 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
420 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
421 * \brief Map a physical page to a virtual one
423 int MM_Map(tVAddr VAddr, tPAddr PAddr)
425 //ENTER("xVAddr xPAddr", VAddr, PAddr);
427 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
428 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
434 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
436 // Check if the directory is mapped
437 if( gaPageDir[ VAddr >> 22 ] == 0 )
439 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
442 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
444 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
445 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
447 // Check if the page is already allocated
448 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
449 Warning("MM_Map - Allocating to used address");
455 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
457 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
459 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
460 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
465 //LOG("INVLPG( 0x%x )", VAddr);
473 * \fn tVAddr MM_ClearUser()
474 * \brief Clear user's address space
476 tVAddr MM_ClearUser()
481 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
483 // Check if directory is not allocated
484 if( !(gaPageDir[i] & PF_PRESENT) ) {
490 for( j = 0; j < 1024; j ++ )
492 if( gaPageTable[i*1024+j] & 1 )
493 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
494 gaPageTable[i*1024+j] = 0;
497 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
499 INVLPG( &gaPageTable[i*1024] );
507 * \fn tPAddr MM_Clone()
508 * \brief Clone the current address space
515 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
518 LOCK( &gilTempFractal );
520 // Create Directory Table
521 *gpTmpCR3 = MM_AllocPhys() | 3;
523 //LOG("Allocated Directory (%x)", *gpTmpCR3);
524 memsetd( gaTmpDir, 0, 1024 );
529 // Check if table is allocated
530 if( !(gaPageDir[i] & PF_PRESENT) ) {
536 // Allocate new table
537 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
538 INVLPG( &gaTmpTable[page] );
540 for( j = 0; j < 1024; j ++, page++ )
542 if( !(gaPageTable[page] & PF_PRESENT) ) {
543 gaTmpTable[page] = 0;
548 MM_RefPhys( gaPageTable[page] & ~0xFFF );
550 if(gaPageTable[page] & PF_WRITE) {
551 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
552 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
553 INVLPG( page << 12 );
556 gaTmpTable[page] = gaPageTable[page];
560 // Map in kernel tables (and make fractal mapping)
561 for( i = 768; i < 1024; i ++ )
564 if( i == (PAGE_TABLE_ADDR >> 22) ) {
565 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
569 if( gaPageDir[i] == 0 ) {
574 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
575 MM_RefPhys( gaPageDir[i] & ~0xFFF );
576 gaTmpDir[i] = gaPageDir[i];
579 // Allocate kernel stack
580 for(i = KERNEL_STACKS >> 22;
581 i < KERNEL_STACKS_END >> 22;
584 // Check if directory is allocated
585 if( (gaPageDir[i] & 1) == 0 ) {
590 // We don't care about other kernel stacks, just the current one
591 if( i != kStackBase >> 22 ) {
592 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
598 gaTmpDir[i] = MM_AllocPhys() | 3;
599 INVLPG( &gaTmpTable[i*1024] );
600 for( j = 0; j < 1024; j ++ )
602 // Is the page allocated? If not, skip
603 if( !(gaPageTable[i*1024+j] & 1) ) {
604 gaTmpTable[i*1024+j] = 0;
608 // We don't care about other kernel stacks
609 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
610 gaTmpTable[i*1024+j] = 0;
615 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
617 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
619 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
620 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
621 MM_FreeTemp( (Uint)tmp );
625 ret = *gpTmpCR3 & ~0xFFF;
626 RELEASE( &gilTempFractal );
633 * \fn tVAddr MM_NewKStack()
634 * \brief Create a new kernel stack
636 tVAddr MM_NewKStack()
638 tVAddr base = KERNEL_STACKS;
640 for(;base<KERNEL_STACKS_END;base+=KERNEL_STACK_SIZE)
642 if(MM_GetPhysAddr(base) != 0) continue;
643 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
646 return base+KERNEL_STACK_SIZE;
648 Warning("MM_NewKStack - No address space left\n");
653 * \fn tVAddr MM_NewWorkerStack()
654 * \brief Creates a new worker stack
656 tVAddr MM_NewWorkerStack()
663 tPAddr pages[WORKER_STACK_SIZE>>12];
665 // Get the old ESP and EBP
666 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
667 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
669 // Find a free worker stack address
670 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
673 if( gWorkerStacks[base/32] == -1 ) {
674 base += 31; base &= ~31;
675 base --; // Counteracted by the base++
679 if( gWorkerStacks[base/32] & (1 << base) ) {
684 if(base >= NUM_WORKER_STACKS) {
685 Warning("Uh-oh! Out of worker stacks");
690 gWorkerStacks[base/32] |= (1 << base);
691 // Make life easier for later calls
692 giLastUsedWorker = base;
694 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
695 //Log(" MM_NewWorkerStack: base = 0x%x", base);
697 // Acquire the lock for the temp fractal mappings
698 LOCK(&gilTempFractal);
700 // Set the temp fractals to TID0's address space
701 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
702 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
706 // Check if the directory is mapped (we are assuming that the stacks
707 // will fit neatly in a directory)
708 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
709 if(gaTmpDir[ base >> 22 ] == 0) {
710 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
711 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
715 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
717 pages[ addr >> 12 ] = MM_AllocPhys();
718 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
721 // Release the temp mapping lock
722 RELEASE(&gilTempFractal);
724 // Copy the old stack
725 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
726 esp = oldstack - esp; // ESP as an offset in the stack
728 // Make `base` be the top of the stack
729 base += WORKER_STACK_SIZE;
731 i = (WORKER_STACK_SIZE>>12) - 1;
732 // Copy the contents of the old stack to the new one, altering the addresses
733 // `addr` is refering to bytes from the stack base (mem downwards)
734 for(addr = 0; addr < esp; addr += 0x1000)
736 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
737 tmpPage = (void*)MM_MapTemp( pages[i] );
739 for(j = 0; j < 1024; j++)
741 // Possible Stack address?
742 if(oldstack-esp < stack[j] && stack[j] < oldstack)
743 tmpPage[j] = base - (oldstack - stack[j]);
744 else // Seems not, best leave it alone
745 tmpPage[j] = stack[j];
747 MM_FreeTemp((tVAddr)tmpPage);
751 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
756 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
757 * \brief Sets the flags on a page
759 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
762 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
763 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
765 ent = &gaPageTable[VAddr >> 12];
768 if( Mask & MM_PFLAG_RO )
770 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
771 else *ent |= PF_WRITE;
775 if( Mask & MM_PFLAG_KERNEL )
777 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
778 else *ent |= PF_USER;
782 if( Mask & MM_PFLAG_COW )
784 if( Flags & MM_PFLAG_COW ) {
796 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
797 * \brief Duplicates a virtual page to a physical one
799 tPAddr MM_DuplicatePage(tVAddr VAddr)
806 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
807 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
813 ret = MM_AllocPhys();
815 // Write-lock the page (to keep data constistent), saving its R/W state
816 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
817 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
821 temp = MM_MapTemp(ret);
822 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
825 // Restore Writeable status
826 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
833 * \fn Uint MM_MapTemp(tPAddr PAddr)
834 * \brief Create a temporary memory mapping
835 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
837 tVAddr MM_MapTemp(tPAddr PAddr)
841 //ENTER("XPAddr", PAddr);
845 //LOG("gilTempMappings = %i", gilTempMappings);
849 LOCK( &gilTempMappings );
851 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
853 // Check if page used
854 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
856 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
857 INVLPG( TEMP_MAP_ADDR + (i << 12) );
858 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
859 RELEASE( &gilTempMappings );
860 return TEMP_MAP_ADDR + (i << 12);
862 RELEASE( &gilTempMappings );
868 * \fn void MM_FreeTemp(tVAddr PAddr)
869 * \brief Free's a temp mapping
871 void MM_FreeTemp(tVAddr VAddr)
874 //ENTER("xVAddr", VAddr);
876 if(i >= (TEMP_MAP_ADDR >> 12))
877 gaPageTable[ i ] = 0;
883 * \fn tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number)
884 * \brief Allocates a contigous number of pages
886 tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number)
893 for( i = 0; i < NUM_HW_PAGES; i ++ )
895 // Check if addr used
896 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
899 // Check possible region
900 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
902 // If there is an allocated page in the region we are testing, break
903 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
909 for( j = 0; j < Number; j++ ) {
910 MM_RefPhys( PAddr + (j<<12) );
911 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
913 return HW_MAP_ADDR + (i<<12);
916 // If we don't find any, return NULL
921 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
922 * \brief Allocates DMA physical memory
923 * \param Pages Number of pages required
924 * \param MaxBits Maximum number of bits the physical address can have
925 * \param PhysAddr Pointer to the location to place the physical address allocated
926 * \return Virtual address allocate
928 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
930 tPAddr maxCheck = (1 << MaxBits);
934 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
937 if(MaxBits < 12 || !PhysAddr) {
943 if(MaxBits >= PHYS_BITS) maxCheck = -1;
946 if(Pages == 1 && MaxBits >= PHYS_BITS)
948 phys = MM_AllocPhys();
950 ret = MM_MapHWPage(phys, 1);
961 phys = MM_AllocPhysRange(Pages, MaxBits);
962 // - Was it allocated?
968 // Allocated successfully, now map
969 ret = MM_MapHWPage(phys, Pages);
971 // If it didn't map, free then return 0
972 for(;Pages--;phys+=0x1000)
984 * \fn void MM_UnmapHWPage(tVAddr VAddr, Uint Number)
985 * \brief Unmap a hardware page
987 void MM_UnmapHWPage(tVAddr VAddr, Uint Number)
991 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
995 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
997 for( j = 0; j < Number; j++ )
999 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
1000 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
1003 RELEASE( &gilTempMappings );
1007 EXPORT(MM_GetPhysAddr);
1010 EXPORT(MM_MapHWPage);
1011 EXPORT(MM_AllocDMA);
1012 EXPORT(MM_UnmapHWPage);