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".
23 #include <palacios/vm_guest.h>
24 #include <palacios/vmm_ctrl_regs.h>
25 #include <palacios/vmm.h>
26 #include <palacios/vmm_decoder.h>
27 #include <palacios/vmcb.h>
28 #include <palacios/vm_guest_mem.h>
29 #include <palacios/vmm_lowlevel.h>
30 #include <palacios/vmm_sprintf.h>
31 #include <palacios/vmm_muxer.h>
32 #include <palacios/vmm_xed.h>
35 v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) {
37 struct efer_64 * efer;
38 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
39 struct v3_segment * cs = &(info->segments.cs);
40 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
42 if (info->shdw_pg_mode == SHADOW_PAGING) {
43 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
44 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
45 } else if (info->shdw_pg_mode == NESTED_PAGING) {
46 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
47 efer = (struct efer_64 *)&(guest_state->efer);
49 PrintError("Invalid Paging Mode...\n");
56 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
58 } else if (efer->lme == 0) {
60 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
63 // What about LONG_16_COMPAT???
64 return LONG_32_COMPAT;
68 // Get address width in bytes
69 uint_t v3_get_addr_width(struct guest_info * info) {
71 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
72 struct efer_64 * efer;
73 struct v3_segment * cs = &(info->segments.cs);
74 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
76 if (info->shdw_pg_mode == SHADOW_PAGING) {
77 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
78 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
79 } else if (info->shdw_pg_mode == NESTED_PAGING) {
80 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
81 efer = (struct efer_64 *)&(guest_state->efer);
83 PrintError("Invalid Paging Mode...\n");
90 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
92 } else if (efer->lme == 0) {
94 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
97 // What about LONG_16_COMPAT???
103 static const uchar_t REAL_STR[] = "Real";
104 static const uchar_t PROTECTED_STR[] = "Protected";
105 static const uchar_t PROTECTED_PAE_STR[] = "Protected+PAE";
106 static const uchar_t LONG_STR[] = "Long";
107 static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
108 static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
110 const uchar_t * v3_cpu_mode_to_str(v3_cpu_mode_t mode) {
115 return PROTECTED_STR;
117 return PROTECTED_PAE_STR;
121 return LONG_32_COMPAT_STR;
123 return LONG_16_COMPAT_STR;
129 v3_mem_mode_t v3_get_vm_mem_mode(struct guest_info * info) {
132 if (info->shdw_pg_mode == SHADOW_PAGING) {
133 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
134 } else if (info->shdw_pg_mode == NESTED_PAGING) {
135 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
137 PrintError("Invalid Paging Mode...\n");
149 static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
150 static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
152 const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) {
164 void v3_print_segments(struct v3_segments * segs) {
166 struct v3_segment * seg_ptr;
168 seg_ptr=(struct v3_segment *)segs;
170 char *seg_names[] = {"CS", "DS" , "ES", "FS", "GS", "SS" , "LDTR", "GDTR", "IDTR", "TR", NULL};
171 V3_Print("Segments\n");
173 for (i = 0; seg_names[i] != NULL; i++) {
175 V3_Print("\t%s: Sel=%x, base=%p, limit=%x (long_mode=%d, db=%d)\n", seg_names[i], seg_ptr[i].selector,
176 (void *)(addr_t)seg_ptr[i].base, seg_ptr[i].limit,
177 seg_ptr[i].long_mode, seg_ptr[i].db);
183 // We don't handle those fancy 64 bit system segments...
185 int v3_translate_segment(struct guest_info * info, uint16_t selector, struct v3_segment * seg) {
186 struct v3_segment * gdt = &(info->segments.gdtr);
188 uint16_t seg_offset = (selector & ~0x7);
190 struct gen_segment * gen_seg = NULL;
191 struct seg_selector sel;
193 memset(seg, 0, sizeof(struct v3_segment));
195 sel.value = selector;
198 PrintError("LDT translations not supported\n");
202 if (v3_gva_to_hva(info, gdt->base, &gdt_addr) == -1) {
203 PrintError("Unable to translate GDT address\n");
207 seg_addr = gdt_addr + seg_offset;
208 gen_seg = (struct gen_segment *)seg_addr;
211 seg->selector = selector;
213 seg->limit = gen_seg->limit_hi;
215 seg->limit += gen_seg->limit_lo;
217 seg->base = gen_seg->base_hi;
219 seg->base += gen_seg->base_lo;
221 if (gen_seg->granularity == 1) {
226 seg->type = gen_seg->type;
227 seg->system = gen_seg->system;
228 seg->dpl = gen_seg->dpl;
229 seg->present = gen_seg->present;
230 seg->avail = gen_seg->avail;
231 seg->long_mode = gen_seg->long_mode;
232 seg->db = gen_seg->db;
233 seg->granularity = gen_seg->granularity;
241 void v3_print_ctrl_regs(struct guest_info * info) {
242 struct v3_ctrl_regs * regs = &(info->ctrl_regs);
245 char * reg_names[] = {"CR0", "CR2", "CR3", "CR4", "CR8", "FLAGS", NULL};
246 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(info->vmm_data);
248 reg_ptr = (v3_reg_t *)regs;
250 V3_Print("32 bit Ctrl Regs:\n");
252 for (i = 0; reg_names[i] != NULL; i++) {
253 V3_Print("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
256 V3_Print("\tEFER=0x%p\n", (void*)(addr_t)(guest_state->efer));
261 static int safe_gva_to_hva(struct guest_info * info, addr_t linear_addr, addr_t * host_addr) {
262 /* select the proper translation based on guest mode */
263 if (info->mem_mode == PHYSICAL_MEM) {
264 if (v3_gpa_to_hva(info, linear_addr, host_addr) == -1) return -1;
265 } else if (info->mem_mode == VIRTUAL_MEM) {
266 if (v3_gva_to_hva(info, linear_addr, host_addr) == -1) return -1;
271 static int v3_print_disassembly(struct guest_info * info) {
273 addr_t rip, rip_linear, rip_host;
275 /* we don't know where the instructions preceding RIP start, so we just take
276 * a guess and hope the instruction stream synced up with our disassembly
277 * some time before RIP; if it has not we correct RIP at that point
280 /* start disassembly 64 bytes before current RIP, continue 32 bytes after */
281 rip = (addr_t) info->rip - 64;
282 while ((int) (rip - info->rip) < 32) {
283 /* always print RIP, even if the instructions before were bad */
284 if (!passed_rip && rip >= info->rip) {
285 if (rip != info->rip) {
286 V3_Print("***** bad disassembly up to this point *****\n");
292 /* look up host virtual address for this instruction */
293 rip_linear = get_addr_linear(info, rip, &(info->segments.cs));
294 if (safe_gva_to_hva(info, rip_linear, &rip_host) < 0) {
299 /* print disassembled instrcution (updates rip) */
300 if (v3_disasm(info, (void *) rip_host, &rip, rip == info->rip) < 0) {
310 void v3_print_guest_state(struct guest_info * info) {
311 addr_t linear_addr = 0;
313 V3_Print("RIP: %p\n", (void *)(addr_t)(info->rip));
314 linear_addr = get_addr_linear(info, info->rip, &(info->segments.cs));
315 V3_Print("RIP Linear: %p\n", (void *)linear_addr);
317 V3_Print("NumExits: %u\n", (uint32_t)info->num_exits);
319 v3_print_segments(&(info->segments));
320 v3_print_ctrl_regs(info);
322 if (info->shdw_pg_mode == SHADOW_PAGING) {
323 V3_Print("Shadow Paging Guest Registers:\n");
324 V3_Print("\tGuest CR0=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr0));
325 V3_Print("\tGuest CR3=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr3));
326 V3_Print("\tGuest EFER=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_efer.value));
331 v3_print_mem_map(info->vm_info);
333 v3_print_stack(info);
335 v3_print_disassembly(info);
339 void v3_print_stack(struct guest_info * info) {
340 addr_t linear_addr = 0;
341 addr_t host_addr = 0;
343 v3_cpu_mode_t cpu_mode = v3_get_vm_cpu_mode(info);
346 linear_addr = get_addr_linear(info, info->vm_regs.rsp, &(info->segments.ss));
348 V3_Print("Stack at %p:\n", (void *)linear_addr);
350 if (info->mem_mode == PHYSICAL_MEM) {
351 if (v3_gpa_to_hva(info, linear_addr, &host_addr) == -1) {
352 PrintError("Could not translate Stack address\n");
355 } else if (info->mem_mode == VIRTUAL_MEM) {
356 if (v3_gva_to_hva(info, linear_addr, &host_addr) == -1) {
357 PrintError("Could not translate Virtual Stack address\n");
362 V3_Print("Host Address of rsp = 0x%p\n", (void *)host_addr);
364 // We start i at one because the current stack pointer points to an unused stack element
365 for (i = 0; i <= 24; i++) {
366 if (cpu_mode == LONG) {
367 V3_Print("\t%p\n", (void *)*(addr_t *)(host_addr + (i * 8)));
368 } else if (cpu_mode == REAL) {
369 V3_Print("Don't currently handle 16 bit stacks... \n");
372 V3_Print("\t%.8x\n", *(uint32_t *)(host_addr + (i * 4)));
380 void v3_print_GPRs(struct guest_info * info) {
381 struct v3_gprs * regs = &(info->vm_regs);
384 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", NULL};
386 reg_ptr= (v3_reg_t *)regs;
388 V3_Print("32 bit GPRs:\n");
390 for (i = 0; reg_names[i] != NULL; i++) {
391 V3_Print("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
397 void v3_print_GPRs(struct guest_info * info) {
398 struct v3_gprs * regs = &(info->vm_regs);
401 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", \
402 "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", NULL};
404 reg_ptr = (v3_reg_t *)regs;
406 V3_Print("64 bit GPRs:\n");
408 for (i = 0; reg_names[i] != NULL; i++) {
409 V3_Print("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
416 #include <palacios/vmcs.h>
417 #include <palacios/vmcb.h>
418 static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_data) {
419 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
422 V3_Print("************** Guest State ************\n");
423 v3_print_guest_state(core);
427 if ((cpu_type == V3_SVM_CPU) || (cpu_type == V3_SVM_REV3_CPU)) {
429 PrintDebugVMCB((vmcb_t *)(core->vmm_data));
433 if ((cpu_type == V3_VMX_CPU) || (cpu_type == V3_VMX_EPT_CPU)) {
439 PrintError("Invalid CPU Type 0x%x\n", cpu_type);
449 #include <palacios/svm.h>
450 #include <palacios/svm_io.h>
451 #include <palacios/svm_msr.h>
455 #include <palacios/vmx.h>
456 #include <palacios/vmx_io.h>
457 #include <palacios/vmx_msr.h>
461 int v3_init_vm(struct v3_vm_info * vm) {
462 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
465 if (v3_get_foreground_vm() == NULL) {
466 v3_set_foreground_vm(vm);
469 #ifdef CONFIG_TELEMETRY
470 v3_init_telemetry(vm);
473 v3_init_hypercall_map(vm);
476 v3_init_cpuid_map(vm);
477 v3_init_host_events(vm);
478 v3_init_intr_routers(vm);
480 // Initialize the memory map
481 if (v3_init_mem_map(vm) == -1) {
482 PrintError("Could not initialize shadow map\n");
486 v3_init_mem_hooks(vm);
488 if (v3_init_shdw_impl(vm) == -1) {
489 PrintError("VM initialization error in shadow implementaion\n");
495 #ifdef CONFIG_SYMBIOTIC
496 v3_init_symbiotic_vm(vm);
504 if ((cpu_type == V3_SVM_CPU) || (cpu_type == V3_SVM_REV3_CPU)) {
505 v3_init_svm_io_map(vm);
506 v3_init_svm_msr_map(vm);
511 if ((cpu_type == V3_VMX_CPU) || (cpu_type == V3_VMX_EPT_CPU)) {
512 v3_init_vmx_io_map(vm);
513 v3_init_vmx_msr_map(vm);
518 PrintError("Invalid CPU Type 0x%x\n", cpu_type);
522 v3_register_hypercall(vm, GUEST_INFO_HCALL, info_hcall, NULL);
524 V3_Print("GUEST_INFO_HCALL=%x\n", GUEST_INFO_HCALL);
529 int v3_init_core(struct guest_info * core) {
530 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
531 struct v3_vm_info * vm = core->vm_info;
534 * Initialize the subsystem data strutures
536 #ifdef CONFIG_TELEMETRY
537 v3_init_core_telemetry(core);
540 if (core->shdw_pg_mode == SHADOW_PAGING) {
541 v3_init_shdw_pg_state(core);
545 v3_init_intr_controllers(core);
546 v3_init_exception_state(core);
548 v3_init_decoder(core);
551 #ifdef CONFIG_SYMBIOTIC
552 v3_init_symbiotic_core(core);
561 case V3_SVM_REV3_CPU:
562 if (v3_init_svm_vmcb(core, vm->vm_class) == -1) {
563 PrintError("Error in SVM initialization\n");
571 if (v3_init_vmx_vmcs(core, vm->vm_class) == -1) {
572 PrintError("Error in VMX initialization\n");
578 PrintError("Invalid CPU Type 0x%x\n", cpu_type);