+ MM_DumpTables(0, -1L);
+}
+
+void MM_FinishVirtualInit(void)
+{
+}
+
+/**
+ * \brief Called on a page fault
+ */
+void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
+{
+ // TODO: Copy on Write
+ #if 0
+ if( gaPageDir [Addr>>22] & PF_PRESENT
+ && gaPageTable[Addr>>12] & PF_PRESENT
+ && gaPageTable[Addr>>12] & PF_COW )
+ {
+ tPAddr paddr;
+ if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
+ {
+ gaPageTable[Addr>>12] &= ~PF_COW;
+ gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
+ }
+ else
+ {
+ //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
+ paddr = MM_DuplicatePage( Addr );
+ MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
+ gaPageTable[Addr>>12] &= PF_USER;
+ gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
+ }
+
+ INVLPG( Addr & ~0xFFF );
+ return;
+ }
+ #endif
+
+ // If it was a user, tell the thread handler
+ if(ErrorCode & 4) {
+ Warning("%s %s %s memory%s",
+ (ErrorCode&4?"User":"Kernel"),
+ (ErrorCode&2?"write to":"read from"),
+ (ErrorCode&1?"bad/locked":"non-present"),
+ (ErrorCode&16?" (Instruction Fetch)":"")
+ );
+ Warning("User Pagefault: Instruction at %04x:%08x accessed %p",
+ Regs->CS, Regs->RIP, Addr);
+ __asm__ __volatile__ ("sti"); // Restart IRQs
+// Threads_SegFault(Addr);
+ return ;
+ }
+
+ // Kernel #PF
+ Debug_KernelPanic();
+ // -- Check Error Code --
+ if(ErrorCode & 8)
+ Warning("Reserved Bits Trashed!");
+ else
+ {
+ Warning("%s %s %s memory%s",
+ (ErrorCode&4?"User":"Kernel"),
+ (ErrorCode&2?"write to":"read from"),
+ (ErrorCode&1?"bad/locked":"non-present"),
+ (ErrorCode&16?" (Instruction Fetch)":"")
+ );
+ }
+
+ Log("Code at %p accessed %p", Regs->RIP, Addr);
+ // Print Stack Backtrace
+// Error_Backtrace(Regs->RIP, Regs->RBP);
+
+ MM_DumpTables(0, -1);
+
+ __asm__ __volatile__ ("cli");
+ for( ;; )
+ HALT();
+}
+
+/**
+ * \brief Dumps the layout of the page tables
+ */
+void MM_DumpTables(tVAddr Start, tVAddr End)
+{
+ const tPAddr CHANGEABLE_BITS = 0xFF8;
+ const tPAddr MASK = ~CHANGEABLE_BITS; // Physical address and access bits
+ tVAddr rangeStart = 0;
+ tPAddr expected = CHANGEABLE_BITS; // MASK is used because it's not a vaild value
+ tVAddr curPos;
+ Uint page;
+
+ Log("Table Entries: (%p to %p)", Start, End);
+
+ End &= (1L << 48) - 1;
+
+ Start >>= 12; End >>= 12;
+
+ 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)
+ || !(PAGEDIRPTR(page>>18) & PF_PRESENT)
+ || !(PAGEDIR(page>>9) & PF_PRESENT)
+ || !(PAGETABLE(page) & PF_PRESENT)
+ || (PAGETABLE(page) & MASK) != expected)
+ {
+ if(expected != CHANGEABLE_BITS) {
+ Log("%016x-0x%016x => %013x-%013x (%c%c%c%c)",
+ rangeStart, curPos - 1,
+ PAGETABLE(rangeStart>>12) & ~0xFFF,
+ (expected & ~0xFFF) - 1,
+ (expected & PF_PAGED ? 'p' : '-'),
+ (expected & PF_COW ? 'C' : '-'),
+ (expected & PF_USER ? 'U' : '-'),
+ (expected & PF_WRITE ? 'W' : '-')
+ );
+ expected = CHANGEABLE_BITS;
+ }
+ 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;
+
+ expected = (PAGETABLE(page) & MASK);
+ rangeStart = curPos;
+ }
+ if(expected != CHANGEABLE_BITS)
+ expected += 0x1000;
+ }