2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
26 #define KERNEL_STACKS 0xF0000000
27 #define KERNEL_STACK_SIZE 0x00008000
28 #define KERNEL_STACKS_END 0xFC000000
29 #define WORKER_STACKS 0x00100000 // Thread0 Only!
30 #define WORKER_STACK_SIZE KERNEL_STACK_SIZE
31 #define WORKER_STACKS_END 0xB0000000
32 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
34 #define PAE_PAGE_TABLE_ADDR 0xFC000000 // 16 MiB
35 #define PAE_PAGE_DIR_ADDR 0xFCFC0000 // 16 KiB
36 #define PAE_PAGE_PDPT_ADDR 0xFCFC3F00 // 32 bytes
37 #define PAE_TMP_PDPT_ADDR 0xFCFC3F20 // 32 bytes
38 #define PAE_TMP_DIR_ADDR 0xFCFE0000 // 16 KiB
39 #define PAE_TMP_TABLE_ADDR 0xFD000000 // 16 MiB
41 #define PAGE_TABLE_ADDR 0xFC000000
42 #define PAGE_DIR_ADDR 0xFC3F0000
43 #define PAGE_CR3_ADDR 0xFC3F0FC0
44 #define TMP_CR3_ADDR 0xFC3F0FC4 // Part of core instead of temp
45 #define TMP_DIR_ADDR 0xFC3F1000 // Same
46 #define TMP_TABLE_ADDR 0xFC400000
48 #define HW_MAP_ADDR 0xFE000000
49 #define HW_MAP_MAX 0xFFEF0000
50 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
51 #define TEMP_MAP_ADDR 0xFFEF0000 // Allows 16 "temp" pages
52 #define NUM_TEMP_PAGES 16
53 #define LAST_BLOCK_ADDR 0xFFFF0000 // Free space for kernel provided user code/ *(-1) protection
55 #define PF_PRESENT 0x1
59 #define PF_PAGED 0x400
61 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
64 typedef Uint64 tTabEnt;
66 typedef Uint32 tTabEnt;
70 extern void _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();
78 void MM_InstallVirtual();
79 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
80 void MM_DumpTables(tVAddr Start, tVAddr End);
81 tPAddr MM_DuplicatePage(tVAddr VAddr);
84 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
85 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
86 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
87 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
88 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
89 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
91 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
92 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
93 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
94 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
95 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
96 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
98 int gilTempMappings = 0;
99 int gilTempFractal = 0;
100 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
101 int giLastUsedWorker = 0;
105 * \fn void MM_PreinitVirtual()
106 * \brief Maps the fractal mappings
108 void MM_PreinitVirtual()
111 gaInitPageDir[ ((PAGE_TABLE_ADDR >> TAB)-3*512+3)*2 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
113 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
115 INVLPG( PAGE_TABLE_ADDR );
119 * \fn void MM_InstallVirtual()
120 * \brief Sets up the constant page mappings
122 void MM_InstallVirtual()
127 // --- Pre-Allocate kernel tables
128 for( i = KERNEL_BASE >> TAB; i < 1024*4; i ++ )
130 if( gaPAE_PageDir[ i ] ) continue;
132 // Skip stack tables, they are process unique
133 if( i > KERNEL_STACKS >> TAB && i < KERNEL_STACKS_END >> TAB) {
134 gaPAE_PageDir[ i ] = 0;
138 gaPAE_PageDir[ i ] = MM_AllocPhys() | 3;
139 INVLPG( &gaPAE_PageTable[i*512] );
140 memset( &gaPAE_PageTable[i*512], 0, 0x1000 );
143 // --- Pre-Allocate kernel tables
144 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
146 if( gaPageDir[ i ] ) continue;
147 // Skip stack tables, they are process unique
148 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
153 gaPageDir[ i ] = MM_AllocPhys() | 3;
154 INVLPG( &gaPageTable[i*1024] );
155 memset( &gaPageTable[i*1024], 0, 0x1000 );
159 // Unset kernel on the User Text pages
160 for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
161 Log("MM_SetFlags( 0x%08x, 0, MM_PFLAG_KERNEL)", (tVAddr)&_UsertextBase + i*4096);
162 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
167 * \brief Cleans up the SMP required mappings
169 void MM_FinishVirtualInit()
174 gaInitPageDir[ 0 ] = 0;
179 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
180 * \brief Called on a page fault
182 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
184 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
186 // -- Check for COW --
187 if( gaPageDir [Addr>>22] & PF_PRESENT
188 && gaPageTable[Addr>>12] & PF_PRESENT
189 && gaPageTable[Addr>>12] & PF_COW )
192 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
194 gaPageTable[Addr>>12] &= ~PF_COW;
195 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
199 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
200 paddr = MM_DuplicatePage( Addr );
201 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
202 gaPageTable[Addr>>12] &= PF_USER;
203 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
206 INVLPG( Addr & ~0xFFF );
211 // If it was a user, tell the thread handler
213 Warning("%s %s %s memory%s",
214 (ErrorCode&4?"User":"Kernel"),
215 (ErrorCode&2?"write to":"read from"),
216 (ErrorCode&1?"bad/locked":"non-present"),
217 (ErrorCode&16?" (Instruction Fetch)":"")
219 Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
220 __asm__ __volatile__ ("sti"); // Restart IRQs
221 Threads_SegFault(Addr);
227 // -- Check Error Code --
229 Warning("Reserved Bits Trashed!");
232 Warning("%s %s %s memory%s",
233 (ErrorCode&4?"User":"Kernel"),
234 (ErrorCode&2?"write to":"read from"),
235 (ErrorCode&1?"bad/locked":"non-present"),
236 (ErrorCode&16?" (Instruction Fetch)":"")
240 Log("Code at %p accessed %p", Regs->eip, Addr);
241 // Print Stack Backtrace
242 Error_Backtrace(Regs->eip, Regs->ebp);
244 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
245 if( gaPageDir[Addr>>22] & PF_PRESENT )
246 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
248 //MM_DumpTables(0, -1);
250 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
254 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
255 * \brief Dumps the layout of the page tables
257 void MM_DumpTables(tVAddr Start, tVAddr End)
259 tVAddr rangeStart = 0;
263 const tPAddr MASK = ~0xF98;
265 Start >>= 12; End >>= 12;
268 Log("Directory Entries:");
269 for(page = Start >> 10;
270 page < (End >> 10)+1;
275 Log(" 0x%08x-0x%08x :: 0x%08x",
276 page<<22, ((page+1)<<22)-1,
277 gaPageDir[page]&~0xFFF
283 Log("Table Entries:");
284 for(page = Start, curPos = Start<<12;
286 curPos += 0x1000, page++)
288 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
289 || !(gaPageTable[page] & PF_PRESENT)
290 || (gaPageTable[page] & MASK) != expected)
293 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
294 rangeStart, curPos - 1,
295 gaPageTable[rangeStart>>12] & ~0xFFF,
296 (expected & ~0xFFF) - 1,
297 (expected & PF_PAGED ? "p" : "-"),
298 (expected & PF_COW ? "C" : "-"),
299 (expected & PF_USER ? "U" : "-"),
300 (expected & PF_WRITE ? "W" : "-")
304 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
305 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
307 expected = (gaPageTable[page] & MASK);
310 if(expected) expected += 0x1000;
314 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
315 rangeStart, curPos - 1,
316 gaPageTable[rangeStart>>12] & ~0xFFF,
317 (expected & ~0xFFF) - 1,
318 (expected & PF_PAGED ? "p" : "-"),
319 (expected & PF_COW ? "C" : "-"),
320 (expected & PF_USER ? "U" : "-"),
321 (expected & PF_WRITE ? "W" : "-")
328 * \fn tPAddr MM_Allocate(tVAddr VAddr)
330 tPAddr MM_Allocate(tVAddr VAddr)
333 //ENTER("xVAddr", VAddr);
334 //__asm__ __volatile__ ("xchg %bx,%bx");
335 // Check if the directory is mapped
336 if( gaPageDir[ VAddr >> 22 ] == 0 )
338 // Allocate directory
339 paddr = MM_AllocPhys();
340 //LOG("paddr = 0x%llx (new table)", paddr);
342 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
347 gaPageDir[ VAddr >> 22 ] = paddr | 3;
349 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
351 INVLPG( &gaPageDir[ VAddr >> 22 ] );
352 //LOG("Clearing new table");
353 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
355 // Check if the page is already allocated
356 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
357 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
358 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
359 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
363 paddr = MM_AllocPhys();
364 //LOG("paddr = 0x%llx", paddr);
366 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
367 VAddr, __builtin_return_address(0));
372 gaPageTable[ VAddr >> 12 ] = paddr | 3;
374 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
375 // Invalidate Cache for address
376 INVLPG( VAddr & ~0xFFF );
383 * \fn void MM_Deallocate(tVAddr VAddr)
385 void MM_Deallocate(tVAddr VAddr)
387 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
388 Warning("MM_Deallocate - Directory not mapped");
392 if(gaPageTable[ VAddr >> 12 ] == 0) {
393 Warning("MM_Deallocate - Page is not allocated");
398 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
400 gaPageTable[ VAddr >> 12 ] = 0;
404 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
405 * \brief Checks if the passed address is accesable
407 tPAddr MM_GetPhysAddr(tVAddr Addr)
409 if( !(gaPageDir[Addr >> 22] & 1) )
411 if( !(gaPageTable[Addr >> 12] & 1) )
413 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
417 * \fn void MM_SetCR3(Uint CR3)
418 * \brief Sets the current process space
420 void MM_SetCR3(Uint CR3)
422 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
426 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
427 * \brief Map a physical page to a virtual one
429 int MM_Map(tVAddr VAddr, tPAddr PAddr)
431 //ENTER("xVAddr xPAddr", VAddr, PAddr);
433 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
434 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
440 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
442 // Check if the directory is mapped
443 if( gaPageDir[ VAddr >> 22 ] == 0 )
445 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
448 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
450 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
451 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
453 // Check if the page is already allocated
454 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
455 Warning("MM_Map - Allocating to used address");
461 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
463 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
465 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
466 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
471 //LOG("INVLPG( 0x%x )", VAddr);
479 * \fn tVAddr MM_ClearUser()
480 * \brief Clear user's address space
482 tVAddr MM_ClearUser()
487 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
489 // Check if directory is not allocated
490 if( !(gaPageDir[i] & PF_PRESENT) ) {
496 for( j = 0; j < 1024; j ++ )
498 if( gaPageTable[i*1024+j] & 1 )
499 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
500 gaPageTable[i*1024+j] = 0;
503 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
505 INVLPG( &gaPageTable[i*1024] );
513 * \fn tPAddr MM_Clone()
514 * \brief Clone the current address space
521 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
524 LOCK( &gilTempFractal );
526 // Create Directory Table
527 *gpTmpCR3 = MM_AllocPhys() | 3;
529 //LOG("Allocated Directory (%x)", *gpTmpCR3);
530 memsetd( gaTmpDir, 0, 1024 );
533 for( i = 0; i < 768; i ++)
535 // Check if table is allocated
536 if( !(gaPageDir[i] & PF_PRESENT) ) {
542 // Allocate new table
543 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
544 INVLPG( &gaTmpTable[page] );
546 for( j = 0; j < 1024; j ++, page++ )
548 if( !(gaPageTable[page] & PF_PRESENT) ) {
549 gaTmpTable[page] = 0;
554 MM_RefPhys( gaPageTable[page] & ~0xFFF );
556 if(gaPageTable[page] & PF_WRITE) {
557 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
558 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
559 INVLPG( page << 12 );
562 gaTmpTable[page] = gaPageTable[page];
566 // Map in kernel tables (and make fractal mapping)
567 for( i = 768; i < 1024; i ++ )
570 if( i == (PAGE_TABLE_ADDR >> 22) ) {
571 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
575 if( gaPageDir[i] == 0 ) {
580 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
581 MM_RefPhys( gaPageDir[i] & ~0xFFF );
582 gaTmpDir[i] = gaPageDir[i];
585 // Allocate kernel stack
586 for(i = KERNEL_STACKS >> 22;
587 i < KERNEL_STACKS_END >> 22;
590 // Check if directory is allocated
591 if( (gaPageDir[i] & 1) == 0 ) {
596 // We don't care about other kernel stacks, just the current one
597 if( i != kStackBase >> 22 ) {
598 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
604 gaTmpDir[i] = MM_AllocPhys() | 3;
605 INVLPG( &gaTmpTable[i*1024] );
606 for( j = 0; j < 1024; j ++ )
608 // Is the page allocated? If not, skip
609 if( !(gaPageTable[i*1024+j] & 1) ) {
610 gaTmpTable[i*1024+j] = 0;
614 // We don't care about other kernel stacks
615 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
616 gaTmpTable[i*1024+j] = 0;
621 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
623 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
625 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
626 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
627 MM_FreeTemp( (Uint)tmp );
631 ret = *gpTmpCR3 & ~0xFFF;
632 RELEASE( &gilTempFractal );
639 * \fn tVAddr MM_NewKStack()
640 * \brief Create a new kernel stack
642 tVAddr MM_NewKStack()
644 tVAddr base = KERNEL_STACKS;
646 for(;base<KERNEL_STACKS_END;base+=KERNEL_STACK_SIZE)
648 if(MM_GetPhysAddr(base) != 0) continue;
649 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
652 return base+KERNEL_STACK_SIZE;
654 Warning("MM_NewKStack - No address space left\n");
659 * \fn tVAddr MM_NewWorkerStack()
660 * \brief Creates a new worker stack
662 tVAddr MM_NewWorkerStack()
669 tPAddr pages[WORKER_STACK_SIZE>>12];
671 // Get the old ESP and EBP
672 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
673 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
675 // Find a free worker stack address
676 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
679 if( gWorkerStacks[base/32] == -1 ) {
680 base += 31; base &= ~31;
681 base --; // Counteracted by the base++
685 if( gWorkerStacks[base/32] & (1 << base) ) {
690 if(base >= NUM_WORKER_STACKS) {
691 Warning("Uh-oh! Out of worker stacks");
696 gWorkerStacks[base/32] |= (1 << base);
697 // Make life easier for later calls
698 giLastUsedWorker = base;
700 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
701 //Log(" MM_NewWorkerStack: base = 0x%x", base);
703 // Acquire the lock for the temp fractal mappings
704 LOCK(&gilTempFractal);
706 // Set the temp fractals to TID0's address space
707 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
708 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
712 // Check if the directory is mapped (we are assuming that the stacks
713 // will fit neatly in a directory)
714 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
715 if(gaTmpDir[ base >> 22 ] == 0) {
716 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
717 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
721 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
723 pages[ addr >> 12 ] = MM_AllocPhys();
724 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
727 // Release the temp mapping lock
728 RELEASE(&gilTempFractal);
730 // Copy the old stack
731 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
732 esp = oldstack - esp; // ESP as an offset in the stack
734 // Make `base` be the top of the stack
735 base += WORKER_STACK_SIZE;
737 i = (WORKER_STACK_SIZE>>12) - 1;
738 // Copy the contents of the old stack to the new one, altering the addresses
739 // `addr` is refering to bytes from the stack base (mem downwards)
740 for(addr = 0; addr < esp; addr += 0x1000)
742 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
743 tmpPage = (void*)MM_MapTemp( pages[i] );
745 for(j = 0; j < 1024; j++)
747 // Possible Stack address?
748 if(oldstack-esp < stack[j] && stack[j] < oldstack)
749 tmpPage[j] = base - (oldstack - stack[j]);
750 else // Seems not, best leave it alone
751 tmpPage[j] = stack[j];
753 MM_FreeTemp((tVAddr)tmpPage);
757 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
762 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
763 * \brief Sets the flags on a page
765 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
768 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
769 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
771 ent = &gaPageTable[VAddr >> 12];
774 if( Mask & MM_PFLAG_RO )
776 if( Flags & MM_PFLAG_RO ) {
780 gaPageDir[VAddr >> 22] |= PF_WRITE;
786 if( Mask & MM_PFLAG_KERNEL )
788 if( Flags & MM_PFLAG_KERNEL ) {
792 gaPageDir[VAddr >> 22] |= PF_USER;
798 if( Mask & MM_PFLAG_COW )
800 if( Flags & MM_PFLAG_COW ) {
810 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
811 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
815 * \brief Get the flags on a page
817 Uint MM_GetFlags(tVAddr VAddr)
823 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
824 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
826 ent = &gaPageTable[VAddr >> 12];
829 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
831 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
833 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
839 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
840 * \brief Duplicates a virtual page to a physical one
842 tPAddr MM_DuplicatePage(tVAddr VAddr)
848 //ENTER("xVAddr", VAddr);
851 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
852 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
858 ret = MM_AllocPhys();
860 // Write-lock the page (to keep data constistent), saving its R/W state
861 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
862 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
866 temp = MM_MapTemp(ret);
867 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
870 // Restore Writeable status
871 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
879 * \fn Uint MM_MapTemp(tPAddr PAddr)
880 * \brief Create a temporary memory mapping
881 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
883 tVAddr MM_MapTemp(tPAddr PAddr)
887 //ENTER("XPAddr", PAddr);
891 //LOG("gilTempMappings = %i", gilTempMappings);
895 LOCK( &gilTempMappings );
897 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
899 // Check if page used
900 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
902 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
903 INVLPG( TEMP_MAP_ADDR + (i << 12) );
904 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
905 RELEASE( &gilTempMappings );
906 return TEMP_MAP_ADDR + (i << 12);
908 RELEASE( &gilTempMappings );
914 * \fn void MM_FreeTemp(tVAddr PAddr)
915 * \brief Free's a temp mapping
917 void MM_FreeTemp(tVAddr VAddr)
920 //ENTER("xVAddr", VAddr);
922 if(i >= (TEMP_MAP_ADDR >> 12))
923 gaPageTable[ i ] = 0;
929 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
930 * \brief Allocates a contigous number of pages
932 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
939 for( i = 0; i < NUM_HW_PAGES; i ++ )
941 // Check if addr used
942 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
945 // Check possible region
946 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
948 // If there is an allocated page in the region we are testing, break
949 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
955 for( j = 0; j < Number; j++ ) {
956 MM_RefPhys( PAddr + (j<<12) );
957 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
959 return HW_MAP_ADDR + (i<<12);
962 // If we don't find any, return NULL
967 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
968 * \brief Allocates DMA physical memory
969 * \param Pages Number of pages required
970 * \param MaxBits Maximum number of bits the physical address can have
971 * \param PhysAddr Pointer to the location to place the physical address allocated
972 * \return Virtual address allocate
974 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
976 tPAddr maxCheck = (1 << MaxBits);
980 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
983 if(MaxBits < 12 || !PhysAddr) {
989 if(MaxBits >= PHYS_BITS) maxCheck = -1;
992 if(Pages == 1 && MaxBits >= PHYS_BITS)
994 phys = MM_AllocPhys();
996 ret = MM_MapHWPages(phys, 1);
1007 phys = MM_AllocPhysRange(Pages, MaxBits);
1008 // - Was it allocated?
1014 // Allocated successfully, now map
1015 ret = MM_MapHWPages(phys, Pages);
1017 // If it didn't map, free then return 0
1018 for(;Pages--;phys+=0x1000)
1030 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1031 * \brief Unmap a hardware page
1033 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1037 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1040 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1044 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
1047 for( j = 0; j < Number; j++ )
1049 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1050 gaPageTable[ i + j ] = 0;
1053 RELEASE( &gilTempMappings );
1057 EXPORT(MM_GetPhysAddr);
1060 EXPORT(MM_MapHWPages);
1061 EXPORT(MM_AllocDMA);
1062 EXPORT(MM_UnmapHWPages);