xref: /qemu/target/i386/hvf/hvf.c (revision 3b502b0e470867369ba6e0a94e9ba6d91bb176c2)
1c97d6d2cSSergio Andres Gomez Del Real /* Copyright 2008 IBM Corporation
2c97d6d2cSSergio Andres Gomez Del Real  *           2008 Red Hat, Inc.
3c97d6d2cSSergio Andres Gomez Del Real  * Copyright 2011 Intel Corporation
4c97d6d2cSSergio Andres Gomez Del Real  * Copyright 2016 Veertu, Inc.
5c97d6d2cSSergio Andres Gomez Del Real  * Copyright 2017 The Android Open Source Project
6c97d6d2cSSergio Andres Gomez Del Real  *
7c97d6d2cSSergio Andres Gomez Del Real  * QEMU Hypervisor.framework support
8c97d6d2cSSergio Andres Gomez Del Real  *
9c97d6d2cSSergio Andres Gomez Del Real  * This program is free software; you can redistribute it and/or
10c97d6d2cSSergio Andres Gomez Del Real  * modify it under the terms of version 2 of the GNU General Public
11c97d6d2cSSergio Andres Gomez Del Real  * License as published by the Free Software Foundation.
12c97d6d2cSSergio Andres Gomez Del Real  *
13c97d6d2cSSergio Andres Gomez Del Real  * This program is distributed in the hope that it will be useful,
14c97d6d2cSSergio Andres Gomez Del Real  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15c97d6d2cSSergio Andres Gomez Del Real  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16e361a772SThomas Huth  * General Public License for more details.
17c97d6d2cSSergio Andres Gomez Del Real  *
18e361a772SThomas Huth  * You should have received a copy of the GNU General Public License
19e361a772SThomas Huth  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20d781e24dSIzik Eidus  *
21d781e24dSIzik Eidus  * This file contain code under public domain from the hvdos project:
22d781e24dSIzik Eidus  * https://github.com/mist64/hvdos
234d98a8e5SPaolo Bonzini  *
244d98a8e5SPaolo Bonzini  * Parts Copyright (c) 2011 NetApp, Inc.
254d98a8e5SPaolo Bonzini  * All rights reserved.
264d98a8e5SPaolo Bonzini  *
274d98a8e5SPaolo Bonzini  * Redistribution and use in source and binary forms, with or without
284d98a8e5SPaolo Bonzini  * modification, are permitted provided that the following conditions
294d98a8e5SPaolo Bonzini  * are met:
304d98a8e5SPaolo Bonzini  * 1. Redistributions of source code must retain the above copyright
314d98a8e5SPaolo Bonzini  *    notice, this list of conditions and the following disclaimer.
324d98a8e5SPaolo Bonzini  * 2. Redistributions in binary form must reproduce the above copyright
334d98a8e5SPaolo Bonzini  *    notice, this list of conditions and the following disclaimer in the
344d98a8e5SPaolo Bonzini  *    documentation and/or other materials provided with the distribution.
354d98a8e5SPaolo Bonzini  *
364d98a8e5SPaolo Bonzini  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
374d98a8e5SPaolo Bonzini  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
384d98a8e5SPaolo Bonzini  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
394d98a8e5SPaolo Bonzini  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
404d98a8e5SPaolo Bonzini  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
414d98a8e5SPaolo Bonzini  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
424d98a8e5SPaolo Bonzini  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
434d98a8e5SPaolo Bonzini  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
444d98a8e5SPaolo Bonzini  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
454d98a8e5SPaolo Bonzini  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
464d98a8e5SPaolo Bonzini  * SUCH DAMAGE.
47c97d6d2cSSergio Andres Gomez Del Real  */
4854d31236SMarkus Armbruster 
49c97d6d2cSSergio Andres Gomez Del Real #include "qemu/osdep.h"
50c97d6d2cSSergio Andres Gomez Del Real #include "qemu-common.h"
51c97d6d2cSSergio Andres Gomez Del Real #include "qemu/error-report.h"
52c97d6d2cSSergio Andres Gomez Del Real 
53c97d6d2cSSergio Andres Gomez Del Real #include "sysemu/hvf.h"
5454d31236SMarkus Armbruster #include "sysemu/runstate.h"
55c97d6d2cSSergio Andres Gomez Del Real #include "hvf-i386.h"
5669e0a03cSPaolo Bonzini #include "vmcs.h"
5769e0a03cSPaolo Bonzini #include "vmx.h"
5869e0a03cSPaolo Bonzini #include "x86.h"
5969e0a03cSPaolo Bonzini #include "x86_descr.h"
6069e0a03cSPaolo Bonzini #include "x86_mmu.h"
6169e0a03cSPaolo Bonzini #include "x86_decode.h"
6269e0a03cSPaolo Bonzini #include "x86_emu.h"
6369e0a03cSPaolo Bonzini #include "x86_task.h"
6469e0a03cSPaolo Bonzini #include "x86hvf.h"
65c97d6d2cSSergio Andres Gomez Del Real 
66c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv.h>
67c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv_vmx.h>
68*3b502b0eSVladislav Yaroshchuk #include <sys/sysctl.h>
69c97d6d2cSSergio Andres Gomez Del Real 
70c97d6d2cSSergio Andres Gomez Del Real #include "exec/address-spaces.h"
71c97d6d2cSSergio Andres Gomez Del Real #include "hw/i386/apic_internal.h"
72c97d6d2cSSergio Andres Gomez Del Real #include "qemu/main-loop.h"
73940e43aaSClaudio Fontana #include "qemu/accel.h"
74c97d6d2cSSergio Andres Gomez Del Real #include "target/i386/cpu.h"
75c97d6d2cSSergio Andres Gomez Del Real 
76b86f59c7SClaudio Fontana #include "hvf-accel-ops.h"
77b52bcba7SClaudio Fontana 
78c97d6d2cSSergio Andres Gomez Del Real HVFState *hvf_state;
79c97d6d2cSSergio Andres Gomez Del Real 
80c97d6d2cSSergio Andres Gomez Del Real static void assert_hvf_ok(hv_return_t ret)
81c97d6d2cSSergio Andres Gomez Del Real {
82c97d6d2cSSergio Andres Gomez Del Real     if (ret == HV_SUCCESS) {
83c97d6d2cSSergio Andres Gomez Del Real         return;
84c97d6d2cSSergio Andres Gomez Del Real     }
85c97d6d2cSSergio Andres Gomez Del Real 
86c97d6d2cSSergio Andres Gomez Del Real     switch (ret) {
87c97d6d2cSSergio Andres Gomez Del Real     case HV_ERROR:
882d9178d9SLaurent Vivier         error_report("Error: HV_ERROR");
89c97d6d2cSSergio Andres Gomez Del Real         break;
90c97d6d2cSSergio Andres Gomez Del Real     case HV_BUSY:
912d9178d9SLaurent Vivier         error_report("Error: HV_BUSY");
92c97d6d2cSSergio Andres Gomez Del Real         break;
93c97d6d2cSSergio Andres Gomez Del Real     case HV_BAD_ARGUMENT:
942d9178d9SLaurent Vivier         error_report("Error: HV_BAD_ARGUMENT");
95c97d6d2cSSergio Andres Gomez Del Real         break;
96c97d6d2cSSergio Andres Gomez Del Real     case HV_NO_RESOURCES:
972d9178d9SLaurent Vivier         error_report("Error: HV_NO_RESOURCES");
98c97d6d2cSSergio Andres Gomez Del Real         break;
99c97d6d2cSSergio Andres Gomez Del Real     case HV_NO_DEVICE:
1002d9178d9SLaurent Vivier         error_report("Error: HV_NO_DEVICE");
101c97d6d2cSSergio Andres Gomez Del Real         break;
102c97d6d2cSSergio Andres Gomez Del Real     case HV_UNSUPPORTED:
1032d9178d9SLaurent Vivier         error_report("Error: HV_UNSUPPORTED");
104c97d6d2cSSergio Andres Gomez Del Real         break;
105c97d6d2cSSergio Andres Gomez Del Real     default:
1062d9178d9SLaurent Vivier         error_report("Unknown Error");
107c97d6d2cSSergio Andres Gomez Del Real     }
108c97d6d2cSSergio Andres Gomez Del Real 
109c97d6d2cSSergio Andres Gomez Del Real     abort();
110c97d6d2cSSergio Andres Gomez Del Real }
111c97d6d2cSSergio Andres Gomez Del Real 
112c97d6d2cSSergio Andres Gomez Del Real /* Memory slots */
113fbafbb6dSCameron Esfahani hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size)
114c97d6d2cSSergio Andres Gomez Del Real {
115c97d6d2cSSergio Andres Gomez Del Real     hvf_slot *slot;
116c97d6d2cSSergio Andres Gomez Del Real     int x;
117c97d6d2cSSergio Andres Gomez Del Real     for (x = 0; x < hvf_state->num_slots; ++x) {
118c97d6d2cSSergio Andres Gomez Del Real         slot = &hvf_state->slots[x];
119c97d6d2cSSergio Andres Gomez Del Real         if (slot->size && start < (slot->start + slot->size) &&
120fbafbb6dSCameron Esfahani             (start + size) > slot->start) {
121c97d6d2cSSergio Andres Gomez Del Real             return slot;
122c97d6d2cSSergio Andres Gomez Del Real         }
123c97d6d2cSSergio Andres Gomez Del Real     }
124c97d6d2cSSergio Andres Gomez Del Real     return NULL;
125c97d6d2cSSergio Andres Gomez Del Real }
126c97d6d2cSSergio Andres Gomez Del Real 
127c97d6d2cSSergio Andres Gomez Del Real struct mac_slot {
128c97d6d2cSSergio Andres Gomez Del Real     int present;
129c97d6d2cSSergio Andres Gomez Del Real     uint64_t size;
130c97d6d2cSSergio Andres Gomez Del Real     uint64_t gpa_start;
131c97d6d2cSSergio Andres Gomez Del Real     uint64_t gva;
132c97d6d2cSSergio Andres Gomez Del Real };
133c97d6d2cSSergio Andres Gomez Del Real 
134c97d6d2cSSergio Andres Gomez Del Real struct mac_slot mac_slots[32];
135c97d6d2cSSergio Andres Gomez Del Real 
136fbafbb6dSCameron Esfahani static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags)
137c97d6d2cSSergio Andres Gomez Del Real {
138c97d6d2cSSergio Andres Gomez Del Real     struct mac_slot *macslot;
139c97d6d2cSSergio Andres Gomez Del Real     hv_return_t ret;
140c97d6d2cSSergio Andres Gomez Del Real 
141c97d6d2cSSergio Andres Gomez Del Real     macslot = &mac_slots[slot->slot_id];
142c97d6d2cSSergio Andres Gomez Del Real 
143c97d6d2cSSergio Andres Gomez Del Real     if (macslot->present) {
144c97d6d2cSSergio Andres Gomez Del Real         if (macslot->size != slot->size) {
145c97d6d2cSSergio Andres Gomez Del Real             macslot->present = 0;
146c97d6d2cSSergio Andres Gomez Del Real             ret = hv_vm_unmap(macslot->gpa_start, macslot->size);
147c97d6d2cSSergio Andres Gomez Del Real             assert_hvf_ok(ret);
148c97d6d2cSSergio Andres Gomez Del Real         }
149c97d6d2cSSergio Andres Gomez Del Real     }
150c97d6d2cSSergio Andres Gomez Del Real 
151c97d6d2cSSergio Andres Gomez Del Real     if (!slot->size) {
152c97d6d2cSSergio Andres Gomez Del Real         return 0;
153c97d6d2cSSergio Andres Gomez Del Real     }
154c97d6d2cSSergio Andres Gomez Del Real 
155c97d6d2cSSergio Andres Gomez Del Real     macslot->present = 1;
156c97d6d2cSSergio Andres Gomez Del Real     macslot->gpa_start = slot->start;
157c97d6d2cSSergio Andres Gomez Del Real     macslot->size = slot->size;
158c97d6d2cSSergio Andres Gomez Del Real     ret = hv_vm_map((hv_uvaddr_t)slot->mem, slot->start, slot->size, flags);
159c97d6d2cSSergio Andres Gomez Del Real     assert_hvf_ok(ret);
160c97d6d2cSSergio Andres Gomez Del Real     return 0;
161c97d6d2cSSergio Andres Gomez Del Real }
162c97d6d2cSSergio Andres Gomez Del Real 
163c97d6d2cSSergio Andres Gomez Del Real void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
164c97d6d2cSSergio Andres Gomez Del Real {
165c97d6d2cSSergio Andres Gomez Del Real     hvf_slot *mem;
166c97d6d2cSSergio Andres Gomez Del Real     MemoryRegion *area = section->mr;
167fbafbb6dSCameron Esfahani     bool writeable = !area->readonly && !area->rom_device;
168fbafbb6dSCameron Esfahani     hv_memory_flags_t flags;
169c97d6d2cSSergio Andres Gomez Del Real 
170c97d6d2cSSergio Andres Gomez Del Real     if (!memory_region_is_ram(area)) {
171fbafbb6dSCameron Esfahani         if (writeable) {
172c97d6d2cSSergio Andres Gomez Del Real             return;
173fbafbb6dSCameron Esfahani         } else if (!memory_region_is_romd(area)) {
174fbafbb6dSCameron Esfahani             /*
175fbafbb6dSCameron Esfahani              * If the memory device is not in romd_mode, then we actually want
176fbafbb6dSCameron Esfahani              * to remove the hvf memory slot so all accesses will trap.
177fbafbb6dSCameron Esfahani              */
178fbafbb6dSCameron Esfahani              add = false;
179fbafbb6dSCameron Esfahani         }
180c97d6d2cSSergio Andres Gomez Del Real     }
181c97d6d2cSSergio Andres Gomez Del Real 
182c97d6d2cSSergio Andres Gomez Del Real     mem = hvf_find_overlap_slot(
183c97d6d2cSSergio Andres Gomez Del Real             section->offset_within_address_space,
184fbafbb6dSCameron Esfahani             int128_get64(section->size));
185c97d6d2cSSergio Andres Gomez Del Real 
186c97d6d2cSSergio Andres Gomez Del Real     if (mem && add) {
187c97d6d2cSSergio Andres Gomez Del Real         if (mem->size == int128_get64(section->size) &&
188c97d6d2cSSergio Andres Gomez Del Real             mem->start == section->offset_within_address_space &&
189c97d6d2cSSergio Andres Gomez Del Real             mem->mem == (memory_region_get_ram_ptr(area) +
190c97d6d2cSSergio Andres Gomez Del Real             section->offset_within_region)) {
191c97d6d2cSSergio Andres Gomez Del Real             return; /* Same region was attempted to register, go away. */
192c97d6d2cSSergio Andres Gomez Del Real         }
193c97d6d2cSSergio Andres Gomez Del Real     }
194c97d6d2cSSergio Andres Gomez Del Real 
195c97d6d2cSSergio Andres Gomez Del Real     /* Region needs to be reset. set the size to 0 and remap it. */
196c97d6d2cSSergio Andres Gomez Del Real     if (mem) {
197c97d6d2cSSergio Andres Gomez Del Real         mem->size = 0;
198fbafbb6dSCameron Esfahani         if (do_hvf_set_memory(mem, 0)) {
1992d9178d9SLaurent Vivier             error_report("Failed to reset overlapping slot");
200c97d6d2cSSergio Andres Gomez Del Real             abort();
201c97d6d2cSSergio Andres Gomez Del Real         }
202c97d6d2cSSergio Andres Gomez Del Real     }
203c97d6d2cSSergio Andres Gomez Del Real 
204c97d6d2cSSergio Andres Gomez Del Real     if (!add) {
205c97d6d2cSSergio Andres Gomez Del Real         return;
206c97d6d2cSSergio Andres Gomez Del Real     }
207c97d6d2cSSergio Andres Gomez Del Real 
208fbafbb6dSCameron Esfahani     if (area->readonly ||
209fbafbb6dSCameron Esfahani         (!memory_region_is_ram(area) && memory_region_is_romd(area))) {
210fbafbb6dSCameron Esfahani         flags = HV_MEMORY_READ | HV_MEMORY_EXEC;
211fbafbb6dSCameron Esfahani     } else {
212fbafbb6dSCameron Esfahani         flags = HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC;
213fbafbb6dSCameron Esfahani     }
214fbafbb6dSCameron Esfahani 
215c97d6d2cSSergio Andres Gomez Del Real     /* Now make a new slot. */
216c97d6d2cSSergio Andres Gomez Del Real     int x;
217c97d6d2cSSergio Andres Gomez Del Real 
218c97d6d2cSSergio Andres Gomez Del Real     for (x = 0; x < hvf_state->num_slots; ++x) {
219c97d6d2cSSergio Andres Gomez Del Real         mem = &hvf_state->slots[x];
220c97d6d2cSSergio Andres Gomez Del Real         if (!mem->size) {
221c97d6d2cSSergio Andres Gomez Del Real             break;
222c97d6d2cSSergio Andres Gomez Del Real         }
223c97d6d2cSSergio Andres Gomez Del Real     }
224c97d6d2cSSergio Andres Gomez Del Real 
225c97d6d2cSSergio Andres Gomez Del Real     if (x == hvf_state->num_slots) {
2262d9178d9SLaurent Vivier         error_report("No free slots");
227c97d6d2cSSergio Andres Gomez Del Real         abort();
228c97d6d2cSSergio Andres Gomez Del Real     }
229c97d6d2cSSergio Andres Gomez Del Real 
230c97d6d2cSSergio Andres Gomez Del Real     mem->size = int128_get64(section->size);
231c97d6d2cSSergio Andres Gomez Del Real     mem->mem = memory_region_get_ram_ptr(area) + section->offset_within_region;
232c97d6d2cSSergio Andres Gomez Del Real     mem->start = section->offset_within_address_space;
233babfa20cSSergio Andres Gomez Del Real     mem->region = area;
234c97d6d2cSSergio Andres Gomez Del Real 
235fbafbb6dSCameron Esfahani     if (do_hvf_set_memory(mem, flags)) {
2362d9178d9SLaurent Vivier         error_report("Error registering new memory slot");
237c97d6d2cSSergio Andres Gomez Del Real         abort();
238c97d6d2cSSergio Andres Gomez Del Real     }
239c97d6d2cSSergio Andres Gomez Del Real }
240c97d6d2cSSergio Andres Gomez Del Real 
241c97d6d2cSSergio Andres Gomez Del Real void vmx_update_tpr(CPUState *cpu)
242c97d6d2cSSergio Andres Gomez Del Real {
243c97d6d2cSSergio Andres Gomez Del Real     /* TODO: need integrate APIC handling */
244c97d6d2cSSergio Andres Gomez Del Real     X86CPU *x86_cpu = X86_CPU(cpu);
245c97d6d2cSSergio Andres Gomez Del Real     int tpr = cpu_get_apic_tpr(x86_cpu->apic_state) << 4;
246c97d6d2cSSergio Andres Gomez Del Real     int irr = apic_get_highest_priority_irr(x86_cpu->apic_state);
247c97d6d2cSSergio Andres Gomez Del Real 
248c97d6d2cSSergio Andres Gomez Del Real     wreg(cpu->hvf_fd, HV_X86_TPR, tpr);
249c97d6d2cSSergio Andres Gomez Del Real     if (irr == -1) {
250c97d6d2cSSergio Andres Gomez Del Real         wvmcs(cpu->hvf_fd, VMCS_TPR_THRESHOLD, 0);
251c97d6d2cSSergio Andres Gomez Del Real     } else {
252c97d6d2cSSergio Andres Gomez Del Real         wvmcs(cpu->hvf_fd, VMCS_TPR_THRESHOLD, (irr > tpr) ? tpr >> 4 :
253c97d6d2cSSergio Andres Gomez Del Real               irr >> 4);
254c97d6d2cSSergio Andres Gomez Del Real     }
255c97d6d2cSSergio Andres Gomez Del Real }
256c97d6d2cSSergio Andres Gomez Del Real 
257583ae161SRoman Bolshakov static void update_apic_tpr(CPUState *cpu)
258c97d6d2cSSergio Andres Gomez Del Real {
259c97d6d2cSSergio Andres Gomez Del Real     X86CPU *x86_cpu = X86_CPU(cpu);
260c97d6d2cSSergio Andres Gomez Del Real     int tpr = rreg(cpu->hvf_fd, HV_X86_TPR) >> 4;
261c97d6d2cSSergio Andres Gomez Del Real     cpu_set_apic_tpr(x86_cpu->apic_state, tpr);
262c97d6d2cSSergio Andres Gomez Del Real }
263c97d6d2cSSergio Andres Gomez Del Real 
264c97d6d2cSSergio Andres Gomez Del Real #define VECTORING_INFO_VECTOR_MASK     0xff
265c97d6d2cSSergio Andres Gomez Del Real 
266c97d6d2cSSergio Andres Gomez Del Real void hvf_handle_io(CPUArchState *env, uint16_t port, void *buffer,
267c97d6d2cSSergio Andres Gomez Del Real                   int direction, int size, int count)
268c97d6d2cSSergio Andres Gomez Del Real {
269c97d6d2cSSergio Andres Gomez Del Real     int i;
270c97d6d2cSSergio Andres Gomez Del Real     uint8_t *ptr = buffer;
271c97d6d2cSSergio Andres Gomez Del Real 
272c97d6d2cSSergio Andres Gomez Del Real     for (i = 0; i < count; i++) {
273c97d6d2cSSergio Andres Gomez Del Real         address_space_rw(&address_space_io, port, MEMTXATTRS_UNSPECIFIED,
274c97d6d2cSSergio Andres Gomez Del Real                          ptr, size,
275c97d6d2cSSergio Andres Gomez Del Real                          direction);
276c97d6d2cSSergio Andres Gomez Del Real         ptr += size;
277c97d6d2cSSergio Andres Gomez Del Real     }
278c97d6d2cSSergio Andres Gomez Del Real }
279c97d6d2cSSergio Andres Gomez Del Real 
280c97d6d2cSSergio Andres Gomez Del Real static void do_hvf_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
281c97d6d2cSSergio Andres Gomez Del Real {
282eae009deSRoman Bolshakov     if (!cpu->vcpu_dirty) {
283eae009deSRoman Bolshakov         hvf_get_registers(cpu);
284eae009deSRoman Bolshakov         cpu->vcpu_dirty = true;
285eae009deSRoman Bolshakov     }
286c97d6d2cSSergio Andres Gomez Del Real }
287c97d6d2cSSergio Andres Gomez Del Real 
288eae009deSRoman Bolshakov void hvf_cpu_synchronize_state(CPUState *cpu)
289c97d6d2cSSergio Andres Gomez Del Real {
290eae009deSRoman Bolshakov     if (!cpu->vcpu_dirty) {
291eae009deSRoman Bolshakov         run_on_cpu(cpu, do_hvf_cpu_synchronize_state, RUN_ON_CPU_NULL);
292c97d6d2cSSergio Andres Gomez Del Real     }
293c97d6d2cSSergio Andres Gomez Del Real }
294c97d6d2cSSergio Andres Gomez Del Real 
295eae009deSRoman Bolshakov static void do_hvf_cpu_synchronize_post_reset(CPUState *cpu,
296eae009deSRoman Bolshakov                                               run_on_cpu_data arg)
297c97d6d2cSSergio Andres Gomez Del Real {
298eae009deSRoman Bolshakov     hvf_put_registers(cpu);
299eae009deSRoman Bolshakov     cpu->vcpu_dirty = false;
300c97d6d2cSSergio Andres Gomez Del Real }
301c97d6d2cSSergio Andres Gomez Del Real 
302eae009deSRoman Bolshakov void hvf_cpu_synchronize_post_reset(CPUState *cpu)
303c97d6d2cSSergio Andres Gomez Del Real {
304eae009deSRoman Bolshakov     run_on_cpu(cpu, do_hvf_cpu_synchronize_post_reset, RUN_ON_CPU_NULL);
305c97d6d2cSSergio Andres Gomez Del Real }
306c97d6d2cSSergio Andres Gomez Del Real 
307583ae161SRoman Bolshakov static void do_hvf_cpu_synchronize_post_init(CPUState *cpu,
308583ae161SRoman Bolshakov                                              run_on_cpu_data arg)
309c97d6d2cSSergio Andres Gomez Del Real {
310eae009deSRoman Bolshakov     hvf_put_registers(cpu);
311eae009deSRoman Bolshakov     cpu->vcpu_dirty = false;
312c97d6d2cSSergio Andres Gomez Del Real }
313c97d6d2cSSergio Andres Gomez Del Real 
314eae009deSRoman Bolshakov void hvf_cpu_synchronize_post_init(CPUState *cpu)
315c97d6d2cSSergio Andres Gomez Del Real {
316eae009deSRoman Bolshakov     run_on_cpu(cpu, do_hvf_cpu_synchronize_post_init, RUN_ON_CPU_NULL);
317c97d6d2cSSergio Andres Gomez Del Real }
318c97d6d2cSSergio Andres Gomez Del Real 
3195536c98eSRoman Bolshakov static void do_hvf_cpu_synchronize_pre_loadvm(CPUState *cpu,
3205536c98eSRoman Bolshakov                                               run_on_cpu_data arg)
3215536c98eSRoman Bolshakov {
3225536c98eSRoman Bolshakov     cpu->vcpu_dirty = true;
3235536c98eSRoman Bolshakov }
3245536c98eSRoman Bolshakov 
3255536c98eSRoman Bolshakov void hvf_cpu_synchronize_pre_loadvm(CPUState *cpu)
3265536c98eSRoman Bolshakov {
3275536c98eSRoman Bolshakov     run_on_cpu(cpu, do_hvf_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL);
3285536c98eSRoman Bolshakov }
3295536c98eSRoman Bolshakov 
330ff2de166SPaolo Bonzini static bool ept_emulation_fault(hvf_slot *slot, uint64_t gpa, uint64_t ept_qual)
331c97d6d2cSSergio Andres Gomez Del Real {
332c97d6d2cSSergio Andres Gomez Del Real     int read, write;
333c97d6d2cSSergio Andres Gomez Del Real 
334c97d6d2cSSergio Andres Gomez Del Real     /* EPT fault on an instruction fetch doesn't make sense here */
335c97d6d2cSSergio Andres Gomez Del Real     if (ept_qual & EPT_VIOLATION_INST_FETCH) {
336c97d6d2cSSergio Andres Gomez Del Real         return false;
337c97d6d2cSSergio Andres Gomez Del Real     }
338c97d6d2cSSergio Andres Gomez Del Real 
339c97d6d2cSSergio Andres Gomez Del Real     /* EPT fault must be a read fault or a write fault */
340c97d6d2cSSergio Andres Gomez Del Real     read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
341c97d6d2cSSergio Andres Gomez Del Real     write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
342c97d6d2cSSergio Andres Gomez Del Real     if ((read | write) == 0) {
343c97d6d2cSSergio Andres Gomez Del Real         return false;
344c97d6d2cSSergio Andres Gomez Del Real     }
345c97d6d2cSSergio Andres Gomez Del Real 
346babfa20cSSergio Andres Gomez Del Real     if (write && slot) {
347babfa20cSSergio Andres Gomez Del Real         if (slot->flags & HVF_SLOT_LOG) {
348babfa20cSSergio Andres Gomez Del Real             memory_region_set_dirty(slot->region, gpa - slot->start, 1);
349babfa20cSSergio Andres Gomez Del Real             hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size,
350babfa20cSSergio Andres Gomez Del Real                           HV_MEMORY_READ | HV_MEMORY_WRITE);
351babfa20cSSergio Andres Gomez Del Real         }
352babfa20cSSergio Andres Gomez Del Real     }
353babfa20cSSergio Andres Gomez Del Real 
354c97d6d2cSSergio Andres Gomez Del Real     /*
355c97d6d2cSSergio Andres Gomez Del Real      * The EPT violation must have been caused by accessing a
356c97d6d2cSSergio Andres Gomez Del Real      * guest-physical address that is a translation of a guest-linear
357c97d6d2cSSergio Andres Gomez Del Real      * address.
358c97d6d2cSSergio Andres Gomez Del Real      */
359c97d6d2cSSergio Andres Gomez Del Real     if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
360c97d6d2cSSergio Andres Gomez Del Real         (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
361c97d6d2cSSergio Andres Gomez Del Real         return false;
362c97d6d2cSSergio Andres Gomez Del Real     }
363c97d6d2cSSergio Andres Gomez Del Real 
364fbafbb6dSCameron Esfahani     if (!slot) {
365fbafbb6dSCameron Esfahani         return true;
366fbafbb6dSCameron Esfahani     }
367fbafbb6dSCameron Esfahani     if (!memory_region_is_ram(slot->region) &&
368fbafbb6dSCameron Esfahani         !(read && memory_region_is_romd(slot->region))) {
369fbafbb6dSCameron Esfahani         return true;
370fbafbb6dSCameron Esfahani     }
371fbafbb6dSCameron Esfahani     return false;
372babfa20cSSergio Andres Gomez Del Real }
373babfa20cSSergio Andres Gomez Del Real 
374babfa20cSSergio Andres Gomez Del Real static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on)
375babfa20cSSergio Andres Gomez Del Real {
376babfa20cSSergio Andres Gomez Del Real     hvf_slot *slot;
377babfa20cSSergio Andres Gomez Del Real 
378babfa20cSSergio Andres Gomez Del Real     slot = hvf_find_overlap_slot(
379babfa20cSSergio Andres Gomez Del Real             section->offset_within_address_space,
380fbafbb6dSCameron Esfahani             int128_get64(section->size));
381babfa20cSSergio Andres Gomez Del Real 
382babfa20cSSergio Andres Gomez Del Real     /* protect region against writes; begin tracking it */
383babfa20cSSergio Andres Gomez Del Real     if (on) {
384babfa20cSSergio Andres Gomez Del Real         slot->flags |= HVF_SLOT_LOG;
385babfa20cSSergio Andres Gomez Del Real         hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size,
386babfa20cSSergio Andres Gomez Del Real                       HV_MEMORY_READ);
387babfa20cSSergio Andres Gomez Del Real     /* stop tracking region*/
388babfa20cSSergio Andres Gomez Del Real     } else {
389babfa20cSSergio Andres Gomez Del Real         slot->flags &= ~HVF_SLOT_LOG;
390babfa20cSSergio Andres Gomez Del Real         hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size,
391babfa20cSSergio Andres Gomez Del Real                       HV_MEMORY_READ | HV_MEMORY_WRITE);
392babfa20cSSergio Andres Gomez Del Real     }
393babfa20cSSergio Andres Gomez Del Real }
394babfa20cSSergio Andres Gomez Del Real 
395babfa20cSSergio Andres Gomez Del Real static void hvf_log_start(MemoryListener *listener,
396babfa20cSSergio Andres Gomez Del Real                           MemoryRegionSection *section, int old, int new)
397babfa20cSSergio Andres Gomez Del Real {
398babfa20cSSergio Andres Gomez Del Real     if (old != 0) {
399babfa20cSSergio Andres Gomez Del Real         return;
400babfa20cSSergio Andres Gomez Del Real     }
401babfa20cSSergio Andres Gomez Del Real 
402babfa20cSSergio Andres Gomez Del Real     hvf_set_dirty_tracking(section, 1);
403babfa20cSSergio Andres Gomez Del Real }
404babfa20cSSergio Andres Gomez Del Real 
405babfa20cSSergio Andres Gomez Del Real static void hvf_log_stop(MemoryListener *listener,
406babfa20cSSergio Andres Gomez Del Real                          MemoryRegionSection *section, int old, int new)
407babfa20cSSergio Andres Gomez Del Real {
408babfa20cSSergio Andres Gomez Del Real     if (new != 0) {
409babfa20cSSergio Andres Gomez Del Real         return;
410babfa20cSSergio Andres Gomez Del Real     }
411babfa20cSSergio Andres Gomez Del Real 
412babfa20cSSergio Andres Gomez Del Real     hvf_set_dirty_tracking(section, 0);
413babfa20cSSergio Andres Gomez Del Real }
414babfa20cSSergio Andres Gomez Del Real 
415babfa20cSSergio Andres Gomez Del Real static void hvf_log_sync(MemoryListener *listener,
416babfa20cSSergio Andres Gomez Del Real                          MemoryRegionSection *section)
417babfa20cSSergio Andres Gomez Del Real {
418babfa20cSSergio Andres Gomez Del Real     /*
419babfa20cSSergio Andres Gomez Del Real      * sync of dirty pages is handled elsewhere; just make sure we keep
420babfa20cSSergio Andres Gomez Del Real      * tracking the region.
421babfa20cSSergio Andres Gomez Del Real      */
422babfa20cSSergio Andres Gomez Del Real     hvf_set_dirty_tracking(section, 1);
423c97d6d2cSSergio Andres Gomez Del Real }
424c97d6d2cSSergio Andres Gomez Del Real 
425c97d6d2cSSergio Andres Gomez Del Real static void hvf_region_add(MemoryListener *listener,
426c97d6d2cSSergio Andres Gomez Del Real                            MemoryRegionSection *section)
427c97d6d2cSSergio Andres Gomez Del Real {
428c97d6d2cSSergio Andres Gomez Del Real     hvf_set_phys_mem(section, true);
429c97d6d2cSSergio Andres Gomez Del Real }
430c97d6d2cSSergio Andres Gomez Del Real 
431c97d6d2cSSergio Andres Gomez Del Real static void hvf_region_del(MemoryListener *listener,
432c97d6d2cSSergio Andres Gomez Del Real                            MemoryRegionSection *section)
433c97d6d2cSSergio Andres Gomez Del Real {
434c97d6d2cSSergio Andres Gomez Del Real     hvf_set_phys_mem(section, false);
435c97d6d2cSSergio Andres Gomez Del Real }
436c97d6d2cSSergio Andres Gomez Del Real 
437c97d6d2cSSergio Andres Gomez Del Real static MemoryListener hvf_memory_listener = {
438c97d6d2cSSergio Andres Gomez Del Real     .priority = 10,
439c97d6d2cSSergio Andres Gomez Del Real     .region_add = hvf_region_add,
440c97d6d2cSSergio Andres Gomez Del Real     .region_del = hvf_region_del,
441babfa20cSSergio Andres Gomez Del Real     .log_start = hvf_log_start,
442babfa20cSSergio Andres Gomez Del Real     .log_stop = hvf_log_stop,
443babfa20cSSergio Andres Gomez Del Real     .log_sync = hvf_log_sync,
444c97d6d2cSSergio Andres Gomez Del Real };
445c97d6d2cSSergio Andres Gomez Del Real 
446c97d6d2cSSergio Andres Gomez Del Real void hvf_vcpu_destroy(CPUState *cpu)
447c97d6d2cSSergio Andres Gomez Del Real {
448fe76b09cSRoman Bolshakov     X86CPU *x86_cpu = X86_CPU(cpu);
449fe76b09cSRoman Bolshakov     CPUX86State *env = &x86_cpu->env;
450fe76b09cSRoman Bolshakov 
451c97d6d2cSSergio Andres Gomez Del Real     hv_return_t ret = hv_vcpu_destroy((hv_vcpuid_t)cpu->hvf_fd);
452fe76b09cSRoman Bolshakov     g_free(env->hvf_mmio_buf);
453c97d6d2cSSergio Andres Gomez Del Real     assert_hvf_ok(ret);
454c97d6d2cSSergio Andres Gomez Del Real }
455c97d6d2cSSergio Andres Gomez Del Real 
456c97d6d2cSSergio Andres Gomez Del Real static void dummy_signal(int sig)
457c97d6d2cSSergio Andres Gomez Del Real {
458c97d6d2cSSergio Andres Gomez Del Real }
459c97d6d2cSSergio Andres Gomez Del Real 
460*3b502b0eSVladislav Yaroshchuk static void init_tsc_freq(CPUX86State *env)
461*3b502b0eSVladislav Yaroshchuk {
462*3b502b0eSVladislav Yaroshchuk     size_t length;
463*3b502b0eSVladislav Yaroshchuk     uint64_t tsc_freq;
464*3b502b0eSVladislav Yaroshchuk 
465*3b502b0eSVladislav Yaroshchuk     if (env->tsc_khz != 0) {
466*3b502b0eSVladislav Yaroshchuk         return;
467*3b502b0eSVladislav Yaroshchuk     }
468*3b502b0eSVladislav Yaroshchuk 
469*3b502b0eSVladislav Yaroshchuk     length = sizeof(uint64_t);
470*3b502b0eSVladislav Yaroshchuk     if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) {
471*3b502b0eSVladislav Yaroshchuk         return;
472*3b502b0eSVladislav Yaroshchuk     }
473*3b502b0eSVladislav Yaroshchuk     env->tsc_khz = tsc_freq / 1000;  /* Hz to KHz */
474*3b502b0eSVladislav Yaroshchuk }
475*3b502b0eSVladislav Yaroshchuk 
476*3b502b0eSVladislav Yaroshchuk static void init_apic_bus_freq(CPUX86State *env)
477*3b502b0eSVladislav Yaroshchuk {
478*3b502b0eSVladislav Yaroshchuk     size_t length;
479*3b502b0eSVladislav Yaroshchuk     uint64_t bus_freq;
480*3b502b0eSVladislav Yaroshchuk 
481*3b502b0eSVladislav Yaroshchuk     if (env->apic_bus_freq != 0) {
482*3b502b0eSVladislav Yaroshchuk         return;
483*3b502b0eSVladislav Yaroshchuk     }
484*3b502b0eSVladislav Yaroshchuk 
485*3b502b0eSVladislav Yaroshchuk     length = sizeof(uint64_t);
486*3b502b0eSVladislav Yaroshchuk     if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
487*3b502b0eSVladislav Yaroshchuk         return;
488*3b502b0eSVladislav Yaroshchuk     }
489*3b502b0eSVladislav Yaroshchuk     env->apic_bus_freq = bus_freq;
490*3b502b0eSVladislav Yaroshchuk }
491*3b502b0eSVladislav Yaroshchuk 
492*3b502b0eSVladislav Yaroshchuk static inline bool tsc_is_known(CPUX86State *env)
493*3b502b0eSVladislav Yaroshchuk {
494*3b502b0eSVladislav Yaroshchuk     return env->tsc_khz != 0;
495*3b502b0eSVladislav Yaroshchuk }
496*3b502b0eSVladislav Yaroshchuk 
497*3b502b0eSVladislav Yaroshchuk static inline bool apic_bus_freq_is_known(CPUX86State *env)
498*3b502b0eSVladislav Yaroshchuk {
499*3b502b0eSVladislav Yaroshchuk     return env->apic_bus_freq != 0;
500*3b502b0eSVladislav Yaroshchuk }
501*3b502b0eSVladislav Yaroshchuk 
502c97d6d2cSSergio Andres Gomez Del Real int hvf_init_vcpu(CPUState *cpu)
503c97d6d2cSSergio Andres Gomez Del Real {
504c97d6d2cSSergio Andres Gomez Del Real 
505c97d6d2cSSergio Andres Gomez Del Real     X86CPU *x86cpu = X86_CPU(cpu);
506c97d6d2cSSergio Andres Gomez Del Real     CPUX86State *env = &x86cpu->env;
507c97d6d2cSSergio Andres Gomez Del Real     int r;
508c97d6d2cSSergio Andres Gomez Del Real 
509c97d6d2cSSergio Andres Gomez Del Real     /* init cpu signals */
510c97d6d2cSSergio Andres Gomez Del Real     sigset_t set;
511c97d6d2cSSergio Andres Gomez Del Real     struct sigaction sigact;
512c97d6d2cSSergio Andres Gomez Del Real 
513c97d6d2cSSergio Andres Gomez Del Real     memset(&sigact, 0, sizeof(sigact));
514c97d6d2cSSergio Andres Gomez Del Real     sigact.sa_handler = dummy_signal;
515c97d6d2cSSergio Andres Gomez Del Real     sigaction(SIG_IPI, &sigact, NULL);
516c97d6d2cSSergio Andres Gomez Del Real 
517c97d6d2cSSergio Andres Gomez Del Real     pthread_sigmask(SIG_BLOCK, NULL, &set);
518c97d6d2cSSergio Andres Gomez Del Real     sigdelset(&set, SIG_IPI);
519c97d6d2cSSergio Andres Gomez Del Real 
520c97d6d2cSSergio Andres Gomez Del Real     init_emu();
521c97d6d2cSSergio Andres Gomez Del Real     init_decoder();
522c97d6d2cSSergio Andres Gomez Del Real 
523c97d6d2cSSergio Andres Gomez Del Real     hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
524fe76b09cSRoman Bolshakov     env->hvf_mmio_buf = g_new(char, 4096);
525c97d6d2cSSergio Andres Gomez Del Real 
526*3b502b0eSVladislav Yaroshchuk     if (x86cpu->vmware_cpuid_freq) {
527*3b502b0eSVladislav Yaroshchuk         init_tsc_freq(env);
528*3b502b0eSVladislav Yaroshchuk         init_apic_bus_freq(env);
529*3b502b0eSVladislav Yaroshchuk 
530*3b502b0eSVladislav Yaroshchuk         if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
531*3b502b0eSVladislav Yaroshchuk             error_report("vmware-cpuid-freq: feature couldn't be enabled");
532*3b502b0eSVladislav Yaroshchuk         }
533*3b502b0eSVladislav Yaroshchuk     }
534*3b502b0eSVladislav Yaroshchuk 
535c97d6d2cSSergio Andres Gomez Del Real     r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf_fd, HV_VCPU_DEFAULT);
536c97d6d2cSSergio Andres Gomez Del Real     cpu->vcpu_dirty = 1;
537c97d6d2cSSergio Andres Gomez Del Real     assert_hvf_ok(r);
538c97d6d2cSSergio Andres Gomez Del Real 
539c97d6d2cSSergio Andres Gomez Del Real     if (hv_vmx_read_capability(HV_VMX_CAP_PINBASED,
540c97d6d2cSSergio Andres Gomez Del Real         &hvf_state->hvf_caps->vmx_cap_pinbased)) {
541c97d6d2cSSergio Andres Gomez Del Real         abort();
542c97d6d2cSSergio Andres Gomez Del Real     }
543c97d6d2cSSergio Andres Gomez Del Real     if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED,
544c97d6d2cSSergio Andres Gomez Del Real         &hvf_state->hvf_caps->vmx_cap_procbased)) {
545c97d6d2cSSergio Andres Gomez Del Real         abort();
546c97d6d2cSSergio Andres Gomez Del Real     }
547c97d6d2cSSergio Andres Gomez Del Real     if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2,
548c97d6d2cSSergio Andres Gomez Del Real         &hvf_state->hvf_caps->vmx_cap_procbased2)) {
549c97d6d2cSSergio Andres Gomez Del Real         abort();
550c97d6d2cSSergio Andres Gomez Del Real     }
551c97d6d2cSSergio Andres Gomez Del Real     if (hv_vmx_read_capability(HV_VMX_CAP_ENTRY,
552c97d6d2cSSergio Andres Gomez Del Real         &hvf_state->hvf_caps->vmx_cap_entry)) {
553c97d6d2cSSergio Andres Gomez Del Real         abort();
554c97d6d2cSSergio Andres Gomez Del Real     }
555c97d6d2cSSergio Andres Gomez Del Real 
556c97d6d2cSSergio Andres Gomez Del Real     /* set VMCS control fields */
557c97d6d2cSSergio Andres Gomez Del Real     wvmcs(cpu->hvf_fd, VMCS_PIN_BASED_CTLS,
558c97d6d2cSSergio Andres Gomez Del Real           cap2ctrl(hvf_state->hvf_caps->vmx_cap_pinbased,
559c97d6d2cSSergio Andres Gomez Del Real           VMCS_PIN_BASED_CTLS_EXTINT |
560c97d6d2cSSergio Andres Gomez Del Real           VMCS_PIN_BASED_CTLS_NMI |
561c97d6d2cSSergio Andres Gomez Del Real           VMCS_PIN_BASED_CTLS_VNMI));
562c97d6d2cSSergio Andres Gomez Del Real     wvmcs(cpu->hvf_fd, VMCS_PRI_PROC_BASED_CTLS,
563c97d6d2cSSergio Andres Gomez Del Real           cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased,
564c97d6d2cSSergio Andres Gomez Del Real           VMCS_PRI_PROC_BASED_CTLS_HLT |
565c97d6d2cSSergio Andres Gomez Del Real           VMCS_PRI_PROC_BASED_CTLS_MWAIT |
566c97d6d2cSSergio Andres Gomez Del Real           VMCS_PRI_PROC_BASED_CTLS_TSC_OFFSET |
567c97d6d2cSSergio Andres Gomez Del Real           VMCS_PRI_PROC_BASED_CTLS_TPR_SHADOW) |
568c97d6d2cSSergio Andres Gomez Del Real           VMCS_PRI_PROC_BASED_CTLS_SEC_CONTROL);
569c97d6d2cSSergio Andres Gomez Del Real     wvmcs(cpu->hvf_fd, VMCS_SEC_PROC_BASED_CTLS,
570c97d6d2cSSergio Andres Gomez Del Real           cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased2,
571c97d6d2cSSergio Andres Gomez Del Real                    VMCS_PRI_PROC_BASED2_CTLS_APIC_ACCESSES));
572c97d6d2cSSergio Andres Gomez Del Real 
573c97d6d2cSSergio Andres Gomez Del Real     wvmcs(cpu->hvf_fd, VMCS_ENTRY_CTLS, cap2ctrl(hvf_state->hvf_caps->vmx_cap_entry,
574c97d6d2cSSergio Andres Gomez Del Real           0));
575c97d6d2cSSergio Andres Gomez Del Real     wvmcs(cpu->hvf_fd, VMCS_EXCEPTION_BITMAP, 0); /* Double fault */
576c97d6d2cSSergio Andres Gomez Del Real 
577c97d6d2cSSergio Andres Gomez Del Real     wvmcs(cpu->hvf_fd, VMCS_TPR_THRESHOLD, 0);
578c97d6d2cSSergio Andres Gomez Del Real 
579c97d6d2cSSergio Andres Gomez Del Real     x86cpu = X86_CPU(cpu);
5805b8063c4SLiran Alon     x86cpu->env.xsave_buf = qemu_memalign(4096, 4096);
581c97d6d2cSSergio Andres Gomez Del Real 
582c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_STAR, 1);
583c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_LSTAR, 1);
584c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_CSTAR, 1);
585c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_FMASK, 1);
586c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_FSBASE, 1);
587c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_GSBASE, 1);
588c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_KERNELGSBASE, 1);
589c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_TSC_AUX, 1);
5909fedbbeeSCameron Esfahani     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_TSC, 1);
591c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_SYSENTER_CS, 1);
592c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_SYSENTER_EIP, 1);
593c97d6d2cSSergio Andres Gomez Del Real     hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_SYSENTER_ESP, 1);
594c97d6d2cSSergio Andres Gomez Del Real 
595c97d6d2cSSergio Andres Gomez Del Real     return 0;
596c97d6d2cSSergio Andres Gomez Del Real }
597c97d6d2cSSergio Andres Gomez Del Real 
598b7394c83SSergio Andres Gomez Del Real static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_info)
599b7394c83SSergio Andres Gomez Del Real {
600b7394c83SSergio Andres Gomez Del Real     X86CPU *x86_cpu = X86_CPU(cpu);
601b7394c83SSergio Andres Gomez Del Real     CPUX86State *env = &x86_cpu->env;
602b7394c83SSergio Andres Gomez Del Real 
603fd13f23bSLiran Alon     env->exception_nr = -1;
604fd13f23bSLiran Alon     env->exception_pending = 0;
605fd13f23bSLiran Alon     env->exception_injected = 0;
606b7394c83SSergio Andres Gomez Del Real     env->interrupt_injected = -1;
607b7394c83SSergio Andres Gomez Del Real     env->nmi_injected = false;
60864bef038SCameron Esfahani     env->ins_len = 0;
60964bef038SCameron Esfahani     env->has_error_code = false;
610b7394c83SSergio Andres Gomez Del Real     if (idtvec_info & VMCS_IDT_VEC_VALID) {
611b7394c83SSergio Andres Gomez Del Real         switch (idtvec_info & VMCS_IDT_VEC_TYPE) {
612b7394c83SSergio Andres Gomez Del Real         case VMCS_IDT_VEC_HWINTR:
613b7394c83SSergio Andres Gomez Del Real         case VMCS_IDT_VEC_SWINTR:
614b7394c83SSergio Andres Gomez Del Real             env->interrupt_injected = idtvec_info & VMCS_IDT_VEC_VECNUM;
615b7394c83SSergio Andres Gomez Del Real             break;
616b7394c83SSergio Andres Gomez Del Real         case VMCS_IDT_VEC_NMI:
617b7394c83SSergio Andres Gomez Del Real             env->nmi_injected = true;
618b7394c83SSergio Andres Gomez Del Real             break;
619b7394c83SSergio Andres Gomez Del Real         case VMCS_IDT_VEC_HWEXCEPTION:
620b7394c83SSergio Andres Gomez Del Real         case VMCS_IDT_VEC_SWEXCEPTION:
621fd13f23bSLiran Alon             env->exception_nr = idtvec_info & VMCS_IDT_VEC_VECNUM;
622fd13f23bSLiran Alon             env->exception_injected = 1;
623b7394c83SSergio Andres Gomez Del Real             break;
624b7394c83SSergio Andres Gomez Del Real         case VMCS_IDT_VEC_PRIV_SWEXCEPTION:
625b7394c83SSergio Andres Gomez Del Real         default:
626b7394c83SSergio Andres Gomez Del Real             abort();
627b7394c83SSergio Andres Gomez Del Real         }
628b7394c83SSergio Andres Gomez Del Real         if ((idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWEXCEPTION ||
629b7394c83SSergio Andres Gomez Del Real             (idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWINTR) {
630b7394c83SSergio Andres Gomez Del Real             env->ins_len = ins_len;
631b7394c83SSergio Andres Gomez Del Real         }
63264bef038SCameron Esfahani         if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
633b7394c83SSergio Andres Gomez Del Real             env->has_error_code = true;
634b7394c83SSergio Andres Gomez Del Real             env->error_code = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_ERROR);
635b7394c83SSergio Andres Gomez Del Real         }
636b7394c83SSergio Andres Gomez Del Real     }
637b7394c83SSergio Andres Gomez Del Real     if ((rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) &
638b7394c83SSergio Andres Gomez Del Real         VMCS_INTERRUPTIBILITY_NMI_BLOCKING)) {
639b7394c83SSergio Andres Gomez Del Real         env->hflags2 |= HF2_NMI_MASK;
640b7394c83SSergio Andres Gomez Del Real     } else {
641b7394c83SSergio Andres Gomez Del Real         env->hflags2 &= ~HF2_NMI_MASK;
642b7394c83SSergio Andres Gomez Del Real     }
643b7394c83SSergio Andres Gomez Del Real     if (rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) &
644b7394c83SSergio Andres Gomez Del Real          (VMCS_INTERRUPTIBILITY_STI_BLOCKING |
645b7394c83SSergio Andres Gomez Del Real          VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) {
646b7394c83SSergio Andres Gomez Del Real         env->hflags |= HF_INHIBIT_IRQ_MASK;
647b7394c83SSergio Andres Gomez Del Real     } else {
648b7394c83SSergio Andres Gomez Del Real         env->hflags &= ~HF_INHIBIT_IRQ_MASK;
649b7394c83SSergio Andres Gomez Del Real     }
650b7394c83SSergio Andres Gomez Del Real }
651b7394c83SSergio Andres Gomez Del Real 
652*3b502b0eSVladislav Yaroshchuk static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
653*3b502b0eSVladislav Yaroshchuk                               uint32_t *eax, uint32_t *ebx,
654*3b502b0eSVladislav Yaroshchuk                               uint32_t *ecx, uint32_t *edx)
655*3b502b0eSVladislav Yaroshchuk {
656*3b502b0eSVladislav Yaroshchuk     /*
657*3b502b0eSVladislav Yaroshchuk      * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs,
658*3b502b0eSVladislav Yaroshchuk      * leafs 0x40000001-0x4000000F are filled with zeros
659*3b502b0eSVladislav Yaroshchuk      * Provides vmware-cpuid-freq support to hvf
660*3b502b0eSVladislav Yaroshchuk      *
661*3b502b0eSVladislav Yaroshchuk      * Note: leaf 0x40000000 not exposes HVF,
662*3b502b0eSVladislav Yaroshchuk      * leaving hypervisor signature empty
663*3b502b0eSVladislav Yaroshchuk      */
664*3b502b0eSVladislav Yaroshchuk 
665*3b502b0eSVladislav Yaroshchuk     if (index < 0x40000000 || index > 0x40000010 ||
666*3b502b0eSVladislav Yaroshchuk         !tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
667*3b502b0eSVladislav Yaroshchuk 
668*3b502b0eSVladislav Yaroshchuk         cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
669*3b502b0eSVladislav Yaroshchuk         return;
670*3b502b0eSVladislav Yaroshchuk     }
671*3b502b0eSVladislav Yaroshchuk 
672*3b502b0eSVladislav Yaroshchuk     switch (index) {
673*3b502b0eSVladislav Yaroshchuk     case 0x40000000:
674*3b502b0eSVladislav Yaroshchuk         *eax = 0x40000010;    /* Max available cpuid leaf */
675*3b502b0eSVladislav Yaroshchuk         *ebx = 0;             /* Leave signature empty */
676*3b502b0eSVladislav Yaroshchuk         *ecx = 0;
677*3b502b0eSVladislav Yaroshchuk         *edx = 0;
678*3b502b0eSVladislav Yaroshchuk         break;
679*3b502b0eSVladislav Yaroshchuk     case 0x40000010:
680*3b502b0eSVladislav Yaroshchuk         *eax = env->tsc_khz;
681*3b502b0eSVladislav Yaroshchuk         *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
682*3b502b0eSVladislav Yaroshchuk         *ecx = 0;
683*3b502b0eSVladislav Yaroshchuk         *edx = 0;
684*3b502b0eSVladislav Yaroshchuk         break;
685*3b502b0eSVladislav Yaroshchuk     default:
686*3b502b0eSVladislav Yaroshchuk         *eax = 0;
687*3b502b0eSVladislav Yaroshchuk         *ebx = 0;
688*3b502b0eSVladislav Yaroshchuk         *ecx = 0;
689*3b502b0eSVladislav Yaroshchuk         *edx = 0;
690*3b502b0eSVladislav Yaroshchuk         break;
691*3b502b0eSVladislav Yaroshchuk     }
692*3b502b0eSVladislav Yaroshchuk }
693*3b502b0eSVladislav Yaroshchuk 
694c97d6d2cSSergio Andres Gomez Del Real int hvf_vcpu_exec(CPUState *cpu)
695c97d6d2cSSergio Andres Gomez Del Real {
696c97d6d2cSSergio Andres Gomez Del Real     X86CPU *x86_cpu = X86_CPU(cpu);
697c97d6d2cSSergio Andres Gomez Del Real     CPUX86State *env = &x86_cpu->env;
698c97d6d2cSSergio Andres Gomez Del Real     int ret = 0;
699c97d6d2cSSergio Andres Gomez Del Real     uint64_t rip = 0;
700c97d6d2cSSergio Andres Gomez Del Real 
701c97d6d2cSSergio Andres Gomez Del Real     if (hvf_process_events(cpu)) {
702c97d6d2cSSergio Andres Gomez Del Real         return EXCP_HLT;
703c97d6d2cSSergio Andres Gomez Del Real     }
704c97d6d2cSSergio Andres Gomez Del Real 
705c97d6d2cSSergio Andres Gomez Del Real     do {
706c97d6d2cSSergio Andres Gomez Del Real         if (cpu->vcpu_dirty) {
707c97d6d2cSSergio Andres Gomez Del Real             hvf_put_registers(cpu);
708c97d6d2cSSergio Andres Gomez Del Real             cpu->vcpu_dirty = false;
709c97d6d2cSSergio Andres Gomez Del Real         }
710c97d6d2cSSergio Andres Gomez Del Real 
711b7394c83SSergio Andres Gomez Del Real         if (hvf_inject_interrupts(cpu)) {
712b7394c83SSergio Andres Gomez Del Real             return EXCP_INTERRUPT;
713b7394c83SSergio Andres Gomez Del Real         }
714c97d6d2cSSergio Andres Gomez Del Real         vmx_update_tpr(cpu);
715c97d6d2cSSergio Andres Gomez Del Real 
716c97d6d2cSSergio Andres Gomez Del Real         qemu_mutex_unlock_iothread();
717c97d6d2cSSergio Andres Gomez Del Real         if (!cpu_is_bsp(X86_CPU(cpu)) && cpu->halted) {
718c97d6d2cSSergio Andres Gomez Del Real             qemu_mutex_lock_iothread();
719c97d6d2cSSergio Andres Gomez Del Real             return EXCP_HLT;
720c97d6d2cSSergio Andres Gomez Del Real         }
721c97d6d2cSSergio Andres Gomez Del Real 
722c97d6d2cSSergio Andres Gomez Del Real         hv_return_t r  = hv_vcpu_run(cpu->hvf_fd);
723c97d6d2cSSergio Andres Gomez Del Real         assert_hvf_ok(r);
724c97d6d2cSSergio Andres Gomez Del Real 
725c97d6d2cSSergio Andres Gomez Del Real         /* handle VMEXIT */
726c97d6d2cSSergio Andres Gomez Del Real         uint64_t exit_reason = rvmcs(cpu->hvf_fd, VMCS_EXIT_REASON);
727c97d6d2cSSergio Andres Gomez Del Real         uint64_t exit_qual = rvmcs(cpu->hvf_fd, VMCS_EXIT_QUALIFICATION);
728c97d6d2cSSergio Andres Gomez Del Real         uint32_t ins_len = (uint32_t)rvmcs(cpu->hvf_fd,
729c97d6d2cSSergio Andres Gomez Del Real                                            VMCS_EXIT_INSTRUCTION_LENGTH);
730b7394c83SSergio Andres Gomez Del Real 
731c97d6d2cSSergio Andres Gomez Del Real         uint64_t idtvec_info = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_INFO);
732b7394c83SSergio Andres Gomez Del Real 
733b7394c83SSergio Andres Gomez Del Real         hvf_store_events(cpu, ins_len, idtvec_info);
734c97d6d2cSSergio Andres Gomez Del Real         rip = rreg(cpu->hvf_fd, HV_X86_RIP);
735967f4da2SRoman Bolshakov         env->eflags = rreg(cpu->hvf_fd, HV_X86_RFLAGS);
736c97d6d2cSSergio Andres Gomez Del Real 
737c97d6d2cSSergio Andres Gomez Del Real         qemu_mutex_lock_iothread();
738c97d6d2cSSergio Andres Gomez Del Real 
739c97d6d2cSSergio Andres Gomez Del Real         update_apic_tpr(cpu);
740c97d6d2cSSergio Andres Gomez Del Real         current_cpu = cpu;
741c97d6d2cSSergio Andres Gomez Del Real 
742c97d6d2cSSergio Andres Gomez Del Real         ret = 0;
743c97d6d2cSSergio Andres Gomez Del Real         switch (exit_reason) {
744c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_HLT: {
745c97d6d2cSSergio Andres Gomez Del Real             macvm_set_rip(cpu, rip + ins_len);
746c97d6d2cSSergio Andres Gomez Del Real             if (!((cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
747967f4da2SRoman Bolshakov                 (env->eflags & IF_MASK))
748c97d6d2cSSergio Andres Gomez Del Real                 && !(cpu->interrupt_request & CPU_INTERRUPT_NMI) &&
749c97d6d2cSSergio Andres Gomez Del Real                 !(idtvec_info & VMCS_IDT_VEC_VALID)) {
750c97d6d2cSSergio Andres Gomez Del Real                 cpu->halted = 1;
751c97d6d2cSSergio Andres Gomez Del Real                 ret = EXCP_HLT;
7523b9c59daSChen Zhang                 break;
753c97d6d2cSSergio Andres Gomez Del Real             }
754c97d6d2cSSergio Andres Gomez Del Real             ret = EXCP_INTERRUPT;
755c97d6d2cSSergio Andres Gomez Del Real             break;
756c97d6d2cSSergio Andres Gomez Del Real         }
757c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_MWAIT: {
758c97d6d2cSSergio Andres Gomez Del Real             ret = EXCP_INTERRUPT;
759c97d6d2cSSergio Andres Gomez Del Real             break;
760c97d6d2cSSergio Andres Gomez Del Real         }
761fbafbb6dSCameron Esfahani         /* Need to check if MMIO or unmapped fault */
762c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_EPT_FAULT:
763c97d6d2cSSergio Andres Gomez Del Real         {
764c97d6d2cSSergio Andres Gomez Del Real             hvf_slot *slot;
765ff2de166SPaolo Bonzini             uint64_t gpa = rvmcs(cpu->hvf_fd, VMCS_GUEST_PHYSICAL_ADDRESS);
766c97d6d2cSSergio Andres Gomez Del Real 
767c97d6d2cSSergio Andres Gomez Del Real             if (((idtvec_info & VMCS_IDT_VEC_VALID) == 0) &&
768c97d6d2cSSergio Andres Gomez Del Real                 ((exit_qual & EXIT_QUAL_NMIUDTI) != 0)) {
769c97d6d2cSSergio Andres Gomez Del Real                 vmx_set_nmi_blocking(cpu);
770c97d6d2cSSergio Andres Gomez Del Real             }
771c97d6d2cSSergio Andres Gomez Del Real 
772fbafbb6dSCameron Esfahani             slot = hvf_find_overlap_slot(gpa, 1);
773c97d6d2cSSergio Andres Gomez Del Real             /* mmio */
774babfa20cSSergio Andres Gomez Del Real             if (ept_emulation_fault(slot, gpa, exit_qual)) {
775c97d6d2cSSergio Andres Gomez Del Real                 struct x86_decode decode;
776c97d6d2cSSergio Andres Gomez Del Real 
777c97d6d2cSSergio Andres Gomez Del Real                 load_regs(cpu);
778c97d6d2cSSergio Andres Gomez Del Real                 decode_instruction(env, &decode);
779c97d6d2cSSergio Andres Gomez Del Real                 exec_instruction(env, &decode);
780c97d6d2cSSergio Andres Gomez Del Real                 store_regs(cpu);
781c97d6d2cSSergio Andres Gomez Del Real                 break;
782c97d6d2cSSergio Andres Gomez Del Real             }
783c97d6d2cSSergio Andres Gomez Del Real             break;
784c97d6d2cSSergio Andres Gomez Del Real         }
785c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_INOUT:
786c97d6d2cSSergio Andres Gomez Del Real         {
787c97d6d2cSSergio Andres Gomez Del Real             uint32_t in = (exit_qual & 8) != 0;
788c97d6d2cSSergio Andres Gomez Del Real             uint32_t size =  (exit_qual & 7) + 1;
789c97d6d2cSSergio Andres Gomez Del Real             uint32_t string =  (exit_qual & 16) != 0;
790c97d6d2cSSergio Andres Gomez Del Real             uint32_t port =  exit_qual >> 16;
791c97d6d2cSSergio Andres Gomez Del Real             /*uint32_t rep = (exit_qual & 0x20) != 0;*/
792c97d6d2cSSergio Andres Gomez Del Real 
793c97d6d2cSSergio Andres Gomez Del Real             if (!string && in) {
794c97d6d2cSSergio Andres Gomez Del Real                 uint64_t val = 0;
795c97d6d2cSSergio Andres Gomez Del Real                 load_regs(cpu);
796c97d6d2cSSergio Andres Gomez Del Real                 hvf_handle_io(env, port, &val, 0, size, 1);
797c97d6d2cSSergio Andres Gomez Del Real                 if (size == 1) {
798c97d6d2cSSergio Andres Gomez Del Real                     AL(env) = val;
799c97d6d2cSSergio Andres Gomez Del Real                 } else if (size == 2) {
800c97d6d2cSSergio Andres Gomez Del Real                     AX(env) = val;
801c97d6d2cSSergio Andres Gomez Del Real                 } else if (size == 4) {
802c97d6d2cSSergio Andres Gomez Del Real                     RAX(env) = (uint32_t)val;
803c97d6d2cSSergio Andres Gomez Del Real                 } else {
804da20f5cdSPaolo Bonzini                     RAX(env) = (uint64_t)val;
805c97d6d2cSSergio Andres Gomez Del Real                 }
8065d32173fSRoman Bolshakov                 env->eip += ins_len;
807c97d6d2cSSergio Andres Gomez Del Real                 store_regs(cpu);
808c97d6d2cSSergio Andres Gomez Del Real                 break;
809c97d6d2cSSergio Andres Gomez Del Real             } else if (!string && !in) {
810c97d6d2cSSergio Andres Gomez Del Real                 RAX(env) = rreg(cpu->hvf_fd, HV_X86_RAX);
811c97d6d2cSSergio Andres Gomez Del Real                 hvf_handle_io(env, port, &RAX(env), 1, size, 1);
812c97d6d2cSSergio Andres Gomez Del Real                 macvm_set_rip(cpu, rip + ins_len);
813c97d6d2cSSergio Andres Gomez Del Real                 break;
814c97d6d2cSSergio Andres Gomez Del Real             }
815c97d6d2cSSergio Andres Gomez Del Real             struct x86_decode decode;
816c97d6d2cSSergio Andres Gomez Del Real 
817c97d6d2cSSergio Andres Gomez Del Real             load_regs(cpu);
818c97d6d2cSSergio Andres Gomez Del Real             decode_instruction(env, &decode);
819e62963bfSPaolo Bonzini             assert(ins_len == decode.len);
820c97d6d2cSSergio Andres Gomez Del Real             exec_instruction(env, &decode);
821c97d6d2cSSergio Andres Gomez Del Real             store_regs(cpu);
822c97d6d2cSSergio Andres Gomez Del Real 
823c97d6d2cSSergio Andres Gomez Del Real             break;
824c97d6d2cSSergio Andres Gomez Del Real         }
825c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_CPUID: {
826c97d6d2cSSergio Andres Gomez Del Real             uint32_t rax = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RAX);
827c97d6d2cSSergio Andres Gomez Del Real             uint32_t rbx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RBX);
828c97d6d2cSSergio Andres Gomez Del Real             uint32_t rcx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
829c97d6d2cSSergio Andres Gomez Del Real             uint32_t rdx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
830c97d6d2cSSergio Andres Gomez Del Real 
831*3b502b0eSVladislav Yaroshchuk             hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
832c97d6d2cSSergio Andres Gomez Del Real 
833c97d6d2cSSergio Andres Gomez Del Real             wreg(cpu->hvf_fd, HV_X86_RAX, rax);
834c97d6d2cSSergio Andres Gomez Del Real             wreg(cpu->hvf_fd, HV_X86_RBX, rbx);
835c97d6d2cSSergio Andres Gomez Del Real             wreg(cpu->hvf_fd, HV_X86_RCX, rcx);
836c97d6d2cSSergio Andres Gomez Del Real             wreg(cpu->hvf_fd, HV_X86_RDX, rdx);
837c97d6d2cSSergio Andres Gomez Del Real 
838c97d6d2cSSergio Andres Gomez Del Real             macvm_set_rip(cpu, rip + ins_len);
839c97d6d2cSSergio Andres Gomez Del Real             break;
840c97d6d2cSSergio Andres Gomez Del Real         }
841c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_XSETBV: {
842c97d6d2cSSergio Andres Gomez Del Real             X86CPU *x86_cpu = X86_CPU(cpu);
843c97d6d2cSSergio Andres Gomez Del Real             CPUX86State *env = &x86_cpu->env;
844c97d6d2cSSergio Andres Gomez Del Real             uint32_t eax = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RAX);
845c97d6d2cSSergio Andres Gomez Del Real             uint32_t ecx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RCX);
846c97d6d2cSSergio Andres Gomez Del Real             uint32_t edx = (uint32_t)rreg(cpu->hvf_fd, HV_X86_RDX);
847c97d6d2cSSergio Andres Gomez Del Real 
848c97d6d2cSSergio Andres Gomez Del Real             if (ecx) {
849c97d6d2cSSergio Andres Gomez Del Real                 macvm_set_rip(cpu, rip + ins_len);
850c97d6d2cSSergio Andres Gomez Del Real                 break;
851c97d6d2cSSergio Andres Gomez Del Real             }
852c97d6d2cSSergio Andres Gomez Del Real             env->xcr0 = ((uint64_t)edx << 32) | eax;
853c97d6d2cSSergio Andres Gomez Del Real             wreg(cpu->hvf_fd, HV_X86_XCR0, env->xcr0 | 1);
854c97d6d2cSSergio Andres Gomez Del Real             macvm_set_rip(cpu, rip + ins_len);
855c97d6d2cSSergio Andres Gomez Del Real             break;
856c97d6d2cSSergio Andres Gomez Del Real         }
857c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_INTR_WINDOW:
858c97d6d2cSSergio Andres Gomez Del Real             vmx_clear_int_window_exiting(cpu);
859c97d6d2cSSergio Andres Gomez Del Real             ret = EXCP_INTERRUPT;
860c97d6d2cSSergio Andres Gomez Del Real             break;
861c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_NMI_WINDOW:
862c97d6d2cSSergio Andres Gomez Del Real             vmx_clear_nmi_window_exiting(cpu);
863c97d6d2cSSergio Andres Gomez Del Real             ret = EXCP_INTERRUPT;
864c97d6d2cSSergio Andres Gomez Del Real             break;
865c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_EXT_INTR:
866c97d6d2cSSergio Andres Gomez Del Real             /* force exit and allow io handling */
867c97d6d2cSSergio Andres Gomez Del Real             ret = EXCP_INTERRUPT;
868c97d6d2cSSergio Andres Gomez Del Real             break;
869c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_RDMSR:
870c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_WRMSR:
871c97d6d2cSSergio Andres Gomez Del Real         {
872c97d6d2cSSergio Andres Gomez Del Real             load_regs(cpu);
873c97d6d2cSSergio Andres Gomez Del Real             if (exit_reason == EXIT_REASON_RDMSR) {
874c97d6d2cSSergio Andres Gomez Del Real                 simulate_rdmsr(cpu);
875c97d6d2cSSergio Andres Gomez Del Real             } else {
876c97d6d2cSSergio Andres Gomez Del Real                 simulate_wrmsr(cpu);
877c97d6d2cSSergio Andres Gomez Del Real             }
8785d32173fSRoman Bolshakov             env->eip += ins_len;
879c97d6d2cSSergio Andres Gomez Del Real             store_regs(cpu);
880c97d6d2cSSergio Andres Gomez Del Real             break;
881c97d6d2cSSergio Andres Gomez Del Real         }
882c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_CR_ACCESS: {
883c97d6d2cSSergio Andres Gomez Del Real             int cr;
884c97d6d2cSSergio Andres Gomez Del Real             int reg;
885c97d6d2cSSergio Andres Gomez Del Real 
886c97d6d2cSSergio Andres Gomez Del Real             load_regs(cpu);
887c97d6d2cSSergio Andres Gomez Del Real             cr = exit_qual & 15;
888c97d6d2cSSergio Andres Gomez Del Real             reg = (exit_qual >> 8) & 15;
889c97d6d2cSSergio Andres Gomez Del Real 
890c97d6d2cSSergio Andres Gomez Del Real             switch (cr) {
891c97d6d2cSSergio Andres Gomez Del Real             case 0x0: {
892c97d6d2cSSergio Andres Gomez Del Real                 macvm_set_cr0(cpu->hvf_fd, RRX(env, reg));
893c97d6d2cSSergio Andres Gomez Del Real                 break;
894c97d6d2cSSergio Andres Gomez Del Real             }
895c97d6d2cSSergio Andres Gomez Del Real             case 4: {
896c97d6d2cSSergio Andres Gomez Del Real                 macvm_set_cr4(cpu->hvf_fd, RRX(env, reg));
897c97d6d2cSSergio Andres Gomez Del Real                 break;
898c97d6d2cSSergio Andres Gomez Del Real             }
899c97d6d2cSSergio Andres Gomez Del Real             case 8: {
900c97d6d2cSSergio Andres Gomez Del Real                 X86CPU *x86_cpu = X86_CPU(cpu);
901c97d6d2cSSergio Andres Gomez Del Real                 if (exit_qual & 0x10) {
902c97d6d2cSSergio Andres Gomez Del Real                     RRX(env, reg) = cpu_get_apic_tpr(x86_cpu->apic_state);
903c97d6d2cSSergio Andres Gomez Del Real                 } else {
904c97d6d2cSSergio Andres Gomez Del Real                     int tpr = RRX(env, reg);
905c97d6d2cSSergio Andres Gomez Del Real                     cpu_set_apic_tpr(x86_cpu->apic_state, tpr);
906c97d6d2cSSergio Andres Gomez Del Real                     ret = EXCP_INTERRUPT;
907c97d6d2cSSergio Andres Gomez Del Real                 }
908c97d6d2cSSergio Andres Gomez Del Real                 break;
909c97d6d2cSSergio Andres Gomez Del Real             }
910c97d6d2cSSergio Andres Gomez Del Real             default:
9112d9178d9SLaurent Vivier                 error_report("Unrecognized CR %d", cr);
912c97d6d2cSSergio Andres Gomez Del Real                 abort();
913c97d6d2cSSergio Andres Gomez Del Real             }
9145d32173fSRoman Bolshakov             env->eip += ins_len;
915c97d6d2cSSergio Andres Gomez Del Real             store_regs(cpu);
916c97d6d2cSSergio Andres Gomez Del Real             break;
917c97d6d2cSSergio Andres Gomez Del Real         }
918c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_APIC_ACCESS: { /* TODO */
919c97d6d2cSSergio Andres Gomez Del Real             struct x86_decode decode;
920c97d6d2cSSergio Andres Gomez Del Real 
921c97d6d2cSSergio Andres Gomez Del Real             load_regs(cpu);
922c97d6d2cSSergio Andres Gomez Del Real             decode_instruction(env, &decode);
923c97d6d2cSSergio Andres Gomez Del Real             exec_instruction(env, &decode);
924c97d6d2cSSergio Andres Gomez Del Real             store_regs(cpu);
925c97d6d2cSSergio Andres Gomez Del Real             break;
926c97d6d2cSSergio Andres Gomez Del Real         }
927c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_TPR: {
928c97d6d2cSSergio Andres Gomez Del Real             ret = 1;
929c97d6d2cSSergio Andres Gomez Del Real             break;
930c97d6d2cSSergio Andres Gomez Del Real         }
931c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_TASK_SWITCH: {
932c97d6d2cSSergio Andres Gomez Del Real             uint64_t vinfo = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_INFO);
933c97d6d2cSSergio Andres Gomez Del Real             x68_segment_selector sel = {.sel = exit_qual & 0xffff};
934c97d6d2cSSergio Andres Gomez Del Real             vmx_handle_task_switch(cpu, sel, (exit_qual >> 30) & 0x3,
935c97d6d2cSSergio Andres Gomez Del Real              vinfo & VMCS_INTR_VALID, vinfo & VECTORING_INFO_VECTOR_MASK, vinfo
936c97d6d2cSSergio Andres Gomez Del Real              & VMCS_INTR_T_MASK);
937c97d6d2cSSergio Andres Gomez Del Real             break;
938c97d6d2cSSergio Andres Gomez Del Real         }
939c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_TRIPLE_FAULT: {
940c97d6d2cSSergio Andres Gomez Del Real             qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
941c97d6d2cSSergio Andres Gomez Del Real             ret = EXCP_INTERRUPT;
942c97d6d2cSSergio Andres Gomez Del Real             break;
943c97d6d2cSSergio Andres Gomez Del Real         }
944c97d6d2cSSergio Andres Gomez Del Real         case EXIT_REASON_RDPMC:
945c97d6d2cSSergio Andres Gomez Del Real             wreg(cpu->hvf_fd, HV_X86_RAX, 0);
946c97d6d2cSSergio Andres Gomez Del Real             wreg(cpu->hvf_fd, HV_X86_RDX, 0);
947c97d6d2cSSergio Andres Gomez Del Real             macvm_set_rip(cpu, rip + ins_len);
948c97d6d2cSSergio Andres Gomez Del Real             break;
949c97d6d2cSSergio Andres Gomez Del Real         case VMX_REASON_VMCALL:
950fd13f23bSLiran Alon             env->exception_nr = EXCP0D_GPF;
951fd13f23bSLiran Alon             env->exception_injected = 1;
9523010460fSSergio Andres Gomez Del Real             env->has_error_code = true;
9533010460fSSergio Andres Gomez Del Real             env->error_code = 0;
954c97d6d2cSSergio Andres Gomez Del Real             break;
955c97d6d2cSSergio Andres Gomez Del Real         default:
9562d9178d9SLaurent Vivier             error_report("%llx: unhandled exit %llx", rip, exit_reason);
957c97d6d2cSSergio Andres Gomez Del Real         }
958c97d6d2cSSergio Andres Gomez Del Real     } while (ret == 0);
959c97d6d2cSSergio Andres Gomez Del Real 
960c97d6d2cSSergio Andres Gomez Del Real     return ret;
961c97d6d2cSSergio Andres Gomez Del Real }
962c97d6d2cSSergio Andres Gomez Del Real 
96392cc3aaaSRoman Bolshakov bool hvf_allowed;
964c97d6d2cSSergio Andres Gomez Del Real 
965c97d6d2cSSergio Andres Gomez Del Real static int hvf_accel_init(MachineState *ms)
966c97d6d2cSSergio Andres Gomez Del Real {
967c97d6d2cSSergio Andres Gomez Del Real     int x;
968c97d6d2cSSergio Andres Gomez Del Real     hv_return_t ret;
969c97d6d2cSSergio Andres Gomez Del Real     HVFState *s;
970c97d6d2cSSergio Andres Gomez Del Real 
971c97d6d2cSSergio Andres Gomez Del Real     ret = hv_vm_create(HV_VM_DEFAULT);
972c97d6d2cSSergio Andres Gomez Del Real     assert_hvf_ok(ret);
973c97d6d2cSSergio Andres Gomez Del Real 
974c97d6d2cSSergio Andres Gomez Del Real     s = g_new0(HVFState, 1);
975c97d6d2cSSergio Andres Gomez Del Real 
976c97d6d2cSSergio Andres Gomez Del Real     s->num_slots = 32;
977c97d6d2cSSergio Andres Gomez Del Real     for (x = 0; x < s->num_slots; ++x) {
978c97d6d2cSSergio Andres Gomez Del Real         s->slots[x].size = 0;
979c97d6d2cSSergio Andres Gomez Del Real         s->slots[x].slot_id = x;
980c97d6d2cSSergio Andres Gomez Del Real     }
981c97d6d2cSSergio Andres Gomez Del Real 
982c97d6d2cSSergio Andres Gomez Del Real     hvf_state = s;
983c97d6d2cSSergio Andres Gomez Del Real     memory_listener_register(&hvf_memory_listener, &address_space_memory);
984c97d6d2cSSergio Andres Gomez Del Real     return 0;
985c97d6d2cSSergio Andres Gomez Del Real }
986c97d6d2cSSergio Andres Gomez Del Real 
987c97d6d2cSSergio Andres Gomez Del Real static void hvf_accel_class_init(ObjectClass *oc, void *data)
988c97d6d2cSSergio Andres Gomez Del Real {
989c97d6d2cSSergio Andres Gomez Del Real     AccelClass *ac = ACCEL_CLASS(oc);
990c97d6d2cSSergio Andres Gomez Del Real     ac->name = "HVF";
991c97d6d2cSSergio Andres Gomez Del Real     ac->init_machine = hvf_accel_init;
992c97d6d2cSSergio Andres Gomez Del Real     ac->allowed = &hvf_allowed;
993c97d6d2cSSergio Andres Gomez Del Real }
994c97d6d2cSSergio Andres Gomez Del Real 
995c97d6d2cSSergio Andres Gomez Del Real static const TypeInfo hvf_accel_type = {
996c97d6d2cSSergio Andres Gomez Del Real     .name = TYPE_HVF_ACCEL,
997c97d6d2cSSergio Andres Gomez Del Real     .parent = TYPE_ACCEL,
998c97d6d2cSSergio Andres Gomez Del Real     .class_init = hvf_accel_class_init,
999c97d6d2cSSergio Andres Gomez Del Real };
1000c97d6d2cSSergio Andres Gomez Del Real 
1001c97d6d2cSSergio Andres Gomez Del Real static void hvf_type_init(void)
1002c97d6d2cSSergio Andres Gomez Del Real {
1003c97d6d2cSSergio Andres Gomez Del Real     type_register_static(&hvf_accel_type);
1004c97d6d2cSSergio Andres Gomez Del Real }
1005c97d6d2cSSergio Andres Gomez Del Real 
1006c97d6d2cSSergio Andres Gomez Del Real type_init(hvf_type_init);
1007