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".
21 static inline int activate_shadow_pt_64(struct guest_info * info) {
22 struct cr3_64 * shadow_cr3 = (struct cr3_64 *)&(info->ctrl_regs.cr3);
23 struct cr3_64 * guest_cr3 = (struct cr3_64 *)&(info->shdw_pg_state.guest_cr3);
24 struct shadow_page_data * shadow_pt = create_new_shadow_pt(info);
25 addr_t shadow_pt_addr = shadow_pt->page_pa;
27 // Because this is a new CR3 load the allocated page is the new CR3 value
28 shadow_pt->cr3 = shadow_pt->page_pa;
30 PrintDebug("Top level Shadow page pa=%p\n", (void *)shadow_pt_addr);
32 shadow_cr3->pml4t_base_addr = PAGE_BASE_ADDR_4KB(shadow_pt_addr);
33 PrintDebug("Creating new 64 bit shadow page table %p\n", (void *)BASE_TO_PAGE_ADDR(shadow_cr3->pml4t_base_addr));
36 shadow_cr3->pwt = guest_cr3->pwt;
37 shadow_cr3->pcd = guest_cr3->pcd;
50 * * 64 bit Page table fault handlers
55 static int handle_2MB_shadow_pagefault_pde_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
56 pt_access_status_t shadow_pde_access, pde64_2MB_t * shadow_pt,
57 pde64_2MB_t * large_guest_pde);
58 static int handle_2MB_shadow_pagefault_pte_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
59 pte64_t * shadow_pt, pde64_2MB_t * large_guest_pde);
61 static int handle_pte_shadow_pagefault_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
62 pte64_t * shadow_pt, pte64_t * guest_pt);
64 static int handle_pde_shadow_pagefault_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
65 pde64_t * shadow_pd, pde64_t * guest_pd);
67 static int handle_pdpe_shadow_pagefault_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
68 pdpe64_t * shadow_pdp, pdpe64_t * guest_pdp);
71 static inline int handle_shadow_pagefault_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
72 pml4e64_t * guest_pml = NULL;
73 pml4e64_t * shadow_pml = CR3_TO_PML4E64_VA(info->ctrl_regs.cr3);
74 addr_t guest_cr3 = CR3_TO_PML4E64_PA(info->shdw_pg_state.guest_cr3);
75 pt_access_status_t guest_pml4e_access;
76 pt_access_status_t shadow_pml4e_access;
77 pml4e64_t * guest_pml4e = NULL;
78 pml4e64_t * shadow_pml4e = (pml4e64_t *)&(shadow_pml[PML4E64_INDEX(fault_addr)]);
80 PrintDebug("64 bit Shadow page fault handler: %p\n", (void *)fault_addr);
81 PrintDebug("Handling PML fault\n");
83 if (v3_gpa_to_hva(info, guest_cr3, (addr_t*)&guest_pml) == -1) {
84 PrintError("Invalid Guest PML4E Address: 0x%p\n", (void *)guest_cr3);
88 guest_pml4e = (pml4e64_t *)&(guest_pml[PML4E64_INDEX(fault_addr)]);
90 PrintDebug("Checking Guest %p\n", (void *)guest_pml);
91 // Check the guest page permissions
92 guest_pml4e_access = v3_can_access_pml4e64(guest_pml, fault_addr, error_code);
94 PrintDebug("Checking shadow %p\n", (void *)shadow_pml);
95 // Check the shadow page permissions
96 shadow_pml4e_access = v3_can_access_pml4e64(shadow_pml, fault_addr, error_code);
98 /* Was the page fault caused by the Guest's page tables? */
99 if (v3_is_guest_pf(guest_pml4e_access, shadow_pml4e_access) == 1) {
100 PrintDebug("Injecting PML4E pf to guest: (guest access error=%d) (pf error code=%d)\n",
101 *(uint_t *)&guest_pml4e_access, *(uint_t *)&error_code);
102 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
103 PrintError("Could not inject guest page fault\n");
109 if (shadow_pml4e_access == PT_ACCESS_USER_ERROR) {
111 // PML4 Entry marked non-user
113 PrintDebug("Shadow Paging User access error (shadow_pml4e_access=0x%x, guest_pml4e_access=0x%x)\n",
114 shadow_pml4e_access, guest_pml4e_access);
115 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
116 PrintError("Could not inject guest page fault\n");
120 } else if ((shadow_pml4e_access != PT_ACCESS_NOT_PRESENT) &&
121 (shadow_pml4e_access != PT_ACCESS_OK)) {
122 // inject page fault in guest
123 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
124 PrintError("Could not inject guest page fault\n");
127 PrintDebug("Unknown Error occurred (shadow_pde_access=%d)\n", shadow_pml4e_access);
128 PrintDebug("Manual Says to inject page fault into guest\n");
133 pdpe64_t * shadow_pdp = NULL;
134 pdpe64_t * guest_pdp = NULL;
136 // Get the next shadow page level, allocate if not present
138 if (shadow_pml4e_access == PT_ACCESS_NOT_PRESENT) {
139 struct shadow_page_data * shdw_page = create_new_shadow_pt(info);
140 shadow_pdp = (pdpe64_t *)V3_VAddr((void *)shdw_page->page_pa);
143 shadow_pml4e->present = 1;
144 shadow_pml4e->user_page = guest_pml4e->user_page;
145 shadow_pml4e->writable = guest_pml4e->writable;
146 shadow_pml4e->cache_disable = guest_pml4e->cache_disable;
147 shadow_pml4e->write_through = guest_pml4e->write_through;
149 guest_pml4e->accessed = 1;
151 shadow_pml4e->pdp_base_addr = PAGE_BASE_ADDR(shdw_page->page_pa);
153 shadow_pdp = (pdpe64_t *)V3_VAddr((void *)(addr_t)BASE_TO_PAGE_ADDR(shadow_pml4e->pdp_base_addr));
156 // Continue processing at the next level
158 if (v3_gpa_to_hva(info, BASE_TO_PAGE_ADDR(guest_pml4e->pdp_base_addr), (addr_t *)&guest_pdp) == -1) {
159 // Machine check the guest
160 PrintError("Invalid Guest PDP Address: 0x%p\n", (void *)BASE_TO_PAGE_ADDR(guest_pml4e->pdp_base_addr));
161 v3_raise_exception(info, MC_EXCEPTION);
165 if (handle_pdpe_shadow_pagefault_64(info, fault_addr, error_code, shadow_pdp, guest_pdp) == -1) {
166 PrintError("Error handling Page fault caused by PDPE\n");
175 // For now we are not going to handle 1 Gigabyte pages
176 static int handle_pdpe_shadow_pagefault_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
177 pdpe64_t * shadow_pdp, pdpe64_t * guest_pdp) {
178 pt_access_status_t guest_pdpe_access;
179 pt_access_status_t shadow_pdpe_access;
180 pdpe64_t * guest_pdpe = (pdpe64_t *)&(guest_pdp[PDPE64_INDEX(fault_addr)]);
181 pdpe64_t * shadow_pdpe = (pdpe64_t *)&(shadow_pdp[PDPE64_INDEX(fault_addr)]);
183 PrintDebug("Handling PDP fault\n");
186 PrintDebug("Guest Page Tree for guest virtual address zero fault\n");
187 PrintGuestPageTree(info,fault_addr,(addr_t)(info->shdw_pg_state.guest_cr3));
188 PrintDebug("Host Page Tree for guest virtual address zero fault\n");
189 PrintHostPageTree(info,fault_addr,(addr_t)(info->ctrl_regs.cr3));
192 // Check the guest page permissions
193 guest_pdpe_access = v3_can_access_pdpe64(guest_pdp, fault_addr, error_code);
195 // Check the shadow page permissions
196 shadow_pdpe_access = v3_can_access_pdpe64(shadow_pdp, fault_addr, error_code);
198 /* Was the page fault caused by the Guest's page tables? */
199 if (v3_is_guest_pf(guest_pdpe_access, shadow_pdpe_access) == 1) {
200 PrintDebug("Injecting PDPE pf to guest: (guest access error=%d) (pf error code=%d)\n",
201 *(uint_t *)&guest_pdpe_access, *(uint_t *)&error_code);
202 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
203 PrintError("Could not inject guest page fault\n");
209 if (shadow_pdpe_access == PT_ACCESS_USER_ERROR) {
211 // PML4 Entry marked non-user
213 PrintDebug("Shadow Paging User access error (shadow_pdpe_access=0x%x, guest_pdpe_access=0x%x)\n",
214 shadow_pdpe_access, guest_pdpe_access);
215 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
216 PrintError("Could not inject guest page fault\n");
220 } else if ((shadow_pdpe_access != PT_ACCESS_NOT_PRESENT) &&
221 (shadow_pdpe_access != PT_ACCESS_OK)) {
222 // inject page fault in guest
223 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
224 PrintError("Could not inject guest page fault\n");
227 PrintDebug("Unknown Error occurred (shadow_pde_access=%d)\n", shadow_pdpe_access);
228 PrintDebug("Manual Says to inject page fault into guest\n");
233 pde64_t * shadow_pd = NULL;
234 pde64_t * guest_pd = NULL;
236 // Get the next shadow page level, allocate if not present
238 if (shadow_pdpe_access == PT_ACCESS_NOT_PRESENT) {
239 struct shadow_page_data * shdw_page = create_new_shadow_pt(info);
240 shadow_pd = (pde64_t *)V3_VAddr((void *)shdw_page->page_pa);
243 shadow_pdpe->present = 1;
244 shadow_pdpe->user_page = guest_pdpe->user_page;
245 shadow_pdpe->writable = guest_pdpe->writable;
246 shadow_pdpe->write_through = guest_pdpe->write_through;
247 shadow_pdpe->cache_disable = guest_pdpe->cache_disable;
250 guest_pdpe->accessed = 1;
252 shadow_pdpe->pd_base_addr = PAGE_BASE_ADDR(shdw_page->page_pa);
254 shadow_pd = (pde64_t *)V3_VAddr((void *)(addr_t)BASE_TO_PAGE_ADDR(shadow_pdpe->pd_base_addr));
257 // Continue processing at the next level
259 if (v3_gpa_to_hva(info, BASE_TO_PAGE_ADDR(guest_pdpe->pd_base_addr), (addr_t *)&guest_pd) == -1) {
260 // Machine check the guest
261 PrintError("Invalid Guest PTE Address: 0x%p\n", (void *)BASE_TO_PAGE_ADDR(guest_pdpe->pd_base_addr));
262 v3_raise_exception(info, MC_EXCEPTION);
266 if (handle_pde_shadow_pagefault_64(info, fault_addr, error_code, shadow_pd, guest_pd) == -1) {
267 PrintError("Error handling Page fault caused by PDE\n");
275 static int handle_pde_shadow_pagefault_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
276 pde64_t * shadow_pd, pde64_t * guest_pd) {
277 pt_access_status_t guest_pde_access;
278 pt_access_status_t shadow_pde_access;
279 pde64_t * guest_pde = (pde64_t *)&(guest_pd[PDE64_INDEX(fault_addr)]);
280 pde64_t * shadow_pde = (pde64_t *)&(shadow_pd[PDE64_INDEX(fault_addr)]);
282 PrintDebug("Handling PDE fault\n");
284 // Check the guest page permissions
285 guest_pde_access = v3_can_access_pde64(guest_pd, fault_addr, error_code);
287 // Check the shadow page permissions
288 shadow_pde_access = v3_can_access_pde64(shadow_pd, fault_addr, error_code);
290 /* Was the page fault caused by the Guest's page tables? */
291 if (v3_is_guest_pf(guest_pde_access, shadow_pde_access) == 1) {
292 PrintDebug("Injecting PDE pf to guest: (guest access error=%d) (pf error code=%d)\n",
293 *(uint_t *)&guest_pde_access, *(uint_t *)&error_code);
294 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
295 PrintError("Could not inject guest page fault\n");
301 if (shadow_pde_access == PT_ACCESS_USER_ERROR) {
303 // PDE Entry marked non-user
305 PrintDebug("Shadow Paging User access error (shadow_pdpe_access=0x%x, guest_pdpe_access=0x%x)\n",
306 shadow_pde_access, guest_pde_access);
307 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
308 PrintError("Could not inject guest page fault\n");
313 } else if ((shadow_pde_access == PT_ACCESS_WRITE_ERROR) &&
314 (guest_pde->large_page == 1)) {
316 ((pde64_2MB_t *)guest_pde)->dirty = 1;
317 shadow_pde->writable = guest_pde->writable;
319 //PrintDebug("Returning due to large page Write Error\n");
320 //PrintHostPageTree(info, fault_addr, info->ctrl_regs.cr3);
323 } else if ((shadow_pde_access != PT_ACCESS_NOT_PRESENT) &&
324 (shadow_pde_access != PT_ACCESS_OK)) {
325 // inject page fault in guest
326 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
327 PrintError("Could not inject guest page fault\n");
330 PrintDebug("Unknown Error occurred (shadow_pde_access=%d)\n", shadow_pde_access);
331 PrintDebug("Manual Says to inject page fault into guest\n");
335 // Handle as a shadow large page if possible
336 if (guest_pde->large_page
337 && (info->vm_info->mem_align >= PAGE_SIZE_2MB)) {
338 if (handle_2MB_shadow_pagefault_pde_64(info, fault_addr, error_code, shadow_pde_access,
339 (pde64_2MB_t *)shadow_pde, (pde64_2MB_t *)guest_pde) == -1) {
340 PrintError("Error handling large pagefault with large page\n");
347 pte64_t * shadow_pt = NULL;
348 pte64_t * guest_pt = NULL;
350 // get the next shadow page level, allocate if not present
351 if (shadow_pde_access == PT_ACCESS_NOT_PRESENT) {
352 struct shadow_page_data * shdw_page = create_new_shadow_pt(info);
353 shadow_pt = (pte64_t *)V3_VAddr((void *)shdw_page->page_pa);
355 PrintDebug("Creating new shadow PT: %p\n", shadow_pt);
357 shadow_pde->present = 1;
358 shadow_pde->user_page = guest_pde->user_page;
361 if (guest_pde->large_page == 0) {
362 shadow_pde->writable = guest_pde->writable;
364 // This large page flag is temporary until we can get a working cache....
365 ((pde64_2MB_t *)guest_pde)->vmm_info = V3_LARGE_PG;
367 if (error_code.write) {
368 shadow_pde->writable = guest_pde->writable;
369 ((pde64_2MB_t *)guest_pde)->dirty = 1;
371 shadow_pde->writable = 0;
372 ((pde64_2MB_t *)guest_pde)->dirty = 0;
376 // VMM Specific options
377 shadow_pde->write_through = guest_pde->write_through;
378 shadow_pde->cache_disable = guest_pde->cache_disable;
379 shadow_pde->global_page = guest_pde->global_page;
382 guest_pde->accessed = 1;
384 shadow_pde->pt_base_addr = PAGE_BASE_ADDR(shdw_page->page_pa);
386 shadow_pt = (pte64_t *)V3_VAddr((void *)BASE_TO_PAGE_ADDR(shadow_pde->pt_base_addr));
389 // Continue processing at the next level
390 if (guest_pde->large_page == 0) {
391 if (v3_gpa_to_hva(info, BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr), (addr_t *)&guest_pt) == -1) {
392 // Machine check the guest
393 PrintError("Invalid Guest PTE Address: 0x%p\n", (void *)BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr));
394 v3_raise_exception(info, MC_EXCEPTION);
398 if (handle_pte_shadow_pagefault_64(info, fault_addr, error_code, shadow_pt, guest_pt) == -1) {
399 PrintError("Error handling Page fault caused by PDE\n");
403 if (handle_2MB_shadow_pagefault_pte_64(info, fault_addr, error_code, shadow_pt, (pde64_2MB_t *)guest_pde) == -1) {
404 PrintError("Error handling large pagefault with small page\n");
413 static int handle_pte_shadow_pagefault_64(struct guest_info * info, addr_t fault_addr, pf_error_t error_code,
414 pte64_t * shadow_pt, pte64_t * guest_pt) {
415 pt_access_status_t guest_pte_access;
416 pt_access_status_t shadow_pte_access;
417 pte64_t * guest_pte = (pte64_t *)&(guest_pt[PTE64_INDEX(fault_addr)]);;
418 pte64_t * shadow_pte = (pte64_t *)&(shadow_pt[PTE64_INDEX(fault_addr)]);
419 addr_t guest_pa = BASE_TO_PAGE_ADDR((addr_t)(guest_pte->page_base_addr)) + PAGE_OFFSET(fault_addr);
420 // struct shadow_page_state * state = &(info->shdw_pg_state);
422 PrintDebug("Handling PTE fault\n");
424 struct v3_mem_region * shdw_reg = v3_get_mem_region(info->vm_info, info->cpu_id, guest_pa);
428 if (shdw_reg == NULL) {
429 // Inject a machine check in the guest
430 PrintError("Invalid Guest Address in page table (0x%p)\n", (void *)guest_pa);
431 v3_raise_exception(info, MC_EXCEPTION);
435 // Check the guest page permissions
436 guest_pte_access = v3_can_access_pte64(guest_pt, fault_addr, error_code);
438 // Check the shadow page permissions
439 shadow_pte_access = v3_can_access_pte64(shadow_pt, fault_addr, error_code);
441 /* Was the page fault caused by the Guest's page tables? */
442 if (v3_is_guest_pf(guest_pte_access, shadow_pte_access) == 1) {
444 PrintDebug("Access error injecting pf to guest (guest access error=%d) (pf error code=%d)\n",
445 guest_pte_access, *(uint_t*)&error_code);
447 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
448 PrintError("Could not inject guest page fault\n");
456 if (shadow_pte_access == PT_ACCESS_OK) {
457 // Inconsistent state...
458 // Guest Re-Entry will flush page tables and everything should now work
459 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
464 if (shadow_pte_access == PT_ACCESS_NOT_PRESENT) {
465 // Page Table Entry Not Present
466 PrintDebug("guest_pa =%p\n", (void *)guest_pa);
468 if ((shdw_reg->flags.alloced == 1) ||
469 (shdw_reg->flags.read == 1)) {
470 addr_t shadow_pa = 0;
472 if (v3_gpa_to_hpa(info, guest_pa, &shadow_pa) == -1) {
473 PrintError("could not translate page fault address (%p)\n", (void *)guest_pa);
477 shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
479 shadow_pte->present = guest_pte->present;
480 shadow_pte->user_page = guest_pte->user_page;
482 //set according to VMM policy
483 shadow_pte->write_through = guest_pte->write_through;
484 shadow_pte->cache_disable = guest_pte->cache_disable;
485 shadow_pte->global_page = guest_pte->global_page;
488 guest_pte->accessed = 1;
490 if (guest_pte->dirty == 1) {
491 shadow_pte->writable = guest_pte->writable;
492 } else if ((guest_pte->dirty == 0) && (error_code.write == 1)) {
493 shadow_pte->writable = guest_pte->writable;
494 guest_pte->dirty = 1;
495 } else if ((guest_pte->dirty == 0) && (error_code.write == 0)) {
496 shadow_pte->writable = 0;
500 // Write hooks trump all, and are set Read Only
501 if (shdw_reg->flags.write == 0) {
502 shadow_pte->writable = 0;
506 // Pass to unhandled call back
507 if (shdw_reg->unhandled(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
508 PrintError("Special Page fault handler returned error for address: %p\n", (void *)fault_addr);
512 } else if (shadow_pte_access == PT_ACCESS_WRITE_ERROR) {
513 guest_pte->dirty = 1;
516 if (shdw_reg->flags.write == 1) {
517 PrintDebug("Shadow PTE Write Error\n");
518 shadow_pte->writable = guest_pte->writable;
520 if (shdw_reg->unhandled(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
521 PrintError("Special Page fault handler returned error for address: %p\n", (void *)fault_addr);
531 // Inject page fault into the guest
532 if (v3_inject_guest_pf(info, fault_addr, error_code) == -1) {
533 PrintError("Could not inject guest page fault\n");
536 PrintError("PTE Page fault fell through... Not sure if this should ever happen\n");
537 PrintError("Manual Says to inject page fault into guest\n");
545 static int handle_2MB_shadow_pagefault_pde_64(struct guest_info * info,
546 addr_t fault_addr, pf_error_t error_code,
547 pt_access_status_t shadow_pde_access,
548 pde64_2MB_t * large_shadow_pde, pde64_2MB_t * large_guest_pde)
550 addr_t guest_fault_pa = BASE_TO_PAGE_ADDR_2MB(large_guest_pde->page_base_addr) + PAGE_OFFSET_2MB(fault_addr);
551 // struct shadow_page_state * state = &(info->shdw_pg_state);
553 PrintDebug("Handling 2MB fault with large page (guest_fault_pa=%p) (error_code=%x)\n", (void *)guest_fault_pa, *(uint_t*)&error_code);
554 PrintDebug("LargeShadowPDE=%p, LargeGuestPDE=%p\n", large_shadow_pde, large_guest_pde);
556 struct v3_mem_region * shdw_reg = v3_get_mem_region(info->vm_info, info->cpu_id, guest_fault_pa);
558 if (shdw_reg == NULL) {
559 // Inject a machine check in the guest
560 PrintError("Invalid Guest Address in page table (0x%p)\n", (void *)guest_fault_pa);
561 v3_raise_exception(info, MC_EXCEPTION);
565 if (shadow_pde_access == PT_ACCESS_OK) {
566 // Inconsistent state...
567 // Guest Re-Entry will flush tables and everything should now workd
568 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
569 //PrintHostPageTree(info, fault_addr, info->ctrl_regs.cr3);
574 if (shadow_pde_access == PT_ACCESS_NOT_PRESENT) {
575 // Get the guest physical address of the fault
577 if ((shdw_reg->flags.alloced == 1) ||
578 (shdw_reg->flags.read == 1)) {
579 addr_t shadow_pa = 0;
581 if (v3_gpa_to_hpa(info, guest_fault_pa, &shadow_pa) == -1) {
582 PrintError("could not translate page fault address (%p)\n", (void *)guest_fault_pa);
586 large_guest_pde->vmm_info = V3_LARGE_PG; /* For invalidations */
587 large_shadow_pde->page_base_addr = PAGE_BASE_ADDR_2MB(shadow_pa);
588 large_shadow_pde->large_page = 1;
589 large_shadow_pde->present = 1;
590 large_shadow_pde->user_page = 1;
592 if (shdw_reg->flags.write == 0) {
593 large_shadow_pde->writable = 0;
595 large_shadow_pde->writable = 1;
598 //set according to VMM policy
599 large_shadow_pde->write_through = large_guest_pde->write_through;
600 large_shadow_pde->cache_disable = large_guest_pde->cache_disable;
601 large_shadow_pde->global_page = large_guest_pde->global_page;
605 if (shdw_reg->unhandled(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
606 PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
610 } else if (shadow_pde_access == PT_ACCESS_WRITE_ERROR) {
611 if (shdw_reg->unhandled(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
612 PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
616 PrintError("Error in large page fault handler...\n");
617 PrintError("This case should have been handled at the top level handler\n");
621 // PrintHostPageTree(info, fault_addr, info->ctrl_regs.cr3);
622 PrintDebug("Returning from large page->large page fault handler\n");
626 static int handle_2MB_shadow_pagefault_pte_64(struct guest_info * info,
627 addr_t fault_addr, pf_error_t error_code,
628 pte64_t * shadow_pt, pde64_2MB_t * large_guest_pde)
630 pt_access_status_t shadow_pte_access = v3_can_access_pte64(shadow_pt, fault_addr, error_code);
631 pte64_t * shadow_pte = (pte64_t *)&(shadow_pt[PTE64_INDEX(fault_addr)]);
632 addr_t guest_fault_pa = BASE_TO_PAGE_ADDR_2MB(large_guest_pde->page_base_addr) + PAGE_OFFSET_2MB(fault_addr);
633 // struct shadow_page_state * state = &(info->shdw_pg_state);
635 PrintDebug("Handling 2MB fault (guest_fault_pa=%p) (error_code=%x)\n", (void *)guest_fault_pa, *(uint_t*)&error_code);
636 PrintDebug("ShadowPT=%p, LargeGuestPDE=%p\n", shadow_pt, large_guest_pde);
638 struct v3_mem_region * shdw_reg = v3_get_mem_region(info->vm_info, info->cpu_id, guest_fault_pa);
641 if (shdw_reg == NULL) {
642 // Inject a machine check in the guest
643 PrintError("Invalid Guest Address in page table (0x%p)\n", (void *)guest_fault_pa);
644 v3_raise_exception(info, MC_EXCEPTION);
648 if (shadow_pte_access == PT_ACCESS_OK) {
649 // Inconsistent state...
650 // Guest Re-Entry will flush tables and everything should now workd
651 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
652 //PrintHostPageTree(info, fault_addr, info->ctrl_regs.cr3);
657 if (shadow_pte_access == PT_ACCESS_NOT_PRESENT) {
658 // Get the guest physical address of the fault
660 if ((shdw_reg->flags.alloced == 1) ||
661 (shdw_reg->flags.read == 1)) {
662 addr_t shadow_pa = 0;
664 if (v3_gpa_to_hpa(info, guest_fault_pa, &shadow_pa) == -1) {
665 PrintError("could not translate page fault address (%p)\n", (void *)guest_fault_pa);
669 shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
671 shadow_pte->present = 1;
673 /* We are assuming that the PDE entry has precedence
674 * so the Shadow PDE will mirror the guest PDE settings,
675 * and we don't have to worry about them here
678 shadow_pte->user_page = 1;
680 if (shdw_reg->flags.write == 0) {
681 shadow_pte->writable = 0;
683 shadow_pte->writable = 1;
686 //set according to VMM policy
687 shadow_pte->write_through = large_guest_pde->write_through;
688 shadow_pte->cache_disable = large_guest_pde->cache_disable;
689 shadow_pte->global_page = large_guest_pde->global_page;
693 if (shdw_reg->unhandled(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
694 PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
698 } else if (shadow_pte_access == PT_ACCESS_WRITE_ERROR) {
699 if (shdw_reg->unhandled(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
700 PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
704 PrintError("Error in large page fault handler...\n");
705 PrintError("This case should have been handled at the top level handler\n");
709 // PrintHostPageTree(info, fault_addr, info->ctrl_regs.cr3);
710 PrintDebug("Returning from large page->small page fault handler\n");
717 static int invalidation_cb_64(struct guest_info * info, page_type_t type,
718 addr_t vaddr, addr_t page_ptr, addr_t page_pa,
719 void * private_data) {
724 pml4e64_t * pml = (pml4e64_t *)page_ptr;
726 if (pml[PML4E64_INDEX(vaddr)].present == 0) {
733 pdpe64_t * pdp = (pdpe64_t *)page_ptr;
734 pdpe64_t * pdpe = &(pdp[PDPE64_INDEX(vaddr)]);
736 if (pdpe->present == 0) {
740 if (pdpe->vmm_info == V3_LARGE_PG) {
741 PrintError("1 Gigabyte pages not supported\n");
752 pde64_t * pd = (pde64_t *)page_ptr;
753 pde64_t * pde = &(pd[PDE64_INDEX(vaddr)]);
755 if (pde->present == 0) {
759 if (pde->vmm_info == V3_LARGE_PG) {
768 pte64_t * pt = (pte64_t *)page_ptr;
770 pt[PTE64_INDEX(vaddr)].present = 0;
775 PrintError("Invalid Page Type\n");
780 // should not get here
781 PrintError("Should not get here....\n");
786 static inline int handle_shadow_invlpg_64(struct guest_info * info, addr_t vaddr) {
787 PrintDebug("INVLPG64 - %p\n",(void*)vaddr);
789 int ret = v3_drill_host_pt_64(info, info->ctrl_regs.cr3, vaddr, invalidation_cb_64, NULL);
791 PrintError("Page table drill returned error.... \n");
792 PrintHostPageTree(info, vaddr, info->ctrl_regs.cr3);
795 return (ret == -1) ? -1 : 0;