hvf: Implement Apple Silicon Support
v7 cover hvf: Implement Apple Silicon Support
Now that Apple Silicon is widely available, people are obviously excited to try and run virtualized workloads on them, such as Linux and Windows.
This patch set implements a fully functional version to get the ball going on that. With this applied, I can successfully run both Linux and Windows as guests. I am not aware of any limitations specific to Hypervisor.framework apart from:
- Live migration / savevm
- gdbstub debugging (SP register)
Enjoy!
Alex
v1 -> v2:
- New patch: hvf: Actually set SIG_IPI mask
- New patch: hvf: Introduce hvf vcpu struct
- New patch: hvf: arm: Mark CPU as dirty on reset
- Removed patch: hw/arm/virt: Disable highmem when on hypervisor.framework
- Removed patch: arm: Synchronize CPU on PSCI on
- Fix build on 32bit arm
- Merge vcpu kick function patch into ARM enablement
- Implement WFI handling (allows vCPUs to sleep)
- Synchronize system registers (fixes OVMF crashes and reboot)
- Don't always call cpu_synchronize_state()
- Use more fine grained iothread locking
- Populate aa64mmfr0 from hardware
- Make safe to ctrl-C entitlement application
v2 -> v3:
- Removed patch: hvf: Actually set SIG_IPI mask
- New patch: hvf: arm: Add support for GICv3
- New patch: hvf: arm: Implement -cpu host
- Advance PC on SMC
- Use cp list interface for sysreg syncs
- Do not set current_cpu
- Fix sysreg isread mask
- Move sysreg handling to functions
- Remove WFI logic again
- Revert to global iothread locking
v3 -> v4:
- Removed patch: hvf: arm: Mark CPU as dirty on reset
- New patch: hvf: Simplify post reset/init/loadvm hooks
- Remove i386-softmmu target (meson.build for hvf target)
- Combine both if statements (PSCI)
- Use hv.h instead of Hypervisor.h for 10.15 compat
- Remove manual inclusion of Hypervisor.h in common .c files
- No longer include Hypervisor.h in arm hvf .c files
- Remove unused exe_full variable
- Reuse exe_name variable
v4 -> v5:
- Use g_free() on destroy
v5 -> v6:
- Switch SYSREG() macro order to the same as asm intrinsics
v6 -> v7:
- Already merged: hvf: Add hypervisor entitlement to output binaries
- Already merged: hvf: x86: Remove unused definitions
- Patch split: hvf: Move common code out
-> hvf: Move assert_hvf_ok() into common directory
-> hvf: Move vcpu thread functions into common directory
-> hvf: Move cpu functions into common directory
-> hvf: Move hvf internal definitions into common header
-> hvf: Make hvf_set_phys_mem() static
-> hvf: Remove use of hv_uvaddr_t and hv_gpaddr_t
-> hvf: Split out common code on vcpu init and destroy
-> hvf: Use cpu_synchronize_state()
-> hvf: Make synchronize functions static
-> hvf: Remove hvf-accel-ops.h - New patch: hvf: arm: Implement PSCI handling
- New patch: arm: Enable Windows 10 trusted SMCCC boot call
- New patch: hvf: arm: Handle Windows 10 SMC call
- Removed patch: "arm: Set PSCI to 0.2 for HVF" (included above)
- Removed patch: "hvf: arm: Add support for GICv3" (deferred to later)
- Remove osdep.h include from hvf_int.h
- Synchronize SIMD registers as well
- Prepend 0x for hex values
- Convert DPRINTF to trace points
- Use main event loop (fixes gdbstub issues)
- Remove PSCI support, inject UDEF on HVC/SMC
- Change vtimer logic to look at ctl.istatus for vtimer mask sync
- Add kick callback again (fixes remote CPU notification)
- Move function define to own header
- Do not propagate SVE features for HVF
- Remove stray whitespace change
- Verify that EL0 and EL1 do not allow AArch32 mode
- Only probe host CPU features once
- Move WFI into function
- Improve comment wording
- Simplify HVF matching logic in meson build file
Alexander Graf (18):
hvf: Move assert_hvf_ok() into common directory
hvf: Move vcpu thread functions into common directory
hvf: Move cpu functions into common directory
hvf: Move hvf internal definitions into common header
hvf: Make hvf_set_phys_mem() static
hvf: Remove use of hv_uvaddr_t and hv_gpaddr_t
hvf: Split out common code on vcpu init and destroy
hvf: Use cpu_synchronize_state()
hvf: Make synchronize functions static
hvf: Remove hvf-accel-ops.h
hvf: Introduce hvf vcpu struct
hvf: Simplify post reset/init/loadvm hooks
hvf: Add Apple Silicon support
hvf: arm: Implement -cpu host
hvf: arm: Implement PSCI handling
arm: Add Hypervisor.framework build target
arm: Enable Windows 10 trusted SMCCC boot call
hvf: arm: Handle Windows 10 SMC call
Peter Collingbourne (1):
arm/hvf: Add a WFI handler
MAINTAINERS | 13 + accel/hvf/hvf-accel-ops.c | 484 ++++++++++++++++ accel/hvf/hvf-all.c | 47 ++ accel/hvf/meson.build | 7 + accel/meson.build | 1 + include/hw/core/cpu.h | 3 +- include/sysemu/hvf_int.h | 66 +++ meson.build | 8 + target/arm/cpu.c | 13 +- target/arm/cpu.h | 2 + target/arm/hvf/hvf.c | 959 ++++++++++++++++++++++++++++++++ target/arm/hvf/meson.build | 3 + target/arm/hvf/trace-events | 11 + target/arm/hvf_arm.h | 19 + target/arm/kvm-consts.h | 2 + target/arm/kvm_arm.h | 2 - target/arm/meson.build | 2 + target/arm/psci.c | 2 + target/i386/hvf/hvf-accel-ops.c | 146 ----- target/i386/hvf/hvf-accel-ops.h | 23 - target/i386/hvf/hvf-i386.h | 33 +- target/i386/hvf/hvf.c | 464 ++------------- target/i386/hvf/meson.build | 1 - target/i386/hvf/vmx.h | 24 +- target/i386/hvf/x86.c | 28 +- target/i386/hvf/x86_descr.c | 26 +- target/i386/hvf/x86_emu.c | 62 +-- target/i386/hvf/x86_mmu.c | 4 +- target/i386/hvf/x86_task.c | 12 +- target/i386/hvf/x86hvf.c | 222 ++++---- target/i386/hvf/x86hvf.h | 2 - 31 files changed, 1884 insertions(+), 807 deletions(-) create mode 100644 accel/hvf/hvf-accel-ops.c create mode 100644 accel/hvf/hvf-all.c create mode 100644 accel/hvf/meson.build create mode 100644 include/sysemu/hvf_int.h create mode 100644 target/arm/hvf/hvf.c create mode 100644 target/arm/hvf/meson.build create mode 100644 target/arm/hvf/trace-events create mode 100644 target/arm/hvf_arm.h delete mode 100644 target/i386/hvf/hvf-accel-ops.c delete mode 100644 target/i386/hvf/hvf-accel-ops.h
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 1/19 hvf: Move assert_hvf_ok() into common directory
Until now, Hypervisor.framework has only been available on x86_64 systems. With Apple Silicon shipping now, it extends its reach to aarch64. To prepare for support for multiple architectures, let's start moving common code out into its own accel directory.
This patch moves assert_hvf_ok() and introduces generic build infrastructure.
--- MAINTAINERS | 8 +++++++ accel/hvf/hvf-all.c | 47 ++++++++++++++++++++++++++++++++++++++++ accel/hvf/meson.build | 6 +++++ accel/meson.build | 1 + include/sysemu/hvf_int.h | 18 +++++++++++++++ target/i386/hvf/hvf.c | 33 +--------------------------- 6 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 accel/hvf/hvf-all.c create mode 100644 accel/hvf/meson.build create mode 100644 include/sysemu/hvf_int.h
MAINTAINERS +8 -0
@@ -434,7 +434,15 @@ M: Roman Bolshakov <[email protected]> W: https://wiki.qemu.org/Features/HVF S: Maintained F: target/i386/hvf/ + +HVF +M: Cameron Esfahani <[email protected]> +M: Roman Bolshakov <[email protected]> +W: https://wiki.qemu.org/Features/HVF +S: Maintained +F: accel/hvf/ F: include/sysemu/hvf.h +F: include/sysemu/hvf_int.h WHPX CPUs M: Sunil Muthuswamy <[email protected]>
accel/hvf/hvf-all.c new file +47 -0
47 lines not on this page · raw message
accel/hvf/meson.build new file +6 -0
@@ -0,0 +1,6 @@ +hvf_ss = ss.source_set() +hvf_ss.add(files( + 'hvf-all.c', +)) + +specific_ss.add_all(when: 'CONFIG_HVF', if_true: hvf_ss)
accel/meson.build +1 -0
@@ -2,6 +2,7 @@ specific_ss.add(files('accel-common.c')) softmmu_ss.add(files('accel-softmmu.c')) user_ss.add(files('accel-user.c')) +subdir('hvf') subdir('qtest') subdir('kvm') subdir('tcg')
include/sysemu/hvf_int.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* QEMU Hypervisor.framework (HVF) support +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +/* header to be included in HVF-specific code */ + +#ifndef HVF_INT_H +#define HVF_INT_H + +#include <Hypervisor/hv.h> + +void assert_hvf_ok (hv_return_t ret); + +#endif
target/i386/hvf/hvf.c +1 -32
@@ -51,6 +51,7 @@#include "qemu/error-report.h" #include "sysemu/hvf.h" +#include "sysemu/hvf_int.h" #include "sysemu/runstate.h" #include "hvf-i386.h" #include "vmcs.h"
@@ -76,38 +77,6 @@HVFState *hvf_state; -static void assert_hvf_ok (hv_return_t ret) -{ -if (ret ==HV_SUCCESS ) { -return ; - } - -switch (ret) { -case HV_ERROR : -error_report ("Error: HV_ERROR" ); -break ; -case HV_BUSY : -error_report ("Error: HV_BUSY" ); -break ; -case HV_BAD_ARGUMENT : -error_report ("Error: HV_BAD_ARGUMENT" ); -break ; -case HV_NO_RESOURCES : -error_report ("Error: HV_NO_RESOURCES" ); -break ; -case HV_NO_DEVICE : -error_report ("Error: HV_NO_DEVICE" ); -break ; -case HV_UNSUPPORTED : -error_report ("Error: HV_UNSUPPORTED" ); -break ; -default : -error_report ("Unknown Error" ); - } - -abort (); -} -/* Memory slots */ hvf_slot *hvf_find_overlap_slot (uint64_t start,uint64_t size) {
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 2/19 hvf: Move vcpu thread functions into common directory
In reply to Alexander GrafUntil now, Hypervisor.framework has only been available on x86_64 systems. With Apple Silicon shipping now, it extends its reach to aarch64. To prepare for support for multiple architectures, let's start moving common code out into its own accel directory.
This patch moves the vCPU thread loop over.
---
{target/i386 => accel}/hvf/hvf-accel-ops.c | 0
{target/i386 => accel}/hvf/hvf-accel-ops.h | 0
accel/hvf/meson.build | 1 +
target/i386/hvf/meson.build | 1 -
target/i386/hvf/x86hvf.c | 2 +-
5 files changed, 2 insertions(+), 2 deletions(-)
rename {target/i386 => accel}/hvf/hvf-accel-ops.c (100%)
rename {target/i386 => accel}/hvf/hvf-accel-ops.h (100%)accel/hvf/hvf-accel-ops.c renamed from target/i386/hvf/hvf-accel-ops.c +0 -0
accel/hvf/hvf-accel-ops.h renamed from target/i386/hvf/hvf-accel-ops.h +0 -0
accel/hvf/meson.build +1 -0
@@ -1,6 +1,7 @@ hvf_ss = ss.source_set() hvf_ss.add(files( 'hvf-all.c', + 'hvf-accel-ops.c', )) specific_ss.add_all(when: 'CONFIG_HVF', if_true: hvf_ss)
target/i386/hvf/meson.build +0 -1
@@ -1,6 +1,5 @@ i386_softmmu_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files( 'hvf.c', - 'hvf-accel-ops.c', 'x86.c', 'x86_cpuid.c', 'x86_decode.c',
target/i386/hvf/x86hvf.c +1 -1
@@ -32,7 +32,7 @@#include <Hypervisor/hv.h> #include <Hypervisor/hv_vmx.h> -#include "hvf-accel-ops.h" +#include "accel/hvf/hvf-accel-ops.h" void hvf_set_segment (struct CPUState *cpu,struct vmx_segment *vmx_seg,SegmentCache *qseg,bool is_tr)
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 3/19 hvf: Move cpu functions into common directory
In reply to Alexander GrafUntil now, Hypervisor.framework has only been available on x86_64 systems. With Apple Silicon shipping now, it extends its reach to aarch64. To prepare for support for multiple architectures, let's start moving common code out into its own accel directory.
This patch moves CPU and memory operations over. While at it, make sure the code is consumable on non-i386 systems.
--- accel/hvf/hvf-accel-ops.c | 308 ++++++++++++++++++++++++++++++++++++- include/sysemu/hvf_int.h | 4 + target/i386/hvf/hvf-i386.h | 2 - target/i386/hvf/hvf.c | 302 ------------------------------------ target/i386/hvf/x86hvf.h | 2 - 5 files changed, 311 insertions(+), 307 deletions(-)
accel/hvf/hvf-accel-ops.c +307 -1
@@ -50,13 +50,319 @@#include "qemu/osdep.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" +#include "exec/address-spaces.h" +#include "exec/exec-all.h" +#include "sysemu/cpus.h" #include "sysemu/hvf.h" +#include "sysemu/hvf_int.h" #include "sysemu/runstate.h" -#include "target/i386/cpu.h" #include "qemu/guest-random.h" #include "hvf-accel-ops.h" +HVFState *hvf_state; + +/* Memory slots */ + +hvf_slot *hvf_find_overlap_slot (uint64_t start,uint64_t size) +{ +hvf_slot *slot; +int x; +for (x =0 ; x < hvf_state->num_slots; ++x) { + slot = &hvf_state->slots[x]; +if (slot->size && start < (slot->start + slot->size) && + (start + size) > slot->start) { +return slot; + } + } +return NULL ; +} + +struct mac_slot { +int present; +uint64_t size; +uint64_t gpa_start; +uint64_t gva; +}; + +struct mac_slot mac_slots[32 ]; + +static int do_hvf_set_memory (hvf_slot *slot,hv_memory_flags_t flags) +{ +struct mac_slot *macslot; +hv_return_t ret; + + macslot = &mac_slots[slot->slot_id]; + +if (macslot->present) { +if (macslot->size != slot->size) { + macslot->present =0 ; + ret =hv_vm_unmap (macslot->gpa_start, macslot->size); +assert_hvf_ok (ret); + } + } + +if (!slot->size) { +return 0 ; + } + + macslot->present =1 ; + macslot->gpa_start = slot->start; + macslot->size = slot->size; + ret =hv_vm_map ((hv_uvaddr_t )slot->mem, slot->start, slot->size, flags); +assert_hvf_ok (ret); +return 0 ; +} + +void hvf_set_phys_mem (MemoryRegionSection *section,bool add) +{ +hvf_slot *mem; +MemoryRegion *area = section->mr; +bool writeable = !area->readonly && !area->rom_device; +hv_memory_flags_t flags; + +if (!memory_region_is_ram (area)) { +if (writeable) { +return ; + }else if (!memory_region_is_romd (area)) { +/* +* If the memory device is not in romd_mode, then we actually want +* to remove the hvf memory slot so all accesses will trap. +*/ + add = false; + } + } + + mem =hvf_find_overlap_slot ( + section->offset_within_address_space, +int128_get64 (section->size)); + +if (mem && add) { +if (mem->size ==int128_get64 (section->size) && + mem->start == section->offset_within_address_space && + mem->mem == (memory_region_get_ram_ptr (area) + + section->offset_within_region)) { +return ;/* Same region was attempted to register, go away. */ + } + } + +/* Region needs to be reset. set the size to 0 and remap it. */ +if (mem) { + mem->size =0 ; +if (do_hvf_set_memory (mem,0 )) { +error_report ("Failed to reset overlapping slot" ); +abort (); + } + } + +if (!add) { +return ; + } + +if (area->readonly || + (!memory_region_is_ram (area) &&memory_region_is_romd (area))) { + flags =HV_MEMORY_READ |HV_MEMORY_EXEC ; + }else { + flags =HV_MEMORY_READ |HV_MEMORY_WRITE |HV_MEMORY_EXEC ; + } + +/* Now make a new slot. */ +int x; + +for (x =0 ; x < hvf_state->num_slots; ++x) { + mem = &hvf_state->slots[x]; +if (!mem->size) { +break ; + } + } + +if (x == hvf_state->num_slots) { +error_report ("No free slots" ); +abort (); + } + + mem->size =int128_get64 (section->size); + mem->mem =memory_region_get_ram_ptr (area) + section->offset_within_region; + mem->start = section->offset_within_address_space; + mem->region = area; + +if (do_hvf_set_memory (mem, flags)) { +error_report ("Error registering new memory slot" ); +abort (); + } +} + +static void do_hvf_cpu_synchronize_state (CPUState *cpu,run_on_cpu_data arg) +{ +if (!cpu->vcpu_dirty) { +hvf_get_registers (cpu); + cpu->vcpu_dirty = true; + } +} + +void hvf_cpu_synchronize_state (CPUState *cpu) +{ +if (!cpu->vcpu_dirty) { +run_on_cpu (cpu, do_hvf_cpu_synchronize_state,RUN_ON_CPU_NULL ); + } +} + +static void do_hvf_cpu_synchronize_post_reset (CPUState *cpu, +run_on_cpu_data arg) +{ +hvf_put_registers (cpu); + cpu->vcpu_dirty = false; +} + +void hvf_cpu_synchronize_post_reset (CPUState *cpu) +{ +run_on_cpu (cpu, do_hvf_cpu_synchronize_post_reset,RUN_ON_CPU_NULL ); +} + +static void do_hvf_cpu_synchronize_post_init (CPUState *cpu, +run_on_cpu_data arg) +{ +hvf_put_registers (cpu); + cpu->vcpu_dirty = false; +} + +void hvf_cpu_synchronize_post_init (CPUState *cpu) +{ +run_on_cpu (cpu, do_hvf_cpu_synchronize_post_init,RUN_ON_CPU_NULL ); +} + +static void do_hvf_cpu_synchronize_pre_loadvm (CPUState *cpu, +run_on_cpu_data arg) +{ + cpu->vcpu_dirty = true; +} + +void hvf_cpu_synchronize_pre_loadvm (CPUState *cpu) +{ +run_on_cpu (cpu, do_hvf_cpu_synchronize_pre_loadvm,RUN_ON_CPU_NULL ); +} + +static void hvf_set_dirty_tracking (MemoryRegionSection *section,bool on) +{ +hvf_slot *slot; + + slot =hvf_find_overlap_slot ( + section->offset_within_address_space, +int128_get64 (section->size)); + +/* protect region against writes; begin tracking it */ +if (on) { + slot->flags |=HVF_SLOT_LOG ; +hv_vm_protect ((hv_gpaddr_t )slot->start, (size_t )slot->size, +HV_MEMORY_READ ); +/* stop tracking region*/ + }else { + slot->flags &= ~HVF_SLOT_LOG ; +hv_vm_protect ((hv_gpaddr_t )slot->start, (size_t )slot->size, +HV_MEMORY_READ |HV_MEMORY_WRITE ); + } +} + +static void hvf_log_start (MemoryListener *listener, +MemoryRegionSection *section,int old,int new) +{ +if (old !=0 ) { +return ; + } + +hvf_set_dirty_tracking (section,1 ); +} + +static void hvf_log_stop (MemoryListener *listener, +MemoryRegionSection *section,int old,int new) +{ +if (new !=0 ) { +return ; + } + +hvf_set_dirty_tracking (section,0 ); +} + +static void hvf_log_sync (MemoryListener *listener, +MemoryRegionSection *section) +{ +/* +* sync of dirty pages is handled elsewhere; just make sure we keep +* tracking the region. +*/ +hvf_set_dirty_tracking (section,1 ); +} + +static void hvf_region_add (MemoryListener *listener, +MemoryRegionSection *section) +{ +hvf_set_phys_mem (section, true); +} + +static void hvf_region_del (MemoryListener *listener, +MemoryRegionSection *section) +{ +hvf_set_phys_mem (section, false); +} + +static MemoryListener hvf_memory_listener = { + .priority =10 , + .region_add = hvf_region_add, + .region_del = hvf_region_del, + .log_start = hvf_log_start, + .log_stop = hvf_log_stop, + .log_sync = hvf_log_sync, +}; + +static void dummy_signal (int sig) +{ +} + +bool hvf_allowed; + +static int hvf_accel_init (MachineState *ms) +{ +int x; +hv_return_t ret; +HVFState *s; + + ret =hv_vm_create (HV_VM_DEFAULT ); +assert_hvf_ok (ret); + + s =g_new0 (HVFState,1 ); + + s->num_slots =32 ; +for (x =0 ; x < s->num_slots; ++x) { + s->slots[x].size =0 ; + s->slots[x].slot_id = x; + } + + hvf_state = s; +memory_listener_register (&hvf_memory_listener, &address_space_memory); +return 0 ; +} + +static void hvf_accel_class_init (ObjectClass *oc,void *data) +{ +AccelClass *ac =ACCEL_CLASS (oc); + ac->name ="HVF" ; + ac->init_machine = hvf_accel_init; + ac->allowed = &hvf_allowed; +} + +static const TypeInfo hvf_accel_type = { + .name =TYPE_HVF_ACCEL , + .parent =TYPE_ACCEL , + .class_init = hvf_accel_class_init, +}; + +static void hvf_type_init (void ) +{ +type_register_static (&hvf_accel_type); +} + +type_init (hvf_type_init); +/* * The HVF-specific vCPU thread function. This one should only run when the host * CPU supports the VMX "unrestricted guest" feature.
include/sysemu/hvf_int.h +4 -0
@@ -13,6 +13,10 @@#include <Hypervisor/hv.h> +void hvf_set_phys_mem (MemoryRegionSection *,bool );void assert_hvf_ok (hv_return_t ret); +hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t ); +int hvf_put_registers (CPUState *); +int hvf_get_registers (CPUState *);#endif
target/i386/hvf/hvf-i386.h +0 -2
@@ -51,9 +51,7 @@ struct HVFState { };extern HVFState *hvf_state; -void hvf_set_phys_mem (MemoryRegionSection *,bool );void hvf_handle_io (CPUArchState *,uint16_t ,void *,int ,int ,int ); -hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t );#ifdef NEED_CPU_H /* Functions exported to host specific mode */
target/i386/hvf/hvf.c +0 -302
329 lines not on this page · raw message
target/i386/hvf/x86hvf.h +0 -2
@@ -21,8 +21,6 @@#include "x86_descr.h" int hvf_process_events (CPUState *); -int hvf_put_registers (CPUState *); -int hvf_get_registers (CPUState *);bool hvf_inject_interrupts (CPUState *);void hvf_set_segment (struct CPUState *cpu,struct vmx_segment *vmx_seg,SegmentCache *qseg,bool is_tr);
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 4/19 hvf: Move hvf internal definitions into common header
In reply to Alexander GrafUntil now, Hypervisor.framework has only been available on x86_64 systems. With Apple Silicon shipping now, it extends its reach to aarch64. To prepare for support for multiple architectures, let's start moving common code out into its own accel directory.
This patch moves a few internal struct and constant defines over.
--- include/sysemu/hvf_int.h | 30 ++++++++++++++++++++++++++++++ target/i386/hvf/hvf-i386.h | 31 +------------------------------ 2 files changed, 31 insertions(+), 30 deletions(-)
include/sysemu/hvf_int.h +30 -0
@@ -13,6 +13,36 @@#include <Hypervisor/hv.h> +/* hvf_slot flags */ +#define HVF_SLOT_LOG (1 << 0) + +typedef struct hvf_slot { +uint64_t start; +uint64_t size; +uint8_t *mem; +int slot_id; +uint32_t flags; +MemoryRegion *region; +}hvf_slot ; + +typedef struct hvf_vcpu_caps { +uint64_t vmx_cap_pinbased; +uint64_t vmx_cap_procbased; +uint64_t vmx_cap_procbased2; +uint64_t vmx_cap_entry; +uint64_t vmx_cap_exit; +uint64_t vmx_cap_preemption_timer; +}hvf_vcpu_caps ; + +struct HVFState { +AccelState parent; +hvf_slot slots[32 ]; +int num_slots; + +hvf_vcpu_caps *hvf_caps; +}; +extern HVFState *hvf_state; +void hvf_set_phys_mem (MemoryRegionSection *,bool );void assert_hvf_ok (hv_return_t ret);hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t );
target/i386/hvf/hvf-i386.h +1 -30
@@ -18,39 +18,10 @@#include "qemu/accel.h" #include "sysemu/hvf.h" +#include "sysemu/hvf_int.h" #include "cpu.h" #include "x86.h" -/* hvf_slot flags */ -#define HVF_SLOT_LOG (1 << 0) - -typedef struct hvf_slot { -uint64_t start; -uint64_t size; -uint8_t *mem; -int slot_id; -uint32_t flags; -MemoryRegion *region; -}hvf_slot ; - -typedef struct hvf_vcpu_caps { -uint64_t vmx_cap_pinbased; -uint64_t vmx_cap_procbased; -uint64_t vmx_cap_procbased2; -uint64_t vmx_cap_entry; -uint64_t vmx_cap_exit; -uint64_t vmx_cap_preemption_timer; -}hvf_vcpu_caps ; - -struct HVFState { -AccelState parent; -hvf_slot slots[32 ]; -int num_slots; - -hvf_vcpu_caps *hvf_caps; -}; -extern HVFState *hvf_state; -void hvf_handle_io (CPUArchState *,uint16_t ,void *,int ,int ,int );#ifdef NEED_CPU_H
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 5/19 hvf: Make hvf_set_phys_mem() static
In reply to Alexander GrafThe hvf_set_phys_mem() function is only called within the same file. Make it static.
--- accel/hvf/hvf-accel-ops.c | 2 +- include/sysemu/hvf_int.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-)
accel/hvf/hvf-accel-ops.c +1 -1
@@ -114,7 +114,7 @@ static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags)return 0 ; } -void hvf_set_phys_mem (MemoryRegionSection *section,bool add) +static void hvf_set_phys_mem (MemoryRegionSection *section,bool add) {hvf_slot *mem;MemoryRegion *area = section->mr;
include/sysemu/hvf_int.h +0 -1
@@ -43,7 +43,6 @@ struct HVFState { };extern HVFState *hvf_state; -void hvf_set_phys_mem (MemoryRegionSection *,bool );void assert_hvf_ok (hv_return_t ret);hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t );int hvf_put_registers (CPUState *);
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 6/19 hvf: Remove use of hv_uvaddr_t and hv_gpaddr_t
In reply to Alexander GrafThe ARM version of Hypervisor.framework no longer defines these two types, so let's just revert to standard ones.
--- accel/hvf/hvf-accel-ops.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
accel/hvf/hvf-accel-ops.c +3 -3
@@ -109,7 +109,7 @@ static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags) macslot->present =1 ; macslot->gpa_start = slot->start; macslot->size = slot->size; - ret =hv_vm_map ((hv_uvaddr_t )slot->mem, slot->start, slot->size, flags); + ret =hv_vm_map (slot->mem, slot->start, slot->size, flags);assert_hvf_ok (ret);return 0 ; }
@@ -253,12 +253,12 @@ static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on)/* protect region against writes; begin tracking it */ if (on) { slot->flags |=HVF_SLOT_LOG ; -hv_vm_protect ((hv_gpaddr_t )slot->start, (size_t )slot->size, +hv_vm_protect ((uintptr_t )slot->start, (size_t )slot->size,HV_MEMORY_READ );/* stop tracking region*/ }else { slot->flags &= ~HVF_SLOT_LOG ; -hv_vm_protect ((hv_gpaddr_t )slot->start, (size_t )slot->size, +hv_vm_protect ((uintptr_t )slot->start, (size_t )slot->size,HV_MEMORY_READ |HV_MEMORY_WRITE ); } }
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 7/19 hvf: Split out common code on vcpu init and destroy
In reply to Alexander GrafUntil now, Hypervisor.framework has only been available on x86_64 systems. With Apple Silicon shipping now, it extends its reach to aarch64. To prepare for support for multiple architectures, let's start moving common code out into its own accel directory.
This patch splits the vcpu init and destroy functions into a generic and an architecture specific portion. This also allows us to move the generic functions into the generic hvf code, removing exported functions.
--- accel/hvf/hvf-accel-ops.c | 30 ++++++++++++++++++++++++++++++ accel/hvf/hvf-accel-ops.h | 2 -- include/sysemu/hvf_int.h | 2 ++ target/i386/hvf/hvf.c | 23 ++--------------------- 4 files changed, 34 insertions(+), 23 deletions(-)
accel/hvf/hvf-accel-ops.c +30 -0
@@ -363,6 +363,36 @@ static void hvf_type_init(void)type_init (hvf_type_init); +static void hvf_vcpu_destroy (CPUState *cpu) +{ +hv_return_t ret =hv_vcpu_destroy (cpu->hvf_fd); +assert_hvf_ok (ret); + +hvf_arch_vcpu_destroy (cpu); +} + +static int hvf_init_vcpu (CPUState *cpu) +{ +int r; + +/* init cpu signals */ +sigset_t set; +struct sigaction sigact; + +memset (&sigact,0 ,sizeof (sigact)); + sigact.sa_handler = dummy_signal; +sigaction (SIG_IPI , &sigact,NULL ); + +pthread_sigmask (SIG_BLOCK ,NULL , &set); +sigdelset (&set,SIG_IPI ); + + r =hv_vcpu_create ((hv_vcpuid_t *)&cpu->hvf_fd,HV_VCPU_DEFAULT ); + cpu->vcpu_dirty =1 ; +assert_hvf_ok (r); + +return hvf_arch_init_vcpu (cpu); +} +/* * The HVF-specific vCPU thread function. This one should only run when the host * CPU supports the VMX "unrestricted guest" feature.
accel/hvf/hvf-accel-ops.h +0 -2
@@ -12,12 +12,10 @@#include "sysemu/cpus.h" -int hvf_init_vcpu (CPUState *);int hvf_vcpu_exec (CPUState *);void hvf_cpu_synchronize_state (CPUState *);void hvf_cpu_synchronize_post_reset (CPUState *);void hvf_cpu_synchronize_post_init (CPUState *);void hvf_cpu_synchronize_pre_loadvm (CPUState *); -void hvf_vcpu_destroy (CPUState *);#endif /* HVF_CPUS_H */
include/sysemu/hvf_int.h +2 -0
@@ -44,6 +44,8 @@ struct HVFState {extern HVFState *hvf_state;void assert_hvf_ok (hv_return_t ret); +int hvf_arch_init_vcpu (CPUState *cpu); +void hvf_arch_vcpu_destroy (CPUState *cpu);hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t );int hvf_put_registers (CPUState *);int hvf_get_registers (CPUState *);
target/i386/hvf/hvf.c +2 -21
@@ -158,14 +158,12 @@ static bool ept_emulation_fault(hvf_slot *slot, uint64_t gpa, uint64_t ept_qual)return false; } -void hvf_vcpu_destroy (CPUState *cpu) +void hvf_arch_vcpu_destroy (CPUState *cpu) {X86CPU *x86_cpu =X86_CPU (cpu);CPUX86State *env = &x86_cpu->env; -hv_return_t ret =hv_vcpu_destroy ((hv_vcpuid_t )cpu->hvf_fd);g_free (env->hvf_mmio_buf); -assert_hvf_ok (ret); }static void init_tsc_freq (CPUX86State *env)
@@ -210,23 +208,10 @@ static inline bool apic_bus_freq_is_known(CPUX86State *env)return env->apic_bus_freq !=0 ; } -int hvf_init_vcpu (CPUState *cpu) +int hvf_arch_init_vcpu (CPUState *cpu) { -X86CPU *x86cpu =X86_CPU (cpu);CPUX86State *env = &x86cpu->env; -int r; - -/* init cpu signals */ -sigset_t set; -struct sigaction sigact; - -memset (&sigact,0 ,sizeof (sigact)); - sigact.sa_handler = dummy_signal; -sigaction (SIG_IPI , &sigact,NULL ); - -pthread_sigmask (SIG_BLOCK ,NULL , &set); -sigdelset (&set,SIG_IPI );init_emu ();init_decoder ();
@@ -243,10 +228,6 @@ int hvf_init_vcpu(CPUState *cpu) } } - r =hv_vcpu_create ((hv_vcpuid_t *)&cpu->hvf_fd,HV_VCPU_DEFAULT ); - cpu->vcpu_dirty =1 ; -assert_hvf_ok (r); -if (hv_vmx_read_capability (HV_VMX_CAP_PINBASED , &hvf_state->hvf_caps->vmx_cap_pinbased)) {abort ();
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 8/19 hvf: Use cpu_synchronize_state()
In reply to Alexander GrafThere is no reason to call the hvf specific hvf_cpu_synchronize_state() when we can just use the generic cpu_synchronize_state() instead. This allows us to have less dependency on internal function definitions and allows us to make hvf_cpu_synchronize_state() static.
--- accel/hvf/hvf-accel-ops.c | 2 +- accel/hvf/hvf-accel-ops.h | 1 - target/i386/hvf/x86hvf.c | 9 ++++----- 3 files changed, 5 insertions(+), 7 deletions(-)
accel/hvf/hvf-accel-ops.c +1 -1
@@ -200,7 +200,7 @@ static void do_hvf_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg) } } -void hvf_cpu_synchronize_state (CPUState *cpu) +static void hvf_cpu_synchronize_state (CPUState *cpu) {if (!cpu->vcpu_dirty) {run_on_cpu (cpu, do_hvf_cpu_synchronize_state,RUN_ON_CPU_NULL );
accel/hvf/hvf-accel-ops.h +0 -1
@@ -13,7 +13,6 @@#include "sysemu/cpus.h" int hvf_vcpu_exec (CPUState *); -void hvf_cpu_synchronize_state (CPUState *);void hvf_cpu_synchronize_post_reset (CPUState *);void hvf_cpu_synchronize_post_init (CPUState *);void hvf_cpu_synchronize_pre_loadvm (CPUState *);
target/i386/hvf/x86hvf.c +4 -5
@@ -26,14 +26,13 @@#include "cpu.h" #include "x86_descr.h" #include "x86_decode.h" +#include "sysemu/hw_accel.h" #include "hw/i386/apic_internal.h" #include <Hypervisor/hv.h> #include <Hypervisor/hv_vmx.h> -#include "accel/hvf/hvf-accel-ops.h" -void hvf_set_segment (struct CPUState *cpu,struct vmx_segment *vmx_seg,SegmentCache *qseg,bool is_tr) {
@@ -437,7 +436,7 @@ int hvf_process_events(CPUState *cpu_state) env->eflags =rreg (cpu_state->hvf_fd,HV_X86_RFLAGS );if (cpu_state->interrupt_request &CPU_INTERRUPT_INIT ) { -hvf_cpu_synchronize_state (cpu_state); +cpu_synchronize_state (cpu_state);do_cpu_init (cpu); }
@@ -451,12 +450,12 @@ int hvf_process_events(CPUState *cpu_state) cpu_state->halted =0 ; }if (cpu_state->interrupt_request &CPU_INTERRUPT_SIPI ) { -hvf_cpu_synchronize_state (cpu_state); +cpu_synchronize_state (cpu_state);do_cpu_sipi (cpu); }if (cpu_state->interrupt_request &CPU_INTERRUPT_TPR ) { cpu_state->interrupt_request &= ~CPU_INTERRUPT_TPR ; -hvf_cpu_synchronize_state (cpu_state); +cpu_synchronize_state (cpu_state);apic_handle_tpr_access_report (cpu->apic_state, env->eip, env->tpr_access_type); }
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 9/19 hvf: Make synchronize functions static
In reply to Alexander GrafThe hvf accel synchronize functions are only used as input for local callback functions, so we can make them static.
--- accel/hvf/hvf-accel-ops.c | 6 +++--- accel/hvf/hvf-accel-ops.h | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-)
accel/hvf/hvf-accel-ops.c +3 -3
@@ -214,7 +214,7 @@ static void do_hvf_cpu_synchronize_post_reset(CPUState *cpu, cpu->vcpu_dirty = false; } -void hvf_cpu_synchronize_post_reset (CPUState *cpu) +static void hvf_cpu_synchronize_post_reset (CPUState *cpu) {run_on_cpu (cpu, do_hvf_cpu_synchronize_post_reset,RUN_ON_CPU_NULL ); }
@@ -226,7 +226,7 @@ static void do_hvf_cpu_synchronize_post_init(CPUState *cpu, cpu->vcpu_dirty = false; } -void hvf_cpu_synchronize_post_init (CPUState *cpu) +static void hvf_cpu_synchronize_post_init (CPUState *cpu) {run_on_cpu (cpu, do_hvf_cpu_synchronize_post_init,RUN_ON_CPU_NULL ); }
@@ -237,7 +237,7 @@ static void do_hvf_cpu_synchronize_pre_loadvm(CPUState *cpu, cpu->vcpu_dirty = true; } -void hvf_cpu_synchronize_pre_loadvm (CPUState *cpu) +static void hvf_cpu_synchronize_pre_loadvm (CPUState *cpu) {run_on_cpu (cpu, do_hvf_cpu_synchronize_pre_loadvm,RUN_ON_CPU_NULL ); }
accel/hvf/hvf-accel-ops.h +0 -3
@@ -13,8 +13,5 @@#include "sysemu/cpus.h" int hvf_vcpu_exec (CPUState *); -void hvf_cpu_synchronize_post_reset (CPUState *); -void hvf_cpu_synchronize_post_init (CPUState *); -void hvf_cpu_synchronize_pre_loadvm (CPUState *);#endif /* HVF_CPUS_H */
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 10/19 hvf: Remove hvf-accel-ops.h
In reply to Alexander GrafWe can move the definition of hvf_vcpu_exec() into our internal hvf header, obsoleting the need for hvf-accel-ops.h.
--- accel/hvf/hvf-accel-ops.c | 2 -- accel/hvf/hvf-accel-ops.h | 17 ----------------- include/sysemu/hvf_int.h | 1 + target/i386/hvf/hvf.c | 2 -- 4 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 accel/hvf/hvf-accel-ops.h
accel/hvf/hvf-accel-ops.c +0 -2
@@ -58,8 +58,6 @@#include "sysemu/runstate.h" #include "qemu/guest-random.h" -#include "hvf-accel-ops.h" -HVFState *hvf_state;/* Memory slots */
accel/hvf/hvf-accel-ops.h deleted +0 -17
@@ -1,17 +0,0 @@ -/* -* Accelerator CPUS Interface -* -* Copyright 2020 SUSE LLC -* -* This work is licensed under the terms of the GNU GPL, version 2 or later. -* See the COPYING file in the top-level directory. -*/ - -#ifndef HVF_CPUS_H -#define HVF_CPUS_H - -#include "sysemu/cpus.h" - -int hvf_vcpu_exec (CPUState *); - -#endif /* HVF_CPUS_H */
include/sysemu/hvf_int.h +1 -0
@@ -46,6 +46,7 @@ extern HVFState *hvf_state;void assert_hvf_ok (hv_return_t ret);int hvf_arch_init_vcpu (CPUState *cpu);void hvf_arch_vcpu_destroy (CPUState *cpu); +int hvf_vcpu_exec (CPUState *);hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t );int hvf_put_registers (CPUState *);int hvf_get_registers (CPUState *);
target/i386/hvf/hvf.c +0 -2
@@ -73,8 +73,6 @@#include "qemu/accel.h" #include "target/i386/cpu.h" -#include "hvf-accel-ops.h" -void vmx_update_tpr (CPUState *cpu) {/* TODO: need integrate APIC handling */
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 11/19 hvf: Introduce hvf vcpu struct
In reply to Alexander GrafWe will need more than a single field for hvf going forward. To keep the global vcpu struct uncluttered, let's allocate a special hvf vcpu struct, similar to how hax does it.
---
v4 -> v5:
- Use g_free() on destroy
--- accel/hvf/hvf-accel-ops.c | 8 +- include/hw/core/cpu.h | 3 +- include/sysemu/hvf_int.h | 4 + target/i386/hvf/hvf.c | 104 +++++++++--------- target/i386/hvf/vmx.h | 24 +++-- target/i386/hvf/x86.c | 28 ++--- target/i386/hvf/x86_descr.c | 26 ++--- target/i386/hvf/x86_emu.c | 62 +++++------ target/i386/hvf/x86_mmu.c | 4 +- target/i386/hvf/x86_task.c | 12 +-- target/i386/hvf/x86hvf.c | 210 ++++++++++++++++++------------------ 11 files changed, 248 insertions(+), 237 deletions(-)
accel/hvf/hvf-accel-ops.c +6 -2
@@ -363,16 +363,20 @@ type_init(hvf_type_init);static void hvf_vcpu_destroy (CPUState *cpu) { -hv_return_t ret =hv_vcpu_destroy (cpu->hvf_fd); +hv_return_t ret =hv_vcpu_destroy (cpu->hvf->fd);assert_hvf_ok (ret);hvf_arch_vcpu_destroy (cpu); +g_free (cpu->hvf); + cpu->hvf =NULL ; }static int hvf_init_vcpu (CPUState *cpu) {int r; + cpu->hvf =g_malloc0 (sizeof (*cpu->hvf)); +/* init cpu signals */ sigset_t set;struct sigaction sigact;
@@ -384,7 +388,7 @@ static int hvf_init_vcpu(CPUState *cpu)pthread_sigmask (SIG_BLOCK ,NULL , &set);sigdelset (&set,SIG_IPI ); - r =hv_vcpu_create ((hv_vcpuid_t *)&cpu->hvf_fd,HV_VCPU_DEFAULT ); + r =hv_vcpu_create ((hv_vcpuid_t *)&cpu->hvf->fd,HV_VCPU_DEFAULT ); cpu->vcpu_dirty =1 ;assert_hvf_ok (r);
include/hw/core/cpu.h +2 -1
@@ -251,6 +251,7 @@ struct KVMState;struct kvm_run ;struct hax_vcpu_state ; +struct hvf_vcpu_state ;#define TB_JMP_CACHE_BITS 12#define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
@@ -436,7 +437,7 @@ struct CPUState {struct hax_vcpu_state *hax_vcpu; -int hvf_fd; +struct hvf_vcpu_state *hvf;/* track IOMMUs whose translations we've cached in the TCG TLB */ GArray *iommu_notifiers;
include/sysemu/hvf_int.h +4 -0
@@ -43,6 +43,10 @@ struct HVFState { };extern HVFState *hvf_state; +struct hvf_vcpu_state { +int fd; +}; +void assert_hvf_ok (hv_return_t ret);int hvf_arch_init_vcpu (CPUState *cpu);void hvf_arch_vcpu_destroy (CPUState *cpu);
target/i386/hvf/hvf.c +52 -52
226 lines not on this page · raw message
target/i386/hvf/vmx.h +13 -11
@@ -30,6 +30,8 @@#include "vmcs.h" #include "cpu.h" #include "x86.h" +#include "sysemu/hvf.h" +#include "sysemu/hvf_int.h" #include "exec/address-spaces.h"
@@ -179,15 +181,15 @@ static inline void macvm_set_rip(CPUState *cpu, uint64_t rip)uint64_t val;/* BUG, should take considering overlap.. */ -wreg (cpu->hvf_fd,HV_X86_RIP , rip); +wreg (cpu->hvf->fd,HV_X86_RIP , rip); env->eip = rip;/* after moving forward in rip, we need to clean INTERRUPTABILITY */ - val =rvmcs (cpu->hvf_fd,VMCS_GUEST_INTERRUPTIBILITY ); + val =rvmcs (cpu->hvf->fd,VMCS_GUEST_INTERRUPTIBILITY );if (val & (VMCS_INTERRUPTIBILITY_STI_BLOCKING |VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING )) { env->hflags &= ~HF_INHIBIT_IRQ_MASK ; -wvmcs (cpu->hvf_fd,VMCS_GUEST_INTERRUPTIBILITY , +wvmcs (cpu->hvf->fd,VMCS_GUEST_INTERRUPTIBILITY , val & ~(VMCS_INTERRUPTIBILITY_STI_BLOCKING |VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING )); }
@@ -199,9 +201,9 @@ static inline void vmx_clear_nmi_blocking(CPUState *cpu)CPUX86State *env = &x86_cpu->env; env->hflags2 &= ~HF2_NMI_MASK ; -uint32_t gi = (uint32_t )rvmcs (cpu->hvf_fd,VMCS_GUEST_INTERRUPTIBILITY ); +uint32_t gi = (uint32_t )rvmcs (cpu->hvf->fd,VMCS_GUEST_INTERRUPTIBILITY ); gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING ; -wvmcs (cpu->hvf_fd,VMCS_GUEST_INTERRUPTIBILITY , gi); +wvmcs (cpu->hvf->fd,VMCS_GUEST_INTERRUPTIBILITY , gi); }static inline void vmx_set_nmi_blocking (CPUState *cpu)
@@ -210,16 +212,16 @@ static inline void vmx_set_nmi_blocking(CPUState *cpu)CPUX86State *env = &x86_cpu->env; env->hflags2 |=HF2_NMI_MASK ; -uint32_t gi = (uint32_t )rvmcs (cpu->hvf_fd,VMCS_GUEST_INTERRUPTIBILITY ); +uint32_t gi = (uint32_t )rvmcs (cpu->hvf->fd,VMCS_GUEST_INTERRUPTIBILITY ); gi |=VMCS_INTERRUPTIBILITY_NMI_BLOCKING ; -wvmcs (cpu->hvf_fd,VMCS_GUEST_INTERRUPTIBILITY , gi); +wvmcs (cpu->hvf->fd,VMCS_GUEST_INTERRUPTIBILITY , gi); }static inline void vmx_set_nmi_window_exiting (CPUState *cpu) {uint64_t val; - val =rvmcs (cpu->hvf_fd,VMCS_PRI_PROC_BASED_CTLS ); -wvmcs (cpu->hvf_fd,VMCS_PRI_PROC_BASED_CTLS , val | + val =rvmcs (cpu->hvf->fd,VMCS_PRI_PROC_BASED_CTLS ); +wvmcs (cpu->hvf->fd,VMCS_PRI_PROC_BASED_CTLS , val |VMCS_PRI_PROC_BASED_CTLS_NMI_WINDOW_EXITING ); }
@@ -228,8 +230,8 @@ static inline void vmx_clear_nmi_window_exiting(CPUState *cpu) {uint64_t val; - val =rvmcs (cpu->hvf_fd,VMCS_PRI_PROC_BASED_CTLS ); -wvmcs (cpu->hvf_fd,VMCS_PRI_PROC_BASED_CTLS , val & + val =rvmcs (cpu->hvf->fd,VMCS_PRI_PROC_BASED_CTLS ); +wvmcs (cpu->hvf->fd,VMCS_PRI_PROC_BASED_CTLS , val & ~VMCS_PRI_PROC_BASED_CTLS_NMI_WINDOW_EXITING ); }
target/i386/hvf/x86.c +14 -14
@@ -62,11 +62,11 @@ bool x86_read_segment_descriptor(struct CPUState *cpu, }if (GDT_SEL == sel.ti) { - base =rvmcs (cpu->hvf_fd,VMCS_GUEST_GDTR_BASE ); - limit =rvmcs (cpu->hvf_fd,VMCS_GUEST_GDTR_LIMIT ); + base =rvmcs (cpu->hvf->fd,VMCS_GUEST_GDTR_BASE ); + limit =rvmcs (cpu->hvf->fd,VMCS_GUEST_GDTR_LIMIT ); }else { - base =rvmcs (cpu->hvf_fd,VMCS_GUEST_LDTR_BASE ); - limit =rvmcs (cpu->hvf_fd,VMCS_GUEST_LDTR_LIMIT ); + base =rvmcs (cpu->hvf->fd,VMCS_GUEST_LDTR_BASE ); + limit =rvmcs (cpu->hvf->fd,VMCS_GUEST_LDTR_LIMIT ); }if (sel.index *8 >= limit) {
@@ -85,11 +85,11 @@ bool x86_write_segment_descriptor(struct CPUState *cpu,uint32_t limit;if (GDT_SEL == sel.ti) { - base =rvmcs (cpu->hvf_fd,VMCS_GUEST_GDTR_BASE ); - limit =rvmcs (cpu->hvf_fd,VMCS_GUEST_GDTR_LIMIT ); + base =rvmcs (cpu->hvf->fd,VMCS_GUEST_GDTR_BASE ); + limit =rvmcs (cpu->hvf->fd,VMCS_GUEST_GDTR_LIMIT ); }else { - base =rvmcs (cpu->hvf_fd,VMCS_GUEST_LDTR_BASE ); - limit =rvmcs (cpu->hvf_fd,VMCS_GUEST_LDTR_LIMIT ); + base =rvmcs (cpu->hvf->fd,VMCS_GUEST_LDTR_BASE ); + limit =rvmcs (cpu->hvf->fd,VMCS_GUEST_LDTR_LIMIT ); }if (sel.index *8 >= limit) {
@@ -103,8 +103,8 @@ bool x86_write_segment_descriptor(struct CPUState *cpu,bool x86_read_call_gate (struct CPUState *cpu,struct x86_call_gate *idt_desc,int gate) { -target_ulong base =rvmcs (cpu->hvf_fd,VMCS_GUEST_IDTR_BASE ); -uint32_t limit =rvmcs (cpu->hvf_fd,VMCS_GUEST_IDTR_LIMIT ); +target_ulong base =rvmcs (cpu->hvf->fd,VMCS_GUEST_IDTR_BASE ); +uint32_t limit =rvmcs (cpu->hvf->fd,VMCS_GUEST_IDTR_LIMIT );memset (idt_desc,0 ,sizeof (*idt_desc));if (gate *8 >= limit) {
@@ -118,7 +118,7 @@ bool x86_read_call_gate(struct CPUState *cpu, struct x86_call_gate *idt_desc,bool x86_is_protected (struct CPUState *cpu) { -uint64_t cr0 =rvmcs (cpu->hvf_fd,VMCS_GUEST_CR0 ); +uint64_t cr0 =rvmcs (cpu->hvf->fd,VMCS_GUEST_CR0 );return cr0 &CR0_PE ; }
@@ -136,7 +136,7 @@ bool x86_is_v8086(struct CPUState *cpu)bool x86_is_long_mode (struct CPUState *cpu) { -return rvmcs (cpu->hvf_fd,VMCS_GUEST_IA32_EFER ) &MSR_EFER_LMA ; +return rvmcs (cpu->hvf->fd,VMCS_GUEST_IA32_EFER ) &MSR_EFER_LMA ; }bool x86_is_long64_mode (struct CPUState *cpu)
@@ -149,13 +149,13 @@ bool x86_is_long64_mode(struct CPUState *cpu)bool x86_is_paging_mode (struct CPUState *cpu) { -uint64_t cr0 =rvmcs (cpu->hvf_fd,VMCS_GUEST_CR0 ); +uint64_t cr0 =rvmcs (cpu->hvf->fd,VMCS_GUEST_CR0 );return cr0 &CR0_PG ; }bool x86_is_pae_enabled (struct CPUState *cpu) { -uint64_t cr4 =rvmcs (cpu->hvf_fd,VMCS_GUEST_CR4 ); +uint64_t cr4 =rvmcs (cpu->hvf->fd,VMCS_GUEST_CR4 );return cr4 &CR4_PAE ; }
target/i386/hvf/x86_descr.c +13 -13
@@ -48,47 +48,47 @@ static const struct vmx_segment_field {uint32_t vmx_read_segment_limit (CPUState *cpu,X86Seg seg) { -return (uint32_t )rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].limit); +return (uint32_t )rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].limit); }uint32_t vmx_read_segment_ar (CPUState *cpu,X86Seg seg) { -return (uint32_t )rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].ar_bytes); +return (uint32_t )rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].ar_bytes); }uint64_t vmx_read_segment_base (CPUState *cpu,X86Seg seg) { -return rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].base); +return rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].base); }x68_segment_selector vmx_read_segment_selector (CPUState *cpu,X86Seg seg) {x68_segment_selector sel; - sel.sel =rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].selector); + sel.sel =rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].selector);return sel; }void vmx_write_segment_selector (struct CPUState *cpu,x68_segment_selector selector,X86Seg seg) { -wvmcs (cpu->hvf_fd, vmx_segment_fields[seg].selector, selector.sel); +wvmcs (cpu->hvf->fd, vmx_segment_fields[seg].selector, selector.sel); }void vmx_read_segment_descriptor (struct CPUState *cpu,struct vmx_segment *desc,X86Seg seg) { - desc->sel =rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].selector); - desc->base =rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].base); - desc->limit =rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].limit); - desc->ar =rvmcs (cpu->hvf_fd, vmx_segment_fields[seg].ar_bytes); + desc->sel =rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].selector); + desc->base =rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].base); + desc->limit =rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].limit); + desc->ar =rvmcs (cpu->hvf->fd, vmx_segment_fields[seg].ar_bytes); }void vmx_write_segment_descriptor (CPUState *cpu,struct vmx_segment *desc,X86Seg seg) {const struct vmx_segment_field *sf = &vmx_segment_fields[seg]; -wvmcs (cpu->hvf_fd, sf->base, desc->base); -wvmcs (cpu->hvf_fd, sf->limit, desc->limit); -wvmcs (cpu->hvf_fd, sf->selector, desc->sel); -wvmcs (cpu->hvf_fd, sf->ar_bytes, desc->ar); +wvmcs (cpu->hvf->fd, sf->base, desc->base); +wvmcs (cpu->hvf->fd, sf->limit, desc->limit); +wvmcs (cpu->hvf->fd, sf->selector, desc->sel); +wvmcs (cpu->hvf->fd, sf->ar_bytes, desc->ar); }void x86_segment_descriptor_to_vmx (struct CPUState *cpu,x68_segment_selector selector,struct x86_segment_descriptor *desc,struct vmx_segment *vmx_desc)
target/i386/hvf/x86_emu.c +31 -31
117 lines not on this page · raw message
target/i386/hvf/x86_mmu.c +2 -2
@@ -127,7 +127,7 @@ static bool test_pt_entry(struct CPUState *cpu, struct gpt_translation *pt, pt->err_code |=MMU_PAGE_PT ; } -uint32_t cr0 =rvmcs (cpu->hvf_fd,VMCS_GUEST_CR0 ); +uint32_t cr0 =rvmcs (cpu->hvf->fd,VMCS_GUEST_CR0 );/* check protection */ if (cr0 &CR0_WP ) {if (pt->write_access && !pte_write_access (pte)) {
@@ -172,7 +172,7 @@ static bool walk_gpt(struct CPUState *cpu, target_ulong addr, int err_code, {int top_level, level;bool is_large = false; -target_ulong cr3 =rvmcs (cpu->hvf_fd,VMCS_GUEST_CR3 ); +target_ulong cr3 =rvmcs (cpu->hvf->fd,VMCS_GUEST_CR3 );uint64_t page_mask = pae ?PAE_PTE_PAGE_MASK :LEGACY_PTE_PAGE_MASK ;memset (pt,0 ,sizeof (*pt));
target/i386/hvf/x86_task.c +6 -6
@@ -62,7 +62,7 @@ static void load_state_from_tss32(CPUState *cpu, struct x86_tss_segment32 *tss)X86CPU *x86_cpu =X86_CPU (cpu);CPUX86State *env = &x86_cpu->env; -wvmcs (cpu->hvf_fd,VMCS_GUEST_CR3 , tss->cr3); +wvmcs (cpu->hvf->fd,VMCS_GUEST_CR3 , tss->cr3); env->eip = tss->eip; env->eflags = tss->eflags |2 ;
@@ -111,11 +111,11 @@ static int task_switch_32(CPUState *cpu, x68_segment_selector tss_sel, x68_segmevoid vmx_handle_task_switch (CPUState *cpu,x68_segment_selector tss_sel,int reason,bool gate_valid,uint8_t gate,uint64_t gate_type) { -uint64_t rip =rreg (cpu->hvf_fd,HV_X86_RIP ); +uint64_t rip =rreg (cpu->hvf->fd,HV_X86_RIP );if (!gate_valid || (gate_type !=VMCS_INTR_T_HWEXCEPTION && gate_type !=VMCS_INTR_T_HWINTR && gate_type !=VMCS_INTR_T_NMI )) { -int ins_len =rvmcs (cpu->hvf_fd,VMCS_EXIT_INSTRUCTION_LENGTH ); +int ins_len =rvmcs (cpu->hvf->fd,VMCS_EXIT_INSTRUCTION_LENGTH );macvm_set_rip (cpu, rip + ins_len);return ; }
@@ -174,12 +174,12 @@ void vmx_handle_task_switch(CPUState *cpu, x68_segment_selector tss_sel, int rea//ret = task_switch_16(cpu, tss_sel, old_tss_sel, old_tss_base, &next_tss_desc); VM_PANIC ("task_switch_16" ); -macvm_set_cr0 (cpu->hvf_fd,rvmcs (cpu->hvf_fd,VMCS_GUEST_CR0 ) |CR0_TS ); +macvm_set_cr0 (cpu->hvf->fd,rvmcs (cpu->hvf->fd,VMCS_GUEST_CR0 ) |CR0_TS );x86_segment_descriptor_to_vmx (cpu, tss_sel, &next_tss_desc, &vmx_seg);vmx_write_segment_descriptor (cpu, &vmx_seg,R_TR );store_regs (cpu); -hv_vcpu_invalidate_tlb (cpu->hvf_fd); -hv_vcpu_flush (cpu->hvf_fd); +hv_vcpu_invalidate_tlb (cpu->hvf->fd); +hv_vcpu_flush (cpu->hvf->fd); }
target/i386/hvf/x86hvf.c +105 -105
354 lines not on this page · raw message
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 12/19 hvf: Simplify post reset/init/loadvm hooks
In reply to Alexander GrafThe hooks we have that call us after reset, init and loadvm really all just want to say "The reference of all register state is in the QEMU vcpu struct, please push it".
We already have a working pushing mechanism though called cpu->vcpu_dirty, so we can just reuse that for all of the above, syncing state properly the next time we actually execute a vCPU.
This fixes PSCI resets on ARM, as they modify CPU state even after the post init call has completed, but before we execute the vCPU again.
To also make the scheme work for x86, we have to make sure we don't move stale eflags into our env when the vcpu state is dirty.
--- accel/hvf/hvf-accel-ops.c | 27 +++++++-------------------- target/i386/hvf/x86hvf.c | 5 ++++- 2 files changed, 11 insertions(+), 21 deletions(-)
accel/hvf/hvf-accel-ops.c +7 -20
@@ -205,39 +205,26 @@ static void hvf_cpu_synchronize_state(CPUState *cpu) } } -static void do_hvf_cpu_synchronize_post_reset (CPUState *cpu, -run_on_cpu_data arg) +static void do_hvf_cpu_synchronize_set_dirty (CPUState *cpu, +run_on_cpu_data arg) { -hvf_put_registers (cpu); - cpu->vcpu_dirty = false; +/* QEMU state is the reference, push it to HVF now and on next entry */ + cpu->vcpu_dirty = true; }static void hvf_cpu_synchronize_post_reset (CPUState *cpu) { -run_on_cpu (cpu, do_hvf_cpu_synchronize_post_reset,RUN_ON_CPU_NULL ); -} - -static void do_hvf_cpu_synchronize_post_init (CPUState *cpu, -run_on_cpu_data arg) -{ -hvf_put_registers (cpu); - cpu->vcpu_dirty = false; +run_on_cpu (cpu, do_hvf_cpu_synchronize_set_dirty,RUN_ON_CPU_NULL ); }static void hvf_cpu_synchronize_post_init (CPUState *cpu) { -run_on_cpu (cpu, do_hvf_cpu_synchronize_post_init,RUN_ON_CPU_NULL ); -} - -static void do_hvf_cpu_synchronize_pre_loadvm (CPUState *cpu, -run_on_cpu_data arg) -{ - cpu->vcpu_dirty = true; +run_on_cpu (cpu, do_hvf_cpu_synchronize_set_dirty,RUN_ON_CPU_NULL ); }static void hvf_cpu_synchronize_pre_loadvm (CPUState *cpu) { -run_on_cpu (cpu, do_hvf_cpu_synchronize_pre_loadvm,RUN_ON_CPU_NULL ); +run_on_cpu (cpu, do_hvf_cpu_synchronize_set_dirty,RUN_ON_CPU_NULL ); }static void hvf_set_dirty_tracking (MemoryRegionSection *section,bool on)
target/i386/hvf/x86hvf.c +4 -1
@@ -433,7 +433,10 @@ int hvf_process_events(CPUState *cpu_state)X86CPU *cpu =X86_CPU (cpu_state);CPUX86State *env = &cpu->env; - env->eflags =rreg (cpu_state->hvf->fd,HV_X86_RFLAGS ); +if (!cpu_state->vcpu_dirty) { +/* light weight sync for CPU_INTERRUPT_HARD and IF_MASK */ + env->eflags =rreg (cpu_state->hvf->fd,HV_X86_RFLAGS ); + }if (cpu_state->interrupt_request &CPU_INTERRUPT_INIT ) {cpu_synchronize_state (cpu_state);
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 13/19 hvf: Add Apple Silicon support
In reply to Alexander GrafWith Apple Silicon available to the masses, it's a good time to add support for driving its virtualization extensions from QEMU.
This patch adds all necessary architecture specific code to get basic VMs working. It's still pretty raw, but definitely functional.
Known limitations:
- Vtimer acknowledgement is hacky
- Should implement more sysregs and fault on invalid ones then
- WFI handling is missing, need to marry it with vtimer
---
v1 -> v2:
- Merge vcpu kick function patch
- Implement WFI handling (allows vCPUs to sleep)
- Synchronize system registers (fixes OVMF crashes and reboot)
- Don't always call cpu_synchronize_state()
- Use more fine grained iothread locking
- Populate aa64mmfr0 from hardware
v2 -> v3:
- Advance PC on SMC
- Use cp list interface for sysreg syncs
- Do not set current_cpu
- Fix sysreg isread mask
- Move sysreg handling to functions
- Remove WFI logic again
- Revert to global iothread locking
- Use Hypervisor.h on arm, hv.h does not contain aarch64 definitions
v3 -> v4:
- No longer include Hypervisor.h
v5 -> v6:
- Swap sysreg definition order. This way we're in line with asm outputs.
v6 -> v7:
- Remove osdep.h include from hvf_int.h
- Synchronize SIMD registers as well
- Prepend 0x for hex values
- Convert DPRINTF to trace points
- Use main event loop (fixes gdbstub issues)
- Remove PSCI support, inject UDEF on HVC/SMC
- Change vtimer logic to look at ctl.istatus for vtimer mask sync
- Add kick callback again (fixes remote CPU notification)
--- MAINTAINERS | 5 + accel/hvf/hvf-accel-ops.c | 14 + include/sysemu/hvf_int.h | 9 +- meson.build | 1 + target/arm/hvf/hvf.c | 699 ++++++++++++++++++++++++++++++++++++ target/arm/hvf/trace-events | 10 + 6 files changed, 737 insertions(+), 1 deletion(-) create mode 100644 target/arm/hvf/hvf.c create mode 100644 target/arm/hvf/trace-events
MAINTAINERS +5 -0
@@ -428,6 +428,11 @@ F: accel/accel-*.c F: accel/Makefile.objs F: accel/stubs/Makefile.objs +Apple Silicon HVF CPUs +M: Alexander Graf <[email protected]> +S: Maintained +F: target/arm/hvf/ + X86 HVF CPUs M: Cameron Esfahani <[email protected]> M: Roman Bolshakov <[email protected]>
accel/hvf/hvf-accel-ops.c +14 -0
@@ -60,6 +60,10 @@HVFState *hvf_state; +#ifdef __aarch64__ +#define HV_VM_DEFAULT NULL +#endif +/* Memory slots */ hvf_slot *hvf_find_overlap_slot (uint64_t start,uint64_t size)
@@ -375,7 +379,11 @@ static int hvf_init_vcpu(CPUState *cpu)pthread_sigmask (SIG_BLOCK ,NULL , &set);sigdelset (&set,SIG_IPI ); +#ifdef __aarch64__ + r =hv_vcpu_create (&cpu->hvf->fd, (hv_vcpu_exit_t **)&cpu->hvf->exit,NULL ); +#else r =hv_vcpu_create ((hv_vcpuid_t *)&cpu->hvf->fd,HV_VCPU_DEFAULT ); +#endif cpu->vcpu_dirty =1 ;assert_hvf_ok (r);
@@ -446,11 +454,17 @@ static void hvf_start_vcpu_thread(CPUState *cpu) cpu, QEMU_THREAD_JOINABLE); } +__attribute__((weak))void hvf_kick_vcpu_thread (CPUState *cpu) +{ +cpus_kick_thread (cpu); +} +static void hvf_accel_ops_class_init (ObjectClass *oc,void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc); ops->create_vcpu_thread = hvf_start_vcpu_thread; + ops->kick_vcpu_thread = hvf_kick_vcpu_thread; ops->synchronize_post_reset = hvf_cpu_synchronize_post_reset; ops->synchronize_post_init = hvf_cpu_synchronize_post_init;
include/sysemu/hvf_int.h +8 -1
@@ -11,7 +11,11 @@#ifndef HVF_INT_H #define HVF_INT_H +#ifdef __aarch64__ +#include <Hypervisor/Hypervisor.h> +#else #include <Hypervisor/hv.h> +#endif /* hvf_slot flags */ #define HVF_SLOT_LOG (1 << 0)
@@ -44,7 +48,9 @@ struct HVFState {extern HVFState *hvf_state;struct hvf_vcpu_state { -int fd; +uint64_t fd; +void *exit; +bool vtimer_masked; };void assert_hvf_ok (hv_return_t ret);
@@ -54,5 +60,6 @@ int hvf_vcpu_exec(CPUState *);hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t );int hvf_put_registers (CPUState *);int hvf_get_registers (CPUState *); +void hvf_kick_vcpu_thread (CPUState *cpu);#endif
meson.build +1 -0
@@ -1855,6 +1855,7 @@ if have_system or have_user 'accel/tcg', 'hw/core', 'target/arm', + 'target/arm/hvf', 'target/hppa', 'target/i386', 'target/i386/kvm',
target/arm/hvf/hvf.c new file +699 -0
699 lines not on this page · raw message
target/arm/hvf/trace-events new file +10 -0
@@ -0,0 +1,10 @@ +hvf_unhandled_sysreg_read(uint32_t reg, uint32_t op0, uint32_t op1, uint32_t crn, uint32_t crm, uint32_t op2) "unhandled sysreg read 0x%08x (op0=%d op1=%d crn=%d crm=%d op2=%d)" +hvf_unhandled_sysreg_write(uint32_t reg, uint32_t op0, uint32_t op1, uint32_t crn, uint32_t crm, uint32_t op2) "unhandled sysreg write 0x%08x (op0=%d op1=%d crn=%d crm=%d op2=%d)" +hvf_inject_fiq(void) "injecting FIQ" +hvf_inject_irq(void) "injecting IRQ" +hvf_data_abort(uint64_t pc, uint64_t va, uint64_t pa, bool isv, bool iswrite, bool s1ptw, uint32_t len, uint32_t srt) "data abort: [pc=0x%"PRIx64" va=0x%016"PRIx64" pa=0x%016"PRIx64" isv=%d iswrite=%d s1ptw=%d len=%d srt=%d]" +hvf_sysreg_read(uint32_t reg, uint32_t op0, uint32_t op1, uint32_t crn, uint32_t crm, uint32_t op2, uint64_t val) "sysreg read 0x%08x (op0=%d op1=%d crn=%d crm=%d op2=%d) = %016"PRIx64 +hvf_sysreg_write(uint32_t reg, uint32_t op0, uint32_t op1, uint32_t crn, uint32_t crm, uint32_t op2, uint64_t val) "sysreg write 0x%08x (op0=%d op1=%d crn=%d crm=%d op2=%d, val=%016"PRIx64")" +hvf_unknown_hvf(uint64_t x0) "unknown HVC! %016"PRIx64 +hvf_unknown_smc(uint64_t x0) "unknown SMC! %016"PRIx64 +hvf_exit(uint64_t syndrome, uint32_t ec, uint64_t pc) "exit: 0x%"PRIx64" [ec=0x%x pc=0x%"PRIx64"]"
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 14/19 arm/hvf: Add a WFI handler
In reply to Alexander GrafFrom: Peter Collingbourne <[email protected]>
Sleep on WFI until the VTIMER is due but allow ourselves to be woken up on IPI.
In this implementation IPI is blocked on the CPU thread at startup and pselect() is used to atomically unblock the signal and begin sleeping. The signal is sent unconditionally so there's no need to worry about races between actually sleeping and the "we think we're sleeping" state. It may lead to an extra wakeup but that's better than missing it entirely.
---
v6 -> v7:
- Move WFI into function
- Improve comment wording
--- accel/hvf/hvf-accel-ops.c | 5 ++- include/sysemu/hvf_int.h | 1 + target/arm/hvf/hvf.c | 68 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-)
accel/hvf/hvf-accel-ops.c +2 -3
@@ -369,15 +369,14 @@ static int hvf_init_vcpu(CPUState *cpu) cpu->hvf =g_malloc0 (sizeof (*cpu->hvf));/* init cpu signals */ -sigset_t set;struct sigaction sigact;memset (&sigact,0 ,sizeof (sigact)); sigact.sa_handler = dummy_signal;sigaction (SIG_IPI , &sigact,NULL ); -pthread_sigmask (SIG_BLOCK ,NULL , &set); -sigdelset (&set,SIG_IPI ); +pthread_sigmask (SIG_BLOCK ,NULL , &cpu->hvf->unblock_ipi_mask); +sigdelset (&cpu->hvf->unblock_ipi_mask,SIG_IPI );#ifdef __aarch64__ r =hv_vcpu_create (&cpu->hvf->fd, (hv_vcpu_exit_t **)&cpu->hvf->exit,NULL );
include/sysemu/hvf_int.h +1 -0
@@ -51,6 +51,7 @@ struct hvf_vcpu_state {uint64_t fd;void *exit;bool vtimer_masked; +sigset_t unblock_ipi_mask; };void assert_hvf_ok (hv_return_t ret);
target/arm/hvf/hvf.c +68 -0
98 lines not on this page · raw message
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 15/19 hvf: arm: Implement -cpu host
In reply to Alexander GrafNow that we have working system register sync, we push more target CPU properties into the virtual machine. That might be useful in some situations, but is not the typical case that users want.
So let's add a -cpu host option that allows them to explicitly pass all CPU capabilities of their host CPU into the guest.
---
v6 -> v7:
- Move function define to own header
- Do not propagate SVE features for HVF
- Remove stray whitespace change
- Verify that EL0 and EL1 do not allow AArch32 mode
- Only probe host CPU features once
--- target/arm/cpu.c | 9 ++++-- target/arm/cpu.h | 2 ++ target/arm/hvf/hvf.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ target/arm/hvf_arm.h | 19 ++++++++++++ target/arm/kvm_arm.h | 2 -- 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 target/arm/hvf_arm.h
target/arm/cpu.c +7 -2
@@ -39,6 +39,7 @@#include "sysemu/tcg.h" #include "sysemu/hw_accel.h" #include "kvm_arm.h" +#include "hvf_arm.h" #include "disas/capstone.h" #include "fpu/softfloat.h"
@@ -1998,15 +1999,19 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)#endif /* CONFIG_TCG */ } -#ifdef CONFIG_KVM +#if defined(CONFIG_KVM ) || defined(CONFIG_HVF )static void arm_host_initfn (Object *obj) {ARMCPU *cpu =ARM_CPU (obj); +#ifdef CONFIG_KVM kvm_arm_set_cpu_features_from_host (cpu);if (arm_feature (&cpu->env,ARM_FEATURE_AARCH64 )) {aarch64_add_sve_properties (obj); } +#else +hvf_arm_set_cpu_features_from_host (cpu); +#endif arm_cpu_post_init (obj); }
@@ -2066,7 +2071,7 @@ static void arm_cpu_register_types(void) {type_register_static (&arm_cpu_type_info); -#ifdef CONFIG_KVM +#if defined(CONFIG_KVM ) || defined(CONFIG_HVF )type_register_static (&host_arm_cpu_type_info);#endif }
target/arm/cpu.h +2 -0
@@ -2977,6 +2977,8 @@ bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync);#define ARM_CPU_TYPE_NAME (name) (name ARM_CPU_TYPE_SUFFIX)#define CPU_RESOLVING_TYPE TYPE_ARM_CPU +#define TYPE_ARM_HOST_CPU "host-" TYPE_ARM_CPU +#define cpu_signal_handler cpu_arm_signal_handler#define cpu_list arm_cpu_list
target/arm/hvf/hvf.c +72 -0
90 lines not on this page · raw message
target/arm/hvf_arm.h new file +19 -0
@@ -0,0 +1,19 @@ +/* +* QEMU Hypervisor.framework (HVF) support -- ARM specifics +* +* Copyright (c) 2021 Alexander Graf +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef QEMU_HVF_ARM_H +#define QEMU_HVF_ARM_H + +#include "qemu/accel.h" +#include "cpu.h" + +void hvf_arm_set_cpu_features_from_host (struct ARMCPU *cpu); + +#endif
target/arm/kvm_arm.h +0 -2
@@ -214,8 +214,6 @@ bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,*/ void kvm_arm_destroy_scratch_host_vcpu (int *fdarray); -#define TYPE_ARM_HOST_CPU "host-" TYPE_ARM_CPU -/** * ARMHostCPUFeatures: information about the host CPU (identified * by asking the host kernel)
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 16/19 hvf: arm: Implement PSCI handling
In reply to Alexander GrafWe need to handle PSCI calls. Most of the TCG code works for us, but we can simplify it to only handle aa64 mode and we need to handle SUSPEND differently.
This patch takes the TCG code as template and duplicates it in HVF.
To tell the guest that we support PSCI 0.2 now, update the check in arm_cpu_initfn() as well.
---
v6 -> v7:
- This patch integrates "arm: Set PSCI to 0.2 for HVF"
--- target/arm/cpu.c | 4 +- target/arm/hvf/hvf.c | 124 ++++++++++++++++++++++++++++++++++-- target/arm/hvf/trace-events | 1 + 3 files changed, 123 insertions(+), 6 deletions(-)
target/arm/cpu.c +2 -2
@@ -1079,8 +1079,8 @@ static void arm_cpu_initfn(Object *obj) cpu->psci_version =1 ;/* By default assume PSCI v0.1 */ cpu->kvm_target =QEMU_KVM_ARM_TARGET_NONE ; -if (tcg_enabled ()) { - cpu->psci_version =2 ;/* TCG implements PSCI 0.2 */ +if (tcg_enabled () ||hvf_enabled ()) { + cpu->psci_version =2 ;/* TCG and HVF implement PSCI 0.2 */ } }
target/arm/hvf/hvf.c +120 -4
157 lines not on this page · raw message
target/arm/hvf/trace-events +1 -0
@@ -8,3 +8,4 @@ hvf_sysreg_write(uint32_t reg, uint32_t op0, uint32_t op1, uint32_t crn, uint32_ hvf_unknown_hvf(uint64_t x0) "unknown HVC! %016"PRIx64 hvf_unknown_smc(uint64_t x0) "unknown SMC! %016"PRIx64 hvf_exit(uint64_t syndrome, uint32_t ec, uint64_t pc) "exit: 0x%"PRIx64" [ec=0x%x pc=0x%"PRIx64"]" +hvf_psci_call(uint64_t x0, uint64_t x1, uint64_t x2, uint64_t x3, uint32_t cpuid) "PSCI Call x0=%016"PRIx64" x1=%016"PRIx64" x2=%016"PRIx64" x3=%016"PRIx64" cpu=%x"
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 17/19 arm: Add Hypervisor.framework build target
In reply to Alexander GrafNow that we have all logic in place that we need to handle Hypervisor.framework on Apple Silicon systems, let's add CONFIG_HVF for aarch64 as well so that we can build it.
---
v1 -> v2:
- Fix build on 32bit arm
v3 -> v4:
- Remove i386-softmmu target
v6 -> v7:
- Simplify HVF matching logic in meson build file
--- meson.build | 7 +++++++ target/arm/hvf/meson.build | 3 +++ target/arm/meson.build | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 target/arm/hvf/meson.build
meson.build +7 -0
@@ -77,6 +77,13 @@ else endif accelerator_targets = { 'CONFIG_KVM': kvm_targets } + +if cpu in ['aarch64'] + accelerator_targets += { + 'CONFIG_HVF': ['aarch64-softmmu'] + } +endif + if cpu in ['x86', 'x86_64', 'arm', 'aarch64'] # i368 emulator provides xenpv machine type for multiple architectures accelerator_targets += {
target/arm/hvf/meson.build new file +3 -0
@@ -0,0 +1,3 @@ +arm_softmmu_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files( + 'hvf.c', +))
target/arm/meson.build +2 -0
@@ -57,5 +57,7 @@ arm_softmmu_ss.add(files( 'psci.c', )) +subdir('hvf') + target_arch += {'arm': arm_ss} target_softmmu_arch += {'arm': arm_softmmu_ss}
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 18/19 arm: Enable Windows 10 trusted SMCCC boot call
In reply to Alexander GrafWindows 10 calls an SMCCC call via SMC unconditionally on boot. It lives in the trusted application call number space, but its purpose is unknown.
In our current SMC implementation, we inject a UDEF for unknown SMC calls, including this one. However, Windows breaks on boot when we do this. Instead, let's return an error code.
With this and -M virt,virtualization=on I can successfully boot the current Windows 10 Insider Preview in TCG.
--- target/arm/kvm-consts.h | 2 ++ target/arm/psci.c | 2 ++ 2 files changed, 4 insertions(+)
target/arm/kvm-consts.h +2 -0
@@ -85,6 +85,8 @@ MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_CPU_SUSPEND, PSCI_0_2_FN64_CPU_SUSPEND);MISMATCH_CHECK (QEMU_PSCI_0_2_FN64_CPU_ON ,PSCI_0_2_FN64_CPU_ON );MISMATCH_CHECK (QEMU_PSCI_0_2_FN64_MIGRATE ,PSCI_0_2_FN64_MIGRATE ); +#define QEMU_SMCCC_TC_WINDOWS10_BOOT 0xc3000001 +/* PSCI v0.2 return values used by TCG emulation of PSCI */ /* No Trusted OS migration to worry about when offlining CPUs */
target/arm/psci.c +2 -0
@@ -69,6 +69,7 @@ bool arm_is_psci_call(ARMCPU *cpu, int excp_type)case QEMU_PSCI_0_2_FN64_CPU_SUSPEND :case QEMU_PSCI_0_1_FN_MIGRATE :case QEMU_PSCI_0_2_FN_MIGRATE : +case QEMU_SMCCC_TC_WINDOWS10_BOOT :return true;default :return false;
@@ -194,6 +195,7 @@ void arm_handle_psci_call(ARMCPU *cpu)break ;case QEMU_PSCI_0_1_FN_MIGRATE :case QEMU_PSCI_0_2_FN_MIGRATE : +case QEMU_SMCCC_TC_WINDOWS10_BOOT : ret =QEMU_PSCI_RET_NOT_SUPPORTED ;break ;default :
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH v7 19/19 hvf: arm: Handle Windows 10 SMC call
In reply to Alexander GrafWindows 10 calls an SMCCC call via SMC unconditionally on boot. It lives in the trusted application call number space, but its purpose is unknown.
In our current SMC implementation, we inject a UDEF for unknown SMC calls, including this one. However, Windows breaks on boot when we do this. Instead, let's return an error code.
With this patch applied I can successfully boot the current Windows 10 Insider Preview in HVF.
--- target/arm/hvf/hvf.c | 4 ++++ 1 file changed, 4 insertions(+)
target/arm/hvf/hvf.c +4 -0
@@ -928,6 +928,10 @@ int hvf_vcpu_exec(CPUState *cpu)cpu_synchronize_state (cpu);if (!hvf_handle_psci_call (cpu)) { advance_pc = true; + }else if (env->xregs[0 ] ==QEMU_SMCCC_TC_WINDOWS10_BOOT ) { +/* This special SMC is called by Windows 10 on boot. Return error. */ + env->xregs[0 ] =-1 ; + advance_pc = true; }else {trace_hvf_unknown_smc (env->xregs[0 ]);hvf_raise_exception (env,EXCP_UDEF ,syn_uncategorized ());
Signature · 1 line
2.30.1 (Apple Git-130)
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [PATCH v7 00/19] hvf: Implement Apple Silicon Support
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
9b1e81d..6005ee0 master -> master
* [new tag] patchew/[email protected] -> patchew/[email protected]
Auto packing the repository for optimum performance. You may also run "git gc" manually. See "git help gc" for more information. Switched to a new branch 'test'
51b6048 hvf: arm: Handle Windows 10 SMC call
f23720b arm: Enable Windows 10 trusted SMCCC boot call 06e2184 arm: Add Hypervisor.framework build target e5c9561 hvf: arm: Implement PSCI handling
d61ad07 hvf: arm: Implement -cpu host
22268ee arm/hvf: Add a WFI handler
d4bfd38 hvf: Add Apple Silicon support
2a57549 hvf: Simplify post reset/init/loadvm hooks 2169cd5 hvf: Introduce hvf vcpu struct
fb67de4 hvf: Remove hvf-accel-ops.h
a6dc95d hvf: Make synchronize functions static
26045be hvf: Use cpu_synchronize_state()
d888737 hvf: Split out common code on vcpu init and destroy 6742b0a hvf: Remove use of hv_uvaddr_t and hv_gpaddr_t bad87f2 hvf: Make hvf_set_phys_mem() static
37ba42a hvf: Move hvf internal definitions into common header db1c2eb hvf: Move cpu functions into common directory 5e5ba4e hvf: Move vcpu thread functions into common directory 1501777 hvf: Move assert_hvf_ok() into common directory
=== OUTPUT BEGIN ===
1/19 Checking commit 1501777fce49 (hvf: Move assert_hvf_ok() into common directory) 2/19 Checking commit 5e5ba4e4171b (hvf: Move vcpu thread functions into common directory)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #17:
{target/i386 => accel}/hvf/hvf-accel-ops.c | 0total: 0 errors, 1 warnings, 21 lines checked
Patch 2/19 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
3/19 Checking commit db1c2ebca7b9 (hvf: Move cpu functions into common directory) 4/19 Checking commit 37ba42a9217d (hvf: Move hvf internal definitions into common header) 5/19 Checking commit bad87f2351a2 (hvf: Make hvf_set_phys_mem() static) 6/19 Checking commit 6742b0a0e9a0 (hvf: Remove use of hv_uvaddr_t and hv_gpaddr_t) 7/19 Checking commit d8887370f041 (hvf: Split out common code on vcpu init and destroy) 8/19 Checking commit 26045bee3b8a (hvf: Use cpu_synchronize_state())
9/19 Checking commit a6dc95d38d0b (hvf: Make synchronize functions static) 10/19 Checking commit fb67de47006a (hvf: Remove hvf-accel-ops.h)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #33:
deleted file mode 100644
total: 0 errors, 1 warnings, 23 lines checked
Patch 10/19 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
11/19 Checking commit 2169cd5bfabb (hvf: Introduce hvf vcpu struct)
WARNING: line over 80 characters
#154: FILE: target/i386/hvf/hvf.c:263:
+ wvmcs(cpu->hvf->fd, VMCS_ENTRY_CTLS, cap2ctrl(hvf_state->hvf_caps->vmx_cap_entry,
ERROR: "(foo*)" should be "(foo *)"
#767: FILE: target/i386/hvf/x86hvf.c:83:
+ if (hv_vcpu_write_fpstate(cpu_state->hvf->fd, (void*)xsave, 4096)) {
ERROR: "(foo*)" should be "(foo *)"
#848: FILE: target/i386/hvf/x86hvf.c:165:
+ if (hv_vcpu_read_fpstate(cpu_state->hvf->fd, (void*)xsave, 4096)) {
total: 2 errors, 1 warnings, 1001 lines checked
Patch 11/19 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
12/19 Checking commit 2a575495eee5 (hvf: Simplify post reset/init/loadvm hooks) 13/19 Checking commit d4bfd3851069 (hvf: Add Apple Silicon support)
WARNING: architecture specific defines should be avoided
#56: FILE: accel/hvf/hvf-accel-ops.c:63:
+#ifdef __aarch64__
WARNING: architecture specific defines should be avoided #67: FILE: accel/hvf/hvf-accel-ops.c:382:
+#ifdef __aarch64__
WARNING: architecture specific defines should be avoided #101: FILE: include/sysemu/hvf_int.h:14:
+#ifdef __aarch64__
WARNING: line over 80 characters
#475: FILE: target/arm/hvf/hvf.c:331:
+ ret = hv_vcpu_set_simd_fp_reg(cpu->hvf->fd, hvf_fpreg_match[i].reg, fpval);
WARNING: line over 80 characters
#606: FILE: target/arm/hvf/hvf.c:462:
+static void hvf_raise_exception(CPUARMState *env, uint32_t excp, uint32_t syndrome)
ERROR: superfluous trailing semicolon
#609: FILE: target/arm/hvf/hvf.c:465:
+ unsigned int old_mode = pstate_read(env);;
WARNING: line over 80 characters
#668: FILE: target/arm/hvf/hvf.c:524:
+ hv_vcpu_set_pending_interrupt(cpu->hvf->fd, HV_INTERRUPT_TYPE_FIQ, true);
WARNING: line over 80 characters
#673: FILE: target/arm/hvf/hvf.c:529:
+ hv_vcpu_set_pending_interrupt(cpu->hvf->fd, HV_INTERRUPT_TYPE_IRQ, true);
ERROR: Hex numbers must be prefixed with '0x'
#855: FILE: target/arm/hvf/trace-events:6:
+hvf_sysreg_read(uint32_t reg, uint32_t op0, uint32_t op1, uint32_t crn, uint32_t crm, uint32_t op2, uint64_t val) "sysreg read 0x%08x (op0=%d op1=%d crn=%d crm=%d op2=%d) = %016"PRIx64
ERROR: Hex numbers must be prefixed with '0x'
#856: FILE: target/arm/hvf/trace-events:7:
+hvf_sysreg_write(uint32_t reg, uint32_t op0, uint32_t op1, uint32_t crn, uint32_t crm, uint32_t op2, uint64_t val) "sysreg write 0x%08x (op0=%d op1=%d crn=%d crm=%d op2=%d, val=%016"PRIx64")"
ERROR: Hex numbers must be prefixed with '0x' #857: FILE: target/arm/hvf/trace-events:8:
+hvf_unknown_hvf(uint64_t x0) "unknown HVC! %016"PRIx64
ERROR: Hex numbers must be prefixed with '0x' #858: FILE: target/arm/hvf/trace-events:9:
+hvf_unknown_smc(uint64_t x0) "unknown SMC! %016"PRIx64
total: 5 errors, 7 warnings, 792 lines checked
Patch 13/19 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
14/19 Checking commit 22268eef970e (arm/hvf: Add a WFI handler) 15/19 Checking commit d61ad072f45b (hvf: arm: Implement -cpu host)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #179:
new file mode 100644
total: 0 errors, 1 warnings, 160 lines checked
Patch 15/19 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
16/19 Checking commit e5c95615d9ef (hvf: arm: Implement PSCI handling)
WARNING: line over 80 characters
#92: FILE: target/arm/hvf/hvf.c:584:
+ trace_hvf_psci_call(param[0], param[1], param[2], param[3], arm_cpu->mp_affinity);
WARNING: Block comments use a leading /* on a separate line #123: FILE: target/arm/hvf/hvf.c:615:
+ /* QEMU reset and shutdown are async requests, but PSCI
ERROR: Hex numbers must be prefixed with '0x'
#214: FILE: target/arm/hvf/trace-events:11:
+hvf_psci_call(uint64_t x0, uint64_t x1, uint64_t x2, uint64_t x3, uint32_t cpuid) "PSCI Call x0=%016"PRIx64" x1=%016"PRIx64" x2=%016"PRIx64" x3=%016"PRIx64" cpu=%x"
total: 1 errors, 2 warnings, 171 lines checked
Patch 16/19 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
17/19 Checking commit 06e21844ac34 (arm: Add Hypervisor.framework build target)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #41:
new file mode 100644
total: 0 errors, 1 warnings, 23 lines checked
Patch 17/19 has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
18/19 Checking commit f23720b08e1c (arm: Enable Windows 10 trusted SMCCC boot call) 19/19 Checking commit 51b6048f8a96 (hvf: arm: Handle Windows 10 SMC call)
WARNING: line over 80 characters
#32: FILE: target/arm/hvf/hvf.c:932:
+ /* This special SMC is called by Windows 10 on boot. Return error. */
total: 0 errors, 1 warnings, 10 lines checked
Patch 19/19 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]