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);
41 tVAddr MM_NewKStack(int bGlobal);
46 int MM_InitialiseVirtual(void)
51 void MM_int_GetTables(tVAddr VAddr, Uint32 **Table0, Uint32 **Table1)
53 if(VAddr & 0x80000000) {
54 *Table0 = (void*)&kernel_table0; // Level 0
55 *Table1 = (void*)MM_TABLE1KERN; // Level 1
58 *Table0 = (void*)MM_TABLE0USER;
59 *Table1 = (void*)MM_TABLE1USER;
63 int MM_int_AllocateCoarse(tVAddr VAddr, int Domain)
65 Uint32 *table0, *table1;
69 ENTER("xVAddr iDomain", VAddr, Domain);
71 MM_int_GetTables(VAddr, &table0, &table1);
73 VAddr &= ~(0x400000-1); // 4MiB per "block", 1 Page
75 desc = &table0[ VAddr>>20];
76 LOG("desc = %p", desc);
78 // table0: 4 bytes = 1 MiB
80 LOG("desc[0] = %x", desc[0]);
81 LOG("desc[1] = %x", desc[1]);
82 LOG("desc[2] = %x", desc[2]);
83 LOG("desc[3] = %x", desc[3]);
85 if( (desc[0] & 3) != 0 || (desc[1] & 3) != 0
86 || (desc[2] & 3) != 0 || (desc[3] & 3) != 0 )
93 paddr = MM_AllocPhys();
101 *desc = paddr | (Domain << 5) | 1;
102 desc[1] = desc[0] + 0x400;
103 desc[2] = desc[0] + 0x800;
104 desc[3] = desc[0] + 0xC00;
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
163 case 20: // Section or unmapped
164 Warning("TODO: Implement sections");
166 case 24: // Supersection
167 // Error if not aligned
168 if( VAddr & 0xFFFFFF ) {
172 if( (*desc & 3) == 0 || ((*desc & 3) == 2 && (*desc & (1 << 18))) )
174 if( pi->PhysAddr == 0 ) {
176 // TODO: Apply to all entries
181 *desc = pi->PhysAddr & 0xFF000000;
182 // *desc |= ((pi->PhysAddr >> 32) & 0xF) << 20;
183 // *desc |= ((pi->PhysAddr >> 36) & 0x7) << 5;
184 *desc |= 2 | (1 << 18);
185 // TODO: Apply to all entries
198 extern tShortSpinlock glDebug_Lock;
200 int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
202 Uint32 *table0, *table1;
205 MM_int_GetTables(VAddr, &table0, &table1);
207 desc = table0[ VAddr >> 20 ];
209 // if( VAddr > 0x90000000)
210 // LOG("table0 desc(%p) = %x", &table0[ VAddr >> 20 ], desc);
226 // 1: Coarse page table
228 // Domain from top level table
229 pi->Domain = (desc >> 5) & 7;
231 desc = table1[ VAddr >> 12 ];
232 // LOG("table1 desc(%p) = %x", &table1[ VAddr >> 12 ], desc);
239 // 1: Large Page (64KiB)
242 pi->PhysAddr = desc & 0xFFFF0000;
248 pi->PhysAddr = desc & 0xFFFFF000;
249 pi->bExecutable = desc & 1;
250 pi->bGlobal = !(desc >> 11);
251 pi->bShared = (desc >> 10) & 1;
256 // 2: Section (or Supersection)
258 if( desc & (1 << 18) ) {
260 pi->PhysAddr = desc & 0xFF000000;
261 pi->PhysAddr |= (Uint64)((desc >> 20) & 0xF) << 32;
262 pi->PhysAddr |= (Uint64)((desc >> 5) & 0x7) << 36;
264 pi->Domain = 0; // Superpages default to zero
269 pi->PhysAddr = desc & 0xFFF80000;
271 pi->Domain = (desc >> 5) & 7;
274 // 3: Reserved (invalid)
285 tPAddr MM_GetPhysAddr(tVAddr VAddr)
288 if( MM_int_GetPageInfo(VAddr, &pi) )
290 return pi.PhysAddr | (VAddr & ((1 << pi.Size)-1));
293 Uint MM_GetFlags(tVAddr VAddr)
298 if( MM_int_GetPageInfo(VAddr, &pi) )
306 ret |= MM_PFLAG_KERNEL;
309 ret |= MM_PFLAG_KERNEL|MM_PFLAG_RO;
318 if( pi.bExecutable ) ret |= MM_PFLAG_EXEC;
322 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
325 if( MM_int_GetPageInfo(VAddr, &pi) )
332 int MM_Map(tVAddr VAddr, tPAddr PAddr)
334 tMM_PageInfo pi = {0};
337 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
339 if( MM_int_SetPageInfo(VAddr, &pi) ) {
340 MM_DerefPhys(pi.PhysAddr);
346 tPAddr MM_Allocate(tVAddr VAddr)
348 tMM_PageInfo pi = {0};
350 ENTER("pVAddr", VAddr);
352 pi.PhysAddr = MM_AllocPhys();
353 if( pi.PhysAddr == 0 ) LEAVE_RET('i', 0);
355 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
357 if( MM_int_SetPageInfo(VAddr, &pi) ) {
358 MM_DerefPhys(pi.PhysAddr);
362 LEAVE('x', pi.PhysAddr);
366 void MM_Deallocate(tVAddr VAddr)
370 if( MM_int_GetPageInfo(VAddr, &pi) ) return ;
372 if( pi.PhysAddr == 0 ) return;
373 MM_DerefPhys(pi.PhysAddr);
378 MM_int_SetPageInfo(VAddr, &pi);
381 tPAddr MM_ClearUser(void)
383 // TODO: Implement ClearUser
387 tVAddr MM_MapTemp(tPAddr PAddr)
389 // TODO: Implement MapTemp
393 void MM_FreeTemp(tVAddr VAddr)
395 // TODO: Implement FreeTemp
398 tVAddr MM_NewKStack(int bGlobal)
400 // TODO: Implement NewKStack
401 // TODO: Should I support global stacks? if only for the idle thread
405 void MM_DumpTables(tVAddr Start, tVAddr End)