Skip to contentSkip to conversation

Add ARM64 support for MSHV accelerator

PATCH Superseded Aastha Rawat 4 participants 31 messages Apr 21 – May 4 Arm/Amlogic Meson SoC support

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add ARM64 support for MSHV accelerator

This series adds ARM64 guest support to the MSHV (Microsoft Hypervisor) accelerator, enabling QEMU to run aarch64 VMs on Microsoft's hypervisor using the mshv Linux kernel module.

The first few patches refactor the existing x86 MSHV code to separate arch-specific pieces from common infrastructure: moving MSR handling to target/i386, extracting shared register hypercall helpers, provisioning host CPU features, and introducing arch-specific init hooks.

The remaining patches add the ARM64 backend:
- vCPU state get/set using hypervisor register hypercalls
- -cpu host support by querying ID registers from the hypervisor
- vCPU run loop with MMIO emulation via ESR_EL2 syndrome decoding
- In-kernel vGICv3 backed by HVCALL_ASSERT_VIRTUAL_INTERRUPT
- Interrupt control structure adjustments for arm64

With this series, a standard aarch64 virt machine can be launched with:
qemu-system-aarch64 -accel mshv -cpu host -M virt ...

Caveats:
- Live migration is not yet supported.

---
Changes in v2:
- Removed PATCH: "accel/mshv: provision guests with the same features as host".
- Extracted common MMIO emulation logic for hvf, whpx and mshv to a
common place.
- Fixed checkpatch errors/warnings.
- Picked up reviewed-by tags.
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
Changes in v3:
- Handled GPA Intercept in vCPU run loop to fix EDK2 firmware boot.
- Updated the cover letter: removed caveat about EDK2 firmware boot as
it is now fixed.
- Link to v2: https://lore.kernel.org/qemu-devel/20260402-mshv_accel_arm64_supp-v2-0-754895c15e9e@linux.microsoft.com/

---
Aastha Rawat (8):
accel/mshv: move msr.c to target/i386
accel/mshv: extract common CPU register helpers
meson, target/arm/mshv: Enable arm64 build & add initial MSHV support target/arm/mshv: implement vcpu state operations for ARM64 target/arm/mshv: implement -cpu host for MSHV
accel/mshv: Add access_vp_regs synthetic proc features target/arm: cpu: Mark MSHV supporting PSCI 1.3 target/arm: extract MMIO emulation logic for HVF & WHPX

Anirudh Rayabharam (Microsoft) (5):
accel/mshv: add arch-specific accelerator init hook target/arm/mshv: add vCPU run loop
include/hw/hyperv: adjust hv_interrupt_control structure for arm64 hw/intc,target/arm/mshv: add MSHV vGICv3 implementation
MAINTAINERS: updates for MSHV arm64 code

Magnus Kulke (1):
accel/mshv: implement cpu_thread_is_idle() hook

 MAINTAINERS                        |   8 +
 accel/mshv/irq.c                   |   2 +
 accel/mshv/meson.build             |   4 +-
 accel/mshv/mshv-all.c              |  26 +-
 accel/mshv/mshv-cpu-common.c       | 151 ++++++++++
 hw/arm/virt.c                      |  11 +-
 hw/intc/arm_gicv3_common.c         |   3 +
 hw/intc/arm_gicv3_mshv.c           | 181 ++++++++++++
 hw/intc/meson.build                |   1 +
 include/hw/hyperv/hvgdk_mini.h     | 130 +++++++++
 include/hw/hyperv/hvhdk.h          | 101 ++++++-
 include/hw/hyperv/hvhdk_mini.h     |   6 +
 include/hw/intc/arm_gicv3_common.h |   1 +
 include/system/hw_accel.h          |   3 +-
 include/system/mshv.h              |   2 +
 include/system/mshv_int.h          |   5 +
 meson.build                        |   7 +-
 target/arm/cpu.c                   |   8 +-
 target/arm/cpu64.c                 |  24 +-
 target/arm/helper.c                |  60 ++++
 target/arm/helper.h                |   5 +
 target/arm/hvf/hvf.c               |  64 +++--
 target/arm/meson.build             |   1 +
 target/arm/mshv/meson.build        |   7 +
 target/arm/mshv/mshv-all.c         | 567 +++++++++++++++++++++++++++++++++++++
 target/arm/mshv_arm.h              |  18 ++
 target/arm/syndrome.h              |  37 +++
 target/arm/whpx/whpx-all.c         |  62 +---
 target/i386/mshv/meson.build       |   2 +
 target/i386/mshv/mshv-all.c        |  85 ++++++
 target/i386/mshv/mshv-cpu.c        | 170 +----------
 {accel => target/i386}/mshv/msr.c  |   0
 32 files changed, 1480 insertions(+), 272 deletions(-)

---
base-commit: f429c4276c8349f2cc94fb4e46a2a7918bec95c3
change-id: 20260311-mshv_accel_arm64_supp-e86b0082aee4

Best regards,

Signature · 1 line
Aastha Rawat <[email protected]>
#1 lore raw

PATCH v3 1/14 accel/mshv: move msr.c to target/i386

This file has x86 specific implementations. Move this file to i386 folder in preparation for ARM64 MSHV support.

---
 accel/mshv/meson.build            | 1 -
 target/i386/mshv/meson.build      | 1 +
 {accel => target/i386}/mshv/msr.c | 0
 3 files changed, 1 insertion(+), 1 deletion(-)
accel/mshv/meson.build +0 -1
index c1b1787c5e..e433187cde 100644
@@ -1,6 +1,5 @@
 system_ss.add(when: 'CONFIG_MSHV', if_true: files(
   'irq.c',
   'mem.c',
-  'msr.c',
   'mshv-all.c'
 ))
target/i386/mshv/meson.build +1 -0
index 49f28d4a5b..6091c21887 100644
@@ -2,6 +2,7 @@ i386_mshv_ss = ss.source_set()
 
 i386_mshv_ss.add(files(
   'mshv-cpu.c',
+  'msr.c',
 ))
 
 i386_system_ss.add_all(when: 'CONFIG_MSHV', if_true: i386_mshv_ss)
target/i386/mshv/msr.c renamed from accel/mshv/msr.c +0 -0
similarity index 100%
rename from accel/mshv/msr.c
rename to target/i386/mshv/msr.c
Signature · 1 line
2.45.4
Acked-by Reviewed-by Mohamed Mediouni on v3 1/14
#2 lore raw

Re: [PATCH v3 01/14] accel/mshv: move msr.c to target/i386

Quoted 6 lines · Aastha Rawat

This file has x86 specific implementations. Move this file to i386 folder in preparation for ARM64 MSHV support.

Normally these tags go in chronological order.

No need to resend. Just something to keep in mind for the future.

Wei

#3 lore raw

Re: [PATCH v3 01/14] accel/mshv: move msr.c to target/i386

Quoted 10 lines · Wei Liu
Quoted 6 lines · Aastha Rawat

This file has x86 specific implementations. Move this file to i386 folder in preparation for ARM64 MSHV support.

Normally these tags go in chronological order.

Quoted 7 lines

No need to resend. Just something to keep in mind for the future.

Wei

Got it, thanks!

Aastha

#4 lore raw

PATCH v3 2/14 accel/mshv: extract common CPU register helpers

In reply to Aastha Rawat

Move arch-independent MSHV CPU register handling functions into accel/mshv/mshv-cpu-common.c. Update x86 MSHV backends to use these shared helpers.

Prefix get_generic_regs() with mshv_ for proper namespacing since it is now public.

---
 accel/mshv/meson.build       |   3 +-
 accel/mshv/mshv-cpu-common.c | 151 +++++++++++++++++++++++++++++++++++++++++++
 include/system/mshv_int.h    |   3 +
 target/i386/mshv/mshv-cpu.c  | 130 +------------------------------------
 4 files changed, 159 insertions(+), 128 deletions(-)
accel/mshv/meson.build +2 -1
index e433187cde..ebe32921b3 100644
@@ -1,5 +1,6 @@
 system_ss.add(when: 'CONFIG_MSHV', if_true: files(
   'irq.c',
   'mem.c',
-  'mshv-all.c'
+  'mshv-all.c',
+  'mshv-cpu-common.c'
 ))
accel/mshv/mshv-cpu-common.c new file +151 -0

151 lines not on this page · raw message

include/system/mshv_int.h +3 -0
index 35386c422f..ff3ab957b5 100644
@@ -89,12 +89,15 @@ int mshv_load_regs(CPUState *cpu);
 int mshv_store_regs(CPUState *cpu);
 int mshv_set_generic_regs(const CPUState *cpu, const hv_register_assoc *assocs,
                           size_t n_regs);
+int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs,
+                        size_t n_regs);
 int mshv_arch_put_registers(const CPUState *cpu);
 void mshv_arch_init_vcpu(CPUState *cpu);
 void mshv_arch_destroy_vcpu(CPUState *cpu);
 void mshv_arch_amend_proc_features(
     union hv_partition_synthetic_processor_features *features);
 int mshv_arch_post_init_vm(int vm_fd);
+void mshv_setup_hvcall_args(AccelCPUState *state);
 
 typedef struct mshv_root_hvcall mshv_root_hvcall;
 int mshv_hvcall(int fd, const mshv_root_hvcall *args);
target/i386/mshv/mshv-cpu.c +3 -127

166 lines not on this page · raw message

Signature · 1 line
2.45.4
#5 lore raw

PATCH v3 3/14 meson, target/arm/mshv: Enable arm64 build & add initial MSHV support

In reply to Aastha Rawat

Enable the build of the MSHV accelerator for arm64 in the Meson build system. Introduce initial files and stub implementations to support MSHV for the ARM target.

---
 meson.build                 |  7 +++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++++
 target/arm/mshv/mshv-all.c  | 54 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 66 insertions(+), 3 deletions(-)
