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_xed.h>
32 #include <palacios/vmm_direct_paging.h>
33 #include <palacios/vmm_barrier.h>
34 #include <palacios/vmm_debug.h>
37 v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) {
39 struct efer_64 * efer;
40 struct v3_segment * cs = &(info->segments.cs);
43 if (info->shdw_pg_mode == SHADOW_PAGING) {
44 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
45 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
46 cr4 = (struct cr4_32 *)&(info->shdw_pg_state.guest_cr4);
47 } else if (info->shdw_pg_mode == NESTED_PAGING) {
48 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
49 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
50 cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
52 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
53 V3_ASSERT(info->vm_info, info, 0);
59 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
61 } else if (efer->lme == 0) {
63 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
66 // What about LONG_16_COMPAT???
67 return LONG_32_COMPAT;
71 // Get address width in bytes
72 uint_t v3_get_addr_width(struct guest_info * info) {
74 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
75 struct efer_64 * efer;
76 struct v3_segment * cs = &(info->segments.cs);
79 if (info->shdw_pg_mode == SHADOW_PAGING) {
80 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
81 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
82 } else if (info->shdw_pg_mode == NESTED_PAGING) {
83 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
84 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
86 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
87 V3_ASSERT(info->vm_info, info, 0);
93 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
95 } else if (efer->lme == 0) {
97 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
100 // What about LONG_16_COMPAT???
106 static const uchar_t REAL_STR[] = "Real";
107 static const uchar_t PROTECTED_STR[] = "Protected";
108 static const uchar_t PROTECTED_PAE_STR[] = "Protected+PAE";
109 static const uchar_t LONG_STR[] = "Long";
110 static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
111 static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
113 const uchar_t * v3_cpu_mode_to_str(v3_cpu_mode_t mode) {
118 return PROTECTED_STR;
120 return PROTECTED_PAE_STR;
124 return LONG_32_COMPAT_STR;
126 return LONG_16_COMPAT_STR;
132 v3_mem_mode_t v3_get_vm_mem_mode(struct guest_info * info) {
135 if (info->shdw_pg_mode == SHADOW_PAGING) {
136 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
137 } else if (info->shdw_pg_mode == NESTED_PAGING) {
138 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
140 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
141 V3_ASSERT(info->vm_info, info, 0);
152 static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
153 static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
155 const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) {
172 #include <palacios/vmcs.h>
173 #include <palacios/vmcb.h>
174 static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_data) {
175 extern v3_cpu_arch_t v3_mach_type;
178 V3_Print(core->vm_info, core, "************** Guest State ************\n");
179 v3_print_guest_state(core);
183 if ((v3_mach_type == V3_SVM_CPU) || (v3_mach_type == V3_SVM_REV3_CPU)) {
185 PrintDebugVMCB((vmcb_t *)(core->vmm_data));
189 if ((v3_mach_type == V3_VMX_CPU) || (v3_mach_type == V3_VMX_EPT_CPU) || (v3_mach_type == V3_VMX_EPT_UG_CPU)) {
195 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
205 #include <palacios/svm.h>
206 #include <palacios/svm_io.h>
207 #include <palacios/svm_msr.h>
208 #include <palacios/svm_exits.h>
212 #include <palacios/vmx.h>
213 #include <palacios/vmx_io.h>
214 #include <palacios/vmx_msr.h>
215 #include <palacios/vmx_exits.h>
219 int v3_init_vm(struct v3_vm_info * vm) {
220 extern v3_cpu_arch_t v3_mach_type;
224 #ifdef V3_CONFIG_TELEMETRY
225 v3_init_telemetry(vm);
228 v3_init_exit_hooks(vm);
229 v3_init_hypercall_map(vm);
232 v3_init_cpuid_map(vm);
233 v3_init_host_events(vm);
235 v3_init_intr_routers(vm);
236 v3_init_ext_manager(vm);
240 // Initialize the memory map
241 if (v3_init_mem_map(vm) == -1) {
242 PrintError(vm, VCORE_NONE, "Could not initialize shadow map\n");
246 v3_init_mem_hooks(vm);
248 if (v3_init_shdw_impl(vm) == -1) {
249 PrintError(vm, VCORE_NONE, "VM initialization error in shadow implementaion\n");
253 if (v3_init_passthrough_paging(vm) == -1) {
254 PrintError(vm, VCORE_NONE, "VM initialization error in passthrough paging\n");
258 if (v3_init_nested_paging(vm) == -1) {
259 PrintError(vm, VCORE_NONE, "VM initialization error in nested paging\n");
265 v3_init_vm_debugging(vm);
268 #ifdef V3_CONFIG_SYMBIOTIC
269 v3_init_symbiotic_vm(vm);
276 switch (v3_mach_type) {
279 case V3_SVM_REV3_CPU:
280 v3_init_svm_io_map(vm);
281 v3_init_svm_msr_map(vm);
282 v3_init_svm_exits(vm);
288 case V3_VMX_EPT_UG_CPU:
289 v3_init_vmx_io_map(vm);
290 v3_init_vmx_msr_map(vm);
291 v3_init_vmx_exits(vm);
295 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
299 v3_register_hypercall(vm, GUEST_INFO_HCALL, info_hcall, NULL);
301 V3_Print(vm, VCORE_NONE, "GUEST_INFO_HCALL=%x\n", GUEST_INFO_HCALL);
307 int v3_free_vm_internal(struct v3_vm_info * vm) {
308 extern v3_cpu_arch_t v3_mach_type;
310 v3_remove_hypercall(vm, GUEST_INFO_HCALL);
314 #ifdef V3_CONFIG_SYMBIOTIC
315 v3_deinit_symbiotic_vm(vm);
319 switch (v3_mach_type) {
322 case V3_SVM_REV3_CPU:
323 v3_deinit_svm_io_map(vm);
324 v3_deinit_svm_msr_map(vm);
330 case V3_VMX_EPT_UG_CPU:
331 v3_deinit_vmx_io_map(vm);
332 v3_deinit_vmx_msr_map(vm);
336 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
340 v3_deinit_dev_mgr(vm);
342 v3_deinit_time_vm(vm);
344 v3_deinit_mem_hooks(vm);
345 v3_delete_mem_map(vm);
346 v3_deinit_shdw_impl(vm);
347 v3_deinit_passthrough_paging(vm);
348 v3_deinit_nested_paging(vm);
350 v3_deinit_ext_manager(vm);
351 v3_deinit_intr_routers(vm);
352 v3_deinit_host_events(vm);
354 v3_deinit_barrier(vm);
356 v3_deinit_cpuid_map(vm);
357 v3_deinit_msr_map(vm);
358 v3_deinit_io_map(vm);
359 v3_deinit_hypercall_map(vm);
361 v3_deinit_exit_hooks(vm);
363 #ifdef V3_CONFIG_TELEMETRY
364 v3_deinit_telemetry(vm);
367 v3_deinit_events(vm);
369 v3_fw_cfg_deinit(vm);
376 int v3_init_core(struct guest_info * core) {
377 extern v3_cpu_arch_t v3_mach_type;
378 struct v3_vm_info * vm = core->vm_info;
383 * Initialize the subsystem data strutures
385 #ifdef V3_CONFIG_TELEMETRY
386 v3_init_core_telemetry(core);
389 if (core->shdw_pg_mode == SHADOW_PAGING) {
390 v3_init_passthrough_paging_core(core);
391 v3_init_shdw_pg_state(core);
393 //done later due to SVM/VMX differences
394 //v3_init_nested_paging_core(core);
397 v3_init_time_core(core);
398 v3_init_intr_controllers(core);
399 v3_init_exception_state(core);
401 v3_init_decoder(core);
404 #ifdef V3_CONFIG_SYMBIOTIC
405 v3_init_symbiotic_core(core);
411 switch (v3_mach_type) {
414 case V3_SVM_REV3_CPU:
415 if (v3_init_svm_vmcb(core, vm->vm_class) == -1) {
416 PrintError(vm, core, "Error in SVM initialization\n");
424 case V3_VMX_EPT_UG_CPU:
425 if (v3_init_vmx_vmcs(core, vm->vm_class) == -1) {
426 PrintError(vm, core, "Error in VMX initialization\n");
432 PrintError(vm, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
436 v3_init_exit_hooks_core(core);
444 int v3_free_core(struct guest_info * core) {
445 extern v3_cpu_arch_t v3_mach_type;
448 #ifdef V3_CONFIG_SYMBIOTIC
449 v3_deinit_symbiotic_core(core);
452 v3_deinit_decoder(core);
454 v3_deinit_intr_controllers(core);
455 v3_deinit_time_core(core);
457 if (core->shdw_pg_mode == SHADOW_PAGING) {
458 v3_deinit_shdw_pg_state(core);
459 v3_deinit_passthrough_paging_core(core);
461 v3_deinit_nested_paging_core(core);
464 v3_free_passthrough_pts(core);
466 #ifdef V3_CONFIG_TELEMETRY
467 v3_deinit_core_telemetry(core);
470 switch (v3_mach_type) {
473 case V3_SVM_REV3_CPU:
474 if (v3_deinit_svm_vmcb(core) == -1) {
475 PrintError(VM_NONE,VCORE_NONE, "Error in SVM deinitialization\n");
483 case V3_VMX_EPT_UG_CPU:
484 if (v3_deinit_vmx_vmcs(core) == -1) {
485 PrintError(VM_NONE, VCORE_NONE, "Error in VMX initialization\n");
491 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);