4 * Virtual Memory Manager
16 #define PADDR_MASK 0x7FFFFFFF##FFFFF000
17 #define PAGE_MASK (((Uint)1 << 36)-1)
18 #define TABLE_MASK (((Uint)1 << 27)-1)
19 #define PDP_MASK (((Uint)1 << 18)-1)
20 #define PML4_MASK (((Uint)1 << 9)-1)
22 #define PF_PRESENT 0x1
26 #define PF_PAGED 0x400
27 #define PF_NX 0x80000000##00000000
30 #define PAGETABLE(idx) (*((tPAddr*)MM_FRACTAL_BASE+((idx)&PAGE_MASK)))
31 #define PAGEDIR(idx) PAGETABLE((MM_FRACTAL_BASE>>12)+((idx)&TABLE_MASK))
32 #define PAGEDIRPTR(idx) PAGEDIR((MM_FRACTAL_BASE>>21)+((idx)&PDP_MASK))
33 #define PAGEMAPLVL4(idx) PAGEDIRPTR((MM_FRACTAL_BASE>>30)+((idx)&PML4_MASK))
35 #define INVLPG(__addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(__addr));
38 void MM_InitVirt(void);
39 void MM_FinishVirtualInit(void);
40 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
41 void MM_DumpTables(tVAddr Start, tVAddr End);
42 int MM_Map(tVAddr VAddr, tPAddr PAddr);
47 void MM_InitVirt(void)
51 void MM_FinishVirtualInit(void)
56 * \brief Called on a page fault
58 void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
60 // TODO: Copy on Write
62 if( gaPageDir [Addr>>22] & PF_PRESENT
63 && gaPageTable[Addr>>12] & PF_PRESENT
64 && gaPageTable[Addr>>12] & PF_COW )
67 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
69 gaPageTable[Addr>>12] &= ~PF_COW;
70 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
74 //Log("MM_PageFault: COW - MM_DuplicatePage(0x%x)", Addr);
75 paddr = MM_DuplicatePage( Addr );
76 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
77 gaPageTable[Addr>>12] &= PF_USER;
78 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
81 INVLPG( Addr & ~0xFFF );
86 // If it was a user, tell the thread handler
88 Warning("%s %s %s memory%s",
89 (ErrorCode&4?"User":"Kernel"),
90 (ErrorCode&2?"write to":"read from"),
91 (ErrorCode&1?"bad/locked":"non-present"),
92 (ErrorCode&16?" (Instruction Fetch)":"")
94 Warning("User Pagefault: Instruction at %04x:%08x accessed %p",
95 Regs->CS, Regs->RIP, Addr);
96 __asm__ __volatile__ ("sti"); // Restart IRQs
97 // Threads_SegFault(Addr);
103 // -- Check Error Code --
105 Warning("Reserved Bits Trashed!");
108 Warning("%s %s %s memory%s",
109 (ErrorCode&4?"User":"Kernel"),
110 (ErrorCode&2?"write to":"read from"),
111 (ErrorCode&1?"bad/locked":"non-present"),
112 (ErrorCode&16?" (Instruction Fetch)":"")
116 Log("Code at %p accessed %p", Regs->RIP, Addr);
117 // Print Stack Backtrace
118 // Error_Backtrace(Regs->RIP, Regs->RBP);
120 MM_DumpTables(0, -1);
124 * \brief Dumps the layout of the page tables
126 void MM_DumpTables(tVAddr Start, tVAddr End)
128 const tPAddr CHANGEABLE_BITS = 0xFF8;
129 const tPAddr MASK = ~CHANGEABLE_BITS; // Physical address and access bits
130 tVAddr rangeStart = 0;
131 tPAddr expected = CHANGEABLE_BITS; // MASK is used because it's not a vaild value
135 End &= (1L << 48) - 1;
137 Start >>= 12; End >>= 12;
139 Log("Table Entries:");
140 for(page = Start, curPos = Start<<12;
142 curPos += 0x1000, page++)
144 if( curPos == 0x800000000000L )
145 curPos = 0xFFFF800000000000L;
149 !(PAGEMAPLVL4(page>>27) & PF_PRESENT)
150 || !(PAGEDIRPTR(page>>18) & PF_PRESENT)
151 || !(PAGEDIR(page>>9) & PF_PRESENT)
152 || !(PAGETABLE(page) & PF_PRESENT)
153 || (PAGETABLE(page) & MASK) != expected)
155 if(expected != CHANGEABLE_BITS) {
156 Log("%016x-0x%016x => %013x-%013x (%c%c%c%c)",
157 rangeStart, curPos - 1,
158 PAGETABLE(rangeStart>>12) & ~0xFFF,
159 (expected & ~0xFFF) - 1,
160 (expected & PF_PAGED ? 'p' : '-'),
161 (expected & PF_COW ? 'C' : '-'),
162 (expected & PF_USER ? 'U' : '-'),
163 (expected & PF_WRITE ? 'W' : '-')
165 expected = CHANGEABLE_BITS;
167 if( !(PAGEMAPLVL4(page>>27) & PF_PRESENT) ) {
168 page += (1 << 27) - 1;
169 curPos += (1L << 39) - 0x1000;
172 if( !(PAGEDIRPTR(page>>18) & PF_PRESENT) ) {
173 page += (1 << 18) - 1;
174 curPos += (1L << 30) - 0x1000;
177 if( !(PAGEDIR(page>>9) & PF_PRESENT) ) {
178 page += (1 << 9) - 1;
179 curPos += (1L << 21) - 0x1000;
182 if( !(PAGETABLE(page) & PF_PRESENT) ) continue;
184 expected = (PAGETABLE(page) & MASK);
187 if(expected != CHANGEABLE_BITS)
191 if(expected != CHANGEABLE_BITS) {
192 Log("%016x-%016x => %013x-%013x (%s%s%s%s)",
193 rangeStart, curPos - 1,
194 PAGETABLE(rangeStart>>12) & ~0xFFF,
195 (expected & ~0xFFF) - 1,
196 (expected & PF_PAGED ? "p" : "-"),
197 (expected & PF_COW ? "C" : "-"),
198 (expected & PF_USER ? "U" : "-"),
199 (expected & PF_WRITE ? "W" : "-")
206 * \brief Map a physical page to a virtual one
208 int MM_Map(tVAddr VAddr, tPAddr PAddr)
212 Log("MM_Map: (VAddr=0x%x, PAddr=0x%x)", VAddr, PAddr);
215 //Log(" MM_Map: &PAGEMAPLVL4(%x) = %x", VAddr >> 39, &PAGEMAPLVL4(VAddr >> 39));
216 //Log(" MM_Map: &PAGEDIRPTR(%x) = %x", VAddr >> 30, &PAGEDIRPTR(VAddr >> 30));
217 //Log(" MM_Map: &PAGEDIR(%x) = %x", VAddr >> 21, &PAGEDIR(VAddr >> 21));
218 //Log(" MM_Map: &PAGETABLE(%x) = %x", VAddr >> 12, &PAGETABLE(VAddr >> 12));
219 //Log(" MM_Map: &PAGETABLE(0) = %x", &PAGETABLE(0));
220 if( !(PAGEMAPLVL4(VAddr >> 39) & 1) )
222 tmp = MM_AllocPhys();
224 PAGEMAPLVL4(VAddr >> 39) = tmp | 3;
225 INVLPG( &PAGEDIRPTR( (VAddr>>39)<<9 ) );
226 memset( &PAGEDIRPTR( (VAddr>>39)<<9 ), 0, 4096 );
230 if( !(PAGEDIRPTR(VAddr >> 30) & 1) )
232 tmp = MM_AllocPhys();
234 PAGEDIRPTR(VAddr >> 30) = tmp | 3;
235 INVLPG( &PAGEDIR( (VAddr>>30)<<9 ) );
236 memset( &PAGEDIR( (VAddr>>30)<<9 ), 0, 0x1000 );
240 if( !(PAGEDIR(VAddr >> 21) & 1) )
242 tmp = MM_AllocPhys();
244 PAGEDIR(VAddr >> 21) = tmp | 3;
245 INVLPG( &PAGETABLE( (VAddr>>21)<<9 ) );
246 memset( &PAGETABLE( (VAddr>>21)<<9 ), 0, 4096 );
249 // Check if this virtual address is already mapped
250 if( PAGETABLE(VAddr >> PTAB_SHIFT) & 1 )
253 PAGETABLE(VAddr >> PTAB_SHIFT) = PAddr | 3;
256 Log("MM_Map: RETURN 1");
262 * \brief Removed a mapped page
264 void MM_Unmap(tVAddr VAddr)
267 if( !(PAGEMAPLVL4(VAddr >> 39) & 1) ) return ;
269 if( !(PAGEDIRPTR(VAddr >> 30) & 1) ) return ;
271 if( !(PAGEDIR(VAddr >> 21) & 1) ) return ;
273 PAGETABLE(VAddr >> PTAB_SHIFT) = 0;
278 * \brief Allocate a block of memory at the specified virtual address
280 tPAddr MM_Allocate(tVAddr VAddr)
284 ENTER("xVAddr", VAddr);
286 if( !MM_Map(VAddr, 0) ) // Make sure things are allocated
288 Warning("MM_Allocate: Unable to map, tables did not initialise");
293 ret = MM_AllocPhys();
294 LOG("ret = %x", ret);
300 if( !MM_Map(VAddr, ret) )
302 Warning("MM_Allocate: Unable to map. Strange, we should have errored earlier");
312 void MM_Deallocate(tVAddr VAddr)
316 phys = MM_GetPhysAddr(VAddr);
325 * \brief Get the physical address of a virtual location
327 tPAddr MM_GetPhysAddr(tVAddr Addr)
329 Log("MM_GetPhysAddr: (Addr=0x%x)", Addr);
330 if( !(PAGEMAPLVL4(Addr >> 39) & 1) )
332 Log(" MM_GetPhysAddr: PDP Valid");
333 if( !(PAGEDIRPTR(Addr >> 30) & 1) )
335 Log(" MM_GetPhysAddr: PD Valid");
336 if( !(PAGEDIR(Addr >> 21) & 1) )
338 Log(" MM_GetPhysAddr: PT Valid");
339 if( !(PAGETABLE(Addr >> PTAB_SHIFT) & 1) )
341 Log(" MM_GetPhysAddr: Page Valid");
343 return (PAGETABLE(Addr >> PTAB_SHIFT) & ~0xFFF) | (Addr & 0xFFF);
347 * \brief Sets the flags on a page
349 void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask)
354 if( !(PAGEMAPLVL4(VAddr >> 39) & 1) )
356 if( !(PAGEDIRPTR(VAddr >> 30) & 1) )
358 if( !(PAGEDIR(VAddr >> 21) & 1) )
360 if( !(PAGETABLE(VAddr >> 12) & 1) )
364 ent = &PAGETABLE(VAddr >> 12);
367 if( Mask & MM_PFLAG_RO )
369 if( Flags & MM_PFLAG_RO ) {
378 if( Mask & MM_PFLAG_KERNEL )
380 if( Flags & MM_PFLAG_KERNEL ) {
389 if( Mask & MM_PFLAG_COW )
391 if( Flags & MM_PFLAG_COW ) {
402 if( Mask & MM_PFLAG_EXEC )
404 if( Flags & MM_PFLAG_EXEC ) {
414 * \brief Get the flags applied to a page
416 Uint MM_GetFlags(tVAddr VAddr)
422 if( !(PAGEMAPLVL4(VAddr >> 39) & 1) )
424 if( !(PAGEDIRPTR(VAddr >> 30) & 1) )
426 if( !(PAGEDIR(VAddr >> 21) & 1) )
428 if( !(PAGETABLE(VAddr >> 12) & 1) )
432 ent = &PAGETABLE(VAddr >> 12);
435 if( !(*ent & PF_WRITE) ) ret |= MM_PFLAG_RO;
437 if( !(*ent & PF_USER) ) ret |= MM_PFLAG_KERNEL;
439 if( *ent & PF_COW ) ret |= MM_PFLAG_COW;
441 if( !(*ent & PF_NX) ) ret |= MM_PFLAG_EXEC;
446 // --- Hardware Mappings ---
448 * \brief Map a range of hardware pages
450 tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number)
452 Log_KernelPanic("MM", "TODO: Implement MM_MapHWPages");
457 * \brief Free a range of hardware pages
459 void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
461 Log_KernelPanic("MM", "TODO: Implement MM_UnmapHWPages");
464 // --- Tempory Mappings ---
465 tVAddr MM_MapTemp(tPAddr PAddr)
467 Log_KernelPanic("MM", "TODO: Implement MM_MapTemp");
471 void MM_FreeTemp(tVAddr VAddr)
473 Log_KernelPanic("MM", "TODO: Implement MM_FreeTemp");
478 // --- Address Space Clone --
479 tPAddr MM_Clone(void)
483 // #1 Create a copy of the PML4
484 ret = MM_AllocPhys();
487 // #2 Alter the fractal pointer
488 // #3 Set Copy-On-Write to all user pages
493 void MM_ClearUser(void)
496 // #1 Traverse the structure < 2^47, Deref'ing all pages
497 // #2 Free tables/dirs/pdps once they have been cleared
499 for( addr = 0; addr < 0x800000000000; )
501 if( PAGEMAPLVL4(addr >> PML4_SHIFT) & 1 )
503 if( PAGEDIRPTR(addr >> PDP_SHIFT) & 1 )
505 if( PAGEDIR(addr >> PDIR_SHIFT) & 1 )
508 if( PAGETABLE(addr >> PTAB_SHIFT) & 1 ) {
509 MM_DerefPhys( PAGETABLE(addr >> PTAB_SHIFT) & PADDR_MASK );
510 PAGETABLE(addr >> PTAB_SHIFT) = 0;
512 addr += 1 << PTAB_SHIFT;
513 // Dereference the PDIR Entry
514 if( (addr + (1 << PTAB_SHIFT)) >> PDIR_SHIFT != (addr >> PDIR_SHIFT) ) {
515 MM_DerefPhys( PAGEMAPLVL4(addr >> PDIR_SHIFT) & PADDR_MASK );
516 PAGEDIR(addr >> PDIR_SHIFT) = 0;
520 addr += 1 << PDIR_SHIFT;
523 // Dereference the PDP Entry
524 if( (addr + (1 << PDIR_SHIFT)) >> PDP_SHIFT != (addr >> PDP_SHIFT) ) {
525 MM_DerefPhys( PAGEMAPLVL4(addr >> PDP_SHIFT) & PADDR_MASK );
526 PAGEDIRPTR(addr >> PDP_SHIFT) = 0;
530 addr += 1 << PDP_SHIFT;
533 // Dereference the PML4 Entry
534 if( (addr + (1 << PDP_SHIFT)) >> PML4_SHIFT != (addr >> PML4_SHIFT) ) {
535 MM_DerefPhys( PAGEMAPLVL4(addr >> PML4_SHIFT) & PADDR_MASK );
536 PAGEMAPLVL4(addr >> PML4_SHIFT) = 0;
540 addr += (tVAddr)1 << PML4_SHIFT;
546 tVAddr MM_NewWorkerStack(void)
551 tVAddr MM_NewKStack(void)