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);
217 // -- Check Error Code --
219 Warning("Reserved Bits Trashed!");
222 Warning("%s %s %s memory%s",
223 (ErrorCode&4?"User":"Kernel"),
224 (ErrorCode&2?"write to":"read from"),
225 (ErrorCode&1?"bad/locked":"non-present"),
226 (ErrorCode&16?" (Instruction Fetch)":"")
230 Log("Code at %p accessed %p", Regs->eip, Addr);
231 // Print Stack Backtrace
232 Error_Backtrace(Regs->eip, Regs->ebp);
234 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
235 if( gaPageDir[Addr>>22] & PF_PRESENT )
236 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
238 //MM_DumpTables(0, -1);
240 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
244 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
245 * \brief Dumps the layout of the page tables
247 void MM_DumpTables(tVAddr Start, tVAddr End)
249 tVAddr rangeStart = 0;
253 const tPAddr MASK = ~0xF98;
255 Start >>= 12; End >>= 12;
258 Log("Directory Entries:");
259 for(page = Start >> 10;
260 page < (End >> 10)+1;
265 Log(" 0x%08x-0x%08x :: 0x%08x",
266 page<<22, ((page+1)<<22)-1,
267 gaPageDir[page]&~0xFFF
273 Log("Table Entries:");
274 for(page = Start, curPos = Start<<12;
276 curPos += 0x1000, page++)
278 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
279 || !(gaPageTable[page] & PF_PRESENT)
280 || (gaPageTable[page] & MASK) != expected)
283 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
284 rangeStart, curPos - 1,
285 gaPageTable[rangeStart>>12] & ~0xFFF,
286 (expected & ~0xFFF) - 1,
287 (expected & PF_PAGED ? "p" : "-"),
288 (expected & PF_COW ? "C" : "-"),
289 (expected & PF_USER ? "U" : "-"),
290 (expected & PF_WRITE ? "W" : "-")
294 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
295 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
297 expected = (gaPageTable[page] & MASK);
300 if(expected) expected += 0x1000;
304 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
305 rangeStart, curPos - 1,
306 gaPageTable[rangeStart>>12] & ~0xFFF,
307 (expected & ~0xFFF) - 1,
308 (expected & PF_PAGED ? "p" : "-"),
309 (expected & PF_COW ? "C" : "-"),
310 (expected & PF_USER ? "U" : "-"),
311 (expected & PF_WRITE ? "W" : "-")
318 * \fn tPAddr MM_Allocate(tVAddr VAddr)
320 tPAddr MM_Allocate(tVAddr VAddr)
323 //ENTER("xVAddr", VAddr);
324 //__asm__ __volatile__ ("xchg %bx,%bx");
325 // Check if the directory is mapped
326 if( gaPageDir[ VAddr >> 22 ] == 0 )
328 // Allocate directory
329 paddr = MM_AllocPhys();
330 //LOG("paddr = 0x%llx (new table)", paddr);
332 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
337 gaPageDir[ VAddr >> 22 ] = paddr | 3;
339 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
341 INVLPG( &gaPageDir[ VAddr >> 22 ] );
342 //LOG("Clearing new table");
343 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
345 // Check if the page is already allocated
346 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
347 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
348 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
349 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
353 paddr = MM_AllocPhys();
354 //LOG("paddr = 0x%llx", paddr);
356 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
357 VAddr, __builtin_return_address(0));
362 gaPageTable[ VAddr >> 12 ] = paddr | 3;
364 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
365 // Invalidate Cache for address
366 INVLPG( VAddr & ~0xFFF );
373 * \fn void MM_Deallocate(tVAddr VAddr)
375 void MM_Deallocate(tVAddr VAddr)
377 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
378 Warning("MM_Deallocate - Directory not mapped");
382 if(gaPageTable[ VAddr >> 12 ] == 0) {
383 Warning("MM_Deallocate - Page is not allocated");
388 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
390 gaPageTable[ VAddr >> 12 ] = 0;
394 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
395 * \brief Checks if the passed address is accesable
397 tPAddr MM_GetPhysAddr(tVAddr Addr)
399 if( !(gaPageDir[Addr >> 22] & 1) )
401 if( !(gaPageTable[Addr >> 12] & 1) )
403 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
408 * \fn int MM_IsUser(tVAddr VAddr)
409 * \brief Checks if a page is user accessable
411 int MM_IsUser(tVAddr VAddr)
413 if( !(gaPageDir[VAddr >> 22] & 1) )
415 if( !(gaPageTable[VAddr >> 12] & 1) )
417 if( !(gaPageTable[VAddr >> 12] & PF_USER) )
423 * \fn void MM_SetCR3(tPAddr CR3)
424 * \brief Sets the current process space
426 void MM_SetCR3(tPAddr CR3)
428 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
432 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
433 * \brief Map a physical page to a virtual one
435 int MM_Map(tVAddr VAddr, tPAddr PAddr)
437 //ENTER("xVAddr xPAddr", VAddr, PAddr);
439 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
440 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
446 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
448 // Check if the directory is mapped
449 if( gaPageDir[ VAddr >> 22 ] == 0 )
451 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
454 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
456 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
457 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
459 // Check if the page is already allocated
460 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
461 Warning("MM_Map - Allocating to used address");
467 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
469 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
471 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
472 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
477 //LOG("INVLPG( 0x%x )", VAddr);
485 * \fn tVAddr MM_ClearUser()
486 * \brief Clear user's address space
488 tVAddr MM_ClearUser()
493 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
495 // Check if directory is not allocated
496 if( !(gaPageDir[i] & PF_PRESENT) ) {
502 for( j = 0; j < 1024; j ++ )
504 if( gaPageTable[i*1024+j] & 1 )
505 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
506 gaPageTable[i*1024+j] = 0;
509 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
511 INVLPG( &gaPageTable[i*1024] );
519 * \fn tPAddr MM_Clone()
520 * \brief Clone the current address space
527 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
530 LOCK( &gilTempFractal );
532 // Create Directory Table
533 *gpTmpCR3 = MM_AllocPhys() | 3;
535 //LOG("Allocated Directory (%x)", *gpTmpCR3);
536 memsetd( gaTmpDir, 0, 1024 );
539 for( i = 0; i < 768; i ++)
541 // Check if table is allocated
542 if( !(gaPageDir[i] & PF_PRESENT) ) {
548 // Allocate new table
549 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
550 INVLPG( &gaTmpTable[page] );
552 for( j = 0; j < 1024; j ++, page++ )
554 if( !(gaPageTable[page] & PF_PRESENT) ) {
555 gaTmpTable[page] = 0;
560 MM_RefPhys( gaPageTable[page] & ~0xFFF );
562 if(gaPageTable[page] & PF_WRITE) {
563 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
564 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
565 INVLPG( page << 12 );
568 gaTmpTable[page] = gaPageTable[page];
572 // Map in kernel tables (and make fractal mapping)
573 for( i = 768; i < 1024; i ++ )
576 if( i == (PAGE_TABLE_ADDR >> 22) ) {
577 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
581 if( gaPageDir[i] == 0 ) {
586 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
587 MM_RefPhys( gaPageDir[i] & ~0xFFF );
588 gaTmpDir[i] = gaPageDir[i];
591 // Allocate kernel stack
592 for(i = KERNEL_STACKS >> 22;
593 i < KERNEL_STACKS_END >> 22;
596 // Check if directory is allocated
597 if( (gaPageDir[i] & 1) == 0 ) {
602 // We don't care about other kernel stacks, just the current one
603 if( i != kStackBase >> 22 ) {
604 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
610 gaTmpDir[i] = MM_AllocPhys() | 3;
611 INVLPG( &gaTmpTable[i*1024] );
612 for( j = 0; j < 1024; j ++ )
614 // Is the page allocated? If not, skip
615 if( !(gaPageTable[i*1024+j] & 1) ) {
616 gaTmpTable[i*1024+j] = 0;
620 // We don't care about other kernel stacks
621 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
622 gaTmpTable[i*1024+j] = 0;
627 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
629 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
631 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
632 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
633 MM_FreeTemp( (Uint)tmp );
637 ret = *gpTmpCR3 & ~0xFFF;
638 RELEASE( &gilTempFractal );
645 * \fn tVAddr MM_NewKStack()
646 * \brief Create a new kernel stack
648 tVAddr MM_NewKStack()
650 tVAddr base = KERNEL_STACKS;
652 for(;base<KERNEL_STACKS_END;base+=KERNEL_STACK_SIZE)
654 if(MM_GetPhysAddr(base) != 0) continue;
655 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
658 return base+KERNEL_STACK_SIZE;
660 Warning("MM_NewKStack - No address space left\n");
665 * \fn tVAddr MM_NewWorkerStack()
666 * \brief Creates a new worker stack
668 tVAddr MM_NewWorkerStack()
675 tPAddr pages[WORKER_STACK_SIZE>>12];
677 // Get the old ESP and EBP
678 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
679 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
681 // Find a free worker stack address
682 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
685 if( gWorkerStacks[base/32] == -1 ) {
686 base += 31; base &= ~31;
687 base --; // Counteracted by the base++
691 if( gWorkerStacks[base/32] & (1 << base) ) {
696 if(base >= NUM_WORKER_STACKS) {
697 Warning("Uh-oh! Out of worker stacks");
702 gWorkerStacks[base/32] |= (1 << base);
703 // Make life easier for later calls
704 giLastUsedWorker = base;
706 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
707 //Log(" MM_NewWorkerStack: base = 0x%x", base);
709 // Acquire the lock for the temp fractal mappings
710 LOCK(&gilTempFractal);
712 // Set the temp fractals to TID0's address space
713 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
714 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
718 // Check if the directory is mapped (we are assuming that the stacks
719 // will fit neatly in a directory)
720 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
721 if(gaTmpDir[ base >> 22 ] == 0) {
722 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
723 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
727 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
729 pages[ addr >> 12 ] = MM_AllocPhys();
730 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
733 // Release the temp mapping lock
734 RELEASE(&gilTempFractal);
736 // Copy the old stack
737 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
738 esp = oldstack - esp; // ESP as an offset in the stack
740 // Make `base` be the top of the stack
741 base += WORKER_STACK_SIZE;
743 i = (WORKER_STACK_SIZE>>12) - 1;
744 // Copy the contents of the old stack to the new one, altering the addresses
745 // `addr` is refering to bytes from the stack base (mem downwards)
746 for(addr = 0; addr < esp; addr += 0x1000)
748 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
749 tmpPage = (void*)MM_MapTemp( pages[i] );
751 for(j = 0; j < 1024; j++)
753 // Possible Stack address?
754 if(oldstack-esp < stack[j] && stack[j] < oldstack)
755 tmpPage[j] = base - (oldstack - stack[j]);
756 else // Seems not, best leave it alone
757 tmpPage[j] = stack[j];
759 MM_FreeTemp((tVAddr)tmpPage);
763 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
768 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
769 * \brief Sets the flags on a page
771 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
774 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
775 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
777 ent = &gaPageTable[VAddr >> 12];
780 if( Mask & MM_PFLAG_RO )
782 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
783 else *ent |= PF_WRITE;
787 if( Mask & MM_PFLAG_KERNEL )
789 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
790 else *ent |= PF_USER;
794 if( Mask & MM_PFLAG_COW )
796 if( Flags & MM_PFLAG_COW ) {
808 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
809 * \brief Duplicates a virtual page to a physical one
811 tPAddr MM_DuplicatePage(tVAddr VAddr)
817 //ENTER("xVAddr", VAddr);
820 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
821 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
827 ret = MM_AllocPhys();
829 // Write-lock the page (to keep data constistent), saving its R/W state
830 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
831 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
835 temp = MM_MapTemp(ret);
836 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
839 // Restore Writeable status
840 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
848 * \fn Uint MM_MapTemp(tPAddr PAddr)
849 * \brief Create a temporary memory mapping
850 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
852 tVAddr MM_MapTemp(tPAddr PAddr)
856 //ENTER("XPAddr", PAddr);
860 //LOG("gilTempMappings = %i", gilTempMappings);
864 LOCK( &gilTempMappings );
866 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
868 // Check if page used
869 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
871 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
872 INVLPG( TEMP_MAP_ADDR + (i << 12) );
873 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
874 RELEASE( &gilTempMappings );
875 return TEMP_MAP_ADDR + (i << 12);
877 RELEASE( &gilTempMappings );
883 * \fn void MM_FreeTemp(tVAddr PAddr)
884 * \brief Free's a temp mapping
886 void MM_FreeTemp(tVAddr VAddr)
889 //ENTER("xVAddr", VAddr);
891 if(i >= (TEMP_MAP_ADDR >> 12))
892 gaPageTable[ i ] = 0;
898 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
899 * \brief Allocates a contigous number of pages
901 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
908 for( i = 0; i < NUM_HW_PAGES; i ++ )
910 // Check if addr used
911 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
914 // Check possible region
915 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
917 // If there is an allocated page in the region we are testing, break
918 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
924 for( j = 0; j < Number; j++ ) {
925 MM_RefPhys( PAddr + (j<<12) );
926 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
928 return HW_MAP_ADDR + (i<<12);
931 // If we don't find any, return NULL
936 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
937 * \brief Allocates DMA physical memory
938 * \param Pages Number of pages required
939 * \param MaxBits Maximum number of bits the physical address can have
940 * \param PhysAddr Pointer to the location to place the physical address allocated
941 * \return Virtual address allocate
943 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
945 tPAddr maxCheck = (1 << MaxBits);
949 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
952 if(MaxBits < 12 || !PhysAddr) {
958 if(MaxBits >= PHYS_BITS) maxCheck = -1;
961 if(Pages == 1 && MaxBits >= PHYS_BITS)
963 phys = MM_AllocPhys();
965 ret = MM_MapHWPages(phys, 1);
976 phys = MM_AllocPhysRange(Pages, MaxBits);
977 // - Was it allocated?
983 // Allocated successfully, now map
984 ret = MM_MapHWPages(phys, Pages);
986 // If it didn't map, free then return 0
987 for(;Pages--;phys+=0x1000)
999 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1000 * \brief Unmap a hardware page
1002 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1006 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
1010 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
1012 for( j = 0; j < Number; j++ )
1014 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
1015 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
1018 RELEASE( &gilTempMappings );
1022 EXPORT(MM_GetPhysAddr);
1025 EXPORT(MM_MapHWPages);
1026 EXPORT(MM_AllocDMA);
1027 EXPORT(MM_UnmapHWPages);