1 #include <palacios/vmm_shadow_paging.h>
4 #include <palacios/vmm.h>
5 #include <palacios/vm_guest_mem.h>
6 #include <palacios/vmm_decoder.h>
8 #ifndef DEBUG_SHADOW_PAGING
10 #define PrintDebug(fmt, args...)
22 DEFINE_HASHTABLE_INSERT(add_cr3_to_cache, addr_t, struct hashtable *);
23 DEFINE_HASHTABLE_SEARCH(find_cr3_in_cache, addr_t, struct hashtable *);
24 DEFINE_HASHTABLE_REMOVE(del_cr3_from_cache, addr_t, struct hashtable *, 0);
27 DEFINE_HASHTABLE_INSERT(add_pte_map, addr_t, addr_t);
28 DEFINE_HASHTABLE_SEARCH(find_pte_map, addr_t, addr_t);
29 DEFINE_HASHTABLE_REMOVE(del_pte_map, addr_t, addr_t, 0);
34 static uint_t pte_hash_fn(addr_t key) {
35 return hash_long(key, 32);
38 static int pte_equals(addr_t key1, addr_t key2) {
39 return (key1 == key2);
42 static uint_t cr3_hash_fn(addr_t key) {
43 return hash_long(key, 32);
46 static int cr3_equals(addr_t key1, addr_t key2) {
47 return (key1 == key2);
51 static int handle_shadow_pte32_fault(struct guest_info* info,
53 pf_error_t error_code,
57 static int handle_shadow_pagefault32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
59 int init_shadow_page_state(struct guest_info * info) {
60 struct shadow_page_state * state = &(info->shdw_pg_state);
61 state->guest_mode = PDE32;
62 state->shadow_mode = PDE32;
65 state->shadow_cr3 = 0;
68 state->cr3_cache = create_hashtable(0, &cr3_hash_fn, &cr3_equals);
70 state->cached_cr3 = 0;
71 state->cached_ptes = NULL;
80 For now we'll do something a little more lightweight
81 int cache_page_tables32(struct guest_info * info, addr_t pde) {
82 struct shadow_page_state * state = &(info->shdw_pg_state);
85 struct hashtable * pte_cache = NULL;
89 pte_cache = (struct hashtable *)find_cr3_in_cache(state->cr3_cache, pde);
90 if (pte_cache != NULL) {
91 PrintError("CR3 already present in cache\n");
92 state->current_ptes = pte_cache;
95 PrintError("Creating new CR3 cache entry\n");
96 pte_cache = create_hashtable(0, &pte_hash_fn, &pte_equals);
97 state->current_ptes = pte_cache;
98 add_cr3_to_cache(state->cr3_cache, pde, pte_cache);
101 if (guest_pa_to_host_va(info, pde, &pde_host_addr) == -1) {
102 PrintError("Could not lookup host address of guest PDE\n");
106 tmp_pde = (pde32_t *)pde_host_addr;
108 add_pte_map(pte_cache, pde, pde_host_addr);
111 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
112 if ((tmp_pde[i].present) && (tmp_pde[i].large_page == 0)) {
113 addr_t pte_host_addr;
115 if (guest_pa_to_host_va(info, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), &pte_host_addr) == -1) {
116 PrintError("Could not lookup host address of guest PDE\n");
120 add_pte_map(pte_cache, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), pte_host_addr);
129 int cache_page_tables32(struct guest_info * info, addr_t pde) {
130 struct shadow_page_state * state = &(info->shdw_pg_state);
131 addr_t pde_host_addr;
133 struct hashtable * pte_cache = NULL;
136 if (pde == state->cached_cr3) {
140 if (state->cached_ptes != NULL) {
141 hashtable_destroy(state->cached_ptes, 0, 0);
142 state->cached_ptes = NULL;
145 state->cached_cr3 = pde;
147 pte_cache = create_hashtable(0, &pte_hash_fn, &pte_equals);
148 state->cached_ptes = pte_cache;
150 if (guest_pa_to_host_pa(info, pde, &pde_host_addr) == -1) {
151 PrintError("Could not lookup host address of guest PDE\n");
155 tmp_pde = (pde32_t *)pde_host_addr;
157 add_pte_map(pte_cache, pde, pde_host_addr);
160 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
161 if ((tmp_pde[i].present) && (tmp_pde[i].large_page == 0)) {
162 addr_t pte_host_addr;
164 if (guest_pa_to_host_pa(info, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), &pte_host_addr) == -1) {
165 PrintError("Could not lookup host address of guest PDE\n");
169 add_pte_map(pte_cache, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), pte_host_addr);
179 int v3_replace_shdw_page32(struct guest_info * info, addr_t location, pte32_t * new_page, pte32_t * old_page) {
180 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
181 pde32_t * shadow_pde = (pde32_t *)&(shadow_pd[PDE32_INDEX(location)]);
183 if (shadow_pde->large_page == 0) {
184 pte32_t * shadow_pt = (pte32_t *)PDE32_T_ADDR((*shadow_pde));
185 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(location)]);
187 //if (shadow_pte->present == 1) {
188 *(uint_t *)old_page = *(uint_t *)shadow_pte;
191 *(uint_t *)shadow_pte = *(uint_t *)new_page;
194 // currently unhandled
206 int handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
208 if (info->mem_mode == PHYSICAL_MEM) {
209 // If paging is not turned on we need to handle the special cases
210 return handle_special_page_fault(info, fault_addr, fault_addr, error_code);
211 } else if (info->mem_mode == VIRTUAL_MEM) {
213 switch (info->cpu_mode) {
215 return handle_shadow_pagefault32(info, fault_addr, error_code);
220 PrintError("Unhandled CPU Mode\n");
224 PrintError("Invalid Memory mode\n");
229 addr_t create_new_shadow_pt32() {
232 host_pde = V3_AllocPages(1);
233 memset(host_pde, 0, PAGE_SIZE);
235 return (addr_t)host_pde;
239 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
240 info->ctrl_regs.cr2 = fault_addr;
241 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
245 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
246 /* basically the reasoning is that there can be multiple reasons for a page fault:
247 If there is a permissions failure for a page present in the guest _BUT_
248 the reason for the fault was that the page is not present in the shadow,
249 _THEN_ we have to map the shadow page in and reexecute, this will generate
250 a permissions fault which is _THEN_ valid to send to the guest
251 _UNLESS_ both the guest and shadow have marked the page as not present
255 if (guest_access != PT_ACCESS_OK) {
256 // Guest Access Error
258 if ((shadow_access != PT_ENTRY_NOT_PRESENT) &&
259 (guest_access != PT_ENTRY_NOT_PRESENT)) {
260 // aka (guest permission error)
264 if ((shadow_access == PT_ENTRY_NOT_PRESENT) &&
265 (guest_access == PT_ENTRY_NOT_PRESENT)) {
266 // Page tables completely blank, handle guest first
270 // Otherwise we'll handle the guest fault later...?
279 /* The guest status checks have already been done,
280 * only special case shadow checks remain
282 static int handle_large_pagefault32(struct guest_info * info,
283 addr_t fault_addr, pf_error_t error_code,
284 pte32_t * shadow_pt, pde32_4MB_t * large_guest_pde)
286 pt_access_status_t shadow_pte_access = can_access_pte32(shadow_pt, fault_addr, error_code);
287 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
289 if (shadow_pte_access == PT_ACCESS_OK) {
290 // Inconsistent state...
291 // Guest Re-Entry will flush tables and everything should now workd
292 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
297 if (shadow_pte_access == PT_ENTRY_NOT_PRESENT) {
298 // Get the guest physical address of the fault
299 addr_t guest_fault_pa = PDE32_4MB_T_ADDR(*large_guest_pde) + PD32_4MB_PAGE_OFFSET(fault_addr);
300 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_fault_pa);
303 if (host_page_type == HOST_REGION_INVALID) {
304 // Inject a machine check in the guest
305 PrintDebug("Invalid Guest Address in page table (0x%x)\n", guest_fault_pa);
306 v3_raise_exception(info, MC_EXCEPTION);
310 if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
311 struct shadow_page_state * state = &(info->shdw_pg_state);
312 addr_t shadow_pa = get_shadow_addr(info, guest_fault_pa);
314 shadow_pte->page_base_addr = PT32_BASE_ADDR(shadow_pa);
316 shadow_pte->present = 1;
318 /* We are assuming that the PDE entry has precedence
319 * so the Shadow PDE will mirror the guest PDE settings,
320 * and we don't have to worry about them here
323 shadow_pte->user_page = 1;
325 if (find_pte_map(state->cached_ptes, PT32_PAGE_ADDR(guest_fault_pa)) != NULL) {
326 // Check if the entry is a page table...
327 PrintDebug("Marking page as Guest Page Table (large page)\n");
328 shadow_pte->vmm_info = PT32_GUEST_PT;
329 shadow_pte->writable = 0;
331 shadow_pte->writable = 1;
335 //set according to VMM policy
336 shadow_pte->write_through = 0;
337 shadow_pte->cache_disable = 0;
338 shadow_pte->global_page = 0;
342 // Handle hooked pages as well as other special pages
343 if (handle_special_page_fault(info, fault_addr, guest_fault_pa, error_code) == -1) {
344 PrintError("Special Page Fault handler returned error for address: %x\n", fault_addr);
348 } else if ((shadow_pte_access == PT_WRITE_ERROR) &&
349 (shadow_pte->vmm_info == PT32_GUEST_PT)) {
351 struct shadow_page_state * state = &(info->shdw_pg_state);
352 PrintDebug("Write operation on Guest PAge Table Page (large page)\n");
353 state->cached_cr3 = 0;
354 shadow_pte->writable = 1;
357 PrintError("Error in large page fault handler...\n");
358 PrintError("This case should have been handled at the top level handler\n");
362 PrintDebug("Returning from large page fault handler\n");
367 static int handle_shadow_pagefault32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
368 pde32_t * guest_pd = NULL;
369 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
370 addr_t guest_cr3 = CR3_TO_PDE32(info->shdw_pg_state.guest_cr3);
371 pt_access_status_t guest_pde_access;
372 pt_access_status_t shadow_pde_access;
373 pde32_t * guest_pde = NULL;
374 pde32_t * shadow_pde = (pde32_t *)&(shadow_pd[PDE32_INDEX(fault_addr)]);
376 PrintDebug("Shadow page fault handler\n");
378 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
379 PrintError("Invalid Guest PDE Address: 0x%x\n", guest_cr3);
383 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(fault_addr)]);
386 // Check the guest page permissions
387 guest_pde_access = can_access_pde32(guest_pd, fault_addr, error_code);
389 // Check the shadow page permissions
390 shadow_pde_access = can_access_pde32(shadow_pd, fault_addr, error_code);
392 /* Was the page fault caused by the Guest's page tables? */
393 if (is_guest_pf(guest_pde_access, shadow_pde_access) == 1) {
394 PrintDebug("Injecting PDE pf to guest: (guest access error=%d) (pf error code=%d)\n",
395 guest_pde_access, error_code);
396 inject_guest_pf(info, fault_addr, error_code);
401 if (shadow_pde_access == PT_ENTRY_NOT_PRESENT)
403 pte32_t * shadow_pt = (pte32_t *)create_new_shadow_pt32();
405 shadow_pde->present = 1;
406 shadow_pde->user_page = guest_pde->user_page;
407 // shadow_pde->large_page = guest_pde->large_page;
408 shadow_pde->large_page = 0;
411 // VMM Specific options
412 shadow_pde->write_through = 0;
413 shadow_pde->cache_disable = 0;
414 shadow_pde->global_page = 0;
417 guest_pde->accessed = 1;
419 shadow_pde->pt_base_addr = PD32_BASE_ADDR(shadow_pt);
421 if (guest_pde->large_page == 0) {
422 shadow_pde->writable = guest_pde->writable;
424 ((pde32_4MB_t *)guest_pde)->dirty = 0;
425 shadow_pde->writable = 0;
428 else if (shadow_pde_access == PT_ACCESS_OK)
433 pte32_t * shadow_pt = (pte32_t *)PDE32_T_ADDR((*shadow_pde));
435 if (guest_pde->large_page == 0) {
436 pte32_t * guest_pt = NULL;
437 if (guest_pa_to_host_va(info, PDE32_T_ADDR((*guest_pde)), (addr_t*)&guest_pt) == -1) {
438 // Machine check the guest
439 PrintDebug("Invalid Guest PTE Address: 0x%x\n", PDE32_T_ADDR((*guest_pde)));
440 v3_raise_exception(info, MC_EXCEPTION);
444 if (handle_shadow_pte32_fault(info, fault_addr, error_code, shadow_pt, guest_pt) == -1) {
445 PrintError("Error handling Page fault caused by PTE\n");
448 } else if (guest_pde->large_page == 1) {
449 if (handle_large_pagefault32(info, fault_addr, error_code, shadow_pt, (pde32_4MB_t *)guest_pde) == -1) {
450 PrintError("Error handling large pagefault\n");
455 else if ((shadow_pde_access == PT_WRITE_ERROR) &&
456 (guest_pde->large_page == 1) &&
457 (((pde32_4MB_t *)guest_pde)->dirty == 0))
460 // Page Directory Entry marked read-only
461 // Its a large page and we need to update the dirty bit in the guest
464 PrintDebug("Large page write error... Setting dirty bit and returning\n");
465 ((pde32_4MB_t *)guest_pde)->dirty = 1;
466 shadow_pde->writable = guest_pde->writable;
470 else if (shadow_pde_access == PT_USER_ERROR)
473 // Page Directory Entry marked non-user
475 PrintDebug("Shadow Paging User access error (shadow_pde_access=0x%x, guest_pde_access=0x%x)\n",
476 shadow_pde_access, guest_pde_access);
477 inject_guest_pf(info, fault_addr, error_code);
482 // inject page fault in guest
483 inject_guest_pf(info, fault_addr, error_code);
484 PrintDebug("Unknown Error occurred (shadow_pde_access=%d)\n", shadow_pde_access);
485 PrintDebug("Manual Says to inject page fault into guest\n");
486 #ifdef DEBUG_SHADOW_PAGING
487 PrintDebug("Guest PDE: (access=%d)\n\t", guest_pde_access);
488 PrintPDE32(fault_addr, guest_pde);
489 PrintDebug("Shadow PDE: (access=%d)\n\t", shadow_pde_access);
490 PrintPDE32(fault_addr, shadow_pde);
496 PrintDebug("Returning end of PDE function (rip=%x)\n", info->rip);
503 * We assume the the guest pte pointer has already been translated to a host virtual address
505 static int handle_shadow_pte32_fault(struct guest_info * info,
507 pf_error_t error_code,
509 pte32_t * guest_pt) {
511 pt_access_status_t guest_pte_access;
512 pt_access_status_t shadow_pte_access;
513 pte32_t * guest_pte = (pte32_t *)&(guest_pt[PTE32_INDEX(fault_addr)]);;
514 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
517 // Check the guest page permissions
518 guest_pte_access = can_access_pte32(guest_pt, fault_addr, error_code);
520 // Check the shadow page permissions
521 shadow_pte_access = can_access_pte32(shadow_pt, fault_addr, error_code);
523 #ifdef DEBUG_SHADOW_PAGING
524 PrintDebug("Guest PTE: (access=%d)\n\t", guest_pte_access);
525 PrintPTE32(fault_addr, guest_pte);
526 PrintDebug("Shadow PTE: (access=%d)\n\t", shadow_pte_access);
527 PrintPTE32(fault_addr, shadow_pte);
530 /* Was the page fault caused by the Guest's page tables? */
531 if (is_guest_pf(guest_pte_access, shadow_pte_access) == 1) {
532 PrintDebug("Access error injecting pf to guest (guest access error=%d) (pf error code=%d)\n",
533 guest_pte_access, *(uint_t*)&error_code);
534 inject_guest_pf(info, fault_addr, error_code);
539 if (shadow_pte_access == PT_ACCESS_OK) {
540 // Inconsistent state...
541 // Guest Re-Entry will flush page tables and everything should now work
542 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
547 if (shadow_pte_access == PT_ENTRY_NOT_PRESENT) {
549 addr_t guest_pa = PTE32_T_ADDR((*guest_pte)) + PT32_PAGE_OFFSET(fault_addr);
551 // Page Table Entry Not Present
553 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_pa);
555 if (host_page_type == HOST_REGION_INVALID) {
556 // Inject a machine check in the guest
557 PrintDebug("Invalid Guest Address in page table (0x%x)\n", guest_pa);
558 v3_raise_exception(info, MC_EXCEPTION);
564 if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
565 struct shadow_page_state * state = &(info->shdw_pg_state);
566 addr_t shadow_pa = get_shadow_addr(info, guest_pa);
568 shadow_pte->page_base_addr = PT32_BASE_ADDR(shadow_pa);
570 shadow_pte->present = guest_pte->present;
571 shadow_pte->user_page = guest_pte->user_page;
573 //set according to VMM policy
574 shadow_pte->write_through = 0;
575 shadow_pte->cache_disable = 0;
576 shadow_pte->global_page = 0;
579 guest_pte->accessed = 1;
581 if (find_pte_map(state->cached_ptes, PT32_PAGE_ADDR(guest_pa)) != NULL) {
582 // Check if the entry is a page table...
583 PrintDebug("Marking page as Guest Page Table\n", shadow_pte->writable);
584 shadow_pte->vmm_info = PT32_GUEST_PT;
587 if (guest_pte->dirty == 1) {
588 shadow_pte->writable = guest_pte->writable;
589 } else if ((guest_pte->dirty == 0) && (error_code.write == 1)) {
590 shadow_pte->writable = guest_pte->writable;
591 guest_pte->dirty = 1;
593 if (shadow_pte->vmm_info == PT32_GUEST_PT) {
594 // Well that was quick...
595 struct shadow_page_state * state = &(info->shdw_pg_state);
596 PrintDebug("Immediate Write operation on Guest PAge Table Page\n");
597 state->cached_cr3 = 0;
600 } else if ((guest_pte->dirty = 0) && (error_code.write == 0)) {
601 shadow_pte->writable = 0;
607 // Page fault handled by hook functions
608 if (handle_special_page_fault(info, fault_addr, guest_pa, error_code) == -1) {
609 PrintError("Special Page fault handler returned error for address: %x\n", fault_addr);
614 } else if ((shadow_pte_access == PT_WRITE_ERROR) &&
615 (guest_pte->dirty == 0)) {
617 PrintDebug("Shadow PTE Write Error\n");
618 guest_pte->dirty = 1;
619 shadow_pte->writable = guest_pte->writable;
621 if (shadow_pte->vmm_info == PT32_GUEST_PT) {
622 struct shadow_page_state * state = &(info->shdw_pg_state);
623 PrintDebug("Write operation on Guest PAge Table Page\n");
624 state->cached_cr3 = 0;
630 // Inject page fault into the guest
631 inject_guest_pf(info, fault_addr, error_code);
632 PrintError("PTE Page fault fell through... Not sure if this should ever happen\n");
633 PrintError("Manual Says to inject page fault into guest\n");
637 PrintDebug("Returning end of function\n");
646 /* Currently Does not work with Segmentation!!! */
647 int handle_shadow_invlpg(struct guest_info * info) {
648 if (info->mem_mode != VIRTUAL_MEM) {
649 // Paging must be turned on...
650 // should handle with some sort of fault I think
651 PrintError("ERROR: INVLPG called in non paged mode\n");
656 if (info->cpu_mode == PROTECTED) {
661 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
663 PrintError("Could not read instruction 0x%x (ret=%d)\n", info->rip, ret);
668 /* Can INVLPG work with Segments?? */
669 while (is_prefix_byte(instr[index])) {
674 if ((instr[index] == (uchar_t)0x0f) &&
675 (instr[index + 1] == (uchar_t)0x01)) {
677 addr_t first_operand;
678 addr_t second_operand;
679 operand_type_t addr_type;
680 addr_t guest_cr3 = CR3_TO_PDE32(info->shdw_pg_state.guest_cr3);
682 pde32_t * guest_pd = NULL;
684 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
685 PrintError("Invalid Guest PDE Address: 0x%x\n", guest_cr3);
694 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
696 if (addr_type == MEM_OPERAND) {
697 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
698 pde32_t * shadow_pde = (pde32_t *)&shadow_pd[PDE32_INDEX(first_operand)];
701 //PrintDebug("PDE Index=%d\n", PDE32_INDEX(first_operand));
702 //PrintDebug("FirstOperand = %x\n", first_operand);
704 PrintDebug("Invalidating page for %x\n", first_operand);
706 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(first_operand)]);
708 if (guest_pde->large_page == 1) {
709 shadow_pde->present = 0;
710 PrintDebug("Invalidating Large Page\n");
713 if (shadow_pde->present == 1) {
714 pte32_t * shadow_pt = (pte32_t *)PDE32_T_ADDR((*shadow_pde));
715 pte32_t * shadow_pte = (pte32_t *)&shadow_pt[PTE32_INDEX(first_operand)];
717 #ifdef DEBUG_SHADOW_PAGING
718 PrintDebug("Setting not present\n");
719 PrintPTE32(first_operand, shadow_pte);
722 shadow_pte->present = 0;
729 PrintError("Invalid Operand type\n");
733 PrintError("invalid Instruction Opcode\n");
734 PrintTraceMemDump(instr, 15);
746 static int create_pd32_nonaligned_4MB_page(struct guest_info * info, pte32_t * pt, addr_t guest_addr, pde32_4MB_t * large_shadow_pde) {
748 pte32_t * pte_cursor;
751 for (i = 0; i < 1024; i++) {
752 guest_pa = guest_addr + (PAGE_SIZE * i);
753 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_pa);
755 pte_cursor = &(pt[i]);
757 if (host_page_type == HOST_REGION_INVALID) {
758 // Currently we don't support this, but in theory we could
759 PrintError("Invalid Host Memory Type\n");
761 } else if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
762 addr_t shadow_pa = get_shadow_addr(info, guest_pa);
765 pte_cursor->page_base_addr = PT32_BASE_ADDR(shadow_pa);
766 pte_cursor->present = 1;
767 pte_cursor->writable = large_shadow_pde->writable;
768 pte_cursor->user_page = large_shadow_pde->user_page;
769 pte_cursor->write_through = 0;
770 pte_cursor->cache_disable = 0;
771 pte_cursor->global_page = 0;
774 PrintError("Unsupported Host Memory Type\n");
782 static int handle_large_pagefault32(struct guest_info * info,
783 pde32_t * guest_pde, pde32_t * shadow_pde,
784 addr_t fault_addr, pf_error_t error_code ) {
785 struct shadow_region * mem_reg;
786 pde32_4MB_t * large_guest_pde = (pde32_4MB_t *)guest_pde;
787 pde32_4MB_t * large_shadow_pde = (pde32_4MB_t *)shadow_pde;
788 host_region_type_t host_page_type;
789 addr_t guest_start_addr = PDE32_4MB_T_ADDR(*large_guest_pde);
790 // addr_t guest_end_addr = guest_start_addr + PAGE_SIZE_4MB; // start address + 4MB
793 // Check that the Guest PDE entry points to valid memory
794 // else Machine Check the guest
795 PrintDebug("Large Page: Page Base Addr=%x\n", guest_start_addr);
797 host_page_type = get_shadow_addr_type(info, guest_start_addr);
799 if (host_page_type == HOST_REGION_INVALID) {
800 PrintError("Invalid guest address in large page (0x%x)\n", guest_start_addr);
801 v3_raise_exception(info, MC_EXCEPTION);
807 if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
809 addr_t host_start_addr = 0;
810 addr_t region_end_addr = 0;
812 // Check for a large enough region in host memory
813 mem_reg = get_shadow_region_by_addr(&(info->mem_map), guest_start_addr);
814 PrintDebug("Host region: host_addr=%x (guest_start=%x, end=%x)\n",
815 mem_reg->host_addr, mem_reg->guest_start, mem_reg->guest_end);
816 host_start_addr = mem_reg->host_addr + (guest_start_addr - mem_reg->guest_start);
817 region_end_addr = mem_reg->host_addr + (mem_reg->guest_end - mem_reg->guest_start);
819 PrintDebug("Host Start Addr=%x; Region End Addr=%x\n", host_start_addr, region_end_addr);
823 if (large_guest_pde->dirty == 1) { // dirty
824 large_shadow_pde->writable = guest_pde->writable;
825 } else if (error_code.write == 1) { // not dirty, access is write
826 large_shadow_pde->writable = guest_pde->writable;
827 large_guest_pde->dirty = 1;
828 } else { // not dirty, access is read
829 large_shadow_pde->writable = 0;
833 // Check if the region is at least an additional 4MB
837 if ((PD32_4MB_PAGE_OFFSET(host_start_addr) == 0) &&
838 (region_end_addr >= host_start_addr + PAGE_SIZE_4MB)) { // if 4MB boundary
839 large_shadow_pde->page_base_addr = PD32_4MB_BASE_ADDR(host_start_addr);
840 } else { // else generate 4k pages
841 pte32_t * shadow_pt = NULL;
842 PrintDebug("Handling non aligned large page\n");
844 shadow_pde->large_page = 0;
846 shadow_pt = create_new_shadow_pt32();
848 if (create_pd32_nonaligned_4MB_page(info, shadow_pt, guest_start_addr, large_shadow_pde) == -1) {
849 PrintError("Non Aligned Large Page Error\n");
855 #ifdef DEBUG_SHADOW_PAGING
856 PrintDebug("non-aligned Shadow PT\n");
857 PrintPT32(PT32_PAGE_ADDR(fault_addr), shadow_pt);
859 shadow_pde->pt_base_addr = PD32_BASE_ADDR(shadow_pt);
863 // Handle hooked pages as well as other special pages
864 if (handle_special_page_fault(info, fault_addr, guest_start_addr, error_code) == -1) {
865 PrintError("Special Page Fault handler returned error for address: %x\n", fault_addr);