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", Addr);
180 INVLPG( Addr & ~0xFFF );
184 // If it was a user, tell the thread handler
186 Log_Warning("MMVirt", "%s %s %s memory%s",
187 (ErrorCode&4?"User":"Kernel"),
188 (ErrorCode&2?"write to":"read from"),
189 (ErrorCode&1?"bad/locked":"non-present"),
190 (ErrorCode&16?" (Instruction Fetch)":"")
192 Warning("User Pagefault: Instruction at %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("%s %s %s memory%s",
209 (ErrorCode&4?"User":"Kernel"),
210 (ErrorCode&2?"write to":"read from"),
211 (ErrorCode&1?"bad/locked":"non-present"),
212 (ErrorCode&16?" (Instruction Fetch)":"")
216 Log("Code at %p accessed %p", Regs->eip, Addr);
217 // Print Stack Backtrace
218 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;
252 const tPAddr MASK = ~0xF78;
254 Start >>= 12; End >>= 12;
257 Log("Directory Entries:");
258 for(page = Start >> 10;
259 page < (End >> 10)+1;
264 Log(" 0x%08x-0x%08x :: 0x%08x",
265 page<<22, ((page+1)<<22)-1,
266 gaPageDir[page]&~0xFFF
272 Log("Table Entries:");
273 for(page = Start, curPos = Start<<12;
275 curPos += 0x1000, page++)
277 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
278 || !(gaPageTable[page] & PF_PRESENT)
279 || (gaPageTable[page] & MASK) != expected)
282 tPAddr orig = gaPageTable[rangeStart>>12];
283 Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s)",
287 (orig & PF_NOPAGE ? "P" : "-"),
288 (orig & PF_COW ? "C" : "-"),
289 (orig & PF_GLOBAL ? "G" : "-"),
290 (orig & PF_USER ? "U" : "-"),
291 (orig & PF_WRITE ? "W" : "-")
295 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
296 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
298 expected = (gaPageTable[page] & MASK);
301 if(expected) expected += 0x1000;
305 Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s)",
307 gaPageTable[rangeStart>>12] & ~0xFFF,
309 (expected & PF_NOPAGE ? "p" : "-"),
310 (expected & PF_COW ? "C" : "-"),
311 (expected & PF_USER ? "U" : "-"),
312 (expected & PF_WRITE ? "W" : "-")
319 * \fn tPAddr MM_Allocate(tVAddr VAddr)
321 tPAddr MM_Allocate(tVAddr VAddr)
324 //ENTER("xVAddr", VAddr);
325 //__asm__ __volatile__ ("xchg %bx,%bx");
326 // Check if the directory is mapped
327 if( gaPageDir[ VAddr >> 22 ] == 0 )
329 // Allocate directory
330 paddr = MM_AllocPhys();
332 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
336 // Map and mark as user (if needed)
337 gaPageDir[ VAddr >> 22 ] = paddr | 3;
338 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
340 INVLPG( &gaPageDir[ VAddr >> 22 ] );
341 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
343 // Check if the page is already allocated
344 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
345 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
346 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
347 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
351 paddr = MM_AllocPhys();
352 //LOG("paddr = 0x%llx", paddr);
354 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
355 VAddr, __builtin_return_address(0));
360 gaPageTable[ VAddr >> 12 ] = paddr | 3;
362 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
363 // Invalidate Cache for address
364 INVLPG( VAddr & ~0xFFF );
371 * \fn void MM_Deallocate(tVAddr VAddr)
373 void MM_Deallocate(tVAddr VAddr)
375 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
376 Warning("MM_Deallocate - Directory not mapped");
380 if(gaPageTable[ VAddr >> 12 ] == 0) {
381 Warning("MM_Deallocate - Page is not allocated");
386 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
388 gaPageTable[ VAddr >> 12 ] = 0;
392 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
393 * \brief Checks if the passed address is accesable
395 tPAddr MM_GetPhysAddr(tVAddr Addr)
397 if( !(gaPageDir[Addr >> 22] & 1) )
399 if( !(gaPageTable[Addr >> 12] & 1) )
401 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
405 * \fn void MM_SetCR3(Uint CR3)
406 * \brief Sets the current process space
408 void MM_SetCR3(Uint CR3)
410 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
414 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
415 * \brief Map a physical page to a virtual one
417 int MM_Map(tVAddr VAddr, tPAddr PAddr)
419 //ENTER("xVAddr xPAddr", VAddr, PAddr);
421 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
422 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
428 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
430 // Check if the directory is mapped
431 if( gaPageDir[ VAddr >> 22 ] == 0 )
433 tPAddr tmp = MM_AllocPhys();
436 gaPageDir[ VAddr >> 22 ] = tmp | 3;
439 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
441 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
442 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
444 // Check if the page is already allocated
445 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
446 Warning("MM_Map - Allocating to used address");
452 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
454 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
456 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
457 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
462 //LOG("INVLPG( 0x%x )", VAddr);
470 * \fn tVAddr MM_ClearUser()
471 * \brief Clear user's address space
473 tVAddr MM_ClearUser(void)
477 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
479 // Check if directory is not allocated
480 if( !(gaPageDir[i] & PF_PRESENT) ) {
486 for( j = 0; j < 1024; j ++ )
488 if( gaPageTable[i*1024+j] & 1 )
489 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
490 gaPageTable[i*1024+j] = 0;
493 // Deallocate directory
494 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
496 INVLPG( &gaPageTable[i*1024] );
504 * \fn tPAddr MM_Clone(void)
505 * \brief Clone the current address space
507 tPAddr MM_Clone(void)
512 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
515 Mutex_Acquire( &glTempFractal );
517 // Create Directory Table
518 *gpTmpCR3 = MM_AllocPhys() | 3;
519 if( *gpTmpCR3 == 3 ) {
524 //LOG("Allocated Directory (%x)", *gpTmpCR3);
525 memsetd( gaTmpDir, 0, 1024 );
527 if( Threads_GetPID() != 0 )
530 for( i = 0; i < 768; i ++)
532 // Check if table is allocated
533 if( !(gaPageDir[i] & PF_PRESENT) ) {
539 // Allocate new table
540 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
541 INVLPG( &gaTmpTable[page] );
543 for( j = 0; j < 1024; j ++, page++ )
545 if( !(gaPageTable[page] & PF_PRESENT) ) {
546 gaTmpTable[page] = 0;
551 MM_RefPhys( gaPageTable[page] & ~0xFFF );
553 if(gaPageTable[page] & PF_WRITE) {
554 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
555 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
556 INVLPG( page << 12 );
559 gaTmpTable[page] = gaPageTable[page];
564 // Map in kernel tables (and make fractal mapping)
565 for( i = 768; i < 1024; i ++ )
568 if( i == (PAGE_TABLE_ADDR >> 22) ) {
569 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
573 if( gaPageDir[i] == 0 ) {
578 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
579 MM_RefPhys( gaPageDir[i] & ~0xFFF );
580 gaTmpDir[i] = gaPageDir[i];
583 // Allocate kernel stack
584 for(i = KERNEL_STACKS >> 22;
585 i < KERNEL_STACKS_END >> 22;
588 // Check if directory is allocated
589 if( (gaPageDir[i] & 1) == 0 ) {
594 // We don't care about other kernel stacks, just the current one
595 if( i != kStackBase >> 22 ) {
596 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
602 gaTmpDir[i] = MM_AllocPhys() | 3;
603 INVLPG( &gaTmpTable[i*1024] );
604 for( j = 0; j < 1024; j ++ )
606 // Is the page allocated? If not, skip
607 if( !(gaPageTable[i*1024+j] & 1) ) {
608 gaTmpTable[i*1024+j] = 0;
612 // We don't care about other kernel stacks
613 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
614 gaTmpTable[i*1024+j] = 0;
619 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
621 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
623 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
624 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
625 MM_FreeTemp( (Uint)tmp );
629 ret = *gpTmpCR3 & ~0xFFF;
630 Mutex_Release( &glTempFractal );
637 * \fn tVAddr MM_NewKStack(void)
638 * \brief Create a new kernel stack
640 tVAddr MM_NewKStack(void)
644 for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
646 // Check if space is free
647 if(MM_GetPhysAddr(base) != 0) continue;
649 //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
650 for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
652 if( MM_Allocate(base+i) == 0 )
654 // On error, print a warning and return error
655 Warning("MM_NewKStack - Out of memory");
657 //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
658 // MM_Deallocate(base+i);
663 Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
664 return base+KERNEL_STACK_SIZE;
667 Warning("MM_NewKStack - No address space left");
672 * \fn tVAddr MM_NewWorkerStack()
673 * \brief Creates a new worker stack
675 tVAddr MM_NewWorkerStack()
682 tPAddr pages[WORKER_STACK_SIZE>>12];
684 // Get the old ESP and EBP
685 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
686 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
688 // TODO: Thread safety
689 // Find a free worker stack address
690 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
693 if( gWorkerStacks[base/32] == -1 ) {
694 base += 31; base &= ~31;
695 base --; // Counteracted by the base++
699 if( gWorkerStacks[base/32] & (1 << base) ) {
704 if(base >= NUM_WORKER_STACKS) {
705 Warning("Uh-oh! Out of worker stacks");
710 gWorkerStacks[base/32] |= (1 << base);
711 // Make life easier for later calls
712 giLastUsedWorker = base;
714 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
715 //Log(" MM_NewWorkerStack: base = 0x%x", base);
717 // Acquire the lock for the temp fractal mappings
718 Mutex_Acquire(&glTempFractal);
720 // Set the temp fractals to TID0's address space
721 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
722 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
726 // Check if the directory is mapped (we are assuming that the stacks
727 // will fit neatly in a directory)
728 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
729 if(gaTmpDir[ base >> 22 ] == 0) {
730 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
731 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
735 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
736 //for( addr = WORKER_STACK_SIZE; addr; addr -= 0x1000 )
738 pages[ addr >> 12 ] = MM_AllocPhys();
739 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
742 // Release the temp mapping lock
743 Mutex_Release(&glTempFractal);
745 // Copy the old stack
746 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
747 esp = oldstack - esp; // ESP as an offset in the stack
749 // Make `base` be the top of the stack
750 base += WORKER_STACK_SIZE;
752 i = (WORKER_STACK_SIZE>>12) - 1;
753 // Copy the contents of the old stack to the new one, altering the addresses
754 // `addr` is refering to bytes from the stack base (mem downwards)
755 for(addr = 0; addr < esp; addr += 0x1000)
757 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
758 tmpPage = (void*)MM_MapTemp( pages[i] );
760 for(j = 0; j < 1024; j++)
762 // Possible Stack address?
763 if(oldstack-esp < stack[j] && stack[j] < oldstack)
764 tmpPage[j] = base - (oldstack - stack[j]);
765 else // Seems not, best leave it alone
766 tmpPage[j] = stack[j];
768 MM_FreeTemp((tVAddr)tmpPage);
772 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
777 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
778 * \brief Sets the flags on a page
780 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
783 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
784 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
786 ent = &gaPageTable[VAddr >> 12];
789 if( Mask & MM_PFLAG_RO )
791 if( Flags & MM_PFLAG_RO ) {
795 gaPageDir[VAddr >> 22] |= PF_WRITE;
801 if( Mask & MM_PFLAG_KERNEL )
803 if( Flags & MM_PFLAG_KERNEL ) {
807 gaPageDir[VAddr >> 22] |= PF_USER;
813 if( Mask & MM_PFLAG_COW )
815 if( Flags & MM_PFLAG_COW ) {
825 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
826 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
830 * \brief Get the flags on a page
832 Uint MM_GetFlags(tVAddr VAddr)
838 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
839 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
841 ent = &gaPageTable[VAddr >> 12];
844 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
846 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
848 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
854 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
855 * \brief Duplicates a virtual page to a physical one
857 tPAddr MM_DuplicatePage(tVAddr VAddr)
863 //ENTER("xVAddr", VAddr);
866 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
867 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
873 ret = MM_AllocPhys();
878 // Write-lock the page (to keep data constistent), saving its R/W state
879 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
880 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
884 temp = MM_MapTemp(ret);
885 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
888 // Restore Writeable status
889 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
897 * \fn Uint MM_MapTemp(tPAddr PAddr)
898 * \brief Create a temporary memory mapping
899 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
901 tVAddr MM_MapTemp(tPAddr PAddr)
905 //ENTER("XPAddr", PAddr);
909 //LOG("glTempMappings = %i", glTempMappings);
913 Mutex_Acquire( &glTempMappings );
915 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
917 // Check if page used
918 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
920 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
921 INVLPG( TEMP_MAP_ADDR + (i << 12) );
922 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
923 Mutex_Release( &glTempMappings );
924 return TEMP_MAP_ADDR + (i << 12);
926 Mutex_Release( &glTempMappings );
927 Threads_Yield(); // TODO: Use a sleep queue here instead
932 * \fn void MM_FreeTemp(tVAddr PAddr)
933 * \brief Free's a temp mapping
935 void MM_FreeTemp(tVAddr VAddr)
938 //ENTER("xVAddr", VAddr);
940 if(i >= (TEMP_MAP_ADDR >> 12))
941 gaPageTable[ i ] = 0;
947 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
948 * \brief Allocates a contigous number of pages
950 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
957 for( i = 0; i < NUM_HW_PAGES; i ++ )
959 // Check if addr used
960 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
963 // Check possible region
964 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
966 // If there is an allocated page in the region we are testing, break
967 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
973 for( j = 0; j < Number; j++ ) {
974 MM_RefPhys( PAddr + (j<<12) );
975 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
977 return HW_MAP_ADDR + (i<<12);
980 // If we don't find any, return NULL
985 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
986 * \brief Allocates DMA physical memory
987 * \param Pages Number of pages required
988 * \param MaxBits Maximum number of bits the physical address can have
989 * \param PhysAddr Pointer to the location to place the physical address allocated
990 * \return Virtual address allocate
992 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
994 tPAddr maxCheck = (1 << MaxBits);
998 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1001 if(MaxBits < 12 || !PhysAddr) {
1007 if(MaxBits >= PHYS_BITS) maxCheck = -1;
1010 if(Pages == 1 && MaxBits >= PHYS_BITS)
1012 phys = MM_AllocPhys();
1018 ret = MM_MapHWPages(phys, 1);
1029 phys = MM_AllocPhysRange(Pages, MaxBits);
1030 // - Was it allocated?
1036 // Allocated successfully, now map
1037 ret = MM_MapHWPages(phys, Pages);
1039 // If it didn't map, free then return 0
1040 for(;Pages--;phys+=0x1000)
1052 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1053 * \brief Unmap a hardware page
1055 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1059 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1062 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1066 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1068 for( j = 0; j < Number; j++ )
1070 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1071 gaPageTable[ i + j ] = 0;
1074 Mutex_Release( &glTempMappings );