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);
44 extern void Error_Backtrace(Uint eip, Uint ebp);
47 void MM_PreinitVirtual();
48 void MM_InstallVirtual();
49 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
50 void MM_DumpTables(tVAddr Start, tVAddr End);
51 tPAddr MM_DuplicatePage(Uint VAddr);
54 tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR;
55 tPAddr *gaPageDir = (void*)PAGE_DIR_ADDR;
56 tPAddr *gaPageCR3 = (void*)PAGE_CR3_ADDR;
57 tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR;
58 tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR;
59 tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR;
60 int gilTempMappings = 0;
64 * \fn void MM_PreinitVirtual()
65 * \brief Maps the fractal mappings
67 void MM_PreinitVirtual()
69 gaInitPageDir[ 0 ] = 0;
70 gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((Uint)&gaInitPageDir - KERNEL_BASE) | 3;
74 * \fn void MM_InstallVirtual()
75 * \brief Sets up the constant page mappings
77 void MM_InstallVirtual()
81 // --- Pre-Allocate kernel tables
82 for( i = KERNEL_BASE>>22; i < 1024; i ++ )
84 if( gaPageDir[ i ] ) continue;
85 // Skip stack tables, they are process unique
86 if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACK_END >> 22) {
91 gaPageDir[ i ] = MM_AllocPhys() | 3;
92 INVLPG( &gaPageTable[i*1024] );
93 memset( &gaPageTable[i*1024], 0, 0x1000 );
98 * \fn void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
99 * \brief Called on a page fault
101 void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs)
103 //ENTER("xAddr bErrorCode", Addr, ErrorCode);
105 // -- Check for COW --
106 if( gaPageDir [Addr>>22] & PF_PRESENT
107 && gaPageTable[Addr>>12] & PF_PRESENT
108 && gaPageTable[Addr>>12] & PF_COW )
111 if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1)
113 gaPageTable[Addr>>12] &= ~PF_COW;
114 gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE;
118 paddr = MM_DuplicatePage( Addr );
119 MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF );
120 gaPageTable[Addr>>12] &= PF_USER;
121 gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
124 INVLPG( Addr & ~0xFFF );
129 // If it was a user, tell the thread handler
131 Warning("User Pagefault: Instruction at %p accessed %p\n", Regs->eip, Addr);
132 Threads_SegFault(Addr);
136 // -- Check Error Code --
138 Warning("Reserved Bits Trashed!");
141 Warning("%s %s %s memory%s",
142 (ErrorCode&4?"User":"Kernel"),
143 (ErrorCode&2?"write to":"read from"),
144 (ErrorCode&1?"bad/locked":"non-present"),
145 (ErrorCode&16?" (Instruction Fetch)":"")
149 Log("Code at %p accessed %p", Regs->eip, Addr);
150 // Print Stack Backtrace
151 Error_Backtrace(Regs->eip, Regs->ebp);
153 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
154 if( gaPageDir[Addr>>22] & PF_PRESENT )
155 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
157 MM_DumpTables(0, -1);
159 Panic("Page Fault at 0x%x\n", Regs->eip);
163 * \fn void MM_DumpTables(Uint Start, Uint End)
164 * \brief Dumps the layout of the page tables
166 void MM_DumpTables(tVAddr Start, tVAddr End)
168 tVAddr rangeStart = 0;
172 const tPAddr MASK = ~0xF98;
174 Start >>= 12; End >>= 12;
177 Log("Directory Entries:");
178 for(page = Start >> 10;
179 page < (End >> 10)+1;
184 Log(" 0x%08x-0x%08x :: 0x%08x",
185 page<<22, ((page+1)<<22)-1,
186 gaPageDir[page]&~0xFFF
192 Log("Table Entries:");
193 for(page = Start, curPos = Start<<12;
195 curPos += 0x1000, page++)
197 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
198 || !(gaPageTable[page] & PF_PRESENT)
199 || (gaPageTable[page] & MASK) != expected)
202 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
203 rangeStart, curPos - 1,
204 gaPageTable[rangeStart>>12] & ~0xFFF,
205 (expected & ~0xFFF) - 1,
206 (expected & PF_PAGED ? "p" : "-"),
207 (expected & PF_COW ? "C" : "-"),
208 (expected & PF_USER ? "U" : "-"),
209 (expected & PF_WRITE ? "W" : "-")
213 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
214 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
216 expected = (gaPageTable[page] & MASK);
219 if(expected) expected += 0x1000;
223 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
224 rangeStart, curPos - 1,
225 gaPageTable[rangeStart>>12] & ~0xFFF,
226 (expected & ~0xFFF) - 1,
227 (expected & PF_PAGED ? "p" : "-"),
228 (expected & PF_COW ? "C" : "-"),
229 (expected & PF_USER ? "U" : "-"),
230 (expected & PF_WRITE ? "W" : "-")
237 * \fn tPAddr MM_Allocate(Uint VAddr)
239 tPAddr MM_Allocate(Uint VAddr)
242 // Check if the directory is mapped
243 if( gaPageDir[ VAddr >> 22 ] == 0 )
245 // Allocate directory
246 paddr = MM_AllocPhys();
248 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
252 gaPageDir[ VAddr >> 22 ] = paddr | 3;
254 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
256 INVLPG( &gaPageDir[ VAddr >> 22 ] );
257 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
259 // Check if the page is already allocated
260 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
261 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
262 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
266 paddr = MM_AllocPhys();
268 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
269 VAddr, __builtin_return_address(0));
273 gaPageTable[ VAddr >> 12 ] = paddr | 3;
275 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
276 // Invalidate Cache for address
277 INVLPG( VAddr & ~0xFFF );
283 * \fn void MM_Deallocate(Uint VAddr)
285 void MM_Deallocate(Uint VAddr)
287 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
288 Warning("MM_Deallocate - Directory not mapped");
292 if(gaPageTable[ VAddr >> 12 ] == 0) {
293 Warning("MM_Deallocate - Page is not allocated");
298 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
300 gaPageTable[ VAddr >> 12 ] = 0;
304 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
305 * \brief Checks if the passed address is accesable
307 tPAddr MM_GetPhysAddr(Uint Addr)
309 if( !(gaPageDir[Addr >> 22] & 1) )
311 if( !(gaPageTable[Addr >> 12] & 1) )
313 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
317 * \fn void MM_SetCR3(Uint CR3)
318 * \brief Sets the current process space
320 void MM_SetCR3(Uint CR3)
322 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
326 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
327 * \brief Map a physical page to a virtual one
329 int MM_Map(Uint VAddr, tPAddr PAddr)
331 //ENTER("xVAddr xPAddr", VAddr, PAddr);
333 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
334 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
340 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
342 // Check if the directory is mapped
343 if( gaPageDir[ VAddr >> 22 ] == 0 )
345 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
348 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
350 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
351 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
353 // Check if the page is already allocated
354 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
355 Warning("MM_Map - Allocating to used address");
361 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
363 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
365 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
366 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
371 //LOG("INVLPG( 0x%x )", VAddr);
379 * \fn Uint MM_ClearUser()
380 * \brief Clear user's address space
387 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
389 // Check if directory is not allocated
390 if( !(gaPageDir[i] & PF_PRESENT) ) {
396 for( j = 0; j < 1024; j ++ )
398 if( gaPageTable[i*1024+j] & 1 )
399 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
400 gaPageTable[i*1024+j] = 0;
403 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
405 INVLPG( &gaPageTable[i*1024] );
413 * \fn Uint MM_Clone()
414 * \brief Clone the current address space
420 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
425 // Create Directory Table
426 *gTmpCR3 = MM_AllocPhys() | 3;
428 //LOG("Allocated Directory (%x)", *gTmpCR3);
429 memsetd( gaTmpDir, 0, 1024 );
434 // Check if table is allocated
435 if( !(gaPageDir[i] & PF_PRESENT) ) {
441 // Allocate new table
442 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
443 INVLPG( &gaTmpTable[page] );
445 for( j = 0; j < 1024; j ++, page++ )
447 if( !(gaPageTable[page] & PF_PRESENT) ) {
448 gaTmpTable[page] = 0;
453 MM_RefPhys( gaPageTable[page] & ~0xFFF );
455 if(gaPageTable[page] & PF_WRITE) {
456 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
457 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
458 INVLPG( page << 12 );
461 gaTmpTable[page] = gaPageTable[page];
465 // Map in kernel tables (and make fractal mapping)
466 for( i = 768; i < 1024; i ++ )
469 if( i == (PAGE_TABLE_ADDR >> 22) ) {
470 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
474 if( gaPageDir[i] == 0 ) {
479 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
480 MM_RefPhys( gaPageDir[i] & ~0xFFF );
481 gaTmpDir[i] = gaPageDir[i];
484 // Allocate kernel stack
485 for(i = KERNEL_STACKS >> 22;
486 i < KERNEL_STACK_END >> 22;
489 // Check if directory is allocated
490 if( (gaPageDir[i] & 1) == 0 ) {
495 // We don't care about other kernel stacks, just the current one
496 if( i != kStackBase >> 22 ) {
497 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
503 gaTmpDir[i] = MM_AllocPhys() | 3;
504 INVLPG( &gaTmpTable[i*1024] );
505 for( j = 0; j < 1024; j ++ )
507 // Is the page allocated? If not, skip
508 if( !(gaPageTable[i*1024+j] & 1) ) {
509 gaTmpTable[i*1024+j] = 0;
513 // We don't care about other kernel stacks
514 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
515 gaTmpTable[i*1024+j] = 0;
520 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
522 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
524 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
525 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
526 MM_FreeTemp( (Uint)tmp );
530 //LEAVE('x', *gTmpCR3 & ~0xFFF);
531 return *gTmpCR3 & ~0xFFF;
535 * \fn Uint MM_NewKStack()
536 * \brief Create a new kernel stack
540 Uint base = KERNEL_STACKS;
542 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
544 if(MM_GetPhysAddr(base) != 0) continue;
545 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
548 return base+KERNEL_STACK_SIZE;
550 Warning("MM_NewKStack - No address space left\n");
555 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
556 * \brief Sets the flags on a page
558 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
561 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
562 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
564 ent = &gaPageTable[VAddr >> 12];
567 if( Mask & MM_PFLAG_RO )
569 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
570 else *ent |= PF_WRITE;
574 if( Mask & MM_PFLAG_KERNEL )
576 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
577 else *ent |= PF_USER;
581 if( Mask & MM_PFLAG_COW )
583 if( Flags & MM_PFLAG_COW ) {
595 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
596 * \brief Duplicates a virtual page to a physical one
598 tPAddr MM_DuplicatePage(Uint VAddr)
605 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
606 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
612 ret = MM_AllocPhys();
614 // Write-lock the page (to keep data constistent), saving its R/W state
615 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
616 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
620 temp = MM_MapTemp(ret);
621 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
624 // Restore Writeable status
625 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
632 * \fn Uint MM_MapTemp(tPAddr PAddr)
633 * \brief Create a temporary memory mapping
634 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
636 Uint MM_MapTemp(tPAddr PAddr)
640 //ENTER("XPAddr", PAddr);
644 //LOG("gilTempMappings = %i", gilTempMappings);
648 LOCK( &gilTempMappings );
650 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
652 // Check if page used
653 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
655 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
656 INVLPG( TEMP_MAP_ADDR + (i << 12) );
657 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
658 RELEASE( &gilTempMappings );
659 return TEMP_MAP_ADDR + (i << 12);
661 RELEASE( &gilTempMappings );
667 * \fn void MM_FreeTemp(Uint PAddr)
668 * \brief Free's a temp mapping
670 void MM_FreeTemp(Uint VAddr)
673 //ENTER("xVAddr", VAddr);
675 if(i >= (TEMP_MAP_ADDR >> 12))
676 gaPageTable[ i ] = 0;
682 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
683 * \brief Allocates a contigous number of pages
685 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
692 for( i = 0; i < NUM_HW_PAGES; i ++ )
694 // Check if addr used
695 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
698 // Check possible region
699 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
701 // If there is an allocated page in the region we are testing, break
702 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
708 for( j = 0; j < Number; j++ ) {
709 MM_RefPhys( PAddr + (j<<12) );
710 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
712 return HW_MAP_ADDR + (i<<12);
715 // If we don't find any, return NULL
720 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
721 * \brief Unmap a hardware page
723 void MM_UnmapHWPage(Uint VAddr, Uint Number)
727 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
731 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
733 for( j = 0; j < Number; j++ )
735 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
736 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
739 RELEASE( &gilTempMappings );
743 EXPORT(MM_GetPhysAddr);
746 EXPORT(MM_MapHWPage);
747 EXPORT(MM_UnmapHWPage);