meson.build +4 -3
index ed82247111..bf12fda40f 100644
@@ -303,7 +303,8 @@ if cpu == 'aarch64'
   accelerator_targets += {
     'CONFIG_HVF': ['aarch64-softmmu'],
     'CONFIG_NITRO': ['aarch64-softmmu'],
-    'CONFIG_WHPX': ['aarch64-softmmu']
+    'CONFIG_WHPX': ['aarch64-softmmu'],
+    'CONFIG_MSHV': ['aarch64-softmmu'],
   }
 elif cpu == 'x86_64'
   accelerator_targets += {
@@ -855,8 +856,8 @@ if get_option('kvm').allowed() and host_os == 'linux'
 endif
 
 if get_option('mshv').allowed() and host_os == 'linux'
-  if get_option('mshv').enabled() and host_machine.cpu() != 'x86_64'
-    error('mshv accelerator requires x64_64 host')
+  if get_option('mshv').enabled() and not (host_machine.cpu() in [ 'x86_64', 'aarch64' ])
+    error('The mshv accelerator requires an x86_64 or aarch64 host')
   endif
   accelerators += 'CONFIG_MSHV'
 endif
target/arm/meson.build +1 -0
index 6e0e504a40..8aa8cac137 100644
@@ -59,6 +59,7 @@ arm_common_system_ss.add(files(
 
 subdir('hvf')
 subdir('whpx')
+subdir('mshv')
 
 if 'CONFIG_TCG' in config_all_accel
    subdir('tcg')
target/arm/mshv/meson.build new file +7 -0
new file mode 100644
index 0000000000..169643691e
@@ -0,0 +1,7 @@
+arm_mshv_ss = ss.source_set()
+
+arm_mshv_ss.add(files(
+  'mshv-all.c',
+))
+
+arm_system_ss.add_all(when: 'CONFIG_MSHV', if_true: arm_mshv_ss)
target/arm/mshv/mshv-all.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.45.4
#6 lore raw

Re: [PATCH v3 03/14] meson, target/arm/mshv: Enable arm64 build & add initial MSHV support

meson.build if get_option('kvm').allowed() and host_os == 'linux' · line 863

Quoted 11 lines · Aastha Rawat
meson.build +2 -2
@@ -855,8 +856,8 @@ if get_option('kvm').allowed() and host_os == 'linux'
  endif
 
  if get_option('mshv').allowed() and host_os == 'linux'
-  if get_option('mshv').enabled() and host_machine.cpu() != 'x86_64'
-    error('mshv accelerator requires x64_64 host')
+  if get_option('mshv').enabled() and not (host_machine.cpu() in [ 'x86_64', 'aarch64' ])
+    error('The mshv accelerator requires an x86_64 or aarch64 host')
    endif
    accelerators += 'CONFIG_MSHV'
  endif

This error should be handled by

if 'CONFIG_MSHV' not in accelerators and get_option('mshv').enabled()
error('mshv not available on this platform')
endif

later in meson.build.

r~

#7 lore raw

Re: [PATCH v3 03/14] meson, target/arm/mshv: Enable arm64 build & add initial MSHV support

Quoted 14 lines · Richard Henderson

meson.build if get_option('kvm').allowed() and host_os == 'linux' · line 862

Quoted 10 lines · Aastha Rawat
meson.build +2 -2
@@ -855,8 +856,8 @@ if get_option('kvm').allowed() and host_os == 'linux'
  endif
  if get_option('mshv').allowed() and host_os == 'linux'
-  if get_option('mshv').enabled() and host_machine.cpu() != 'x86_64'
-    error('mshv accelerator requires x64_64 host')
+  if get_option('mshv').enabled() and not (host_machine.cpu() in [ 'x86_64', 'aarch64' ])
+    error('The mshv accelerator requires an x86_64 or aarch64 host')
    endif
    accelerators += 'CONFIG_MSHV'
  endif

This error should be handled by

Quoted 8 lines

if 'CONFIG_MSHV' not in accelerators and get_option('mshv').enabled()
error('mshv not available on this platform')
endif

later in meson.build.

r~

Thanks for the review. I'll remove the explicit error and fold the CPU check into the outer condition.

if 'CONFIG_MSHV' not in accelerators and get_option('mshv').enabled()
error('mshv not available on this platform')
endif

This check is already handling the error.

Thanks,
Aastha

#8 lore raw

Re: [PATCH v3 03/14] meson, target/arm/mshv: Enable arm64 build & add initial MSHV support

In reply to Aastha Rawat
Quoted 9 lines · Aastha Rawat
target/arm/mshv/mshv-all.c new file +3 -0
new file mode 100644
index 0000000000..1c82e2c593
@@ -0,0 +1,54 @@
+/*
+ * QEMU MSHV support
+ *

target/arm/mshv/mshv-all.c line 13

Quoted 10 lines
target/arm/mshv/mshv-all.c new file +10 -0
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat<[email protected]>
+ *          Anirudh Rayabharam<[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "system/mshv.h"
+#include "system/mshv_int.h"

#include "qemu/osdep.h"

is always first, for all .c files.

r~

#9 lore raw

Re: [PATCH v3 03/14] meson, target/arm/mshv: Enable arm64 build & add initial MSHV support

Quoted 23 lines · Richard Henderson
Quoted 9 lines · Aastha Rawat
target/arm/mshv/mshv-all.c new file +3 -0
new file mode 100644
index 0000000000..1c82e2c593
@@ -0,0 +1,54 @@
+/*
+ * QEMU MSHV support
+ *

target/arm/mshv/mshv-all.c line 13

Quoted 10 lines
target/arm/mshv/mshv-all.c new file +10 -0
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat<[email protected]>
+ *          Anirudh Rayabharam<[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "system/mshv.h"
+#include "system/mshv_int.h"

#include "qemu/osdep.h"

Quoted 5 lines

is always first, for all .c files.

r~

Thanks, will add the missing #include "qemu/osdep.h" as the first include. Will be fixed in v4.

Aastha

#10 lore raw

PATCH v3 4/14 target/arm/mshv: implement vcpu state operations for ARM64

In reply to Aastha Rawat

Add support for reading and writing ARM64 CPU registers in the MSHV accelerator. This includes functions to set and get registers, initialize and destroy VCPU state, and manage register state synchronization between QEMU and hypervisor.

---
 include/hw/hyperv/hvgdk_mini.h |  42 +++++++++++++
 target/arm/mshv/mshv-all.c     | 138 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 180 insertions(+)
include/hw/hyperv/hvgdk_mini.h +42 -0
index cb52cc9de2..dfe94050f4 100644
@@ -13,6 +13,46 @@ typedef enum hv_register_name {
     /* Pending Interruption Register */
     HV_REGISTER_PENDING_INTERRUPTION = 0x00010002,
 
+#if defined(__aarch64__)
+    HV_ARM64_REGISTER_XZR   = 0x0002FFFE,
+    HV_ARM64_REGISTER_X0    = 0x00020000,
+    HV_ARM64_REGISTER_X1    = 0x00020001,
+    HV_ARM64_REGISTER_X2    = 0x00020002,
+    HV_ARM64_REGISTER_X3    = 0x00020003,
+    HV_ARM64_REGISTER_X4    = 0x00020004,
+    HV_ARM64_REGISTER_X5    = 0x00020005,
+    HV_ARM64_REGISTER_X6    = 0x00020006,
+    HV_ARM64_REGISTER_X7    = 0x00020007,
+    HV_ARM64_REGISTER_X8    = 0x00020008,
+    HV_ARM64_REGISTER_X9    = 0x00020009,
+    HV_ARM64_REGISTER_X10   = 0x0002000A,
+    HV_ARM64_REGISTER_X11   = 0x0002000B,
+    HV_ARM64_REGISTER_X12   = 0x0002000C,
+    HV_ARM64_REGISTER_X13   = 0x0002000D,
+    HV_ARM64_REGISTER_X14   = 0x0002000E,
+    HV_ARM64_REGISTER_X15   = 0x0002000F,
+    HV_ARM64_REGISTER_X16   = 0x00020010,
+    HV_ARM64_REGISTER_X17   = 0x00020011,
+    HV_ARM64_REGISTER_X18   = 0x00020012,
+    HV_ARM64_REGISTER_X19   = 0x00020013,
+    HV_ARM64_REGISTER_X20   = 0x00020014,
+    HV_ARM64_REGISTER_X21   = 0x00020015,
+    HV_ARM64_REGISTER_X22   = 0x00020016,
+    HV_ARM64_REGISTER_X23   = 0x00020017,
+    HV_ARM64_REGISTER_X24   = 0x00020018,
+    HV_ARM64_REGISTER_X25   = 0x00020019,
+    HV_ARM64_REGISTER_X26   = 0x0002001A,
+    HV_ARM64_REGISTER_X27   = 0x0002001B,
+    HV_ARM64_REGISTER_X28   = 0x0002001C,
+    HV_ARM64_REGISTER_FP    = 0x0002001D,
+    HV_ARM64_REGISTER_LR    = 0x0002001E,
+    HV_ARM64_REGISTER_PC    = 0x00020022,
+
+    /* AArch64 System Register Descriptions: General system control registers */
+    HV_ARM64_REGISTER_MIDR_EL1   = 0x00040051,
+    HV_ARM64_REGISTER_MPIDR_EL1  = 0x00040001,
+
+#elif defined(__x86_64__)
     /* X64 User-Mode Registers */
     HV_X64_REGISTER_RAX     = 0x00020000,
     HV_X64_REGISTER_RCX     = 0x00020001,
@@ -157,6 +197,8 @@ typedef enum hv_register_name {
     /* Other MSRs */
     HV_X64_REGISTER_MSR_IA32_MISC_ENABLE = 0x000800A0,
 
+#endif
+
     /* Misc */
     HV_REGISTER_GUEST_OS_ID         = 0x00090002,
     HV_REGISTER_REFERENCE_TSC       = 0x00090017,
target/arm/mshv/mshv-all.c +138 -0

166 lines not on this page · raw message

Signature · 1 line
2.45.4
#11 lore raw

Re: [PATCH v3 04/14] target/arm/mshv: implement vcpu state operations for ARM64

Quoted 16 lines · Aastha Rawat

Add support for reading and writing ARM64 CPU registers in the MSHV accelerator. This includes functions to set and get registers, initialize and destroy VCPU state, and manage register state synchronization between QEMU and hypervisor.

---
  include/hw/hyperv/hvgdk_mini.h |  42 +++++++++++++
  target/arm/mshv/mshv-all.c     | 138 +++++++++++++++++++++++++++++++++++++++++
  2 files changed, 180 insertions(+)
include/hw/hyperv/hvgdk_mini.h +0 -0
index cb52cc9de2..dfe94050f4 100644

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name { · line 16

Quoted 4 lines
include/hw/hyperv/hvgdk_mini.h +1 -0
@@ -13,6 +13,46 @@ typedef enum hv_register_name {
      /* Pending Interruption Register */
      HV_REGISTER_PENDING_INTERRUPTION = 0x00010002,
 
+#if defined(__aarch64__)

Not ideal, having target/host-specific stuff in a global header. That said, you don't actually need the ifdefs at all.

Quoted 31 lines
include/hw/hyperv/hvgdk_mini.h +31 -0
+    HV_ARM64_REGISTER_XZR   = 0x0002FFFE,
+    HV_ARM64_REGISTER_X0    = 0x00020000,
+    HV_ARM64_REGISTER_X1    = 0x00020001,
+    HV_ARM64_REGISTER_X2    = 0x00020002,
+    HV_ARM64_REGISTER_X3    = 0x00020003,
+    HV_ARM64_REGISTER_X4    = 0x00020004,
+    HV_ARM64_REGISTER_X5    = 0x00020005,
+    HV_ARM64_REGISTER_X6    = 0x00020006,
+    HV_ARM64_REGISTER_X7    = 0x00020007,
+    HV_ARM64_REGISTER_X8    = 0x00020008,
+    HV_ARM64_REGISTER_X9    = 0x00020009,
+    HV_ARM64_REGISTER_X10   = 0x0002000A,
+    HV_ARM64_REGISTER_X11   = 0x0002000B,
+    HV_ARM64_REGISTER_X12   = 0x0002000C,
+    HV_ARM64_REGISTER_X13   = 0x0002000D,
+    HV_ARM64_REGISTER_X14   = 0x0002000E,
+    HV_ARM64_REGISTER_X15   = 0x0002000F,
+    HV_ARM64_REGISTER_X16   = 0x00020010,
+    HV_ARM64_REGISTER_X17   = 0x00020011,
+    HV_ARM64_REGISTER_X18   = 0x00020012,
+    HV_ARM64_REGISTER_X19   = 0x00020013,
+    HV_ARM64_REGISTER_X20   = 0x00020014,
+    HV_ARM64_REGISTER_X21   = 0x00020015,
+    HV_ARM64_REGISTER_X22   = 0x00020016,
+    HV_ARM64_REGISTER_X23   = 0x00020017,
+    HV_ARM64_REGISTER_X24   = 0x00020018,
+    HV_ARM64_REGISTER_X25   = 0x00020019,
+    HV_ARM64_REGISTER_X26   = 0x0002001A,
+    HV_ARM64_REGISTER_X27   = 0x0002001B,
+    HV_ARM64_REGISTER_X28   = 0x0002001C,
+    HV_ARM64_REGISTER_FP    = 0x0002001D,

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name {

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +9 -0
+    HV_ARM64_REGISTER_LR    = 0x0002001E,
+    HV_ARM64_REGISTER_PC    = 0x00020022,
+
+    /* AArch64 System Register Descriptions: General system control registers */
+    HV_ARM64_REGISTER_MIDR_EL1   = 0x00040051,
+    HV_ARM64_REGISTER_MPIDR_EL1  = 0x00040001,
+
+#elif defined(__x86_64__)
 
+static enum hv_register_name STANDARD_REGISTER_NAMES[32] = {

const.

Quoted 22 lines
include/hw/hyperv/hvgdk_mini.h +22 -0
+    HV_ARM64_REGISTER_X0,
+    HV_ARM64_REGISTER_X1,
+    HV_ARM64_REGISTER_X2,
+    HV_ARM64_REGISTER_X3,
+    HV_ARM64_REGISTER_X4,
+    HV_ARM64_REGISTER_X5,
+    HV_ARM64_REGISTER_X6,
+    HV_ARM64_REGISTER_X7,
+    HV_ARM64_REGISTER_X8,
+    HV_ARM64_REGISTER_X9,
+    HV_ARM64_REGISTER_X10,
+    HV_ARM64_REGISTER_X11,
+    HV_ARM64_REGISTER_X12,
+    HV_ARM64_REGISTER_X13,
+    HV_ARM64_REGISTER_X14,
+    HV_ARM64_REGISTER_X15,
+    HV_ARM64_REGISTER_X16,
+    HV_ARM64_REGISTER_X17,
+    HV_ARM64_REGISTER_X18,
+    HV_ARM64_REGISTER_X19,
+    HV_ARM64_REGISTER_X20,
+    HV_ARM64_REGISTER_X21,

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name {

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +10 -0
+    HV_ARM64_REGISTER_X22,
+    HV_ARM64_REGISTER_X23,
+    HV_ARM64_REGISTER_X24,
+    HV_ARM64_REGISTER_X25,
+    HV_ARM64_REGISTER_X26,
+    HV_ARM64_REGISTER_X27,
+    HV_ARM64_REGISTER_X28,
+    HV_ARM64_REGISTER_FP,
+    HV_ARM64_REGISTER_LR,
+    HV_ARM64_REGISTER_PC,

Why are you placing PC at name[31], given that you have to special case it everywhere else below?

include/hw/hyperv/hvgdk_mini.h

Quoted 9 lines
include/hw/hyperv/hvgdk_mini.h +9 -0
+static int set_standard_regs(const CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc *assocs;
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    assocs = g_new0(hv_register_assoc, n_regs);

This is small enough to allocate in the local stack frame. Same for get_standard_regs.

include/hw/hyperv/hvgdk_mini.h

Quoted 12 lines
include/hw/hyperv/hvgdk_mini.h +12 -0
+static void populate_standard_regs(const hv_register_assoc *assocs,
+                                   CPUARMState *env)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        env->xregs[i] = assocs[i].value.reg64;
+    }
+
+    /* Last register is the program counter */
+    env->pc = assocs[n_regs - 1].value.reg64;
+}

Why is this separate from get_standard_regs? You don't have a similar split for set_standard_regs...

Quoted 1 line

+int mshv_get_standard_regs(CPUState *cpu)

I'll note that this isn't used outside of this file, and similarly for i386/mshv. It doesn't need to be declared globally at all.

r~

#12 lore raw

Re: [PATCH v3 04/14] target/arm/mshv: implement vcpu state operations for ARM64

Quoted 12 lines · Richard Henderson
Quoted 11 lines · Aastha Rawat

Add support for reading and writing ARM64 CPU registers in the MSHV accelerator. This includes functions to set and get registers, initialize and destroy VCPU state, and manage register state synchronization between QEMU and hypervisor.

---
  include/hw/hyperv/hvgdk_mini.h |  42 +++++++++++++
  target/arm/mshv/mshv-all.c     | 138 +++++++++++++++++++++++++++++++++++++++++
  2 files changed, 180 insertions(+)
Quoted 11 lines

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name { · line 15

Quoted 8 lines
include/hw/hyperv/hvgdk_mini.h +1 -0
index cb52cc9de2..dfe94050f4 100644
@@ -13,6 +13,46 @@ typedef enum hv_register_name {
      /* Pending Interruption Register */
      HV_REGISTER_PENDING_INTERRUPTION = 0x00010002,
+#if defined(__aarch64__)

Not ideal, having target/host-specific stuff in a global header. That said, you don't actually need the ifdefs at all.

These header definitions are imported directly from the hypervisor headers to maintain parity with MSHV interface. We need to maintain this layout.

Quoted 4 lines

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name { · line 16

Quoted 1 line
include/hw/hyperv/hvgdk_mini.h +1 -0
+static enum hv_register_name STANDARD_REGISTER_NAMES[32] = {

const.

Will fix.

Quoted 33 lines
Quoted 22 lines
include/hw/hyperv/hvgdk_mini.h +22 -0
+    HV_ARM64_REGISTER_X0,
+    HV_ARM64_REGISTER_X1,
+    HV_ARM64_REGISTER_X2,
+    HV_ARM64_REGISTER_X3,
+    HV_ARM64_REGISTER_X4,
+    HV_ARM64_REGISTER_X5,
+    HV_ARM64_REGISTER_X6,
+    HV_ARM64_REGISTER_X7,
+    HV_ARM64_REGISTER_X8,
+    HV_ARM64_REGISTER_X9,
+    HV_ARM64_REGISTER_X10,
+    HV_ARM64_REGISTER_X11,
+    HV_ARM64_REGISTER_X12,
+    HV_ARM64_REGISTER_X13,
+    HV_ARM64_REGISTER_X14,
+    HV_ARM64_REGISTER_X15,
+    HV_ARM64_REGISTER_X16,
+    HV_ARM64_REGISTER_X17,
+    HV_ARM64_REGISTER_X18,
+    HV_ARM64_REGISTER_X19,
+    HV_ARM64_REGISTER_X20,
+    HV_ARM64_REGISTER_X21,

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name { · line 48

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +10 -0
+    HV_ARM64_REGISTER_X22,
+    HV_ARM64_REGISTER_X23,
+    HV_ARM64_REGISTER_X24,
+    HV_ARM64_REGISTER_X25,
+    HV_ARM64_REGISTER_X26,
+    HV_ARM64_REGISTER_X27,
+    HV_ARM64_REGISTER_X28,
+    HV_ARM64_REGISTER_FP,
+    HV_ARM64_REGISTER_LR,
+    HV_ARM64_REGISTER_PC,
Quoted 2 lines

Why are you placing PC at name[31], given that you have to special case it everywhere else below?

Again, this is same as the register layout used by the hypervisor.

Quoted 4 lines
Quoted 4 lines
include/hw/hyperv/hvgdk_mini.h +4 -0
+static int set_standard_regs(const CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc *assocs;
Quoted 8 lines

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name { · line 57

Quoted 5 lines
include/hw/hyperv/hvgdk_mini.h +5 -0
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    assocs = g_new0(hv_register_assoc, n_regs);

This is small enough to allocate in the local stack frame. Same for get_standard_regs.

Will switch to stack allocation for both.

Quoted 7 lines
Quoted 7 lines
include/hw/hyperv/hvgdk_mini.h +7 -0
+static void populate_standard_regs(const hv_register_assoc *assocs,
+                                   CPUARMState *env)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        env->xregs[i] = assocs[i].value.reg64;
Quoted 8 lines

include/hw/hyperv/hvgdk_mini.h

Quoted 5 lines
include/hw/hyperv/hvgdk_mini.h +5 -0
+    }
+
+    /* Last register is the program counter */
+    env->pc = assocs[n_regs - 1].value.reg64;
+}

Why is this separate from get_standard_regs? You don't have a similar split for set_standard_regs...

Right. Will inline populate_standard_regs into get_standard_regs.

Quoted 4 lines
Quoted 1 line

+int mshv_get_standard_regs(CPUState *cpu)

I'll note that this isn't used outside of this file, and similarly for i386/mshv. It doesn't need to be declared globally at all.

Sure, will make it static and drop declaration from mshv_int.h.

Thanks,
Aastha

#13 lore raw

PATCH v3 5/14 target/arm/mshv: implement -cpu host for MSHV

In reply to Aastha Rawat

This commit enables support for the -cpu host option with MSHV accelerator on ARM64. The implementation queries the partition's CPU's ID registers and features via hypervisor interface, allowing to pass all CPU capabilities for the host CPU to the guest.

Replace preprocessor config checks with runtime calls to mshv_enabled(), kvm_enabled(), etc. This ensures that the correct accelerator is selected at runtime when both MSHV and KVM are enabled.

---
 accel/mshv/mshv-all.c          |   3 +-
 hw/arm/virt.c                  |   3 +-
 include/hw/hyperv/hvgdk_mini.h |  17 ++++
 include/hw/hyperv/hvhdk.h      |  10 ++
 include/system/hw_accel.h      |   3 +-
 target/arm/cpu.c               |   6 +-
 target/arm/cpu64.c             |  24 +++--
 target/arm/mshv/mshv-all.c     | 204 +++++++++++++++++++++++++++++++++++++++++
 target/arm/mshv_arm.h          |  18 ++++
 9 files changed, 273 insertions(+), 15 deletions(-)
accel/mshv/mshv-all.c +2 -1
index d4cc7f5371..44f35a1463 100644
@@ -400,13 +400,14 @@ static int mshv_init_vcpu(CPUState *cpu)
     int ret;
 
     cpu->accel = g_new0(AccelCPUState, 1);
-    mshv_arch_init_vcpu(cpu);
 
     ret = mshv_create_vcpu(vm_fd, vp_index, &cpu->accel->cpufd);
     if (ret < 0) {
         return -1;
     }
 
+    mshv_arch_init_vcpu(cpu);
+
     cpu->accel->dirty = true;
 
     return 0;
hw/arm/virt.c +2 -1
index 7456614d05..34eb5248a9 100644
@@ -50,6 +50,7 @@
 #include "system/kvm.h"
 #include "system/hvf.h"
 #include "system/whpx.h"
+#include "system/mshv.h"
 #include "system/qtest.h"
 #include "system/system.h"
 #include "hw/core/loader.h"
@@ -3450,7 +3451,7 @@ static GPtrArray *virt_get_valid_cpu_types(const MachineState *ms)
     if (target_aarch64()) {
         g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("cortex-a53")));
         g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("cortex-a57")));
-        if (kvm_enabled() || hvf_enabled()) {
+        if (kvm_enabled() || hvf_enabled() || mshv_enabled()) {
             g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("host")));
         }
     }
include/hw/hyperv/hvgdk_mini.h +17 -0
index dfe94050f4..d56be0d70f 100644
@@ -48,6 +48,20 @@ typedef enum hv_register_name {
     HV_ARM64_REGISTER_LR    = 0x0002001E,
     HV_ARM64_REGISTER_PC    = 0x00020022,
 
+    /* AArch64 System Register Descriptions: ID Registers */
+    HV_ARM64_REGISTER_ID_MIDR_EL1         = 0x00022000,
+    HV_ARM64_REGISTER_ID_MPIDR_EL1        = 0x00022005,
+    HV_ARM64_REGISTER_ID_AA64_PFR0_EL1    = 0x00022020,
+    HV_ARM64_REGISTER_ID_AA64_PFR1_EL1    = 0x00022021,
+    HV_ARM64_REGISTER_ID_AA64_ISAR0_EL1   = 0x00022030,
+    HV_ARM64_REGISTER_ID_AA64_ISAR1_EL1   = 0x00022031,
+    HV_ARM64_REGISTER_ID_AA64_ISAR2_EL1   = 0x00022032,
+    HV_ARM64_REGISTER_ID_AA64_MMFR0_EL1   = 0x00022038,
+    HV_ARM64_REGISTER_ID_AA64_MMFR1_EL1   = 0x00022039,
+    HV_ARM64_REGISTER_ID_AA64_MMFR2_EL1   = 0x0002203a,
+    HV_ARM64_REGISTER_ID_AA64_DFR0_EL1    = 0x00022028,
+    HV_ARM64_REGISTER_ID_AA64_DFR1_EL1    = 0x00022029,
+
     /* AArch64 System Register Descriptions: General system control registers */
     HV_ARM64_REGISTER_MIDR_EL1   = 0x00040051,
     HV_ARM64_REGISTER_MPIDR_EL1  = 0x00040001,
@@ -841,6 +855,9 @@ struct hv_cpuid {
 #define HV_HYP_PAGE_SIZE        BIT(HV_HYP_PAGE_SHIFT)
 #define HV_HYP_PAGE_MASK        (~(HV_HYP_PAGE_SIZE - 1))
 
+#define HV_ANY_VP               ((uint32_t)-1)
+#define HV_VTL_ALL              0xF
+
 #define HVCALL_GET_PARTITION_PROPERTY    0x0044
 #define HVCALL_SET_PARTITION_PROPERTY    0x0045
 #define HVCALL_GET_VP_REGISTERS          0x0050
include/hw/hyperv/hvhdk.h +10 -0
index 866c8211bf..2e1ef80972 100644
@@ -18,6 +18,16 @@ struct hv_input_set_partition_property {
     uint64_t property_value;
 };
 
+struct hv_input_get_partition_property {
+    uint64_t partition_id;
+    uint32_t property_code; /* enum hv_partition_property_code */
+    uint32_t padding;
+};
+
+struct hv_output_get_partition_property {
+    uint64_t property_value;
+};
+
 union hv_partition_synthetic_processor_features {
     uint64_t as_uint64[HV_PARTITION_SYNTHETIC_PROCESSOR_FEATURES_BANKS];
 
include/system/hw_accel.h +2 -1
index f0c10b6d80..614ea60be3 100644
@@ -51,7 +51,8 @@ static inline bool hwaccel_enabled(void)
     return hvf_enabled()
         || kvm_enabled()
         || nvmm_enabled()
-        || whpx_enabled();
+        || whpx_enabled()
+        || mshv_enabled();
 }
 
 #endif /* QEMU_HW_ACCEL_H */
target/arm/cpu.c +4 -2
index 7e3e84b4bb..8aa22747ff 100644
@@ -46,6 +46,7 @@
 #include "system/tcg.h"
 #include "system/qtest.h"
 #include "system/hw_accel.h"
+#include "system/mshv.h"
 #include "kvm_arm.h"
 #include "disas/capstone.h"
 #include "fpu/softfloat.h"
@@ -1629,8 +1630,9 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
      * this is the first point where we can report it.
      */
     if (cpu->host_cpu_probe_failed) {
-        if (!kvm_enabled() && !hvf_enabled()) {
-            error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
+        if (!kvm_enabled() && !hvf_enabled() && !mshv_enabled()) {
+            error_setg(errp,
+                       "The 'host' CPU type can only be used with KVM, HVF, or MSHV");
         } else {
             error_setg(errp, "Failed to retrieve host CPU features");
         }
target/arm/cpu64.c +14 -10
index d6feba220e..59cb23dd99 100644
@@ -26,12 +26,14 @@
 #include "qemu/units.h"
 #include "system/kvm.h"
 #include "system/hvf.h"
+#include "system/mshv.h"
 #include "system/whpx.h"
 #include "system/hw_accel.h"
 #include "system/qtest.h"
 #include "system/tcg.h"
 #include "kvm_arm.h"
 #include "hvf_arm.h"
+#include "mshv_arm.h"
 #include "whpx_arm.h"
 #include "qapi/visitor.h"
 #include "hw/core/qdev-properties.h"
@@ -821,16 +823,18 @@ static void aarch64_host_initfn(Object *obj)
     }
 #endif
 
-#if defined(CONFIG_KVM)
-    kvm_arm_set_cpu_features_from_host(cpu);
-    aarch64_add_sve_properties(obj);
-#elif defined(CONFIG_HVF)
-    hvf_arm_set_cpu_features_from_host(cpu);
-#elif defined(CONFIG_WHPX)
-    whpx_arm_set_cpu_features_from_host(cpu);
-#else
-    g_assert_not_reached();
-#endif
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);
+    } else if (kvm_enabled()) {
+        kvm_arm_set_cpu_features_from_host(cpu);
+        aarch64_add_sve_properties(obj);
+    } else if (hvf_enabled()) {
+        hvf_arm_set_cpu_features_from_host(cpu);
+    } else if (whpx_enabled()) {
+        whpx_arm_set_cpu_features_from_host(cpu);
+    } else {
+        g_assert_not_reached();
+    }
     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
         aarch64_add_pauth_properties(obj);
     }
target/arm/mshv/mshv-all.c +204 -0

217 lines not on this page · raw message

target/arm/mshv_arm.h new file +18 -0
new file mode 100644
index 0000000000..a03f9a4405
@@ -0,0 +1,18 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat   <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef QEMU_MSHV_ARM_H
+#define QEMU_MSHV_ARM_H
+
+#include "target/arm/cpu.h"
+
+void mshv_arm_set_cpu_features_from_host(ARMCPU *cpu);
+
+#endif
Signature · 1 line
2.45.4
#14 lore raw

Re: [PATCH v3 05/14] target/arm/mshv: implement -cpu host for MSHV

Quoted 74 lines · Aastha Rawat

This commit enables support for the -cpu host option with MSHV accelerator on ARM64. The implementation queries the partition's CPU's ID registers and features via hypervisor interface, allowing to pass all CPU capabilities for the host CPU to the guest.

Replace preprocessor config checks with runtime calls to mshv_enabled(), kvm_enabled(), etc. This ensures that the correct accelerator is selected at runtime when both MSHV and KVM are enabled.

---
  accel/mshv/mshv-all.c          |   3 +-
  hw/arm/virt.c                  |   3 +-
  include/hw/hyperv/hvgdk_mini.h |  17 ++++
  include/hw/hyperv/hvhdk.h      |  10 ++
  include/system/hw_accel.h      |   3 +-
  target/arm/cpu.c               |   6 +-
  target/arm/cpu64.c             |  24 +++--
  target/arm/mshv/mshv-all.c     | 204 +++++++++++++++++++++++++++++++++++++++++
  target/arm/mshv_arm.h          |  18 ++++
  9 files changed, 273 insertions(+), 15 deletions(-)
accel/mshv/mshv-all.c +2 -1
index d4cc7f5371..44f35a1463 100644
@@ -400,13 +400,14 @@ static int mshv_init_vcpu(CPUState *cpu)
      int ret;
 
      cpu->accel = g_new0(AccelCPUState, 1);
-    mshv_arch_init_vcpu(cpu);
 
      ret = mshv_create_vcpu(vm_fd, vp_index, &cpu->accel->cpufd);
      if (ret < 0) {
          return -1;
      }
 
+    mshv_arch_init_vcpu(cpu);
+
      cpu->accel->dirty = true;
 
      return 0;
hw/arm/virt.c +2 -1
index 7456614d05..34eb5248a9 100644
@@ -50,6 +50,7 @@
  #include "system/kvm.h"
  #include "system/hvf.h"
  #include "system/whpx.h"
+#include "system/mshv.h"
  #include "system/qtest.h"
  #include "system/system.h"
  #include "hw/core/loader.h"
@@ -3450,7 +3451,7 @@ static GPtrArray *virt_get_valid_cpu_types(const MachineState *ms)
      if (target_aarch64()) {
          g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("cortex-a53")));
          g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("cortex-a57")));
-        if (kvm_enabled() || hvf_enabled()) {
+        if (kvm_enabled() || hvf_enabled() || mshv_enabled()) {
              g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("host")));
          }
      }
include/hw/hyperv/hvgdk_mini.h +3 -0
index dfe94050f4..d56be0d70f 100644
@@ -48,6 +48,20 @@ typedef enum hv_register_name {
      HV_ARM64_REGISTER_LR    = 0x0002001E,
      HV_ARM64_REGISTER_PC    = 0x00020022,
 
+    /* AArch64 System Register Descriptions: ID Registers */
+    HV_ARM64_REGISTER_ID_MIDR_EL1         = 0x00022000,
+    HV_ARM64_REGISTER_ID_MPIDR_EL1        = 0x00022005,

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name { · line 63

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +10 -0
+    HV_ARM64_REGISTER_ID_AA64_PFR0_EL1    = 0x00022020,
+    HV_ARM64_REGISTER_ID_AA64_PFR1_EL1    = 0x00022021,
+    HV_ARM64_REGISTER_ID_AA64_ISAR0_EL1   = 0x00022030,
+    HV_ARM64_REGISTER_ID_AA64_ISAR1_EL1   = 0x00022031,
+    HV_ARM64_REGISTER_ID_AA64_ISAR2_EL1   = 0x00022032,
+    HV_ARM64_REGISTER_ID_AA64_MMFR0_EL1   = 0x00022038,
+    HV_ARM64_REGISTER_ID_AA64_MMFR1_EL1   = 0x00022039,
+    HV_ARM64_REGISTER_ID_AA64_MMFR2_EL1   = 0x0002203a,
+    HV_ARM64_REGISTER_ID_AA64_DFR0_EL1    = 0x00022028,
+    HV_ARM64_REGISTER_ID_AA64_DFR1_EL1    = 0x00022029,

Please use the actual name of the register and not add the extra _ after AA64.

Quoted 12 lines
include/hw/hyperv/hvgdk_mini.h +2 -10
-#if defined(CONFIG_KVM)
-    kvm_arm_set_cpu_features_from_host(cpu);
-    aarch64_add_sve_properties(obj);
-#elif defined(CONFIG_HVF)
-    hvf_arm_set_cpu_features_from_host(cpu);
-#elif defined(CONFIG_WHPX)
-    whpx_arm_set_cpu_features_from_host(cpu);
-#else
-    g_assert_not_reached();
-#endif
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name {

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +10 -0
+    } else if (kvm_enabled()) {
+        kvm_arm_set_cpu_features_from_host(cpu);
+        aarch64_add_sve_properties(obj);
+    } else if (hvf_enabled()) {
+        hvf_arm_set_cpu_features_from_host(cpu);
+    } else if (whpx_enabled()) {
+        whpx_arm_set_cpu_features_from_host(cpu);
+    } else {
+        g_assert_not_reached();
+    }

If this is because both mshv and kvm can be enabled simultaneously, then this should be a separate patch to transform the ifdef ladder.

include/hw/hyperv/hvgdk_mini.h

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +10 -0
+static void clamp_id_aa64mmfr0_parange_to_ipa_size(int mshv_fd,
+                                                   ARMISARegisters *isar)
+{
+    uint32_t ipa_size = mshv_arm_get_ipa_bit_size(mshv_fd);
+    uint64_t id_aa64mmfr0;
+
+    /* Clamp down the PARange to the IPA size the kernel supports. */
+    uint8_t index = round_down_to_parange_index(ipa_size);
+    id_aa64mmfr0 = GET_IDREG(isar, ID_AA64MMFR0);
+    id_aa64mmfr0 = (id_aa64mmfr0 & ~R_ID_AA64MMFR0_PARANGE_MASK) | index;

id_aa64_mmfr0 = FIELD_DPREG64(id_aa64mmfr0, AA64MMFR0, PARANGE, index);

include/hw/hyperv/hvgdk_mini.h

Quoted 3 lines
include/hw/hyperv/hvgdk_mini.h +3 -0
+    args.in_ptr = (uintptr_t)(in_buffer);
+    args.out_sz = values_sz;
+    args.out_ptr = (uintptr_t)(out_buffer);

Drop unnecessary ().

Quoted 1 line

+ args.reps = (uint16_t) n_regs;

Unnecessary cast.

include/hw/hyperv/hvgdk_mini.h

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +10 -0
+static bool mshv_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
+{
+    int mshv_fd = mshv_state->fd;
+    int vm_fd = mshv_state->vm;
+    int i, ret;
+    bool success = true;
+    uint64_t pfr0, pfr1;
+    gchar *contents = NULL;
+
+    const struct {

static

include/hw/hyperv/hvgdk_mini.h

Quoted 13 lines
include/hw/hyperv/hvgdk_mini.h +13 -0
+        hv_register_name name;
+        int isar_idx;
+    } regs[] = {
+        { HV_ARM64_REGISTER_ID_AA64_PFR0_EL1,  ID_AA64PFR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_PFR1_EL1,  ID_AA64PFR1_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_ISAR0_EL1, ID_AA64ISAR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_ISAR1_EL1, ID_AA64ISAR1_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_ISAR2_EL1, ID_AA64ISAR2_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_MMFR0_EL1, ID_AA64MMFR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_MMFR1_EL1, ID_AA64MMFR1_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_MMFR2_EL1, ID_AA64MMFR2_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_DFR0_EL1,  ID_AA64DFR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_DFR1_EL1,  ID_AA64DFR1_EL1_IDX },

Per above, if you use matching names, you can macroize to avoid typos:

#define R(X) { HV_ARM64_REGISTER_##X, X##_IDX }

    R(ID_AA64PFR0_EL1),
    R(ID_AA64PFR1_EL1),
    ...

#undef R

Also, there are a *lot* more id registers. Any chance all, or at least more, are defined by MSHV?

r~

#15 lore raw

Re: [PATCH v3 05/14] target/arm/mshv: implement -cpu host for MSHV

Quoted 74 lines · Richard Henderson
Quoted 73 lines · Aastha Rawat

This commit enables support for the -cpu host option with MSHV accelerator on ARM64. The implementation queries the partition's CPU's ID registers and features via hypervisor interface, allowing to pass all CPU capabilities for the host CPU to the guest.

Replace preprocessor config checks with runtime calls to mshv_enabled(), kvm_enabled(), etc. This ensures that the correct accelerator is selected at runtime when both MSHV and KVM are enabled.

---
  accel/mshv/mshv-all.c          |   3 +-
  hw/arm/virt.c                  |   3 +-
  include/hw/hyperv/hvgdk_mini.h |  17 ++++
  include/hw/hyperv/hvhdk.h      |  10 ++
  include/system/hw_accel.h      |   3 +-
  target/arm/cpu.c               |   6 +-
  target/arm/cpu64.c             |  24 +++--
  target/arm/mshv/mshv-all.c     | 204 +++++++++++++++++++++++++++++++++++++++++
  target/arm/mshv_arm.h          |  18 ++++
  9 files changed, 273 insertions(+), 15 deletions(-)
accel/mshv/mshv-all.c +2 -1
index d4cc7f5371..44f35a1463 100644
@@ -400,13 +400,14 @@ static int mshv_init_vcpu(CPUState *cpu)
      int ret;
      cpu->accel = g_new0(AccelCPUState, 1);
-    mshv_arch_init_vcpu(cpu);
      ret = mshv_create_vcpu(vm_fd, vp_index, &cpu->accel->cpufd);
      if (ret < 0) {
          return -1;
      }
+    mshv_arch_init_vcpu(cpu);
+
      cpu->accel->dirty = true;
      return 0;
hw/arm/virt.c +2 -1
index 7456614d05..34eb5248a9 100644
@@ -50,6 +50,7 @@
  #include "system/kvm.h"
  #include "system/hvf.h"
  #include "system/whpx.h"
+#include "system/mshv.h"
  #include "system/qtest.h"
  #include "system/system.h"
  #include "hw/core/loader.h"
@@ -3450,7 +3451,7 @@ static GPtrArray *virt_get_valid_cpu_types(const MachineState *ms)
      if (target_aarch64()) {
          g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("cortex-a53")));
          g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("cortex-a57")));
-        if (kvm_enabled() || hvf_enabled()) {
+        if (kvm_enabled() || hvf_enabled() || mshv_enabled()) {
              g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("host")));
          }
      }
include/hw/hyperv/hvgdk_mini.h +7 -0
index dfe94050f4..d56be0d70f 100644
@@ -48,6 +48,20 @@ typedef enum hv_register_name {
      HV_ARM64_REGISTER_LR    = 0x0002001E,
      HV_ARM64_REGISTER_PC    = 0x00020022,
+    /* AArch64 System Register Descriptions: ID Registers */
+    HV_ARM64_REGISTER_ID_MIDR_EL1         = 0x00022000,
+    HV_ARM64_REGISTER_ID_MPIDR_EL1        = 0x00022005,
+    HV_ARM64_REGISTER_ID_AA64_PFR0_EL1    = 0x00022020,
+    HV_ARM64_REGISTER_ID_AA64_PFR1_EL1    = 0x00022021,
+    HV_ARM64_REGISTER_ID_AA64_ISAR0_EL1   = 0x00022030,
+    HV_ARM64_REGISTER_ID_AA64_ISAR1_EL1   = 0x00022031,
Quoted 8 lines

include/hw/hyperv/hvgdk_mini.h typedef enum hv_register_name { · line 63

Quoted 6 lines
include/hw/hyperv/hvgdk_mini.h +6 -0
+    HV_ARM64_REGISTER_ID_AA64_ISAR2_EL1   = 0x00022032,
+    HV_ARM64_REGISTER_ID_AA64_MMFR0_EL1   = 0x00022038,
+    HV_ARM64_REGISTER_ID_AA64_MMFR1_EL1   = 0x00022039,
+    HV_ARM64_REGISTER_ID_AA64_MMFR2_EL1   = 0x0002203a,
+    HV_ARM64_REGISTER_ID_AA64_DFR0_EL1    = 0x00022028,
+    HV_ARM64_REGISTER_ID_AA64_DFR1_EL1    = 0x00022029,

Please use the actual name of the register and not add the extra _ after AA64.

These names are imported exactly as they’re defined in the hypervisor headers, so we want to keep them as-is.

Quoted 17 lines
Quoted 17 lines
include/hw/hyperv/hvgdk_mini.h +7 -10
-#if defined(CONFIG_KVM)
-    kvm_arm_set_cpu_features_from_host(cpu);
-    aarch64_add_sve_properties(obj);
-#elif defined(CONFIG_HVF)
-    hvf_arm_set_cpu_features_from_host(cpu);
-#elif defined(CONFIG_WHPX)
-    whpx_arm_set_cpu_features_from_host(cpu);
-#else
-    g_assert_not_reached();
-#endif
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);
+    } else if (kvm_enabled()) {
+        kvm_arm_set_cpu_features_from_host(cpu);
+        aarch64_add_sve_properties(obj);
+    } else if (hvf_enabled()) {
+        hvf_arm_set_cpu_features_from_host(cpu);
Quoted 8 lines

include/hw/hyperv/hvgdk_mini.h

Quoted 5 lines
include/hw/hyperv/hvgdk_mini.h +5 -0
+    } else if (whpx_enabled()) {
+        whpx_arm_set_cpu_features_from_host(cpu);
+    } else {
+        g_assert_not_reached();
+    }

If this is because both mshv and kvm can be enabled simultaneously, then this should be a separate patch to transform the ifdef ladder.

Right, sure.

Quoted 4 lines
Quoted 4 lines
include/hw/hyperv/hvgdk_mini.h +4 -0
+static void clamp_id_aa64mmfr0_parange_to_ipa_size(int mshv_fd,
+                                                   ARMISARegisters *isar)
+{
+    uint32_t ipa_size = mshv_arm_get_ipa_bit_size(mshv_fd);
Quoted 8 lines

include/hw/hyperv/hvgdk_mini.h

Quoted 6 lines
include/hw/hyperv/hvgdk_mini.h +6 -0
+    uint64_t id_aa64mmfr0;
+
+    /* Clamp down the PARange to the IPA size the kernel supports. */
+    uint8_t index = round_down_to_parange_index(ipa_size);
+    id_aa64mmfr0 = GET_IDREG(isar, ID_AA64MMFR0);
+    id_aa64mmfr0 = (id_aa64mmfr0 & ~R_ID_AA64MMFR0_PARANGE_MASK) | index;

id_aa64_mmfr0 = FIELD_DPREG64(id_aa64mmfr0, AA64MMFR0, PARANGE, index);

Ack.

Quoted 5 lines

include/hw/hyperv/hvgdk_mini.h

Quoted 3 lines
include/hw/hyperv/hvgdk_mini.h +3 -0
+    args.in_ptr = (uintptr_t)(in_buffer);
+    args.out_sz = values_sz;
+    args.out_ptr = (uintptr_t)(out_buffer);

Drop unnecessary ().

Ack, will do.

Quoted 3 lines
Quoted 1 line

+ args.reps = (uint16_t) n_regs;

Unnecessary cast.

Ack, will remove.

Quoted 4 lines
Quoted 4 lines
include/hw/hyperv/hvgdk_mini.h +4 -0
+static bool mshv_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
+{
+    int mshv_fd = mshv_state->fd;
+    int vm_fd = mshv_state->vm;
Quoted 8 lines

include/hw/hyperv/hvgdk_mini.h

Quoted 6 lines
include/hw/hyperv/hvgdk_mini.h +6 -0
+    int i, ret;
+    bool success = true;
+    uint64_t pfr0, pfr1;
+    gchar *contents = NULL;
+
+    const struct {

static

Ack.

Quoted 16 lines

include/hw/hyperv/hvgdk_mini.h

Quoted 13 lines
include/hw/hyperv/hvgdk_mini.h +13 -0
+        hv_register_name name;
+        int isar_idx;
+    } regs[] = {
+        { HV_ARM64_REGISTER_ID_AA64_PFR0_EL1,  ID_AA64PFR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_PFR1_EL1,  ID_AA64PFR1_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_ISAR0_EL1, ID_AA64ISAR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_ISAR1_EL1, ID_AA64ISAR1_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_ISAR2_EL1, ID_AA64ISAR2_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_MMFR0_EL1, ID_AA64MMFR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_MMFR1_EL1, ID_AA64MMFR1_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_MMFR2_EL1, ID_AA64MMFR2_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_DFR0_EL1,  ID_AA64DFR0_EL1_IDX },
+        { HV_ARM64_REGISTER_ID_AA64_DFR1_EL1,  ID_AA64DFR1_EL1_IDX },

Per above, if you use matching names, you can macroize to avoid typos:

Quoted 7 lines

#define R(X) { HV_ARM64_REGISTER_##X, X##_IDX }

    R(ID_AA64PFR0_EL1),
    R(ID_AA64PFR1_EL1),
    ...

#undef R

The Hyper-V register names don’t match the ARM names exactly, so a simple R(x) macro won’t work without renaming. Since we have to maintain them as-is, we can’t do this.

Quoted 5 lines

Also, there are a *lot* more id registers. Any chance all, or at least more, are defined by MSHV?

r~

Yes, MSHV defines many more ID registers, but for now I’ve only included the ones that are actually used. We can add more as needed in future patches.

Thanks,
Aastha

#16 lore raw

PATCH v3 6/14 accel/mshv: add arch-specific accelerator init hook

In reply to Aastha Rawat

From: "Anirudh Rayabharam (Microsoft)" <[email protected]>

Introduce mshv_arch_accel_init() as an arch-specific hook called early in mshv_init(), before VM creation. This allows each architecture to perform platform-specific initialization at accelerator init time.

For arm64, the hook queries the hypervisor for the supported IPA bit size and validates it against the guest memory map via the machine's get_physical_address_range callback, following the same pattern used by HVF and WHPX. This also populates the memory map which comes in handy later when retreiving the vGIC layout.

For x86, the hook calls mshv_init_mmio_emu() which was previously called directly from the common init path. Also move set_unimplemented_msr_action() and mshv_arch_post_init_vm() from mshv-cpu.c to the new mshv-all.c, as they are not vCPU-specific.

---
 accel/mshv/mshv-all.c        |  5 ++-
 include/system/mshv_int.h    |  1 +
 target/arm/mshv/mshv-all.c   | 25 ++++++++++----
 target/i386/mshv/meson.build |  1 +
 target/i386/mshv/mshv-all.c  | 80 ++++++++++++++++++++++++++++++++++++++++++++
 target/i386/mshv/mshv-cpu.c  | 40 ----------------------
 6 files changed, 105 insertions(+), 47 deletions(-)
accel/mshv/mshv-all.c +4 -1
index 44f35a1463..6940ad9989 100644
@@ -434,7 +434,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
         return -1;
     }
 
-    mshv_init_mmio_emu();
+    ret = mshv_arch_accel_init(as, ms, mshv_fd);
+    if (ret < 0) {
+        return -1;
+    }
 
     mshv_init_msicontrol();
 
include/system/mshv_int.h +1 -0
index ff3ab957b5..c72c91cd23 100644
@@ -96,6 +96,7 @@ void mshv_arch_init_vcpu(CPUState *cpu);
 void mshv_arch_destroy_vcpu(CPUState *cpu);
 void mshv_arch_amend_proc_features(
     union hv_partition_synthetic_processor_features *features);
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd);
 int mshv_arch_post_init_vm(int vm_fd);
 void mshv_setup_hvcall_args(AccelCPUState *state);
 
target/arm/mshv/mshv-all.c +19 -6
index db1174b444..8d16971c0d 100644
@@ -9,12 +9,13 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
-
 #include "qemu/osdep.h"
 #include <sys/ioctl.h>
 
 #include "qemu/error-report.h"
 #include "qemu/memalign.h"
+#include "hw/arm/bsa.h"
+#include "hw/arm/virt.h"
 
 #include "system/cpus.h"
 #include "target/arm/cpu.h"
@@ -188,11 +189,6 @@ void mshv_arch_destroy_vcpu(CPUState *cpu)
     state->hvcall_args = (MshvHvCallArgs){0};
 }
 
-void mshv_init_mmio_emu(void)
-{
-
-}
-
 void mshv_arch_amend_proc_features(
     union hv_partition_synthetic_processor_features *features)
 {
@@ -394,3 +390,20 @@ void mshv_arm_set_cpu_features_from_host(ARMCPU *cpu)
     cpu->midr = arm_host_cpu_features.midr;
     cpu->reset_sctlr = arm_host_cpu_features.reset_sctlr;
 }
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+    int pa_range;
+    uint32_t ipa_size;
+
+    if (mc->get_physical_address_range) {
+        ipa_size = mshv_arm_get_ipa_bit_size(mshv_fd);
+        pa_range = mc->get_physical_address_range(ms, ipa_size, ipa_size);
+        if (pa_range < 0) {
+            return -EINVAL;
+        }
+    }
+
+    return 0;
+}
target/i386/mshv/meson.build +1 -0
index 6091c21887..ce54e134cb 100644
@@ -1,6 +1,7 @@
 i386_mshv_ss = ss.source_set()
 
 i386_mshv_ss.add(files(
+  'mshv-all.c',
   'mshv-cpu.c',
   'msr.c',
 ))
target/i386/mshv/mshv-all.c new file +80 -0

80 lines not on this page · raw message

target/i386/mshv/mshv-cpu.c +0 -40
index 9a80dc34d0..8325ac74a3 100644
@@ -1482,43 +1482,3 @@ void mshv_arch_destroy_vcpu(CPUState *cpu)
     state->hvcall_args = (MshvHvCallArgs){0};
     g_clear_pointer(&env->emu_mmio_buf, g_free);
 }
-
-/*
- * Default Microsoft Hypervisor behavior for unimplemented MSR is to send a
- * fault to the guest if it tries to access it. It is possible to override
- * this behavior with a more suitable option i.e., ignore writes from the guest
- * and return zero in attempt to read unimplemented.
- */
-static int set_unimplemented_msr_action(int vm_fd)
-{
-    struct hv_input_set_partition_property in = {0};
-    struct mshv_root_hvcall args = {0};
-
-    in.property_code  = HV_PARTITION_PROPERTY_UNIMPLEMENTED_MSR_ACTION;
-    in.property_value = HV_UNIMPLEMENTED_MSR_ACTION_IGNORE_WRITE_READ_ZERO;
-
-    args.code   = HVCALL_SET_PARTITION_PROPERTY;
-    args.in_sz  = sizeof(in);
-    args.in_ptr = (uint64_t)&in;
-
-    trace_mshv_hvcall_args("unimplemented_msr_action", args.code, args.in_sz);
-
-    int ret = mshv_hvcall(vm_fd, &args);
-    if (ret < 0) {
-        error_report("Failed to set unimplemented MSR action");
-        return -1;
-    }
-    return 0;
-}
-
-int mshv_arch_post_init_vm(int vm_fd)
-{
-    int ret;
-
-    ret = set_unimplemented_msr_action(vm_fd);
-    if (ret < 0) {
-        error_report("Failed to set unimplemented MSR action");
-    }
-
-    return ret;
-}
Signature · 1 line
2.45.4
#17 lore raw

Re: [PATCH v3 06/14] accel/mshv: add arch-specific accelerator init hook

Quoted 75 lines · Aastha Rawat

From: "Anirudh Rayabharam (Microsoft)" <[email protected]>

Introduce mshv_arch_accel_init() as an arch-specific hook called early in mshv_init(), before VM creation. This allows each architecture to perform platform-specific initialization at accelerator init time.

For arm64, the hook queries the hypervisor for the supported IPA bit size and validates it against the guest memory map via the machine's get_physical_address_range callback, following the same pattern used by HVF and WHPX. This also populates the memory map which comes in handy later when retreiving the vGIC layout.

For x86, the hook calls mshv_init_mmio_emu() which was previously called directly from the common init path. Also move set_unimplemented_msr_action() and mshv_arch_post_init_vm() from mshv-cpu.c to the new mshv-all.c, as they are not vCPU-specific.

---
  accel/mshv/mshv-all.c        |  5 ++-
  include/system/mshv_int.h    |  1 +
  target/arm/mshv/mshv-all.c   | 25 ++++++++++----
  target/i386/mshv/meson.build |  1 +
  target/i386/mshv/mshv-all.c  | 80 ++++++++++++++++++++++++++++++++++++++++++++
  target/i386/mshv/mshv-cpu.c  | 40 ----------------------
  6 files changed, 105 insertions(+), 47 deletions(-)
accel/mshv/mshv-all.c +4 -1
index 44f35a1463..6940ad9989 100644
@@ -434,7 +434,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
          return -1;
      }
 
