4 * Virtual Memory Manager
9 #include <threads_int.h>
13 #define PHYS_BITS 52 // TODO: Move out
21 #define PADDR_MASK 0x7FFFFFFF##FFFFF000
22 #define PAGE_MASK ((1LL << 36)-1)
23 #define TABLE_MASK ((1LL << 27)-1)
24 #define PDP_MASK ((1LL << 18)-1)
25 #define PML4_MASK ((1LL << 9)-1)
27 #define PF_PRESENT 0x001
28 #define PF_WRITE 0x002
30 #define PF_LARGE 0x000
32 #define PF_PAGED 0x400
33 #define PF_NX 0x80000000##00000000
36 #define PAGETABLE(idx) (*((Uint64*)MM_FRACTAL_BASE+((idx)&PAGE_MASK)))
37 #define PAGEDIR(idx) PAGETABLE((MM_FRACTAL_BASE>>12)+((idx)&TABLE_MASK))
38 #define PAGEDIRPTR(idx) PAGEDIR((MM_FRACTAL_BASE>>21)+((idx)&PDP_MASK))
39 #define PAGEMAPLVL4(idx) PAGEDIRPTR((MM_FRACTAL_BASE>>30)+((idx)&PML4_MASK))
41 #define TMPCR3() PAGEMAPLVL4(MM_TMPFRAC_BASE>>39)
42 #define TMPTABLE(idx) (*((Uint64*)MM_TMPFRAC_BASE+((idx)&PAGE_MASK)))
43 #define TMPDIR(idx) PAGETABLE((MM_TMPFRAC_BASE>>12)+((idx)&TABLE_MASK))
44 #define TMPDIRPTR(idx) PAGEDIR((MM_TMPFRAC_BASE>>21)+((idx)&PDP_MASK))
45 #define TMPMAPLVL4(idx) PAGEDIRPTR((MM_TMPFRAC_BASE>>30)+((idx)&PML4_MASK))
47 #define INVLPG(__addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(__addr))
48 #define INVLPG_ALL() __asm__ __volatile__ ("mov %cr3,%rax;\n\tmov %rax,%cr3;")
49 #define INVLPG_GLOBAL() __asm__ __volatile__ ("mov %cr4,%rax;\n\txorl $0x80, %eax;\n\tmov %rax,%cr4;\n\txorl $0x80, %eax;\n\tmov %rax,%cr4")
52 //tPAddr * const gaPageTable = MM_FRACTAL_BASE;
55 extern void Error_Backtrace(Uint IP, Uint BP);
56 extern tPAddr gInitialPML4[512];
57 extern void Threads_SegFault(tVAddr Addr);
60 void MM_InitVirt(void);
61 //void MM_FinishVirtualInit(void);
62 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
63 void MM_DumpTables(tVAddr Start, tVAddr End);
64 int MM_GetPageEntryPtr(tVAddr Addr, BOOL bTemp, BOOL bAllocate, BOOL bLargePage, tPAddr **Pointer);
65 int MM_MapEx(tVAddr VAddr, tPAddr PAddr, BOOL bTemp, BOOL bLarge);
66 // int MM_Map(tVAddr VAddr, tPAddr PAddr);
67 void MM_Unmap(tVAddr VAddr);
68 void MM_ClearUser(void);
69 int MM_GetPageEntry(tVAddr Addr, tPAddr *Phys, Uint *Flags);
72 tMutex glMM_TempFractalLock;
75 void MM_InitVirt(void)
77 MM_DumpTables(0, -1L);
80 void MM_FinishVirtualInit(void)
86 * \brief Called on a page fault
88 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
90 // TODO: Implement Copy-on-Write
92 if( gaPageDir [Addr>>22] & PF_PRESENT
93 && gaPageTable[Addr>>12] & PF_PRESENT
94 && gaPageTable[Addr>>12] & PF_COW )
97 if(MM_GetRefCount( gaPageTable[Addr>>12] & PADDR_MASK ) == 1)
99 gaPageTable[Addr>>12] &= ~PF_COW;
100 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
104 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
105 paddr = MM_DuplicatePage( Addr );
106 MM_DerefPhys( gaPageTable[Addr>>12] & PADDR_MASK );
107 gaPageTable[Addr>>12] &= PF_USER;
108 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
111 INVLPG( Addr & ~0xFFF );
116 // If it was a user, tell the thread handler
118 Warning("User %s %s memory%s",
119 (ErrorCode&2?"write to":"read from"),
120 (ErrorCode&1?"bad/locked":"non-present"),
121 (ErrorCode&16?" (Instruction Fetch)":"")
123 Warning("User Pagefault: Instruction at %04x:%p accessed %p",
124 Regs->CS, Regs->RIP, Addr);
125 __asm__ __volatile__ ("sti"); // Restart IRQs
126 Threads_SegFault(Addr);
132 // -- Check Error Code --
134 Warning("Reserved Bits Trashed!");
137 Warning("Kernel %s %s memory%s",
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 = ~(PF_PRESENT|PF_WRITE|PF_USER|PF_COW|PF_PAGED) & 0xFFF;
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));
187 if(!(PAGEMAPLVL4(page>>27) & PF_PRESENT)
188 || !(PAGEDIRPTR(page>>18) & PF_PRESENT)
189 || !(PAGEDIR(page>>9) & PF_PRESENT)
190 || !(PAGETABLE(page) & PF_PRESENT)
191 || (PAGETABLE(page) & MASK) != expected)
193 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]);
265 #define BITMASK(bits) ( (1LL << (bits))-1 )
269 pmlevels[3] = &TMPTABLE(0); // Page Table
270 pmlevels[2] = &TMPDIR(0); // PDIR
271 pmlevels[1] = &TMPDIRPTR(0); // PDPT
272 pmlevels[0] = &TMPMAPLVL4(0); // PML4
276 pmlevels[3] = (void*)MM_FRACTAL_BASE; // Page Table
277 pmlevels[2] = &pmlevels[3][(MM_FRACTAL_BASE>>12)&BITMASK(VIRT_BITS-12)]; // PDIR
278 pmlevels[1] = &pmlevels[2][(MM_FRACTAL_BASE>>21)&BITMASK(VIRT_BITS-21)]; // PDPT
279 pmlevels[0] = &pmlevels[1][(MM_FRACTAL_BASE>>30)&BITMASK(VIRT_BITS-30)]; // PML4
283 Addr &= (1ULL << 48)-1;
285 for( i = 0; i < nADDR_SIZES-1; i ++ )
287 // INVLPG( &pmlevels[i][ (Addr >> ADDR_SIZES[i]) &
289 // Check for a large page
290 if( (Addr & ((1ULL << ADDR_SIZES[i])-1)) == 0 && bLargePage )
292 if(Pointer) *Pointer = &pmlevels[i][Addr >> ADDR_SIZES[i]];
293 return ADDR_SIZES[i];
295 // Allocate an entry if required
296 if( !(pmlevels[i][Addr >> ADDR_SIZES[i]] & 1) )
298 if( !bAllocate ) return -4; // If allocation is not requested, error
299 if( !(tmp = MM_AllocPhys()) ) return -2;
300 pmlevels[i][Addr >> ADDR_SIZES[i]] = tmp | 3;
301 if( Addr < 0x800000000000 )
302 pmlevels[i][Addr >> ADDR_SIZES[i]] |= PF_USER;
303 INVLPG( &pmlevels[i+1][ (Addr>>ADDR_SIZES[i])*512 ] );
304 memset( &pmlevels[i+1][ (Addr>>ADDR_SIZES[i])*512 ], 0, 0x1000 );
305 LOG("Init PML%i ent 0x%x %p with %P", 4 - i,
307 (Addr>>ADDR_SIZES[i])<<ADDR_SIZES[i], tmp);
310 else if( pmlevels[i][Addr >> ADDR_SIZES[i]] & PF_LARGE )
313 if( (Addr & ((1ULL << ADDR_SIZES[i])-1)) != 0 ) return -3;
314 if(Pointer) *Pointer = &pmlevels[i][Addr >> ADDR_SIZES[i]];
315 return ADDR_SIZES[i]; // Large page warning
319 // And, set the page table entry
320 if(Pointer) *Pointer = &pmlevels[i][Addr >> ADDR_SIZES[i]];
321 return ADDR_SIZES[i];
325 * \brief Map a physical page to a virtual one
326 * \param VAddr Target virtual address
327 * \param PAddr Physical address of page
328 * \param bTemp Use tempoary mappings
329 * \param bLarge Treat as a large page
331 int MM_MapEx(tVAddr VAddr, tPAddr PAddr, BOOL bTemp, BOOL bLarge)
336 ENTER("xVAddr xPAddr", VAddr, PAddr);
338 // Get page pointer (Allow allocating)
339 rv = MM_GetPageEntryPtr(VAddr, bTemp, 1, bLarge, &ent);
340 if(rv < 0) LEAVE_RET('i', 0);
342 if( *ent & 1 ) LEAVE_RET('i', 0);
346 if( VAddr < 0x800000000000 )
356 * \brief Map a physical page to a virtual one
357 * \param VAddr Target virtual address
358 * \param PAddr Physical address of page
360 int MM_Map(tVAddr VAddr, tPAddr PAddr)
362 return MM_MapEx(VAddr, PAddr, 0, 0);
366 * \brief Removed a mapped page
368 void MM_Unmap(tVAddr VAddr)
371 if( !(PAGEMAPLVL4(VAddr >> 39) & 1) ) return ;
373 if( !(PAGEDIRPTR(VAddr >> 30) & 1) ) return ;
375 if( !(PAGEDIR(VAddr >> 21) & 1) ) return ;
377 PAGETABLE(VAddr >> PTAB_SHIFT) = 0;
382 * \brief Allocate a block of memory at the specified virtual address
384 tPAddr MM_Allocate(tVAddr VAddr)
388 ENTER("xVAddr", VAddr);
390 // Ensure the tables are allocated before the page (keeps things neat)
391 MM_GetPageEntryPtr(VAddr, 0, 1, 0, NULL);
394 ret = MM_AllocPhys();
395 LOG("ret = %x", ret);
396 if(!ret) LEAVE_RET('i', 0);
398 if( !MM_Map(VAddr, ret) )
400 Warning("MM_Allocate: Unable to map. Strange, we should have errored earlier");
411 * \brief Deallocate a page at a virtual address
413 void MM_Deallocate(tVAddr VAddr)
417 phys = MM_GetPhysAddr(VAddr);
426 * \brief Get the page table entry of a virtual address
427 * \param Addr Virtual Address
428 * \param Phys Location to put the physical address
429 * \param Flags Flags on the entry (set to zero if unmapped)
430 * \return Size of the entry (in address bits) - 12 = 4KiB page
432 int MM_GetPageEntry(tVAddr Addr, tPAddr *Phys, Uint *Flags)
437 if(!Phys || !Flags) return 0;
439 ret = MM_GetPageEntryPtr(Addr, 0, 0, 0, &ptr);
440 if( ret < 0 ) return 0;
442 *Phys = *ptr & PADDR_MASK;
443 *Flags = *ptr & 0xFFF;
448 * \brief Get the physical address of a virtual location
450 tPAddr MM_GetPhysAddr(tVAddr Addr)
455 ret = MM_GetPageEntryPtr(Addr, 0, 0, 0, &ptr);
456 if( ret < 0 ) return 0;
458 if( !(*ptr & 1) ) return 0;
460 return (*ptr & PADDR_MASK) | (Addr & 0xFFF);
464 * \brief Sets the flags on a page
466 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
472 rv = MM_GetPageEntryPtr(VAddr, 0, 0, 0, &ent);
475 // Ensure the entry is valid
476 if( !(*ent & 1) ) return ;
479 if( Mask & MM_PFLAG_RO )
481 if( Flags & MM_PFLAG_RO ) {
490 if( Mask & MM_PFLAG_KERNEL )
492 if( Flags & MM_PFLAG_KERNEL ) {
501 if( Mask & MM_PFLAG_COW )
503 if( Flags & MM_PFLAG_COW ) {
514 if( Mask & MM_PFLAG_EXEC )
516 if( Flags & MM_PFLAG_EXEC ) {
526 * \brief Get the flags applied to a page
528 Uint MM_GetFlags(tVAddr VAddr)
533 rv = MM_GetPageEntryPtr(VAddr, 0, 0, 0, &ent);
536 if( !(*ent & 1) ) return 0;
539 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
541 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
543 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
545 if( !(*ent & PF_NX) ) ret |= MM_PFLAG_EXEC;
550 // --- Hardware Mappings ---
552 * \brief Map a range of hardware pages
554 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
559 //TODO: Add speedups (memory of first possible free)
560 for( ret = MM_HWMAP_BASE; ret < MM_HWMAP_TOP; ret += 0x1000 )
562 for( num = Number; num -- && ret < MM_HWMAP_TOP; ret += 0x1000 )
564 if( MM_GetPhysAddr(ret) != 0 ) break;
566 if( num >= 0 ) continue;
568 PAddr += 0x1000 * Number;
580 Log_KernelPanic("MM", "TODO: Implement MM_MapHWPages");
585 * \brief Free a range of hardware pages
587 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
589 // Log_KernelPanic("MM", "TODO: Implement MM_UnmapHWPages");
599 * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
600 * \brief Allocates DMA physical memory
601 * \param Pages Number of pages required
602 * \param MaxBits Maximum number of bits the physical address can have
603 * \param PhysAddr Pointer to the location to place the physical address allocated
604 * \return Virtual address allocate
606 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
612 if(MaxBits < 12 || !PhysAddr) return 0;
615 if(Pages == 1 && MaxBits >= PHYS_BITS)
617 phys = MM_AllocPhys();
619 ret = MM_MapHWPages(phys, 1);
628 phys = MM_AllocPhysRange(Pages, MaxBits);
629 // - Was it allocated?
630 if(phys == 0) return 0;
632 // Allocated successfully, now map
633 ret = MM_MapHWPages(phys, Pages);
635 // If it didn't map, free then return 0
636 for(;Pages--;phys+=0x1000)
645 // --- Tempory Mappings ---
646 tVAddr MM_MapTemp(tPAddr PAddr)
648 const int max_slots = (MM_TMPMAP_END - MM_TMPMAP_BASE) / PAGE_SIZE;
649 tVAddr ret = MM_TMPMAP_BASE;
652 for( i = 0; i < max_slots; i ++, ret += PAGE_SIZE )
655 if( MM_GetPageEntryPtr( ret, 0, 1, 0, &ent) < 0 ) {
668 void MM_FreeTemp(tVAddr VAddr)
670 MM_Deallocate(VAddr);
675 // --- Address Space Clone --
676 tPAddr MM_Clone(void)
682 // tThread->KernelStack is the top
683 // There is 1 guard page below the stack
684 kstackbase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE + 0x1000;
686 Log("MM_Clone: kstackbase = %p", kstackbase);
688 // #1 Create a copy of the PML4
689 ret = MM_AllocPhys();
692 // #2 Alter the fractal pointer
693 Mutex_Acquire(&glMM_TempFractalLock);
697 // #3 Set Copy-On-Write to all user pages
698 for( i = 0; i < 256; i ++)
700 TMPMAPLVL4(i) = PAGEMAPLVL4(i);
701 // Log_Debug("MM", "TMPMAPLVL4(%i) = 0x%016llx", i, TMPMAPLVL4(i));
702 if( TMPMAPLVL4(i) & 1 )
704 MM_RefPhys( TMPMAPLVL4(i) & PADDR_MASK );
705 TMPMAPLVL4(i) |= PF_COW;
706 TMPMAPLVL4(i) &= ~PF_WRITE;
710 // #4 Map in kernel pages
711 for( i = 256; i < 512; i ++ )
714 // 320 0xFFFFA.... - Kernel Stacks
715 if( i == 320 ) continue;
716 // 509 0xFFFFFE0.. - Fractal mapping
717 if( i == 508 ) continue;
718 // 510 0xFFFFFE8.. - Temp fractal mapping
719 if( i == 509 ) continue;
721 TMPMAPLVL4(i) = PAGEMAPLVL4(i);
722 if( TMPMAPLVL4(i) & 1 )
723 MM_RefPhys( TMPMAPLVL4(i) & PADDR_MASK );
726 // #5 Set fractal mapping
727 TMPMAPLVL4(508) = ret | 3;
728 TMPMAPLVL4(509) = 0; // Temp
730 // #6 Create kernel stack (-1 to account for the guard)
732 for( i = 0; i < KERNEL_STACK_SIZE/0x1000-1; i ++ )
734 tPAddr phys = MM_AllocPhys();
736 MM_MapEx(kstackbase+i*0x1000, phys, 1, 0);
738 tmpmapping = MM_MapTemp(phys);
739 memcpy((void*)tmpmapping, (void*)(kstackbase+i*0x1000), 0x1000);
740 MM_FreeTemp(tmpmapping);
748 Mutex_Release(&glMM_TempFractalLock);
749 Log("MM_Clone: RETURN %P\n", ret);
753 void MM_ClearUser(void)
756 int pml4, pdpt, pd, pt;
758 for( pml4 = 0; pml4 < 256; pml4 ++ )
760 // Catch an un-allocated PML4 entry
761 if( !(PAGEMAPLVL4(pml4) & 1) ) {
762 addr += 1ULL << PML4_SHIFT;
767 if( (PAGEMAPLVL4(pml4) & PF_COW) ) {
768 addr += 1ULL << PML4_SHIFT;
775 for( pdpt = 0; pdpt < 512; pdpt ++ )
778 if( !(PAGEDIRPTR(addr >> PDP_SHIFT) & 1) ) {
779 addr += 1ULL << PDP_SHIFT;
784 if( (PAGEDIRPTR(addr >> PDP_SHIFT) & PF_COW) ) {
785 addr += 1ULL << PDP_SHIFT;
789 for( pd = 0; pd < 512; pd ++ )
791 // Unallocated PDir entry
792 if( !(PAGEDIR(addr >> PDIR_SHIFT) & 1) ) {
793 addr += 1ULL << PDIR_SHIFT;
798 if( PAGEDIR(addr >> PDIR_SHIFT) & PF_COW ) {
799 addr += 1ULL << PDIR_SHIFT;
803 // TODO: Catch large pages
806 for( pt = 0; pt < 512; pt ++ )
809 if( PAGETABLE(addr >> PTAB_SHIFT) & 1 ) {
810 MM_DerefPhys( PAGETABLE(addr >> PTAB_SHIFT) & PADDR_MASK );
811 PAGETABLE(addr >> PTAB_SHIFT) = 0;
817 MM_DerefPhys( PAGEDIR(addr >> PDIR_SHIFT) & PADDR_MASK );
818 PAGEDIR(addr >> PDIR_SHIFT) = 0;
821 // Free page directory
822 MM_DerefPhys( PAGEDIRPTR(addr >> PDP_SHIFT) & PADDR_MASK );
823 PAGEDIRPTR(addr >> PDP_SHIFT) = 0;
826 // Free page directory pointer table (PML4 entry)
827 MM_DerefPhys( PAGEMAPLVL4(pml4) & PADDR_MASK );
828 PAGEMAPLVL4(pml4) = 0;
832 tVAddr MM_NewWorkerStack(void)
837 // #1 Set temp fractal to PID0
838 Mutex_Acquire(&glMM_TempFractalLock);
839 TMPCR3() = ((tPAddr)gInitialPML4 - KERNEL_BASE) | 3;
841 // #2 Scan for a free stack addresss < 2^47
842 for(ret = 0x100000; ret < (1ULL << 47); ret += KERNEL_STACK_SIZE)
844 if( MM_GetPhysAddr(ret) == 0 ) break;
846 if( ret >= (1ULL << 47) ) {
847 Mutex_Release(&glMM_TempFractalLock);
851 // #3 Map all save the last page in the range
852 // - This acts as as guard page, and doesn't cost us anything.
853 for( i = 0; i < KERNEL_STACK_SIZE/0x1000 - 1; i ++ )
855 tPAddr phys = MM_AllocPhys();
858 Log_Error("MM", "MM_NewWorkerStack - Unable to allocate page");
861 MM_MapEx(ret + i*0x1000, phys, 1, 0);
864 Mutex_Release(&glMM_TempFractalLock);
866 return ret + i*0x1000;
870 * \brief Allocate a new kernel stack
872 tVAddr MM_NewKStack(void)
874 tVAddr base = MM_KSTACK_BASE;
876 for( ; base < MM_KSTACK_TOP; base += KERNEL_STACK_SIZE )
878 if(MM_GetPhysAddr(base) != 0)
881 //Log("MM_NewKStack: Found one at %p", base + KERNEL_STACK_SIZE);
882 for( i = 0; i < KERNEL_STACK_SIZE; i += 0x1000)
884 if( !MM_Allocate(base+i) )
886 Log_Warning("MM", "MM_NewKStack - Allocation failed");
887 for( i -= 0x1000; i; i -= 0x1000)
888 MM_Deallocate(base+i);
893 return base + KERNEL_STACK_SIZE;
895 Log_Warning("MM", "MM_NewKStack - No address space left\n");