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>
33 #include <palacios/vmm_direct_paging.h>
37 v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) {
39 struct efer_64 * efer;
40 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
41 struct v3_segment * cs = &(info->segments.cs);
42 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
44 if (info->shdw_pg_mode == SHADOW_PAGING) {
45 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
46 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
47 } else if (info->shdw_pg_mode == NESTED_PAGING) {
48 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
49 efer = (struct efer_64 *)&(guest_state->efer);
51 PrintError("Invalid Paging Mode...\n");
58 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
60 } else if (efer->lme == 0) {
62 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
65 // What about LONG_16_COMPAT???
66 return LONG_32_COMPAT;
70 // Get address width in bytes
71 uint_t v3_get_addr_width(struct guest_info * info) {
73 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
74 struct efer_64 * efer;
75 struct v3_segment * cs = &(info->segments.cs);
76 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
78 if (info->shdw_pg_mode == SHADOW_PAGING) {
79 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
80 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
81 } else if (info->shdw_pg_mode == NESTED_PAGING) {
82 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
83 efer = (struct efer_64 *)&(guest_state->efer);
85 PrintError("Invalid Paging Mode...\n");
92 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
94 } else if (efer->lme == 0) {
96 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
99 // What about LONG_16_COMPAT???
105 static const uchar_t REAL_STR[] = "Real";
106 static const uchar_t PROTECTED_STR[] = "Protected";
107 static const uchar_t PROTECTED_PAE_STR[] = "Protected+PAE";
108 static const uchar_t LONG_STR[] = "Long";
109 static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
110 static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
112 const uchar_t * v3_cpu_mode_to_str(v3_cpu_mode_t mode) {
117 return PROTECTED_STR;
119 return PROTECTED_PAE_STR;
123 return LONG_32_COMPAT_STR;
125 return LONG_16_COMPAT_STR;
131 v3_mem_mode_t v3_get_vm_mem_mode(struct guest_info * info) {
134 if (info->shdw_pg_mode == SHADOW_PAGING) {
135 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
136 } else if (info->shdw_pg_mode == NESTED_PAGING) {
137 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
139 PrintError("Invalid Paging Mode...\n");
151 static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
152 static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
154 const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) {
166 void v3_print_segments(struct v3_segments * segs) {
168 struct v3_segment * seg_ptr;
170 seg_ptr=(struct v3_segment *)segs;
172 char *seg_names[] = {"CS", "DS" , "ES", "FS", "GS", "SS" , "LDTR", "GDTR", "IDTR", "TR", NULL};
173 V3_Print("Segments\n");
175 for (i = 0; seg_names[i] != NULL; i++) {
177 V3_Print("\t%s: Sel=%x, base=%p, limit=%x (long_mode=%d, db=%d)\n", seg_names[i], seg_ptr[i].selector,
178 (void *)(addr_t)seg_ptr[i].base, seg_ptr[i].limit,
179 seg_ptr[i].long_mode, seg_ptr[i].db);
185 // We don't handle those fancy 64 bit system segments...
187 int v3_translate_segment(struct guest_info * info, uint16_t selector, struct v3_segment * seg) {
188 struct v3_segment * gdt = &(info->segments.gdtr);
190 uint16_t seg_offset = (selector & ~0x7);
192 struct gen_segment * gen_seg = NULL;
193 struct seg_selector sel;
195 memset(seg, 0, sizeof(struct v3_segment));
197 sel.value = selector;
200 PrintError("LDT translations not supported\n");
204 if (v3_gva_to_hva(info, gdt->base, &gdt_addr) == -1) {
205 PrintError("Unable to translate GDT address\n");
209 seg_addr = gdt_addr + seg_offset;
210 gen_seg = (struct gen_segment *)seg_addr;
213 seg->selector = selector;
215 seg->limit = gen_seg->limit_hi;
217 seg->limit += gen_seg->limit_lo;
219 seg->base = gen_seg->base_hi;
221 seg->base += gen_seg->base_lo;
223 if (gen_seg->granularity == 1) {
228 seg->type = gen_seg->type;
229 seg->system = gen_seg->system;
230 seg->dpl = gen_seg->dpl;
231 seg->present = gen_seg->present;
232 seg->avail = gen_seg->avail;
233 seg->long_mode = gen_seg->long_mode;
234 seg->db = gen_seg->db;
235 seg->granularity = gen_seg->granularity;
243 void v3_print_ctrl_regs(struct guest_info * info) {
244 struct v3_ctrl_regs * regs = &(info->ctrl_regs);
247 char * reg_names[] = {"CR0", "CR2", "CR3", "CR4", "CR8", "FLAGS", NULL};
248 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(info->vmm_data);
250 reg_ptr = (v3_reg_t *)regs;
252 V3_Print("32 bit Ctrl Regs:\n");
254 for (i = 0; reg_names[i] != NULL; i++) {
255 V3_Print("\t%s=0x%p (at %p)\n", reg_names[i], (void *)(addr_t)reg_ptr[i], &(reg_ptr[i]));
258 V3_Print("\tEFER=0x%p\n", (void*)(addr_t)(guest_state->efer));
263 static int safe_gva_to_hva(struct guest_info * info, addr_t linear_addr, addr_t * host_addr) {
264 /* select the proper translation based on guest mode */
265 if (info->mem_mode == PHYSICAL_MEM) {
266 if (v3_gpa_to_hva(info, linear_addr, host_addr) == -1) return -1;
267 } else if (info->mem_mode == VIRTUAL_MEM) {
268 if (v3_gva_to_hva(info, linear_addr, host_addr) == -1) return -1;
273 static int v3_print_disassembly(struct guest_info * info) {
275 addr_t rip, rip_linear, rip_host;
277 /* we don't know where the instructions preceding RIP start, so we just take
278 * a guess and hope the instruction stream synced up with our disassembly
279 * some time before RIP; if it has not we correct RIP at that point
282 /* start disassembly 64 bytes before current RIP, continue 32 bytes after */
283 rip = (addr_t) info->rip - 64;
284 while ((int) (rip - info->rip) < 32) {
285 V3_Print("disassembly step\n");
287 /* always print RIP, even if the instructions before were bad */
288 if (!passed_rip && rip >= info->rip) {
289 if (rip != info->rip) {
290 V3_Print("***** bad disassembly up to this point *****\n");
296 /* look up host virtual address for this instruction */
297 rip_linear = get_addr_linear(info, rip, &(info->segments.cs));
298 if (safe_gva_to_hva(info, rip_linear, &rip_host) < 0) {
303 /* print disassembled instrcution (updates rip) */
304 if (v3_disasm(info, (void *) rip_host, &rip, rip == info->rip) < 0) {
316 void v3_print_guest_state(struct guest_info * info) {
317 addr_t linear_addr = 0;
319 V3_Print("RIP: %p\n", (void *)(addr_t)(info->rip));
320 linear_addr = get_addr_linear(info, info->rip, &(info->segments.cs));
321 V3_Print("RIP Linear: %p\n", (void *)linear_addr);
323 V3_Print("NumExits: %u\n", (uint32_t)info->num_exits);
325 v3_print_segments(&(info->segments));
326 v3_print_ctrl_regs(info);
328 if (info->shdw_pg_mode == SHADOW_PAGING) {
329 V3_Print("Shadow Paging Guest Registers:\n");
330 V3_Print("\tGuest CR0=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr0));
331 V3_Print("\tGuest CR3=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr3));
332 V3_Print("\tGuest EFER=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_efer.value));
337 v3_print_mem_map(info->vm_info);
339 v3_print_stack(info);
341 // v3_print_disassembly(info);
344 void v3_print_guest_state_all(struct v3_vm_info * vm) {
347 V3_Print("VM Core states for %s\n", vm->name);
349 for (i = 0; i < 80; i++) {
353 for (i = 0; i < vm->num_cores; i++) {
354 v3_print_guest_state(&vm->cores[i]);
357 for (i = 0; i < 80; i++) {
365 void v3_print_stack(struct guest_info * info) {
366 addr_t linear_addr = 0;
367 addr_t host_addr = 0;
369 v3_cpu_mode_t cpu_mode = v3_get_vm_cpu_mode(info);
372 linear_addr = get_addr_linear(info, info->vm_regs.rsp, &(info->segments.ss));
374 V3_Print("Stack at %p:\n", (void *)linear_addr);
376 if (info->mem_mode == PHYSICAL_MEM) {
377 if (v3_gpa_to_hva(info, linear_addr, &host_addr) == -1) {
378 PrintError("Could not translate Stack address\n");
381 } else if (info->mem_mode == VIRTUAL_MEM) {
382 if (v3_gva_to_hva(info, linear_addr, &host_addr) == -1) {
383 PrintError("Could not translate Virtual Stack address\n");
388 V3_Print("Host Address of rsp = 0x%p\n", (void *)host_addr);
390 // We start i at one because the current stack pointer points to an unused stack element
391 for (i = 0; i <= 24; i++) {
392 if (cpu_mode == LONG) {
393 V3_Print("\t%p\n", (void *)*(addr_t *)(host_addr + (i * 8)));
394 } else if (cpu_mode == REAL) {
395 V3_Print("Don't currently handle 16 bit stacks... \n");
398 V3_Print("\t%.8x\n", *(uint32_t *)(host_addr + (i * 4)));
406 void v3_print_GPRs(struct guest_info * info) {
407 struct v3_gprs * regs = &(info->vm_regs);
410 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", NULL};
412 reg_ptr = (v3_reg_t *)regs;
414 V3_Print("32 bit GPRs:\n");
416 for (i = 0; reg_names[i] != NULL; i++) {
417 V3_Print("\t%s=0x%p (at %p)\n", reg_names[i], (void *)(addr_t)reg_ptr[i], &(reg_ptr[i]));
423 void v3_print_GPRs(struct guest_info * info) {
424 struct v3_gprs * regs = &(info->vm_regs);
427 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", \
428 "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", NULL};
430 reg_ptr = (v3_reg_t *)regs;
432 V3_Print("64 bit GPRs:\n");
434 for (i = 0; reg_names[i] != NULL; i++) {
435 V3_Print("\t%s=0x%p (at %p)\n", reg_names[i], (void *)(addr_t)reg_ptr[i], &(reg_ptr[i]));
442 #include <palacios/vmcs.h>
443 #include <palacios/vmcb.h>
444 static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_data) {
445 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
448 V3_Print("************** Guest State ************\n");
449 v3_print_guest_state(core);
453 if ((cpu_type == V3_SVM_CPU) || (cpu_type == V3_SVM_REV3_CPU)) {
455 PrintDebugVMCB((vmcb_t *)(core->vmm_data));
459 if ((cpu_type == V3_VMX_CPU) || (cpu_type == V3_VMX_EPT_CPU)) {
465 PrintError("Invalid CPU Type 0x%x\n", cpu_type);
475 #include <palacios/svm.h>
476 #include <palacios/svm_io.h>
477 #include <palacios/svm_msr.h>
481 #include <palacios/vmx.h>
482 #include <palacios/vmx_io.h>
483 #include <palacios/vmx_msr.h>
487 int v3_init_vm(struct v3_vm_info * vm) {
488 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
490 if (v3_get_foreground_vm() == NULL) {
491 v3_set_foreground_vm(vm);
494 #ifdef CONFIG_TELEMETRY
495 v3_init_telemetry(vm);
498 v3_init_hypercall_map(vm);
501 v3_init_cpuid_map(vm);
502 v3_init_host_events(vm);
503 v3_init_intr_routers(vm);
504 v3_init_ext_manager(vm);
506 // Initialize the memory map
507 if (v3_init_mem_map(vm) == -1) {
508 PrintError("Could not initialize shadow map\n");
512 v3_init_mem_hooks(vm);
514 if (v3_init_shdw_impl(vm) == -1) {
515 PrintError("VM initialization error in shadow implementaion\n");
523 #ifdef CONFIG_SYMBIOTIC
524 v3_init_symbiotic_vm(vm);
534 case V3_SVM_REV3_CPU:
535 v3_init_svm_io_map(vm);
536 v3_init_svm_msr_map(vm);
542 v3_init_vmx_io_map(vm);
543 v3_init_vmx_msr_map(vm);
547 PrintError("Invalid CPU Type 0x%x\n", cpu_type);
551 v3_register_hypercall(vm, GUEST_INFO_HCALL, info_hcall, NULL);
553 V3_Print("GUEST_INFO_HCALL=%x\n", GUEST_INFO_HCALL);
559 int v3_free_vm_internal(struct v3_vm_info * vm) {
560 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
562 v3_remove_hypercall(vm, GUEST_INFO_HCALL);
566 #ifdef CONFIG_SYMBIOTIC
567 v3_deinit_symbiotic_vm(vm);
574 case V3_SVM_REV3_CPU:
575 v3_deinit_svm_io_map(vm);
576 v3_deinit_svm_msr_map(vm);
582 v3_deinit_vmx_io_map(vm);
583 v3_deinit_vmx_msr_map(vm);
587 PrintError("Invalid CPU Type 0x%x\n", cpu_type);
591 v3_deinit_dev_mgr(vm);
593 v3_deinit_time_vm(vm);
595 v3_deinit_mem_hooks(vm);
596 v3_delete_mem_map(vm);
597 v3_deinit_shdw_impl(vm);
599 v3_deinit_intr_routers(vm);
600 v3_deinit_host_events(vm);
602 v3_deinit_cpuid_map(vm);
603 v3_deinit_msr_map(vm);
604 v3_deinit_io_map(vm);
605 v3_deinit_hypercall_map(vm);
607 #ifdef CONFIG_TELEMETRY
608 v3_deinit_telemetry(vm);
617 int v3_init_core(struct guest_info * core) {
618 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
619 struct v3_vm_info * vm = core->vm_info;
622 * Initialize the subsystem data strutures
624 #ifdef CONFIG_TELEMETRY
625 v3_init_core_telemetry(core);
628 if (core->shdw_pg_mode == SHADOW_PAGING) {
629 v3_init_shdw_pg_state(core);
632 v3_init_time_core(core);
633 v3_init_intr_controllers(core);
634 v3_init_exception_state(core);
636 v3_init_decoder(core);
639 #ifdef CONFIG_SYMBIOTIC
640 v3_init_symbiotic_core(core);
649 case V3_SVM_REV3_CPU:
650 if (v3_init_svm_vmcb(core, vm->vm_class) == -1) {
651 PrintError("Error in SVM initialization\n");
659 if (v3_init_vmx_vmcs(core, vm->vm_class) == -1) {
660 PrintError("Error in VMX initialization\n");
666 PrintError("Invalid CPU Type 0x%x\n", cpu_type);
675 int v3_free_core(struct guest_info * core) {
676 v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
679 #ifdef CONFIG_SYMBIOTIC
680 v3_deinit_symbiotic_core(core);
683 v3_deinit_decoder(core);
685 v3_deinit_intr_controllers(core);
686 v3_deinit_time_core(core);
688 if (core->shdw_pg_mode == SHADOW_PAGING) {
689 v3_deinit_shdw_pg_state(core);
692 v3_free_passthrough_pts(core);
694 #ifdef CONFIG_TELEMETRY
695 v3_deinit_core_telemetry(core);
701 case V3_SVM_REV3_CPU:
702 if (v3_deinit_svm_vmcb(core) == -1) {
703 PrintError("Error in SVM initialization\n");
711 if (v3_deinit_vmx_vmcs(core) == -1) {
712 PrintError("Error in VMX initialization\n");
718 PrintError("Invalid CPU Type 0x%x\n", cpu_type);