2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
25 #define KERNEL_STACKS 0xF0000000
26 #define KERNEL_STACK_SIZE 0x00008000
27 #define KERNEL_STACKS_END 0xFC000000
28 #define WORKER_STACKS 0x00100000 // Thread0 Only!
29 #define WORKER_STACK_SIZE KERNEL_STACK_SIZE
30 #define WORKER_STACKS_END 0xB0000000
31 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
33 #define PAE_PAGE_TABLE_ADDR 0xFC000000 // 16 MiB
34 #define PAE_PAGE_DIR_ADDR 0xFCFC0000 // 16 KiB
35 #define PAE_PAGE_PDPT_ADDR 0xFCFC3F00 // 32 bytes
36 #define PAE_TMP_PDPT_ADDR 0xFCFC3F20 // 32 bytes
37 #define PAE_TMP_DIR_ADDR 0xFCFE0000 // 16 KiB
38 #define PAE_TMP_TABLE_ADDR 0xFD000000 // 16 MiB
40 #define PAGE_TABLE_ADDR 0xFC000000
41 #define PAGE_DIR_ADDR 0xFC3F0000
42 #define PAGE_CR3_ADDR 0xFC3F0FC0
43 #define TMP_CR3_ADDR 0xFC3F0FC4 // Part of core instead of temp
44 #define TMP_DIR_ADDR 0xFC3F1000 // Same
45 #define TMP_TABLE_ADDR 0xFC400000
47 #define HW_MAP_ADDR 0xFE000000
48 #define HW_MAP_MAX 0xFFEF0000
49 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
50 #define TEMP_MAP_ADDR 0xFFEF0000 // Allows 16 "temp" pages
51 #define NUM_TEMP_PAGES 16
52 #define LAST_BLOCK_ADDR 0xFFFF0000 // Free space for kernel provided user code/ *(-1) protection
54 #define PF_PRESENT 0x1
58 #define PF_PAGED 0x400
60 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
63 typedef Uint64 tTabEnt;
65 typedef Uint32 tTabEnt;
69 extern Uint32 gaInitPageDir[1024];
70 extern Uint32 gaInitPageTable[1024];
71 extern void Threads_SegFault(tVAddr Addr);
72 extern void Error_Backtrace(Uint eip, Uint ebp);
75 void MM_PreinitVirtual();
76 void MM_InstallVirtual();
77 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
78 void MM_DumpTables(tVAddr Start, tVAddr End);
79 tPAddr MM_DuplicatePage(tVAddr VAddr);
82 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
83 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
84 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
85 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
86 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
87 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
89 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
90 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
91 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
92 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
93 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
94 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
96 int gilTempMappings = 0;
97 int gilTempFractal = 0;
98 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
99 int giLastUsedWorker = 0;
103 * \fn void MM_PreinitVirtual()
104 * \brief Maps the fractal mappings
106 void MM_PreinitVirtual()
109 gaInitPageDir[ ((PAGE_TABLE_ADDR >> TAB)-3*512+3)*2 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
111 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
113 INVLPG( PAGE_TABLE_ADDR );
117 * \fn void MM_InstallVirtual()
118 * \brief Sets up the constant page mappings
120 void MM_InstallVirtual()
125 // --- Pre-Allocate kernel tables
126 for( i = KERNEL_BASE >> TAB; i < 1024*4; i ++ )
128 if( gaPAE_PageDir[ i ] ) continue;
130 // Skip stack tables, they are process unique
131 if( i > KERNEL_STACKS >> TAB && i < KERNEL_STACKS_END >> TAB) {
132 gaPAE_PageDir[ i ] = 0;
136 gaPAE_PageDir[ i ] = MM_AllocPhys() | 3;
137 INVLPG( &gaPAE_PageTable[i*512] );
138 memset( &gaPAE_PageTable[i*512], 0, 0x1000 );
141 // --- Pre-Allocate kernel tables
142 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
144 if( gaPageDir[ i ] ) continue;
145 // Skip stack tables, they are process unique
146 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
151 gaPageDir[ i ] = MM_AllocPhys() | 3;
152 INVLPG( &gaPageTable[i*1024] );
153 memset( &gaPageTable[i*1024], 0, 0x1000 );
159 * \brief Cleans up the SMP required mappings
161 void MM_FinishVirtualInit()
166 gaInitPageDir[ 0 ] = 0;
171 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
172 * \brief Called on a page fault
174 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
176 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
178 // -- Check for COW --
179 if( gaPageDir [Addr>>22] & PF_PRESENT
180 && gaPageTable[Addr>>12] & PF_PRESENT
181 && gaPageTable[Addr>>12] & PF_COW )
184 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
186 gaPageTable[Addr>>12] &= ~PF_COW;
187 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
191 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
192 paddr = MM_DuplicatePage( Addr );
193 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
194 gaPageTable[Addr>>12] &= PF_USER;
195 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
198 INVLPG( Addr & ~0xFFF );
203 // If it was a user, tell the thread handler
205 Warning("%s %s %s memory%s",
206 (ErrorCode&4?"User":"Kernel"),
207 (ErrorCode&2?"write to":"read from"),
208 (ErrorCode&1?"bad/locked":"non-present"),
209 (ErrorCode&16?" (Instruction Fetch)":"")
211 Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
212 __asm__ __volatile__ ("sti"); // Restart IRQs
213 Threads_SegFault(Addr);
219 // -- Check Error Code --
221 Warning("Reserved Bits Trashed!");
224 Warning("%s %s %s memory%s",
225 (ErrorCode&4?"User":"Kernel"),
226 (ErrorCode&2?"write to":"read from"),
227 (ErrorCode&1?"bad/locked":"non-present"),
228 (ErrorCode&16?" (Instruction Fetch)":"")
232 Log("Code at %p accessed %p", Regs->eip, Addr);
233 // Print Stack Backtrace
234 Error_Backtrace(Regs->eip, Regs->ebp);
236 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
237 if( gaPageDir[Addr>>22] & PF_PRESENT )
238 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
240 //MM_DumpTables(0, -1);
242 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
246 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
247 * \brief Dumps the layout of the page tables
249 void MM_DumpTables(tVAddr Start, tVAddr End)
251 tVAddr rangeStart = 0;
255 const tPAddr MASK = ~0xF98;
257 Start >>= 12; End >>= 12;
260 Log("Directory Entries:");
261 for(page = Start >> 10;
262 page < (End >> 10)+1;
267 Log(" 0x%08x-0x%08x :: 0x%08x",
268 page<<22, ((page+1)<<22)-1,
269 gaPageDir[page]&~0xFFF
275 Log("Table Entries:");
276 for(page = Start, curPos = Start<<12;
278 curPos += 0x1000, page++)
280 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
281 || !(gaPageTable[page] & PF_PRESENT)
282 || (gaPageTable[page] & MASK) != expected)
285 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
286 rangeStart, curPos - 1,
287 gaPageTable[rangeStart>>12] & ~0xFFF,
288 (expected & ~0xFFF) - 1,
289 (expected & PF_PAGED ? "p" : "-"),
290 (expected & PF_COW ? "C" : "-"),
291 (expected & PF_USER ? "U" : "-"),
292 (expected & PF_WRITE ? "W" : "-")
296 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
297 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
299 expected = (gaPageTable[page] & MASK);
302 if(expected) expected += 0x1000;
306 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
307 rangeStart, curPos - 1,
308 gaPageTable[rangeStart>>12] & ~0xFFF,
309 (expected & ~0xFFF) - 1,
310 (expected & PF_PAGED ? "p" : "-"),
311 (expected & PF_COW ? "C" : "-"),
312 (expected & PF_USER ? "U" : "-"),
313 (expected & PF_WRITE ? "W" : "-")
320 * \fn tPAddr MM_Allocate(tVAddr VAddr)
322 tPAddr MM_Allocate(tVAddr VAddr)
325 //ENTER("xVAddr", VAddr);
326 //__asm__ __volatile__ ("xchg %bx,%bx");
327 // Check if the directory is mapped
328 if( gaPageDir[ VAddr >> 22 ] == 0 )
330 // Allocate directory
331 paddr = MM_AllocPhys();
332 //LOG("paddr = 0x%llx (new table)", paddr);
334 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
339 gaPageDir[ VAddr >> 22 ] = paddr | 3;
341 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
343 INVLPG( &gaPageDir[ VAddr >> 22 ] );
344 //LOG("Clearing new table");
345 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
347 // Check if the page is already allocated
348 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
349 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
350 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
351 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
355 paddr = MM_AllocPhys();
356 //LOG("paddr = 0x%llx", paddr);
358 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
359 VAddr, __builtin_return_address(0));
364 gaPageTable[ VAddr >> 12 ] = paddr | 3;
366 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
367 // Invalidate Cache for address
368 INVLPG( VAddr & ~0xFFF );
375 * \fn void MM_Deallocate(tVAddr VAddr)
377 void MM_Deallocate(tVAddr VAddr)
379 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
380 Warning("MM_Deallocate - Directory not mapped");
384 if(gaPageTable[ VAddr >> 12 ] == 0) {
385 Warning("MM_Deallocate - Page is not allocated");
390 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
392 gaPageTable[ VAddr >> 12 ] = 0;
396 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
397 * \brief Checks if the passed address is accesable
399 tPAddr MM_GetPhysAddr(tVAddr Addr)
401 if( !(gaPageDir[Addr >> 22] & 1) )
403 if( !(gaPageTable[Addr >> 12] & 1) )
405 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
410 * \fn int MM_IsUser(tVAddr VAddr)
411 * \brief Checks if a page is user accessable
413 int MM_IsUser(tVAddr VAddr)
415 if( !(gaPageDir[VAddr >> 22] & 1) )
417 if( !(gaPageTable[VAddr >> 12] & 1) )
419 if( !(gaPageTable[VAddr >> 12] & PF_USER) )
425 * \fn void MM_SetCR3(tPAddr CR3)
426 * \brief Sets the current process space
428 void MM_SetCR3(tPAddr CR3)
430 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
434 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
435 * \brief Map a physical page to a virtual one
437 int MM_Map(tVAddr VAddr, tPAddr PAddr)
439 //ENTER("xVAddr xPAddr", VAddr, PAddr);
441 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
442 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
448 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
450 // Check if the directory is mapped
451 if( gaPageDir[ VAddr >> 22 ] == 0 )
453 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
456 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
458 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
459 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
461 // Check if the page is already allocated
462 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
463 Warning("MM_Map - Allocating to used address");
469 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
471 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
473 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
474 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
479 //LOG("INVLPG( 0x%x )", VAddr);
487 * \fn tVAddr MM_ClearUser()
488 * \brief Clear user's address space
490 tVAddr MM_ClearUser()
495 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
497 // Check if directory is not allocated
498 if( !(gaPageDir[i] & PF_PRESENT) ) {
504 for( j = 0; j < 1024; j ++ )
506 if( gaPageTable[i*1024+j] & 1 )
507 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
508 gaPageTable[i*1024+j] = 0;
511 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
513 INVLPG( &gaPageTable[i*1024] );
521 * \fn tPAddr MM_Clone()
522 * \brief Clone the current address space
529 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
532 LOCK( &gilTempFractal );
534 // Create Directory Table
535 *gpTmpCR3 = MM_AllocPhys() | 3;
537 //LOG("Allocated Directory (%x)", *gpTmpCR3);
538 memsetd( gaTmpDir, 0, 1024 );
541 for( i = 0; i < 768; i ++)
543 // Check if table is allocated
544 if( !(gaPageDir[i] & PF_PRESENT) ) {
550 // Allocate new table
551 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
552 INVLPG( &gaTmpTable[page] );
554 for( j = 0; j < 1024; j ++, page++ )
556 if( !(gaPageTable[page] & PF_PRESENT) ) {
557 gaTmpTable[page] = 0;
562 MM_RefPhys( gaPageTable[page] & ~0xFFF );
564 if(gaPageTable[page] & PF_WRITE) {
565 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
566 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
567 INVLPG( page << 12 );
570 gaTmpTable[page] = gaPageTable[page];
574 // Map in kernel tables (and make fractal mapping)
575 for( i = 768; i < 1024; i ++ )
578 if( i == (PAGE_TABLE_ADDR >> 22) ) {
579 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
583 if( gaPageDir[i] == 0 ) {
588 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
589 MM_RefPhys( gaPageDir[i] & ~0xFFF );
590 gaTmpDir[i] = gaPageDir[i];
593 // Allocate kernel stack
594 for(i = KERNEL_STACKS >> 22;
595 i < KERNEL_STACKS_END >> 22;
598 // Check if directory is allocated
599 if( (gaPageDir[i] & 1) == 0 ) {
604 // We don't care about other kernel stacks, just the current one
605 if( i != kStackBase >> 22 ) {
606 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
612 gaTmpDir[i] = MM_AllocPhys() | 3;
613 INVLPG( &gaTmpTable[i*1024] );
614 for( j = 0; j < 1024; j ++ )
616 // Is the page allocated? If not, skip
617 if( !(gaPageTable[i*1024+j] & 1) ) {
618 gaTmpTable[i*1024+j] = 0;
622 // We don't care about other kernel stacks
623 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
624 gaTmpTable[i*1024+j] = 0;
629 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
631 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
633 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
634 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
635 MM_FreeTemp( (Uint)tmp );
639 ret = *gpTmpCR3 & ~0xFFF;
640 RELEASE( &gilTempFractal );
647 * \fn tVAddr MM_NewKStack()
648 * \brief Create a new kernel stack
650 tVAddr MM_NewKStack()
652 tVAddr base = KERNEL_STACKS;
654 for(;base<KERNEL_STACKS_END;base+=KERNEL_STACK_SIZE)
656 if(MM_GetPhysAddr(base) != 0) continue;
657 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
660 return base+KERNEL_STACK_SIZE;
662 Warning("MM_NewKStack - No address space left\n");
667 * \fn tVAddr MM_NewWorkerStack()
668 * \brief Creates a new worker stack
670 tVAddr MM_NewWorkerStack()
677 tPAddr pages[WORKER_STACK_SIZE>>12];
679 // Get the old ESP and EBP
680 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
681 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
683 // Find a free worker stack address
684 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
687 if( gWorkerStacks[base/32] == -1 ) {
688 base += 31; base &= ~31;
689 base --; // Counteracted by the base++
693 if( gWorkerStacks[base/32] & (1 << base) ) {
698 if(base >= NUM_WORKER_STACKS) {
699 Warning("Uh-oh! Out of worker stacks");
704 gWorkerStacks[base/32] |= (1 << base);
705 // Make life easier for later calls
706 giLastUsedWorker = base;
708 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
709 //Log(" MM_NewWorkerStack: base = 0x%x", base);
711 // Acquire the lock for the temp fractal mappings
712 LOCK(&gilTempFractal);
714 // Set the temp fractals to TID0's address space
715 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
716 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
720 // Check if the directory is mapped (we are assuming that the stacks
721 // will fit neatly in a directory)
722 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
723 if(gaTmpDir[ base >> 22 ] == 0) {
724 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
725 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
729 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
731 pages[ addr >> 12 ] = MM_AllocPhys();
732 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
735 // Release the temp mapping lock
736 RELEASE(&gilTempFractal);
738 // Copy the old stack
739 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
740 esp = oldstack - esp; // ESP as an offset in the stack
742 // Make `base` be the top of the stack
743 base += WORKER_STACK_SIZE;
745 i = (WORKER_STACK_SIZE>>12) - 1;
746 // Copy the contents of the old stack to the new one, altering the addresses
747 // `addr` is refering to bytes from the stack base (mem downwards)
748 for(addr = 0; addr < esp; addr += 0x1000)
750 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
751 tmpPage = (void*)MM_MapTemp( pages[i] );
753 for(j = 0; j < 1024; j++)
755 // Possible Stack address?
756 if(oldstack-esp < stack[j] && stack[j] < oldstack)
757 tmpPage[j] = base - (oldstack - stack[j]);
758 else // Seems not, best leave it alone
759 tmpPage[j] = stack[j];
761 MM_FreeTemp((tVAddr)tmpPage);
765 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
770 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
771 * \brief Sets the flags on a page
773 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
776 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
777 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
779 ent = &gaPageTable[VAddr >> 12];
782 if( Mask & MM_PFLAG_RO )
784 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
785 else *ent |= PF_WRITE;
789 if( Mask & MM_PFLAG_KERNEL )
791 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
792 else *ent |= PF_USER;
796 if( Mask & MM_PFLAG_COW )
798 if( Flags & MM_PFLAG_COW ) {
810 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
811 * \brief Duplicates a virtual page to a physical one
813 tPAddr MM_DuplicatePage(tVAddr VAddr)
819 //ENTER("xVAddr", VAddr);
822 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
823 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
829 ret = MM_AllocPhys();
831 // Write-lock the page (to keep data constistent), saving its R/W state
832 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
833 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
837 temp = MM_MapTemp(ret);
838 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
841 // Restore Writeable status
842 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
850 * \fn Uint MM_MapTemp(tPAddr PAddr)
851 * \brief Create a temporary memory mapping
852 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
854 tVAddr MM_MapTemp(tPAddr PAddr)
858 //ENTER("XPAddr", PAddr);
862 //LOG("gilTempMappings = %i", gilTempMappings);
866 LOCK( &gilTempMappings );
868 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
870 // Check if page used
871 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
873 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
874 INVLPG( TEMP_MAP_ADDR + (i << 12) );
875 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
876 RELEASE( &gilTempMappings );
877 return TEMP_MAP_ADDR + (i << 12);
879 RELEASE( &gilTempMappings );
885 * \fn void MM_FreeTemp(tVAddr PAddr)
886 * \brief Free's a temp mapping
888 void MM_FreeTemp(tVAddr VAddr)
891 //ENTER("xVAddr", VAddr);
893 if(i >= (TEMP_MAP_ADDR >> 12))
894 gaPageTable[ i ] = 0;
900 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
901 * \brief Allocates a contigous number of pages
903 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
910 for( i = 0; i < NUM_HW_PAGES; i ++ )
912 // Check if addr used
913 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
916 // Check possible region
917 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
919 // If there is an allocated page in the region we are testing, break
920 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
926 for( j = 0; j < Number; j++ ) {
927 MM_RefPhys( PAddr + (j<<12) );
928 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
930 return HW_MAP_ADDR + (i<<12);
933 // If we don't find any, return NULL
938 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
939 * \brief Allocates DMA physical memory
940 * \param Pages Number of pages required
941 * \param MaxBits Maximum number of bits the physical address can have
942 * \param PhysAddr Pointer to the location to place the physical address allocated
943 * \return Virtual address allocate
945 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
947 tPAddr maxCheck = (1 << MaxBits);
951 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
954 if(MaxBits < 12 || !PhysAddr) {
960 if(MaxBits >= PHYS_BITS) maxCheck = -1;
963 if(Pages == 1 && MaxBits >= PHYS_BITS)
965 phys = MM_AllocPhys();
967 ret = MM_MapHWPages(phys, 1);
978 phys = MM_AllocPhysRange(Pages, MaxBits);
979 // - Was it allocated?
985 // Allocated successfully, now map
986 ret = MM_MapHWPages(phys, Pages);
988 // If it didn't map, free then return 0
989 for(;Pages--;phys+=0x1000)
1001 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1002 * \brief Unmap a hardware page
1004 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1008 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
1012 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
1014 for( j = 0; j < Number; j++ )
1016 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
1017 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
1020 RELEASE( &gilTempMappings );
1024 EXPORT(MM_GetPhysAddr);
1027 EXPORT(MM_MapHWPages);
1028 EXPORT(MM_AllocDMA);
1029 EXPORT(MM_UnmapHWPages);