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/error-report.h"
515df022cfSPeter Maydell #include "qemu/memalign.h"
529c267239SPhil Dennis-Jordan #include "qapi/error.h"
539c267239SPhil Dennis-Jordan #include "migration/blocker.h"
54c97d6d2cSSergio Andres Gomez Del Real
5532cad1ffSPhilippe Mathieu-Daudé #include "system/hvf.h"
5632cad1ffSPhilippe Mathieu-Daudé #include "system/hvf_int.h"
5732cad1ffSPhilippe Mathieu-Daudé #include "system/runstate.h"
5832cad1ffSPhilippe Mathieu-Daudé #include "system/cpus.h"
59c97d6d2cSSergio Andres Gomez Del Real #include "hvf-i386.h"
6069e0a03cSPaolo Bonzini #include "vmcs.h"
6169e0a03cSPaolo Bonzini #include "vmx.h"
6227458df8SWei Liu #include "emulate/x86.h"
6369e0a03cSPaolo Bonzini #include "x86_descr.h"
6427458df8SWei Liu #include "emulate/x86_flags.h"
6569e0a03cSPaolo Bonzini #include "x86_mmu.h"
6627458df8SWei Liu #include "emulate/x86_decode.h"
6727458df8SWei Liu #include "emulate/x86_emu.h"
6869e0a03cSPaolo Bonzini #include "x86_task.h"
6969e0a03cSPaolo Bonzini #include "x86hvf.h"
70c97d6d2cSSergio Andres Gomez Del Real
71c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv.h>
72c97d6d2cSSergio Andres Gomez Del Real #include <Hypervisor/hv_vmx.h>
733b502b0eSVladislav Yaroshchuk #include <sys/sysctl.h>
74c97d6d2cSSergio Andres Gomez Del Real
75c97d6d2cSSergio Andres Gomez Del Real #include "hw/i386/apic_internal.h"
76c97d6d2cSSergio Andres Gomez Del Real #include "qemu/main-loop.h"
77940e43aaSClaudio Fontana #include "qemu/accel.h"
78c97d6d2cSSergio Andres Gomez Del Real #include "target/i386/cpu.h"
79*785f945bSWei Liu #include "exec/target_page.h"
80c97d6d2cSSergio Andres Gomez Del Real
819c267239SPhil Dennis-Jordan static Error *invtsc_mig_blocker;
829c267239SPhil Dennis-Jordan
vmx_update_tpr(CPUState * cpu)83c97d6d2cSSergio Andres Gomez Del Real void vmx_update_tpr(CPUState *cpu)
84c97d6d2cSSergio Andres Gomez Del Real {
85c97d6d2cSSergio Andres Gomez Del Real /* TODO: need integrate APIC handling */
86c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu);
87c97d6d2cSSergio Andres Gomez Del Real int tpr = cpu_get_apic_tpr(x86_cpu->apic_state) << 4;
88c97d6d2cSSergio Andres Gomez Del Real int irr = apic_get_highest_priority_irr(x86_cpu->apic_state);
89c97d6d2cSSergio Andres Gomez Del Real
903b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_TPR, tpr);
91c97d6d2cSSergio Andres Gomez Del Real if (irr == -1) {
923b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_TPR_THRESHOLD, 0);
93c97d6d2cSSergio Andres Gomez Del Real } else {
943b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_TPR_THRESHOLD, (irr > tpr) ? tpr >> 4 :
95c97d6d2cSSergio Andres Gomez Del Real irr >> 4);
96c97d6d2cSSergio Andres Gomez Del Real }
97c97d6d2cSSergio Andres Gomez Del Real }
98c97d6d2cSSergio Andres Gomez Del Real
update_apic_tpr(CPUState * cpu)99583ae161SRoman Bolshakov static void update_apic_tpr(CPUState *cpu)
100c97d6d2cSSergio Andres Gomez Del Real {
101c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu);
1023b295bcbSPhilippe Mathieu-Daudé int tpr = rreg(cpu->accel->fd, HV_X86_TPR) >> 4;
103c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr);
104c97d6d2cSSergio Andres Gomez Del Real }
105c97d6d2cSSergio Andres Gomez Del Real
106c97d6d2cSSergio Andres Gomez Del Real #define VECTORING_INFO_VECTOR_MASK 0xff
107c97d6d2cSSergio Andres Gomez Del Real
hvf_handle_io(CPUState * env,uint16_t port,void * buffer,int direction,int size,int count)108bc4fa8c3SWei Liu void hvf_handle_io(CPUState *env, uint16_t port, void *buffer,
109c97d6d2cSSergio Andres Gomez Del Real int direction, int size, int count)
110c97d6d2cSSergio Andres Gomez Del Real {
111c97d6d2cSSergio Andres Gomez Del Real int i;
112c97d6d2cSSergio Andres Gomez Del Real uint8_t *ptr = buffer;
113c97d6d2cSSergio Andres Gomez Del Real
114c97d6d2cSSergio Andres Gomez Del Real for (i = 0; i < count; i++) {
115c97d6d2cSSergio Andres Gomez Del Real address_space_rw(&address_space_io, port, MEMTXATTRS_UNSPECIFIED,
116c97d6d2cSSergio Andres Gomez Del Real ptr, size,
117c97d6d2cSSergio Andres Gomez Del Real direction);
118c97d6d2cSSergio Andres Gomez Del Real ptr += size;
119c97d6d2cSSergio Andres Gomez Del Real }
120c97d6d2cSSergio Andres Gomez Del Real }
121c97d6d2cSSergio Andres Gomez Del Real
ept_emulation_fault(hvf_slot * slot,uint64_t gpa,uint64_t ept_qual)122ff2de166SPaolo Bonzini static bool ept_emulation_fault(hvf_slot *slot, uint64_t gpa, uint64_t ept_qual)
123c97d6d2cSSergio Andres Gomez Del Real {
124c97d6d2cSSergio Andres Gomez Del Real int read, write;
125c97d6d2cSSergio Andres Gomez Del Real
126c97d6d2cSSergio Andres Gomez Del Real /* EPT fault on an instruction fetch doesn't make sense here */
127c97d6d2cSSergio Andres Gomez Del Real if (ept_qual & EPT_VIOLATION_INST_FETCH) {
128c97d6d2cSSergio Andres Gomez Del Real return false;
129c97d6d2cSSergio Andres Gomez Del Real }
130c97d6d2cSSergio Andres Gomez Del Real
131c97d6d2cSSergio Andres Gomez Del Real /* EPT fault must be a read fault or a write fault */
132c97d6d2cSSergio Andres Gomez Del Real read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0;
133c97d6d2cSSergio Andres Gomez Del Real write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0;
134c97d6d2cSSergio Andres Gomez Del Real if ((read | write) == 0) {
135c97d6d2cSSergio Andres Gomez Del Real return false;
136c97d6d2cSSergio Andres Gomez Del Real }
137c97d6d2cSSergio Andres Gomez Del Real
138babfa20cSSergio Andres Gomez Del Real if (write && slot) {
139babfa20cSSergio Andres Gomez Del Real if (slot->flags & HVF_SLOT_LOG) {
1403e2c6727SPhil Dennis-Jordan uint64_t dirty_page_start = gpa & ~(TARGET_PAGE_SIZE - 1u);
141babfa20cSSergio Andres Gomez Del Real memory_region_set_dirty(slot->region, gpa - slot->start, 1);
1423e2c6727SPhil Dennis-Jordan hv_vm_protect(dirty_page_start, TARGET_PAGE_SIZE,
1433e2c6727SPhil Dennis-Jordan HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC);
144babfa20cSSergio Andres Gomez Del Real }
145babfa20cSSergio Andres Gomez Del Real }
146babfa20cSSergio Andres Gomez Del Real
147c97d6d2cSSergio Andres Gomez Del Real /*
148c97d6d2cSSergio Andres Gomez Del Real * The EPT violation must have been caused by accessing a
149c97d6d2cSSergio Andres Gomez Del Real * guest-physical address that is a translation of a guest-linear
150c97d6d2cSSergio Andres Gomez Del Real * address.
151c97d6d2cSSergio Andres Gomez Del Real */
152c97d6d2cSSergio Andres Gomez Del Real if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 ||
153c97d6d2cSSergio Andres Gomez Del Real (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) {
154c97d6d2cSSergio Andres Gomez Del Real return false;
155c97d6d2cSSergio Andres Gomez Del Real }
156c97d6d2cSSergio Andres Gomez Del Real
157fbafbb6dSCameron Esfahani if (!slot) {
158fbafbb6dSCameron Esfahani return true;
159fbafbb6dSCameron Esfahani }
160fbafbb6dSCameron Esfahani if (!memory_region_is_ram(slot->region) &&
161fbafbb6dSCameron Esfahani !(read && memory_region_is_romd(slot->region))) {
162fbafbb6dSCameron Esfahani return true;
163fbafbb6dSCameron Esfahani }
164fbafbb6dSCameron Esfahani return false;
165babfa20cSSergio Andres Gomez Del Real }
166babfa20cSSergio Andres Gomez Del Real
hvf_arch_vcpu_destroy(CPUState * cpu)167cfe58455SAlexander Graf void hvf_arch_vcpu_destroy(CPUState *cpu)
168c97d6d2cSSergio Andres Gomez Del Real {
169fe76b09cSRoman Bolshakov X86CPU *x86_cpu = X86_CPU(cpu);
170fe76b09cSRoman Bolshakov CPUX86State *env = &x86_cpu->env;
171fe76b09cSRoman Bolshakov
172e9c40026SWei Liu g_free(env->emu_mmio_buf);
173c97d6d2cSSergio Andres Gomez Del Real }
174c97d6d2cSSergio Andres Gomez Del Real
init_tsc_freq(CPUX86State * env)1753b502b0eSVladislav Yaroshchuk static void init_tsc_freq(CPUX86State *env)
1763b502b0eSVladislav Yaroshchuk {
1773b502b0eSVladislav Yaroshchuk size_t length;
1783b502b0eSVladislav Yaroshchuk uint64_t tsc_freq;
1793b502b0eSVladislav Yaroshchuk
1803b502b0eSVladislav Yaroshchuk if (env->tsc_khz != 0) {
1813b502b0eSVladislav Yaroshchuk return;
1823b502b0eSVladislav Yaroshchuk }
1833b502b0eSVladislav Yaroshchuk
1843b502b0eSVladislav Yaroshchuk length = sizeof(uint64_t);
1853b502b0eSVladislav Yaroshchuk if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) {
1863b502b0eSVladislav Yaroshchuk return;
1873b502b0eSVladislav Yaroshchuk }
1883b502b0eSVladislav Yaroshchuk env->tsc_khz = tsc_freq / 1000; /* Hz to KHz */
1893b502b0eSVladislav Yaroshchuk }
1903b502b0eSVladislav Yaroshchuk
init_apic_bus_freq(CPUX86State * env)1913b502b0eSVladislav Yaroshchuk static void init_apic_bus_freq(CPUX86State *env)
1923b502b0eSVladislav Yaroshchuk {
1933b502b0eSVladislav Yaroshchuk size_t length;
1943b502b0eSVladislav Yaroshchuk uint64_t bus_freq;
1953b502b0eSVladislav Yaroshchuk
1963b502b0eSVladislav Yaroshchuk if (env->apic_bus_freq != 0) {
1973b502b0eSVladislav Yaroshchuk return;
1983b502b0eSVladislav Yaroshchuk }
1993b502b0eSVladislav Yaroshchuk
2003b502b0eSVladislav Yaroshchuk length = sizeof(uint64_t);
2013b502b0eSVladislav Yaroshchuk if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) {
2023b502b0eSVladislav Yaroshchuk return;
2033b502b0eSVladislav Yaroshchuk }
2043b502b0eSVladislav Yaroshchuk env->apic_bus_freq = bus_freq;
2053b502b0eSVladislav Yaroshchuk }
2063b502b0eSVladislav Yaroshchuk
tsc_is_known(CPUX86State * env)2073b502b0eSVladislav Yaroshchuk static inline bool tsc_is_known(CPUX86State *env)
2083b502b0eSVladislav Yaroshchuk {
2093b502b0eSVladislav Yaroshchuk return env->tsc_khz != 0;
2103b502b0eSVladislav Yaroshchuk }
2113b502b0eSVladislav Yaroshchuk
apic_bus_freq_is_known(CPUX86State * env)2123b502b0eSVladislav Yaroshchuk static inline bool apic_bus_freq_is_known(CPUX86State *env)
2133b502b0eSVladislav Yaroshchuk {
2143b502b0eSVladislav Yaroshchuk return env->apic_bus_freq != 0;
2153b502b0eSVladislav Yaroshchuk }
2163b502b0eSVladislav Yaroshchuk
hvf_kick_vcpu_thread(CPUState * cpu)217a1477da3SAlexander Graf void hvf_kick_vcpu_thread(CPUState *cpu)
218a1477da3SAlexander Graf {
219a1477da3SAlexander Graf cpus_kick_thread(cpu);
220bf9bf230SPhil Dennis-Jordan hv_vcpu_interrupt(&cpu->accel->fd, 1);
221a1477da3SAlexander Graf }
222a1477da3SAlexander Graf
hvf_arch_init(void)223ce7f5b1cSAlexander Graf int hvf_arch_init(void)
224ce7f5b1cSAlexander Graf {
225ce7f5b1cSAlexander Graf return 0;
226ce7f5b1cSAlexander Graf }
227ce7f5b1cSAlexander Graf
hvf_arch_vm_create(MachineState * ms,uint32_t pa_range)2282c760670SDanny Canter hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)
2292c760670SDanny Canter {
2302c760670SDanny Canter return hv_vm_create(HV_VM_DEFAULT);
2312c760670SDanny Canter }
2322c760670SDanny Canter
hvf_read_segment_descriptor(CPUState * s,struct x86_segment_descriptor * desc,X86Seg seg)23326a44d9dSWei Liu static void hvf_read_segment_descriptor(CPUState *s, struct x86_segment_descriptor *desc,
23426a44d9dSWei Liu X86Seg seg)
23526a44d9dSWei Liu {
23626a44d9dSWei Liu struct vmx_segment vmx_segment;
23726a44d9dSWei Liu vmx_read_segment_descriptor(s, &vmx_segment, seg);
23826a44d9dSWei Liu vmx_segment_to_x86_descriptor(s, &vmx_segment, desc);
23926a44d9dSWei Liu }
24026a44d9dSWei Liu
hvf_read_mem(CPUState * cpu,void * data,target_ulong gva,int bytes)24126a44d9dSWei Liu static void hvf_read_mem(CPUState *cpu, void *data, target_ulong gva, int bytes)
24226a44d9dSWei Liu {
24326a44d9dSWei Liu vmx_read_mem(cpu, data, gva, bytes);
24426a44d9dSWei Liu }
24526a44d9dSWei Liu
hvf_write_mem(CPUState * cpu,void * data,target_ulong gva,int bytes)24663d8bc66SWei Liu static void hvf_write_mem(CPUState *cpu, void *data, target_ulong gva, int bytes)
24763d8bc66SWei Liu {
24863d8bc66SWei Liu vmx_write_mem(cpu, gva, data, bytes);
24963d8bc66SWei Liu }
25063d8bc66SWei Liu
25126a44d9dSWei Liu static const struct x86_emul_ops hvf_x86_emul_ops = {
25226a44d9dSWei Liu .read_mem = hvf_read_mem,
25363d8bc66SWei Liu .write_mem = hvf_write_mem,
25426a44d9dSWei Liu .read_segment_descriptor = hvf_read_segment_descriptor,
255444bae08SWei Liu .handle_io = hvf_handle_io,
25658567864SWei Liu .simulate_rdmsr = hvf_simulate_rdmsr,
25758567864SWei Liu .simulate_wrmsr = hvf_simulate_wrmsr,
25826a44d9dSWei Liu };
25926a44d9dSWei Liu
hvf_arch_init_vcpu(CPUState * cpu)260cfe58455SAlexander Graf int hvf_arch_init_vcpu(CPUState *cpu)
261c97d6d2cSSergio Andres Gomez Del Real {
262c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86cpu = X86_CPU(cpu);
263c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86cpu->env;
2649c267239SPhil Dennis-Jordan Error *local_err = NULL;
2659c267239SPhil Dennis-Jordan int r;
266d8cf2c29SCameron Esfahani uint64_t reqCap;
267c97d6d2cSSergio Andres Gomez Del Real
26826a44d9dSWei Liu init_emu(&hvf_x86_emul_ops);
269c97d6d2cSSergio Andres Gomez Del Real init_decoder();
270c97d6d2cSSergio Andres Gomez Del Real
2713a75ba65SPhil Dennis-Jordan if (hvf_state->hvf_caps == NULL) {
272c97d6d2cSSergio Andres Gomez Del Real hvf_state->hvf_caps = g_new0(struct hvf_vcpu_caps, 1);
2733a75ba65SPhil Dennis-Jordan }
274e9c40026SWei Liu env->emu_mmio_buf = g_new(char, 4096);
275c97d6d2cSSergio Andres Gomez Del Real
2763b502b0eSVladislav Yaroshchuk if (x86cpu->vmware_cpuid_freq) {
2773b502b0eSVladislav Yaroshchuk init_tsc_freq(env);
2783b502b0eSVladislav Yaroshchuk init_apic_bus_freq(env);
2793b502b0eSVladislav Yaroshchuk
2803b502b0eSVladislav Yaroshchuk if (!tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
2813b502b0eSVladislav Yaroshchuk error_report("vmware-cpuid-freq: feature couldn't be enabled");
2823b502b0eSVladislav Yaroshchuk }
2833b502b0eSVladislav Yaroshchuk }
2843b502b0eSVladislav Yaroshchuk
2859c267239SPhil Dennis-Jordan if ((env->features[FEAT_8000_0007_EDX] & CPUID_APM_INVTSC) &&
2869c267239SPhil Dennis-Jordan invtsc_mig_blocker == NULL) {
2879c267239SPhil Dennis-Jordan error_setg(&invtsc_mig_blocker,
2889c267239SPhil Dennis-Jordan "State blocked by non-migratable CPU device (invtsc flag)");
2899c267239SPhil Dennis-Jordan r = migrate_add_blocker(&invtsc_mig_blocker, &local_err);
2909c267239SPhil Dennis-Jordan if (r < 0) {
2919c267239SPhil Dennis-Jordan error_report_err(local_err);
2929c267239SPhil Dennis-Jordan return r;
2939c267239SPhil Dennis-Jordan }
2949c267239SPhil Dennis-Jordan }
2959c267239SPhil Dennis-Jordan
2969c267239SPhil Dennis-Jordan
297c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PINBASED,
298c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_pinbased)) {
299c97d6d2cSSergio Andres Gomez Del Real abort();
300c97d6d2cSSergio Andres Gomez Del Real }
301c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED,
302c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased)) {
303c97d6d2cSSergio Andres Gomez Del Real abort();
304c97d6d2cSSergio Andres Gomez Del Real }
305c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2,
306c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_procbased2)) {
307c97d6d2cSSergio Andres Gomez Del Real abort();
308c97d6d2cSSergio Andres Gomez Del Real }
309c97d6d2cSSergio Andres Gomez Del Real if (hv_vmx_read_capability(HV_VMX_CAP_ENTRY,
310c97d6d2cSSergio Andres Gomez Del Real &hvf_state->hvf_caps->vmx_cap_entry)) {
311c97d6d2cSSergio Andres Gomez Del Real abort();
312c97d6d2cSSergio Andres Gomez Del Real }
313c97d6d2cSSergio Andres Gomez Del Real
314c97d6d2cSSergio Andres Gomez Del Real /* set VMCS control fields */
3153b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_PIN_BASED_CTLS,
316c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_pinbased,
317c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_EXTINT |
318c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_NMI |
319c97d6d2cSSergio Andres Gomez Del Real VMCS_PIN_BASED_CTLS_VNMI));
3203b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_PRI_PROC_BASED_CTLS,
321c97d6d2cSSergio Andres Gomez Del Real cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased,
322c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_HLT |
323c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_MWAIT |
324c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TSC_OFFSET |
325c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_TPR_SHADOW) |
326c97d6d2cSSergio Andres Gomez Del Real VMCS_PRI_PROC_BASED_CTLS_SEC_CONTROL);
327d8cf2c29SCameron Esfahani
328d8cf2c29SCameron Esfahani reqCap = VMCS_PRI_PROC_BASED2_CTLS_APIC_ACCESSES;
329d8cf2c29SCameron Esfahani
330d8cf2c29SCameron Esfahani /* Is RDTSCP support in CPUID? If so, enable it in the VMCS. */
331d8cf2c29SCameron Esfahani if (hvf_get_supported_cpuid(0x80000001, 0, R_EDX) & CPUID_EXT2_RDTSCP) {
332d8cf2c29SCameron Esfahani reqCap |= VMCS_PRI_PROC_BASED2_CTLS_RDTSCP;
333d8cf2c29SCameron Esfahani }
334d8cf2c29SCameron Esfahani
3353b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_SEC_PROC_BASED_CTLS,
336d8cf2c29SCameron Esfahani cap2ctrl(hvf_state->hvf_caps->vmx_cap_procbased2, reqCap));
337c97d6d2cSSergio Andres Gomez Del Real
3383b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_ENTRY_CTLS,
3393b295bcbSPhilippe Mathieu-Daudé cap2ctrl(hvf_state->hvf_caps->vmx_cap_entry, 0));
3403b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_EXCEPTION_BITMAP, 0); /* Double fault */
341c97d6d2cSSergio Andres Gomez Del Real
3423b295bcbSPhilippe Mathieu-Daudé wvmcs(cpu->accel->fd, VMCS_TPR_THRESHOLD, 0);
343c97d6d2cSSergio Andres Gomez Del Real
344c97d6d2cSSergio Andres Gomez Del Real x86cpu = X86_CPU(cpu);
345c0198c5fSDavid Edmondson x86cpu->env.xsave_buf_len = 4096;
346c0198c5fSDavid Edmondson x86cpu->env.xsave_buf = qemu_memalign(4096, x86cpu->env.xsave_buf_len);
347c97d6d2cSSergio Andres Gomez Del Real
348fea45008SDavid Edmondson /*
349fea45008SDavid Edmondson * The allocated storage must be large enough for all of the
350fea45008SDavid Edmondson * possible XSAVE state components.
351fea45008SDavid Edmondson */
352fea45008SDavid Edmondson assert(hvf_get_supported_cpuid(0xd, 0, R_ECX) <= x86cpu->env.xsave_buf_len);
353fea45008SDavid Edmondson
3543b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_STAR, 1);
3553b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_LSTAR, 1);
3563b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_CSTAR, 1);
3573b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_FMASK, 1);
3583b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_FSBASE, 1);
3593b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_GSBASE, 1);
3603b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_KERNELGSBASE, 1);
3613b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_TSC_AUX, 1);
3623b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_TSC, 1);
3633b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_SYSENTER_CS, 1);
3643b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_SYSENTER_EIP, 1);
3653b295bcbSPhilippe Mathieu-Daudé hv_vcpu_enable_native_msr(cpu->accel->fd, MSR_IA32_SYSENTER_ESP, 1);
366c97d6d2cSSergio Andres Gomez Del Real
367c97d6d2cSSergio Andres Gomez Del Real return 0;
368c97d6d2cSSergio Andres Gomez Del Real }
369c97d6d2cSSergio Andres Gomez Del Real
hvf_store_events(CPUState * cpu,uint32_t ins_len,uint64_t idtvec_info)370b7394c83SSergio Andres Gomez Del Real static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_info)
371b7394c83SSergio Andres Gomez Del Real {
372b7394c83SSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu);
373b7394c83SSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env;
374b7394c83SSergio Andres Gomez Del Real
375fd13f23bSLiran Alon env->exception_nr = -1;
376fd13f23bSLiran Alon env->exception_pending = 0;
377fd13f23bSLiran Alon env->exception_injected = 0;
378b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = -1;
379b7394c83SSergio Andres Gomez Del Real env->nmi_injected = false;
38064bef038SCameron Esfahani env->ins_len = 0;
38164bef038SCameron Esfahani env->has_error_code = false;
382b7394c83SSergio Andres Gomez Del Real if (idtvec_info & VMCS_IDT_VEC_VALID) {
383b7394c83SSergio Andres Gomez Del Real switch (idtvec_info & VMCS_IDT_VEC_TYPE) {
384b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWINTR:
385b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWINTR:
386b7394c83SSergio Andres Gomez Del Real env->interrupt_injected = idtvec_info & VMCS_IDT_VEC_VECNUM;
387b7394c83SSergio Andres Gomez Del Real break;
388b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_NMI:
389b7394c83SSergio Andres Gomez Del Real env->nmi_injected = true;
390b7394c83SSergio Andres Gomez Del Real break;
391b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_HWEXCEPTION:
392b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_SWEXCEPTION:
393fd13f23bSLiran Alon env->exception_nr = idtvec_info & VMCS_IDT_VEC_VECNUM;
394fd13f23bSLiran Alon env->exception_injected = 1;
395b7394c83SSergio Andres Gomez Del Real break;
396b7394c83SSergio Andres Gomez Del Real case VMCS_IDT_VEC_PRIV_SWEXCEPTION:
397b7394c83SSergio Andres Gomez Del Real default:
398b7394c83SSergio Andres Gomez Del Real abort();
399b7394c83SSergio Andres Gomez Del Real }
400b7394c83SSergio Andres Gomez Del Real if ((idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWEXCEPTION ||
401b7394c83SSergio Andres Gomez Del Real (idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWINTR) {
402b7394c83SSergio Andres Gomez Del Real env->ins_len = ins_len;
403b7394c83SSergio Andres Gomez Del Real }
40464bef038SCameron Esfahani if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) {
405b7394c83SSergio Andres Gomez Del Real env->has_error_code = true;
4063b295bcbSPhilippe Mathieu-Daudé env->error_code = rvmcs(cpu->accel->fd, VMCS_IDT_VECTORING_ERROR);
407b7394c83SSergio Andres Gomez Del Real }
408b7394c83SSergio Andres Gomez Del Real }
4093b295bcbSPhilippe Mathieu-Daudé if ((rvmcs(cpu->accel->fd, VMCS_GUEST_INTERRUPTIBILITY) &
410b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_NMI_BLOCKING)) {
411b7394c83SSergio Andres Gomez Del Real env->hflags2 |= HF2_NMI_MASK;
412b7394c83SSergio Andres Gomez Del Real } else {
413b7394c83SSergio Andres Gomez Del Real env->hflags2 &= ~HF2_NMI_MASK;
414b7394c83SSergio Andres Gomez Del Real }
4153b295bcbSPhilippe Mathieu-Daudé if (rvmcs(cpu->accel->fd, VMCS_GUEST_INTERRUPTIBILITY) &
416b7394c83SSergio Andres Gomez Del Real (VMCS_INTERRUPTIBILITY_STI_BLOCKING |
417b7394c83SSergio Andres Gomez Del Real VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) {
418b7394c83SSergio Andres Gomez Del Real env->hflags |= HF_INHIBIT_IRQ_MASK;
419b7394c83SSergio Andres Gomez Del Real } else {
420b7394c83SSergio Andres Gomez Del Real env->hflags &= ~HF_INHIBIT_IRQ_MASK;
421b7394c83SSergio Andres Gomez Del Real }
422b7394c83SSergio Andres Gomez Del Real }
423b7394c83SSergio Andres Gomez Del Real
hvf_cpu_x86_cpuid(CPUX86State * env,uint32_t index,uint32_t count,uint32_t * eax,uint32_t * ebx,uint32_t * ecx,uint32_t * edx)4243b502b0eSVladislav Yaroshchuk static void hvf_cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
4253b502b0eSVladislav Yaroshchuk uint32_t *eax, uint32_t *ebx,
4263b502b0eSVladislav Yaroshchuk uint32_t *ecx, uint32_t *edx)
4273b502b0eSVladislav Yaroshchuk {
4283b502b0eSVladislav Yaroshchuk /*
4293b502b0eSVladislav Yaroshchuk * A wrapper extends cpu_x86_cpuid with 0x40000000 and 0x40000010 leafs,
4303b502b0eSVladislav Yaroshchuk * leafs 0x40000001-0x4000000F are filled with zeros
4313b502b0eSVladislav Yaroshchuk * Provides vmware-cpuid-freq support to hvf
4323b502b0eSVladislav Yaroshchuk *
4333b502b0eSVladislav Yaroshchuk * Note: leaf 0x40000000 not exposes HVF,
4343b502b0eSVladislav Yaroshchuk * leaving hypervisor signature empty
4353b502b0eSVladislav Yaroshchuk */
4363b502b0eSVladislav Yaroshchuk
4373b502b0eSVladislav Yaroshchuk if (index < 0x40000000 || index > 0x40000010 ||
4383b502b0eSVladislav Yaroshchuk !tsc_is_known(env) || !apic_bus_freq_is_known(env)) {
4393b502b0eSVladislav Yaroshchuk
4403b502b0eSVladislav Yaroshchuk cpu_x86_cpuid(env, index, count, eax, ebx, ecx, edx);
4413b502b0eSVladislav Yaroshchuk return;
4423b502b0eSVladislav Yaroshchuk }
4433b502b0eSVladislav Yaroshchuk
4443b502b0eSVladislav Yaroshchuk switch (index) {
4453b502b0eSVladislav Yaroshchuk case 0x40000000:
4463b502b0eSVladislav Yaroshchuk *eax = 0x40000010; /* Max available cpuid leaf */
4473b502b0eSVladislav Yaroshchuk *ebx = 0; /* Leave signature empty */
4483b502b0eSVladislav Yaroshchuk *ecx = 0;
4493b502b0eSVladislav Yaroshchuk *edx = 0;
4503b502b0eSVladislav Yaroshchuk break;
4513b502b0eSVladislav Yaroshchuk case 0x40000010:
4523b502b0eSVladislav Yaroshchuk *eax = env->tsc_khz;
4533b502b0eSVladislav Yaroshchuk *ebx = env->apic_bus_freq / 1000; /* Hz to KHz */
4543b502b0eSVladislav Yaroshchuk *ecx = 0;
4553b502b0eSVladislav Yaroshchuk *edx = 0;
4563b502b0eSVladislav Yaroshchuk break;
4573b502b0eSVladislav Yaroshchuk default:
4583b502b0eSVladislav Yaroshchuk *eax = 0;
4593b502b0eSVladislav Yaroshchuk *ebx = 0;
4603b502b0eSVladislav Yaroshchuk *ecx = 0;
4613b502b0eSVladislav Yaroshchuk *edx = 0;
4623b502b0eSVladislav Yaroshchuk break;
4633b502b0eSVladislav Yaroshchuk }
4643b502b0eSVladislav Yaroshchuk }
4653b502b0eSVladislav Yaroshchuk
hvf_load_regs(CPUState * cs)466dbccd48dSWei Liu void hvf_load_regs(CPUState *cs)
467dbccd48dSWei Liu {
468dbccd48dSWei Liu X86CPU *cpu = X86_CPU(cs);
469dbccd48dSWei Liu CPUX86State *env = &cpu->env;
470dbccd48dSWei Liu
471dbccd48dSWei Liu int i = 0;
472dbccd48dSWei Liu RRX(env, R_EAX) = rreg(cs->accel->fd, HV_X86_RAX);
473dbccd48dSWei Liu RRX(env, R_EBX) = rreg(cs->accel->fd, HV_X86_RBX);
474dbccd48dSWei Liu RRX(env, R_ECX) = rreg(cs->accel->fd, HV_X86_RCX);
475dbccd48dSWei Liu RRX(env, R_EDX) = rreg(cs->accel->fd, HV_X86_RDX);
476dbccd48dSWei Liu RRX(env, R_ESI) = rreg(cs->accel->fd, HV_X86_RSI);
477dbccd48dSWei Liu RRX(env, R_EDI) = rreg(cs->accel->fd, HV_X86_RDI);
478dbccd48dSWei Liu RRX(env, R_ESP) = rreg(cs->accel->fd, HV_X86_RSP);
479dbccd48dSWei Liu RRX(env, R_EBP) = rreg(cs->accel->fd, HV_X86_RBP);
480dbccd48dSWei Liu for (i = 8; i < 16; i++) {
481dbccd48dSWei Liu RRX(env, i) = rreg(cs->accel->fd, HV_X86_RAX + i);
482dbccd48dSWei Liu }
483dbccd48dSWei Liu
484dbccd48dSWei Liu env->eflags = rreg(cs->accel->fd, HV_X86_RFLAGS);
485dbccd48dSWei Liu rflags_to_lflags(env);
486dbccd48dSWei Liu env->eip = rreg(cs->accel->fd, HV_X86_RIP);
487dbccd48dSWei Liu }
488dbccd48dSWei Liu
hvf_store_regs(CPUState * cs)489dbccd48dSWei Liu void hvf_store_regs(CPUState *cs)
490dbccd48dSWei Liu {
491dbccd48dSWei Liu X86CPU *cpu = X86_CPU(cs);
492dbccd48dSWei Liu CPUX86State *env = &cpu->env;
493dbccd48dSWei Liu
494dbccd48dSWei Liu int i = 0;
495dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RAX, RAX(env));
496dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RBX, RBX(env));
497dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RCX, RCX(env));
498dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RDX, RDX(env));
499dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RSI, RSI(env));
500dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RDI, RDI(env));
501dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RBP, RBP(env));
502dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RSP, RSP(env));
503dbccd48dSWei Liu for (i = 8; i < 16; i++) {
504dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RAX + i, RRX(env, i));
505dbccd48dSWei Liu }
506dbccd48dSWei Liu
507dbccd48dSWei Liu lflags_to_rflags(env);
508dbccd48dSWei Liu wreg(cs->accel->fd, HV_X86_RFLAGS, env->eflags);
509dbccd48dSWei Liu macvm_set_rip(cs, env->eip);
510dbccd48dSWei Liu }
511dbccd48dSWei Liu
hvf_simulate_rdmsr(CPUState * cs)51258567864SWei Liu void hvf_simulate_rdmsr(CPUState *cs)
51399e5aaf9SWei Liu {
51458567864SWei Liu X86CPU *cpu = X86_CPU(cs);
51558567864SWei Liu CPUX86State *env = &cpu->env;
51699e5aaf9SWei Liu uint32_t msr = ECX(env);
51799e5aaf9SWei Liu uint64_t val = 0;
51899e5aaf9SWei Liu
51999e5aaf9SWei Liu switch (msr) {
52099e5aaf9SWei Liu case MSR_IA32_TSC:
52199e5aaf9SWei Liu val = rdtscp() + rvmcs(cs->accel->fd, VMCS_TSC_OFFSET);
52299e5aaf9SWei Liu break;
52399e5aaf9SWei Liu case MSR_IA32_APICBASE:
52499e5aaf9SWei Liu val = cpu_get_apic_base(cpu->apic_state);
52599e5aaf9SWei Liu break;
52699e5aaf9SWei Liu case MSR_APIC_START ... MSR_APIC_END: {
52799e5aaf9SWei Liu int ret;
52899e5aaf9SWei Liu int index = (uint32_t)env->regs[R_ECX] - MSR_APIC_START;
52999e5aaf9SWei Liu
53099e5aaf9SWei Liu ret = apic_msr_read(index, &val);
53199e5aaf9SWei Liu if (ret < 0) {
53299e5aaf9SWei Liu x86_emul_raise_exception(env, EXCP0D_GPF, 0);
53399e5aaf9SWei Liu }
53499e5aaf9SWei Liu
53599e5aaf9SWei Liu break;
53699e5aaf9SWei Liu }
53799e5aaf9SWei Liu case MSR_IA32_UCODE_REV:
53899e5aaf9SWei Liu val = cpu->ucode_rev;
53999e5aaf9SWei Liu break;
54099e5aaf9SWei Liu case MSR_EFER:
54199e5aaf9SWei Liu val = rvmcs(cs->accel->fd, VMCS_GUEST_IA32_EFER);
54299e5aaf9SWei Liu break;
54399e5aaf9SWei Liu case MSR_FSBASE:
54499e5aaf9SWei Liu val = rvmcs(cs->accel->fd, VMCS_GUEST_FS_BASE);
54599e5aaf9SWei Liu break;
54699e5aaf9SWei Liu case MSR_GSBASE:
54799e5aaf9SWei Liu val = rvmcs(cs->accel->fd, VMCS_GUEST_GS_BASE);
54899e5aaf9SWei Liu break;
54999e5aaf9SWei Liu case MSR_KERNELGSBASE:
55099e5aaf9SWei Liu val = rvmcs(cs->accel->fd, VMCS_HOST_FS_BASE);
55199e5aaf9SWei Liu break;
55299e5aaf9SWei Liu case MSR_STAR:
55399e5aaf9SWei Liu abort();
55499e5aaf9SWei Liu break;
55599e5aaf9SWei Liu case MSR_LSTAR:
55699e5aaf9SWei Liu abort();
55799e5aaf9SWei Liu break;
55899e5aaf9SWei Liu case MSR_CSTAR:
55999e5aaf9SWei Liu abort();
56099e5aaf9SWei Liu break;
56199e5aaf9SWei Liu case MSR_IA32_MISC_ENABLE:
56299e5aaf9SWei Liu val = env->msr_ia32_misc_enable;
56399e5aaf9SWei Liu break;
56499e5aaf9SWei Liu case MSR_MTRRphysBase(0):
56599e5aaf9SWei Liu case MSR_MTRRphysBase(1):
56699e5aaf9SWei Liu case MSR_MTRRphysBase(2):
56799e5aaf9SWei Liu case MSR_MTRRphysBase(3):
56899e5aaf9SWei Liu case MSR_MTRRphysBase(4):
56999e5aaf9SWei Liu case MSR_MTRRphysBase(5):
57099e5aaf9SWei Liu case MSR_MTRRphysBase(6):
57199e5aaf9SWei Liu case MSR_MTRRphysBase(7):
57299e5aaf9SWei Liu val = env->mtrr_var[(ECX(env) - MSR_MTRRphysBase(0)) / 2].base;
57399e5aaf9SWei Liu break;
57499e5aaf9SWei Liu case MSR_MTRRphysMask(0):
57599e5aaf9SWei Liu case MSR_MTRRphysMask(1):
57699e5aaf9SWei Liu case MSR_MTRRphysMask(2):
57799e5aaf9SWei Liu case MSR_MTRRphysMask(3):
57899e5aaf9SWei Liu case MSR_MTRRphysMask(4):
57999e5aaf9SWei Liu case MSR_MTRRphysMask(5):
58099e5aaf9SWei Liu case MSR_MTRRphysMask(6):
58199e5aaf9SWei Liu case MSR_MTRRphysMask(7):
58299e5aaf9SWei Liu val = env->mtrr_var[(ECX(env) - MSR_MTRRphysMask(0)) / 2].mask;
58399e5aaf9SWei Liu break;
58499e5aaf9SWei Liu case MSR_MTRRfix64K_00000:
58599e5aaf9SWei Liu val = env->mtrr_fixed[0];
58699e5aaf9SWei Liu break;
58799e5aaf9SWei Liu case MSR_MTRRfix16K_80000:
58899e5aaf9SWei Liu case MSR_MTRRfix16K_A0000:
58999e5aaf9SWei Liu val = env->mtrr_fixed[ECX(env) - MSR_MTRRfix16K_80000 + 1];
59099e5aaf9SWei Liu break;
59199e5aaf9SWei Liu case MSR_MTRRfix4K_C0000:
59299e5aaf9SWei Liu case MSR_MTRRfix4K_C8000:
59399e5aaf9SWei Liu case MSR_MTRRfix4K_D0000:
59499e5aaf9SWei Liu case MSR_MTRRfix4K_D8000:
59599e5aaf9SWei Liu case MSR_MTRRfix4K_E0000:
59699e5aaf9SWei Liu case MSR_MTRRfix4K_E8000:
59799e5aaf9SWei Liu case MSR_MTRRfix4K_F0000:
59899e5aaf9SWei Liu case MSR_MTRRfix4K_F8000:
59999e5aaf9SWei Liu val = env->mtrr_fixed[ECX(env) - MSR_MTRRfix4K_C0000 + 3];
60099e5aaf9SWei Liu break;
60199e5aaf9SWei Liu case MSR_MTRRdefType:
60299e5aaf9SWei Liu val = env->mtrr_deftype;
60399e5aaf9SWei Liu break;
60499e5aaf9SWei Liu case MSR_CORE_THREAD_COUNT:
60599e5aaf9SWei Liu val = cpu_x86_get_msr_core_thread_count(cpu);
60699e5aaf9SWei Liu break;
60799e5aaf9SWei Liu default:
60899e5aaf9SWei Liu /* fprintf(stderr, "%s: unknown msr 0x%x\n", __func__, msr); */
60999e5aaf9SWei Liu val = 0;
61099e5aaf9SWei Liu break;
61199e5aaf9SWei Liu }
61299e5aaf9SWei Liu
61399e5aaf9SWei Liu RAX(env) = (uint32_t)val;
61499e5aaf9SWei Liu RDX(env) = (uint32_t)(val >> 32);
61599e5aaf9SWei Liu }
61699e5aaf9SWei Liu
hvf_simulate_wrmsr(CPUState * cs)61758567864SWei Liu void hvf_simulate_wrmsr(CPUState *cs)
61899e5aaf9SWei Liu {
61958567864SWei Liu X86CPU *cpu = X86_CPU(cs);
62058567864SWei Liu CPUX86State *env = &cpu->env;
62199e5aaf9SWei Liu uint32_t msr = ECX(env);
62299e5aaf9SWei Liu uint64_t data = ((uint64_t)EDX(env) << 32) | EAX(env);
62399e5aaf9SWei Liu
62499e5aaf9SWei Liu switch (msr) {
62599e5aaf9SWei Liu case MSR_IA32_TSC:
62699e5aaf9SWei Liu break;
62799e5aaf9SWei Liu case MSR_IA32_APICBASE: {
62899e5aaf9SWei Liu int r;
62999e5aaf9SWei Liu
63099e5aaf9SWei Liu r = cpu_set_apic_base(cpu->apic_state, data);
63199e5aaf9SWei Liu if (r < 0) {
63299e5aaf9SWei Liu x86_emul_raise_exception(env, EXCP0D_GPF, 0);
63399e5aaf9SWei Liu }
63499e5aaf9SWei Liu
63599e5aaf9SWei Liu break;
63699e5aaf9SWei Liu }
63799e5aaf9SWei Liu case MSR_APIC_START ... MSR_APIC_END: {
63899e5aaf9SWei Liu int ret;
63999e5aaf9SWei Liu int index = (uint32_t)env->regs[R_ECX] - MSR_APIC_START;
64099e5aaf9SWei Liu
64199e5aaf9SWei Liu ret = apic_msr_write(index, data);
64299e5aaf9SWei Liu if (ret < 0) {
64399e5aaf9SWei Liu x86_emul_raise_exception(env, EXCP0D_GPF, 0);
64499e5aaf9SWei Liu }
64599e5aaf9SWei Liu
64699e5aaf9SWei Liu break;
64799e5aaf9SWei Liu }
64899e5aaf9SWei Liu case MSR_FSBASE:
64999e5aaf9SWei Liu wvmcs(cs->accel->fd, VMCS_GUEST_FS_BASE, data);
65099e5aaf9SWei Liu break;
65199e5aaf9SWei Liu case MSR_GSBASE:
65299e5aaf9SWei Liu wvmcs(cs->accel->fd, VMCS_GUEST_GS_BASE, data);
65399e5aaf9SWei Liu break;
65499e5aaf9SWei Liu case MSR_KERNELGSBASE:
65599e5aaf9SWei Liu wvmcs(cs->accel->fd, VMCS_HOST_FS_BASE, data);
65699e5aaf9SWei Liu break;
65799e5aaf9SWei Liu case MSR_STAR:
65899e5aaf9SWei Liu abort();
65999e5aaf9SWei Liu break;
66099e5aaf9SWei Liu case MSR_LSTAR:
66199e5aaf9SWei Liu abort();
66299e5aaf9SWei Liu break;
66399e5aaf9SWei Liu case MSR_CSTAR:
66499e5aaf9SWei Liu abort();
66599e5aaf9SWei Liu break;
66699e5aaf9SWei Liu case MSR_EFER:
66799e5aaf9SWei Liu /*printf("new efer %llx\n", EFER(cs));*/
66899e5aaf9SWei Liu wvmcs(cs->accel->fd, VMCS_GUEST_IA32_EFER, data);
66999e5aaf9SWei Liu if (data & MSR_EFER_NXE) {
67099e5aaf9SWei Liu hv_vcpu_invalidate_tlb(cs->accel->fd);
67199e5aaf9SWei Liu }
67299e5aaf9SWei Liu break;
67399e5aaf9SWei Liu case MSR_MTRRphysBase(0):
67499e5aaf9SWei Liu case MSR_MTRRphysBase(1):
67599e5aaf9SWei Liu case MSR_MTRRphysBase(2):
67699e5aaf9SWei Liu case MSR_MTRRphysBase(3):
67799e5aaf9SWei Liu case MSR_MTRRphysBase(4):
67899e5aaf9SWei Liu case MSR_MTRRphysBase(5):
67999e5aaf9SWei Liu case MSR_MTRRphysBase(6):
68099e5aaf9SWei Liu case MSR_MTRRphysBase(7):
68199e5aaf9SWei Liu env->mtrr_var[(ECX(env) - MSR_MTRRphysBase(0)) / 2].base = data;
68299e5aaf9SWei Liu break;
68399e5aaf9SWei Liu case MSR_MTRRphysMask(0):
68499e5aaf9SWei Liu case MSR_MTRRphysMask(1):
68599e5aaf9SWei Liu case MSR_MTRRphysMask(2):
68699e5aaf9SWei Liu case MSR_MTRRphysMask(3):
68799e5aaf9SWei Liu case MSR_MTRRphysMask(4):
68899e5aaf9SWei Liu case MSR_MTRRphysMask(5):
68999e5aaf9SWei Liu case MSR_MTRRphysMask(6):
69099e5aaf9SWei Liu case MSR_MTRRphysMask(7):
69199e5aaf9SWei Liu env->mtrr_var[(ECX(env) - MSR_MTRRphysMask(0)) / 2].mask = data;
69299e5aaf9SWei Liu break;
69399e5aaf9SWei Liu case MSR_MTRRfix64K_00000:
69499e5aaf9SWei Liu env->mtrr_fixed[ECX(env) - MSR_MTRRfix64K_00000] = data;
69599e5aaf9SWei Liu break;
69699e5aaf9SWei Liu case MSR_MTRRfix16K_80000:
69799e5aaf9SWei Liu case MSR_MTRRfix16K_A0000:
69899e5aaf9SWei Liu env->mtrr_fixed[ECX(env) - MSR_MTRRfix16K_80000 + 1] = data;
69999e5aaf9SWei Liu break;
70099e5aaf9SWei Liu case MSR_MTRRfix4K_C0000:
70199e5aaf9SWei Liu case MSR_MTRRfix4K_C8000:
70299e5aaf9SWei Liu case MSR_MTRRfix4K_D0000:
70399e5aaf9SWei Liu case MSR_MTRRfix4K_D8000:
70499e5aaf9SWei Liu case MSR_MTRRfix4K_E0000:
70599e5aaf9SWei Liu case MSR_MTRRfix4K_E8000:
70699e5aaf9SWei Liu case MSR_MTRRfix4K_F0000:
70799e5aaf9SWei Liu case MSR_MTRRfix4K_F8000:
70899e5aaf9SWei Liu env->mtrr_fixed[ECX(env) - MSR_MTRRfix4K_C0000 + 3] = data;
70999e5aaf9SWei Liu break;
71099e5aaf9SWei Liu case MSR_MTRRdefType:
71199e5aaf9SWei Liu env->mtrr_deftype = data;
71299e5aaf9SWei Liu break;
71399e5aaf9SWei Liu default:
71499e5aaf9SWei Liu break;
71599e5aaf9SWei Liu }
71699e5aaf9SWei Liu
71799e5aaf9SWei Liu /* Related to support known hypervisor interface */
71899e5aaf9SWei Liu /* if (g_hypervisor_iface)
71999e5aaf9SWei Liu g_hypervisor_iface->wrmsr_handler(cs, msr, data);
72099e5aaf9SWei Liu
72199e5aaf9SWei Liu printf("write msr %llx\n", RCX(cs));*/
72299e5aaf9SWei Liu }
72399e5aaf9SWei Liu
hvf_vcpu_exec(CPUState * cpu)724c97d6d2cSSergio Andres Gomez Del Real int hvf_vcpu_exec(CPUState *cpu)
725c97d6d2cSSergio Andres Gomez Del Real {
726c97d6d2cSSergio Andres Gomez Del Real X86CPU *x86_cpu = X86_CPU(cpu);
727c97d6d2cSSergio Andres Gomez Del Real CPUX86State *env = &x86_cpu->env;
728c97d6d2cSSergio Andres Gomez Del Real int ret = 0;
729c97d6d2cSSergio Andres Gomez Del Real uint64_t rip = 0;
730c97d6d2cSSergio Andres Gomez Del Real
731c97d6d2cSSergio Andres Gomez Del Real if (hvf_process_events(cpu)) {
732c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT;
733c97d6d2cSSergio Andres Gomez Del Real }
734c97d6d2cSSergio Andres Gomez Del Real
735c97d6d2cSSergio Andres Gomez Del Real do {
736e6203636SPhilippe Mathieu-Daudé if (cpu->accel->dirty) {
737c97d6d2cSSergio Andres Gomez Del Real hvf_put_registers(cpu);
738e6203636SPhilippe Mathieu-Daudé cpu->accel->dirty = false;
739c97d6d2cSSergio Andres Gomez Del Real }
740c97d6d2cSSergio Andres Gomez Del Real
741b7394c83SSergio Andres Gomez Del Real if (hvf_inject_interrupts(cpu)) {
742b7394c83SSergio Andres Gomez Del Real return EXCP_INTERRUPT;
743b7394c83SSergio Andres Gomez Del Real }
744c97d6d2cSSergio Andres Gomez Del Real vmx_update_tpr(cpu);
745c97d6d2cSSergio Andres Gomez Del Real
746195801d7SStefan Hajnoczi bql_unlock();
747c97d6d2cSSergio Andres Gomez Del Real if (!cpu_is_bsp(X86_CPU(cpu)) && cpu->halted) {
748195801d7SStefan Hajnoczi bql_lock();
749c97d6d2cSSergio Andres Gomez Del Real return EXCP_HLT;
750c97d6d2cSSergio Andres Gomez Del Real }
751c97d6d2cSSergio Andres Gomez Del Real
752f64933c8SAkihiko Odaki hv_return_t r = hv_vcpu_run_until(cpu->accel->fd, HV_DEADLINE_FOREVER);
753c97d6d2cSSergio Andres Gomez Del Real assert_hvf_ok(r);
754c97d6d2cSSergio Andres Gomez Del Real
755c97d6d2cSSergio Andres Gomez Del Real /* handle VMEXIT */
7563b295bcbSPhilippe Mathieu-Daudé uint64_t exit_reason = rvmcs(cpu->accel->fd, VMCS_EXIT_REASON);
7573b295bcbSPhilippe Mathieu-Daudé uint64_t exit_qual = rvmcs(cpu->accel->fd, VMCS_EXIT_QUALIFICATION);
7583b295bcbSPhilippe Mathieu-Daudé uint32_t ins_len = (uint32_t)rvmcs(cpu->accel->fd,
759c97d6d2cSSergio Andres Gomez Del Real VMCS_EXIT_INSTRUCTION_LENGTH);
760b7394c83SSergio Andres Gomez Del Real
7613b295bcbSPhilippe Mathieu-Daudé uint64_t idtvec_info = rvmcs(cpu->accel->fd, VMCS_IDT_VECTORING_INFO);
762b7394c83SSergio Andres Gomez Del Real
763b7394c83SSergio Andres Gomez Del Real hvf_store_events(cpu, ins_len, idtvec_info);
7643b295bcbSPhilippe Mathieu-Daudé rip = rreg(cpu->accel->fd, HV_X86_RIP);
7653b295bcbSPhilippe Mathieu-Daudé env->eflags = rreg(cpu->accel->fd, HV_X86_RFLAGS);
766c97d6d2cSSergio Andres Gomez Del Real
767195801d7SStefan Hajnoczi bql_lock();
768c97d6d2cSSergio Andres Gomez Del Real
769c97d6d2cSSergio Andres Gomez Del Real update_apic_tpr(cpu);
770c97d6d2cSSergio Andres Gomez Del Real current_cpu = cpu;
771c97d6d2cSSergio Andres Gomez Del Real
772c97d6d2cSSergio Andres Gomez Del Real ret = 0;
773c97d6d2cSSergio Andres Gomez Del Real switch (exit_reason) {
774c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_HLT: {
775c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len);
776c97d6d2cSSergio Andres Gomez Del Real if (!((cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
777967f4da2SRoman Bolshakov (env->eflags & IF_MASK))
778c97d6d2cSSergio Andres Gomez Del Real && !(cpu->interrupt_request & CPU_INTERRUPT_NMI) &&
779c97d6d2cSSergio Andres Gomez Del Real !(idtvec_info & VMCS_IDT_VEC_VALID)) {
780c97d6d2cSSergio Andres Gomez Del Real cpu->halted = 1;
781c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_HLT;
7823b9c59daSChen Zhang break;
783c97d6d2cSSergio Andres Gomez Del Real }
784c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT;
785c97d6d2cSSergio Andres Gomez Del Real break;
786c97d6d2cSSergio Andres Gomez Del Real }
787c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_MWAIT: {
788c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT;
789c97d6d2cSSergio Andres Gomez Del Real break;
790c97d6d2cSSergio Andres Gomez Del Real }
791fbafbb6dSCameron Esfahani /* Need to check if MMIO or unmapped fault */
792c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EPT_FAULT:
793c97d6d2cSSergio Andres Gomez Del Real {
794c97d6d2cSSergio Andres Gomez Del Real hvf_slot *slot;
7953b295bcbSPhilippe Mathieu-Daudé uint64_t gpa = rvmcs(cpu->accel->fd, VMCS_GUEST_PHYSICAL_ADDRESS);
796c97d6d2cSSergio Andres Gomez Del Real
797c97d6d2cSSergio Andres Gomez Del Real if (((idtvec_info & VMCS_IDT_VEC_VALID) == 0) &&
798c97d6d2cSSergio Andres Gomez Del Real ((exit_qual & EXIT_QUAL_NMIUDTI) != 0)) {
799c97d6d2cSSergio Andres Gomez Del Real vmx_set_nmi_blocking(cpu);
800c97d6d2cSSergio Andres Gomez Del Real }
801c97d6d2cSSergio Andres Gomez Del Real
802fbafbb6dSCameron Esfahani slot = hvf_find_overlap_slot(gpa, 1);
803c97d6d2cSSergio Andres Gomez Del Real /* mmio */
804babfa20cSSergio Andres Gomez Del Real if (ept_emulation_fault(slot, gpa, exit_qual)) {
805c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode;
806c97d6d2cSSergio Andres Gomez Del Real
807dbccd48dSWei Liu hvf_load_regs(cpu);
808c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode);
809c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode);
810dbccd48dSWei Liu hvf_store_regs(cpu);
811c97d6d2cSSergio Andres Gomez Del Real break;
812c97d6d2cSSergio Andres Gomez Del Real }
813c97d6d2cSSergio Andres Gomez Del Real break;
814c97d6d2cSSergio Andres Gomez Del Real }
815c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INOUT:
816c97d6d2cSSergio Andres Gomez Del Real {
817c97d6d2cSSergio Andres Gomez Del Real uint32_t in = (exit_qual & 8) != 0;
818c97d6d2cSSergio Andres Gomez Del Real uint32_t size = (exit_qual & 7) + 1;
819c97d6d2cSSergio Andres Gomez Del Real uint32_t string = (exit_qual & 16) != 0;
820c97d6d2cSSergio Andres Gomez Del Real uint32_t port = exit_qual >> 16;
821c97d6d2cSSergio Andres Gomez Del Real /*uint32_t rep = (exit_qual & 0x20) != 0;*/
822c97d6d2cSSergio Andres Gomez Del Real
823c97d6d2cSSergio Andres Gomez Del Real if (!string && in) {
824c97d6d2cSSergio Andres Gomez Del Real uint64_t val = 0;
825dbccd48dSWei Liu hvf_load_regs(cpu);
826bc4fa8c3SWei Liu hvf_handle_io(env_cpu(env), port, &val, 0, size, 1);
827c97d6d2cSSergio Andres Gomez Del Real if (size == 1) {
828c97d6d2cSSergio Andres Gomez Del Real AL(env) = val;
829c97d6d2cSSergio Andres Gomez Del Real } else if (size == 2) {
830c97d6d2cSSergio Andres Gomez Del Real AX(env) = val;
831c97d6d2cSSergio Andres Gomez Del Real } else if (size == 4) {
832c97d6d2cSSergio Andres Gomez Del Real RAX(env) = (uint32_t)val;
833c97d6d2cSSergio Andres Gomez Del Real } else {
834da20f5cdSPaolo Bonzini RAX(env) = (uint64_t)val;
835c97d6d2cSSergio Andres Gomez Del Real }
8365d32173fSRoman Bolshakov env->eip += ins_len;
837dbccd48dSWei Liu hvf_store_regs(cpu);
838c97d6d2cSSergio Andres Gomez Del Real break;
839c97d6d2cSSergio Andres Gomez Del Real } else if (!string && !in) {
8403b295bcbSPhilippe Mathieu-Daudé RAX(env) = rreg(cpu->accel->fd, HV_X86_RAX);
841bc4fa8c3SWei Liu hvf_handle_io(env_cpu(env), port, &RAX(env), 1, size, 1);
842c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len);
843c97d6d2cSSergio Andres Gomez Del Real break;
844c97d6d2cSSergio Andres Gomez Del Real }
845c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode;
846c97d6d2cSSergio Andres Gomez Del Real
847dbccd48dSWei Liu hvf_load_regs(cpu);
848c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode);
849e62963bfSPaolo Bonzini assert(ins_len == decode.len);
850c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode);
851dbccd48dSWei Liu hvf_store_regs(cpu);
852c97d6d2cSSergio Andres Gomez Del Real
853c97d6d2cSSergio Andres Gomez Del Real break;
854c97d6d2cSSergio Andres Gomez Del Real }
855c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CPUID: {
8563b295bcbSPhilippe Mathieu-Daudé uint32_t rax = (uint32_t)rreg(cpu->accel->fd, HV_X86_RAX);
8573b295bcbSPhilippe Mathieu-Daudé uint32_t rbx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RBX);
8583b295bcbSPhilippe Mathieu-Daudé uint32_t rcx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RCX);
8593b295bcbSPhilippe Mathieu-Daudé uint32_t rdx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RDX);
860c97d6d2cSSergio Andres Gomez Del Real
861106f91d5SAlexander Graf if (rax == 1) {
862106f91d5SAlexander Graf /* CPUID1.ecx.OSXSAVE needs to know CR4 */
8633b295bcbSPhilippe Mathieu-Daudé env->cr[4] = rvmcs(cpu->accel->fd, VMCS_GUEST_CR4);
864106f91d5SAlexander Graf }
8653b502b0eSVladislav Yaroshchuk hvf_cpu_x86_cpuid(env, rax, rcx, &rax, &rbx, &rcx, &rdx);
866c97d6d2cSSergio Andres Gomez Del Real
8673b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RAX, rax);
8683b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RBX, rbx);
8693b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RCX, rcx);
8703b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RDX, rdx);
871c97d6d2cSSergio Andres Gomez Del Real
872c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len);
873c97d6d2cSSergio Andres Gomez Del Real break;
874c97d6d2cSSergio Andres Gomez Del Real }
875c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_XSETBV: {
8763b295bcbSPhilippe Mathieu-Daudé uint32_t eax = (uint32_t)rreg(cpu->accel->fd, HV_X86_RAX);
8773b295bcbSPhilippe Mathieu-Daudé uint32_t ecx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RCX);
8783b295bcbSPhilippe Mathieu-Daudé uint32_t edx = (uint32_t)rreg(cpu->accel->fd, HV_X86_RDX);
879c97d6d2cSSergio Andres Gomez Del Real
880c97d6d2cSSergio Andres Gomez Del Real if (ecx) {
881c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len);
882c97d6d2cSSergio Andres Gomez Del Real break;
883c97d6d2cSSergio Andres Gomez Del Real }
884c97d6d2cSSergio Andres Gomez Del Real env->xcr0 = ((uint64_t)edx << 32) | eax;
8853b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_XCR0, env->xcr0 | 1);
886c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len);
887c97d6d2cSSergio Andres Gomez Del Real break;
888c97d6d2cSSergio Andres Gomez Del Real }
889c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_INTR_WINDOW:
890c97d6d2cSSergio Andres Gomez Del Real vmx_clear_int_window_exiting(cpu);
891c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT;
892c97d6d2cSSergio Andres Gomez Del Real break;
893c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_NMI_WINDOW:
894c97d6d2cSSergio Andres Gomez Del Real vmx_clear_nmi_window_exiting(cpu);
895c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT;
896c97d6d2cSSergio Andres Gomez Del Real break;
897c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_EXT_INTR:
898c97d6d2cSSergio Andres Gomez Del Real /* force exit and allow io handling */
899c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT;
900c97d6d2cSSergio Andres Gomez Del Real break;
901c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDMSR:
902c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_WRMSR:
903c97d6d2cSSergio Andres Gomez Del Real {
904dbccd48dSWei Liu hvf_load_regs(cpu);
905c97d6d2cSSergio Andres Gomez Del Real if (exit_reason == EXIT_REASON_RDMSR) {
90658567864SWei Liu hvf_simulate_rdmsr(cpu);
907c97d6d2cSSergio Andres Gomez Del Real } else {
90858567864SWei Liu hvf_simulate_wrmsr(cpu);
909c97d6d2cSSergio Andres Gomez Del Real }
9105d32173fSRoman Bolshakov env->eip += ins_len;
911dbccd48dSWei Liu hvf_store_regs(cpu);
912c97d6d2cSSergio Andres Gomez Del Real break;
913c97d6d2cSSergio Andres Gomez Del Real }
914c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_CR_ACCESS: {
915c97d6d2cSSergio Andres Gomez Del Real int cr;
916c97d6d2cSSergio Andres Gomez Del Real int reg;
917c97d6d2cSSergio Andres Gomez Del Real
918dbccd48dSWei Liu hvf_load_regs(cpu);
919c97d6d2cSSergio Andres Gomez Del Real cr = exit_qual & 15;
920c97d6d2cSSergio Andres Gomez Del Real reg = (exit_qual >> 8) & 15;
921c97d6d2cSSergio Andres Gomez Del Real
922c97d6d2cSSergio Andres Gomez Del Real switch (cr) {
923c97d6d2cSSergio Andres Gomez Del Real case 0x0: {
9243b295bcbSPhilippe Mathieu-Daudé macvm_set_cr0(cpu->accel->fd, RRX(env, reg));
925c97d6d2cSSergio Andres Gomez Del Real break;
926c97d6d2cSSergio Andres Gomez Del Real }
927c97d6d2cSSergio Andres Gomez Del Real case 4: {
9283b295bcbSPhilippe Mathieu-Daudé macvm_set_cr4(cpu->accel->fd, RRX(env, reg));
929c97d6d2cSSergio Andres Gomez Del Real break;
930c97d6d2cSSergio Andres Gomez Del Real }
931c97d6d2cSSergio Andres Gomez Del Real case 8: {
932c97d6d2cSSergio Andres Gomez Del Real if (exit_qual & 0x10) {
933c97d6d2cSSergio Andres Gomez Del Real RRX(env, reg) = cpu_get_apic_tpr(x86_cpu->apic_state);
934c97d6d2cSSergio Andres Gomez Del Real } else {
935c97d6d2cSSergio Andres Gomez Del Real int tpr = RRX(env, reg);
936c97d6d2cSSergio Andres Gomez Del Real cpu_set_apic_tpr(x86_cpu->apic_state, tpr);
937c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT;
938c97d6d2cSSergio Andres Gomez Del Real }
939c97d6d2cSSergio Andres Gomez Del Real break;
940c97d6d2cSSergio Andres Gomez Del Real }
941c97d6d2cSSergio Andres Gomez Del Real default:
9422d9178d9SLaurent Vivier error_report("Unrecognized CR %d", cr);
943c97d6d2cSSergio Andres Gomez Del Real abort();
944c97d6d2cSSergio Andres Gomez Del Real }
9455d32173fSRoman Bolshakov env->eip += ins_len;
946dbccd48dSWei Liu hvf_store_regs(cpu);
947c97d6d2cSSergio Andres Gomez Del Real break;
948c97d6d2cSSergio Andres Gomez Del Real }
949c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_APIC_ACCESS: { /* TODO */
950c97d6d2cSSergio Andres Gomez Del Real struct x86_decode decode;
951c97d6d2cSSergio Andres Gomez Del Real
952dbccd48dSWei Liu hvf_load_regs(cpu);
953c97d6d2cSSergio Andres Gomez Del Real decode_instruction(env, &decode);
954c97d6d2cSSergio Andres Gomez Del Real exec_instruction(env, &decode);
955dbccd48dSWei Liu hvf_store_regs(cpu);
956c97d6d2cSSergio Andres Gomez Del Real break;
957c97d6d2cSSergio Andres Gomez Del Real }
958c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TPR: {
959c97d6d2cSSergio Andres Gomez Del Real ret = 1;
960c97d6d2cSSergio Andres Gomez Del Real break;
961c97d6d2cSSergio Andres Gomez Del Real }
962c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TASK_SWITCH: {
9633b295bcbSPhilippe Mathieu-Daudé uint64_t vinfo = rvmcs(cpu->accel->fd, VMCS_IDT_VECTORING_INFO);
96425409172SWei Liu x86_segment_selector sel = {.sel = exit_qual & 0xffff};
965c97d6d2cSSergio Andres Gomez Del Real vmx_handle_task_switch(cpu, sel, (exit_qual >> 30) & 0x3,
966c97d6d2cSSergio Andres Gomez Del Real vinfo & VMCS_INTR_VALID, vinfo & VECTORING_INFO_VECTOR_MASK, vinfo
967c97d6d2cSSergio Andres Gomez Del Real & VMCS_INTR_T_MASK);
968c97d6d2cSSergio Andres Gomez Del Real break;
969c97d6d2cSSergio Andres Gomez Del Real }
970c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_TRIPLE_FAULT: {
971c97d6d2cSSergio Andres Gomez Del Real qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
972c97d6d2cSSergio Andres Gomez Del Real ret = EXCP_INTERRUPT;
973c97d6d2cSSergio Andres Gomez Del Real break;
974c97d6d2cSSergio Andres Gomez Del Real }
975c97d6d2cSSergio Andres Gomez Del Real case EXIT_REASON_RDPMC:
9763b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RAX, 0);
9773b295bcbSPhilippe Mathieu-Daudé wreg(cpu->accel->fd, HV_X86_RDX, 0);
978c97d6d2cSSergio Andres Gomez Del Real macvm_set_rip(cpu, rip + ins_len);
979c97d6d2cSSergio Andres Gomez Del Real break;
980c97d6d2cSSergio Andres Gomez Del Real case VMX_REASON_VMCALL:
981fd13f23bSLiran Alon env->exception_nr = EXCP0D_GPF;
982fd13f23bSLiran Alon env->exception_injected = 1;
9833010460fSSergio Andres Gomez Del Real env->has_error_code = true;
9843010460fSSergio Andres Gomez Del Real env->error_code = 0;
985c97d6d2cSSergio Andres Gomez Del Real break;
986c97d6d2cSSergio Andres Gomez Del Real default:
9872d9178d9SLaurent Vivier error_report("%llx: unhandled exit %llx", rip, exit_reason);
988c97d6d2cSSergio Andres Gomez Del Real }
989c97d6d2cSSergio Andres Gomez Del Real } while (ret == 0);
990c97d6d2cSSergio Andres Gomez Del Real
991c97d6d2cSSergio Andres Gomez Del Real return ret;
992c97d6d2cSSergio Andres Gomez Del Real }
993f4152040SFrancesco Cagnin
hvf_arch_insert_sw_breakpoint(CPUState * cpu,struct hvf_sw_breakpoint * bp)994f4152040SFrancesco Cagnin int hvf_arch_insert_sw_breakpoint(CPUState *cpu, struct hvf_sw_breakpoint *bp)
995f4152040SFrancesco Cagnin {
996f4152040SFrancesco Cagnin return -ENOSYS;
997f4152040SFrancesco Cagnin }
998f4152040SFrancesco Cagnin
hvf_arch_remove_sw_breakpoint(CPUState * cpu,struct hvf_sw_breakpoint * bp)999f4152040SFrancesco Cagnin int hvf_arch_remove_sw_breakpoint(CPUState *cpu, struct hvf_sw_breakpoint *bp)
1000f4152040SFrancesco Cagnin {
1001f4152040SFrancesco Cagnin return -ENOSYS;
1002f4152040SFrancesco Cagnin }
1003f4152040SFrancesco Cagnin
hvf_arch_insert_hw_breakpoint(vaddr addr,vaddr len,int type)1004d447a624SAnton Johansson int hvf_arch_insert_hw_breakpoint(vaddr addr, vaddr len, int type)
1005f4152040SFrancesco Cagnin {
1006f4152040SFrancesco Cagnin return -ENOSYS;
1007f4152040SFrancesco Cagnin }
1008f4152040SFrancesco Cagnin
hvf_arch_remove_hw_breakpoint(vaddr addr,vaddr len,int type)1009d447a624SAnton Johansson int hvf_arch_remove_hw_breakpoint(vaddr addr, vaddr len, int type)
1010f4152040SFrancesco Cagnin {
1011f4152040SFrancesco Cagnin return -ENOSYS;
1012f4152040SFrancesco Cagnin }
1013f4152040SFrancesco Cagnin
hvf_arch_remove_all_hw_breakpoints(void)1014f4152040SFrancesco Cagnin void hvf_arch_remove_all_hw_breakpoints(void)
1015f4152040SFrancesco Cagnin {
1016f4152040SFrancesco Cagnin }
1017eb2edc42SFrancesco Cagnin
hvf_arch_update_guest_debug(CPUState * cpu)1018eb2edc42SFrancesco Cagnin void hvf_arch_update_guest_debug(CPUState *cpu)
1019eb2edc42SFrancesco Cagnin {
1020eb2edc42SFrancesco Cagnin }
1021eb2edc42SFrancesco Cagnin
hvf_arch_supports_guest_debug(void)1022d6fd5d83SPhilippe Mathieu-Daudé bool hvf_arch_supports_guest_debug(void)
1023eb2edc42SFrancesco Cagnin {
1024eb2edc42SFrancesco Cagnin return false;
1025eb2edc42SFrancesco Cagnin }
1026