-    mshv_init_mmio_emu();
+    ret = mshv_arch_accel_init(as, ms, mshv_fd);
+    if (ret < 0) {
+        return -1;
+    }
 
      mshv_init_msicontrol();
 
include/system/mshv_int.h +1 -0
index ff3ab957b5..c72c91cd23 100644
@@ -96,6 +96,7 @@ void mshv_arch_init_vcpu(CPUState *cpu);
  void mshv_arch_destroy_vcpu(CPUState *cpu);
  void mshv_arch_amend_proc_features(
      union hv_partition_synthetic_processor_features *features);
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd);
  int mshv_arch_post_init_vm(int vm_fd);
  void mshv_setup_hvcall_args(AccelCPUState *state);
 
target/arm/mshv/mshv-all.c +2 -1
index db1174b444..8d16971c0d 100644
@@ -9,12 +9,13 @@
   * SPDX-License-Identifier: GPL-2.0-or-later
   */
 
-
  #include "qemu/osdep.h"
  #include <sys/ioctl.h>
 
  #include "qemu/error-report.h"
  #include "qemu/memalign.h"
+#include "hw/arm/bsa.h"
+#include "hw/arm/virt.h"
 
  #include "system/cpus.h"
  #include "target/arm/cpu.h"

target/arm/mshv/mshv-all.c void mshv_arch_destroy_vcpu(CPUState *cpu) · line 194

