4 * ARM7 Virtual Memory Manager
5 * - arch/arm7/mm_virt.c
12 #define AP_KRW_ONLY 0x1
13 #define AP_KRO_ONLY 0x5
14 #define AP_RW_BOTH 0x3
15 #define AP_RO_BOTH 0x6
18 extern Uint32 kernel_table0[];
32 //#define FRACTAL(table1, addr) ((table1)[ (0xFF8/4*1024) + ((addr)>>20)])
33 #define FRACTAL(table1, addr) ((table1)[ (0xFF8/4*1024) + ((addr)>>22)])
34 #define TLBIALL() __asm__ __volatile__ ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0))
37 void MM_int_GetTables(tVAddr VAddr, Uint32 **Table0, Uint32 **Table1);
38 int MM_int_AllocateCoarse(tVAddr VAddr, int Domain);
39 int MM_int_SetPageInfo(tVAddr VAddr, tMM_PageInfo *pi);
40 int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi);
45 int MM_InitialiseVirtual(void)
50 void MM_int_GetTables(tVAddr VAddr, Uint32 **Table0, Uint32 **Table1)
52 if(VAddr & 0x80000000) {
53 *Table0 = (void*)&kernel_table0; // Level 0
54 *Table1 = (void*)MM_TABLE1KERN; // Level 1
57 *Table0 = (void*)MM_TABLE0USER;
58 *Table1 = (void*)MM_TABLE1USER;
62 int MM_int_AllocateCoarse(tVAddr VAddr, int Domain)
64 Uint32 *table0, *table1;
68 ENTER("xVAddr iDomain", VAddr, Domain);
70 MM_int_GetTables(VAddr, &table0, &table1);
72 VAddr &= ~(0x400000-1); // 4MiB per "block", 1 Page
74 desc = &table0[ VAddr>>20];
75 LOG("desc = %p", desc);
77 // table0: 4 bytes = 1 MiB
79 LOG("desc[0] = %x", desc[0]);
80 LOG("desc[1] = %x", desc[1]);
81 LOG("desc[2] = %x", desc[2]);
82 LOG("desc[3] = %x", desc[3]);
84 if( (desc[0] & 3) != 0 || (desc[1] & 3) != 0
85 || (desc[2] & 3) != 0 || (desc[3] & 3) != 0 )
92 paddr = MM_AllocPhys();
100 *desc = paddr | (Domain << 5) | 1;
101 desc[1] = desc[0] + 0x400;
102 desc[2] = desc[0] + 0x800;
103 desc[3] = desc[0] + 0xC00;
105 Log("FRACTAL(%p, %p) = %p", table1, VAddr, &FRACTAL(table1, VAddr));
106 FRACTAL(table1, VAddr) = paddr | 3;
115 int MM_int_SetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
117 Uint32 *table0, *table1;
120 ENTER("pVADdr ppi", VAddr, pi);
122 MM_int_GetTables(VAddr, &table0, &table1);
124 desc = &table0[ VAddr >> 20 ];
125 LOG("desc = %p", desc);
129 case 12: // Small Page
130 case 16: // Large Page
132 if( (*desc & 3) == 0 ) {
133 MM_int_AllocateCoarse( VAddr, pi->Domain );
135 desc = &table1[ VAddr >> 12 ];
136 LOG("desc (2) = %p", desc);
140 // - Error if overwriting a large page
141 if( (*desc & 3) == 1 ) LEAVE_RET('i', 1);
142 if( pi->PhysAddr == 0 ) {
148 *desc = (pi->PhysAddr & 0xFFFFF000) | 2;
149 if(!pi->bExecutable) *desc |= 1; // XN
150 if(!pi->bGlobal) *desc |= 1 << 11; // NG
151 if( pi->bShared) *desc |= 1 << 10; // S
152 *desc |= (pi->AP & 3) << 4; // AP
153 *desc |= ((pi->AP >> 2) & 1) << 9; // APX
161 case 20: // Section or unmapped
162 Log_Warning("MM", "TODO: Implement sections");
164 case 24: // Supersection
165 // Error if not aligned
166 if( VAddr & 0xFFFFFF ) {
170 if( (*desc & 3) == 0 || ((*desc & 3) == 2 && (*desc & (1 << 18))) )
172 if( pi->PhysAddr == 0 ) {
174 // TODO: Apply to all entries
179 *desc = pi->PhysAddr & 0xFF000000;
180 // *desc |= ((pi->PhysAddr >> 32) & 0xF) << 20;
181 // *desc |= ((pi->PhysAddr >> 36) & 0x7) << 5;
182 *desc |= 2 | (1 << 18);
183 // TODO: Apply to all entries
195 int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
197 Uint32 *table0, *table1;
200 MM_int_GetTables(VAddr, &table0, &table1);
202 desc = table0[ VAddr >> 20 ];
217 // 1: Coarse page table
219 // Domain from top level table
220 pi->Domain = (desc >> 5) & 7;
222 desc = table1[ VAddr >> 12 ];
229 // 1: Large Page (64KiB)
232 pi->PhysAddr = desc & 0xFFFF0000;
238 pi->PhysAddr = desc & 0xFFFFF000;
239 pi->bExecutable = desc & 1;
240 pi->bGlobal = !(desc >> 11);
241 pi->bShared = (desc >> 10) & 1;
246 // 2: Section (or Supersection)
248 if( desc & (1 << 18) ) {
250 pi->PhysAddr = desc & 0xFF000000;
251 pi->PhysAddr |= (Uint64)((desc >> 20) & 0xF) << 32;
252 pi->PhysAddr |= (Uint64)((desc >> 5) & 0x7) << 36;
254 pi->Domain = 0; // Superpages default to zero
259 pi->PhysAddr = desc & 0xFFF80000;
261 pi->Domain = (desc >> 5) & 7;
264 // 3: Reserved (invalid)
275 tPAddr MM_GetPhysAddr(tVAddr VAddr)
278 if( MM_int_GetPageInfo(VAddr, &pi) )
283 Uint MM_GetFlags(tVAddr VAddr)
288 if( MM_int_GetPageInfo(VAddr, &pi) )
296 ret |= MM_PFLAG_KERNEL;
299 ret |= MM_PFLAG_KERNEL|MM_PFLAG_RO;
308 if( pi.bExecutable ) ret |= MM_PFLAG_EXEC;
312 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
315 if( MM_int_GetPageInfo(VAddr, &pi) )
322 int MM_Map(tVAddr VAddr, tPAddr PAddr)
324 tMM_PageInfo pi = {0};
327 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
329 if( MM_int_SetPageInfo(VAddr, &pi) ) {
330 MM_DerefPhys(pi.PhysAddr);
336 tPAddr MM_Allocate(tVAddr VAddr)
338 tMM_PageInfo pi = {0};
340 ENTER("pVAddr", VAddr);
342 pi.PhysAddr = MM_AllocPhys();
343 if( pi.PhysAddr == 0 ) LEAVE_RET('i', 0);
345 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
347 if( MM_int_SetPageInfo(VAddr, &pi) ) {
348 MM_DerefPhys(pi.PhysAddr);
352 LEAVE('x', pi.PhysAddr);
356 void MM_Deallocate(tVAddr VAddr)
360 if( MM_int_GetPageInfo(VAddr, &pi) ) return ;
362 if( pi.PhysAddr == 0 ) return;
363 MM_DerefPhys(pi.PhysAddr);
368 MM_int_SetPageInfo(VAddr, &pi);
371 tPAddr MM_ClearUser(void)
373 // TODO: Implement ClearUser
377 tVAddr MM_MapTemp(tPAddr PAddr)
379 // TODO: Implement MapTemp
383 void MM_FreeTemp(tVAddr VAddr)
385 // TODO: Implement FreeTemp
388 void MM_DumpTables(tVAddr Start, tVAddr End)