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>
39 #ifdef V3_CONFIG_CACHEPART
40 #include <palacios/vmm_cachepart.h>
44 v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) {
46 struct efer_64 * efer;
47 struct v3_segment * cs = &(info->segments.cs);
50 if (info->shdw_pg_mode == SHADOW_PAGING) {
51 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
52 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
53 cr4 = (struct cr4_32 *)&(info->shdw_pg_state.guest_cr4);
54 } else if (info->shdw_pg_mode == NESTED_PAGING) {
55 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
56 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
57 cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
59 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
60 V3_ASSERT(info->vm_info, info, 0);
66 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
68 } else if (efer->lme == 0) {
70 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
73 // What about LONG_16_COMPAT???
74 return LONG_32_COMPAT;
78 // Get address width in bytes
79 uint_t v3_get_addr_width(struct guest_info * info) {
81 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
82 struct efer_64 * efer;
83 struct v3_segment * cs = &(info->segments.cs);
86 if (info->shdw_pg_mode == SHADOW_PAGING) {
87 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
88 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
89 } else if (info->shdw_pg_mode == NESTED_PAGING) {
90 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
91 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
93 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
94 V3_ASSERT(info->vm_info, info, 0);
100 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
102 } else if (efer->lme == 0) {
104 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
107 // What about LONG_16_COMPAT???
113 static const uchar_t REAL_STR[] = "Real";
114 static const uchar_t PROTECTED_STR[] = "Protected";
115 static const uchar_t PROTECTED_PAE_STR[] = "Protected+PAE";
116 static const uchar_t LONG_STR[] = "Long";
117 static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
118 static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
120 const uchar_t * v3_cpu_mode_to_str(v3_cpu_mode_t mode) {
125 return PROTECTED_STR;
127 return PROTECTED_PAE_STR;
131 return LONG_32_COMPAT_STR;
133 return LONG_16_COMPAT_STR;
139 v3_mem_mode_t v3_get_vm_mem_mode(struct guest_info * info) {
142 if (info->shdw_pg_mode == SHADOW_PAGING) {
143 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
144 } else if (info->shdw_pg_mode == NESTED_PAGING) {
145 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
147 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
148 V3_ASSERT(info->vm_info, info, 0);
159 static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
160 static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
162 const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) {
179 #include <palacios/vmcs.h>
180 #include <palacios/vmcb.h>
181 static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_data) {
182 extern v3_cpu_arch_t v3_mach_type;
185 V3_Print(core->vm_info, core, "************** Guest State ************\n");
186 v3_print_guest_state(core);
190 if ((v3_mach_type == V3_SVM_CPU) || (v3_mach_type == V3_SVM_REV3_CPU)) {
192 PrintDebugVMCB((vmcb_t *)(core->vmm_data));
196 if ((v3_mach_type == V3_VMX_CPU) || (v3_mach_type == V3_VMX_EPT_CPU) || (v3_mach_type == V3_VMX_EPT_UG_CPU)) {
202 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
212 #include <palacios/svm.h>
213 #include <palacios/svm_io.h>
214 #include <palacios/svm_msr.h>
215 #include <palacios/svm_exits.h>
219 #include <palacios/vmx.h>
220 #include <palacios/vmx_io.h>
221 #include <palacios/vmx_msr.h>
222 #include <palacios/vmx_exits.h>
226 int v3_init_vm(struct v3_vm_info * vm) {
227 extern v3_cpu_arch_t v3_mach_type;
231 #ifdef V3_CONFIG_TELEMETRY
232 v3_init_telemetry(vm);
235 v3_init_exit_hooks(vm);
236 v3_init_hypercall_map(vm);
239 v3_init_cpuid_map(vm);
240 v3_init_host_events(vm);
242 v3_init_intr_routers(vm);
243 v3_init_ext_manager(vm);
247 // Initialize the memory map
248 if (v3_init_mem_map(vm) == -1) {
249 PrintError(vm, VCORE_NONE, "Could not initialize shadow map\n");
253 v3_init_mem_hooks(vm);
255 if (v3_init_shdw_impl(vm) == -1) {
256 PrintError(vm, VCORE_NONE, "VM initialization error in shadow implementaion\n");
260 if (v3_init_passthrough_paging(vm) == -1) {
261 PrintError(vm, VCORE_NONE, "VM initialization error in passthrough paging\n");
265 if (v3_init_nested_paging(vm) == -1) {
266 PrintError(vm, VCORE_NONE, "VM initialization error in nested paging\n");
270 #ifdef V3_CONFIG_MEM_TRACK
271 v3_mem_track_init(vm);
276 v3_init_vm_debugging(vm);
279 #ifdef V3_CONFIG_SYMBIOTIC
280 v3_init_symbiotic_vm(vm);
287 switch (v3_mach_type) {
290 case V3_SVM_REV3_CPU:
291 v3_init_svm_io_map(vm);
292 v3_init_svm_msr_map(vm);
293 v3_init_svm_exits(vm);
299 case V3_VMX_EPT_UG_CPU:
300 v3_init_vmx_io_map(vm);
301 v3_init_vmx_msr_map(vm);
302 v3_init_vmx_exits(vm);
306 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
310 v3_register_hypercall(vm, GUEST_INFO_HCALL, info_hcall, NULL);
312 V3_Print(vm, VCORE_NONE, "GUEST_INFO_HCALL=%x\n", GUEST_INFO_HCALL);
318 int v3_free_vm_internal(struct v3_vm_info * vm) {
319 extern v3_cpu_arch_t v3_mach_type;
321 v3_remove_hypercall(vm, GUEST_INFO_HCALL);
325 v3_deinit_hvm_vm(vm);
328 #ifdef V3_CONFIG_MULTIBOOT
329 v3_deinit_multiboot_vm(vm);
333 #ifdef V3_CONFIG_SYMBIOTIC
334 v3_deinit_symbiotic_vm(vm);
338 switch (v3_mach_type) {
341 case V3_SVM_REV3_CPU:
342 v3_deinit_svm_io_map(vm);
343 v3_deinit_svm_msr_map(vm);
349 case V3_VMX_EPT_UG_CPU:
350 v3_deinit_vmx_io_map(vm);
351 v3_deinit_vmx_msr_map(vm);
355 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
359 v3_deinit_dev_mgr(vm);
361 v3_deinit_time_vm(vm);
363 v3_deinit_mem_hooks(vm);
365 #ifdef V3_CONFIG_SWAPPING
366 v3_deinit_swapping_vm(vm);
369 v3_delete_mem_map(vm);
370 v3_deinit_shdw_impl(vm);
371 v3_deinit_passthrough_paging(vm);
372 v3_deinit_nested_paging(vm);
374 v3_deinit_ext_manager(vm);
375 v3_deinit_intr_routers(vm);
376 v3_deinit_host_events(vm);
378 v3_deinit_barrier(vm);
380 v3_deinit_cpuid_map(vm);
381 v3_deinit_msr_map(vm);
382 v3_deinit_io_map(vm);
383 v3_deinit_hypercall_map(vm);
385 v3_deinit_exit_hooks(vm);
387 #ifdef V3_CONFIG_TELEMETRY
388 v3_deinit_telemetry(vm);
392 v3_deinit_events(vm);
394 #ifdef V3_CONFIG_MEM_TRACK
395 v3_mem_track_deinit(vm);
398 v3_fw_cfg_deinit(vm);
400 #ifdef V3_CONFIG_CACHEPART
401 v3_deinit_cachepart_vm(vm);
408 int v3_init_core(struct guest_info * core) {
409 extern v3_cpu_arch_t v3_mach_type;
410 struct v3_vm_info * vm = core->vm_info;
415 * Initialize the subsystem data strutures
419 #ifdef V3_CONFIG_CACHEPART
420 v3_init_cachepart_core(core);
423 #ifdef V3_CONFIG_TELEMETRY
424 v3_init_core_telemetry(core);
427 if (core->shdw_pg_mode == SHADOW_PAGING) {
428 v3_init_passthrough_paging_core(core);
429 v3_init_shdw_pg_state(core);
431 //done later due to SVM/VMX differences
432 //v3_init_nested_paging_core(core);
435 v3_init_time_core(core);
436 v3_init_intr_controllers(core);
437 v3_init_exception_state(core);
439 v3_init_decoder(core);
442 #ifdef V3_CONFIG_SYMBIOTIC
443 v3_init_symbiotic_core(core);
449 switch (v3_mach_type) {
452 case V3_SVM_REV3_CPU:
453 if (v3_init_svm_vmcb(core, vm->vm_class) == -1) {
454 PrintError(vm, core, "Error in SVM initialization\n");
462 case V3_VMX_EPT_UG_CPU:
463 if (v3_init_vmx_vmcs(core, vm->vm_class) == -1) {
464 PrintError(vm, core, "Error in VMX initialization\n");
470 PrintError(vm, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
474 v3_init_exit_hooks_core(core);
482 int v3_free_core(struct guest_info * core) {
483 extern v3_cpu_arch_t v3_mach_type;
486 #ifdef V3_CONFIG_SYMBIOTIC
487 v3_deinit_symbiotic_core(core);
491 v3_deinit_hvm_core(core);
494 #ifdef V3_CONFIG_MULTIBOOT
495 v3_deinit_multiboot_core(core);
498 v3_deinit_decoder(core);
500 v3_deinit_intr_controllers(core);
501 v3_deinit_time_core(core);
503 if (core->shdw_pg_mode == SHADOW_PAGING) {
504 v3_deinit_shdw_pg_state(core);
505 v3_deinit_passthrough_paging_core(core);
507 v3_deinit_nested_paging_core(core);
510 v3_free_passthrough_pts(core);
512 #ifdef V3_CONFIG_TELEMETRY
513 v3_deinit_core_telemetry(core);
518 switch (v3_mach_type) {
521 case V3_SVM_REV3_CPU:
522 if (v3_deinit_svm_vmcb(core) == -1) {
523 PrintError(VM_NONE,VCORE_NONE, "Error in SVM deinitialization\n");
531 case V3_VMX_EPT_UG_CPU:
532 if (v3_deinit_vmx_vmcs(core) == -1) {
533 PrintError(VM_NONE, VCORE_NONE, "Error in VMX initialization\n");
539 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
543 #ifdef V3_CONFIG_CACHEPART
544 v3_deinit_cachepart_core(core);