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
16 #define PADDR_MASK_LVL1 0xFFFFFC00
19 extern Uint32 kernel_table0[];
33 //#define FRACTAL(table1, addr) ((table1)[ (0xFF8/4*1024) + ((addr)>>20)])
34 #define FRACTAL(table1, addr) ((table1)[ (0xFF8/4*1024) + ((addr)>>22)])
35 #define USRFRACTAL(addr) (*((Uint32*)(0x7FDFF000) + ((addr)>>22)))
36 #define TLBIALL() __asm__ __volatile__ ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0))
37 #define TLBIMVA(addr) __asm__ __volatile__ ("mcr p15, 0, %0, c8, c7, 1" : : "r" (addr))
40 void MM_int_GetTables(tVAddr VAddr, Uint32 **Table0, Uint32 **Table1);
41 int MM_int_AllocateCoarse(tVAddr VAddr, int Domain);
42 int MM_int_SetPageInfo(tVAddr VAddr, tMM_PageInfo *pi);
43 int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi);
44 tPAddr MM_AllocateRootTable(void);
45 void MM_int_CloneTable(Uint32 *DestEnt, int Table);
46 tPAddr MM_Clone(void);
47 tVAddr MM_NewKStack(int bGlobal);
48 void MM_int_DumpTableEnt(tVAddr Start, size_t Len, tMM_PageInfo *Info);
49 //void MM_DumpTables(tVAddr Start, tVAddr End);
54 int MM_InitialiseVirtual(void)
59 void MM_int_GetTables(tVAddr VAddr, Uint32 **Table0, Uint32 **Table1)
61 if(VAddr & 0x80000000) {
62 *Table0 = (void*)&kernel_table0; // Level 0
63 *Table1 = (void*)MM_TABLE1KERN; // Level 1
66 *Table0 = (void*)MM_TABLE0USER;
67 *Table1 = (void*)MM_TABLE1USER;
71 int MM_int_AllocateCoarse(tVAddr VAddr, int Domain)
73 Uint32 *table0, *table1;
77 ENTER("xVAddr iDomain", VAddr, Domain);
79 MM_int_GetTables(VAddr, &table0, &table1);
81 VAddr &= ~(0x400000-1); // 4MiB per "block", 1 Page
83 desc = &table0[ VAddr>>20];
84 LOG("desc = %p", desc);
86 // table0: 4 bytes = 1 MiB
88 LOG("desc[0] = %x", desc[0]);
89 LOG("desc[1] = %x", desc[1]);
90 LOG("desc[2] = %x", desc[2]);
91 LOG("desc[3] = %x", desc[3]);
93 if( (desc[0] & 3) != 0 || (desc[1] & 3) != 0
94 || (desc[2] & 3) != 0 || (desc[3] & 3) != 0 )
101 paddr = MM_AllocPhys();
109 *desc = paddr | (Domain << 5) | 1;
110 desc[1] = desc[0] + 0x400;
111 desc[2] = desc[0] + 0x800;
112 desc[3] = desc[0] + 0xC00;
114 if( VAddr < 0x80000000 ) {
115 // Log("USRFRACTAL(%p) = %p", VAddr, &USRFRACTAL(VAddr));
116 USRFRACTAL(VAddr) = paddr | 3;
119 // Log("FRACTAL(%p) = %p", VAddr, &FRACTAL(table1, VAddr));
120 FRACTAL(table1, VAddr) = paddr | 3;
130 int MM_int_SetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
132 Uint32 *table0, *table1;
135 ENTER("pVAddr ppi", VAddr, pi);
137 MM_int_GetTables(VAddr, &table0, &table1);
139 desc = &table0[ VAddr >> 20 ];
140 LOG("desc = %p", desc);
144 case 12: // Small Page
145 case 16: // Large Page
147 if( (*desc & 3) == 0 ) {
148 MM_int_AllocateCoarse( VAddr, pi->Domain );
150 desc = &table1[ VAddr >> 12 ];
151 LOG("desc (2) = %p", desc);
155 // - Error if overwriting a large page
156 if( (*desc & 3) == 1 ) LEAVE_RET('i', 1);
157 if( pi->PhysAddr == 0 ) {
163 *desc = (pi->PhysAddr & 0xFFFFF000) | 2;
164 if(!pi->bExecutable) *desc |= 1; // XN
165 if(!pi->bGlobal) *desc |= 1 << 11; // NG
166 if( pi->bShared) *desc |= 1 << 10; // S
167 *desc |= (pi->AP & 3) << 4; // AP
168 *desc |= ((pi->AP >> 2) & 1) << 9; // APX
169 TLBIMVA(VAddr & 0xFFFFF000);
177 Log_Warning("MMVirt", "TODO: Implement large pages in MM_int_SetPageInfo");
180 case 20: // Section or unmapped
181 Warning("TODO: Implement sections");
183 case 24: // Supersection
184 // Error if not aligned
185 if( VAddr & 0xFFFFFF ) {
189 if( (*desc & 3) == 0 || ((*desc & 3) == 2 && (*desc & (1 << 18))) )
191 if( pi->PhysAddr == 0 ) {
193 // TODO: Apply to all entries
198 *desc = pi->PhysAddr & 0xFF000000;
199 // *desc |= ((pi->PhysAddr >> 32) & 0xF) << 20;
200 // *desc |= ((pi->PhysAddr >> 36) & 0x7) << 5;
201 *desc |= 2 | (1 << 18);
202 // TODO: Apply to all entries
215 int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
217 Uint32 *table0, *table1;
220 // LogF("MM_int_GetPageInfo: VAddr=%p, pi=%p\n", VAddr, pi);
222 MM_int_GetTables(VAddr, &table0, &table1);
224 desc = table0[ VAddr >> 20 ];
226 // if( VAddr > 0x90000000)
227 // LOG("table0 desc(%p) = %x", &table0[ VAddr >> 20 ], desc);
243 // 1: Coarse page table
245 // Domain from top level table
246 pi->Domain = (desc >> 5) & 7;
248 desc = table1[ VAddr >> 12 ];
249 // LOG("table1 desc(%p) = %x", &table1[ VAddr >> 12 ], desc);
256 // 1: Large Page (64KiB)
259 pi->PhysAddr = desc & 0xFFFF0000;
260 pi->AP = ((desc >> 4) & 3) | (((desc >> 9) & 1) << 2);
261 pi->bExecutable = !(desc & 0x8000);
262 pi->bShared = (desc >> 10) & 1;
268 pi->PhysAddr = desc & 0xFFFFF000;
269 pi->bExecutable = !(desc & 1);
270 pi->bGlobal = !(desc >> 11);
271 pi->bShared = (desc >> 10) & 1;
272 pi->AP = ((desc >> 4) & 3) | (((desc >> 9) & 1) << 2);
277 // 2: Section (or Supersection)
279 if( desc & (1 << 18) ) {
281 pi->PhysAddr = desc & 0xFF000000;
282 pi->PhysAddr |= (Uint64)((desc >> 20) & 0xF) << 32;
283 pi->PhysAddr |= (Uint64)((desc >> 5) & 0x7) << 36;
285 pi->Domain = 0; // Supersections default to zero
286 pi->AP = ((desc >> 10) & 3) | (((desc >> 15) & 1) << 2);
291 pi->PhysAddr = desc & 0xFFF80000;
293 pi->Domain = (desc >> 5) & 7;
294 pi->AP = ((desc >> 10) & 3) | (((desc >> 15) & 1) << 2);
297 // 3: Reserved (invalid)
308 tPAddr MM_GetPhysAddr(tVAddr VAddr)
311 if( MM_int_GetPageInfo(VAddr, &pi) )
313 return pi.PhysAddr | (VAddr & ((1 << pi.Size)-1));
316 Uint MM_GetFlags(tVAddr VAddr)
321 if( MM_int_GetPageInfo(VAddr, &pi) )
329 ret |= MM_PFLAG_KERNEL;
332 ret |= MM_PFLAG_KERNEL|MM_PFLAG_RO;
341 if( pi.bExecutable ) ret |= MM_PFLAG_EXEC;
345 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
348 if( MM_int_GetPageInfo(VAddr, &pi) )
352 int MM_Map(tVAddr VAddr, tPAddr PAddr)
354 tMM_PageInfo pi = {0};
355 // Log("MM_Map %P=>%p", PAddr, VAddr);
359 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
361 if( MM_int_SetPageInfo(VAddr, &pi) ) {
362 MM_DerefPhys(pi.PhysAddr);
368 tPAddr MM_Allocate(tVAddr VAddr)
370 tMM_PageInfo pi = {0};
372 ENTER("pVAddr", VAddr);
374 pi.PhysAddr = MM_AllocPhys();
375 if( pi.PhysAddr == 0 ) LEAVE_RET('i', 0);
377 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
379 if( MM_int_SetPageInfo(VAddr, &pi) ) {
380 MM_DerefPhys(pi.PhysAddr);
384 LEAVE('x', pi.PhysAddr);
388 void MM_Deallocate(tVAddr VAddr)
392 if( MM_int_GetPageInfo(VAddr, &pi) ) return ;
394 if( pi.PhysAddr == 0 ) return;
395 MM_DerefPhys(pi.PhysAddr);
400 MM_int_SetPageInfo(VAddr, &pi);
403 tPAddr MM_AllocateRootTable(void)
407 ret = MM_AllocPhysRange(2, -1);
410 MM_DerefPhys(ret+0x1000);
411 ret = MM_AllocPhysRange(3, -1);
415 // Log("MM_AllocateRootTable: Second try not aligned, %P", ret);
418 MM_DerefPhys(ret + 0x2000);
419 // Log("MM_AllocateRootTable: Second try aligned, %P", ret);
423 // Log("MM_AllocateRootTable: Got it in one, %P", ret);
427 void MM_int_CloneTable(Uint32 *DestEnt, int Table)
431 Uint32 *cur = (void*)MM_TABLE0USER;
432 // Uint32 *cur = &FRACTAL(MM_TABLE1USER,0);
435 table = MM_AllocPhys();
438 tmp_map = (void*)MM_MapTemp(table);
440 for( i = 0; i < 1024; i ++ )
444 case 0: tmp_map[i] = 0; break;
447 Log_Error("MMVirt", "TODO: Support large pages in MM_int_CloneTable");
454 if( (cur[Table*256] & 0x230) == 0x030 )
455 cur[Table*256+i] |= 0x200; // Set to full RO (Full RO=COW, User RO = RO)
456 tmp_map[i] = cur[Table*256+i];
461 DestEnt[0] = table + 0*0x400 + 1;
462 DestEnt[1] = table + 1*0x400 + 1;
463 DestEnt[2] = table + 2*0x400 + 1;
464 DestEnt[3] = table + 3*0x400 + 1;
467 tPAddr MM_Clone(void)
470 Uint32 *new_lvl1_1, *new_lvl1_2, *cur;
474 ret = MM_AllocateRootTable();
476 cur = (void*)MM_TABLE0USER;
477 new_lvl1_1 = (void*)MM_MapTemp(ret);
478 new_lvl1_2 = (void*)MM_MapTemp(ret+0x1000);
479 tmp_map = new_lvl1_1;
480 for( i = 0; i < 0x800-4; i ++ )
484 tmp_map = &new_lvl1_2[-0x400];
487 case 0: tmp_map[i] = 0; break;
489 MM_int_CloneTable(&tmp_map[i], i);
490 i += 3; // Tables are alocated in blocks of 4
494 Log_Error("MMVirt", "TODO: Support Sections/Supersections in MM_Clone (i=%i)", i);
500 // Allocate Fractal table
503 tPAddr tmp = MM_AllocPhys();
504 Uint32 *table = (void*)MM_MapTemp(tmp);
506 register Uint32 __SP asm("sp");
508 // Map table to last 4MiB of user space
509 new_lvl1_2[0x3FC] = tmp + 0*0x400 + 1;
510 new_lvl1_2[0x3FD] = tmp + 1*0x400 + 1;
511 new_lvl1_2[0x3FE] = tmp + 2*0x400 + 1;
512 new_lvl1_2[0x3FF] = tmp + 3*0x400 + 1;
514 tmp_map = new_lvl1_1;
515 for( j = 0; j < 512; j ++ )
518 tmp_map = &new_lvl1_2[-0x400];
519 if( (tmp_map[j*4] & 3) == 1 )
521 table[j] = tmp_map[j*4] & PADDR_MASK_LVL1;// 0xFFFFFC00;
522 table[j] |= 0x813; // nG, Kernel Only, Small page, XN
528 table[j++] = (ret + 0x0000) | 0x813;
529 table[j++] = (ret + 0x1000) | 0x813;
531 for( ; j < 1024; j ++ )
534 // Get kernel stack bottom
535 sp = __SP & ~(MM_KSTACK_SIZE-1);
536 j = (sp / 0x1000) % 1024;
537 num = MM_KSTACK_SIZE/0x1000;
540 for(; num--; j ++, sp += 0x1000)
545 page = MM_AllocPhys();
546 table[j] = page | 0x813;
548 tmp_page = (void*)MM_MapTemp(page);
549 memcpy(tmp_page, (void*)sp, 0x1000);
550 MM_FreeTemp( (tVAddr) tmp_page );
553 MM_FreeTemp( (tVAddr)table );
556 MM_FreeTemp( (tVAddr)new_lvl1_1 );
557 MM_FreeTemp( (tVAddr)new_lvl1_2 );
562 tPAddr MM_ClearUser(void)
564 // TODO: Implement ClearUser
568 tVAddr MM_MapTemp(tPAddr PAddr)
573 for( ret = MM_TMPMAP_BASE; ret < MM_TMPMAP_END - PAGE_SIZE; ret += PAGE_SIZE )
575 if( MM_int_GetPageInfo(ret, &pi) == 0 )
578 // Log("MapTemp %P at %p", PAddr, ret);
579 MM_RefPhys(PAddr); // Counter the MM_Deallocate in FreeTemp
584 Log_Warning("MMVirt", "MM_MapTemp: All slots taken");
588 void MM_FreeTemp(tVAddr VAddr)
590 // TODO: Implement FreeTemp
591 if( VAddr < MM_TMPMAP_BASE || VAddr >= MM_TMPMAP_END ) {
592 Log_Warning("MMVirt", "MM_FreeTemp: Passed an addr not from MM_MapTemp (%p)", VAddr);
596 MM_Deallocate(VAddr);
599 tVAddr MM_MapHWPages(tPAddr PAddr, Uint NPages)
605 ENTER("xPAddr iNPages", PAddr, NPages);
607 // Scan for a location
608 for( ret = MM_HWMAP_BASE; ret < MM_HWMAP_END - NPages * PAGE_SIZE; ret += PAGE_SIZE )
610 // LOG("checking %p", ret);
611 // Check if there is `NPages` free pages
612 for( i = 0; i < NPages; i ++ )
614 if( MM_int_GetPageInfo(ret + i*PAGE_SIZE, &pi) == 0 )
617 // Nope, jump to after the used page found and try again
618 // LOG("i = %i, ==? %i", i, NPages);
620 ret += i * PAGE_SIZE;
625 for( i = 0; i < NPages; i ++ )
626 MM_Map(ret+i*PAGE_SIZE, PAddr+i*PAddr);
631 Log_Warning("MMVirt", "MM_MapHWPages: No space for a %i page block", NPages);
636 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PAddr)
638 Log_Error("MMVirt", "TODO: Implement MM_AllocDMA");
642 void MM_UnmapHWPages(tVAddr Vaddr, Uint Number)
644 Log_Error("MMVirt", "TODO: Implement MM_UnmapHWPages");
647 tVAddr MM_NewKStack(int bShared)
649 tVAddr min_addr, max_addr;
653 min_addr = MM_GLOBALSTACKS;
654 max_addr = MM_GLOBALSTACKS_END;
657 min_addr = MM_KSTACK_BASE;
658 max_addr = MM_KSTACK_END;
661 // Locate a free slot
662 for( addr = min_addr; addr < max_addr; addr += MM_KSTACK_SIZE )
665 if( MM_int_GetPageInfo(addr+MM_KSTACK_SIZE-PAGE_SIZE, &pi) ) break;
668 // Check for an error
669 if(addr >= max_addr) {
674 for( ofs = PAGE_SIZE; ofs < MM_KSTACK_SIZE; ofs += PAGE_SIZE )
676 if( MM_Allocate(addr + ofs) == 0 )
681 MM_Deallocate(addr + ofs);
683 Log_Warning("MMVirt", "MM_NewKStack: Unable to allocate");
690 void MM_int_DumpTableEnt(tVAddr Start, size_t Len, tMM_PageInfo *Info)
692 Log("%p => %8x - 0x%7x %i %x",
693 Start, Info->PhysAddr-Len, Len,
699 void MM_DumpTables(tVAddr Start, tVAddr End)
701 tVAddr range_start = 0, addr;
702 tMM_PageInfo pi, pi_old;
703 int i = 0, inRange=0;
707 Log("Page Table Dump:");
709 for( addr = Start; i == 0 || (addr && addr < End); i = 1 )
711 // Log("addr = %p", addr);
712 int rv = MM_int_GetPageInfo(addr, &pi);
714 || pi.Size != pi_old.Size
715 || pi.Domain != pi_old.Domain
716 || pi.AP != pi_old.AP
717 || pi_old.PhysAddr != pi.PhysAddr )
720 MM_int_DumpTableEnt(range_start, addr - range_start, &pi_old);
722 addr &= ~((1 << pi.Size)-1);
727 pi_old.PhysAddr += 1 << pi_old.Size;
728 addr += 1 << pi_old.Size;
732 MM_int_DumpTableEnt(range_start, addr - range_start, &pi);