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 #include <palacios/vmm_shadow_paging.h>
24 #include <palacios/vmm.h>
25 #include <palacios/vm_guest_mem.h>
26 #include <palacios/vmm_decoder.h>
28 #ifndef DEBUG_SHADOW_PAGING
30 #define PrintDebug(fmt, args...)
42 DEFINE_HASHTABLE_INSERT(add_cr3_to_cache, addr_t, struct hashtable *);
43 DEFINE_HASHTABLE_SEARCH(find_cr3_in_cache, addr_t, struct hashtable *);
44 DEFINE_HASHTABLE_REMOVE(del_cr3_from_cache, addr_t, struct hashtable *, 0);
47 DEFINE_HASHTABLE_INSERT(add_pte_map, addr_t, addr_t);
48 DEFINE_HASHTABLE_SEARCH(find_pte_map, addr_t, addr_t);
49 DEFINE_HASHTABLE_REMOVE(del_pte_map, addr_t, addr_t, 0);
54 static uint_t pte_hash_fn(addr_t key) {
55 return hash_long(key, 32);
58 static int pte_equals(addr_t key1, addr_t key2) {
59 return (key1 == key2);
62 static uint_t cr3_hash_fn(addr_t key) {
63 return hash_long(key, 32);
66 static int cr3_equals(addr_t key1, addr_t key2) {
67 return (key1 == key2);
71 static int handle_shadow_pte32_fault(struct guest_info* info,
73 pf_error_t error_code,
77 static int handle_shadow_pagefault32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
79 int v3_init_shadow_page_state(struct guest_info * info) {
80 struct shadow_page_state * state = &(info->shdw_pg_state);
83 state->shadow_cr3 = 0;
86 state->cr3_cache = create_hashtable(0, &cr3_hash_fn, &cr3_equals);
88 state->cached_cr3 = 0;
89 state->cached_ptes = NULL;
98 For now we'll do something a little more lightweight
99 int cache_page_tables32(struct guest_info * info, addr_t pde) {
100 struct shadow_page_state * state = &(info->shdw_pg_state);
101 addr_t pde_host_addr;
103 struct hashtable * pte_cache = NULL;
107 pte_cache = (struct hashtable *)find_cr3_in_cache(state->cr3_cache, pde);
108 if (pte_cache != NULL) {
109 PrintError("CR3 already present in cache\n");
110 state->current_ptes = pte_cache;
113 PrintError("Creating new CR3 cache entry\n");
114 pte_cache = create_hashtable(0, &pte_hash_fn, &pte_equals);
115 state->current_ptes = pte_cache;
116 add_cr3_to_cache(state->cr3_cache, pde, pte_cache);
119 if (guest_pa_to_host_va(info, pde, &pde_host_addr) == -1) {
120 PrintError("Could not lookup host address of guest PDE\n");
124 tmp_pde = (pde32_t *)pde_host_addr;
126 add_pte_map(pte_cache, pde, pde_host_addr);
129 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
130 if ((tmp_pde[i].present) && (tmp_pde[i].large_page == 0)) {
131 addr_t pte_host_addr;
133 if (guest_pa_to_host_va(info, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), &pte_host_addr) == -1) {
134 PrintError("Could not lookup host address of guest PDE\n");
138 add_pte_map(pte_cache, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), pte_host_addr);
147 int v3_cache_page_tables32(struct guest_info * info, addr_t pde) {
148 struct shadow_page_state * state = &(info->shdw_pg_state);
149 addr_t pde_host_addr;
151 struct hashtable * pte_cache = NULL;
154 if (pde == state->cached_cr3) {
158 if (state->cached_ptes != NULL) {
159 hashtable_destroy(state->cached_ptes, 0, 0);
160 state->cached_ptes = NULL;
163 state->cached_cr3 = pde;
165 pte_cache = create_hashtable(0, &pte_hash_fn, &pte_equals);
166 state->cached_ptes = pte_cache;
168 if (guest_pa_to_host_va(info, pde, &pde_host_addr) == -1) {
169 PrintError("Could not lookup host address of guest PDE\n");
173 tmp_pde = (pde32_t *)pde_host_addr;
175 add_pte_map(pte_cache, pde, pde_host_addr);
178 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
179 if ((tmp_pde[i].present) && (tmp_pde[i].large_page == 0)) {
180 addr_t pte_host_addr;
182 if (guest_pa_to_host_va(info, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), &pte_host_addr) == -1) {
183 PrintError("Could not lookup host address of guest PDE\n");
187 add_pte_map(pte_cache, (addr_t)(PDE32_T_ADDR(tmp_pde[i])), pte_host_addr);
197 int v3_replace_shdw_page32(struct guest_info * info, addr_t location, pte32_t * new_page, pte32_t * old_page) {
198 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
199 pde32_t * shadow_pde = (pde32_t *)&(shadow_pd[PDE32_INDEX(location)]);
201 if (shadow_pde->large_page == 0) {
202 pte32_t * shadow_pt = (pte32_t *)(addr_t)PDE32_T_ADDR((*shadow_pde));
203 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(location)]);
205 //if (shadow_pte->present == 1) {
206 *(uint_t *)old_page = *(uint_t *)shadow_pte;
209 *(uint_t *)shadow_pte = *(uint_t *)new_page;
212 // currently unhandled
224 int v3_handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
226 if (info->mem_mode == PHYSICAL_MEM) {
227 // If paging is not turned on we need to handle the special cases
229 #ifdef DEBUG_SHADOW_PAGING
230 PrintPageTree(info->cpu_mode, fault_addr, info->ctrl_regs.cr3);
233 return handle_special_page_fault(info, fault_addr, fault_addr, error_code);
234 } else if (info->mem_mode == VIRTUAL_MEM) {
236 switch (info->cpu_mode) {
238 return handle_shadow_pagefault32(info, fault_addr, error_code);
243 PrintError("Unhandled CPU Mode\n");
247 PrintError("Invalid Memory mode\n");
252 addr_t v3_create_new_shadow_pt() {
255 host_pde = V3_VAddr(V3_AllocPages(1));
256 memset(host_pde, 0, PAGE_SIZE);
258 return (addr_t)host_pde;
262 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
263 info->ctrl_regs.cr2 = fault_addr;
264 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
268 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
269 /* basically the reasoning is that there can be multiple reasons for a page fault:
270 If there is a permissions failure for a page present in the guest _BUT_
271 the reason for the fault was that the page is not present in the shadow,
272 _THEN_ we have to map the shadow page in and reexecute, this will generate
273 a permissions fault which is _THEN_ valid to send to the guest
274 _UNLESS_ both the guest and shadow have marked the page as not present
278 if (guest_access != PT_ACCESS_OK) {
279 // Guest Access Error
281 if ((shadow_access != PT_ENTRY_NOT_PRESENT) &&
282 (guest_access != PT_ENTRY_NOT_PRESENT)) {
283 // aka (guest permission error)
287 if ((shadow_access == PT_ENTRY_NOT_PRESENT) &&
288 (guest_access == PT_ENTRY_NOT_PRESENT)) {
289 // Page tables completely blank, handle guest first
293 // Otherwise we'll handle the guest fault later...?
302 /* The guest status checks have already been done,
303 * only special case shadow checks remain
305 static int handle_large_pagefault32(struct guest_info * info,
306 addr_t fault_addr, pf_error_t error_code,
307 pte32_t * shadow_pt, pde32_4MB_t * large_guest_pde)
309 pt_access_status_t shadow_pte_access = can_access_pte32(shadow_pt, fault_addr, error_code);
310 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
312 if (shadow_pte_access == PT_ACCESS_OK) {
313 // Inconsistent state...
314 // Guest Re-Entry will flush tables and everything should now workd
315 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
320 if (shadow_pte_access == PT_ENTRY_NOT_PRESENT) {
321 // Get the guest physical address of the fault
322 addr_t guest_fault_pa = PDE32_4MB_T_ADDR(*large_guest_pde) + PD32_4MB_PAGE_OFFSET(fault_addr);
323 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_fault_pa);
326 if (host_page_type == HOST_REGION_INVALID) {
327 // Inject a machine check in the guest
328 PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_fault_pa);
329 v3_raise_exception(info, MC_EXCEPTION);
333 if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
334 struct shadow_page_state * state = &(info->shdw_pg_state);
335 addr_t shadow_pa = get_shadow_addr(info, guest_fault_pa);
337 shadow_pte->page_base_addr = PT32_BASE_ADDR(shadow_pa);
339 shadow_pte->present = 1;
341 /* We are assuming that the PDE entry has precedence
342 * so the Shadow PDE will mirror the guest PDE settings,
343 * and we don't have to worry about them here
346 shadow_pte->user_page = 1;
348 if (find_pte_map(state->cached_ptes, PT32_PAGE_ADDR(guest_fault_pa)) != NULL) {
349 // Check if the entry is a page table...
350 PrintDebug("Marking page as Guest Page Table (large page)\n");
351 shadow_pte->vmm_info = PT32_GUEST_PT;
352 shadow_pte->writable = 0;
354 shadow_pte->writable = 1;
358 //set according to VMM policy
359 shadow_pte->write_through = 0;
360 shadow_pte->cache_disable = 0;
361 shadow_pte->global_page = 0;
365 // Handle hooked pages as well as other special pages
366 if (handle_special_page_fault(info, fault_addr, guest_fault_pa, error_code) == -1) {
367 PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
371 } else if ((shadow_pte_access == PT_WRITE_ERROR) &&
372 (shadow_pte->vmm_info == PT32_GUEST_PT)) {
374 struct shadow_page_state * state = &(info->shdw_pg_state);
375 PrintDebug("Write operation on Guest PAge Table Page (large page)\n");
376 state->cached_cr3 = 0;
377 shadow_pte->writable = 1;
380 PrintError("Error in large page fault handler...\n");
381 PrintError("This case should have been handled at the top level handler\n");
385 PrintDebug("Returning from large page fault handler\n");
390 static int handle_shadow_pagefault32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
391 pde32_t * guest_pd = NULL;
392 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
393 addr_t guest_cr3 = (addr_t) V3_PAddr( CR3_TO_PDE32(info->shdw_pg_state.guest_cr3) );
394 pt_access_status_t guest_pde_access;
395 pt_access_status_t shadow_pde_access;
396 pde32_t * guest_pde = NULL;
397 pde32_t * shadow_pde = (pde32_t *)&(shadow_pd[PDE32_INDEX(fault_addr)]);
399 PrintDebug("Shadow page fault handler: %p\n", (void*) fault_addr );
401 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
402 PrintError("Invalid Guest PDE Address: 0x%p\n", (void *)guest_cr3);
406 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(fault_addr)]);
409 // Check the guest page permissions
410 guest_pde_access = can_access_pde32(guest_pd, fault_addr, error_code);
412 // Check the shadow page permissions
413 shadow_pde_access = can_access_pde32(shadow_pd, fault_addr, error_code);
415 /* Was the page fault caused by the Guest's page tables? */
416 if (is_guest_pf(guest_pde_access, shadow_pde_access) == 1) {
417 PrintDebug("Injecting PDE pf to guest: (guest access error=%d) (pf error code=%d)\n",
418 *(uint_t *)&guest_pde_access, *(uint_t *)&error_code);
419 inject_guest_pf(info, fault_addr, error_code);
424 if (shadow_pde_access == PT_ENTRY_NOT_PRESENT)
426 pte32_t * shadow_pt = (pte32_t *)v3_create_new_shadow_pt();
428 shadow_pde->present = 1;
429 shadow_pde->user_page = guest_pde->user_page;
430 // shadow_pde->large_page = guest_pde->large_page;
431 shadow_pde->large_page = 0;
434 // VMM Specific options
435 shadow_pde->write_through = 0;
436 shadow_pde->cache_disable = 0;
437 shadow_pde->global_page = 0;
440 guest_pde->accessed = 1;
442 shadow_pde->pt_base_addr = PD32_BASE_ADDR((addr_t)V3_PAddr(shadow_pt));
444 if (guest_pde->large_page == 0) {
445 shadow_pde->writable = guest_pde->writable;
447 // ?? What if guest pde is dirty a this point?
448 ((pde32_4MB_t *)guest_pde)->dirty = 0;
449 shadow_pde->writable = 0;
452 else if (shadow_pde_access == PT_ACCESS_OK)
457 pte32_t * shadow_pt = (pte32_t *)V3_VAddr( (void*)(addr_t) PDE32_T_ADDR(*shadow_pde) );
459 if (guest_pde->large_page == 0) {
460 pte32_t * guest_pt = NULL;
461 if (guest_pa_to_host_va(info, PDE32_T_ADDR((*guest_pde)), (addr_t*)&guest_pt) == -1) {
462 // Machine check the guest
463 PrintDebug("Invalid Guest PTE Address: 0x%x\n", PDE32_T_ADDR((*guest_pde)));
464 v3_raise_exception(info, MC_EXCEPTION);
468 if (handle_shadow_pte32_fault(info, fault_addr, error_code, shadow_pt, guest_pt) == -1) {
469 PrintError("Error handling Page fault caused by PTE\n");
472 } else if (guest_pde->large_page == 1) {
473 if (handle_large_pagefault32(info, fault_addr, error_code, shadow_pt, (pde32_4MB_t *)guest_pde) == -1) {
474 PrintError("Error handling large pagefault\n");
479 else if ((shadow_pde_access == PT_WRITE_ERROR) &&
480 (guest_pde->large_page == 1) &&
481 (((pde32_4MB_t *)guest_pde)->dirty == 0))
484 // Page Directory Entry marked read-only
485 // Its a large page and we need to update the dirty bit in the guest
488 PrintDebug("Large page write error... Setting dirty bit and returning\n");
489 ((pde32_4MB_t *)guest_pde)->dirty = 1;
490 shadow_pde->writable = guest_pde->writable;
494 else if (shadow_pde_access == PT_USER_ERROR)
497 // Page Directory Entry marked non-user
499 PrintDebug("Shadow Paging User access error (shadow_pde_access=0x%x, guest_pde_access=0x%x)\n",
500 shadow_pde_access, guest_pde_access);
501 inject_guest_pf(info, fault_addr, error_code);
506 // inject page fault in guest
507 inject_guest_pf(info, fault_addr, error_code);
508 PrintDebug("Unknown Error occurred (shadow_pde_access=%d)\n", shadow_pde_access);
509 PrintDebug("Manual Says to inject page fault into guest\n");
510 #ifdef DEBUG_SHADOW_PAGING
511 PrintDebug("Guest PDE: (access=%d)\n\t", guest_pde_access);
512 PrintPDE32(fault_addr, guest_pde);
513 PrintDebug("Shadow PDE: (access=%d)\n\t", shadow_pde_access);
514 PrintPDE32(fault_addr, shadow_pde);
520 PrintDebug("Returning end of PDE function (rip=%p)\n", (void *)(addr_t)(info->rip));
527 * We assume the the guest pte pointer has already been translated to a host virtual address
529 static int handle_shadow_pte32_fault(struct guest_info * info,
531 pf_error_t error_code,
533 pte32_t * guest_pt) {
535 pt_access_status_t guest_pte_access;
536 pt_access_status_t shadow_pte_access;
537 pte32_t * guest_pte = (pte32_t *)&(guest_pt[PTE32_INDEX(fault_addr)]);;
538 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
541 // Check the guest page permissions
542 guest_pte_access = can_access_pte32(guest_pt, fault_addr, error_code);
544 // Check the shadow page permissions
545 shadow_pte_access = can_access_pte32(shadow_pt, fault_addr, error_code);
547 #ifdef DEBUG_SHADOW_PAGING
548 PrintDebug("Guest PTE: (access=%d)\n\t", guest_pte_access);
549 PrintPTE32(fault_addr, guest_pte);
550 PrintDebug("Shadow PTE: (access=%d)\n\t", shadow_pte_access);
551 PrintPTE32(fault_addr, shadow_pte);
554 /* Was the page fault caused by the Guest's page tables? */
555 if (is_guest_pf(guest_pte_access, shadow_pte_access) == 1) {
556 PrintDebug("Access error injecting pf to guest (guest access error=%d) (pf error code=%d)\n",
557 guest_pte_access, *(uint_t*)&error_code);
558 inject_guest_pf(info, fault_addr, error_code);
563 if (shadow_pte_access == PT_ACCESS_OK) {
564 // Inconsistent state...
565 // Guest Re-Entry will flush page tables and everything should now work
566 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
571 if (shadow_pte_access == PT_ENTRY_NOT_PRESENT) {
573 addr_t guest_pa = PTE32_T_ADDR((*guest_pte)) + PT32_PAGE_OFFSET(fault_addr);
575 // Page Table Entry Not Present
577 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_pa);
579 if (host_page_type == HOST_REGION_INVALID) {
580 // Inject a machine check in the guest
581 PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_pa);
582 v3_raise_exception(info, MC_EXCEPTION);
588 if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
589 struct shadow_page_state * state = &(info->shdw_pg_state);
590 addr_t shadow_pa = get_shadow_addr(info, guest_pa);
592 shadow_pte->page_base_addr = PT32_BASE_ADDR(shadow_pa);
594 shadow_pte->present = guest_pte->present;
595 shadow_pte->user_page = guest_pte->user_page;
597 //set according to VMM policy
598 shadow_pte->write_through = 0;
599 shadow_pte->cache_disable = 0;
600 shadow_pte->global_page = 0;
603 guest_pte->accessed = 1;
605 if (find_pte_map(state->cached_ptes, PT32_PAGE_ADDR(guest_pa)) != NULL) {
606 // Check if the entry is a page table...
607 PrintDebug("Marking page as Guest Page Table %d\n", shadow_pte->writable);
608 shadow_pte->vmm_info = PT32_GUEST_PT;
611 if (guest_pte->dirty == 1) {
612 shadow_pte->writable = guest_pte->writable;
613 } else if ((guest_pte->dirty == 0) && (error_code.write == 1)) {
614 shadow_pte->writable = guest_pte->writable;
615 guest_pte->dirty = 1;
617 if (shadow_pte->vmm_info == PT32_GUEST_PT) {
618 // Well that was quick...
619 struct shadow_page_state * state = &(info->shdw_pg_state);
620 PrintDebug("Immediate Write operation on Guest PAge Table Page\n");
621 state->cached_cr3 = 0;
624 } else if ((guest_pte->dirty == 0) && (error_code.write == 0)) { // was =
625 shadow_pte->writable = 0;
631 // Page fault handled by hook functions
632 if (handle_special_page_fault(info, fault_addr, guest_pa, error_code) == -1) {
633 PrintError("Special Page fault handler returned error for address: %p\n", (void *)fault_addr);
638 } else if ((shadow_pte_access == PT_WRITE_ERROR) &&
639 (guest_pte->dirty == 0)) {
641 PrintDebug("Shadow PTE Write Error\n");
642 guest_pte->dirty = 1;
643 shadow_pte->writable = guest_pte->writable;
645 if (shadow_pte->vmm_info == PT32_GUEST_PT) {
646 struct shadow_page_state * state = &(info->shdw_pg_state);
647 PrintDebug("Write operation on Guest PAge Table Page\n");
648 state->cached_cr3 = 0;
654 // Inject page fault into the guest
655 inject_guest_pf(info, fault_addr, error_code);
656 PrintError("PTE Page fault fell through... Not sure if this should ever happen\n");
657 PrintError("Manual Says to inject page fault into guest\n");
661 PrintDebug("Returning end of function\n");
670 /* Currently Does not work with Segmentation!!! */
671 int v3_handle_shadow_invlpg(struct guest_info * info)
673 if (info->mem_mode != VIRTUAL_MEM) {
674 // Paging must be turned on...
675 // should handle with some sort of fault I think
676 PrintError("ERROR: INVLPG called in non paged mode\n");
681 if (info->cpu_mode != PROTECTED)
687 int ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
689 PrintError("Could not read instruction 0x%p (ret=%d)\n", (void *)(addr_t)(info->rip), ret);
694 /* Can INVLPG work with Segments?? */
695 while (is_prefix_byte(instr[index])) {
700 if( instr[index + 0] != (uchar_t) 0x0f
701 || instr[index + 1] != (uchar_t) 0x01
703 PrintError("invalid Instruction Opcode\n");
704 PrintTraceMemDump(instr, 15);
708 addr_t first_operand;
709 addr_t second_operand;
710 addr_t guest_cr3 = (addr_t)V3_PAddr( (void*)(addr_t) CR3_TO_PDE32(info->shdw_pg_state.guest_cr3) );
712 pde32_t * guest_pd = NULL;
714 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1)
716 PrintError("Invalid Guest PDE Address: 0x%p\n", (void *)guest_cr3);
722 v3_operand_type_t addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
724 if (addr_type != MEM_OPERAND) {
725 PrintError("Invalid Operand type\n");
729 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
730 pde32_t * shadow_pde = (pde32_t *)&shadow_pd[PDE32_INDEX(first_operand)];
733 //PrintDebug("PDE Index=%d\n", PDE32_INDEX(first_operand));
734 //PrintDebug("FirstOperand = %x\n", first_operand);
736 PrintDebug("Invalidating page for %p\n", (void *)first_operand);
738 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(first_operand)]);
740 if (guest_pde->large_page == 1) {
741 shadow_pde->present = 0;
742 PrintDebug("Invalidating Large Page\n");
744 if (shadow_pde->present == 1) {
745 pte32_t * shadow_pt = (pte32_t *)(addr_t)PDE32_T_ADDR((*shadow_pde));
746 pte32_t * shadow_pte = (pte32_t *) V3_VAddr( (void*) &shadow_pt[PTE32_INDEX(first_operand)] );
748 #ifdef DEBUG_SHADOW_PAGING
749 PrintDebug("Setting not present\n");
750 PrintPTE32(first_operand, shadow_pte );
753 shadow_pte->present = 0;
765 static int create_pd32_nonaligned_4MB_page(struct guest_info * info, pte32_t * pt, addr_t guest_addr, pde32_4MB_t * large_shadow_pde) {
767 pte32_t * pte_cursor;
770 for (i = 0; i < 1024; i++) {
771 guest_pa = guest_addr + (PAGE_SIZE * i);
772 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_pa);
774 pte_cursor = &(pt[i]);
776 if (host_page_type == HOST_REGION_INVALID) {
777 // Currently we don't support this, but in theory we could
778 PrintError("Invalid Host Memory Type\n");
780 } else if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
781 addr_t shadow_pa = get_shadow_addr(info, guest_pa);
784 pte_cursor->page_base_addr = PT32_BASE_ADDR(shadow_pa);
785 pte_cursor->present = 1;
786 pte_cursor->writable = large_shadow_pde->writable;
787 pte_cursor->user_page = large_shadow_pde->user_page;
788 pte_cursor->write_through = 0;
789 pte_cursor->cache_disable = 0;
790 pte_cursor->global_page = 0;
793 PrintError("Unsupported Host Memory Type\n");
801 static int handle_large_pagefault32(struct guest_info * info,
802 pde32_t * guest_pde, pde32_t * shadow_pde,
803 addr_t fault_addr, pf_error_t error_code ) {
804 struct shadow_region * mem_reg;
805 pde32_4MB_t * large_guest_pde = (pde32_4MB_t *)guest_pde;
806 pde32_4MB_t * large_shadow_pde = (pde32_4MB_t *)shadow_pde;
807 host_region_type_t host_page_type;
808 addr_t guest_start_addr = PDE32_4MB_T_ADDR(*large_guest_pde);
809 // addr_t guest_end_addr = guest_start_addr + PAGE_SIZE_4MB; // start address + 4MB
812 // Check that the Guest PDE entry points to valid memory
813 // else Machine Check the guest
814 PrintDebug("Large Page: Page Base Addr=%x\n", guest_start_addr);
816 host_page_type = get_shadow_addr_type(info, guest_start_addr);
818 if (host_page_type == HOST_REGION_INVALID) {
819 PrintError("Invalid guest address in large page (0x%x)\n", guest_start_addr);
820 v3_raise_exception(info, MC_EXCEPTION);
826 if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
828 addr_t host_start_addr = 0;
829 addr_t region_end_addr = 0;
831 // Check for a large enough region in host memory
832 mem_reg = get_shadow_region_by_addr(&(info->mem_map), guest_start_addr);
833 PrintDebug("Host region: host_addr=%x (guest_start=%x, end=%x)\n",
834 mem_reg->host_addr, mem_reg->guest_start, mem_reg->guest_end);
835 host_start_addr = mem_reg->host_addr + (guest_start_addr - mem_reg->guest_start);
836 region_end_addr = mem_reg->host_addr + (mem_reg->guest_end - mem_reg->guest_start);
838 PrintDebug("Host Start Addr=%x; Region End Addr=%x\n", host_start_addr, region_end_addr);
842 if (large_guest_pde->dirty == 1) { // dirty
843 large_shadow_pde->writable = guest_pde->writable;
844 } else if (error_code.write == 1) { // not dirty, access is write
845 large_shadow_pde->writable = guest_pde->writable;
846 large_guest_pde->dirty = 1;
847 } else { // not dirty, access is read
848 large_shadow_pde->writable = 0;
852 // Check if the region is at least an additional 4MB
856 if ((PD32_4MB_PAGE_OFFSET(host_start_addr) == 0) &&
857 (region_end_addr >= host_start_addr + PAGE_SIZE_4MB)) { // if 4MB boundary
858 large_shadow_pde->page_base_addr = PD32_4MB_BASE_ADDR(host_start_addr);
859 } else { // else generate 4k pages
860 pte32_t * shadow_pt = NULL;
861 PrintDebug("Handling non aligned large page\n");
863 shadow_pde->large_page = 0;
865 shadow_pt = create_new_shadow_pt32();
867 if (create_pd32_nonaligned_4MB_page(info, shadow_pt, guest_start_addr, large_shadow_pde) == -1) {
868 PrintError("Non Aligned Large Page Error\n");
874 #ifdef DEBUG_SHADOW_PAGING
875 PrintDebug("non-aligned Shadow PT\n");
876 PrintPT32(PT32_PAGE_ADDR(fault_addr), shadow_pt);
878 shadow_pde->pt_base_addr = PD32_BASE_ADDR(shadow_pt);
882 // Handle hooked pages as well as other special pages
883 if (handle_special_page_fault(info, fault_addr, guest_start_addr, error_code) == -1) {
884 PrintError("Special Page Fault handler returned error for address: %x\n", fault_addr);