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(table1, addr) ((table1)[ (0x7F8/4*1024) + ((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 FRACTAL(table1, VAddr) = paddr | 3;
123 int MM_int_SetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
125 Uint32 *table0, *table1;
128 ENTER("pVAddr ppi", VAddr, pi);
130 MM_int_GetTables(VAddr, &table0, &table1);
132 desc = &table0[ VAddr >> 20 ];
133 LOG("desc = %p", desc);
137 case 12: // Small Page
138 case 16: // Large Page
140 if( (*desc & 3) == 0 ) {
141 MM_int_AllocateCoarse( VAddr, pi->Domain );
143 desc = &table1[ VAddr >> 12 ];
144 LOG("desc (2) = %p", desc);
148 // - Error if overwriting a large page
149 if( (*desc & 3) == 1 ) LEAVE_RET('i', 1);
150 if( pi->PhysAddr == 0 ) {
156 *desc = (pi->PhysAddr & 0xFFFFF000) | 2;
157 if(!pi->bExecutable) *desc |= 1; // XN
158 if(!pi->bGlobal) *desc |= 1 << 11; // NG
159 if( pi->bShared) *desc |= 1 << 10; // S
160 *desc |= (pi->AP & 3) << 4; // AP
161 *desc |= ((pi->AP >> 2) & 1) << 9; // APX
162 TLBIMVA(VAddr & 0xFFFFF000);
170 Log_Warning("MMVirt", "TODO: Implement large pages in MM_int_SetPageInfo");
173 case 20: // Section or unmapped
174 Warning("TODO: Implement sections");
176 case 24: // Supersection
177 // Error if not aligned
178 if( VAddr & 0xFFFFFF ) {
182 if( (*desc & 3) == 0 || ((*desc & 3) == 2 && (*desc & (1 << 18))) )
184 if( pi->PhysAddr == 0 ) {
186 // TODO: Apply to all entries
191 *desc = pi->PhysAddr & 0xFF000000;
192 // *desc |= ((pi->PhysAddr >> 32) & 0xF) << 20;
193 // *desc |= ((pi->PhysAddr >> 36) & 0x7) << 5;
194 *desc |= 2 | (1 << 18);
195 // TODO: Apply to all entries
208 int MM_int_GetPageInfo(tVAddr VAddr, tMM_PageInfo *pi)
210 Uint32 *table0, *table1;
213 // LogF("MM_int_GetPageInfo: VAddr=%p, pi=%p\n", VAddr, pi);
215 MM_int_GetTables(VAddr, &table0, &table1);
217 desc = table0[ VAddr >> 20 ];
219 // if( VAddr > 0x90000000)
220 // LOG("table0 desc(%p) = %x", &table0[ VAddr >> 20 ], desc);
236 // 1: Coarse page table
238 // Domain from top level table
239 pi->Domain = (desc >> 5) & 7;
241 desc = table1[ VAddr >> 12 ];
242 // LOG("table1 desc(%p) = %x", &table1[ VAddr >> 12 ], desc);
249 // 1: Large Page (64KiB)
252 pi->PhysAddr = desc & 0xFFFF0000;
253 pi->AP = ((desc >> 4) & 3) | (((desc >> 9) & 1) << 2);
254 pi->bExecutable = !(desc & 0x8000);
255 pi->bShared = (desc >> 10) & 1;
261 pi->PhysAddr = desc & 0xFFFFF000;
262 pi->bExecutable = !(desc & 1);
263 pi->bGlobal = !(desc >> 11);
264 pi->bShared = (desc >> 10) & 1;
265 pi->AP = ((desc >> 4) & 3) | (((desc >> 9) & 1) << 2);
270 // 2: Section (or Supersection)
272 if( desc & (1 << 18) ) {
274 pi->PhysAddr = desc & 0xFF000000;
275 pi->PhysAddr |= (Uint64)((desc >> 20) & 0xF) << 32;
276 pi->PhysAddr |= (Uint64)((desc >> 5) & 0x7) << 36;
278 pi->Domain = 0; // Supersections default to zero
279 pi->AP = ((desc >> 10) & 3) | (((desc >> 15) & 1) << 2);
284 pi->PhysAddr = desc & 0xFFF80000;
286 pi->Domain = (desc >> 5) & 7;
287 pi->AP = ((desc >> 10) & 3) | (((desc >> 15) & 1) << 2);
290 // 3: Reserved (invalid)
301 tPAddr MM_GetPhysAddr(tVAddr VAddr)
304 if( MM_int_GetPageInfo(VAddr, &pi) )
306 return pi.PhysAddr | (VAddr & ((1 << pi.Size)-1));
309 Uint MM_GetFlags(tVAddr VAddr)
314 if( MM_int_GetPageInfo(VAddr, &pi) )
322 ret |= MM_PFLAG_KERNEL;
325 ret |= MM_PFLAG_KERNEL|MM_PFLAG_RO;
334 if( pi.bExecutable ) ret |= MM_PFLAG_EXEC;
338 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
341 if( MM_int_GetPageInfo(VAddr, &pi) )
345 int MM_Map(tVAddr VAddr, tPAddr PAddr)
347 tMM_PageInfo pi = {0};
348 // Log("MM_Map %P=>%p", PAddr, VAddr);
352 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
354 if( MM_int_SetPageInfo(VAddr, &pi) ) {
355 MM_DerefPhys(pi.PhysAddr);
361 tPAddr MM_Allocate(tVAddr VAddr)
363 tMM_PageInfo pi = {0};
365 ENTER("pVAddr", VAddr);
367 pi.PhysAddr = MM_AllocPhys();
368 if( pi.PhysAddr == 0 ) LEAVE_RET('i', 0);
370 pi.AP = AP_KRW_ONLY; // Kernel Read/Write
372 if( MM_int_SetPageInfo(VAddr, &pi) ) {
373 MM_DerefPhys(pi.PhysAddr);
377 LEAVE('x', pi.PhysAddr);
381 void MM_Deallocate(tVAddr VAddr)
385 if( MM_int_GetPageInfo(VAddr, &pi) ) return ;
387 if( pi.PhysAddr == 0 ) return;
388 MM_DerefPhys(pi.PhysAddr);
393 MM_int_SetPageInfo(VAddr, &pi);
396 tPAddr MM_AllocateRootTable(void)
400 ret = MM_AllocPhysRange(2, -1);
403 MM_DerefPhys(ret+0x1000);
404 ret = MM_AllocPhysRange(3, -1);
408 // Log("MM_AllocateRootTable: Second try not aligned, %P", ret);
411 MM_DerefPhys(ret + 0x2000);
412 // Log("MM_AllocateRootTable: Second try aligned, %P", ret);
416 // Log("MM_AllocateRootTable: Got it in one, %P", ret);
420 void MM_int_CloneTable(Uint32 *DestEnt, int Table)
424 Uint32 *cur = (void*)MM_TABLE0USER;
425 // Uint32 *cur = &FRACTAL(MM_TABLE1USER,0);
428 table = MM_AllocPhys();
431 tmp_map = (void*)MM_MapTemp(table);
433 for( i = 0; i < 1024; i ++ )
437 case 0: tmp_map[i] = 0; break;
440 Log_Error("MMVirt", "TODO: Support large pages in MM_int_CloneTable");
447 if( (cur[Table*256] & 0x230) == 0x030 )
448 cur[Table*256+i] |= 0x200; // Set to full RO (Full RO=COW, User RO = RO)
449 tmp_map[i] = cur[Table*256+i];
454 DestEnt[0] = table + 0*0x400 + 1;
455 DestEnt[1] = table + 1*0x400 + 1;
456 DestEnt[2] = table + 2*0x400 + 1;
457 DestEnt[3] = table + 3*0x400 + 1;
460 tPAddr MM_Clone(void)
463 Uint32 *new_lvl1_1, *new_lvl1_2, *cur;
467 ret = MM_AllocateRootTable();
469 cur = (void*)MM_TABLE0USER;
470 new_lvl1_1 = (void*)MM_MapTemp(ret);
471 new_lvl1_2 = (void*)MM_MapTemp(ret+0x1000);
472 tmp_map = new_lvl1_1;
473 new_lvl1_1[0] = 0x8202; // Section mapping the first meg for exception vectors (K-RO)
474 for( i = 1; i < 0x800-4; i ++ )
478 tmp_map = &new_lvl1_2[-0x400];
481 case 0: tmp_map[i] = 0; break;
483 MM_int_CloneTable(&tmp_map[i], i);
484 i += 3; // Tables are alocated in blocks of 4
488 Log_Error("MMVirt", "TODO: Support Sections/Supersections in MM_Clone (i=%i)", i);
494 // Allocate Fractal table
497 tPAddr tmp = MM_AllocPhys();
498 Uint32 *table = (void*)MM_MapTemp(tmp);
500 register Uint32 __SP asm("sp");
501 Log("new_lvl1_2 = %p, &new_lvl1_2[0x3FC] = %p", new_lvl1_2, &new_lvl1_2[0x3FC]);
502 // Map table to last 4MiB of user space
503 new_lvl1_2[0x3FC] = tmp + 0*0x400 + 1;
504 new_lvl1_2[0x3FD] = tmp + 1*0x400 + 1;
505 new_lvl1_2[0x3FE] = tmp + 2*0x400 + 1;
506 new_lvl1_2[0x3FF] = tmp + 3*0x400 + 1;
508 tmp_map = new_lvl1_1;
509 for( j = 0; j < 512; j ++ )
512 tmp_map = &new_lvl1_2[-0x400];
513 if( (tmp_map[j*4] & 3) == 1 )
515 table[j] = tmp_map[j*4] & PADDR_MASK_LVL1;// 0xFFFFFC00;
516 table[j] |= 0x813; // nG, Kernel Only, Small page, XN
522 table[j++] = (ret + 0x0000) | 0x813;
523 table[j++] = (ret + 0x1000) | 0x813;
524 Log("table[%i] = %x, table[%i] = %x", j-2, table[j-2], j-1, table[j-1]);
525 for( ; j < 1024; j ++ )
528 // Get kernel stack bottom
529 sp = __SP & ~(MM_KSTACK_SIZE-1);
530 j = (sp / 0x1000) % 1024;
531 num = MM_KSTACK_SIZE/0x1000;
532 Log("sp = %p, j = %i", sp, j);
535 for(; num--; j ++, sp += 0x1000)
540 page = MM_AllocPhys();
541 table[j] = page | 0x813;
543 tmp_page = (void*)MM_MapTemp(page);
544 memcpy(tmp_page, (void*)sp, 0x1000);
545 MM_FreeTemp( (tVAddr) tmp_page );
548 // Debug_HexDump("MMVirt - last table", table, 0x1000);
550 MM_FreeTemp( (tVAddr)table );
553 // Debug_HexDump("MMVirt - Return page 1", new_lvl1_1, 0x1000);
554 // Debug_HexDump("MMVirt - Return page 2", new_lvl1_2, 0x1000);
556 MM_FreeTemp( (tVAddr)new_lvl1_1 );
557 MM_FreeTemp( (tVAddr)new_lvl1_2 );
559 // Log("Table dump");
560 // MM_DumpTables(0, -1);
565 tPAddr MM_ClearUser(void)
567 // TODO: Implement ClearUser
571 tVAddr MM_MapTemp(tPAddr PAddr)
576 for( ret = MM_TMPMAP_BASE; ret < MM_TMPMAP_END - PAGE_SIZE; ret += PAGE_SIZE )
578 if( MM_int_GetPageInfo(ret, &pi) == 0 )
581 // Log("MapTemp %P at %p", PAddr, ret);
582 MM_RefPhys(PAddr); // Counter the MM_Deallocate in FreeTemp
587 Log_Warning("MMVirt", "MM_MapTemp: All slots taken");
591 void MM_FreeTemp(tVAddr VAddr)
593 // TODO: Implement FreeTemp
594 if( VAddr < MM_TMPMAP_BASE || VAddr >= MM_TMPMAP_END ) {
595 Log_Warning("MMVirt", "MM_FreeTemp: Passed an addr not from MM_MapTemp (%p)", VAddr);
599 MM_Deallocate(VAddr);
602 tVAddr MM_MapHWPages(tPAddr PAddr, Uint NPages)
608 ENTER("xPAddr iNPages", PAddr, NPages);
610 // Scan for a location
611 for( ret = MM_HWMAP_BASE; ret < MM_HWMAP_END - NPages * PAGE_SIZE; ret += PAGE_SIZE )
613 // LOG("checking %p", ret);
614 // Check if there is `NPages` free pages
615 for( i = 0; i < NPages; i ++ )
617 if( MM_int_GetPageInfo(ret + i*PAGE_SIZE, &pi) == 0 )
620 // Nope, jump to after the used page found and try again
621 // LOG("i = %i, ==? %i", i, NPages);
623 ret += i * PAGE_SIZE;
628 for( i = 0; i < NPages; i ++ )
629 MM_Map(ret+i*PAGE_SIZE, PAddr+i*PAddr);
634 Log_Warning("MMVirt", "MM_MapHWPages: No space for a %i page block", NPages);
639 tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PAddr)
641 Log_Error("MMVirt", "TODO: Implement MM_AllocDMA");
645 void MM_UnmapHWPages(tVAddr Vaddr, Uint Number)
647 Log_Error("MMVirt", "TODO: Implement MM_UnmapHWPages");
650 tVAddr MM_NewKStack(int bShared)
652 tVAddr min_addr, max_addr;
656 min_addr = MM_GLOBALSTACKS;
657 max_addr = MM_GLOBALSTACKS_END;
660 min_addr = MM_KSTACK_BASE;
661 max_addr = MM_KSTACK_END;
664 // Locate a free slot
665 for( addr = min_addr; addr < max_addr; addr += MM_KSTACK_SIZE )
668 if( MM_int_GetPageInfo(addr+MM_KSTACK_SIZE-PAGE_SIZE, &pi) ) break;
671 // Check for an error
672 if(addr >= max_addr) {
677 for( ofs = PAGE_SIZE; ofs < MM_KSTACK_SIZE; ofs += PAGE_SIZE )
679 if( MM_Allocate(addr + ofs) == 0 )
684 MM_Deallocate(addr + ofs);
686 Log_Warning("MMVirt", "MM_NewKStack: Unable to allocate");
693 void MM_int_DumpTableEnt(tVAddr Start, size_t Len, tMM_PageInfo *Info)
695 Log("%p => %8x - 0x%7x %i %x",
696 Start, Info->PhysAddr-Len, Len,
702 void MM_DumpTables(tVAddr Start, tVAddr End)
704 tVAddr range_start = 0, addr;
705 tMM_PageInfo pi, pi_old;
706 int i = 0, inRange=0;
710 Log("Page Table Dump:");
712 for( addr = Start; i == 0 || (addr && addr < End); i = 1 )
714 // Log("addr = %p", addr);
715 int rv = MM_int_GetPageInfo(addr, &pi);
717 || pi.Size != pi_old.Size
718 || pi.Domain != pi_old.Domain
719 || pi.AP != pi_old.AP
720 || pi_old.PhysAddr != pi.PhysAddr )
723 MM_int_DumpTableEnt(range_start, addr - range_start, &pi_old);
725 addr &= ~((1 << pi.Size)-1);
730 pi_old.PhysAddr += 1 << pi_old.Size;
731 addr += 1 << pi_old.Size;
735 MM_int_DumpTableEnt(range_start, addr - range_start, &pi);