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>
36 #ifdef V3_CONFIG_SHADOW_PAGING_TELEMETRY
37 #include <palacios/vmm_telemetry.h>
40 #ifdef V3_CONFIG_SYMBIOTIC_SWAP
41 #include <palacios/vmm_sym_swap.h>
44 #ifndef V3_CONFIG_DEBUG_SHADOW_PAGING
46 #define PrintDebug(fmt, args...)
50 static const char default_strategy[] = "VTLB";
53 static struct hashtable * master_shdw_pg_table = NULL;
56 struct event_callback {
57 int (*callback)(struct guest_info *core, struct v3_shdw_pg_event *event, void *priv_data);
60 struct list_head node;
63 static uint_t shdw_pg_hash_fn(addr_t key) {
64 char * name = (char *)key;
65 return v3_hash_buffer((uint8_t *)name, strlen(name));
68 static int shdw_pg_eq_fn(addr_t key1, addr_t key2) {
69 char * name1 = (char *)key1;
70 char * name2 = (char *)key2;
72 return (strcmp(name1, name2) == 0);
75 static int have_callbacks(struct guest_info *core)
77 return !list_empty(&(core->vm_info->shdw_impl.event_callback_list));
80 static void dispatch_event(struct guest_info *core, struct v3_shdw_pg_event *event)
82 struct event_callback *cb,*temp;
84 list_for_each_entry_safe(cb,
86 &(core->vm_info->shdw_impl.event_callback_list),
88 cb->callback(core,event,cb->priv_data);
93 int V3_init_shdw_paging() {
94 extern struct v3_shdw_pg_impl * __start__v3_shdw_pg_impls[];
95 extern struct v3_shdw_pg_impl * __stop__v3_shdw_pg_impls[];
96 struct v3_shdw_pg_impl ** tmp_impl = __start__v3_shdw_pg_impls;
99 master_shdw_pg_table = v3_create_htable(0, shdw_pg_hash_fn, shdw_pg_eq_fn);
102 while (tmp_impl != __stop__v3_shdw_pg_impls) {
103 V3_Print(VM_NONE, VCORE_NONE, "Registering Shadow Paging Impl (%s)\n", (*tmp_impl)->name);
105 if (v3_htable_search(master_shdw_pg_table, (addr_t)((*tmp_impl)->name))) {
106 PrintError(VM_NONE, VCORE_NONE, "Multiple instances of shadow paging impl (%s)\n", (*tmp_impl)->name);
110 if (v3_htable_insert(master_shdw_pg_table,
111 (addr_t)((*tmp_impl)->name),
112 (addr_t)(*tmp_impl)) == 0) {
113 PrintError(VM_NONE, VCORE_NONE, "Could not register shadow paging impl (%s)\n", (*tmp_impl)->name);
117 tmp_impl = &(__start__v3_shdw_pg_impls[++i]);
123 int V3_deinit_shdw_paging() {
124 v3_free_htable(master_shdw_pg_table, 0, 0);
135 #ifdef V3_CONFIG_SHADOW_PAGING_TELEMETRY
136 static void telemetry_cb(struct v3_vm_info * vm, void * private_data, char * hdr) {
138 for (i = 0; i < vm->num_cores; i++) {
139 struct guest_info * core = &(vm->cores[i]);
141 V3_Print(vm, core, "%s Guest Page faults: %d\n", hdr, core->shdw_pg_state.guest_faults);
148 int v3_init_shdw_pg_state(struct guest_info * core) {
149 struct v3_shdw_pg_state * state = &(core->shdw_pg_state);
150 struct v3_shdw_pg_impl * impl = core->vm_info->shdw_impl.current_impl;
153 state->guest_cr3 = 0;
154 state->guest_cr0 = 0;
155 state->guest_efer.value = 0x0LL;
157 if (impl->local_init(core) == -1) {
158 PrintError(core->vm_info, core, "Error in Shadow paging local initialization (%s)\n", impl->name);
163 #ifdef V3_CONFIG_SHADOW_PAGING_TELEMETRY
164 v3_add_telemetry_cb(core->vm_info, telemetry_cb, NULL);
172 int v3_deinit_shdw_pg_state(struct guest_info * core) {
173 struct v3_shdw_pg_impl * impl = core->vm_info->shdw_impl.current_impl;
175 if (impl->local_deinit(core) == -1) {
176 PrintError(core->vm_info, core, "Error deinitializing shadow paging state\n");
186 int v3_init_shdw_impl(struct v3_vm_info * vm) {
187 struct v3_shdw_impl_state * impl_state = &(vm->shdw_impl);
188 v3_cfg_tree_t * pg_cfg = v3_cfg_subtree(vm->cfg_data->cfg, "paging");
189 char * pg_mode = v3_cfg_val(pg_cfg, "mode");
190 char * pg_strat = v3_cfg_val(pg_cfg, "strategy");
191 struct v3_shdw_pg_impl * impl = NULL;
193 PrintDebug(vm, VCORE_NONE, "Checking if shadow paging requested.\n");
194 if (pg_mode == NULL) {
195 V3_Print(vm, VCORE_NONE, "No paging mode specified, assuming shadow with defaults\n");
198 if (strcasecmp(pg_mode, "nested") == 0) {
199 // this check is repeated here (compare to vmm_config's determine paging mode) since
200 // shadow paging initialization *precedes* per-core pre-config.
201 extern v3_cpu_arch_t v3_mach_type;
202 if ((v3_mach_type == V3_SVM_REV3_CPU) ||
203 (v3_mach_type == V3_VMX_EPT_CPU) ||
204 (v3_mach_type == V3_VMX_EPT_UG_CPU)) {
205 PrintDebug(vm, VCORE_NONE, "Nested paging specified on machine that supports it - not initializing shadow paging\n");
208 V3_Print(vm, VCORE_NONE, "Nested paging specified but machine does not support it - falling back to shadow paging with defaults\n");
211 } else if (strcasecmp(pg_mode, "shadow") != 0) {
212 V3_Print(vm, VCORE_NONE, "Unknown paging mode '%s' specified - falling back to shadow paging with defaults\n",pg_mode);
217 if (pg_strat == NULL) {
218 pg_strat = (char *)default_strategy;
221 V3_Print(vm, VCORE_NONE,"Initialization of Shadow Paging implementation\n");
223 impl = (struct v3_shdw_pg_impl *)v3_htable_search(master_shdw_pg_table, (addr_t)pg_strat);
226 PrintError(vm, VCORE_NONE, "Could not find shadow paging impl (%s)\n", pg_strat);
230 INIT_LIST_HEAD(&(impl_state->event_callback_list));
232 impl_state->current_impl = impl;
234 if (impl->init(vm, pg_cfg) == -1) {
235 PrintError(vm, VCORE_NONE, "Could not initialize Shadow paging implemenation (%s)\n", impl->name);
244 int v3_deinit_shdw_impl(struct v3_vm_info * vm) {
245 struct v3_shdw_pg_impl * impl = vm->shdw_impl.current_impl;
246 struct event_callback *cb,*temp;
249 // Shadow paging not implemented
253 if (impl->deinit(vm) == -1) {
254 PrintError(vm, VCORE_NONE,"Error deinitializing shadow paging implementation\n");
258 list_for_each_entry_safe(cb,
260 &(vm->shdw_impl.event_callback_list),
262 list_del(&(cb->node));
270 // Reads the guest CR3 register
271 // creates new shadow page tables
272 // updates the shadow CR3 register to point to the new pts
273 int v3_activate_shadow_pt(struct guest_info * core) {
274 struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
275 struct v3_shdw_pg_impl * impl = state->current_impl;
277 if (!have_callbacks(core)) {
278 return impl->activate_shdw_pt(core);
281 struct v3_shdw_pg_event event_pre={SHADOW_ACTIVATE,SHADOW_PREIMPL,0,{0,0,0,0,0,0}};
282 struct v3_shdw_pg_event event_post={SHADOW_ACTIVATE,SHADOW_POSTIMPL,0,{0,0,0,0,0,0}};
284 dispatch_event(core,&event_pre);
286 rc =impl->activate_shdw_pt(core);
288 dispatch_event(core,&event_post);
296 // This must flush any caches
297 // and reset the cr3 value to the correct value
298 int v3_invalidate_shadow_pts(struct guest_info * core) {
299 struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
300 struct v3_shdw_pg_impl * impl = state->current_impl;
302 if (!have_callbacks(core)) {
303 return impl->invalidate_shdw_pt(core);
306 struct v3_shdw_pg_event event_pre={SHADOW_INVALIDATE,SHADOW_PREIMPL,0,{0,0,0,0,0,0}};
307 struct v3_shdw_pg_event event_post={SHADOW_INVALIDATE,SHADOW_POSTIMPL,0,{0,0,0,0,0,0}};
309 dispatch_event(core,&event_pre);
311 rc = impl->invalidate_shdw_pt(core);
313 dispatch_event(core,&event_post);
320 int v3_handle_shadow_pagefault(struct guest_info * core, addr_t fault_addr, pf_error_t error_code)
325 if (have_callbacks(core)) {
326 struct v3_shdw_pg_event event={SHADOW_PAGEFAULT,SHADOW_PREIMPL,fault_addr,error_code};
327 dispatch_event(core,&event);
330 if (v3_get_vm_mem_mode(core) == PHYSICAL_MEM) {
331 // If paging is not turned on we need to handle the special cases
332 rc = v3_handle_passthrough_pagefault(core, fault_addr, error_code);
333 } else if (v3_get_vm_mem_mode(core) == VIRTUAL_MEM) {
334 struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
335 struct v3_shdw_pg_impl * impl = state->current_impl;
337 rc = impl->handle_pagefault(core, fault_addr, error_code);
339 PrintError(core->vm_info, core, "Invalid Memory mode\n");
343 if (have_callbacks(core)) {
344 struct v3_shdw_pg_event event={SHADOW_PAGEFAULT,SHADOW_POSTIMPL,fault_addr,error_code};
345 dispatch_event(core,&event);
352 int v3_handle_shadow_invlpg(struct guest_info * core) {
354 struct x86_instr dec_instr;
358 if (v3_get_vm_mem_mode(core) != VIRTUAL_MEM) {
359 // Paging must be turned on...
360 // should handle with some sort of fault I think
361 PrintError(core->vm_info, core, "ERROR: INVLPG called in non paged mode\n");
365 if (v3_get_vm_mem_mode(core) == PHYSICAL_MEM) {
366 ret = v3_read_gpa_memory(core, get_addr_linear(core, core->rip, &(core->segments.cs)), 15, instr);
368 ret = v3_read_gva_memory(core, get_addr_linear(core, core->rip, &(core->segments.cs)), 15, instr);
372 PrintError(core->vm_info, core, "Could not read instruction into buffer\n");
376 if (v3_decode(core, (addr_t)instr, &dec_instr) == -1) {
377 PrintError(core->vm_info, core, "Decoding Error\n");
381 if ((dec_instr.op_type != V3_OP_INVLPG) ||
382 (dec_instr.num_operands != 1) ||
383 (dec_instr.dst_operand.type != MEM_OPERAND)) {
384 PrintError(core->vm_info, core, "Decoder Error: Not a valid INVLPG instruction...\n");
388 vaddr = dec_instr.dst_operand.operand;
390 core->rip += dec_instr.instr_length;
393 struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
394 struct v3_shdw_pg_impl * impl = state->current_impl;
397 if (have_callbacks(core)) {
398 struct v3_shdw_pg_event event={SHADOW_INVLPG,SHADOW_PREIMPL,vaddr,{0,0,0,0,0,0}};
399 dispatch_event(core,&event);
402 rc=impl->handle_invlpg(core, vaddr);
404 if (have_callbacks(core)) {
405 struct v3_shdw_pg_event event={SHADOW_INVLPG,SHADOW_POSTIMPL,vaddr,{0,0,0,0,0,0}};
406 dispatch_event(core,&event);
418 int v3_inject_guest_pf(struct guest_info * core, addr_t fault_addr, pf_error_t error_code) {
419 core->ctrl_regs.cr2 = fault_addr;
421 #ifdef V3_CONFIG_SHADOW_PAGING_TELEMETRY
422 core->shdw_pg_state.guest_faults++;
425 return v3_raise_exception_with_error(core, PF_EXCEPTION, *(uint_t *)&error_code);
429 int v3_is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
430 /* basically the reasoning is that there can be multiple reasons for a page fault:
431 If there is a permissions failure for a page present in the guest _BUT_
432 the reason for the fault was that the page is not present in the shadow,
433 _THEN_ we have to map the shadow page in and reexecute, this will generate
434 a permissions fault which is _THEN_ valid to send to the guest
435 _UNLESS_ both the guest and shadow have marked the page as not present
439 if (guest_access != PT_ACCESS_OK) {
440 // Guest Access Error
442 if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
443 (guest_access != PT_ACCESS_NOT_PRESENT)) {
444 // aka (guest permission error)
449 if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
450 (guest_access == PT_ACCESS_NOT_PRESENT)) {
451 // Page tables completely blank, handle guest first
456 if (guest_access == PT_ACCESS_NOT_PRESENT) {
457 // Page tables completely blank, handle guest first
461 // Otherwise we'll handle the guest fault later...?
468 int v3_register_shadow_paging_event_callback(struct v3_vm_info *vm,
469 int (*callback)(struct guest_info *core,
470 struct v3_shdw_pg_event *event,
474 struct event_callback *ec = V3_Malloc(sizeof(struct event_callback));
477 PrintError(vm, VCORE_NONE, "Unable to allocate for a shadow paging event callback\n");
481 ec->callback = callback;
482 ec->priv_data = priv_data;
484 list_add(&(ec->node),&(vm->shdw_impl.event_callback_list));
490 int v3_unregister_shadow_paging_event_callback(struct v3_vm_info *vm,
491 int (*callback)(struct guest_info *core,
492 struct v3_shdw_pg_event *event,
496 struct event_callback *cb,*temp;
498 list_for_each_entry_safe(cb,
500 &(vm->shdw_impl.event_callback_list),
502 if ((callback == cb->callback) && (priv_data == cb->priv_data)) {
503 list_del(&(cb->node));
509 PrintError(vm, VCORE_NONE, "No callback found!\n");