diff options
| author | sotech117 <michael_foiani@brown.edu> | 2024-05-13 09:27:24 +0000 |
|---|---|---|
| committer | sotech117 <michael_foiani@brown.edu> | 2024-05-13 09:27:24 +0000 |
| commit | f09878f6327426631d9419d825a4e8396e3b9dc4 (patch) | |
| tree | 009d1f1b1386baf6d07b3b7d9a436590ada14094 /kernel/vm | |
| parent | 0e2acbe54e5800621692c2f6e9e9590aa369e165 (diff) | |
weenix
Diffstat (limited to 'kernel/vm')
| -rw-r--r-- | kernel/vm/anon.c | 33 | ||||
| -rw-r--r-- | kernel/vm/brk.c | 101 | ||||
| -rw-r--r-- | kernel/vm/mmap.c | 165 | ||||
| -rw-r--r-- | kernel/vm/pagefault.c | 75 | ||||
| -rw-r--r-- | kernel/vm/shadow.c | 188 | ||||
| -rw-r--r-- | kernel/vm/vmmap.c | 556 |
6 files changed, 1077 insertions, 41 deletions
diff --git a/kernel/vm/anon.c b/kernel/vm/anon.c index 4a92fc9..a433395 100644 --- a/kernel/vm/anon.c +++ b/kernel/vm/anon.c @@ -27,7 +27,8 @@ static mobj_ops_t anon_mobj_ops = {.get_pframe = NULL, */ void anon_init() { - NOT_YET_IMPLEMENTED("VM: anon_init"); + // NOT_YET_IMPLEMENTED("VM: anon_init"); + anon_allocator = slab_allocator_create("anon", sizeof(mobj_t)); } /* @@ -36,8 +37,17 @@ void anon_init() */ mobj_t *anon_create() { - NOT_YET_IMPLEMENTED("VM: anon_create"); - return NULL; + // NOT_YET_IMPLEMENTED("VM: anon_create"); + // make a new mobj + mobj_t *mobj = (mobj_t *)slab_obj_alloc(anon_allocator); + // initialize the mobj + if (mobj) + { + mobj_init(mobj, MOBJ_ANON, &anon_mobj_ops); + mobj_lock(mobj); + return mobj; + } + panic("MINE anon_create: slab_obj_alloc failed"); } /* @@ -46,7 +56,15 @@ mobj_t *anon_create() */ static long anon_fill_pframe(mobj_t *o, pframe_t *pf) { - NOT_YET_IMPLEMENTED("VM: anon_fill_pframe"); + // NOT_YET_IMPLEMENTED("VM: anon_fill_pframe"); + + // set the pframe's mobj to the given mobj + // pf->pf_addr = o; + // // set the pframe's flags to dirty + // pf->pf_dirty = 1; + + memset(pf->pf_addr, 0, PAGE_SIZE); + return 0; } @@ -61,5 +79,10 @@ static long anon_flush_pframe(mobj_t *o, pframe_t *pf) { return 0; } */ static void anon_destructor(mobj_t *o) { - NOT_YET_IMPLEMENTED("VM: anon_destructor"); + // NOT_YET_IMPLEMENTED("VM: anon_destructor"); + // call the default destructor + mobj_default_destructor(o); + + // free the mobj + slab_obj_free(anon_allocator, o); } diff --git a/kernel/vm/brk.c b/kernel/vm/brk.c index 46d6fc2..69a315f 100644 --- a/kernel/vm/brk.c +++ b/kernel/vm/brk.c @@ -53,6 +53,105 @@ */ long do_brk(void *addr, void **ret) { - NOT_YET_IMPLEMENTED("VM: do_brk"); + // NOT_YET_IMPLEMENTED("VM: do_brk"); + + // If addr is NULL, return the current break + if (addr == NULL) + { + *ret = curproc->p_brk; + return 0; + } + + // Check if the address is within the valid range + if ((uintptr_t)addr > USER_MEM_HIGH) + { + return -ENOMEM; + } + + // Check if the address is within the valid range + if (addr < curproc->p_start_brk) + { + return -ENOMEM; + } + + // Check if the address is the same as the current break + // if (addr == curproc->p_brk) + // { + // *ret = curproc->p_brk; + // return 0; + // } + + // Check if the address is page aligned + uintptr_t addr_page_aligned = ADDR_TO_PN(PAGE_ALIGN_UP(addr)); + uintptr_t p_brk_page_aligned = ADDR_TO_PN(PAGE_ALIGN_UP(curproc->p_brk)); + uintptr_t p_start_brk_page_aligned = ADDR_TO_PN(PAGE_ALIGN_UP(curproc->p_start_brk)); + + // Lookup the vmarea that represents the heap + vmarea_t *heap_vmarea = vmmap_lookup(curproc->p_vmmap, p_start_brk_page_aligned); + + // Check if the address is the same as the current break + // If so, set rets and end here + if (addr_page_aligned == p_brk_page_aligned) + { + curproc->p_brk = addr; + *ret = addr; + return 0; + } + + // Check the three cases, whether the heap needs to be created, modified or shrinked + if (heap_vmarea == NULL) + { + // Create the heap + long ret = vmmap_is_range_empty(curproc->p_vmmap, p_start_brk_page_aligned, addr_page_aligned - p_brk_page_aligned); + if (!ret) + { + // On fail, return -ENOMEM + return -ENOMEM; + } + + // Map the heap + int flags = MAP_PRIVATE | MAP_ANON; + int prot = PROT_READ | PROT_WRITE; + ret = vmmap_map( + curproc->p_vmmap, NULL, + p_start_brk_page_aligned, + addr_page_aligned - p_start_brk_page_aligned, + prot, flags, + 0, + VMMAP_DIR_LOHI, &heap_vmarea + ); + if (ret < 0) + { + // On fail, return ret + return ret; + } + } + else if (addr_page_aligned < p_brk_page_aligned) + { + // Shrink the heap + long ret = vmmap_remove(curproc->p_vmmap, addr_page_aligned, p_brk_page_aligned - addr_page_aligned); + if (ret < 0) + { + // On fail, return ret + return ret; + } + } + else + { + // Modify the heap + long ret = vmmap_is_range_empty(curproc->p_vmmap, p_brk_page_aligned, addr_page_aligned - p_brk_page_aligned); + if (!ret) + { + // On fail, return -ENOMEM + return -ENOMEM; + } + // Update the heap + heap_vmarea->vma_end = addr_page_aligned; + } + + + // Update rets & return 0 on success + curproc->p_brk = addr; + *ret = addr; return 0; } diff --git a/kernel/vm/mmap.c b/kernel/vm/mmap.c index 082149b..78aa3b5 100644 --- a/kernel/vm/mmap.c +++ b/kernel/vm/mmap.c @@ -55,8 +55,134 @@ long do_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off, void **ret) { - NOT_YET_IMPLEMENTED("VM: do_mmap"); - return -1; + // NOT_YET_IMPLEMENTED("VM: do_mmap"); + + // check if addr is page aligned when MAP_FIXED is specified + if (PAGE_ALIGNED(addr) == 0 && (flags & MAP_FIXED)) + { + return -EINVAL; + } + + // check if MAP_FIXED is specified and addr is out of range of the user address space + if ((flags & MAP_FIXED) && ((uintptr_t)addr < USER_MEM_LOW || (uintptr_t)addr + len > USER_MEM_HIGH)) + { + return -EINVAL; + } + + // check if len is not zero (len is an unsigned value, so it is always positive) + if (len == 0) + { + return -EINVAL; + } + + // check if offset is positive and aligned + if (off < 0 || PAGE_ALIGNED(off) == 0) + { + return -EINVAL; + } + + // check if flags do not contain MAP_PRIVATE or MAP_SHARED + if ((flags & MAP_PRIVATE) == 0 && (flags & MAP_SHARED) == 0) + { + return -EINVAL; + } + + // check if fd is not a valid file descriptor and MAP_ANON was not set + if (fd < 0 && (flags & MAP_ANON) == 0) + { + return -EBADF; + } + + // check if a file mapping was requested, but fd is not open for reading + // file error checking is done in if statement below + file_t *file = NULL; + if (fd >= 0 && (flags & MAP_ANON) == 0) + { + // get the file and check if it is valid + file = fget(fd); + if (file == NULL) + { + return -EBADF; + } + + // ENODEV CHECKS + + // check if the file's vnode's mmap operation doesn't exist + if (file->f_vnode->vn_ops == NULL || file->f_vnode->vn_ops->mmap == NULL) + { + fput(&file); + return -ENODEV; + } + + // ACCESS CHECKS + + // check if thef FMODE_READ flag is not set + if ((file->f_mode & FMODE_READ) == 0) + { + fput(&file); + return -EACCES; + } + + // check if append mode is set and PROT_WRITE is set + if ((prot & PROT_WRITE) && (file->f_mode & FMODE_APPEND)) + { + fput(&file); + return -EACCES; + } + + // check if MAP_SHARED was requested and PROT_WRITE is set, but fd is not open in read/write (O_RDWR) mode + if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && (file->f_mode & FMODE_READ) == 0) + { + fput(&file); + return -EACCES; + } + + // check if PROT_WRITE is set, but the file has FMODE_APPEND specified + if ((prot & PROT_WRITE) && (file->f_mode & FMODE_APPEND)) + { + fput(&file); + return -EACCES; + } + + fput(&file); + } + + + // Now that error checking is done, we can proceed with the mapping + vmarea_t *vma = NULL; + long err = vmmap_map( + curproc->p_vmmap, + file ? file->f_vnode : NULL, + ADDR_TO_PN(PAGE_ALIGN_DOWN(addr)), + ADDR_TO_PN(PAGE_ALIGN_UP((uintptr_t)addr + len)) - ADDR_TO_PN(PAGE_ALIGN_DOWN(addr)), + prot, + flags, + off, + VMMAP_DIR_HILO, + &vma + ); + + // check if vmmap_map() failed + if (err < 0) + { + return err; + } + + // set ret if it was provided + void *start = PN_TO_ADDR(vma->vma_start); + if (ret) + { + *ret = start; + } + + // flush the TLB + tlb_flush_range( + (uintptr_t) start, + PAGE_SIZE * (vma->vma_end - vma->vma_start) + ); + + // return 0 on success + return 0; } /* @@ -78,6 +204,37 @@ long do_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off, */ long do_munmap(void *addr, size_t len) { - NOT_YET_IMPLEMENTED("VM: do_munmap"); - return -1; + // NOT_YET_IMPLEMENTED("VM: do_munmap"); + + // Check if addr is page aligned + if (PAGE_ALIGNED(addr) == 0) + { + return -EINVAL; + } + + // Check if len is in bounds + if (len > USER_MEM_HIGH) + { + return -EINVAL; + } + + // Check if the addr is out of range of the user address space + if ((uintptr_t)addr < USER_MEM_LOW || (uintptr_t)addr + len > USER_MEM_HIGH) + { + return -EINVAL; + } + + // Check if len is 0 + if (len == 0) + { + return -EINVAL; + } + + // Remove the mapping + long ret = vmmap_remove( + curproc->p_vmmap, + ADDR_TO_PN(addr), + ADDR_TO_PN(PAGE_ALIGN_UP((uintptr_t)addr + len)) + ); + return ret; }
\ No newline at end of file diff --git a/kernel/vm/pagefault.c b/kernel/vm/pagefault.c index 764ce85..2e0c92d 100644 --- a/kernel/vm/pagefault.c +++ b/kernel/vm/pagefault.c @@ -49,5 +49,78 @@ void handle_pagefault(uintptr_t vaddr, uintptr_t cause) { dbg(DBG_VM, "vaddr = 0x%p (0x%p), cause = %lu\n", (void *)vaddr, PAGE_ALIGN_DOWN(vaddr), cause); - NOT_YET_IMPLEMENTED("VM: handle_pagefault"); + // NOT_YET_IMPLEMENTED("VM: handle_pagefault"); + + // 1) Find the vmarea that contains vaddr, if it exists. + // check that the vaddr is valid + if (vaddr < USER_MEM_LOW || vaddr > USER_MEM_HIGH) + { + do_exit(EFAULT); + } + // lookup the vmarea for this addr + vmarea_t *vma = vmmap_lookup(curproc->p_vmmap, ADDR_TO_PN(vaddr)); + if (vma == NULL) + { + do_exit(EFAULT); + } + + // 2) Check the vmarea's protections (see the vmarea_t struct) against the 'cause' + // error out if the fault has cause write and we don't have write permission in the area + if ((cause & FAULT_WRITE) && !(vma->vma_prot & PROT_WRITE)) + { + do_exit(EFAULT); + } + // error out if the fault has cause exec and we don't have exec permission in the area + if ((cause & FAULT_EXEC) && !(vma->vma_prot & PROT_EXEC)) + { + do_exit(EFAULT); + } + // error out if we don't have read permission in the area + if (!(vma->vma_prot & PROT_READ)) + { + do_exit(EFAULT); + } + // error our if we don't have any permission in the area + if (vma->vma_prot == PROT_NONE) + { + do_exit(EFAULT); + } + + // 3) Obtain the corresponding pframe from the vmarea's mobj. + pframe_t *pf; + mobj_lock(vma->vma_obj); + int ret = mobj_get_pframe( + vma->vma_obj, + vma->vma_off + (ADDR_TO_PN(vaddr) - vma->vma_start), + cause & FAULT_WRITE ? 1 : 0, + &pf + ); + mobj_unlock(vma->vma_obj); + if (ret < 0) + { + do_exit(EFAULT); + } + + // 4) Finally, set up a call to pt_map to insert a new mapping into the appropriate pagetable + int pdflags = PT_PRESENT | PT_WRITE | PT_USER; + int ptflags = PT_PRESENT | PT_USER; + if (cause & FAULT_WRITE) + { + ptflags |= PT_WRITE; + } + + int err = pt_map( + curproc->p_pml4, + pt_virt_to_phys((uintptr_t) pf->pf_addr), + (uintptr_t) PAGE_ALIGN_DOWN(vaddr), + pdflags, + ptflags + ); + if (err < 0) + { + do_exit(EFAULT); + } + + // 5) Flush the TLB + tlb_flush((uintptr_t) PAGE_ALIGN_DOWN(vaddr)); } diff --git a/kernel/vm/shadow.c b/kernel/vm/shadow.c index 312b32e..91b1fce 100644 --- a/kernel/vm/shadow.c +++ b/kernel/vm/shadow.c @@ -41,7 +41,8 @@ static mobj_ops_t shadow_mobj_ops = {.get_pframe = shadow_get_pframe, */ void shadow_init() { - NOT_YET_IMPLEMENTED("VM: shadow_init"); + // NOT_YET_IMPLEMENTED("VM: shadow_init"); + shadow_allocator = slab_allocator_create("shadow", sizeof(mobj_shadow_t)); } /* @@ -60,8 +61,37 @@ void shadow_init() */ mobj_t *shadow_create(mobj_t *shadowed) { - NOT_YET_IMPLEMENTED("VM: shadow_create"); - return NULL; + // NOT_YET_IMPLEMENTED("VM: shadow_create"); + + // create a new shadow object + mobj_shadow_t *so = (mobj_shadow_t *)slab_obj_alloc(shadow_allocator); + if (!so) + { + return NULL; + } + + // initialize the mobj_shadow_t + + // set the bottom_mobj based on the two cases + if (shadowed->mo_type == MOBJ_SHADOW) + { + so->bottom_mobj = MOBJ_TO_SO(so->shadowed)->bottom_mobj; + } + else + { + so->bottom_mobj = shadowed; + } + // init the other fields + so->shadowed = shadowed; + mobj_init(&so->mobj, MOBJ_SHADOW, &shadow_mobj_ops); + mobj_ref(so->shadowed); + mobj_ref(so->bottom_mobj); + + // lock the shadow object + mobj_lock(&so->mobj); + + // return the shadow object + return &so->mobj; } /* @@ -80,7 +110,52 @@ mobj_t *shadow_create(mobj_t *shadowed) */ void shadow_collapse(mobj_t *o) { - NOT_YET_IMPLEMENTED("VM: shadow_collapse"); + // NOT_YET_IMPLEMENTED("VM: shadow_collapse"); + + // get the mobj_shadow_t and it's mobj + mobj_shadow_t *so = MOBJ_TO_SO(o); + mobj_t *iter = so->shadowed; + // iterate through the shadow chain + while (iter && so->shadowed->mo_type == MOBJ_SHADOW) + { + // check to see if the refcount is not 1. if so, continue to next shadowed object + if (so->shadowed->mo_refcount != 1) + { + iter = so->shadowed; + continue; + } + // else, go over the shadowed object's pframes + + // iterate through the pframes + mobj_lock(&so->shadowed); + list_iterate(&so->shadowed->mo_pframes, pframe, pframe_t, pf_link) + { + // get the pframe from the shadow object + pframe_t *spf = NULL; + + mobj_lock(iter); // lock before getting the pframe + mobj_find_pframe(o, pframe->pf_pagenum, &spf); + mobj_unlock(iter); + + // check if the pframe is not in the shadow object when migrating + if (spf == NULL) + { + // if not, remove the pframe from the shadowed object + // and insert it into out iterated shadow object + list_remove(&pframe->pf_link); + list_insert_tail(&iter->mo_pframes, &pframe->pf_link); + } + else + { + // if it is, release the pframe we found + pframe_release(&spf); + } + } + + // put locked the shadowed object after iterating through it + mobj_put_locked(&so->shadowed); + // FIXME: this is probably wrong + } } /* @@ -111,8 +186,47 @@ void shadow_collapse(mobj_t *o) static long shadow_get_pframe(mobj_t *o, size_t pagenum, long forwrite, pframe_t **pfp) { - NOT_YET_IMPLEMENTED("VM: shadow_get_pframe"); - return 0; + // NOT_YET_IMPLEMENTED("VM: shadow_get_pframe"); + + // if forwrite is set, use mobj_default_get_pframe + if (forwrite) + { + return mobj_default_get_pframe(o, pagenum, forwrite, pfp); + } + + // else, check if the object already contains the desired frame + pframe_t *pf = NULL; + mobj_find_pframe(o, pagenum, &pf); + if (pf) + { + // if it does, return the pframe + *pfp = pf; + return 0; + } + + // iterate through the shadow chain to find the nearest shadow mobj that has the frame + mobj_shadow_t *so = MOBJ_TO_SO(o); + mobj_t *iter = so->shadowed; + while (iter && iter->mo_type == MOBJ_SHADOW) + { + mobj_lock(iter); + mobj_find_pframe(o, pagenum, &pf); + mobj_unlock(iter); + if (pf) + { + *pfp = pf; + return 0; + } + // update the iterator + iter = MOBJ_TO_SO(iter)->shadowed; + } + + // if no shadow objects have the page, call mobj_get_pframe() to get the page from the bottom object + // at this point, iter is the bottom object + mobj_lock(iter); + long ret = mobj_get_pframe(iter, pagenum, forwrite, pfp); + mobj_unlock(iter); + return ret; } /* @@ -138,8 +252,47 @@ static long shadow_get_pframe(mobj_t *o, size_t pagenum, long forwrite, */ static long shadow_fill_pframe(mobj_t *o, pframe_t *pf) { - NOT_YET_IMPLEMENTED("VM: shadow_fill_pframe"); - return -1; + // NOT_YET_IMPLEMENTED("VM: shadow_fill_pframe"); + + // get the mobj_shadow_t + mobj_shadow_t *so = MOBJ_TO_SO(o); + // iterate over the shadow chain + mobj_t *iter = so->shadowed; + while (iter && iter->mo_type == MOBJ_SHADOW) + { + // get the pframe from the shadow object + pframe_t *spf = NULL; + mobj_lock(iter); + mobj_find_pframe(o, pf->pf_pagenum, &spf); + mobj_unlock(iter); + + // if the pframe is found, copy the contents into pf + // then release the pframe + if (spf) + { + memcpy(pf->pf_addr, spf->pf_addr, PAGE_SIZE); + pframe_release(&spf); + return 0; + } + + // update the iterator + iter = MOBJ_TO_SO(iter)->shadowed; + } + + // if none of the shadow objects have a copy of the frame, use mobj_get_pframe on the bottom object + pframe_t *spf = NULL; + mobj_lock(iter); + long ret = mobj_get_pframe(iter, pf->pf_pagenum, 0, &spf); + mobj_unlock(iter); + // check if the operation was sucessful, memcpy the contents into pf + // and release the pframe + if (ret == 0) + { + memcpy(pf->pf_addr, pf->pf_addr, PAGE_SIZE); + pframe_release(&spf); + } + + return ret; } /* @@ -153,8 +306,8 @@ static long shadow_fill_pframe(mobj_t *o, pframe_t *pf) */ static long shadow_flush_pframe(mobj_t *o, pframe_t *pf) { - NOT_YET_IMPLEMENTED("VM: shadow_flush_pframe"); - return -1; + // NOT_YET_IMPLEMENTED("VM: shadow_flush_pframe"); + return 0; } /* @@ -169,5 +322,18 @@ static long shadow_flush_pframe(mobj_t *o, pframe_t *pf) */ static void shadow_destructor(mobj_t *o) { - NOT_YET_IMPLEMENTED("VM: shadow_destructor"); + // NOT_YET_IMPLEMENTED("VM: shadow_destructor"); + + // get the mobj_shadow_t + mobj_shadow_t *so = MOBJ_TO_SO(o); + + // call the default destructor + mobj_default_destructor(o); + + // put the shadow and bottom_mobj + mobj_put(&so->shadowed); + mobj_put(&so->bottom_mobj); + + // free the slab + slab_obj_free(shadow_allocator, so); } diff --git a/kernel/vm/vmmap.c b/kernel/vm/vmmap.c index 0e2dad6..fd99c55 100644 --- a/kernel/vm/vmmap.c +++ b/kernel/vm/vmmap.c @@ -16,6 +16,7 @@ #include "mm/mm.h" #include "mm/mman.h" #include "mm/slab.h" +#include "mm/tlb.h" static slab_allocator_t *vmmap_allocator; static slab_allocator_t *vmarea_allocator; @@ -32,8 +33,27 @@ void vmmap_init(void) */ vmarea_t *vmarea_alloc(void) { - NOT_YET_IMPLEMENTED("VM: vmarea_alloc"); - return NULL; + // NOT_YET_IMPLEMENTED("VM: vmarea_alloc"); + + // Allocate a new vmarea + vmarea_t *new_vmarea = (vmarea_t *)slab_obj_alloc(vmarea_allocator); + if (new_vmarea == NULL) + { + return NULL; + } + + // Initialize the fields of the vmarea + new_vmarea->vma_start = 0; + new_vmarea->vma_end = 0; + new_vmarea->vma_off = 0; + new_vmarea->vma_prot = 0; + new_vmarea->vma_flags = 0; + new_vmarea->vma_obj = NULL; + new_vmarea->vma_obj = NULL; + list_link_init(&new_vmarea->vma_plink); + + // Return the new vmarea + return new_vmarea; } /* @@ -42,7 +62,22 @@ vmarea_t *vmarea_alloc(void) */ void vmarea_free(vmarea_t *vma) { - NOT_YET_IMPLEMENTED("VM: vmarea_free"); + // NOT_YET_IMPLEMENTED("VM: vmarea_free"); + + // Remove the vmarea from any lists it may be on + if (list_link_is_linked(&vma->vma_plink)) + { + list_remove(&vma->vma_plink); + } + + // Put the vma_obj if it exists + if (vma->vma_obj != NULL) + { + mobj_put(&vma->vma_obj); + } + + // Free the vmarea + slab_obj_free(vmarea_allocator, vma); } /* @@ -50,8 +85,20 @@ void vmarea_free(vmarea_t *vma) */ vmmap_t *vmmap_create(void) { - NOT_YET_IMPLEMENTED("VM: vmmap_create"); - return NULL; + // NOT_YET_IMPLEMENTED("VM: vmmap_create"); + + // Allocate a new vmmap + vmmap_t *new_vmmap = (vmmap_t *)slab_obj_alloc(vmmap_allocator); + if (new_vmmap == NULL) + { + return NULL; + } + + // Initialize the fields of the vmmap + list_init(&new_vmmap->vmm_list); + new_vmmap->vmm_proc = curproc; + + return new_vmmap; } /* @@ -60,7 +107,22 @@ vmmap_t *vmmap_create(void) */ void vmmap_destroy(vmmap_t **mapp) { - NOT_YET_IMPLEMENTED("VM: vmmap_destroy"); + // NOT_YET_IMPLEMENTED("VM: vmmap_destroy"); + + vmmap_t *map = *mapp; + + // Iterate through the list of vmareas and free each one + list_iterate(&(map)->vmm_list, vma, vmarea_t, vma_plink) + { + list_remove(&vma->vma_plink); + vmarea_free(vma); + } + + // Free the map + slab_obj_free(vmmap_allocator, map); + + // Set the map to NULL + *mapp = NULL; } /* @@ -70,7 +132,22 @@ void vmmap_destroy(vmmap_t **mapp) */ void vmmap_insert(vmmap_t *map, vmarea_t *new_vma) { - NOT_YET_IMPLEMENTED("VM: vmmap_insert*"); + // NOT_YET_IMPLEMENTED("VM: vmmap_insert*"); + + // iterate over the list of vmareas + list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink) + { + // if the new vmarea is after the current vmarea + if (vma->vma_start > new_vma->vma_end) + { + // insert the new vmarea before the current vmarea + list_insert_before(&vma->vma_plink, &new_vma->vma_plink); + return; + } + } + + // insert this map to the tail + list_insert_tail(&map->vmm_list, &new_vma->vma_plink); } /* @@ -90,7 +167,51 @@ void vmmap_insert(vmmap_t *map, vmarea_t *new_vma) */ ssize_t vmmap_find_range(vmmap_t *map, size_t npages, int dir) { - NOT_YET_IMPLEMENTED("VM: vmmap_find_range"); + // NOT_YET_IMPLEMENTED("VM: vmmap_find_range"); + + // case 1: dir is VMMAP_DIR_LOHI + if (dir == VMMAP_DIR_LOHI) + { + // iterate over the page numbers + size_t vfn = ADDR_TO_PN(USER_MEM_LOW); + while (vfn <= ADDR_TO_PN(USER_MEM_HIGH) - npages) + { + // Lookup the vmarea for this page number + vmarea_t *vma = vmmap_lookup(map, vfn); + + // if the vmarea is NULL, return the page number + if (vma == NULL) + { + return vfn; + } + + // if the vmarea is not NULL, set the page number to the end of the vmarea + vfn = vma->vma_end; + } + } + + // case 2: dir is VMMAP_DIR_HILO + else if (dir == VMMAP_DIR_HILO) + { + // iterate over the page numbers + size_t vfn = ADDR_TO_PN(USER_MEM_HIGH) - npages; + while (vfn >= ADDR_TO_PN(USER_MEM_LOW)) + { + // Lookup the vmarea for this page number + vmarea_t *vma = vmmap_lookup(map, vfn); + + // if the vmarea is NULL, return the page number + if (vma == NULL) + { + return vfn; + } + + // if the vmarea is not NULL, set the page number to the start of the vmarea + vfn = vma->vma_start - npages; + } + } + + // if no range exists, return -1 return -1; } @@ -100,7 +221,19 @@ ssize_t vmmap_find_range(vmmap_t *map, size_t npages, int dir) */ vmarea_t *vmmap_lookup(vmmap_t *map, size_t vfn) { - NOT_YET_IMPLEMENTED("VM: vmmap_lookup"); + // NOT_YET_IMPLEMENTED("VM: vmmap_lookup"); + + // iterate over the list of vmareas + list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink) + { + // if the vfn lies within the range of the current vmarea + if (vfn >= vma->vma_start && vfn < vma->vma_end) + { + return vma; + } + } + + // if the page is unmapped, return NULL return NULL; } @@ -140,8 +273,83 @@ void vmmap_collapse(vmmap_t *map) */ vmmap_t *vmmap_clone(vmmap_t *map) { - NOT_YET_IMPLEMENTED("VM: vmmap_clone"); - return NULL; + // NOT_YET_IMPLEMENTED("VM: vmmap_clone"); + + // Create a new vmmap + vmmap_t *new_vmmap = vmmap_create(); + if (new_vmmap == NULL) + { + return NULL; + } + + // Iterate over the list of vmareas + list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink) + { + // Create a new vmarea + vmarea_t *new_vmarea = vmarea_alloc(); + if (new_vmarea == NULL) + { + vmmap_destroy(&new_vmmap); + return NULL; + } + + // Clone the fields of the vmarea + new_vmarea->vma_start = vma->vma_start; + new_vmarea->vma_end = vma->vma_end; + new_vmarea->vma_off = vma->vma_off; + new_vmarea->vma_prot = vma->vma_prot; + new_vmarea->vma_flags = vma->vma_flags; + + // If the vmarea is share-mapped + if (vma->vma_flags & MAP_SHARED) + { + new_vmarea->vma_obj = vma->vma_obj; + mobj_ref(new_vmarea->vma_obj); + } + + // If the vmarea is not share-mapped + else + { + // Create two shadow objects + mobj_lock(vma->vma_obj); + mobj_t *shadow_obj_map = shadow_create(vma->vma_obj); + mobj_unlock(vma->vma_obj); + + mobj_unlock(shadow_obj_map); // unlock the map before use + if (shadow_obj_map == NULL) + { + vmarea_free(new_vmarea); + vmmap_destroy(&new_vmmap); + return NULL; + } + + mobj_lock(vma->vma_obj); + mobj_t *shadow_obj_new = shadow_create(vma->vma_obj); + mobj_unlock(vma->vma_obj); + + mobj_unlock(shadow_obj_new); // unlock the new before use + if (shadow_obj_new == NULL) + { + mobj_put(&shadow_obj_map); + vmarea_free(new_vmarea); + vmmap_destroy(&new_vmmap); + return NULL; + } + + // Put the original vma_obj + mobj_put(&vma->vma_obj); + + // Insert the shadow objects into their respective vmareas + new_vmarea->vma_obj = shadow_obj_new; + vma->vma_obj = shadow_obj_map; + } + + // Insert the new vmarea into the new vmmap + vmmap_insert(new_vmmap, new_vmarea); + } + + // Return the new vmmap + return new_vmmap; } /* @@ -182,8 +390,98 @@ vmmap_t *vmmap_clone(vmmap_t *map) long vmmap_map(vmmap_t *map, vnode_t *file, size_t lopage, size_t npages, int prot, int flags, off_t off, int dir, vmarea_t **new_vma) { - NOT_YET_IMPLEMENTED("VM: vmmap_map"); - return -1; + // NOT_YET_IMPLEMENTED("VM: vmmap_map"); + + // Create a new vmarea + // see if lopage is 0. if so, use vmmap_find_range() to get a valid range + if (lopage == 0) + { + lopage = vmmap_find_range(map, npages, dir); + if (lopage == -1) + { + return -ENOMEM; + } + } + + // Alloc the new vmarea + vmarea_t *new_vmarea = vmarea_alloc(); + if (new_vmarea == NULL) + { + return -ENOMEM; + } + // Set the fields of the new vmarea + new_vmarea->vma_start = lopage; + new_vmarea->vma_end = lopage + npages; + new_vmarea->vma_off = ADDR_TO_PN(off); + new_vmarea->vma_prot = prot; + new_vmarea->vma_flags = flags; + new_vmarea->vma_vmmap = map; + new_vmarea->vma_obj = NULL; + + // If file is NULL, create an anon object + if (file == NULL) + { + new_vmarea->vma_obj = anon_create(); + mobj_unlock(new_vmarea->vma_obj); // unlock the anon object before use + if (new_vmarea->vma_obj == NULL) + { + vmarea_free(new_vmarea); + return -ENOMEM; + } + } + else + { + // If file is non-NULL, use the vnode's mmap operation to get the mobj + long ret = file->vn_ops->mmap(file, &new_vmarea->vma_obj); + if (ret < 0) + { + // on fail, free the new vmarea and return the error + vmarea_free(new_vmarea); + return ret; + } + } + + // If MAP_PRIVATE is specified, set up a shadow object + if (flags & MAP_PRIVATE) + { + mobj_lock(new_vmarea->vma_obj); + mobj_t *shadow_obj = shadow_create(new_vmarea->vma_obj); + mobj_unlock(new_vmarea->vma_obj); + mobj_unlock(shadow_obj); // unlock the shadow object before use + if (shadow_obj == NULL) + { + vmarea_free(new_vmarea); + return -ENOMEM; + } + new_vmarea->vma_obj = shadow_obj; + } + + // If MAP_FIXED is specified and the given range overlaps with any preexisting mappings, remove the preexisting mappings + if (lopage != 0 && (flags & MAP_FIXED)) + { + long ret = vmmap_remove(map, lopage, npages); + if (ret < 0) + { + vmarea_free(new_vmarea); + // remove/put the shadow/annon object if it exists + if (new_vmarea->vma_obj) + { + mobj_put(&new_vmarea->vma_obj); // FIXME: is this correct! + } + + return ret; + } + } + + // Insert the new vmarea into the map + vmmap_insert(map, new_vmarea); + + // set ret val and return 0 + if (new_vma) + { + *new_vma = new_vmarea; + } + return 0; } /* @@ -219,8 +517,107 @@ long vmmap_map(vmmap_t *map, vnode_t *file, size_t lopage, size_t npages, */ long vmmap_remove(vmmap_t *map, size_t lopage, size_t npages) { - NOT_YET_IMPLEMENTED("VM: vmmap_remove"); - return -1; + // NOT_YET_IMPLEMENTED("VM: vmmap_remove"); + + // Iterate over the list of vmareas + list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink) + { + // if the vmarea is completely inside the region to be unmapped + if (vma->vma_start < lopage && vma->vma_end > lopage + npages) + { + // split the old vmarea into two vmareas + vmarea_t *new_vmarea = vmarea_alloc(); + if (new_vmarea == NULL) + { + return -ENOMEM; + } + + // Set the fields of the new vmarea + new_vmarea->vma_start = lopage + npages; + new_vmarea->vma_end = vma->vma_end; + new_vmarea->vma_off = vma->vma_off + (new_vmarea->vma_start - vma->vma_start); + new_vmarea->vma_prot = vma->vma_prot; + new_vmarea->vma_flags = vma->vma_flags; + new_vmarea->vma_vmmap = map; + new_vmarea->vma_obj = vma->vma_obj; + // increment the refcount of the object associated with the vmarea + mobj_ref(new_vmarea->vma_obj); + + // Shorten the length of the old vmarea + vma->vma_end = lopage; + + // Insert the new vmarea into the map + vmmap_insert(map, new_vmarea); + + // call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB + pt_unmap_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage), + PN_TO_ADDR(lopage + npages) + ); + tlb_flush_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage + npages) - PN_TO_ADDR(lopage) + ); + } + + // if the region overlaps the end of the vmarea + else if (vma->vma_start < lopage && vma->vma_end > lopage && vma->vma_end <= lopage + npages) + { + // shorten the length of the mapping + vma->vma_end = lopage; + // call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB + pt_unmap_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage), + PN_TO_ADDR(lopage + npages) + ); + tlb_flush_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage + npages) - PN_TO_ADDR(lopage) + ); + } + + // if the region overlaps the beginning of the vmarea + else if (vma->vma_start >= lopage && vma->vma_end > lopage + npages && vma->vma_start < lopage + npages) + { + // move the beginning of the mapping and shorten its length + vma->vma_off += (lopage + npages - vma->vma_start); + vma->vma_start = lopage + npages; + + // call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB + pt_unmap_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage), + PN_TO_ADDR(lopage + npages) + ); + tlb_flush_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage + npages) - PN_TO_ADDR(lopage) + ); + } + + // if the region completely contains the vmarea + else if (vma->vma_start >= lopage && vma->vma_end <= lopage + npages) + { + // remove the vmarea from the list + list_remove(&vma->vma_plink); + vmarea_free(vma); + + // call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB + pt_unmap_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage), + PN_TO_ADDR(lopage + npages) + ); + tlb_flush_range( + map->vmm_proc->p_pml4, + PN_TO_ADDR(lopage + npages) - PN_TO_ADDR(lopage) + ); + } + } + + return 0; } /* @@ -229,8 +626,29 @@ long vmmap_remove(vmmap_t *map, size_t lopage, size_t npages) */ long vmmap_is_range_empty(vmmap_t *map, size_t startvfn, size_t npages) { - NOT_YET_IMPLEMENTED("VM: vmmap_is_range_empty"); - return 0; + // NOT_YET_IMPLEMENTED("VM: vmmap_is_range_empty"); + + // Iterate over the list of vmareas + list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink) + { + // if the range completely contains the vmarea + if (vma->vma_start <= startvfn && vma->vma_end >= startvfn + npages) + { + return 0; + } + // if the start of the vmarea is greater than or equal to the start of the range + if (vma->vma_start < startvfn + npages && vma->vma_start >= startvfn) + { + return 0; + } + // check if the end of the vmarea is greater than the start of the range + if (vma->vma_end > startvfn && vma->vma_end <= startvfn + npages) + { + return 0; + } + } + + return 1; } /* @@ -250,7 +668,55 @@ long vmmap_is_range_empty(vmmap_t *map, size_t startvfn, size_t npages) */ long vmmap_read(vmmap_t *map, const void *vaddr, void *buf, size_t count) { - NOT_YET_IMPLEMENTED("VM: vmmap_read"); + // NOT_YET_IMPLEMENTED("VM: vmmap_read"); + + // Iterate over the page numbers + size_t vfn = ADDR_TO_PN(vaddr); + size_t end_vfn = ADDR_TO_PN(vaddr + count); + size_t bytes_read = 0; + while (vfn < end_vfn) + { + // Lookup the vmarea for this page number + vmarea_t *vma = vmmap_lookup(map, vfn); + if (vma == NULL) + { + return -EFAULT; + } + + // Find the pframe for this page number + pframe_t *pf; + mobj_lock(vma->vma_obj); + long ret = mobj_get_pframe(vma->vma_obj, vfn - vma->vma_start + vma->vma_off, 0, &pf); + mobj_unlock(vma->vma_obj); + if (ret < 0) + { + return ret; + } + + // Read from the pframe and copy it into buf + void *cursor = (void *)(bytes_read + vaddr); + size_t bytes_this_iteration = MIN(PAGE_SIZE - PAGE_OFFSET(cursor), count - bytes_read); + memcpy( + buf + bytes_read, + (void *)pf->pf_addr + PAGE_OFFSET(cursor), + bytes_this_iteration + ); + + // Unlock the pframe + pframe_release(&pf); + + // Increment the bytes read + bytes_read += bytes_this_iteration; + + // check if we have read enough + if (bytes_read >= count) + { + return 0; + } + + // Increment the page number + vfn++; + } return 0; } @@ -272,7 +738,59 @@ long vmmap_read(vmmap_t *map, const void *vaddr, void *buf, size_t count) */ long vmmap_write(vmmap_t *map, void *vaddr, const void *buf, size_t count) { - NOT_YET_IMPLEMENTED("VM: vmmap_write"); + // NOT_YET_IMPLEMENTED("VM: vmmap_write"); + + // Iterate over the page numbers + size_t vfn = ADDR_TO_PN(vaddr); + size_t end_vfn = ADDR_TO_PN(vaddr + count); + size_t bytes_written = 0; + while(vfn < end_vfn) + { + // Lookup the vmarea for this page number + vmarea_t *vma = vmmap_lookup(map, vfn); + if (vma == NULL) + { + return -EFAULT; + } + + // Find the pframe for this page number + pframe_t *pf; + mobj_lock(vma->vma_obj); + long ret = mobj_get_pframe(vma->vma_obj, vfn - vma->vma_start + vma->vma_off, 1, &pf); + mobj_unlock(vma->vma_obj); + if (ret < 0) + { + return ret; + } + + // Write to the pframe, copying data from buf + void *cursor = (void *)(bytes_written + vaddr); + size_t bytes_this_iteration = MIN(PAGE_SIZE - PAGE_OFFSET(cursor), count - bytes_written); + memcpy( + (void *)pf->pf_addr + PAGE_OFFSET(cursor), + buf + bytes_written, + bytes_this_iteration + ); + + // Dirty the page + pf->pf_dirty = 1; + + // Unlock the pframe + pframe_release(&pf); + + // Increment the bytes written + bytes_written += bytes_this_iteration; + + // check if we have written enough + if (bytes_written >= count) + { + return 0; + } + + // Increment the page number + vfn++; + } + return 0; } |