Quoted 7 lines
target/arm/mshv/mshv-all.c +0 -4
@@ -188,11 +189,6 @@ void mshv_arch_destroy_vcpu(CPUState *cpu)
      state->hvcall_args = (MshvHvCallArgs){0};
  }
 
-void mshv_init_mmio_emu(void)
-{
-
-}

It would be nice to introduce the mshv_arch_accel_init hook earlier in the series, so that you don't have to add and then remove this function.

r~

#18 lore raw

Re: [PATCH v3 06/14] accel/mshv: add arch-specific accelerator init hook

Quoted 80 lines · Richard Henderson
Quoted 68 lines · Aastha Rawat

From: "Anirudh Rayabharam (Microsoft)" <[email protected]>

Introduce mshv_arch_accel_init() as an arch-specific hook called early in mshv_init(), before VM creation. This allows each architecture to perform platform-specific initialization at accelerator init time.

For arm64, the hook queries the hypervisor for the supported IPA bit size and validates it against the guest memory map via the machine's get_physical_address_range callback, following the same pattern used by HVF and WHPX. This also populates the memory map which comes in handy later when retreiving the vGIC layout.

For x86, the hook calls mshv_init_mmio_emu() which was previously called directly from the common init path. Also move set_unimplemented_msr_action() and mshv_arch_post_init_vm() from mshv-cpu.c to the new mshv-all.c, as they are not vCPU-specific.

