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