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
221 Threads_SegFault(Addr);
227 // -- Check Error Code --
229 Warning("Reserved Bits Trashed!");
232 Warning("%s %s %s memory%s",
233 (ErrorCode&4?"User":"Kernel"),
234 (ErrorCode&2?"write to":"read from"),
235 (ErrorCode&1?"bad/locked":"non-present"),
236 (ErrorCode&16?" (Instruction Fetch)":"")
240 Log("Code at %p accessed %p", Regs->eip, Addr);
241 // Print Stack Backtrace
242 Error_Backtrace(Regs->eip, Regs->ebp);
244 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
245 if( gaPageDir[Addr>>22] & PF_PRESENT )
246 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
248 //MM_DumpTables(0, -1);
251 Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
252 Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
253 //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
254 Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
255 Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
258 __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
259 __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
260 Log("DR0 %08x DR1 %08x", dr0, dr1);
263 Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
267 * \fn void MM_DumpTables(tVAddr Start, tVAddr End)
268 * \brief Dumps the layout of the page tables
270 void MM_DumpTables(tVAddr Start, tVAddr End)
272 tVAddr rangeStart = 0;
276 const tPAddr MASK = ~0xF98;
278 Start >>= 12; End >>= 12;
281 Log("Directory Entries:");
282 for(page = Start >> 10;
283 page < (End >> 10)+1;
288 Log(" 0x%08x-0x%08x :: 0x%08x",
289 page<<22, ((page+1)<<22)-1,
290 gaPageDir[page]&~0xFFF
296 Log("Table Entries:");
297 for(page = Start, curPos = Start<<12;
299 curPos += 0x1000, page++)
301 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
302 || !(gaPageTable[page] & PF_PRESENT)
303 || (gaPageTable[page] & MASK) != expected)
306 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
307 rangeStart, curPos - 1,
308 gaPageTable[rangeStart>>12] & ~0xFFF,
309 (expected & ~0xFFF) - 1,
310 (expected & PF_NOPAGE ? "P" : "-"),
311 (expected & PF_COW ? "C" : "-"),
312 (expected & PF_USER ? "U" : "-"),
313 (expected & PF_WRITE ? "W" : "-")
317 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
318 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
320 expected = (gaPageTable[page] & MASK);
323 if(expected) expected += 0x1000;
327 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
328 rangeStart, curPos - 1,
329 gaPageTable[rangeStart>>12] & ~0xFFF,
330 (expected & ~0xFFF) - 1,
331 (expected & PF_NOPAGE ? "p" : "-"),
332 (expected & PF_COW ? "C" : "-"),
333 (expected & PF_USER ? "U" : "-"),
334 (expected & PF_WRITE ? "W" : "-")
341 * \fn tPAddr MM_Allocate(tVAddr VAddr)
343 tPAddr MM_Allocate(tVAddr VAddr)
346 //ENTER("xVAddr", VAddr);
347 //__asm__ __volatile__ ("xchg %bx,%bx");
348 // Check if the directory is mapped
349 if( gaPageDir[ VAddr >> 22 ] == 0 )
351 // Allocate directory
352 paddr = MM_AllocPhys();
353 //LOG("paddr = 0x%llx (new table)", paddr);
355 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
360 gaPageDir[ VAddr >> 22 ] = paddr | 3;
362 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
364 INVLPG( &gaPageDir[ VAddr >> 22 ] );
365 //LOG("Clearing new table");
366 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
368 // Check if the page is already allocated
369 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
370 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
371 //LEAVE('X', gaPageTable[ VAddr >> 12 ] & ~0xFFF);
372 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
376 paddr = MM_AllocPhys();
377 //LOG("paddr = 0x%llx", paddr);
379 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
380 VAddr, __builtin_return_address(0));
385 gaPageTable[ VAddr >> 12 ] = paddr | 3;
387 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
388 // Invalidate Cache for address
389 INVLPG( VAddr & ~0xFFF );
396 * \fn void MM_Deallocate(tVAddr VAddr)
398 void MM_Deallocate(tVAddr VAddr)
400 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
401 Warning("MM_Deallocate - Directory not mapped");
405 if(gaPageTable[ VAddr >> 12 ] == 0) {
406 Warning("MM_Deallocate - Page is not allocated");
411 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
413 gaPageTable[ VAddr >> 12 ] = 0;
417 * \fn tPAddr MM_GetPhysAddr(tVAddr Addr)
418 * \brief Checks if the passed address is accesable
420 tPAddr MM_GetPhysAddr(tVAddr Addr)
422 if( !(gaPageDir[Addr >> 22] & 1) )
424 if( !(gaPageTable[Addr >> 12] & 1) )
426 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
430 * \fn void MM_SetCR3(Uint CR3)
431 * \brief Sets the current process space
433 void MM_SetCR3(Uint CR3)
435 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
439 * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr)
440 * \brief Map a physical page to a virtual one
442 int MM_Map(tVAddr VAddr, tPAddr PAddr)
444 //ENTER("xVAddr xPAddr", VAddr, PAddr);
446 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
447 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
453 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
455 // Check if the directory is mapped
456 if( gaPageDir[ VAddr >> 22 ] == 0 )
458 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
461 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
463 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
464 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
466 // Check if the page is already allocated
467 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
468 Warning("MM_Map - Allocating to used address");
474 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
476 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
478 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
479 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
484 //LOG("INVLPG( 0x%x )", VAddr);
492 * \fn tVAddr MM_ClearUser()
493 * \brief Clear user's address space
495 tVAddr MM_ClearUser(void)
500 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
502 // Check if directory is not allocated
503 if( !(gaPageDir[i] & PF_PRESENT) ) {
509 for( j = 0; j < 1024; j ++ )
511 if( gaPageTable[i*1024+j] & 1 )
512 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
513 gaPageTable[i*1024+j] = 0;
516 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
518 INVLPG( &gaPageTable[i*1024] );
526 * \fn tPAddr MM_Clone(void)
527 * \brief Clone the current address space
529 tPAddr MM_Clone(void)
534 tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
537 Mutex_Acquire( &glTempFractal );
539 // Create Directory Table
540 *gpTmpCR3 = MM_AllocPhys() | 3;
542 //LOG("Allocated Directory (%x)", *gpTmpCR3);
543 memsetd( gaTmpDir, 0, 1024 );
545 if( Threads_GetPID() != 0 )
548 for( i = 0; i < 768; i ++)
550 // Check if table is allocated
551 if( !(gaPageDir[i] & PF_PRESENT) ) {
557 // Allocate new table
558 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
559 INVLPG( &gaTmpTable[page] );
561 for( j = 0; j < 1024; j ++, page++ )
563 if( !(gaPageTable[page] & PF_PRESENT) ) {
564 gaTmpTable[page] = 0;
569 MM_RefPhys( gaPageTable[page] & ~0xFFF );
571 if(gaPageTable[page] & PF_WRITE) {
572 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
573 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
574 INVLPG( page << 12 );
577 gaTmpTable[page] = gaPageTable[page];
582 // Map in kernel tables (and make fractal mapping)
583 for( i = 768; i < 1024; i ++ )
586 if( i == (PAGE_TABLE_ADDR >> 22) ) {
587 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3;
591 if( gaPageDir[i] == 0 ) {
596 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
597 MM_RefPhys( gaPageDir[i] & ~0xFFF );
598 gaTmpDir[i] = gaPageDir[i];
601 // Allocate kernel stack
602 for(i = KERNEL_STACKS >> 22;
603 i < KERNEL_STACKS_END >> 22;
606 // Check if directory is allocated
607 if( (gaPageDir[i] & 1) == 0 ) {
612 // We don't care about other kernel stacks, just the current one
613 if( i != kStackBase >> 22 ) {
614 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
620 gaTmpDir[i] = MM_AllocPhys() | 3;
621 INVLPG( &gaTmpTable[i*1024] );
622 for( j = 0; j < 1024; j ++ )
624 // Is the page allocated? If not, skip
625 if( !(gaPageTable[i*1024+j] & 1) ) {
626 gaTmpTable[i*1024+j] = 0;
630 // We don't care about other kernel stacks
631 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
632 gaTmpTable[i*1024+j] = 0;
637 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
639 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
641 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
642 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
643 MM_FreeTemp( (Uint)tmp );
647 ret = *gpTmpCR3 & ~0xFFF;
648 Mutex_Release( &glTempFractal );
655 * \fn tVAddr MM_NewKStack(void)
656 * \brief Create a new kernel stack
658 tVAddr MM_NewKStack(void)
662 for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
664 // Check if space is free
665 if(MM_GetPhysAddr(base) != 0) continue;
667 //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
668 for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
670 if( MM_Allocate(base+i) == 0 )
672 // On error, print a warning and return error
673 Warning("MM_NewKStack - Out of memory");
675 //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
676 // MM_Deallocate(base+i);
681 Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
682 return base+KERNEL_STACK_SIZE;
685 Warning("MM_NewKStack - No address space left");
690 * \fn tVAddr MM_NewWorkerStack()
691 * \brief Creates a new worker stack
693 tVAddr MM_NewWorkerStack()
700 tPAddr pages[WORKER_STACK_SIZE>>12];
702 // Get the old ESP and EBP
703 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
704 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
706 // TODO: Thread safety
707 // Find a free worker stack address
708 for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
711 if( gWorkerStacks[base/32] == -1 ) {
712 base += 31; base &= ~31;
713 base --; // Counteracted by the base++
717 if( gWorkerStacks[base/32] & (1 << base) ) {
722 if(base >= NUM_WORKER_STACKS) {
723 Warning("Uh-oh! Out of worker stacks");
728 gWorkerStacks[base/32] |= (1 << base);
729 // Make life easier for later calls
730 giLastUsedWorker = base;
732 base = WORKER_STACKS + base * WORKER_STACK_SIZE;
733 //Log(" MM_NewWorkerStack: base = 0x%x", base);
735 // Acquire the lock for the temp fractal mappings
736 Mutex_Acquire(&glTempFractal);
738 // Set the temp fractals to TID0's address space
739 *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
740 //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3);
744 // Check if the directory is mapped (we are assuming that the stacks
745 // will fit neatly in a directory)
746 //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]);
747 if(gaTmpDir[ base >> 22 ] == 0) {
748 gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3;
749 INVLPG( &gaTmpTable[ (base>>12) & ~0x3FF ] );
753 for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
754 //for( addr = WORKER_STACK_SIZE; addr; addr -= 0x1000 )
756 pages[ addr >> 12 ] = MM_AllocPhys();
757 gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
760 // Release the temp mapping lock
761 Mutex_Release(&glTempFractal);
763 // Copy the old stack
764 oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
765 esp = oldstack - esp; // ESP as an offset in the stack
767 // Make `base` be the top of the stack
768 base += WORKER_STACK_SIZE;
770 i = (WORKER_STACK_SIZE>>12) - 1;
771 // Copy the contents of the old stack to the new one, altering the addresses
772 // `addr` is refering to bytes from the stack base (mem downwards)
773 for(addr = 0; addr < esp; addr += 0x1000)
775 Uint *stack = (Uint*)( oldstack-(addr+0x1000) );
776 tmpPage = (void*)MM_MapTemp( pages[i] );
778 for(j = 0; j < 1024; j++)
780 // Possible Stack address?
781 if(oldstack-esp < stack[j] && stack[j] < oldstack)
782 tmpPage[j] = base - (oldstack - stack[j]);
783 else // Seems not, best leave it alone
784 tmpPage[j] = stack[j];
786 MM_FreeTemp((tVAddr)tmpPage);
790 //Log("MM_NewWorkerStack: RETURN 0x%x", base);
795 * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
796 * \brief Sets the flags on a page
798 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
801 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
802 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
804 ent = &gaPageTable[VAddr >> 12];
807 if( Mask & MM_PFLAG_RO )
809 if( Flags & MM_PFLAG_RO ) {
813 gaPageDir[VAddr >> 22] |= PF_WRITE;
819 if( Mask & MM_PFLAG_KERNEL )
821 if( Flags & MM_PFLAG_KERNEL ) {
825 gaPageDir[VAddr >> 22] |= PF_USER;
831 if( Mask & MM_PFLAG_COW )
833 if( Flags & MM_PFLAG_COW ) {
843 //Log("MM_SetFlags: *ent = 0x%08x, gaPageDir[%i] = 0x%08x",
844 // *ent, VAddr >> 22, gaPageDir[VAddr >> 22]);
848 * \brief Get the flags on a page
850 Uint MM_GetFlags(tVAddr VAddr)
856 if( !(gaPageDir[VAddr >> 22] & 1) ) return 0;
857 if( !(gaPageTable[VAddr >> 12] & 1) ) return 0;
859 ent = &gaPageTable[VAddr >> 12];
862 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
864 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
866 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
872 * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
873 * \brief Duplicates a virtual page to a physical one
875 tPAddr MM_DuplicatePage(tVAddr VAddr)
881 //ENTER("xVAddr", VAddr);
884 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
885 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
891 ret = MM_AllocPhys();
893 // Write-lock the page (to keep data constistent), saving its R/W state
894 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
895 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
899 temp = MM_MapTemp(ret);
900 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
903 // Restore Writeable status
904 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
912 * \fn Uint MM_MapTemp(tPAddr PAddr)
913 * \brief Create a temporary memory mapping
914 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
916 tVAddr MM_MapTemp(tPAddr PAddr)
920 //ENTER("XPAddr", PAddr);
924 //LOG("glTempMappings = %i", glTempMappings);
928 Mutex_Acquire( &glTempMappings );
930 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
932 // Check if page used
933 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
935 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
936 INVLPG( TEMP_MAP_ADDR + (i << 12) );
937 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
938 Mutex_Release( &glTempMappings );
939 return TEMP_MAP_ADDR + (i << 12);
941 Mutex_Release( &glTempMappings );
942 Threads_Yield(); // TODO: Use a sleep queue here instead
947 * \fn void MM_FreeTemp(tVAddr PAddr)
948 * \brief Free's a temp mapping
950 void MM_FreeTemp(tVAddr VAddr)
953 //ENTER("xVAddr", VAddr);
955 if(i >= (TEMP_MAP_ADDR >> 12))
956 gaPageTable[ i ] = 0;
962 * \fn tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
963 * \brief Allocates a contigous number of pages
965 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
972 for( i = 0; i < NUM_HW_PAGES; i ++ )
974 // Check if addr used
975 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
978 // Check possible region
979 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
981 // If there is an allocated page in the region we are testing, break
982 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
988 for( j = 0; j < Number; j++ ) {
989 MM_RefPhys( PAddr + (j<<12) );
990 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
992 return HW_MAP_ADDR + (i<<12);
995 // If we don't find any, return NULL
1000 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1001 * \brief Allocates DMA physical memory
1002 * \param Pages Number of pages required
1003 * \param MaxBits Maximum number of bits the physical address can have
1004 * \param PhysAddr Pointer to the location to place the physical address allocated
1005 * \return Virtual address allocate
1007 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
1009 tPAddr maxCheck = (1 << MaxBits);
1013 ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr);
1016 if(MaxBits < 12 || !PhysAddr) {
1022 if(MaxBits >= PHYS_BITS) maxCheck = -1;
1025 if(Pages == 1 && MaxBits >= PHYS_BITS)
1027 phys = MM_AllocPhys();
1029 ret = MM_MapHWPages(phys, 1);
1040 phys = MM_AllocPhysRange(Pages, MaxBits);
1041 // - Was it allocated?
1047 // Allocated successfully, now map
1048 ret = MM_MapHWPages(phys, Pages);
1050 // If it didn't map, free then return 0
1051 for(;Pages--;phys+=0x1000)
1063 * \fn void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1064 * \brief Unmap a hardware page
1066 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
1070 //Log_Debug("VirtMem", "MM_UnmapHWPages: (VAddr=0x%08x, Number=%i)", VAddr, Number);
1073 if(VAddr < HW_MAP_ADDR || VAddr+Number*0x1000 > HW_MAP_MAX) return;
1077 Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock
1079 for( j = 0; j < Number; j++ )
1081 MM_DerefPhys( gaPageTable[ i + j ] & ~0xFFF );
1082 gaPageTable[ i + j ] = 0;
1085 Mutex_Release( &glTempMappings );
1089 EXPORT(MM_GetPhysAddr);
1092 EXPORT(MM_MapHWPages);
1093 EXPORT(MM_AllocDMA);
1094 EXPORT(MM_UnmapHWPages);