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;
123 INVLPG( Addr & ~0xFFF );
128 // If it was a user, tell the thread handler
130 Warning("User Pagefault: Instruction at %p accessed %p\n", Regs->eip, Addr);
131 Threads_SegFault(Addr);
135 // -- Check Error Code --
137 Warning("Reserved Bits Trashed!");
140 Warning("%s %s %s memory%s",
141 (ErrorCode&4?"User":"Kernel"),
142 (ErrorCode&2?"write to":"read from"),
143 (ErrorCode&1?"bad/locked":"non-present"),
144 (ErrorCode&16?" (Instruction Fetch)":"")
148 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
149 if( gaPageDir[Addr>>22] & PF_PRESENT )
150 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
152 MM_DumpTables(0, -1);
154 Panic("Page Fault at 0x%x\n", Regs->eip);
158 * \fn void MM_DumpTables(Uint Start, Uint End)
159 * \brief Dumps the layout of the page tables
161 void MM_DumpTables(tVAddr Start, tVAddr End)
163 tVAddr rangeStart = 0;
167 const tPAddr MASK = ~0xF98;
169 Start >>= 12; End >>= 12;
171 Log("Directory Entries:");
172 for(page = Start >> 10;
173 page < (End >> 10)+1;
178 Log(" 0x%08x-0x%08x :: 0x%08x",
179 page<<22, ((page+1)<<22)-1,
180 gaPageDir[page]&~0xFFF
185 Log("Table Entries:");
186 for(page = Start, curPos = Start<<12;
188 curPos += 0x1000, page++)
190 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
191 || !(gaPageTable[page] & PF_PRESENT)
192 || (gaPageTable[page] & MASK) != expected)
195 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
196 rangeStart, curPos - 1,
197 gaPageTable[rangeStart>>12] & ~0xFFF,
198 (expected & ~0xFFF) - 1,
199 (expected & PF_PAGED ? "p" : "-"),
200 (expected & PF_COW ? "C" : "-"),
201 (expected & PF_USER ? "U" : "-"),
202 (expected & PF_WRITE ? "W" : "-")
206 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
207 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
209 expected = (gaPageTable[page] & MASK);
212 if(expected) expected += 0x1000;
216 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
217 rangeStart, curPos - 1,
218 gaPageTable[rangeStart>>12] & ~0xFFF,
219 (expected & ~0xFFF) - 1,
220 (expected & PF_PAGED ? "p" : "-"),
221 (expected & PF_COW ? "C" : "-"),
222 (expected & PF_USER ? "U" : "-"),
223 (expected & PF_WRITE ? "W" : "-")
230 * \fn tPAddr MM_Allocate(Uint VAddr)
232 tPAddr MM_Allocate(Uint VAddr)
235 // Check if the directory is mapped
236 if( gaPageDir[ VAddr >> 22 ] == 0 )
238 // Allocate directory
239 paddr = MM_AllocPhys();
241 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
245 gaPageDir[ VAddr >> 22 ] = paddr | 3;
247 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
249 INVLPG( &gaPageDir[ VAddr >> 22 ] );
250 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
252 // Check if the page is already allocated
253 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
254 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
255 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
259 paddr = MM_AllocPhys();
261 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
262 VAddr, __builtin_return_address(0));
266 gaPageTable[ VAddr >> 12 ] = paddr | 3;
268 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
269 // Invalidate Cache for address
270 INVLPG( VAddr & ~0xFFF );
276 * \fn void MM_Deallocate(Uint VAddr)
278 void MM_Deallocate(Uint VAddr)
280 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
281 Warning("MM_Deallocate - Directory not mapped");
285 if(gaPageTable[ VAddr >> 12 ] == 0) {
286 Warning("MM_Deallocate - Page is not allocated");
291 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
293 gaPageTable[ VAddr >> 12 ] = 0;
297 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
298 * \brief Checks if the passed address is accesable
300 tPAddr MM_GetPhysAddr(Uint Addr)
302 if( !(gaPageDir[Addr >> 22] & 1) )
304 if( !(gaPageTable[Addr >> 12] & 1) )
306 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
310 * \fn void MM_SetCR3(Uint CR3)
311 * \brief Sets the current process space
313 void MM_SetCR3(Uint CR3)
315 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
319 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
320 * \brief Map a physical page to a virtual one
322 int MM_Map(Uint VAddr, tPAddr PAddr)
324 //ENTER("xVAddr xPAddr", VAddr, PAddr);
326 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
327 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
333 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
335 // Check if the directory is mapped
336 if( gaPageDir[ VAddr >> 22 ] == 0 )
338 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
341 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
343 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
344 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
346 // Check if the page is already allocated
347 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
348 Warning("MM_Map - Allocating to used address");
354 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
356 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
358 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
359 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
364 //LOG("INVLPG( 0x%x )", VAddr);
372 * \fn Uint MM_ClearUser()
373 * \brief Clear user's address space
380 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
382 // Check if directory is not allocated
383 if( !(gaPageDir[i] & PF_PRESENT) ) {
389 for( j = 0; j < 1024; j ++ )
391 if( gaPageTable[i*1024+j] & 1 )
392 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
393 gaPageTable[i*1024+j] = 0;
396 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
398 INVLPG( &gaPageTable[i*1024] );
406 * \fn Uint MM_Clone()
407 * \brief Clone the current address space
413 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
418 // Create Directory Table
419 *gTmpCR3 = MM_AllocPhys() | 3;
421 //LOG("Allocated Directory (%x)", *gTmpCR3);
422 memsetd( gaTmpDir, 0, 1024 );
427 // Check if table is allocated
428 if( !(gaPageDir[i] & PF_PRESENT) ) {
434 // Allocate new table
435 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
436 INVLPG( &gaTmpTable[page] );
438 for( j = 0; j < 1024; j ++, page++ )
440 if( !(gaPageTable[page] & PF_PRESENT) ) {
441 gaTmpTable[page] = 0;
446 MM_RefPhys( gaPageTable[page] & ~0xFFF );
448 if(gaPageTable[page] & PF_WRITE) {
449 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
450 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
451 INVLPG( page << 12 );
454 gaTmpTable[page] = gaPageTable[page];
458 // Map in kernel tables (and make fractal mapping)
459 for( i = 768; i < 1024; i ++ )
462 if( i == (PAGE_TABLE_ADDR >> 22) ) {
463 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
467 if( gaPageDir[i] == 0 ) {
472 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
473 MM_RefPhys( gaPageDir[i] & ~0xFFF );
474 gaTmpDir[i] = gaPageDir[i];
477 // Allocate kernel stack
478 for(i = KERNEL_STACKS >> 22;
479 i < KERNEL_STACK_END >> 22;
482 // Check if directory is allocated
483 if( (gaPageDir[i] & 1) == 0 ) {
488 // We don't care about other kernel stacks, just the current one
489 if( i != kStackBase >> 22 ) {
490 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
496 gaTmpDir[i] = MM_AllocPhys() | 3;
497 INVLPG( &gaTmpTable[i*1024] );
498 for( j = 0; j < 1024; j ++ )
500 // Is the page allocated? If not, skip
501 if( !(gaPageTable[i*1024+j] & 1) ) {
502 gaTmpTable[i*1024+j] = 0;
506 // We don't care about other kernel stacks
507 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
508 gaTmpTable[i*1024+j] = 0;
513 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
515 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
517 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
518 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
519 MM_FreeTemp( (Uint)tmp );
523 //LEAVE('x', *gTmpCR3 & ~0xFFF);
524 return *gTmpCR3 & ~0xFFF;
528 * \fn Uint MM_NewKStack()
529 * \brief Create a new kernel stack
533 Uint base = KERNEL_STACKS;
535 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
537 if(MM_GetPhysAddr(base) != 0) continue;
538 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
541 return base+KERNEL_STACK_SIZE;
543 Warning("MM_NewKStack - No address space left\n");
548 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
549 * \brief Sets the flags on a page
551 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
554 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
555 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
557 ent = &gaPageTable[VAddr >> 12];
560 if( Mask & MM_PFLAG_RO )
562 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
563 else *ent |= PF_WRITE;
567 if( Mask & MM_PFLAG_KERNEL )
569 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
570 else *ent |= PF_USER;
574 if( Mask & MM_PFLAG_COW )
576 if( Flags & MM_PFLAG_COW ) {
588 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
589 * \brief Duplicates a virtual page to a physical one
591 tPAddr MM_DuplicatePage(Uint VAddr)
598 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
599 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
605 ret = MM_AllocPhys();
607 // Write-lock the page (to keep data constistent), saving its R/W state
608 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
609 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
613 temp = MM_MapTemp(ret);
614 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
617 // Restore Writeable status
618 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
625 * \fn Uint MM_MapTemp(tPAddr PAddr)
626 * \brief Create a temporary memory mapping
627 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
629 Uint MM_MapTemp(tPAddr PAddr)
633 //ENTER("XPAddr", PAddr);
637 //LOG("gilTempMappings = %i", gilTempMappings);
641 LOCK( &gilTempMappings );
643 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
645 // Check if page used
646 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
648 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
649 INVLPG( TEMP_MAP_ADDR + (i << 12) );
650 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
651 RELEASE( &gilTempMappings );
652 return TEMP_MAP_ADDR + (i << 12);
654 RELEASE( &gilTempMappings );
660 * \fn void MM_FreeTemp(Uint PAddr)
661 * \brief Free's a temp mapping
663 void MM_FreeTemp(Uint VAddr)
666 //ENTER("xVAddr", VAddr);
668 if(i >= (TEMP_MAP_ADDR >> 12))
669 gaPageTable[ i ] = 0;
675 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
676 * \brief Allocates a contigous number of pages
678 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
685 for( i = 0; i < NUM_HW_PAGES; i ++ )
687 // Check if addr used
688 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
691 // Check possible region
692 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
694 // If there is an allocated page in the region we are testing, break
695 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
701 for( j = 0; j < Number; j++ ) {
702 MM_RefPhys( PAddr + (j<<12) );
703 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
705 return HW_MAP_ADDR + (i<<12);
708 // If we don't find any, return NULL
713 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
714 * \brief Unmap a hardware page
716 void MM_UnmapHWPage(Uint VAddr, Uint Number)
720 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
724 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
726 for( j = 0; j < Number; j++ )
728 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
729 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
732 RELEASE( &gilTempMappings );
736 EXPORT(MM_GetPhysAddr);
739 EXPORT(MM_MapHWPage);
740 EXPORT(MM_UnmapHWPage);