Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


restructuring of shadow paging implementations
[palacios.git] / palacios / src / palacios / mmu / vmm_shdw_pg_kvm_32.h
similarity index 73%
rename from palacios/src/palacios/vmm_shdw_pg_tlb_32.h
rename to palacios/src/palacios/mmu/vmm_shdw_pg_kvm_32.h
index 3958250..9aa89ea 100644 (file)
  */
 
 
-
+#ifdef CONFIG_SHADOW_CACHE
 
 static inline int activate_shadow_pt_32(struct guest_info * core) {
     struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(core->ctrl_regs.cr3);
     struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(core->shdw_pg_state.guest_cr3);
-    struct shadow_page_data * shdw_page = create_new_shadow_pt(core);
+
+    struct shadow_page_cache_data * shdw_page;
+
+    if (core->n_free_shadow_pages < MIN_FREE_SHADOW_PAGES) {   
+       shadow_free_some_pages(core);
+    }
+
+    shdw_page = shadow_page_get_page(core, (addr_t)(guest_cr3->pdt_base_addr), 2, 0, 0, 0, 0);
+    PrintDebug("act shdw pt: gcr3 %p\n",(void *)BASE_TO_PAGE_ADDR(guest_cr3->pdt_base_addr));
 
     shdw_page->cr3 = shdw_page->page_pa;
     
@@ -46,17 +54,99 @@ static inline int activate_shadow_pt_32(struct guest_info * core) {
  * *
  * *
  */
-static int handle_4MB_shadow_pagefault_32(struct guest_info * info,  addr_t fault_addr, pf_error_t error_code, 
+
+static inline int fix_read_pf_32(pte32_t * shadow_pte, uint_t vmm_info) {
+    PrintDebug("\trdipf, start vmm_info %d\n",vmm_info);
+    if((vmm_info & PT_USER_MASK) && !(shadow_pte->user_page)) {
+       shadow_pte->user_page = 1;
+       shadow_pte->writable = 0;
+       return 1;
+    }  
+    return 0;
+}
+
+static inline int fix_write_pf_32(struct guest_info * core, pte32_t * shadow_pte, pte32_t * guest_pte, 
+    int user, int * write_pt, addr_t guest_fn, uint_t vmm_info) {
+       
+    int writable_shadow;
+    struct cr0_32 * guest_cr0;
+    struct shadow_page_cache_data * page;
+    *write_pt = 0;
+
+    PrintDebug("\twripf, start vmm_info %d\n",vmm_info);
+       
+    if(shadow_pte->writable) {
+       return 0;
+    }
+
+    PrintDebug("\twrpf: pass writable\n");
+       
+    writable_shadow = vmm_info & PT_WRITABLE_MASK;
+
+    PrintDebug("\twrpf: writable_shadow %d\n",writable_shadow);
+       
+    if(user) {
+       if(!(vmm_info & PT_USER_MASK) || !writable_shadow) {
+           PrintDebug("\twrpf: 1st usr chk\n");
+           return 0;
+       }
+    } else {
+       if(!writable_shadow) {
+           guest_cr0 = (struct cr0_32 *)&(core->shdw_pg_state.guest_cr0);
+           PrintDebug("\twrpf: wp %d\n",guest_cr0->wp);
+
+           if (guest_cr0->wp) {
+               return 0;
+           }
+           shadow_pte->user_page = 0;                          
+       }
+    }
+                       
+    if (guest_pte->present == 0) { 
+       memset((void *)shadow_pte, 0, sizeof(struct pte32));
+       PrintDebug("\twrpf: guest non present\n");
+       return 0;
+    }
+
+    if (user) {
+       while ((page = shadow_page_lookup_page(core, guest_fn, 0)) != NULL) {
+           shadow_zap_page(core,page);
+       }
+
+       PrintDebug("\twrpf: zap\n");
+
+    } else if((page = shadow_page_lookup_page(core, guest_fn,0))  != NULL)  {
+       if ((page = shadow_page_lookup_page(core, guest_fn,0)) != NULL) {
+           guest_pte->dirty = 1;
+           *write_pt = 1;
+           PrintDebug("\twrpf: write need\n");
+           return 0;
+       }
+    }  
+
+    shadow_pte->writable = 1; //will lost every pt update trap not sure for this
+    guest_pte->dirty = 1;      
+       
+    rmap_add(core, (addr_t)shadow_pte);
+       
+    PrintDebug("\twrpf: on writable\n");
+    return 1;
+}
+
+#endif
+
+
+static int handle_4MB_shadow_pagefault_32(struct guest_info * core,  addr_t fault_addr, pf_error_t error_code, 
                                          pte32_t * shadow_pt, pde32_4MB_t * large_guest_pde);
 
-static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
+static int handle_pte_shadow_pagefault_32(struct guest_info * core, addr_t fault_addr, pf_error_t error_code,
                                          pte32_t * shadow_pt,  pte32_t * guest_pt);
 
 
-static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
+static inline int handle_shadow_pagefault_32(struct guest_info * core, addr_t fault_addr, pf_error_t error_code) {
     pde32_t * guest_pd = NULL;
-    pde32_t * shadow_pd = CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
-    addr_t guest_cr3 = CR3_TO_PDE32_PA(info->shdw_pg_state.guest_cr3);
+    pde32_t * shadow_pd = CR3_TO_PDE32_VA(core->ctrl_regs.cr3);
+    addr_t guest_cr3 = CR3_TO_PDE32_PA(core->shdw_pg_state.guest_cr3);
     pt_access_status_t guest_pde_access;
     pt_access_status_t shadow_pde_access;
     pde32_t * guest_pde = NULL;
@@ -65,7 +155,7 @@ static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fa
     PrintDebug("Shadow page fault handler: %p\n", (void*) fault_addr );
     PrintDebug("Handling PDE32 Fault\n");
 
-    if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
+    if (guest_pa_to_host_va(core, guest_cr3, (addr_t*)&guest_pd) == -1) {
        PrintError("Invalid Guest PDE Address: 0x%p\n",  (void *)guest_cr3);
        return -1;
     } 
@@ -83,7 +173,7 @@ static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fa
     if (v3_is_guest_pf(guest_pde_access, shadow_pde_access) == 1) {
        PrintDebug("Injecting PDE pf to guest: (guest access error=%d) (shdw access error=%d)  (pf error code=%d)\n", 
                   *(uint_t *)&guest_pde_access, *(uint_t *)&shadow_pde_access, *(uint_t *)&error_code);
-       if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
+       if (v3_inject_guest_pf(core, fault_addr, error_code) == -1) {
            PrintError("Could not inject guest page fault for vaddr %p\n", (void *)fault_addr);
            return -1;
        }
@@ -99,7 +189,7 @@ static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fa
        PrintDebug("Shadow Paging User access error (shadow_pde_access=0x%x, guest_pde_access=0x%x)\n", 
                   shadow_pde_access, guest_pde_access);
        
-       if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
+       if (v3_inject_guest_pf(core, fault_addr, error_code) == -1) {
            PrintError("Could not inject guest page fault for vaddr %p\n", (void *)fault_addr);
            return -1;
        }
@@ -113,7 +203,7 @@ static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fa
     } else if ((shadow_pde_access != PT_ACCESS_NOT_PRESENT) &&
               (shadow_pde_access != PT_ACCESS_OK)) {
        // inject page fault in guest
-       if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
+       if (v3_inject_guest_pf(core, fault_addr, error_code) == -1) {
            PrintError("Could not inject guest page fault for vaddr %p\n", (void *)fault_addr);
            return -1;
        }
@@ -129,7 +219,7 @@ static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fa
     // Get the next shadow page level, allocate if not present
 
     if (shadow_pde_access == PT_ACCESS_NOT_PRESENT) {
-       struct shadow_page_data * shdw_page =  create_new_shadow_pt(info);
+       struct shadow_page_data * shdw_page =  create_new_shadow_pt(core);
        shadow_pt = (pte32_t *)V3_VAddr((void *)shdw_page->page_pa);
 
        shadow_pde->present = 1;
@@ -171,19 +261,19 @@ static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fa
 
       
     if (guest_pde->large_page == 0) {
-       if (guest_pa_to_host_va(info, BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr), (addr_t*)&guest_pt) == -1) {
+       if (guest_pa_to_host_va(core, BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr), (addr_t*)&guest_pt) == -1) {
            // Machine check the guest
            PrintDebug("Invalid Guest PTE Address: 0x%p\n", (void *)BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr));
-           v3_raise_exception(info, MC_EXCEPTION);
+           v3_raise_exception(core, MC_EXCEPTION);
            return 0;
        }
 
-       if (handle_pte_shadow_pagefault_32(info, fault_addr, error_code, shadow_pt, guest_pt)  == -1) {
+       if (handle_pte_shadow_pagefault_32(core, fault_addr, error_code, shadow_pt, guest_pt)  == -1) {
            PrintError("Error handling Page fault caused by PTE\n");
            return -1;
        }
     } else {
-       if (handle_4MB_shadow_pagefault_32(info, fault_addr, error_code, shadow_pt, (pde32_4MB_t *)guest_pde) == -1) {
+       if (handle_4MB_shadow_pagefault_32(core, fault_addr, error_code, shadow_pt, (pde32_4MB_t *)guest_pde) == -1) {
            PrintError("Error handling large pagefault\n");
            return -1;
        }       
@@ -193,7 +283,7 @@ static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fa
 }
 
 
-static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
+static int handle_pte_shadow_pagefault_32(struct guest_info * core, addr_t fault_addr, pf_error_t error_code,
                                          pte32_t * shadow_pt, pte32_t * guest_pt) {
 
     pt_access_status_t guest_pte_access;
@@ -202,12 +292,12 @@ static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault
     pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
     addr_t guest_pa = BASE_TO_PAGE_ADDR((addr_t)(guest_pte->page_base_addr)) +  PAGE_OFFSET(fault_addr);
 
-    struct v3_shadow_region * shdw_reg =  v3_get_shadow_region(info->vm_info, info->cpu_id, guest_pa);
+    struct v3_shadow_region * shdw_reg =  v3_get_shadow_region(core->vm_info, core->cpu_id, guest_pa);
 
     if (shdw_reg == NULL) {
        // Inject a machine check in the guest
        PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_pa);
-       v3_raise_exception(info, MC_EXCEPTION);
+       v3_raise_exception(core, MC_EXCEPTION);
        return 0;
     }
 
@@ -226,7 +316,7 @@ static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault
        
 
        //   inject:
-       if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
+       if (v3_inject_guest_pf(core, fault_addr, error_code) == -1) {
            PrintError("Could not inject guest page fault for vaddr %p\n", (void *)fault_addr);
            return -1;
        }       
@@ -250,7 +340,7 @@ static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault
 
        if ((shdw_reg->host_type == SHDW_REGION_ALLOCATED) ||
            (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK)) {
-           addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, info->cpu_id, guest_pa);
+           addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, core->cpu_id, guest_pa);
       
            shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
 
@@ -286,7 +376,7 @@ static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault
        } else {
            // Page fault handled by hook functions
 
-           if (v3_handle_mem_full_hook(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
+           if (v3_handle_mem_full_hook(core, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
                PrintError("Special Page fault handler returned error for address: %p\n",  (void *)fault_addr);
                return -1;
            }
@@ -295,7 +385,7 @@ static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault
        guest_pte->dirty = 1;
 
        if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
-           if (v3_handle_mem_wr_hook(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
+           if (v3_handle_mem_wr_hook(core, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
                PrintError("Special Page fault handler returned error for address: %p\n",  (void *)fault_addr);
                return -1;
            }
@@ -309,7 +399,7 @@ static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault
 
     } else {
        // Inject page fault into the guest     
-       if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
+       if (v3_inject_guest_pf(core, fault_addr, error_code) == -1) {
            PrintError("Could not inject guest page fault for vaddr %p\n", (void *)fault_addr);
            return -1;
        }
@@ -324,7 +414,7 @@ static int handle_pte_shadow_pagefault_32(struct guest_info * info, addr_t fault
 
 
 
-static int handle_4MB_shadow_pagefault_32(struct guest_info * info, 
+static int handle_4MB_shadow_pagefault_32(struct guest_info * core, 
                                     addr_t fault_addr, pf_error_t error_code, 
                                     pte32_t * shadow_pt, pde32_4MB_t * large_guest_pde) 
 {
@@ -336,13 +426,13 @@ static int handle_4MB_shadow_pagefault_32(struct guest_info * info,
     PrintDebug("Handling 4MB fault (guest_fault_pa=%p) (error_code=%x)\n", (void *)guest_fault_pa, *(uint_t*)&error_code);
     PrintDebug("ShadowPT=%p, LargeGuestPDE=%p\n", shadow_pt, large_guest_pde);
 
-    struct v3_shadow_region * shdw_reg = v3_get_shadow_region(info->vm_info, info->cpu_id, guest_fault_pa);
+    struct v3_shadow_region * shdw_reg = v3_get_shadow_region(core->vm_info, core->cpu_id, guest_fault_pa);
 
  
     if (shdw_reg == NULL) {
        // Inject a machine check in the guest
        PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_fault_pa);
-       v3_raise_exception(info, MC_EXCEPTION);
+       v3_raise_exception(core, MC_EXCEPTION);
        return -1;
     }
 
@@ -359,7 +449,7 @@ static int handle_4MB_shadow_pagefault_32(struct guest_info * info,
 
        if ((shdw_reg->host_type == SHDW_REGION_ALLOCATED) || 
            (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK)) {
-           addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, info->cpu_id, guest_fault_pa);
+           addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, core->cpu_id, guest_fault_pa);
 
            shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
 
@@ -387,7 +477,7 @@ static int handle_4MB_shadow_pagefault_32(struct guest_info * info,
            //
       
        } else {
-           if (v3_handle_mem_full_hook(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
+           if (v3_handle_mem_full_hook(core, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
                PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
                return -1;
            }
@@ -396,7 +486,7 @@ static int handle_4MB_shadow_pagefault_32(struct guest_info * info,
 
        if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
 
-           if (v3_handle_mem_wr_hook(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
+           if (v3_handle_mem_wr_hook(core, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
                PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
                return -1;
            }
@@ -424,15 +514,15 @@ static int handle_4MB_shadow_pagefault_32(struct guest_info * info,
 
 
 /* If we start to optimize we should look up the guest pages in the cache... */
-static inline int handle_shadow_invlpg_32(struct guest_info * info, addr_t vaddr) {
-    pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
+static inline int handle_shadow_invlpg_32(struct guest_info * core, addr_t vaddr) {
+    pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32_VA(core->ctrl_regs.cr3);
     pde32_t * shadow_pde = (pde32_t *)&shadow_pd[PDE32_INDEX(vaddr)];
 
-    addr_t guest_cr3 =  CR3_TO_PDE32_PA(info->shdw_pg_state.guest_cr3);
+    addr_t guest_cr3 =  CR3_TO_PDE32_PA(core->shdw_pg_state.guest_cr3);
     pde32_t * guest_pd = NULL;
     pde32_t * guest_pde;
 
-    if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
+    if (guest_pa_to_host_va(core, guest_cr3, (addr_t*)&guest_pd) == -1) {
        PrintError("Invalid Guest PDE Address: 0x%p\n",  (void *)guest_cr3);
        return -1;
     }
@@ -452,3 +542,5 @@ static inline int handle_shadow_invlpg_32(struct guest_info * info, addr_t vaddr
     }
     return 0;
 }
+
+#endif