---
  accel/mshv/mshv-all.c        |  5 ++-
  include/system/mshv_int.h    |  1 +
  target/arm/mshv/mshv-all.c   | 25 ++++++++++----
  target/i386/mshv/meson.build |  1 +
  target/i386/mshv/mshv-all.c  | 80 ++++++++++++++++++++++++++++++++++++++++++++
  target/i386/mshv/mshv-cpu.c  | 40 ----------------------
  6 files changed, 105 insertions(+), 47 deletions(-)
accel/mshv/mshv-all.c +4 -1
index 44f35a1463..6940ad9989 100644
@@ -434,7 +434,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
          return -1;
      }
-    mshv_init_mmio_emu();
+    ret = mshv_arch_accel_init(as, ms, mshv_fd);
+    if (ret < 0) {
+        return -1;
+    }
      mshv_init_msicontrol();
include/system/mshv_int.h +1 -0
index ff3ab957b5..c72c91cd23 100644
@@ -96,6 +96,7 @@ void mshv_arch_init_vcpu(CPUState *cpu);
  void mshv_arch_destroy_vcpu(CPUState *cpu);
  void mshv_arch_amend_proc_features(
      union hv_partition_synthetic_processor_features *features);
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd);
  int mshv_arch_post_init_vm(int vm_fd);
  void mshv_setup_hvcall_args(AccelCPUState *state);
target/arm/mshv/mshv-all.c +2 -1
index db1174b444..8d16971c0d 100644
@@ -9,12 +9,13 @@
   * SPDX-License-Identifier: GPL-2.0-or-later
   */
-
  #include "qemu/osdep.h"
  #include <sys/ioctl.h>
  #include "qemu/error-report.h"
  #include "qemu/memalign.h"
+#include "hw/arm/bsa.h"
+#include "hw/arm/virt.h"
  #include "system/cpus.h"
  #include "target/arm/cpu.h"

target/arm/mshv/mshv-all.c void mshv_arch_destroy_vcpu(CPUState *cpu) · line 193

Quoted 6 lines
target/arm/mshv/mshv-all.c +0 -4
@@ -188,11 +189,6 @@ void mshv_arch_destroy_vcpu(CPUState *cpu)
      state->hvcall_args = (MshvHvCallArgs){0};
  }
-void mshv_init_mmio_emu(void)
-{
-
-}

It would be nice to introduce the mshv_arch_accel_init hook earlier in the series, so that you don't have to add and then remove this function.

Quoted 1 line

r~

Makes sense, will reorder the series in v4.

Thanks,
Aastha

#19 lore raw

PATCH v3 7/14 accel/mshv: Add access_vp_regs synthetic proc features

In reply to Aastha Rawat

Enable access_vp_regs feature for regs. In arm64, this feature bit allows the guest to set VP registers using hypercall. This wasn't required for x86 because such registers are set using wrmsr instead of hypercall.

---
 accel/mshv/mshv-all.c     |  1 +
 include/hw/hyperv/hvhdk.h | 91 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 91 insertions(+), 1 deletion(-)
accel/mshv/mshv-all.c +1 -0
index 6940ad9989..5ce76e86db 100644
@@ -156,6 +156,7 @@ static int set_synthetic_proc_features(int vm_fd)
     features.tb_flush_hypercalls = 1;
     features.synthetic_cluster_ipi = 1;
     features.direct_synthetic_timers = 1;
+    features.access_vp_regs = 1;
 
     mshv_arch_amend_proc_features(&features);
 
include/hw/hyperv/hvhdk.h +90 -1

125 lines not on this page · raw message

Signature · 1 line
2.45.4
#20 lore raw

PATCH v3 8/14 target/arm: cpu: Mark MSHV supporting PSCI 1.3

In reply to Aastha Rawat

MSHV supports PSCI 1.3. Expose this version to guests.

---
 target/arm/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.c +1 -1
