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/error-report.h" 515df022cfSPeter Maydell #include "qemu/memalign.h" 529c267239SPhil Dennis-Jordan #include "qapi/error.h" 539c267239SPhil Dennis-Jordan #include "migration/blocker.h" 54c97d6d2cSSergio Andres Gomez Del Real 5532cad1ffSPhilippe Mathieu-Daudé #include "system/hvf.h" 5632cad1ffSPhilippe Mathieu-Daudé #include "system/hvf_int.h" 5732cad1ffSPhilippe Mathieu-Daudé #include "system/runstate.h" 5832cad1ffSPhilippe Mathieu-Daudé #include "system/cpus.h" 59c97d6d2cSSergio Andres Gomez Del Real #include "hvf-i386.h" 6069e0a03cSPaolo Bonzini #include "vmcs.h" 6169e0a03cSPaolo Bonzini #include "vmx.h" 6269e0a03cSPaolo Bonzini #include "x86.h" 6369e0a03cSPaolo Bonzini #include "x86_descr.h" 64*dbccd48dSWei Liu #include "x86_flags.h" 6569e0a03cSPaolo Bonzini #include "x86_mmu.h" 6669e0a03cSPaolo Bonzini #include "x86_decode.h" 6769e0a03cSPaolo Bonzini #include "x86_emu.h" 6869e0a03cSPaolo Bonzini #include "x86_task.h" 6969e0a03cSPaolo Bonzini #include "x86hvf.h" 70c97d6d2cSSergio Andres Gomez Del Real 71c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv.h> 72c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv_vmx.h> 733b502b0eSVladislav Yaroshchuk #include <sys/sysctl.h> 74c97d6d2cSSergio Andres Gomez Del Real 75c97d6d2cSSergio Andres Gomez Del Real #include "hw/i386/apic_internal.h" 76c97d6d2cSSergio Andres Gomez Del Real #include "qemu/main-loop.h" 77940e43aaSClaudio Fontana #include "qemu/accel.h" 78c97d6d2cSSergio Andres Gomez Del Real #include "target/i386/cpu.h" 79c97d6d2cSSergio Andres Gomez Del Real 809c267239SPhil Dennis-Jordan static Error *invtsc_mig_blocker; 819c267239SPhil Dennis-Jordan 82c97d6d2cSSergio Andres Gomez Del Real void vmx_update_tpr(CPUState *cpu) 83c97d6d2cSSergio Andres Gomez Del Real { 84c97d6d2cSSergio Andres Gomez Del Real /* TODO: need integrate APIC handling */ 85c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 86c97d6d2cSSergio Andres Gomez Del Real int tpr = cpu_get_apic_tpr(x86_cpu->apic_state) << 4; 87c97d6d2cSSergio Andres Gomez Del Real int irr = apic_get_highest_priority_irr(x86_cpu->apic_state); 88c97d6d2cSSergio Andres Gomez Del Real 893b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_TPR, tpr); 90c97d6d2cSSergio Andres Gomez Del Real if (irr == -1) { 913b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_TPR_THRESHOLD, 0); 92c97d6d2cSSergio Andres Gomez Del Real } else { 933b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_TPR_THRESHOLD, (irr > tpr) ? tpr >> 4 : 94c97d6d2cSSergio Andres Gomez Del Real irr >> 4); 95c97d6d2cSSergio Andres Gomez Del Real } 96c97d6d2cSSergio Andres Gomez Del Real } 97c97d6d2cSSergio Andres Gomez Del Real 98583ae161SRoman Bolshakov static void update_apic_tpr(CPUState *cpu) 99c97d6d2cSSergio Andres Gomez Del Real { 100c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 1013b295bcbSPhilippe Mathieu-Daudé int tpr = rreg(cpu->accel->fd, HV_X86_TPR) >> 4; 102c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr); 103c97d6d2cSSergio Andres Gomez Del Real } 104c97d6d2cSSergio Andres Gomez Del Real 105c97d6d2cSSergio Andres Gomez Del Real #define VECTORING_INFO_VECTOR_MASK 0xff 106c97d6d2cSSergio Andres Gomez Del Real 107bc4fa8c3SWei Liu void hvf_handle_io(CPUState *env, uint16_t port, void *buffer, 108c97d6d2cSSergio Andres Gomez Del Real int direction, int size, int count) 109c97d6d2cSSergio Andres Gomez Del Real { 110c97d6d2cSSergio Andres Gomez Del Real int i; 111c97d6d2cSSergio Andres Gomez Del Real uint8_t *ptr = buffer; 112c97d6d2cSSergio Andres Gomez Del Real 113c97d6d2cSSergio Andres Gomez Del Real for (i = 0; i < count; i++) { 114c97d6d2cSSergio Andres Gomez Del Real address_space_rw(&address_space_io, port, MEMTXATTRS_UNSPECIFIED, 115c97d6d2cSSergio Andres Gomez Del Real ptr, size, 116c97d6d2cSSergio Andres Gomez Del Real direction); 117c97d6d2cSSergio Andres Gomez Del Real ptr += size; 118c97d6d2cSSergio Andres Gomez Del Real } 119c97d6d2cSSergio Andres Gomez Del Real } 120c97d6d2cSSergio Andres Gomez Del Real 121ff2de166SPaolo Bonzini static bool ept_emulation_fault(hvf_slot *slot, uint64_t gpa, uint64_t ept_qual) 122c97d6d2cSSergio Andres Gomez Del Real { 123c97d6d2cSSergio Andres Gomez Del Real int read, write; 124c97d6d2cSSergio Andres Gomez Del Real 125c97d6d2cSSergio Andres Gomez Del Real /* EPT fault on an instruction fetch doesn't make sense here */ 126c97d6d2cSSergio Andres Gomez Del Real if (ept_qual & EPT_VIOLATION_INST_FETCH) { 127c97d6d2cSSergio Andres Gomez Del Real return false; 128c97d6d2cSSergio Andres Gomez Del Real } 129c97d6d2cSSergio Andres Gomez Del Real 130c97d6d2cSSergio Andres Gomez Del Real /* EPT fault must be a read fault or a write fault */ 131c97d6d2cSSergio Andres Gomez Del Real read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0; 132c97d6d2cSSergio Andres Gomez Del Real write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0; 133c97d6d2cSSergio Andres Gomez Del Real if ((read | write) == 0) { 134c97d6d2cSSergio Andres Gomez Del Real return false; 135c97d6d2cSSergio Andres Gomez Del Real } 136c97d6d2cSSergio Andres Gomez Del Real 137babfa20cSSergio Andres Gomez Del Real if (write && slot) { 138babfa20cSSergio Andres Gomez Del Real if (slot->flags & HVF_SLOT_LOG) { 1393e2c6727SPhil Dennis-Jordan uint64_t dirty_page_start = gpa & ~(TARGET_PAGE_SIZE - 1u); 140babfa20cSSergio Andres Gomez Del Real memory_region_set_dirty(slot->region, gpa - slot->start, 1); 1413e2c6727SPhil Dennis-Jordan hv_vm_protect(dirty_page_start, TARGET_PAGE_SIZE, 1423e2c6727SPhil Dennis-Jordan HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC); 143babfa20cSSergio Andres Gomez Del Real } 144babfa20cSSergio Andres Gomez Del Real } 145babfa20cSSergio Andres Gomez Del Real 146c97d6d2cSSergio Andres Gomez Del Real /* 147c97d6d2cSSergio Andres Gomez Del Real * The EPT violation must have been caused by accessing a 148c97d6d2cSSergio Andres Gomez Del Real * guest-physical address that is a translation of a guest-linear 149c97d6d2cSSergio Andres Gomez Del Real * address. 150c97d6d2cSSergio Andres Gomez Del Real */ 151c97d6d2cSSergio Andres Gomez Del Real if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 || 152c97d6d2cSSergio Andres Gomez Del Real (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) { 153c97d6d2cSSergio Andres Gomez Del Real return false; 154c97d6d2cSSergio Andres Gomez Del Real } 155c97d6d2cSSergio Andres Gomez Del Real 156fbafbb6dSCameron Esfahani if (!slot) { 157fbafbb6dSCameron Esfahani return true; 158fbafbb6dSCameron Esfahani } 159fbafbb6dSCameron Esfahani if (!memory_region_is_ram(slot->region) && 160fbafbb6dSCameron Esfahani !(read && memory_region_is_romd(slot->region))) { 161fbafbb6dSCameron Esfahani return true; 162fbafbb6dSCameron Esfahani } 163fbafbb6dSCameron Esfahani return false; 164babfa20cSSergio Andres Gomez Del Real } 165babfa20cSSergio Andres Gomez Del Real 166cfe58455SAlexander Graf void hvf_arch_vcpu_destroy(CPUState *cpu) 167c97d6d2cSSergio Andres Gomez Del Real { 168fe76b09cSRoman Bolshakov X86CPU *x86_cpu = X86_CPU(cpu); 169fe76b09cSRoman Bolshakov CPUX86State *env = &x86_cpu->env; 170fe76b09cSRoman Bolshakov 171fe76b09cSRoman Bolshakov g_free(env->hvf_mmio_buf); 172c97d6d2cSSergio Andres Gomez Del Real } 173c97d6d2cSSergio Andres Gomez Del Real 1743b502b0eSVladislav Yaroshchuk static void init_tsc_freq(CPUX86State *env) 1753b502b0eSVladislav Yaroshchuk { 1763b502b0eSVladislav Yaroshchuk size_t length; 1773b502b0eSVladislav Yaroshchuk uint64_t tsc_freq; 1783b502b0eSVladislav Yaroshchuk 1793b502b0eSVladislav Yaroshchuk if (env->tsc_khz != 0) { 1803b502b0eSVladislav Yaroshchuk return; 1813b502b0eSVladislav Yaroshchuk } 1823b502b0eSVladislav Yaroshchuk 1833b502b0eSVladislav Yaroshchuk length = sizeof(uint64_t); 1843b502b0eSVladislav Yaroshchuk if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) { 1853b502b0eSVladislav Yaroshchuk return; 1863b502b0eSVladislav Yaroshchuk } 1873b502b0eSVladislav Yaroshchuk env->tsc_khz = tsc_freq / 1000; /* Hz to KHz */ 1883b502b0eSVladislav Yaroshchuk } 1893b502b0eSVladislav Yaroshchuk 1903b502b0eSVladislav Yaroshchuk static void init_apic_bus_freq(CPUX86State *env) 1913b502b0eSVladislav Yaroshchuk { 1923b502b0eSVladislav Yaroshchuk size_t length; 1933b502b0eSVladislav Yaroshchuk uint64_t bus_freq; 1943b502b0eSVladislav Yaroshchuk 1953b502b0eSVladislav Yaroshchuk if (env->apic_bus_freq != 0) { 1963b502b0eSVladislav Yaroshchuk return; 1973b502b0eSVladislav Yaroshchuk } 1983b502b0eSVladislav Yaroshchuk 1993b502b0eSVladislav Yaroshchuk length = sizeof(uint64_t); 2003b502b0eSVladislav Yaroshchuk if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) { 2013b502b0eSVladislav Yaroshchuk return; 2023b502b0eSVladislav Yaroshchuk } 2033b502b0eSVladislav Yaroshchuk env->apic_bus_freq = bus_freq; 2043b502b0eSVladislav Yaroshchuk } 2053b502b0eSVladislav Yaroshchuk 2063b502b0eSVladislav Yaroshchuk static inline bool tsc_is_known(CPUX86State *env) 2073b502b0eSVladislav Yaroshchuk { 2083b502b0eSVladislav Yaroshchuk return env->tsc_khz != 0; 2093b502b0eSVladislav Yaroshchuk } 2103b502b0eSVladislav Yaroshchuk 2113b502b0eSVladislav Yaroshchuk static inline bool apic_bus_freq_is_known(CPUX86State *env) 2123b502b0eSVladislav Yaroshchuk { 2133b502b0eSVladislav Yaroshchuk return env->apic_bus_freq != 0; 2143b502b0eSVladislav Yaroshchuk } 2153b502b0eSVladislav Yaroshchuk 216a1477da3SAlexander Graf void hvf_kick_vcpu_thread(CPUState *cpu) 217a1477da3SAlexander Graf { 218a1477da3SAlexander Graf cpus_kick_thread(cpu); 219bf9bf230SPhil Dennis-Jordan hv_vcpu_interrupt(&cpu->accel->fd, 1); 220a1477da3SAlexander Graf } 221a1477da3SAlexander Graf 222ce7f5b1cSAlexander Graf int hvf_arch_init(void) 223ce7f5b1cSAlexander Graf { 224ce7f5b1cSAlexander Graf return 0; 225ce7f5b1cSAlexander Graf } 226ce7f5b1cSAlexander Graf 2272c760670SDanny Canter hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) 2282c760670SDanny Canter { 2292c760670SDanny Canter return hv_vm_create(HV_VM_DEFAULT); 2302c760670SDanny Canter } 2312c760670SDanny Canter 232cfe58455SAlexander Graf int hvf_arch_init_vcpu(CPUState *cpu) 233c97d6d2cSSergio Andres Gomez Del Real { 234c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86cpu = X86_CPU(cpu); 235c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86cpu->env; 2369c267239SPhil Dennis-Jordan Error *local_err = NULL; 2379c267239SPhil Dennis-Jordan int r; 238d8cf2c29SCameron Esfahani uint64_t reqCap; 239c97d6d2cSSergio Andres Gomez Del Real 240c97d6d2cSSergio Andres Gomez Del Real init_emu(); 241c97d6d2cSSergio Andres Gomez Del Real init_decoder(); 242c97d6d2cSSergio Andres Gomez Del Real 2433a75ba65SPhil Dennis-Jordan if (hvf_state->hvf_caps == NULL) { 244c97d6d2cSSergio Andres Gomez Del Real hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1); 2453a75ba65SPhil Dennis-Jordan } 246fe76b09cSRoman Bolshakov env->hvf_mmio_buf = g_new(char, 4096); 247c97d6d2cSSergio Andres Gomez Del Real 2483b502b0eSVladislav Yaroshchuk if (x86cpu->vmware_cpuid_freq) { 2493b502b0eSVladislav Yaroshchuk init_tsc_freq(env); 2503b502b0eSVladislav Yaroshchuk init_apic_bus_freq(env); 2513b502b0eSVladislav Yaroshchuk 2523b502b0eSVladislav Yaroshchuk if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) { 2533b502b0eSVladislav Yaroshchuk error_report("vmware-cpuid-freq: feature couldn't be enabled"); 2543b502b0eSVladislav Yaroshchuk } 2553b502b0eSVladislav Yaroshchuk } 2563b502b0eSVladislav Yaroshchuk 2579c267239SPhil Dennis-Jordan if ((env->features[FEAT_8000_0007_EDX] & CPUID_APM_INVTSC) && 2589c267239SPhil Dennis-Jordan invtsc_mig_blocker == NULL) { 2599c267239SPhil Dennis-Jordan error_setg(&invtsc_mig_blocker, 2609c267239SPhil Dennis-Jordan "State blocked by non-migratable CPU device (invtsc flag)"); 2619c267239SPhil Dennis-Jordan r = migrate_add_blocker(&invtsc_mig_blocker, &local_err); 2629c267239SPhil Dennis-Jordan if (r < 0) { 2639c267239SPhil Dennis-Jordan error_report_err(local_err); 2649c267239SPhil Dennis-Jordan return r; 2659c267239SPhil Dennis-Jordan } 2669c267239SPhil Dennis-Jordan } 2679c267239SPhil Dennis-Jordan 2689c267239SPhil Dennis-Jordan 269c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PINBASED, 270c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_pinbased)) { 271c97d6d2cSSergio Andres Gomez Del Real abort(); 272c97d6d2cSSergio Andres Gomez Del Real } 273c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED, 274c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased)) { 275c97d6d2cSSergio Andres Gomez Del Real abort(); 276c97d6d2cSSergio Andres Gomez Del Real } 277c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2, 278c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased2)) { 279c97d6d2cSSergio Andres Gomez Del Real abort(); 280c97d6d2cSSergio Andres Gomez Del Real } 281c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_ENTRY, 282c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_entry)) { 283c97d6d2cSSergio Andres Gomez Del Real abort(); 284c97d6d2cSSergio Andres Gomez Del Real } 285c97d6d2cSSergio Andres Gomez Del Real 286c97d6d2cSSergio Andres Gomez Del Real /* set VMCS control fields */ 2873b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_PIN_BASED_CTLS, 288c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_pinbased, 289c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_EXTINT | 290c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_NMI | 291c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_VNMI)); 2923b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_PRI_PROC_BASED_CTLS, 293c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased, 294c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_HLT | 295c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_MWAIT | 296c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TSC_OFFSET | 297c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TPR_SHADOW) | 298c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_SEC_CONTROL); 299d8cf2c29SCameron Esfahani 300d8cf2c29SCameron Esfahani reqCap = VMCS_PRI_PROC_BASED2_CTLS_APIC_ACCESSES; 301d8cf2c29SCameron Esfahani 302d8cf2c29SCameron Esfahani /* Is RDTSCP support in CPUID? If so, enable it in the VMCS. */ 303d8cf2c29SCameron Esfahani if (hvf_get_supported_cpuid(0x80000001, 0, R_EDX) & CPUID_EXT2_RDTSCP) { 304d8cf2c29SCameron Esfahani reqCap |= VMCS_PRI_PROC_BASED2_CTLS_RDTSCP; 305d8cf2c29SCameron Esfahani } 306d8cf2c29SCameron Esfahani 3073b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_SEC_PROC_BASED_CTLS, 308d8cf2c29SCameron Esfahani cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased2, reqCap)); 309c97d6d2cSSergio Andres Gomez Del Real 3103b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_ENTRY_CTLS, 3113b295bcbSPhilippe Mathieu-Daudé cap2ctrl(hvf_state->hvf_caps->vmx_cap_entry, 0)); 3123b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_EXCEPTION_BITMAP, 0); /* Double fault */ 313c97d6d2cSSergio Andres Gomez Del Real 3143b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_TPR_THRESHOLD, 0); 315c97d6d2cSSergio Andres Gomez Del Real 316c97d6d2cSSergio Andres Gomez Del Real x86cpu = X86_CPU(cpu); 317c0198c5fSDavid Edmondson x86cpu->env.xsave_buf_len = 4096; 318c0198c5fSDavid Edmondson x86cpu->env.xsave_buf = qemu_memalign(4096, x86cpu->env.xsave_buf_len); 319c97d6d2cSSergio Andres Gomez Del Real 320fea45008SDavid Edmondson /* 321fea45008SDavid Edmondson * The allocated storage must be large enough for all of the 322fea45008SDavid Edmondson * possible XSAVE state components. 323fea45008SDavid Edmondson */ 324fea45008SDavid Edmondson assert(hvf_get_supported_cpuid(0xd, 0, R_ECX) <= x86cpu->env.xsave_buf_len); 325fea45008SDavid Edmondson 3263b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_STAR, 1); 3273b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_LSTAR, 1); 3283b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_CSTAR, 1); 3293b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_FMASK, 1); 3303b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_FSBASE, 1); 3313b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_GSBASE, 1); 3323b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_KERNELGSBASE, 1); 3333b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_TSC_AUX, 1); 3343b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_TSC, 1); 3353b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_SYSENTER_CS, 1); 3363b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_SYSENTER_EIP, 1); 3373b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_SYSENTER_ESP, 1); 338c97d6d2cSSergio Andres Gomez Del Real 339c97d6d2cSSergio Andres Gomez Del Real return 0; 340c97d6d2cSSergio Andres Gomez Del Real } 341c97d6d2cSSergio Andres Gomez Del Real 342b7394c83SSergio Andres Gomez Del Real static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_info) 343b7394c83SSergio Andres Gomez Del Real { 344b7394c83SSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 345b7394c83SSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 346b7394c83SSergio Andres Gomez Del Real 347fd13f23bSLiran Alon env->exception_nr = -1; 348fd13f23bSLiran Alon env->exception_pending = 0; 349fd13f23bSLiran Alon env->exception_injected = 0; 350b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = -1; 351b7394c83SSergio Andres Gomez Del Real env->nmi_injected = false; 35264bef038SCameron Esfahani env->ins_len = 0; 35364bef038SCameron Esfahani env->has_error_code = false; 354b7394c83SSergio Andres Gomez Del Real if (idtvec_info & VMCS_IDT_VEC_VALID) { 355b7394c83SSergio Andres Gomez Del Real switch (idtvec_info & VMCS_IDT_VEC_TYPE) { 356b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWINTR: 357b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWINTR: 358b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = idtvec_info & VMCS_IDT_VEC_VECNUM; 359b7394c83SSergio Andres Gomez Del Real break; 360b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_NMI: 361b7394c83SSergio Andres Gomez Del Real env->nmi_injected = true; 362b7394c83SSergio Andres Gomez Del Real break; 363b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWEXCEPTION: 364b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWEXCEPTION: 365fd13f23bSLiran Alon env->exception_nr = idtvec_info & VMCS_IDT_VEC_VECNUM; 366fd13f23bSLiran Alon env->exception_injected = 1; 367b7394c83SSergio Andres Gomez Del Real break; 368b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_PRIV_SWEXCEPTION: 369b7394c83SSergio Andres Gomez Del Real default: 370b7394c83SSergio Andres Gomez Del Real abort(); 371b7394c83SSergio Andres Gomez Del Real } 372b7394c83SSergio Andres Gomez Del Real if ((idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWEXCEPTION || 373b7394c83SSergio Andres Gomez Del Real (idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWINTR) { 374b7394c83SSergio Andres Gomez Del Real env->ins_len = ins_len; 375b7394c83SSergio Andres Gomez Del Real } 37664bef038SCameron Esfahani if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) { 377b7394c83SSergio Andres Gomez Del Real env->has_error_code = true; 3783b295bcbSPhilippe Mathieu-Daudé env->error_code = rvmcs(cpu->accel->fd, VMCS_IDT_VECTORING_ERROR); 379b7394c83SSergio Andres Gomez Del Real } 380b7394c83SSergio Andres Gomez Del Real } 3813b295bcbSPhilippe Mathieu-Daudé if ((rvmcs(cpu->accel->fd, VMCS_GUEST_INTERRUPTIBILITY) & 382b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_NMI_BLOCKING)) { 383b7394c83SSergio Andres Gomez Del Real env->hflags2 |= HF2_NMI_MASK; 384b7394c83SSergio Andres Gomez Del Real } else { 385b7394c83SSergio Andres Gomez Del Real env->hflags2 &= ~HF2_NMI_MASK; 386b7394c83SSergio Andres Gomez Del Real } 3873b295bcbSPhilippe Mathieu-Daudé if (rvmcs(cpu->accel->fd, VMCS_GUEST_INTERRUPTIBILITY) & 388b7394c83SSergio Andres Gomez Del Real (VMCS_INTERRUPTIBILITY_STI_BLOCKING | 389b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) { 390b7394c83SSergio Andres Gomez Del Real env->hflags |= HF_INHIBIT_IRQ_MASK; 391b7394c83SSergio Andres Gomez Del Real } else { 392b7394c83SSergio Andres Gomez Del Real env->hflags &= ~HF_INHIBIT_IRQ_MASK; 393b7394c83SSergio Andres Gomez Del Real } 394b7394c83SSergio Andres Gomez Del Real } 395b7394c83SSergio Andres Gomez Del Real 3963b502b0eSVladislav Yaroshchuk static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, 3973b502b0eSVladislav Yaroshchuk uint32_t *eax, uint32_t *ebx, 3983b502b0eSVladislav Yaroshchuk uint32_t *ecx, uint32_t *edx) 3993b502b0eSVladislav Yaroshchuk { 4003b502b0eSVladislav Yaroshchuk /* 4013b502b0eSVladislav Yaroshchuk * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs, 4023b502b0eSVladislav Yaroshchuk * leafs 0x40000001-0x4000000F are filled with zeros 4033b502b0eSVladislav Yaroshchuk * Provides vmware-cpuid-freq support to hvf 4043b502b0eSVladislav Yaroshchuk * 4053b502b0eSVladislav Yaroshchuk * Note: leaf 0x40000000 not exposes HVF, 4063b502b0eSVladislav Yaroshchuk * leaving hypervisor signature empty 4073b502b0eSVladislav Yaroshchuk */ 4083b502b0eSVladislav Yaroshchuk 4093b502b0eSVladislav Yaroshchuk if (index < 0x40000000 || index > 0x40000010 || 4103b502b0eSVladislav Yaroshchuk !tsc_is_known(env) || !apic_bus_freq_is_known(env)) { 4113b502b0eSVladislav Yaroshchuk 4123b502b0eSVladislav Yaroshchuk cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx); 4133b502b0eSVladislav Yaroshchuk return; 4143b502b0eSVladislav Yaroshchuk } 4153b502b0eSVladislav Yaroshchuk 4163b502b0eSVladislav Yaroshchuk switch (index) { 4173b502b0eSVladislav Yaroshchuk case 0x40000000: 4183b502b0eSVladislav Yaroshchuk *eax = 0x40000010; /* Max available cpuid leaf */ 4193b502b0eSVladislav Yaroshchuk *ebx = 0; /* Leave signature empty */ 4203b502b0eSVladislav Yaroshchuk *ecx = 0; 4213b502b0eSVladislav Yaroshchuk *edx = 0; 4223b502b0eSVladislav Yaroshchuk break; 4233b502b0eSVladislav Yaroshchuk case 0x40000010: 4243b502b0eSVladislav Yaroshchuk *eax = env->tsc_khz; 4253b502b0eSVladislav Yaroshchuk *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */ 4263b502b0eSVladislav Yaroshchuk *ecx = 0; 4273b502b0eSVladislav Yaroshchuk *edx = 0; 4283b502b0eSVladislav Yaroshchuk break; 4293b502b0eSVladislav Yaroshchuk default: 4303b502b0eSVladislav Yaroshchuk *eax = 0; 4313b502b0eSVladislav Yaroshchuk *ebx = 0; 4323b502b0eSVladislav Yaroshchuk *ecx = 0; 4333b502b0eSVladislav Yaroshchuk *edx = 0; 4343b502b0eSVladislav Yaroshchuk break; 4353b502b0eSVladislav Yaroshchuk } 4363b502b0eSVladislav Yaroshchuk } 4373b502b0eSVladislav Yaroshchuk 438*dbccd48dSWei Liu void hvf_load_regs(CPUState *cs) 439*dbccd48dSWei Liu { 440*dbccd48dSWei Liu X86CPU *cpu = X86_CPU(cs); 441*dbccd48dSWei Liu CPUX86State *env = &cpu->env; 442*dbccd48dSWei Liu 443*dbccd48dSWei Liu int i = 0; 444*dbccd48dSWei Liu RRX(env, R_EAX) = rreg(cs->accel->fd, HV_X86_RAX); 445*dbccd48dSWei Liu RRX(env, R_EBX) = rreg(cs->accel->fd, HV_X86_RBX); 446*dbccd48dSWei Liu RRX(env, R_ECX) = rreg(cs->accel->fd, HV_X86_RCX); 447*dbccd48dSWei Liu RRX(env, R_EDX) = rreg(cs->accel->fd, HV_X86_RDX); 448*dbccd48dSWei Liu RRX(env, R_ESI) = rreg(cs->accel->fd, HV_X86_RSI); 449*dbccd48dSWei Liu RRX(env, R_EDI) = rreg(cs->accel->fd, HV_X86_RDI); 450*dbccd48dSWei Liu RRX(env, R_ESP) = rreg(cs->accel->fd, HV_X86_RSP); 451*dbccd48dSWei Liu RRX(env, R_EBP) = rreg(cs->accel->fd, HV_X86_RBP); 452*dbccd48dSWei Liu for (i = 8; i < 16; i++) { 453*dbccd48dSWei Liu RRX(env, i) = rreg(cs->accel->fd, HV_X86_RAX + i); 454*dbccd48dSWei Liu } 455*dbccd48dSWei Liu 456*dbccd48dSWei Liu env->eflags = rreg(cs->accel->fd, HV_X86_RFLAGS); 457*dbccd48dSWei Liu rflags_to_lflags(env); 458*dbccd48dSWei Liu env->eip = rreg(cs->accel->fd, HV_X86_RIP); 459*dbccd48dSWei Liu } 460*dbccd48dSWei Liu 461*dbccd48dSWei Liu void hvf_store_regs(CPUState *cs) 462*dbccd48dSWei Liu { 463*dbccd48dSWei Liu X86CPU *cpu = X86_CPU(cs); 464*dbccd48dSWei Liu CPUX86State *env = &cpu->env; 465*dbccd48dSWei Liu 466*dbccd48dSWei Liu int i = 0; 467*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RAX, RAX(env)); 468*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RBX, RBX(env)); 469*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RCX, RCX(env)); 470*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RDX, RDX(env)); 471*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RSI, RSI(env)); 472*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RDI, RDI(env)); 473*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RBP, RBP(env)); 474*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RSP, RSP(env)); 475*dbccd48dSWei Liu for (i = 8; i < 16; i++) { 476*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RAX + i, RRX(env, i)); 477*dbccd48dSWei Liu } 478*dbccd48dSWei Liu 479*dbccd48dSWei Liu lflags_to_rflags(env); 480*dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RFLAGS, env->eflags); 481*dbccd48dSWei Liu macvm_set_rip(cs, env->eip); 482*dbccd48dSWei Liu } 483*dbccd48dSWei Liu 484c97d6d2cSSergio Andres Gomez Del Real int hvf_vcpu_exec(CPUState *cpu) 485c97d6d2cSSergio Andres Gomez Del Real { 486c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 487c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 488c97d6d2cSSergio Andres Gomez Del Real int ret = 0; 489c97d6d2cSSergio Andres Gomez Del Real uint64_t rip = 0; 490c97d6d2cSSergio Andres Gomez Del Real 491c97d6d2cSSergio Andres Gomez Del Real if (hvf_process_events(cpu)) { 492c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT; 493c97d6d2cSSergio Andres Gomez Del Real } 494c97d6d2cSSergio Andres Gomez Del Real 495c97d6d2cSSergio Andres Gomez Del Real do { 496e6203636SPhilippe Mathieu-Daudé if (cpu->accel->dirty) { 497c97d6d2cSSergio Andres Gomez Del Real hvf_put_registers(cpu); 498e6203636SPhilippe Mathieu-Daudé cpu->accel->dirty = false; 499c97d6d2cSSergio Andres Gomez Del Real } 500c97d6d2cSSergio Andres Gomez Del Real 501b7394c83SSergio Andres Gomez Del Real if (hvf_inject_interrupts(cpu)) { 502b7394c83SSergio Andres Gomez Del Real return EXCP_INTERRUPT; 503b7394c83SSergio Andres Gomez Del Real } 504c97d6d2cSSergio Andres Gomez Del Real vmx_update_tpr(cpu); 505c97d6d2cSSergio Andres Gomez Del Real 506195801d7SStefan Hajnoczi bql_unlock(); 507c97d6d2cSSergio Andres Gomez Del Real if (!cpu_is_bsp(X86_CPU(cpu)) && cpu->halted) { 508195801d7SStefan Hajnoczi bql_lock(); 509c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT; 510c97d6d2cSSergio Andres Gomez Del Real } 511c97d6d2cSSergio Andres Gomez Del Real 512f64933c8SAkihiko Odaki hv_return_t r = hv_vcpu_run_until(cpu->accel->fd, HV_DEADLINE_FOREVER); 513c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(r); 514c97d6d2cSSergio Andres Gomez Del Real 515c97d6d2cSSergio Andres Gomez Del Real /* handle VMEXIT */ 5163b295bcbSPhilippe Mathieu-Daudé uint64_t exit_reason = rvmcs(cpu->accel->fd, VMCS_EXIT_REASON); 5173b295bcbSPhilippe Mathieu-Daudé uint64_t exit_qual = rvmcs(cpu->accel->fd, VMCS_EXIT_QUALIFICATION); 5183b295bcbSPhilippe Mathieu-Daudé uint32_t ins_len = (uint32_t)rvmcs(cpu->accel->fd, 519c97d6d2cSSergio Andres Gomez Del Real VMCS_EXIT_INSTRUCTION_LENGTH); 520b7394c83SSergio Andres Gomez Del Real 5213b295bcbSPhilippe Mathieu-Daudé uint64_t idtvec_info = rvmcs(cpu->accel->fd, VMCS_IDT_VECTORING_INFO); 522b7394c83SSergio Andres Gomez Del Real 523b7394c83SSergio Andres Gomez Del Real hvf_store_events(cpu, ins_len, idtvec_info); 5243b295bcbSPhilippe Mathieu-Daudé rip = rreg(cpu->accel->fd, HV_X86_RIP); 5253b295bcbSPhilippe Mathieu-Daudé env->eflags = rreg(cpu->accel->fd, HV_X86_RFLAGS); 526c97d6d2cSSergio Andres Gomez Del Real 527195801d7SStefan Hajnoczi bql_lock(); 528c97d6d2cSSergio Andres Gomez Del Real 529c97d6d2cSSergio Andres Gomez Del Real update_apic_tpr(cpu); 530c97d6d2cSSergio Andres Gomez Del Real current_cpu = cpu; 531c97d6d2cSSergio Andres Gomez Del Real 532c97d6d2cSSergio Andres Gomez Del Real ret = 0; 533c97d6d2cSSergio Andres Gomez Del Real switch (exit_reason) { 534c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_HLT: { 535c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 536c97d6d2cSSergio Andres Gomez Del Real if (!((cpu->interrupt_request & CPU_INTERRUPT_HARD) && 537967f4da2SRoman Bolshakov (env->eflags & IF_MASK)) 538c97d6d2cSSergio Andres Gomez Del Real && !(cpu->interrupt_request & CPU_INTERRUPT_NMI) && 539c97d6d2cSSergio Andres Gomez Del Real !(idtvec_info & VMCS_IDT_VEC_VALID)) { 540c97d6d2cSSergio Andres Gomez Del Real cpu->halted = 1; 541c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_HLT; 5423b9c59daSChen Zhang break; 543c97d6d2cSSergio Andres Gomez Del Real } 544c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 545c97d6d2cSSergio Andres Gomez Del Real break; 546c97d6d2cSSergio Andres Gomez Del Real } 547c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_MWAIT: { 548c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 549c97d6d2cSSergio Andres Gomez Del Real break; 550c97d6d2cSSergio Andres Gomez Del Real } 551fbafbb6dSCameron Esfahani /* Need to check if MMIO or unmapped fault */ 552c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EPT_FAULT: 553c97d6d2cSSergio Andres Gomez Del Real { 554c97d6d2cSSergio Andres Gomez Del Real hvf_slot *slot; 5553b295bcbSPhilippe Mathieu-Daudé uint64_t gpa = rvmcs(cpu->accel->fd, VMCS_GUEST_PHYSICAL_ADDRESS); 556c97d6d2cSSergio Andres Gomez Del Real 557c97d6d2cSSergio Andres Gomez Del Real if (((idtvec_info & VMCS_IDT_VEC_VALID) == 0) && 558c97d6d2cSSergio Andres Gomez Del Real ((exit_qual & EXIT_QUAL_NMIUDTI) != 0)) { 559c97d6d2cSSergio Andres Gomez Del Real vmx_set_nmi_blocking(cpu); 560c97d6d2cSSergio Andres Gomez Del Real } 561c97d6d2cSSergio Andres Gomez Del Real 562fbafbb6dSCameron Esfahani slot = hvf_find_overlap_slot(gpa, 1); 563c97d6d2cSSergio Andres Gomez Del Real /* mmio */ 564babfa20cSSergio Andres Gomez Del Real if (ept_emulation_fault(slot, gpa, exit_qual)) { 565c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 566c97d6d2cSSergio Andres Gomez Del Real 567*dbccd48dSWei Liu hvf_load_regs(cpu); 568c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 569c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 570*dbccd48dSWei Liu hvf_store_regs(cpu); 571c97d6d2cSSergio Andres Gomez Del Real break; 572c97d6d2cSSergio Andres Gomez Del Real } 573c97d6d2cSSergio Andres Gomez Del Real break; 574c97d6d2cSSergio Andres Gomez Del Real } 575c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INOUT: 576c97d6d2cSSergio Andres Gomez Del Real { 577c97d6d2cSSergio Andres Gomez Del Real uint32_t in = (exit_qual & 8) != 0; 578c97d6d2cSSergio Andres Gomez Del Real uint32_t size = (exit_qual & 7) + 1; 579c97d6d2cSSergio Andres Gomez Del Real uint32_t string = (exit_qual & 16) != 0; 580c97d6d2cSSergio Andres Gomez Del Real uint32_t port = exit_qual >> 16; 581c97d6d2cSSergio Andres Gomez Del Real /*uint32_t rep = (exit_qual & 0x20) != 0;*/ 582c97d6d2cSSergio Andres Gomez Del Real 583c97d6d2cSSergio Andres Gomez Del Real if (!string && in) { 584c97d6d2cSSergio Andres Gomez Del Real uint64_t val = 0; 585*dbccd48dSWei Liu hvf_load_regs(cpu); 586bc4fa8c3SWei Liu hvf_handle_io(env_cpu(env), port, &val, 0, size, 1); 587c97d6d2cSSergio Andres Gomez Del Real if (size == 1) { 588c97d6d2cSSergio Andres Gomez Del Real AL(env) = val; 589c97d6d2cSSergio Andres Gomez Del Real } else if (size == 2) { 590c97d6d2cSSergio Andres Gomez Del Real AX(env) = val; 591c97d6d2cSSergio Andres Gomez Del Real } else if (size == 4) { 592c97d6d2cSSergio Andres Gomez Del Real RAX(env) = (uint32_t)val; 593c97d6d2cSSergio Andres Gomez Del Real } else { 594da20f5cdSPaolo Bonzini RAX(env) = (uint64_t)val; 595c97d6d2cSSergio Andres Gomez Del Real } 5965d32173fSRoman Bolshakov env->eip += ins_len; 597*dbccd48dSWei Liu hvf_store_regs(cpu); 598c97d6d2cSSergio Andres Gomez Del Real break; 599c97d6d2cSSergio Andres Gomez Del Real } else if (!string && !in) { 6003b295bcbSPhilippe Mathieu-Daudé RAX(env) = rreg(cpu->accel->fd, HV_X86_RAX); 601bc4fa8c3SWei Liu hvf_handle_io(env_cpu(env), port, &RAX(env), 1, size, 1); 602c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 603c97d6d2cSSergio Andres Gomez Del Real break; 604c97d6d2cSSergio Andres Gomez Del Real } 605c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 606c97d6d2cSSergio Andres Gomez Del Real 607*dbccd48dSWei Liu hvf_load_regs(cpu); 608c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 609e62963bfSPaolo Bonzini assert(ins_len == decode.len); 610c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 611*dbccd48dSWei Liu hvf_store_regs(cpu); 612c97d6d2cSSergio Andres Gomez Del Real 613c97d6d2cSSergio Andres Gomez Del Real break; 614c97d6d2cSSergio Andres Gomez Del Real } 615c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CPUID: { 6163b295bcbSPhilippe Mathieu-Daudé uint32_t rax = (uint32_t)rreg(cpu->accel->fd, HV_X86_RAX); 6173b295bcbSPhilippe Mathieu-Daudé uint32_t rbx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RBX); 6183b295bcbSPhilippe Mathieu-Daudé uint32_t rcx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RCX); 6193b295bcbSPhilippe Mathieu-Daudé uint32_t rdx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RDX); 620c97d6d2cSSergio Andres Gomez Del Real 621106f91d5SAlexander Graf if (rax == 1) { 622106f91d5SAlexander Graf /* CPUID1.ecx.OSXSAVE needs to know CR4 */ 6233b295bcbSPhilippe Mathieu-Daudé env->cr[4] = rvmcs(cpu->accel->fd, VMCS_GUEST_CR4); 624106f91d5SAlexander Graf } 6253b502b0eSVladislav Yaroshchuk hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx); 626c97d6d2cSSergio Andres Gomez Del Real 6273b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RAX, rax); 6283b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RBX, rbx); 6293b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RCX, rcx); 6303b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RDX, rdx); 631c97d6d2cSSergio Andres Gomez Del Real 632c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 633c97d6d2cSSergio Andres Gomez Del Real break; 634c97d6d2cSSergio Andres Gomez Del Real } 635c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_XSETBV: { 6363b295bcbSPhilippe Mathieu-Daudé uint32_t eax = (uint32_t)rreg(cpu->accel->fd, HV_X86_RAX); 6373b295bcbSPhilippe Mathieu-Daudé uint32_t ecx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RCX); 6383b295bcbSPhilippe Mathieu-Daudé uint32_t edx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RDX); 639c97d6d2cSSergio Andres Gomez Del Real 640c97d6d2cSSergio Andres Gomez Del Real if (ecx) { 641c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 642c97d6d2cSSergio Andres Gomez Del Real break; 643c97d6d2cSSergio Andres Gomez Del Real } 644c97d6d2cSSergio Andres Gomez Del Real env->xcr0 = ((uint64_t)edx << 32) | eax; 6453b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_XCR0, env->xcr0 | 1); 646c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 647c97d6d2cSSergio Andres Gomez Del Real break; 648c97d6d2cSSergio Andres Gomez Del Real } 649c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INTR_WINDOW: 650c97d6d2cSSergio Andres Gomez Del Real vmx_clear_int_window_exiting(cpu); 651c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 652c97d6d2cSSergio Andres Gomez Del Real break; 653c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_NMI_WINDOW: 654c97d6d2cSSergio Andres Gomez Del Real vmx_clear_nmi_window_exiting(cpu); 655c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 656c97d6d2cSSergio Andres Gomez Del Real break; 657c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EXT_INTR: 658c97d6d2cSSergio Andres Gomez Del Real /* force exit and allow io handling */ 659c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 660c97d6d2cSSergio Andres Gomez Del Real break; 661c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDMSR: 662c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_WRMSR: 663c97d6d2cSSergio Andres Gomez Del Real { 664*dbccd48dSWei Liu hvf_load_regs(cpu); 665c97d6d2cSSergio Andres Gomez Del Real if (exit_reason == EXIT_REASON_RDMSR) { 666a9e445dfSPhilippe Mathieu-Daudé simulate_rdmsr(env); 667c97d6d2cSSergio Andres Gomez Del Real } else { 668a9e445dfSPhilippe Mathieu-Daudé simulate_wrmsr(env); 669c97d6d2cSSergio Andres Gomez Del Real } 6705d32173fSRoman Bolshakov env->eip += ins_len; 671*dbccd48dSWei Liu hvf_store_regs(cpu); 672c97d6d2cSSergio Andres Gomez Del Real break; 673c97d6d2cSSergio Andres Gomez Del Real } 674c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CR_ACCESS: { 675c97d6d2cSSergio Andres Gomez Del Real int cr; 676c97d6d2cSSergio Andres Gomez Del Real int reg; 677c97d6d2cSSergio Andres Gomez Del Real 678*dbccd48dSWei Liu hvf_load_regs(cpu); 679c97d6d2cSSergio Andres Gomez Del Real cr = exit_qual & 15; 680c97d6d2cSSergio Andres Gomez Del Real reg = (exit_qual >> 8) & 15; 681c97d6d2cSSergio Andres Gomez Del Real 682c97d6d2cSSergio Andres Gomez Del Real switch (cr) { 683c97d6d2cSSergio Andres Gomez Del Real case 0x0: { 6843b295bcbSPhilippe Mathieu-Daudé macvm_set_cr0(cpu->accel->fd, RRX(env, reg)); 685c97d6d2cSSergio Andres Gomez Del Real break; 686c97d6d2cSSergio Andres Gomez Del Real } 687c97d6d2cSSergio Andres Gomez Del Real case 4: { 6883b295bcbSPhilippe Mathieu-Daudé macvm_set_cr4(cpu->accel->fd, RRX(env, reg)); 689c97d6d2cSSergio Andres Gomez Del Real break; 690c97d6d2cSSergio Andres Gomez Del Real } 691c97d6d2cSSergio Andres Gomez Del Real case 8: { 692c97d6d2cSSergio Andres Gomez Del Real if (exit_qual & 0x10) { 693c97d6d2cSSergio Andres Gomez Del Real RRX(env, reg) = cpu_get_apic_tpr(x86_cpu->apic_state); 694c97d6d2cSSergio Andres Gomez Del Real } else { 695c97d6d2cSSergio Andres Gomez Del Real int tpr = RRX(env, reg); 696c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr); 697c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 698c97d6d2cSSergio Andres Gomez Del Real } 699c97d6d2cSSergio Andres Gomez Del Real break; 700c97d6d2cSSergio Andres Gomez Del Real } 701c97d6d2cSSergio Andres Gomez Del Real default: 7022d9178d9SLaurent Vivier error_report("Unrecognized CR %d", cr); 703c97d6d2cSSergio Andres Gomez Del Real abort(); 704c97d6d2cSSergio Andres Gomez Del Real } 7055d32173fSRoman Bolshakov env->eip += ins_len; 706*dbccd48dSWei Liu hvf_store_regs(cpu); 707c97d6d2cSSergio Andres Gomez Del Real break; 708c97d6d2cSSergio Andres Gomez Del Real } 709c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_APIC_ACCESS: { /* TODO */ 710c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 711c97d6d2cSSergio Andres Gomez Del Real 712*dbccd48dSWei Liu hvf_load_regs(cpu); 713c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 714c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 715*dbccd48dSWei Liu hvf_store_regs(cpu); 716c97d6d2cSSergio Andres Gomez Del Real break; 717c97d6d2cSSergio Andres Gomez Del Real } 718c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TPR: { 719c97d6d2cSSergio Andres Gomez Del Real ret = 1; 720c97d6d2cSSergio Andres Gomez Del Real break; 721c97d6d2cSSergio Andres Gomez Del Real } 722c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TASK_SWITCH: { 7233b295bcbSPhilippe Mathieu-Daudé uint64_t vinfo = rvmcs(cpu->accel->fd, VMCS_IDT_VECTORING_INFO); 72425409172SWei Liu x86_segment_selector sel = {.sel = exit_qual & 0xffff}; 725c97d6d2cSSergio Andres Gomez Del Real vmx_handle_task_switch(cpu, sel, (exit_qual >> 30) & 0x3, 726c97d6d2cSSergio Andres Gomez Del Real vinfo & VMCS_INTR_VALID, vinfo & VECTORING_INFO_VECTOR_MASK, vinfo 727c97d6d2cSSergio Andres Gomez Del Real & VMCS_INTR_T_MASK); 728c97d6d2cSSergio Andres Gomez Del Real break; 729c97d6d2cSSergio Andres Gomez Del Real } 730c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TRIPLE_FAULT: { 731c97d6d2cSSergio Andres Gomez Del Real qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 732c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 733c97d6d2cSSergio Andres Gomez Del Real break; 734c97d6d2cSSergio Andres Gomez Del Real } 735c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDPMC: 7363b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RAX, 0); 7373b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RDX, 0); 738c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 739c97d6d2cSSergio Andres Gomez Del Real break; 740c97d6d2cSSergio Andres Gomez Del Real case VMX_REASON_VMCALL: 741fd13f23bSLiran Alon env->exception_nr = EXCP0D_GPF; 742fd13f23bSLiran Alon env->exception_injected = 1; 7433010460fSSergio Andres Gomez Del Real env->has_error_code = true; 7443010460fSSergio Andres Gomez Del Real env->error_code = 0; 745c97d6d2cSSergio Andres Gomez Del Real break; 746c97d6d2cSSergio Andres Gomez Del Real default: 7472d9178d9SLaurent Vivier error_report("%llx: unhandled exit %llx", rip, exit_reason); 748c97d6d2cSSergio Andres Gomez Del Real } 749c97d6d2cSSergio Andres Gomez Del Real } while (ret == 0); 750c97d6d2cSSergio Andres Gomez Del Real 751c97d6d2cSSergio Andres Gomez Del Real return ret; 752c97d6d2cSSergio Andres Gomez Del Real } 753f4152040SFrancesco Cagnin 754f4152040SFrancesco Cagnin int hvf_arch_insert_sw_breakpoint(CPUState *cpu, struct hvf_sw_breakpoint *bp) 755f4152040SFrancesco Cagnin { 756f4152040SFrancesco Cagnin return -ENOSYS; 757f4152040SFrancesco Cagnin } 758f4152040SFrancesco Cagnin 759f4152040SFrancesco Cagnin int hvf_arch_remove_sw_breakpoint(CPUState *cpu, struct hvf_sw_breakpoint *bp) 760f4152040SFrancesco Cagnin { 761f4152040SFrancesco Cagnin return -ENOSYS; 762f4152040SFrancesco Cagnin } 763f4152040SFrancesco Cagnin 764d447a624SAnton Johansson int hvf_arch_insert_hw_breakpoint(vaddr addr, vaddr len, int type) 765f4152040SFrancesco Cagnin { 766f4152040SFrancesco Cagnin return -ENOSYS; 767f4152040SFrancesco Cagnin } 768f4152040SFrancesco Cagnin 769d447a624SAnton Johansson int hvf_arch_remove_hw_breakpoint(vaddr addr, vaddr len, int type) 770f4152040SFrancesco Cagnin { 771f4152040SFrancesco Cagnin return -ENOSYS; 772f4152040SFrancesco Cagnin } 773f4152040SFrancesco Cagnin 774f4152040SFrancesco Cagnin void hvf_arch_remove_all_hw_breakpoints(void) 775f4152040SFrancesco Cagnin { 776f4152040SFrancesco Cagnin } 777eb2edc42SFrancesco Cagnin 778eb2edc42SFrancesco Cagnin void hvf_arch_update_guest_debug(CPUState *cpu) 779eb2edc42SFrancesco Cagnin { 780eb2edc42SFrancesco Cagnin } 781eb2edc42SFrancesco Cagnin 782d6fd5d83SPhilippe Mathieu-Daudé bool hvf_arch_supports_guest_debug(void) 783eb2edc42SFrancesco Cagnin { 784eb2edc42SFrancesco Cagnin return false; 785eb2edc42SFrancesco Cagnin } 786