2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vm_guest_mem.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_paging.h>
24 #ifndef V3_CONFIG_DEBUG_MEM_ERRORS
26 #define PrintDebug(fmt, args...)
29 extern struct v3_os_hooks * os_hooks;
32 /**********************************/
34 /**********************************/
36 int v3_hva_to_hpa(addr_t hva, addr_t * hpa) {
37 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) {
39 *hpa = (addr_t)(os_hooks)->vaddr_to_paddr((void *)hva);
42 PrintDebug("In HVA->HPA: Invalid HVA(%p)->HPA lookup\n",
47 PrintDebug("In HVA->HPA: os_hooks not defined\n");
54 int v3_hpa_to_hva(addr_t hpa, addr_t * hva) {
55 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) {
57 *hva = (addr_t)(os_hooks)->paddr_to_vaddr((void *)hpa);
60 PrintDebug("In HPA->HVA: Invalid HPA(%p)->HVA lookup\n",
65 PrintDebug("In HPA->HVA: os_hooks not defined\n");
71 int v3_gpa_to_hpa(struct guest_info * info, addr_t gpa, addr_t * hpa) {
72 struct v3_mem_region * reg = v3_get_mem_region(info->vm_info, info->vcpu_id, gpa);
75 PrintDebug("In GPA->HPA: Could not find address in shadow map (addr=%p) (NULL REGION)\n",
80 if (reg->flags.alloced == 0) {
81 //PrintDebug("In GPA->HPA: Tried to translate physical address of non allocated page (addr=%p)\n",
83 //v3_print_mem_map(info->vm_info);
87 *hpa = (gpa - reg->guest_start) + reg->host_addr;
93 /* !! Currently not implemented !! */
94 // This is a scan of the shadow map
95 // For now we ignore it
97 int v3_hpa_to_gpa(struct guest_info * guest_info, addr_t hpa, addr_t * gpa) {
99 PrintDebug("ERROR!!! HPA->GPA currently not implemented!!!\n");
106 /**********************************/
108 /**********************************/
111 /* !! Currently not implemented !! */
112 // This will return negative until we implement hpa_to_guest_pa()
113 int v3_hva_to_gpa(struct guest_info * guest_info, addr_t hva, addr_t * gpa) {
117 if (v3_hva_to_hpa(hva, &hpa) != 0) {
118 PrintDebug("In HVA->GPA: Invalid HVA(%p)->HPA lookup\n",
123 if (v3_hpa_to_gpa(guest_info, hpa, gpa) != 0) {
124 PrintDebug("In HVA->GPA: Invalid HPA(%p)->GPA lookup\n",
135 int v3_gpa_to_hva(struct guest_info * guest_info, addr_t gpa, addr_t * hva) {
140 if (v3_gpa_to_hpa(guest_info, gpa, &hpa) != 0) {
141 // PrintDebug("In GPA->HVA: Invalid GPA(%p)->HPA lookup\n",
146 if (v3_hpa_to_hva(hpa, hva) != 0) {
147 PrintDebug("In GPA->HVA: Invalid HPA(%p)->HVA lookup\n",
156 int v3_gva_to_gpa(struct guest_info * guest_info, addr_t gva, addr_t * gpa) {
157 v3_reg_t guest_cr3 = 0;
159 if (guest_info->mem_mode == PHYSICAL_MEM) {
160 // guest virtual address is the same as the physical
165 if (guest_info->shdw_pg_mode == SHADOW_PAGING) {
166 guest_cr3 = guest_info->shdw_pg_state.guest_cr3;
168 guest_cr3 = guest_info->ctrl_regs.cr3;
172 // Guest Is in Paged mode
173 switch (guest_info->cpu_mode) {
175 if (v3_translate_guest_pt_32(guest_info, guest_cr3, gva, gpa) == -1) {
176 PrintDebug("Could not translate addr (%p) through 32 bit guest PT at %p\n",
177 (void *)gva, (void *)(addr_t)guest_cr3);
182 if (v3_translate_guest_pt_32pae(guest_info, guest_cr3, gva, gpa) == -1) {
183 PrintDebug("Could not translate addr (%p) through 32 bitpae guest PT at %p\n",
184 (void *)gva, (void *)(addr_t)guest_cr3);
191 if (v3_translate_guest_pt_64(guest_info, guest_cr3, gva, gpa) == -1) {
192 PrintDebug("Could not translate addr (%p) through 64 bit guest PT at %p\n",
193 (void *)gva, (void *)(addr_t)guest_cr3);
206 /* !! Currently not implemented !! */
207 /* This will be a real pain.... its your standard page table walker in guest memory
209 * For now we ignore it...
211 int v3_gpa_to_gva(struct guest_info * guest_info, addr_t gpa, addr_t * gva) {
213 PrintDebug("ERROR!!: GPA->GVA Not Implemented!!\n");
218 /**********************************/
220 /**********************************/
223 int v3_gva_to_hpa(struct guest_info * guest_info, addr_t gva, addr_t * hpa) {
228 if (v3_gva_to_gpa(guest_info, gva, &gpa) != 0) {
229 PrintDebug("In GVA->HPA: Invalid GVA(%p)->GPA lookup\n",
234 if (v3_gpa_to_hpa(guest_info, gpa, hpa) != 0) {
235 PrintDebug("In GVA->HPA: Invalid GPA(%p)->HPA lookup\n",
243 /* !! Currently not implemented !! */
244 int v3_hpa_to_gva(struct guest_info * guest_info, addr_t hpa, addr_t * gva) {
249 if (v3_hpa_to_gpa(guest_info, hpa, &gpa) != 0) {
250 PrintDebug("In HPA->GVA: Invalid HPA(%p)->GPA lookup\n",
255 if (v3_gpa_to_gva(guest_info, gpa, gva) != 0) {
256 PrintDebug("In HPA->GVA: Invalid GPA(%p)->GVA lookup\n",
267 int v3_gva_to_hva(struct guest_info * guest_info, addr_t gva, addr_t * hva) {
273 if (v3_gva_to_gpa(guest_info, gva, &gpa) != 0) {
274 PrintDebug("In GVA->HVA: Invalid GVA(%p)->GPA lookup\n",
279 if (v3_gpa_to_hpa(guest_info, gpa, &hpa) != 0) {
280 PrintDebug("In GVA->HVA: Invalid GPA(%p)->HPA lookup\n",
285 if (v3_hpa_to_hva(hpa, hva) != 0) {
286 PrintDebug("In GVA->HVA: Invalid HPA(%p)->HVA lookup\n",
295 /* !! Currently not implemented !! */
296 int v3_hva_to_gva(struct guest_info * guest_info, addr_t hva, addr_t * gva) {
302 if (v3_hva_to_hpa(hva, &hpa) != 0) {
303 PrintDebug("In HVA->GVA: Invalid HVA(%p)->HPA lookup\n",
308 if (v3_hpa_to_gpa(guest_info, hpa, &gpa) != 0) {
309 PrintDebug("In HVA->GVA: Invalid HPA(%p)->GPA lookup\n",
314 if (v3_gpa_to_gva(guest_info, gpa, gva) != 0) {
315 PrintDebug("In HVA->GVA: Invalid GPA(%p)->GVA lookup\n",
328 /* This is a straight address conversion + copy,
329 * except for the tiny little issue of crossing page boundries.....
331 int v3_read_gva_memory(struct guest_info * guest_info, addr_t gva, int count, uchar_t * dest) {
338 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
339 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
340 addr_t host_addr = 0;
343 if (v3_gva_to_hva(guest_info, cursor, &host_addr) != 0) {
344 PrintDebug("Invalid GVA(%p)->HVA lookup\n", (void *)cursor);
350 memcpy(dest + bytes_read, (void*)host_addr, bytes_to_copy);
352 bytes_read += bytes_to_copy;
353 count -= bytes_to_copy;
354 cursor += bytes_to_copy;
365 /* This is a straight address conversion + copy,
366 * except for the tiny little issue of crossing page boundries.....
368 int v3_read_gpa_memory(struct guest_info * guest_info, addr_t gpa, int count, uchar_t * dest) {
373 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
374 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
375 addr_t host_addr = 0;
377 if (v3_gpa_to_hva(guest_info, cursor, &host_addr) != 0) {
382 PrintDebug("Trying to read %d bytes\n", bytes_to_copy);
383 PrintDebug("Dist to page edge=%d\n", dist_to_pg_edge);
384 PrintDebug("PAGE_ADDR=0x%x\n", PAGE_ADDR(cursor));
385 PrintDebug("guest_pa=0x%x\n", guest_pa);
388 memcpy(dest + bytes_read, (void*)host_addr, bytes_to_copy);
390 bytes_read += bytes_to_copy;
391 count -= bytes_to_copy;
392 cursor += bytes_to_copy;
401 /* This is a straight address conversion + copy,
402 * except for the tiny little issue of crossing page boundries.....
404 int v3_write_gpa_memory(struct guest_info * guest_info, addr_t gpa, int count, uchar_t * src) {
406 int bytes_written = 0;
409 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
410 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
413 if (v3_gpa_to_hva(guest_info, cursor, &host_addr) != 0) {
414 return bytes_written;
418 memcpy((void*)host_addr, src + bytes_written, bytes_to_copy);
420 bytes_written += bytes_to_copy;
421 count -= bytes_to_copy;
422 cursor += bytes_to_copy;
425 return bytes_written;