index 8aa22747ff..67f1991622 100644
@@ -1157,7 +1157,7 @@ static void arm_cpu_initfn(Object *obj)
     if (tcg_enabled() || hvf_enabled()) {
         /* TCG and HVF implement PSCI 1.1 */
         cpu->psci_version = QEMU_PSCI_VERSION_1_1;
-    } else if (whpx_enabled()) {
+    } else if (whpx_enabled() || mshv_enabled()) {
         cpu->psci_version = QEMU_PSCI_VERSION_1_3;
     }
 }
Signature · 1 line
2.45.4
#21 lore raw

PATCH v3 9/14 accel/mshv: implement cpu_thread_is_idle() hook

In reply to Aastha Rawat

From: Magnus Kulke <[email protected]>

In MSHV the hypervisor APIC is always used, so we to implement this hook to make sure the AP's vcpu thread is not blocked waiting for an INIT SIPI by the BSP. Without this change soft reboots with -smp cpus>=2 will hang.

---
 accel/mshv/mshv-all.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
accel/mshv/mshv-all.c +12 -0
index 5ce76e86db..6c0ddbf3ca 100644
@@ -704,11 +704,23 @@ static const TypeInfo mshv_accel_type = {
     .instance_size = sizeof(MshvState),
 };
 
+/*
+ * MSHV manages the LAPIC in the hypervisor. SIPI for APs are handled
+ * internally. Halted vCPUs must still enter mshv_cpu_exec() so that
+ * MSHV_RUN_VP is called and the hypervisor will deliver SIPI to wake APs.
+ */
+
+static bool mshv_vcpu_thread_is_idle(CPUState *cpu)
+{
+    return false;
+}
+
 static void mshv_accel_ops_class_init(ObjectClass *oc, const void *data)
 {
     AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
 
     ops->create_vcpu_thread = mshv_start_vcpu_thread;
+    ops->cpu_thread_is_idle = mshv_vcpu_thread_is_idle;
     ops->synchronize_post_init = mshv_cpu_synchronize_post_init;
     ops->synchronize_post_reset = mshv_cpu_synchronize_post_reset;
     ops->synchronize_state = mshv_cpu_synchronize;
Signature · 1 line
2.45.4
#22 lore raw

Re: [PATCH v3 09/14] accel/mshv: implement cpu_thread_is_idle() hook

Quoted 21 lines · Aastha Rawat

From: Magnus Kulke <[email protected]>

In MSHV the hypervisor APIC is always used, so we to implement this hook to make sure the AP's vcpu thread is not blocked waiting for an INIT SIPI by the BSP. Without this change soft reboots with -smp cpus>=2 will hang.

---
  accel/mshv/mshv-all.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
accel/mshv/mshv-all.c +0 -0
index 5ce76e86db..6c0ddbf3ca 100644
@@ -704,11 +704,23 @@ static const TypeInfo mshv_accel_type = {
      .instance_size = sizeof(MshvState),
  };
 

accel/mshv/mshv-all.c static const TypeInfo mshv_accel_type = { · line 716

Quoted 10 lines
accel/mshv/mshv-all.c +10 -0
+/*
+ * MSHV manages the LAPIC in the hypervisor. SIPI for APs are handled
+ * internally. Halted vCPUs must still enter mshv_cpu_exec() so that
+ * MSHV_RUN_VP is called and the hypervisor will deliver SIPI to wake APs.
+ */
+
+static bool mshv_vcpu_thread_is_idle(CPUState *cpu)
+{
+    return false;
+}

The comment, at least, is x86 specific.

r~

Quoted 11 lines
accel/mshv/mshv-all.c +2 -0
+
  static void mshv_accel_ops_class_init(ObjectClass *oc, const void *data)
  {
      AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
 
      ops->create_vcpu_thread = mshv_start_vcpu_thread;
+    ops->cpu_thread_is_idle = mshv_vcpu_thread_is_idle;
      ops->synchronize_post_init = mshv_cpu_synchronize_post_init;
      ops->synchronize_post_reset = mshv_cpu_synchronize_post_reset;
      ops->synchronize_state = mshv_cpu_synchronize;
#23 lore raw

Re: [PATCH v3 09/14] accel/mshv: implement cpu_thread_is_idle() hook

Quoted 25 lines · Richard Henderson
Quoted 24 lines · Aastha Rawat

From: Magnus Kulke <[email protected]>

In MSHV the hypervisor APIC is always used, so we to implement this hook to make sure the AP's vcpu thread is not blocked waiting for an INIT SIPI by the BSP. Without this change soft reboots with -smp cpus>=2 will hang.

---
  accel/mshv/mshv-all.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
accel/mshv/mshv-all.c +4 -0
index 5ce76e86db..6c0ddbf3ca 100644
@@ -704,11 +704,23 @@ static const TypeInfo mshv_accel_type = {
      .instance_size = sizeof(MshvState),
  };
+/*
+ * MSHV manages the LAPIC in the hypervisor. SIPI for APs are handled
+ * internally. Halted vCPUs must still enter mshv_cpu_exec() so that
+ * MSHV_RUN_VP is called and the hypervisor will deliver SIPI to wake APs.
Quoted 8 lines

accel/mshv/mshv-all.c static const TypeInfo mshv_accel_type = { · line 716

Quoted 6 lines
accel/mshv/mshv-all.c +6 -0
+ */
+
+static bool mshv_vcpu_thread_is_idle(CPUState *cpu)
+{
+    return false;
+}

The comment, at least, is x86 specific.

The function itself is correct for both architectures (returns false). The logic applies to x86 (SIPI) and ARM64 (PSCI), haulted vCPUs must still enter MSHV_RUN_VP so that hypervisor can wake them. I can update the comment.

Thanks,
Aastha

#24 lore raw

PATCH v3 10/14 target/arm: extract MMIO emulation logic for HVF & WHPX

In reply to Aastha Rawat

- Move common MMIO emulation code from HVF and WHPX backend to arm_emulate_mmio() in helper.c.
- Update HVF and WHPX to use the new helper and shared types.

---
 target/arm/helper.c        | 60 +++++++++++++++++++++++++++++++++++++++++++
 target/arm/helper.h        |  5 ++++
 target/arm/hvf/hvf.c       | 64 ++++++++++++++++++++++++----------------------
 target/arm/syndrome.h      | 37 +++++++++++++++++++++++++++
 target/arm/whpx/whpx-all.c | 62 +++++++++++---------------------------------
 5 files changed, 150 insertions(+), 78 deletions(-)
target/arm/helper.c +60 -0
index 7389f2988c..c9457c8384 100644
@@ -34,6 +34,8 @@
 #endif
 #include "cpregs.h"
 #include "target/arm/gtimer.h"
+#include "target/arm/helper.h"
+#include "target/arm/syndrome.h"
 #include "qemu/plugin.h"
 
 static void switch_mode(CPUARMState *env, int mode);
@@ -9130,6 +9132,64 @@ static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
     take_aarch32_exception(env, new_mode, mask, offset, addr);
 }
 
+/*
+ * Emulate a data abort syndrome for MMIO/guest memory access
+ */
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa)
+{
+    CPUARMState *env = &ARM_CPU(cpu)->env;
+    int ret;
+    IssDataAbort iss = { 0 };
+    iss.raw = syndrome.iss;
+
+    if (!(syndrome.ec == data_abort_lower || syndrome.ec == data_abort)) {
+        error_report("Unknown exception class 0x%x", syndrome.ec);
+        return -1;
+    }
+
+    if (!iss.isv) {
+        error_report("Cannot emulate MMIO. ISS not valid.");
+        return -1;
+    }
+
+    AddressSpace *as = cpu_get_address_space(cpu, ARMASIdx_NS);
+    uint64_t len = 1ULL << iss.sas;
+    bool sign_extend = iss.sse;
+    uint64_t reg_index = iss.srt;
+
+    if (iss.wnr) {
+        uint8_t data[8];
+        uint64_t val = reg_index < 31 ? env->xregs[reg_index] : 0ULL;
+        val = cpu_to_le64(val);
+        memcpy(data, &val, sizeof(val));
+        ret = address_space_write(as, gpa, MEMTXATTRS_UNSPECIFIED, data, len);
+        if (ret != MEMTX_OK) {
+            error_report("Failed to write guest memory");
+            return -1;
+        }
+    } else {
+        uint8_t data[8] = { 0 };
+        ret = address_space_read(as, gpa, MEMTXATTRS_UNSPECIFIED, data, len);
+        if (ret != MEMTX_OK) {
+            error_report("Failed to read guest memory");
+            return -1;
+        }
+        uint64_t val;
+        memcpy(&val, data, sizeof(val));
+        val = le64_to_cpu(val);
+        if (sign_extend) {
+            uint64_t shift = 64 - (len * 8);
+            val = (((int64_t)val << shift) >> shift);
+        }
+        if (!iss.sf) {
+            val &= 0xffffffff;
+        }
+        env->xregs[reg_index] = val;
+    }
+
+    return 0;
+}
+
 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
 {
     /*
target/arm/helper.h +5 -0
index b1c26c180e..3bc22595fd 100644
@@ -3,8 +3,13 @@
 #ifndef HELPER__H
 #define HELPER__H
 
+#include <stdint.h>
+#include <stdbool.h>
 #include "exec/helper-proto-common.h"
 #include "exec/helper-gen-common.h"
+#include "target/arm/syndrome.h"
+
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa);
 
 #define HELPER_H "tcg/helper-defs.h"
 #include "exec/helper-proto.h.inc"
target/arm/hvf/hvf.c +33 -31

113 lines not on this page · raw message

target/arm/syndrome.h +37 -0
index bff61f052c..777d4fb975 100644
@@ -101,6 +101,43 @@ typedef enum {
     GCS_IT_GCSPOPX = 9,
 } GCSInstructionType;
 
+typedef union {
+    uint64_t raw;
+    struct {
+        uint32_t iss:25;
+        uint32_t il:1;
+        uint32_t ec:6;
+        uint32_t iss2:5;
+        uint32_t _rsvd:27;
+    } QEMU_PACKED;
+} EsrEl2;
+
+typedef union {
+    uint32_t raw;
+    struct {
+        uint32_t dfsc:6;
+        uint32_t wnr:1;
+        uint32_t s1ptw:1;
+        uint32_t cm:1;
+        uint32_t ea:1;
+        uint32_t fnv:1;
+        uint32_t set:2;
+        uint32_t vncr:1;
+        uint32_t ar:1;
+        uint32_t sf:1;
+        uint32_t srt:5;
+        uint32_t sse:1;
+        uint32_t sas:2;
+        uint32_t isv:1;
+        uint32_t _unused:7;
+    } QEMU_PACKED;
+} IssDataAbort;
+
+typedef enum {
+    data_abort_lower = 36,
+    data_abort = 37,
+} ExceptionClass;
+
 #define ARM_EL_EC_LENGTH 6
 #define ARM_EL_EC_SHIFT 26
 #define ARM_EL_IL_SHIFT 25
target/arm/whpx/whpx-all.c +15 -47

80 lines not on this page · raw message

Signature · 1 line
2.45.4
#25 lore raw

Re: [PATCH v3 10/14] target/arm: extract MMIO emulation logic for HVF & WHPX

Quoted 7 lines · Aastha Rawat

- Move common MMIO emulation code from HVF and WHPX backend to arm_emulate_mmio() in helper.c.
- Update HVF and WHPX to use the new helper and shared types.

Hi,

This one is a bit interesting and not sure if it’s ok in this form.

That’s for one reason: it causes a slowdown on WHP and HVF by fetching state (and sometimes setting) outside of their respective shared register pages.

In both of those APIs, the existence of the shared register page is opaque/hidden away, but if you aren’t careful about what you fetch you’re going to hit the slow path very easily.

On x86 WHPX, we have a state level abstraction with a FAST_RUNTIME_STATE that only fetches a (sub)set accessible that is readable using only information from the shared register page - and that is supposed to be enough for the vast majority of MMIO - that doesn’t touch the FP/MMX/SSE register files.

And another possibility is putting it in a target/arm/emulate with mimicking the x86_emul_ops interface with adding register callbacks to that so that state is fetched/set dynamically.

Quoted 367 lines

---

target/arm/helper.c        | 60 +++++++++++++++++++++++++++++++++++++++++++
target/arm/helper.h        |  5 ++++
target/arm/hvf/hvf.c       | 64 ++++++++++++++++++++++++----------------------
target/arm/syndrome.h      | 37 +++++++++++++++++++++++++++
target/arm/whpx/whpx-all.c | 62 +++++++++++---------------------------------
5 files changed, 150 insertions(+), 78 deletions(-)
target/arm/helper.c +60 -0
index 7389f2988c..c9457c8384 100644
@@ -34,6 +34,8 @@
 #endif
 #include "cpregs.h"
 #include "target/arm/gtimer.h"
+#include "target/arm/helper.h"
+#include "target/arm/syndrome.h"
 #include "qemu/plugin.h"
 
 static void switch_mode(CPUARMState *env, int mode);
@@ -9130,6 +9132,64 @@ static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
    take_aarch32_exception(env, new_mode, mask, offset, addr);
 }
 
+/*
+ * Emulate a data abort syndrome for MMIO/guest memory access
+ */
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa)
+{
+    CPUARMState *env = &ARM_CPU(cpu)->env;
+    int ret;
+    IssDataAbort iss = { 0 };
+    iss.raw = syndrome.iss;
+
+    if (!(syndrome.ec == data_abort_lower || syndrome.ec == data_abort)) {
+        error_report("Unknown exception class 0x%x", syndrome.ec);
+        return -1;
+    }
+
+    if (!iss.isv) {
+        error_report("Cannot emulate MMIO. ISS not valid.");
+        return -1;
+    }
+
+    AddressSpace *as = cpu_get_address_space(cpu, ARMASIdx_NS);
+    uint64_t len = 1ULL << iss.sas;
+    bool sign_extend = iss.sse;
+    uint64_t reg_index = iss.srt;
+
+    if (iss.wnr) {
+        uint8_t data[8];
+        uint64_t val = reg_index < 31 ? env->xregs[reg_index] : 0ULL;
+        val = cpu_to_le64(val);
+        memcpy(data, &val, sizeof(val));
+        ret = address_space_write(as, gpa, MEMTXATTRS_UNSPECIFIED, data, len);
+        if (ret != MEMTX_OK) {
+            error_report("Failed to write guest memory");
+            return -1;
+        }
+    } else {
+        uint8_t data[8] = { 0 };
+        ret = address_space_read(as, gpa, MEMTXATTRS_UNSPECIFIED, data, len);
+        if (ret != MEMTX_OK) {
+            error_report("Failed to read guest memory");
+            return -1;
+        }
+        uint64_t val;
+        memcpy(&val, data, sizeof(val));
+        val = le64_to_cpu(val);
+        if (sign_extend) {
+            uint64_t shift = 64 - (len * 8);
+            val = (((int64_t)val << shift) >> shift);
+        }
+        if (!iss.sf) {
+            val &= 0xffffffff;
+        }
+        env->xregs[reg_index] = val;
+    }
+
+    return 0;
+}
+
 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
 {
    /*
target/arm/helper.h +5 -0
index b1c26c180e..3bc22595fd 100644
@@ -3,8 +3,13 @@
 #ifndef HELPER__H
 #define HELPER__H
 
+#include <stdint.h>
+#include <stdbool.h>
 #include "exec/helper-proto-common.h"
 #include "exec/helper-gen-common.h"
+#include "target/arm/syndrome.h"
+
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa);
 
 #define HELPER_H "tcg/helper-defs.h"
 #include "exec/helper-proto.h.inc"
target/arm/hvf/hvf.c +33 -31
index 5fc8f6bbbd..9ecad6c222 100644
@@ -13,6 +13,7 @@
 #include "qemu/error-report.h"
 #include "qemu/log.h"
 
+#include "syndrome.h"
 #include "system/runstate.h"
 #include "system/hvf.h"
 #include "system/hvf_int.h"
@@ -35,6 +36,7 @@
 #include "target/arm/multiprocessing.h"
 #include "target/arm/gtimer.h"
 #include "target/arm/trace.h"
+#include "target/arm/helper.h"
 #include "trace.h"
 #include "migration/vmstate.h"
 
@@ -2072,12 +2074,15 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
 {
    CPUARMState *env = cpu_env(cpu);
    ARMCPU *arm_cpu = env_archcpu(env);
-    uint64_t syndrome = excp->syndrome;
-    uint32_t ec = syn_get_ec(syndrome);
+    uint64_t syndrome;
+    uint32_t ec;
    bool advance_pc = false;
    hv_return_t r;
    int ret = 0;
 
+    syndrome = excp->syndrome;
+    ec = syn_get_ec(syndrome);
+
    switch (ec) {
    case EC_SOFTWARESTEP: {
        ret = EXCP_DEBUG;
@@ -2123,29 +2128,22 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
        break;
    }
    case EC_DATAABORT: {
-        bool isv = syndrome & ARM_EL_ISV;
-        bool iswrite = (syndrome >> 6) & 1;
-        bool s1ptw = (syndrome >> 7) & 1;
-        bool sse = (syndrome >> 21) & 1;
-        uint32_t sas = (syndrome >> 22) & 3;
-        uint32_t len = 1 << sas;
-        uint32_t srt = (syndrome >> 16) & 0x1f;
-        uint32_t cm = (syndrome >> 8) & 0x1;
-        uint64_t val = 0;
+        EsrEl2 esr_el2 = { .raw = syndrome };
+        IssDataAbort iss = { .raw = esr_el2.iss };
        uint64_t ipa = excp->physical_address;
        AddressSpace *as = cpu_get_address_space(cpu, ARMASIdx_NS);
 
-        trace_hvf_data_abort(excp->virtual_address, ipa, isv,
-                             iswrite, s1ptw, len, srt);
+        trace_hvf_data_abort(excp->virtual_address, ipa, iss.isv,
+                             iss.wnr, iss.s1ptw, 1ULL << iss.sas, iss.srt);
 
-        if (cm) {
+        if (iss.cm) {
            /* We don't cache MMIO regions */
            advance_pc = true;
            break;
        }
 
        /* Handle dirty page logging for ram. */
-        if (iswrite) {
+        if (iss.wnr) {
            hwaddr xlat;
            MemoryRegion *mr = address_space_translate(as, ipa, &xlat,
                                                       NULL, true,
@@ -2172,27 +2170,31 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
         * TODO: If s1ptw, this is an error in the guest os page tables.
         * Inject the exception into the guest.
         */
-        assert(!s1ptw);
-
-        /*
-         * TODO: ISV will be 0 for SIMD or SVE accesses.
-         * Inject the exception into the guest.
-         */
-        assert(isv);
+        assert(!iss.s1ptw);
 
        /*
         * Emulate MMIO.
         * TODO: Inject faults for errors.
         */
-        if (iswrite) {
-            val = hvf_get_reg(cpu, srt);
-            address_space_write(as, ipa, MEMTXATTRS_UNSPECIFIED, &val, len);
-        } else {
-            address_space_read(as, ipa, MEMTXATTRS_UNSPECIFIED, &val, len);
-            if (sse) {
-                val = sextract64(val, 0, len * 8);
-            }
-            hvf_set_reg(cpu, srt, val);
+        ret = hvf_arch_get_registers(cpu);
+        if (ret < 0) {
+            error_report("Failed to get registers for MMIO");
+                         esr_el2.raw, ipa);
+            return -1;
+        }
+
+        ret = arm_emulate_mmio(cpu, esr_el2, ipa);
+        if (ret < 0) {
+            error_report("Failed to emulate MMIO, syndrome=0x%llx, gpa=0x%llx",
+                         esr_el2.raw, ipa);
+            return -1;
+        }
+
+        ret = hvf_arch_put_registers(cpu);
+        if (ret < 0) {
+            error_report("Failed to set registers after MMIO emulation");
+                         esr_el2.raw, ipa);
+            return -1;
        }
        advance_pc = true;
        break;
target/arm/syndrome.h +37 -0
index bff61f052c..777d4fb975 100644
@@ -101,6 +101,43 @@ typedef enum {
    GCS_IT_GCSPOPX = 9,
 } GCSInstructionType;
 
+typedef union {
+    uint64_t raw;
+    struct {
+        uint32_t iss:25;
+        uint32_t il:1;
+        uint32_t ec:6;
+        uint32_t iss2:5;
+        uint32_t _rsvd:27;
+    } QEMU_PACKED;
+} EsrEl2;
+
+typedef union {
+    uint32_t raw;
+    struct {
+        uint32_t dfsc:6;
+        uint32_t wnr:1;
+        uint32_t s1ptw:1;
+        uint32_t cm:1;
+        uint32_t ea:1;
+        uint32_t fnv:1;
+        uint32_t set:2;
+        uint32_t vncr:1;
+        uint32_t ar:1;
+        uint32_t sf:1;
+        uint32_t srt:5;
+        uint32_t sse:1;
+        uint32_t sas:2;
+        uint32_t isv:1;
+        uint32_t _unused:7;
+    } QEMU_PACKED;
+} IssDataAbort;
+
+typedef enum {
+    data_abort_lower = 36,
+    data_abort = 37,
+} ExceptionClass;
+
 #define ARM_EL_EC_LENGTH 6
 #define ARM_EL_EC_SHIFT 26
 #define ARM_EL_IL_SHIFT 25
target/arm/whpx/whpx-all.c +15 -47
index 513551bec1..b4daeaff4d 100644
@@ -28,6 +28,8 @@
 
 #include "syndrome.h"
 #include "target/arm/cpregs.h"
+#include "target/arm/helper.h"
+#include "target/arm/syndrome.h"
 #include "internals.h"
 
 #include "system/whpx-internal.h"
@@ -330,59 +332,25 @@ static void whpx_set_global_reg(WHV_REGISTER_NAME reg, WHV_REGISTER_VALUE val)
    }
 }
 
-static uint64_t whpx_get_gp_reg(CPUState *cpu, int rt)
-{
-    assert(rt <= 31);
-    if (rt == 31) {
-        return 0;
-    }
-    WHV_REGISTER_NAME reg = WHvArm64RegisterX0 + rt;
-    WHV_REGISTER_VALUE val;
-    whpx_get_reg(cpu, reg, &val);
-
-    return val.Reg64;
-}
-
-static void whpx_set_gp_reg(CPUState *cpu, int rt, uint64_t val)
-{
-    assert(rt < 31);
-    WHV_REGISTER_NAME reg = WHvArm64RegisterX0 + rt;
-    WHV_REGISTER_VALUE reg_val = {.Reg64 = val};
-
-    whpx_set_reg(cpu, reg, reg_val);
-}
-
 static int whpx_handle_mmio(CPUState *cpu, WHV_MEMORY_ACCESS_CONTEXT *ctx)
 {
-    uint64_t syndrome = ctx->Syndrome;
-
-    bool isv = syndrome & ARM_EL_ISV;
-    bool iswrite = (syndrome >> 6) & 1;
-    bool sse = (syndrome >> 21) & 1;
-    uint32_t sas = (syndrome >> 22) & 3;
-    uint32_t len = 1 << sas;
-    uint32_t srt = (syndrome >> 16) & 0x1f;
-    uint32_t cm = (syndrome >> 8) & 0x1;
-    uint64_t val = 0;
+    int ret = 0;
+    EsrEl2 esr = { .raw = ctx->Syndrome };
+    uint32_t cm = (esr.iss >> 8) & 0x1;
 
    assert(!cm);
-    assert(isv);
-
-    if (iswrite) {
-        val = whpx_get_gp_reg(cpu, srt);
-        address_space_write(&address_space_memory,
-                            ctx->Gpa,
-                            MEMTXATTRS_UNSPECIFIED, &val, len);
-    } else {
-        address_space_read(&address_space_memory,
-                           ctx->Gpa,
-                           MEMTXATTRS_UNSPECIFIED, &val, len);
-        if (sse) {
-            val = sextract64(val, 0, len * 8);
-        }
-        whpx_set_gp_reg(cpu, srt, val);
+
+    whpx_get_registers(cpu, WHPX_LEVEL_RUNTIME_STATE);
+
+    ret = arm_emulate_mmio(cpu, esr, ctx->Gpa);
+    if (ret < 0) {
+        error_report("WHPX: Failed to handle MMIO, syndrome=0x%llx, gpa=0x%llx",
+                     esr.raw, ctx->Gpa);
+        return -1;
    }
 
+    whpx_set_registers(cpu, WHPX_LEVEL_RUNTIME_STATE);
+
    return 0;
 }
 
Signature · 2 lines
-- 
2.45.4
#26 lore raw

Re: [PATCH v3 10/14] target/arm: extract MMIO emulation logic for HVF & WHPX

Quoted 19 lines · Mohamed Mediouni
Quoted 7 lines · Aastha Rawat

- Move common MMIO emulation code from HVF and WHPX backend to arm_emulate_mmio() in helper.c.
- Update HVF and WHPX to use the new helper and shared types.

Hi,

This one is a bit interesting and not sure if it’s ok in this form.

That’s for one reason: it causes a slowdown on WHP and HVF by fetching state (and sometimes setting) outside of their respective shared register pages.

In both of those APIs, the existence of the shared register page is opaque/hidden away, but if you aren’t careful about what you fetch you’re going to hit the slow path very easily.

Quoted 8 lines

On x86 WHPX, we have a state level abstraction with a FAST_RUNTIME_STATE that only fetches a (sub)set accessible that is readable using only information from the shared register page - and that is supposed to be enough for the vast majority of MMIO - that doesn’t touch the FP/MMX/SSE register files.

And another possibility is putting it in a target/arm/emulate with mimicking the x86_emul_ops interface with adding register callbacks to that so that state is fetched/set dynamically.

Thanks for the review. I agree with you, the current approach fetches far more state than needed for a typical MMIO access, which only touches a single GP register (Xt). That would indeed bypass the shared register page fast path on both HVF and WHPX.

I like the suggestion of mirroring the x86_emul_ops pattern. For v4, I'll rework this to introduce a struct (arm_emul_ops) with register access callbacks, something along the lines of:

struct arm_emul_ops {
    uint64_t (*get_reg)(CPUState *cpu, unsigned reg);
    void (*set_reg)(CPUState *cpu, unsigned reg, uint64_t val);
};

This would use the existing backend implementation for WHPX & HVF. HVF can use hvf_get_reg()/hvf_set_reg(), WHPX can go through the shared register page via whpx_get_reg()/whpx_set_reg(), and MSHV can access env->xregs[] directly since it already syncs state in the run loop.

I'll have this reworked in v4.

Thanks,
Aastha

#27 lore raw

PATCH v3 11/14 target/arm/mshv: add vCPU run loop

In reply to Aastha Rawat

From: "Anirudh Rayabharam (Microsoft)" <[email protected]>

Add the main vCPU run loop for MSHV using the MSHV_RUN_VP_IOCTL.

Handle MMIO exits by emulating the instruction using the syndrome information from ESR_EL2.

---
 include/hw/hyperv/hvgdk_mini.h | 44 +++++++++++++++++++
 target/arm/mshv/mshv-all.c     | 96 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 140 insertions(+)
include/hw/hyperv/hvgdk_mini.h +44 -0
index d56be0d70f..84b3c6af5f 100644
@@ -750,6 +750,50 @@ struct hv_x64_memory_intercept_message {
     uint8_t instruction_bytes[16];
 };
 
+union hv_arm64_vp_execution_state {
+    uint16_t as_uint16;
+    struct {
+        uint16_t cpl:2;
+        uint16_t debug_active:1;
+        uint16_t interruption_pending:1;
+        uint16_t vtl:4;
+        uint16_t virtualization_fault_active:1;
+        uint16_t reserved:7;
+    };
+};
+
+struct hv_arm64_intercept_message_header {
+    uint32_t vp_index;
+    uint8_t instruction_length;
+    uint8_t intercept_access_type;
+    union hv_arm64_vp_execution_state execution_state;
+    uint64_t pc;
+    uint64_t cpsr;
+};
+
+union hv_arm64_memory_access_info {
+    uint8_t as_uint8;
+    struct {
+        uint8_t gva_valid:1;
+        uint8_t gva_gpa_valid:1;
+        uint8_t hypercall_output_pending:1;
+        uint8_t reserved:5;
+    };
+};
+
+struct hv_arm64_memory_intercept_message {
+    struct hv_arm64_intercept_message_header header;
+    uint32_t cache_type; /* enum hv_cache_type */
+    uint8_t instruction_byte_count;
+    union hv_arm64_memory_access_info memory_access_info;
+    uint16_t reserved1;
+    uint8_t instruction_bytes[4];
+    uint32_t reserved2;
+    uint64_t guest_virtual_address;
+    uint64_t guest_physical_address;
+    uint64_t syndrome;
+};
+
 union hv_message_flags {
     uint8_t asu8;
     struct {
target/arm/mshv/mshv-all.c +96 -0

110 lines not on this page · raw message

Signature · 1 line
2.45.4
#28 lore raw

PATCH v3 12/14 include/hw/hyperv: adjust hv_interrupt_control structure for arm64

In reply to Aastha Rawat

From: "Anirudh Rayabharam (Microsoft)" <[email protected]>

Add the arm64 specific fields to the hv_interrupt_control struct and adjust the existing usage accordingly. mshv_request_interrupt is only needed only for x86 so compile it out entirely for arm64.

---
 accel/mshv/irq.c               |  2 ++
 include/hw/hyperv/hvgdk_mini.h | 25 +++++++++++++++++++++++++
 include/system/mshv.h          |  2 ++
 3 files changed, 29 insertions(+)
accel/mshv/irq.c +2 -0
index 3c238c33c3..a391d7db86 100644
@@ -310,6 +310,7 @@ int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev)
     return 0;
 }
 
+#if defined(__x86_64__)
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
                            uint32_t vp_index, bool logical_dest_mode,
                            bool level_triggered)
@@ -346,6 +347,7 @@ int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint3
     }
     return 0;
 }
