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 0xFD3F47F4
22 //#define TMP_CR3_ADDR 0xFD3F47F8 // Part of core instead of temp
23 #define PAGE_CR3_ADDR 0xFD3F4FD0
24 #define TMP_CR3_ADDR 0xFD3F4FD4 // Part of core instead of temp
25 #define TMP_DIR_ADDR 0xFD3F5000 // Same
26 #define TMP_TABLE_ADDR 0xFD400000
27 #define HW_MAP_ADDR 0xFD800000
28 #define HW_MAP_MAX 0xFEFF0000
29 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
30 #define TEMP_MAP_ADDR 0xFEFF0000 // Allows 16 "temp" pages
31 #define NUM_TEMP_PAGES 16
35 #define PF_PRESENT 0x1
39 #define PF_PAGED 0x400
41 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
44 extern Uint32 gaInitPageDir[1024];
45 extern Uint32 gaInitPageTable[1024];
48 void MM_PreinitVirtual();
49 void MM_InstallVirtual();
50 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
51 void MM_DumpTables(tVAddr Start, tVAddr End);
52 tPAddr MM_DuplicatePage(Uint VAddr);
55 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
56 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
57 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
58 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
59 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
60 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
61 int gilTempMappings = 0;
65 * \fn void MM_PreinitVirtual()
67 void MM_PreinitVirtual()
69 gaInitPageDir[ 0 ] = 0;
70 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
73 * \fn void MM_InstallVirtual()
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 paddr = MM_DuplicatePage( Addr );
110 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
111 gaPageTable[Addr>>12] &= PF_USER;
112 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
113 INVLPG( Addr & ~0xFFF );
116 // -- Check Error Code --
118 Warning("Reserved Bits Trashed!");
121 Warning("%s %s %s memory%s",
122 (ErrorCode&4?"User":"Kernel"),
123 (ErrorCode&2?"write to":"read from"),
124 (ErrorCode&1?"bad/locked":"non-present"),
125 (ErrorCode&16?" (Instruction Fetch)":"")
129 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
130 if( gaPageDir[Addr>>22] & PF_PRESENT )
131 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
133 MM_DumpTables(0, -1);
135 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");
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;
395 MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
397 if(gaPageTable[i*1024+j] & PF_WRITE) {
398 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
399 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
402 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
403 LOG("gaTmpTable[0x%x] = 0x%x", i*1024+j, gaTmpTable[i*1024+j]);
405 gaTmpTable[i*1024+j] = MM_DuplicatePage( (i*1024+j)<<12 ) | (gaPageTable[i*1024+j]&7);
410 // Map in kernel tables (and make fractal mapping)
411 for( i = 768; i < 1024; i ++ )
414 if( i == (PAGE_TABLE_ADDR >> 22) ) {
415 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
419 if( gaPageDir[i] == 0 ) {
424 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
425 MM_RefPhys( gaPageDir[i] & ~0xFFF );
426 gaTmpDir[i] = gaPageDir[i];
429 // Allocate kernel stack
430 for(i = KERNEL_STACKS >> 22;
431 i < KERNEL_STACK_END >> 22;
434 // Check if directory is allocated
435 if( (gaPageDir[i] & 1) == 0 ) {
440 // We don't care about other kernel stacks, just the current one
441 if( i != kStackBase >> 22 ) {
442 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
448 gaTmpDir[i] = MM_AllocPhys() | 3;
449 INVLPG( &gaTmpTable[i*1024] );
450 for( j = 0; j < 1024; j ++ )
452 // Is the page allocated? If not, skip
453 if( !(gaPageTable[i*1024+j] & 1) ) {
454 gaTmpTable[i*1024+j] = 0;
458 // We don't care about other kernel stacks
459 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
460 gaTmpTable[i*1024+j] = 0;
465 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
467 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
469 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
470 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
471 MM_FreeTemp( (Uint)tmp );
475 //LEAVE('x', *gTmpCR3 & ~0xFFF);
476 return *gTmpCR3 & ~0xFFF;
480 * \fn Uint MM_NewKStack()
481 * \brief Create a new kernel stack
485 Uint base = KERNEL_STACKS;
487 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
489 if(MM_GetPhysAddr(base) != 0) continue;
490 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
493 return base+KERNEL_STACK_SIZE;
495 Warning("MM_NewKStack - No address space left\n");
500 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
501 * \brief Sets the flags on a page
503 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
506 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
507 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
509 ent = &gaPageTable[VAddr >> 12];
512 if( Mask & MM_PFLAG_RO )
514 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
515 else *ent |= PF_WRITE;
519 if( Mask & MM_PFLAG_KERNEL )
521 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
522 else *ent |= PF_USER;
527 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
528 * \brief Duplicates a virtual page to a physical one
530 tPAddr MM_DuplicatePage(Uint VAddr)
537 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
538 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
544 ret = MM_AllocPhys();
546 // Write-lock the page (to keep data constistent), saving its R/W state
547 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
548 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
552 temp = MM_MapTemp(ret);
553 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
556 // Restore Writeable status
557 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
564 * \fn Uint MM_MapTemp(tPAddr PAddr)
565 * \brief Create a temporary memory mapping
566 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
568 Uint MM_MapTemp(tPAddr PAddr)
572 //ENTER("XPAddr", PAddr);
576 //LOG("gilTempMappings = %i", gilTempMappings);
580 LOCK( &gilTempMappings );
582 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
584 // Check if page used
585 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
587 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
588 INVLPG( TEMP_MAP_ADDR + (i << 12) );
589 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
590 RELEASE( &gilTempMappings );
591 return TEMP_MAP_ADDR + (i << 12);
593 RELEASE( &gilTempMappings );
599 * \fn void MM_FreeTemp(Uint PAddr)
600 * \brief Free's a temp mapping
602 void MM_FreeTemp(Uint VAddr)
605 //ENTER("xVAddr", VAddr);
607 if(i >= (TEMP_MAP_ADDR >> 12))
608 gaPageTable[ i ] = 0;
614 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
615 * \brief Allocates a contigous number of pages
617 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
624 for( i = 0; i < NUM_HW_PAGES; i ++ )
626 // Check if addr used
627 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
630 // Check possible region
631 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
633 // If there is an allocated page in the region we are testing, break
634 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
640 for( j = 0; j < Number; j++ ) {
641 MM_RefPhys( PAddr + (j<<12) );
642 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
644 return HW_MAP_ADDR + (i<<12);
647 // If we don't find any, return NULL
652 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
653 * \brief Unmap a hardware page
655 void MM_UnmapHWPage(Uint VAddr, Uint Number)
659 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
663 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
665 for( j = 0; j < Number; j++ )
667 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
668 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
671 RELEASE( &gilTempMappings );
675 EXPORT(MM_GetPhysAddr);
678 EXPORT(MM_MapHWPage);
679 EXPORT(MM_UnmapHWPage);