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 extern struct v3_os_hooks * os_hooks;
27 /**********************************/
29 /**********************************/
31 int host_va_to_host_pa(addr_t host_va, addr_t * host_pa) {
32 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) {
34 *host_pa = (addr_t)(os_hooks)->vaddr_to_paddr((void *)host_va);
37 PrintError("In HVA->HPA: Invalid HVA(%p)->HPA lookup\n",
42 PrintError("In HVA->HPA: os_hooks not defined\n");
49 int host_pa_to_host_va(addr_t host_pa, addr_t * host_va) {
50 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) {
52 *host_va = (addr_t)(os_hooks)->paddr_to_vaddr((void *)host_pa);
55 PrintError("In HPA->HVA: Invalid HPA(%p)->HVA lookup\n",
60 PrintError("In HPA->HVA: os_hooks not defined\n");
68 int guest_pa_to_host_pa(struct guest_info * guest_info, addr_t guest_pa, addr_t * host_pa) {
69 // we use the shadow map here...
70 host_region_type_t reg_type = lookup_shadow_map_addr(&(guest_info->mem_map), guest_pa, host_pa);
72 if (reg_type != HOST_REGION_PHYSICAL_MEMORY) {
73 PrintError("In GPA->HPA: Could not find address in shadow map (addr=%p) (reg_type=%d)\n",
74 (void *)guest_pa, reg_type);
82 /* !! Currently not implemented !! */
83 // This is a scan of the shadow map
84 // For now we ignore it
86 int host_pa_to_guest_pa(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_pa) {
88 PrintError("ERROR!!! HPA->GPA currently not implemented!!!\n");
95 /**********************************/
97 /**********************************/
100 /* !! Currently not implemented !! */
101 // This will return negative until we implement host_pa_to_guest_pa()
102 int host_va_to_guest_pa(struct guest_info * guest_info, addr_t host_va, addr_t * guest_pa) {
106 if (host_va_to_host_pa(host_va, &host_pa) != 0) {
107 PrintError("In HVA->GPA: Invalid HVA(%p)->HPA lookup\n",
112 if (host_pa_to_guest_pa(guest_info, host_pa, guest_pa) != 0) {
113 PrintError("In HVA->GPA: Invalid HPA(%p)->GPA lookup\n",
124 int guest_pa_to_host_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * host_va) {
129 if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) {
130 PrintError("In GPA->HVA: Invalid GPA(%p)->HPA lookup\n",
135 if (host_pa_to_host_va(host_pa, host_va) != 0) {
136 PrintError("In GPA->HVA: Invalid HPA(%p)->HVA lookup\n",
145 int guest_va_to_guest_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * guest_pa) {
146 if (guest_info->mem_mode == PHYSICAL_MEM) {
147 // guest virtual address is the same as the physical
148 *guest_pa = guest_va;
154 // Guest Is in Paged mode
155 switch (guest_info->cpu_mode) {
160 addr_t guest_pde = 0;
162 if (guest_info->shdw_pg_mode == SHADOW_PAGING) {
163 guest_pde = (addr_t)V3_PAddr((void *)(addr_t)CR3_TO_PDE32((void *)(addr_t)(guest_info->shdw_pg_state.guest_cr3)));
164 } else if (guest_info->shdw_pg_mode == NESTED_PAGING) {
165 guest_pde = (addr_t)V3_PAddr((void *)(addr_t)CR3_TO_PDE32((void *)(addr_t)(guest_info->ctrl_regs.cr3)));
168 if (guest_pa_to_host_va(guest_info, guest_pde, (addr_t *)&pde) == -1) {
169 PrintError("In GVA->GPA: Invalid GPA(%p)->HVA PDE32 lookup\n",
175 switch (pde32_lookup(pde, guest_va, &tmp_pa)) {
176 case PDE32_ENTRY_NOT_PRESENT:
179 case PDE32_ENTRY_LARGE_PAGE:
182 case PDE32_ENTRY_PTE32:
187 if (guest_pa_to_host_va(guest_info, tmp_pa, (addr_t*)&pte) == -1) {
188 PrintError("In GVA->GPA: Invalid GPA(%p)->HVA PTE32 lookup\n",
193 //PrintDebug("PTE host addr=%x, GVA=%x, GPA=%x(should be 0)\n", pte, guest_va, *guest_pa);
195 if (pte32_lookup(pte, guest_va, guest_pa) != 0) {
196 PrintError("In GVA->GPA: PTE32 Lookup failure GVA=%p; PTE=%p\n",
197 (void *)guest_va, (void *)pte);
198 // PrintPT32(PDE32_INDEX(guest_va) << 22, pte);
227 /* !! Currently not implemented !! */
228 /* This will be a real pain.... its your standard page table walker in guest memory
230 * For now we ignore it...
232 int guest_pa_to_guest_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * guest_va) {
234 PrintError("ERROR!!: GPA->GVA Not Implemented!!\n");
239 /**********************************/
241 /**********************************/
244 int guest_va_to_host_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * host_pa) {
249 if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) {
250 PrintError("In GVA->HPA: Invalid GVA(%p)->GPA lookup\n",
255 if (guest_pa_to_host_pa(guest_info, guest_pa, host_pa) != 0) {
256 PrintError("In GVA->HPA: Invalid GPA(%p)->HPA lookup\n",
264 /* !! Currently not implemented !! */
265 int host_pa_to_guest_va(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_va) {
270 if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) {
271 PrintError("In HPA->GVA: Invalid HPA(%p)->GPA lookup\n",
276 if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) {
277 PrintError("In HPA->GVA: Invalid GPA(%p)->GVA lookup\n",
288 int guest_va_to_host_va(struct guest_info * guest_info, addr_t guest_va, addr_t * host_va) {
294 if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) {
295 PrintError("In GVA->HVA: Invalid GVA(%p)->GPA lookup\n",
300 if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) {
301 PrintError("In GVA->HVA: Invalid GPA(%p)->HPA lookup\n",
306 if (host_pa_to_host_va(host_pa, host_va) != 0) {
307 PrintError("In GVA->HVA: Invalid HPA(%p)->HVA lookup\n",
316 /* !! Currently not implemented !! */
317 int host_va_to_guest_va(struct guest_info * guest_info, addr_t host_va, addr_t * guest_va) {
323 if (host_va_to_host_pa(host_va, &host_pa) != 0) {
324 PrintError("In HVA->GVA: Invalid HVA(%p)->HPA lookup\n",
329 if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) {
330 PrintError("In HVA->GVA: Invalid HPA(%p)->GPA lookup\n",
335 if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) {
336 PrintError("In HVA->GVA: Invalid GPA(%p)->GVA lookup\n",
349 /* This is a straight address conversion + copy,
350 * except for the tiny little issue of crossing page boundries.....
352 int read_guest_va_memory(struct guest_info * guest_info, addr_t guest_va, int count, uchar_t * dest) {
353 addr_t cursor = guest_va;
359 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
360 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
361 addr_t host_addr = 0;
364 if (guest_va_to_host_va(guest_info, cursor, &host_addr) != 0) {
365 PrintDebug("Invalid GVA(%p)->HVA lookup\n", (void *)cursor);
371 memcpy(dest + bytes_read, (void*)host_addr, bytes_to_copy);
373 bytes_read += bytes_to_copy;
374 count -= bytes_to_copy;
375 cursor += bytes_to_copy;
386 /* This is a straight address conversion + copy,
387 * except for the tiny little issue of crossing page boundries.....
389 int read_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int count, uchar_t * dest) {
390 addr_t cursor = guest_pa;
394 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
395 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
396 addr_t host_addr = 0;
398 if (guest_pa_to_host_va(guest_info, cursor, &host_addr) != 0) {
403 PrintDebug("Trying to read %d bytes\n", bytes_to_copy);
404 PrintDebug("Dist to page edge=%d\n", dist_to_pg_edge);
405 PrintDebug("PAGE_ADDR=0x%x\n", PAGE_ADDR(cursor));
406 PrintDebug("guest_pa=0x%x\n", guest_pa);
409 memcpy(dest + bytes_read, (void*)host_addr, bytes_to_copy);
411 bytes_read += bytes_to_copy;
412 count -= bytes_to_copy;
413 cursor += bytes_to_copy;
422 /* This is a straight address conversion + copy,
423 * except for the tiny little issue of crossing page boundries.....
425 int write_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int count, uchar_t * src) {
426 addr_t cursor = guest_pa;
427 int bytes_written = 0;
430 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
431 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
434 if (guest_pa_to_host_va(guest_info, cursor, &host_addr) != 0) {
435 return bytes_written;
439 memcpy((void*)host_addr, src + bytes_written, bytes_to_copy);
441 bytes_written += bytes_to_copy;
442 count -= bytes_to_copy;
443 cursor += bytes_to_copy;
446 return bytes_written;