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 Log("Code at %p accessed %p\n", Regs->eip, Addr);
154 MM_DumpTables(0, -1);
156 Panic("Page Fault at 0x%x\n", Regs->eip);
160 * \fn void MM_DumpTables(Uint Start, Uint End)
161 * \brief Dumps the layout of the page tables
163 void MM_DumpTables(tVAddr Start, tVAddr End)
165 tVAddr rangeStart = 0;
169 const tPAddr MASK = ~0xF98;
171 Start >>= 12; End >>= 12;
174 Log("Directory Entries:");
175 for(page = Start >> 10;
176 page < (End >> 10)+1;
181 Log(" 0x%08x-0x%08x :: 0x%08x",
182 page<<22, ((page+1)<<22)-1,
183 gaPageDir[page]&~0xFFF
189 Log("Table Entries:");
190 for(page = Start, curPos = Start<<12;
192 curPos += 0x1000, page++)
194 if( !(gaPageDir[curPos>>22] & PF_PRESENT)
195 || !(gaPageTable[page] & PF_PRESENT)
196 || (gaPageTable[page] & MASK) != expected)
199 Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
200 rangeStart, curPos - 1,
201 gaPageTable[rangeStart>>12] & ~0xFFF,
202 (expected & ~0xFFF) - 1,
203 (expected & PF_PAGED ? "p" : "-"),
204 (expected & PF_COW ? "C" : "-"),
205 (expected & PF_USER ? "U" : "-"),
206 (expected & PF_WRITE ? "W" : "-")
210 if( !(gaPageDir[curPos>>22] & PF_PRESENT) ) continue;
211 if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue;
213 expected = (gaPageTable[page] & MASK);
216 if(expected) expected += 0x1000;
220 Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
221 rangeStart, curPos - 1,
222 gaPageTable[rangeStart>>12] & ~0xFFF,
223 (expected & ~0xFFF) - 1,
224 (expected & PF_PAGED ? "p" : "-"),
225 (expected & PF_COW ? "C" : "-"),
226 (expected & PF_USER ? "U" : "-"),
227 (expected & PF_WRITE ? "W" : "-")
234 * \fn tPAddr MM_Allocate(Uint VAddr)
236 tPAddr MM_Allocate(Uint VAddr)
239 // Check if the directory is mapped
240 if( gaPageDir[ VAddr >> 22 ] == 0 )
242 // Allocate directory
243 paddr = MM_AllocPhys();
245 Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
249 gaPageDir[ VAddr >> 22 ] = paddr | 3;
251 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
253 INVLPG( &gaPageDir[ VAddr >> 22 ] );
254 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
256 // Check if the page is already allocated
257 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
258 Warning("MM_Allocate - Allocating to used address (%p)", VAddr);
259 return gaPageTable[ VAddr >> 12 ] & ~0xFFF;
263 paddr = MM_AllocPhys();
265 Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)",
266 VAddr, __builtin_return_address(0));
270 gaPageTable[ VAddr >> 12 ] = paddr | 3;
272 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
273 // Invalidate Cache for address
274 INVLPG( VAddr & ~0xFFF );
280 * \fn void MM_Deallocate(Uint VAddr)
282 void MM_Deallocate(Uint VAddr)
284 if( gaPageDir[ VAddr >> 22 ] == 0 ) {
285 Warning("MM_Deallocate - Directory not mapped");
289 if(gaPageTable[ VAddr >> 12 ] == 0) {
290 Warning("MM_Deallocate - Page is not allocated");
295 MM_DerefPhys( gaPageTable[ VAddr >> 12 ] & ~0xFFF );
297 gaPageTable[ VAddr >> 12 ] = 0;
301 * \fn tPAddr MM_GetPhysAddr(Uint Addr)
302 * \brief Checks if the passed address is accesable
304 tPAddr MM_GetPhysAddr(Uint Addr)
306 if( !(gaPageDir[Addr >> 22] & 1) )
308 if( !(gaPageTable[Addr >> 12] & 1) )
310 return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF);
314 * \fn void MM_SetCR3(Uint CR3)
315 * \brief Sets the current process space
317 void MM_SetCR3(Uint CR3)
319 __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3));
323 * \fn int MM_Map(Uint VAddr, tPAddr PAddr)
324 * \brief Map a physical page to a virtual one
326 int MM_Map(Uint VAddr, tPAddr PAddr)
328 //ENTER("xVAddr xPAddr", VAddr, PAddr);
330 if( PAddr & 0xFFF || VAddr & 0xFFF ) {
331 Warning("MM_Map - Physical or Virtual Addresses are not aligned");
337 PAddr &= ~0xFFF; VAddr &= ~0xFFF;
339 // Check if the directory is mapped
340 if( gaPageDir[ VAddr >> 22 ] == 0 )
342 gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
345 if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
347 INVLPG( &gaPageTable[ (VAddr >> 12) & ~0x3FF ] );
348 memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
350 // Check if the page is already allocated
351 else if( gaPageTable[ VAddr >> 12 ] != 0 ) {
352 Warning("MM_Map - Allocating to used address");
358 gaPageTable[ VAddr >> 12 ] = PAddr | 3;
360 if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER;
362 //LOG("gaPageTable[ 0x%x ] = (Uint)%p = 0x%x",
363 // VAddr >> 12, &gaPageTable[ VAddr >> 12 ], gaPageTable[ VAddr >> 12 ]);
368 //LOG("INVLPG( 0x%x )", VAddr);
376 * \fn Uint MM_ClearUser()
377 * \brief Clear user's address space
384 for( i = 0; i < (MM_USER_MAX>>22); i ++ )
386 // Check if directory is not allocated
387 if( !(gaPageDir[i] & PF_PRESENT) ) {
393 for( j = 0; j < 1024; j ++ )
395 if( gaPageTable[i*1024+j] & 1 )
396 MM_DerefPhys( gaPageTable[i*1024+j] & ~0xFFF );
397 gaPageTable[i*1024+j] = 0;
400 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
402 INVLPG( &gaPageTable[i*1024] );
410 * \fn Uint MM_Clone()
411 * \brief Clone the current address space
417 Uint kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
422 // Create Directory Table
423 *gTmpCR3 = MM_AllocPhys() | 3;
425 //LOG("Allocated Directory (%x)", *gTmpCR3);
426 memsetd( gaTmpDir, 0, 1024 );
431 // Check if table is allocated
432 if( !(gaPageDir[i] & PF_PRESENT) ) {
438 // Allocate new table
439 gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
440 INVLPG( &gaTmpTable[page] );
442 for( j = 0; j < 1024; j ++, page++ )
444 if( !(gaPageTable[page] & PF_PRESENT) ) {
445 gaTmpTable[page] = 0;
450 MM_RefPhys( gaPageTable[page] & ~0xFFF );
452 if(gaPageTable[page] & PF_WRITE) {
453 gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
454 gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
455 INVLPG( page << 12 );
458 gaTmpTable[page] = gaPageTable[page];
462 // Map in kernel tables (and make fractal mapping)
463 for( i = 768; i < 1024; i ++ )
466 if( i == (PAGE_TABLE_ADDR >> 22) ) {
467 gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gTmpCR3;
471 if( gaPageDir[i] == 0 ) {
476 //LOG("gaPageDir[%x/4] = 0x%x", i*4, gaPageDir[i]);
477 MM_RefPhys( gaPageDir[i] & ~0xFFF );
478 gaTmpDir[i] = gaPageDir[i];
481 // Allocate kernel stack
482 for(i = KERNEL_STACKS >> 22;
483 i < KERNEL_STACK_END >> 22;
486 // Check if directory is allocated
487 if( (gaPageDir[i] & 1) == 0 ) {
492 // We don't care about other kernel stacks, just the current one
493 if( i != kStackBase >> 22 ) {
494 MM_DerefPhys( gaPageDir[i] & ~0xFFF );
500 gaTmpDir[i] = MM_AllocPhys() | 3;
501 INVLPG( &gaTmpTable[i*1024] );
502 for( j = 0; j < 1024; j ++ )
504 // Is the page allocated? If not, skip
505 if( !(gaPageTable[i*1024+j] & 1) ) {
506 gaTmpTable[i*1024+j] = 0;
510 // We don't care about other kernel stacks
511 if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
512 gaTmpTable[i*1024+j] = 0;
517 gaTmpTable[i*1024+j] = MM_AllocPhys() | 3;
519 MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF );
521 tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF );
522 memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 );
523 MM_FreeTemp( (Uint)tmp );
527 //LEAVE('x', *gTmpCR3 & ~0xFFF);
528 return *gTmpCR3 & ~0xFFF;
532 * \fn Uint MM_NewKStack()
533 * \brief Create a new kernel stack
537 Uint base = KERNEL_STACKS;
539 for(;base<KERNEL_STACK_END;base+=KERNEL_STACK_SIZE)
541 if(MM_GetPhysAddr(base) != 0) continue;
542 for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
545 return base+KERNEL_STACK_SIZE;
547 Warning("MM_NewKStack - No address space left\n");
552 * \fn void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
553 * \brief Sets the flags on a page
555 void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask)
558 if( !(gaPageDir[VAddr >> 22] & 1) ) return ;
559 if( !(gaPageTable[VAddr >> 12] & 1) ) return ;
561 ent = &gaPageTable[VAddr >> 12];
564 if( Mask & MM_PFLAG_RO )
566 if( Flags & MM_PFLAG_RO ) *ent &= ~PF_WRITE;
567 else *ent |= PF_WRITE;
571 if( Mask & MM_PFLAG_KERNEL )
573 if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER;
574 else *ent |= PF_USER;
578 if( Mask & MM_PFLAG_COW )
580 if( Flags & MM_PFLAG_COW ) {
592 * \fn tPAddr MM_DuplicatePage(Uint VAddr)
593 * \brief Duplicates a virtual page to a physical one
595 tPAddr MM_DuplicatePage(Uint VAddr)
602 if( !(gaPageDir [VAddr >> 22] & PF_PRESENT) ) return 0;
603 if( !(gaPageTable[VAddr >> 12] & PF_PRESENT) ) return 0;
609 ret = MM_AllocPhys();
611 // Write-lock the page (to keep data constistent), saving its R/W state
612 wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
613 gaPageTable[VAddr >> 12] &= ~PF_WRITE;
617 temp = MM_MapTemp(ret);
618 memcpy( (void*)temp, (void*)VAddr, 0x1000 );
621 // Restore Writeable status
622 if(!wasRO) gaPageTable[VAddr >> 12] |= PF_WRITE;
629 * \fn Uint MM_MapTemp(tPAddr PAddr)
630 * \brief Create a temporary memory mapping
631 * \todo Show Luigi Barone (C Lecturer) and see what he thinks
633 Uint MM_MapTemp(tPAddr PAddr)
637 //ENTER("XPAddr", PAddr);
641 //LOG("gilTempMappings = %i", gilTempMappings);
645 LOCK( &gilTempMappings );
647 for( i = 0; i < NUM_TEMP_PAGES; i ++ )
649 // Check if page used
650 if(gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] & 1) continue;
652 gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
653 INVLPG( TEMP_MAP_ADDR + (i << 12) );
654 //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
655 RELEASE( &gilTempMappings );
656 return TEMP_MAP_ADDR + (i << 12);
658 RELEASE( &gilTempMappings );
664 * \fn void MM_FreeTemp(Uint PAddr)
665 * \brief Free's a temp mapping
667 void MM_FreeTemp(Uint VAddr)
670 //ENTER("xVAddr", VAddr);
672 if(i >= (TEMP_MAP_ADDR >> 12))
673 gaPageTable[ i ] = 0;
679 * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
680 * \brief Allocates a contigous number of pages
682 Uint MM_MapHWPage(tPAddr PAddr, Uint Number)
689 for( i = 0; i < NUM_HW_PAGES; i ++ )
691 // Check if addr used
692 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i ] & 1 )
695 // Check possible region
696 for( j = 0; j < Number && i + j < NUM_HW_PAGES; j ++ )
698 // If there is an allocated page in the region we are testing, break
699 if( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] & 1 ) break;
705 for( j = 0; j < Number; j++ ) {
706 MM_RefPhys( PAddr + (j<<12) );
707 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = (PAddr + (j<<12)) | 3;
709 return HW_MAP_ADDR + (i<<12);
712 // If we don't find any, return NULL
717 * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number)
718 * \brief Unmap a hardware page
720 void MM_UnmapHWPage(Uint VAddr, Uint Number)
724 if(VAddr < HW_MAP_ADDR || VAddr-Number*0x1000 > HW_MAP_MAX) return;
728 LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock
730 for( j = 0; j < Number; j++ )
732 MM_DerefPhys( gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] );
733 gaPageTable[ (HW_MAP_ADDR >> 12) + i + j ] = 0;
736 RELEASE( &gilTempMappings );
740 EXPORT(MM_GetPhysAddr);
743 EXPORT(MM_MapHWPage);
744 EXPORT(MM_UnmapHWPage);