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", Regs->eip, Addr);
132 __asm__ __volatile__ ("sti"); // Restart IRQs
133 Threads_SegFault(Addr);
137 // -- Check Error Code --
139 Warning("Reserved Bits Trashed!");
142 Warning("%s %s %s memory%s",
143 (ErrorCode&4?"User":"Kernel"),
144 (ErrorCode&2?"write to":"read from"),
145 (ErrorCode&1?"bad/locked":"non-present"),
146 (ErrorCode&16?" (Instruction Fetch)":"")
150 Log("Code at %p accessed %p", Regs->eip, Addr);
151 // Print Stack Backtrace
152 Error_Backtrace(Regs->eip, Regs->ebp);
154 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]);
155 if( gaPageDir[Addr>>22] & PF_PRESENT )
156 Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]);
158 MM_DumpTables(0, -1);
160 Panic("Page Fault at 0x%x\n", Regs->eip);
164 * \fn void MM_DumpTables(Uint Start, Uint End)
165 * \brief Dumps the layout of the page tables
167 void MM_DumpTables(tVAddr Start, tVAddr End)
169 tVAddr rangeStart = 0;
173 const tPAddr MASK = ~0xF98;
175 Start >>= 12; End >>= 12;
178 Log("Directory Entries:");
179 for(page = Start >> 10;
180 page < (End >> 10)+1;
185 Log(" 0x%08x-0x%08x :: 0x%08x",
186 page<<22, ((page+1)<<22)-1,
187 gaPageDir[page]&~0xFFF
193 Log("Table Entries:");
194 for(page = Start, curPos = Start<<12;
196 curPos += 0x1000, page++)
198 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
199 || !(gaPageTable[page] & PF_PRESENT)
200 || (gaPageTable[page] & MASK) != expected)
203 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
204 rangeStart, curPos - 1,
205 gaPageTable[rangeStart>>12] & ~0xFFF,
206 (expected & ~0xFFF) - 1,
207 (expected & PF_PAGED ? "p" : "-"),
208 (expected & PF_COW ? "C" : "-"),
209 (expected & PF_USER ? "U" : "-"),
210 (expected & PF_WRITE ? "W" : "-")
214 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
215 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
217 expected = (gaPageTable[page] & MASK);
220 if(expected) expected += 0x1000;
224 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
225 rangeStart, curPos - 1,
226 gaPageTable[rangeStart>>12] & ~0xFFF,
227 (expected & ~0xFFF) - 1,
228 (expected & PF_PAGED ? "p" : "-"),
229 (expected & PF_COW ? "C" : "-"),
230 (expected & PF_USER ? "U" : "-"),
231 (expected & PF_WRITE ? "W" : "-")
238 * \fn tPAddr MM_Allocate(Uint VAddr)
240 tPAddr MM_Allocate(Uint VAddr)
243 // Check if the directory is mapped
244 if( gaPageDir[ VAddr >> 22 ] == 0 )
246 // Allocate directory
247 paddr = MM_AllocPhys();
249 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
253 gaPageDir[ VAddr >> 22 ] = paddr | 3;
255 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
257 INVLPG( &gaPageDir[ VAddr >> 22 ] );
258 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
260 // Check if the page is already allocated
261 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
262 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
263 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
267 paddr = MM_AllocPhys();
269 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
270 VAddr, __builtin_return_address(0));
274 gaPageTable[ VAddr >> 12 ] = paddr | 3;
276 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
277 // Invalidate Cache for address
278 INVLPG( VAddr & ~0xFFF );
284 * \fn void MM_Deallocate(Uint VAddr)
286 void MM_Deallocate(Uint VAddr)
288 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
289 Warning("MM_Deallocate - Directory not mapped");
293 if(gaPageTable[ VAddr >> 12 ] == 0) {
294 Warning("MM_Deallocate - Page is not allocated");
299 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
301 gaPageTable[ VAddr >> 12 ] = 0;
305 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
306 * \brief Checks if the passed address is accesable
308 tPAddr MM_GetPhysAddr(Uint Addr)
310 if( !(gaPageDir[Addr >> 22] & 1) )
312 if( !(gaPageTable[Addr >> 12] & 1) )
314 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
318 * \fn void MM_SetCR3(Uint CR3)
319 * \brief Sets the current process space
321 void MM_SetCR3(Uint CR3)
323 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
327 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
328 * \brief Map a physical page to a virtual one
330 int MM_Map(Uint VAddr, tPAddr PAddr)
332 //ENTER("xVAddr xPAddr", VAddr, PAddr);
334 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
335 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
341 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
343 // Check if the directory is mapped
344 if( gaPageDir[ VAddr >> 22 ] == 0 )
346 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
349 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
351 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
352 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
354 // Check if the page is already allocated
355 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
356 Warning("MM_Map - Allocating to used address");
362 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
364 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
366 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
367 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
372 //LOG("INVLPG( 0x%x )", VAddr);
380 * \fn Uint MM_ClearUser()
381 * \brief Clear user's address space
388 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
390 // Check if directory is not allocated
391 if( !(gaPageDir[i] & PF_PRESENT) ) {
397 for( j = 0; j < 1024; j ++ )
399 if( gaPageTable[i*1024+j] & 1 )
400 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
401 gaPageTable[i*1024+j] = 0;
404 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
406 INVLPG( &gaPageTable[i*1024] );
414 * \fn Uint MM_Clone()
415 * \brief Clone the current address space
421 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
426 // Create Directory Table
427 *gTmpCR3 = MM_AllocPhys() | 3;
429 //LOG("Allocated Directory (%x)", *gTmpCR3);
430 memsetd( gaTmpDir, 0, 1024 );
435 // Check if table is allocated
436 if( !(gaPageDir[i] & PF_PRESENT) ) {
442 // Allocate new table
443 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
444 INVLPG( &gaTmpTable[page] );
446 for( j = 0; j < 1024; j ++, page++ )
448 if( !(gaPageTable[page] & PF_PRESENT) ) {
449 gaTmpTable[page] = 0;
454 MM_RefPhys( gaPageTable[page] & ~0xFFF );
456 if(gaPageTable[page] & PF_WRITE) {
457 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
458 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
459 INVLPG( page << 12 );
462 gaTmpTable[page] = gaPageTable[page];
466 // Map in kernel tables (and make fractal mapping)
467 for( i = 768; i < 1024; i ++ )
470 if( i == (PAGE_TABLE_ADDR >> 22) ) {
471 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
475 if( gaPageDir[i] == 0 ) {
480 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
481 MM_RefPhys( gaPageDir[i] & ~0xFFF );
482 gaTmpDir[i] = gaPageDir[i];
485 // Allocate kernel stack
486 for(i = KERNEL_STACKS >> 22;
487 i < KERNEL_STACK_END >> 22;
490 // Check if directory is allocated
491 if( (gaPageDir[i] & 1) == 0 ) {
496 // We don't care about other kernel stacks, just the current one
497 if( i != kStackBase >> 22 ) {
498 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
504 gaTmpDir[i] = MM_AllocPhys() | 3;
505 INVLPG( &gaTmpTable[i*1024] );
506 for( j = 0; j < 1024; j ++ )
508 // Is the page allocated? If not, skip
509 if( !(gaPageTable[i*1024+j] & 1) ) {
510 gaTmpTable[i*1024+j] = 0;
514 // We don't care about other kernel stacks
515 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
516 gaTmpTable[i*1024+j] = 0;
521 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
523 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
525 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
526 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
527 MM_FreeTemp( (Uint)tmp );
531 //LEAVE('x', *gTmpCR3 & ~0xFFF);
532 return *gTmpCR3 & ~0xFFF;
536 * \fn Uint MM_NewKStack()
537 * \brief Create a new kernel stack
541 Uint base = KERNEL_STACKS;
543 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
545 if(MM_GetPhysAddr(base) != 0) continue;
546 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
549 return base+KERNEL_STACK_SIZE;
551 Warning("MM_NewKStack - No address space left\n");
556 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
557 * \brief Sets the flags on a page
559 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
562 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
563 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
565 ent = &gaPageTable[VAddr >> 12];
568 if( Mask & MM_PFLAG_RO )
570 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
571 else *ent |= PF_WRITE;
575 if( Mask & MM_PFLAG_KERNEL )
577 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
578 else *ent |= PF_USER;
582 if( Mask & MM_PFLAG_COW )
584 if( Flags & MM_PFLAG_COW ) {
596 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
597 * \brief Duplicates a virtual page to a physical one
599 tPAddr MM_DuplicatePage(Uint VAddr)
606 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
607 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
613 ret = MM_AllocPhys();
615 // Write-lock the page (to keep data constistent), saving its R/W state
616 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
617 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
621 temp = MM_MapTemp(ret);
622 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
625 // Restore Writeable status
626 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
633 * \fn Uint MM_MapTemp(tPAddr PAddr)
634 * \brief Create a temporary memory mapping
635 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
637 Uint MM_MapTemp(tPAddr PAddr)
641 //ENTER("XPAddr", PAddr);
645 //LOG("gilTempMappings = %i", gilTempMappings);
649 LOCK( &gilTempMappings );
651 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
653 // Check if page used
654 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
656 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
657 INVLPG( TEMP_MAP_ADDR + (i << 12) );
658 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
659 RELEASE( &gilTempMappings );
660 return TEMP_MAP_ADDR + (i << 12);
662 RELEASE( &gilTempMappings );
668 * \fn void MM_FreeTemp(Uint PAddr)
669 * \brief Free's a temp mapping
671 void MM_FreeTemp(Uint VAddr)
674 //ENTER("xVAddr", VAddr);
676 if(i >= (TEMP_MAP_ADDR >> 12))
677 gaPageTable[ i ] = 0;
683 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
684 * \brief Allocates a contigous number of pages
686 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
693 for( i = 0; i < NUM_HW_PAGES; i ++ )
695 // Check if addr used
696 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
699 // Check possible region
700 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
702 // If there is an allocated page in the region we are testing, break
703 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
709 for( j = 0; j < Number; j++ ) {
710 MM_RefPhys( PAddr + (j<<12) );
711 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
713 return HW_MAP_ADDR + (i<<12);
716 // If we don't find any, return NULL
721 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
722 * \brief Unmap a hardware page
724 void MM_UnmapHWPage(Uint VAddr, Uint Number)
728 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
732 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
734 for( j = 0; j < Number; j++ )
736 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
737 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
740 RELEASE( &gilTempMappings );
744 EXPORT(MM_GetPhysAddr);
747 EXPORT(MM_MapHWPage);
748 EXPORT(MM_UnmapHWPage);