1c97d6d2cSSergio Andres Gomez Del Real /* Copyright 2008 IBM Corporation 2c97d6d2cSSergio Andres Gomez Del Real * 2008 Red Hat, Inc. 3c97d6d2cSSergio Andres Gomez Del Real * Copyright 2011 Intel Corporation 4c97d6d2cSSergio Andres Gomez Del Real * Copyright 2016 Veertu, Inc. 5c97d6d2cSSergio Andres Gomez Del Real * Copyright 2017 The Android Open Source Project 6c97d6d2cSSergio Andres Gomez Del Real * 7c97d6d2cSSergio Andres Gomez Del Real * QEMU Hypervisor.framework support 8c97d6d2cSSergio Andres Gomez Del Real * 9c97d6d2cSSergio Andres Gomez Del Real * This program is free software; you can redistribute it and/or 10c97d6d2cSSergio Andres Gomez Del Real * modify it under the terms of version 2 of the GNU General Public 11c97d6d2cSSergio Andres Gomez Del Real * License as published by the Free Software Foundation. 12c97d6d2cSSergio Andres Gomez Del Real * 13c97d6d2cSSergio Andres Gomez Del Real * This program is distributed in the hope that it will be useful, 14c97d6d2cSSergio Andres Gomez Del Real * but WITHOUT ANY WARRANTY; without even the implied warranty of 15c97d6d2cSSergio Andres Gomez Del Real * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16e361a772SThomas Huth * General Public License for more details. 17c97d6d2cSSergio Andres Gomez Del Real * 18e361a772SThomas Huth * You should have received a copy of the GNU General Public License 19e361a772SThomas Huth * along with this program; if not, see <http://www.gnu.org/licenses/>. 20d781e24dSIzik Eidus * 21d781e24dSIzik Eidus * This file contain code under public domain from the hvdos project: 22d781e24dSIzik Eidus * https://github.com/mist64/hvdos 234d98a8e5SPaolo Bonzini * 244d98a8e5SPaolo Bonzini * Parts Copyright (c) 2011 NetApp, Inc. 254d98a8e5SPaolo Bonzini * All rights reserved. 264d98a8e5SPaolo Bonzini * 274d98a8e5SPaolo Bonzini * Redistribution and use in source and binary forms, with or without 284d98a8e5SPaolo Bonzini * modification, are permitted provided that the following conditions 294d98a8e5SPaolo Bonzini * are met: 304d98a8e5SPaolo Bonzini * 1. Redistributions of source code must retain the above copyright 314d98a8e5SPaolo Bonzini * notice, this list of conditions and the following disclaimer. 324d98a8e5SPaolo Bonzini * 2. Redistributions in binary form must reproduce the above copyright 334d98a8e5SPaolo Bonzini * notice, this list of conditions and the following disclaimer in the 344d98a8e5SPaolo Bonzini * documentation and/or other materials provided with the distribution. 354d98a8e5SPaolo Bonzini * 364d98a8e5SPaolo Bonzini * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 374d98a8e5SPaolo Bonzini * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 384d98a8e5SPaolo Bonzini * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 394d98a8e5SPaolo Bonzini * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 404d98a8e5SPaolo Bonzini * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 414d98a8e5SPaolo Bonzini * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 424d98a8e5SPaolo Bonzini * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 434d98a8e5SPaolo Bonzini * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 444d98a8e5SPaolo Bonzini * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 454d98a8e5SPaolo Bonzini * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 464d98a8e5SPaolo Bonzini * SUCH DAMAGE. 47c97d6d2cSSergio Andres Gomez Del Real */ 4854d31236SMarkus Armbruster 49c97d6d2cSSergio Andres Gomez Del Real #include "qemu/osdep.h" 50c97d6d2cSSergio Andres Gomez Del Real #include "qemu-common.h" 51c97d6d2cSSergio Andres Gomez Del Real #include "qemu/error-report.h" 52c97d6d2cSSergio Andres Gomez Del Real 53c97d6d2cSSergio Andres Gomez Del Real #include "sysemu/hvf.h" 54d57bc3c1SAlexander Graf #include "sysemu/hvf_int.h" 5554d31236SMarkus Armbruster #include "sysemu/runstate.h" 56c97d6d2cSSergio Andres Gomez Del Real #include "hvf-i386.h" 5769e0a03cSPaolo Bonzini #include "vmcs.h" 5869e0a03cSPaolo Bonzini #include "vmx.h" 5969e0a03cSPaolo Bonzini #include "x86.h" 6069e0a03cSPaolo Bonzini #include "x86_descr.h" 6169e0a03cSPaolo Bonzini #include "x86_mmu.h" 6269e0a03cSPaolo Bonzini #include "x86_decode.h" 6369e0a03cSPaolo Bonzini #include "x86_emu.h" 6469e0a03cSPaolo Bonzini #include "x86_task.h" 6569e0a03cSPaolo Bonzini #include "x86hvf.h" 66c97d6d2cSSergio Andres Gomez Del Real 67c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv.h> 68c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv_vmx.h> 693b502b0eSVladislav Yaroshchuk #include <sys/sysctl.h> 70c97d6d2cSSergio Andres Gomez Del Real 71c97d6d2cSSergio Andres Gomez Del Real #include "hw/i386/apic_internal.h" 72c97d6d2cSSergio Andres Gomez Del Real #include "qemu/main-loop.h" 73940e43aaSClaudio Fontana #include "qemu/accel.h" 74c97d6d2cSSergio Andres Gomez Del Real #include "target/i386/cpu.h" 75c97d6d2cSSergio Andres Gomez Del Real 76c97d6d2cSSergio Andres Gomez Del Real void vmx_update_tpr(CPUState *cpu) 77c97d6d2cSSergio Andres Gomez Del Real { 78c97d6d2cSSergio Andres Gomez Del Real /* TODO: need integrate APIC handling */ 79c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 80c97d6d2cSSergio Andres Gomez Del Real int tpr = cpu_get_apic_tpr(x86_cpu->apic_state) << 4; 81c97d6d2cSSergio Andres Gomez Del Real int irr = apic_get_highest_priority_irr(x86_cpu->apic_state); 82c97d6d2cSSergio Andres Gomez Del Real 83b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_TPR, tpr); 84c97d6d2cSSergio Andres Gomez Del Real if (irr == -1) { 85b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_TPR_THRESHOLD, 0); 86c97d6d2cSSergio Andres Gomez Del Real } else { 87b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_TPR_THRESHOLD, (irr > tpr) ? tpr >> 4 : 88c97d6d2cSSergio Andres Gomez Del Real irr >> 4); 89c97d6d2cSSergio Andres Gomez Del Real } 90c97d6d2cSSergio Andres Gomez Del Real } 91c97d6d2cSSergio Andres Gomez Del Real 92583ae161SRoman Bolshakov static void update_apic_tpr(CPUState *cpu) 93c97d6d2cSSergio Andres Gomez Del Real { 94c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 95b533450eSAlexander Graf int tpr = rreg(cpu->hvf->fd, HV_X86_TPR) >> 4; 96c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr); 97c97d6d2cSSergio Andres Gomez Del Real } 98c97d6d2cSSergio Andres Gomez Del Real 99c97d6d2cSSergio Andres Gomez Del Real #define VECTORING_INFO_VECTOR_MASK 0xff 100c97d6d2cSSergio Andres Gomez Del Real 101c97d6d2cSSergio Andres Gomez Del Real void hvf_handle_io(CPUArchState *env, uint16_t port, void *buffer, 102c97d6d2cSSergio Andres Gomez Del Real int direction, int size, int count) 103c97d6d2cSSergio Andres Gomez Del Real { 104c97d6d2cSSergio Andres Gomez Del Real int i; 105c97d6d2cSSergio Andres Gomez Del Real uint8_t *ptr = buffer; 106c97d6d2cSSergio Andres Gomez Del Real 107c97d6d2cSSergio Andres Gomez Del Real for (i = 0; i < count; i++) { 108c97d6d2cSSergio Andres Gomez Del Real address_space_rw(&address_space_io, port, MEMTXATTRS_UNSPECIFIED, 109c97d6d2cSSergio Andres Gomez Del Real ptr, size, 110c97d6d2cSSergio Andres Gomez Del Real direction); 111c97d6d2cSSergio Andres Gomez Del Real ptr += size; 112c97d6d2cSSergio Andres Gomez Del Real } 113c97d6d2cSSergio Andres Gomez Del Real } 114c97d6d2cSSergio Andres Gomez Del Real 115ff2de166SPaolo Bonzini static bool ept_emulation_fault(hvf_slot *slot, uint64_t gpa, uint64_t ept_qual) 116c97d6d2cSSergio Andres Gomez Del Real { 117c97d6d2cSSergio Andres Gomez Del Real int read, write; 118c97d6d2cSSergio Andres Gomez Del Real 119c97d6d2cSSergio Andres Gomez Del Real /* EPT fault on an instruction fetch doesn't make sense here */ 120c97d6d2cSSergio Andres Gomez Del Real if (ept_qual & EPT_VIOLATION_INST_FETCH) { 121c97d6d2cSSergio Andres Gomez Del Real return false; 122c97d6d2cSSergio Andres Gomez Del Real } 123c97d6d2cSSergio Andres Gomez Del Real 124c97d6d2cSSergio Andres Gomez Del Real /* EPT fault must be a read fault or a write fault */ 125c97d6d2cSSergio Andres Gomez Del Real read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0; 126c97d6d2cSSergio Andres Gomez Del Real write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0; 127c97d6d2cSSergio Andres Gomez Del Real if ((read | write) == 0) { 128c97d6d2cSSergio Andres Gomez Del Real return false; 129c97d6d2cSSergio Andres Gomez Del Real } 130c97d6d2cSSergio Andres Gomez Del Real 131babfa20cSSergio Andres Gomez Del Real if (write && slot) { 132babfa20cSSergio Andres Gomez Del Real if (slot->flags & HVF_SLOT_LOG) { 133babfa20cSSergio Andres Gomez Del Real memory_region_set_dirty(slot->region, gpa - slot->start, 1); 134babfa20cSSergio Andres Gomez Del Real hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size, 135babfa20cSSergio Andres Gomez Del Real HV_MEMORY_READ | HV_MEMORY_WRITE); 136babfa20cSSergio Andres Gomez Del Real } 137babfa20cSSergio Andres Gomez Del Real } 138babfa20cSSergio Andres Gomez Del Real 139c97d6d2cSSergio Andres Gomez Del Real /* 140c97d6d2cSSergio Andres Gomez Del Real * The EPT violation must have been caused by accessing a 141c97d6d2cSSergio Andres Gomez Del Real * guest-physical address that is a translation of a guest-linear 142c97d6d2cSSergio Andres Gomez Del Real * address. 143c97d6d2cSSergio Andres Gomez Del Real */ 144c97d6d2cSSergio Andres Gomez Del Real if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 || 145c97d6d2cSSergio Andres Gomez Del Real (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) { 146c97d6d2cSSergio Andres Gomez Del Real return false; 147c97d6d2cSSergio Andres Gomez Del Real } 148c97d6d2cSSergio Andres Gomez Del Real 149fbafbb6dSCameron Esfahani if (!slot) { 150fbafbb6dSCameron Esfahani return true; 151fbafbb6dSCameron Esfahani } 152fbafbb6dSCameron Esfahani if (!memory_region_is_ram(slot->region) && 153fbafbb6dSCameron Esfahani !(read && memory_region_is_romd(slot->region))) { 154fbafbb6dSCameron Esfahani return true; 155fbafbb6dSCameron Esfahani } 156fbafbb6dSCameron Esfahani return false; 157babfa20cSSergio Andres Gomez Del Real } 158babfa20cSSergio Andres Gomez Del Real 159cfe58455SAlexander Graf void hvf_arch_vcpu_destroy(CPUState *cpu) 160c97d6d2cSSergio Andres Gomez Del Real { 161fe76b09cSRoman Bolshakov X86CPU *x86_cpu = X86_CPU(cpu); 162fe76b09cSRoman Bolshakov CPUX86State *env = &x86_cpu->env; 163fe76b09cSRoman Bolshakov 164fe76b09cSRoman Bolshakov g_free(env->hvf_mmio_buf); 165c97d6d2cSSergio Andres Gomez Del Real } 166c97d6d2cSSergio Andres Gomez Del Real 1673b502b0eSVladislav Yaroshchuk static void init_tsc_freq(CPUX86State *env) 1683b502b0eSVladislav Yaroshchuk { 1693b502b0eSVladislav Yaroshchuk size_t length; 1703b502b0eSVladislav Yaroshchuk uint64_t tsc_freq; 1713b502b0eSVladislav Yaroshchuk 1723b502b0eSVladislav Yaroshchuk if (env->tsc_khz != 0) { 1733b502b0eSVladislav Yaroshchuk return; 1743b502b0eSVladislav Yaroshchuk } 1753b502b0eSVladislav Yaroshchuk 1763b502b0eSVladislav Yaroshchuk length = sizeof(uint64_t); 1773b502b0eSVladislav Yaroshchuk if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) { 1783b502b0eSVladislav Yaroshchuk return; 1793b502b0eSVladislav Yaroshchuk } 1803b502b0eSVladislav Yaroshchuk env->tsc_khz = tsc_freq / 1000; /* Hz to KHz */ 1813b502b0eSVladislav Yaroshchuk } 1823b502b0eSVladislav Yaroshchuk 1833b502b0eSVladislav Yaroshchuk static void init_apic_bus_freq(CPUX86State *env) 1843b502b0eSVladislav Yaroshchuk { 1853b502b0eSVladislav Yaroshchuk size_t length; 1863b502b0eSVladislav Yaroshchuk uint64_t bus_freq; 1873b502b0eSVladislav Yaroshchuk 1883b502b0eSVladislav Yaroshchuk if (env->apic_bus_freq != 0) { 1893b502b0eSVladislav Yaroshchuk return; 1903b502b0eSVladislav Yaroshchuk } 1913b502b0eSVladislav Yaroshchuk 1923b502b0eSVladislav Yaroshchuk length = sizeof(uint64_t); 1933b502b0eSVladislav Yaroshchuk if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) { 1943b502b0eSVladislav Yaroshchuk return; 1953b502b0eSVladislav Yaroshchuk } 1963b502b0eSVladislav Yaroshchuk env->apic_bus_freq = bus_freq; 1973b502b0eSVladislav Yaroshchuk } 1983b502b0eSVladislav Yaroshchuk 1993b502b0eSVladislav Yaroshchuk static inline bool tsc_is_known(CPUX86State *env) 2003b502b0eSVladislav Yaroshchuk { 2013b502b0eSVladislav Yaroshchuk return env->tsc_khz != 0; 2023b502b0eSVladislav Yaroshchuk } 2033b502b0eSVladislav Yaroshchuk 2043b502b0eSVladislav Yaroshchuk static inline bool apic_bus_freq_is_known(CPUX86State *env) 2053b502b0eSVladislav Yaroshchuk { 2063b502b0eSVladislav Yaroshchuk return env->apic_bus_freq != 0; 2073b502b0eSVladislav Yaroshchuk } 2083b502b0eSVladislav Yaroshchuk 209cfe58455SAlexander Graf int hvf_arch_init_vcpu(CPUState *cpu) 210c97d6d2cSSergio Andres Gomez Del Real { 211c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86cpu = X86_CPU(cpu); 212c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86cpu->env; 213c97d6d2cSSergio Andres Gomez Del Real 214c97d6d2cSSergio Andres Gomez Del Real init_emu(); 215c97d6d2cSSergio Andres Gomez Del Real init_decoder(); 216c97d6d2cSSergio Andres Gomez Del Real 217c97d6d2cSSergio Andres Gomez Del Real hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1); 218fe76b09cSRoman Bolshakov env->hvf_mmio_buf = g_new(char, 4096); 219c97d6d2cSSergio Andres Gomez Del Real 2203b502b0eSVladislav Yaroshchuk if (x86cpu->vmware_cpuid_freq) { 2213b502b0eSVladislav Yaroshchuk init_tsc_freq(env); 2223b502b0eSVladislav Yaroshchuk init_apic_bus_freq(env); 2233b502b0eSVladislav Yaroshchuk 2243b502b0eSVladislav Yaroshchuk if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) { 2253b502b0eSVladislav Yaroshchuk error_report("vmware-cpuid-freq: feature couldn't be enabled"); 2263b502b0eSVladislav Yaroshchuk } 2273b502b0eSVladislav Yaroshchuk } 2283b502b0eSVladislav Yaroshchuk 229c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PINBASED, 230c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_pinbased)) { 231c97d6d2cSSergio Andres Gomez Del Real abort(); 232c97d6d2cSSergio Andres Gomez Del Real } 233c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED, 234c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased)) { 235c97d6d2cSSergio Andres Gomez Del Real abort(); 236c97d6d2cSSergio Andres Gomez Del Real } 237c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2, 238c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased2)) { 239c97d6d2cSSergio Andres Gomez Del Real abort(); 240c97d6d2cSSergio Andres Gomez Del Real } 241c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_ENTRY, 242c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_entry)) { 243c97d6d2cSSergio Andres Gomez Del Real abort(); 244c97d6d2cSSergio Andres Gomez Del Real } 245c97d6d2cSSergio Andres Gomez Del Real 246c97d6d2cSSergio Andres Gomez Del Real /* set VMCS control fields */ 247b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_PIN_BASED_CTLS, 248c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_pinbased, 249c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_EXTINT | 250c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_NMI | 251c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_VNMI)); 252b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_PRI_PROC_BASED_CTLS, 253c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased, 254c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_HLT | 255c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_MWAIT | 256c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TSC_OFFSET | 257c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TPR_SHADOW) | 258c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_SEC_CONTROL); 259b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_SEC_PROC_BASED_CTLS, 260c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased2, 261c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED2_CTLS_APIC_ACCESSES)); 262c97d6d2cSSergio Andres Gomez Del Real 263b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_ENTRY_CTLS, cap2ctrl(hvf_state->hvf_caps->vmx_cap_entry, 264c97d6d2cSSergio Andres Gomez Del Real 0)); 265b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_EXCEPTION_BITMAP, 0); /* Double fault */ 266c97d6d2cSSergio Andres Gomez Del Real 267b533450eSAlexander Graf wvmcs(cpu->hvf->fd, VMCS_TPR_THRESHOLD, 0); 268c97d6d2cSSergio Andres Gomez Del Real 269c97d6d2cSSergio Andres Gomez Del Real x86cpu = X86_CPU(cpu); 270c0198c5fSDavid Edmondson x86cpu->env.xsave_buf_len = 4096; 271c0198c5fSDavid Edmondson x86cpu->env.xsave_buf = qemu_memalign(4096, x86cpu->env.xsave_buf_len); 272c97d6d2cSSergio Andres Gomez Del Real 273*fea45008SDavid Edmondson /* 274*fea45008SDavid Edmondson * The allocated storage must be large enough for all of the 275*fea45008SDavid Edmondson * possible XSAVE state components. 276*fea45008SDavid Edmondson */ 277*fea45008SDavid Edmondson assert(hvf_get_supported_cpuid(0xd, 0, R_ECX) <= x86cpu->env.xsave_buf_len); 278*fea45008SDavid Edmondson 279b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_STAR, 1); 280b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_LSTAR, 1); 281b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_CSTAR, 1); 282b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_FMASK, 1); 283b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_FSBASE, 1); 284b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_GSBASE, 1); 285b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_KERNELGSBASE, 1); 286b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_TSC_AUX, 1); 287b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_IA32_TSC, 1); 288b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_IA32_SYSENTER_CS, 1); 289b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_IA32_SYSENTER_EIP, 1); 290b533450eSAlexander Graf hv_vcpu_enable_native_msr(cpu->hvf->fd, MSR_IA32_SYSENTER_ESP, 1); 291c97d6d2cSSergio Andres Gomez Del Real 292c97d6d2cSSergio Andres Gomez Del Real return 0; 293c97d6d2cSSergio Andres Gomez Del Real } 294c97d6d2cSSergio Andres Gomez Del Real 295b7394c83SSergio Andres Gomez Del Real static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_info) 296b7394c83SSergio Andres Gomez Del Real { 297b7394c83SSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 298b7394c83SSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 299b7394c83SSergio Andres Gomez Del Real 300fd13f23bSLiran Alon env->exception_nr = -1; 301fd13f23bSLiran Alon env->exception_pending = 0; 302fd13f23bSLiran Alon env->exception_injected = 0; 303b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = -1; 304b7394c83SSergio Andres Gomez Del Real env->nmi_injected = false; 30564bef038SCameron Esfahani env->ins_len = 0; 30664bef038SCameron Esfahani env->has_error_code = false; 307b7394c83SSergio Andres Gomez Del Real if (idtvec_info & VMCS_IDT_VEC_VALID) { 308b7394c83SSergio Andres Gomez Del Real switch (idtvec_info & VMCS_IDT_VEC_TYPE) { 309b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWINTR: 310b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWINTR: 311b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = idtvec_info & VMCS_IDT_VEC_VECNUM; 312b7394c83SSergio Andres Gomez Del Real break; 313b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_NMI: 314b7394c83SSergio Andres Gomez Del Real env->nmi_injected = true; 315b7394c83SSergio Andres Gomez Del Real break; 316b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWEXCEPTION: 317b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWEXCEPTION: 318fd13f23bSLiran Alon env->exception_nr = idtvec_info & VMCS_IDT_VEC_VECNUM; 319fd13f23bSLiran Alon env->exception_injected = 1; 320b7394c83SSergio Andres Gomez Del Real break; 321b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_PRIV_SWEXCEPTION: 322b7394c83SSergio Andres Gomez Del Real default: 323b7394c83SSergio Andres Gomez Del Real abort(); 324b7394c83SSergio Andres Gomez Del Real } 325b7394c83SSergio Andres Gomez Del Real if ((idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWEXCEPTION || 326b7394c83SSergio Andres Gomez Del Real (idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWINTR) { 327b7394c83SSergio Andres Gomez Del Real env->ins_len = ins_len; 328b7394c83SSergio Andres Gomez Del Real } 32964bef038SCameron Esfahani if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) { 330b7394c83SSergio Andres Gomez Del Real env->has_error_code = true; 331b533450eSAlexander Graf env->error_code = rvmcs(cpu->hvf->fd, VMCS_IDT_VECTORING_ERROR); 332b7394c83SSergio Andres Gomez Del Real } 333b7394c83SSergio Andres Gomez Del Real } 334b533450eSAlexander Graf if ((rvmcs(cpu->hvf->fd, VMCS_GUEST_INTERRUPTIBILITY) & 335b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_NMI_BLOCKING)) { 336b7394c83SSergio Andres Gomez Del Real env->hflags2 |= HF2_NMI_MASK; 337b7394c83SSergio Andres Gomez Del Real } else { 338b7394c83SSergio Andres Gomez Del Real env->hflags2 &= ~HF2_NMI_MASK; 339b7394c83SSergio Andres Gomez Del Real } 340b533450eSAlexander Graf if (rvmcs(cpu->hvf->fd, VMCS_GUEST_INTERRUPTIBILITY) & 341b7394c83SSergio Andres Gomez Del Real (VMCS_INTERRUPTIBILITY_STI_BLOCKING | 342b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) { 343b7394c83SSergio Andres Gomez Del Real env->hflags |= HF_INHIBIT_IRQ_MASK; 344b7394c83SSergio Andres Gomez Del Real } else { 345b7394c83SSergio Andres Gomez Del Real env->hflags &= ~HF_INHIBIT_IRQ_MASK; 346b7394c83SSergio Andres Gomez Del Real } 347b7394c83SSergio Andres Gomez Del Real } 348b7394c83SSergio Andres Gomez Del Real 3493b502b0eSVladislav Yaroshchuk static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, 3503b502b0eSVladislav Yaroshchuk uint32_t *eax, uint32_t *ebx, 3513b502b0eSVladislav Yaroshchuk uint32_t *ecx, uint32_t *edx) 3523b502b0eSVladislav Yaroshchuk { 3533b502b0eSVladislav Yaroshchuk /* 3543b502b0eSVladislav Yaroshchuk * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs, 3553b502b0eSVladislav Yaroshchuk * leafs 0x40000001-0x4000000F are filled with zeros 3563b502b0eSVladislav Yaroshchuk * Provides vmware-cpuid-freq support to hvf 3573b502b0eSVladislav Yaroshchuk * 3583b502b0eSVladislav Yaroshchuk * Note: leaf 0x40000000 not exposes HVF, 3593b502b0eSVladislav Yaroshchuk * leaving hypervisor signature empty 3603b502b0eSVladislav Yaroshchuk */ 3613b502b0eSVladislav Yaroshchuk 3623b502b0eSVladislav Yaroshchuk if (index < 0x40000000 || index > 0x40000010 || 3633b502b0eSVladislav Yaroshchuk !tsc_is_known(env) || !apic_bus_freq_is_known(env)) { 3643b502b0eSVladislav Yaroshchuk 3653b502b0eSVladislav Yaroshchuk cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx); 3663b502b0eSVladislav Yaroshchuk return; 3673b502b0eSVladislav Yaroshchuk } 3683b502b0eSVladislav Yaroshchuk 3693b502b0eSVladislav Yaroshchuk switch (index) { 3703b502b0eSVladislav Yaroshchuk case 0x40000000: 3713b502b0eSVladislav Yaroshchuk *eax = 0x40000010; /* Max available cpuid leaf */ 3723b502b0eSVladislav Yaroshchuk *ebx = 0; /* Leave signature empty */ 3733b502b0eSVladislav Yaroshchuk *ecx = 0; 3743b502b0eSVladislav Yaroshchuk *edx = 0; 3753b502b0eSVladislav Yaroshchuk break; 3763b502b0eSVladislav Yaroshchuk case 0x40000010: 3773b502b0eSVladislav Yaroshchuk *eax = env->tsc_khz; 3783b502b0eSVladislav Yaroshchuk *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */ 3793b502b0eSVladislav Yaroshchuk *ecx = 0; 3803b502b0eSVladislav Yaroshchuk *edx = 0; 3813b502b0eSVladislav Yaroshchuk break; 3823b502b0eSVladislav Yaroshchuk default: 3833b502b0eSVladislav Yaroshchuk *eax = 0; 3843b502b0eSVladislav Yaroshchuk *ebx = 0; 3853b502b0eSVladislav Yaroshchuk *ecx = 0; 3863b502b0eSVladislav Yaroshchuk *edx = 0; 3873b502b0eSVladislav Yaroshchuk break; 3883b502b0eSVladislav Yaroshchuk } 3893b502b0eSVladislav Yaroshchuk } 3903b502b0eSVladislav Yaroshchuk 391c97d6d2cSSergio Andres Gomez Del Real int hvf_vcpu_exec(CPUState *cpu) 392c97d6d2cSSergio Andres Gomez Del Real { 393c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 394c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 395c97d6d2cSSergio Andres Gomez Del Real int ret = 0; 396c97d6d2cSSergio Andres Gomez Del Real uint64_t rip = 0; 397c97d6d2cSSergio Andres Gomez Del Real 398c97d6d2cSSergio Andres Gomez Del Real if (hvf_process_events(cpu)) { 399c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT; 400c97d6d2cSSergio Andres Gomez Del Real } 401c97d6d2cSSergio Andres Gomez Del Real 402c97d6d2cSSergio Andres Gomez Del Real do { 403c97d6d2cSSergio Andres Gomez Del Real if (cpu->vcpu_dirty) { 404c97d6d2cSSergio Andres Gomez Del Real hvf_put_registers(cpu); 405c97d6d2cSSergio Andres Gomez Del Real cpu->vcpu_dirty = false; 406c97d6d2cSSergio Andres Gomez Del Real } 407c97d6d2cSSergio Andres Gomez Del Real 408b7394c83SSergio Andres Gomez Del Real if (hvf_inject_interrupts(cpu)) { 409b7394c83SSergio Andres Gomez Del Real return EXCP_INTERRUPT; 410b7394c83SSergio Andres Gomez Del Real } 411c97d6d2cSSergio Andres Gomez Del Real vmx_update_tpr(cpu); 412c97d6d2cSSergio Andres Gomez Del Real 413c97d6d2cSSergio Andres Gomez Del Real qemu_mutex_unlock_iothread(); 414c97d6d2cSSergio Andres Gomez Del Real if (!cpu_is_bsp(X86_CPU(cpu)) && cpu->halted) { 415c97d6d2cSSergio Andres Gomez Del Real qemu_mutex_lock_iothread(); 416c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT; 417c97d6d2cSSergio Andres Gomez Del Real } 418c97d6d2cSSergio Andres Gomez Del Real 419b533450eSAlexander Graf hv_return_t r = hv_vcpu_run(cpu->hvf->fd); 420c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(r); 421c97d6d2cSSergio Andres Gomez Del Real 422c97d6d2cSSergio Andres Gomez Del Real /* handle VMEXIT */ 423b533450eSAlexander Graf uint64_t exit_reason = rvmcs(cpu->hvf->fd, VMCS_EXIT_REASON); 424b533450eSAlexander Graf uint64_t exit_qual = rvmcs(cpu->hvf->fd, VMCS_EXIT_QUALIFICATION); 425b533450eSAlexander Graf uint32_t ins_len = (uint32_t)rvmcs(cpu->hvf->fd, 426c97d6d2cSSergio Andres Gomez Del Real VMCS_EXIT_INSTRUCTION_LENGTH); 427b7394c83SSergio Andres Gomez Del Real 428b533450eSAlexander Graf uint64_t idtvec_info = rvmcs(cpu->hvf->fd, VMCS_IDT_VECTORING_INFO); 429b7394c83SSergio Andres Gomez Del Real 430b7394c83SSergio Andres Gomez Del Real hvf_store_events(cpu, ins_len, idtvec_info); 431b533450eSAlexander Graf rip = rreg(cpu->hvf->fd, HV_X86_RIP); 432b533450eSAlexander Graf env->eflags = rreg(cpu->hvf->fd, HV_X86_RFLAGS); 433c97d6d2cSSergio Andres Gomez Del Real 434c97d6d2cSSergio Andres Gomez Del Real qemu_mutex_lock_iothread(); 435c97d6d2cSSergio Andres Gomez Del Real 436c97d6d2cSSergio Andres Gomez Del Real update_apic_tpr(cpu); 437c97d6d2cSSergio Andres Gomez Del Real current_cpu = cpu; 438c97d6d2cSSergio Andres Gomez Del Real 439c97d6d2cSSergio Andres Gomez Del Real ret = 0; 440c97d6d2cSSergio Andres Gomez Del Real switch (exit_reason) { 441c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_HLT: { 442c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 443c97d6d2cSSergio Andres Gomez Del Real if (!((cpu->interrupt_request & CPU_INTERRUPT_HARD) && 444967f4da2SRoman Bolshakov (env->eflags & IF_MASK)) 445c97d6d2cSSergio Andres Gomez Del Real && !(cpu->interrupt_request & CPU_INTERRUPT_NMI) && 446c97d6d2cSSergio Andres Gomez Del Real !(idtvec_info & VMCS_IDT_VEC_VALID)) { 447c97d6d2cSSergio Andres Gomez Del Real cpu->halted = 1; 448c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_HLT; 4493b9c59daSChen Zhang break; 450c97d6d2cSSergio Andres Gomez Del Real } 451c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 452c97d6d2cSSergio Andres Gomez Del Real break; 453c97d6d2cSSergio Andres Gomez Del Real } 454c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_MWAIT: { 455c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 456c97d6d2cSSergio Andres Gomez Del Real break; 457c97d6d2cSSergio Andres Gomez Del Real } 458fbafbb6dSCameron Esfahani /* Need to check if MMIO or unmapped fault */ 459c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EPT_FAULT: 460c97d6d2cSSergio Andres Gomez Del Real { 461c97d6d2cSSergio Andres Gomez Del Real hvf_slot *slot; 462b533450eSAlexander Graf uint64_t gpa = rvmcs(cpu->hvf->fd, VMCS_GUEST_PHYSICAL_ADDRESS); 463c97d6d2cSSergio Andres Gomez Del Real 464c97d6d2cSSergio Andres Gomez Del Real if (((idtvec_info & VMCS_IDT_VEC_VALID) == 0) && 465c97d6d2cSSergio Andres Gomez Del Real ((exit_qual & EXIT_QUAL_NMIUDTI) != 0)) { 466c97d6d2cSSergio Andres Gomez Del Real vmx_set_nmi_blocking(cpu); 467c97d6d2cSSergio Andres Gomez Del Real } 468c97d6d2cSSergio Andres Gomez Del Real 469fbafbb6dSCameron Esfahani slot = hvf_find_overlap_slot(gpa, 1); 470c97d6d2cSSergio Andres Gomez Del Real /* mmio */ 471babfa20cSSergio Andres Gomez Del Real if (ept_emulation_fault(slot, gpa, exit_qual)) { 472c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 473c97d6d2cSSergio Andres Gomez Del Real 474c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 475c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 476c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 477c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 478c97d6d2cSSergio Andres Gomez Del Real break; 479c97d6d2cSSergio Andres Gomez Del Real } 480c97d6d2cSSergio Andres Gomez Del Real break; 481c97d6d2cSSergio Andres Gomez Del Real } 482c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INOUT: 483c97d6d2cSSergio Andres Gomez Del Real { 484c97d6d2cSSergio Andres Gomez Del Real uint32_t in = (exit_qual & 8) != 0; 485c97d6d2cSSergio Andres Gomez Del Real uint32_t size = (exit_qual & 7) + 1; 486c97d6d2cSSergio Andres Gomez Del Real uint32_t string = (exit_qual & 16) != 0; 487c97d6d2cSSergio Andres Gomez Del Real uint32_t port = exit_qual >> 16; 488c97d6d2cSSergio Andres Gomez Del Real /*uint32_t rep = (exit_qual & 0x20) != 0;*/ 489c97d6d2cSSergio Andres Gomez Del Real 490c97d6d2cSSergio Andres Gomez Del Real if (!string && in) { 491c97d6d2cSSergio Andres Gomez Del Real uint64_t val = 0; 492c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 493c97d6d2cSSergio Andres Gomez Del Real hvf_handle_io(env, port, &val, 0, size, 1); 494c97d6d2cSSergio Andres Gomez Del Real if (size == 1) { 495c97d6d2cSSergio Andres Gomez Del Real AL(env) = val; 496c97d6d2cSSergio Andres Gomez Del Real } else if (size == 2) { 497c97d6d2cSSergio Andres Gomez Del Real AX(env) = val; 498c97d6d2cSSergio Andres Gomez Del Real } else if (size == 4) { 499c97d6d2cSSergio Andres Gomez Del Real RAX(env) = (uint32_t)val; 500c97d6d2cSSergio Andres Gomez Del Real } else { 501da20f5cdSPaolo Bonzini RAX(env) = (uint64_t)val; 502c97d6d2cSSergio Andres Gomez Del Real } 5035d32173fSRoman Bolshakov env->eip += ins_len; 504c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 505c97d6d2cSSergio Andres Gomez Del Real break; 506c97d6d2cSSergio Andres Gomez Del Real } else if (!string && !in) { 507b533450eSAlexander Graf RAX(env) = rreg(cpu->hvf->fd, HV_X86_RAX); 508c97d6d2cSSergio Andres Gomez Del Real hvf_handle_io(env, port, &RAX(env), 1, size, 1); 509c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 510c97d6d2cSSergio Andres Gomez Del Real break; 511c97d6d2cSSergio Andres Gomez Del Real } 512c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 513c97d6d2cSSergio Andres Gomez Del Real 514c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 515c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 516e62963bfSPaolo Bonzini assert(ins_len == decode.len); 517c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 518c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 519c97d6d2cSSergio Andres Gomez Del Real 520c97d6d2cSSergio Andres Gomez Del Real break; 521c97d6d2cSSergio Andres Gomez Del Real } 522c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CPUID: { 523b533450eSAlexander Graf uint32_t rax = (uint32_t)rreg(cpu->hvf->fd, HV_X86_RAX); 524b533450eSAlexander Graf uint32_t rbx = (uint32_t)rreg(cpu->hvf->fd, HV_X86_RBX); 525b533450eSAlexander Graf uint32_t rcx = (uint32_t)rreg(cpu->hvf->fd, HV_X86_RCX); 526b533450eSAlexander Graf uint32_t rdx = (uint32_t)rreg(cpu->hvf->fd, HV_X86_RDX); 527c97d6d2cSSergio Andres Gomez Del Real 528106f91d5SAlexander Graf if (rax == 1) { 529106f91d5SAlexander Graf /* CPUID1.ecx.OSXSAVE needs to know CR4 */ 530b533450eSAlexander Graf env->cr[4] = rvmcs(cpu->hvf->fd, VMCS_GUEST_CR4); 531106f91d5SAlexander Graf } 5323b502b0eSVladislav Yaroshchuk hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx); 533c97d6d2cSSergio Andres Gomez Del Real 534b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_RAX, rax); 535b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_RBX, rbx); 536b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_RCX, rcx); 537b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_RDX, rdx); 538c97d6d2cSSergio Andres Gomez Del Real 539c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 540c97d6d2cSSergio Andres Gomez Del Real break; 541c97d6d2cSSergio Andres Gomez Del Real } 542c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_XSETBV: { 543c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 544c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 545b533450eSAlexander Graf uint32_t eax = (uint32_t)rreg(cpu->hvf->fd, HV_X86_RAX); 546b533450eSAlexander Graf uint32_t ecx = (uint32_t)rreg(cpu->hvf->fd, HV_X86_RCX); 547b533450eSAlexander Graf uint32_t edx = (uint32_t)rreg(cpu->hvf->fd, HV_X86_RDX); 548c97d6d2cSSergio Andres Gomez Del Real 549c97d6d2cSSergio Andres Gomez Del Real if (ecx) { 550c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 551c97d6d2cSSergio Andres Gomez Del Real break; 552c97d6d2cSSergio Andres Gomez Del Real } 553c97d6d2cSSergio Andres Gomez Del Real env->xcr0 = ((uint64_t)edx << 32) | eax; 554b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_XCR0, env->xcr0 | 1); 555c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 556c97d6d2cSSergio Andres Gomez Del Real break; 557c97d6d2cSSergio Andres Gomez Del Real } 558c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INTR_WINDOW: 559c97d6d2cSSergio Andres Gomez Del Real vmx_clear_int_window_exiting(cpu); 560c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 561c97d6d2cSSergio Andres Gomez Del Real break; 562c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_NMI_WINDOW: 563c97d6d2cSSergio Andres Gomez Del Real vmx_clear_nmi_window_exiting(cpu); 564c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 565c97d6d2cSSergio Andres Gomez Del Real break; 566c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EXT_INTR: 567c97d6d2cSSergio Andres Gomez Del Real /* force exit and allow io handling */ 568c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 569c97d6d2cSSergio Andres Gomez Del Real break; 570c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDMSR: 571c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_WRMSR: 572c97d6d2cSSergio Andres Gomez Del Real { 573c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 574c97d6d2cSSergio Andres Gomez Del Real if (exit_reason == EXIT_REASON_RDMSR) { 575c97d6d2cSSergio Andres Gomez Del Real simulate_rdmsr(cpu); 576c97d6d2cSSergio Andres Gomez Del Real } else { 577c97d6d2cSSergio Andres Gomez Del Real simulate_wrmsr(cpu); 578c97d6d2cSSergio Andres Gomez Del Real } 5795d32173fSRoman Bolshakov env->eip += ins_len; 580c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 581c97d6d2cSSergio Andres Gomez Del Real break; 582c97d6d2cSSergio Andres Gomez Del Real } 583c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CR_ACCESS: { 584c97d6d2cSSergio Andres Gomez Del Real int cr; 585c97d6d2cSSergio Andres Gomez Del Real int reg; 586c97d6d2cSSergio Andres Gomez Del Real 587c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 588c97d6d2cSSergio Andres Gomez Del Real cr = exit_qual & 15; 589c97d6d2cSSergio Andres Gomez Del Real reg = (exit_qual >> 8) & 15; 590c97d6d2cSSergio Andres Gomez Del Real 591c97d6d2cSSergio Andres Gomez Del Real switch (cr) { 592c97d6d2cSSergio Andres Gomez Del Real case 0x0: { 593b533450eSAlexander Graf macvm_set_cr0(cpu->hvf->fd, RRX(env, reg)); 594c97d6d2cSSergio Andres Gomez Del Real break; 595c97d6d2cSSergio Andres Gomez Del Real } 596c97d6d2cSSergio Andres Gomez Del Real case 4: { 597b533450eSAlexander Graf macvm_set_cr4(cpu->hvf->fd, RRX(env, reg)); 598c97d6d2cSSergio Andres Gomez Del Real break; 599c97d6d2cSSergio Andres Gomez Del Real } 600c97d6d2cSSergio Andres Gomez Del Real case 8: { 601c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 602c97d6d2cSSergio Andres Gomez Del Real if (exit_qual & 0x10) { 603c97d6d2cSSergio Andres Gomez Del Real RRX(env, reg) = cpu_get_apic_tpr(x86_cpu->apic_state); 604c97d6d2cSSergio Andres Gomez Del Real } else { 605c97d6d2cSSergio Andres Gomez Del Real int tpr = RRX(env, reg); 606c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr); 607c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 608c97d6d2cSSergio Andres Gomez Del Real } 609c97d6d2cSSergio Andres Gomez Del Real break; 610c97d6d2cSSergio Andres Gomez Del Real } 611c97d6d2cSSergio Andres Gomez Del Real default: 6122d9178d9SLaurent Vivier error_report("Unrecognized CR %d", cr); 613c97d6d2cSSergio Andres Gomez Del Real abort(); 614c97d6d2cSSergio Andres Gomez Del Real } 6155d32173fSRoman Bolshakov env->eip += ins_len; 616c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 617c97d6d2cSSergio Andres Gomez Del Real break; 618c97d6d2cSSergio Andres Gomez Del Real } 619c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_APIC_ACCESS: { /* TODO */ 620c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 621c97d6d2cSSergio Andres Gomez Del Real 622c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 623c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 624c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 625c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 626c97d6d2cSSergio Andres Gomez Del Real break; 627c97d6d2cSSergio Andres Gomez Del Real } 628c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TPR: { 629c97d6d2cSSergio Andres Gomez Del Real ret = 1; 630c97d6d2cSSergio Andres Gomez Del Real break; 631c97d6d2cSSergio Andres Gomez Del Real } 632c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TASK_SWITCH: { 633b533450eSAlexander Graf uint64_t vinfo = rvmcs(cpu->hvf->fd, VMCS_IDT_VECTORING_INFO); 634c97d6d2cSSergio Andres Gomez Del Real x68_segment_selector sel = {.sel = exit_qual & 0xffff}; 635c97d6d2cSSergio Andres Gomez Del Real vmx_handle_task_switch(cpu, sel, (exit_qual >> 30) & 0x3, 636c97d6d2cSSergio Andres Gomez Del Real vinfo & VMCS_INTR_VALID, vinfo & VECTORING_INFO_VECTOR_MASK, vinfo 637c97d6d2cSSergio Andres Gomez Del Real & VMCS_INTR_T_MASK); 638c97d6d2cSSergio Andres Gomez Del Real break; 639c97d6d2cSSergio Andres Gomez Del Real } 640c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TRIPLE_FAULT: { 641c97d6d2cSSergio Andres Gomez Del Real qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 642c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 643c97d6d2cSSergio Andres Gomez Del Real break; 644c97d6d2cSSergio Andres Gomez Del Real } 645c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDPMC: 646b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_RAX, 0); 647b533450eSAlexander Graf wreg(cpu->hvf->fd, HV_X86_RDX, 0); 648c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 649c97d6d2cSSergio Andres Gomez Del Real break; 650c97d6d2cSSergio Andres Gomez Del Real case VMX_REASON_VMCALL: 651fd13f23bSLiran Alon env->exception_nr = EXCP0D_GPF; 652fd13f23bSLiran Alon env->exception_injected = 1; 6533010460fSSergio Andres Gomez Del Real env->has_error_code = true; 6543010460fSSergio Andres Gomez Del Real env->error_code = 0; 655c97d6d2cSSergio Andres Gomez Del Real break; 656c97d6d2cSSergio Andres Gomez Del Real default: 6572d9178d9SLaurent Vivier error_report("%llx: unhandled exit %llx", rip, exit_reason); 658c97d6d2cSSergio Andres Gomez Del Real } 659c97d6d2cSSergio Andres Gomez Del Real } while (ret == 0); 660c97d6d2cSSergio Andres Gomez Del Real 661c97d6d2cSSergio Andres Gomez Del Real return ret; 662c97d6d2cSSergio Andres Gomez Del Real } 663