2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
20 #include <semaphore.h>
24 #define WORKER_STACKS 0x00100000 // Thread0 Only!
25 #define WORKER_STACK_SIZE MM_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 #define GET_TEMP_MAPPING(cr3) do { \
61 __AtomicTestSetLoop( (Uint *)gpTmpCR3, cr3 | 3 ); \
63 #define REL_TEMP_MAPPING() do { \
68 typedef Uint32 tTabEnt;
71 extern char _UsertextEnd[], _UsertextBase[];
72 extern Uint32 gaInitPageDir[1024];
73 extern Uint32 gaInitPageTable[1024];
74 extern void Threads_SegFault(tVAddr Addr);
75 extern void Error_Backtrace(Uint eip, Uint ebp);
78 void MM_PreinitVirtual(void);
79 void MM_InstallVirtual(void);
80 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
81 //void MM_DumpTables(tVAddr Start, tVAddr End);
82 //void MM_ClearUser(void);
83 tPAddr MM_DuplicatePage(tVAddr VAddr);
86 #define gaPageTable ((tTabEnt*)PAGE_TABLE_ADDR)
87 #define gaPageDir ((tTabEnt*)PAGE_DIR_ADDR)
88 #define gaTmpTable ((tTabEnt*)TMP_TABLE_ADDR)
89 #define gaTmpDir ((tTabEnt*)TMP_DIR_ADDR)
90 #define gpPageCR3 ((tTabEnt*)PAGE_CR3_ADDR)
91 #define gpTmpCR3 ((tTabEnt*)TMP_CR3_ADDR)
93 #define gaPAE_PageTable ((tTabEnt*)PAE_PAGE_TABLE_ADDR)
94 #define gaPAE_PageDir ((tTabEnt*)PAE_PAGE_DIR_ADDR)
95 #define gaPAE_MainPDPT ((tTabEnt*)PAE_PAGE_PDPT_ADDR)
96 #define gaPAE_TmpTable ((tTabEnt*)PAE_TMP_DIR_ADDR)
97 #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR)
98 #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR)
100 tMutex glTempMappings;
101 tSemaphore gTempMappingsSem;
102 tMutex glTempFractal;
103 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
104 int giLastUsedWorker = 0;
111 } *gaMappedRegions; // sizeof = 24 bytes
115 * \fn void MM_PreinitVirtual(void)
116 * \brief Maps the fractal mappings
118 void MM_PreinitVirtual(void)
120 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
121 INVLPG( PAGE_TABLE_ADDR );
123 Semaphore_Init(&gTempMappingsSem, NUM_TEMP_PAGES, NUM_TEMP_PAGES, "MMVirt", "Temp Mappings");
127 * \fn void MM_InstallVirtual(void)
128 * \brief Sets up the constant page mappings
130 void MM_InstallVirtual(void)
134 // --- Pre-Allocate kernel tables
135 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
137 if( gaPageDir[ i ] ) continue;
138 // Skip stack tables, they are process unique
139 if( i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) {
144 gaPageDir[ i ] = MM_AllocPhys() | 3;
145 INVLPG( &gaPageTable[i*1024] );
146 memset( &gaPageTable[i*1024], 0, 0x1000 );
149 // Unset kernel on the User Text pages
150 for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
151 MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
158 * \brief Cleans up the SMP required mappings
160 void MM_FinishVirtualInit(void)
162 gaInitPageDir[ 0 ] = 0;
166 * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
167 * \brief Called on a page fault
169 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
171 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
173 // -- Check for COW --
174 if( gaPageDir [Addr>>22] & PF_PRESENT && gaPageTable[Addr>>12] & PF_PRESENT
175 && gaPageTable[Addr>>12] & PF_COW )
178 __asm__ __volatile__ ("sti");
179 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
181 gaPageTable[Addr>>12] &= ~PF_COW;
182 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
186 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
187 paddr = MM_DuplicatePage( Addr );
188 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
189 gaPageTable[Addr>>12] &= PF_USER;
190 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
193 // Log_Debug("MMVirt", "COW for %p (%P)", Addr, gaPageTable[Addr>>12]);
195 INVLPG( Addr & ~0xFFF );
199 // Disable instruction tracing
200 __ASM__("pushf; andw $0xFEFF, 0(%esp); popf");
201 Proc_GetCurThread()->bInstrTrace = 0;
203 // If it was a user, tell the thread handler
205 __asm__ __volatile__ ("sti");
206 Log_Warning("MMVirt", "User %s %s memory%s",
207 (ErrorCode&2?"write to":"read from"),
208 (ErrorCode&1?"bad/locked":"non-present"),
209 (ErrorCode&16?" (Instruction Fetch)":"")
211 Log_Warning("MMVirt", "Instruction %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
212 __ASM__("sti"); // Restart IRQs
214 Error_Backtrace(Regs->eip, Regs->ebp);
216 Threads_SegFault(Addr);
222 // -- Check Error Code --
224 Warning("Reserved Bits Trashed!");
227 Warning("Kernel %s %s memory%s",
228 (ErrorCode&2?"write to":"read from"),
229 (ErrorCode&1?"bad/locked":"non-present"),
230 (ErrorCode&16?" (Instruction Fetch)":"")
234 Log("CPU %i - Code at %p accessed %p", GetCPUNum(), Regs->eip, Addr);
235 // Print Stack Backtrace
236 Error_Backtrace(Regs->eip, Regs->ebp);
239 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
240 if( gaPageDir[Addr>>22] & PF_PRESENT )
241 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
243 //MM_DumpTables(0, -1);
246 Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
247 Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
248 //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
249 Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
250 Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
253 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
254 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
255 Log("DR0 %08x DR1 %08x", dr0, dr1);
258 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
262 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
263 * \brief Dumps the layout of the page tables
265 void MM_DumpTables(tVAddr Start, tVAddr End)
267 tVAddr rangeStart = 0;
269 void *expected_node = NULL, *tmpnode = NULL;
272 const tPAddr MASK = ~0xF78;
274 Start >>= 12; End >>= 12;
277 Log("Directory Entries:");
278 for(page = Start >> 10;
279 page < (End >> 10)+1;
284 Log(" 0x%08x-0x%08x :: 0x%08x",
285 page<<22, ((page+1)<<22)-1,
286 gaPageDir[page]&~0xFFF
292 Log("Table Entries:");
293 for(page = Start, curPos = Start<<12;
295 curPos += 0x1000, page++)
297 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
298 || !(gaPageTable[page] & PF_PRESENT)
299 || (gaPageTable[page] & MASK) != expected
300 || (tmpnode=NULL,MM_GetPageNode(expected, &tmpnode), tmpnode != expected_node))
303 tPAddr orig = gaPageTable[rangeStart>>12];
304 Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
308 (orig & PF_NOPAGE ? "P" : "-"),
309 (orig & PF_COW ? "C" : "-"),
310 (orig & PF_GLOBAL ? "G" : "-"),
311 (orig & PF_USER ? "U" : "-"),
312 (orig & PF_WRITE ? "W" : "-"),
317 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
318 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
320 expected = (gaPageTable[page] & MASK);
321 MM_GetPageNode(expected, &expected_node);
324 if(expected) expected += 0x1000;
328 tPAddr orig = gaPageTable[rangeStart>>12];
329 Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
333 (orig & PF_NOPAGE ? "p" : "-"),
334 (orig & PF_COW ? "C" : "-"),
335 (orig & PF_GLOBAL ? "G" : "-"),
336 (orig & PF_USER ? "U" : "-"),
337 (orig & PF_WRITE ? "W" : "-"),
345 * \fn tPAddr MM_Allocate(tVAddr VAddr)
347 tPAddr MM_Allocate(tVAddr VAddr)
350 //ENTER("xVAddr", VAddr);
351 //__ASM__("xchg %bx,%bx");
352 // Check if the directory is mapped
353 if( gaPageDir[ VAddr >> 22 ] == 0 )
355 // Allocate directory
356 paddr = MM_AllocPhys();
358 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
362 // Map and mark as user (if needed)
363 gaPageDir[ VAddr >> 22 ] = paddr | 3;
364 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
366 INVLPG( &gaPageDir[ VAddr >> 22 ] );
367 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
369 // Check if the page is already allocated
370 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
371 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
372 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
373 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
377 paddr = MM_AllocPhys();
378 //LOG("paddr = 0x%llx", paddr);
380 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
381 VAddr, __builtin_return_address(0));
386 gaPageTable[ VAddr >> 12 ] = paddr | 3;
388 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
389 // Invalidate Cache for address
390 INVLPG( VAddr & ~0xFFF );
397 * \fn void MM_Deallocate(tVAddr VAddr)
399 void MM_Deallocate(tVAddr VAddr)
401 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
402 Warning("MM_Deallocate - Directory not mapped");
406 if(gaPageTable[ VAddr >> 12 ] == 0) {
407 Warning("MM_Deallocate - Page is not allocated");
412 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
414 gaPageTable[ VAddr >> 12 ] = 0;
418 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
419 * \brief Checks if the passed address is accesable
421 tPAddr MM_GetPhysAddr(volatile const void *Addr)
423 tVAddr addr = (tVAddr)Addr;
424 if( !(gaPageDir[addr >> 22] & 1) )
426 if( !(gaPageTable[addr >> 12] & 1) )
428 return (gaPageTable[addr >> 12] & ~0xFFF) | (addr & 0xFFF);
432 * \fn void MM_SetCR3(Uint CR3)
433 * \brief Sets the current process space
435 void MM_SetCR3(Uint CR3)
437 __ASM__("mov %0, %%cr3"::"r"(CR3));
441 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
442 * \brief Map a physical page to a virtual one
444 int MM_Map(tVAddr VAddr, tPAddr PAddr)
446 //ENTER("xVAddr xPAddr", VAddr, PAddr);
448 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
449 Log_Warning("MM_Virt", "MM_Map - Physical or Virtual Addresses are not aligned (0x%P and %p)",
456 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
458 // Check if the directory is mapped
459 if( gaPageDir[ VAddr >> 22 ] == 0 )
461 tPAddr tmp = MM_AllocPhys();
464 gaPageDir[ VAddr >> 22 ] = tmp | 3;
467 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
469 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
470 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
472 // Check if the page is already allocated
473 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
474 Warning("MM_Map - Allocating to used address");
480 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
482 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
484 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
485 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
490 //LOG("INVLPG( 0x%x )", VAddr);
498 * \brief Clear user's address space
500 void MM_ClearUser(void)
504 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
506 // Check if directory is not allocated
507 if( !(gaPageDir[i] & PF_PRESENT) ) {
513 for( j = 0; j < 1024; j ++ )
515 if( gaPageTable[i*1024+j] & 1 )
516 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
517 gaPageTable[i*1024+j] = 0;
520 // Deallocate directory
521 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
523 INVLPG( &gaPageTable[i*1024] );
529 * \brief Deallocate an address space
531 void MM_ClearSpace(Uint32 CR3)
535 if(CR3 == (*gpPageCR3 & ~0xFFF)) {
536 Log_Error("MMVirt", "Can't clear current address space");
540 if( MM_GetRefCount(CR3) > 1 ) {
542 Log_Log("MMVirt", "CR3 %P is still referenced, not cleaning (but dereferenced)", CR3);
546 Log_Debug("MMVirt", "Clearing out address space 0x%x from 0x%x", CR3, *gpPageCR3);
548 GET_TEMP_MAPPING(CR3);
551 for( i = 0; i < 1024; i ++ )
553 Uint32 *table = &gaTmpTable[i*1024];
554 if( !(gaTmpDir[i] & PF_PRESENT) )
559 if( i < 768 || (i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) )
561 for( j = 0; j < 1024; j ++ )
563 if( !(table[j] & 1) )
565 MM_DerefPhys( table[j] & ~0xFFF );
569 if( i != (PAGE_TABLE_ADDR >> 22) )
571 MM_DerefPhys( gaTmpDir[i] & ~0xFFF );
582 * \fn tPAddr MM_Clone(void)
583 * \brief Clone the current address space
585 tPAddr MM_Clone(int bNoUserCopy)
590 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - MM_KERNEL_STACK_SIZE;
593 // Create Directory Table
594 ret = MM_AllocPhys();
600 GET_TEMP_MAPPING( ret );
602 memsetd( gaTmpDir, 0, 1024 );
604 if( Threads_GetPID() != 0 && !bNoUserCopy )
607 for( i = 0; i < 768; i ++)
609 // Check if table is allocated
610 if( !(gaPageDir[i] & PF_PRESENT) ) {
616 // Allocate new table
617 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
618 INVLPG( &gaTmpTable[page] );
620 for( j = 0; j < 1024; j ++, page++ )
622 if( !(gaPageTable[page] & PF_PRESENT) ) {
623 gaTmpTable[page] = 0;
628 MM_RefPhys( gaPageTable[page] & ~0xFFF );
630 if(gaPageTable[page] & PF_WRITE) {
631 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
632 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
633 INVLPG( page << 12 );
636 gaTmpTable[page] = gaPageTable[page];
641 // Map in kernel tables (and make fractal mapping)
642 for( i = 768; i < 1024; i ++ )
645 if( i == (PAGE_TABLE_ADDR >> 22) ) {
646 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
649 if( i == (TMP_TABLE_ADDR >> 22) ) {
650 gaTmpDir[ TMP_TABLE_ADDR >> 22 ] = 0;
654 if( gaPageDir[i] == 0 ) {
659 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
660 MM_RefPhys( gaPageDir[i] & ~0xFFF );
661 gaTmpDir[i] = gaPageDir[i];
664 // Allocate kernel stack
665 for(i = MM_KERNEL_STACKS >> 22; i < MM_KERNEL_STACKS_END >> 22; i ++ )
667 // Check if directory is allocated
668 if( (gaPageDir[i] & 1) == 0 ) {
673 // We don't care about other kernel stacks, just the current one
674 if( i != kStackBase >> 22 ) {
675 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
681 gaTmpDir[i] = MM_AllocPhys() | 3;
682 INVLPG( &gaTmpTable[i*1024] );
683 for( j = 0; j < 1024; j ++ )
685 // Is the page allocated? If not, skip
686 if( !(gaPageTable[i*1024+j] & 1) ) {
687 gaTmpTable[i*1024+j] = 0;
691 // We don't care about other kernel stacks
692 if( ((i*1024+j)*4096 & ~(MM_KERNEL_STACK_SIZE-1)) != kStackBase ) {
693 gaTmpTable[i*1024+j] = 0;
698 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
700 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
702 tmp = MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
703 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
715 * \fn tVAddr MM_NewKStack(void)
716 * \brief Create a new kernel stack
718 tVAddr MM_NewKStack(void)
722 for(base = MM_KERNEL_STACKS; base < MM_KERNEL_STACKS_END; base += MM_KERNEL_STACK_SIZE)
724 // Check if space is free
725 if(MM_GetPhysAddr( (void*) base) != 0)
728 //for(i = MM_KERNEL_STACK_SIZE; i -= 0x1000 ; )
729 for(i = 0; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
731 if( MM_Allocate(base+i) == 0 )
733 // On error, print a warning and return error
734 Warning("MM_NewKStack - Out of memory");
736 //for( i += 0x1000 ; i < MM_KERNEL_STACK_SIZE; i += 0x1000 )
737 // MM_Deallocate(base+i);
742 // Log("MM_NewKStack - Allocated %p", base + MM_KERNEL_STACK_SIZE);
743 return base+MM_KERNEL_STACK_SIZE;
746 Log_Warning("MMVirt", "MM_NewKStack - No address space left");
751 * \fn tVAddr MM_NewWorkerStack()
752 * \brief Creates a new worker stack
754 tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize)
760 // TODO: Thread safety
761 // Find a free worker stack address
762 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
765 if( gWorkerStacks[base/32] == -1 ) {
766 base += 31; base &= ~31;
767 base --; // Counteracted by the base++
771 if( gWorkerStacks[base/32] & (1 << base) ) {
776 if(base >= NUM_WORKER_STACKS) {
777 Warning("Uh-oh! Out of worker stacks");
782 gWorkerStacks[base/32] |= (1 << base);
783 // Make life easier for later calls
784 giLastUsedWorker = base;
786 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
787 //Log(" MM_NewWorkerStack: base = 0x%x", base);
789 // Set the temp fractals to TID0's address space
790 GET_TEMP_MAPPING( ((Uint)gaInitPageDir - KERNEL_BASE) );
793 // Check if the directory is mapped (we are assuming that the stacks
794 // will fit neatly in a directory)
795 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
796 if(gaTmpDir[ base >> 22 ] == 0) {
797 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
798 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
802 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
804 page = MM_AllocPhys();
805 gaTmpTable[ (base + addr) >> 12 ] = page | 3;
808 // Release temporary fractal
811 // NOTE: Max of 1 page
812 // `page` is the last allocated page from the previious for loop
813 tmpPage = (tVAddr)MM_MapTemp( page );
814 memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize);
815 MM_FreeTemp( (void*)tmpPage );
817 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
818 return base + WORKER_STACK_SIZE;
822 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
823 * \brief Sets the flags on a page
825 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
828 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
829 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
831 ent = &gaPageTable[VAddr >> 12];
834 if( Mask & MM_PFLAG_RO )
836 if( Flags & MM_PFLAG_RO ) {
840 gaPageDir[VAddr >> 22] |= PF_WRITE;
846 if( Mask & MM_PFLAG_KERNEL )
848 if( Flags & MM_PFLAG_KERNEL ) {
852 gaPageDir[VAddr >> 22] |= PF_USER;
858 if( Mask & MM_PFLAG_COW )
860 if( Flags & MM_PFLAG_COW ) {
870 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
871 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
875 * \brief Get the flags on a page
877 Uint MM_GetFlags(tVAddr VAddr)
883 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
884 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
886 ent = &gaPageTable[VAddr >> 12];
889 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
891 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
893 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
899 * \brief Check if the provided buffer is valid
900 * \return Boolean valid
902 int MM_IsValidBuffer(tVAddr Addr, size_t Size)
907 Size += Addr & (PAGE_SIZE-1);
908 Addr &= ~(PAGE_SIZE-1);
913 // Debug("Addr = %p, Size = 0x%x, dir = %i, tab = %i", Addr, Size, dir, tab);
915 if( !(gaPageDir[dir] & 1) ) return 0;
916 if( !(gaPageTable[tab] & 1) ) return 0;
918 bIsUser = !!(gaPageTable[tab] & PF_USER);
920 while( Size >= PAGE_SIZE )
922 if( (tab & 1023) == 0 )
925 if( !(gaPageDir[dir] & 1) ) return 0;
928 if( !(gaPageTable[tab] & 1) ) return 0;
929 if( bIsUser && !(gaPageTable[tab] & PF_USER) ) return 0;
938 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
939 * \brief Duplicates a virtual page to a physical one
941 tPAddr MM_DuplicatePage(tVAddr VAddr)
947 //ENTER("xVAddr", VAddr);
950 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
951 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
957 ret = MM_AllocPhys();
962 // Write-lock the page (to keep data constistent), saving its R/W state
963 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
964 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
968 temp = MM_MapTemp(ret);
969 memcpy( temp, (void*)VAddr, 0x1000 );
972 // Restore Writeable status
973 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
981 * \fn Uint MM_MapTemp(tPAddr PAddr)
982 * \brief Create a temporary memory mapping
983 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
985 void * MM_MapTemp(tPAddr PAddr)
987 //ENTER("XPAddr", PAddr);
991 //LOG("glTempMappings = %i", glTempMappings);
993 if( Semaphore_Wait(&gTempMappingsSem, 1) != 1 )
995 Mutex_Acquire( &glTempMappings );
996 for( int i = 0; i < NUM_TEMP_PAGES; i ++ )
998 // Check if page used
999 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
1001 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
1002 INVLPG( TEMP_MAP_ADDR + (i << 12) );
1003 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
1004 Mutex_Release( &glTempMappings );
1005 return (void*)( TEMP_MAP_ADDR + (i << 12) );
1007 Mutex_Release( &glTempMappings );
1008 Log_KernelPanic("MMVirt", "Semaphore suplied a mapping, but none are avaliable");
1013 * \fn void MM_FreeTemp(tVAddr PAddr)
1014 * \brief Free's a temp mapping
1016 void MM_FreeTemp(void *VAddr)
1018 int i = (tVAddr)VAddr >> 12;
1019 //ENTER("xVAddr", VAddr);
1021 if(i >= (TEMP_MAP_ADDR >> 12)) {
1022 gaPageTable[ i ] = 0;
1023 Semaphore_Signal(&gTempMappingsSem, 1);
1030 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
1031 * \brief Allocates a contigous number of pages
1033 void *MM_MapHWPages(tPAddr PAddr, Uint Number)
1039 if( PAddr < 1024*1024 && (1024*1024-PAddr) >= Number * PAGE_SIZE )
1041 return (void*)(KERNEL_BASE + PAddr);
1045 for( int i = 0; i < NUM_HW_PAGES; i ++ )
1047 // Check if addr used
1048 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
1051 // Check possible region
1052 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
1054 // If there is an allocated page in the region we are testing, break
1055 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
1061 for( j = 0; j < Number; j++ ) {
1062 MM_RefPhys( PAddr + (j<<12) );
1063 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
1065 return (void*)(HW_MAP_ADDR + (i<<12));
1068 // If we don't find any, return NULL
1073 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1074 * \brief Allocates DMA physical memory
1075 * \param Pages Number of pages required
1076 * \param MaxBits Maximum number of bits the physical address can have
1077 * \param PhysAddr Pointer to the location to place the physical address allocated
1078 * \return Virtual address allocate
1080 void *MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1085 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1088 MaxBits = PHYS_BITS;
1097 if(Pages == 1 && MaxBits >= PHYS_BITS)
1099 phys = MM_AllocPhys();
1105 ret = MM_MapHWPages(phys, 1);
1116 phys = MM_AllocPhysRange(Pages, MaxBits);
1117 // - Was it allocated?
1123 // Allocated successfully, now map
1124 ret = MM_MapHWPages(phys, Pages);
1126 // If it didn't map, free then return 0
1127 for(;Pages--;phys+=0x1000)
1140 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1141 * \brief Unmap a hardware page
1143 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1147 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1150 if( KERNEL_BASE <= VAddr && VAddr < KERNEL_BASE + 1024*1024 )
1154 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1158 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1160 for( j = 0; j < Number; j++ )
1162 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1163 gaPageTable[ i + j ] = 0;
1164 INVLPG( (tVAddr)(i+j) << 12 );
1167 Mutex_Release( &glTempMappings );