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 paddr = MM_DuplicatePage( Addr );
109 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
110 gaPageTable[Addr>>12] &= PF_USER;
111 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
112 INVLPG( Addr & ~0xFFF );
117 // -- Check Error Code --
119 Warning("Reserved Bits Trashed!");
122 Warning("%s %s %s memory%s",
123 (ErrorCode&4?"User":"Kernel"),
124 (ErrorCode&2?"write to":"read from"),
125 (ErrorCode&1?"bad/locked":"non-present"),
126 (ErrorCode&16?" (Instruction Fetch)":"")
130 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
131 if( gaPageDir[Addr>>22] & PF_PRESENT )
132 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
134 MM_DumpTables(0, -1);
136 Panic("Page Fault at 0x%x\n", Regs->eip);
140 * \fn void MM_DumpTables(Uint Start, Uint End)
141 * \brief Dumps the layout of the page tables
143 void MM_DumpTables(tVAddr Start, tVAddr End)
145 tVAddr rangeStart = 0;
149 const tPAddr MASK = ~0xF98;
151 Start >>= 12; End >>= 12;
152 for(page = Start, curPos = Start<<12;
154 curPos += 0x1000, page++)
156 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
157 || !(gaPageTable[page] & PF_PRESENT)
158 || (gaPageTable[page] & MASK) != expected)
161 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
162 rangeStart, curPos - 1,
163 gaPageTable[rangeStart>>12] & ~0xFFF,
164 (expected & ~0xFFF) - 1,
165 (expected & PF_PAGED ? "p" : "-"),
166 (expected & PF_COW ? "C" : "-"),
167 (expected & PF_USER ? "U" : "-"),
168 (expected & PF_WRITE ? "W" : "-")
172 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
173 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
175 expected = (gaPageTable[page] & MASK);
178 if(expected) expected += 0x1000;
182 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
183 rangeStart, curPos - 1,
184 gaPageTable[rangeStart>>12] & ~0xFFF,
185 (expected & ~0xFFF) - 1,
186 (expected & PF_PAGED ? "p" : "-"),
187 (expected & PF_COW ? "C" : "-"),
188 (expected & PF_USER ? "U" : "-"),
189 (expected & PF_WRITE ? "W" : "-")
196 * \fn tPAddr MM_Allocate(Uint VAddr)
198 tPAddr MM_Allocate(Uint VAddr)
200 // Check if the directory is mapped
201 if( gaPageDir[ VAddr >> 22 ] == 0 )
203 // Allocate directory
204 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
206 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
208 INVLPG( &gaPageDir[ VAddr >> 22 ] );
209 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
211 // Check if the page is already allocated
212 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
213 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
214 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
218 gaPageTable[ VAddr >> 12 ] = MM_AllocPhys() | 3;
220 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
221 // Invalidate Cache for address
222 INVLPG( VAddr & ~0xFFF );
224 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
228 * \fn void MM_Deallocate(Uint VAddr)
230 void MM_Deallocate(Uint VAddr)
232 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
233 Warning("MM_Deallocate - Directory not mapped");
237 if(gaPageTable[ VAddr >> 12 ] == 0) {
238 Warning("MM_Deallocate - Page is not allocated");
243 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
245 gaPageTable[ VAddr >> 12 ] = 0;
249 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
250 * \brief Checks if the passed address is accesable
252 tPAddr MM_GetPhysAddr(Uint Addr)
254 if( !(gaPageDir[Addr >> 22] & 1) )
256 if( !(gaPageTable[Addr >> 12] & 1) )
258 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
262 * \fn void MM_SetCR3(Uint CR3)
263 * \brief Sets the current process space
265 void MM_SetCR3(Uint CR3)
267 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
271 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
272 * \brief Map a physical page to a virtual one
274 int MM_Map(Uint VAddr, tPAddr PAddr)
276 //ENTER("xVAddr xPAddr", VAddr, PAddr);
278 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
279 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
285 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
287 // Check if the directory is mapped
288 if( gaPageDir[ VAddr >> 22 ] == 0 )
290 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
293 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
295 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
296 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
298 // Check if the page is already allocated
299 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
300 Warning("MM_Map - Allocating to used address");
306 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
308 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
310 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
311 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
316 //LOG("INVLPG( 0x%x )", VAddr);
324 * \fn Uint MM_ClearUser()
325 * \brief Clear user's address space
332 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
334 // Check if directory is not allocated
335 if( !(gaPageDir[i] & PF_PRESENT) ) {
341 for( j = 0; j < 1024; j ++ )
343 if( gaPageTable[i*1024+j] & 1 )
344 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
345 gaPageTable[i*1024+j] = 0;
348 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
356 * \fn Uint MM_Clone()
357 * \brief Clone the current address space
362 Uint kStackBase = gCurrentThread->KernelStack - KERNEL_STACK_SIZE;
367 // Create Directory Table
368 *gTmpCR3 = MM_AllocPhys() | 3;
370 //LOG("Allocated Directory (%x)", *gTmpCR3);
371 memsetd( gaTmpDir, 0, 1024 );
376 // Check if table is allocated
377 if( !(gaPageDir[i] & PF_PRESENT) ) {
382 // Allocate new table
383 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
384 INVLPG( &gaTmpTable[i*1024] );
386 for( j = 0; j < 1024; j ++ )
388 if( !(gaPageTable[i*1024+j] & PF_PRESENT) ) {
389 gaTmpTable[i*1024+j] = 0;
394 MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
396 if(gaPageTable[i*1024+j] & PF_WRITE) {
397 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
398 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
401 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
405 // Map in kernel tables (and make fractal mapping)
406 for( i = 768; i < 1024; i ++ )
409 if( i == (PAGE_TABLE_ADDR >> 22) ) {
410 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
414 if( gaPageDir[i] == 0 ) {
419 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
420 MM_RefPhys( gaPageDir[i] & ~0xFFF );
421 gaTmpDir[i] = gaPageDir[i];
424 // Allocate kernel stack
425 for(i = KERNEL_STACKS >> 22;
426 i < KERNEL_STACK_END >> 22;
429 // Check if directory is allocated
430 if( (gaPageDir[i] & 1) == 0 ) {
435 // We don't care about other kernel stacks, just the current one
436 if( i != kStackBase >> 22 ) {
437 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
443 gaTmpDir[i] = MM_AllocPhys() | 3;
444 INVLPG( &gaTmpTable[i*1024] );
445 for( j = 0; j < 1024; j ++ )
447 // Is the page allocated? If not, skip
448 if( !(gaPageTable[i*1024+j] & 1) ) {
449 gaTmpTable[i*1024+j] = 0;
453 // We don't care about other kernel stacks
454 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
455 gaTmpTable[i*1024+j] = 0;
460 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
462 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
464 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
465 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
466 MM_FreeTemp( (Uint)tmp );
470 //LEAVE('x', *gTmpCR3 & ~0xFFF);
471 return *gTmpCR3 & ~0xFFF;
475 * \fn Uint MM_NewKStack()
476 * \brief Create a new kernel stack
480 Uint base = KERNEL_STACKS;
482 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
484 if(MM_GetPhysAddr(base) != 0) continue;
485 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
488 return base+KERNEL_STACK_SIZE;
490 Warning("MM_NewKStack - No address space left\n");
495 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
496 * \brief Sets the flags on a page
498 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
501 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
502 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
504 ent = &gaPageTable[VAddr >> 12];
507 if( Mask & MM_PFLAG_RO )
509 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
510 else *ent |= PF_WRITE;
514 if( Mask & MM_PFLAG_KERNEL )
516 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
517 else *ent |= PF_USER;
521 if( Mask & MM_PFLAG_COW )
523 if( Flags & MM_PFLAG_COW ) {
535 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
536 * \brief Duplicates a virtual page to a physical one
538 tPAddr MM_DuplicatePage(Uint VAddr)
545 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
546 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
552 ret = MM_AllocPhys();
554 // Write-lock the page (to keep data constistent), saving its R/W state
555 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
556 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
560 temp = MM_MapTemp(ret);
561 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
564 // Restore Writeable status
565 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
572 * \fn Uint MM_MapTemp(tPAddr PAddr)
573 * \brief Create a temporary memory mapping
574 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
576 Uint MM_MapTemp(tPAddr PAddr)
580 //ENTER("XPAddr", PAddr);
584 //LOG("gilTempMappings = %i", gilTempMappings);
588 LOCK( &gilTempMappings );
590 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
592 // Check if page used
593 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
595 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
596 INVLPG( TEMP_MAP_ADDR + (i << 12) );
597 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
598 RELEASE( &gilTempMappings );
599 return TEMP_MAP_ADDR + (i << 12);
601 RELEASE( &gilTempMappings );
607 * \fn void MM_FreeTemp(Uint PAddr)
608 * \brief Free's a temp mapping
610 void MM_FreeTemp(Uint VAddr)
613 //ENTER("xVAddr", VAddr);
615 if(i >= (TEMP_MAP_ADDR >> 12))
616 gaPageTable[ i ] = 0;
622 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
623 * \brief Allocates a contigous number of pages
625 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
632 for( i = 0; i < NUM_HW_PAGES; i ++ )
634 // Check if addr used
635 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
638 // Check possible region
639 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
641 // If there is an allocated page in the region we are testing, break
642 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
648 for( j = 0; j < Number; j++ ) {
649 MM_RefPhys( PAddr + (j<<12) );
650 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
652 return HW_MAP_ADDR + (i<<12);
655 // If we don't find any, return NULL
660 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
661 * \brief Unmap a hardware page
663 void MM_UnmapHWPage(Uint VAddr, Uint Number)
667 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
671 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
673 for( j = 0; j < Number; j++ )
675 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
676 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
679 RELEASE( &gilTempMappings );
683 EXPORT(MM_GetPhysAddr);
686 EXPORT(MM_MapHWPage);
687 EXPORT(MM_UnmapHWPage);