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 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
35 #include <palacios/vmm_telemetry.h>
38 #ifdef CONFIG_SYMBIOTIC_SWAP
39 #include <palacios/vmm_sym_swap.h>
42 #ifndef CONFIG_DEBUG_SHADOW_PAGING
44 #define PrintDebug(fmt, args...)
53 struct shadow_page_data {
57 struct list_head page_list_node;
62 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info);
63 static int inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
64 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access);
67 #include "vmm_shadow_paging_32.h"
68 #include "vmm_shadow_paging_32pae.h"
69 #include "vmm_shadow_paging_64.h"
73 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
74 static void telemetry_cb(struct guest_info * info, void * private_data, char * hdr) {
75 V3_Print("%s Guest Page faults: %d\n", hdr, info->shdw_pg_state.guest_faults);
81 int v3_init_shadow_page_state(struct guest_info * info) {
82 struct shadow_page_state * state = &(info->shdw_pg_state);
84 state->prev_guest_cr3 = 0;
88 state->guest_efer.value = 0x0LL;
90 INIT_LIST_HEAD(&(state->page_list));
92 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
93 if (info->enable_telemetry) {
94 v3_add_telemetry_cb(info, telemetry_cb, NULL);
103 // Reads the guest CR3 register
104 // creates new shadow page tables
105 // updates the shadow CR3 register to point to the new pts
106 int v3_activate_shadow_pt(struct guest_info * info) {
107 switch (v3_get_vm_cpu_mode(info)) {
110 return activate_shadow_pt_32(info);
112 return activate_shadow_pt_32pae(info);
116 return activate_shadow_pt_64(info);
118 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(info)));
127 // This must flush any caches
128 // and reset the cr3 value to the correct value
129 int v3_invalidate_shadow_pts(struct guest_info * info) {
130 return v3_activate_shadow_pt(info);
134 int v3_handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
136 if (v3_get_vm_mem_mode(info) == PHYSICAL_MEM) {
137 // If paging is not turned on we need to handle the special cases
138 return v3_handle_passthrough_pagefault(info, fault_addr, error_code);
139 } else if (v3_get_vm_mem_mode(info) == VIRTUAL_MEM) {
141 switch (v3_get_vm_cpu_mode(info)) {
143 return handle_shadow_pagefault_32(info, fault_addr, error_code);
146 return handle_shadow_pagefault_32pae(info, fault_addr, error_code);
150 return handle_shadow_pagefault_64(info, fault_addr, error_code);
153 PrintError("Unhandled CPU Mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(info)));
157 PrintError("Invalid Memory mode\n");
163 int v3_handle_shadow_invlpg(struct guest_info * info) {
165 struct x86_instr dec_instr;
169 if (v3_get_vm_mem_mode(info) != VIRTUAL_MEM) {
170 // Paging must be turned on...
171 // should handle with some sort of fault I think
172 PrintError("ERROR: INVLPG called in non paged mode\n");
176 if (v3_get_vm_mem_mode(info) == PHYSICAL_MEM) {
177 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
179 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
183 PrintError("Could not read instruction into buffer\n");
187 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
188 PrintError("Decoding Error\n");
192 if ((dec_instr.op_type != V3_OP_INVLPG) ||
193 (dec_instr.num_operands != 1) ||
194 (dec_instr.dst_operand.type != MEM_OPERAND)) {
195 PrintError("Decoder Error: Not a valid INVLPG instruction...\n");
199 vaddr = dec_instr.dst_operand.operand;
201 info->rip += dec_instr.instr_length;
203 switch (v3_get_vm_cpu_mode(info)) {
205 return handle_shadow_invlpg_32(info, vaddr);
207 return handle_shadow_invlpg_32pae(info, vaddr);
211 return handle_shadow_invlpg_64(info, vaddr);
213 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(info)));
221 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info) {
222 struct shadow_page_state * state = &(info->shdw_pg_state);
223 v3_reg_t cur_cr3 = info->ctrl_regs.cr3;
224 struct shadow_page_data * page_tail = NULL;
225 addr_t shdw_page = 0;
227 if (!list_empty(&(state->page_list))) {
228 page_tail = list_tail_entry(&(state->page_list), struct shadow_page_data, page_list_node);
230 if (page_tail->cr3 != cur_cr3) {
231 PrintDebug("Reusing old shadow Page: %p (cur_CR3=%p)(page_cr3=%p) \n",
232 (void *)(addr_t)page_tail->page_pa,
233 (void *)(addr_t)cur_cr3,
234 (void *)(addr_t)(page_tail->cr3));
236 list_move(&(page_tail->page_list_node), &(state->page_list));
238 memset(V3_VAddr((void *)(page_tail->page_pa)), 0, PAGE_SIZE_4KB);
247 page_tail = (struct shadow_page_data *)V3_Malloc(sizeof(struct shadow_page_data));
248 page_tail->page_pa = (addr_t)V3_AllocPages(1);
250 PrintDebug("Allocating new shadow Page: %p (cur_cr3=%p)\n",
251 (void *)(addr_t)page_tail->page_pa,
252 (void *)(addr_t)cur_cr3);
254 page_tail->cr3 = cur_cr3;
255 list_add(&(page_tail->page_list_node), &(state->page_list));
257 shdw_page = (addr_t)V3_VAddr((void *)(page_tail->page_pa));
258 memset((void *)shdw_page, 0, PAGE_SIZE_4KB);
264 static int inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
265 info->ctrl_regs.cr2 = fault_addr;
267 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
268 info->shdw_pg_state.guest_faults++;
271 return v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
275 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
276 /* basically the reasoning is that there can be multiple reasons for a page fault:
277 If there is a permissions failure for a page present in the guest _BUT_
278 the reason for the fault was that the page is not present in the shadow,
279 _THEN_ we have to map the shadow page in and reexecute, this will generate
280 a permissions fault which is _THEN_ valid to send to the guest
281 _UNLESS_ both the guest and shadow have marked the page as not present
285 if (guest_access != PT_ACCESS_OK) {
286 // Guest Access Error
288 if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
289 (guest_access != PT_ACCESS_NOT_PRESENT)) {
290 // aka (guest permission error)
295 if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
296 (guest_access == PT_ACCESS_NOT_PRESENT)) {
297 // Page tables completely blank, handle guest first
302 if (guest_access == PT_ACCESS_NOT_PRESENT) {
303 // Page tables completely blank, handle guest first
307 // Otherwise we'll handle the guest fault later...?