2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
22 #define WORKER_STACKS 0x00100000 // Thread0 Only!
23 #define WORKER_STACK_SIZE MM_KERNEL_STACK_SIZE
24 #define WORKER_STACKS_END 0xB0000000
25 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
27 #define PAE_PAGE_TABLE_ADDR 0xFC000000 // 16 MiB
28 #define PAE_PAGE_DIR_ADDR 0xFCFC0000 // 16 KiB
29 #define PAE_PAGE_PDPT_ADDR 0xFCFC3F00 // 32 bytes
30 #define PAE_TMP_PDPT_ADDR 0xFCFC3F20 // 32 bytes
31 #define PAE_TMP_DIR_ADDR 0xFCFE0000 // 16 KiB
32 #define PAE_TMP_TABLE_ADDR 0xFD000000 // 16 MiB
34 #define PAGE_TABLE_ADDR 0xFC000000
35 #define PAGE_DIR_ADDR 0xFC3F0000
36 #define PAGE_CR3_ADDR 0xFC3F0FC0
37 #define TMP_CR3_ADDR 0xFC3F0FC4 // Part of core instead of temp
38 #define TMP_DIR_ADDR 0xFC3F1000 // Same
39 #define TMP_TABLE_ADDR 0xFC400000
41 #define HW_MAP_ADDR 0xFE000000
42 #define HW_MAP_MAX 0xFFEF0000
43 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
44 #define TEMP_MAP_ADDR 0xFFEF0000 // Allows 16 "temp" pages
45 #define NUM_TEMP_PAGES 16
46 #define LAST_BLOCK_ADDR 0xFFFF0000 // Free space for kernel provided user code/ *(-1) protection
48 #define PF_PRESENT 0x1
51 #define PF_GLOBAL 0x80
53 #define PF_NOPAGE 0x400
55 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
57 typedef Uint32 tTabEnt;
60 extern char _UsertextEnd[], _UsertextBase[];
61 extern Uint32 gaInitPageDir[1024];
62 extern Uint32 gaInitPageTable[1024];
63 extern void Threads_SegFault(tVAddr Addr);
64 extern void Error_Backtrace(Uint eip, Uint ebp);
67 void MM_PreinitVirtual(void);
68 void MM_InstallVirtual(void);
69 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
70 //void MM_DumpTables(tVAddr Start, tVAddr End);
71 //void MM_ClearUser(void);
72 tPAddr MM_DuplicatePage(tVAddr VAddr);
75 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
76 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
77 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
78 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
79 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
80 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
82 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
83 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
84 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
85 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
86 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
87 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
89 tMutex glTempMappings;
91 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
92 int giLastUsedWorker = 0;
99 } *gaMappedRegions; // sizeof = 24 bytes
103 * \fn void MM_PreinitVirtual(void)
104 * \brief Maps the fractal mappings
106 void MM_PreinitVirtual(void)
108 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
109 INVLPG( PAGE_TABLE_ADDR );
113 * \fn void MM_InstallVirtual(void)
114 * \brief Sets up the constant page mappings
116 void MM_InstallVirtual(void)
120 // --- Pre-Allocate kernel tables
121 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
123 if( gaPageDir[ i ] ) continue;
124 // Skip stack tables, they are process unique
125 if( i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) {
130 gaPageDir[ i ] = MM_AllocPhys() | 3;
131 INVLPG( &gaPageTable[i*1024] );
132 memset( &gaPageTable[i*1024], 0, 0x1000 );
135 // Unset kernel on the User Text pages
136 for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
137 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
142 * \brief Cleans up the SMP required mappings
144 void MM_FinishVirtualInit(void)
146 gaInitPageDir[ 0 ] = 0;
150 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
151 * \brief Called on a page fault
153 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
155 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
157 // -- Check for COW --
158 if( gaPageDir [Addr>>22] & PF_PRESENT && gaPageTable[Addr>>12] & PF_PRESENT
159 && gaPageTable[Addr>>12] & PF_COW )
162 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
164 gaPageTable[Addr>>12] &= ~PF_COW;
165 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
169 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
170 paddr = MM_DuplicatePage( Addr );
171 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
172 gaPageTable[Addr>>12] &= PF_USER;
173 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
176 // Log_Debug("MMVirt", "COW for %p (%P)", Addr, gaPageTable[Addr>>12]);
178 INVLPG( Addr & ~0xFFF );
182 __asm__ __volatile__ ("pushf; andw $0xFEFF, 0(%esp); popf");
183 Proc_GetCurThread()->bInstrTrace = 0;
185 // If it was a user, tell the thread handler
187 Log_Warning("MMVirt", "User %s %s memory%s",
188 (ErrorCode&2?"write to":"read from"),
189 (ErrorCode&1?"bad/locked":"non-present"),
190 (ErrorCode&16?" (Instruction Fetch)":"")
192 Log_Warning("MMVirt", "Instruction %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
193 __asm__ __volatile__ ("sti"); // Restart IRQs
195 Error_Backtrace(Regs->eip, Regs->ebp);
197 Threads_SegFault(Addr);
203 // -- Check Error Code --
205 Warning("Reserved Bits Trashed!");
208 Warning("Kernel %s %s memory%s",
209 (ErrorCode&2?"write to":"read from"),
210 (ErrorCode&1?"bad/locked":"non-present"),
211 (ErrorCode&16?" (Instruction Fetch)":"")
215 Log("CPU %i - Code at %p accessed %p", GetCPUNum(), Regs->eip, Addr);
216 // Print Stack Backtrace
217 Error_Backtrace(Regs->eip, Regs->ebp);
220 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
221 if( gaPageDir[Addr>>22] & PF_PRESENT )
222 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
224 //MM_DumpTables(0, -1);
227 Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
228 Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
229 //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
230 Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
231 Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
234 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
235 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
236 Log("DR0 %08x DR1 %08x", dr0, dr1);
239 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
243 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
244 * \brief Dumps the layout of the page tables
246 void MM_DumpTables(tVAddr Start, tVAddr End)
248 tVAddr rangeStart = 0;
250 void *expected_node = NULL, *tmpnode = NULL;
253 const tPAddr MASK = ~0xF78;
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
281 || (tmpnode=NULL,MM_GetPageNode(expected, &tmpnode), tmpnode != expected_node))
284 tPAddr orig = gaPageTable[rangeStart>>12];
285 Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
289 (orig & PF_NOPAGE ? "P" : "-"),
290 (orig & PF_COW ? "C" : "-"),
291 (orig & PF_GLOBAL ? "G" : "-"),
292 (orig & PF_USER ? "U" : "-"),
293 (orig & PF_WRITE ? "W" : "-"),
298 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
299 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
301 expected = (gaPageTable[page] & MASK);
302 MM_GetPageNode(expected, &expected_node);
305 if(expected) expected += 0x1000;
309 tPAddr orig = gaPageTable[rangeStart>>12];
310 Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
314 (orig & PF_NOPAGE ? "p" : "-"),
315 (orig & PF_COW ? "C" : "-"),
316 (orig & PF_GLOBAL ? "G" : "-"),
317 (orig & PF_USER ? "U" : "-"),
318 (orig & PF_WRITE ? "W" : "-"),
326 * \fn tPAddr MM_Allocate(tVAddr VAddr)
328 tPAddr MM_Allocate(tVAddr VAddr)
331 //ENTER("xVAddr", VAddr);
332 //__asm__ __volatile__ ("xchg %bx,%bx");
333 // Check if the directory is mapped
334 if( gaPageDir[ VAddr >> 22 ] == 0 )
336 // Allocate directory
337 paddr = MM_AllocPhys();
339 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
343 // Map and mark as user (if needed)
344 gaPageDir[ VAddr >> 22 ] = paddr | 3;
345 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
347 INVLPG( &gaPageDir[ VAddr >> 22 ] );
348 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
350 // Check if the page is already allocated
351 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
352 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
353 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
354 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
358 paddr = MM_AllocPhys();
359 //LOG("paddr = 0x%llx", paddr);
361 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
362 VAddr, __builtin_return_address(0));
367 gaPageTable[ VAddr >> 12 ] = paddr | 3;
369 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
370 // Invalidate Cache for address
371 INVLPG( VAddr & ~0xFFF );
378 * \fn void MM_Deallocate(tVAddr VAddr)
380 void MM_Deallocate(tVAddr VAddr)
382 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
383 Warning("MM_Deallocate - Directory not mapped");
387 if(gaPageTable[ VAddr >> 12 ] == 0) {
388 Warning("MM_Deallocate - Page is not allocated");
393 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
395 gaPageTable[ VAddr >> 12 ] = 0;
399 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
400 * \brief Checks if the passed address is accesable
402 tPAddr MM_GetPhysAddr(tVAddr Addr)
404 if( !(gaPageDir[Addr >> 22] & 1) )
406 if( !(gaPageTable[Addr >> 12] & 1) )
408 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
412 * \fn void MM_SetCR3(Uint CR3)
413 * \brief Sets the current process space
415 void MM_SetCR3(Uint CR3)
417 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
421 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
422 * \brief Map a physical page to a virtual one
424 int MM_Map(tVAddr VAddr, tPAddr PAddr)
426 //ENTER("xVAddr xPAddr", VAddr, PAddr);
428 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
429 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
435 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
437 // Check if the directory is mapped
438 if( gaPageDir[ VAddr >> 22 ] == 0 )
440 tPAddr tmp = MM_AllocPhys();
443 gaPageDir[ VAddr >> 22 ] = tmp | 3;
446 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
448 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
449 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
451 // Check if the page is already allocated
452 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
453 Warning("MM_Map - Allocating to used address");
459 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
461 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
463 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
464 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
469 //LOG("INVLPG( 0x%x )", VAddr);
477 * \brief Clear user's address space
479 void MM_ClearUser(void)
483 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
485 // Check if directory is not allocated
486 if( !(gaPageDir[i] & PF_PRESENT) ) {
492 for( j = 0; j < 1024; j ++ )
494 if( gaPageTable[i*1024+j] & 1 )
495 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
496 gaPageTable[i*1024+j] = 0;
499 // Deallocate directory
500 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
502 INVLPG( &gaPageTable[i*1024] );
508 * \fn tPAddr MM_Clone(void)
509 * \brief Clone the current address space
511 tPAddr MM_Clone(void)
516 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - MM_KERNEL_STACK_SIZE;
519 Mutex_Acquire( &glTempFractal );
521 // Create Directory Table
522 *gpTmpCR3 = MM_AllocPhys() | 3;
523 if( *gpTmpCR3 == 3 ) {
528 //LOG("Allocated Directory (%x)", *gpTmpCR3);
529 memsetd( gaTmpDir, 0, 1024 );
531 if( Threads_GetPID() != 0 )
534 for( i = 0; i < 768; i ++)
536 // Check if table is allocated
537 if( !(gaPageDir[i] & PF_PRESENT) ) {
543 // Allocate new table
544 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
545 INVLPG( &gaTmpTable[page] );
547 for( j = 0; j < 1024; j ++, page++ )
549 if( !(gaPageTable[page] & PF_PRESENT) ) {
550 gaTmpTable[page] = 0;
555 MM_RefPhys( gaPageTable[page] & ~0xFFF );
557 if(gaPageTable[page] & PF_WRITE) {
558 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
559 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
560 INVLPG( page << 12 );
563 gaTmpTable[page] = gaPageTable[page];
568 // Map in kernel tables (and make fractal mapping)
569 for( i = 768; i < 1024; i ++ )
572 if( i == (PAGE_TABLE_ADDR >> 22) ) {
573 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
577 if( gaPageDir[i] == 0 ) {
582 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
583 MM_RefPhys( gaPageDir[i] & ~0xFFF );
584 gaTmpDir[i] = gaPageDir[i];
587 // Allocate kernel stack
588 for(i = MM_KERNEL_STACKS >> 22; i < MM_KERNEL_STACKS_END >> 22; i ++ )
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 & ~(MM_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 Mutex_Release( &glTempFractal );
639 * \fn tVAddr MM_NewKStack(void)
640 * \brief Create a new kernel stack
642 tVAddr MM_NewKStack(void)
646 for(base = MM_KERNEL_STACKS; base < MM_KERNEL_STACKS_END; base += MM_KERNEL_STACK_SIZE)
648 // Check if space is free
649 if(MM_GetPhysAddr(base) != 0) continue;
651 //for(i = MM_KERNEL_STACK_SIZE; i -= 0x1000 ; )
652 for(i = 0; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
654 if( MM_Allocate(base+i) == 0 )
656 // On error, print a warning and return error
657 Warning("MM_NewKStack - Out of memory");
659 //for( i += 0x1000 ; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
660 // MM_Deallocate(base+i);
665 // Log("MM_NewKStack - Allocated %p", base + MM_KERNEL_STACK_SIZE);
666 return base+MM_KERNEL_STACK_SIZE;
669 Log_Warning("MMVirt", "MM_NewKStack - No address space left");
674 * \fn tVAddr MM_NewWorkerStack()
675 * \brief Creates a new worker stack
677 tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize)
683 // TODO: Thread safety
684 // Find a free worker stack address
685 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
688 if( gWorkerStacks[base/32] == -1 ) {
689 base += 31; base &= ~31;
690 base --; // Counteracted by the base++
694 if( gWorkerStacks[base/32] & (1 << base) ) {
699 if(base >= NUM_WORKER_STACKS) {
700 Warning("Uh-oh! Out of worker stacks");
705 gWorkerStacks[base/32] |= (1 << base);
706 // Make life easier for later calls
707 giLastUsedWorker = base;
709 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
710 //Log(" MM_NewWorkerStack: base = 0x%x", base);
712 // Acquire the lock for the temp fractal mappings
713 Mutex_Acquire(&glTempFractal);
715 // Set the temp fractals to TID0's address space
716 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
717 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
721 // Check if the directory is mapped (we are assuming that the stacks
722 // will fit neatly in a directory)
723 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
724 if(gaTmpDir[ base >> 22 ] == 0) {
725 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
726 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
730 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
732 page = MM_AllocPhys();
733 gaTmpTable[ (base + addr) >> 12 ] = page | 3;
736 // Release the temp mapping lock
737 Mutex_Release(&glTempFractal);
739 // NOTE: Max of 1 page
740 // `page` is the last allocated page from the previious for loop
741 tmpPage = MM_MapTemp( page );
742 memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize);
743 MM_FreeTemp(tmpPage);
745 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
746 return base + WORKER_STACK_SIZE;
750 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
751 * \brief Sets the flags on a page
753 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
756 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
757 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
759 ent = &gaPageTable[VAddr >> 12];
762 if( Mask & MM_PFLAG_RO )
764 if( Flags & MM_PFLAG_RO ) {
768 gaPageDir[VAddr >> 22] |= PF_WRITE;
774 if( Mask & MM_PFLAG_KERNEL )
776 if( Flags & MM_PFLAG_KERNEL ) {
780 gaPageDir[VAddr >> 22] |= PF_USER;
786 if( Mask & MM_PFLAG_COW )
788 if( Flags & MM_PFLAG_COW ) {
798 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
799 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
803 * \brief Get the flags on a page
805 Uint MM_GetFlags(tVAddr VAddr)
811 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
812 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
814 ent = &gaPageTable[VAddr >> 12];
817 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
819 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
821 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
827 * \brief Check if the provided buffer is valid
828 * \return Boolean valid
830 int MM_IsValidBuffer(tVAddr Addr, size_t Size)
835 Size += Addr & (PAGE_SIZE-1);
836 Addr &= ~(PAGE_SIZE-1);
841 // Debug("Addr = %p, Size = 0x%x, dir = %i, tab = %i", Addr, Size, dir, tab);
843 if( !(gaPageDir[dir] & 1) ) return 0;
844 if( !(gaPageTable[tab] & 1) ) return 0;
846 bIsUser = !!(gaPageTable[tab] & PF_USER);
848 while( Size >= PAGE_SIZE )
850 if( (tab & 1023) == 0 )
853 if( !(gaPageDir[dir] & 1) ) return 0;
856 if( !(gaPageTable[tab] & 1) ) return 0;
857 if( bIsUser && !(gaPageTable[tab] & PF_USER) ) return 0;
866 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
867 * \brief Duplicates a virtual page to a physical one
869 tPAddr MM_DuplicatePage(tVAddr VAddr)
875 //ENTER("xVAddr", VAddr);
878 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
879 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
885 ret = MM_AllocPhys();
890 // Write-lock the page (to keep data constistent), saving its R/W state
891 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
892 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
896 temp = MM_MapTemp(ret);
897 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
900 // Restore Writeable status
901 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
909 * \fn Uint MM_MapTemp(tPAddr PAddr)
910 * \brief Create a temporary memory mapping
911 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
913 tVAddr MM_MapTemp(tPAddr PAddr)
917 //ENTER("XPAddr", PAddr);
921 //LOG("glTempMappings = %i", glTempMappings);
925 Mutex_Acquire( &glTempMappings );
927 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
929 // Check if page used
930 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
932 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
933 INVLPG( TEMP_MAP_ADDR + (i << 12) );
934 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
935 Mutex_Release( &glTempMappings );
936 return TEMP_MAP_ADDR + (i << 12);
938 Mutex_Release( &glTempMappings );
939 Threads_Yield(); // TODO: Use a sleep queue here instead
944 * \fn void MM_FreeTemp(tVAddr PAddr)
945 * \brief Free's a temp mapping
947 void MM_FreeTemp(tVAddr VAddr)
950 //ENTER("xVAddr", VAddr);
952 if(i >= (TEMP_MAP_ADDR >> 12))
953 gaPageTable[ i ] = 0;
959 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
960 * \brief Allocates a contigous number of pages
962 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
969 for( i = 0; i < NUM_HW_PAGES; i ++ )
971 // Check if addr used
972 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
975 // Check possible region
976 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
978 // If there is an allocated page in the region we are testing, break
979 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
985 for( j = 0; j < Number; j++ ) {
986 MM_RefPhys( PAddr + (j<<12) );
987 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
989 return HW_MAP_ADDR + (i<<12);
992 // If we don't find any, return NULL
997 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
998 * \brief Allocates DMA physical memory
999 * \param Pages Number of pages required
1000 * \param MaxBits Maximum number of bits the physical address can have
1001 * \param PhysAddr Pointer to the location to place the physical address allocated
1002 * \return Virtual address allocate
1004 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1006 tPAddr maxCheck = (1 << MaxBits);
1010 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1013 if(MaxBits < 12 || !PhysAddr) {
1019 if(MaxBits >= PHYS_BITS) maxCheck = -1;
1022 if(Pages == 1 && MaxBits >= PHYS_BITS)
1024 phys = MM_AllocPhys();
1030 ret = MM_MapHWPages(phys, 1);
1041 phys = MM_AllocPhysRange(Pages, MaxBits);
1042 // - Was it allocated?
1048 // Allocated successfully, now map
1049 ret = MM_MapHWPages(phys, Pages);
1051 // If it didn't map, free then return 0
1052 for(;Pages--;phys+=0x1000)
1064 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1065 * \brief Unmap a hardware page
1067 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1071 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1074 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1078 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1080 for( j = 0; j < Number; j++ )
1082 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1083 gaPageTable[ i + j ] = 0;
1086 Mutex_Release( &glTempMappings );