2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
17 #define KERNEL_STACKS 0xF0000000
18 #define KERNEL_STACK_SIZE 0x00002000
19 #define KERNEL_STACK_END 0xFD000000
20 #define PAGE_TABLE_ADDR 0xFD000000
21 #define PAGE_DIR_ADDR 0xFD3F4000
22 #define PAGE_CR3_ADDR 0xFD3F4FD0
23 #define TMP_CR3_ADDR 0xFD3F4FD4 // Part of core instead of temp
24 #define TMP_DIR_ADDR 0xFD3F5000 // Same
25 #define TMP_TABLE_ADDR 0xFD400000
26 #define HW_MAP_ADDR 0xFD800000
27 #define HW_MAP_MAX 0xFEFF0000
28 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
29 #define TEMP_MAP_ADDR 0xFEFF0000 // Allows 16 "temp" pages
30 #define NUM_TEMP_PAGES 16
32 #define PF_PRESENT 0x1
36 #define PF_PAGED 0x400
38 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
41 extern Uint32 gaInitPageDir[1024];
42 extern Uint32 gaInitPageTable[1024];
45 void MM_PreinitVirtual();
46 void MM_InstallVirtual();
47 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
48 void MM_DumpTables(tVAddr Start, tVAddr End);
49 tPAddr MM_DuplicatePage(Uint VAddr);
52 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
53 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
54 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
55 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
56 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
57 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
58 int gilTempMappings = 0;
62 * \fn void MM_PreinitVirtual()
63 * \brief Maps the fractal mappings
65 void MM_PreinitVirtual()
67 gaInitPageDir[ 0 ] = 0;
68 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
72 * \fn void MM_InstallVirtual()
73 * \brief Sets up the constant page mappings
75 void MM_InstallVirtual()
79 // --- Pre-Allocate kernel tables
80 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
82 if( gaPageDir[ i ] ) continue;
83 // Skip stack tables, they are process unique
84 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACK_END >> 22) {
89 gaPageDir[ i ] = MM_AllocPhys() | 3;
90 INVLPG( &gaPageTable[i*1024] );
91 memset( &gaPageTable[i*1024], 0, 0x1000 );
96 * \fn void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
97 * \brief Called on a page fault
99 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
101 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
103 // -- Check for COW --
104 if( gaPageDir [Addr>>22] & PF_PRESENT
105 && gaPageTable[Addr>>12] & PF_PRESENT
106 && gaPageTable[Addr>>12] & PF_COW )
109 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
111 gaPageTable[Addr>>12] &= ~PF_COW;
112 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
116 paddr = MM_DuplicatePage( Addr );
117 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
118 gaPageTable[Addr>>12] &= PF_USER;
119 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
121 LOG("Duplicated page at %p to 0x%x", Addr&~0xFFF, gaPageTable[Addr>>12]);
122 INVLPG( Addr & ~0xFFF );
127 // -- Check Error Code --
129 Warning("Reserved Bits Trashed!");
132 Warning("%s %s %s memory%s",
133 (ErrorCode&4?"User":"Kernel"),
134 (ErrorCode&2?"write to":"read from"),
135 (ErrorCode&1?"bad/locked":"non-present"),
136 (ErrorCode&16?" (Instruction Fetch)":"")
140 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
141 if( gaPageDir[Addr>>22] & PF_PRESENT )
142 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
144 MM_DumpTables(0, -1);
146 Panic("Page Fault at 0x%x\n", Regs->eip);
150 * \fn void MM_DumpTables(Uint Start, Uint End)
151 * \brief Dumps the layout of the page tables
153 void MM_DumpTables(tVAddr Start, tVAddr End)
155 tVAddr rangeStart = 0;
159 const tPAddr MASK = ~0xF98;
161 Start >>= 12; End >>= 12;
162 for(page = Start, curPos = Start<<12;
164 curPos += 0x1000, page++)
166 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
167 || !(gaPageTable[page] & PF_PRESENT)
168 || (gaPageTable[page] & MASK) != expected)
171 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
172 rangeStart, curPos - 1,
173 gaPageTable[rangeStart>>12] & ~0xFFF,
174 (expected & ~0xFFF) - 1,
175 (expected & PF_PAGED ? "p" : "-"),
176 (expected & PF_COW ? "C" : "-"),
177 (expected & PF_USER ? "U" : "-"),
178 (expected & PF_WRITE ? "W" : "-")
182 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
183 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
185 expected = (gaPageTable[page] & MASK);
188 if(expected) expected += 0x1000;
192 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
193 rangeStart, curPos - 1,
194 gaPageTable[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 * \fn tPAddr MM_Allocate(Uint VAddr)
208 tPAddr MM_Allocate(Uint VAddr)
211 // Check if the directory is mapped
212 if( gaPageDir[ VAddr >> 22 ] == 0 )
214 // Allocate directory
215 paddr = MM_AllocPhys();
217 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
221 gaPageDir[ VAddr >> 22 ] = paddr | 3;
223 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
225 INVLPG( &gaPageDir[ VAddr >> 22 ] );
226 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
228 // Check if the page is already allocated
229 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
230 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
231 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
235 paddr = MM_AllocPhys();
237 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
238 VAddr, __builtin_return_address(0));
242 gaPageTable[ VAddr >> 12 ] = paddr | 3;
244 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
245 // Invalidate Cache for address
246 INVLPG( VAddr & ~0xFFF );
252 * \fn void MM_Deallocate(Uint VAddr)
254 void MM_Deallocate(Uint VAddr)
256 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
257 Warning("MM_Deallocate - Directory not mapped");
261 if(gaPageTable[ VAddr >> 12 ] == 0) {
262 Warning("MM_Deallocate - Page is not allocated");
267 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
269 gaPageTable[ VAddr >> 12 ] = 0;
273 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
274 * \brief Checks if the passed address is accesable
276 tPAddr MM_GetPhysAddr(Uint Addr)
278 if( !(gaPageDir[Addr >> 22] & 1) )
280 if( !(gaPageTable[Addr >> 12] & 1) )
282 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
286 * \fn void MM_SetCR3(Uint CR3)
287 * \brief Sets the current process space
289 void MM_SetCR3(Uint CR3)
291 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
295 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
296 * \brief Map a physical page to a virtual one
298 int MM_Map(Uint VAddr, tPAddr PAddr)
300 //ENTER("xVAddr xPAddr", VAddr, PAddr);
302 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
303 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
309 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
311 // Check if the directory is mapped
312 if( gaPageDir[ VAddr >> 22 ] == 0 )
314 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
317 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
319 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
320 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
322 // Check if the page is already allocated
323 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
324 Warning("MM_Map - Allocating to used address");
330 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
332 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
334 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
335 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
340 //LOG("INVLPG( 0x%x )", VAddr);
348 * \fn Uint MM_ClearUser()
349 * \brief Clear user's address space
356 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
358 // Check if directory is not allocated
359 if( !(gaPageDir[i] & PF_PRESENT) ) {
365 for( j = 0; j < 1024; j ++ )
367 if( gaPageTable[i*1024+j] & 1 )
368 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
369 gaPageTable[i*1024+j] = 0;
372 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
380 * \fn Uint MM_Clone()
381 * \brief Clone the current address space
386 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
391 // Create Directory Table
392 *gTmpCR3 = MM_AllocPhys() | 3;
394 //LOG("Allocated Directory (%x)", *gTmpCR3);
395 memsetd( gaTmpDir, 0, 1024 );
400 // Check if table is allocated
401 if( !(gaPageDir[i] & PF_PRESENT) ) {
406 // Allocate new table
407 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
408 INVLPG( &gaTmpTable[i*1024] );
410 for( j = 0; j < 1024; j ++ )
412 if( !(gaPageTable[i*1024+j] & PF_PRESENT) ) {
413 gaTmpTable[i*1024+j] = 0;
418 MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
420 if(gaPageTable[i*1024+j] & PF_WRITE) {
421 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
422 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
425 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
429 // Map in kernel tables (and make fractal mapping)
430 for( i = 768; i < 1024; i ++ )
433 if( i == (PAGE_TABLE_ADDR >> 22) ) {
434 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
438 if( gaPageDir[i] == 0 ) {
443 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
444 MM_RefPhys( gaPageDir[i] & ~0xFFF );
445 gaTmpDir[i] = gaPageDir[i];
448 // Allocate kernel stack
449 for(i = KERNEL_STACKS >> 22;
450 i < KERNEL_STACK_END >> 22;
453 // Check if directory is allocated
454 if( (gaPageDir[i] & 1) == 0 ) {
459 // We don't care about other kernel stacks, just the current one
460 if( i != kStackBase >> 22 ) {
461 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
467 gaTmpDir[i] = MM_AllocPhys() | 3;
468 INVLPG( &gaTmpTable[i*1024] );
469 for( j = 0; j < 1024; j ++ )
471 // Is the page allocated? If not, skip
472 if( !(gaPageTable[i*1024+j] & 1) ) {
473 gaTmpTable[i*1024+j] = 0;
477 // We don't care about other kernel stacks
478 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
479 gaTmpTable[i*1024+j] = 0;
484 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
486 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
488 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
489 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
490 MM_FreeTemp( (Uint)tmp );
494 //LEAVE('x', *gTmpCR3 & ~0xFFF);
495 return *gTmpCR3 & ~0xFFF;
499 * \fn Uint MM_NewKStack()
500 * \brief Create a new kernel stack
504 Uint base = KERNEL_STACKS;
506 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
508 if(MM_GetPhysAddr(base) != 0) continue;
509 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
512 return base+KERNEL_STACK_SIZE;
514 Warning("MM_NewKStack - No address space left\n");
519 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
520 * \brief Sets the flags on a page
522 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
525 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
526 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
528 ent = &gaPageTable[VAddr >> 12];
531 if( Mask & MM_PFLAG_RO )
533 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
534 else *ent |= PF_WRITE;
538 if( Mask & MM_PFLAG_KERNEL )
540 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
541 else *ent |= PF_USER;
545 if( Mask & MM_PFLAG_COW )
547 if( Flags & MM_PFLAG_COW ) {
559 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
560 * \brief Duplicates a virtual page to a physical one
562 tPAddr MM_DuplicatePage(Uint VAddr)
569 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
570 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
576 ret = MM_AllocPhys();
578 // Write-lock the page (to keep data constistent), saving its R/W state
579 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
580 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
584 temp = MM_MapTemp(ret);
585 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
588 // Restore Writeable status
589 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
596 * \fn Uint MM_MapTemp(tPAddr PAddr)
597 * \brief Create a temporary memory mapping
598 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
600 Uint MM_MapTemp(tPAddr PAddr)
604 //ENTER("XPAddr", PAddr);
608 //LOG("gilTempMappings = %i", gilTempMappings);
612 LOCK( &gilTempMappings );
614 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
616 // Check if page used
617 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
619 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
620 INVLPG( TEMP_MAP_ADDR + (i << 12) );
621 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
622 RELEASE( &gilTempMappings );
623 return TEMP_MAP_ADDR + (i << 12);
625 RELEASE( &gilTempMappings );
631 * \fn void MM_FreeTemp(Uint PAddr)
632 * \brief Free's a temp mapping
634 void MM_FreeTemp(Uint VAddr)
637 //ENTER("xVAddr", VAddr);
639 if(i >= (TEMP_MAP_ADDR >> 12))
640 gaPageTable[ i ] = 0;
646 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
647 * \brief Allocates a contigous number of pages
649 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
656 for( i = 0; i < NUM_HW_PAGES; i ++ )
658 // Check if addr used
659 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
662 // Check possible region
663 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
665 // If there is an allocated page in the region we are testing, break
666 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
672 for( j = 0; j < Number; j++ ) {
673 MM_RefPhys( PAddr + (j<<12) );
674 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
676 return HW_MAP_ADDR + (i<<12);
679 // If we don't find any, return NULL
684 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
685 * \brief Unmap a hardware page
687 void MM_UnmapHWPage(Uint VAddr, Uint Number)
691 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
695 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
697 for( j = 0; j < Number; j++ )
699 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
700 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
703 RELEASE( &gilTempMappings );
707 EXPORT(MM_GetPhysAddr);
710 EXPORT(MM_MapHWPage);
711 EXPORT(MM_UnmapHWPage);