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