2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
21 #define KERNEL_STACKS 0xF0000000
22 #define KERNEL_STACK_SIZE 0x00008000
23 #define KERNEL_STACKS_END 0xFC000000
24 #define WORKER_STACKS 0x00100000 // Thread0 Only!
25 #define WORKER_STACK_SIZE KERNEL_STACK_SIZE
26 #define WORKER_STACKS_END 0xB0000000
27 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
29 #define PAE_PAGE_TABLE_ADDR 0xFC000000 // 16 MiB
30 #define PAE_PAGE_DIR_ADDR 0xFCFC0000 // 16 KiB
31 #define PAE_PAGE_PDPT_ADDR 0xFCFC3F00 // 32 bytes
32 #define PAE_TMP_PDPT_ADDR 0xFCFC3F20 // 32 bytes
33 #define PAE_TMP_DIR_ADDR 0xFCFE0000 // 16 KiB
34 #define PAE_TMP_TABLE_ADDR 0xFD000000 // 16 MiB
36 #define PAGE_TABLE_ADDR 0xFC000000
37 #define PAGE_DIR_ADDR 0xFC3F0000
38 #define PAGE_CR3_ADDR 0xFC3F0FC0
39 #define TMP_CR3_ADDR 0xFC3F0FC4 // Part of core instead of temp
40 #define TMP_DIR_ADDR 0xFC3F1000 // Same
41 #define TMP_TABLE_ADDR 0xFC400000
43 #define HW_MAP_ADDR 0xFE000000
44 #define HW_MAP_MAX 0xFFEF0000
45 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
46 #define TEMP_MAP_ADDR 0xFFEF0000 // Allows 16 "temp" pages
47 #define NUM_TEMP_PAGES 16
48 #define LAST_BLOCK_ADDR 0xFFFF0000 // Free space for kernel provided user code/ *(-1) protection
50 #define PF_PRESENT 0x1
53 #define PF_GLOBAL 0x80
55 #define PF_NOPAGE 0x400
57 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
59 typedef Uint32 tTabEnt;
62 extern void _UsertextEnd, _UsertextBase;
63 extern Uint32 gaInitPageDir[1024];
64 extern Uint32 gaInitPageTable[1024];
65 extern void Threads_SegFault(tVAddr Addr);
66 extern void Error_Backtrace(Uint eip, Uint ebp);
69 void MM_PreinitVirtual(void);
70 void MM_InstallVirtual(void);
71 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
72 void MM_DumpTables(tVAddr Start, tVAddr End);
73 tVAddr MM_ClearUser(void);
74 tPAddr MM_DuplicatePage(tVAddr VAddr);
77 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
78 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
79 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
80 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
81 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
82 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
84 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
85 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
86 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
87 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
88 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
89 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
91 tMutex glTempMappings;
93 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
94 int giLastUsedWorker = 0;
101 } *gaMappedRegions; // sizeof = 24 bytes
105 * \fn void MM_PreinitVirtual(void)
106 * \brief Maps the fractal mappings
108 void MM_PreinitVirtual(void)
110 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
111 INVLPG( PAGE_TABLE_ADDR );
115 * \fn void MM_InstallVirtual(void)
116 * \brief Sets up the constant page mappings
118 void MM_InstallVirtual(void)
122 // --- Pre-Allocate kernel tables
123 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
125 if( gaPageDir[ i ] ) continue;
126 // Skip stack tables, they are process unique
127 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
132 gaPageDir[ i ] = MM_AllocPhys() | 3;
133 INVLPG( &gaPageTable[i*1024] );
134 memset( &gaPageTable[i*1024], 0, 0x1000 );
137 // Unset kernel on the User Text pages
138 for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
139 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
144 * \brief Cleans up the SMP required mappings
146 void MM_FinishVirtualInit(void)
148 gaInitPageDir[ 0 ] = 0;
152 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
153 * \brief Called on a page fault
155 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
157 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
159 // -- Check for COW --
160 if( gaPageDir [Addr>>22] & PF_PRESENT && gaPageTable[Addr>>12] & PF_PRESENT
161 && gaPageTable[Addr>>12] & PF_COW )
164 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
166 gaPageTable[Addr>>12] &= ~PF_COW;
167 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
171 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
172 paddr = MM_DuplicatePage( Addr );
173 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
174 gaPageTable[Addr>>12] &= PF_USER;
175 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
178 // Log_Debug("MMVirt", "COW for %p (%P)", Addr, gaPageTable[Addr>>12]);
180 INVLPG( Addr & ~0xFFF );
184 __asm__ __volatile__ ("pushf; andw $0xFEFF, 0(%esp); popf");
185 Proc_GetCurThread()->bInstrTrace = 0;
187 // If it was a user, tell the thread handler
189 Log_Warning("MMVirt", "User %s %s memory%s",
190 (ErrorCode&2?"write to":"read from"),
191 (ErrorCode&1?"bad/locked":"non-present"),
192 (ErrorCode&16?" (Instruction Fetch)":"")
194 Log_Warning("MMVirt", "Instruction %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
195 __asm__ __volatile__ ("sti"); // Restart IRQs
197 Error_Backtrace(Regs->eip, Regs->ebp);
199 Threads_SegFault(Addr);
205 // -- Check Error Code --
207 Warning("Reserved Bits Trashed!");
210 Warning("Kernel %s %s memory%s",
211 (ErrorCode&2?"write to":"read from"),
212 (ErrorCode&1?"bad/locked":"non-present"),
213 (ErrorCode&16?" (Instruction Fetch)":"")
217 Log("Code at %p accessed %p", Regs->eip, Addr);
218 // Print Stack Backtrace
219 Error_Backtrace(Regs->eip, Regs->ebp);
221 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
222 if( gaPageDir[Addr>>22] & PF_PRESENT )
223 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
225 //MM_DumpTables(0, -1);
228 Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
229 Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
230 //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
231 Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
232 Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
235 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
236 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
237 Log("DR0 %08x DR1 %08x", dr0, dr1);
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;
251 void *expected_node = NULL, *tmpnode = NULL;
254 const tPAddr MASK = ~0xF78;
256 Start >>= 12; End >>= 12;
259 Log("Directory Entries:");
260 for(page = Start >> 10;
261 page < (End >> 10)+1;
266 Log(" 0x%08x-0x%08x :: 0x%08x",
267 page<<22, ((page+1)<<22)-1,
268 gaPageDir[page]&~0xFFF
274 Log("Table Entries:");
275 for(page = Start, curPos = Start<<12;
277 curPos += 0x1000, page++)
279 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
280 || !(gaPageTable[page] & PF_PRESENT)
281 || (gaPageTable[page] & MASK) != expected
282 || (tmpnode=NULL,MM_GetPageNode(expected, &tmpnode), tmpnode != expected_node))
285 tPAddr orig = gaPageTable[rangeStart>>12];
286 Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
290 (orig & PF_NOPAGE ? "P" : "-"),
291 (orig & PF_COW ? "C" : "-"),
292 (orig & PF_GLOBAL ? "G" : "-"),
293 (orig & PF_USER ? "U" : "-"),
294 (orig & PF_WRITE ? "W" : "-"),
299 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
300 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
302 expected = (gaPageTable[page] & MASK);
303 MM_GetPageNode(expected, &expected_node);
306 if(expected) expected += 0x1000;
310 tPAddr orig = gaPageTable[rangeStart>>12];
311 Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
315 (orig & PF_NOPAGE ? "p" : "-"),
316 (orig & PF_COW ? "C" : "-"),
317 (orig & PF_GLOBAL ? "G" : "-"),
318 (orig & PF_USER ? "U" : "-"),
319 (orig & PF_WRITE ? "W" : "-"),
327 * \fn tPAddr MM_Allocate(tVAddr VAddr)
329 tPAddr MM_Allocate(tVAddr VAddr)
332 //ENTER("xVAddr", VAddr);
333 //__asm__ __volatile__ ("xchg %bx,%bx");
334 // Check if the directory is mapped
335 if( gaPageDir[ VAddr >> 22 ] == 0 )
337 // Allocate directory
338 paddr = MM_AllocPhys();
340 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
344 // Map and mark as user (if needed)
345 gaPageDir[ VAddr >> 22 ] = paddr | 3;
346 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
348 INVLPG( &gaPageDir[ VAddr >> 22 ] );
349 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
351 // Check if the page is already allocated
352 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
353 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
354 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
355 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
359 paddr = MM_AllocPhys();
360 //LOG("paddr = 0x%llx", paddr);
362 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
363 VAddr, __builtin_return_address(0));
368 gaPageTable[ VAddr >> 12 ] = paddr | 3;
370 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
371 // Invalidate Cache for address
372 INVLPG( VAddr & ~0xFFF );
379 * \fn void MM_Deallocate(tVAddr VAddr)
381 void MM_Deallocate(tVAddr VAddr)
383 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
384 Warning("MM_Deallocate - Directory not mapped");
388 if(gaPageTable[ VAddr >> 12 ] == 0) {
389 Warning("MM_Deallocate - Page is not allocated");
394 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
396 gaPageTable[ VAddr >> 12 ] = 0;
400 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
401 * \brief Checks if the passed address is accesable
403 tPAddr MM_GetPhysAddr(tVAddr Addr)
405 if( !(gaPageDir[Addr >> 22] & 1) )
407 if( !(gaPageTable[Addr >> 12] & 1) )
409 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
413 * \fn void MM_SetCR3(Uint CR3)
414 * \brief Sets the current process space
416 void MM_SetCR3(Uint CR3)
418 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
422 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
423 * \brief Map a physical page to a virtual one
425 int MM_Map(tVAddr VAddr, tPAddr PAddr)
427 //ENTER("xVAddr xPAddr", VAddr, PAddr);
429 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
430 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
436 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
438 // Check if the directory is mapped
439 if( gaPageDir[ VAddr >> 22 ] == 0 )
441 tPAddr tmp = MM_AllocPhys();
444 gaPageDir[ VAddr >> 22 ] = tmp | 3;
447 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
449 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
450 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
452 // Check if the page is already allocated
453 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
454 Warning("MM_Map - Allocating to used address");
460 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
462 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
464 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
465 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
470 //LOG("INVLPG( 0x%x )", VAddr);
478 * \fn tVAddr MM_ClearUser()
479 * \brief Clear user's address space
481 tVAddr MM_ClearUser(void)
485 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
487 // Check if directory is not allocated
488 if( !(gaPageDir[i] & PF_PRESENT) ) {
494 for( j = 0; j < 1024; j ++ )
496 if( gaPageTable[i*1024+j] & 1 )
497 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
498 gaPageTable[i*1024+j] = 0;
501 // Deallocate directory
502 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
504 INVLPG( &gaPageTable[i*1024] );
512 * \fn tPAddr MM_Clone(void)
513 * \brief Clone the current address space
515 tPAddr MM_Clone(void)
520 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
523 Mutex_Acquire( &glTempFractal );
525 // Create Directory Table
526 *gpTmpCR3 = MM_AllocPhys() | 3;
527 if( *gpTmpCR3 == 3 ) {
532 //LOG("Allocated Directory (%x)", *gpTmpCR3);
533 memsetd( gaTmpDir, 0, 1024 );
535 if( Threads_GetPID() != 0 )
538 for( i = 0; i < 768; i ++)
540 // Check if table is allocated
541 if( !(gaPageDir[i] & PF_PRESENT) ) {
547 // Allocate new table
548 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
549 INVLPG( &gaTmpTable[page] );
551 for( j = 0; j < 1024; j ++, page++ )
553 if( !(gaPageTable[page] & PF_PRESENT) ) {
554 gaTmpTable[page] = 0;
559 MM_RefPhys( gaPageTable[page] & ~0xFFF );
561 if(gaPageTable[page] & PF_WRITE) {
562 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
563 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
564 INVLPG( page << 12 );
567 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 Mutex_Release( &glTempFractal );
645 * \fn tVAddr MM_NewKStack(void)
646 * \brief Create a new kernel stack
648 tVAddr MM_NewKStack(void)
652 for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
654 // Check if space is free
655 if(MM_GetPhysAddr(base) != 0) continue;
657 //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
658 for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
660 if( MM_Allocate(base+i) == 0 )
662 // On error, print a warning and return error
663 Warning("MM_NewKStack - Out of memory");
665 //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
666 // MM_Deallocate(base+i);
671 Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
672 return base+KERNEL_STACK_SIZE;
675 Warning("MM_NewKStack - No address space left");
680 * \fn tVAddr MM_NewWorkerStack()
681 * \brief Creates a new worker stack
683 tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize)
689 // TODO: Thread safety
690 // Find a free worker stack address
691 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
694 if( gWorkerStacks[base/32] == -1 ) {
695 base += 31; base &= ~31;
696 base --; // Counteracted by the base++
700 if( gWorkerStacks[base/32] & (1 << base) ) {
705 if(base >= NUM_WORKER_STACKS) {
706 Warning("Uh-oh! Out of worker stacks");
711 gWorkerStacks[base/32] |= (1 << base);
712 // Make life easier for later calls
713 giLastUsedWorker = base;
715 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
716 //Log(" MM_NewWorkerStack: base = 0x%x", base);
718 // Acquire the lock for the temp fractal mappings
719 Mutex_Acquire(&glTempFractal);
721 // Set the temp fractals to TID0's address space
722 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
723 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
727 // Check if the directory is mapped (we are assuming that the stacks
728 // will fit neatly in a directory)
729 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
730 if(gaTmpDir[ base >> 22 ] == 0) {
731 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
732 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
736 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
738 page = MM_AllocPhys();
739 gaTmpTable[ (base + addr) >> 12 ] = page | 3;
742 // Release the temp mapping lock
743 Mutex_Release(&glTempFractal);
745 // NOTE: Max of 1 page
746 // `page` is the last allocated page from the previious for loop
747 tmpPage = MM_MapTemp( page );
748 memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize);
749 MM_FreeTemp(tmpPage);
751 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
752 return base + WORKER_STACK_SIZE;
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 ) {
774 gaPageDir[VAddr >> 22] |= PF_WRITE;
780 if( Mask & MM_PFLAG_KERNEL )
782 if( Flags & MM_PFLAG_KERNEL ) {
786 gaPageDir[VAddr >> 22] |= PF_USER;
792 if( Mask & MM_PFLAG_COW )
794 if( Flags & MM_PFLAG_COW ) {
804 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
805 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
809 * \brief Get the flags on a page
811 Uint MM_GetFlags(tVAddr VAddr)
817 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
818 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
820 ent = &gaPageTable[VAddr >> 12];
823 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
825 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
827 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
833 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
834 * \brief Duplicates a virtual page to a physical one
836 tPAddr MM_DuplicatePage(tVAddr VAddr)
842 //ENTER("xVAddr", VAddr);
845 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
846 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
852 ret = MM_AllocPhys();
857 // Write-lock the page (to keep data constistent), saving its R/W state
858 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
859 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
863 temp = MM_MapTemp(ret);
864 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
867 // Restore Writeable status
868 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
876 * \fn Uint MM_MapTemp(tPAddr PAddr)
877 * \brief Create a temporary memory mapping
878 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
880 tVAddr MM_MapTemp(tPAddr PAddr)
884 //ENTER("XPAddr", PAddr);
888 //LOG("glTempMappings = %i", glTempMappings);
892 Mutex_Acquire( &glTempMappings );
894 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
896 // Check if page used
897 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
899 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
900 INVLPG( TEMP_MAP_ADDR + (i << 12) );
901 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
902 Mutex_Release( &glTempMappings );
903 return TEMP_MAP_ADDR + (i << 12);
905 Mutex_Release( &glTempMappings );
906 Threads_Yield(); // TODO: Use a sleep queue here instead
911 * \fn void MM_FreeTemp(tVAddr PAddr)
912 * \brief Free's a temp mapping
914 void MM_FreeTemp(tVAddr VAddr)
917 //ENTER("xVAddr", VAddr);
919 if(i >= (TEMP_MAP_ADDR >> 12))
920 gaPageTable[ i ] = 0;
926 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
927 * \brief Allocates a contigous number of pages
929 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
936 for( i = 0; i < NUM_HW_PAGES; i ++ )
938 // Check if addr used
939 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
942 // Check possible region
943 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
945 // If there is an allocated page in the region we are testing, break
946 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
952 for( j = 0; j < Number; j++ ) {
953 MM_RefPhys( PAddr + (j<<12) );
954 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
956 return HW_MAP_ADDR + (i<<12);
959 // If we don't find any, return NULL
964 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
965 * \brief Allocates DMA physical memory
966 * \param Pages Number of pages required
967 * \param MaxBits Maximum number of bits the physical address can have
968 * \param PhysAddr Pointer to the location to place the physical address allocated
969 * \return Virtual address allocate
971 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
973 tPAddr maxCheck = (1 << MaxBits);
977 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
980 if(MaxBits < 12 || !PhysAddr) {
986 if(MaxBits >= PHYS_BITS) maxCheck = -1;
989 if(Pages == 1 && MaxBits >= PHYS_BITS)
991 phys = MM_AllocPhys();
997 ret = MM_MapHWPages(phys, 1);
1008 phys = MM_AllocPhysRange(Pages, MaxBits);
1009 // - Was it allocated?
1015 // Allocated successfully, now map
1016 ret = MM_MapHWPages(phys, Pages);
1018 // If it didn't map, free then return 0
1019 for(;Pages--;phys+=0x1000)
1031 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1032 * \brief Unmap a hardware page
1034 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1038 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1041 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1045 Mutex_Acquire( &glTempMappings ); // 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 Mutex_Release( &glTempMappings );