G2A Many GEOs
SHARE
TWEET

adderspace.c

a guest Apr 6th, 2020 249 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <types.h>
  2. #include <kern/errno.h>
  3. #include <lib.h>
  4. #include <addrspace.h>
  5. #include <vm.h>
  6. #include <machine/tlb.h>
  7. #include <elf.h>
  8. /*
  9.  * Note! If OPT_DUMBVM is set, as is the case until you start the VM
  10.  * assignment, this file is not compiled or linked or in any way
  11.  * used. The cheesy hack versions in dumbvm.c are used instead.
  12.  */
  13.  
  14. extern size_t numofpgs;
  15. extern struct Coremap_struct *Coremap;
  16.  
  17.  
  18. struct addrspace *
  19. as_create(void)
  20. {
  21.     struct addrspace *as = kmalloc(sizeof(struct addrspace));
  22.     if (as==NULL) {
  23.         return NULL;
  24.     }
  25.  
  26.     /*
  27.      * Initialize as needed.
  28.      */
  29.     as->as_regions = array_create();
  30.     as->start_heap = 0;
  31.     as->end_heap = 0;
  32.    
  33.     int i;
  34.     for (i = 0; i < PT_SIZE; i++){
  35.         as->as_ptes[i] = NULL;
  36.     }
  37.  
  38.     return as;
  39. }
  40.  
  41. int
  42. as_copy(struct addrspace *old, struct addrspace **ret)
  43. {
  44.     int spl = splhigh();
  45.     struct addrspace *newas;
  46.  
  47.     newas = as_create();
  48.     if (newas==NULL) {
  49.         return ENOMEM;
  50.     }
  51.  
  52.     //COPYING REGIONS**************************************************************************
  53.  
  54.     unsigned int i;
  55.     for (i = 0; i < array_getnum(old->as_regions); i++) {
  56.         struct as_region* copy = kmalloc(sizeof(struct as_region));
  57.         *copy = *((struct as_region*)array_getguy(old->as_regions, i));// not sure if these too lines fully copy stuff
  58.         array_add(newas->as_regions, copy);
  59.     }
  60.  
  61.     //COPYING HEAP*********************************************************************************
  62.  
  63.     newas->start_heap = old->start_heap;
  64.     newas->end_heap = old->end_heap;
  65.     newas->permissions = old->permissions;
  66. //is there anything im missing??? right now im copying the last 12 bits.. 1 (startbit) + 1(endbit) + 10(permission) = 12
  67.  
  68.     //COPYING PAGE TABLE ENTRIES PTE*********************************************************************
  69.  
  70.     int j;
  71.     for (j = 0; j < 1024; j++) {
  72.         if(old->as_ptes[j] != NULL) {
  73.             newas->as_ptes[j] = (struct as_pagetable*) kmalloc(sizeof(struct as_pagetable));
  74.             struct as_pagetable *src = old->as_ptes[i];
  75.             struct as_pagetable *copying = newas->as_ptes[i];
  76.             int k;
  77.             for (k = 0; k < 1024; k++) {
  78.                 copying->PTE[k] = 0;
  79.                 if(src->PTE[k] & 0x00000800) { 
  80.                     paddr_t src_paddr = (src->PTE[k] & PAGE_FRAME);
  81.                     vaddr_t dest_vaddr = (j << 22) + (k << 12);
  82.                     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
  83.                     memmove((void *) PADDR_TO_KVADDR(dest_paddr),
  84.                     (const void*) PADDR_TO_KVADDR(src_paddr), PAGE_SIZE);
  85.                     copying->PTE[k] |= dest_paddr;
  86.                     copying->PTE[k] |= 0x00000800;
  87.                 }
  88.                 else{
  89.                     copying->PTE[k] = 0;
  90.                 }
  91.             }
  92.         }
  93.         else{
  94.             newas->as_ptes[j] = NULL;
  95.         }
  96.     }
  97.                
  98.     //(void)old;
  99.    
  100.     *ret = newas;
  101.     splx(spl);
  102.     return 0;
  103. }
  104.  
  105.  
  106. paddr_t alloc_page_userspace(struct addrspace * as, vaddr_t vir_as) {
  107.  
  108.     int id;
  109.     int i;
  110.     for (i = 0; i < numofpgs; i++) {
  111.         if (Coremap[i].state == 0){
  112.             id = i;
  113.             break;
  114.         }
  115.     }
  116.     if(as == NULL)
  117.         Coremap[id].addspace = curthr->t_vmspace;
  118.     else
  119.         Coremap[id].addspace = as;
  120.  
  121.     Coremap[id].state = 2;
  122.     Coremap[id].vir_addspace = vir_as;
  123.     Coremap[id].length = 1;
  124.  
  125.     return Coremap[id].phy_addspace;
  126. }
  127.  
  128.  
  129. void
  130. as_destroy(struct addrspace *as)
  131. {
  132.     /*
  133.      * Clean up as needed.
  134.      */
  135.     int spl = splhigh();
  136.     int i;
  137.     for (i = 0; i < numofpgs; i++) {
  138.         if(Coremap[i].state != 0 && Coremap[i].addspace == as){
  139.             Coremap[i].addspace = NULL;
  140.             Coremap[i].vir_addspace = 0;
  141.             Coremap[i].state = 0;
  142.             Coremap[i].length = 0;
  143.         }
  144.     }
  145.  
  146.     array_destroy(as->as_regions);
  147.    
  148.     for(i = 0; i < 1024; i++) {
  149.         if(as->as_ptes[i] != NULL)
  150.             kfree(as->as_ptes[i]);
  151.     }
  152.    
  153.     kfree(as);
  154.     splx(spl);
  155. }
  156.  
  157. void
  158. as_activate(struct addrspace *as)
  159. {
  160.     /*
  161.      * Write this.
  162.      */
  163.  
  164.     (void)as;  // suppress warning until code gets written
  165. }
  166.  
  167. /*
  168.  * Set up a segment at virtual address VADDR of size MEMSIZE. The
  169.  * segment in memory extends from VADDR up to (but not including)
  170.  * VADDR+MEMSIZE.
  171.  *
  172.  * The READABLE, WRITEABLE, and EXECUTABLE flags are set if read,
  173.  * write, or execute permission should be set on the segment. At the
  174.  * moment, these are ignored. When you write the VM system, you may
  175.  * want to implement them.
  176.  */
  177. int
  178. as_define_region(struct addrspace *as, vaddr_t vaddr, size_t sz,
  179.          int readable, int writeable, int executable)
  180. {
  181.     /*
  182.      * Write this.
  183.      */
  184.  
  185.     (void)as;
  186.     (void)vaddr;
  187.     (void)sz;
  188.     (void)readable;
  189.     (void)writeable;
  190.     (void)executable;
  191.     return EUNIMP;
  192. }
  193.  
  194. int
  195. as_prepare_load(struct addrspace *as)
  196. {
  197.     /*
  198.      * Write this.
  199.      */
  200.  
  201.     (void)as;
  202.     return 0;
  203. }
  204.  
  205. int
  206. as_complete_load(struct addrspace *as)
  207. {
  208.     /*
  209.      * Write this.
  210.      */
  211.  
  212.     (void)as;
  213.     return 0;
  214. }
  215.  
  216. int
  217. as_define_stack(struct addrspace *as, vaddr_t *stackptr)
  218. {
  219.     /*
  220.      * Write this.
  221.      */
  222.  
  223.     (void)as;
  224.  
  225.     /* Initial user-level stack pointer */
  226.     *stackptr = USERSTACK;
  227.    
  228.     return 0;
  229. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top