X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvm_guest_mem.c;h=caf8a8985ecb9175d136444e1cd03b5d8425d073;hb=1f9fa9cd9662a600f49a3b12fcc02fd2ffca93c8;hp=3f6be1e12563527510f96f33bedc0a9f78522960;hpb=36c1666cf64c49302d906a79d8cafbf760452d86;p=palacios.git diff --git a/palacios/src/palacios/vm_guest_mem.c b/palacios/src/palacios/vm_guest_mem.c index 3f6be1e..caf8a89 100644 --- a/palacios/src/palacios/vm_guest_mem.c +++ b/palacios/src/palacios/vm_guest_mem.c @@ -28,14 +28,14 @@ extern struct v3_os_hooks * os_hooks; /* GROUP 0 */ /**********************************/ -int host_va_to_host_pa(addr_t host_va, addr_t * host_pa) { +int v3_hva_to_hpa(addr_t hva, addr_t * hpa) { if ((os_hooks) && (os_hooks)->vaddr_to_paddr) { - *host_pa = (addr_t)(os_hooks)->vaddr_to_paddr((void *)host_va); + *hpa = (addr_t)(os_hooks)->vaddr_to_paddr((void *)hva); - if (*host_pa == 0) { + if (*hpa == 0) { PrintError("In HVA->HPA: Invalid HVA(%p)->HPA lookup\n", - (void *)host_va); + (void *)hva); return -1; } } else { @@ -46,14 +46,14 @@ int host_va_to_host_pa(addr_t host_va, addr_t * host_pa) { } -int host_pa_to_host_va(addr_t host_pa, addr_t * host_va) { +int v3_hpa_to_hva(addr_t hpa, addr_t * hva) { if ((os_hooks) && (os_hooks)->paddr_to_vaddr) { - *host_va = (addr_t)(os_hooks)->paddr_to_vaddr((void *)host_pa); + *hva = (addr_t)(os_hooks)->paddr_to_vaddr((void *)hpa); - if (*host_va == 0) { + if (*hva == 0) { PrintError("In HPA->HVA: Invalid HPA(%p)->HVA lookup\n", - (void *)host_pa); + (void *)hpa); return -1; } } else { @@ -63,24 +63,23 @@ int host_pa_to_host_va(addr_t host_pa, addr_t * host_va) { return 0; } +int v3_gpa_to_hpa(struct guest_info * info, addr_t gpa, addr_t * hpa) { + struct v3_mem_region * reg = v3_get_mem_region(info->vm_info, info->vcpu_id, gpa); - -int guest_pa_to_host_pa(struct guest_info * info, addr_t guest_pa, addr_t * host_pa) { - struct v3_shadow_region * shdw_reg = v3_get_shadow_region(info->vm_info, info->cpu_id, guest_pa); - - if (shdw_reg == NULL) { + if (reg == NULL) { PrintError("In GPA->HPA: Could not find address in shadow map (addr=%p) (NULL REGION)\n", - (void *)guest_pa); + (void *)gpa); return -1; } - if (shdw_reg->host_type == SHDW_REGION_FULL_HOOK) { - PrintError("In GPA->HPA: Could not find address in shadow map (addr=%p) (reg_type=%s)\n", - (void *)guest_pa, v3_shdw_region_type_to_str(shdw_reg->host_type)); + if (reg->flags.alloced == 0) { + //PrintError("In GPA->HPA: Tried to translate physical address of non allocated page (addr=%p)\n", + // (void *)gpa); + //v3_print_mem_map(info->vm_info); return -1; } - - *host_pa = v3_get_shadow_addr(shdw_reg, info->cpu_id, guest_pa); + + *hpa = (gpa - reg->guest_start) + reg->host_addr; return 0; } @@ -90,8 +89,8 @@ int guest_pa_to_host_pa(struct guest_info * info, addr_t guest_pa, addr_t * host // This is a scan of the shadow map // For now we ignore it // -int host_pa_to_guest_pa(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_pa) { - *guest_pa = 0; +int v3_hpa_to_gpa(struct guest_info * guest_info, addr_t hpa, addr_t * gpa) { + *gpa = 0; PrintError("ERROR!!! HPA->GPA currently not implemented!!!\n"); return -1; @@ -105,20 +104,20 @@ int host_pa_to_guest_pa(struct guest_info * guest_info, addr_t host_pa, addr_t * /* !! Currently not implemented !! */ -// This will return negative until we implement host_pa_to_guest_pa() -int host_va_to_guest_pa(struct guest_info * guest_info, addr_t host_va, addr_t * guest_pa) { - addr_t host_pa = 0; - *guest_pa = 0; +// This will return negative until we implement hpa_to_guest_pa() +int v3_hva_to_gpa(struct guest_info * guest_info, addr_t hva, addr_t * gpa) { + addr_t hpa = 0; + *gpa = 0; - if (host_va_to_host_pa(host_va, &host_pa) != 0) { + if (v3_hva_to_hpa(hva, &hpa) != 0) { PrintError("In HVA->GPA: Invalid HVA(%p)->HPA lookup\n", - (void *)host_va); + (void *)hva); return -1; } - if (host_pa_to_guest_pa(guest_info, host_pa, guest_pa) != 0) { + if (v3_hpa_to_gpa(guest_info, hpa, gpa) != 0) { PrintError("In HVA->GPA: Invalid HPA(%p)->GPA lookup\n", - (void *)host_pa); + (void *)hpa); return -1; } @@ -128,20 +127,20 @@ int host_va_to_guest_pa(struct guest_info * guest_info, addr_t host_va, addr_t * -int guest_pa_to_host_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * host_va) { - addr_t host_pa = 0; +int v3_gpa_to_hva(struct guest_info * guest_info, addr_t gpa, addr_t * hva) { + addr_t hpa = 0; - *host_va = 0; + *hva = 0; - if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) { - PrintError("In GPA->HVA: Invalid GPA(%p)->HPA lookup\n", - (void *)guest_pa); + if (v3_gpa_to_hpa(guest_info, gpa, &hpa) != 0) { + // PrintError("In GPA->HVA: Invalid GPA(%p)->HPA lookup\n", + // (void *)gpa); return -1; } - if (host_pa_to_host_va(host_pa, host_va) != 0) { + if (v3_hpa_to_hva(hpa, hva) != 0) { PrintError("In GPA->HVA: Invalid HPA(%p)->HVA lookup\n", - (void *)host_pa); + (void *)hpa); return -1; } @@ -149,12 +148,12 @@ int guest_pa_to_host_va(struct guest_info * guest_info, addr_t guest_pa, addr_t } -int guest_va_to_guest_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * guest_pa) { +int v3_gva_to_gpa(struct guest_info * guest_info, addr_t gva, addr_t * gpa) { v3_reg_t guest_cr3 = 0; if (guest_info->mem_mode == PHYSICAL_MEM) { // guest virtual address is the same as the physical - *guest_pa = guest_va; + *gpa = gva; return 0; } @@ -168,25 +167,25 @@ int guest_va_to_guest_pa(struct guest_info * guest_info, addr_t guest_va, addr_t // Guest Is in Paged mode switch (guest_info->cpu_mode) { case PROTECTED: - if (v3_translate_guest_pt_32(guest_info, guest_cr3, guest_va, guest_pa) == -1) { - PrintDebug("Could not translate addr (%p) through 32 bit guest PT at %p\n", - (void *)guest_va, (void *)(addr_t)guest_cr3); + if (v3_translate_guest_pt_32(guest_info, guest_cr3, gva, gpa) == -1) { + /*PrintDebug("Could not translate addr (%p) through 32 bit guest PT at %p\n", + (void *)gva, (void *)(addr_t)guest_cr3);*/ return -1; } break; case PROTECTED_PAE: - if (v3_translate_guest_pt_32pae(guest_info, guest_cr3, guest_va, guest_pa) == -1) { - PrintDebug("Could not translate addr (%p) through 32 bitpae guest PT at %p\n", - (void *)guest_va, (void *)(addr_t)guest_cr3); + if (v3_translate_guest_pt_32pae(guest_info, guest_cr3, gva, gpa) == -1) { + /*PrintDebug("Could not translate addr (%p) through 32 bitpae guest PT at %p\n", + (void *)gva, (void *)(addr_t)guest_cr3);*/ return -1; } break; case LONG: case LONG_32_COMPAT: case LONG_16_COMPAT: - if (v3_translate_guest_pt_64(guest_info, guest_cr3, guest_va, guest_pa) == -1) { - PrintDebug("Could not translate addr (%p) through 64 bit guest PT at %p\n", - (void *)guest_va, (void *)(addr_t)guest_cr3); + if (v3_translate_guest_pt_64(guest_info, guest_cr3, gva, gpa) == -1) { + /*PrintDebug("Could not translate addr (%p) through 64 bit guest PT at %p\n", + (void *)gva, (void *)(addr_t)guest_cr3);*/ return -1; } break; @@ -204,8 +203,8 @@ int guest_va_to_guest_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * * For now we ignore it... */ -int guest_pa_to_guest_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * guest_va) { - *guest_va = 0; +int v3_gpa_to_gva(struct guest_info * guest_info, addr_t gpa, addr_t * gva) { + *gva = 0; PrintError("ERROR!!: GPA->GVA Not Implemented!!\n"); return -1; } @@ -216,20 +215,20 @@ int guest_pa_to_guest_va(struct guest_info * guest_info, addr_t guest_pa, addr_t /**********************************/ -int guest_va_to_host_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * host_pa) { - addr_t guest_pa = 0; +int v3_gva_to_hpa(struct guest_info * guest_info, addr_t gva, addr_t * hpa) { + addr_t gpa = 0; - *host_pa = 0; + *hpa = 0; - if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) { + if (v3_gva_to_gpa(guest_info, gva, &gpa) != 0) { PrintError("In GVA->HPA: Invalid GVA(%p)->GPA lookup\n", - (void *)guest_va); + (void *)gva); return -1; } - if (guest_pa_to_host_pa(guest_info, guest_pa, host_pa) != 0) { + if (v3_gpa_to_hpa(guest_info, gpa, hpa) != 0) { PrintError("In GVA->HPA: Invalid GPA(%p)->HPA lookup\n", - (void *)guest_pa); + (void *)gpa); return -1; } @@ -237,20 +236,20 @@ int guest_va_to_host_pa(struct guest_info * guest_info, addr_t guest_va, addr_t } /* !! Currently not implemented !! */ -int host_pa_to_guest_va(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_va) { - addr_t guest_pa = 0; +int v3_hpa_to_gva(struct guest_info * guest_info, addr_t hpa, addr_t * gva) { + addr_t gpa = 0; - *guest_va = 0; + *gva = 0; - if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) { + if (v3_hpa_to_gpa(guest_info, hpa, &gpa) != 0) { PrintError("In HPA->GVA: Invalid HPA(%p)->GPA lookup\n", - (void *)host_pa); + (void *)hpa); return -1; } - if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) { + if (v3_gpa_to_gva(guest_info, gpa, gva) != 0) { PrintError("In HPA->GVA: Invalid GPA(%p)->GVA lookup\n", - (void *)guest_pa); + (void *)gpa); return -1; } @@ -260,27 +259,27 @@ int host_pa_to_guest_va(struct guest_info * guest_info, addr_t host_pa, addr_t * -int guest_va_to_host_va(struct guest_info * guest_info, addr_t guest_va, addr_t * host_va) { - addr_t guest_pa = 0; - addr_t host_pa = 0; +int v3_gva_to_hva(struct guest_info * guest_info, addr_t gva, addr_t * hva) { + addr_t gpa = 0; + addr_t hpa = 0; - *host_va = 0; + *hva = 0; - if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) { - PrintError("In GVA->HVA: Invalid GVA(%p)->GPA lookup\n", - (void *)guest_va); + if (v3_gva_to_gpa(guest_info, gva, &gpa) != 0) { + /*PrintError("In GVA->HVA: Invalid GVA(%p)->GPA lookup\n", + (void *)gva);*/ return -1; } - if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) { + if (v3_gpa_to_hpa(guest_info, gpa, &hpa) != 0) { PrintError("In GVA->HVA: Invalid GPA(%p)->HPA lookup\n", - (void *)guest_pa); + (void *)gpa); return -1; } - if (host_pa_to_host_va(host_pa, host_va) != 0) { + if (v3_hpa_to_hva(hpa, hva) != 0) { PrintError("In GVA->HVA: Invalid HPA(%p)->HVA lookup\n", - (void *)host_pa); + (void *)hpa); return -1; } @@ -289,27 +288,27 @@ int guest_va_to_host_va(struct guest_info * guest_info, addr_t guest_va, addr_t /* !! Currently not implemented !! */ -int host_va_to_guest_va(struct guest_info * guest_info, addr_t host_va, addr_t * guest_va) { - addr_t host_pa = 0; - addr_t guest_pa = 0; +int v3_hva_to_gva(struct guest_info * guest_info, addr_t hva, addr_t * gva) { + addr_t hpa = 0; + addr_t gpa = 0; - *guest_va = 0; + *gva = 0; - if (host_va_to_host_pa(host_va, &host_pa) != 0) { + if (v3_hva_to_hpa(hva, &hpa) != 0) { PrintError("In HVA->GVA: Invalid HVA(%p)->HPA lookup\n", - (void *)host_va); + (void *)hva); return -1; } - if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) { + if (v3_hpa_to_gpa(guest_info, hpa, &gpa) != 0) { PrintError("In HVA->GVA: Invalid HPA(%p)->GPA lookup\n", - (void *)host_va); + (void *)hva); return -1; } - if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) { + if (v3_gpa_to_gva(guest_info, gpa, gva) != 0) { PrintError("In HVA->GVA: Invalid GPA(%p)->GVA lookup\n", - (void *)guest_pa); + (void *)gpa); return -1; } @@ -318,14 +317,64 @@ int host_va_to_guest_va(struct guest_info * guest_info, addr_t host_va, addr_t * +/* KCH: currently only checks if we can perform a user-mode write + return 1 on success */ +int v3_gva_can_access(struct guest_info * core, addr_t gva) { + + v3_reg_t guest_cr3 = 0; + pf_error_t access_type; + pt_access_status_t access_status; + + access_type.write = 1; + access_type.user = 1; + + if (core->mem_mode == PHYSICAL_MEM) { + return -1; + } + + if (core->shdw_pg_mode == SHADOW_PAGING) { + guest_cr3 = core->shdw_pg_state.guest_cr3; + } else { + guest_cr3 = core->ctrl_regs.cr3; + } + + // guest is in paged mode + switch (core->cpu_mode) { + case PROTECTED: + if (v3_check_guest_pt_32(core, guest_cr3, gva, access_type, &access_status) == -1) { + return -1; + } + break; + case PROTECTED_PAE: + if (v3_check_guest_pt_32pae(core, guest_cr3, gva, access_type, &access_status) == -1) { + return -1; + } + break; + case LONG: + case LONG_32_COMPAT: + case LONG_16_COMPAT: + if (v3_check_guest_pt_64(core, guest_cr3, gva, access_type, &access_status) == -1) { + return -1; + } + break; + default: + return -1; + } + + if (access_status != PT_ACCESS_OK) { + return 0; + } else { + return 1; + } +} /* This is a straight address conversion + copy, * except for the tiny little issue of crossing page boundries..... */ -int read_guest_va_memory(struct guest_info * guest_info, addr_t guest_va, int count, uchar_t * dest) { - addr_t cursor = guest_va; +int v3_read_gva_memory(struct guest_info * guest_info, addr_t gva, int count, uchar_t * dest) { + addr_t cursor = gva; int bytes_read = 0; @@ -336,7 +385,7 @@ int read_guest_va_memory(struct guest_info * guest_info, addr_t guest_va, int co addr_t host_addr = 0; - if (guest_va_to_host_va(guest_info, cursor, &host_addr) != 0) { + if (v3_gva_to_hva(guest_info, cursor, &host_addr) != 0) { PrintDebug("Invalid GVA(%p)->HVA lookup\n", (void *)cursor); return bytes_read; } @@ -361,8 +410,8 @@ int read_guest_va_memory(struct guest_info * guest_info, addr_t guest_va, int co /* This is a straight address conversion + copy, * except for the tiny little issue of crossing page boundries..... */ -int read_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int count, uchar_t * dest) { - addr_t cursor = guest_pa; +int v3_read_gpa_memory(struct guest_info * guest_info, addr_t gpa, int count, uchar_t * dest) { + addr_t cursor = gpa; int bytes_read = 0; while (count > 0) { @@ -370,7 +419,7 @@ int read_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int co int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge; addr_t host_addr = 0; - if (guest_pa_to_host_va(guest_info, cursor, &host_addr) != 0) { + if (v3_gpa_to_hva(guest_info, cursor, &host_addr) != 0) { return bytes_read; } @@ -397,8 +446,8 @@ int read_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int co /* This is a straight address conversion + copy, * except for the tiny little issue of crossing page boundries..... */ -int write_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int count, uchar_t * src) { - addr_t cursor = guest_pa; +int v3_write_gpa_memory(struct guest_info * guest_info, addr_t gpa, int count, uchar_t * src) { + addr_t cursor = gpa; int bytes_written = 0; while (count > 0) { @@ -406,7 +455,7 @@ int write_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int c int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge; addr_t host_addr; - if (guest_pa_to_host_va(guest_info, cursor, &host_addr) != 0) { + if (v3_gpa_to_hva(guest_info, cursor, &host_addr) != 0) { return bytes_written; }