X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fsvm.c;h=89e33d35453311429700c820789c200d90e47a8c;hb=e02cc15e36e8f5d7ee66390986351748dd9a16a1;hp=a88ff0e097a525125fba75abb7311f88264f18ba;hpb=3a0ee76fe3accce60ae161615cadc6e56cac008b;p=palacios.git diff --git a/palacios/src/palacios/svm.c b/palacios/src/palacios/svm.c index a88ff0e..89e33d3 100644 --- a/palacios/src/palacios/svm.c +++ b/palacios/src/palacios/svm.c @@ -64,7 +64,15 @@ extern int v3_svm_launch(vmcb_t * vmcb, struct v3_gprs * vm_regs, vmcb_t * host_ static vmcb_t * Allocate_VMCB() { - vmcb_t * vmcb_page = (vmcb_t *)V3_VAddr(V3_AllocPages(1)); + vmcb_t * vmcb_page = NULL; + addr_t vmcb_pa = (addr_t)V3_AllocPages(1); + + if ((void *)vmcb_pa == NULL) { + PrintError("Error allocating VMCB\n"); + return NULL; + } + + vmcb_page = (vmcb_t *)V3_VAddr((void *)vmcb_pa); memset(vmcb_page, 0, 4096); @@ -210,6 +218,11 @@ static void Init_VMCB_BIOS(vmcb_t * vmcb, struct guest_info * core) { ctrl_area->instrs.INTR = 1; + v3_hook_msr(core->vm_info, EFER_MSR, + &v3_handle_efer_read, + &v3_handle_efer_write, + core); + if (core->shdw_pg_mode == SHADOW_PAGING) { PrintDebug("Creating initial shadow page table\n"); @@ -238,10 +251,7 @@ static void Init_VMCB_BIOS(vmcb_t * vmcb, struct guest_info * core) { ctrl_area->cr_reads.cr3 = 1; ctrl_area->cr_writes.cr3 = 1; - v3_hook_msr(core->vm_info, EFER_MSR, - &v3_handle_efer_read, - &v3_handle_efer_write, - core); + ctrl_area->instrs.INVLPG = 1; @@ -271,17 +281,28 @@ static void Init_VMCB_BIOS(vmcb_t * vmcb, struct guest_info * core) { guest_state->g_pat = 0x7040600070406ULL; } + + /* tell the guest that we don't support SVM */ + v3_hook_msr(core->vm_info, SVM_VM_CR_MSR, + &v3_handle_vm_cr_read, + &v3_handle_vm_cr_write, + core); } -int v3_init_svm_vmcb(struct guest_info * info, v3_vm_class_t vm_class) { +int v3_init_svm_vmcb(struct guest_info * core, v3_vm_class_t vm_class) { PrintDebug("Allocating VMCB\n"); - info->vmm_data = (void*)Allocate_VMCB(); + core->vmm_data = (void *)Allocate_VMCB(); + if (core->vmm_data == NULL) { + PrintError("Could not allocate VMCB, Exiting...\n"); + return -1; + } + if (vm_class == V3_PC_VM) { - PrintDebug("Initializing VMCB (addr=%p)\n", (void *)info->vmm_data); - Init_VMCB_BIOS((vmcb_t*)(info->vmm_data), info); + PrintDebug("Initializing VMCB (addr=%p)\n", (void *)core->vmm_data); + Init_VMCB_BIOS((vmcb_t*)(core->vmm_data), core); } else { PrintError("Invalid VM class\n"); return -1; @@ -291,6 +312,11 @@ int v3_init_svm_vmcb(struct guest_info * info, v3_vm_class_t vm_class) { } +int v3_deinit_svm_vmcb(struct guest_info * core) { + V3_FreePages(V3_PAddr(core->vmm_data), 1); + return 0; +} + static int update_irq_exit_state(struct guest_info * info) { vmcb_ctrl_t * guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data)); @@ -427,12 +453,15 @@ int v3_svm_enter(struct guest_info * info) { vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data)); addr_t exit_code = 0, exit_info1 = 0, exit_info2 = 0; - v3_update_timers(info); - v3_adjust_time(info); - // Conditionally yield the CPU if the timeslice has expired v3_yield_cond(info); + // Perform any additional yielding needed for time adjustment + v3_adjust_time(info); + + // Update timer devices prior to entering VM. + v3_update_timers(info); + // disable global interrupts for vm state transition v3_clgi(); @@ -480,18 +509,20 @@ int v3_svm_enter(struct guest_info * info) { } #endif + v3_time_enter_vm(info); guest_ctrl->TSC_OFFSET = v3_tsc_host_offset(&info->time_state); //V3_Print("Calling v3_svm_launch\n"); - v3_svm_launch((vmcb_t *)V3_PAddr(info->vmm_data), &(info->vm_regs), (vmcb_t *)host_vmcbs[info->cpu_id]); + v3_svm_launch((vmcb_t *)V3_PAddr(info->vmm_data), &(info->vm_regs), (vmcb_t *)host_vmcbs[info->host_cpu_id]); //V3_Print("SVM Returned: Exit Code: %x, guest_rip=%lx\n", (uint32_t)(guest_ctrl->exit_code), (unsigned long)guest_state->rip); v3_last_exit = (uint32_t)(guest_ctrl->exit_code); - //PrintDebug("SVM Returned\n"); - + // Immediate exit from VM time bookkeeping + v3_time_exit_vm(info); + info->num_exits++; // Save Guest state from VMCB @@ -745,7 +776,27 @@ void v3_init_svm_cpu(int cpu_id) { +void v3_deinit_svm_cpu(int cpu_id) { + reg_ex_t msr; + extern v3_cpu_arch_t v3_cpu_types[]; + // reset SVM_VM_HSAVE_PA_MSR + // Does setting it to NULL disable?? + msr.r_reg = 0; + v3_set_msr(SVM_VM_HSAVE_PA_MSR, msr.e_reg.high, msr.e_reg.low); + + // Disable SVM? + v3_get_msr(EFER_MSR, &(msr.e_reg.high), &(msr.e_reg.low)); + msr.e_reg.low &= ~EFER_MSR_svm_enable; + v3_set_msr(EFER_MSR, 0, msr.e_reg.low); + + v3_cpu_types[cpu_id] = V3_INVALID_CPU; + + V3_FreePages((void *)host_vmcbs[cpu_id], 4); + + V3_Print("Host CPU %d host area freed, and SVM disabled\n", cpu_id); + return; +}