X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86%2Fmm_virt.c;h=bd7b1dff66adb4474783db55a1d589fcf7d80857;hb=a2495c6ea4f4cab16b5d339ae511428e92e89e73;hp=3c893f8e57c4642da86d7f6c348e658f7caee000;hpb=3b0cee8ca739fc5c564c78467fdc65c4d83d45a1;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86/mm_virt.c b/Kernel/arch/x86/mm_virt.c index 3c893f8e..bd7b1dff 100644 --- a/Kernel/arch/x86/mm_virt.c +++ b/Kernel/arch/x86/mm_virt.c @@ -15,19 +15,13 @@ #include #include #include +#include +#include -#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 KERNEL_STACKS_END 0xFC000000 #define WORKER_STACKS 0x00100000 // Thread0 Only! -#define WORKER_STACK_SIZE KERNEL_STACK_SIZE +#define WORKER_STACK_SIZE MM_KERNEL_STACK_SIZE #define WORKER_STACKS_END 0xB0000000 #define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE) @@ -55,19 +49,25 @@ #define PF_PRESENT 0x1 #define PF_WRITE 0x2 #define PF_USER 0x4 +#define PF_GLOBAL 0x80 #define PF_COW 0x200 -#define PF_PAGED 0x400 +#define PF_NOPAGE 0x400 #define INVLPG(addr) __asm__ __volatile__ ("invlpg (%0)"::"r"(addr)) -#if USE_PAE -typedef Uint64 tTabEnt; -#else +#define GET_TEMP_MAPPING(cr3) do { \ + __ASM__("cli"); \ + __AtomicTestSetLoop( (Uint *)gpTmpCR3, cr3 | 3 ); \ +} while(0) +#define REL_TEMP_MAPPING() do { \ + *gpTmpCR3 = 0; \ + __ASM__("sti"); \ +} while(0) + typedef Uint32 tTabEnt; -#endif // === IMPORTS === -extern void _UsertextEnd, _UsertextBase; +extern char _UsertextEnd[], _UsertextBase[]; extern Uint32 gaInitPageDir[1024]; extern Uint32 gaInitPageTable[1024]; extern void Threads_SegFault(tVAddr Addr); @@ -77,7 +77,8 @@ extern void Error_Backtrace(Uint eip, Uint ebp); void MM_PreinitVirtual(void); void MM_InstallVirtual(void); void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs); -void MM_DumpTables(tVAddr Start, tVAddr End); +//void MM_DumpTables(tVAddr Start, tVAddr End); +//void MM_ClearUser(void); tPAddr MM_DuplicatePage(tVAddr VAddr); // === GLOBALS === @@ -99,6 +100,13 @@ tMutex glTempMappings; 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 === /** @@ -107,11 +115,7 @@ Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32]; */ 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 ); } @@ -123,29 +127,12 @@ void MM_InstallVirtual(void) { 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 ++ ) { if( gaPageDir[ i ] ) continue; // Skip stack tables, they are process unique - if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) { + if( i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) { gaPageDir[ i ] = 0; continue; } @@ -154,12 +141,13 @@ void MM_InstallVirtual(void) 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--; ) { MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL ); } + + *gpTmpCR3 = 0; } /** @@ -167,11 +155,7 @@ void MM_InstallVirtual(void) */ void MM_FinishVirtualInit(void) { - #if USE_PAE - gaInitPDPT[ 0 ] = 0; - #else gaInitPageDir[ 0 ] = 0; - #endif } /** @@ -183,8 +167,7 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs) //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; @@ -202,21 +185,28 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs) gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE; } +// Log_Debug("MMVirt", "COW for %p (%P)", Addr, gaPageTable[Addr>>12]); + INVLPG( Addr & ~0xFFF ); - //LEAVE('-') return; } - + + // Disable instruction tracing + __ASM__("pushf; andw $0xFEFF, 0(%esp); popf"); + Proc_GetCurThread()->bInstrTrace = 0; + // If it was a user, tell the thread handler if(ErrorCode & 4) { - Warning("%s %s %s memory%s", - (ErrorCode&4?"User":"Kernel"), + Log_Warning("MMVirt", "User %s %s memory%s", (ErrorCode&2?"write to":"read from"), (ErrorCode&1?"bad/locked":"non-present"), (ErrorCode&16?" (Instruction Fetch)":"") ); - Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr); - __asm__ __volatile__ ("sti"); // Restart IRQs + Log_Warning("MMVirt", "Instruction %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr); + __ASM__("sti"); // Restart IRQs + #if 1 + Error_Backtrace(Regs->eip, Regs->ebp); + #endif Threads_SegFault(Addr); return ; } @@ -228,22 +218,22 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs) Warning("Reserved Bits Trashed!"); else { - Warning("%s %s %s memory%s", - (ErrorCode&4?"User":"Kernel"), + Warning("Kernel %s %s memory%s", (ErrorCode&2?"write to":"read from"), (ErrorCode&1?"bad/locked":"non-present"), (ErrorCode&16?" (Instruction Fetch)":"") ); } - Log("Code at %p accessed %p", Regs->eip, Addr); + Log("CPU %i - Code at %p accessed %p", GetCPUNum(), Regs->eip, Addr); // Print Stack Backtrace Error_Backtrace(Regs->eip, Regs->ebp); - + + #if 0 Log("gaPageDir[0x%x] = 0x%x", Addr>>22, gaPageDir[Addr>>22]); if( gaPageDir[Addr>>22] & PF_PRESENT ) Log("gaPageTable[0x%x] = 0x%x", Addr>>12, gaPageTable[Addr>>12]); - + #endif //MM_DumpTables(0, -1); // Register Dump @@ -270,9 +260,10 @@ void MM_DumpTables(tVAddr Start, tVAddr End) { tVAddr rangeStart = 0; tPAddr expected = 0; + void *expected_node = NULL, *tmpnode = NULL; tVAddr curPos; Uint page; - const tPAddr MASK = ~0xF98; + const tPAddr MASK = ~0xF78; Start >>= 12; End >>= 12; @@ -299,17 +290,21 @@ void MM_DumpTables(tVAddr Start, tVAddr End) { if( !(gaPageDir[curPos>>22] & PF_PRESENT) || !(gaPageTable[page] & PF_PRESENT) - || (gaPageTable[page] & MASK) != expected) + || (gaPageTable[page] & MASK) != expected + || (tmpnode=NULL,MM_GetPageNode(expected, &tmpnode), tmpnode != expected_node)) { 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) %p", + 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_node ); expected = 0; } @@ -317,20 +312,24 @@ void MM_DumpTables(tVAddr Start, tVAddr End) if( !(gaPageTable[curPos>>12] & PF_PRESENT) ) continue; expected = (gaPageTable[page] & MASK); + MM_GetPageNode(expected, &expected_node); rangeStart = curPos; } if(expected) expected += 0x1000; } 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) %p", + 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_node ); expected = 0; } @@ -343,25 +342,22 @@ tPAddr MM_Allocate(tVAddr VAddr) { tPAddr paddr; //ENTER("xVAddr", VAddr); - //__asm__ __volatile__ ("xchg %bx,%bx"); + //__ASM__("xchg %bx,%bx"); // Check if the directory is mapped if( gaPageDir[ VAddr >> 22 ] == 0 ) { // 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 @@ -431,7 +427,7 @@ tPAddr MM_GetPhysAddr(tVAddr Addr) */ void MM_SetCR3(Uint CR3) { - __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3)); + __ASM__("mov %0, %%cr3"::"r"(CR3)); } /** @@ -443,7 +439,7 @@ int MM_Map(tVAddr VAddr, tPAddr PAddr) //ENTER("xVAddr xPAddr", VAddr, PAddr); // Sanity check if( PAddr & 0xFFF || VAddr & 0xFFF ) { - Warning("MM_Map - Physical or Virtual Addresses are not aligned"); + Log_Warning("MM_Virt", "MM_Map - Physical or Virtual Addresses are not aligned"); //LEAVE('i', 0); return 0; } @@ -454,7 +450,10 @@ int MM_Map(tVAddr VAddr, tPAddr PAddr) // 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; @@ -488,14 +487,12 @@ int MM_Map(tVAddr VAddr, tPAddr PAddr) } /** - * \fn tVAddr MM_ClearUser() * \brief Clear user's address space */ -tVAddr MM_ClearUser(void) +void MM_ClearUser(void) { Uint i, j; - // Copy Directories for( i = 0; i < (MM_USER_MAX>>22); i ++ ) { // Check if directory is not allocated @@ -504,7 +501,7 @@ tVAddr MM_ClearUser(void) continue; } - + // Deallocate tables for( j = 0; j < 1024; j ++ ) { if( gaPageTable[i*1024+j] & 1 ) @@ -512,66 +509,124 @@ tVAddr MM_ClearUser(void) gaPageTable[i*1024+j] = 0; } + // Deallocate directory MM_DerefPhys( gaPageDir[i] & ~0xFFF ); gaPageDir[i] = 0; INVLPG( &gaPageTable[i*1024] ); } INVLPG( gaPageDir ); +} + +/** + * \brief Deallocate an address space + */ +void MM_ClearSpace(Uint32 CR3) +{ + int i, j; - return *gpPageCR3; + if(CR3 == (*gpPageCR3 & ~0xFFF)) { + Log_Error("MMVirt", "Can't clear current address space"); + return ; + } + + if( MM_GetRefCount(CR3) > 1 ) { + MM_DerefPhys(CR3); + Log_Log("MMVirt", "CR3 %P is still referenced, not cleaning (but dereferenced)", CR3); + return ; + } + + Log_Debug("MMVirt", "Clearing out address space 0x%x from 0x%x", CR3, *gpPageCR3); + + GET_TEMP_MAPPING(CR3); + INVLPG( gaTmpDir ); + + for( i = 0; i < 1024; i ++ ) + { + Uint32 *table = &gaTmpTable[i*1024]; + if( !(gaTmpDir[i] & PF_PRESENT) ) + continue ; + + INVLPG( table ); + + if( i < 768 || (i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) ) + { + for( j = 0; j < 1024; j ++ ) + { + if( !(table[j] & 1) ) + continue; + MM_DerefPhys( table[j] & ~0xFFF ); + } + } + + if( i != (PAGE_TABLE_ADDR >> 22) ) + { + MM_DerefPhys( gaTmpDir[i] & ~0xFFF ); + } + } + + + MM_DerefPhys( CR3 ); + + REL_TEMP_MAPPING(); } /** * \fn tPAddr MM_Clone(void) * \brief Clone the current address space */ -tPAddr MM_Clone(void) +tPAddr MM_Clone(int bNoUserCopy) { Uint i, j; - tVAddr ret; + tPAddr ret; Uint page = 0; - tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE; + tVAddr kStackBase = Proc_GetCurThread()->KernelStack - MM_KERNEL_STACK_SIZE; void *tmp; - Mutex_Acquire( &glTempFractal ); - // Create Directory Table - *gpTmpCR3 = MM_AllocPhys() | 3; + ret = MM_AllocPhys(); + if( ret == 0 ) { + return 0; + } + + // Map + GET_TEMP_MAPPING( ret ); INVLPG( gaTmpDir ); - //LOG("Allocated Directory (%x)", *gpTmpCR3); memsetd( gaTmpDir, 0, 1024 ); - // Copy Tables - for( i = 0; i < 768; i ++) - { - // Check if table is allocated - if( !(gaPageDir[i] & PF_PRESENT) ) { - gaTmpDir[i] = 0; - page += 1024; - continue; - } - - // Allocate new table - gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7); - INVLPG( &gaTmpTable[page] ); - // Fill - for( j = 0; j < 1024; j ++, page++ ) + if( Threads_GetPID() != 0 && !bNoUserCopy ) + { + // Copy Tables + for( i = 0; i < 768; i ++) { - if( !(gaPageTable[page] & PF_PRESENT) ) { - gaTmpTable[page] = 0; + // Check if table is allocated + if( !(gaPageDir[i] & PF_PRESENT) ) { + gaTmpDir[i] = 0; + page += 1024; continue; } - // Refrence old page - MM_RefPhys( gaPageTable[page] & ~0xFFF ); - // Add to new table - if(gaPageTable[page] & PF_WRITE) { - gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW; - gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW; - INVLPG( page << 12 ); + // Allocate new table + gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7); + INVLPG( &gaTmpTable[page] ); + // Fill + for( j = 0; j < 1024; j ++, page++ ) + { + if( !(gaPageTable[page] & PF_PRESENT) ) { + gaTmpTable[page] = 0; + continue; + } + + // Refrence old page + MM_RefPhys( gaPageTable[page] & ~0xFFF ); + // Add to new table + if(gaPageTable[page] & PF_WRITE) { + gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW; + gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW; + INVLPG( page << 12 ); + } + else + gaTmpTable[page] = gaPageTable[page]; } - else - gaTmpTable[page] = gaPageTable[page]; } } @@ -583,6 +638,10 @@ tPAddr MM_Clone(void) gaTmpDir[ PAGE_TABLE_ADDR >> 22 ] = *gpTmpCR3; continue; } + if( i == (TMP_TABLE_ADDR >> 22) ) { + gaTmpDir[ TMP_TABLE_ADDR >> 22 ] = 0; + continue ; + } if( gaPageDir[i] == 0 ) { gaTmpDir[i] = 0; @@ -595,9 +654,7 @@ tPAddr MM_Clone(void) } // Allocate kernel stack - for(i = KERNEL_STACKS >> 22; - i < KERNEL_STACKS_END >> 22; - i ++ ) + for(i = MM_KERNEL_STACKS >> 22; i < MM_KERNEL_STACKS_END >> 22; i ++ ) { // Check if directory is allocated if( (gaPageDir[i] & 1) == 0 ) { @@ -624,7 +681,7 @@ tPAddr MM_Clone(void) } // We don't care about other kernel stacks - if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) { + if( ((i*1024+j)*4096 & ~(MM_KERNEL_STACK_SIZE-1)) != kStackBase ) { gaTmpTable[i*1024+j] = 0; continue; } @@ -640,8 +697,7 @@ tPAddr MM_Clone(void) } } - ret = *gpTmpCR3 & ~0xFFF; - Mutex_Release( &glTempFractal ); + REL_TEMP_MAPPING(); //LEAVE('x', ret); return ret; @@ -655,16 +711,30 @@ tVAddr MM_NewKStack(void) { tVAddr base; Uint i; - for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE) + for(base = MM_KERNEL_STACKS; base < MM_KERNEL_STACKS_END; base += MM_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 = MM_KERNEL_STACK_SIZE; i -= 0x1000 ; ) + for(i = 0; i < MM_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 < MM_KERNEL_STACK_SIZE; i += 0x1000 ) + // MM_Deallocate(base+i); + return 0; + } } - Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE); - return base+KERNEL_STACK_SIZE; + // Success +// Log("MM_NewKStack - Allocated %p", base + MM_KERNEL_STACK_SIZE); + return base+MM_KERNEL_STACK_SIZE; } - Warning("MM_NewKStack - No address space left\n"); + // No stacks left + Log_Warning("MMVirt", "MM_NewKStack - No address space left"); return 0; } @@ -672,19 +742,13 @@ tVAddr MM_NewKStack(void) * \fn tVAddr MM_NewWorkerStack() * \brief Creates a new worker stack */ -tVAddr MM_NewWorkerStack() +tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize) { - Uint esp, ebp; - Uint oldstack; Uint base, addr; - int i, j; - Uint *tmpPage; - tPAddr pages[WORKER_STACK_SIZE>>12]; - - // Get the old ESP and EBP - __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp)); - __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp)); + tVAddr tmpPage; + tPAddr page; + // TODO: Thread safety // Find a free worker stack address for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++) { @@ -713,15 +777,10 @@ tVAddr MM_NewWorkerStack() base = WORKER_STACKS + base * WORKER_STACK_SIZE; //Log(" MM_NewWorkerStack: base = 0x%x", base); - // Acquire the lock for the temp fractal mappings - Mutex_Acquire(&glTempFractal); - // Set the temp fractals to TID0's address space - *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3; - //Log(" MM_NewWorkerStack: *gpTmpCR3 = 0x%x", *gpTmpCR3); + GET_TEMP_MAPPING( ((Uint)gaInitPageDir - KERNEL_BASE) ); INVLPG( gaTmpDir ); - // Check if the directory is mapped (we are assuming that the stacks // will fit neatly in a directory) //Log(" MM_NewWorkerStack: gaTmpDir[ 0x%x ] = 0x%x", base>>22, gaTmpDir[ base >> 22 ]); @@ -733,42 +792,21 @@ tVAddr MM_NewWorkerStack() // Mapping Time! for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 ) { - pages[ addr >> 12 ] = MM_AllocPhys(); - gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3; - } - *gpTmpCR3 = 0; - // Release the temp mapping lock - Mutex_Release(&glTempFractal); - - // Copy the old stack - oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1); - esp = oldstack - esp; // ESP as an offset in the stack - - // Make `base` be the top of the stack - base += WORKER_STACK_SIZE; - - i = (WORKER_STACK_SIZE>>12) - 1; - // Copy the contents of the old stack to the new one, altering the addresses - // `addr` is refering to bytes from the stack base (mem downwards) - for(addr = 0; addr < esp; addr += 0x1000) - { - Uint *stack = (Uint*)( oldstack-(addr+0x1000) ); - tmpPage = (void*)MM_MapTemp( pages[i] ); - // Copy old stack - for(j = 0; j < 1024; j++) - { - // Possible Stack address? - if(oldstack-esp < stack[j] && stack[j] < oldstack) - tmpPage[j] = base - (oldstack - stack[j]); - else // Seems not, best leave it alone - tmpPage[j] = stack[j]; - } - MM_FreeTemp((tVAddr)tmpPage); - i --; + page = MM_AllocPhys(); + gaTmpTable[ (base + addr) >> 12 ] = page | 3; } + + // Release temporary fractal + REL_TEMP_MAPPING(); + + // NOTE: Max of 1 page + // `page` is the last allocated page from the previious for loop + tmpPage = MM_MapTemp( page ); + memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize); + MM_FreeTemp(tmpPage); //Log("MM_NewWorkerStack: RETURN 0x%x", base); - return base; + return base + WORKER_STACK_SIZE; } /** @@ -848,6 +886,45 @@ Uint MM_GetFlags(tVAddr VAddr) return ret; } +/** + * \brief Check if the provided buffer is valid + * \return Boolean valid + */ +int MM_IsValidBuffer(tVAddr Addr, size_t Size) +{ + int bIsUser; + int dir, tab; + + Size += Addr & (PAGE_SIZE-1); + Addr &= ~(PAGE_SIZE-1); + + dir = Addr >> 22; + tab = Addr >> 12; + +// Debug("Addr = %p, Size = 0x%x, dir = %i, tab = %i", Addr, Size, dir, tab); + + if( !(gaPageDir[dir] & 1) ) return 0; + if( !(gaPageTable[tab] & 1) ) return 0; + + bIsUser = !!(gaPageTable[tab] & PF_USER); + + while( Size >= PAGE_SIZE ) + { + if( (tab & 1023) == 0 ) + { + dir ++; + if( !(gaPageDir[dir] & 1) ) return 0; + } + + if( !(gaPageTable[tab] & 1) ) return 0; + if( bIsUser && !(gaPageTable[tab] & PF_USER) ) return 0; + + tab ++; + Size -= PAGE_SIZE; + } + return 1; +} + /** * \fn tPAddr MM_DuplicatePage(tVAddr VAddr) * \brief Duplicates a virtual page to a physical one @@ -869,6 +946,9 @@ tPAddr MM_DuplicatePage(tVAddr VAddr) // 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); @@ -1005,6 +1085,10 @@ tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr) 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) { @@ -1065,10 +1149,3 @@ void MM_UnmapHWPages(tVAddr VAddr, Uint Number) Mutex_Release( &glTempMappings ); } -// --- EXPORTS --- -EXPORT(MM_GetPhysAddr); -EXPORT(MM_Map); -//EXPORT(MM_Unmap); -EXPORT(MM_MapHWPages); -EXPORT(MM_AllocDMA); -EXPORT(MM_UnmapHWPages);