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