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 FRACTAL(table1, VAddr) = paddr | 3;
114 int MM_int_SetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
116 Uint32 *table0, *table1;
119 ENTER("pVADdr ppi", VAddr, pi);
121 MM_int_GetTables(VAddr, &table0, &table1);
123 desc = &table0[ VAddr >> 20 ];
124 LOG("desc = %p", desc);
128 case 12: // Small Page
129 case 16: // Large Page
131 if( (*desc & 3) == 0 ) {
132 MM_int_AllocateCoarse( VAddr, pi->Domain );
134 desc = &table1[ VAddr >> 12 ];
135 LOG("desc (2) = %p", desc);
139 // - Error if overwriting a large page
140 if( (*desc & 3) == 1 ) LEAVE_RET('i', 1);
141 if( pi->PhysAddr == 0 ) {
147 *desc = (pi->PhysAddr & 0xFFFFF000) | 2;
148 if(!pi->bExecutable) *desc |= 1; // XN
149 if(!pi->bGlobal) *desc |= 1 << 11; // NG
150 if( pi->bShared) *desc |= 1 << 10; // S
151 *desc |= (pi->AP & 3) << 4; // AP
152 *desc |= ((pi->AP >> 2) & 1) << 9; // APX
162 case 20: // Section or unmapped
163 Warning("TODO: Implement sections");
165 case 24: // Supersection
166 // Error if not aligned
167 if( VAddr & 0xFFFFFF ) {
171 if( (*desc & 3) == 0 || ((*desc & 3) == 2 && (*desc & (1 << 18))) )
173 if( pi->PhysAddr == 0 ) {
175 // TODO: Apply to all entries
180 *desc = pi->PhysAddr & 0xFF000000;
181 // *desc |= ((pi->PhysAddr >> 32) & 0xF) << 20;
182 // *desc |= ((pi->PhysAddr >> 36) & 0x7) << 5;
183 *desc |= 2 | (1 << 18);
184 // TODO: Apply to all entries
197 extern tShortSpinlock glDebug_Lock;
199 int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
201 Uint32 *table0, *table1;
204 MM_int_GetTables(VAddr, &table0, &table1);
206 desc = table0[ VAddr >> 20 ];
208 // if( VAddr > 0x90000000)
209 // LOG("table0 desc(%p) = %x", &table0[ VAddr >> 20 ], desc);
225 // 1: Coarse page table
227 // Domain from top level table
228 pi->Domain = (desc >> 5) & 7;
230 desc = table1[ VAddr >> 12 ];
231 // LOG("table1 desc(%p) = %x", &table1[ VAddr >> 12 ], desc);
238 // 1: Large Page (64KiB)
241 pi->PhysAddr = desc & 0xFFFF0000;
247 pi->PhysAddr = desc & 0xFFFFF000;
248 pi->bExecutable = desc & 1;
249 pi->bGlobal = !(desc >> 11);
250 pi->bShared = (desc >> 10) & 1;
255 // 2: Section (or Supersection)
257 if( desc & (1 << 18) ) {
259 pi->PhysAddr = desc & 0xFF000000;
260 pi->PhysAddr |= (Uint64)((desc >> 20) & 0xF) << 32;
261 pi->PhysAddr |= (Uint64)((desc >> 5) & 0x7) << 36;
263 pi->Domain = 0; // Superpages default to zero
268 pi->PhysAddr = desc & 0xFFF80000;
270 pi->Domain = (desc >> 5) & 7;
273 // 3: Reserved (invalid)
284 tPAddr MM_GetPhysAddr(tVAddr VAddr)
287 if( MM_int_GetPageInfo(VAddr, &pi) )
289 return pi.PhysAddr | (VAddr & ((1 << pi.Size)-1));
292 Uint MM_GetFlags(tVAddr VAddr)
297 if( MM_int_GetPageInfo(VAddr, &pi) )
305 ret |= MM_PFLAG_KERNEL;
308 ret |= MM_PFLAG_KERNEL|MM_PFLAG_RO;
317 if( pi.bExecutable ) ret |= MM_PFLAG_EXEC;
321 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
324 if( MM_int_GetPageInfo(VAddr, &pi) )
331 int MM_Map(tVAddr VAddr, tPAddr PAddr)
333 tMM_PageInfo pi = {0};
336 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
338 if( MM_int_SetPageInfo(VAddr, &pi) ) {
339 MM_DerefPhys(pi.PhysAddr);
345 tPAddr MM_Allocate(tVAddr VAddr)
347 tMM_PageInfo pi = {0};
349 ENTER("pVAddr", VAddr);
351 pi.PhysAddr = MM_AllocPhys();
352 if( pi.PhysAddr == 0 ) LEAVE_RET('i', 0);
354 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
356 if( MM_int_SetPageInfo(VAddr, &pi) ) {
357 MM_DerefPhys(pi.PhysAddr);
361 LEAVE('x', pi.PhysAddr);
365 void MM_Deallocate(tVAddr VAddr)
369 if( MM_int_GetPageInfo(VAddr, &pi) ) return ;
371 if( pi.PhysAddr == 0 ) return;
372 MM_DerefPhys(pi.PhysAddr);
377 MM_int_SetPageInfo(VAddr, &pi);
380 tPAddr MM_ClearUser(void)
382 // TODO: Implement ClearUser
386 tVAddr MM_MapTemp(tPAddr PAddr)
388 // TODO: Implement MapTemp
392 void MM_FreeTemp(tVAddr VAddr)
394 // TODO: Implement FreeTemp
397 void MM_DumpTables(tVAddr Start, tVAddr End)