Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <types.h>
- #include <kern/errno.h>
- #include <lib.h>
- #include <addrspace.h>
- #include <vm.h>
- #include <machine/tlb.h>
- #include <elf.h>
- /*
- * Note! If OPT_DUMBVM is set, as is the case until you start the VM
- * assignment, this file is not compiled or linked or in any way
- * used. The cheesy hack versions in dumbvm.c are used instead.
- */
- extern size_t numofpgs;
- extern struct Coremap_struct *Coremap;
- struct addrspace *
- as_create(void)
- {
- struct addrspace *as = kmalloc(sizeof(struct addrspace));
- if (as==NULL) {
- return NULL;
- }
- /*
- * Initialize as needed.
- */
- as->as_regions = array_create();
- as->start_heap = 0;
- as->end_heap = 0;
- int i;
- for (i = 0; i < PT_SIZE; i++){
- as->as_ptes[i] = NULL;
- }
- return as;
- }
- int
- as_copy(struct addrspace *old, struct addrspace **ret)
- {
- int spl = splhigh();
- struct addrspace *newas;
- newas = as_create();
- if (newas==NULL) {
- return ENOMEM;
- }
- //COPYING REGIONS**************************************************************************
- unsigned int i;
- for (i = 0; i < array_getnum(old->as_regions); i++) {
- struct as_region* copy = kmalloc(sizeof(struct as_region));
- *copy = *((struct as_region*)array_getguy(old->as_regions, i));// not sure if these too lines fully copy stuff
- array_add(newas->as_regions, copy);
- }
- //COPYING HEAP*********************************************************************************
- newas->start_heap = old->start_heap;
- newas->end_heap = old->end_heap;
- newas->permissions = old->permissions;
- //is there anything im missing??? right now im copying the last 12 bits.. 1 (startbit) + 1(endbit) + 10(permission) = 12
- //COPYING PAGE TABLE ENTRIES PTE*********************************************************************
- int j;
- for (j = 0; j < 1024; j++) {
- if(old->as_ptes[j] != NULL) {
- newas->as_ptes[j] = (struct as_pagetable*) kmalloc(sizeof(struct as_pagetable));
- struct as_pagetable *src = old->as_ptes[i];
- struct as_pagetable *copying = newas->as_ptes[i];
- int k;
- for (k = 0; k < 1024; k++) {
- copying->PTE[k] = 0;
- if(src->PTE[k] & 0x00000800) {
- paddr_t src_paddr = (src->PTE[k] & PAGE_FRAME);
- vaddr_t dest_vaddr = (j << 22) + (k << 12);
- paddr_t dest_paddr = alloc_page_userspace(newas, dest_vaddr);// this function is at the bottom but i havent defined it yet as idk where i shd do it
- memmove((void *) PADDR_TO_KVADDR(dest_paddr),
- (const void*) PADDR_TO_KVADDR(src_paddr), PAGE_SIZE);
- copying->PTE[k] |= dest_paddr;
- copying->PTE[k] |= 0x00000800;
- }
- else{
- copying->PTE[k] = 0;
- }
- }
- }
- else{
- newas->as_ptes[j] = NULL;
- }
- }
- //(void)old;
- *ret = newas;
- splx(spl);
- return 0;
- }
- paddr_t alloc_page_userspace(struct addrspace * as, vaddr_t vir_as) {
- int id;
- int i;
- for (i = 0; i < numofpgs; i++) {
- if (Coremap[i].state == 0){
- id = i;
- break;
- }
- }
- if(as == NULL)
- Coremap[id].addspace = curthr->t_vmspace;
- else
- Coremap[id].addspace = as;
- Coremap[id].state = 2;
- Coremap[id].vir_addspace = vir_as;
- Coremap[id].length = 1;
- return Coremap[id].phy_addspace;
- }
- void
- as_destroy(struct addrspace *as)
- {
- /*
- * Clean up as needed.
- */
- int spl = splhigh();
- int i;
- for (i = 0; i < numofpgs; i++) {
- if(Coremap[i].state != 0 && Coremap[i].addspace == as){
- Coremap[i].addspace = NULL;
- Coremap[i].vir_addspace = 0;
- Coremap[i].state = 0;
- Coremap[i].length = 0;
- }
- }
- array_destroy(as->as_regions);
- for(i = 0; i < 1024; i++) {
- if(as->as_ptes[i] != NULL)
- kfree(as->as_ptes[i]);
- }
- kfree(as);
- splx(spl);
- }
- void
- as_activate(struct addrspace *as)
- {
- /*
- * Write this.
- */
- (void)as; // suppress warning until code gets written
- }
- /*
- * Set up a segment at virtual address VADDR of size MEMSIZE. The
- * segment in memory extends from VADDR up to (but not including)
- * VADDR+MEMSIZE.
- *
- * The READABLE, WRITEABLE, and EXECUTABLE flags are set if read,
- * write, or execute permission should be set on the segment. At the
- * moment, these are ignored. When you write the VM system, you may
- * want to implement them.
- */
- int
- as_define_region(struct addrspace *as, vaddr_t vaddr, size_t sz,
- int readable, int writeable, int executable)
- {
- /*
- * Write this.
- */
- (void)as;
- (void)vaddr;
- (void)sz;
- (void)readable;
- (void)writeable;
- (void)executable;
- return EUNIMP;
- }
- int
- as_prepare_load(struct addrspace *as)
- {
- /*
- * Write this.
- */
- (void)as;
- return 0;
- }
- int
- as_complete_load(struct addrspace *as)
- {
- /*
- * Write this.
- */
- (void)as;
- return 0;
- }
- int
- as_define_stack(struct addrspace *as, vaddr_t *stackptr)
- {
- /*
- * Write this.
- */
- (void)as;
- /* Initial user-level stack pointer */
- *stackptr = USERSTACK;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement