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 #ifndef DEBUG_SHADOW_PAGING
33 #define PrintDebug(fmt, args...)
42 struct shadow_page_data {
46 struct list_head page_list_node;
50 DEFINE_HASHTABLE_INSERT(add_pte_map, addr_t, addr_t);
51 DEFINE_HASHTABLE_SEARCH(find_pte_map, addr_t, addr_t);
52 //DEFINE_HASHTABLE_REMOVE(del_pte_map, addr_t, addr_t, 0);
56 static uint_t pte_hash_fn(addr_t key) {
57 return hash_long(key, 32);
60 static int pte_equals(addr_t key1, addr_t key2) {
61 return (key1 == key2);
64 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info);
65 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
66 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access);
69 #include "vmm_shadow_paging_32.h"
70 #include "vmm_shadow_paging_32pae.h"
71 #include "vmm_shadow_paging_64.h"
75 int v3_init_shadow_page_state(struct guest_info * info) {
76 struct shadow_page_state * state = &(info->shdw_pg_state);
81 INIT_LIST_HEAD(&(state->page_list));
83 state->cached_ptes = NULL;
84 state->cached_cr3 = 0;
91 // Reads the guest CR3 register
92 // creates new shadow page tables
93 // updates the shadow CR3 register to point to the new pts
94 int v3_activate_shadow_pt(struct guest_info * info) {
95 switch (v3_get_cpu_mode(info)) {
98 return activate_shadow_pt_32(info);
100 return activate_shadow_pt_32pae(info);
104 return activate_shadow_pt_64(info);
106 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
114 int v3_activate_passthrough_pt(struct guest_info * info) {
115 // For now... But we need to change this....
116 // As soon as shadow paging becomes active the passthrough tables are hosed
117 // So this will cause chaos if it is called at that time
119 info->ctrl_regs.cr3 = *(addr_t*)&(info->direct_map_pt);
120 //PrintError("Activate Passthrough Page tables not implemented\n");
126 int v3_handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
128 if (v3_get_mem_mode(info) == PHYSICAL_MEM) {
129 // If paging is not turned on we need to handle the special cases
130 return handle_special_page_fault(info, fault_addr, fault_addr, error_code);
131 } else if (v3_get_mem_mode(info) == VIRTUAL_MEM) {
133 switch (v3_get_cpu_mode(info)) {
135 return handle_shadow_pagefault_32(info, fault_addr, error_code);
138 return handle_shadow_pagefault_32pae(info, fault_addr, error_code);
142 return handle_shadow_pagefault_64(info, fault_addr, error_code);
145 PrintError("Unhandled CPU Mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
149 PrintError("Invalid Memory mode\n");
155 int v3_handle_shadow_invlpg(struct guest_info * info) {
157 struct x86_instr dec_instr;
161 if (v3_get_mem_mode(info) != VIRTUAL_MEM) {
162 // Paging must be turned on...
163 // should handle with some sort of fault I think
164 PrintError("ERROR: INVLPG called in non paged mode\n");
168 if (v3_get_mem_mode(info) == PHYSICAL_MEM) {
169 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
171 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
175 PrintError("Could not read instruction into buffer\n");
179 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
180 PrintError("Decoding Error\n");
184 if ((dec_instr.op_type != V3_OP_INVLPG) ||
185 (dec_instr.num_operands != 1) ||
186 (dec_instr.dst_operand.type != MEM_OPERAND)) {
187 PrintError("Decoder Error: Not a valid INVLPG instruction...\n");
191 vaddr = dec_instr.dst_operand.operand;
193 info->rip += dec_instr.instr_length;
195 switch (v3_get_cpu_mode(info)) {
197 return handle_shadow_invlpg_32(info, vaddr);
199 return handle_shadow_invlpg_32pae(info, vaddr);
203 return handle_shadow_invlpg_64(info, vaddr);
205 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
213 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info) {
214 struct shadow_page_state * state = &(info->shdw_pg_state);
215 v3_reg_t cur_cr3 = info->ctrl_regs.cr3;
216 struct shadow_page_data * page_tail = NULL;
217 addr_t shdw_page = 0;
219 if (!list_empty(&(state->page_list))) {
220 page_tail = list_tail_entry(&(state->page_list), struct shadow_page_data, page_list_node);
222 if (page_tail->cr3 != cur_cr3) {
223 PrintDebug("Reusing old shadow Page: %p (cur_CR3=%p)(page_cr3=%p) \n",
224 (void *) page_tail->page_pa, (void *)cur_cr3, (void *)(page_tail->cr3));
226 list_move(&(page_tail->page_list_node), &(state->page_list));
228 memset(V3_VAddr((void *)(page_tail->page_pa)), 0, PAGE_SIZE_4KB);
237 page_tail = (struct shadow_page_data *)V3_Malloc(sizeof(struct shadow_page_data));
238 page_tail->page_pa = (addr_t)V3_AllocPages(1);
240 PrintDebug("Allocating new shadow Page: %p (cur_cr3=%p)\n", (void *)page_tail->page_pa, (void *)cur_cr3);
242 page_tail->cr3 = cur_cr3;
243 list_add(&(page_tail->page_list_node), &(state->page_list));
245 shdw_page = (addr_t)V3_VAddr((void *)(page_tail->page_pa));
246 memset((void *)shdw_page, 0, PAGE_SIZE_4KB);
252 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
253 if (info->enable_profiler) {
254 info->profiler.guest_pf_cnt++;
257 info->ctrl_regs.cr2 = fault_addr;
258 v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
262 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
263 /* basically the reasoning is that there can be multiple reasons for a page fault:
264 If there is a permissions failure for a page present in the guest _BUT_
265 the reason for the fault was that the page is not present in the shadow,
266 _THEN_ we have to map the shadow page in and reexecute, this will generate
267 a permissions fault which is _THEN_ valid to send to the guest
268 _UNLESS_ both the guest and shadow have marked the page as not present
272 if (guest_access != PT_ACCESS_OK) {
273 // Guest Access Error
275 if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
276 (guest_access != PT_ACCESS_NOT_PRESENT)) {
277 // aka (guest permission error)
281 if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
282 (guest_access == PT_ACCESS_NOT_PRESENT)) {
283 // Page tables completely blank, handle guest first
287 // Otherwise we'll handle the guest fault later...?