*
* Virtual Memory Manager
*/
+#define DEBUG 0
#include <acess.h>
#include <mm_virt.h>
#include <proc.h>
// === CODE ===
void MM_InitVirt(void)
{
+ MM_DumpTables(0, -1L);
}
void MM_FinishVirtualInit(void)
// Error_Backtrace(Regs->RIP, Regs->RBP);
MM_DumpTables(0, -1);
+
+ __asm__ __volatile__ ("cli");
+ for( ;; )
+ HALT();
}
/**
tVAddr curPos;
Uint page;
+ Log("Table Entries: (%p to %p)", Start, End);
+
End &= (1L << 48) - 1;
Start >>= 12; End >>= 12;
- Log("Table Entries:");
for(page = Start, curPos = Start<<12;
page < End;
curPos += 0x1000, page++)
if( curPos == 0x800000000000L )
curPos = 0xFFFF800000000000L;
+ //Debug("&PAGEMAPLVL4(%i page>>27) = %p", page>>27, &PAGEMAPLVL4(page>>27));
+ //Debug("&PAGEDIRPTR(%i page>>18) = %p", page>>18, &PAGEDIRPTR(page>>18));
+ //Debug("&PAGEDIR(%i page>>9) = %p", page>>9, &PAGEDIR(page>>9));
+ //Debug("&PAGETABLE(%i page) = %p", page, &PAGETABLE(page));
+
// End of a range
if(
!(PAGEMAPLVL4(page>>27) & PF_PRESENT)
if( !(PAGEMAPLVL4(page>>27) & PF_PRESENT) ) {
page += (1 << 27) - 1;
curPos += (1L << 39) - 0x1000;
+ //Debug("pml4 ent unset (page = 0x%x now)", page);
continue;
}
if( !(PAGEDIRPTR(page>>18) & PF_PRESENT) ) {
page += (1 << 18) - 1;
curPos += (1L << 30) - 0x1000;
+ //Debug("pdp ent unset (page = 0x%x now)", page);
continue;
}
if( !(PAGEDIR(page>>9) & PF_PRESENT) ) {
page += (1 << 9) - 1;
curPos += (1L << 21) - 0x1000;
+ //Debug("pd ent unset (page = 0x%x now)", page);
continue;
}
if( !(PAGETABLE(page) & PF_PRESENT) ) continue;
{
tPAddr tmp;
- Log("MM_Map: (VAddr=0x%x, PAddr=0x%x)", VAddr, PAddr);
+ ENTER("xVAddr xPAddr", VAddr, PAddr);
// Check PML4
//Log(" MM_Map: &PAGEMAPLVL4(%x) = %x", VAddr >> 39, &PAGEMAPLVL4(VAddr >> 39));
PAGETABLE(VAddr >> PTAB_SHIFT) = PAddr | 3;
INVLPG( VAddr );
- Log("MM_Map: RETURN 1");
-
+
+ LEAVE('i', 1);
return 1;
}
{
tPAddr ret;
- Log("MM_Allocate: (VAddr=%x)", VAddr);
- Log("MM_Allocate: MM_AllocPhys()");
+ ENTER("xVAddr", VAddr);
+
+ // NOTE: This is hack, but I like my dumps to be neat
+ #if 1
+ if( !MM_Map(VAddr, 0) ) // Make sure things are allocated
+ {
+ Warning("MM_Allocate: Unable to map, tables did not initialise");
+ LEAVE('i', 0);
+ return 0;
+ }
+ MM_Unmap(VAddr);
+ #endif
+
ret = MM_AllocPhys();
- Log("MM_Allocate: ret = %x", ret);
- if(!ret) return 0;
+ LOG("ret = %x", ret);
+ if(!ret) {
+ LEAVE('i', 0);
+ return 0;
+ }
if( !MM_Map(VAddr, ret) )
{
- Warning("MM_Allocate: Unable to map", ret);
+ Warning("MM_Allocate: Unable to map. Strange, we should have errored earlier");
MM_DerefPhys(ret);
+ LEAVE('i');
return 0;
}
+ LEAVE('x', ret);
return ret;
}
*/
tPAddr MM_GetPhysAddr(tVAddr Addr)
{
- Log("MM_GetPhysAddr: (Addr=0x%x)", Addr);
if( !(PAGEMAPLVL4(Addr >> 39) & 1) )
return 0;
- Log(" MM_GetPhysAddr: PDP Valid");
if( !(PAGEDIRPTR(Addr >> 30) & 1) )
return 0;
- Log(" MM_GetPhysAddr: PD Valid");
if( !(PAGEDIR(Addr >> 21) & 1) )
return 0;
- Log(" MM_GetPhysAddr: PT Valid");
if( !(PAGETABLE(Addr >> PTAB_SHIFT) & 1) )
return 0;
- Log(" MM_GetPhysAddr: Page Valid");
return (PAGETABLE(Addr >> PTAB_SHIFT) & ~0xFFF) | (Addr & 0xFFF);
}
ret = MM_AllocPhys();
if(!ret) return 0;
+ Log_KernelPanic("MM", "TODO: Implement MM_Clone");
+
// #2 Alter the fractal pointer
// #3 Set Copy-On-Write to all user pages
// #4 Return
tVAddr MM_NewWorkerStack(void)
{
+ Log_KernelPanic("MM", "TODO: Implement MM_NewWorkerStack");
return 0;
}
+/**
+ * \brief Allocate a new kernel stack
+ */
tVAddr MM_NewKStack(void)
{
+ tVAddr base = MM_KSTACK_BASE;
+ Uint i;
+ for( ; base < MM_KSTACK_TOP; base += KERNEL_STACK_SIZE )
+ {
+ if(MM_GetPhysAddr(base) != 0)
+ continue;
+
+ Log("MM_NewKStack: Found one at %p", base + KERNEL_STACK_SIZE);
+ for( i = 0; i < KERNEL_STACK_SIZE; i += 0x1000)
+ MM_Allocate(base+i);
+
+ return base + KERNEL_STACK_SIZE;
+ }
+ Log_Warning("MM", "MM_NewKStack - No address space left\n");
return 0;
}