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)
209 // Check if the directory is mapped
210 if( gaPageDir[ VAddr >> 22 ] == 0 )
212 // Allocate directory
213 paddr = MM_AllocPhys();
215 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
219 gaPageDir[ VAddr >> 22 ] = paddr | 3;
221 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
223 INVLPG( &gaPageDir[ VAddr >> 22 ] );
224 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
226 // Check if the page is already allocated
227 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
228 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
229 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
233 paddr = MM_AllocPhys();
235 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
236 VAddr, __builtin_return_address(0));
240 gaPageTable[ VAddr >> 12 ] = paddr | 3;
242 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
243 // Invalidate Cache for address
244 INVLPG( VAddr & ~0xFFF );
250 * \fn void MM_Deallocate(Uint VAddr)
252 void MM_Deallocate(Uint VAddr)
254 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
255 Warning("MM_Deallocate - Directory not mapped");
259 if(gaPageTable[ VAddr >> 12 ] == 0) {
260 Warning("MM_Deallocate - Page is not allocated");
265 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
267 gaPageTable[ VAddr >> 12 ] = 0;
271 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
272 * \brief Checks if the passed address is accesable
274 tPAddr MM_GetPhysAddr(Uint Addr)
276 if( !(gaPageDir[Addr >> 22] & 1) )
278 if( !(gaPageTable[Addr >> 12] & 1) )
280 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
284 * \fn void MM_SetCR3(Uint CR3)
285 * \brief Sets the current process space
287 void MM_SetCR3(Uint CR3)
289 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
293 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
294 * \brief Map a physical page to a virtual one
296 int MM_Map(Uint VAddr, tPAddr PAddr)
298 //ENTER("xVAddr xPAddr", VAddr, PAddr);
300 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
301 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
307 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
309 // Check if the directory is mapped
310 if( gaPageDir[ VAddr >> 22 ] == 0 )
312 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
315 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
317 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
318 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
320 // Check if the page is already allocated
321 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
322 Warning("MM_Map - Allocating to used address");
328 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
330 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
332 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
333 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
338 //LOG("INVLPG( 0x%x )", VAddr);
346 * \fn Uint MM_ClearUser()
347 * \brief Clear user's address space
354 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
356 // Check if directory is not allocated
357 if( !(gaPageDir[i] & PF_PRESENT) ) {
363 for( j = 0; j < 1024; j ++ )
365 if( gaPageTable[i*1024+j] & 1 )
366 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
367 gaPageTable[i*1024+j] = 0;
370 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
378 * \fn Uint MM_Clone()
379 * \brief Clone the current address space
384 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
389 // Create Directory Table
390 *gTmpCR3 = MM_AllocPhys() | 3;
392 //LOG("Allocated Directory (%x)", *gTmpCR3);
393 memsetd( gaTmpDir, 0, 1024 );
398 // Check if table is allocated
399 if( !(gaPageDir[i] & PF_PRESENT) ) {
404 // Allocate new table
405 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
406 INVLPG( &gaTmpTable[i*1024] );
408 for( j = 0; j < 1024; j ++ )
410 if( !(gaPageTable[i*1024+j] & PF_PRESENT) ) {
411 gaTmpTable[i*1024+j] = 0;
416 MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
418 if(gaPageTable[i*1024+j] & PF_WRITE) {
419 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
420 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
423 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
427 // Map in kernel tables (and make fractal mapping)
428 for( i = 768; i < 1024; i ++ )
431 if( i == (PAGE_TABLE_ADDR >> 22) ) {
432 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
436 if( gaPageDir[i] == 0 ) {
441 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
442 MM_RefPhys( gaPageDir[i] & ~0xFFF );
443 gaTmpDir[i] = gaPageDir[i];
446 // Allocate kernel stack
447 for(i = KERNEL_STACKS >> 22;
448 i < KERNEL_STACK_END >> 22;
451 // Check if directory is allocated
452 if( (gaPageDir[i] & 1) == 0 ) {
457 // We don't care about other kernel stacks, just the current one
458 if( i != kStackBase >> 22 ) {
459 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
465 gaTmpDir[i] = MM_AllocPhys() | 3;
466 INVLPG( &gaTmpTable[i*1024] );
467 for( j = 0; j < 1024; j ++ )
469 // Is the page allocated? If not, skip
470 if( !(gaPageTable[i*1024+j] & 1) ) {
471 gaTmpTable[i*1024+j] = 0;
475 // We don't care about other kernel stacks
476 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
477 gaTmpTable[i*1024+j] = 0;
482 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
484 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
486 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
487 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
488 MM_FreeTemp( (Uint)tmp );
492 //LEAVE('x', *gTmpCR3 & ~0xFFF);
493 return *gTmpCR3 & ~0xFFF;
497 * \fn Uint MM_NewKStack()
498 * \brief Create a new kernel stack
502 Uint base = KERNEL_STACKS;
504 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
506 if(MM_GetPhysAddr(base) != 0) continue;
507 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
510 return base+KERNEL_STACK_SIZE;
512 Warning("MM_NewKStack - No address space left\n");
517 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
518 * \brief Sets the flags on a page
520 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
523 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
524 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
526 ent = &gaPageTable[VAddr >> 12];
529 if( Mask & MM_PFLAG_RO )
531 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
532 else *ent |= PF_WRITE;
536 if( Mask & MM_PFLAG_KERNEL )
538 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
539 else *ent |= PF_USER;
543 if( Mask & MM_PFLAG_COW )
545 if( Flags & MM_PFLAG_COW ) {
557 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
558 * \brief Duplicates a virtual page to a physical one
560 tPAddr MM_DuplicatePage(Uint VAddr)
567 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
568 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
574 ret = MM_AllocPhys();
576 // Write-lock the page (to keep data constistent), saving its R/W state
577 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
578 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
582 temp = MM_MapTemp(ret);
583 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
586 // Restore Writeable status
587 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
594 * \fn Uint MM_MapTemp(tPAddr PAddr)
595 * \brief Create a temporary memory mapping
596 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
598 Uint MM_MapTemp(tPAddr PAddr)
602 //ENTER("XPAddr", PAddr);
606 //LOG("gilTempMappings = %i", gilTempMappings);
610 LOCK( &gilTempMappings );
612 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
614 // Check if page used
615 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
617 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
618 INVLPG( TEMP_MAP_ADDR + (i << 12) );
619 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
620 RELEASE( &gilTempMappings );
621 return TEMP_MAP_ADDR + (i << 12);
623 RELEASE( &gilTempMappings );
629 * \fn void MM_FreeTemp(Uint PAddr)
630 * \brief Free's a temp mapping
632 void MM_FreeTemp(Uint VAddr)
635 //ENTER("xVAddr", VAddr);
637 if(i >= (TEMP_MAP_ADDR >> 12))
638 gaPageTable[ i ] = 0;
644 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
645 * \brief Allocates a contigous number of pages
647 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
654 for( i = 0; i < NUM_HW_PAGES; i ++ )
656 // Check if addr used
657 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
660 // Check possible region
661 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
663 // If there is an allocated page in the region we are testing, break
664 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
670 for( j = 0; j < Number; j++ ) {
671 MM_RefPhys( PAddr + (j<<12) );
672 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
674 return HW_MAP_ADDR + (i<<12);
677 // If we don't find any, return NULL
682 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
683 * \brief Unmap a hardware page
685 void MM_UnmapHWPage(Uint VAddr, Uint Number)
689 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
693 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
695 for( j = 0; j < Number; j++ )
697 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
698 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
701 RELEASE( &gilTempMappings );
705 EXPORT(MM_GetPhysAddr);
708 EXPORT(MM_MapHWPage);
709 EXPORT(MM_UnmapHWPage);