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>
41 v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) {
43 struct efer_64 * efer;
44 struct v3_segment * cs = &(info->segments.cs);
47 if (info->shdw_pg_mode == SHADOW_PAGING) {
48 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
49 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
50 cr4 = (struct cr4_32 *)&(info->shdw_pg_state.guest_cr4);
51 } else if (info->shdw_pg_mode == NESTED_PAGING) {
52 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
53 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
54 cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
56 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
57 V3_ASSERT(info->vm_info, info, 0);
63 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
65 } else if (efer->lme == 0) {
67 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
70 // What about LONG_16_COMPAT???
71 return LONG_32_COMPAT;
75 // Get address width in bytes
76 uint_t v3_get_addr_width(struct guest_info * info) {
78 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
79 struct efer_64 * efer;
80 struct v3_segment * cs = &(info->segments.cs);
83 if (info->shdw_pg_mode == SHADOW_PAGING) {
84 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
85 efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
86 } else if (info->shdw_pg_mode == NESTED_PAGING) {
87 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
88 efer = (struct efer_64 *)&(info->ctrl_regs.efer);
90 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
91 V3_ASSERT(info->vm_info, info, 0);
97 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
99 } else if (efer->lme == 0) {
101 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
104 // What about LONG_16_COMPAT???
110 static const uchar_t REAL_STR[] = "Real";
111 static const uchar_t PROTECTED_STR[] = "Protected";
112 static const uchar_t PROTECTED_PAE_STR[] = "Protected+PAE";
113 static const uchar_t LONG_STR[] = "Long";
114 static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
115 static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
117 const uchar_t * v3_cpu_mode_to_str(v3_cpu_mode_t mode) {
122 return PROTECTED_STR;
124 return PROTECTED_PAE_STR;
128 return LONG_32_COMPAT_STR;
130 return LONG_16_COMPAT_STR;
136 v3_mem_mode_t v3_get_vm_mem_mode(struct guest_info * info) {
139 if (info->shdw_pg_mode == SHADOW_PAGING) {
140 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
141 } else if (info->shdw_pg_mode == NESTED_PAGING) {
142 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
144 PrintError(info->vm_info, info, "Invalid Paging Mode...\n");
145 V3_ASSERT(info->vm_info, info, 0);
156 static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
157 static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
159 const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) {
176 #include <palacios/vmcs.h>
177 #include <palacios/vmcb.h>
178 static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_data) {
179 extern v3_cpu_arch_t v3_mach_type;
182 V3_Print(core->vm_info, core, "************** Guest State ************\n");
183 v3_print_guest_state(core);
187 if ((v3_mach_type == V3_SVM_CPU) || (v3_mach_type == V3_SVM_REV3_CPU)) {
189 PrintDebugVMCB((vmcb_t *)(core->vmm_data));
193 if ((v3_mach_type == V3_VMX_CPU) || (v3_mach_type == V3_VMX_EPT_CPU) || (v3_mach_type == V3_VMX_EPT_UG_CPU)) {
199 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
209 #include <palacios/svm.h>
210 #include <palacios/svm_io.h>
211 #include <palacios/svm_msr.h>
212 #include <palacios/svm_exits.h>
216 #include <palacios/vmx.h>
217 #include <palacios/vmx_io.h>
218 #include <palacios/vmx_msr.h>
219 #include <palacios/vmx_exits.h>
223 int v3_init_vm(struct v3_vm_info * vm) {
224 extern v3_cpu_arch_t v3_mach_type;
228 #ifdef V3_CONFIG_TELEMETRY
229 v3_init_telemetry(vm);
232 v3_init_exit_hooks(vm);
233 v3_init_hypercall_map(vm);
236 v3_init_cpuid_map(vm);
237 v3_init_host_events(vm);
239 v3_init_intr_routers(vm);
240 v3_init_ext_manager(vm);
244 // Initialize the memory map
245 if (v3_init_mem_map(vm) == -1) {
246 PrintError(vm, VCORE_NONE, "Could not initialize shadow map\n");
250 v3_init_mem_hooks(vm);
252 if (v3_init_shdw_impl(vm) == -1) {
253 PrintError(vm, VCORE_NONE, "VM initialization error in shadow implementaion\n");
257 if (v3_init_passthrough_paging(vm) == -1) {
258 PrintError(vm, VCORE_NONE, "VM initialization error in passthrough paging\n");
262 if (v3_init_nested_paging(vm) == -1) {
263 PrintError(vm, VCORE_NONE, "VM initialization error in nested paging\n");
267 #ifdef V3_CONFIG_MEM_TRACK
268 v3_mem_track_init(vm);
273 v3_init_vm_debugging(vm);
276 #ifdef V3_CONFIG_SYMBIOTIC
277 v3_init_symbiotic_vm(vm);
284 switch (v3_mach_type) {
287 case V3_SVM_REV3_CPU:
288 v3_init_svm_io_map(vm);
289 v3_init_svm_msr_map(vm);
290 v3_init_svm_exits(vm);
296 case V3_VMX_EPT_UG_CPU:
297 v3_init_vmx_io_map(vm);
298 v3_init_vmx_msr_map(vm);
299 v3_init_vmx_exits(vm);
303 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
307 v3_register_hypercall(vm, GUEST_INFO_HCALL, info_hcall, NULL);
309 V3_Print(vm, VCORE_NONE, "GUEST_INFO_HCALL=%x\n", GUEST_INFO_HCALL);
315 int v3_free_vm_internal(struct v3_vm_info * vm) {
316 extern v3_cpu_arch_t v3_mach_type;
318 v3_remove_hypercall(vm, GUEST_INFO_HCALL);
322 v3_deinit_hvm_vm(vm);
325 #ifdef V3_CONFIG_MULTIBOOT
326 v3_deinit_multiboot_vm(vm);
330 #ifdef V3_CONFIG_SYMBIOTIC
331 v3_deinit_symbiotic_vm(vm);
335 switch (v3_mach_type) {
338 case V3_SVM_REV3_CPU:
339 v3_deinit_svm_io_map(vm);
340 v3_deinit_svm_msr_map(vm);
346 case V3_VMX_EPT_UG_CPU:
347 v3_deinit_vmx_io_map(vm);
348 v3_deinit_vmx_msr_map(vm);
352 PrintError(vm, VCORE_NONE, "Invalid CPU Type 0x%x\n", v3_mach_type);
356 v3_deinit_dev_mgr(vm);
358 v3_deinit_time_vm(vm);
360 v3_deinit_mem_hooks(vm);
362 #ifdef V3_CONFIG_SWAPPING
363 v3_deinit_swapping_vm(vm);
366 v3_delete_mem_map(vm);
367 v3_deinit_shdw_impl(vm);
368 v3_deinit_passthrough_paging(vm);
369 v3_deinit_nested_paging(vm);
371 v3_deinit_ext_manager(vm);
372 v3_deinit_intr_routers(vm);
373 v3_deinit_host_events(vm);
375 v3_deinit_barrier(vm);
377 v3_deinit_cpuid_map(vm);
378 v3_deinit_msr_map(vm);
379 v3_deinit_io_map(vm);
380 v3_deinit_hypercall_map(vm);
382 v3_deinit_exit_hooks(vm);
384 #ifdef V3_CONFIG_TELEMETRY
385 v3_deinit_telemetry(vm);
389 v3_deinit_events(vm);
391 #ifdef V3_CONFIG_MEM_TRACK
392 v3_mem_track_deinit(vm);
395 v3_fw_cfg_deinit(vm);
402 int v3_init_core(struct guest_info * core) {
403 extern v3_cpu_arch_t v3_mach_type;
404 struct v3_vm_info * vm = core->vm_info;
409 * Initialize the subsystem data strutures
411 #ifdef V3_CONFIG_TELEMETRY
412 v3_init_core_telemetry(core);
415 if (core->shdw_pg_mode == SHADOW_PAGING) {
416 v3_init_passthrough_paging_core(core);
417 v3_init_shdw_pg_state(core);
419 //done later due to SVM/VMX differences
420 //v3_init_nested_paging_core(core);
423 v3_init_time_core(core);
424 v3_init_intr_controllers(core);
425 v3_init_exception_state(core);
427 v3_init_decoder(core);
430 #ifdef V3_CONFIG_SYMBIOTIC
431 v3_init_symbiotic_core(core);
437 switch (v3_mach_type) {
440 case V3_SVM_REV3_CPU:
441 if (v3_init_svm_vmcb(core, vm->vm_class) == -1) {
442 PrintError(vm, core, "Error in SVM initialization\n");
450 case V3_VMX_EPT_UG_CPU:
451 if (v3_init_vmx_vmcs(core, vm->vm_class) == -1) {
452 PrintError(vm, core, "Error in VMX initialization\n");
458 PrintError(vm, core, "Invalid CPU Type 0x%x\n", v3_mach_type);
462 v3_init_exit_hooks_core(core);
470 int v3_free_core(struct guest_info * core) {
471 extern v3_cpu_arch_t v3_mach_type;
474 #ifdef V3_CONFIG_SYMBIOTIC
475 v3_deinit_symbiotic_core(core);
479 v3_deinit_hvm_core(core);
482 #ifdef V3_CONFIG_MULTIBOOT
483 v3_deinit_multiboot_core(core);
486 v3_deinit_decoder(core);
488 v3_deinit_intr_controllers(core);
489 v3_deinit_time_core(core);
491 if (core->shdw_pg_mode == SHADOW_PAGING) {
492 v3_deinit_shdw_pg_state(core);
493 v3_deinit_passthrough_paging_core(core);
495 v3_deinit_nested_paging_core(core);
498 v3_free_passthrough_pts(core);
500 #ifdef V3_CONFIG_TELEMETRY
501 v3_deinit_core_telemetry(core);
505 switch (v3_mach_type) {
508 case V3_SVM_REV3_CPU:
509 if (v3_deinit_svm_vmcb(core) == -1) {
510 PrintError(VM_NONE,VCORE_NONE, "Error in SVM deinitialization\n");
518 case V3_VMX_EPT_UG_CPU:
519 if (v3_deinit_vmx_vmcs(core) == -1) {
520 PrintError(VM_NONE, VCORE_NONE, "Error in VMX initialization\n");
526 PrintError(core->vm_info, core, "Invalid CPU Type 0x%x\n", v3_mach_type);