X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86%2Fmm_virt.c;h=44e186517b726d42f5f84dcbc011c6dd0ffc4b61;hb=47e9dfd89189fc6b150bd6b20229cb047c7e0858;hp=b4fd2225a3d90616c3c7d4883efb8f137ff4a0a7;hpb=cf00ee7fb53960fabd8b3c1072a54605118adf9c;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86/mm_virt.c b/Kernel/arch/x86/mm_virt.c index b4fd2225..44e18651 100644 --- a/Kernel/arch/x86/mm_virt.c +++ b/Kernel/arch/x86/mm_virt.c @@ -9,13 +9,18 @@ * 0xFE - Unused * 0xFF - System Calls / Kernel's User Code */ +#define DEBUG 1 #include #include #include -#define KERNEL_STACKS 0xF0000000 +#define KERNEL_STACKS 0xF0000000 #define KERNEL_STACK_SIZE 0x00002000 -#define KERNEL_STACK_END 0xFD000000 +#define KERNEL_STACKS_END 0xFD000000 +#define WORKER_STACKS 0x00100000 // Thread0 Only! +#define WORKER_STACK_SIZE KERNEL_STACK_SIZE +#define WORKER_STACKS_END 0xB0000000 +#define NUM_WORKER_STACKS ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE) #define PAGE_TABLE_ADDR 0xFD000000 #define PAGE_DIR_ADDR 0xFD3F4000 #define PAGE_CR3_ADDR 0xFD3F4FD0 @@ -28,8 +33,6 @@ #define TEMP_MAP_ADDR 0xFEFF0000 // Allows 16 "temp" pages #define NUM_TEMP_PAGES 16 -#define USE_COW 1 - #define PF_PRESENT 0x1 #define PF_WRITE 0x2 #define PF_USER 0x4 @@ -41,13 +44,15 @@ // === IMPORTS === extern Uint32 gaInitPageDir[1024]; extern Uint32 gaInitPageTable[1024]; +extern void Threads_SegFault(tVAddr Addr); +extern void Error_Backtrace(Uint eip, Uint ebp); // === PROTOTYPES === void MM_PreinitVirtual(); void MM_InstallVirtual(); -void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs); +void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs); void MM_DumpTables(tVAddr Start, tVAddr End); -tPAddr MM_DuplicatePage(Uint VAddr); +tPAddr MM_DuplicatePage(tVAddr VAddr); // === GLOBALS === tPAddr *gaPageTable = (void*)PAGE_TABLE_ADDR; @@ -57,6 +62,9 @@ tPAddr *gaTmpTable = (void*)TMP_TABLE_ADDR; tPAddr *gaTmpDir = (void*)TMP_DIR_ADDR; tPAddr *gTmpCR3 = (void*)TMP_CR3_ADDR; int gilTempMappings = 0; + int gilTempFractal = 0; +Uint32 gWorkerStacks[NUM_WORKER_STACKS/32]; + int giLastUsedWorker = 0; // === CODE === /** @@ -82,7 +90,7 @@ void MM_InstallVirtual() { if( gaPageDir[ i ] ) continue; // Skip stack tables, they are process unique - if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACK_END >> 22) { + if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) { gaPageDir[ i ] = 0; continue; } @@ -94,12 +102,12 @@ void MM_InstallVirtual() } /** - * \fn void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs) + * \fn void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs) * \brief Called on a page fault */ -void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs) +void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs) { - ENTER("xAddr bErrorCode", Addr, ErrorCode); + //ENTER("xAddr bErrorCode", Addr, ErrorCode); // -- Check for COW -- if( gaPageDir [Addr>>22] & PF_PRESENT @@ -107,15 +115,38 @@ void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs) && gaPageTable[Addr>>12] & PF_COW ) { tPAddr paddr; - paddr = MM_DuplicatePage( Addr ); - MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF ); - gaPageTable[Addr>>12] &= PF_USER; - gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE; + if(MM_GetRefCount( gaPageTable[Addr>>12] & ~0xFFF ) == 1) + { + gaPageTable[Addr>>12] &= ~PF_COW; + gaPageTable[Addr>>12] |= PF_PRESENT|PF_WRITE; + } + else + { + paddr = MM_DuplicatePage( Addr ); + MM_DerefPhys( gaPageTable[Addr>>12] & ~0xFFF ); + gaPageTable[Addr>>12] &= PF_USER; + gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE; + } + INVLPG( Addr & ~0xFFF ); - LEAVE('-'); + //LEAVE('-') return; } + // If it was a user, tell the thread handler + if(ErrorCode & 4) { + Warning("%s %s %s memory%s", + (ErrorCode&4?"User":"Kernel"), + (ErrorCode&2?"write to":"read from"), + (ErrorCode&1?"bad/locked":"non-present"), + (ErrorCode&16?" (Instruction Fetch)":"") + ); + Warning("User Pagefault: Instruction at %p accessed %p", Regs->eip, Addr); + __asm__ __volatile__ ("sti"); // Restart IRQs + Threads_SegFault(Addr); + return ; + } + // -- Check Error Code -- if(ErrorCode & 8) Warning("Reserved Bits Trashed!"); @@ -129,6 +160,10 @@ void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs) ); } + Log("Code at %p accessed %p", Regs->eip, Addr); + // Print Stack Backtrace + Error_Backtrace(Regs->eip, Regs->ebp); + 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]); @@ -136,11 +171,10 @@ void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs) MM_DumpTables(0, -1); Panic("Page Fault at 0x%x\n", Regs->eip); - LEAVE('-'); } /** - * \fn void MM_DumpTables(Uint Start, Uint End) + * \fn void MM_DumpTables(tVAddr Start, tVAddr End) * \brief Dumps the layout of the page tables */ void MM_DumpTables(tVAddr Start, tVAddr End) @@ -152,6 +186,24 @@ void MM_DumpTables(tVAddr Start, tVAddr End) const tPAddr MASK = ~0xF98; Start >>= 12; End >>= 12; + + #if 0 + Log("Directory Entries:"); + for(page = Start >> 10; + page < (End >> 10)+1; + page ++) + { + if(gaPageDir[page]) + { + Log(" 0x%08x-0x%08x :: 0x%08x", + page<<22, ((page+1)<<22)-1, + gaPageDir[page]&~0xFFF + ); + } + } + #endif + + Log("Table Entries:"); for(page = Start, curPos = Start<<12; page < End; curPos += 0x1000, page++) @@ -161,7 +213,7 @@ void MM_DumpTables(tVAddr Start, tVAddr End) || (gaPageTable[page] & MASK) != expected) { if(expected) { - Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)", + Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)", rangeStart, curPos - 1, gaPageTable[rangeStart>>12] & ~0xFFF, (expected & ~0xFFF) - 1, @@ -196,15 +248,22 @@ void MM_DumpTables(tVAddr Start, tVAddr End) } /** - * \fn tPAddr MM_Allocate(Uint VAddr) + * \fn tPAddr MM_Allocate(tVAddr VAddr) */ -tPAddr MM_Allocate(Uint VAddr) +tPAddr MM_Allocate(tVAddr VAddr) { + tPAddr paddr; // Check if the directory is mapped if( gaPageDir[ VAddr >> 22 ] == 0 ) { // Allocate directory - gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3; + paddr = MM_AllocPhys(); + if( paddr == 0 ) { + Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0)); + return 0; + } + // Map + gaPageDir[ VAddr >> 22 ] = paddr | 3; // Mark as user if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER; @@ -213,24 +272,31 @@ tPAddr MM_Allocate(Uint VAddr) } // Check if the page is already allocated else if( gaPageTable[ VAddr >> 12 ] != 0 ) { - Warning("MM_Allocate - Allocating to used address"); + Warning("MM_Allocate - Allocating to used address (%p)", VAddr); return gaPageTable[ VAddr >> 12 ] & ~0xFFF; } // Allocate - gaPageTable[ VAddr >> 12 ] = MM_AllocPhys() | 3; + paddr = MM_AllocPhys(); + if( paddr == 0 ) { + Warning("MM_Allocate - Out of Memory when allocating at %p (Called by %p)", + VAddr, __builtin_return_address(0)); + return 0; + } + // Map + gaPageTable[ VAddr >> 12 ] = paddr | 3; // Mark as user if(VAddr < MM_USER_MAX) gaPageTable[ VAddr >> 12 ] |= PF_USER; // Invalidate Cache for address INVLPG( VAddr & ~0xFFF ); - return gaPageTable[ VAddr >> 12 ] & ~0xFFF; + return paddr; } /** - * \fn void MM_Deallocate(Uint VAddr) + * \fn void MM_Deallocate(tVAddr VAddr) */ -void MM_Deallocate(Uint VAddr) +void MM_Deallocate(tVAddr VAddr) { if( gaPageDir[ VAddr >> 22 ] == 0 ) { Warning("MM_Deallocate - Directory not mapped"); @@ -249,10 +315,10 @@ void MM_Deallocate(Uint VAddr) } /** - * \fn tPAddr MM_GetPhysAddr(Uint Addr) + * \fn tPAddr MM_GetPhysAddr(tVAddr Addr) * \brief Checks if the passed address is accesable */ -tPAddr MM_GetPhysAddr(Uint Addr) +tPAddr MM_GetPhysAddr(tVAddr Addr) { if( !(gaPageDir[Addr >> 22] & 1) ) return 0; @@ -261,20 +327,36 @@ tPAddr MM_GetPhysAddr(Uint Addr) return (gaPageTable[Addr >> 12] & ~0xFFF) | (Addr & 0xFFF); } + +/** + * \fn int MM_IsUser(tVAddr Addr) + * \brief Checks if a page is user accessable + */ +int MM_IsUser(tVAddr Addr) +{ + if( !(gaPageDir[Addr >> 22] & 1) ) + return 0; + if( !(gaPageTable[Addr >> 12] & 1) ) + return 0; + if( !(gaPageTable[Addr >> 12] & PF_USER) ) + return 0; + return 1; +} + /** - * \fn void MM_SetCR3(Uint CR3) + * \fn void MM_SetCR3(tPAddr CR3) * \brief Sets the current process space */ -void MM_SetCR3(Uint CR3) +void MM_SetCR3(tPAddr CR3) { __asm__ __volatile__ ("mov %0, %%cr3"::"r"(CR3)); } /** - * \fn int MM_Map(Uint VAddr, tPAddr PAddr) + * \fn int MM_Map(tVAddr VAddr, tPAddr PAddr) * \brief Map a physical page to a virtual one */ -int MM_Map(Uint VAddr, tPAddr PAddr) +int MM_Map(tVAddr VAddr, tPAddr PAddr) { //ENTER("xVAddr xPAddr", VAddr, PAddr); // Sanity check @@ -324,10 +406,10 @@ int MM_Map(Uint VAddr, tPAddr PAddr) } /** - * \fn Uint MM_ClearUser() + * \fn tVAddr MM_ClearUser() * \brief Clear user's address space */ -Uint MM_ClearUser() +tVAddr MM_ClearUser() { Uint i, j; @@ -349,23 +431,27 @@ Uint MM_ClearUser() } MM_DerefPhys( gaPageDir[i] & ~0xFFF ); + gaPageDir[i] = 0; + INVLPG( &gaPageTable[i*1024] ); } + INVLPG( gaPageDir ); - - return *gTmpCR3; + return *gaPageCR3; } /** - * \fn Uint MM_Clone() + * \fn tPAddr MM_Clone() * \brief Clone the current address space */ -Uint MM_Clone() +tPAddr MM_Clone() { Uint i, j; - Uint kStackBase = gCurrentThread->KernelStack - KERNEL_STACK_SIZE; + tVAddr ret; + Uint page = 0; + tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE; void *tmp; - //ENTER(""); + LOCK( &gilTempFractal ); // Create Directory Table *gTmpCR3 = MM_AllocPhys() | 3; @@ -379,34 +465,31 @@ Uint MM_Clone() // 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[i*1024] ); + INVLPG( &gaTmpTable[page] ); // Fill - for( j = 0; j < 1024; j ++ ) + for( j = 0; j < 1024; j ++, page++ ) { - if( !(gaPageTable[i*1024+j] & PF_PRESENT) ) { - gaTmpTable[i*1024+j] = 0; + if( !(gaPageTable[page] & PF_PRESENT) ) { + gaTmpTable[page] = 0; continue; } - #if USE_COW // Refrence old page - MM_RefPhys( gaPageTable[i*1024+j] & ~0xFFF ); + MM_RefPhys( gaPageTable[page] & ~0xFFF ); // Add to new table - if(gaPageTable[i*1024+j] & PF_WRITE) { - gaTmpTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW; - gaPageTable[i*1024+j] = (gaPageTable[i*1024+j] & ~PF_WRITE) | PF_COW; + 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[i*1024+j] = gaPageTable[i*1024+j]; - LOG("gaTmpTable[0x%x] = 0x%x", i*1024+j, gaTmpTable[i*1024+j]); - #else - gaTmpTable[i*1024+j] = MM_DuplicatePage( (i*1024+j)<<12 ) | (gaPageTable[i*1024+j]&7); - #endif + gaTmpTable[page] = gaPageTable[page]; } } @@ -431,7 +514,7 @@ Uint MM_Clone() // Allocate kernel stack for(i = KERNEL_STACKS >> 22; - i < KERNEL_STACK_END >> 22; + i < KERNEL_STACKS_END >> 22; i ++ ) { // Check if directory is allocated @@ -475,19 +558,22 @@ Uint MM_Clone() } } - //LEAVE('x', *gTmpCR3 & ~0xFFF); - return *gTmpCR3 & ~0xFFF; + ret = *gTmpCR3 & ~0xFFF; + RELEASE( &gilTempFractal ); + + //LEAVE('x', ret); + return ret; } /** - * \fn Uint MM_NewKStack() + * \fn tVAddr MM_NewKStack() * \brief Create a new kernel stack */ -Uint MM_NewKStack() +tVAddr MM_NewKStack() { - Uint base = KERNEL_STACKS; + tVAddr base = KERNEL_STACKS; Uint i; - for(;base>12]; + + // Get the old ESP and EBP + __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp)); + __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp)); + + // Find a free worker stack address + for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++) + { + // Used block + if( gWorkerStacks[base/32] == -1 ) { + base += 31; base &= ~31; + base --; // Counteracted by the base++ + continue; + } + // Used stack + if( gWorkerStacks[base/32] & (1 << base) ) { + continue; + } + } + if(base >= NUM_WORKER_STACKS) { + Warning("Uh-oh! Out of worker stacks"); + return 0; + } + + // It's ours now! + gWorkerStacks[base/32] |= (1 << base); + // Make life easier for later calls + giLastUsedWorker = base; + // We have one + base = WORKER_STACKS + base * WORKER_STACK_SIZE; + + // Acquire the lock for the temp fractal mappings + LOCK(&gilTempFractal); + + // Set the temp fractals to TID0's address space + *gTmpCR3 = (Uint)gaInitPageDir | 3; + INVLPG( gaTmpDir ); + + // Check if the directory is mapped (we are assuming that the stacks + // will fit neatly in a directory + if(gaTmpDir[ base >> 22 ] == 0) { + gaTmpDir[ base >> 22 ] = MM_AllocPhys() | 3; + INVLPG( &gaTmpTable[ (base>>22) & ~0x3FF ] ); + } + + // Mapping Time! + for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 ) + { + pages[ addr >> 12 ] = MM_AllocPhys(); + gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3; + } + // Release the temp mapping lock + RELEASE(&gilTempFractal); + + // Copy the old stack + oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1); + esp = oldstack - esp; // ESP as an offset in the stack + + 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((Uint)tmpPage); + i --; + } + + return base; +} + +/** + * \fn void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask) * \brief Sets the flags on a page */ -void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask) +void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask) { tPAddr *ent; if( !(gaPageDir[VAddr >> 22] & 1) ) return ; @@ -524,13 +703,26 @@ void MM_SetFlags(Uint VAddr, Uint Flags, Uint Mask) if( Flags & MM_PFLAG_KERNEL ) *ent &= ~PF_USER; else *ent |= PF_USER; } + + // Copy-On-Write + if( Mask & MM_PFLAG_COW ) + { + if( Flags & MM_PFLAG_COW ) { + *ent &= ~PF_WRITE; + *ent |= PF_COW; + } + else { + *ent &= ~PF_COW; + *ent |= PF_WRITE; + } + } } /** - * \fn tPAddr MM_DuplicatePage(Uint VAddr) + * \fn tPAddr MM_DuplicatePage(tVAddr VAddr) * \brief Duplicates a virtual page to a physical one */ -tPAddr MM_DuplicatePage(Uint VAddr) +tPAddr MM_DuplicatePage(tVAddr VAddr) { tPAddr ret; Uint temp; @@ -568,7 +760,7 @@ tPAddr MM_DuplicatePage(Uint VAddr) * \brief Create a temporary memory mapping * \todo Show Luigi Barone (C Lecturer) and see what he thinks */ -Uint MM_MapTemp(tPAddr PAddr) +tVAddr MM_MapTemp(tPAddr PAddr) { int i; @@ -594,15 +786,15 @@ Uint MM_MapTemp(tPAddr PAddr) return TEMP_MAP_ADDR + (i << 12); } RELEASE( &gilTempMappings ); - Proc_Yield(); + Threads_Yield(); } } /** - * \fn void MM_FreeTemp(Uint PAddr) + * \fn void MM_FreeTemp(tVAddr PAddr) * \brief Free's a temp mapping */ -void MM_FreeTemp(Uint VAddr) +void MM_FreeTemp(tVAddr VAddr) { int i = VAddr >> 12; //ENTER("xVAddr", VAddr); @@ -614,10 +806,10 @@ void MM_FreeTemp(Uint VAddr) } /** - * \fn Uint MM_MapHWPage(tPAddr PAddr, Uint Number) + * \fn tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number) * \brief Allocates a contigous number of pages */ -Uint MM_MapHWPage(tPAddr PAddr, Uint Number) +tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number) { int i, j; @@ -652,10 +844,82 @@ Uint MM_MapHWPage(tPAddr PAddr, Uint Number) } /** - * \fn void MM_UnmapHWPage(Uint VAddr, Uint Number) + * \fn tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr) + * \brief Allocates DMA physical memory + * \param Pages Number of pages required + * \param MaxBits Maximum number of bits the physical address can have + * \param PhysAddr Pointer to the location to place the physical address allocated + * \return Virtual address allocate + */ +tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr) +{ + tPAddr maxCheck = (1 << MaxBits); + tPAddr phys; + tVAddr ret; + + ENTER("iPages iMaxBits pPhysAddr", Pages, MaxBits, PhysAddr); + + // Sanity Check + if(MaxBits < 12 || !PhysAddr) { + LEAVE('i', 0); + return 0; + } + + // Bound + if(MaxBits >= PHYS_BITS) maxCheck = -1; + + // Fast Allocate + if(Pages == 1 && MaxBits >= PHYS_BITS) + { + phys = MM_AllocPhys(); + *PhysAddr = phys; + ret = MM_MapHWPage(phys, 1); + if(ret == 0) { + MM_DerefPhys(phys); + LEAVE('i', 0); + return 0; + } + LEAVE('x', ret); + return ret; + } + + // Slow Allocate + phys = MM_AllocPhysRange(Pages); + // - Was it allocated? + if(phys == 0) { + LEAVE('i', 0); + return 0; + } + // - Check if the memory is OK + if(phys + (Pages-1)*0x1000 > maxCheck) + { + // Deallocate and return 0 + for(;Pages--;phys+=0x1000) + MM_DerefPhys(phys); + LEAVE('i', 0); + return 0; + } + + // Allocated successfully, now map + ret = MM_MapHWPage(phys, Pages); + if( ret == 0 ) { + // If it didn't map, free then return 0 + for(;Pages--;phys+=0x1000) + MM_DerefPhys(phys); + LEAVE('i', 0); + return 0; + } + + *PhysAddr = phys; + LEAVE('x', ret); + return ret; +} + +/** + * \fn void MM_UnmapHWPage(tVAddr VAddr, Uint Number) * \brief Unmap a hardware page */ -void MM_UnmapHWPage(Uint VAddr, Uint Number) +void MM_UnmapHWPage(tVAddr VAddr, Uint Number) { int i, j; // Sanity Check @@ -679,4 +943,5 @@ EXPORT(MM_GetPhysAddr); EXPORT(MM_Map); //EXPORT(MM_Unmap); EXPORT(MM_MapHWPage); +EXPORT(MM_AllocDMA); EXPORT(MM_UnmapHWPage);