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 16c97d6d2cSSergio Andres Gomez Del Real * Lesser General Public License for more details. 17c97d6d2cSSergio Andres Gomez Del Real * 18c97d6d2cSSergio Andres Gomez Del Real * You should have received a copy of the GNU Lesser General Public 19c97d6d2cSSergio Andres Gomez Del Real * License 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 23*4d98a8e5SPaolo Bonzini * 24*4d98a8e5SPaolo Bonzini * Parts Copyright (c) 2011 NetApp, Inc. 25*4d98a8e5SPaolo Bonzini * All rights reserved. 26*4d98a8e5SPaolo Bonzini * 27*4d98a8e5SPaolo Bonzini * Redistribution and use in source and binary forms, with or without 28*4d98a8e5SPaolo Bonzini * modification, are permitted provided that the following conditions 29*4d98a8e5SPaolo Bonzini * are met: 30*4d98a8e5SPaolo Bonzini * 1. Redistributions of source code must retain the above copyright 31*4d98a8e5SPaolo Bonzini * notice, this list of conditions and the following disclaimer. 32*4d98a8e5SPaolo Bonzini * 2. Redistributions in binary form must reproduce the above copyright 33*4d98a8e5SPaolo Bonzini * notice, this list of conditions and the following disclaimer in the 34*4d98a8e5SPaolo Bonzini * documentation and/or other materials provided with the distribution. 35*4d98a8e5SPaolo Bonzini * 36*4d98a8e5SPaolo Bonzini * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 37*4d98a8e5SPaolo Bonzini * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38*4d98a8e5SPaolo Bonzini * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39*4d98a8e5SPaolo Bonzini * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 40*4d98a8e5SPaolo Bonzini * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41*4d98a8e5SPaolo Bonzini * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42*4d98a8e5SPaolo Bonzini * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43*4d98a8e5SPaolo Bonzini * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44*4d98a8e5SPaolo Bonzini * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45*4d98a8e5SPaolo Bonzini * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46*4d98a8e5SPaolo Bonzini * SUCH DAMAGE. 47c97d6d2cSSergio Andres Gomez Del Real */ 48c97d6d2cSSergio Andres Gomez Del Real #include "qemu/osdep.h" 49c97d6d2cSSergio Andres Gomez Del Real #include "qemu-common.h" 50c97d6d2cSSergio Andres Gomez Del Real #include "qemu/error-report.h" 51c97d6d2cSSergio Andres Gomez Del Real 52c97d6d2cSSergio Andres Gomez Del Real #include "sysemu/hvf.h" 53c97d6d2cSSergio Andres Gomez Del Real #include "hvf-i386.h" 5469e0a03cSPaolo Bonzini #include "vmcs.h" 5569e0a03cSPaolo Bonzini #include "vmx.h" 5669e0a03cSPaolo Bonzini #include "x86.h" 5769e0a03cSPaolo Bonzini #include "x86_descr.h" 5869e0a03cSPaolo Bonzini #include "x86_mmu.h" 5969e0a03cSPaolo Bonzini #include "x86_decode.h" 6069e0a03cSPaolo Bonzini #include "x86_emu.h" 6169e0a03cSPaolo Bonzini #include "x86_task.h" 6269e0a03cSPaolo Bonzini #include "x86hvf.h" 63c97d6d2cSSergio Andres Gomez Del Real 64c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv.h> 65c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv_vmx.h> 66c97d6d2cSSergio Andres Gomez Del Real 67c97d6d2cSSergio Andres Gomez Del Real #include "exec/address-spaces.h" 68c97d6d2cSSergio Andres Gomez Del Real #include "exec/exec-all.h" 69c97d6d2cSSergio Andres Gomez Del Real #include "exec/ioport.h" 70c97d6d2cSSergio Andres Gomez Del Real #include "hw/i386/apic_internal.h" 71c97d6d2cSSergio Andres Gomez Del Real #include "hw/boards.h" 72c97d6d2cSSergio Andres Gomez Del Real #include "qemu/main-loop.h" 73c97d6d2cSSergio Andres Gomez Del Real #include "strings.h" 74c97d6d2cSSergio Andres Gomez Del Real #include "sysemu/accel.h" 75c97d6d2cSSergio Andres Gomez Del Real #include "sysemu/sysemu.h" 76c97d6d2cSSergio Andres Gomez Del Real #include "target/i386/cpu.h" 77c97d6d2cSSergio Andres Gomez Del Real 78c97d6d2cSSergio Andres Gomez Del Real pthread_rwlock_t mem_lock = PTHREAD_RWLOCK_INITIALIZER; 79c97d6d2cSSergio Andres Gomez Del Real HVFState *hvf_state; 80c97d6d2cSSergio Andres Gomez Del Real int hvf_disabled = 1; 81c97d6d2cSSergio Andres Gomez Del Real 82c97d6d2cSSergio Andres Gomez Del Real static void assert_hvf_ok(hv_return_t ret) 83c97d6d2cSSergio Andres Gomez Del Real { 84c97d6d2cSSergio Andres Gomez Del Real if (ret == HV_SUCCESS) { 85c97d6d2cSSergio Andres Gomez Del Real return; 86c97d6d2cSSergio Andres Gomez Del Real } 87c97d6d2cSSergio Andres Gomez Del Real 88c97d6d2cSSergio Andres Gomez Del Real switch (ret) { 89c97d6d2cSSergio Andres Gomez Del Real case HV_ERROR: 90c97d6d2cSSergio Andres Gomez Del Real error_report("Error: HV_ERROR\n"); 91c97d6d2cSSergio Andres Gomez Del Real break; 92c97d6d2cSSergio Andres Gomez Del Real case HV_BUSY: 93c97d6d2cSSergio Andres Gomez Del Real error_report("Error: HV_BUSY\n"); 94c97d6d2cSSergio Andres Gomez Del Real break; 95c97d6d2cSSergio Andres Gomez Del Real case HV_BAD_ARGUMENT: 96c97d6d2cSSergio Andres Gomez Del Real error_report("Error: HV_BAD_ARGUMENT\n"); 97c97d6d2cSSergio Andres Gomez Del Real break; 98c97d6d2cSSergio Andres Gomez Del Real case HV_NO_RESOURCES: 99c97d6d2cSSergio Andres Gomez Del Real error_report("Error: HV_NO_RESOURCES\n"); 100c97d6d2cSSergio Andres Gomez Del Real break; 101c97d6d2cSSergio Andres Gomez Del Real case HV_NO_DEVICE: 102c97d6d2cSSergio Andres Gomez Del Real error_report("Error: HV_NO_DEVICE\n"); 103c97d6d2cSSergio Andres Gomez Del Real break; 104c97d6d2cSSergio Andres Gomez Del Real case HV_UNSUPPORTED: 105c97d6d2cSSergio Andres Gomez Del Real error_report("Error: HV_UNSUPPORTED\n"); 106c97d6d2cSSergio Andres Gomez Del Real break; 107c97d6d2cSSergio Andres Gomez Del Real default: 108c97d6d2cSSergio Andres Gomez Del Real error_report("Unknown Error\n"); 109c97d6d2cSSergio Andres Gomez Del Real } 110c97d6d2cSSergio Andres Gomez Del Real 111c97d6d2cSSergio Andres Gomez Del Real abort(); 112c97d6d2cSSergio Andres Gomez Del Real } 113c97d6d2cSSergio Andres Gomez Del Real 114c97d6d2cSSergio Andres Gomez Del Real /* Memory slots */ 115c97d6d2cSSergio Andres Gomez Del Real hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t end) 116c97d6d2cSSergio Andres Gomez Del Real { 117c97d6d2cSSergio Andres Gomez Del Real hvf_slot *slot; 118c97d6d2cSSergio Andres Gomez Del Real int x; 119c97d6d2cSSergio Andres Gomez Del Real for (x = 0; x < hvf_state->num_slots; ++x) { 120c97d6d2cSSergio Andres Gomez Del Real slot = &hvf_state->slots[x]; 121c97d6d2cSSergio Andres Gomez Del Real if (slot->size && start < (slot->start + slot->size) && 122c97d6d2cSSergio Andres Gomez Del Real end > slot->start) { 123c97d6d2cSSergio Andres Gomez Del Real return slot; 124c97d6d2cSSergio Andres Gomez Del Real } 125c97d6d2cSSergio Andres Gomez Del Real } 126c97d6d2cSSergio Andres Gomez Del Real return NULL; 127c97d6d2cSSergio Andres Gomez Del Real } 128c97d6d2cSSergio Andres Gomez Del Real 129c97d6d2cSSergio Andres Gomez Del Real struct mac_slot { 130c97d6d2cSSergio Andres Gomez Del Real int present; 131c97d6d2cSSergio Andres Gomez Del Real uint64_t size; 132c97d6d2cSSergio Andres Gomez Del Real uint64_t gpa_start; 133c97d6d2cSSergio Andres Gomez Del Real uint64_t gva; 134c97d6d2cSSergio Andres Gomez Del Real }; 135c97d6d2cSSergio Andres Gomez Del Real 136c97d6d2cSSergio Andres Gomez Del Real struct mac_slot mac_slots[32]; 137c97d6d2cSSergio Andres Gomez Del Real #define ALIGN(x, y) (((x) + (y) - 1) & ~((y) - 1)) 138c97d6d2cSSergio Andres Gomez Del Real 139c97d6d2cSSergio Andres Gomez Del Real static int do_hvf_set_memory(hvf_slot *slot) 140c97d6d2cSSergio Andres Gomez Del Real { 141c97d6d2cSSergio Andres Gomez Del Real struct mac_slot *macslot; 142c97d6d2cSSergio Andres Gomez Del Real hv_memory_flags_t flags; 143c97d6d2cSSergio Andres Gomez Del Real hv_return_t ret; 144c97d6d2cSSergio Andres Gomez Del Real 145c97d6d2cSSergio Andres Gomez Del Real macslot = &mac_slots[slot->slot_id]; 146c97d6d2cSSergio Andres Gomez Del Real 147c97d6d2cSSergio Andres Gomez Del Real if (macslot->present) { 148c97d6d2cSSergio Andres Gomez Del Real if (macslot->size != slot->size) { 149c97d6d2cSSergio Andres Gomez Del Real macslot->present = 0; 150c97d6d2cSSergio Andres Gomez Del Real ret = hv_vm_unmap(macslot->gpa_start, macslot->size); 151c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(ret); 152c97d6d2cSSergio Andres Gomez Del Real } 153c97d6d2cSSergio Andres Gomez Del Real } 154c97d6d2cSSergio Andres Gomez Del Real 155c97d6d2cSSergio Andres Gomez Del Real if (!slot->size) { 156c97d6d2cSSergio Andres Gomez Del Real return 0; 157c97d6d2cSSergio Andres Gomez Del Real } 158c97d6d2cSSergio Andres Gomez Del Real 159c97d6d2cSSergio Andres Gomez Del Real flags = HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; 160c97d6d2cSSergio Andres Gomez Del Real 161c97d6d2cSSergio Andres Gomez Del Real macslot->present = 1; 162c97d6d2cSSergio Andres Gomez Del Real macslot->gpa_start = slot->start; 163c97d6d2cSSergio Andres Gomez Del Real macslot->size = slot->size; 164c97d6d2cSSergio Andres Gomez Del Real ret = hv_vm_map((hv_uvaddr_t)slot->mem, slot->start, slot->size, flags); 165c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(ret); 166c97d6d2cSSergio Andres Gomez Del Real return 0; 167c97d6d2cSSergio Andres Gomez Del Real } 168c97d6d2cSSergio Andres Gomez Del Real 169c97d6d2cSSergio Andres Gomez Del Real void hvf_set_phys_mem(MemoryRegionSection *section, bool add) 170c97d6d2cSSergio Andres Gomez Del Real { 171c97d6d2cSSergio Andres Gomez Del Real hvf_slot *mem; 172c97d6d2cSSergio Andres Gomez Del Real MemoryRegion *area = section->mr; 173c97d6d2cSSergio Andres Gomez Del Real 174c97d6d2cSSergio Andres Gomez Del Real if (!memory_region_is_ram(area)) { 175c97d6d2cSSergio Andres Gomez Del Real return; 176c97d6d2cSSergio Andres Gomez Del Real } 177c97d6d2cSSergio Andres Gomez Del Real 178c97d6d2cSSergio Andres Gomez Del Real mem = hvf_find_overlap_slot( 179c97d6d2cSSergio Andres Gomez Del Real section->offset_within_address_space, 180c97d6d2cSSergio Andres Gomez Del Real section->offset_within_address_space + int128_get64(section->size)); 181c97d6d2cSSergio Andres Gomez Del Real 182c97d6d2cSSergio Andres Gomez Del Real if (mem && add) { 183c97d6d2cSSergio Andres Gomez Del Real if (mem->size == int128_get64(section->size) && 184c97d6d2cSSergio Andres Gomez Del Real mem->start == section->offset_within_address_space && 185c97d6d2cSSergio Andres Gomez Del Real mem->mem == (memory_region_get_ram_ptr(area) + 186c97d6d2cSSergio Andres Gomez Del Real section->offset_within_region)) { 187c97d6d2cSSergio Andres Gomez Del Real return; /* Same region was attempted to register, go away. */ 188c97d6d2cSSergio Andres Gomez Del Real } 189c97d6d2cSSergio Andres Gomez Del Real } 190c97d6d2cSSergio Andres Gomez Del Real 191c97d6d2cSSergio Andres Gomez Del Real /* Region needs to be reset. set the size to 0 and remap it. */ 192c97d6d2cSSergio Andres Gomez Del Real if (mem) { 193c97d6d2cSSergio Andres Gomez Del Real mem->size = 0; 194c97d6d2cSSergio Andres Gomez Del Real if (do_hvf_set_memory(mem)) { 195c97d6d2cSSergio Andres Gomez Del Real error_report("Failed to reset overlapping slot\n"); 196c97d6d2cSSergio Andres Gomez Del Real abort(); 197c97d6d2cSSergio Andres Gomez Del Real } 198c97d6d2cSSergio Andres Gomez Del Real } 199c97d6d2cSSergio Andres Gomez Del Real 200c97d6d2cSSergio Andres Gomez Del Real if (!add) { 201c97d6d2cSSergio Andres Gomez Del Real return; 202c97d6d2cSSergio Andres Gomez Del Real } 203c97d6d2cSSergio Andres Gomez Del Real 204c97d6d2cSSergio Andres Gomez Del Real /* Now make a new slot. */ 205c97d6d2cSSergio Andres Gomez Del Real int x; 206c97d6d2cSSergio Andres Gomez Del Real 207c97d6d2cSSergio Andres Gomez Del Real for (x = 0; x < hvf_state->num_slots; ++x) { 208c97d6d2cSSergio Andres Gomez Del Real mem = &hvf_state->slots[x]; 209c97d6d2cSSergio Andres Gomez Del Real if (!mem->size) { 210c97d6d2cSSergio Andres Gomez Del Real break; 211c97d6d2cSSergio Andres Gomez Del Real } 212c97d6d2cSSergio Andres Gomez Del Real } 213c97d6d2cSSergio Andres Gomez Del Real 214c97d6d2cSSergio Andres Gomez Del Real if (x == hvf_state->num_slots) { 215c97d6d2cSSergio Andres Gomez Del Real error_report("No free slots\n"); 216c97d6d2cSSergio Andres Gomez Del Real abort(); 217c97d6d2cSSergio Andres Gomez Del Real } 218c97d6d2cSSergio Andres Gomez Del Real 219c97d6d2cSSergio Andres Gomez Del Real mem->size = int128_get64(section->size); 220c97d6d2cSSergio Andres Gomez Del Real mem->mem = memory_region_get_ram_ptr(area) + section->offset_within_region; 221c97d6d2cSSergio Andres Gomez Del Real mem->start = section->offset_within_address_space; 222babfa20cSSergio Andres Gomez Del Real mem->region = area; 223c97d6d2cSSergio Andres Gomez Del Real 224c97d6d2cSSergio Andres Gomez Del Real if (do_hvf_set_memory(mem)) { 225c97d6d2cSSergio Andres Gomez Del Real error_report("Error registering new memory slot\n"); 226c97d6d2cSSergio Andres Gomez Del Real abort(); 227c97d6d2cSSergio Andres Gomez Del Real } 228c97d6d2cSSergio Andres Gomez Del Real } 229c97d6d2cSSergio Andres Gomez Del Real 230c97d6d2cSSergio Andres Gomez Del Real void vmx_update_tpr(CPUState *cpu) 231c97d6d2cSSergio Andres Gomez Del Real { 232c97d6d2cSSergio Andres Gomez Del Real /* TODO: need integrate APIC handling */ 233c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 234c97d6d2cSSergio Andres Gomez Del Real int tpr = cpu_get_apic_tpr(x86_cpu->apic_state) << 4; 235c97d6d2cSSergio Andres Gomez Del Real int irr = apic_get_highest_priority_irr(x86_cpu->apic_state); 236c97d6d2cSSergio Andres Gomez Del Real 237c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_TPR, tpr); 238c97d6d2cSSergio Andres Gomez Del Real if (irr == -1) { 239c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_TPR_THRESHOLD, 0); 240c97d6d2cSSergio Andres Gomez Del Real } else { 241c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_TPR_THRESHOLD, (irr > tpr) ? tpr >> 4 : 242c97d6d2cSSergio Andres Gomez Del Real irr >> 4); 243c97d6d2cSSergio Andres Gomez Del Real } 244c97d6d2cSSergio Andres Gomez Del Real } 245c97d6d2cSSergio Andres Gomez Del Real 246c97d6d2cSSergio Andres Gomez Del Real void update_apic_tpr(CPUState *cpu) 247c97d6d2cSSergio Andres Gomez Del Real { 248c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 249c97d6d2cSSergio Andres Gomez Del Real int tpr = rreg(cpu->hvf_fd, HV_X86_TPR) >> 4; 250c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr); 251c97d6d2cSSergio Andres Gomez Del Real } 252c97d6d2cSSergio Andres Gomez Del Real 253c97d6d2cSSergio Andres Gomez Del Real #define VECTORING_INFO_VECTOR_MASK 0xff 254c97d6d2cSSergio Andres Gomez Del Real 255c97d6d2cSSergio Andres Gomez Del Real static void hvf_handle_interrupt(CPUState * cpu, int mask) 256c97d6d2cSSergio Andres Gomez Del Real { 257c97d6d2cSSergio Andres Gomez Del Real cpu->interrupt_request |= mask; 258c97d6d2cSSergio Andres Gomez Del Real if (!qemu_cpu_is_self(cpu)) { 259c97d6d2cSSergio Andres Gomez Del Real qemu_cpu_kick(cpu); 260c97d6d2cSSergio Andres Gomez Del Real } 261c97d6d2cSSergio Andres Gomez Del Real } 262c97d6d2cSSergio Andres Gomez Del Real 263c97d6d2cSSergio Andres Gomez Del Real void hvf_handle_io(CPUArchState *env, uint16_t port, void *buffer, 264c97d6d2cSSergio Andres Gomez Del Real int direction, int size, int count) 265c97d6d2cSSergio Andres Gomez Del Real { 266c97d6d2cSSergio Andres Gomez Del Real int i; 267c97d6d2cSSergio Andres Gomez Del Real uint8_t *ptr = buffer; 268c97d6d2cSSergio Andres Gomez Del Real 269c97d6d2cSSergio Andres Gomez Del Real for (i = 0; i < count; i++) { 270c97d6d2cSSergio Andres Gomez Del Real address_space_rw(&address_space_io, port, MEMTXATTRS_UNSPECIFIED, 271c97d6d2cSSergio Andres Gomez Del Real ptr, size, 272c97d6d2cSSergio Andres Gomez Del Real direction); 273c97d6d2cSSergio Andres Gomez Del Real ptr += size; 274c97d6d2cSSergio Andres Gomez Del Real } 275c97d6d2cSSergio Andres Gomez Del Real } 276c97d6d2cSSergio Andres Gomez Del Real 277c97d6d2cSSergio Andres Gomez Del Real /* TODO: synchronize vcpu state */ 278c97d6d2cSSergio Andres Gomez Del Real static void do_hvf_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg) 279c97d6d2cSSergio Andres Gomez Del Real { 280c97d6d2cSSergio Andres Gomez Del Real CPUState *cpu_state = cpu; 281c97d6d2cSSergio Andres Gomez Del Real if (cpu_state->vcpu_dirty == 0) { 282c97d6d2cSSergio Andres Gomez Del Real hvf_get_registers(cpu_state); 283c97d6d2cSSergio Andres Gomez Del Real } 284c97d6d2cSSergio Andres Gomez Del Real 285c97d6d2cSSergio Andres Gomez Del Real cpu_state->vcpu_dirty = 1; 286c97d6d2cSSergio Andres Gomez Del Real } 287c97d6d2cSSergio Andres Gomez Del Real 288c97d6d2cSSergio Andres Gomez Del Real void hvf_cpu_synchronize_state(CPUState *cpu_state) 289c97d6d2cSSergio Andres Gomez Del Real { 290c97d6d2cSSergio Andres Gomez Del Real if (cpu_state->vcpu_dirty == 0) { 291c97d6d2cSSergio Andres Gomez Del Real run_on_cpu(cpu_state, do_hvf_cpu_synchronize_state, RUN_ON_CPU_NULL); 292c97d6d2cSSergio Andres Gomez Del Real } 293c97d6d2cSSergio Andres Gomez Del Real } 294c97d6d2cSSergio Andres Gomez Del Real 295c97d6d2cSSergio Andres Gomez Del Real static void do_hvf_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg) 296c97d6d2cSSergio Andres Gomez Del Real { 297c97d6d2cSSergio Andres Gomez Del Real CPUState *cpu_state = cpu; 298c97d6d2cSSergio Andres Gomez Del Real hvf_put_registers(cpu_state); 299c97d6d2cSSergio Andres Gomez Del Real cpu_state->vcpu_dirty = false; 300c97d6d2cSSergio Andres Gomez Del Real } 301c97d6d2cSSergio Andres Gomez Del Real 302c97d6d2cSSergio Andres Gomez Del Real void hvf_cpu_synchronize_post_reset(CPUState *cpu_state) 303c97d6d2cSSergio Andres Gomez Del Real { 304c97d6d2cSSergio Andres Gomez Del Real run_on_cpu(cpu_state, do_hvf_cpu_synchronize_post_reset, RUN_ON_CPU_NULL); 305c97d6d2cSSergio Andres Gomez Del Real } 306c97d6d2cSSergio Andres Gomez Del Real 307c97d6d2cSSergio Andres Gomez Del Real void _hvf_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg) 308c97d6d2cSSergio Andres Gomez Del Real { 309c97d6d2cSSergio Andres Gomez Del Real CPUState *cpu_state = cpu; 310c97d6d2cSSergio Andres Gomez Del Real hvf_put_registers(cpu_state); 311c97d6d2cSSergio Andres Gomez Del Real cpu_state->vcpu_dirty = false; 312c97d6d2cSSergio Andres Gomez Del Real } 313c97d6d2cSSergio Andres Gomez Del Real 314c97d6d2cSSergio Andres Gomez Del Real void hvf_cpu_synchronize_post_init(CPUState *cpu_state) 315c97d6d2cSSergio Andres Gomez Del Real { 316c97d6d2cSSergio Andres Gomez Del Real run_on_cpu(cpu_state, _hvf_cpu_synchronize_post_init, RUN_ON_CPU_NULL); 317c97d6d2cSSergio Andres Gomez Del Real } 318c97d6d2cSSergio Andres Gomez Del Real 319ff2de166SPaolo Bonzini static bool ept_emulation_fault(hvf_slot *slot, uint64_t gpa, uint64_t ept_qual) 320c97d6d2cSSergio Andres Gomez Del Real { 321c97d6d2cSSergio Andres Gomez Del Real int read, write; 322c97d6d2cSSergio Andres Gomez Del Real 323c97d6d2cSSergio Andres Gomez Del Real /* EPT fault on an instruction fetch doesn't make sense here */ 324c97d6d2cSSergio Andres Gomez Del Real if (ept_qual & EPT_VIOLATION_INST_FETCH) { 325c97d6d2cSSergio Andres Gomez Del Real return false; 326c97d6d2cSSergio Andres Gomez Del Real } 327c97d6d2cSSergio Andres Gomez Del Real 328c97d6d2cSSergio Andres Gomez Del Real /* EPT fault must be a read fault or a write fault */ 329c97d6d2cSSergio Andres Gomez Del Real read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0; 330c97d6d2cSSergio Andres Gomez Del Real write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0; 331c97d6d2cSSergio Andres Gomez Del Real if ((read | write) == 0) { 332c97d6d2cSSergio Andres Gomez Del Real return false; 333c97d6d2cSSergio Andres Gomez Del Real } 334c97d6d2cSSergio Andres Gomez Del Real 335babfa20cSSergio Andres Gomez Del Real if (write && slot) { 336babfa20cSSergio Andres Gomez Del Real if (slot->flags & HVF_SLOT_LOG) { 337babfa20cSSergio Andres Gomez Del Real memory_region_set_dirty(slot->region, gpa - slot->start, 1); 338babfa20cSSergio Andres Gomez Del Real hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size, 339babfa20cSSergio Andres Gomez Del Real HV_MEMORY_READ | HV_MEMORY_WRITE); 340babfa20cSSergio Andres Gomez Del Real } 341babfa20cSSergio Andres Gomez Del Real } 342babfa20cSSergio Andres Gomez Del Real 343c97d6d2cSSergio Andres Gomez Del Real /* 344c97d6d2cSSergio Andres Gomez Del Real * The EPT violation must have been caused by accessing a 345c97d6d2cSSergio Andres Gomez Del Real * guest-physical address that is a translation of a guest-linear 346c97d6d2cSSergio Andres Gomez Del Real * address. 347c97d6d2cSSergio Andres Gomez Del Real */ 348c97d6d2cSSergio Andres Gomez Del Real if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 || 349c97d6d2cSSergio Andres Gomez Del Real (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) { 350c97d6d2cSSergio Andres Gomez Del Real return false; 351c97d6d2cSSergio Andres Gomez Del Real } 352c97d6d2cSSergio Andres Gomez Del Real 353babfa20cSSergio Andres Gomez Del Real return !slot; 354babfa20cSSergio Andres Gomez Del Real } 355babfa20cSSergio Andres Gomez Del Real 356babfa20cSSergio Andres Gomez Del Real static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on) 357babfa20cSSergio Andres Gomez Del Real { 358babfa20cSSergio Andres Gomez Del Real hvf_slot *slot; 359babfa20cSSergio Andres Gomez Del Real 360babfa20cSSergio Andres Gomez Del Real slot = hvf_find_overlap_slot( 361babfa20cSSergio Andres Gomez Del Real section->offset_within_address_space, 362babfa20cSSergio Andres Gomez Del Real section->offset_within_address_space + int128_get64(section->size)); 363babfa20cSSergio Andres Gomez Del Real 364babfa20cSSergio Andres Gomez Del Real /* protect region against writes; begin tracking it */ 365babfa20cSSergio Andres Gomez Del Real if (on) { 366babfa20cSSergio Andres Gomez Del Real slot->flags |= HVF_SLOT_LOG; 367babfa20cSSergio Andres Gomez Del Real hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size, 368babfa20cSSergio Andres Gomez Del Real HV_MEMORY_READ); 369babfa20cSSergio Andres Gomez Del Real /* stop tracking region*/ 370babfa20cSSergio Andres Gomez Del Real } else { 371babfa20cSSergio Andres Gomez Del Real slot->flags &= ~HVF_SLOT_LOG; 372babfa20cSSergio Andres Gomez Del Real hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size, 373babfa20cSSergio Andres Gomez Del Real HV_MEMORY_READ | HV_MEMORY_WRITE); 374babfa20cSSergio Andres Gomez Del Real } 375babfa20cSSergio Andres Gomez Del Real } 376babfa20cSSergio Andres Gomez Del Real 377babfa20cSSergio Andres Gomez Del Real static void hvf_log_start(MemoryListener *listener, 378babfa20cSSergio Andres Gomez Del Real MemoryRegionSection *section, int old, int new) 379babfa20cSSergio Andres Gomez Del Real { 380babfa20cSSergio Andres Gomez Del Real if (old != 0) { 381babfa20cSSergio Andres Gomez Del Real return; 382babfa20cSSergio Andres Gomez Del Real } 383babfa20cSSergio Andres Gomez Del Real 384babfa20cSSergio Andres Gomez Del Real hvf_set_dirty_tracking(section, 1); 385babfa20cSSergio Andres Gomez Del Real } 386babfa20cSSergio Andres Gomez Del Real 387babfa20cSSergio Andres Gomez Del Real static void hvf_log_stop(MemoryListener *listener, 388babfa20cSSergio Andres Gomez Del Real MemoryRegionSection *section, int old, int new) 389babfa20cSSergio Andres Gomez Del Real { 390babfa20cSSergio Andres Gomez Del Real if (new != 0) { 391babfa20cSSergio Andres Gomez Del Real return; 392babfa20cSSergio Andres Gomez Del Real } 393babfa20cSSergio Andres Gomez Del Real 394babfa20cSSergio Andres Gomez Del Real hvf_set_dirty_tracking(section, 0); 395babfa20cSSergio Andres Gomez Del Real } 396babfa20cSSergio Andres Gomez Del Real 397babfa20cSSergio Andres Gomez Del Real static void hvf_log_sync(MemoryListener *listener, 398babfa20cSSergio Andres Gomez Del Real MemoryRegionSection *section) 399babfa20cSSergio Andres Gomez Del Real { 400babfa20cSSergio Andres Gomez Del Real /* 401babfa20cSSergio Andres Gomez Del Real * sync of dirty pages is handled elsewhere; just make sure we keep 402babfa20cSSergio Andres Gomez Del Real * tracking the region. 403babfa20cSSergio Andres Gomez Del Real */ 404babfa20cSSergio Andres Gomez Del Real hvf_set_dirty_tracking(section, 1); 405c97d6d2cSSergio Andres Gomez Del Real } 406c97d6d2cSSergio Andres Gomez Del Real 407c97d6d2cSSergio Andres Gomez Del Real static void hvf_region_add(MemoryListener *listener, 408c97d6d2cSSergio Andres Gomez Del Real MemoryRegionSection *section) 409c97d6d2cSSergio Andres Gomez Del Real { 410c97d6d2cSSergio Andres Gomez Del Real hvf_set_phys_mem(section, true); 411c97d6d2cSSergio Andres Gomez Del Real } 412c97d6d2cSSergio Andres Gomez Del Real 413c97d6d2cSSergio Andres Gomez Del Real static void hvf_region_del(MemoryListener *listener, 414c97d6d2cSSergio Andres Gomez Del Real MemoryRegionSection *section) 415c97d6d2cSSergio Andres Gomez Del Real { 416c97d6d2cSSergio Andres Gomez Del Real hvf_set_phys_mem(section, false); 417c97d6d2cSSergio Andres Gomez Del Real } 418c97d6d2cSSergio Andres Gomez Del Real 419c97d6d2cSSergio Andres Gomez Del Real static MemoryListener hvf_memory_listener = { 420c97d6d2cSSergio Andres Gomez Del Real .priority = 10, 421c97d6d2cSSergio Andres Gomez Del Real .region_add = hvf_region_add, 422c97d6d2cSSergio Andres Gomez Del Real .region_del = hvf_region_del, 423babfa20cSSergio Andres Gomez Del Real .log_start = hvf_log_start, 424babfa20cSSergio Andres Gomez Del Real .log_stop = hvf_log_stop, 425babfa20cSSergio Andres Gomez Del Real .log_sync = hvf_log_sync, 426c97d6d2cSSergio Andres Gomez Del Real }; 427c97d6d2cSSergio Andres Gomez Del Real 428c97d6d2cSSergio Andres Gomez Del Real void hvf_reset_vcpu(CPUState *cpu) { 429c97d6d2cSSergio Andres Gomez Del Real 430c97d6d2cSSergio Andres Gomez Del Real /* TODO: this shouldn't be needed; there is already a call to 431c97d6d2cSSergio Andres Gomez Del Real * cpu_synchronize_all_post_reset in vl.c 432c97d6d2cSSergio Andres Gomez Del Real */ 433c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_ENTRY_CTLS, 0); 434c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_IA32_EFER, 0); 435c97d6d2cSSergio Andres Gomez Del Real macvm_set_cr0(cpu->hvf_fd, 0x60000010); 436c97d6d2cSSergio Andres Gomez Del Real 437c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_CR4_MASK, CR4_VMXE_MASK); 438c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_CR4_SHADOW, 0x0); 439c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_CR4, CR4_VMXE_MASK); 440c97d6d2cSSergio Andres Gomez Del Real 441c97d6d2cSSergio Andres Gomez Del Real /* set VMCS guest state fields */ 442c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_CS_SELECTOR, 0xf000); 443c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_CS_LIMIT, 0xffff); 444c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_CS_ACCESS_RIGHTS, 0x9b); 445c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_CS_BASE, 0xffff0000); 446c97d6d2cSSergio Andres Gomez Del Real 447c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_DS_SELECTOR, 0); 448c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_DS_LIMIT, 0xffff); 449c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_DS_ACCESS_RIGHTS, 0x93); 450c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_DS_BASE, 0); 451c97d6d2cSSergio Andres Gomez Del Real 452c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_ES_SELECTOR, 0); 453c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_ES_LIMIT, 0xffff); 454c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_ES_ACCESS_RIGHTS, 0x93); 455c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_ES_BASE, 0); 456c97d6d2cSSergio Andres Gomez Del Real 457c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_FS_SELECTOR, 0); 458c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_FS_LIMIT, 0xffff); 459c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_FS_ACCESS_RIGHTS, 0x93); 460c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_FS_BASE, 0); 461c97d6d2cSSergio Andres Gomez Del Real 462c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_GS_SELECTOR, 0); 463c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_GS_LIMIT, 0xffff); 464c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_GS_ACCESS_RIGHTS, 0x93); 465c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_GS_BASE, 0); 466c97d6d2cSSergio Andres Gomez Del Real 467c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_SS_SELECTOR, 0); 468c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_SS_LIMIT, 0xffff); 469c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_SS_ACCESS_RIGHTS, 0x93); 470c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_SS_BASE, 0); 471c97d6d2cSSergio Andres Gomez Del Real 472c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_LDTR_SELECTOR, 0); 473c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_LDTR_LIMIT, 0); 474c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_LDTR_ACCESS_RIGHTS, 0x10000); 475c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_LDTR_BASE, 0); 476c97d6d2cSSergio Andres Gomez Del Real 477c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_TR_SELECTOR, 0); 478c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_TR_LIMIT, 0); 479c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_TR_ACCESS_RIGHTS, 0x83); 480c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_TR_BASE, 0); 481c97d6d2cSSergio Andres Gomez Del Real 482c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_GDTR_LIMIT, 0); 483c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_GDTR_BASE, 0); 484c97d6d2cSSergio Andres Gomez Del Real 485c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_IDTR_LIMIT, 0); 486c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_IDTR_BASE, 0); 487c97d6d2cSSergio Andres Gomez Del Real 488c97d6d2cSSergio Andres Gomez Del Real /*wvmcs(cpu->hvf_fd, VMCS_GUEST_CR2, 0x0);*/ 489c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_GUEST_CR3, 0x0); 490c97d6d2cSSergio Andres Gomez Del Real 491c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RIP, 0xfff0); 492c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RDX, 0x623); 493c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RFLAGS, 0x2); 494c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RSP, 0x0); 495c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RAX, 0x0); 496c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RBX, 0x0); 497c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RCX, 0x0); 498c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RSI, 0x0); 499c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RDI, 0x0); 500c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RBP, 0x0); 501c97d6d2cSSergio Andres Gomez Del Real 502c97d6d2cSSergio Andres Gomez Del Real for (int i = 0; i < 8; i++) { 503c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_R8 + i, 0x0); 504c97d6d2cSSergio Andres Gomez Del Real } 505c97d6d2cSSergio Andres Gomez Del Real 506c97d6d2cSSergio Andres Gomez Del Real hv_vm_sync_tsc(0); 507c97d6d2cSSergio Andres Gomez Del Real cpu->halted = 0; 508c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_invalidate_tlb(cpu->hvf_fd); 509c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_flush(cpu->hvf_fd); 510c97d6d2cSSergio Andres Gomez Del Real } 511c97d6d2cSSergio Andres Gomez Del Real 512c97d6d2cSSergio Andres Gomez Del Real void hvf_vcpu_destroy(CPUState *cpu) 513c97d6d2cSSergio Andres Gomez Del Real { 514c97d6d2cSSergio Andres Gomez Del Real hv_return_t ret = hv_vcpu_destroy((hv_vcpuid_t)cpu->hvf_fd); 515c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(ret); 516c97d6d2cSSergio Andres Gomez Del Real } 517c97d6d2cSSergio Andres Gomez Del Real 518c97d6d2cSSergio Andres Gomez Del Real static void dummy_signal(int sig) 519c97d6d2cSSergio Andres Gomez Del Real { 520c97d6d2cSSergio Andres Gomez Del Real } 521c97d6d2cSSergio Andres Gomez Del Real 522c97d6d2cSSergio Andres Gomez Del Real int hvf_init_vcpu(CPUState *cpu) 523c97d6d2cSSergio Andres Gomez Del Real { 524c97d6d2cSSergio Andres Gomez Del Real 525c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86cpu = X86_CPU(cpu); 526c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86cpu->env; 527c97d6d2cSSergio Andres Gomez Del Real int r; 528c97d6d2cSSergio Andres Gomez Del Real 529c97d6d2cSSergio Andres Gomez Del Real /* init cpu signals */ 530c97d6d2cSSergio Andres Gomez Del Real sigset_t set; 531c97d6d2cSSergio Andres Gomez Del Real struct sigaction sigact; 532c97d6d2cSSergio Andres Gomez Del Real 533c97d6d2cSSergio Andres Gomez Del Real memset(&sigact, 0, sizeof(sigact)); 534c97d6d2cSSergio Andres Gomez Del Real sigact.sa_handler = dummy_signal; 535c97d6d2cSSergio Andres Gomez Del Real sigaction(SIG_IPI, &sigact, NULL); 536c97d6d2cSSergio Andres Gomez Del Real 537c97d6d2cSSergio Andres Gomez Del Real pthread_sigmask(SIG_BLOCK, NULL, &set); 538c97d6d2cSSergio Andres Gomez Del Real sigdelset(&set, SIG_IPI); 539c97d6d2cSSergio Andres Gomez Del Real 540c97d6d2cSSergio Andres Gomez Del Real init_emu(); 541c97d6d2cSSergio Andres Gomez Del Real init_decoder(); 542c97d6d2cSSergio Andres Gomez Del Real 543c97d6d2cSSergio Andres Gomez Del Real hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1); 544c97d6d2cSSergio Andres Gomez Del Real env->hvf_emul = g_new0(HVFX86EmulatorState, 1); 545c97d6d2cSSergio Andres Gomez Del Real 546c97d6d2cSSergio Andres Gomez Del Real r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT); 547c97d6d2cSSergio Andres Gomez Del Real cpu->vcpu_dirty = 1; 548c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(r); 549c97d6d2cSSergio Andres Gomez Del Real 550c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PINBASED, 551c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_pinbased)) { 552c97d6d2cSSergio Andres Gomez Del Real abort(); 553c97d6d2cSSergio Andres Gomez Del Real } 554c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED, 555c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased)) { 556c97d6d2cSSergio Andres Gomez Del Real abort(); 557c97d6d2cSSergio Andres Gomez Del Real } 558c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2, 559c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased2)) { 560c97d6d2cSSergio Andres Gomez Del Real abort(); 561c97d6d2cSSergio Andres Gomez Del Real } 562c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_ENTRY, 563c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_entry)) { 564c97d6d2cSSergio Andres Gomez Del Real abort(); 565c97d6d2cSSergio Andres Gomez Del Real } 566c97d6d2cSSergio Andres Gomez Del Real 567c97d6d2cSSergio Andres Gomez Del Real /* set VMCS control fields */ 568c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_PIN_BASED_CTLS, 569c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_pinbased, 570c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_EXTINT | 571c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_NMI | 572c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_VNMI)); 573c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_PRI_PROC_BASED_CTLS, 574c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased, 575c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_HLT | 576c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_MWAIT | 577c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TSC_OFFSET | 578c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TPR_SHADOW) | 579c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_SEC_CONTROL); 580c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_SEC_PROC_BASED_CTLS, 581c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased2, 582c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED2_CTLS_APIC_ACCESSES)); 583c97d6d2cSSergio Andres Gomez Del Real 584c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_ENTRY_CTLS, cap2ctrl(hvf_state->hvf_caps->vmx_cap_entry, 585c97d6d2cSSergio Andres Gomez Del Real 0)); 586c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_EXCEPTION_BITMAP, 0); /* Double fault */ 587c97d6d2cSSergio Andres Gomez Del Real 588c97d6d2cSSergio Andres Gomez Del Real wvmcs(cpu->hvf_fd, VMCS_TPR_THRESHOLD, 0); 589c97d6d2cSSergio Andres Gomez Del Real 590c97d6d2cSSergio Andres Gomez Del Real hvf_reset_vcpu(cpu); 591c97d6d2cSSergio Andres Gomez Del Real 592c97d6d2cSSergio Andres Gomez Del Real x86cpu = X86_CPU(cpu); 593f585195eSSergio Andres Gomez Del Real x86cpu->env.kvm_xsave_buf = qemu_memalign(4096, 4096); 594c97d6d2cSSergio Andres Gomez Del Real 595c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_STAR, 1); 596c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_LSTAR, 1); 597c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_CSTAR, 1); 598c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_FMASK, 1); 599c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_FSBASE, 1); 600c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_GSBASE, 1); 601c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_KERNELGSBASE, 1); 602c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_TSC_AUX, 1); 603c97d6d2cSSergio Andres Gomez Del Real /*hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_TSC, 1);*/ 604c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_SYSENTER_CS, 1); 605c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_SYSENTER_EIP, 1); 606c97d6d2cSSergio Andres Gomez Del Real hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_SYSENTER_ESP, 1); 607c97d6d2cSSergio Andres Gomez Del Real 608c97d6d2cSSergio Andres Gomez Del Real return 0; 609c97d6d2cSSergio Andres Gomez Del Real } 610c97d6d2cSSergio Andres Gomez Del Real 611c97d6d2cSSergio Andres Gomez Del Real void hvf_disable(int shouldDisable) 612c97d6d2cSSergio Andres Gomez Del Real { 613c97d6d2cSSergio Andres Gomez Del Real hvf_disabled = shouldDisable; 614c97d6d2cSSergio Andres Gomez Del Real } 615c97d6d2cSSergio Andres Gomez Del Real 616b7394c83SSergio Andres Gomez Del Real static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_info) 617b7394c83SSergio Andres Gomez Del Real { 618b7394c83SSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 619b7394c83SSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 620b7394c83SSergio Andres Gomez Del Real 621b7394c83SSergio Andres Gomez Del Real env->exception_injected = -1; 622b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = -1; 623b7394c83SSergio Andres Gomez Del Real env->nmi_injected = false; 624b7394c83SSergio Andres Gomez Del Real if (idtvec_info & VMCS_IDT_VEC_VALID) { 625b7394c83SSergio Andres Gomez Del Real switch (idtvec_info & VMCS_IDT_VEC_TYPE) { 626b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWINTR: 627b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWINTR: 628b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = idtvec_info & VMCS_IDT_VEC_VECNUM; 629b7394c83SSergio Andres Gomez Del Real break; 630b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_NMI: 631b7394c83SSergio Andres Gomez Del Real env->nmi_injected = true; 632b7394c83SSergio Andres Gomez Del Real break; 633b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWEXCEPTION: 634b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWEXCEPTION: 635b7394c83SSergio Andres Gomez Del Real env->exception_injected = idtvec_info & VMCS_IDT_VEC_VECNUM; 636b7394c83SSergio Andres Gomez Del Real break; 637b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_PRIV_SWEXCEPTION: 638b7394c83SSergio Andres Gomez Del Real default: 639b7394c83SSergio Andres Gomez Del Real abort(); 640b7394c83SSergio Andres Gomez Del Real } 641b7394c83SSergio Andres Gomez Del Real if ((idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWEXCEPTION || 642b7394c83SSergio Andres Gomez Del Real (idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWINTR) { 643b7394c83SSergio Andres Gomez Del Real env->ins_len = ins_len; 644b7394c83SSergio Andres Gomez Del Real } 645b7394c83SSergio Andres Gomez Del Real if (idtvec_info & VMCS_INTR_DEL_ERRCODE) { 646b7394c83SSergio Andres Gomez Del Real env->has_error_code = true; 647b7394c83SSergio Andres Gomez Del Real env->error_code = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_ERROR); 648b7394c83SSergio Andres Gomez Del Real } 649b7394c83SSergio Andres Gomez Del Real } 650b7394c83SSergio Andres Gomez Del Real if ((rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) & 651b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_NMI_BLOCKING)) { 652b7394c83SSergio Andres Gomez Del Real env->hflags2 |= HF2_NMI_MASK; 653b7394c83SSergio Andres Gomez Del Real } else { 654b7394c83SSergio Andres Gomez Del Real env->hflags2 &= ~HF2_NMI_MASK; 655b7394c83SSergio Andres Gomez Del Real } 656b7394c83SSergio Andres Gomez Del Real if (rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) & 657b7394c83SSergio Andres Gomez Del Real (VMCS_INTERRUPTIBILITY_STI_BLOCKING | 658b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) { 659b7394c83SSergio Andres Gomez Del Real env->hflags |= HF_INHIBIT_IRQ_MASK; 660b7394c83SSergio Andres Gomez Del Real } else { 661b7394c83SSergio Andres Gomez Del Real env->hflags &= ~HF_INHIBIT_IRQ_MASK; 662b7394c83SSergio Andres Gomez Del Real } 663b7394c83SSergio Andres Gomez Del Real } 664b7394c83SSergio Andres Gomez Del Real 665c97d6d2cSSergio Andres Gomez Del Real int hvf_vcpu_exec(CPUState *cpu) 666c97d6d2cSSergio Andres Gomez Del Real { 667c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 668c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 669c97d6d2cSSergio Andres Gomez Del Real int ret = 0; 670c97d6d2cSSergio Andres Gomez Del Real uint64_t rip = 0; 671c97d6d2cSSergio Andres Gomez Del Real 672c97d6d2cSSergio Andres Gomez Del Real cpu->halted = 0; 673c97d6d2cSSergio Andres Gomez Del Real 674c97d6d2cSSergio Andres Gomez Del Real if (hvf_process_events(cpu)) { 675c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT; 676c97d6d2cSSergio Andres Gomez Del Real } 677c97d6d2cSSergio Andres Gomez Del Real 678c97d6d2cSSergio Andres Gomez Del Real do { 679c97d6d2cSSergio Andres Gomez Del Real if (cpu->vcpu_dirty) { 680c97d6d2cSSergio Andres Gomez Del Real hvf_put_registers(cpu); 681c97d6d2cSSergio Andres Gomez Del Real cpu->vcpu_dirty = false; 682c97d6d2cSSergio Andres Gomez Del Real } 683c97d6d2cSSergio Andres Gomez Del Real 684b7394c83SSergio Andres Gomez Del Real if (hvf_inject_interrupts(cpu)) { 685b7394c83SSergio Andres Gomez Del Real return EXCP_INTERRUPT; 686b7394c83SSergio Andres Gomez Del Real } 687c97d6d2cSSergio Andres Gomez Del Real vmx_update_tpr(cpu); 688c97d6d2cSSergio Andres Gomez Del Real 689c97d6d2cSSergio Andres Gomez Del Real qemu_mutex_unlock_iothread(); 690c97d6d2cSSergio Andres Gomez Del Real if (!cpu_is_bsp(X86_CPU(cpu)) && cpu->halted) { 691c97d6d2cSSergio Andres Gomez Del Real qemu_mutex_lock_iothread(); 692c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT; 693c97d6d2cSSergio Andres Gomez Del Real } 694c97d6d2cSSergio Andres Gomez Del Real 695c97d6d2cSSergio Andres Gomez Del Real hv_return_t r = hv_vcpu_run(cpu->hvf_fd); 696c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(r); 697c97d6d2cSSergio Andres Gomez Del Real 698c97d6d2cSSergio Andres Gomez Del Real /* handle VMEXIT */ 699c97d6d2cSSergio Andres Gomez Del Real uint64_t exit_reason = rvmcs(cpu->hvf_fd, VMCS_EXIT_REASON); 700c97d6d2cSSergio Andres Gomez Del Real uint64_t exit_qual = rvmcs(cpu->hvf_fd, VMCS_EXIT_QUALIFICATION); 701c97d6d2cSSergio Andres Gomez Del Real uint32_t ins_len = (uint32_t)rvmcs(cpu->hvf_fd, 702c97d6d2cSSergio Andres Gomez Del Real VMCS_EXIT_INSTRUCTION_LENGTH); 703b7394c83SSergio Andres Gomez Del Real 704c97d6d2cSSergio Andres Gomez Del Real uint64_t idtvec_info = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_INFO); 705b7394c83SSergio Andres Gomez Del Real 706b7394c83SSergio Andres Gomez Del Real hvf_store_events(cpu, ins_len, idtvec_info); 707c97d6d2cSSergio Andres Gomez Del Real rip = rreg(cpu->hvf_fd, HV_X86_RIP); 708c97d6d2cSSergio Andres Gomez Del Real RFLAGS(env) = rreg(cpu->hvf_fd, HV_X86_RFLAGS); 709c97d6d2cSSergio Andres Gomez Del Real env->eflags = RFLAGS(env); 710c97d6d2cSSergio Andres Gomez Del Real 711c97d6d2cSSergio Andres Gomez Del Real qemu_mutex_lock_iothread(); 712c97d6d2cSSergio Andres Gomez Del Real 713c97d6d2cSSergio Andres Gomez Del Real update_apic_tpr(cpu); 714c97d6d2cSSergio Andres Gomez Del Real current_cpu = cpu; 715c97d6d2cSSergio Andres Gomez Del Real 716c97d6d2cSSergio Andres Gomez Del Real ret = 0; 717c97d6d2cSSergio Andres Gomez Del Real switch (exit_reason) { 718c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_HLT: { 719c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 720c97d6d2cSSergio Andres Gomez Del Real if (!((cpu->interrupt_request & CPU_INTERRUPT_HARD) && 721c97d6d2cSSergio Andres Gomez Del Real (EFLAGS(env) & IF_MASK)) 722c97d6d2cSSergio Andres Gomez Del Real && !(cpu->interrupt_request & CPU_INTERRUPT_NMI) && 723c97d6d2cSSergio Andres Gomez Del Real !(idtvec_info & VMCS_IDT_VEC_VALID)) { 724c97d6d2cSSergio Andres Gomez Del Real cpu->halted = 1; 725c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_HLT; 726c97d6d2cSSergio Andres Gomez Del Real } 727c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 728c97d6d2cSSergio Andres Gomez Del Real break; 729c97d6d2cSSergio Andres Gomez Del Real } 730c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_MWAIT: { 731c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 732c97d6d2cSSergio Andres Gomez Del Real break; 733c97d6d2cSSergio Andres Gomez Del Real } 734c97d6d2cSSergio Andres Gomez Del Real /* Need to check if MMIO or unmmaped fault */ 735c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EPT_FAULT: 736c97d6d2cSSergio Andres Gomez Del Real { 737c97d6d2cSSergio Andres Gomez Del Real hvf_slot *slot; 738ff2de166SPaolo Bonzini uint64_t gpa = rvmcs(cpu->hvf_fd, VMCS_GUEST_PHYSICAL_ADDRESS); 739c97d6d2cSSergio Andres Gomez Del Real 740c97d6d2cSSergio Andres Gomez Del Real if (((idtvec_info & VMCS_IDT_VEC_VALID) == 0) && 741c97d6d2cSSergio Andres Gomez Del Real ((exit_qual & EXIT_QUAL_NMIUDTI) != 0)) { 742c97d6d2cSSergio Andres Gomez Del Real vmx_set_nmi_blocking(cpu); 743c97d6d2cSSergio Andres Gomez Del Real } 744c97d6d2cSSergio Andres Gomez Del Real 745c97d6d2cSSergio Andres Gomez Del Real slot = hvf_find_overlap_slot(gpa, gpa); 746c97d6d2cSSergio Andres Gomez Del Real /* mmio */ 747babfa20cSSergio Andres Gomez Del Real if (ept_emulation_fault(slot, gpa, exit_qual)) { 748c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 749c97d6d2cSSergio Andres Gomez Del Real 750c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 751c97d6d2cSSergio Andres Gomez Del Real env->hvf_emul->fetch_rip = rip; 752c97d6d2cSSergio Andres Gomez Del Real 753c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 754c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 755c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 756c97d6d2cSSergio Andres Gomez Del Real break; 757c97d6d2cSSergio Andres Gomez Del Real } 758c97d6d2cSSergio Andres Gomez Del Real break; 759c97d6d2cSSergio Andres Gomez Del Real } 760c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INOUT: 761c97d6d2cSSergio Andres Gomez Del Real { 762c97d6d2cSSergio Andres Gomez Del Real uint32_t in = (exit_qual & 8) != 0; 763c97d6d2cSSergio Andres Gomez Del Real uint32_t size = (exit_qual & 7) + 1; 764c97d6d2cSSergio Andres Gomez Del Real uint32_t string = (exit_qual & 16) != 0; 765c97d6d2cSSergio Andres Gomez Del Real uint32_t port = exit_qual >> 16; 766c97d6d2cSSergio Andres Gomez Del Real /*uint32_t rep = (exit_qual & 0x20) != 0;*/ 767c97d6d2cSSergio Andres Gomez Del Real 768c97d6d2cSSergio Andres Gomez Del Real if (!string && in) { 769c97d6d2cSSergio Andres Gomez Del Real uint64_t val = 0; 770c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 771c97d6d2cSSergio Andres Gomez Del Real hvf_handle_io(env, port, &val, 0, size, 1); 772c97d6d2cSSergio Andres Gomez Del Real if (size == 1) { 773c97d6d2cSSergio Andres Gomez Del Real AL(env) = val; 774c97d6d2cSSergio Andres Gomez Del Real } else if (size == 2) { 775c97d6d2cSSergio Andres Gomez Del Real AX(env) = val; 776c97d6d2cSSergio Andres Gomez Del Real } else if (size == 4) { 777c97d6d2cSSergio Andres Gomez Del Real RAX(env) = (uint32_t)val; 778c97d6d2cSSergio Andres Gomez Del Real } else { 779da20f5cdSPaolo Bonzini RAX(env) = (uint64_t)val; 780c97d6d2cSSergio Andres Gomez Del Real } 781c97d6d2cSSergio Andres Gomez Del Real RIP(env) += ins_len; 782c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 783c97d6d2cSSergio Andres Gomez Del Real break; 784c97d6d2cSSergio Andres Gomez Del Real } else if (!string && !in) { 785c97d6d2cSSergio Andres Gomez Del Real RAX(env) = rreg(cpu->hvf_fd, HV_X86_RAX); 786c97d6d2cSSergio Andres Gomez Del Real hvf_handle_io(env, port, &RAX(env), 1, size, 1); 787c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 788c97d6d2cSSergio Andres Gomez Del Real break; 789c97d6d2cSSergio Andres Gomez Del Real } 790c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 791c97d6d2cSSergio Andres Gomez Del Real 792c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 793c97d6d2cSSergio Andres Gomez Del Real env->hvf_emul->fetch_rip = rip; 794c97d6d2cSSergio Andres Gomez Del Real 795c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 796e62963bfSPaolo Bonzini assert(ins_len == decode.len); 797c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 798c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 799c97d6d2cSSergio Andres Gomez Del Real 800c97d6d2cSSergio Andres Gomez Del Real break; 801c97d6d2cSSergio Andres Gomez Del Real } 802c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CPUID: { 803c97d6d2cSSergio Andres Gomez Del Real uint32_t rax = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RAX); 804c97d6d2cSSergio Andres Gomez Del Real uint32_t rbx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RBX); 805c97d6d2cSSergio Andres Gomez Del Real uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX); 806c97d6d2cSSergio Andres Gomez Del Real uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX); 807c97d6d2cSSergio Andres Gomez Del Real 808c97d6d2cSSergio Andres Gomez Del Real cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx); 809c97d6d2cSSergio Andres Gomez Del Real 810c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RAX, rax); 811c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RBX, rbx); 812c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RCX, rcx); 813c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RDX, rdx); 814c97d6d2cSSergio Andres Gomez Del Real 815c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 816c97d6d2cSSergio Andres Gomez Del Real break; 817c97d6d2cSSergio Andres Gomez Del Real } 818c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_XSETBV: { 819c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 820c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env; 821c97d6d2cSSergio Andres Gomez Del Real uint32_t eax = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RAX); 822c97d6d2cSSergio Andres Gomez Del Real uint32_t ecx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX); 823c97d6d2cSSergio Andres Gomez Del Real uint32_t edx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX); 824c97d6d2cSSergio Andres Gomez Del Real 825c97d6d2cSSergio Andres Gomez Del Real if (ecx) { 826c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 827c97d6d2cSSergio Andres Gomez Del Real break; 828c97d6d2cSSergio Andres Gomez Del Real } 829c97d6d2cSSergio Andres Gomez Del Real env->xcr0 = ((uint64_t)edx << 32) | eax; 830c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_XCR0, env->xcr0 | 1); 831c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 832c97d6d2cSSergio Andres Gomez Del Real break; 833c97d6d2cSSergio Andres Gomez Del Real } 834c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INTR_WINDOW: 835c97d6d2cSSergio Andres Gomez Del Real vmx_clear_int_window_exiting(cpu); 836c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 837c97d6d2cSSergio Andres Gomez Del Real break; 838c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_NMI_WINDOW: 839c97d6d2cSSergio Andres Gomez Del Real vmx_clear_nmi_window_exiting(cpu); 840c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 841c97d6d2cSSergio Andres Gomez Del Real break; 842c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EXT_INTR: 843c97d6d2cSSergio Andres Gomez Del Real /* force exit and allow io handling */ 844c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 845c97d6d2cSSergio Andres Gomez Del Real break; 846c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDMSR: 847c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_WRMSR: 848c97d6d2cSSergio Andres Gomez Del Real { 849c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 850c97d6d2cSSergio Andres Gomez Del Real if (exit_reason == EXIT_REASON_RDMSR) { 851c97d6d2cSSergio Andres Gomez Del Real simulate_rdmsr(cpu); 852c97d6d2cSSergio Andres Gomez Del Real } else { 853c97d6d2cSSergio Andres Gomez Del Real simulate_wrmsr(cpu); 854c97d6d2cSSergio Andres Gomez Del Real } 855c97d6d2cSSergio Andres Gomez Del Real RIP(env) += rvmcs(cpu->hvf_fd, VMCS_EXIT_INSTRUCTION_LENGTH); 856c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 857c97d6d2cSSergio Andres Gomez Del Real break; 858c97d6d2cSSergio Andres Gomez Del Real } 859c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CR_ACCESS: { 860c97d6d2cSSergio Andres Gomez Del Real int cr; 861c97d6d2cSSergio Andres Gomez Del Real int reg; 862c97d6d2cSSergio Andres Gomez Del Real 863c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 864c97d6d2cSSergio Andres Gomez Del Real cr = exit_qual & 15; 865c97d6d2cSSergio Andres Gomez Del Real reg = (exit_qual >> 8) & 15; 866c97d6d2cSSergio Andres Gomez Del Real 867c97d6d2cSSergio Andres Gomez Del Real switch (cr) { 868c97d6d2cSSergio Andres Gomez Del Real case 0x0: { 869c97d6d2cSSergio Andres Gomez Del Real macvm_set_cr0(cpu->hvf_fd, RRX(env, reg)); 870c97d6d2cSSergio Andres Gomez Del Real break; 871c97d6d2cSSergio Andres Gomez Del Real } 872c97d6d2cSSergio Andres Gomez Del Real case 4: { 873c97d6d2cSSergio Andres Gomez Del Real macvm_set_cr4(cpu->hvf_fd, RRX(env, reg)); 874c97d6d2cSSergio Andres Gomez Del Real break; 875c97d6d2cSSergio Andres Gomez Del Real } 876c97d6d2cSSergio Andres Gomez Del Real case 8: { 877c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu); 878c97d6d2cSSergio Andres Gomez Del Real if (exit_qual & 0x10) { 879c97d6d2cSSergio Andres Gomez Del Real RRX(env, reg) = cpu_get_apic_tpr(x86_cpu->apic_state); 880c97d6d2cSSergio Andres Gomez Del Real } else { 881c97d6d2cSSergio Andres Gomez Del Real int tpr = RRX(env, reg); 882c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr); 883c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 884c97d6d2cSSergio Andres Gomez Del Real } 885c97d6d2cSSergio Andres Gomez Del Real break; 886c97d6d2cSSergio Andres Gomez Del Real } 887c97d6d2cSSergio Andres Gomez Del Real default: 888c97d6d2cSSergio Andres Gomez Del Real error_report("Unrecognized CR %d\n", cr); 889c97d6d2cSSergio Andres Gomez Del Real abort(); 890c97d6d2cSSergio Andres Gomez Del Real } 891c97d6d2cSSergio Andres Gomez Del Real RIP(env) += ins_len; 892c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 893c97d6d2cSSergio Andres Gomez Del Real break; 894c97d6d2cSSergio Andres Gomez Del Real } 895c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_APIC_ACCESS: { /* TODO */ 896c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode; 897c97d6d2cSSergio Andres Gomez Del Real 898c97d6d2cSSergio Andres Gomez Del Real load_regs(cpu); 899c97d6d2cSSergio Andres Gomez Del Real env->hvf_emul->fetch_rip = rip; 900c97d6d2cSSergio Andres Gomez Del Real 901c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode); 902c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode); 903c97d6d2cSSergio Andres Gomez Del Real store_regs(cpu); 904c97d6d2cSSergio Andres Gomez Del Real break; 905c97d6d2cSSergio Andres Gomez Del Real } 906c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TPR: { 907c97d6d2cSSergio Andres Gomez Del Real ret = 1; 908c97d6d2cSSergio Andres Gomez Del Real break; 909c97d6d2cSSergio Andres Gomez Del Real } 910c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TASK_SWITCH: { 911c97d6d2cSSergio Andres Gomez Del Real uint64_t vinfo = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_INFO); 912c97d6d2cSSergio Andres Gomez Del Real x68_segment_selector sel = {.sel = exit_qual & 0xffff}; 913c97d6d2cSSergio Andres Gomez Del Real vmx_handle_task_switch(cpu, sel, (exit_qual >> 30) & 0x3, 914c97d6d2cSSergio Andres Gomez Del Real vinfo & VMCS_INTR_VALID, vinfo & VECTORING_INFO_VECTOR_MASK, vinfo 915c97d6d2cSSergio Andres Gomez Del Real & VMCS_INTR_T_MASK); 916c97d6d2cSSergio Andres Gomez Del Real break; 917c97d6d2cSSergio Andres Gomez Del Real } 918c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TRIPLE_FAULT: { 919c97d6d2cSSergio Andres Gomez Del Real qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 920c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT; 921c97d6d2cSSergio Andres Gomez Del Real break; 922c97d6d2cSSergio Andres Gomez Del Real } 923c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDPMC: 924c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RAX, 0); 925c97d6d2cSSergio Andres Gomez Del Real wreg(cpu->hvf_fd, HV_X86_RDX, 0); 926c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len); 927c97d6d2cSSergio Andres Gomez Del Real break; 928c97d6d2cSSergio Andres Gomez Del Real case VMX_REASON_VMCALL: 9293010460fSSergio Andres Gomez Del Real env->exception_injected = EXCP0D_GPF; 9303010460fSSergio Andres Gomez Del Real env->has_error_code = true; 9313010460fSSergio Andres Gomez Del Real env->error_code = 0; 932c97d6d2cSSergio Andres Gomez Del Real break; 933c97d6d2cSSergio Andres Gomez Del Real default: 934c97d6d2cSSergio Andres Gomez Del Real error_report("%llx: unhandled exit %llx\n", rip, exit_reason); 935c97d6d2cSSergio Andres Gomez Del Real } 936c97d6d2cSSergio Andres Gomez Del Real } while (ret == 0); 937c97d6d2cSSergio Andres Gomez Del Real 938c97d6d2cSSergio Andres Gomez Del Real return ret; 939c97d6d2cSSergio Andres Gomez Del Real } 940c97d6d2cSSergio Andres Gomez Del Real 941c97d6d2cSSergio Andres Gomez Del Real static bool hvf_allowed; 942c97d6d2cSSergio Andres Gomez Del Real 943c97d6d2cSSergio Andres Gomez Del Real static int hvf_accel_init(MachineState *ms) 944c97d6d2cSSergio Andres Gomez Del Real { 945c97d6d2cSSergio Andres Gomez Del Real int x; 946c97d6d2cSSergio Andres Gomez Del Real hv_return_t ret; 947c97d6d2cSSergio Andres Gomez Del Real HVFState *s; 948c97d6d2cSSergio Andres Gomez Del Real 949c97d6d2cSSergio Andres Gomez Del Real hvf_disable(0); 950c97d6d2cSSergio Andres Gomez Del Real ret = hv_vm_create(HV_VM_DEFAULT); 951c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(ret); 952c97d6d2cSSergio Andres Gomez Del Real 953c97d6d2cSSergio Andres Gomez Del Real s = g_new0(HVFState, 1); 954c97d6d2cSSergio Andres Gomez Del Real 955c97d6d2cSSergio Andres Gomez Del Real s->num_slots = 32; 956c97d6d2cSSergio Andres Gomez Del Real for (x = 0; x < s->num_slots; ++x) { 957c97d6d2cSSergio Andres Gomez Del Real s->slots[x].size = 0; 958c97d6d2cSSergio Andres Gomez Del Real s->slots[x].slot_id = x; 959c97d6d2cSSergio Andres Gomez Del Real } 960c97d6d2cSSergio Andres Gomez Del Real 961c97d6d2cSSergio Andres Gomez Del Real hvf_state = s; 962c97d6d2cSSergio Andres Gomez Del Real cpu_interrupt_handler = hvf_handle_interrupt; 963c97d6d2cSSergio Andres Gomez Del Real memory_listener_register(&hvf_memory_listener, &address_space_memory); 964c97d6d2cSSergio Andres Gomez Del Real return 0; 965c97d6d2cSSergio Andres Gomez Del Real } 966c97d6d2cSSergio Andres Gomez Del Real 967c97d6d2cSSergio Andres Gomez Del Real static void hvf_accel_class_init(ObjectClass *oc, void *data) 968c97d6d2cSSergio Andres Gomez Del Real { 969c97d6d2cSSergio Andres Gomez Del Real AccelClass *ac = ACCEL_CLASS(oc); 970c97d6d2cSSergio Andres Gomez Del Real ac->name = "HVF"; 971c97d6d2cSSergio Andres Gomez Del Real ac->init_machine = hvf_accel_init; 972c97d6d2cSSergio Andres Gomez Del Real ac->allowed = &hvf_allowed; 973c97d6d2cSSergio Andres Gomez Del Real } 974c97d6d2cSSergio Andres Gomez Del Real 975c97d6d2cSSergio Andres Gomez Del Real static const TypeInfo hvf_accel_type = { 976c97d6d2cSSergio Andres Gomez Del Real .name = TYPE_HVF_ACCEL, 977c97d6d2cSSergio Andres Gomez Del Real .parent = TYPE_ACCEL, 978c97d6d2cSSergio Andres Gomez Del Real .class_init = hvf_accel_class_init, 979c97d6d2cSSergio Andres Gomez Del Real }; 980c97d6d2cSSergio Andres Gomez Del Real 981c97d6d2cSSergio Andres Gomez Del Real static void hvf_type_init(void) 982c97d6d2cSSergio Andres Gomez Del Real { 983c97d6d2cSSergio Andres Gomez Del Real type_register_static(&hvf_accel_type); 984c97d6d2cSSergio Andres Gomez Del Real } 985c97d6d2cSSergio Andres Gomez Del Real 986c97d6d2cSSergio Andres Gomez Del Real type_init(hvf_type_init); 987