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".
23 static inline int activate_shadow_pt_32(struct guest_info * info) {
24 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->ctrl_regs.cr3);
25 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
26 struct shadow_page_data * shdw_page = create_new_shadow_pt(info);
28 shdw_page->cr3 = shdw_page->page_pa;
30 shadow_cr3->pdt_base_addr = PAGE_BASE_ADDR(shdw_page->page_pa);
31 PrintDebug( "Created new shadow page table %p\n", (void *)BASE_TO_PAGE_ADDR(shadow_cr3->pdt_base_addr));
33 shadow_cr3->pwt = guest_cr3->pwt;
34 shadow_cr3->pcd = guest_cr3->pcd;
42 * * 32 bit Page table fault handlers
46 static int handle_large_pagefault_32(struct guest_info * info,
47 addr_t fault_addr, pf_error_t error_code,
48 pte32_t * shadow_pt, pde32_4MB_t * large_guest_pde);
50 static int handle_shadow_pte32_fault(struct guest_info * info,
52 pf_error_t error_code,
57 static inline int handle_shadow_pagefault_32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
58 pde32_t * guest_pd = NULL;
59 pde32_t * shadow_pd = CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
60 addr_t guest_cr3 = CR3_TO_PDE32_PA(info->shdw_pg_state.guest_cr3);
61 pt_access_status_t guest_pde_access;
62 pt_access_status_t shadow_pde_access;
63 pde32_t * guest_pde = NULL;
64 pde32_t * shadow_pde = (pde32_t *)&(shadow_pd[PDE32_INDEX(fault_addr)]);
66 PrintDebug("Shadow page fault handler: %p\n", (void*) fault_addr );
68 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
69 PrintError("Invalid Guest PDE Address: 0x%p\n", (void *)guest_cr3);
73 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(fault_addr)]);
76 // Check the guest page permissions
77 guest_pde_access = v3_can_access_pde32(guest_pd, fault_addr, error_code);
79 // Check the shadow page permissions
80 shadow_pde_access = v3_can_access_pde32(shadow_pd, fault_addr, error_code);
82 /* Was the page fault caused by the Guest's page tables? */
83 if (is_guest_pf(guest_pde_access, shadow_pde_access) == 1) {
84 PrintDebug("Injecting PDE pf to guest: (guest access error=%d) (pf error code=%d)\n",
85 *(uint_t *)&guest_pde_access, *(uint_t *)&error_code);
86 inject_guest_pf(info, fault_addr, error_code);
91 if (shadow_pde_access == PT_ACCESS_NOT_PRESENT)
93 struct shadow_page_data * shdw_page = create_new_shadow_pt(info);
94 pte32_t * shadow_pt = (pte32_t *)V3_VAddr((void *)shdw_page->page_pa);
96 shadow_pde->present = 1;
97 shadow_pde->user_page = guest_pde->user_page;
98 // shadow_pde->large_page = guest_pde->large_page;
99 shadow_pde->large_page = 0;
102 // VMM Specific options
103 shadow_pde->write_through = guest_pde->write_through;
104 shadow_pde->cache_disable = guest_pde->cache_disable;
105 shadow_pde->global_page = guest_pde->global_page;
108 guest_pde->accessed = 1;
110 shadow_pde->pt_base_addr = PAGE_BASE_ADDR((addr_t)V3_PAddr(shadow_pt));
112 if (guest_pde->large_page == 0) {
113 pte32_t * guest_pt = NULL;
114 shadow_pde->writable = guest_pde->writable;
116 if (guest_pa_to_host_va(info, BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr), (addr_t*)&guest_pt) == -1) {
117 // Machine check the guest
118 PrintDebug("Invalid Guest PTE Address: 0x%p\n", (void *)BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr));
119 v3_raise_exception(info, MC_EXCEPTION);
123 if (handle_shadow_pte32_fault(info, fault_addr, error_code, shadow_pt, guest_pt) == -1) {
124 PrintError("Error handling Page fault caused by PTE\n");
128 // ?? What if guest pde is dirty a this point?
129 ((pde32_4MB_t *)guest_pde)->dirty = 0;
130 shadow_pde->writable = 0;
132 if (handle_large_pagefault_32(info, fault_addr, error_code, shadow_pt, (pde32_4MB_t *)guest_pde) == -1) {
133 PrintError("Error handling large pagefault\n");
139 else if (shadow_pde_access == PT_ACCESS_OK)
144 pte32_t * shadow_pt = (pte32_t *)V3_VAddr( (void*)(addr_t) BASE_TO_PAGE_ADDR(shadow_pde->pt_base_addr) );
146 if (guest_pde->large_page == 0) {
147 pte32_t * guest_pt = NULL;
149 if (guest_pa_to_host_va(info, BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr), (addr_t*)&guest_pt) == -1) {
150 // Machine check the guest
151 PrintDebug("Invalid Guest PTE Address: 0x%p\n", (void *)BASE_TO_PAGE_ADDR(guest_pde->pt_base_addr));
152 v3_raise_exception(info, MC_EXCEPTION);
156 if (handle_shadow_pte32_fault(info, fault_addr, error_code, shadow_pt, guest_pt) == -1) {
157 PrintError("Error handling Page fault caused by PTE\n");
160 } else if (guest_pde->large_page == 1) {
161 if (handle_large_pagefault_32(info, fault_addr, error_code, shadow_pt, (pde32_4MB_t *)guest_pde) == -1) {
162 PrintError("Error handling large pagefault\n");
167 else if ((shadow_pde_access == PT_ACCESS_WRITE_ERROR) &&
168 (guest_pde->large_page == 1))
171 // Page Directory Entry marked read-only
172 // Its a large page and we need to update the dirty bit in the guest
175 PrintDebug("Large page write error... Setting dirty bit and returning\n");
176 ((pde32_4MB_t *)guest_pde)->dirty = 1;
177 shadow_pde->writable = guest_pde->writable;
181 else if (shadow_pde_access == PT_ACCESS_USER_ERROR)
184 // Page Directory Entry marked non-user
186 PrintDebug("Shadow Paging User access error (shadow_pde_access=0x%x, guest_pde_access=0x%x)\n",
187 shadow_pde_access, guest_pde_access);
188 inject_guest_pf(info, fault_addr, error_code);
193 // inject page fault in guest
194 inject_guest_pf(info, fault_addr, error_code);
195 PrintDebug("Unknown Error occurred (shadow_pde_access=%d)\n", shadow_pde_access);
196 PrintDebug("Manual Says to inject page fault into guest\n");
197 #ifdef DEBUG_SHADOW_PAGING
198 PrintDebug("Guest PDE: (access=%d)\n\t", guest_pde_access);
199 PrintPTEntry(info, PAGE_PD32, fault_addr, guest_pde);
200 PrintDebug("Shadow PDE: (access=%d)\n\t", shadow_pde_access);
201 PrintPTEntry(info, PAGE_PD32, fault_addr, shadow_pde);
207 PrintDebug("Returning end of PDE function (rip=%p)\n", (void *)(addr_t)(info->rip));
213 /* The guest status checks have already been done,
214 * only special case shadow checks remain
216 static int handle_large_pagefault_32(struct guest_info * info,
217 addr_t fault_addr, pf_error_t error_code,
218 pte32_t * shadow_pt, pde32_4MB_t * large_guest_pde)
220 pt_access_status_t shadow_pte_access = v3_can_access_pte32(shadow_pt, fault_addr, error_code);
221 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
222 addr_t guest_fault_pa = BASE_TO_PAGE_ADDR_4MB(large_guest_pde->page_base_addr) + PAGE_OFFSET_4MB(fault_addr);
224 struct v3_shadow_region * shdw_reg = v3_get_shadow_region(info, guest_fault_pa);
227 if (shdw_reg == NULL) {
228 // Inject a machine check in the guest
229 PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_fault_pa);
230 v3_raise_exception(info, MC_EXCEPTION);
234 if (shadow_pte_access == PT_ACCESS_OK) {
235 // Inconsistent state...
236 // Guest Re-Entry will flush tables and everything should now workd
237 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
242 if (shadow_pte_access == PT_ACCESS_NOT_PRESENT) {
243 // Get the guest physical address of the fault
245 if ((shdw_reg->host_type == SHDW_REGION_ALLOCATED) ||
246 (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK)) {
247 addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, guest_fault_pa);
249 shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
251 shadow_pte->present = 1;
253 /* We are assuming that the PDE entry has precedence
254 * so the Shadow PDE will mirror the guest PDE settings,
255 * and we don't have to worry about them here
258 shadow_pte->user_page = 1;
260 if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
261 shadow_pte->writable = 0;
263 shadow_pte->writable = 1;
266 //set according to VMM policy
267 shadow_pte->write_through = large_guest_pde->write_through;
268 shadow_pte->cache_disable = large_guest_pde->cache_disable;
269 shadow_pte->global_page = large_guest_pde->global_page;
273 // Handle hooked pages as well as other special pages
274 // if (handle_special_page_fault(info, fault_addr, guest_fault_pa, error_code) == -1) {
276 if (v3_handle_mem_full_hook(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
277 PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
281 } else if (shadow_pte_access == PT_ACCESS_WRITE_ERROR) {
283 if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
285 if (v3_handle_mem_wr_hook(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
286 PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
295 PrintError("Error in large page fault handler...\n");
296 PrintError("This case should have been handled at the top level handler\n");
300 PrintDebug("Returning from large page fault handler\n");
308 * We assume the the guest pte pointer has already been translated to a host virtual address
310 static int handle_shadow_pte32_fault(struct guest_info * info,
312 pf_error_t error_code,
314 pte32_t * guest_pt) {
316 pt_access_status_t guest_pte_access;
317 pt_access_status_t shadow_pte_access;
318 pte32_t * guest_pte = (pte32_t *)&(guest_pt[PTE32_INDEX(fault_addr)]);;
319 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
320 addr_t guest_pa = BASE_TO_PAGE_ADDR((addr_t)(guest_pte->page_base_addr)) + PAGE_OFFSET(fault_addr);
322 struct v3_shadow_region * shdw_reg = v3_get_shadow_region(info, guest_pa);
324 if (shdw_reg == NULL) {
325 // Inject a machine check in the guest
326 PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_pa);
327 v3_raise_exception(info, MC_EXCEPTION);
331 // Check the guest page permissions
332 guest_pte_access = v3_can_access_pte32(guest_pt, fault_addr, error_code);
334 // Check the shadow page permissions
335 shadow_pte_access = v3_can_access_pte32(shadow_pt, fault_addr, error_code);
337 #ifdef DEBUG_SHADOW_PAGING
338 PrintDebug("Guest PTE: (access=%d)\n\t", guest_pte_access);
339 PrintPTEntry(info, PAGE_PT32, fault_addr, guest_pte);
340 PrintDebug("Shadow PTE: (access=%d)\n\t", shadow_pte_access);
341 PrintPTEntry(info, PAGE_PT32, fault_addr, shadow_pte);
344 /* Was the page fault caused by the Guest's page tables? */
345 if (is_guest_pf(guest_pte_access, shadow_pte_access) == 1) {
346 PrintDebug("Access error injecting pf to guest (guest access error=%d) (pf error code=%d)\n",
347 guest_pte_access, *(uint_t*)&error_code);
348 inject_guest_pf(info, fault_addr, error_code);
354 if (shadow_pte_access == PT_ACCESS_OK) {
355 // Inconsistent state...
356 // Guest Re-Entry will flush page tables and everything should now work
357 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
362 if (shadow_pte_access == PT_ACCESS_NOT_PRESENT) {
363 // Page Table Entry Not Present
364 PrintDebug("guest_pa =%p\n", (void *)guest_pa);
366 if ((shdw_reg->host_type == SHDW_REGION_ALLOCATED) ||
367 (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK)) {
368 addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, guest_pa);
370 shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
372 shadow_pte->present = guest_pte->present;
373 shadow_pte->user_page = guest_pte->user_page;
375 //set according to VMM policy
376 shadow_pte->write_through = guest_pte->write_through;
377 shadow_pte->cache_disable = guest_pte->cache_disable;
378 shadow_pte->global_page = guest_pte->global_page;
381 guest_pte->accessed = 1;
383 if (guest_pte->dirty == 1) {
384 shadow_pte->writable = guest_pte->writable;
385 } else if ((guest_pte->dirty == 0) && (error_code.write == 1)) {
386 shadow_pte->writable = guest_pte->writable;
387 guest_pte->dirty = 1;
388 } else if ((guest_pte->dirty == 0) && (error_code.write == 0)) {
389 shadow_pte->writable = 0;
394 // Write hooks trump all, and are set Read Only
395 if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
396 shadow_pte->writable = 0;
400 // Page fault handled by hook functions
402 if (v3_handle_mem_full_hook(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
403 PrintError("Special Page fault handler returned error for address: %p\n", (void *)fault_addr);
407 } else if (shadow_pte_access == PT_ACCESS_WRITE_ERROR) {
408 guest_pte->dirty = 1;
410 if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
411 if (v3_handle_mem_wr_hook(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
412 PrintError("Special Page fault handler returned error for address: %p\n", (void *)fault_addr);
416 PrintDebug("Shadow PTE Write Error\n");
417 shadow_pte->writable = guest_pte->writable;
424 // Inject page fault into the guest
425 inject_guest_pf(info, fault_addr, error_code);
426 PrintError("PTE Page fault fell through... Not sure if this should ever happen\n");
427 PrintError("Manual Says to inject page fault into guest\n");
431 PrintDebug("Returning end of function\n");
437 /* If we start to optimize we should look up the guest pages in the cache... */
438 static inline int handle_shadow_invlpg_32(struct guest_info * info, addr_t vaddr) {
439 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
440 pde32_t * shadow_pde = (pde32_t *)&shadow_pd[PDE32_INDEX(vaddr)];
442 addr_t guest_cr3 = CR3_TO_PDE32_PA(info->shdw_pg_state.guest_cr3);
443 pde32_t * guest_pd = NULL;
446 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
447 PrintError("Invalid Guest PDE Address: 0x%p\n", (void *)guest_cr3);
451 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(vaddr)]);
453 if (guest_pde->large_page == 1) {
454 shadow_pde->present = 0;
455 PrintDebug("Invalidating Large Page\n");
456 } else if (shadow_pde->present == 1) {
457 pte32_t * shadow_pt = (pte32_t *)(addr_t)BASE_TO_PAGE_ADDR_4KB(shadow_pde->pt_base_addr);
458 pte32_t * shadow_pte = (pte32_t *) V3_VAddr( (void*) &shadow_pt[PTE32_INDEX(vaddr)] );
460 PrintDebug("Setting not present\n");
462 shadow_pte->present = 0;