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>
36 #ifdef V3_CONFIG_MEM_TRACK
37 #include <palacios/vmm_mem_track.h>
40 v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) {
42 struct efer_64 * efer;
43 struct v3_segment * cs = &(info->segments.cs);
46 if (info->shdw_pg_mode == SHADOW_PAGING) {
47 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
48 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
49 cr4 = (struct cr4_32 *)&(info->shdw_pg_state.guest_cr4);
50 } else if (info->shdw_pg_mode == NESTED_PAGING) {
51 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
52 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
53 cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
55 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
56 V3_ASSERT(info->vm_info, info, 0);
62 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
64 } else if (efer->lme == 0) {
66 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
69 // What about LONG_16_COMPAT???
70 return LONG_32_COMPAT;
74 // Get address width in bytes
75 uint_t v3_get_addr_width(struct guest_info * info) {
77 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
78 struct efer_64 * efer;
79 struct v3_segment * cs = &(info->segments.cs);
82 if (info->shdw_pg_mode == SHADOW_PAGING) {
83 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
84 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
85 } else if (info->shdw_pg_mode == NESTED_PAGING) {
86 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
87 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
89 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
90 V3_ASSERT(info->vm_info, info, 0);
96 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
98 } else if (efer->lme == 0) {
100 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
103 // What about LONG_16_COMPAT???
109 static const uchar_t REAL_STR[] = "Real";
110 static const uchar_t PROTECTED_STR[] = "Protected";
111 static const uchar_t PROTECTED_PAE_STR[] = "Protected+PAE";
112 static const uchar_t LONG_STR[] = "Long";
113 static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
114 static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
116 const uchar_t * v3_cpu_mode_to_str(v3_cpu_mode_t mode) {
121 return PROTECTED_STR;
123 return PROTECTED_PAE_STR;
127 return LONG_32_COMPAT_STR;
129 return LONG_16_COMPAT_STR;
135 v3_mem_mode_t v3_get_vm_mem_mode(struct guest_info * info) {
138 if (info->shdw_pg_mode == SHADOW_PAGING) {
139 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
140 } else if (info->shdw_pg_mode == NESTED_PAGING) {
141 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
143 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
144 V3_ASSERT(info->vm_info, info, 0);
155 static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
156 static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
158 const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) {
175 #include <palacios/vmcs.h>
176 #include <palacios/vmcb.h>
177 static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_data) {
178 extern v3_cpu_arch_t v3_mach_type;
181 V3_Print(core->vm_info, core, "************** Guest State ************\n");
182 v3_print_guest_state(core);
186 if ((v3_mach_type == V3_SVM_CPU) || (v3_mach_type == V3_SVM_REV3_CPU)) {
188 PrintDebugVMCB((vmcb_t *)(core->vmm_data));
192 if ((v3_mach_type == V3_VMX_CPU) || (v3_mach_type == V3_VMX_EPT_CPU) || (v3_mach_type == V3_VMX_EPT_UG_CPU)) {
198 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
208 #include <palacios/svm.h>
209 #include <palacios/svm_io.h>
210 #include <palacios/svm_msr.h>
211 #include <palacios/svm_exits.h>
215 #include <palacios/vmx.h>
216 #include <palacios/vmx_io.h>
217 #include <palacios/vmx_msr.h>
218 #include <palacios/vmx_exits.h>
222 int v3_init_vm(struct v3_vm_info * vm) {
223 extern v3_cpu_arch_t v3_mach_type;
227 #ifdef V3_CONFIG_TELEMETRY
228 v3_init_telemetry(vm);
231 v3_init_exit_hooks(vm);
232 v3_init_hypercall_map(vm);
235 v3_init_cpuid_map(vm);
236 v3_init_host_events(vm);
238 v3_init_intr_routers(vm);
239 v3_init_ext_manager(vm);
243 // Initialize the memory map
244 if (v3_init_mem_map(vm) == -1) {
245 PrintError(vm, VCORE_NONE, "Could not initialize shadow map\n");
249 v3_init_mem_hooks(vm);
251 if (v3_init_shdw_impl(vm) == -1) {
252 PrintError(vm, VCORE_NONE, "VM initialization error in shadow implementaion\n");
256 if (v3_init_passthrough_paging(vm) == -1) {
257 PrintError(vm, VCORE_NONE, "VM initialization error in passthrough paging\n");
261 if (v3_init_nested_paging(vm) == -1) {
262 PrintError(vm, VCORE_NONE, "VM initialization error in nested paging\n");
266 #ifdef V3_CONFIG_MEM_TRACK
267 v3_mem_track_init(vm);
272 v3_init_vm_debugging(vm);
275 #ifdef V3_CONFIG_SYMBIOTIC
276 v3_init_symbiotic_vm(vm);
283 switch (v3_mach_type) {
286 case V3_SVM_REV3_CPU:
287 v3_init_svm_io_map(vm);
288 v3_init_svm_msr_map(vm);
289 v3_init_svm_exits(vm);
295 case V3_VMX_EPT_UG_CPU:
296 v3_init_vmx_io_map(vm);
297 v3_init_vmx_msr_map(vm);
298 v3_init_vmx_exits(vm);
302 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
306 v3_register_hypercall(vm, GUEST_INFO_HCALL, info_hcall, NULL);
308 V3_Print(vm, VCORE_NONE, "GUEST_INFO_HCALL=%x\n", GUEST_INFO_HCALL);
314 int v3_free_vm_internal(struct v3_vm_info * vm) {
315 extern v3_cpu_arch_t v3_mach_type;
317 v3_remove_hypercall(vm, GUEST_INFO_HCALL);
321 #ifdef V3_CONFIG_SYMBIOTIC
322 v3_deinit_symbiotic_vm(vm);
326 switch (v3_mach_type) {
329 case V3_SVM_REV3_CPU:
330 v3_deinit_svm_io_map(vm);
331 v3_deinit_svm_msr_map(vm);
337 case V3_VMX_EPT_UG_CPU:
338 v3_deinit_vmx_io_map(vm);
339 v3_deinit_vmx_msr_map(vm);
343 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
347 v3_deinit_dev_mgr(vm);
349 v3_deinit_time_vm(vm);
351 v3_deinit_mem_hooks(vm);
353 #ifdef V3_CONFIG_SWAPPING
354 v3_deinit_swapping_vm(vm);
357 v3_delete_mem_map(vm);
358 v3_deinit_shdw_impl(vm);
359 v3_deinit_passthrough_paging(vm);
360 v3_deinit_nested_paging(vm);
362 v3_deinit_ext_manager(vm);
363 v3_deinit_intr_routers(vm);
364 v3_deinit_host_events(vm);
366 v3_deinit_barrier(vm);
368 v3_deinit_cpuid_map(vm);
369 v3_deinit_msr_map(vm);
370 v3_deinit_io_map(vm);
371 v3_deinit_hypercall_map(vm);
373 v3_deinit_exit_hooks(vm);
375 #ifdef V3_CONFIG_TELEMETRY
376 v3_deinit_telemetry(vm);
379 v3_deinit_events(vm);
381 #ifdef V3_CONFIG_MEM_TRACK
382 v3_mem_track_deinit(vm);
385 v3_fw_cfg_deinit(vm);
392 int v3_init_core(struct guest_info * core) {
393 extern v3_cpu_arch_t v3_mach_type;
394 struct v3_vm_info * vm = core->vm_info;
399 * Initialize the subsystem data strutures
401 #ifdef V3_CONFIG_TELEMETRY
402 v3_init_core_telemetry(core);
405 if (core->shdw_pg_mode == SHADOW_PAGING) {
406 v3_init_passthrough_paging_core(core);
407 v3_init_shdw_pg_state(core);
409 //done later due to SVM/VMX differences
410 //v3_init_nested_paging_core(core);
413 v3_init_time_core(core);
414 v3_init_intr_controllers(core);
415 v3_init_exception_state(core);
417 v3_init_decoder(core);
420 #ifdef V3_CONFIG_SYMBIOTIC
421 v3_init_symbiotic_core(core);
427 switch (v3_mach_type) {
430 case V3_SVM_REV3_CPU:
431 if (v3_init_svm_vmcb(core, vm->vm_class) == -1) {
432 PrintError(vm, core, "Error in SVM initialization\n");
440 case V3_VMX_EPT_UG_CPU:
441 if (v3_init_vmx_vmcs(core, vm->vm_class) == -1) {
442 PrintError(vm, core, "Error in VMX initialization\n");
448 PrintError(vm, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
452 v3_init_exit_hooks_core(core);
460 int v3_free_core(struct guest_info * core) {
461 extern v3_cpu_arch_t v3_mach_type;
464 #ifdef V3_CONFIG_SYMBIOTIC
465 v3_deinit_symbiotic_core(core);
468 v3_deinit_decoder(core);
470 v3_deinit_intr_controllers(core);
471 v3_deinit_time_core(core);
473 if (core->shdw_pg_mode == SHADOW_PAGING) {
474 v3_deinit_shdw_pg_state(core);
475 v3_deinit_passthrough_paging_core(core);
477 v3_deinit_nested_paging_core(core);
480 v3_free_passthrough_pts(core);
482 #ifdef V3_CONFIG_TELEMETRY
483 v3_deinit_core_telemetry(core);
486 switch (v3_mach_type) {
489 case V3_SVM_REV3_CPU:
490 if (v3_deinit_svm_vmcb(core) == -1) {
491 PrintError(VM_NONE,VCORE_NONE, "Error in SVM deinitialization\n");
499 case V3_VMX_EPT_UG_CPU:
500 if (v3_deinit_vmx_vmcs(core) == -1) {
501 PrintError(VM_NONE, VCORE_NONE, "Error in VMX initialization\n");
507 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);