+#endif
 
 void mshv_irqchip_commit_routes(void)
 {
include/hw/hyperv/hvgdk_mini.h +25 -0
index 84b3c6af5f..eb766734d6 100644
@@ -506,13 +506,38 @@ typedef struct hv_input_set_vp_registers {
     struct hv_register_assoc elements[];
 } hv_input_set_vp_registers;
 
+enum hv_interrupt_type {
+#if defined(__x86_64__)
+    HV_X64_INTERRUPT_TYPE_FIXED             = 0x0000,
+    HV_X64_INTERRUPT_TYPE_LOWESTPRIORITY    = 0x0001,
+    HV_X64_INTERRUPT_TYPE_SMI               = 0x0002,
+    HV_X64_INTERRUPT_TYPE_REMOTEREAD        = 0x0003,
+    HV_X64_INTERRUPT_TYPE_NMI               = 0x0004,
+    HV_X64_INTERRUPT_TYPE_INIT              = 0x0005,
+    HV_X64_INTERRUPT_TYPE_SIPI              = 0x0006,
+    HV_X64_INTERRUPT_TYPE_EXTINT            = 0x0007,
+    HV_X64_INTERRUPT_TYPE_LOCALINT0         = 0x0008,
+    HV_X64_INTERRUPT_TYPE_LOCALINT1         = 0x0009,
+    HV_X64_INTERRUPT_TYPE_MAXIMUM           = 0x000A,
+#elif defined(__aarch64__)
+    HV_ARM64_INTERRUPT_TYPE_FIXED           = 0x0000,
+    HV_ARM64_INTERRUPT_TYPE_MAXIMUM         = 0x0008,
+#endif
+};
+
 union hv_interrupt_control {
     uint64_t as_uint64;
     struct {
         uint32_t interrupt_type; /* enum hv_interrupt type */
+#if defined(__x86_64__)
         uint32_t level_triggered:1;
         uint32_t logical_dest_mode:1;
         uint32_t rsvd:30;
+#elif defined(__aarch64__)
+        uint32_t rsvd1:2;
+        uint32_t asserted:1;
+        uint32_t rsvd2:29;
+#endif
     };
 };
 
include/system/mshv.h +2 -0
index 75286baf16..0db18fcee9 100644
@@ -47,10 +47,12 @@ extern bool mshv_allowed;
 typedef struct MshvState MshvState;
 extern MshvState *mshv_state;
 
+#if defined(__x86_64__)
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
                            uint32_t vp_index, bool logical_destination_mode,
                            bool level_triggered);
