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_NOPAGE 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(void);
78 void MM_InstallVirtual(void);
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 tMutex glTempMappings;
100 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
101 int giLastUsedWorker = 0;
105 * \fn void MM_PreinitVirtual(void)
106 * \brief Maps the fractal mappings
108 void MM_PreinitVirtual(void)
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(void)
120 * \brief Sets up the constant page mappings
122 void MM_InstallVirtual(void)
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 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
166 * \brief Cleans up the SMP required mappings
168 void MM_FinishVirtualInit(void)
173 gaInitPageDir[ 0 ] = 0;
178 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
179 * \brief Called on a page fault
181 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
183 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
185 // -- Check for COW --
186 if( gaPageDir [Addr>>22] & PF_PRESENT
187 && gaPageTable[Addr>>12] & PF_PRESENT
188 && gaPageTable[Addr>>12] & PF_COW )
191 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
193 gaPageTable[Addr>>12] &= ~PF_COW;
194 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
198 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
199 paddr = MM_DuplicatePage( Addr );
200 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
201 gaPageTable[Addr>>12] &= PF_USER;
202 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
205 INVLPG( Addr & ~0xFFF );
210 // If it was a user, tell the thread handler
212 Warning("%s %s %s memory%s",
213 (ErrorCode&4?"User":"Kernel"),
214 (ErrorCode&2?"write to":"read from"),
215 (ErrorCode&1?"bad/locked":"non-present"),
216 (ErrorCode&16?" (Instruction Fetch)":"")
218 Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
219 __asm__ __volatile__ ("sti"); // Restart IRQs
220 Threads_SegFault(Addr);
226 // -- Check Error Code --
228 Warning("Reserved Bits Trashed!");
231 Warning("%s %s %s memory%s",
232 (ErrorCode&4?"User":"Kernel"),
233 (ErrorCode&2?"write to":"read from"),
234 (ErrorCode&1?"bad/locked":"non-present"),
235 (ErrorCode&16?" (Instruction Fetch)":"")
239 Log("Code at %p accessed %p", Regs->eip, Addr);
240 // Print Stack Backtrace
241 Error_Backtrace(Regs->eip, Regs->ebp);
243 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
244 if( gaPageDir[Addr>>22] & PF_PRESENT )
245 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
247 //MM_DumpTables(0, -1);
250 Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
251 Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
252 //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
253 Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
254 Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
257 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
258 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
259 Log("DR0 %08x DR1 %08x", dr0, dr1);
262 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
266 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
267 * \brief Dumps the layout of the page tables
269 void MM_DumpTables(tVAddr Start, tVAddr End)
271 tVAddr rangeStart = 0;
275 const tPAddr MASK = ~0xF98;
277 Start >>= 12; End >>= 12;
280 Log("Directory Entries:");
281 for(page = Start >> 10;
282 page < (End >> 10)+1;
287 Log(" 0x%08x-0x%08x :: 0x%08x",
288 page<<22, ((page+1)<<22)-1,
289 gaPageDir[page]&~0xFFF
295 Log("Table Entries:");
296 for(page = Start, curPos = Start<<12;
298 curPos += 0x1000, page++)
300 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
301 || !(gaPageTable[page] & PF_PRESENT)
302 || (gaPageTable[page] & MASK) != expected)
305 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
306 rangeStart, curPos - 1,
307 gaPageTable[rangeStart>>12] & ~0xFFF,
308 (expected & ~0xFFF) - 1,
309 (expected & PF_NOPAGE ? "P" : "-"),
310 (expected & PF_COW ? "C" : "-"),
311 (expected & PF_USER ? "U" : "-"),
312 (expected & PF_WRITE ? "W" : "-")
316 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
317 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
319 expected = (gaPageTable[page] & MASK);
322 if(expected) expected += 0x1000;
326 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
327 rangeStart, curPos - 1,
328 gaPageTable[rangeStart>>12] & ~0xFFF,
329 (expected & ~0xFFF) - 1,
330 (expected & PF_NOPAGE ? "p" : "-"),
331 (expected & PF_COW ? "C" : "-"),
332 (expected & PF_USER ? "U" : "-"),
333 (expected & PF_WRITE ? "W" : "-")
340 * \fn tPAddr MM_Allocate(tVAddr VAddr)
342 tPAddr MM_Allocate(tVAddr VAddr)
345 //ENTER("xVAddr", VAddr);
346 //__asm__ __volatile__ ("xchg %bx,%bx");
347 // Check if the directory is mapped
348 if( gaPageDir[ VAddr >> 22 ] == 0 )
350 // Allocate directory
351 paddr = MM_AllocPhys();
352 //LOG("paddr = 0x%llx (new table)", paddr);
354 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
359 gaPageDir[ VAddr >> 22 ] = paddr | 3;
361 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
363 INVLPG( &gaPageDir[ VAddr >> 22 ] );
364 //LOG("Clearing new table");
365 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
367 // Check if the page is already allocated
368 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
369 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
370 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
371 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
375 paddr = MM_AllocPhys();
376 //LOG("paddr = 0x%llx", paddr);
378 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
379 VAddr, __builtin_return_address(0));
384 gaPageTable[ VAddr >> 12 ] = paddr | 3;
386 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
387 // Invalidate Cache for address
388 INVLPG( VAddr & ~0xFFF );
395 * \fn void MM_Deallocate(tVAddr VAddr)
397 void MM_Deallocate(tVAddr VAddr)
399 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
400 Warning("MM_Deallocate - Directory not mapped");
404 if(gaPageTable[ VAddr >> 12 ] == 0) {
405 Warning("MM_Deallocate - Page is not allocated");
410 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
412 gaPageTable[ VAddr >> 12 ] = 0;
416 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
417 * \brief Checks if the passed address is accesable
419 tPAddr MM_GetPhysAddr(tVAddr Addr)
421 if( !(gaPageDir[Addr >> 22] & 1) )
423 if( !(gaPageTable[Addr >> 12] & 1) )
425 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
429 * \fn void MM_SetCR3(Uint CR3)
430 * \brief Sets the current process space
432 void MM_SetCR3(Uint CR3)
434 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
438 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
439 * \brief Map a physical page to a virtual one
441 int MM_Map(tVAddr VAddr, tPAddr PAddr)
443 //ENTER("xVAddr xPAddr", VAddr, PAddr);
445 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
446 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
452 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
454 // Check if the directory is mapped
455 if( gaPageDir[ VAddr >> 22 ] == 0 )
457 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
460 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
462 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
463 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
465 // Check if the page is already allocated
466 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
467 Warning("MM_Map - Allocating to used address");
473 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
475 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
477 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
478 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
483 //LOG("INVLPG( 0x%x )", VAddr);
491 * \fn tVAddr MM_ClearUser()
492 * \brief Clear user's address space
494 tVAddr MM_ClearUser(void)
499 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
501 // Check if directory is not allocated
502 if( !(gaPageDir[i] & PF_PRESENT) ) {
508 for( j = 0; j < 1024; j ++ )
510 if( gaPageTable[i*1024+j] & 1 )
511 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
512 gaPageTable[i*1024+j] = 0;
515 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
517 INVLPG( &gaPageTable[i*1024] );
525 * \fn tPAddr MM_Clone(void)
526 * \brief Clone the current address space
528 tPAddr MM_Clone(void)
533 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
536 Mutex_Acquire( &glTempFractal );
538 // Create Directory Table
539 *gpTmpCR3 = MM_AllocPhys() | 3;
541 //LOG("Allocated Directory (%x)", *gpTmpCR3);
542 memsetd( gaTmpDir, 0, 1024 );
544 if( Threads_GetPID() != 0 )
547 for( i = 0; i < 768; i ++)
549 // Check if table is allocated
550 if( !(gaPageDir[i] & PF_PRESENT) ) {
556 // Allocate new table
557 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
558 INVLPG( &gaTmpTable[page] );
560 for( j = 0; j < 1024; j ++, page++ )
562 if( !(gaPageTable[page] & PF_PRESENT) ) {
563 gaTmpTable[page] = 0;
568 MM_RefPhys( gaPageTable[page] & ~0xFFF );
570 if(gaPageTable[page] & PF_WRITE) {
571 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
572 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
573 INVLPG( page << 12 );
576 gaTmpTable[page] = gaPageTable[page];
581 // Map in kernel tables (and make fractal mapping)
582 for( i = 768; i < 1024; i ++ )
585 if( i == (PAGE_TABLE_ADDR >> 22) ) {
586 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
590 if( gaPageDir[i] == 0 ) {
595 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
596 MM_RefPhys( gaPageDir[i] & ~0xFFF );
597 gaTmpDir[i] = gaPageDir[i];
600 // Allocate kernel stack
601 for(i = KERNEL_STACKS >> 22;
602 i < KERNEL_STACKS_END >> 22;
605 // Check if directory is allocated
606 if( (gaPageDir[i] & 1) == 0 ) {
611 // We don't care about other kernel stacks, just the current one
612 if( i != kStackBase >> 22 ) {
613 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
619 gaTmpDir[i] = MM_AllocPhys() | 3;
620 INVLPG( &gaTmpTable[i*1024] );
621 for( j = 0; j < 1024; j ++ )
623 // Is the page allocated? If not, skip
624 if( !(gaPageTable[i*1024+j] & 1) ) {
625 gaTmpTable[i*1024+j] = 0;
629 // We don't care about other kernel stacks
630 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
631 gaTmpTable[i*1024+j] = 0;
636 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
638 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
640 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
641 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
642 MM_FreeTemp( (Uint)tmp );
646 ret = *gpTmpCR3 & ~0xFFF;
647 Mutex_Release( &glTempFractal );
654 * \fn tVAddr MM_NewKStack(void)
655 * \brief Create a new kernel stack
657 tVAddr MM_NewKStack(void)
661 for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
663 // Check if space is free
664 if(MM_GetPhysAddr(base) != 0) continue;
666 //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
667 for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
669 if( MM_Allocate(base+i) == 0 )
671 // On error, print a warning and return error
672 Warning("MM_NewKStack - Out of memory");
674 //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
675 // MM_Deallocate(base+i);
680 Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
681 return base+KERNEL_STACK_SIZE;
684 Warning("MM_NewKStack - No address space left");
689 * \fn tVAddr MM_NewWorkerStack()
690 * \brief Creates a new worker stack
692 tVAddr MM_NewWorkerStack()
699 tPAddr pages[WORKER_STACK_SIZE>>12];
701 // Get the old ESP and EBP
702 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
703 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
705 // TODO: Thread safety
706 // Find a free worker stack address
707 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
710 if( gWorkerStacks[base/32] == -1 ) {
711 base += 31; base &= ~31;
712 base --; // Counteracted by the base++
716 if( gWorkerStacks[base/32] & (1 << base) ) {
721 if(base >= NUM_WORKER_STACKS) {
722 Warning("Uh-oh! Out of worker stacks");
727 gWorkerStacks[base/32] |= (1 << base);
728 // Make life easier for later calls
729 giLastUsedWorker = base;
731 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
732 //Log(" MM_NewWorkerStack: base = 0x%x", base);
734 // Acquire the lock for the temp fractal mappings
735 Mutex_Acquire(&glTempFractal);
737 // Set the temp fractals to TID0's address space
738 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
739 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
743 // Check if the directory is mapped (we are assuming that the stacks
744 // will fit neatly in a directory)
745 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
746 if(gaTmpDir[ base >> 22 ] == 0) {
747 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
748 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
752 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
753 //for( addr = WORKER_STACK_SIZE; addr; addr -= 0x1000 )
755 pages[ addr >> 12 ] = MM_AllocPhys();
756 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
759 // Release the temp mapping lock
760 Mutex_Release(&glTempFractal);
762 // Copy the old stack
763 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
764 esp = oldstack - esp; // ESP as an offset in the stack
766 // Make `base` be the top of the stack
767 base += WORKER_STACK_SIZE;
769 i = (WORKER_STACK_SIZE>>12) - 1;
770 // Copy the contents of the old stack to the new one, altering the addresses
771 // `addr` is refering to bytes from the stack base (mem downwards)
772 for(addr = 0; addr < esp; addr += 0x1000)
774 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
775 tmpPage = (void*)MM_MapTemp( pages[i] );
777 for(j = 0; j < 1024; j++)
779 // Possible Stack address?
780 if(oldstack-esp < stack[j] && stack[j] < oldstack)
781 tmpPage[j] = base - (oldstack - stack[j]);
782 else // Seems not, best leave it alone
783 tmpPage[j] = stack[j];
785 MM_FreeTemp((tVAddr)tmpPage);
789 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
794 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
795 * \brief Sets the flags on a page
797 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
800 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
801 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
803 ent = &gaPageTable[VAddr >> 12];
806 if( Mask & MM_PFLAG_RO )
808 if( Flags & MM_PFLAG_RO ) {
812 gaPageDir[VAddr >> 22] |= PF_WRITE;
818 if( Mask & MM_PFLAG_KERNEL )
820 if( Flags & MM_PFLAG_KERNEL ) {
824 gaPageDir[VAddr >> 22] |= PF_USER;
830 if( Mask & MM_PFLAG_COW )
832 if( Flags & MM_PFLAG_COW ) {
842 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
843 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
847 * \brief Get the flags on a page
849 Uint MM_GetFlags(tVAddr VAddr)
855 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
856 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
858 ent = &gaPageTable[VAddr >> 12];
861 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
863 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
865 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
871 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
872 * \brief Duplicates a virtual page to a physical one
874 tPAddr MM_DuplicatePage(tVAddr VAddr)
880 //ENTER("xVAddr", VAddr);
883 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
884 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
890 ret = MM_AllocPhys();
892 // Write-lock the page (to keep data constistent), saving its R/W state
893 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
894 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
898 temp = MM_MapTemp(ret);
899 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
902 // Restore Writeable status
903 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
911 * \fn Uint MM_MapTemp(tPAddr PAddr)
912 * \brief Create a temporary memory mapping
913 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
915 tVAddr MM_MapTemp(tPAddr PAddr)
919 //ENTER("XPAddr", PAddr);
923 //LOG("glTempMappings = %i", glTempMappings);
927 Mutex_Acquire( &glTempMappings );
929 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
931 // Check if page used
932 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
934 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
935 INVLPG( TEMP_MAP_ADDR + (i << 12) );
936 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
937 Mutex_Release( &glTempMappings );
938 return TEMP_MAP_ADDR + (i << 12);
940 Mutex_Release( &glTempMappings );
941 Threads_Yield(); // TODO: Use a sleep queue here instead
946 * \fn void MM_FreeTemp(tVAddr PAddr)
947 * \brief Free's a temp mapping
949 void MM_FreeTemp(tVAddr VAddr)
952 //ENTER("xVAddr", VAddr);
954 if(i >= (TEMP_MAP_ADDR >> 12))
955 gaPageTable[ i ] = 0;
961 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
962 * \brief Allocates a contigous number of pages
964 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
971 for( i = 0; i < NUM_HW_PAGES; i ++ )
973 // Check if addr used
974 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
977 // Check possible region
978 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
980 // If there is an allocated page in the region we are testing, break
981 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
987 for( j = 0; j < Number; j++ ) {
988 MM_RefPhys( PAddr + (j<<12) );
989 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
991 return HW_MAP_ADDR + (i<<12);
994 // If we don't find any, return NULL
999 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1000 * \brief Allocates DMA physical memory
1001 * \param Pages Number of pages required
1002 * \param MaxBits Maximum number of bits the physical address can have
1003 * \param PhysAddr Pointer to the location to place the physical address allocated
1004 * \return Virtual address allocate
1006 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1008 tPAddr maxCheck = (1 << MaxBits);
1012 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1015 if(MaxBits < 12 || !PhysAddr) {
1021 if(MaxBits >= PHYS_BITS) maxCheck = -1;
1024 if(Pages == 1 && MaxBits >= PHYS_BITS)
1026 phys = MM_AllocPhys();
1028 ret = MM_MapHWPages(phys, 1);
1039 phys = MM_AllocPhysRange(Pages, MaxBits);
1040 // - Was it allocated?
1046 // Allocated successfully, now map
1047 ret = MM_MapHWPages(phys, Pages);
1049 // If it didn't map, free then return 0
1050 for(;Pages--;phys+=0x1000)
1062 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1063 * \brief Unmap a hardware page
1065 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1069 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1072 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1076 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1078 for( j = 0; j < Number; j++ )
1080 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1081 gaPageTable[ i + j ] = 0;
1084 Mutex_Release( &glTempMappings );
1088 EXPORT(MM_GetPhysAddr);
1091 EXPORT(MM_MapHWPages);
1092 EXPORT(MM_AllocDMA);
1093 EXPORT(MM_UnmapHWPages);