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 (Called by %p)", __builtin_return_address(0));
239 gaPageTable[ VAddr >> 12 ] = paddr | 3;
241 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
242 // Invalidate Cache for address
243 INVLPG( VAddr & ~0xFFF );
249 * \fn void MM_Deallocate(Uint VAddr)
251 void MM_Deallocate(Uint VAddr)
253 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
254 Warning("MM_Deallocate - Directory not mapped");
258 if(gaPageTable[ VAddr >> 12 ] == 0) {
259 Warning("MM_Deallocate - Page is not allocated");
264 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
266 gaPageTable[ VAddr >> 12 ] = 0;
270 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
271 * \brief Checks if the passed address is accesable
273 tPAddr MM_GetPhysAddr(Uint Addr)
275 if( !(gaPageDir[Addr >> 22] & 1) )
277 if( !(gaPageTable[Addr >> 12] & 1) )
279 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
283 * \fn void MM_SetCR3(Uint CR3)
284 * \brief Sets the current process space
286 void MM_SetCR3(Uint CR3)
288 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
292 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
293 * \brief Map a physical page to a virtual one
295 int MM_Map(Uint VAddr, tPAddr PAddr)
297 //ENTER("xVAddr xPAddr", VAddr, PAddr);
299 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
300 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
306 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
308 // Check if the directory is mapped
309 if( gaPageDir[ VAddr >> 22 ] == 0 )
311 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
314 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
316 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
317 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
319 // Check if the page is already allocated
320 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
321 Warning("MM_Map - Allocating to used address");
327 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
329 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
331 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
332 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
337 //LOG("INVLPG( 0x%x )", VAddr);
345 * \fn Uint MM_ClearUser()
346 * \brief Clear user's address space
353 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
355 // Check if directory is not allocated
356 if( !(gaPageDir[i] & PF_PRESENT) ) {
362 for( j = 0; j < 1024; j ++ )
364 if( gaPageTable[i*1024+j] & 1 )
365 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
366 gaPageTable[i*1024+j] = 0;
369 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
377 * \fn Uint MM_Clone()
378 * \brief Clone the current address space
383 Uint kStackBase = gCurrentThread->KernelStack - KERNEL_STACK_SIZE;
388 // Create Directory Table
389 *gTmpCR3 = MM_AllocPhys() | 3;
391 //LOG("Allocated Directory (%x)", *gTmpCR3);
392 memsetd( gaTmpDir, 0, 1024 );
397 // Check if table is allocated
398 if( !(gaPageDir[i] & PF_PRESENT) ) {
403 // Allocate new table
404 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
405 INVLPG( &gaTmpTable[i*1024] );
407 for( j = 0; j < 1024; j ++ )
409 if( !(gaPageTable[i*1024+j] & PF_PRESENT) ) {
410 gaTmpTable[i*1024+j] = 0;
415 MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
417 if(gaPageTable[i*1024+j] & PF_WRITE) {
418 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
419 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
422 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
426 // Map in kernel tables (and make fractal mapping)
427 for( i = 768; i < 1024; i ++ )
430 if( i == (PAGE_TABLE_ADDR >> 22) ) {
431 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
435 if( gaPageDir[i] == 0 ) {
440 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
441 MM_RefPhys( gaPageDir[i] & ~0xFFF );
442 gaTmpDir[i] = gaPageDir[i];
445 // Allocate kernel stack
446 for(i = KERNEL_STACKS >> 22;
447 i < KERNEL_STACK_END >> 22;
450 // Check if directory is allocated
451 if( (gaPageDir[i] & 1) == 0 ) {
456 // We don't care about other kernel stacks, just the current one
457 if( i != kStackBase >> 22 ) {
458 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
464 gaTmpDir[i] = MM_AllocPhys() | 3;
465 INVLPG( &gaTmpTable[i*1024] );
466 for( j = 0; j < 1024; j ++ )
468 // Is the page allocated? If not, skip
469 if( !(gaPageTable[i*1024+j] & 1) ) {
470 gaTmpTable[i*1024+j] = 0;
474 // We don't care about other kernel stacks
475 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
476 gaTmpTable[i*1024+j] = 0;
481 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
483 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
485 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
486 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
487 MM_FreeTemp( (Uint)tmp );
491 //LEAVE('x', *gTmpCR3 & ~0xFFF);
492 return *gTmpCR3 & ~0xFFF;
496 * \fn Uint MM_NewKStack()
497 * \brief Create a new kernel stack
501 Uint base = KERNEL_STACKS;
503 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
505 if(MM_GetPhysAddr(base) != 0) continue;
506 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
509 return base+KERNEL_STACK_SIZE;
511 Warning("MM_NewKStack - No address space left\n");
516 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
517 * \brief Sets the flags on a page
519 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
522 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
523 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
525 ent = &gaPageTable[VAddr >> 12];
528 if( Mask & MM_PFLAG_RO )
530 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
531 else *ent |= PF_WRITE;
535 if( Mask & MM_PFLAG_KERNEL )
537 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
538 else *ent |= PF_USER;
542 if( Mask & MM_PFLAG_COW )
544 if( Flags & MM_PFLAG_COW ) {
556 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
557 * \brief Duplicates a virtual page to a physical one
559 tPAddr MM_DuplicatePage(Uint VAddr)
566 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
567 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
573 ret = MM_AllocPhys();
575 // Write-lock the page (to keep data constistent), saving its R/W state
576 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
577 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
581 temp = MM_MapTemp(ret);
582 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
585 // Restore Writeable status
586 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
593 * \fn Uint MM_MapTemp(tPAddr PAddr)
594 * \brief Create a temporary memory mapping
595 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
597 Uint MM_MapTemp(tPAddr PAddr)
601 //ENTER("XPAddr", PAddr);
605 //LOG("gilTempMappings = %i", gilTempMappings);
609 LOCK( &gilTempMappings );
611 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
613 // Check if page used
614 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
616 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
617 INVLPG( TEMP_MAP_ADDR + (i << 12) );
618 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
619 RELEASE( &gilTempMappings );
620 return TEMP_MAP_ADDR + (i << 12);
622 RELEASE( &gilTempMappings );
628 * \fn void MM_FreeTemp(Uint PAddr)
629 * \brief Free's a temp mapping
631 void MM_FreeTemp(Uint VAddr)
634 //ENTER("xVAddr", VAddr);
636 if(i >= (TEMP_MAP_ADDR >> 12))
637 gaPageTable[ i ] = 0;
643 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
644 * \brief Allocates a contigous number of pages
646 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
653 for( i = 0; i < NUM_HW_PAGES; i ++ )
655 // Check if addr used
656 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
659 // Check possible region
660 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
662 // If there is an allocated page in the region we are testing, break
663 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
669 for( j = 0; j < Number; j++ ) {
670 MM_RefPhys( PAddr + (j<<12) );
671 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
673 return HW_MAP_ADDR + (i<<12);
676 // If we don't find any, return NULL
681 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
682 * \brief Unmap a hardware page
684 void MM_UnmapHWPage(Uint VAddr, Uint Number)
688 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
692 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
694 for( j = 0; j < Number; j++ )
696 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
697 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
700 RELEASE( &gilTempMappings );
704 EXPORT(MM_GetPhysAddr);
707 EXPORT(MM_MapHWPage);
708 EXPORT(MM_UnmapHWPage);