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...)
18 static int handle_shadow_pte32_fault(struct guest_info* info,
20 pf_error_t error_code,
24 static int handle_shadow_pagefault32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
26 int init_shadow_page_state(struct shadow_page_state * state) {
27 state->guest_mode = PDE32;
28 state->shadow_mode = PDE32;
31 state->shadow_cr3 = 0;
36 int handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
38 if (info->mem_mode == PHYSICAL_MEM) {
39 // If paging is not turned on we need to handle the special cases
40 return handle_special_page_fault(info, fault_addr, error_code);
41 } else if (info->mem_mode == VIRTUAL_MEM) {
43 switch (info->cpu_mode) {
45 return handle_shadow_pagefault32(info, fault_addr, error_code);
50 PrintError("Unhandled CPU Mode\n");
54 PrintError("Invalid Memory mode\n");
59 addr_t create_new_shadow_pt32(struct guest_info * info) {
62 host_pde = V3_AllocPages(1);
63 memset(host_pde, 0, PAGE_SIZE);
65 return (addr_t)host_pde;
69 static int handle_pd32_nonaligned_4MB_page(struct guest_info * info, pte32_t * pt, addr_t guest_addr, pde32_4MB_t * large_shadow_pde) {
74 for (i = 0; i < 1024; i++) {
75 guest_pa = guest_addr + (PAGE_SIZE * i);
76 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_pa);
78 pte_cursor = &(pt[i]);
80 if (host_page_type == HOST_REGION_INVALID) {
81 // Currently we don't support this, but in theory we could
82 PrintError("Invalid Host Memory Type\n");
84 } else if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
85 addr_t shadow_pa = get_shadow_addr(info, guest_pa);
88 pte_cursor->page_base_addr = PT32_BASE_ADDR(shadow_pa);
89 pte_cursor->present = 1;
90 pte_cursor->writable = large_shadow_pde->writable;
91 pte_cursor->user_page = large_shadow_pde->user_page;
92 pte_cursor->write_through = 0;
93 pte_cursor->cache_disable = 0;
94 pte_cursor->global_page = 0;
97 PrintError("Unsupported Host Memory Type\n");
104 static int handle_shadow_pagefault32(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
105 pde32_t * guest_pd = NULL;
106 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
107 addr_t guest_cr3 = CR3_TO_PDE32(info->shdw_pg_state.guest_cr3);
108 pt_access_status_t guest_pde_access;
109 pt_access_status_t shadow_pde_access;
110 pde32_t * guest_pde = NULL;
111 pde32_t * shadow_pde = (pde32_t *)&(shadow_pd[PDE32_INDEX(fault_addr)]);
113 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
114 PrintError("Invalid Guest PDE Address: 0x%x\n", guest_cr3);
119 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(fault_addr)]);
121 // Check the guest page permissions
122 guest_pde_access = can_access_pde32(guest_pd, fault_addr, error_code);
124 // Check the shadow page permissions
125 shadow_pde_access = can_access_pde32(shadow_pd, fault_addr, error_code);
127 /* This should be redone,
128 but basically the reasoning is that there can be multiple reasons for a page fault:
129 If there is a permissions failure for a page present in the guest _BUT_
130 the reason for the fault was that the page is not present in the shadow,
131 _THEN_ we have to map the shadow page in and reexecute, this will generate
132 a permissions fault which is _THEN_ valid to send to the guest
133 _UNLESS_ both the guest and shadow have marked the page as not present
137 if ((guest_pde_access != PT_ACCESS_OK) &&
139 ( (shadow_pde_access != PT_ENTRY_NOT_PRESENT) &&
140 (guest_pde_access != PT_ENTRY_NOT_PRESENT)) // aka (guest permission error)
142 ( (shadow_pde_access == PT_ENTRY_NOT_PRESENT) &&
143 (guest_pde_access == PT_ENTRY_NOT_PRESENT)))) {
144 // inject page fault to the guest (Guest PDE fault)
146 info->ctrl_regs.cr2 = fault_addr;
147 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
150 PrintDebug("Injecting PDE pf to guest: (guest access error=%d) (pf error code=%d)\n", guest_pde_access, error_code);
154 #ifdef DEBUG_SHADOW_PAGING
155 PrintDebug("Guest CR3=%x\n", guest_cr3);
156 PrintDebug("Guest PD\n");
158 PrintDebug("Shadow PD\n");
159 PrintPD32(shadow_pd);
166 //shadow_pde_access = can_access_pde32(shadow_pd, fault_addr, error_code);
169 if (shadow_pde_access == PT_ENTRY_NOT_PRESENT) {
171 shadow_pde->present = 1;
172 shadow_pde->user_page = guest_pde->user_page;
173 shadow_pde->large_page = guest_pde->large_page;
176 // VMM Specific options
177 shadow_pde->write_through = 0;
178 shadow_pde->cache_disable = 0;
179 shadow_pde->global_page = 0;
182 guest_pde->accessed = 1;
184 if (guest_pde->large_page == 0) {
185 pte32_t * shadow_pt = NULL;
187 shadow_pt = V3_AllocPages(1);
188 memset(shadow_pt, 0, PAGE_SIZE);
190 shadow_pde->pt_base_addr = PD32_BASE_ADDR(shadow_pt);
192 shadow_pde->writable = guest_pde->writable;
194 struct shadow_region * mem_reg;
195 pde32_4MB_t * large_guest_pde = (pde32_4MB_t *)guest_pde;
196 pde32_4MB_t * large_shadow_pde = (pde32_4MB_t *)shadow_pde;
197 host_region_type_t host_page_type;
198 addr_t guest_start_addr = PDE32_4MB_T_ADDR(*large_guest_pde);
199 // addr_t guest_end_addr = guest_start_addr + PAGE_SIZE_4MB; // start address + 4MB
202 // Check that the Guest PDE entry points to valid memory
203 // else Machine Check the guest
204 PrintDebug("Large Page: Page Base Addr=%x\n", guest_start_addr);
206 host_page_type = get_shadow_addr_type(info, guest_start_addr);
208 if (host_page_type == HOST_REGION_INVALID) {
210 v3_raise_exception(info, MC_EXCEPTION);
211 PrintError("Invalid guest address in large page (0x%x)\n", guest_start_addr);
213 } else if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
214 addr_t host_start_addr = 0;
215 addr_t region_end_addr = 0;
217 // Check for a large enough region in host memory
218 mem_reg = get_shadow_region_by_addr(&(info->mem_map), guest_start_addr);
219 PrintDebug("Host region: host_addr=%x (guest_start=%x, end=%x)\n",
220 mem_reg->host_addr, mem_reg->guest_start, mem_reg->guest_end);
221 host_start_addr = mem_reg->host_addr + (guest_start_addr - mem_reg->guest_start);
222 region_end_addr = mem_reg->host_addr + (mem_reg->guest_end - mem_reg->guest_start);
224 PrintDebug("Host Start Addr=%x; Region End Addr=%x\n", host_start_addr, region_end_addr);
230 if (large_guest_pde->dirty == 1) { // dirty
231 large_shadow_pde->writable = guest_pde->writable;
232 } else if (error_code.write == 1) { // not dirty, access is write
233 large_shadow_pde->writable = guest_pde->writable;
234 large_guest_pde->dirty = 1;
235 } else { // not dirty, access is read
236 large_shadow_pde->writable = 0;
240 // Check if the region is at least an additional 4MB
244 if ((PD32_4MB_PAGE_OFFSET(host_start_addr) == 0) &&
245 (region_end_addr >= host_start_addr + PAGE_SIZE_4MB)) { // if 4MB boundary
246 large_shadow_pde->page_base_addr = PD32_4MB_BASE_ADDR(host_start_addr);
247 } else { // else generate 4k pages
248 pte32_t * shadow_pt = NULL;
249 PrintDebug("Handling non aligned large page\n");
251 shadow_pde->large_page = 0;
253 shadow_pt = V3_AllocPages(1);
254 memset(shadow_pt, 0, PAGE_SIZE);
256 if (handle_pd32_nonaligned_4MB_page(info, shadow_pt, guest_start_addr, large_shadow_pde) == -1) {
257 PrintError("Non Aligned Large Page Error\n");
263 #ifdef DEBUG_SHADOW_PAGING
264 PrintDebug("non-aligned Shadow PT\n");
265 PrintPT32(PT32_PAGE_ADDR(fault_addr), shadow_pt);
267 shadow_pde->pt_base_addr = PD32_BASE_ADDR(shadow_pt);
272 // Handle hooked pages as well as other special pages
273 if (handle_special_page_fault(info, fault_addr, error_code) == -1) {
274 PrintError("Special Page Fault handler returned error for address: %x\n", fault_addr);
280 } else if ((shadow_pde_access == PT_WRITE_ERROR) &&
281 (guest_pde->large_page = 1) &&
282 (((pde32_4MB_t *)guest_pde)->dirty == 0)) {
285 // Page Directory Entry marked read-only
288 ((pde32_4MB_t *)guest_pde)->dirty = 1;
289 shadow_pde->writable = guest_pde->writable;
292 } else if (shadow_pde_access == PT_USER_ERROR) {
295 // Page Directory Entry marked non-user
298 PrintDebug("Shadow Paging User access error (shadow_pde_access=0x%x, guest_pde_access=0x%x - injecting into guest\n", shadow_pde_access, guest_pde_access);
299 info->ctrl_regs.cr2 = fault_addr;
300 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
303 } else if (shadow_pde_access == PT_ACCESS_OK) {
304 pte32_t * shadow_pt = (pte32_t *)PDE32_T_ADDR((*shadow_pde));
305 pte32_t * guest_pt = NULL;
307 // Page Table Entry fault
309 if (guest_pa_to_host_va(info, PDE32_T_ADDR((*guest_pde)), (addr_t*)&guest_pt) == -1) {
310 PrintDebug("Invalid Guest PTE Address: 0x%x\n", PDE32_T_ADDR((*guest_pde)));
311 // Machine check the guest
313 v3_raise_exception(info, MC_EXCEPTION);
319 if (handle_shadow_pte32_fault(info, fault_addr, error_code, shadow_pt, guest_pt) == -1) {
320 PrintError("Error handling Page fault caused by PTE\n");
325 // Unknown error raise page fault in guest
326 info->ctrl_regs.cr2 = fault_addr;
327 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
329 // For debugging we will return an error here for the time being,
330 // this probably shouldn't ever happen
331 PrintDebug("Unknown Error occurred\n");
332 PrintDebug("Manual Says to inject page fault into guest\n");
339 //PrintDebugPageTables(shadow_pd);
340 PrintDebug("Returning end of PDE function (rip=%x)\n", info->rip);
347 * We assume the the guest pte pointer has already been translated to a host virtual address
349 static int handle_shadow_pte32_fault(struct guest_info * info,
351 pf_error_t error_code,
353 pte32_t * guest_pt) {
355 pt_access_status_t guest_pte_access;
356 pt_access_status_t shadow_pte_access;
357 pte32_t * guest_pte = (pte32_t *)&(guest_pt[PTE32_INDEX(fault_addr)]);;
358 pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
361 // Check the guest page permissions
362 guest_pte_access = can_access_pte32(guest_pt, fault_addr, error_code);
364 // Check the shadow page permissions
365 shadow_pte_access = can_access_pte32(shadow_pt, fault_addr, error_code);
367 #ifdef DEBUG_SHADOW_PAGING
368 PrintDebug("Guest PTE: (access=%d)\n\t", guest_pte_access);
369 PrintPTE32(fault_addr, guest_pte);
370 PrintDebug("Shadow PTE: (access=%d)\n\t", shadow_pte_access);
371 PrintPTE32(fault_addr, shadow_pte);
374 /* This should be redone,
375 but basically the reasoning is that there can be multiple reasons for a page fault:
376 If there is a permissions failure for a page present in the guest _BUT_
377 the reason for the fault was that the page is not present in the shadow,
378 _THEN_ we have to map the shadow page in and reexecute, this will generate
379 a permissions fault which is _THEN_ valid to send to the guest
380 _UNLESS_ both the guest and shadow have marked the page as not present
384 if ((guest_pte_access != PT_ACCESS_OK) &&
386 ((shadow_pte_access != PT_ENTRY_NOT_PRESENT) &&
387 (guest_pte_access != PT_ENTRY_NOT_PRESENT)) // aka (guest permission error)
389 ((shadow_pte_access == PT_ENTRY_NOT_PRESENT) &&
390 (guest_pte_access == PT_ENTRY_NOT_PRESENT)))) {
391 // Inject page fault into the guest
393 info->ctrl_regs.cr2 = fault_addr;
394 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
396 PrintDebug("Access error injecting pf to guest (guest access error=%d) (pf error code=%d)\n", guest_pte_access, *(uint_t*)&error_code);
403 if (shadow_pte_access == PT_ACCESS_OK) {
404 // Inconsistent state...
405 // Guest Re-Entry will flush page tables and everything should now work
406 PrintDebug("Inconsistent state... Guest re-entry should flush tlb\n");
408 } else if (shadow_pte_access == PT_ENTRY_NOT_PRESENT) {
410 addr_t guest_pa = PTE32_T_ADDR((*guest_pte));
412 // Page Table Entry Not Present
414 host_region_type_t host_page_type = get_shadow_addr_type(info, guest_pa);
416 if (host_page_type == HOST_REGION_INVALID) {
417 // Inject a machine check in the guest
419 v3_raise_exception(info, MC_EXCEPTION);
420 #ifdef DEBUG_SHADOW_PAGING
421 PrintDebug("Invalid Guest Address in page table (0x%x)\n", guest_pa);
422 PrintDebug("fault_addr=0x%x next are guest and shadow ptes \n",fault_addr);
423 PrintPTE32(fault_addr,guest_pte);
424 PrintPTE32(fault_addr,shadow_pte);
425 PrintDebug("Done.\n");
429 } else if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
431 shadow_pa = get_shadow_addr(info, guest_pa);
433 shadow_pte->page_base_addr = PT32_BASE_ADDR(shadow_pa);
435 shadow_pte->present = guest_pte->present;
436 shadow_pte->user_page = guest_pte->user_page;
438 //set according to VMM policy
439 shadow_pte->write_through = 0;
440 shadow_pte->cache_disable = 0;
441 shadow_pte->global_page = 0;
444 guest_pte->accessed = 1;
446 if (guest_pte->dirty == 1) {
447 shadow_pte->writable = guest_pte->writable;
448 } else if ((guest_pte->dirty == 0) && (error_code.write == 1)) {
449 shadow_pte->writable = guest_pte->writable;
450 guest_pte->dirty = 1;
451 } else if ((guest_pte->dirty = 0) && (error_code.write == 0)) {
452 shadow_pte->writable = 0;
455 // Page fault handled by hook functions
456 if (handle_special_page_fault(info, fault_addr, error_code) == -1) {
457 PrintError("Special Page fault handler returned error for address: %x\n", fault_addr);
462 } else if ((shadow_pte_access == PT_WRITE_ERROR) &&
463 (guest_pte->dirty == 0)) {
464 guest_pte->dirty = 1;
465 shadow_pte->writable = guest_pte->writable;
467 PrintDebug("Shadow PTE Write Error\n");
471 // Inject page fault into the guest
473 info->ctrl_regs.cr2 = fault_addr;
474 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
476 PrintError("PTE Page fault fell through... Not sure if this should ever happen\n");
477 PrintError("Manual Says to inject page fault into guest\n");
481 PrintDebug("Returning end of function\n");
490 /* Currently Does not work with Segmentation!!! */
491 int handle_shadow_invlpg(struct guest_info * info) {
492 if (info->mem_mode != VIRTUAL_MEM) {
493 // Paging must be turned on...
494 // should handle with some sort of fault I think
495 PrintError("ERROR: INVLPG called in non paged mode\n");
500 if (info->cpu_mode == PROTECTED) {
505 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
507 PrintError("Could not read instruction 0x%x (ret=%d)\n", info->rip, ret);
512 /* Can INVLPG work with Segments?? */
513 while (is_prefix_byte(instr[index])) {
518 if ((instr[index] == (uchar_t)0x0f) &&
519 (instr[index + 1] == (uchar_t)0x01)) {
521 addr_t first_operand;
522 addr_t second_operand;
523 operand_type_t addr_type;
524 addr_t guest_cr3 = CR3_TO_PDE32(info->shdw_pg_state.guest_cr3);
526 pde32_t * guest_pd = NULL;
528 if (guest_pa_to_host_va(info, guest_cr3, (addr_t*)&guest_pd) == -1) {
529 PrintError("Invalid Guest PDE Address: 0x%x\n", guest_cr3);
538 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
540 if (addr_type == MEM_OPERAND) {
541 pde32_t * shadow_pd = (pde32_t *)CR3_TO_PDE32(info->shdw_pg_state.shadow_cr3);
542 pde32_t * shadow_pde = (pde32_t *)&shadow_pd[PDE32_INDEX(first_operand)];
545 //PrintDebug("PDE Index=%d\n", PDE32_INDEX(first_operand));
546 //PrintDebug("FirstOperand = %x\n", first_operand);
548 PrintDebug("Invalidating page for %x\n", first_operand);
550 guest_pde = (pde32_t *)&(guest_pd[PDE32_INDEX(first_operand)]);
552 if (guest_pde->large_page == 1) {
553 shadow_pde->present = 0;
554 PrintDebug("Invalidating Large Page\n");
557 if (shadow_pde->present == 1) {
558 pte32_t * shadow_pt = (pte32_t *)PDE32_T_ADDR((*shadow_pde));
559 pte32_t * shadow_pte = (pte32_t *)&shadow_pt[PTE32_INDEX(first_operand)];
561 #ifdef DEBUG_SHADOW_PAGING
562 PrintDebug("Setting not present\n");
563 PrintPTE32(first_operand, shadow_pte);
566 shadow_pte->present = 0;
573 PrintError("Invalid Operand type\n");
577 PrintError("invalid Instruction Opcode\n");
578 PrintTraceMemDump(instr, 15);