#define AP_KRW_ONLY 1 // Kernel page
#define AP_KRO_ONLY 5 // Kernel RO page
#define AP_RW_BOTH 3 // Standard RW
-#define AP_RO_BOTH 6 // COW Page
+#define AP_RO_BOTH 7 // COW Page
#define AP_RO_USER 2 // User RO Page
#define PADDR_MASK_LVL1 0xFFFFFC00
tVAddr MM_NewKStack(int bGlobal);
void MM_int_DumpTableEnt(tVAddr Start, size_t Len, tMM_PageInfo *Info);
//void MM_DumpTables(tVAddr Start, tVAddr End);
+void MM_PageFault(Uint32 PC, Uint32 Addr, Uint32 DFSR, int bPrefetch);
// === GLOBALS ===
tPAddr giMM_ZeroPage;
desc[3] = desc[0] + 0xC00;
if( VAddr < 0x80000000 ) {
-// Log("USRFRACTAL(%p) = %p", VAddr, &USRFRACTAL(VAddr));
- USRFRACTAL(VAddr) = paddr | 3;
+ USRFRACTAL(VAddr) = paddr | 0x13;
}
else {
-// Log("FRACTAL(%p) = %p", VAddr, &FRACTAL(table1, VAddr));
- FRACTAL(table1, VAddr) = paddr | 3;
+ FRACTAL(table1, VAddr) = paddr | 0x13;
}
// TLBIALL
*desc = (pi->PhysAddr & 0xFFFFF000) | 2;
if(!pi->bExecutable) *desc |= 1; // XN
- if(!pi->bGlobal) *desc |= 1 << 11; // NG
+ if(!pi->bGlobal) *desc |= 1 << 11; // nG
if( pi->bShared) *desc |= 1 << 10; // S
*desc |= (pi->AP & 3) << 4; // AP
*desc |= ((pi->AP >> 2) & 1) << 9; // APX
else
{
// Large page
- // TODO:
Log_Warning("MMVirt", "TODO: Implement large pages in MM_int_SetPageInfo");
}
break;
case 20: // Section or unmapped
- Warning("TODO: Implement sections");
+ Log_Warning("MMVirt", "TODO: Implement sections in MM_int_SetPageInfo");
break;
case 24: // Supersection
// Error if not aligned
{
if( pi->PhysAddr == 0 ) {
*desc = 0;
- // TODO: Apply to all entries
- LEAVE('i', 0);
- return 0;
}
- // Apply
- *desc = pi->PhysAddr & 0xFF000000;
-// *desc |= ((pi->PhysAddr >> 32) & 0xF) << 20;
-// *desc |= ((pi->PhysAddr >> 36) & 0x7) << 5;
- *desc |= 2 | (1 << 18);
+ else {
+ // Apply
+ *desc = pi->PhysAddr & 0xFF000000;
+// *desc |= ((pi->PhysAddr >> 32) & 0xF) << 20;
+// *desc |= ((pi->PhysAddr >> 36) & 0x7) << 5;
+ *desc |= 2 | (1 << 18);
+ }
// TODO: Apply to all entries
+ Log_Warning("MMVirt", "TODO: Apply changes to all entries of supersections");
LEAVE('i', 0);
return 0;
}
// TODO: What here?
+ Log_Warning("MMVirt", "TODO: 24-bit not on supersection?");
LEAVE('i', 1);
return 1;
}
pi.AP = AP_RW_BOTH;
else
pi.AP = AP_KRW_ONLY;
- pi.bExecutable = 1;
+ pi.bExecutable = 0;
if( MM_int_SetPageInfo(VAddr, &pi) ) {
MM_DerefPhys(pi.PhysAddr);
LEAVE('i', 0);
table = MM_AllocPhys();
if(!table) return ;
+
+ cur += 256*Table;
tmp_map = (void*)MM_MapTemp(table);
for( i = 0; i < 1024; i ++ )
{
// Log_Debug("MMVirt", "cur[%i] (%p) = %x", Table*256+i, &cur[Table*256+i], cur[Table*256+i]);
- switch(cur[Table*256+i] & 3)
+ switch(cur[i] & 3)
{
case 0: tmp_map[i] = 0; break;
case 1:
case 3:
// Small page
// - If full RW
- if( (cur[Table*256] & 0x230) == 0x030 )
- cur[Table*256+i] |= 0x200; // Set to full RO (Full RO=COW, User RO = RO)
- tmp_map[i] = cur[Table*256+i];
+ Debug("%p cur[%i] & 0x230 = 0x%x", Table*256*0x1000, i, cur[i] & 0x230);
+ if( (cur[i] & 0x230) == 0x030 )
+ cur[i] |= 0x200; // Set to full RO (Full RO=COW, User RO = RO)
+ tmp_map[i] = cur[i];
break;
}
}
+ MM_FreeTemp( (tVAddr) tmp_map );
DestEnt[0] = table + 0*0x400 + 1;
DestEnt[1] = table + 1*0x400 + 1;
void *tmp_page;
page = MM_AllocPhys();
+ Log("page = %P", page);
table[j] = page | 0x813;
tmp_page = (void*)MM_MapTemp(page);
return ret;
}
-tPAddr MM_ClearUser(void)
+void MM_ClearUser(void)
{
- // TODO: Implement ClearUser
- return 0;
+ int i, j;
+ Uint32 *cur = (void*)MM_TABLE0USER;
+ Uint32 *tab;
+
+// MM_DumpTables(0, 0x80000000);
+
+ for( i = 0; i < 0x800-4; i ++ )
+ {
+ switch( cur[i] & 3 )
+ {
+ case 0: break; // Already unmapped
+ case 1: // Sub pages
+ tab = (void*)(MM_TABLE1USER + i*256*sizeof(Uint32));
+ for( j = 0; j < 1024; j ++ )
+ {
+ switch( tab[j] & 3 )
+ {
+ case 0: break; // Unmapped
+ case 1:
+ Log_Error("MMVirt", "TODO: Support large pages in MM_ClearUser");
+ break;
+ case 2:
+ case 3:
+ MM_DerefPhys( tab[j] & ~(PAGE_SIZE-1) );
+ break;
+ }
+ }
+ MM_DerefPhys( cur[i] & ~(PAGE_SIZE-1) );
+ cur[i+0] = 0;
+ cur[i+1] = 0;
+ cur[i+2] = 0;
+ i += 3;
+ break;
+ case 2:
+ case 3:
+ Log_Error("MMVirt", "TODO: Implement sections/supersections in MM_ClearUser");
+ break;
+ }
+ cur[i] = 0;
+ }
+
+ // Clear out unused stacks
+ {
+ register Uint32 __SP asm("sp");
+ int cur_stack_base = ((__SP & ~(MM_KSTACK_SIZE-1)) / PAGE_SIZE) % 1024;
+
+ tab = (void*)(MM_TABLE1USER + i*256*sizeof(Uint32));
+
+ // First 512 is the Table1 mapping + 2 for Table0 mapping
+ for( j = 512+2; j < 1024; j ++ )
+ {
+ // Skip current stack
+ if( j == cur_stack_base ) {
+ j += (MM_KSTACK_SIZE / PAGE_SIZE) - 1;
+ continue ;
+ }
+ if( !(tab[j] & 3) ) continue;
+ ASSERT( (tab[j] & 3) == 2 );
+ MM_DerefPhys( tab[j] & ~(PAGE_SIZE) );
+ tab[j] = 0;
+ }
+ }
+
+
+ MM_DumpTables(0, 0x80000000);
+// Log_KernelPanic("MMVirt", "TODO: Implement MM_ClearUser");
}
tVAddr MM_MapTemp(tPAddr PAddr)
if( MM_int_GetPageInfo(ret, &pi) == 0 )
continue;
-// Log("MapTemp %P at %p", PAddr, ret);
+ Log("MapTemp %P at %p by %p", PAddr, ret, __builtin_return_address(0));
MM_RefPhys(PAddr); // Counter the MM_Deallocate in FreeTemp
MM_Map(ret, PAddr);
void MM_FreeTemp(tVAddr VAddr)
{
- // TODO: Implement FreeTemp
if( VAddr < MM_TMPMAP_BASE || VAddr >= MM_TMPMAP_END ) {
Log_Warning("MMVirt", "MM_FreeTemp: Passed an addr not from MM_MapTemp (%p)", VAddr);
return ;
range_start = Start;
for( addr = Start; i == 0 || (addr && addr < End); i = 1 )
{
+ int rv;
// Log("addr = %p", addr);
- int rv = MM_int_GetPageInfo(addr, &pi);
+ rv = MM_int_GetPageInfo(addr, &pi);
if( rv
|| pi.Size != pi_old.Size
|| pi.Domain != pi_old.Domain
Debug("Done");
}
+// NOTE: Runs in abort context, not much differe, just a smaller stack
+void MM_PageFault(Uint32 PC, Uint32 Addr, Uint32 DFSR, int bPrefetch)
+{
+ int rv;
+ tMM_PageInfo pi;
+
+ rv = MM_int_GetPageInfo(Addr, &pi);
+
+ // Check for COW
+ if( rv == 0 && pi.AP == AP_RO_BOTH )
+ {
+ Log_Notice("MMVirt", "COW %p caused by %p", Addr, PC);
+ pi.AP = AP_RW_BOTH;
+ if( MM_GetRefCount(pi.PhysAddr) > 1 )
+ {
+ // Duplicate the page
+ tPAddr newpage;
+ void *dst, *src;
+
+ newpage = MM_AllocPhys();
+ if(!newpage) {
+ Log_Error("MMVirt", "Unable to allocate new page for COW");
+ for(;;);
+ }
+ dst = (void*)MM_MapTemp(newpage);
+ src = (void*)(Addr & ~(PAGE_SIZE-1));
+ memcpy( dst, src, PAGE_SIZE );
+ MM_FreeTemp( (tVAddr)dst );
+
+ pi.PhysAddr = newpage;
+ }
+ // Unset COW
+ pi.AP = AP_RW_BOTH;
+ MM_int_SetPageInfo(Addr, &pi);
+ return ;
+ }
+
+
+ Log_Error("MMVirt", "Code at %p accessed %p (DFSR = 0x%x)%s", PC, Addr, DFSR,
+ (bPrefetch ? " - Prefetch" : "")
+ );
+ for(;;);
+}
+