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
33 #define PF_PRESENT 0x1
37 #define PF_PAGED 0x400
39 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
42 extern Uint32 gaInitPageDir[1024];
43 extern Uint32 gaInitPageTable[1024];
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 paddr = MM_DuplicatePage( Addr );
111 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
112 gaPageTable[Addr>>12] &= PF_USER;
113 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
114 INVLPG( Addr & ~0xFFF );
119 // -- Check Error Code --
121 Warning("Reserved Bits Trashed!");
124 Warning("%s %s %s memory%s",
125 (ErrorCode&4?"User":"Kernel"),
126 (ErrorCode&2?"write to":"read from"),
127 (ErrorCode&1?"bad/locked":"non-present"),
128 (ErrorCode&16?" (Instruction Fetch)":"")
132 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
133 if( gaPageDir[Addr>>22] & PF_PRESENT )
134 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
136 MM_DumpTables(0, -1);
138 Panic("Page Fault at 0x%x\n", Regs->eip);
142 * \fn void MM_DumpTables(Uint Start, Uint End)
143 * \brief Dumps the layout of the page tables
145 void MM_DumpTables(tVAddr Start, tVAddr End)
147 tVAddr rangeStart = 0;
151 const tPAddr MASK = ~0xF98;
153 Start >>= 12; End >>= 12;
154 for(page = Start, curPos = Start<<12;
156 curPos += 0x1000, page++)
158 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
159 || !(gaPageTable[page] & PF_PRESENT)
160 || (gaPageTable[page] & MASK) != expected)
163 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
164 rangeStart, curPos - 1,
165 gaPageTable[rangeStart>>12] & ~0xFFF,
166 (expected & ~0xFFF) - 1,
167 (expected & PF_PAGED ? "p" : "-"),
168 (expected & PF_COW ? "C" : "-"),
169 (expected & PF_USER ? "U" : "-"),
170 (expected & PF_WRITE ? "W" : "-")
174 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
175 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
177 expected = (gaPageTable[page] & MASK);
180 if(expected) expected += 0x1000;
184 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
185 rangeStart, curPos - 1,
186 gaPageTable[rangeStart>>12] & ~0xFFF,
187 (expected & ~0xFFF) - 1,
188 (expected & PF_PAGED ? "p" : "-"),
189 (expected & PF_COW ? "C" : "-"),
190 (expected & PF_USER ? "U" : "-"),
191 (expected & PF_WRITE ? "W" : "-")
198 * \fn tPAddr MM_Allocate(Uint VAddr)
200 tPAddr MM_Allocate(Uint VAddr)
202 // Check if the directory is mapped
203 if( gaPageDir[ VAddr >> 22 ] == 0 )
205 // Allocate directory
206 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
208 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
210 INVLPG( &gaPageDir[ VAddr >> 22 ] );
211 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
213 // Check if the page is already allocated
214 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
215 Warning("MM_Allocate - Allocating to used address");
216 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
220 gaPageTable[ VAddr >> 12 ] = MM_AllocPhys() | 3;
222 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
223 // Invalidate Cache for address
224 INVLPG( VAddr & ~0xFFF );
226 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
230 * \fn void MM_Deallocate(Uint VAddr)
232 void MM_Deallocate(Uint VAddr)
234 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
235 Warning("MM_Deallocate - Directory not mapped");
239 if(gaPageTable[ VAddr >> 12 ] == 0) {
240 Warning("MM_Deallocate - Page is not allocated");
245 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
247 gaPageTable[ VAddr >> 12 ] = 0;
251 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
252 * \brief Checks if the passed address is accesable
254 tPAddr MM_GetPhysAddr(Uint Addr)
256 if( !(gaPageDir[Addr >> 22] & 1) )
258 if( !(gaPageTable[Addr >> 12] & 1) )
260 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
264 * \fn void MM_SetCR3(Uint CR3)
265 * \brief Sets the current process space
267 void MM_SetCR3(Uint CR3)
269 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
273 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
274 * \brief Map a physical page to a virtual one
276 int MM_Map(Uint VAddr, tPAddr PAddr)
278 //ENTER("xVAddr xPAddr", VAddr, PAddr);
280 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
281 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
287 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
289 // Check if the directory is mapped
290 if( gaPageDir[ VAddr >> 22 ] == 0 )
292 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
295 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
297 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
298 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
300 // Check if the page is already allocated
301 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
302 Warning("MM_Map - Allocating to used address");
308 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
310 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
312 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
313 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
318 //LOG("INVLPG( 0x%x )", VAddr);
326 * \fn Uint MM_ClearUser()
327 * \brief Clear user's address space
334 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
336 // Check if directory is not allocated
337 if( !(gaPageDir[i] & PF_PRESENT) ) {
343 for( j = 0; j < 1024; j ++ )
345 if( gaPageTable[i*1024+j] & 1 )
346 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
347 gaPageTable[i*1024+j] = 0;
350 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
358 * \fn Uint MM_Clone()
359 * \brief Clone the current address space
364 Uint kStackBase = gCurrentThread->KernelStack - KERNEL_STACK_SIZE;
369 // Create Directory Table
370 *gTmpCR3 = MM_AllocPhys() | 3;
372 //LOG("Allocated Directory (%x)", *gTmpCR3);
373 memsetd( gaTmpDir, 0, 1024 );
378 // Check if table is allocated
379 if( !(gaPageDir[i] & PF_PRESENT) ) {
384 // Allocate new table
385 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
386 INVLPG( &gaTmpTable[i*1024] );
388 for( j = 0; j < 1024; j ++ )
390 if( !(gaPageTable[i*1024+j] & PF_PRESENT) ) {
391 gaTmpTable[i*1024+j] = 0;
397 MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF );
399 if(gaPageTable[i*1024+j] & PF_WRITE) {
400 gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
401 gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW;
404 gaTmpTable[i*1024+j] = gaPageTable[i*1024+j];
405 LOG("gaTmpTable[0x%x] = 0x%x", i*1024+j, gaTmpTable[i*1024+j]);
407 gaTmpTable[i*1024+j] = MM_DuplicatePage( (i*1024+j)<<12 ) | (gaPageTable[i*1024+j]&7);
412 // Map in kernel tables (and make fractal mapping)
413 for( i = 768; i < 1024; i ++ )
416 if( i == (PAGE_TABLE_ADDR >> 22) ) {
417 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
421 if( gaPageDir[i] == 0 ) {
426 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
427 MM_RefPhys( gaPageDir[i] & ~0xFFF );
428 gaTmpDir[i] = gaPageDir[i];
431 // Allocate kernel stack
432 for(i = KERNEL_STACKS >> 22;
433 i < KERNEL_STACK_END >> 22;
436 // Check if directory is allocated
437 if( (gaPageDir[i] & 1) == 0 ) {
442 // We don't care about other kernel stacks, just the current one
443 if( i != kStackBase >> 22 ) {
444 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
450 gaTmpDir[i] = MM_AllocPhys() | 3;
451 INVLPG( &gaTmpTable[i*1024] );
452 for( j = 0; j < 1024; j ++ )
454 // Is the page allocated? If not, skip
455 if( !(gaPageTable[i*1024+j] & 1) ) {
456 gaTmpTable[i*1024+j] = 0;
460 // We don't care about other kernel stacks
461 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
462 gaTmpTable[i*1024+j] = 0;
467 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
469 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
471 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
472 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
473 MM_FreeTemp( (Uint)tmp );
477 //LEAVE('x', *gTmpCR3 & ~0xFFF);
478 return *gTmpCR3 & ~0xFFF;
482 * \fn Uint MM_NewKStack()
483 * \brief Create a new kernel stack
487 Uint base = KERNEL_STACKS;
489 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
491 if(MM_GetPhysAddr(base) != 0) continue;
492 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
495 return base+KERNEL_STACK_SIZE;
497 Warning("MM_NewKStack - No address space left\n");
502 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
503 * \brief Sets the flags on a page
505 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
508 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
509 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
511 ent = &gaPageTable[VAddr >> 12];
514 if( Mask & MM_PFLAG_RO )
516 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
517 else *ent |= PF_WRITE;
521 if( Mask & MM_PFLAG_KERNEL )
523 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
524 else *ent |= PF_USER;
528 if( Mask & MM_PFLAG_COW )
530 if( Flags & MM_PFLAG_COW ) {
542 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
543 * \brief Duplicates a virtual page to a physical one
545 tPAddr MM_DuplicatePage(Uint VAddr)
552 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
553 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
559 ret = MM_AllocPhys();
561 // Write-lock the page (to keep data constistent), saving its R/W state
562 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
563 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
567 temp = MM_MapTemp(ret);
568 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
571 // Restore Writeable status
572 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
579 * \fn Uint MM_MapTemp(tPAddr PAddr)
580 * \brief Create a temporary memory mapping
581 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
583 Uint MM_MapTemp(tPAddr PAddr)
587 //ENTER("XPAddr", PAddr);
591 //LOG("gilTempMappings = %i", gilTempMappings);
595 LOCK( &gilTempMappings );
597 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
599 // Check if page used
600 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
602 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
603 INVLPG( TEMP_MAP_ADDR + (i << 12) );
604 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
605 RELEASE( &gilTempMappings );
606 return TEMP_MAP_ADDR + (i << 12);
608 RELEASE( &gilTempMappings );
614 * \fn void MM_FreeTemp(Uint PAddr)
615 * \brief Free's a temp mapping
617 void MM_FreeTemp(Uint VAddr)
620 //ENTER("xVAddr", VAddr);
622 if(i >= (TEMP_MAP_ADDR >> 12))
623 gaPageTable[ i ] = 0;
629 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
630 * \brief Allocates a contigous number of pages
632 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
639 for( i = 0; i < NUM_HW_PAGES; i ++ )
641 // Check if addr used
642 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
645 // Check possible region
646 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
648 // If there is an allocated page in the region we are testing, break
649 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
655 for( j = 0; j < Number; j++ ) {
656 MM_RefPhys( PAddr + (j<<12) );
657 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
659 return HW_MAP_ADDR + (i<<12);
662 // If we don't find any, return NULL
667 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
668 * \brief Unmap a hardware page
670 void MM_UnmapHWPage(Uint VAddr, Uint Number)
674 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
678 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
680 for( j = 0; j < Number; j++ )
682 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
683 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
686 RELEASE( &gilTempMappings );
690 EXPORT(MM_GetPhysAddr);
693 EXPORT(MM_MapHWPage);
694 EXPORT(MM_UnmapHWPage);