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];
45 void MM_PreinitVirtual();
46 void MM_InstallVirtual();
47 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
48 void MM_DumpTables(tVAddr Start, tVAddr End);
49 tPAddr MM_DuplicatePage(Uint VAddr);
52 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
53 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
54 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
55 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
56 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
57 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
58 int gilTempMappings = 0;
62 * \fn void MM_PreinitVirtual()
63 * \brief Maps the fractal mappings
65 void MM_PreinitVirtual()
67 gaInitPageDir[ 0 ] = 0;
68 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
72 * \fn void MM_InstallVirtual()
73 * \brief Sets up the constant page mappings
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 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
111 gaPageTable[Addr>>12] &= ~PF_COW;
112 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
116 paddr = MM_DuplicatePage( Addr );
117 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
118 gaPageTable[Addr>>12] &= PF_USER;
119 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
121 //LOG("Duplicated page at %p to 0x%x", Addr&~0xFFF, gaPageTable[Addr>>12]);
122 INVLPG( Addr & ~0xFFF );
127 // -- Check Error Code --
129 Warning("Reserved Bits Trashed!");
132 Warning("%s %s %s memory%s",
133 (ErrorCode&4?"User":"Kernel"),
134 (ErrorCode&2?"write to":"read from"),
135 (ErrorCode&1?"bad/locked":"non-present"),
136 (ErrorCode&16?" (Instruction Fetch)":"")
140 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
141 if( gaPageDir[Addr>>22] & PF_PRESENT )
142 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
144 MM_DumpTables(0, -1);
146 Panic("Page Fault at 0x%x\n", Regs->eip);
150 * \fn void MM_DumpTables(Uint Start, Uint End)
151 * \brief Dumps the layout of the page tables
153 void MM_DumpTables(tVAddr Start, tVAddr End)
155 tVAddr rangeStart = 0;
159 const tPAddr MASK = ~0xF98;
161 Start >>= 12; End >>= 12;
163 Log("Directory Entries:");
164 for(page = Start >> 10;
165 page < (End >> 10)+1;
170 Log(" 0x%08x-0x%08x :: 0x%08x",
171 page<<22, ((page+1)<<22)-1,
172 gaPageDir[page]&~0xFFF
177 Log("Table Entries:");
178 for(page = Start, curPos = Start<<12;
180 curPos += 0x1000, page++)
182 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
183 || !(gaPageTable[page] & PF_PRESENT)
184 || (gaPageTable[page] & MASK) != expected)
187 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
188 rangeStart, curPos - 1,
189 gaPageTable[rangeStart>>12] & ~0xFFF,
190 (expected & ~0xFFF) - 1,
191 (expected & PF_PAGED ? "p" : "-"),
192 (expected & PF_COW ? "C" : "-"),
193 (expected & PF_USER ? "U" : "-"),
194 (expected & PF_WRITE ? "W" : "-")
198 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
199 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
201 expected = (gaPageTable[page] & MASK);
204 if(expected) expected += 0x1000;
208 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
209 rangeStart, curPos - 1,
210 gaPageTable[rangeStart>>12] & ~0xFFF,
211 (expected & ~0xFFF) - 1,
212 (expected & PF_PAGED ? "p" : "-"),
213 (expected & PF_COW ? "C" : "-"),
214 (expected & PF_USER ? "U" : "-"),
215 (expected & PF_WRITE ? "W" : "-")
222 * \fn tPAddr MM_Allocate(Uint VAddr)
224 tPAddr MM_Allocate(Uint VAddr)
227 // Check if the directory is mapped
228 if( gaPageDir[ VAddr >> 22 ] == 0 )
230 // Allocate directory
231 paddr = MM_AllocPhys();
233 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
237 gaPageDir[ VAddr >> 22 ] = paddr | 3;
239 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
241 INVLPG( &gaPageDir[ VAddr >> 22 ] );
242 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
244 // Check if the page is already allocated
245 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
246 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
247 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
251 paddr = MM_AllocPhys();
253 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
254 VAddr, __builtin_return_address(0));
258 gaPageTable[ VAddr >> 12 ] = paddr | 3;
260 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
261 // Invalidate Cache for address
262 INVLPG( VAddr & ~0xFFF );
268 * \fn void MM_Deallocate(Uint VAddr)
270 void MM_Deallocate(Uint VAddr)
272 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
273 Warning("MM_Deallocate - Directory not mapped");
277 if(gaPageTable[ VAddr >> 12 ] == 0) {
278 Warning("MM_Deallocate - Page is not allocated");
283 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
285 gaPageTable[ VAddr >> 12 ] = 0;
289 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
290 * \brief Checks if the passed address is accesable
292 tPAddr MM_GetPhysAddr(Uint Addr)
294 if( !(gaPageDir[Addr >> 22] & 1) )
296 if( !(gaPageTable[Addr >> 12] & 1) )
298 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
302 * \fn void MM_SetCR3(Uint CR3)
303 * \brief Sets the current process space
305 void MM_SetCR3(Uint CR3)
307 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
311 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
312 * \brief Map a physical page to a virtual one
314 int MM_Map(Uint VAddr, tPAddr PAddr)
316 //ENTER("xVAddr xPAddr", VAddr, PAddr);
318 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
319 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
325 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
327 // Check if the directory is mapped
328 if( gaPageDir[ VAddr >> 22 ] == 0 )
330 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
333 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
335 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
336 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
338 // Check if the page is already allocated
339 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
340 Warning("MM_Map - Allocating to used address");
346 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
348 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
350 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
351 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
356 //LOG("INVLPG( 0x%x )", VAddr);
364 * \fn Uint MM_ClearUser()
365 * \brief Clear user's address space
372 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
374 // Check if directory is not allocated
375 if( !(gaPageDir[i] & PF_PRESENT) ) {
381 for( j = 0; j < 1024; j ++ )
383 if( gaPageTable[i*1024+j] & 1 )
384 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
385 gaPageTable[i*1024+j] = 0;
388 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
390 INVLPG( &gaPageTable[i*1024] );
398 * \fn Uint MM_Clone()
399 * \brief Clone the current address space
405 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
410 // Create Directory Table
411 *gTmpCR3 = MM_AllocPhys() | 3;
413 //LOG("Allocated Directory (%x)", *gTmpCR3);
414 memsetd( gaTmpDir, 0, 1024 );
419 // Check if table is allocated
420 if( !(gaPageDir[i] & PF_PRESENT) ) {
426 // Allocate new table
427 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
428 INVLPG( &gaTmpTable[page] );
430 for( j = 0; j < 1024; j ++, page++ )
432 if( !(gaPageTable[page] & PF_PRESENT) ) {
433 gaTmpTable[page] = 0;
438 MM_RefPhys( gaPageTable[page] & ~0xFFF );
440 if(gaPageTable[page] & PF_WRITE) {
441 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
442 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
443 INVLPG( page << 12 );
446 gaTmpTable[page] = gaPageTable[page];
450 // Map in kernel tables (and make fractal mapping)
451 for( i = 768; i < 1024; i ++ )
454 if( i == (PAGE_TABLE_ADDR >> 22) ) {
455 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
459 if( gaPageDir[i] == 0 ) {
464 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
465 MM_RefPhys( gaPageDir[i] & ~0xFFF );
466 gaTmpDir[i] = gaPageDir[i];
469 // Allocate kernel stack
470 for(i = KERNEL_STACKS >> 22;
471 i < KERNEL_STACK_END >> 22;
474 // Check if directory is allocated
475 if( (gaPageDir[i] & 1) == 0 ) {
480 // We don't care about other kernel stacks, just the current one
481 if( i != kStackBase >> 22 ) {
482 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
488 gaTmpDir[i] = MM_AllocPhys() | 3;
489 INVLPG( &gaTmpTable[i*1024] );
490 for( j = 0; j < 1024; j ++ )
492 // Is the page allocated? If not, skip
493 if( !(gaPageTable[i*1024+j] & 1) ) {
494 gaTmpTable[i*1024+j] = 0;
498 // We don't care about other kernel stacks
499 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
500 gaTmpTable[i*1024+j] = 0;
505 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
507 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
509 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
510 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
511 MM_FreeTemp( (Uint)tmp );
515 //LEAVE('x', *gTmpCR3 & ~0xFFF);
516 return *gTmpCR3 & ~0xFFF;
520 * \fn Uint MM_NewKStack()
521 * \brief Create a new kernel stack
525 Uint base = KERNEL_STACKS;
527 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
529 if(MM_GetPhysAddr(base) != 0) continue;
530 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
533 return base+KERNEL_STACK_SIZE;
535 Warning("MM_NewKStack - No address space left\n");
540 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
541 * \brief Sets the flags on a page
543 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
546 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
547 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
549 ent = &gaPageTable[VAddr >> 12];
552 if( Mask & MM_PFLAG_RO )
554 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
555 else *ent |= PF_WRITE;
559 if( Mask & MM_PFLAG_KERNEL )
561 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
562 else *ent |= PF_USER;
566 if( Mask & MM_PFLAG_COW )
568 if( Flags & MM_PFLAG_COW ) {
580 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
581 * \brief Duplicates a virtual page to a physical one
583 tPAddr MM_DuplicatePage(Uint VAddr)
590 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
591 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
597 ret = MM_AllocPhys();
599 // Write-lock the page (to keep data constistent), saving its R/W state
600 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
601 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
605 temp = MM_MapTemp(ret);
606 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
609 // Restore Writeable status
610 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
617 * \fn Uint MM_MapTemp(tPAddr PAddr)
618 * \brief Create a temporary memory mapping
619 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
621 Uint MM_MapTemp(tPAddr PAddr)
625 //ENTER("XPAddr", PAddr);
629 //LOG("gilTempMappings = %i", gilTempMappings);
633 LOCK( &gilTempMappings );
635 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
637 // Check if page used
638 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
640 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
641 INVLPG( TEMP_MAP_ADDR + (i << 12) );
642 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
643 RELEASE( &gilTempMappings );
644 return TEMP_MAP_ADDR + (i << 12);
646 RELEASE( &gilTempMappings );
652 * \fn void MM_FreeTemp(Uint PAddr)
653 * \brief Free's a temp mapping
655 void MM_FreeTemp(Uint VAddr)
658 //ENTER("xVAddr", VAddr);
660 if(i >= (TEMP_MAP_ADDR >> 12))
661 gaPageTable[ i ] = 0;
667 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
668 * \brief Allocates a contigous number of pages
670 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
677 for( i = 0; i < NUM_HW_PAGES; i ++ )
679 // Check if addr used
680 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
683 // Check possible region
684 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
686 // If there is an allocated page in the region we are testing, break
687 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
693 for( j = 0; j < Number; j++ ) {
694 MM_RefPhys( PAddr + (j<<12) );
695 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
697 return HW_MAP_ADDR + (i<<12);
700 // If we don't find any, return NULL
705 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
706 * \brief Unmap a hardware page
708 void MM_UnmapHWPage(Uint VAddr, Uint Number)
712 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
716 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
718 for( j = 0; j < Number; j++ )
720 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
721 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
724 RELEASE( &gilTempMappings );
728 EXPORT(MM_GetPhysAddr);
731 EXPORT(MM_MapHWPage);
732 EXPORT(MM_UnmapHWPage);