diff --git a/sys/i386/i386_exec.c b/sys/i386/i386_exec.c index fd1de98..daa38be 100644 --- a/sys/i386/i386_exec.c +++ b/sys/i386/i386_exec.c @@ -367,7 +367,7 @@ /* Set Up Stack Space */ //MrOlsen (2016-01-14) FIX: is the stack start supposed to be addressable xhcnage x= 1 to x=0 - for (x = 0; x < 100; x++) { + for (x = 1; x <= 100; x++) { vmm_remapPage(vmm_findFreePage(newProcess->id), STACK_ADDR - (x * 0x1000), PAGE_DEFAULT | PAGE_STACK, newProcess->id); bzero(STACK_ADDR - (x * 0x1000), 0x1000); } @@ -387,7 +387,7 @@ newProcess->tss.ss2 = 0x0; newProcess->tss.eip = (long) binaryHeader->e_entry; newProcess->tss.eflags = 0x206; - newProcess->tss.esp = STACK_ADDR - 16; + newProcess->tss.esp = STACK_ADDR; newProcess->tss.ebp = STACK_ADDR; newProcess->tss.esi = 0x0; newProcess->tss.edi = 0x0; @@ -411,7 +411,7 @@ fclose(newProcess->files[0]); newProcess->files[0] = 0x0; - tmp = (uInt32 *) newProcess->tss.esp0 - 5; + tmp = (uint32_t *) newProcess->tss.esp0 - 5; tmp[0] = binaryHeader->e_entry; tmp[3] = STACK_ADDR - 12; @@ -524,6 +524,9 @@ //MrOlsen 2017-12-15 - FIX! - This should be done before it was causing a lot of problems why did I free space after loading binary???? vmm_cleanVirtualSpace((uint32_t) 0x8048000); + /* Clear Stack */ + bzero(STACK_ADDR - (100 * PAGE_SIZE), (PAGE_SIZE * 100)); + /* Load ELF Header */ if ((binaryHeader = (Elf_Ehdr *) kmalloc(sizeof(Elf_Ehdr))) == 0x0) K_PANIC("MALLOC FAILED"); @@ -594,10 +597,10 @@ /* Make readonly and read/write !!! */ if (vmm_remapPage(vmm_findFreePage(_current->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x), PAGE_DEFAULT, _current->id) == 0x0) { K_PANIC("Error: Remap Page Failed"); - } + } else { kprintf("rP[0x%X]", (programHeader[i].p_vaddr & 0xFFFFF000) + x); - } + } memset((void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000); @@ -692,7 +695,7 @@ tmp = (uint32_t *) iFrame->user_esp; - memset((char *) tmp, 0x0, ARGV_PAGE + ENVP_PAGE + ELF_AUX + (argc + 1) + (envc + 1) + STACK_PAD); +// memset((char *) tmp, 0x0, ARGV_PAGE + ENVP_PAGE + ELF_AUX + (argc + 1) + (envc + 1) + STACK_PAD); tmp[0] = argc; diff --git a/sys/vmm/copyvirtualspace.c b/sys/vmm/copyvirtualspace.c index 717d368..f478767 100644 --- a/sys/vmm/copyvirtualspace.c +++ b/sys/vmm/copyvirtualspace.c @@ -50,10 +50,11 @@ ************************************************************************/ void *vmm_copyVirtualSpace(pidType pid) { void *newPageDirectoryAddress = 0x0; - uInt32 *parentPageDirectory = 0x0, *newPageDirectory = 0x0; - uInt32 *parentPageTable = 0x0, *newPageTable = 0x0; - uInt32 *parentStackPage = 0x0, *newStackPage = 0x0; - uInt16 x = 0, i = 0, s = 0; + + uint32_t *parentPageDirectory = 0x0, *newPageDirectory = 0x0; + uint32_t *parentPageTable = 0x0, *newPageTable = 0x0; + uint32_t *parentStackPage = 0x0, *newStackPage = 0x0; + uint16_t x = 0, i = 0, s = 0; spinLock(&cvsSpinLock); @@ -68,29 +69,34 @@ newPageDirectoryAddress = (void *) vmm_getPhysicalAddr((uInt32) newPageDirectory); /* First Set Up A Flushed Page Directory */ - memset(newPageDirectory, 0x0, 0x1000); + bzero(newPageDirectory, PAGE_SIZE); - /* Map The Top 1GB Region Of The VM Space */ + /* Map The Top 1GB Region Of The VM Space - This Is The Kernel Space*/ for (x = 768; x < PD_ENTRIES; x++) newPageDirectory[x] = parentPageDirectory[x]; /* * Now For The Fun Stuff For Page Tables 1-766 We Must Map These And Set * The Permissions On Every Mapped Pages To COW This Will Conserve Memory - * Because The Two VM Spaces Will Be Sharing Some Pages + * Because The Two VM Spaces Will Be Sharing Pages Unless an EXECVE Happens + * + * We start at the 4MB boundary as the first 4MB is special */ for (x = 0x1; x <= PD_INDEX(VMM_USER_END); x++) { + /* If Page Table Exists Map It */ if ((parentPageDirectory[x] & PAGE_PRESENT) == PAGE_PRESENT) { /* Set Parent To Propper Page Table */ - parentPageTable = (uInt32 *) (PT_BASE_ADDR + (0x1000 * x)); + parentPageTable = (uint32_t *) (PT_BASE_ADDR + (PAGE_SIZE * x)); /* Allocate A New Page Table */ if ((newPageTable = (uInt32 *) vmm_getFreeKernelPage(pid, 1)) == 0x0) kpanic("Error: newPageTable == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); + bzero(newPageTable, PAGE_SIZE); + /* Set Parent And New Pages To COW */ for (i = 0; i < PD_ENTRIES; i++) { @@ -98,41 +104,48 @@ if ((parentPageTable[i] & PAGE_PRESENT) == PAGE_PRESENT) { /* Check To See If Its A Stack Page */ - if (((uInt32) parentPageTable[i] & PAGE_STACK) == PAGE_STACK) { + if (((uint32_t) parentPageTable[i] & PAGE_STACK) == PAGE_STACK) { + /* Alloc A New Page For This Stack Page */ if ((newStackPage = (uInt32 *) vmm_getFreeKernelPage(pid, 1)) == 0x0) kpanic("Error: newStackPage == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); /* Set Pointer To Parents Stack Page */ - parentStackPage = (uInt32 *) (((1024 * 4096) * x) + (4096 * i)); + parentStackPage = (uint32_t *) (((PD_ENTRIES * PAGE_SIZE) * x) + (PAGE_SIZE * i)); - /* Copy The Tack Byte For Byte (I Should Find A Faster Way) */ - for (s = 0x0; s < PD_ENTRIES; s++) { - newStackPage[s] = parentStackPage[s]; - } + /* Copy The Stack Byte For Byte (I Should Find A Faster Way) */ + memcpy(newStackPage, parentStackPage, PAGE_SIZE); + /* + for (s = 0x0; s < PD_ENTRIES; s++) { + newStackPage[s] = parentStackPage[s]; + } + */ /* Insert New Stack Into Page Table */ - newPageTable[i] = (vmm_getPhysicalAddr((uInt32) newStackPage) | PAGE_DEFAULT | PAGE_STACK); + newPageTable[i] = (vmm_getPhysicalAddr((uint32_t) newStackPage) | PAGE_DEFAULT | PAGE_STACK); + /* Unmap From Kernel Space */ - vmm_unmapPage((uInt32) newStackPage, 1); + vmm_unmapPage((uint32_t) newStackPage, 1); } else { /* Set Page To COW In Parent And Child Space */ - newPageTable[i] = (((uInt32) parentPageTable[i] & 0xFFFFF000) | (PAGE_DEFAULT | PAGE_COW)); + newPageTable[i] = (((uint32_t) parentPageTable[i] & 0xFFFFF000) | (PAGE_DEFAULT | PAGE_COW)); + /* Increment The COW Counter For This Page */ - if (((uInt32) parentPageTable[i] & PAGE_COW) == PAGE_COW) { - adjustCowCounter(((uInt32) parentPageTable[i] & 0xFFFFF000), 1); + if (((uint32_t) parentPageTable[i] & PAGE_COW) == PAGE_COW) { + adjustCowCounter(((uint32_t) parentPageTable[i] & 0xFFFFF000), 1); } else { - adjustCowCounter(((uInt32) parentPageTable[i] & 0xFFFFF000), 2); + /* Add Two If This Is The First Time Setting To COW */ + adjustCowCounter(((uint32_t) parentPageTable[i] & 0xFFFFF000), 2); parentPageTable[i] = newPageTable[i]; } } } else { - newPageTable[i] = (uInt32) 0x0; + newPageTable[i] = (uint32_t) 0x0; } } @@ -141,48 +154,47 @@ /* Unmap Page From Kernel Space But Keep It Marked As Not Avail */ vmm_unmapPage((uInt32) newPageTable, 1); } - else { - newPageDirectory[x] = (uInt32) 0x0; - } } /* * Allocate A New Page For The The First Page Table Where We Will Map The - * Lower Region + * Lower Region First 4MB */ if ((newPageTable = (uInt32 *) vmm_getFreeKernelPage(pid, 1)) == 0x0) kpanic("Error: newPageTable == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); /* Flush The Page From Garbage In Memory */ - memset(newPageTable, 0x0, 0x1000); + bzero(newPageTable, PAGE_SIZE); /* Map This Into The Page Directory */ newPageDirectory[0] = (vmm_getPhysicalAddr((uInt32) newPageTable) | PAGE_DEFAULT); + /* Set Address Of Parents Page Table */ - parentPageTable = (uInt32 *) PT_BASE_ADDR; + parentPageTable = (uint32_t *) PT_BASE_ADDR; + /* Map The First 1MB Worth Of Pages */ for (x = 0; x < (PD_ENTRIES / 4); x++) { newPageTable[x] = parentPageTable[x]; } /* Map The Next 3MB Worth Of Pages But Make Them COW */ - for (x = (PD_ENTRIES / 4) + 1; x < PD_ENTRIES; x++) { + for (x = (PD_ENTRIES / 4); x < PD_ENTRIES; x++) { + /* If Page Is Avaiable Map It */ if ((parentPageTable[x] & 0xFFFFF000) != 0x0) { + /* Set Pages To COW */ - newPageTable[x] = (((uInt32) parentPageTable[x] & 0xFFFFF000) | (PAGE_DEFAULT | PAGE_COW)); + newPageTable[x] = (((uint32_t) parentPageTable[x] & 0xFFFFF000) | (PAGE_DEFAULT | PAGE_COW)); + /* Increment The COW Counter For This Page */ if (((uInt32) parentPageTable[x] & PAGE_COW) == PAGE_COW) { - adjustCowCounter(((uInt32) parentPageTable[x] & 0xFFFFF000), 1); + adjustCowCounter(((uint32_t) parentPageTable[x] & 0xFFFFF000), 1); } else { - adjustCowCounter(((uInt32) parentPageTable[x] & 0xFFFFF000), 2); + adjustCowCounter(((uint32_t) parentPageTable[x] & 0xFFFFF000), 2); parentPageTable[x] = newPageTable[x]; } } - else { - newPageTable[x] = (uInt32) 0x0; - } } /* @@ -193,7 +205,7 @@ */ newPageTable = (uInt32 *) vmm_getFreePage(pid); - newPageDirectory[PD_INDEX(PD_BASE_ADDR)] = (uint32_t) (vmm_getPhysicalAddr((uInt32) newPageTable) | PAGE_DEFAULT); + newPageDirectory[PD_INDEX(PD_BASE_ADDR)] = (uint32_t) (vmm_getPhysicalAddr((uint32_t) newPageTable) | PAGE_DEFAULT); newPageTable[0] = (uint32_t) ((uint32_t) (newPageDirectoryAddress) | PAGE_DEFAULT); //MrOlsen (2017-12-15) - kprintf( "PD3: %i - 0x%X - 0x%X\n", PD_INDEX( PD_BASE_ADDR ), newPageDirectoryAddress, newPageTable[0] ); @@ -207,23 +219,21 @@ * */ - newPageTable = (uInt32 *) vmm_getFreePage(pid); + newPageTable = (uint32_t *) vmm_getFreePage(pid); newPageDirectory[PD_INDEX(PT_BASE_ADDR)] = (uint32_t) (vmm_getPhysicalAddr((uInt32) newPageTable) | PAGE_DEFAULT); /* Flush The Page From Garbage In Memory */ - for (x = 0; x < PD_ENTRIES; x++) - newPageTable[x] = (uInt32) 0x0; + bzero(newPageTable, PAGE_SIZE); for (x = 0; x < PD_ENTRIES; x++) newPageTable[x] = newPageDirectory[x]; /* Unmap Page From Virtual Space */ - vmm_unmapPage((uInt32) newPageTable, 1); + vmm_unmapPage((uint32_t) newPageTable, 1); /* Now We Are Done With The Page Directory So Lets Unmap That Too */ - - vmm_unmapPage((uInt32) newPageDirectory, 1); + vmm_unmapPage((uint32_t) newPageDirectory, 1); spinUnlock(&cvsSpinLock); diff --git a/sys/vmm/createvirtualspace.c b/sys/vmm/createvirtualspace.c index 113b329..17dc20f 100644 --- a/sys/vmm/createvirtualspace.c +++ b/sys/vmm/createvirtualspace.c @@ -53,25 +53,21 @@ ************************************************************************/ void *vmm_createVirtualSpace(pid_t pid) { void *newPageDirectoryAddress = 0x0; - uInt32 *parentPageDirectory = 0x0, *newPageDirectory = 0x0; - uInt32 *parentPageTable = 0x0, *newPageTable = 0x0; + uint32_t *parentPageDirectory = 0x0, *newPageDirectory = 0x0; + uint32_t *parentPageTable = 0x0, *newPageTable = 0x0; int x = 0; /* Set Address Of Parent Page Directory */ - parentPageDirectory = (uInt32 *) PD_BASE_ADDR; + parentPageDirectory = (uint32_t *) PD_BASE_ADDR; /* Allocate A New Page For The New Page Directory */ - - newPageDirectory = (uInt32 *) vmm_getFreePage(pid); + newPageDirectory = (uint32_t *) vmm_getFreePage(pid); /* Set newPageDirectoryAddress To The Newly Created Page Directories Page */ - - newPageDirectoryAddress = (void *) vmm_getPhysicalAddr((uInt32) newPageDirectory); + newPageDirectoryAddress = (void *) vmm_getPhysicalAddr((uint32_t) newPageDirectory); /* First Set Up A Flushed Page Directory */ - for (x = 0; x < PD_ENTRIES; x++) { - newPageDirectory[x] = (uInt32) 0x0; - } + bzero(newPageDirectory, PAGE_SIZE); /* Map The Top Kernel (APPROX 1GB) Region Of The VM Space */ for (x = PD_INDEX(VMM_KERN_START); x < PD_ENTRIES; x++) { @@ -83,18 +79,16 @@ * Lower Region */ - newPageTable = (uInt32 *) vmm_getFreePage(pid); + newPageTable = (uint32_t *) vmm_getFreePage(pid); /* Flush The Page From Garbage In Memory */ - for (x = 0; x < PD_ENTRIES; x++) { - newPageTable[x] = (uInt32) 0x0; - } + bzero(newPageTable, PAGE_SIZE); /* Map This Into The Page Directory */ - newPageDirectory[0] = (vmm_getPhysicalAddr((uInt32) newPageTable) | PAGE_DEFAULT); + newPageDirectory[0] = (vmm_getPhysicalAddr((uint32_t) newPageTable) | PAGE_DEFAULT); /* Set Address Of Parents Page Table */ - parentPageTable = (uInt32 *) PT_BASE_ADDR; + parentPageTable = (uint32_t *) PT_BASE_ADDR; /* Map The First 1MB Worth Of Pages */ for (x = 0; x < (PD_ENTRIES / 4); x++) { @@ -102,7 +96,7 @@ } /* Unmap Page From Virtual Space */ - vmm_unmapPage((uInt32) newPageTable, 1); + vmm_unmapPage((uint32_t) newPageTable, 1); /* * @@ -110,14 +104,13 @@ * First Page After Page Tables * This must be mapped into the page directory before we map all 1024 page directories into the memory space */ - newPageTable = (uInt32 *) vmm_getFreePage(pid); + newPageTable = (uint32_t *) vmm_getFreePage(pid); - newPageDirectory[PD_INDEX(PD_BASE_ADDR)] = (uint32_t) (vmm_getPhysicalAddr((uInt32) newPageTable) | PAGE_DEFAULT); + newPageDirectory[PD_INDEX(PD_BASE_ADDR)] = (uint32_t) (vmm_getPhysicalAddr((uint32_t) newPageTable) | PAGE_DEFAULT); newPageTable[0] = (uint32_t) ((uint32_t) (newPageDirectoryAddress) | PAGE_DEFAULT); - //MrOlsen 2017-12-15 kprintf( "PD3: %i - 0x%X - 0x%X\n", PD_INDEX( PD_BASE_ADDR ), newPageDirectoryAddress, newPageTable[0] ); - vmm_unmapPage((uInt32) newPageTable, 1); + vmm_unmapPage((uint32_t) newPageTable, 1); /* * @@ -126,28 +119,23 @@ * */ - newPageTable = (uInt32 *) vmm_getFreePage(pid); + newPageTable = (uint32_t *) vmm_getFreePage(pid); - newPageDirectory[PD_INDEX(PT_BASE_ADDR)] = (uint32_t) (vmm_getPhysicalAddr((uInt32) newPageTable) | PAGE_DEFAULT); + newPageDirectory[PD_INDEX(PT_BASE_ADDR)] = (uint32_t) (vmm_getPhysicalAddr((uint32_t) newPageTable) | PAGE_DEFAULT); /* Flush The Page From Garbage In Memory */ - for (x = 0; x < PD_ENTRIES; x++) - newPageTable[x] = (uInt32) 0x0; + bzero(newPageTable, PAGE_SIZE); for (x = 0; x < PD_ENTRIES; x++) newPageTable[x] = newPageDirectory[x]; /* Unmap Page From Virtual Space */ - vmm_unmapPage((uInt32) newPageTable, 1); + vmm_unmapPage((uint32_t) newPageTable, 1); /* Now We Are Done With The Page Directory So Lets Unmap That Too */ - vmm_unmapPage((uInt32) newPageDirectory, 1); + vmm_unmapPage((uint32_t) newPageDirectory, 1); + /* Return Physical Address Of Page Directory */ return (newPageDirectoryAddress); } - -/*** - END - ***/ -