2 * AcessOS Microkernel Version
10 * 0xFF - System Calls / Kernel's User Code
17 #define KERNEL_STACKS 0xF0000000
18 #define KERNEL_STACK_SIZE 0x00002000
19 #define KERNEL_STACK_END 0xFD000000
20 #define PAGE_TABLE_ADDR 0xFD000000
21 #define PAGE_DIR_ADDR 0xFD3F4000
22 #define PAGE_CR3_ADDR 0xFD3F4FD0
23 #define TMP_CR3_ADDR 0xFD3F4FD4 // Part of core instead of temp
24 #define TMP_DIR_ADDR 0xFD3F5000 // Same
25 #define TMP_TABLE_ADDR 0xFD400000
26 #define HW_MAP_ADDR 0xFD800000
27 #define HW_MAP_MAX 0xFEFF0000
28 #define NUM_HW_PAGES ((HW_MAP_MAX-HW_MAP_ADDR)/0x1000)
29 #define TEMP_MAP_ADDR 0xFEFF0000 // Allows 16 "temp" pages
30 #define NUM_TEMP_PAGES 16
32 #define PF_PRESENT 0x1
36 #define PF_PAGED 0x400
38 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
41 extern Uint32 gaInitPageDir[1024];
42 extern Uint32 gaInitPageTable[1024];
43 extern void Threads_SegFault(Uint Addr);
46 void MM_PreinitVirtual();
47 void MM_InstallVirtual();
48 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
49 void MM_DumpTables(tVAddr Start, tVAddr End);
50 tPAddr MM_DuplicatePage(Uint VAddr);
53 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
54 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
55 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
56 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
57 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
58 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
59 int gilTempMappings = 0;
63 * \fn void MM_PreinitVirtual()
64 * \brief Maps the fractal mappings
66 void MM_PreinitVirtual()
68 gaInitPageDir[ 0 ] = 0;
69 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
73 * \fn void MM_InstallVirtual()
74 * \brief Sets up the constant page mappings
76 void MM_InstallVirtual()
80 // --- Pre-Allocate kernel tables
81 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
83 if( gaPageDir[ i ] ) continue;
84 // Skip stack tables, they are process unique
85 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACK_END >> 22) {
90 gaPageDir[ i ] = MM_AllocPhys() | 3;
91 INVLPG( &gaPageTable[i*1024] );
92 memset( &gaPageTable[i*1024], 0, 0x1000 );
97 * \fn void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
98 * \brief Called on a page fault
100 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
102 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
104 // -- Check for COW --
105 if( gaPageDir [Addr>>22] & PF_PRESENT
106 && gaPageTable[Addr>>12] & PF_PRESENT
107 && gaPageTable[Addr>>12] & PF_COW )
110 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
112 gaPageTable[Addr>>12] &= ~PF_COW;
113 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
117 paddr = MM_DuplicatePage( Addr );
118 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
119 gaPageTable[Addr>>12] &= PF_USER;
120 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
122 //LOG("Duplicated page at %p to 0x%x", Addr&~0xFFF, gaPageTable[Addr>>12]);
123 INVLPG( Addr & ~0xFFF );
128 // If it was a user, tell the thread handler
130 Threads_SegFault(Addr);
134 // -- Check Error Code --
136 Warning("Reserved Bits Trashed!");
139 Warning("%s %s %s memory%s",
140 (ErrorCode&4?"User":"Kernel"),
141 (ErrorCode&2?"write to":"read from"),
142 (ErrorCode&1?"bad/locked":"non-present"),
143 (ErrorCode&16?" (Instruction Fetch)":"")
147 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
148 if( gaPageDir[Addr>>22] & PF_PRESENT )
149 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
151 MM_DumpTables(0, -1);
153 Panic("Page Fault at 0x%x\n", Regs->eip);
157 * \fn void MM_DumpTables(Uint Start, Uint End)
158 * \brief Dumps the layout of the page tables
160 void MM_DumpTables(tVAddr Start, tVAddr End)
162 tVAddr rangeStart = 0;
166 const tPAddr MASK = ~0xF98;
168 Start >>= 12; End >>= 12;
170 Log("Directory Entries:");
171 for(page = Start >> 10;
172 page < (End >> 10)+1;
177 Log(" 0x%08x-0x%08x :: 0x%08x",
178 page<<22, ((page+1)<<22)-1,
179 gaPageDir[page]&~0xFFF
184 Log("Table Entries:");
185 for(page = Start, curPos = Start<<12;
187 curPos += 0x1000, page++)
189 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
190 || !(gaPageTable[page] & PF_PRESENT)
191 || (gaPageTable[page] & MASK) != expected)
194 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
195 rangeStart, curPos - 1,
196 gaPageTable[rangeStart>>12] & ~0xFFF,
197 (expected & ~0xFFF) - 1,
198 (expected & PF_PAGED ? "p" : "-"),
199 (expected & PF_COW ? "C" : "-"),
200 (expected & PF_USER ? "U" : "-"),
201 (expected & PF_WRITE ? "W" : "-")
205 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
206 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
208 expected = (gaPageTable[page] & MASK);
211 if(expected) expected += 0x1000;
215 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
216 rangeStart, curPos - 1,
217 gaPageTable[rangeStart>>12] & ~0xFFF,
218 (expected & ~0xFFF) - 1,
219 (expected & PF_PAGED ? "p" : "-"),
220 (expected & PF_COW ? "C" : "-"),
221 (expected & PF_USER ? "U" : "-"),
222 (expected & PF_WRITE ? "W" : "-")
229 * \fn tPAddr MM_Allocate(Uint VAddr)
231 tPAddr MM_Allocate(Uint VAddr)
234 // Check if the directory is mapped
235 if( gaPageDir[ VAddr >> 22 ] == 0 )
237 // Allocate directory
238 paddr = MM_AllocPhys();
240 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
244 gaPageDir[ VAddr >> 22 ] = paddr | 3;
246 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
248 INVLPG( &gaPageDir[ VAddr >> 22 ] );
249 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
251 // Check if the page is already allocated
252 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
253 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
254 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
258 paddr = MM_AllocPhys();
260 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
261 VAddr, __builtin_return_address(0));
265 gaPageTable[ VAddr >> 12 ] = paddr | 3;
267 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
268 // Invalidate Cache for address
269 INVLPG( VAddr & ~0xFFF );
275 * \fn void MM_Deallocate(Uint VAddr)
277 void MM_Deallocate(Uint VAddr)
279 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
280 Warning("MM_Deallocate - Directory not mapped");
284 if(gaPageTable[ VAddr >> 12 ] == 0) {
285 Warning("MM_Deallocate - Page is not allocated");
290 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
292 gaPageTable[ VAddr >> 12 ] = 0;
296 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
297 * \brief Checks if the passed address is accesable
299 tPAddr MM_GetPhysAddr(Uint Addr)
301 if( !(gaPageDir[Addr >> 22] & 1) )
303 if( !(gaPageTable[Addr >> 12] & 1) )
305 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
309 * \fn void MM_SetCR3(Uint CR3)
310 * \brief Sets the current process space
312 void MM_SetCR3(Uint CR3)
314 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
318 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
319 * \brief Map a physical page to a virtual one
321 int MM_Map(Uint VAddr, tPAddr PAddr)
323 //ENTER("xVAddr xPAddr", VAddr, PAddr);
325 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
326 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
332 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
334 // Check if the directory is mapped
335 if( gaPageDir[ VAddr >> 22 ] == 0 )
337 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
340 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
342 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
343 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
345 // Check if the page is already allocated
346 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
347 Warning("MM_Map - Allocating to used address");
353 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
355 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
357 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
358 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
363 //LOG("INVLPG( 0x%x )", VAddr);
371 * \fn Uint MM_ClearUser()
372 * \brief Clear user's address space
379 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
381 // Check if directory is not allocated
382 if( !(gaPageDir[i] & PF_PRESENT) ) {
388 for( j = 0; j < 1024; j ++ )
390 if( gaPageTable[i*1024+j] & 1 )
391 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
392 gaPageTable[i*1024+j] = 0;
395 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
397 INVLPG( &gaPageTable[i*1024] );
405 * \fn Uint MM_Clone()
406 * \brief Clone the current address space
412 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
417 // Create Directory Table
418 *gTmpCR3 = MM_AllocPhys() | 3;
420 //LOG("Allocated Directory (%x)", *gTmpCR3);
421 memsetd( gaTmpDir, 0, 1024 );
426 // Check if table is allocated
427 if( !(gaPageDir[i] & PF_PRESENT) ) {
433 // Allocate new table
434 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
435 INVLPG( &gaTmpTable[page] );
437 for( j = 0; j < 1024; j ++, page++ )
439 if( !(gaPageTable[page] & PF_PRESENT) ) {
440 gaTmpTable[page] = 0;
445 MM_RefPhys( gaPageTable[page] & ~0xFFF );
447 if(gaPageTable[page] & PF_WRITE) {
448 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
449 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
450 INVLPG( page << 12 );
453 gaTmpTable[page] = gaPageTable[page];
457 // Map in kernel tables (and make fractal mapping)
458 for( i = 768; i < 1024; i ++ )
461 if( i == (PAGE_TABLE_ADDR >> 22) ) {
462 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
466 if( gaPageDir[i] == 0 ) {
471 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
472 MM_RefPhys( gaPageDir[i] & ~0xFFF );
473 gaTmpDir[i] = gaPageDir[i];
476 // Allocate kernel stack
477 for(i = KERNEL_STACKS >> 22;
478 i < KERNEL_STACK_END >> 22;
481 // Check if directory is allocated
482 if( (gaPageDir[i] & 1) == 0 ) {
487 // We don't care about other kernel stacks, just the current one
488 if( i != kStackBase >> 22 ) {
489 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
495 gaTmpDir[i] = MM_AllocPhys() | 3;
496 INVLPG( &gaTmpTable[i*1024] );
497 for( j = 0; j < 1024; j ++ )
499 // Is the page allocated? If not, skip
500 if( !(gaPageTable[i*1024+j] & 1) ) {
501 gaTmpTable[i*1024+j] = 0;
505 // We don't care about other kernel stacks
506 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
507 gaTmpTable[i*1024+j] = 0;
512 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
514 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
516 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
517 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
518 MM_FreeTemp( (Uint)tmp );
522 //LEAVE('x', *gTmpCR3 & ~0xFFF);
523 return *gTmpCR3 & ~0xFFF;
527 * \fn Uint MM_NewKStack()
528 * \brief Create a new kernel stack
532 Uint base = KERNEL_STACKS;
534 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
536 if(MM_GetPhysAddr(base) != 0) continue;
537 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
540 return base+KERNEL_STACK_SIZE;
542 Warning("MM_NewKStack - No address space left\n");
547 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
548 * \brief Sets the flags on a page
550 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
553 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
554 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
556 ent = &gaPageTable[VAddr >> 12];
559 if( Mask & MM_PFLAG_RO )
561 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
562 else *ent |= PF_WRITE;
566 if( Mask & MM_PFLAG_KERNEL )
568 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
569 else *ent |= PF_USER;
573 if( Mask & MM_PFLAG_COW )
575 if( Flags & MM_PFLAG_COW ) {
587 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
588 * \brief Duplicates a virtual page to a physical one
590 tPAddr MM_DuplicatePage(Uint VAddr)
597 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
598 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
604 ret = MM_AllocPhys();
606 // Write-lock the page (to keep data constistent), saving its R/W state
607 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
608 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
612 temp = MM_MapTemp(ret);
613 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
616 // Restore Writeable status
617 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
624 * \fn Uint MM_MapTemp(tPAddr PAddr)
625 * \brief Create a temporary memory mapping
626 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
628 Uint MM_MapTemp(tPAddr PAddr)
632 //ENTER("XPAddr", PAddr);
636 //LOG("gilTempMappings = %i", gilTempMappings);
640 LOCK( &gilTempMappings );
642 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
644 // Check if page used
645 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
647 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
648 INVLPG( TEMP_MAP_ADDR + (i << 12) );
649 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
650 RELEASE( &gilTempMappings );
651 return TEMP_MAP_ADDR + (i << 12);
653 RELEASE( &gilTempMappings );
659 * \fn void MM_FreeTemp(Uint PAddr)
660 * \brief Free's a temp mapping
662 void MM_FreeTemp(Uint VAddr)
665 //ENTER("xVAddr", VAddr);
667 if(i >= (TEMP_MAP_ADDR >> 12))
668 gaPageTable[ i ] = 0;
674 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
675 * \brief Allocates a contigous number of pages
677 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
684 for( i = 0; i < NUM_HW_PAGES; i ++ )
686 // Check if addr used
687 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
690 // Check possible region
691 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
693 // If there is an allocated page in the region we are testing, break
694 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
700 for( j = 0; j < Number; j++ ) {
701 MM_RefPhys( PAddr + (j<<12) );
702 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
704 return HW_MAP_ADDR + (i<<12);
707 // If we don't find any, return NULL
712 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
713 * \brief Unmap a hardware page
715 void MM_UnmapHWPage(Uint VAddr, Uint Number)
719 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
723 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
725 for( j = 0; j < Number; j++ )
727 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
728 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
731 RELEASE( &gilTempMappings );
735 EXPORT(MM_GetPhysAddr);
738 EXPORT(MM_MapHWPage);
739 EXPORT(MM_UnmapHWPage);