diff --git a/sys/vmm/vmm_init.c b/sys/vmm/vmm_init.c index 8adaf91..736f2cb 100644 --- a/sys/vmm/vmm_init.c +++ b/sys/vmm/vmm_init.c @@ -41,11 +41,13 @@ */ int vmm_init() { - if (vmm_memMapInit() != 0x0) - K_PANIC("Couldn't Initialize vmm_memMap"); - if (vmm_pagingInit() != 0x0) - K_PANIC("Couldn't Initialize paging system"); + if( vmm_memMapInit( ) != 0x0 ) + K_PANIC( "Couldn't Initialize vmm_memMap" ); - return (0x0); + if( vmm_pagingInit() != 0x0 ) + K_PANIC( "Couldn't Initialize paging system" ); + + return( 0x0 ); + } diff --git a/sys/vmm/vmm_memory.c b/sys/vmm/vmm_memory.c index 6139d61..2044fbf 100644 --- a/sys/vmm/vmm_memory.c +++ b/sys/vmm/vmm_memory.c @@ -38,13 +38,13 @@ //MrOlsen (2016-01-11) NOTE: Need to Seperate Out CPU Specific Stuff Over Time #include -static uint32_t freePages = 0; -static struct spinLock vmmSpinLock = SPIN_LOCK_INITIALIZER; +static uint32_t freePages = 0; +static struct spinLock vmmSpinLock = SPIN_LOCK_INITIALIZER; //static struct spinLock vmmCowSpinLock = SPIN_LOCK_INITIALIZER; -int numPages = 0x0; +int numPages = 0x0; -mMap *vmmMemoryMap = (mMap *) VMM_MMAP_ADDR_RMODE; +mMap *vmmMemoryMap = ( mMap * ) VMM_MMAP_ADDR_RMODE; /************************************************************************ @@ -56,47 +56,55 @@ ************************************************************************/ int vmm_memMapInit() { - int i = 0x0; - int memStart = 0x0; - /* Count System Memory */ - numPages = countMemory(); + int i = 0x0; + int memStart = 0x0; - /* Set Memory Map To Point To First Physical Page That We Will Use */ - vmmMemoryMap = (mMap *) VMM_MMAP_ADDR_RMODE; + // Count System Memory + numPages = countMemory(); - /* Initialize Map Make All Pages Not Available */ - for (i = 0x0; i < numPages; i++) { - vmmMemoryMap[i].cowCounter = 0x0; - vmmMemoryMap[i].status = memNotavail; - vmmMemoryMap[i].pid = vmmID; - vmmMemoryMap[i].pageAddr = i * PAGE_SIZE; - } + // Set Memory Map To Point To First Physical Page That We Will Use + vmmMemoryMap = ( mMap * ) VMM_MMAP_ADDR_RMODE; - /* Calculate Start Of Free Memory */ - memStart = (0x101000 / 0x1000); + // Initialize Map Make All Pages Not Available + for( i = 0x0; i < numPages; i++ ) { - memStart += (((sizeof(mMap) * numPages) + (sizeof(mMap) - 1)) / 0x1000); + vmmMemoryMap[i].cowCounter = 0x0; + vmmMemoryMap[i].status = memNotavail; + vmmMemoryMap[i].pid = vmmID; + vmmMemoryMap[i].pageAddr = i * PAGE_SIZE; - /* Initialize All Free Pages To Available */ - vmmMemoryMap[(0x100000 / 0x1000)].status = memAvail; + } - freePages++; + // Calculate Start Of Free Memory + memStart = ( 0x101000 / 0x1000 ); - for (i = memStart; i < numPages; i++) { - vmmMemoryMap[i].status = memAvail; - freePages++; - } + memStart += ( ( ( sizeof( mMap ) * numPages ) + ( sizeof( mMap ) - 1 ) ) / 0x1000 ); - if (systemVitals) - systemVitals->freePages = freePages; + // Initialize All Free Pages To Available + vmmMemoryMap[( 0x100000 / 0x1000 )].status = memAvail; - /* Print Out Amount Of Memory */ - kprintf("Real Memory: %iKB\n", numPages * 4); - kprintf("Available Memory: %iKB\n", freePages * 4); + freePages++; - /* Return */ - return (0); + for( i = memStart; i < numPages; i++ ) { + + vmmMemoryMap[i].status = memAvail; + + freePages++; + + } + + + if( systemVitals ) + systemVitals->freePages = freePages; + + // Print Out Amount Of Memory + kprintf( "Real Memory: %iKB\n", numPages * 4 ); + kprintf( "Available Memory: %iKB\n", freePages * 4 ); + + // Return + return(0x0); + } /************************************************************************ @@ -109,25 +117,32 @@ ************************************************************************/ int countMemory() { - register uInt32 *mem = 0x0; - unsigned long memCount = -1, tempMemory = 0x0; - unsigned short memKb = 8; - unsigned char irq1State, irq2State; - unsigned long cr0 = 0x0; - /* - * Save The States Of Both IRQ 1 And 2 So We Can Turn Them Off And Restore - * Them Later - */ - irq1State = inportByte(0x21); - irq2State = inportByte(0xA1); + register uInt32 *mem = 0x0; - /* Turn Off IRQ 1 And 2 To Prevent Chances Of Faults While Examining Memory */ - outportByte(0x21, 0xFF); - outportByte(0xA1, 0xFF); + unsigned long memCount = -1; + unsigned long tempMemory = 0x0; - /* Save The State Of Register CR0 */ - cr0 = rcr0(); + unsigned short memKb = 8; + + unsigned char irq1State; + unsigned char irq2State; + + unsigned long cr0 = 0x0; + + /* + * Save The States Of Both IRQ 1 And 2 So We Can Turn Them Off And Restore + * Them Later + */ + irq1State = inportByte( 0x21 ); + irq2State = inportByte( 0xA1 ); + + // Turn Off IRQ 1 And 2 To Prevent Chances Of Faults While Examining Memory + outportByte( 0x21, 0xFF ); + outportByte( 0xA1, 0xFF ); + + // Save The State Of Register CR0 + cr0 = rcr0(); /* asm volatile ( @@ -138,9 +153,9 @@ ); */ - asm volatile ("wbinvd"); + asm volatile ( "wbinvd" ); - load_cr0(cr0 | 0x00000001 | 0x40000000 | 0x20000000); + load_cr0( cr0 | 0x00000001 | 0x40000000 | 0x20000000 ); /* asm volatile ( @@ -151,44 +166,56 @@ ); */ - while (memKb < 4096 && memCount != 0) { - memKb++; + while( memKb < 4096 && memCount != 0 ) { - if (memCount == -1) - memCount = 8388608; - else - memCount += 1024 * 1024; + memKb++; - mem = (uInt32 *) memCount; + if( memCount == -1 ) + memCount = 8388608; + else + memCount += 1024 * 1024; - tempMemory = *mem; + mem = ( uInt32 * ) memCount; - *mem = 0x55AA55AA; + tempMemory = *mem; - asm("": : :"memory"); + *mem = 0x55AA55AA; - if (*mem != 0x55AA55AA) { - memCount = 0; - } - else { - *mem = 0xAA55AA55; - asm("": : :"memory"); - if (*mem != 0xAA55AA55) { - memCount = 0; - } - } - asm("": : :"memory"); - *mem = tempMemory; - } + asm("": : :"memory"); - asm("nop"); + if( *mem != 0x55AA55AA ) { - //MrOlsen (2016-01-10) NOTE: I don't like this but I start incrementing form the start. - memKb--; + memCount = 0; - asm("nop"); + } else { - load_cr0(cr0); + *mem = 0xAA55AA55; + + asm( "": : :"memory" ); + + if( *mem != 0xAA55AA55 ) { + + memCount = 0; + + } + + } + + asm( "": : :"memory" ); + + *mem = tempMemory; + + } + + + asm( "nop" ); + + //MrOlsen (2016-01-10) NOTE: I don't like this but I start incrementing form the start. + memKb--; + + asm( "nop" ); + + load_cr0( cr0 ); /* asm volatile ( @@ -199,16 +226,17 @@ ); */ - asm("nop"); + asm("nop"); - /* Restore States For Both IRQ 1 And 2 */ - outportByte(0x21, irq1State); - outportByte(0xA1, irq2State); + // Restore States For Both IRQ 1 And 2 + outportByte( 0x21, irq1State ); + outportByte( 0xA1, irq2State ); - asm("nop"); + asm( "nop" ); - /* Return Amount Of Memory In Pages */ - return ((memKb * 1024 * 1024) / PAGE_SIZE); + // Return Amount Of Memory In Pages + return( ( memKb * 1024 * 1024 ) / PAGE_SIZE ); + } /************************************************************************ @@ -221,37 +249,46 @@ Notes: ************************************************************************/ -uint32_t vmm_findFreePage(pidType pid) { +uint32_t vmm_findFreePage( pidType pid ) { - int i = 0x0; + int i = 0x0; - /* Lets Look For A Free Page */ - if (pid < sysID) - kpanic("Error: invalid PID %i\n", pid); + // Lets Look For A Free Page + if( pid < sysID ) + kpanic( "Error: invalid PID %i\n", pid ); - spinLock(&vmmSpinLock); + spinLock( &vmmSpinLock ); - for (i = 0; i <= numPages; i++) { + for( i = 0; i <= numPages; i++ ) { - /* - * If We Found A Free Page Set It To Not Available After That Set Its Own - * And Return The Address - */ - if ((vmmMemoryMap[i].status == memAvail) && (vmmMemoryMap[i].cowCounter == 0)) { - vmmMemoryMap[i].status = memNotavail; - vmmMemoryMap[i].pid = pid; - freePages--; - if (systemVitals) - systemVitals->freePages = freePages; + /* + * If We Found A Free Page Set It To Not Available After That Set Its Own + * And Return The Address + */ - spinUnlock(&vmmSpinLock); - return (vmmMemoryMap[i].pageAddr); - } - } + if( ( vmmMemoryMap[i].status == memAvail ) && ( vmmMemoryMap[i].cowCounter == 0 ) ) { - /* If No Free Memory Is Found Return NULL */ - kpanic("Out Of Memory!!!!"); - return (0x0); + vmmMemoryMap[i].status = memNotavail; + vmmMemoryMap[i].pid = pid; + + freePages--; + + if( systemVitals ) + systemVitals->freePages = freePages; + + spinUnlock( &vmmSpinLock ); + + return( vmmMemoryMap[i].pageAddr ); + + } + + } + + // If No Free Memory Is Found Return NULL + kpanic( "Out Of Memory!!!!" ); + + return( 0x0 ); + } /************************************************************************ @@ -263,34 +300,40 @@ Notes: ************************************************************************/ -int freePage(uint32_t pageAddr) { +int freePage( uint32_t pageAddr ) { - int pageIndex = 0x0; - assert((pageAddr & 0xFFF) == 0x0); + int pageIndex = 0x0; + assert( ( pageAddr & 0xFFF ) == 0x0 ); - /* Find The Page Index To The Memory Map */ - pageIndex = (pageAddr / 4096); + // Find The Page Index To The Memory Map + pageIndex = ( pageAddr / PAGE_SIZE ); - /* Check If Page COW Is Greater Then 0 If It Is Dec It If Not Free It */ - if (vmmMemoryMap[pageIndex].cowCounter == 0) { + // Check If Page COW Is Greater Then 0 If It Is Dec It If Not Free It + if( vmmMemoryMap[pageIndex].cowCounter == 0 ) { - /* Set Page As Avail So It Can Be Used Again */ - spinLock(&vmmSpinLock); - vmmMemoryMap[pageIndex].status = memAvail; - vmmMemoryMap[pageIndex].cowCounter = 0x0; - vmmMemoryMap[pageIndex].pid = -2; - freePages++; - systemVitals->freePages = freePages; - spinUnlock(&vmmSpinLock); + // Set Page As Avail So It Can Be Used Again + spinLock( &vmmSpinLock ); - } - else { - /* Adjust The COW Counter */ - adjustCowCounter(((uint32_t) vmmMemoryMap[pageIndex].pageAddr), -1); - } + vmmMemoryMap[pageIndex].status = memAvail; + vmmMemoryMap[pageIndex].cowCounter = 0x0; + vmmMemoryMap[pageIndex].pid = -2; - /* Return */ - return (0); + freePages++; + + systemVitals->freePages = freePages; + + spinUnlock( &vmmSpinLock ); + + } else { + + /* Adjust The COW Counter */ + adjustCowCounter( ( ( uint32_t ) vmmMemoryMap[pageIndex].pageAddr ), -1 ); + + } + + // Return + return(0x0); + } /************************************************************************ @@ -305,31 +348,37 @@ 08/01/02 - I Think If Counter Gets To 0 I Should Free The Page ************************************************************************/ -int adjustCowCounter(uInt32 baseAddr, int adjustment) { +int adjustCowCounter( uInt32 baseAddr, int adjustment ) { - int vmmMemoryMapIndex = (baseAddr / PAGE_SIZE); + int vmmMemoryMapIndex = ( baseAddr / PAGE_SIZE ); - assert((baseAddr & 0xFFF) == 0x0); + assert( ( baseAddr & 0xFFF ) == 0x0 ); - spinLock(&vmmSpinLock); - /* Adjust COW Counter */ - vmmMemoryMap[vmmMemoryMapIndex].cowCounter += adjustment; + spinLock( &vmmSpinLock ); - if (vmmMemoryMap[vmmMemoryMapIndex].cowCounter <= 0) { + // Adjust COW Counter + vmmMemoryMap[vmmMemoryMapIndex].cowCounter += adjustment; - if (vmmMemoryMap[vmmMemoryMapIndex].cowCounter < 0) - kprintf("ERROR: Why is COW less than 0"); + if( vmmMemoryMap[vmmMemoryMapIndex].cowCounter <= 0 ) { - vmmMemoryMap[vmmMemoryMapIndex].cowCounter = 0x0; - vmmMemoryMap[vmmMemoryMapIndex].pid = vmmID; - vmmMemoryMap[vmmMemoryMapIndex].status = memAvail; - freePages++; - systemVitals->freePages = freePages; - } + if( vmmMemoryMap[vmmMemoryMapIndex].cowCounter < 0 ) + kprintf( "ERROR: Why is COW less than 0" ); - spinUnlock(&vmmSpinLock); - /* Return */ - return (0); + vmmMemoryMap[vmmMemoryMapIndex].cowCounter = 0x0; + vmmMemoryMap[vmmMemoryMapIndex].pid = vmmID; + vmmMemoryMap[vmmMemoryMapIndex].status = memAvail; + + freePages++; + + systemVitals->freePages = freePages; + + } + + spinUnlock( &vmmSpinLock ); + + // Return + return(0x0); + } /************************************************************************ @@ -345,55 +394,78 @@ ************************************************************************/ /* TODO: This can be greatly improved for performance but it gets the job done */ -void vmm_freeProcessPages(pidType pid) { - int i = 0, x = 0; - uint32_t *tmpPageTable = 0x0; - uint32_t *tmpPageDir = (uInt32 *) PD_BASE_ADDR; +void vmm_freeProcessPages( pidType pid ) { - spinLock(&vmmSpinLock); + int i = 0; + int x = 0; - /* Check Page Directory For An Avail Page Table */ - //NOTE: This cleans all memory space up to kernel space -#ifdef _IGNORE - for (i = 0; i < (PAGE_SIZE - (PAGE_SIZE / 4)); i++) { + uint32_t *tmpPageTable = 0x0; + uint32_t *tmpPageDir = ( uInt32 * ) PD_BASE_ADDR; - if (tmpPageDir[i] != 0) { + spinLock( &vmmSpinLock ); - /* Set Up Page Table Pointer */ - tmpPageTable = (uint32_t *) (PT_BASE_ADDR + (i * PAGE_SIZE)); + // Check Page Directory For An Avail Page Table + // NOTE: This cleans all memory space up to kernel space - /* Check The Page Table For COW Pages */ - for (x = 0; x < PD_ENTRIES; x++) { + #ifdef _IGNORE - /* If The Page Is COW Adjust COW Counter */ - if (((uint32_t) tmpPageTable[x] & PAGE_COW) == PAGE_COW) { - adjustCowCounter(((uint32_t) tmpPageTable[x] & 0xFFFFF000), -1); - } - } - } - } -#endif + for( i = 0; i < ( PAGE_SIZE - ( PAGE_SIZE / 4 ) ); i++ ) { - /* Loop Through Pages To Find Pages Owned By Process */ - for (i = 0; i < numPages; i++) { - if (vmmMemoryMap[i].pid == pid) { - /* Check To See If The cowCounter Is Zero If So We Can Ree It */ - if (vmmMemoryMap[i].cowCounter == 0) { - vmmMemoryMap[i].status = memAvail; - vmmMemoryMap[i].cowCounter = 0x0; - vmmMemoryMap[i].pid = vmmID; - freePages++; - systemVitals->freePages = freePages; - } - else { - spinUnlock(&vmmSpinLock); - adjustCowCounter((i * PAGE_SIZE), -1); - spinLock(&vmmSpinLock); - } - } - } + if( tmpPageDir[i] != 0 ) { - /* Return */ - spinUnlock(&vmmSpinLock); - return; + // Set Up Page Table Pointer + tmpPageTable = ( uint32_t * ) ( PT_BASE_ADDR + ( i * PAGE_SIZE ) ); + + // Check The Page Table For COW Pages + for( x = 0; x < PD_ENTRIES; x++ ) { + + // If The Page Is COW Adjust COW Counter + if( ( ( uint32_t ) tmpPageTable[x] & PAGE_COW ) == PAGE_COW ) { + + adjustCowCounter( ( ( uint32_t ) tmpPageTable[x] & 0xFFFFF000 ), -1 ); + + } + + } + + } + + } + + #endif + + // Loop Through Pages To Find Pages Owned By Process + for( i = 0; i < numPages; i++ ) { + + if( vmmMemoryMap[i].pid == pid ) { + + // Check To See If The cowCounter Is Zero If So We Can Ree It + if( vmmMemoryMap[i].cowCounter == 0 ) { + + vmmMemoryMap[i].status = memAvail; + vmmMemoryMap[i].cowCounter = 0x0; + vmmMemoryMap[i].pid = vmmID; + + freePages++; + + systemVitals->freePages = freePages; + + } else { + + spinUnlock( &vmmSpinLock ); + + adjustCowCounter( ( i * PAGE_SIZE ), -1 ); + + spinLock( &vmmSpinLock ); + + } + + } + + } + + // Return + spinUnlock( &vmmSpinLock ); + + return; } diff --git a/sys/vmm/vmm_mmap.c b/sys/vmm/vmm_mmap.c index b330d99..200d5d2 100644 --- a/sys/vmm/vmm_mmap.c +++ b/sys/vmm/vmm_mmap.c @@ -35,19 +35,27 @@ #include #include -int freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap) { - vm_size_t size, pageoff; - off_t pos; - int align, flags, error; - vm_offset_t addr; +int freebsd6_mmap( struct thread *td, struct freebsd6_mmap_args *uap ) { + + vm_size_t size_0; + vm_size_t size_1; + vm_size_t pageoff; + + off_t pos; + + int align; + int flags; + int error; + + vm_offset_t addr; int i; - error = 0; + error = 0; - size = uap->len; - flags = uap->flags; - pos = uap->pos; + size_0 = uap->len; + flags = uap->flags; + pos = uap->pos; kprintf("uap->flags: [0x%X]\n", uap->flags); kprintf("uap->addr: [0x%X]\n", uap->addr); @@ -57,7 +65,7 @@ kprintf("uap->pad: [0x%X]\n", uap->pad); kprintf("uap->pos: [0x%X]\n", uap->pos); - if ((uap->flags & MAP_ANON) != 0) + if( ( uap->flags & MAP_ANON ) != 0 ) pos = 0; /* @@ -65,23 +73,27 @@ * and save its page offset component. */ - pageoff = (pos & PAGE_MASK); - pos -= pageoff; + pageoff = ( pos & PAGE_MASK ); + pos -= pageoff; /* Adjust size for rounding (on both ends). */ - size += pageoff; - size = (vm_size_t) round_page(size); + size_0 += pageoff; + size_1 = ( vm_size_t ) round_page( size_0 ); + size_0 = sie_1 - /* Ensure alignment is at least a page and fits in a pointer. */ - align = flags & MAP_ALIGNMENT_MASK; - if (align != 0 && align != MAP_ALIGNED_SUPER && (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void*) * NBBY || align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT)) - return (EINVAL); + // Ensure alignment is at least a page and fits in a pointer. + align = flags & MAP_ALIGNMENT_MASK; - if (flags & MAP_FIXED) { - kprintf("FIXED NOT SUPPORTED YET"); - return (EINVAL); - } - else { + if( align != 0 && align != MAP_ALIGNED_SUPER && ( align >> MAP_ALIGNMENT_SHIFT >= sizeof( void * ) * NBBY || align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT ) ) + return( EINVAL ); + + if( flags & MAP_FIXED ) { + + kprintf( "FIXED NOT SUPPORTED YET" ); + + return( EINVAL ); + + } else { /* At Some Point I Need To Proc Lock Incase It's Threaded */ /* MrOlsen (2016-01-15) Temporary comment out if ( addr == 0 || (addr >= round_page( (vm_offset_t) vms->vm_taddr ) && addr < round_page( (vm_offset_t) vms->vm_daddr + lim_max( td->td_proc, RLIMIT_DATA ) )) ) @@ -89,7 +101,7 @@ */ } - if (flags & MAP_ANON) { + if( flags & MAP_ANON ) { /* * Mapping blank space is trivial. */ @@ -100,91 +112,130 @@ maxprot = VM_PROT_ALL; cap_maxprot = VM_PROT_ALL; */ - for (i = addr; i < (addr + size); i += 0x1000) { - if (vmm_remapPage(vmm_findFreePage(_current->id), i, PAGE_DEFAULT, _current->id, 0) == 0x0) - K_PANIC("remap Failed"); + for( i = addr; i < (addr + size_0 ); i += 0x1000 ) { + + if( vmm_remapPage( vmm_findFreePage( _current->id ), i, PAGE_DEFAULT, _current->id, 0 ) == 0x0 ) + K_PANIC( "remap Failed" ); + } - kprintf("td->vm_dsize should be adjust but isn't"); - } - else { - /* Mapping File */ - kprintf("File Mapping Not Supported Yet"); - return (EINVAL); + + kprintf( "td->vm_dsize should be adjust but isn't" ); + + } else { + + /* Mapping File */ + + kprintf( "File Mapping Not Supported Yet" ); + return( EINVAL ); + } - return (0x0); + return( 0x0 ); + } -int sys_munmap(struct thread *td, struct sys_munmap_args *uap) { - //TEMP - //kprintf("[%s:%i] munmap(0x%X:%i)", __FILE__, __LINE__, uap->addr, uap->len); - td->td_retval[0] = 0; - return (0); +int sys_munmap( struct thread *td, struct sys_munmap_args *uap ) { + + //TEMP + + kprintf( "[%s:%i] munmap(0x%X:%i)", __FILE__, __LINE__, uap->addr, uap->len ); + + td->td_retval[0] = 0; + + return(0x0); + } -int sys_mmap(struct thread *td, struct sys_mmap_args *uap) { - vm_offset_t addr = 0x0; - char *tmp = 0x0; - struct file *fd = 0x0; - int x; +int sys_mmap( struct thread *td, struct sys_mmap_args *uap ) { - //kprintf("[%s:%i] mmap(%i-0x%X-%i)", __FILE__, __LINE__, uap->fd, uap->addr, uap->len); + vm_offset_t addr = 0x0; - addr = (vm_offset_t) uap->addr; + char *tmp = 0x0; - if (uap->fd == -1) { - if (uap->addr != 0x0) { - for (x = 0x0; x < round_page(uap->len); x += 0x1000) { - vmm_unmapPage(((uint32_t) uap->addr & 0xFFFFF000) + x, VMM_FREE); - /* Make readonly and read/write !!! */ - if (vmm_remapPage(vmm_findFreePage(_current->id), (((uint32_t) uap->addr & 0xFFFFF000) + x), PAGE_DEFAULT, _current->id, 0) == 0x0) - K_PANIC("Remap Page Failed"); + struct file *fd = 0x0; - } - //kprintf("(tmp3: 0x%X)", tmp); - tmp = uap->addr; - //kprintf("(tmp4: 0x%X)", tmp); - bzero(tmp, uap->len); - td->td_retval[0] = (uint32_t) tmp; - return (0x0); - } + int x = 0x0; - td->td_retval[0] = vmm_getFreeVirtualPage(_current->id, round_page( uap->len ) / 0x1000, VM_TASK); + kprintf("[%s:%i] mmap(%i-0x%X-%i)", __FILE__, __LINE__, uap->fd, uap->addr, uap->len); + + addr = (vm_offset_t) uap->addr; + + if( uap->fd == -1 ) { + + if( uap->addr != 0x0 ) { + + for( x = 0x0; x < round_page( uap->len ); x += 0x1000 ) { + + vmm_unmapPage( ( ( uint32_t ) uap->addr & 0xFFFFF000 ) + x, VMM_FREE ); + + // Make readonly and read/write !!! + if( vmm_remapPage( vmm_findFreePage( _current->id ), ( ( ( uint32_t ) uap->addr & 0xFFFFF000 ) + x ), PAGE_DEFAULT, _current->id, 0 ) == 0x0 ) + K_PANIC( "Remap Page Failed" ); + + } + + //kprintf("(tmp3: 0x%X)", tmp); + + tmp = uap->addr; + + //kprintf("(tmp4: 0x%X)", tmp); + + bzero( tmp, uap->len ); + + td->td_retval[0] = ( uint32_t ) tmp; + + return( 0x0 ); + + } + + td->td_retval[0] = vmm_getFreeVirtualPage( _current->id, round_page( uap->len ) / PAGE_SIZE, VM_TASK ); + //kprintf("(tmp5: 0x%X)", td->td_retval[0]); - bzero(td->td_retval[0], uap->len); - return (0x0); //vmm_getFreeVirtualPage(_current->id, round_page( uap->len ) / 0x1000, VM_THRD)); - } - else { - getfd(td, &fd, uap->fd); + bzero( td->td_retval[0], uap->len ); - if (uap->addr == 0x0) - tmp = (char*) vmm_getFreeVirtualPage(_current->id, round_page(uap->len) / 0x1000, VM_TASK); - else { + return( 0x0 ); //vmm_getFreeVirtualPage(_current->id, round_page( uap->len ) / 0x1000, VM_THRD)); - for (x = 0x0; x < round_page(uap->len); x += 0x1000) { + } else { - vmm_unmapPage(((uint32_t) uap->addr & 0xFFFFF000) + x, 1); + getfd( td, &fd, uap->fd ); - /* Make readonly and read/write !!! */ - if (vmm_remapPage(vmm_findFreePage(_current->id), (((uint32_t) uap->addr & 0xFFFFF000) + x), PAGE_DEFAULT, _current->id, 0) == 0x0) - K_PANIC("Remap Page Failed"); + if( uap->addr == 0x0 ) { + + tmp = ( char * ) vmm_getFreeVirtualPage( _current->id, round_page( uap->len ) / PAGE_SIZE, VM_TASK ); + + } else { + + for( x = 0x0; x < round_page( uap->len ); x += 0x1000 ) { + + vmm_unmapPage( ( ( uint32_t ) uap->addr & 0xFFFFF000 ) + x, 1 ); + + // Make readonly and read/write !!! + if( vmm_remapPage( vmm_findFreePage( _current->id ), ( ( ( uint32_t ) uap->addr & 0xFFFFF000 ) + x ), PAGE_DEFAULT, _current->id, 0 ) == 0x0 ) + K_PANIC( "Remap Page Failed" ); } + // kprintf("(tmp1: 0x%X)", tmp); + tmp = uap->addr; + // kprintf("(tmp2: 0x%X)", tmp); } - kern_fseek(fd->fd, uap->pos, 0x0); - fread(tmp, uap->len, 0x1, fd->fd); + kern_fseek( fd->fd, uap->pos, 0x0 ); - td->td_retval[0] = (uint32_t) tmp; + fread( tmp, uap->len, 0x1, fd->fd ); - if (td->td_retval[0] == (caddr_t) -1) - kpanic("MMAP_FAILED"); + td->td_retval[0] = ( uint32_t ) tmp; + + if( td->td_retval[0] == ( caddr_t ) -1 ) + kpanic( "MMAP_FAILED" ); + } - return (0x0); + + return( 0x0 ); + }