1 /* Northwestern University */
2 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
4 #include <palacios/vm_guest_mem.h>
5 #include <palacios/vmm.h>
6 #include <palacios/vmm_paging.h>
8 extern struct vmm_os_hooks * os_hooks;
11 /**********************************/
13 /**********************************/
15 int host_va_to_host_pa(addr_t host_va, addr_t * host_pa) {
16 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) {
18 *host_pa = (addr_t)(os_hooks)->vaddr_to_paddr((void *)host_va);
21 PrintError("In HVA->HPA: Invalid HVA(%x)->HPA lookup\n", host_va);
25 PrintError("In HVA->HPA: os_hooks not defined\n");
32 int host_pa_to_host_va(addr_t host_pa, addr_t * host_va) {
33 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) {
35 *host_va = (addr_t)(os_hooks)->paddr_to_vaddr((void *)host_pa);
38 PrintError("In HPA->HVA: Invalid HPA(%x)->HVA lookup\n", host_pa);
42 PrintError("In HPA->HVA: os_hooks not defined\n");
50 int guest_pa_to_host_pa(struct guest_info * guest_info, addr_t guest_pa, addr_t * host_pa) {
51 // we use the shadow map here...
52 if (lookup_shadow_map_addr(&(guest_info->mem_map), guest_pa, host_pa) != HOST_REGION_PHYSICAL_MEMORY) {
53 PrintError("In GPA->HPA: Could not find address in shadow map (addr=%x)\n", guest_pa);
61 /* !! Currently not implemented !! */
62 // This is a scan of the shadow map
63 // For now we ignore it
65 int host_pa_to_guest_pa(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_pa) {
67 PrintError("ERROR!!! HPA->GPA currently not implemented!!!\n");
74 /**********************************/
76 /**********************************/
79 /* !! Currently not implemented !! */
80 // This will return negative until we implement host_pa_to_guest_pa()
81 int host_va_to_guest_pa(struct guest_info * guest_info, addr_t host_va, addr_t * guest_pa) {
85 if (host_va_to_host_pa(host_va, &host_pa) != 0) {
86 PrintError("In HVA->GPA: Invalid HVA(%x)->HPA lookup\n", host_va);
90 if (host_pa_to_guest_pa(guest_info, host_pa, guest_pa) != 0) {
91 PrintError("In HVA->GPA: Invalid HPA(%x)->GPA lookup\n", host_pa);
101 int guest_pa_to_host_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * host_va) {
106 if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) {
107 PrintError("In GPA->HVA: Invalid GPA(%x)->HPA lookup\n", guest_pa);
111 if (host_pa_to_host_va(host_pa, host_va) != 0) {
112 PrintError("In GPA->HVA: Invalid HPA(%x)->HVA lookup\n", host_pa);
120 int guest_va_to_guest_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * guest_pa) {
121 if (guest_info->mem_mode == PHYSICAL_MEM) {
122 // guest virtual address is the same as the physical
123 *guest_pa = guest_va;
129 // Guest Is in Paged mode
130 switch (guest_info->cpu_mode) {
135 addr_t guest_pde = 0;
137 if (guest_info->shdw_pg_mode == SHADOW_PAGING) {
138 guest_pde = CR3_TO_PDE32(guest_info->shdw_pg_state.guest_cr3);
139 } else if (guest_info->shdw_pg_mode == NESTED_PAGING) {
140 guest_pde = CR3_TO_PDE32(guest_info->ctrl_regs.cr3);
143 if (guest_pa_to_host_va(guest_info, guest_pde, (addr_t *)&pde) == -1) {
144 PrintError("In GVA->GPA: Invalid GPA(%x)->HVA PDE32 lookup\n", guest_pde);
149 switch (pde32_lookup(pde, guest_va, &tmp_pa)) {
150 case PDE32_ENTRY_NOT_PRESENT:
153 case PDE32_ENTRY_LARGE_PAGE:
156 case PDE32_ENTRY_PTE32:
161 if (guest_pa_to_host_va(guest_info, tmp_pa, (addr_t*)&pte) == -1) {
162 PrintError("In GVA->GPA: Invalid GPA(%x)->HVA PTE32 lookup\n", guest_pa);
166 //PrintDebug("PTE host addr=%x, GVA=%x, GPA=%x(should be 0)\n", pte, guest_va, *guest_pa);
168 if (pte32_lookup(pte, guest_va, guest_pa) != 0) {
169 PrintError("In GVA->GPA: PTE32 Lookup failure GVA=%x; PTE=%x\n", guest_va, pte);
170 // PrintPT32(PDE32_INDEX(guest_va) << 22, pte);
199 /* !! Currently not implemented !! */
200 /* This will be a real pain.... its your standard page table walker in guest memory
202 * For now we ignore it...
204 int guest_pa_to_guest_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * guest_va) {
206 PrintError("ERROR!!: GPA->GVA Not Implemented!!\n");
211 /**********************************/
213 /**********************************/
216 int guest_va_to_host_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * host_pa) {
221 if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) {
222 PrintError("In GVA->HPA: Invalid GVA(%x)->GPA lookup\n", guest_va);
226 if (guest_pa_to_host_pa(guest_info, guest_pa, host_pa) != 0) {
227 PrintError("In GVA->HPA: Invalid GPA(%x)->HPA lookup\n", guest_pa);
234 /* !! Currently not implemented !! */
235 int host_pa_to_guest_va(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_va) {
240 if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) {
241 PrintError("In HPA->GVA: Invalid HPA(%x)->GPA lookup\n", host_pa);
245 if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) {
246 PrintError("In HPA->GVA: Invalid GPA(%x)->GVA lookup\n", guest_pa);
256 int guest_va_to_host_va(struct guest_info * guest_info, addr_t guest_va, addr_t * host_va) {
262 if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) {
263 PrintError("In GVA->HVA: Invalid GVA(%x)->GPA lookup\n", guest_va);
267 if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) {
268 PrintError("In GVA->HVA: Invalid GPA(%x)->HPA lookup\n", guest_pa);
272 if (host_pa_to_host_va(host_pa, host_va) != 0) {
273 PrintError("In GVA->HVA: Invalid HPA(%x)->HVA lookup\n", host_pa);
281 /* !! Currently not implemented !! */
282 int host_va_to_guest_va(struct guest_info * guest_info, addr_t host_va, addr_t * guest_va) {
288 if (host_va_to_host_pa(host_va, &host_pa) != 0) {
289 PrintError("In HVA->GVA: Invalid HVA(%x)->HPA lookup\n", host_va);
293 if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) {
294 PrintError("In HVA->GVA: Invalid HPA(%x)->GPA lookup\n", host_va);
298 if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) {
299 PrintError("In HVA->GVA: Invalid GPA(%x)->GVA lookup\n", guest_pa);
311 /* This is a straight address conversion + copy,
312 * except for the tiny little issue of crossing page boundries.....
314 int read_guest_va_memory(struct guest_info * guest_info, addr_t guest_va, int count, char * dest) {
315 addr_t cursor = guest_va;
321 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
322 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
323 addr_t host_addr = 0;
326 if (guest_va_to_host_va(guest_info, cursor, &host_addr) != 0) {
327 PrintDebug("Invalid GVA(%x)->HVA lookup\n", cursor);
333 memcpy(dest + bytes_read, (void*)host_addr, bytes_to_copy);
335 bytes_read += bytes_to_copy;
336 count -= bytes_to_copy;
337 cursor += bytes_to_copy;
348 /* This is a straight address conversion + copy,
349 * except for the tiny little issue of crossing page boundries.....
351 int read_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int count, char * dest) {
352 addr_t cursor = guest_pa;
356 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
357 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
358 addr_t host_addr = 0;
360 if (guest_pa_to_host_va(guest_info, cursor, &host_addr) != 0) {
365 PrintDebug("Trying to read %d bytes\n", bytes_to_copy);
366 PrintDebug("Dist to page edge=%d\n", dist_to_pg_edge);
367 PrintDebug("PAGE_ADDR=0x%x\n", PAGE_ADDR(cursor));
368 PrintDebug("guest_pa=0x%x\n", guest_pa);
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;
384 /* This is a straight address conversion + copy,
385 * except for the tiny little issue of crossing page boundries.....
387 int write_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int count, char * src) {
388 addr_t cursor = guest_pa;
389 int bytes_written = 0;
392 int dist_to_pg_edge = (PAGE_ADDR(cursor) + PAGE_SIZE) - cursor;
393 int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
396 if (guest_pa_to_host_va(guest_info, cursor, &host_addr) != 0) {
397 return bytes_written;
401 memcpy((void*)host_addr, src + bytes_written, bytes_to_copy);
403 bytes_written += bytes_to_copy;
404 count -= bytes_to_copy;
405 cursor += bytes_to_copy;
408 return bytes_written;