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()
690 tPAddr pages[WORKER_STACK_SIZE>>12];
692 // Get the old ESP and EBP
693 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
694 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
696 // TODO: Thread safety
697 // Find a free worker stack address
698 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
701 if( gWorkerStacks[base/32] == -1 ) {
702 base += 31; base &= ~31;
703 base --; // Counteracted by the base++
707 if( gWorkerStacks[base/32] & (1 << base) ) {
712 if(base >= NUM_WORKER_STACKS) {
713 Warning("Uh-oh! Out of worker stacks");
718 gWorkerStacks[base/32] |= (1 << base);
719 // Make life easier for later calls
720 giLastUsedWorker = base;
722 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
723 //Log(" MM_NewWorkerStack: base = 0x%x", base);
725 // Acquire the lock for the temp fractal mappings
726 Mutex_Acquire(&glTempFractal);
728 // Set the temp fractals to TID0's address space
729 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
730 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
734 // Check if the directory is mapped (we are assuming that the stacks
735 // will fit neatly in a directory)
736 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
737 if(gaTmpDir[ base >> 22 ] == 0) {
738 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
739 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
743 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
744 //for( addr = WORKER_STACK_SIZE; addr; addr -= 0x1000 )
746 pages[ addr >> 12 ] = MM_AllocPhys();
747 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
750 // Release the temp mapping lock
751 Mutex_Release(&glTempFractal);
753 // Copy the old stack
754 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
755 esp = oldstack - esp; // ESP as an offset in the stack
757 // Make `base` be the top of the stack
758 base += WORKER_STACK_SIZE;
760 i = (WORKER_STACK_SIZE>>12) - 1;
761 // Copy the contents of the old stack to the new one, altering the addresses
762 // `addr` is refering to bytes from the stack base (mem downwards)
763 for(addr = 0; addr < esp; addr += 0x1000)
765 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
766 tmpPage = (void*)MM_MapTemp( pages[i] );
768 for(j = 0; j < 1024; j++)
770 // Possible Stack address?
771 if(oldstack-esp < stack[j] && stack[j] < oldstack)
772 tmpPage[j] = base - (oldstack - stack[j]);
773 else // Seems not, best leave it alone
774 tmpPage[j] = stack[j];
776 MM_FreeTemp((tVAddr)tmpPage);
780 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
785 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
786 * \brief Sets the flags on a page
788 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
791 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
792 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
794 ent = &gaPageTable[VAddr >> 12];
797 if( Mask & MM_PFLAG_RO )
799 if( Flags & MM_PFLAG_RO ) {
803 gaPageDir[VAddr >> 22] |= PF_WRITE;
809 if( Mask & MM_PFLAG_KERNEL )
811 if( Flags & MM_PFLAG_KERNEL ) {
815 gaPageDir[VAddr >> 22] |= PF_USER;
821 if( Mask & MM_PFLAG_COW )
823 if( Flags & MM_PFLAG_COW ) {
833 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
834 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
838 * \brief Get the flags on a page
840 Uint MM_GetFlags(tVAddr VAddr)
846 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
847 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
849 ent = &gaPageTable[VAddr >> 12];
852 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
854 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
856 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
862 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
863 * \brief Duplicates a virtual page to a physical one
865 tPAddr MM_DuplicatePage(tVAddr VAddr)
871 //ENTER("xVAddr", VAddr);
874 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
875 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
881 ret = MM_AllocPhys();
886 // Write-lock the page (to keep data constistent), saving its R/W state
887 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
888 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
892 temp = MM_MapTemp(ret);
893 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
896 // Restore Writeable status
897 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
905 * \fn Uint MM_MapTemp(tPAddr PAddr)
906 * \brief Create a temporary memory mapping
907 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
909 tVAddr MM_MapTemp(tPAddr PAddr)
913 //ENTER("XPAddr", PAddr);
917 //LOG("glTempMappings = %i", glTempMappings);
921 Mutex_Acquire( &glTempMappings );
923 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
925 // Check if page used
926 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
928 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
929 INVLPG( TEMP_MAP_ADDR + (i << 12) );
930 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
931 Mutex_Release( &glTempMappings );
932 return TEMP_MAP_ADDR + (i << 12);
934 Mutex_Release( &glTempMappings );
935 Threads_Yield(); // TODO: Use a sleep queue here instead
940 * \fn void MM_FreeTemp(tVAddr PAddr)
941 * \brief Free's a temp mapping
943 void MM_FreeTemp(tVAddr VAddr)
946 //ENTER("xVAddr", VAddr);
948 if(i >= (TEMP_MAP_ADDR >> 12))
949 gaPageTable[ i ] = 0;
955 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
956 * \brief Allocates a contigous number of pages
958 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
965 for( i = 0; i < NUM_HW_PAGES; i ++ )
967 // Check if addr used
968 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
971 // Check possible region
972 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
974 // If there is an allocated page in the region we are testing, break
975 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
981 for( j = 0; j < Number; j++ ) {
982 MM_RefPhys( PAddr + (j<<12) );
983 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
985 return HW_MAP_ADDR + (i<<12);
988 // If we don't find any, return NULL
993 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
994 * \brief Allocates DMA physical memory
995 * \param Pages Number of pages required
996 * \param MaxBits Maximum number of bits the physical address can have
997 * \param PhysAddr Pointer to the location to place the physical address allocated
998 * \return Virtual address allocate
1000 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1002 tPAddr maxCheck = (1 << MaxBits);
1006 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1009 if(MaxBits < 12 || !PhysAddr) {
1015 if(MaxBits >= PHYS_BITS) maxCheck = -1;
1018 if(Pages == 1 && MaxBits >= PHYS_BITS)
1020 phys = MM_AllocPhys();
1026 ret = MM_MapHWPages(phys, 1);
1037 phys = MM_AllocPhysRange(Pages, MaxBits);
1038 // - Was it allocated?
1044 // Allocated successfully, now map
1045 ret = MM_MapHWPages(phys, Pages);
1047 // If it didn't map, free then return 0
1048 for(;Pages--;phys+=0x1000)
1060 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1061 * \brief Unmap a hardware page
1063 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1067 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1070 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1074 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1076 for( j = 0; j < Number; j++ )
1078 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1079 gaPageTable[ i + j ] = 0;
1082 Mutex_Release( &glTempMappings );