2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
16 #define KERNEL_STACKS 0xF0000000
17 #define KERNEL_STACK_SIZE 0x00002000
18 #define KERNEL_STACK_END 0xFD000000
19 #define PAGE_TABLE_ADDR 0xFD000000
20 #define PAGE_DIR_ADDR 0xFD3F4000
21 #define PAGE_CR3_ADDR 0xFD3F4FD0
22 #define TMP_CR3_ADDR 0xFD3F4FD4 // Part of core instead of temp
23 #define TMP_DIR_ADDR 0xFD3F5000 // Same
24 #define TMP_TABLE_ADDR 0xFD400000
25 #define HW_MAP_ADDR 0xFD800000
26 #define HW_MAP_MAX 0xFEFF0000
27 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
28 #define TEMP_MAP_ADDR 0xFEFF0000 // Allows 16 "temp" pages
29 #define NUM_TEMP_PAGES 16
31 #define PF_PRESENT 0x1
35 #define PF_PAGED 0x400
37 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
40 extern Uint32 gaInitPageDir[1024];
41 extern Uint32 gaInitPageTable[1024];
44 void MM_PreinitVirtual();
45 void MM_InstallVirtual();
46 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
47 void MM_DumpTables(tVAddr Start, tVAddr End);
48 tPAddr MM_DuplicatePage(Uint VAddr);
51 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
52 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
53 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
54 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
55 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
56 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
57 int gilTempMappings = 0;
61 * \fn void MM_PreinitVirtual()
62 * \brief Maps the fractal mappings
64 void MM_PreinitVirtual()
66 gaInitPageDir[ 0 ] = 0;
67 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
71 * \fn void MM_InstallVirtual()
72 * \brief Sets up the constant page mappings
74 void MM_InstallVirtual()
78 // --- Pre-Allocate kernel tables
79 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
81 if( gaPageDir[ i ] ) continue;
82 // Skip stack tables, they are process unique
83 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACK_END >> 22) {
88 gaPageDir[ i ] = MM_AllocPhys() | 3;
89 INVLPG( &gaPageTable[i*1024] );
90 memset( &gaPageTable[i*1024], 0, 0x1000 );
95 * \fn void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
96 * \brief Called on a page fault
98 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
100 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
102 // -- Check for COW --
103 if( gaPageDir [Addr>>22] & PF_PRESENT
104 && gaPageTable[Addr>>12] & PF_PRESENT
105 && gaPageTable[Addr>>12] & PF_COW )
108 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 0)
110 gaPageTable[Addr>>12] &= ~PF_COW;
111 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
115 paddr = MM_DuplicatePage( Addr );
116 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
117 gaPageTable[Addr>>12] &= PF_USER;
118 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
120 INVLPG( Addr & ~0xFFF );
125 // -- Check Error Code --
127 Warning("Reserved Bits Trashed!");
130 Warning("%s %s %s memory%s",
131 (ErrorCode&4?"User":"Kernel"),
132 (ErrorCode&2?"write to":"read from"),
133 (ErrorCode&1?"bad/locked":"non-present"),
134 (ErrorCode&16?" (Instruction Fetch)":"")
138 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
139 if( gaPageDir[Addr>>22] & PF_PRESENT )
140 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
142 MM_DumpTables(0, -1);
144 Panic("Page Fault at 0x%x\n", Regs->eip);
148 * \fn void MM_DumpTables(Uint Start, Uint End)
149 * \brief Dumps the layout of the page tables
151 void MM_DumpTables(tVAddr Start, tVAddr End)
153 tVAddr rangeStart = 0;
157 const tPAddr MASK = ~0xF98;
159 Start >>= 12; End >>= 12;
160 for(page = Start, curPos = Start<<12;
162 curPos += 0x1000, page++)
164 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
165 || !(gaPageTable[page] & PF_PRESENT)
166 || (gaPageTable[page] & MASK) != expected)
169 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
170 rangeStart, curPos - 1,
171 gaPageTable[rangeStart>>12] & ~0xFFF,
172 (expected & ~0xFFF) - 1,
173 (expected & PF_PAGED ? "p" : "-"),
174 (expected & PF_COW ? "C" : "-"),
175 (expected & PF_USER ? "U" : "-"),
176 (expected & PF_WRITE ? "W" : "-")
180 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
181 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
183 expected = (gaPageTable[page] & MASK);
186 if(expected) expected += 0x1000;
190 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
191 rangeStart, curPos - 1,
192 gaPageTable[rangeStart>>12] & ~0xFFF,
193 (expected & ~0xFFF) - 1,
194 (expected & PF_PAGED ? "p" : "-"),
195 (expected & PF_COW ? "C" : "-"),
196 (expected & PF_USER ? "U" : "-"),
197 (expected & PF_WRITE ? "W" : "-")
204 * \fn tPAddr MM_Allocate(Uint VAddr)
206 tPAddr MM_Allocate(Uint VAddr)
208 // Check if the directory is mapped
209 if( gaPageDir[ VAddr >> 22 ] == 0 )
211 // Allocate directory
212 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
214 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
216 INVLPG( &gaPageDir[ VAddr >> 22 ] );
217 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
219 // Check if the page is already allocated
220 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
221 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
222 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
226 gaPageTable[ VAddr >> 12 ] = MM_AllocPhys() | 3;
228 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
229 // Invalidate Cache for address
230 INVLPG( VAddr & ~0xFFF );
232 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
236 * \fn void MM_Deallocate(Uint VAddr)
238 void MM_Deallocate(Uint VAddr)
240 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
241 Warning("MM_Deallocate - Directory not mapped");
245 if(gaPageTable[ VAddr >> 12 ] == 0) {
246 Warning("MM_Deallocate - Page is not allocated");
251 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
253 gaPageTable[ VAddr >> 12 ] = 0;
257 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
258 * \brief Checks if the passed address is accesable
260 tPAddr MM_GetPhysAddr(Uint Addr)
262 if( !(gaPageDir[Addr >> 22] & 1) )
264 if( !(gaPageTable[Addr >> 12] & 1) )
266 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
270 * \fn void MM_SetCR3(Uint CR3)
271 * \brief Sets the current process space
273 void MM_SetCR3(Uint CR3)
275 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
279 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
280 * \brief Map a physical page to a virtual one
282 int MM_Map(Uint VAddr, tPAddr PAddr)
284 //ENTER("xVAddr xPAddr", VAddr, PAddr);
286 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
287 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
293 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
295 // Check if the directory is mapped
296 if( gaPageDir[ VAddr >> 22 ] == 0 )
298 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
301 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
303 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
304 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
306 // Check if the page is already allocated
307 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
308 Warning("MM_Map - Allocating to used address");
314 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
316 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
318 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
319 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
324 //LOG("INVLPG( 0x%x )", VAddr);
332 * \fn Uint MM_ClearUser()
333 * \brief Clear user's address space
340 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
342 // Check if directory is not allocated
343 if( !(gaPageDir[i] & PF_PRESENT) ) {
349 for( j = 0; j < 1024; j ++ )
351 if( gaPageTable[i*1024+j] & 1 )
352 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
353 gaPageTable[i*1024+j] = 0;
356 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
364 * \fn Uint MM_Clone()
365 * \brief Clone the current address space
370 Uint kStackBase = gCurrentThread->KernelStack - KERNEL_STACK_SIZE;
375 // Create Directory Table
376 *gTmpCR3 = MM_AllocPhys() | 3;
378 //LOG("Allocated Directory (%x)", *gTmpCR3);
379 memsetd( gaTmpDir, 0, 1024 );
384 // Check if table is allocated
385 if( !(gaPageDir[i] & PF_PRESENT) ) {
390 // Allocate new table
391 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
392 INVLPG( &gaTmpTable[i*1024] );
394 for( j = 0; j < 1024; j ++ )
396 if( !(gaPageTable[i*1024+j] & PF_PRESENT) ) {
397 gaTmpTable[i*1024+j] = 0;
402 MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
404 if(gaPageTable[i*1024+j] & PF_WRITE) {
405 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
406 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
409 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
413 // Map in kernel tables (and make fractal mapping)
414 for( i = 768; i < 1024; i ++ )
417 if( i == (PAGE_TABLE_ADDR >> 22) ) {
418 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
422 if( gaPageDir[i] == 0 ) {
427 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
428 MM_RefPhys( gaPageDir[i] & ~0xFFF );
429 gaTmpDir[i] = gaPageDir[i];
432 // Allocate kernel stack
433 for(i = KERNEL_STACKS >> 22;
434 i < KERNEL_STACK_END >> 22;
437 // Check if directory is allocated
438 if( (gaPageDir[i] & 1) == 0 ) {
443 // We don't care about other kernel stacks, just the current one
444 if( i != kStackBase >> 22 ) {
445 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
451 gaTmpDir[i] = MM_AllocPhys() | 3;
452 INVLPG( &gaTmpTable[i*1024] );
453 for( j = 0; j < 1024; j ++ )
455 // Is the page allocated? If not, skip
456 if( !(gaPageTable[i*1024+j] & 1) ) {
457 gaTmpTable[i*1024+j] = 0;
461 // We don't care about other kernel stacks
462 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
463 gaTmpTable[i*1024+j] = 0;
468 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
470 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
472 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
473 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
474 MM_FreeTemp( (Uint)tmp );
478 //LEAVE('x', *gTmpCR3 & ~0xFFF);
479 return *gTmpCR3 & ~0xFFF;
483 * \fn Uint MM_NewKStack()
484 * \brief Create a new kernel stack
488 Uint base = KERNEL_STACKS;
490 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
492 if(MM_GetPhysAddr(base) != 0) continue;
493 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
496 return base+KERNEL_STACK_SIZE;
498 Warning("MM_NewKStack - No address space left\n");
503 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
504 * \brief Sets the flags on a page
506 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
509 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
510 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
512 ent = &gaPageTable[VAddr >> 12];
515 if( Mask & MM_PFLAG_RO )
517 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
518 else *ent |= PF_WRITE;
522 if( Mask & MM_PFLAG_KERNEL )
524 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
525 else *ent |= PF_USER;
529 if( Mask & MM_PFLAG_COW )
531 if( Flags & MM_PFLAG_COW ) {
543 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
544 * \brief Duplicates a virtual page to a physical one
546 tPAddr MM_DuplicatePage(Uint VAddr)
553 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
554 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
560 ret = MM_AllocPhys();
562 // Write-lock the page (to keep data constistent), saving its R/W state
563 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
564 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
568 temp = MM_MapTemp(ret);
569 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
572 // Restore Writeable status
573 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
580 * \fn Uint MM_MapTemp(tPAddr PAddr)
581 * \brief Create a temporary memory mapping
582 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
584 Uint MM_MapTemp(tPAddr PAddr)
588 //ENTER("XPAddr", PAddr);
592 //LOG("gilTempMappings = %i", gilTempMappings);
596 LOCK( &gilTempMappings );
598 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
600 // Check if page used
601 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
603 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
604 INVLPG( TEMP_MAP_ADDR + (i << 12) );
605 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
606 RELEASE( &gilTempMappings );
607 return TEMP_MAP_ADDR + (i << 12);
609 RELEASE( &gilTempMappings );
615 * \fn void MM_FreeTemp(Uint PAddr)
616 * \brief Free's a temp mapping
618 void MM_FreeTemp(Uint VAddr)
621 //ENTER("xVAddr", VAddr);
623 if(i >= (TEMP_MAP_ADDR >> 12))
624 gaPageTable[ i ] = 0;
630 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
631 * \brief Allocates a contigous number of pages
633 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
640 for( i = 0; i < NUM_HW_PAGES; i ++ )
642 // Check if addr used
643 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
646 // Check possible region
647 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
649 // If there is an allocated page in the region we are testing, break
650 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
656 for( j = 0; j < Number; j++ ) {
657 MM_RefPhys( PAddr + (j<<12) );
658 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
660 return HW_MAP_ADDR + (i<<12);
663 // If we don't find any, return NULL
668 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
669 * \brief Unmap a hardware page
671 void MM_UnmapHWPage(Uint VAddr, Uint Number)
675 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
679 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
681 for( j = 0; j < Number; j++ )
683 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
684 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
687 RELEASE( &gilTempMappings );
691 EXPORT(MM_GetPhysAddr);
694 EXPORT(MM_MapHWPage);
695 EXPORT(MM_UnmapHWPage);