4 * Virtual Memory Manager
9 #include <threads_int.h>
13 #define PHYS_BITS 52 // TODO: Move out
20 #define PADDR_MASK 0x7FFFFFFF##FFFFF000
21 #define PAGE_MASK (((Uint)1 << 36)-1)
22 #define TABLE_MASK (((Uint)1 << 27)-1)
23 #define PDP_MASK (((Uint)1 << 18)-1)
24 #define PML4_MASK (((Uint)1 << 9)-1)
26 #define PF_PRESENT 0x001
27 #define PF_WRITE 0x002
29 #define PF_LARGE 0x000
31 #define PF_PAGED 0x400
32 #define PF_NX 0x80000000##00000000
35 #define PAGETABLE(idx) (*((tPAddr*)MM_FRACTAL_BASE+((idx)&PAGE_MASK)))
36 #define PAGEDIR(idx) PAGETABLE((MM_FRACTAL_BASE>>12)+((idx)&TABLE_MASK))
37 #define PAGEDIRPTR(idx) PAGEDIR((MM_FRACTAL_BASE>>21)+((idx)&PDP_MASK))
38 #define PAGEMAPLVL4(idx) PAGEDIRPTR((MM_FRACTAL_BASE>>30)+((idx)&PML4_MASK))
40 #define TMPCR3() PAGEMAPLVL4(MM_TMPFRAC_BASE>>39)
41 #define TMPTABLE(idx) (*((tPAddr*)MM_TMPFRAC_BASE+((idx)&PAGE_MASK)))
42 #define TMPDIR(idx) PAGETABLE((MM_TMPFRAC_BASE>>12)+((idx)&TABLE_MASK))
43 #define TMPDIRPTR(idx) PAGEDIR((MM_TMPFRAC_BASE>>21)+((idx)&PDP_MASK))
44 #define TMPMAPLVL4(idx) PAGEDIRPTR((MM_TMPFRAC_BASE>>30)+((idx)&PML4_MASK))
46 #define INVLPG(__addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(__addr))
47 #define INVLPG_ALL() __asm__ __volatile__ ("mov %cr3,%rax;\n\tmov %rax,%cr3;")
48 #define INVLPG_GLOBAL() __asm__ __volatile__ ("mov %cr4,%rax;\n\txorl $0x80, %eax;\n\tmov %rax,%cr4;\n\txorl $0x80, %eax;\n\tmov %rax,%cr4")
51 //tPAddr * const gaPageTable = MM_FRACTAL_BASE;
54 extern void Error_Backtrace(Uint IP, Uint BP);
55 extern tPAddr gInitialPML4[512];
58 void MM_InitVirt(void);
59 //void MM_FinishVirtualInit(void);
60 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
61 void MM_DumpTables(tVAddr Start, tVAddr End);
62 int MM_GetPageEntryPtr(tVAddr Addr, BOOL bTemp, BOOL bAllocate, BOOL bLargePage, tPAddr **Pointer);
63 int MM_MapEx(tVAddr VAddr, tPAddr PAddr, BOOL bTemp, BOOL bLarge);
64 // int MM_Map(tVAddr VAddr, tPAddr PAddr);
65 void MM_Unmap(tVAddr VAddr);
66 void MM_ClearUser(void);
67 int MM_GetPageEntry(tVAddr Addr, tPAddr *Phys, Uint *Flags);
70 tMutex glMM_TempFractalLock;
73 void MM_InitVirt(void)
75 MM_DumpTables(0, -1L);
78 void MM_FinishVirtualInit(void)
84 * \brief Called on a page fault
86 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
88 // TODO: Implement Copy-on-Write
90 if( gaPageDir [Addr>>22] & PF_PRESENT
91 && gaPageTable[Addr>>12] & PF_PRESENT
92 && gaPageTable[Addr>>12] & PF_COW )
95 if(MM_GetRefCount( gaPageTable[Addr>>12] & PADDR_MASK ) == 1)
97 gaPageTable[Addr>>12] &= ~PF_COW;
98 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
102 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
103 paddr = MM_DuplicatePage( Addr );
104 MM_DerefPhys( gaPageTable[Addr>>12] & PADDR_MASK );
105 gaPageTable[Addr>>12] &= PF_USER;
106 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
109 INVLPG( Addr & ~0xFFF );
114 // If it was a user, tell the thread handler
116 Warning("%s %s %s memory%s",
117 (ErrorCode&4?"User":"Kernel"),
118 (ErrorCode&2?"write to":"read from"),
119 (ErrorCode&1?"bad/locked":"non-present"),
120 (ErrorCode&16?" (Instruction Fetch)":"")
122 Warning("User Pagefault: Instruction at %04x:%08x accessed %p",
123 Regs->CS, Regs->RIP, Addr);
124 __asm__ __volatile__ ("sti"); // Restart IRQs
125 // Threads_SegFault(Addr);
131 // -- Check Error Code --
133 Warning("Reserved Bits Trashed!");
136 Warning("%s %s %s memory%s",
137 (ErrorCode&4?"User":"Kernel"),
138 (ErrorCode&2?"write to":"read from"),
139 (ErrorCode&1?"bad/locked":"non-present"),
140 (ErrorCode&16?" (Instruction Fetch)":"")
144 Log("Code at %p accessed %p", Regs->RIP, Addr);
145 // Print Stack Backtrace
146 Error_Backtrace(Regs->RIP, Regs->RBP);
148 MM_DumpTables(0, -1);
150 __asm__ __volatile__ ("cli");
156 * \brief Dumps the layout of the page tables
158 void MM_DumpTables(tVAddr Start, tVAddr End)
160 #define CANOICAL(addr) ((addr)&0x800000000000?(addr)|0xFFFF000000000000:(addr))
161 const tPAddr CHANGEABLE_BITS = 0xFF8;
162 const tPAddr MASK = ~CHANGEABLE_BITS; // Physical address and access bits
163 tVAddr rangeStart = 0;
164 tPAddr expected = CHANGEABLE_BITS; // CHANGEABLE_BITS is used because it's not a vaild value
168 Log("Table Entries: (%p to %p)", Start, End);
170 End &= (1L << 48) - 1;
172 Start >>= 12; End >>= 12;
174 for(page = Start, curPos = Start<<12;
176 curPos += 0x1000, page++)
178 if( curPos == 0x800000000000L )
179 curPos = 0xFFFF800000000000L;
181 //Debug("&PAGEMAPLVL4(%i page>>27) = %p", page>>27, &PAGEMAPLVL4(page>>27));
182 //Debug("&PAGEDIRPTR(%i page>>18) = %p", page>>18, &PAGEDIRPTR(page>>18));
183 //Debug("&PAGEDIR(%i page>>9) = %p", page>>9, &PAGEDIR(page>>9));
184 //Debug("&PAGETABLE(%i page) = %p", page, &PAGETABLE(page));
188 !(PAGEMAPLVL4(page>>27) & PF_PRESENT)
189 || !(PAGEDIRPTR(page>>18) & PF_PRESENT)
190 || !(PAGEDIR(page>>9) & PF_PRESENT)
191 || !(PAGETABLE(page) & PF_PRESENT)
192 || (PAGETABLE(page) & MASK) != expected)
194 if(expected != CHANGEABLE_BITS) {
195 Log("%016llx => %013llx : 0x%6llx (%c%c%c%c)",
196 CANOICAL(rangeStart),
197 PAGETABLE(rangeStart>>12) & PADDR_MASK,
199 (expected & PF_PAGED ? 'p' : '-'),
200 (expected & PF_COW ? 'C' : '-'),
201 (expected & PF_USER ? 'U' : '-'),
202 (expected & PF_WRITE ? 'W' : '-')
204 expected = CHANGEABLE_BITS;
206 if( !(PAGEMAPLVL4(page>>27) & PF_PRESENT) ) {
207 page += (1 << 27) - 1;
208 curPos += (1L << 39) - 0x1000;
209 //Debug("pml4 ent unset (page = 0x%x now)", page);
212 if( !(PAGEDIRPTR(page>>18) & PF_PRESENT) ) {
213 page += (1 << 18) - 1;
214 curPos += (1L << 30) - 0x1000;
215 //Debug("pdp ent unset (page = 0x%x now)", page);
218 if( !(PAGEDIR(page>>9) & PF_PRESENT) ) {
219 page += (1 << 9) - 1;
220 curPos += (1L << 21) - 0x1000;
221 //Debug("pd ent unset (page = 0x%x now)", page);
224 if( !(PAGETABLE(page) & PF_PRESENT) ) continue;
226 expected = (PAGETABLE(page) & MASK);
229 if(expected != CHANGEABLE_BITS)
233 if(expected != CHANGEABLE_BITS) {
234 Log("%016llx => %013llx : 0x%6llx (%c%c%c%c)",
235 CANOICAL(rangeStart),
236 PAGETABLE(rangeStart>>12) & PADDR_MASK,
238 (expected & PF_PAGED ? 'p' : '-'),
239 (expected & PF_COW ? 'C' : '-'),
240 (expected & PF_USER ? 'U' : '-'),
241 (expected & PF_WRITE ? 'W' : '-')
249 * \brief Get a pointer to a page entry
250 * \param Addr Virtual Address
251 * \param bTemp Use the Temporary fractal mapping
252 * \param bAllocate Allocate entries
253 * \param bLargePage Request a large page
254 * \param Pointer Location to place the calculated pointer
255 * \return Page size, or -ve on error
257 int MM_GetPageEntryPtr(tVAddr Addr, BOOL bTemp, BOOL bAllocate, BOOL bLargePage, tPAddr **Pointer)
261 const int ADDR_SIZES[] = {39, 30, 21, 12};
262 const int nADDR_SIZES = sizeof(ADDR_SIZES)/sizeof(ADDR_SIZES[0]);
267 pmlevels[3] = &TMPTABLE(0); // Page Table
268 pmlevels[2] = &TMPDIR(0); // PDIR
269 pmlevels[1] = &TMPDIRPTR(0); // PDPT
270 pmlevels[0] = &TMPMAPLVL4(0); // PML4
274 pmlevels[3] = (void*)MM_FRACTAL_BASE; // Page Table
275 pmlevels[2] = &pmlevels[3][(MM_FRACTAL_BASE>>12)&PAGE_MASK]; // PDIR
276 pmlevels[1] = &pmlevels[2][(MM_FRACTAL_BASE>>21)&TABLE_MASK]; // PDPT
277 pmlevels[0] = &pmlevels[1][(MM_FRACTAL_BASE>>30)&PDP_MASK]; // PML4
281 Addr &= (1ULL << 48)-1;
283 for( i = 0; i < nADDR_SIZES-1; i ++ )
285 // INVLPG( &pmlevels[i][ (Addr >> ADDR_SIZES[i]) &
287 // Check for a large page
288 if( (Addr & ((1ULL << ADDR_SIZES[i])-1)) == 0 && bLargePage )
290 if(Pointer) *Pointer = &pmlevels[i][Addr >> ADDR_SIZES[i]];
291 return ADDR_SIZES[i];
293 // Allocate an entry if required
294 if( !(pmlevels[i][Addr >> ADDR_SIZES[i]] & 1) )
296 if( !bAllocate ) return -4; // If allocation is not requested, error
297 tmp = MM_AllocPhys();
299 pmlevels[i][Addr >> ADDR_SIZES[i]] = tmp | 3;
300 INVLPG( &pmlevels[i+1][ (Addr>>ADDR_SIZES[i])*512 ] );
301 memset( &pmlevels[i+1][ (Addr>>ADDR_SIZES[i])*512 ], 0, 0x1000 );
304 else if( pmlevels[i][Addr >> ADDR_SIZES[i]] & PF_LARGE )
307 if( (Addr & ((1ULL << ADDR_SIZES[i])-1)) != 0 ) return -3;
308 if(Pointer) *Pointer = &pmlevels[i][Addr >> ADDR_SIZES[i]];
309 return ADDR_SIZES[i]; // Large page warning
313 // And, set the page table entry
314 if(Pointer) *Pointer = &pmlevels[i][Addr >> ADDR_SIZES[i]];
315 return ADDR_SIZES[i];
319 * \brief Map a physical page to a virtual one
320 * \param VAddr Target virtual address
321 * \param PAddr Physical address of page
322 * \param bTemp Use tempoary mappings
323 * \param bLarge Treat as a large page
325 int MM_MapEx(tVAddr VAddr, tPAddr PAddr, BOOL bTemp, BOOL bLarge)
330 ENTER("xVAddr xPAddr", VAddr, PAddr);
332 // Get page pointer (Allow allocating)
333 rv = MM_GetPageEntryPtr(VAddr, bTemp, 1, bLarge, &ent);
334 if(rv < 0) LEAVE_RET('i', 0);
336 if( *ent & 1 ) LEAVE_RET('i', 0);
347 * \brief Map a physical page to a virtual one
348 * \param VAddr Target virtual address
349 * \param PAddr Physical address of page
351 int MM_Map(tVAddr VAddr, tPAddr PAddr)
353 return MM_MapEx(VAddr, PAddr, 0, 0);
357 * \brief Removed a mapped page
359 void MM_Unmap(tVAddr VAddr)
362 if( !(PAGEMAPLVL4(VAddr >> 39) & 1) ) return ;
364 if( !(PAGEDIRPTR(VAddr >> 30) & 1) ) return ;
366 if( !(PAGEDIR(VAddr >> 21) & 1) ) return ;
368 PAGETABLE(VAddr >> PTAB_SHIFT) = 0;
373 * \brief Allocate a block of memory at the specified virtual address
375 tPAddr MM_Allocate(tVAddr VAddr)
379 ENTER("xVAddr", VAddr);
381 // Ensure the tables are allocated before the page (keeps things neat)
382 MM_GetPageEntryPtr(VAddr, 0, 1, 0, NULL);
385 ret = MM_AllocPhys();
386 LOG("ret = %x", ret);
387 if(!ret) LEAVE_RET('i', 0);
389 if( !MM_Map(VAddr, ret) )
391 Warning("MM_Allocate: Unable to map. Strange, we should have errored earlier");
402 * \brief Deallocate a page at a virtual address
404 void MM_Deallocate(tVAddr VAddr)
408 phys = MM_GetPhysAddr(VAddr);
417 * \brief Get the page table entry of a virtual address
418 * \param Addr Virtual Address
419 * \param Phys Location to put the physical address
420 * \param Flags Flags on the entry (set to zero if unmapped)
421 * \return Size of the entry (in address bits) - 12 = 4KiB page
423 int MM_GetPageEntry(tVAddr Addr, tPAddr *Phys, Uint *Flags)
428 if(!Phys || !Flags) return 0;
430 ret = MM_GetPageEntryPtr(Addr, 0, 0, 0, &ptr);
431 if( ret < 0 ) return 0;
433 *Phys = *ptr & PADDR_MASK;
434 *Flags = *ptr & 0xFFF;
439 * \brief Get the physical address of a virtual location
441 tPAddr MM_GetPhysAddr(tVAddr Addr)
446 ret = MM_GetPageEntryPtr(Addr, 0, 0, 0, &ptr);
447 if( ret < 0 ) return 0;
449 return (*ptr & PADDR_MASK) | (Addr & 0xFFF);
453 * \brief Sets the flags on a page
455 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
461 rv = MM_GetPageEntryPtr(VAddr, 0, 0, 0, &ent);
464 // Ensure the entry is valid
465 if( !(*ent & 1) ) return ;
468 if( Mask & MM_PFLAG_RO )
470 if( Flags & MM_PFLAG_RO ) {
479 if( Mask & MM_PFLAG_KERNEL )
481 if( Flags & MM_PFLAG_KERNEL ) {
490 if( Mask & MM_PFLAG_COW )
492 if( Flags & MM_PFLAG_COW ) {
503 if( Mask & MM_PFLAG_EXEC )
505 if( Flags & MM_PFLAG_EXEC ) {
515 * \brief Get the flags applied to a page
517 Uint MM_GetFlags(tVAddr VAddr)
522 rv = MM_GetPageEntryPtr(VAddr, 0, 0, 0, &ent);
525 if( !(*ent & 1) ) return 0;
528 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
530 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
532 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
534 if( !(*ent & PF_NX) ) ret |= MM_PFLAG_EXEC;
539 // --- Hardware Mappings ---
541 * \brief Map a range of hardware pages
543 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
548 //TODO: Add speedups (memory of first possible free)
549 for( ret = MM_HWMAP_BASE; ret < MM_HWMAP_TOP; ret += 0x1000 )
551 for( num = Number; num -- && ret < MM_HWMAP_TOP; ret += 0x1000 )
553 if( MM_GetPhysAddr(ret) != 0 ) break;
555 if( num >= 0 ) continue;
557 PAddr += 0x1000 * Number;
569 Log_KernelPanic("MM", "TODO: Implement MM_MapHWPages");
574 * \brief Free a range of hardware pages
576 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
578 // Log_KernelPanic("MM", "TODO: Implement MM_UnmapHWPages");
588 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
589 * \brief Allocates DMA physical memory
590 * \param Pages Number of pages required
591 * \param MaxBits Maximum number of bits the physical address can have
592 * \param PhysAddr Pointer to the location to place the physical address allocated
593 * \return Virtual address allocate
595 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
601 if(MaxBits < 12 || !PhysAddr) return 0;
604 if(Pages == 1 && MaxBits >= PHYS_BITS)
606 phys = MM_AllocPhys();
608 ret = MM_MapHWPages(phys, 1);
617 phys = MM_AllocPhysRange(Pages, MaxBits);
618 // - Was it allocated?
619 if(phys == 0) return 0;
621 // Allocated successfully, now map
622 ret = MM_MapHWPages(phys, Pages);
624 // If it didn't map, free then return 0
625 for(;Pages--;phys+=0x1000)
634 // --- Tempory Mappings ---
635 tVAddr MM_MapTemp(tPAddr PAddr)
637 const int max_slots = (MM_TMPMAP_END - MM_TMPMAP_BASE) / PAGE_SIZE;
638 tVAddr ret = MM_TMPMAP_BASE;
641 for( i = 0; i < max_slots; i ++, ret += PAGE_SIZE )
644 if( MM_GetPageEntryPtr( ret, 0, 1, 0, &ent) < 0 ) {
657 void MM_FreeTemp(tVAddr VAddr)
659 MM_Deallocate(VAddr);
664 // --- Address Space Clone --
665 tPAddr MM_Clone(void)
671 // tThread->KernelStack is the top
672 // There is 1 guard page below the stack
673 kstackbase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE + 0x1000;
675 Log("MM_Clone: kstackbase = %p", kstackbase);
677 // #1 Create a copy of the PML4
678 ret = MM_AllocPhys();
681 // #2 Alter the fractal pointer
682 Mutex_Acquire(&glMM_TempFractalLock);
686 // #3 Set Copy-On-Write to all user pages
687 for( i = 0; i < 256; i ++)
689 TMPMAPLVL4(i) = PAGEMAPLVL4(i);
690 // Log_Debug("MM", "TMPMAPLVL4(%i) = 0x%016llx", i, TMPMAPLVL4(i));
691 if( TMPMAPLVL4(i) & 1 )
693 MM_RefPhys( TMPMAPLVL4(i) & PADDR_MASK );
694 TMPMAPLVL4(i) |= PF_COW;
695 TMPMAPLVL4(i) &= ~PF_WRITE;
699 // #4 Map in kernel pages
700 for( i = 256; i < 512; i ++ )
703 // 320 0xFFFFA.... - Kernel Stacks
704 if( i == 320 ) continue;
705 // 509 0xFFFFFE0.. - Fractal mapping
706 if( i == 509 ) continue;
707 // 510 0xFFFFFE8.. - Temp fractal mapping
708 if( i == 510 ) continue;
710 TMPMAPLVL4(i) = PAGEMAPLVL4(i);
711 if( TMPMAPLVL4(i) & 1 )
712 MM_RefPhys( TMPMAPLVL4(i) & PADDR_MASK );
715 // #5 Set fractal mapping
716 TMPMAPLVL4(509) = ret | 3;
717 TMPMAPLVL4(510) = 0; // Temp
719 // #6 Create kernel stack (-1 to account for the guard)
721 for( i = 0; i < KERNEL_STACK_SIZE/0x1000-1; i ++ )
723 tPAddr phys = MM_AllocPhys();
725 MM_MapEx(kstackbase+i*0x1000, phys, 1, 0);
727 tmpmapping = MM_MapTemp(phys);
728 memcpy((void*)tmpmapping, (void*)(kstackbase+i*0x1000), 0x1000);
729 MM_FreeTemp(tmpmapping);
735 Mutex_Release(&glMM_TempFractalLock);
736 Log("MM_Clone: RETURN %P\n", ret);
740 void MM_ClearUser(void)
743 int pml4, pdpt, pd, pt;
745 for( pml4 = 0; pml4 < 256; pml4 ++ )
747 // Catch an un-allocated PML4 entry
748 if( !(PAGEMAPLVL4(pml4) & 1) ) {
749 addr += 1ULL << PML4_SHIFT;
754 if( (PAGEMAPLVL4(pml4) & PF_COW) ) {
755 addr += 1ULL << PML4_SHIFT;
762 for( pdpt = 0; pdpt < 512; pdpt ++ )
765 if( !(PAGEDIRPTR(addr >> PDP_SHIFT) & 1) ) {
766 addr += 1ULL << PDP_SHIFT;
771 if( (PAGEDIRPTR(addr >> PDP_SHIFT) & PF_COW) ) {
772 addr += 1ULL << PDP_SHIFT;
776 for( pd = 0; pd < 512; pd ++ )
778 // Unallocated PDir entry
779 if( !(PAGEDIR(addr >> PDIR_SHIFT) & 1) ) {
780 addr += 1ULL << PDIR_SHIFT;
785 if( PAGEDIR(addr >> PDIR_SHIFT) & PF_COW ) {
786 addr += 1ULL << PDIR_SHIFT;
790 // TODO: Catch large pages
793 for( pt = 0; pt < 512; pt ++ )
796 if( PAGETABLE(addr >> PTAB_SHIFT) & 1 ) {
797 MM_DerefPhys( PAGETABLE(addr >> PTAB_SHIFT) & PADDR_MASK );
798 PAGETABLE(addr >> PTAB_SHIFT) = 0;
804 MM_DerefPhys( PAGEDIR(addr >> PDIR_SHIFT) & PADDR_MASK );
805 PAGEDIR(addr >> PDIR_SHIFT) = 0;
808 // Free page directory
809 MM_DerefPhys( PAGEDIRPTR(addr >> PDP_SHIFT) & PADDR_MASK );
810 PAGEDIRPTR(addr >> PDP_SHIFT) = 0;
813 // Free page directory pointer table (PML4 entry)
814 MM_DerefPhys( PAGEMAPLVL4(pml4) & PADDR_MASK );
815 PAGEMAPLVL4(pml4) = 0;
819 tVAddr MM_NewWorkerStack(void)
824 // #1 Set temp fractal to PID0
825 Mutex_Acquire(&glMM_TempFractalLock);
826 TMPCR3() = ((tPAddr)gInitialPML4 - KERNEL_BASE) | 3;
828 // #2 Scan for a free stack addresss < 2^47
829 for(ret = 0x100000; ret < (1ULL << 47); ret += KERNEL_STACK_SIZE)
831 if( MM_GetPhysAddr(ret) == 0 ) break;
833 if( ret >= (1ULL << 47) ) {
834 Mutex_Release(&glMM_TempFractalLock);
838 // #3 Map all save the last page in the range
839 // - This acts as as guard page, and doesn't cost us anything.
840 for( i = 0; i < KERNEL_STACK_SIZE/0x1000 - 1; i ++ )
842 tPAddr phys = MM_AllocPhys();
845 Log_Error("MM", "MM_NewWorkerStack - Unable to allocate page");
848 MM_MapEx(ret + i*0x1000, phys, 1, 0);
851 Mutex_Release(&glMM_TempFractalLock);
853 return ret + i*0x1000;
857 * \brief Allocate a new kernel stack
859 tVAddr MM_NewKStack(void)
861 tVAddr base = MM_KSTACK_BASE;
863 for( ; base < MM_KSTACK_TOP; base += KERNEL_STACK_SIZE )
865 if(MM_GetPhysAddr(base) != 0)
868 //Log("MM_NewKStack: Found one at %p", base + KERNEL_STACK_SIZE);
869 for( i = 0; i < KERNEL_STACK_SIZE; i += 0x1000)
871 if( !MM_Allocate(base+i) )
873 Log_Warning("MM", "MM_NewKStack - Allocation failed");
874 for( i -= 0x1000; i; i -= 0x1000)
875 MM_Deallocate(base+i);
880 return base + KERNEL_STACK_SIZE;
882 Log_Warning("MM", "MM_NewKStack - No address space left\n");