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 tVAddr MM_ClearUser(void);
82 tPAddr MM_DuplicatePage(tVAddr VAddr);
85 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
86 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
87 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
88 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
89 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
90 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
92 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
93 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
94 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
95 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
96 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
97 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
99 tMutex glTempMappings;
100 tMutex glTempFractal;
101 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
102 int giLastUsedWorker = 0;
106 * \fn void MM_PreinitVirtual(void)
107 * \brief Maps the fractal mappings
109 void MM_PreinitVirtual(void)
112 gaInitPageDir[ ((PAGE_TABLE_ADDR >> TAB)-3*512+3)*2 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
114 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
116 INVLPG( PAGE_TABLE_ADDR );
120 * \fn void MM_InstallVirtual(void)
121 * \brief Sets up the constant page mappings
123 void MM_InstallVirtual(void)
128 // --- Pre-Allocate kernel tables
129 for( i = KERNEL_BASE >> TAB; i < 1024*4; i ++ )
131 if( gaPAE_PageDir[ i ] ) continue;
133 // Skip stack tables, they are process unique
134 if( i > KERNEL_STACKS >> TAB && i < KERNEL_STACKS_END >> TAB) {
135 gaPAE_PageDir[ i ] = 0;
139 gaPAE_PageDir[ i ] = MM_AllocPhys() | 3;
140 INVLPG( &gaPAE_PageTable[i*512] );
141 memset( &gaPAE_PageTable[i*512], 0, 0x1000 );
144 // --- Pre-Allocate kernel tables
145 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
147 if( gaPageDir[ i ] ) continue;
148 // Skip stack tables, they are process unique
149 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
154 gaPageDir[ i ] = MM_AllocPhys() | 3;
155 INVLPG( &gaPageTable[i*1024] );
156 memset( &gaPageTable[i*1024], 0, 0x1000 );
160 // Unset kernel on the User Text pages
161 for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
162 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
167 * \brief Cleans up the SMP required mappings
169 void MM_FinishVirtualInit(void)
174 gaInitPageDir[ 0 ] = 0;
179 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
180 * \brief Called on a page fault
182 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
184 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
186 // -- Check for COW --
187 if( gaPageDir [Addr>>22] & PF_PRESENT
188 && gaPageTable[Addr>>12] & PF_PRESENT
189 && gaPageTable[Addr>>12] & PF_COW )
192 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
194 gaPageTable[Addr>>12] &= ~PF_COW;
195 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
199 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
200 paddr = MM_DuplicatePage( Addr );
201 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
202 gaPageTable[Addr>>12] &= PF_USER;
203 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
206 INVLPG( Addr & ~0xFFF );
211 // If it was a user, tell the thread handler
213 Warning("%s %s %s memory%s",
214 (ErrorCode&4?"User":"Kernel"),
215 (ErrorCode&2?"write to":"read from"),
216 (ErrorCode&1?"bad/locked":"non-present"),
217 (ErrorCode&16?" (Instruction Fetch)":"")
219 Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
220 __asm__ __volatile__ ("sti"); // Restart IRQs
222 Error_Backtrace(Regs->eip, Regs->ebp);
224 Threads_SegFault(Addr);
230 // -- Check Error Code --
232 Warning("Reserved Bits Trashed!");
235 Warning("%s %s %s memory%s",
236 (ErrorCode&4?"User":"Kernel"),
237 (ErrorCode&2?"write to":"read from"),
238 (ErrorCode&1?"bad/locked":"non-present"),
239 (ErrorCode&16?" (Instruction Fetch)":"")
243 Log("Code at %p accessed %p", Regs->eip, Addr);
244 // Print Stack Backtrace
245 Error_Backtrace(Regs->eip, Regs->ebp);
247 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
248 if( gaPageDir[Addr>>22] & PF_PRESENT )
249 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
251 //MM_DumpTables(0, -1);
254 Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
255 Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
256 //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
257 Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
258 Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
261 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
262 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
263 Log("DR0 %08x DR1 %08x", dr0, dr1);
266 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
270 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
271 * \brief Dumps the layout of the page tables
273 void MM_DumpTables(tVAddr Start, tVAddr End)
275 tVAddr rangeStart = 0;
279 const tPAddr MASK = ~0xF98;
281 Start >>= 12; End >>= 12;
284 Log("Directory Entries:");
285 for(page = Start >> 10;
286 page < (End >> 10)+1;
291 Log(" 0x%08x-0x%08x :: 0x%08x",
292 page<<22, ((page+1)<<22)-1,
293 gaPageDir[page]&~0xFFF
299 Log("Table Entries:");
300 for(page = Start, curPos = Start<<12;
302 curPos += 0x1000, page++)
304 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
305 || !(gaPageTable[page] & PF_PRESENT)
306 || (gaPageTable[page] & MASK) != expected)
309 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
310 rangeStart, curPos - 1,
311 gaPageTable[rangeStart>>12] & ~0xFFF,
312 (expected & ~0xFFF) - 1,
313 (expected & PF_NOPAGE ? "P" : "-"),
314 (expected & PF_COW ? "C" : "-"),
315 (expected & PF_USER ? "U" : "-"),
316 (expected & PF_WRITE ? "W" : "-")
320 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
321 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
323 expected = (gaPageTable[page] & MASK);
326 if(expected) expected += 0x1000;
330 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
331 rangeStart, curPos - 1,
332 gaPageTable[rangeStart>>12] & ~0xFFF,
333 (expected & ~0xFFF) - 1,
334 (expected & PF_NOPAGE ? "p" : "-"),
335 (expected & PF_COW ? "C" : "-"),
336 (expected & PF_USER ? "U" : "-"),
337 (expected & PF_WRITE ? "W" : "-")
344 * \fn tPAddr MM_Allocate(tVAddr VAddr)
346 tPAddr MM_Allocate(tVAddr VAddr)
349 //ENTER("xVAddr", VAddr);
350 //__asm__ __volatile__ ("xchg %bx,%bx");
351 // Check if the directory is mapped
352 if( gaPageDir[ VAddr >> 22 ] == 0 )
354 // Allocate directory
355 paddr = MM_AllocPhys();
356 //LOG("paddr = 0x%llx (new table)", paddr);
358 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
363 gaPageDir[ VAddr >> 22 ] = paddr | 3;
365 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
367 INVLPG( &gaPageDir[ VAddr >> 22 ] );
368 //LOG("Clearing new table");
369 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
371 // Check if the page is already allocated
372 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
373 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
374 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
375 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
379 paddr = MM_AllocPhys();
380 //LOG("paddr = 0x%llx", paddr);
382 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
383 VAddr, __builtin_return_address(0));
388 gaPageTable[ VAddr >> 12 ] = paddr | 3;
390 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
391 // Invalidate Cache for address
392 INVLPG( VAddr & ~0xFFF );
399 * \fn void MM_Deallocate(tVAddr VAddr)
401 void MM_Deallocate(tVAddr VAddr)
403 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
404 Warning("MM_Deallocate - Directory not mapped");
408 if(gaPageTable[ VAddr >> 12 ] == 0) {
409 Warning("MM_Deallocate - Page is not allocated");
414 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
416 gaPageTable[ VAddr >> 12 ] = 0;
420 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
421 * \brief Checks if the passed address is accesable
423 tPAddr MM_GetPhysAddr(tVAddr Addr)
425 if( !(gaPageDir[Addr >> 22] & 1) )
427 if( !(gaPageTable[Addr >> 12] & 1) )
429 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
433 * \fn void MM_SetCR3(Uint CR3)
434 * \brief Sets the current process space
436 void MM_SetCR3(Uint CR3)
438 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
442 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
443 * \brief Map a physical page to a virtual one
445 int MM_Map(tVAddr VAddr, tPAddr PAddr)
447 //ENTER("xVAddr xPAddr", VAddr, PAddr);
449 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
450 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
456 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
458 // Check if the directory is mapped
459 if( gaPageDir[ VAddr >> 22 ] == 0 )
461 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
464 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
466 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
467 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
469 // Check if the page is already allocated
470 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
471 Warning("MM_Map - Allocating to used address");
477 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
479 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
481 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
482 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
487 //LOG("INVLPG( 0x%x )", VAddr);
495 * \fn tVAddr MM_ClearUser()
496 * \brief Clear user's address space
498 tVAddr MM_ClearUser(void)
503 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
505 // Check if directory is not allocated
506 if( !(gaPageDir[i] & PF_PRESENT) ) {
512 for( j = 0; j < 1024; j ++ )
514 if( gaPageTable[i*1024+j] & 1 )
515 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
516 gaPageTable[i*1024+j] = 0;
519 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
521 INVLPG( &gaPageTable[i*1024] );
529 * \fn tPAddr MM_Clone(void)
530 * \brief Clone the current address space
532 tPAddr MM_Clone(void)
537 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
540 Mutex_Acquire( &glTempFractal );
542 // Create Directory Table
543 *gpTmpCR3 = MM_AllocPhys() | 3;
545 //LOG("Allocated Directory (%x)", *gpTmpCR3);
546 memsetd( gaTmpDir, 0, 1024 );
548 if( Threads_GetPID() != 0 )
551 for( i = 0; i < 768; i ++)
553 // Check if table is allocated
554 if( !(gaPageDir[i] & PF_PRESENT) ) {
560 // Allocate new table
561 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
562 INVLPG( &gaTmpTable[page] );
564 for( j = 0; j < 1024; j ++, page++ )
566 if( !(gaPageTable[page] & PF_PRESENT) ) {
567 gaTmpTable[page] = 0;
572 MM_RefPhys( gaPageTable[page] & ~0xFFF );
574 if(gaPageTable[page] & PF_WRITE) {
575 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
576 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
577 INVLPG( page << 12 );
580 gaTmpTable[page] = gaPageTable[page];
585 // Map in kernel tables (and make fractal mapping)
586 for( i = 768; i < 1024; i ++ )
589 if( i == (PAGE_TABLE_ADDR >> 22) ) {
590 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
594 if( gaPageDir[i] == 0 ) {
599 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
600 MM_RefPhys( gaPageDir[i] & ~0xFFF );
601 gaTmpDir[i] = gaPageDir[i];
604 // Allocate kernel stack
605 for(i = KERNEL_STACKS >> 22;
606 i < KERNEL_STACKS_END >> 22;
609 // Check if directory is allocated
610 if( (gaPageDir[i] & 1) == 0 ) {
615 // We don't care about other kernel stacks, just the current one
616 if( i != kStackBase >> 22 ) {
617 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
623 gaTmpDir[i] = MM_AllocPhys() | 3;
624 INVLPG( &gaTmpTable[i*1024] );
625 for( j = 0; j < 1024; j ++ )
627 // Is the page allocated? If not, skip
628 if( !(gaPageTable[i*1024+j] & 1) ) {
629 gaTmpTable[i*1024+j] = 0;
633 // We don't care about other kernel stacks
634 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
635 gaTmpTable[i*1024+j] = 0;
640 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
642 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
644 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
645 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
646 MM_FreeTemp( (Uint)tmp );
650 ret = *gpTmpCR3 & ~0xFFF;
651 Mutex_Release( &glTempFractal );
658 * \fn tVAddr MM_NewKStack(void)
659 * \brief Create a new kernel stack
661 tVAddr MM_NewKStack(void)
665 for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
667 // Check if space is free
668 if(MM_GetPhysAddr(base) != 0) continue;
670 //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
671 for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
673 if( MM_Allocate(base+i) == 0 )
675 // On error, print a warning and return error
676 Warning("MM_NewKStack - Out of memory");
678 //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
679 // MM_Deallocate(base+i);
684 Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
685 return base+KERNEL_STACK_SIZE;
688 Warning("MM_NewKStack - No address space left");
693 * \fn tVAddr MM_NewWorkerStack()
694 * \brief Creates a new worker stack
696 tVAddr MM_NewWorkerStack()
703 tPAddr pages[WORKER_STACK_SIZE>>12];
705 // Get the old ESP and EBP
706 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
707 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
709 // TODO: Thread safety
710 // Find a free worker stack address
711 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
714 if( gWorkerStacks[base/32] == -1 ) {
715 base += 31; base &= ~31;
716 base --; // Counteracted by the base++
720 if( gWorkerStacks[base/32] & (1 << base) ) {
725 if(base >= NUM_WORKER_STACKS) {
726 Warning("Uh-oh! Out of worker stacks");
731 gWorkerStacks[base/32] |= (1 << base);
732 // Make life easier for later calls
733 giLastUsedWorker = base;
735 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
736 //Log(" MM_NewWorkerStack: base = 0x%x", base);
738 // Acquire the lock for the temp fractal mappings
739 Mutex_Acquire(&glTempFractal);
741 // Set the temp fractals to TID0's address space
742 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
743 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
747 // Check if the directory is mapped (we are assuming that the stacks
748 // will fit neatly in a directory)
749 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
750 if(gaTmpDir[ base >> 22 ] == 0) {
751 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
752 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
756 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
757 //for( addr = WORKER_STACK_SIZE; addr; addr -= 0x1000 )
759 pages[ addr >> 12 ] = MM_AllocPhys();
760 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
763 // Release the temp mapping lock
764 Mutex_Release(&glTempFractal);
766 // Copy the old stack
767 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
768 esp = oldstack - esp; // ESP as an offset in the stack
770 // Make `base` be the top of the stack
771 base += WORKER_STACK_SIZE;
773 i = (WORKER_STACK_SIZE>>12) - 1;
774 // Copy the contents of the old stack to the new one, altering the addresses
775 // `addr` is refering to bytes from the stack base (mem downwards)
776 for(addr = 0; addr < esp; addr += 0x1000)
778 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
779 tmpPage = (void*)MM_MapTemp( pages[i] );
781 for(j = 0; j < 1024; j++)
783 // Possible Stack address?
784 if(oldstack-esp < stack[j] && stack[j] < oldstack)
785 tmpPage[j] = base - (oldstack - stack[j]);
786 else // Seems not, best leave it alone
787 tmpPage[j] = stack[j];
789 MM_FreeTemp((tVAddr)tmpPage);
793 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
798 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
799 * \brief Sets the flags on a page
801 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
804 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
805 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
807 ent = &gaPageTable[VAddr >> 12];
810 if( Mask & MM_PFLAG_RO )
812 if( Flags & MM_PFLAG_RO ) {
816 gaPageDir[VAddr >> 22] |= PF_WRITE;
822 if( Mask & MM_PFLAG_KERNEL )
824 if( Flags & MM_PFLAG_KERNEL ) {
828 gaPageDir[VAddr >> 22] |= PF_USER;
834 if( Mask & MM_PFLAG_COW )
836 if( Flags & MM_PFLAG_COW ) {
846 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
847 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
851 * \brief Get the flags on a page
853 Uint MM_GetFlags(tVAddr VAddr)
859 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
860 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
862 ent = &gaPageTable[VAddr >> 12];
865 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
867 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
869 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
875 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
876 * \brief Duplicates a virtual page to a physical one
878 tPAddr MM_DuplicatePage(tVAddr VAddr)
884 //ENTER("xVAddr", VAddr);
887 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
888 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
894 ret = MM_AllocPhys();
896 // Write-lock the page (to keep data constistent), saving its R/W state
897 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
898 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
902 temp = MM_MapTemp(ret);
903 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
906 // Restore Writeable status
907 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
915 * \fn Uint MM_MapTemp(tPAddr PAddr)
916 * \brief Create a temporary memory mapping
917 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
919 tVAddr MM_MapTemp(tPAddr PAddr)
923 //ENTER("XPAddr", PAddr);
927 //LOG("glTempMappings = %i", glTempMappings);
931 Mutex_Acquire( &glTempMappings );
933 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
935 // Check if page used
936 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
938 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
939 INVLPG( TEMP_MAP_ADDR + (i << 12) );
940 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
941 Mutex_Release( &glTempMappings );
942 return TEMP_MAP_ADDR + (i << 12);
944 Mutex_Release( &glTempMappings );
945 Threads_Yield(); // TODO: Use a sleep queue here instead
950 * \fn void MM_FreeTemp(tVAddr PAddr)
951 * \brief Free's a temp mapping
953 void MM_FreeTemp(tVAddr VAddr)
956 //ENTER("xVAddr", VAddr);
958 if(i >= (TEMP_MAP_ADDR >> 12))
959 gaPageTable[ i ] = 0;
965 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
966 * \brief Allocates a contigous number of pages
968 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
975 for( i = 0; i < NUM_HW_PAGES; i ++ )
977 // Check if addr used
978 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
981 // Check possible region
982 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
984 // If there is an allocated page in the region we are testing, break
985 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
991 for( j = 0; j < Number; j++ ) {
992 MM_RefPhys( PAddr + (j<<12) );
993 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
995 return HW_MAP_ADDR + (i<<12);
998 // If we don't find any, return NULL
1003 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1004 * \brief Allocates DMA physical memory
1005 * \param Pages Number of pages required
1006 * \param MaxBits Maximum number of bits the physical address can have
1007 * \param PhysAddr Pointer to the location to place the physical address allocated
1008 * \return Virtual address allocate
1010 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1012 tPAddr maxCheck = (1 << MaxBits);
1016 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1019 if(MaxBits < 12 || !PhysAddr) {
1025 if(MaxBits >= PHYS_BITS) maxCheck = -1;
1028 if(Pages == 1 && MaxBits >= PHYS_BITS)
1030 phys = MM_AllocPhys();
1032 ret = MM_MapHWPages(phys, 1);
1043 phys = MM_AllocPhysRange(Pages, MaxBits);
1044 // - Was it allocated?
1050 // Allocated successfully, now map
1051 ret = MM_MapHWPages(phys, Pages);
1053 // If it didn't map, free then return 0
1054 for(;Pages--;phys+=0x1000)
1066 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1067 * \brief Unmap a hardware page
1069 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1073 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1076 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1080 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1082 for( j = 0; j < Number; j++ )
1084 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1085 gaPageTable[ i + j ] = 0;
1088 Mutex_Release( &glTempMappings );
1092 EXPORT(MM_GetPhysAddr);
1095 EXPORT(MM_MapHWPages);
1096 EXPORT(MM_AllocDMA);
1097 EXPORT(MM_UnmapHWPages);