#include <mm_phys.h>
#include <proc.h>
-#if USE_PAE
-# define TAB 21
-# define DIR 30
-#else
-# define TAB 22
-#endif
+#define TAB 22
#define KERNEL_STACKS 0xF0000000
#define KERNEL_STACK_SIZE 0x00008000
#define PF_PRESENT 0x1
#define PF_WRITE 0x2
#define PF_USER 0x4
+#define PF_GLOBAL 0x80
#define PF_COW 0x200
#define PF_NOPAGE 0x400
#define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
-#if USE_PAE
-typedef Uint64 tTabEnt;
-#else
typedef Uint32 tTabEnt;
-#endif
// === IMPORTS ===
extern void _UsertextEnd, _UsertextBase;
void MM_InstallVirtual(void);
void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
void MM_DumpTables(tVAddr Start, tVAddr End);
+tVAddr MM_ClearUser(void);
tPAddr MM_DuplicatePage(tVAddr VAddr);
// === GLOBALS ===
tMutex glTempFractal;
Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
int giLastUsedWorker = 0;
+struct sPageInfo {
+ void *Node;
+ tVAddr Base;
+ Uint64 Offset;
+ int Length;
+ int Flags;
+} *gaMappedRegions; // sizeof = 24 bytes
// === CODE ===
/**
*/
void MM_PreinitVirtual(void)
{
- #if USE_PAE
- gaInitPageDir[ ((PAGE_TABLE_ADDR >> TAB)-3*512+3)*2 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
- #else
gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
- #endif
INVLPG( PAGE_TABLE_ADDR );
}
{
int i;
- #if USE_PAE
- // --- Pre-Allocate kernel tables
- for( i = KERNEL_BASE >> TAB; i < 1024*4; i ++ )
- {
- if( gaPAE_PageDir[ i ] ) continue;
-
- // Skip stack tables, they are process unique
- if( i > KERNEL_STACKS >> TAB && i < KERNEL_STACKS_END >> TAB) {
- gaPAE_PageDir[ i ] = 0;
- continue;
- }
- // Preallocate table
- gaPAE_PageDir[ i ] = MM_AllocPhys() | 3;
- INVLPG( &gaPAE_PageTable[i*512] );
- memset( &gaPAE_PageTable[i*512], 0, 0x1000 );
- }
- #else
// --- Pre-Allocate kernel tables
for( i = KERNEL_BASE>>22; i < 1024; i ++ )
{
INVLPG( &gaPageTable[i*1024] );
memset( &gaPageTable[i*1024], 0, 0x1000 );
}
- #endif
// Unset kernel on the User Text pages
for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
*/
void MM_FinishVirtualInit(void)
{
- #if USE_PAE
- gaInitPDPT[ 0 ] = 0;
- #else
gaInitPageDir[ 0 ] = 0;
- #endif
}
/**
//ENTER("xAddr bErrorCode", Addr, ErrorCode);
// -- Check for COW --
- if( gaPageDir [Addr>>22] & PF_PRESENT
- && gaPageTable[Addr>>12] & PF_PRESENT
+ if( gaPageDir [Addr>>22] & PF_PRESENT && gaPageTable[Addr>>12] & PF_PRESENT
&& gaPageTable[Addr>>12] & PF_COW )
{
tPAddr paddr;
gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
}
+ Log_Debug("MMVirt", "COW for %p", Addr);
+
INVLPG( Addr & ~0xFFF );
- //LEAVE('-')
return;
}
// If it was a user, tell the thread handler
if(ErrorCode & 4) {
- Warning("%s %s %s memory%s",
+ Log_Warning("MMVirt", "%s %s %s memory%s",
(ErrorCode&4?"User":"Kernel"),
(ErrorCode&2?"write to":"read from"),
(ErrorCode&1?"bad/locked":"non-present"),
);
Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
__asm__ __volatile__ ("sti"); // Restart IRQs
+ #if 1
+ Error_Backtrace(Regs->eip, Regs->ebp);
+ #endif
Threads_SegFault(Addr);
return ;
}
tPAddr expected = 0;
tVAddr curPos;
Uint page;
- const tPAddr MASK = ~0xF98;
+ const tPAddr MASK = ~0xF78;
Start >>= 12; End >>= 12;
|| (gaPageTable[page] & MASK) != expected)
{
if(expected) {
- Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
- rangeStart, curPos - 1,
- gaPageTable[rangeStart>>12] & ~0xFFF,
- (expected & ~0xFFF) - 1,
- (expected & PF_PAGED ? "p" : "-"),
- (expected & PF_COW ? "C" : "-"),
- (expected & PF_USER ? "U" : "-"),
- (expected & PF_WRITE ? "W" : "-")
+ tPAddr orig = gaPageTable[rangeStart>>12];
+ Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s)",
+ rangeStart,
+ orig & ~0xFFF,
+ curPos - rangeStart,
+ (orig & PF_NOPAGE ? "P" : "-"),
+ (orig & PF_COW ? "C" : "-"),
+ (orig & PF_GLOBAL ? "G" : "-"),
+ (orig & PF_USER ? "U" : "-"),
+ (orig & PF_WRITE ? "W" : "-")
);
expected = 0;
}
}
if(expected) {
- Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
- rangeStart, curPos - 1,
+ Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s)",
+ rangeStart,
gaPageTable[rangeStart>>12] & ~0xFFF,
- (expected & ~0xFFF) - 1,
- (expected & PF_PAGED ? "p" : "-"),
+ curPos - rangeStart,
+ (expected & PF_NOPAGE ? "p" : "-"),
(expected & PF_COW ? "C" : "-"),
(expected & PF_USER ? "U" : "-"),
(expected & PF_WRITE ? "W" : "-")
{
// Allocate directory
paddr = MM_AllocPhys();
- //LOG("paddr = 0x%llx (new table)", paddr);
if( paddr == 0 ) {
Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
//LEAVE('i',0);
return 0;
}
- // Map
+ // Map and mark as user (if needed)
gaPageDir[ VAddr >> 22 ] = paddr | 3;
- // Mark as user
if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
INVLPG( &gaPageDir[ VAddr >> 22 ] );
- //LOG("Clearing new table");
memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
}
// Check if the page is already allocated
// Check if the directory is mapped
if( gaPageDir[ VAddr >> 22 ] == 0 )
{
- gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
+ tPAddr tmp = MM_AllocPhys();
+ if( tmp == 0 )
+ return 0;
+ gaPageDir[ VAddr >> 22 ] = tmp | 3;
// Mark as user
if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
{
Uint i, j;
- // Copy Directories
for( i = 0; i < (MM_USER_MAX>>22); i ++ )
{
// Check if directory is not allocated
continue;
}
-
+ // Deallocate tables
for( j = 0; j < 1024; j ++ )
{
if( gaPageTable[i*1024+j] & 1 )
gaPageTable[i*1024+j] = 0;
}
+ // Deallocate directory
MM_DerefPhys( gaPageDir[i] & ~0xFFF );
gaPageDir[i] = 0;
INVLPG( &gaPageTable[i*1024] );
// Create Directory Table
*gpTmpCR3 = MM_AllocPhys() | 3;
+ if( *gpTmpCR3 == 3 ) {
+ *gpTmpCR3 = 0;
+ return 0;
+ }
INVLPG( gaTmpDir );
//LOG("Allocated Directory (%x)", *gpTmpCR3);
memsetd( gaTmpDir, 0, 1024 );
Uint i;
for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
{
+ // Check if space is free
if(MM_GetPhysAddr(base) != 0) continue;
- for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000) {
- MM_Allocate(base+i);
+ // Allocate
+ //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
+ for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
+ {
+ if( MM_Allocate(base+i) == 0 )
+ {
+ // On error, print a warning and return error
+ Warning("MM_NewKStack - Out of memory");
+ // - Clean up
+ //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
+ // MM_Deallocate(base+i);
+ return 0;
+ }
}
+ // Success
Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
return base+KERNEL_STACK_SIZE;
}
- Warning("MM_NewKStack - No address space left\n");
+ // No stacks left
+ Warning("MM_NewKStack - No address space left");
return 0;
}
// Allocate new page
ret = MM_AllocPhys();
+ if( !ret ) {
+ return 0;
+ }
// Write-lock the page (to keep data constistent), saving its R/W state
wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
if(Pages == 1 && MaxBits >= PHYS_BITS)
{
phys = MM_AllocPhys();
+ if( !phys ) {
+ *PhysAddr = 0;
+ LEAVE_RET('i', 0);
+ }
*PhysAddr = phys;
ret = MM_MapHWPages(phys, 1);
if(ret == 0) {
Mutex_Release( &glTempMappings );
}
-// --- EXPORTS ---
-EXPORT(MM_GetPhysAddr);
-EXPORT(MM_Map);
-//EXPORT(MM_Unmap);
-EXPORT(MM_MapHWPages);
-EXPORT(MM_AllocDMA);
-EXPORT(MM_UnmapHWPages);