1 /* Copyright 2008 IBM Corporation
3 * Copyright 2011 Intel Corporation
4 * Copyright 2016 Veertu, Inc.
5 * Copyright 2017 The Android Open Source Project
7 * QEMU Hypervisor.framework support
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * This file contain code under public domain from the hvdos project:
22 * https://github.com/mist64/hvdos
24 * Parts Copyright (c) 2011 NetApp, Inc.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
36 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 #include "qemu/osdep.h"
49 #include "qemu-common.h"
50 #include "qemu/error-report.h"
52 #include "sysemu/hvf.h"
57 #include "x86_descr.h"
59 #include "x86_decode.h"
64 #include <Hypervisor/hv.h>
65 #include <Hypervisor/hv_vmx.h>
67 #include "exec/address-spaces.h"
68 #include "exec/exec-all.h"
69 #include "exec/ioport.h"
70 #include "hw/i386/apic_internal.h"
71 #include "hw/boards.h"
72 #include "qemu/main-loop.h"
74 #include "sysemu/accel.h"
75 #include "sysemu/sysemu.h"
76 #include "target/i386/cpu.h"
78 pthread_rwlock_t mem_lock
= PTHREAD_RWLOCK_INITIALIZER
;
82 static void assert_hvf_ok(hv_return_t ret
)
84 if (ret
== HV_SUCCESS
) {
90 error_report("Error: HV_ERROR\n");
93 error_report("Error: HV_BUSY\n");
96 error_report("Error: HV_BAD_ARGUMENT\n");
99 error_report("Error: HV_NO_RESOURCES\n");
102 error_report("Error: HV_NO_DEVICE\n");
105 error_report("Error: HV_UNSUPPORTED\n");
108 error_report("Unknown Error\n");
115 hvf_slot
*hvf_find_overlap_slot(uint64_t start
, uint64_t end
)
119 for (x
= 0; x
< hvf_state
->num_slots
; ++x
) {
120 slot
= &hvf_state
->slots
[x
];
121 if (slot
->size
&& start
< (slot
->start
+ slot
->size
) &&
136 struct mac_slot mac_slots
[32];
137 #define ALIGN(x, y) (((x) + (y) - 1) & ~((y) - 1))
139 static int do_hvf_set_memory(hvf_slot
*slot
)
141 struct mac_slot
*macslot
;
142 hv_memory_flags_t flags
;
145 macslot
= &mac_slots
[slot
->slot_id
];
147 if (macslot
->present
) {
148 if (macslot
->size
!= slot
->size
) {
149 macslot
->present
= 0;
150 ret
= hv_vm_unmap(macslot
->gpa_start
, macslot
->size
);
159 flags
= HV_MEMORY_READ
| HV_MEMORY_WRITE
| HV_MEMORY_EXEC
;
161 macslot
->present
= 1;
162 macslot
->gpa_start
= slot
->start
;
163 macslot
->size
= slot
->size
;
164 ret
= hv_vm_map((hv_uvaddr_t
)slot
->mem
, slot
->start
, slot
->size
, flags
);
169 void hvf_set_phys_mem(MemoryRegionSection
*section
, bool add
)
172 MemoryRegion
*area
= section
->mr
;
174 if (!memory_region_is_ram(area
)) {
178 mem
= hvf_find_overlap_slot(
179 section
->offset_within_address_space
,
180 section
->offset_within_address_space
+ int128_get64(section
->size
));
183 if (mem
->size
== int128_get64(section
->size
) &&
184 mem
->start
== section
->offset_within_address_space
&&
185 mem
->mem
== (memory_region_get_ram_ptr(area
) +
186 section
->offset_within_region
)) {
187 return; /* Same region was attempted to register, go away. */
191 /* Region needs to be reset. set the size to 0 and remap it. */
194 if (do_hvf_set_memory(mem
)) {
195 error_report("Failed to reset overlapping slot\n");
204 /* Now make a new slot. */
207 for (x
= 0; x
< hvf_state
->num_slots
; ++x
) {
208 mem
= &hvf_state
->slots
[x
];
214 if (x
== hvf_state
->num_slots
) {
215 error_report("No free slots\n");
219 mem
->size
= int128_get64(section
->size
);
220 mem
->mem
= memory_region_get_ram_ptr(area
) + section
->offset_within_region
;
221 mem
->start
= section
->offset_within_address_space
;
224 if (do_hvf_set_memory(mem
)) {
225 error_report("Error registering new memory slot\n");
230 void vmx_update_tpr(CPUState
*cpu
)
232 /* TODO: need integrate APIC handling */
233 X86CPU
*x86_cpu
= X86_CPU(cpu
);
234 int tpr
= cpu_get_apic_tpr(x86_cpu
->apic_state
) << 4;
235 int irr
= apic_get_highest_priority_irr(x86_cpu
->apic_state
);
237 wreg(cpu
->hvf_fd
, HV_X86_TPR
, tpr
);
239 wvmcs(cpu
->hvf_fd
, VMCS_TPR_THRESHOLD
, 0);
241 wvmcs(cpu
->hvf_fd
, VMCS_TPR_THRESHOLD
, (irr
> tpr
) ? tpr
>> 4 :
246 void update_apic_tpr(CPUState
*cpu
)
248 X86CPU
*x86_cpu
= X86_CPU(cpu
);
249 int tpr
= rreg(cpu
->hvf_fd
, HV_X86_TPR
) >> 4;
250 cpu_set_apic_tpr(x86_cpu
->apic_state
, tpr
);
253 #define VECTORING_INFO_VECTOR_MASK 0xff
255 static void hvf_handle_interrupt(CPUState
* cpu
, int mask
)
257 cpu
->interrupt_request
|= mask
;
258 if (!qemu_cpu_is_self(cpu
)) {
263 void hvf_handle_io(CPUArchState
*env
, uint16_t port
, void *buffer
,
264 int direction
, int size
, int count
)
267 uint8_t *ptr
= buffer
;
269 for (i
= 0; i
< count
; i
++) {
270 address_space_rw(&address_space_io
, port
, MEMTXATTRS_UNSPECIFIED
,
277 /* TODO: synchronize vcpu state */
278 static void do_hvf_cpu_synchronize_state(CPUState
*cpu
, run_on_cpu_data arg
)
280 CPUState
*cpu_state
= cpu
;
281 if (cpu_state
->vcpu_dirty
== 0) {
282 hvf_get_registers(cpu_state
);
285 cpu_state
->vcpu_dirty
= 1;
288 void hvf_cpu_synchronize_state(CPUState
*cpu_state
)
290 if (cpu_state
->vcpu_dirty
== 0) {
291 run_on_cpu(cpu_state
, do_hvf_cpu_synchronize_state
, RUN_ON_CPU_NULL
);
295 static void do_hvf_cpu_synchronize_post_reset(CPUState
*cpu
, run_on_cpu_data arg
)
297 CPUState
*cpu_state
= cpu
;
298 hvf_put_registers(cpu_state
);
299 cpu_state
->vcpu_dirty
= false;
302 void hvf_cpu_synchronize_post_reset(CPUState
*cpu_state
)
304 run_on_cpu(cpu_state
, do_hvf_cpu_synchronize_post_reset
, RUN_ON_CPU_NULL
);
307 void _hvf_cpu_synchronize_post_init(CPUState
*cpu
, run_on_cpu_data arg
)
309 CPUState
*cpu_state
= cpu
;
310 hvf_put_registers(cpu_state
);
311 cpu_state
->vcpu_dirty
= false;
314 void hvf_cpu_synchronize_post_init(CPUState
*cpu_state
)
316 run_on_cpu(cpu_state
, _hvf_cpu_synchronize_post_init
, RUN_ON_CPU_NULL
);
319 static bool ept_emulation_fault(hvf_slot
*slot
, uint64_t gpa
, uint64_t ept_qual
)
323 /* EPT fault on an instruction fetch doesn't make sense here */
324 if (ept_qual
& EPT_VIOLATION_INST_FETCH
) {
328 /* EPT fault must be a read fault or a write fault */
329 read
= ept_qual
& EPT_VIOLATION_DATA_READ
? 1 : 0;
330 write
= ept_qual
& EPT_VIOLATION_DATA_WRITE
? 1 : 0;
331 if ((read
| write
) == 0) {
336 if (slot
->flags
& HVF_SLOT_LOG
) {
337 memory_region_set_dirty(slot
->region
, gpa
- slot
->start
, 1);
338 hv_vm_protect((hv_gpaddr_t
)slot
->start
, (size_t)slot
->size
,
339 HV_MEMORY_READ
| HV_MEMORY_WRITE
);
344 * The EPT violation must have been caused by accessing a
345 * guest-physical address that is a translation of a guest-linear
348 if ((ept_qual
& EPT_VIOLATION_GLA_VALID
) == 0 ||
349 (ept_qual
& EPT_VIOLATION_XLAT_VALID
) == 0) {
356 static void hvf_set_dirty_tracking(MemoryRegionSection
*section
, bool on
)
360 slot
= hvf_find_overlap_slot(
361 section
->offset_within_address_space
,
362 section
->offset_within_address_space
+ int128_get64(section
->size
));
364 /* protect region against writes; begin tracking it */
366 slot
->flags
|= HVF_SLOT_LOG
;
367 hv_vm_protect((hv_gpaddr_t
)slot
->start
, (size_t)slot
->size
,
369 /* stop tracking region*/
371 slot
->flags
&= ~HVF_SLOT_LOG
;
372 hv_vm_protect((hv_gpaddr_t
)slot
->start
, (size_t)slot
->size
,
373 HV_MEMORY_READ
| HV_MEMORY_WRITE
);
377 static void hvf_log_start(MemoryListener
*listener
,
378 MemoryRegionSection
*section
, int old
, int new)
384 hvf_set_dirty_tracking(section
, 1);
387 static void hvf_log_stop(MemoryListener
*listener
,
388 MemoryRegionSection
*section
, int old
, int new)
394 hvf_set_dirty_tracking(section
, 0);
397 static void hvf_log_sync(MemoryListener
*listener
,
398 MemoryRegionSection
*section
)
401 * sync of dirty pages is handled elsewhere; just make sure we keep
402 * tracking the region.
404 hvf_set_dirty_tracking(section
, 1);
407 static void hvf_region_add(MemoryListener
*listener
,
408 MemoryRegionSection
*section
)
410 hvf_set_phys_mem(section
, true);
413 static void hvf_region_del(MemoryListener
*listener
,
414 MemoryRegionSection
*section
)
416 hvf_set_phys_mem(section
, false);
419 static MemoryListener hvf_memory_listener
= {
421 .region_add
= hvf_region_add
,
422 .region_del
= hvf_region_del
,
423 .log_start
= hvf_log_start
,
424 .log_stop
= hvf_log_stop
,
425 .log_sync
= hvf_log_sync
,
428 void hvf_reset_vcpu(CPUState
*cpu
) {
430 /* TODO: this shouldn't be needed; there is already a call to
431 * cpu_synchronize_all_post_reset in vl.c
433 wvmcs(cpu
->hvf_fd
, VMCS_ENTRY_CTLS
, 0);
434 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_IA32_EFER
, 0);
435 macvm_set_cr0(cpu
->hvf_fd
, 0x60000010);
437 wvmcs(cpu
->hvf_fd
, VMCS_CR4_MASK
, CR4_VMXE_MASK
);
438 wvmcs(cpu
->hvf_fd
, VMCS_CR4_SHADOW
, 0x0);
439 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_CR4
, CR4_VMXE_MASK
);
441 /* set VMCS guest state fields */
442 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_CS_SELECTOR
, 0xf000);
443 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_CS_LIMIT
, 0xffff);
444 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_CS_ACCESS_RIGHTS
, 0x9b);
445 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_CS_BASE
, 0xffff0000);
447 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_DS_SELECTOR
, 0);
448 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_DS_LIMIT
, 0xffff);
449 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_DS_ACCESS_RIGHTS
, 0x93);
450 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_DS_BASE
, 0);
452 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_ES_SELECTOR
, 0);
453 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_ES_LIMIT
, 0xffff);
454 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_ES_ACCESS_RIGHTS
, 0x93);
455 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_ES_BASE
, 0);
457 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_FS_SELECTOR
, 0);
458 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_FS_LIMIT
, 0xffff);
459 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_FS_ACCESS_RIGHTS
, 0x93);
460 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_FS_BASE
, 0);
462 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_GS_SELECTOR
, 0);
463 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_GS_LIMIT
, 0xffff);
464 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_GS_ACCESS_RIGHTS
, 0x93);
465 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_GS_BASE
, 0);
467 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_SS_SELECTOR
, 0);
468 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_SS_LIMIT
, 0xffff);
469 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_SS_ACCESS_RIGHTS
, 0x93);
470 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_SS_BASE
, 0);
472 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_LDTR_SELECTOR
, 0);
473 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_LDTR_LIMIT
, 0);
474 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_LDTR_ACCESS_RIGHTS
, 0x10000);
475 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_LDTR_BASE
, 0);
477 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_TR_SELECTOR
, 0);
478 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_TR_LIMIT
, 0);
479 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_TR_ACCESS_RIGHTS
, 0x83);
480 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_TR_BASE
, 0);
482 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_GDTR_LIMIT
, 0);
483 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_GDTR_BASE
, 0);
485 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_IDTR_LIMIT
, 0);
486 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_IDTR_BASE
, 0);
488 /*wvmcs(cpu->hvf_fd, VMCS_GUEST_CR2, 0x0);*/
489 wvmcs(cpu
->hvf_fd
, VMCS_GUEST_CR3
, 0x0);
491 wreg(cpu
->hvf_fd
, HV_X86_RIP
, 0xfff0);
492 wreg(cpu
->hvf_fd
, HV_X86_RDX
, 0x623);
493 wreg(cpu
->hvf_fd
, HV_X86_RFLAGS
, 0x2);
494 wreg(cpu
->hvf_fd
, HV_X86_RSP
, 0x0);
495 wreg(cpu
->hvf_fd
, HV_X86_RAX
, 0x0);
496 wreg(cpu
->hvf_fd
, HV_X86_RBX
, 0x0);
497 wreg(cpu
->hvf_fd
, HV_X86_RCX
, 0x0);
498 wreg(cpu
->hvf_fd
, HV_X86_RSI
, 0x0);
499 wreg(cpu
->hvf_fd
, HV_X86_RDI
, 0x0);
500 wreg(cpu
->hvf_fd
, HV_X86_RBP
, 0x0);
502 for (int i
= 0; i
< 8; i
++) {
503 wreg(cpu
->hvf_fd
, HV_X86_R8
+ i
, 0x0);
508 hv_vcpu_invalidate_tlb(cpu
->hvf_fd
);
509 hv_vcpu_flush(cpu
->hvf_fd
);
512 void hvf_vcpu_destroy(CPUState
*cpu
)
514 hv_return_t ret
= hv_vcpu_destroy((hv_vcpuid_t
)cpu
->hvf_fd
);
518 static void dummy_signal(int sig
)
522 int hvf_init_vcpu(CPUState
*cpu
)
525 X86CPU
*x86cpu
= X86_CPU(cpu
);
526 CPUX86State
*env
= &x86cpu
->env
;
529 /* init cpu signals */
531 struct sigaction sigact
;
533 memset(&sigact
, 0, sizeof(sigact
));
534 sigact
.sa_handler
= dummy_signal
;
535 sigaction(SIG_IPI
, &sigact
, NULL
);
537 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
538 sigdelset(&set
, SIG_IPI
);
543 hvf_state
->hvf_caps
= g_new0(struct hvf_vcpu_caps
, 1);
544 env
->hvf_emul
= g_new0(HVFX86EmulatorState
, 1);
546 r
= hv_vcpu_create((hv_vcpuid_t
*)&cpu
->hvf_fd
, HV_VCPU_DEFAULT
);
550 if (hv_vmx_read_capability(HV_VMX_CAP_PINBASED
,
551 &hvf_state
->hvf_caps
->vmx_cap_pinbased
)) {
554 if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED
,
555 &hvf_state
->hvf_caps
->vmx_cap_procbased
)) {
558 if (hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2
,
559 &hvf_state
->hvf_caps
->vmx_cap_procbased2
)) {
562 if (hv_vmx_read_capability(HV_VMX_CAP_ENTRY
,
563 &hvf_state
->hvf_caps
->vmx_cap_entry
)) {
567 /* set VMCS control fields */
568 wvmcs(cpu
->hvf_fd
, VMCS_PIN_BASED_CTLS
,
569 cap2ctrl(hvf_state
->hvf_caps
->vmx_cap_pinbased
,
570 VMCS_PIN_BASED_CTLS_EXTINT
|
571 VMCS_PIN_BASED_CTLS_NMI
|
572 VMCS_PIN_BASED_CTLS_VNMI
));
573 wvmcs(cpu
->hvf_fd
, VMCS_PRI_PROC_BASED_CTLS
,
574 cap2ctrl(hvf_state
->hvf_caps
->vmx_cap_procbased
,
575 VMCS_PRI_PROC_BASED_CTLS_HLT
|
576 VMCS_PRI_PROC_BASED_CTLS_MWAIT
|
577 VMCS_PRI_PROC_BASED_CTLS_TSC_OFFSET
|
578 VMCS_PRI_PROC_BASED_CTLS_TPR_SHADOW
) |
579 VMCS_PRI_PROC_BASED_CTLS_SEC_CONTROL
);
580 wvmcs(cpu
->hvf_fd
, VMCS_SEC_PROC_BASED_CTLS
,
581 cap2ctrl(hvf_state
->hvf_caps
->vmx_cap_procbased2
,
582 VMCS_PRI_PROC_BASED2_CTLS_APIC_ACCESSES
));
584 wvmcs(cpu
->hvf_fd
, VMCS_ENTRY_CTLS
, cap2ctrl(hvf_state
->hvf_caps
->vmx_cap_entry
,
586 wvmcs(cpu
->hvf_fd
, VMCS_EXCEPTION_BITMAP
, 0); /* Double fault */
588 wvmcs(cpu
->hvf_fd
, VMCS_TPR_THRESHOLD
, 0);
592 x86cpu
= X86_CPU(cpu
);
593 x86cpu
->env
.kvm_xsave_buf
= qemu_memalign(4096, 4096);
595 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_STAR
, 1);
596 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_LSTAR
, 1);
597 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_CSTAR
, 1);
598 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_FMASK
, 1);
599 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_FSBASE
, 1);
600 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_GSBASE
, 1);
601 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_KERNELGSBASE
, 1);
602 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_TSC_AUX
, 1);
603 /*hv_vcpu_enable_native_msr(cpu->hvf_fd, MSR_IA32_TSC, 1);*/
604 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_IA32_SYSENTER_CS
, 1);
605 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_IA32_SYSENTER_EIP
, 1);
606 hv_vcpu_enable_native_msr(cpu
->hvf_fd
, MSR_IA32_SYSENTER_ESP
, 1);
611 void hvf_disable(int shouldDisable
)
613 hvf_disabled
= shouldDisable
;
616 static void hvf_store_events(CPUState
*cpu
, uint32_t ins_len
, uint64_t idtvec_info
)
618 X86CPU
*x86_cpu
= X86_CPU(cpu
);
619 CPUX86State
*env
= &x86_cpu
->env
;
621 env
->exception_injected
= -1;
622 env
->interrupt_injected
= -1;
623 env
->nmi_injected
= false;
624 if (idtvec_info
& VMCS_IDT_VEC_VALID
) {
625 switch (idtvec_info
& VMCS_IDT_VEC_TYPE
) {
626 case VMCS_IDT_VEC_HWINTR
:
627 case VMCS_IDT_VEC_SWINTR
:
628 env
->interrupt_injected
= idtvec_info
& VMCS_IDT_VEC_VECNUM
;
630 case VMCS_IDT_VEC_NMI
:
631 env
->nmi_injected
= true;
633 case VMCS_IDT_VEC_HWEXCEPTION
:
634 case VMCS_IDT_VEC_SWEXCEPTION
:
635 env
->exception_injected
= idtvec_info
& VMCS_IDT_VEC_VECNUM
;
637 case VMCS_IDT_VEC_PRIV_SWEXCEPTION
:
641 if ((idtvec_info
& VMCS_IDT_VEC_TYPE
) == VMCS_IDT_VEC_SWEXCEPTION
||
642 (idtvec_info
& VMCS_IDT_VEC_TYPE
) == VMCS_IDT_VEC_SWINTR
) {
643 env
->ins_len
= ins_len
;
645 if (idtvec_info
& VMCS_INTR_DEL_ERRCODE
) {
646 env
->has_error_code
= true;
647 env
->error_code
= rvmcs(cpu
->hvf_fd
, VMCS_IDT_VECTORING_ERROR
);
650 if ((rvmcs(cpu
->hvf_fd
, VMCS_GUEST_INTERRUPTIBILITY
) &
651 VMCS_INTERRUPTIBILITY_NMI_BLOCKING
)) {
652 env
->hflags2
|= HF2_NMI_MASK
;
654 env
->hflags2
&= ~HF2_NMI_MASK
;
656 if (rvmcs(cpu
->hvf_fd
, VMCS_GUEST_INTERRUPTIBILITY
) &
657 (VMCS_INTERRUPTIBILITY_STI_BLOCKING
|
658 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING
)) {
659 env
->hflags
|= HF_INHIBIT_IRQ_MASK
;
661 env
->hflags
&= ~HF_INHIBIT_IRQ_MASK
;
665 int hvf_vcpu_exec(CPUState
*cpu
)
667 X86CPU
*x86_cpu
= X86_CPU(cpu
);
668 CPUX86State
*env
= &x86_cpu
->env
;
674 if (hvf_process_events(cpu
)) {
679 if (cpu
->vcpu_dirty
) {
680 hvf_put_registers(cpu
);
681 cpu
->vcpu_dirty
= false;
684 if (hvf_inject_interrupts(cpu
)) {
685 return EXCP_INTERRUPT
;
689 qemu_mutex_unlock_iothread();
690 if (!cpu_is_bsp(X86_CPU(cpu
)) && cpu
->halted
) {
691 qemu_mutex_lock_iothread();
695 hv_return_t r
= hv_vcpu_run(cpu
->hvf_fd
);
699 uint64_t exit_reason
= rvmcs(cpu
->hvf_fd
, VMCS_EXIT_REASON
);
700 uint64_t exit_qual
= rvmcs(cpu
->hvf_fd
, VMCS_EXIT_QUALIFICATION
);
701 uint32_t ins_len
= (uint32_t)rvmcs(cpu
->hvf_fd
,
702 VMCS_EXIT_INSTRUCTION_LENGTH
);
704 uint64_t idtvec_info
= rvmcs(cpu
->hvf_fd
, VMCS_IDT_VECTORING_INFO
);
706 hvf_store_events(cpu
, ins_len
, idtvec_info
);
707 rip
= rreg(cpu
->hvf_fd
, HV_X86_RIP
);
708 RFLAGS(env
) = rreg(cpu
->hvf_fd
, HV_X86_RFLAGS
);
709 env
->eflags
= RFLAGS(env
);
711 qemu_mutex_lock_iothread();
713 update_apic_tpr(cpu
);
717 switch (exit_reason
) {
718 case EXIT_REASON_HLT
: {
719 macvm_set_rip(cpu
, rip
+ ins_len
);
720 if (!((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
721 (EFLAGS(env
) & IF_MASK
))
722 && !(cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) &&
723 !(idtvec_info
& VMCS_IDT_VEC_VALID
)) {
727 ret
= EXCP_INTERRUPT
;
730 case EXIT_REASON_MWAIT
: {
731 ret
= EXCP_INTERRUPT
;
734 /* Need to check if MMIO or unmmaped fault */
735 case EXIT_REASON_EPT_FAULT
:
738 uint64_t gpa
= rvmcs(cpu
->hvf_fd
, VMCS_GUEST_PHYSICAL_ADDRESS
);
740 if (((idtvec_info
& VMCS_IDT_VEC_VALID
) == 0) &&
741 ((exit_qual
& EXIT_QUAL_NMIUDTI
) != 0)) {
742 vmx_set_nmi_blocking(cpu
);
745 slot
= hvf_find_overlap_slot(gpa
, gpa
);
747 if (ept_emulation_fault(slot
, gpa
, exit_qual
)) {
748 struct x86_decode decode
;
751 env
->hvf_emul
->fetch_rip
= rip
;
753 decode_instruction(env
, &decode
);
754 exec_instruction(env
, &decode
);
760 case EXIT_REASON_INOUT
:
762 uint32_t in
= (exit_qual
& 8) != 0;
763 uint32_t size
= (exit_qual
& 7) + 1;
764 uint32_t string
= (exit_qual
& 16) != 0;
765 uint32_t port
= exit_qual
>> 16;
766 /*uint32_t rep = (exit_qual & 0x20) != 0;*/
771 hvf_handle_io(env
, port
, &val
, 0, size
, 1);
774 } else if (size
== 2) {
776 } else if (size
== 4) {
777 RAX(env
) = (uint32_t)val
;
779 RAX(env
) = (uint64_t)val
;
784 } else if (!string
&& !in
) {
785 RAX(env
) = rreg(cpu
->hvf_fd
, HV_X86_RAX
);
786 hvf_handle_io(env
, port
, &RAX(env
), 1, size
, 1);
787 macvm_set_rip(cpu
, rip
+ ins_len
);
790 struct x86_decode decode
;
793 env
->hvf_emul
->fetch_rip
= rip
;
795 decode_instruction(env
, &decode
);
796 assert(ins_len
== decode
.len
);
797 exec_instruction(env
, &decode
);
802 case EXIT_REASON_CPUID
: {
803 uint32_t rax
= (uint32_t)rreg(cpu
->hvf_fd
, HV_X86_RAX
);
804 uint32_t rbx
= (uint32_t)rreg(cpu
->hvf_fd
, HV_X86_RBX
);
805 uint32_t rcx
= (uint32_t)rreg(cpu
->hvf_fd
, HV_X86_RCX
);
806 uint32_t rdx
= (uint32_t)rreg(cpu
->hvf_fd
, HV_X86_RDX
);
808 cpu_x86_cpuid(env
, rax
, rcx
, &rax
, &rbx
, &rcx
, &rdx
);
810 wreg(cpu
->hvf_fd
, HV_X86_RAX
, rax
);
811 wreg(cpu
->hvf_fd
, HV_X86_RBX
, rbx
);
812 wreg(cpu
->hvf_fd
, HV_X86_RCX
, rcx
);
813 wreg(cpu
->hvf_fd
, HV_X86_RDX
, rdx
);
815 macvm_set_rip(cpu
, rip
+ ins_len
);
818 case EXIT_REASON_XSETBV
: {
819 X86CPU
*x86_cpu
= X86_CPU(cpu
);
820 CPUX86State
*env
= &x86_cpu
->env
;
821 uint32_t eax
= (uint32_t)rreg(cpu
->hvf_fd
, HV_X86_RAX
);
822 uint32_t ecx
= (uint32_t)rreg(cpu
->hvf_fd
, HV_X86_RCX
);
823 uint32_t edx
= (uint32_t)rreg(cpu
->hvf_fd
, HV_X86_RDX
);
826 macvm_set_rip(cpu
, rip
+ ins_len
);
829 env
->xcr0
= ((uint64_t)edx
<< 32) | eax
;
830 wreg(cpu
->hvf_fd
, HV_X86_XCR0
, env
->xcr0
| 1);
831 macvm_set_rip(cpu
, rip
+ ins_len
);
834 case EXIT_REASON_INTR_WINDOW
:
835 vmx_clear_int_window_exiting(cpu
);
836 ret
= EXCP_INTERRUPT
;
838 case EXIT_REASON_NMI_WINDOW
:
839 vmx_clear_nmi_window_exiting(cpu
);
840 ret
= EXCP_INTERRUPT
;
842 case EXIT_REASON_EXT_INTR
:
843 /* force exit and allow io handling */
844 ret
= EXCP_INTERRUPT
;
846 case EXIT_REASON_RDMSR
:
847 case EXIT_REASON_WRMSR
:
850 if (exit_reason
== EXIT_REASON_RDMSR
) {
855 RIP(env
) += rvmcs(cpu
->hvf_fd
, VMCS_EXIT_INSTRUCTION_LENGTH
);
859 case EXIT_REASON_CR_ACCESS
: {
865 reg
= (exit_qual
>> 8) & 15;
869 macvm_set_cr0(cpu
->hvf_fd
, RRX(env
, reg
));
873 macvm_set_cr4(cpu
->hvf_fd
, RRX(env
, reg
));
877 X86CPU
*x86_cpu
= X86_CPU(cpu
);
878 if (exit_qual
& 0x10) {
879 RRX(env
, reg
) = cpu_get_apic_tpr(x86_cpu
->apic_state
);
881 int tpr
= RRX(env
, reg
);
882 cpu_set_apic_tpr(x86_cpu
->apic_state
, tpr
);
883 ret
= EXCP_INTERRUPT
;
888 error_report("Unrecognized CR %d\n", cr
);
895 case EXIT_REASON_APIC_ACCESS
: { /* TODO */
896 struct x86_decode decode
;
899 env
->hvf_emul
->fetch_rip
= rip
;
901 decode_instruction(env
, &decode
);
902 exec_instruction(env
, &decode
);
906 case EXIT_REASON_TPR
: {
910 case EXIT_REASON_TASK_SWITCH
: {
911 uint64_t vinfo
= rvmcs(cpu
->hvf_fd
, VMCS_IDT_VECTORING_INFO
);
912 x68_segment_selector sel
= {.sel
= exit_qual
& 0xffff};
913 vmx_handle_task_switch(cpu
, sel
, (exit_qual
>> 30) & 0x3,
914 vinfo
& VMCS_INTR_VALID
, vinfo
& VECTORING_INFO_VECTOR_MASK
, vinfo
918 case EXIT_REASON_TRIPLE_FAULT
: {
919 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
920 ret
= EXCP_INTERRUPT
;
923 case EXIT_REASON_RDPMC
:
924 wreg(cpu
->hvf_fd
, HV_X86_RAX
, 0);
925 wreg(cpu
->hvf_fd
, HV_X86_RDX
, 0);
926 macvm_set_rip(cpu
, rip
+ ins_len
);
928 case VMX_REASON_VMCALL
:
929 env
->exception_injected
= EXCP0D_GPF
;
930 env
->has_error_code
= true;
934 error_report("%llx: unhandled exit %llx\n", rip
, exit_reason
);
941 static bool hvf_allowed
;
943 static int hvf_accel_init(MachineState
*ms
)
950 ret
= hv_vm_create(HV_VM_DEFAULT
);
953 s
= g_new0(HVFState
, 1);
956 for (x
= 0; x
< s
->num_slots
; ++x
) {
957 s
->slots
[x
].size
= 0;
958 s
->slots
[x
].slot_id
= x
;
962 cpu_interrupt_handler
= hvf_handle_interrupt
;
963 memory_listener_register(&hvf_memory_listener
, &address_space_memory
);
967 static void hvf_accel_class_init(ObjectClass
*oc
, void *data
)
969 AccelClass
*ac
= ACCEL_CLASS(oc
);
971 ac
->init_machine
= hvf_accel_init
;
972 ac
->allowed
= &hvf_allowed
;
975 static const TypeInfo hvf_accel_type
= {
976 .name
= TYPE_HVF_ACCEL
,
977 .parent
= TYPE_ACCEL
,
978 .class_init
= hvf_accel_class_init
,
981 static void hvf_type_init(void)
983 type_register_static(&hvf_accel_type
);
986 type_init(hvf_type_init
);