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 cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
41 struct v3_segment * cs = &(info->segments.cs);
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 *)&(info->ctrl_regs.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);
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 *)&(info->ctrl_regs.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) {
171 #include <palacios/vmcs.h>
172 #include <palacios/vmcb.h>
173 static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_data) {
174 extern v3_cpu_arch_t v3_mach_type;
177 V3_Print("************** Guest State ************\n");
178 v3_print_guest_state(core);
182 if ((v3_mach_type == V3_SVM_CPU) || (v3_mach_type == V3_SVM_REV3_CPU)) {
184 PrintDebugVMCB((vmcb_t *)(core->vmm_data));
188 if ((v3_mach_type == V3_VMX_CPU) || (v3_mach_type == V3_VMX_EPT_CPU) || (v3_mach_type == V3_VMX_EPT_UG_CPU)) {
194 PrintError("Invalid CPU Type 0x%x\n", v3_mach_type);
204 #include <palacios/svm.h>
205 #include <palacios/svm_io.h>
206 #include <palacios/svm_msr.h>
207 #include <palacios/svm_exits.h>
211 #include <palacios/vmx.h>
212 #include <palacios/vmx_io.h>
213 #include <palacios/vmx_msr.h>
217 int v3_init_vm(struct v3_vm_info * vm) {
218 extern v3_cpu_arch_t v3_mach_type;
222 #ifdef V3_CONFIG_TELEMETRY
223 v3_init_telemetry(vm);
226 v3_init_exit_hooks(vm);
227 v3_init_hypercall_map(vm);
230 v3_init_cpuid_map(vm);
231 v3_init_host_events(vm);
233 v3_init_intr_routers(vm);
234 v3_init_ext_manager(vm);
238 // Initialize the memory map
239 if (v3_init_mem_map(vm) == -1) {
240 PrintError("Could not initialize shadow map\n");
244 v3_init_mem_hooks(vm);
246 if (v3_init_shdw_impl(vm) == -1) {
247 PrintError("VM initialization error in shadow implementaion\n");
254 v3_init_vm_debugging(vm);
257 #ifdef V3_CONFIG_SYMBIOTIC
258 v3_init_symbiotic_vm(vm);
265 switch (v3_mach_type) {
268 case V3_SVM_REV3_CPU:
269 v3_init_svm_io_map(vm);
270 v3_init_svm_msr_map(vm);
271 v3_init_svm_exits(vm);
277 case V3_VMX_EPT_UG_CPU:
278 v3_init_vmx_io_map(vm);
279 v3_init_vmx_msr_map(vm);
283 PrintError("Invalid CPU Type 0x%x\n", v3_mach_type);
287 v3_register_hypercall(vm, GUEST_INFO_HCALL, info_hcall, NULL);
289 V3_Print("GUEST_INFO_HCALL=%x\n", GUEST_INFO_HCALL);
295 int v3_free_vm_internal(struct v3_vm_info * vm) {
296 extern v3_cpu_arch_t v3_mach_type;
298 v3_remove_hypercall(vm, GUEST_INFO_HCALL);
302 #ifdef V3_CONFIG_SYMBIOTIC
303 v3_deinit_symbiotic_vm(vm);
307 switch (v3_mach_type) {
310 case V3_SVM_REV3_CPU:
311 v3_deinit_svm_io_map(vm);
312 v3_deinit_svm_msr_map(vm);
318 case V3_VMX_EPT_UG_CPU:
319 v3_deinit_vmx_io_map(vm);
320 v3_deinit_vmx_msr_map(vm);
324 PrintError("Invalid CPU Type 0x%x\n", v3_mach_type);
328 v3_deinit_dev_mgr(vm);
330 v3_deinit_time_vm(vm);
332 v3_deinit_mem_hooks(vm);
333 v3_delete_mem_map(vm);
334 v3_deinit_shdw_impl(vm);
336 v3_deinit_ext_manager(vm);
337 v3_deinit_intr_routers(vm);
338 v3_deinit_host_events(vm);
340 v3_deinit_barrier(vm);
342 v3_deinit_cpuid_map(vm);
343 v3_deinit_msr_map(vm);
344 v3_deinit_io_map(vm);
345 v3_deinit_hypercall_map(vm);
347 v3_deinit_exit_hooks(vm);
349 #ifdef V3_CONFIG_TELEMETRY
350 v3_deinit_telemetry(vm);
353 v3_deinit_events(vm);
360 int v3_init_core(struct guest_info * core) {
361 extern v3_cpu_arch_t v3_mach_type;
362 struct v3_vm_info * vm = core->vm_info;
367 * Initialize the subsystem data strutures
369 #ifdef V3_CONFIG_TELEMETRY
370 v3_init_core_telemetry(core);
373 if (core->shdw_pg_mode == SHADOW_PAGING) {
374 v3_init_shdw_pg_state(core);
377 v3_init_time_core(core);
378 v3_init_intr_controllers(core);
379 v3_init_exception_state(core);
381 v3_init_decoder(core);
384 #ifdef V3_CONFIG_SYMBIOTIC
385 v3_init_symbiotic_core(core);
391 switch (v3_mach_type) {
394 case V3_SVM_REV3_CPU:
395 if (v3_init_svm_vmcb(core, vm->vm_class) == -1) {
396 PrintError("Error in SVM initialization\n");
404 case V3_VMX_EPT_UG_CPU:
405 if (v3_init_vmx_vmcs(core, vm->vm_class) == -1) {
406 PrintError("Error in VMX initialization\n");
412 PrintError("Invalid CPU Type 0x%x\n", v3_mach_type);
416 v3_init_exit_hooks_core(core);
424 int v3_free_core(struct guest_info * core) {
425 extern v3_cpu_arch_t v3_mach_type;
428 #ifdef V3_CONFIG_SYMBIOTIC
429 v3_deinit_symbiotic_core(core);
432 v3_deinit_decoder(core);
434 v3_deinit_intr_controllers(core);
435 v3_deinit_time_core(core);
437 if (core->shdw_pg_mode == SHADOW_PAGING) {
438 v3_deinit_shdw_pg_state(core);
441 v3_free_passthrough_pts(core);
443 #ifdef V3_CONFIG_TELEMETRY
444 v3_deinit_core_telemetry(core);
447 switch (v3_mach_type) {
450 case V3_SVM_REV3_CPU:
451 if (v3_deinit_svm_vmcb(core) == -1) {
452 PrintError("Error in SVM initialization\n");
460 case V3_VMX_EPT_UG_CPU:
461 if (v3_deinit_vmx_vmcs(core) == -1) {
462 PrintError("Error in VMX initialization\n");
468 PrintError("Invalid CPU Type 0x%x\n", v3_mach_type);