diff --git a/sys/kernel/ld.c b/sys/kernel/ld.c index 6a60016..f161261 100644 --- a/sys/kernel/ld.c +++ b/sys/kernel/ld.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2002-2018, 2020 The UbixOS Project. + * Copyright (c) 2002-2018, 2020, 2022 The UbixOS Project. * All rights reserved. * * This was developed by Christopher W. Olsen for the UbixOS Project. @@ -59,74 +59,87 @@ Elf_Addr addr; - /* Open our dynamic linker */ - ldFd = fopen(interp, "rb"); + ldFd = fopen( interp, "rb" ); - if (ldFd == 0x0) { + if( ldFd == 0x0 ) { - ldFd = fopen("sys:/libexec/ld.so", "rb"); + ldFd = fopen( "sys:/libexec/ld.so", "rb" ); - if (ldFd == 0x0) - return (0x0); + if ( ldFd == 0x0 ) { + + return (0x0); + + } } - kern_fseek(ldFd, 0x0, 0x0); - binaryHeader = (Elf32_Ehdr*) kmalloc(sizeof(Elf32_Ehdr)); + kern_fseek( ldFd, 0x0, 0x0 ); - assert(binaryHeader); - fread(binaryHeader, sizeof(Elf32_Ehdr), 1, ldFd); + binaryHeader = ( Elf32_Ehdr * ) kmalloc( sizeof( Elf32_Ehdr ) ); - programHeader = (Elf_Phdr*) kmalloc(sizeof(Elf_Phdr) * binaryHeader->e_phnum); - assert(programHeader); + assert( binaryHeader ); - kern_fseek(ldFd, binaryHeader->e_phoff, 0); - fread(programHeader, sizeof(Elf_Shdr), binaryHeader->e_phnum, ldFd); + fread( binaryHeader, sizeof( Elf32_Ehdr ), 1, ldFd ); - sectionHeader = (Elf_Shdr*) kmalloc(sizeof(Elf_Shdr) * binaryHeader->e_shnum); + programHeader = ( Elf_Phdr * ) kmalloc( sizeof( Elf_Phdr ) * binaryHeader->e_phnum ); + + assert( programHeader ); + + kern_fseek( ldFd, binaryHeader->e_phoff, 0 ); + + fread( programHeader, sizeof( Elf_Shdr ), binaryHeader->e_phnum, ldFd ); + + sectionHeader = ( Elf_Shdr * ) kmalloc( sizeof( Elf_Shdr ) * binaryHeader->e_shnum ); assert(sectionHeader); - kern_fseek(ldFd, binaryHeader->e_shoff, 0); - fread(sectionHeader, sizeof(Elf_Shdr), binaryHeader->e_shnum, ldFd); - shStr = (char*) kmalloc(sectionHeader[binaryHeader->e_shstrndx].sh_size); - kern_fseek(ldFd, sectionHeader[binaryHeader->e_shstrndx].sh_offset, 0); - fread(shStr, sectionHeader[binaryHeader->e_shstrndx].sh_size, 1, ldFd); + kern_fseek( ldFd, binaryHeader->e_shoff, 0 ); - for (i = 0x0; i < binaryHeader->e_phnum; i++) { + fread( sectionHeader, sizeof( Elf_Shdr ), binaryHeader->e_shnum, ldFd ); - switch (programHeader[i].p_type) { + shStr = ( char *) kmalloc( sectionHeader[binaryHeader->e_shstrndx].sh_size ); + + kern_fseek( ldFd, sectionHeader[binaryHeader->e_shstrndx].sh_offset, 0 ); + + fread( shStr, sectionHeader[binaryHeader->e_shstrndx].sh_size, 1, ldFd ); + + for( i = 0x0; i < binaryHeader->e_phnum; i++ ) { + + switch ( programHeader[i].p_type ) { case PT_LOAD: - newLoc = (char*) programHeader[i].p_vaddr + LD_START; + newLoc = ( char * ) programHeader[i].p_vaddr + LD_START; /* Allocate Memory Im Going To Have To Make This Load Memory With Correct Settings so it helps us in the future */ - for (x = 0; x < (programHeader[i].p_memsz); x += 0x1000) { + for( x = 0; x < ( programHeader[i].p_memsz ); x += 0x1000 ) { /* make r/w or ro */ - if ((vmm_remapPage(vmm_findFreePage(_current->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x + LD_START), PAGE_DEFAULT, _current->id, 0)) == 0x0) - K_PANIC("vmmRemapPage: ld"); + if( (vmm_remapPage( vmm_findFreePage( _current->id ), ( ( programHeader[i].p_vaddr & 0xFFFFF000 ) + x + LD_START ), PAGE_DEFAULT, _current->id, 0 ) ) == 0x0 ) { - memset((void*) ((programHeader[i].p_vaddr & 0xFFFFF000) + x + LD_START), 0x0, 0x1000); + K_PANIC("vmmRemapPage: ld"); + + } + + memset( ( void * ) ( ( programHeader[i].p_vaddr & 0xFFFFF000 ) + x + LD_START), 0x0, 0x1000 ); } /* Now Load Section To Memory */ - kern_fseek(ldFd, programHeader[i].p_offset, 0x0); - fread(newLoc, programHeader[i].p_filesz, 1, ldFd); + kern_fseek( ldFd, programHeader[i].p_offset, 0x0 ); + fread( newLoc, programHeader[i].p_filesz, 1, ldFd ); break; case PT_DYNAMIC: - kprintf("[%s:%i] PT_DYNAMIC", __FILE__, __LINE__); + //kprintf("[%s:%i] PT_DYNAMIC", __FILE__, __LINE__); - programDynamic = (char *) programHeader[i].p_vaddr + LD_START; + programDynamic = ( char * ) programHeader[i].p_vaddr + LD_START; break; @@ -139,7 +152,7 @@ default: - kprintf("Unhandled Header (kernel) : %08x\n", programHeader[i].p_type); + kprintf( "Unhandled ELF header (kernel) : %08x\n", programHeader[i].p_type ); break; diff --git a/sys/kernel/vfs_calls.c b/sys/kernel/vfs_calls.c index 0045f40..d4db626 100644 --- a/sys/kernel/vfs_calls.c +++ b/sys/kernel/vfs_calls.c @@ -465,7 +465,7 @@ thr->td_retval[0] = fd; } - //kprintf("sO: 0x%X:%s:", args->mode, args->path, td->td_retval[0]); + kprintf("sO: 0x%X:%s:", args->mode, args->path, td->td_retval[0]); return (error); diff --git a/sys/vmm/copyvirtualspace.c b/sys/vmm/copyvirtualspace.c index cebf052..4264309 100644 --- a/sys/vmm/copyvirtualspace.c +++ b/sys/vmm/copyvirtualspace.c @@ -53,10 +53,16 @@ void *newPageDirectoryAddress = 0x0; - 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; + uint32_t *parentPageDirectory = 0x0; + uint32_t *newPageDirectory = 0x0; + uint32_t *parentPageTable = 0x0; + uint32_t *newPageTable = 0x0; + uint32_t *parentStackPage = 0x0; + uint32_t *newStackPage = 0x0; + + uint16_t x = 0; + uint16_t i = 0; + uint16_t s = 0; spinLock( &cvsSpinLock ); @@ -64,9 +70,12 @@ parentPageDirectory = ( uint32_t * ) PD_BASE_ADDR; /* Allocate A New Page For The New Page Directory */ - if ((newPageDirectory = (uint32_t *) vmm_getFreeKernelPage(pid, 1)) == 0x0) + if( ( newPageDirectory = ( uint32_t * ) vmm_getFreeKernelPage( pid, 1 ) ) == 0x0 ) { + kpanic( "Error: newPageDirectory == NULL, File: %s, Line: %i\n", __FILE__, __LINE__ ); + } + /* Set newPageDirectoryAddress To The Newly Created Page Directories Page */ newPageDirectoryAddress = ( void * ) vmm_getPhysicalAddr( ( uint32_t ) newPageDirectory ); @@ -77,121 +86,163 @@ newPageDirectory[0] = parentPageDirectory[0]; //XXX: We Dont Need This - newPageDirectory[1] = parentPageDirectory[1]; - if ((newPageTable = (uint32_t *) vmm_getFreeKernelPage(pid, 1)) == 0x0) - kpanic("Error: newPageTable == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); + if( ( newPageTable = ( uint32_t * ) vmm_getFreeKernelPage( pid, 1 ) ) == 0x0 ) { - parentPageTable = (uint32_t *) (PT_BASE_ADDR + (PAGE_SIZE * 1)); + kpanic( "Error: newPageTable == NULL, File: %s, Line: %i\n", __FILE__, __LINE__ ); - for (x = 0; x < PT_ENTRIES; x++) { - if (((parentPageTable[x]) & PAGE_PRESENT) == PAGE_PRESENT) { + } - /* Set Page To COW In Parent And Child Space */ - newPageTable[x] = (((uint32_t) parentPageTable[x] & 0xFFFFF000) | (KERNEL_PAGE_DEFAULT | PAGE_COW)); + parentPageTable = ( uint32_t * ) ( PT_BASE_ADDR + ( PAGE_SIZE * 1 ) ); - /* Increment The COW Counter For This Page */ - if (((uint32_t) parentPageTable[x] & PAGE_COW) == PAGE_COW) { - adjustCowCounter(((uint32_t) parentPageTable[x] & 0xFFFFF000), 1); - } - else { - /* Add Two If This Is The First Time Setting To COW */ - adjustCowCounter(((uint32_t) parentPageTable[x] & 0xFFFFF000), 2); - parentPageTable[x] |= PAGE_COW; // newPageTable[i]; - } + for( x = 0; x < PT_ENTRIES; x++ ) { - } - else - newPageTable[x] = parentPageTable[x]; - } + if( ( ( parentPageTable[x] ) & PAGE_PRESENT ) == PAGE_PRESENT ) { - newPageDirectory[1] = (vmm_getPhysicalAddr((uint32_t) newPageTable) | KERNEL_PAGE_DEFAULT); + /* Set Page To COW In Parent And Child Space */ + newPageTable[x] = (((uint32_t) parentPageTable[x] & 0xFFFFF000) | (KERNEL_PAGE_DEFAULT | PAGE_COW)); - vmm_unmapPage((uint32_t) newPageTable, 1); + /* Increment The COW Counter For This Page */ + if( ( ( uint32_t ) parentPageTable[x] & PAGE_COW ) == PAGE_COW ) { - newPageTable = 0x0; + adjustCowCounter( ( ( uint32_t ) parentPageTable[x] & 0xFFFFF000 ), 1 ); - /* Map The Kernel Memory Region Entry 770 Address 0xC0800000 */ - for (x = PD_INDEX(VMM_KERN_START); x <= PD_INDEX(VMM_KERN_END); x++) - newPageDirectory[x] = parentPageDirectory[x]; + } else { - /* Map The Kernel Stack Region */ - for (x = PD_INDEX(VMM_KERN_STACK_START); x <= PD_INDEX(VMM_KERN_STACK_END); x++) { - if ((parentPageDirectory[x] & PAGE_PRESENT) == PAGE_PRESENT) { - /* Set Parent To Propper Page Table */ - parentPageTable = (uint32_t *) (PT_BASE_ADDR + (PAGE_SIZE * x)); + /* Add Two If This Is The First Time Setting To COW */ + adjustCowCounter( ( ( uint32_t ) parentPageTable[x] & 0xFFFFF000 ), 2 ); - /* Allocate A New Page Table */ - if ((newPageTable = (uint32_t *) vmm_getFreeKernelPage(pid, 1)) == 0x0) - kpanic("Error: newPageTable == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); + parentPageTable[x] |= PAGE_COW; // newPageTable[i]; - bzero(newPageTable, PAGE_SIZE); + } - for (i = 0; i < PT_ENTRIES; i++) { - if ((parentPageTable[i] & PAGE_PRESENT) == PAGE_PRESENT) { + } else { - /* Alloc A New Page For This Stack Page */ - if ((newStackPage = (uint32_t *) vmm_getFreeKernelPage(pid, 1)) == 0x0) - kpanic("Error: newStackPage == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); + newPageTable[x] = parentPageTable[x]; - /* Set Pointer To Parents Stack Page */ - parentStackPage = (uint32_t *) (((PAGE_SIZE * PD_ENTRIES) * x) + (PAGE_SIZE * i)); + } - /* Copy The Stack Byte For Byte (I Should Find A Faster Way) */ - memcpy(newStackPage, parentStackPage, PAGE_SIZE); + } - /* Insert New Stack Into Page Table */ - newPageTable[i] = (vmm_getPhysicalAddr((uint32_t) newStackPage) | PAGE_DEFAULT | PAGE_STACK); - /* Unmap From Kernel Space */ - vmm_unmapPage((uint32_t) newStackPage, 1); - } - } - /* Put New Page Table Into New Page Directory */ - newPageDirectory[x] = (vmm_getPhysicalAddr((uint32_t) newPageTable) | PAGE_DEFAULT); - /* Unmap Page From Kernel Space But Keep It Marked As Not Avail */ - vmm_unmapPage((uint32_t) newPageTable, 1); - } - } + newPageDirectory[1] = ( vmm_getPhysicalAddr( ( uint32_t ) newPageTable ) | KERNEL_PAGE_DEFAULT); - /* - * Now For The Fun Stuff For Page Tables 2-767 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 Pages Unless an EXECVE Happens - * - * We start at the 4MB boundary as the first 4MB is special - */ - for (x = PD_INDEX(VMM_USER_START); x <= PD_INDEX(VMM_USER_END); x++) { + vmm_unmapPage( ( uint32_t ) newPageTable, 1); - /* If Page Table Exists Map It */ - if ((parentPageDirectory[x] & PAGE_PRESENT) == PAGE_PRESENT) { + + newPageTable = 0x0; + + /* Map The Kernel Memory Region Entry 770 Address 0xC0800000 */ + for( x = PD_INDEX( VMM_KERN_START ); x <= PD_INDEX( VMM_KERN_END ); x++ ) { + + newPageDirectory[x] = parentPageDirectory[x]; + + } + + /* Map The Kernel Stack Region */ + for( x = PD_INDEX( VMM_KERN_STACK_START ); x <= PD_INDEX( VMM_KERN_STACK_END ); x++ ) { + + if( ( parentPageDirectory[x] & PAGE_PRESENT ) == PAGE_PRESENT ) { + + /* Set Parent To Propper Page Table */ + parentPageTable = ( uint32_t * ) ( PT_BASE_ADDR + ( PAGE_SIZE * x ) ); + + + /* Allocate A New Page Table */ + if( ( newPageTable = ( uint32_t * ) vmm_getFreeKernelPage( pid, 1 ) ) == 0x0 ) { + + kpanic("Error: newPageTable == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); + + } + + + bzero( newPageTable, PAGE_SIZE ); + + for( i = 0; i < PT_ENTRIES; i++ ) { + + if( ( parentPageTable[i] & PAGE_PRESENT ) == PAGE_PRESENT ) { + + /* Alloc A New Page For This Stack Page */ + if( ( newStackPage = ( uint32_t * ) vmm_getFreeKernelPage( pid, 1 ) ) == 0x0 ) { + + kpanic( "Error: newStackPage == NULL, File: %s, Line: %i\n", __FILE__, __LINE__ ); + + } + + + /* Set Pointer To Parents Stack Page */ + parentStackPage = ( uint32_t * ) ( ( ( PAGE_SIZE * PD_ENTRIES ) * x ) + ( PAGE_SIZE * i ) ); + + /* Copy The Stack Byte For Byte (I Should Find A Faster Way) */ + memcpy( newStackPage, parentStackPage, PAGE_SIZE ); + + /* Insert New Stack Into Page Table */ + newPageTable[i] = ( vmm_getPhysicalAddr( ( uint32_t ) newStackPage ) | PAGE_DEFAULT | PAGE_STACK); + + + /* Unmap From Kernel Space */ + vmm_unmapPage( ( uint32_t ) newStackPage, 1 ); + + } + + } + + /* Put New Page Table Into New Page Directory */ + newPageDirectory[x] = ( vmm_getPhysicalAddr( ( uint32_t ) newPageTable ) | PAGE_DEFAULT); + + /* Unmap Page From Kernel Space But Keep It Marked As Not Avail */ + vmm_unmapPage( ( uint32_t ) newPageTable, 1 ); + + } + + } + + /* + * Now For The Fun Stuff For Page Tables 2-767 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 Pages Unless an EXECVE Happens + * + * We start at the 4MB boundary as the first 4MB is special + */ + + for( x = PD_INDEX( VMM_USER_START ); 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_t *) (PT_BASE_ADDR + (PAGE_SIZE * x)); + parentPageTable = ( uint32_t * ) ( PT_BASE_ADDR + ( PAGE_SIZE * x ) ); /* Allocate A New Page Table */ - if ((newPageTable = (uint32_t *) vmm_getFreeKernelPage(pid, 1)) == 0x0) - kpanic("Error: newPageTable == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); + if( ( newPageTable = ( uint32_t * ) vmm_getFreeKernelPage( pid, 1 ) ) == 0x0 ) { - bzero(newPageTable, PAGE_SIZE); + 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++) { + for( i = 0; i < PD_ENTRIES; i++ ) { /* If Page Is Mapped */ - if ((parentPageTable[i] & PAGE_PRESENT) == PAGE_PRESENT) { + if( ( parentPageTable[i] & PAGE_PRESENT ) == PAGE_PRESENT ) { /* Check To See If Its A Stack Page */ - if (((uint32_t) 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_t *) vmm_getFreeKernelPage(pid, 1)) == 0x0) - kpanic("Error: newStackPage == NULL, File: %s, Line: %i\n", __FILE__, __LINE__); + /* Alloc A New Page For This Stack Page */ + if( ( newStackPage = ( uint32_t * ) vmm_getFreeKernelPage( pid, 1 ) ) == 0x0 ) { - /* Set Pointer To Parents Stack Page */ - parentStackPage = (uint32_t *) (((PAGE_SIZE * PD_ENTRIES) * x) + (PAGE_SIZE * i)); + kpanic( "Error: newStackPage == NULL, File: %s, Line: %i\n", __FILE__, __LINE__ ); - /* Copy The Stack Byte For Byte (I Should Find A Faster Way) */ - memcpy(newStackPage, parentStackPage, PAGE_SIZE); + } + + /* Set Pointer To Parents Stack Page */ + parentStackPage = (uint32_t *) (((PAGE_SIZE * PD_ENTRIES) * x) + (PAGE_SIZE * i)); + + /* Copy The Stack Byte For Byte (I Should Find A Faster Way) */ + memcpy(newStackPage, parentStackPage, PAGE_SIZE); /* Insert New Stack Into Page Table */ newPageTable[i] = (vmm_getPhysicalAddr((uint32_t) newStackPage) | PAGE_DEFAULT | PAGE_STACK);