#include <mm_virt.h>
#include <hal_proc.h>
-#define AP_KRW_ONLY 0x1
-#define AP_KRO_ONLY 0x5
-#define AP_RW_BOTH 0x3
-#define AP_RO_BOTH 0x6
+#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_USER 2 // User RO Page
#define PADDR_MASK_LVL1 0xFFFFFC00
// === IMPORTS ===
//#define FRACTAL(table1, addr) ((table1)[ (0xFF8/4*1024) + ((addr)>>20)])
#define FRACTAL(table1, addr) ((table1)[ (0xFF8/4*1024) + ((addr)>>22)])
-#define USRFRACTAL(table1, addr) ((table1)[ (0x7F8/4*1024) + ((addr)>>22)])
+#define USRFRACTAL(addr) (*((Uint32*)(0x7FDFF000) + ((addr)>>22)))
#define TLBIALL() __asm__ __volatile__ ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0))
+#define TLBIMVA(addr) __asm__ __volatile__ ("mcr p15, 0, %0, c8, c7, 1" : : "r" (addr))
// === PROTOTYPES ===
void MM_int_GetTables(tVAddr VAddr, Uint32 **Table0, Uint32 **Table1);
int MM_int_AllocateCoarse(tVAddr VAddr, int Domain);
int MM_int_SetPageInfo(tVAddr VAddr, tMM_PageInfo *pi);
int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi);
+tVAddr MM_NewUserStack(void);
+tPAddr MM_AllocateZero(tVAddr VAddr);
tPAddr MM_AllocateRootTable(void);
void MM_int_CloneTable(Uint32 *DestEnt, int Table);
tPAddr MM_Clone(void);
//void MM_DumpTables(tVAddr Start, tVAddr End);
// === GLOBALS ===
+tPAddr giMM_ZeroPage;
// === CODE ===
int MM_InitialiseVirtual(void)
desc[2] = desc[0] + 0x800;
desc[3] = desc[0] + 0xC00;
- FRACTAL(table1, VAddr) = paddr | 3;
+ if( VAddr < 0x80000000 ) {
+// Log("USRFRACTAL(%p) = %p", VAddr, &USRFRACTAL(VAddr));
+ USRFRACTAL(VAddr) = paddr | 3;
+ }
+ else {
+// Log("FRACTAL(%p) = %p", VAddr, &FRACTAL(table1, VAddr));
+ FRACTAL(table1, VAddr) = paddr | 3;
+ }
// TLBIALL
TLBIALL();
Uint32 *table0, *table1;
Uint32 *desc;
- ENTER("pVADdr ppi", VAddr, pi);
+ ENTER("pVAddr ppi", VAddr, pi);
MM_int_GetTables(VAddr, &table0, &table1);
if( pi->bShared) *desc |= 1 << 10; // S
*desc |= (pi->AP & 3) << 4; // AP
*desc |= ((pi->AP >> 2) & 1) << 9; // APX
+ TLBIMVA(VAddr & 0xFFFFF000);
LEAVE('i', 0);
return 0;
}
{
// Large page
// TODO:
+ Log_Warning("MMVirt", "TODO: Implement large pages in MM_int_SetPageInfo");
}
break;
case 20: // Section or unmapped
pi->AP = ((desc >> 4) & 3) | (((desc >> 9) & 1) << 2);
pi->bExecutable = !(desc & 0x8000);
pi->bShared = (desc >> 10) & 1;
-// LogF("Large page, VAddr = %p, table1[VAddr>>12] = %p, desc = %x\n", VAddr, &table1[ VAddr >> 12 ], desc);
-// LogF("Par desc = %p %x\n", &table0[ VAddr >> 20 ], table0[ VAddr >> 20 ]);
return 0;
// 2/3: Small page
case 2:
switch(pi.AP)
{
+ case 0:
+ break;
case AP_KRW_ONLY:
ret |= MM_PFLAG_KERNEL;
break;
case AP_RW_BOTH:
break;
case AP_RO_BOTH:
+ ret |= MM_PFLAG_COW;
+ break;
+ case AP_RO_USER:
ret |= MM_PFLAG_RO;
break;
}
void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
{
tMM_PageInfo pi;
+ Uint curFlags;
+
if( MM_int_GetPageInfo(VAddr, &pi) )
- return;
+ return ;
+
+ curFlags = MM_GetPhysAddr(VAddr);
+ if( (curFlags & Mask) == Flags )
+ return ;
+ curFlags &= ~Mask;
+ curFlags |= Flags;
+
+ if( curFlags & MM_PFLAG_COW )
+ pi.AP = AP_RO_BOTH;
+ else
+ {
+ switch(curFlags & (MM_PFLAG_KERNEL|MM_PFLAG_RO) )
+ {
+ case 0:
+ pi.AP = AP_RW_BOTH; break;
+ case MM_PFLAG_KERNEL:
+ pi.AP = AP_KRW_ONLY; break;
+ case MM_PFLAG_RO:
+ pi.AP = AP_RO_USER; break;
+ case MM_PFLAG_KERNEL|MM_PFLAG_RO:
+ pi.AP = AP_KRO_ONLY; break;
+ }
+ }
+
+ pi.bExecutable = !!(curFlags & MM_PFLAG_EXEC);
+
+ MM_int_SetPageInfo(VAddr, &pi);
}
int MM_Map(tVAddr VAddr, tPAddr PAddr)
{
tMM_PageInfo pi = {0};
+// Log("MM_Map %P=>%p", PAddr, VAddr);
+
pi.PhysAddr = PAddr;
pi.Size = 12;
- pi.AP = AP_KRW_ONLY; // Kernel Read/Write
+ if(VAddr < USER_STACK_TOP)
+ pi.AP = AP_RW_BOTH;
+ else
+ pi.AP = AP_KRW_ONLY; // Kernel Read/Write
pi.bExecutable = 1;
if( MM_int_SetPageInfo(VAddr, &pi) ) {
MM_DerefPhys(pi.PhysAddr);
pi.PhysAddr = MM_AllocPhys();
if( pi.PhysAddr == 0 ) LEAVE_RET('i', 0);
pi.Size = 12;
- pi.AP = AP_KRW_ONLY; // Kernel Read/Write
+ if(VAddr < USER_STACK_TOP)
+ pi.AP = AP_RW_BOTH;
+ else
+ pi.AP = AP_KRW_ONLY;
pi.bExecutable = 1;
if( MM_int_SetPageInfo(VAddr, &pi) ) {
MM_DerefPhys(pi.PhysAddr);
return pi.PhysAddr;
}
+tPAddr MM_AllocateZero(tVAddr VAddr)
+{
+ if( !giMM_ZeroPage ) {
+ giMM_ZeroPage = MM_Allocate(VAddr);
+ MM_RefPhys(giMM_ZeroPage);
+ memset((void*)VAddr, 0, PAGE_SIZE);
+ }
+ else {
+ MM_RefPhys(giMM_ZeroPage);
+ MM_Map(VAddr, giMM_ZeroPage);
+ }
+ MM_SetFlags(VAddr, MM_PFLAG_COW, MM_PFLAG_COW);
+ return giMM_ZeroPage;
+}
+
void MM_Deallocate(tVAddr VAddr)
{
tMM_PageInfo pi;
if( ret & 0x1000 ) {
MM_DerefPhys(ret);
ret += 0x1000;
+// Log("MM_AllocateRootTable: Second try not aligned, %P", ret);
}
else {
MM_DerefPhys(ret + 0x2000);
+// Log("MM_AllocateRootTable: Second try aligned, %P", ret);
}
}
+// else
+// Log("MM_AllocateRootTable: Got it in one, %P", ret);
return ret;
}
new_lvl1_1 = (void*)MM_MapTemp(ret);
new_lvl1_2 = (void*)MM_MapTemp(ret+0x1000);
tmp_map = new_lvl1_1;
- new_lvl1_1[0] = 0x8202; // Section mapping the first meg for exception vectors (K-RO)
- for( i = 1; i < 0x800-4; i ++ )
+ for( i = 0; i < 0x800-4; i ++ )
{
// Log("i = %i", i);
- if( i == 0x400 ) {
+ if( i == 0x400 )
tmp_map = &new_lvl1_2[-0x400];
- Log("tmp_map = %p", tmp_map);
- }
switch( cur[i] & 3 )
{
case 0: tmp_map[i] = 0; break;
Uint32 *table = (void*)MM_MapTemp(tmp);
Uint32 sp;
register Uint32 __SP asm("sp");
+
// Map table to last 4MiB of user space
- tmp_map[i+0] = tmp + 0*0x400 + 1;
- tmp_map[i+1] = tmp + 1*0x400 + 1;
- tmp_map[i+2] = tmp + 2*0x400 + 1;
- tmp_map[i+3] = tmp + 3*0x400 + 1;
- for( j = 0; j < 256; j ++ ) {
- table[j] = new_lvl1_1[j*4] & PADDR_MASK_LVL1;// 0xFFFFFC00;
- table[j] |= 0x10|3; // Kernel Only, Small table, XN
- }
- for( ; j < 512; j ++ ) {
- table[j] = new_lvl1_2[(j-256)*4] & PADDR_MASK_LVL1;// 0xFFFFFC00;
- table[j] |= 0x10|3; // Kernel Only, Small table, XN
+ new_lvl1_2[0x3FC] = tmp + 0*0x400 + 1;
+ new_lvl1_2[0x3FD] = tmp + 1*0x400 + 1;
+ new_lvl1_2[0x3FE] = tmp + 2*0x400 + 1;
+ new_lvl1_2[0x3FF] = tmp + 3*0x400 + 1;
+
+ tmp_map = new_lvl1_1;
+ for( j = 0; j < 512; j ++ )
+ {
+ if( j == 256 )
+ tmp_map = &new_lvl1_2[-0x400];
+ if( (tmp_map[j*4] & 3) == 1 )
+ {
+ table[j] = tmp_map[j*4] & PADDR_MASK_LVL1;// 0xFFFFFC00;
+ table[j] |= 0x813; // nG, Kernel Only, Small page, XN
+ }
+ else
+ table[j] = 0;
}
+ // Fractal
+ table[j++] = (ret + 0x0000) | 0x813;
+ table[j++] = (ret + 0x1000) | 0x813;
+ // Nuke the rest
for( ; j < 1024; j ++ )
table[j] = 0;
// Get kernel stack bottom
- sp = __SP;
- sp &= ~(MM_KSTACK_SIZE-1);
+ sp = __SP & ~(MM_KSTACK_SIZE-1);
j = (sp / 0x1000) % 1024;
num = MM_KSTACK_SIZE/0x1000;
- Log("sp = %p, j = %i", sp, j);
// Copy stack pages
for(; num--; j ++, sp += 0x1000)
{
- tVAddr page = MM_AllocPhys();
+ tVAddr page;
void *tmp_page;
- table[j] = page | 0x13;
+
+ page = MM_AllocPhys();
+ table[j] = page | 0x813;
+
tmp_page = (void*)MM_MapTemp(page);
memcpy(tmp_page, (void*)sp, 0x1000);
- MM_FreeTemp( (tVAddr)tmp_page );
+ MM_FreeTemp( (tVAddr) tmp_page );
}
-
+
MM_FreeTemp( (tVAddr)table );
}
- tmp_map = &tmp_map[0x400];
- MM_FreeTemp( (tVAddr)tmp_map );
-
- Log("Table dump");
- MM_DumpTables(0, -1);
+ MM_FreeTemp( (tVAddr)new_lvl1_1 );
+ MM_FreeTemp( (tVAddr)new_lvl1_2 );
return ret;
}
{
tVAddr ret;
tMM_PageInfo pi;
-
+
for( ret = MM_TMPMAP_BASE; ret < MM_TMPMAP_END - PAGE_SIZE; ret += PAGE_SIZE )
{
if( MM_int_GetPageInfo(ret, &pi) == 0 )
continue;
-
+
+// Log("MapTemp %P at %p", PAddr, ret);
+ MM_RefPhys(PAddr); // Counter the MM_Deallocate in FreeTemp
MM_Map(ret, PAddr);
return ret;
return addr + ofs;
}
+tVAddr MM_NewUserStack(void)
+{
+ tVAddr addr, ofs;
+
+ addr = USER_STACK_TOP - USER_STACK_SIZE;
+ if( MM_GetPhysAddr(addr + PAGE_SIZE) ) {
+ Log_Error("MMVirt", "Unable to create initial user stack, addr %p taken",
+ addr + PAGE_SIZE
+ );
+ return 0;
+ }
+
+ // 1 guard page
+ for( ofs = PAGE_SIZE; ofs < USER_STACK_SIZE; ofs += PAGE_SIZE )
+ {
+ tPAddr rv;
+ if(ofs >= USER_STACK_SIZE - USER_STACK_COMM)
+ rv = MM_Allocate(addr + ofs);
+ else
+ rv = MM_AllocateZero(addr + ofs);
+ if(rv == 0)
+ {
+ while(ofs)
+ {
+ ofs -= PAGE_SIZE;
+ MM_Deallocate(addr + ofs);
+ }
+ Log_Warning("MMVirt", "MM_NewUserStack: Unable to allocate");
+ return 0;
+ }
+ MM_SetFlags(addr+ofs, 0, MM_PFLAG_KERNEL);
+ }
+ Log("Return %p", addr + ofs);
+ MM_DumpTables(0, 0x80000000);
+ return addr + ofs;
+}
+
void MM_int_DumpTableEnt(tVAddr Start, size_t Len, tMM_PageInfo *Info)
{
- Log("%p => %8x - 0x%7x %i %x",
- Start, Info->PhysAddr-Len, Len,
- Info->Domain,
- Info->AP
- );
+ if( giMM_ZeroPage && Info->PhysAddr == giMM_ZeroPage )
+ {
+ Log("%p => %8s - 0x%7x %i %x",
+ Start, "ZERO", Len,
+ Info->Domain, Info->AP
+ );
+ }
+ else
+ {
+ Log("%p => %8x - 0x%7x %i %x",
+ Start, Info->PhysAddr-Len, Len,
+ Info->Domain, Info->AP
+ );
+ }
}
void MM_DumpTables(tVAddr Start, tVAddr End)
pi_old.Size = 0;
+ Log("Page Table Dump:");
range_start = Start;
for( addr = Start; i == 0 || (addr && addr < End); i = 1 )
{
+// Log("addr = %p", addr);
int rv = MM_int_GetPageInfo(addr, &pi);
if( rv
|| pi.Size != pi_old.Size
}
pi_old = pi;
- pi_old.PhysAddr += 1 << pi_old.Size;
+ // Handle the zero page
+ if( !giMM_ZeroPage || pi_old.Size != 12 || pi_old.PhysAddr != giMM_ZeroPage )
+ pi_old.PhysAddr += 1 << pi_old.Size;
addr += 1 << pi_old.Size;
inRange = (rv == 0);
}
if(inRange)
MM_int_DumpTableEnt(range_start, addr - range_start, &pi);
+ Log("Done");
}