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>
27 #include <palacios/vmm_ctrl_regs.h>
29 #include <palacios/vmm_hashtable.h>
31 #include <palacios/vmm_direct_paging.h>
34 // set this to 1 if you want us to attempt to
35 // fetch multiple entries on a page fault
36 #define SPECULATIVE_PAGING 1
38 #define REGULAR_PAGE_FAULT 0
39 #define SPECULATIVE_PAGE_FAULT 1
41 #ifndef DEBUG_SHADOW_PAGING
43 #define PrintDebug(fmt, args...)
52 struct shadow_page_data {
56 struct list_head page_list_node;
60 DEFINE_HASHTABLE_INSERT(add_pte_map, addr_t, addr_t);
61 DEFINE_HASHTABLE_SEARCH(find_pte_map, addr_t, addr_t);
62 //DEFINE_HASHTABLE_REMOVE(del_pte_map, addr_t, addr_t, 0);
66 static uint_t pte_hash_fn(addr_t key) {
67 return hash_long(key, 32);
70 static int pte_equals(addr_t key1, addr_t key2) {
71 return (key1 == key2);
74 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info);
75 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
76 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access);
79 #include "vmm_shadow_paging_32.h"
80 #include "vmm_shadow_paging_32pae.h"
81 #include "vmm_shadow_paging_64.h"
85 int v3_init_shadow_page_state(struct guest_info * info) {
86 struct shadow_page_state * state = &(info->shdw_pg_state);
91 INIT_LIST_HEAD(&(state->page_list));
93 state->cached_ptes = NULL;
94 state->cached_cr3 = 0;
101 // Reads the guest CR3 register
102 // creates new shadow page tables
103 // updates the shadow CR3 register to point to the new pts
104 int v3_activate_shadow_pt(struct guest_info * info) {
105 switch (v3_get_cpu_mode(info)) {
108 return activate_shadow_pt_32(info);
110 return activate_shadow_pt_32pae(info);
114 return activate_shadow_pt_64(info);
116 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
124 int v3_activate_passthrough_pt(struct guest_info * info) {
125 // For now... But we need to change this....
126 // As soon as shadow paging becomes active the passthrough tables are hosed
127 // So this will cause chaos if it is called at that time
129 info->ctrl_regs.cr3 = *(addr_t*)&(info->direct_map_pt);
130 //PrintError("Activate Passthrough Page tables not implemented\n");
136 int v3_handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
139 if (v3_get_mem_mode(info) == PHYSICAL_MEM) {
140 // If paging is not turned on we need to handle the special cases
141 return v3_handle_passthrough_pagefault(info, fault_addr, error_code);
142 } else if (v3_get_mem_mode(info) == VIRTUAL_MEM) {
144 switch (v3_get_cpu_mode(info)) {
146 return handle_shadow_pagefault_32(info, fault_addr, error_code);
149 return handle_shadow_pagefault_32pae(info, fault_addr, error_code);
152 case LONG_16_COMPAT: {
154 addr_t fault_addr_base;
155 // first, we will handle the actual fault, non-speculatively
156 rc=handle_shadow_pagefault_64(info, fault_addr, error_code, REGULAR_PAGE_FAULT);
160 if (!SPECULATIVE_PAGING) {
163 fault_addr_base=PAGE_ADDR_4KB(fault_addr);
164 PrintDebug("Attempting speculative paging around %p\n",(void*)fault_addr_base);
165 for (curr_addr = (fault_addr_base & (~0x1fffffLL)) ;
166 curr_addr < (fault_addr_base | (0x1fffffLL)) ;
167 curr_addr+=PAGE_SIZE) {
168 if (curr_addr!=fault_addr_base) {
169 rc=handle_shadow_pagefault_64(info, curr_addr, error_code, SPECULATIVE_PAGE_FAULT);
171 PrintDebug("Speculative page fault handler failed at %p\n",(void*)curr_addr);
180 PrintError("Unhandled CPU Mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
184 PrintError("Invalid Memory mode\n");
190 int v3_handle_shadow_invlpg(struct guest_info * info) {
192 struct x86_instr dec_instr;
196 if (v3_get_mem_mode(info) != VIRTUAL_MEM) {
197 // Paging must be turned on...
198 // should handle with some sort of fault I think
199 PrintError("ERROR: INVLPG called in non paged mode\n");
203 if (v3_get_mem_mode(info) == PHYSICAL_MEM) {
204 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
206 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
210 PrintError("Could not read instruction into buffer\n");
214 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
215 PrintError("Decoding Error\n");
219 if ((dec_instr.op_type != V3_OP_INVLPG) ||
220 (dec_instr.num_operands != 1) ||
221 (dec_instr.dst_operand.type != MEM_OPERAND)) {
222 PrintError("Decoder Error: Not a valid INVLPG instruction...\n");
226 vaddr = dec_instr.dst_operand.operand;
228 info->rip += dec_instr.instr_length;
230 switch (v3_get_cpu_mode(info)) {
232 return handle_shadow_invlpg_32(info, vaddr);
234 return handle_shadow_invlpg_32pae(info, vaddr);
238 return handle_shadow_invlpg_64(info, vaddr);
240 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
248 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info) {
249 struct shadow_page_state * state = &(info->shdw_pg_state);
250 v3_reg_t cur_cr3 = info->ctrl_regs.cr3;
251 struct shadow_page_data * page_tail = NULL;
252 addr_t shdw_page = 0;
254 if (!list_empty(&(state->page_list))) {
255 page_tail = list_tail_entry(&(state->page_list), struct shadow_page_data, page_list_node);
257 if (page_tail->cr3 != cur_cr3) {
258 PrintDebug("Reusing old shadow Page: %p (cur_CR3=%p)(page_cr3=%p) \n",
259 (void *)(addr_t)page_tail->page_pa,
260 (void *)(addr_t)cur_cr3,
261 (void *)(addr_t)(page_tail->cr3));
263 list_move(&(page_tail->page_list_node), &(state->page_list));
265 memset(V3_VAddr((void *)(page_tail->page_pa)), 0, PAGE_SIZE_4KB);
274 page_tail = (struct shadow_page_data *)V3_Malloc(sizeof(struct shadow_page_data));
275 page_tail->page_pa = (addr_t)V3_AllocPages(1);
277 PrintDebug("Allocating new shadow Page: %p (cur_cr3=%p)\n",
278 (void *)(addr_t)page_tail->page_pa,
279 (void *)(addr_t)cur_cr3);
281 page_tail->cr3 = cur_cr3;
282 list_add(&(page_tail->page_list_node), &(state->page_list));
284 shdw_page = (addr_t)V3_VAddr((void *)(page_tail->page_pa));
285 memset((void *)shdw_page, 0, PAGE_SIZE_4KB);
291 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
292 if (info->enable_profiler) {
293 info->profiler.guest_pf_cnt++;
296 info->ctrl_regs.cr2 = fault_addr;
297 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
301 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
302 /* basically the reasoning is that there can be multiple reasons for a page fault:
303 If there is a permissions failure for a page present in the guest _BUT_
304 the reason for the fault was that the page is not present in the shadow,
305 _THEN_ we have to map the shadow page in and reexecute, this will generate
306 a permissions fault which is _THEN_ valid to send to the guest
307 _UNLESS_ both the guest and shadow have marked the page as not present
311 if (guest_access != PT_ACCESS_OK) {
312 // Guest Access Error
314 if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
315 (guest_access != PT_ACCESS_NOT_PRESENT)) {
316 // aka (guest permission error)
320 if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
321 (guest_access == PT_ACCESS_NOT_PRESENT)) {
322 // Page tables completely blank, handle guest first
326 // Otherwise we'll handle the guest fault later...?