+#endif
 
 int mshv_irqchip_add_msi_route(int vector, PCIDevice *dev);
 int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev);
Signature · 1 line
2.45.4
#29 lore raw

PATCH v3 13/14 hw/intc,target/arm/mshv: add MSHV vGICv3 implementation

In reply to Aastha Rawat

From: "Anirudh Rayabharam (Microsoft)" <[email protected]>

Add in-kernel GICv3 support for the MSHV accelerator, following the same approach as the KVM and WHPX vGIC backends. The implementation handles IRQ injection via the HVCALL_ASSERT_VIRTUAL_INTERRUPT hypercall.

Introduce mshv_arch_pre_init_vm(), an arch-specific hook called after VM creation but before VM initialization, used on arm64 to configure GIC partition properties (GICD base, ITS translater base, timer and PMU PPI numbers). The x86 side provides a no-op stub.

Update the virt machine to treat MSHV like WHPX for GIC version selection (GICv3 only) and MSI controller finalization (no ITS support).

---
 accel/mshv/mshv-all.c              |   5 +
 hw/arm/virt.c                      |   8 +-
 hw/intc/arm_gicv3_common.c         |   3 +
 hw/intc/arm_gicv3_mshv.c           | 181 +++++++++++++++++++++++++++++++++++++
 hw/intc/meson.build                |   1 +
 include/hw/hyperv/hvgdk_mini.h     |   2 +
 include/hw/hyperv/hvhdk_mini.h     |   6 ++
 include/hw/intc/arm_gicv3_common.h |   1 +
 include/system/mshv_int.h          |   1 +
 target/arm/mshv/mshv-all.c         |  62 +++++++++++++
 target/i386/mshv/mshv-all.c        |   5 +
 11 files changed, 271 insertions(+), 4 deletions(-)
accel/mshv/mshv-all.c +5 -0
index 6c0ddbf3ca..628cd0ad17 100644
@@ -200,6 +200,11 @@ static int create_vm(int mshv_fd, int *vm_fd)
         return -1;
     }
 
+    ret = mshv_arch_pre_init_vm(*vm_fd);
+    if (ret < 0) {
+        return -1;
+    }
+
     ret = initialize_vm(*vm_fd);
     if (ret < 0) {
         return -1;
hw/arm/virt.c +4 -4
index 34eb5248a9..5f922ece9c 100644
@@ -2117,7 +2117,7 @@ static void finalize_gic_version(VirtMachineState *vms)
         /* KVM w/o kernel irqchip can only deal with GICv2 */
         gics_supported |= VIRT_GIC_VERSION_2_MASK;
         accel_name = "KVM with kernel-irqchip=off";
-    } else if (whpx_enabled()) {
+    } else if (whpx_enabled() || mshv_enabled()) {
         gics_supported |= VIRT_GIC_VERSION_3_MASK;
     } else if (tcg_enabled() || hvf_enabled() || qtest_enabled())  {
         gics_supported |= VIRT_GIC_VERSION_2_MASK;
@@ -2159,7 +2159,7 @@ static void finalize_msi_controller(VirtMachineState *vms)
     if (vms->msi_controller == VIRT_MSI_CTRL_AUTO) {
         if (vms->gic_version == VIRT_GIC_VERSION_2) {
             vms->msi_controller = VIRT_MSI_CTRL_GICV2M;
-        } else if (whpx_enabled()) {
+        } else if (whpx_enabled() || mshv_enabled()) {
             vms->msi_controller = VIRT_MSI_CTRL_GICV2M;
         } else {
             vms->msi_controller = VIRT_MSI_CTRL_ITS;
@@ -2176,8 +2176,8 @@ static void finalize_msi_controller(VirtMachineState *vms)
             error_report("GICv2 + ITS is an invalid configuration.");
             exit(1);
         }
-        if (whpx_enabled()) {
-            error_report("ITS not supported on WHPX.");
+        if (whpx_enabled() || mshv_enabled()) {
+            error_report("ITS not supported on WHPX and MSHV.");
             exit(1);
         }
     }
hw/intc/arm_gicv3_common.c +3 -0
index 9200671c7a..239c645ab9 100644
@@ -33,6 +33,7 @@
 #include "hw/arm/linux-boot-if.h"
 #include "system/kvm.h"
 #include "system/whpx.h"
+#include "system/mshv.h"
 
 
 static void gicv3_gicd_no_migration_shift_bug_post_load(GICv3State *cs)
@@ -658,6 +659,8 @@ const char *gicv3_class_name(void)
         return "kvm-arm-gicv3";
     } else if (whpx_enabled()) {
         return TYPE_WHPX_GICV3;
+    } else if (mshv_enabled()) {
+        return TYPE_MSHV_GICV3;
     } else {
         if (kvm_enabled()) {
             error_report("Userspace GICv3 is not supported with KVM");
hw/intc/arm_gicv3_mshv.c new file +181 -0

181 lines not on this page · raw message

hw/intc/meson.build +1 -0
index 96742df090..9d824db582 100644
@@ -43,6 +43,7 @@ arm_common_ss.add(when: 'CONFIG_ARM_GICV3', if_true: files('arm_gicv3_cpuif.c'))
 specific_ss.add(when: 'CONFIG_ARM_GIC_KVM', if_true: files('arm_gic_kvm.c'))
 specific_ss.add(when: ['CONFIG_WHPX', 'TARGET_AARCH64'], if_true: files('arm_gicv3_whpx.c'))
 specific_ss.add(when: ['CONFIG_ARM_GIC_KVM', 'TARGET_AARCH64'], if_true: files('arm_gicv3_kvm.c', 'arm_gicv3_its_kvm.c'))
+specific_ss.add(when: ['CONFIG_MSHV', 'TARGET_AARCH64'], if_true: files('arm_gicv3_mshv.c'))
 arm_common_ss.add(when: 'CONFIG_ARM_V7M', if_true: files('armv7m_nvic.c'))
 specific_ss.add(when: 'CONFIG_GRLIB', if_true: files('grlib_irqmp.c'))
 specific_ss.add(when: 'CONFIG_IOAPIC', if_true: files('ioapic.c'))
include/hw/hyperv/hvgdk_mini.h +2 -0
index eb766734d6..b50a65a0d4 100644
@@ -66,6 +66,8 @@ typedef enum hv_register_name {
     HV_ARM64_REGISTER_MIDR_EL1   = 0x00040051,
     HV_ARM64_REGISTER_MPIDR_EL1  = 0x00040001,
 
+    HV_ARM64_REGISTER_GICR_BASE_GPA       = 0x00063000,
+
 #elif defined(__x86_64__)
     /* X64 User-Mode Registers */
     HV_X64_REGISTER_RAX     = 0x00020000,
include/hw/hyperv/hvhdk_mini.h +6 -0
index 9c2f3cf5ae..4d78fa0677 100644
@@ -62,6 +62,12 @@ enum hv_partition_property_code {
     HV_PARTITION_PROPERTY_ISOLATION_POLICY                       = 0x00050014,
     HV_PARTITION_PROPERTY_UNIMPLEMENTED_MSR_ACTION               = 0x00050017,
     HV_PARTITION_PROPERTY_SEV_VMGEXIT_OFFLOADS                   = 0x00050022,
+    HV_PARTITION_PROPERTY_GICD_BASE_ADDRESS                      = 0x00050028,
+    HV_PARTITION_PROPERTY_GITS_TRANSLATER_BASE_ADDRESS           = 0x00050029,
+    HV_PARTITION_PROPERTY_GIC_LPI_INT_ID_BITS                    = 0x0005002A,
+    HV_PARTITION_PROPERTY_GIC_PPI_OVERFLOW_INTERRUPT_FROM_CNTV   = 0x0005002B,
+    HV_PARTITION_PROPERTY_GIC_PPI_OVERFLOW_INTERRUPT_FROM_CNTP   = 0x0005002C,
+    HV_PARTITION_PROPERTY_GIC_PPI_PERFORMANCE_MONITORS_INTERRUPT = 0x0005002D,
 
     /* Compatibility properties */
     HV_PARTITION_PROPERTY_PROCESSOR_VENDOR              = 0x00060000,
include/hw/intc/arm_gicv3_common.h +1 -0
index c55cf18120..3baac1a35c 100644
@@ -315,6 +315,7 @@ DECLARE_OBJ_CHECKERS(GICv3State, ARMGICv3CommonClass,
 
 /* Types for GICv3 kernel-irqchip */
 #define TYPE_WHPX_GICV3 "whpx-arm-gicv3"
+#define TYPE_MSHV_GICV3 "mshv-arm-gicv3"
 
 struct ARMGICv3CommonClass {
     /*< private >*/
include/system/mshv_int.h +1 -0
index c72c91cd23..0ef98abedf 100644
@@ -97,6 +97,7 @@ void mshv_arch_destroy_vcpu(CPUState *cpu);
 void mshv_arch_amend_proc_features(
     union hv_partition_synthetic_processor_features *features);
 int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd);
+int mshv_arch_pre_init_vm(int vm_fd);
 int mshv_arch_post_init_vm(int vm_fd);
 void mshv_setup_hvcall_args(AccelCPUState *state);
 
target/arm/mshv/mshv-all.c +62 -0

68 lines not on this page · raw message

target/i386/mshv/mshv-all.c +5 -0
index f0b43aa86f..ce8b426ea4 100644
@@ -39,6 +39,11 @@ int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
     return 0;
 }
 
+int mshv_arch_pre_init_vm(int vm_fd)
+{
+    return 0;
+}
+
 /*
  * Default Microsoft Hypervisor behavior for unimplemented MSR is to send a
  * fault to the guest if it tries to access it. It is possible to override
Signature · 1 line
2.45.4
#30 lore raw

PATCH v3 14/14 MAINTAINERS: updates for MSHV arm64 code

In reply to Aastha Rawat

From: "Anirudh Rayabharam (Microsoft)" <[email protected]>

Add myself and Aastha Rawat as maintainers.

Add Magnus Kulke as reviewer.

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index 9d1614fd7e..e36b10a8dc 100644
@@ -606,6 +606,14 @@ R: Wei Liu <[email protected]>
 S: Supported
 F: target/i386/mshv/
 
+ARM64 MSHV CPUs
+M: Anirudh Rayabharam <[email protected]>
+M: Aastha Rawat <[email protected]>
+R: Magnus Kulke <[email protected]>
+S: Supported
+F: target/arm/mshv/
+F: hw/intc/arm_gicv3_mshv.c
+
 X86 Instruction Emulator
 M: Cameron Esfahani <[email protected]>
 M: Roman Bolshakov <[email protected]>
Signature · 1 line
2.45.4