Skip to contentSkip to conversation

Add ARM64 support for MSHV accelerator

PATCH In review Aastha Rawat 5 participants 55 messages Oct 1 – Nov 24 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v6 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/

---
Changes in v4:
- Split "target/arm: convert host CPU features ifdef ladder to runtime
checks" out of the -cpu host patch to separate the refactoring.
- Refactored MMIO emulation helper (arm_emulate_mmio) to use callback
ops for per-register access avoiding full CPU state sync for single
register MMIO operations for HVF & WHPX.
- Reordered patches: moved "arch-specific accelerator init hook" earlier
(patch 3), before the arm64 build enablement.
- Updated cpu_thread_is_idle() comment to be arch-neutral.
- Link to v3: https://lore.kernel.org/qemu-devel/20260421-mshv_accel_arm64_supp-v3-0-469f544778ba@linux.microsoft.com

---
Changes in v5:
- Rebased onto v11.1.0 rc0.
- Dropped "accel/mshv: implement cpu_thread_is_idle() hook" as it has been merged upstream and is now part of the base.
- Dropped "accel/mshv: move msr.c to target/i386" for the same reason (msr.c now lives in target/i386/mshv/ upstream).
- Adapted the arm64 code to MSHV API changes already present in the new base:
- * follow the mshv_arch_put_registers() -> mshv_arch_load_vcpu_state() /

       mshv_arch_store_vcpu_state() rename;

- * add an arm64 stub for the new mshv_arch_disable_partition_proc_features()
hook and guard the x86-only xsave handling in create_partition() with
#if defined(__x86_64__).
- Made the arm64 register helpers file-local static (load_regs(), store_regs(), get_standard_regs()).
- Link to v4: https://lore.kernel.org/qemu-devel/20260527-mshv_accel_arm64_supp-v4-0-66d871dcdc53@linux.microsoft.com

---
Changes in v6:
- Rebased to the latest master.
- Fixed an MSHV header-order dependency exposed by the rebase.
- Made MSHV migration initialization architecture-specific; kept MSR,
MP-state, and clock restore code in the x86 backend.
- Added an empty ARM migration-init hook; migration remains unsupported.
- Cleared the x86-only TLB-flush enlightenment on ARM.
- Link to v5: https://lore.kernel.org/qemu-devel/20260717-mshv_accel_arm64_supp-v5-0-ef07d4c6b337@linux.microsoft.com

---
Aastha Rawat (9):
accel/mshv: extract common CPU register helpers accel/mshv: make MSHV migration initialization arch-specific meson, target/arm/mshv: Enable arm64 build & add initial MSHV support target/arm/mshv: implement vcpu state operations for ARM64 target/arm: convert host CPU feature ifdef ladder to runtime checks 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

 MAINTAINERS                        |   8 +
 accel/mshv/irq.c                   |   2 +
 accel/mshv/meson.build             |   3 +-
 accel/mshv/mshv-all.c              |  56 ++--
 accel/mshv/mshv-cpu-common.c       | 151 ++++++++++
 hw/arm/virt.c                      |  11 +-
 hw/i386/mshv/clock.c               |   2 +-
 hw/intc/arm_gicv3_common.c         |   3 +
 hw/intc/arm_gicv3_mshv.c           | 181 ++++++++++++
 hw/intc/meson.build                |   2 +
 include/hw/hyperv/hvgdk_mini.h     | 129 ++++++++
 include/hw/hyperv/hvhdk.h          |  91 +++++-
 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              |   4 +-
 include/system/mshv_int.h          |   4 +
 meson.build                        |   8 +-
 target/arm/cpu.c                   |   8 +-
 target/arm/cpu64.c                 |  27 +-
 target/arm/helper.c                |  62 ++++
 target/arm/helper.h                |  16 +
 target/arm/hvf/hvf.c               |  57 ++--
 target/arm/meson.build             |   1 +
 target/arm/mshv/meson.build        |   7 +
 target/arm/mshv/mshv-all.c         | 585 +++++++++++++++++++++++++++++++++++++
 target/arm/mshv_arm.h              |  18 ++
 target/arm/syndrome.h              |  47 +++
 target/arm/whpx/whpx-all.c         |  45 ++-
 target/i386/mshv/meson.build       |   1 +
 target/i386/mshv/mshv-all.c        | 119 ++++++++
 target/i386/mshv/mshv-cpu.c        | 165 +----------
 32 files changed, 1542 insertions(+), 281 deletions(-)

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

Best regards,

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

PATCH v6 1/14 accel/mshv: extract common CPU register helpers

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    |   1 +
 target/i386/mshv/mshv-cpu.c  | 125 +----------------------------------
 4 files changed, 155 insertions(+), 125 deletions(-)
accel/mshv/meson.build +2 -1
index e433187cde0..ebe32921b3d 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 +1 -0
index 3dffe3c5fbf..2e8caad8fde 100644
@@ -107,6 +107,7 @@ void mshv_arch_amend_proc_features(
 void mshv_arch_disable_partition_proc_features(
      union hv_partition_processor_features *disabled_features);
 int mshv_arch_post_init_vm(int vm_fd);
+void mshv_setup_hvcall_args(AccelCPUState *state);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-cpu.c +1 -124

149 lines not on this page · raw message

Signature · 1 line
2.45.4
Reviewed-by on v6 1/14
#2 lore raw

Re: [PATCH v6 01/14] accel/mshv: extract common CPU register helpers

Quoted 356 lines · 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    |   1 +
 target/i386/mshv/mshv-cpu.c  | 125 +----------------------------------
 4 files changed, 155 insertions(+), 125 deletions(-)
accel/mshv/meson.build +2 -1
index e433187cde0..ebe32921b3d 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
new file mode 100644
index 00000000000..e8ef34689e3
@@ -0,0 +1,151 @@
+/*
+ * QEMU MSHV common CPU register helpers
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Ziqiao Zhou   <[email protected]>
+ *          Magnus Kulke  <[email protected]>
+ *          Jinank Jain   <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+#include "qemu/memalign.h"
+
+#include "system/mshv.h"
+#include "linux/mshv.h"
+#include "system/mshv_int.h"
+
+#include "hw/core/cpu.h"
+
+#include <sys/ioctl.h>
+
+int mshv_set_generic_regs(const CPUState *cpu, const hv_register_assoc *assocs,
+                          size_t n_regs)
+{
+    int cpu_fd = mshv_vcpufd(cpu);
+    int vp_index = cpu->cpu_index;
+    size_t in_sz, assocs_sz;
+    hv_input_set_vp_registers *in = cpu->accel->hvcall_args.input_page;
+    struct mshv_root_hvcall args = {0};
+    int ret;
+
+    /* find out the size of the struct w/ a flexible array at the tail */
+    assocs_sz = n_regs * sizeof(hv_register_assoc);
+    in_sz = sizeof(hv_input_set_vp_registers) + assocs_sz;
+
+    /* fill the input struct */
+    memset(in, 0, sizeof(hv_input_set_vp_registers));
+    in->vp_index = vp_index;
+    memcpy(in->elements, assocs, assocs_sz);
+
+    /* create the hvcall envelope */
+    args.code = HVCALL_SET_VP_REGISTERS;
+    args.in_sz = in_sz;
+    args.in_ptr = (uint64_t) in;
+    args.reps = (uint16_t) n_regs;
+
+    /* perform the call */
+    ret = mshv_hvcall(cpu_fd, &args);
+    if (ret < 0) {
+        error_report("Failed to set registers");
+        return -1;
+    }
+
+    /* assert we set all registers */
+    if (args.reps != n_regs) {
+        error_report("Failed to set registers: expected %zu elements"
+                     ", got %u", n_regs, args.reps);
+        return -1;
+    }
+
+    return 0;
+}
+
+int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs,
+                            size_t n_regs)
+{
+    int cpu_fd = mshv_vcpufd(cpu);
+    int vp_index = cpu->cpu_index;
+    hv_input_get_vp_registers *in = cpu->accel->hvcall_args.input_page;
+    hv_register_value *values = cpu->accel->hvcall_args.output_page;
+    size_t in_sz, names_sz, values_sz;
+    int i, ret;
+    struct mshv_root_hvcall args = {0};
+
+    /* find out the size of the struct w/ a flexible array at the tail */
+    names_sz = n_regs * sizeof(hv_register_name);
+    in_sz = sizeof(hv_input_get_vp_registers) + names_sz;
+
+    /* fill the input struct */
+    memset(in, 0, sizeof(hv_input_get_vp_registers));
+    in->vp_index = vp_index;
+    for (i = 0; i < n_regs; i++) {
+        in->names[i] = assocs[i].name;
+    }
+
+    /* determine size of value output buffer */
+    values_sz = n_regs * sizeof(union hv_register_value);
+
+    /* create the hvcall envelope */
+    args.code = HVCALL_GET_VP_REGISTERS;
+    args.in_sz = in_sz;
+    args.in_ptr = (uint64_t) in;
+    args.out_sz = values_sz;
+    args.out_ptr = (uint64_t) values;
+    args.reps = (uint16_t) n_regs;
+
+    /* perform the call */
+    ret = mshv_hvcall(cpu_fd, &args);
+    if (ret < 0) {
+        error_report("Failed to retrieve registers");
+        return -1;
+    }
+
+    /* assert we got all registers */
+    if (args.reps != n_regs) {
+        error_report("Failed to retrieve registers: expected %zu elements"
+                     ", got %u", n_regs, args.reps);
+        return -1;
+    }
+
+    /* copy values into assoc */
+    for (i = 0; i < n_regs; i++) {
+        assocs[i].value = values[i];
+    }
+
+    return 0;
+}
+
+int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd)
+{
+    int ret;
+    struct mshv_create_vp vp_arg = {
+        .vp_index = vp_index,
+    };
+
+    ret = ioctl(vm_fd, MSHV_CREATE_VP, &vp_arg);
+    if (ret < 0) {
+        error_report("failed to create mshv vcpu: %s", strerror(errno));
+        return -1;
+    }
+
+    *cpu_fd = ret;
+
+    return 0;
+}
+
+void mshv_remove_vcpu(int vm_fd, int cpu_fd)
+{
+    close(cpu_fd);
+}
+
+void mshv_setup_hvcall_args(AccelCPUState *state)
+{
+    void *mem = qemu_memalign(HV_HYP_PAGE_SIZE, 2 * HV_HYP_PAGE_SIZE);
+
+    state->hvcall_args.base = mem;
+    state->hvcall_args.input_page = mem;
+    state->hvcall_args.output_page = (uint8_t *)mem + HV_HYP_PAGE_SIZE;
+}
include/system/mshv_int.h +1 -0
index 3dffe3c5fbf..2e8caad8fde 100644
@@ -107,6 +107,7 @@ void mshv_arch_amend_proc_features(
 void mshv_arch_disable_partition_proc_features(
      union hv_partition_processor_features *disabled_features);
 int mshv_arch_post_init_vm(int vm_fd);
+void mshv_setup_hvcall_args(AccelCPUState *state);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-cpu.c +1 -124
index f528dd2b9a2..3eee097432a 100644
@@ -357,103 +357,6 @@ static int translate_gva(const CPUState *cpu, uint64_t gva, uint64_t *gpa,
     return 0;
 }
 
-int mshv_set_generic_regs(const CPUState *cpu, const hv_register_assoc *assocs,
-                          size_t n_regs)
-{
-    int cpu_fd = mshv_vcpufd(cpu);
-    int vp_index = cpu->cpu_index;
-    size_t in_sz, assocs_sz;
-    hv_input_set_vp_registers *in = cpu->accel->hvcall_args.input_page;
-    struct mshv_root_hvcall args = {0};
-    int ret;
-
-    /* find out the size of the struct w/ a flexible array at the tail */
-    assocs_sz = n_regs * sizeof(hv_register_assoc);
-    in_sz = sizeof(hv_input_set_vp_registers) + assocs_sz;
-
-    /* fill the input struct */
-    memset(in, 0, sizeof(hv_input_set_vp_registers));
-    in->vp_index = vp_index;
-    memcpy(in->elements, assocs, assocs_sz);
-
-    /* create the hvcall envelope */
-    args.code = HVCALL_SET_VP_REGISTERS;
-    args.in_sz = in_sz;
-    args.in_ptr = (uint64_t) in;
-    args.reps = (uint16_t) n_regs;
-
-    /* perform the call */
-    ret = mshv_hvcall(cpu_fd, &args);
-    if (ret < 0) {
-        error_report("Failed to set registers");
-        return -1;
-    }
-
-    /* assert we set all registers */
-    if (args.reps != n_regs) {
-        error_report("Failed to set registers: expected %zu elements"
-                     ", got %u", n_regs, args.reps);
-        return -1;
-    }
-
-    return 0;
-}
-
-int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs,
-                          size_t n_regs)
-{
-    int cpu_fd = mshv_vcpufd(cpu);
-    int vp_index = cpu->cpu_index;
-    hv_input_get_vp_registers *in = cpu->accel->hvcall_args.input_page;
-    hv_register_value *values = cpu->accel->hvcall_args.output_page;
-    size_t in_sz, names_sz, values_sz;
-    int i, ret;
-    struct mshv_root_hvcall args = {0};
-
-    /* find out the size of the struct w/ a flexible array at the tail */
-    names_sz = n_regs * sizeof(hv_register_name);
-    in_sz = sizeof(hv_input_get_vp_registers) + names_sz;
-
-    /* fill the input struct */
-    memset(in, 0, sizeof(hv_input_get_vp_registers));
-    in->vp_index = vp_index;
-    for (i = 0; i < n_regs; i++) {
-        in->names[i] = assocs[i].name;
-    }
-
-    /* determine size of value output buffer */
-    values_sz = n_regs * sizeof(union hv_register_value);
-
-    /* create the hvcall envelope */
-    args.code = HVCALL_GET_VP_REGISTERS;
-    args.in_sz = in_sz;
-    args.in_ptr = (uint64_t) in;
-    args.out_sz = values_sz;
-    args.out_ptr = (uint64_t) values;
-    args.reps = (uint16_t) n_regs;
-
-    /* perform the call */
-    ret = mshv_hvcall(cpu_fd, &args);
-    if (ret < 0) {
-        error_report("Failed to retrieve registers");
-        return -1;
-    }
-
-    /* assert we got all registers */
-    if (args.reps != n_regs) {
-        error_report("Failed to retrieve registers: expected %zu elements"
-                     ", got %u", n_regs, args.reps);
-        return -1;
-    }
-
-    /* copy values into assoc */
-    for (i = 0; i < n_regs; i++) {
-        assocs[i].value = values[i];
-    }
-
-    return 0;
-}
-
 static int set_standard_regs(const CPUState *cpu)
 {
     X86CPU *x86cpu = X86_CPU(cpu);
@@ -2040,29 +1943,6 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
     return 0;
 }
 
-void mshv_remove_vcpu(int vm_fd, int cpu_fd)
-{
-    close(cpu_fd);
-}
-
-
-int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd)
-{
-    int ret;
-    struct mshv_create_vp vp_arg = {
-        .vp_index = vp_index,
-    };
-    ret = ioctl(vm_fd, MSHV_CREATE_VP, &vp_arg);
-    if (ret < 0) {
-        error_report("failed to create mshv vcpu: %s", strerror(errno));
-        return -1;
-    }
-
-    *cpu_fd = ret;
-
-    return 0;
-}
-
 static void read_segment_descriptor(CPUState *cpu,
                                     struct x86_segment_descriptor *desc,
                                     enum X86Seg seg_idx)
@@ -2121,7 +2001,6 @@ void mshv_arch_init_vcpu(CPUState *cpu)
     CPUX86State *env = &x86_cpu->env;
     AccelCPUState *state = cpu->accel;
     size_t page = HV_HYP_PAGE_SIZE, xsave_len;
-    void *mem = qemu_memalign(page, 2 * page);
     int ret;
     X86XSaveHeader *header;
 
@@ -2143,9 +2022,7 @@ void mshv_arch_init_vcpu(CPUState *cpu)
         env->regs_page = (struct hv_vp_register_page *) rp;
     }
 
-    state->hvcall_args.base = mem;
-    state->hvcall_args.input_page = mem;
-    state->hvcall_args.output_page = (uint8_t *)mem + page;
+    mshv_setup_hvcall_args(state);
 
     env->emu_mmio_buf = g_new(char, 4096);
 
Signature · 2 lines
-- 
2.45.4
#3 lore raw

PATCH v6 2/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 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/i386/mshv/meson.build |  1 +
 target/i386/mshv/mshv-all.c  | 80 ++++++++++++++++++++++++++++++++++++++++++++
 target/i386/mshv/mshv-cpu.c  | 40 ----------------------
 5 files changed, 86 insertions(+), 41 deletions(-)
accel/mshv/mshv-all.c +4 -1
index 5921ce693ee..860f511fcba 100644
@@ -544,7 +544,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;
+    }
 
     ret = create_vm(mshv_fd, &vm_fd);
     if (ret < 0) {
include/system/mshv_int.h +1 -0
index 2e8caad8fde..0e68365c63f 100644
@@ -106,6 +106,7 @@ void mshv_arch_amend_proc_features(
     union hv_partition_synthetic_processor_features *features);
 void mshv_arch_disable_partition_proc_features(
      union hv_partition_processor_features *disabled_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);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
target/i386/mshv/meson.build +1 -0
index 31ff4cc9950..0e95c5a00b9 100644
@@ -1,6 +1,7 @@
 i386_mshv_ss = ss.source_set()
 
 i386_mshv_ss.add(files(
+  'mshv-all.c',
   'mshv-cpu.c',
   'msr.c',
   'synic.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 3eee097432a..f1056bd215e 100644
@@ -2125,46 +2125,6 @@ uint32_t mshv_get_supported_cpuid(uint32_t func, uint32_t idx, int reg)
     return ret;
 }
 
-/*
- * 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;
-}
-
 static void mshv_cpu_xsave_init(void)
 {
     static bool first = true;
Signature · 1 line
2.45.4
#4 lore raw

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

Quoted 25 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 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/i386/mshv/meson.build |  1 +
 target/i386/mshv/mshv-all.c  | 80 ++++++++++++++++++++++++++++++++++++++++++++
 target/i386/mshv/mshv-cpu.c  | 40 ----------------------
 5 files changed, 86 insertions(+), 41 deletions(-)
accel/mshv/mshv-all.c +0 -0
index 5921ce693ee..860f511fcba 100644

accel/mshv/mshv-all.c static int mshv_init(AccelState *as, MachineState *ms) · line 548

Quoted 6 lines
accel/mshv/mshv-all.c +2 -1
@@ -544,7 +544,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) {

now that this is fallible, we probably want to close the mshv_fd before returning.

Quoted 169 lines
accel/mshv/mshv-all.c +2 -0
+        return -1;
+    }
 
     ret = create_vm(mshv_fd, &vm_fd);
     if (ret < 0) {
include/system/mshv_int.h +1 -0
index 2e8caad8fde..0e68365c63f 100644
@@ -106,6 +106,7 @@ void mshv_arch_amend_proc_features(
     union hv_partition_synthetic_processor_features *features);
 void mshv_arch_disable_partition_proc_features(
      union hv_partition_processor_features *disabled_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);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
target/i386/mshv/meson.build +1 -0
index 31ff4cc9950..0e95c5a00b9 100644
@@ -1,6 +1,7 @@
 i386_mshv_ss = ss.source_set()
 
 i386_mshv_ss.add(files(
+  'mshv-all.c',
   'mshv-cpu.c',
   'msr.c',
   'synic.c',
target/i386/mshv/mshv-all.c new file +80 -0
new file mode 100644
index 00000000000..f0b43aa86f1
@@ -0,0 +1,80 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Ziqiao Zhou   <[email protected]>
+ *          Magnus Kulke  <[email protected]>
+ *          Jinank Jain   <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+#include "qemu/memalign.h"
+
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+#include "system/address-spaces.h"
+#include "linux/mshv.h"
+#include "hw/hyperv/hvgdk.h"
+#include "hw/hyperv/hvgdk_mini.h"
+#include "hw/hyperv/hvhdk_mini.h"
+#include "hw/i386/apic_internal.h"
+
+#include "cpu.h"
+#include "emulate/x86_decode.h"
+#include "emulate/x86_emu.h"
+#include "emulate/x86_flags.h"
+
+#include "trace-accel_mshv.h"
+#include "trace.h"
+
+#include <sys/ioctl.h>
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    mshv_init_mmio_emu();
+    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
+ * 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;
+}
target/i386/mshv/mshv-cpu.c +0 -40
index 3eee097432a..f1056bd215e 100644
@@ -2125,46 +2125,6 @@ uint32_t mshv_get_supported_cpuid(uint32_t func, uint32_t idx, int reg)
     return ret;
 }
 
-/*
- * 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;
-}
-
 static void mshv_cpu_xsave_init(void)
 {
     static bool first = true;
Signature · 2 lines
-- 
2.45.4
#5 lore raw

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

In reply to Aastha Rawat
Quoted 25 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 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/i386/mshv/meson.build |  1 +
 target/i386/mshv/mshv-all.c  | 80 ++++++++++++++++++++++++++++++++++++++++++++
 target/i386/mshv/mshv-cpu.c  | 40 ----------------------
 5 files changed, 86 insertions(+), 41 deletions(-)
accel/mshv/mshv-all.c +0 -0
index 5921ce693ee..860f511fcba 100644

accel/mshv/mshv-all.c static int mshv_init(AccelState *as, MachineState *ms) · line 548

Quoted 6 lines
accel/mshv/mshv-all.c +2 -1
@@ -544,7 +544,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) {

since this is now fallible, we probably want to close mshv_fd before returning here

Quoted 169 lines
accel/mshv/mshv-all.c +2 -0
+        return -1;
+    }
 
     ret = create_vm(mshv_fd, &vm_fd);
     if (ret < 0) {
include/system/mshv_int.h +1 -0
index 2e8caad8fde..0e68365c63f 100644
@@ -106,6 +106,7 @@ void mshv_arch_amend_proc_features(
     union hv_partition_synthetic_processor_features *features);
 void mshv_arch_disable_partition_proc_features(
      union hv_partition_processor_features *disabled_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);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
target/i386/mshv/meson.build +1 -0
index 31ff4cc9950..0e95c5a00b9 100644
@@ -1,6 +1,7 @@
 i386_mshv_ss = ss.source_set()
 
 i386_mshv_ss.add(files(
+  'mshv-all.c',
   'mshv-cpu.c',
   'msr.c',
   'synic.c',
target/i386/mshv/mshv-all.c new file +80 -0
new file mode 100644
index 00000000000..f0b43aa86f1
@@ -0,0 +1,80 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Ziqiao Zhou   <[email protected]>
+ *          Magnus Kulke  <[email protected]>
+ *          Jinank Jain   <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+#include "qemu/memalign.h"
+
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+#include "system/address-spaces.h"
+#include "linux/mshv.h"
+#include "hw/hyperv/hvgdk.h"
+#include "hw/hyperv/hvgdk_mini.h"
+#include "hw/hyperv/hvhdk_mini.h"
+#include "hw/i386/apic_internal.h"
+
+#include "cpu.h"
+#include "emulate/x86_decode.h"
+#include "emulate/x86_emu.h"
+#include "emulate/x86_flags.h"
+
+#include "trace-accel_mshv.h"
+#include "trace.h"
+
+#include <sys/ioctl.h>
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    mshv_init_mmio_emu();
+    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
+ * 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;
+}
target/i386/mshv/mshv-cpu.c +0 -40
index 3eee097432a..f1056bd215e 100644
@@ -2125,46 +2125,6 @@ uint32_t mshv_get_supported_cpuid(uint32_t func, uint32_t idx, int reg)
     return ret;
 }
 
-/*
- * 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;
-}
-
 static void mshv_cpu_xsave_init(void)
 {
     static bool first = true;
Signature · 2 lines
-- 
2.45.4
#6 lore raw

PATCH v6 3/14 accel/mshv: make MSHV migration initialization arch-specific

In reply to Aastha Rawat

x86 specific migration handlers currently live in common MSHV code. Move these handlers into x86 backend and add a generic architecture migration initialization hook. ARM MSHV live migration support is not added yet, this will allow ARM to add an empty initialization hook.

---
 accel/mshv/mshv-all.c       | 35 ++++-------------------------------
 include/system/mshv_int.h   |  1 +
 target/i386/mshv/mshv-all.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 31 deletions(-)
accel/mshv/mshv-all.c +4 -31
index 860f511fcba..efe19a39140 100644
@@ -60,30 +60,6 @@ static int init_mshv(int *mshv_fd)
     return 0;
 }
 
-static int mshv_load_cleanup(void *opaque)
-{
-    CPUState *cpu;
-    int ret;
-
-    ret = mshv_arch_set_partition_msrs(first_cpu);
-    if (ret < 0) {
-        error_report("Failed to set partition MSRs: %s", strerror(-ret));
-        return -1;
-    }
-
-    CPU_FOREACH(cpu) {
-        ret = mshv_arch_set_mp_state(cpu);
-        if (ret < 0) {
-            error_report("Failed to set mp state for vCPU %d: %s",
-                         cpu->cpu_index, strerror(-ret));
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int get_host_partition_property(int mshv_fd, uint32_t property_code,
                                        uint64_t *value)
 {
@@ -519,10 +495,6 @@ static int mshv_init_vcpu(CPUState *cpu)
     return 0;
 }
 
-static SaveVMHandlers savevm_mshv = {
-    .load_cleanup = mshv_load_cleanup,
-};
-
 static int mshv_init(AccelState *as, MachineState *ms)
 {
     MshvState *s;
@@ -574,9 +546,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
                                   0, "mshv-memory");
     memory_listener_register(&mshv_io_listener, &address_space_io);
 
-    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
-
-    mshv_clock_init();
+    ret = mshv_arch_migration_init(s);
+    if (ret < 0) {
+        return ret;
+    }
 
     return 0;
 }
include/system/mshv_int.h +1 -0
index 0e68365c63f..31d2f4381c7 100644
@@ -109,6 +109,7 @@ void mshv_arch_disable_partition_proc_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);
+int mshv_arch_migration_init(MshvState *s);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-all.c +34 -0
index f0b43aa86f1..968fe2a4494 100644
@@ -78,3 +78,37 @@ int mshv_arch_post_init_vm(int vm_fd)
 
     return ret;
 }
+
+static int mshv_load_cleanup(void *opaque)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = mshv_arch_set_partition_msrs(first_cpu);
+    if (ret < 0) {
+        error_report("Failed to set partition MSRs: %s", strerror(-ret));
+        return -1;
+    }
+
+    CPU_FOREACH(cpu) {
+        ret = mshv_arch_set_mp_state(cpu);
+        if (ret < 0) {
+            error_report("Failed to set mp state for vCPU %d: %s",
+                         cpu->cpu_index, strerror(-ret));
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+static SaveVMHandlers savevm_mshv = {
+    .load_cleanup = mshv_load_cleanup,
+};
+
+int mshv_arch_migration_init(MshvState *s)
+{
+    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
+    mshv_clock_init();
+    return 0;
+}
Signature · 1 line
2.45.4
#7 lore raw

Re: [PATCH v6 03/14] accel/mshv: make MSHV migration initialization arch-specific

Quoted 106 lines · Aastha Rawat

x86 specific migration handlers currently live in common MSHV code. Move these handlers into x86 backend and add a generic architecture migration initialization hook. ARM MSHV live migration support is not added yet, this will allow ARM to add an empty initialization hook.

---
 accel/mshv/mshv-all.c       | 35 ++++-------------------------------
 include/system/mshv_int.h   |  1 +
 target/i386/mshv/mshv-all.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 31 deletions(-)
accel/mshv/mshv-all.c +4 -31
index 860f511fcba..efe19a39140 100644
@@ -60,30 +60,6 @@ static int init_mshv(int *mshv_fd)
     return 0;
 }
 
-static int mshv_load_cleanup(void *opaque)
-{
-    CPUState *cpu;
-    int ret;
-
-    ret = mshv_arch_set_partition_msrs(first_cpu);
-    if (ret < 0) {
-        error_report("Failed to set partition MSRs: %s", strerror(-ret));
-        return -1;
-    }
-
-    CPU_FOREACH(cpu) {
-        ret = mshv_arch_set_mp_state(cpu);
-        if (ret < 0) {
-            error_report("Failed to set mp state for vCPU %d: %s",
-                         cpu->cpu_index, strerror(-ret));
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int get_host_partition_property(int mshv_fd, uint32_t property_code,
                                        uint64_t *value)
 {
@@ -519,10 +495,6 @@ static int mshv_init_vcpu(CPUState *cpu)
     return 0;
 }
 
-static SaveVMHandlers savevm_mshv = {
-    .load_cleanup = mshv_load_cleanup,
-};
-
 static int mshv_init(AccelState *as, MachineState *ms)
 {
     MshvState *s;
@@ -574,9 +546,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
                                   0, "mshv-memory");
     memory_listener_register(&mshv_io_listener, &address_space_io);
 
-    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
-
-    mshv_clock_init();
+    ret = mshv_arch_migration_init(s);
+    if (ret < 0) {
+        return ret;
+    }
 
     return 0;
 }
include/system/mshv_int.h +1 -0
index 0e68365c63f..31d2f4381c7 100644
@@ -109,6 +109,7 @@ void mshv_arch_disable_partition_proc_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);
+int mshv_arch_migration_init(MshvState *s);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-all.c +14 -0
index f0b43aa86f1..968fe2a4494 100644
@@ -78,3 +78,37 @@ int mshv_arch_post_init_vm(int vm_fd)
 
     return ret;
 }
+
+static int mshv_load_cleanup(void *opaque)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = mshv_arch_set_partition_msrs(first_cpu);
+    if (ret < 0) {
+        error_report("Failed to set partition MSRs: %s", strerror(-ret));
+        return -1;
+    }
+
+    CPU_FOREACH(cpu) {
+        ret = mshv_arch_set_mp_state(cpu);

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 104

Quoted 10 lines
target/i386/mshv/mshv-all.c +10 -0
+        if (ret < 0) {
+            error_report("Failed to set mp state for vCPU %d: %s",
+                         cpu->cpu_index, strerror(-ret));
+            return -1;
+        }
+    }
+
+    return 0;
+}
+

For me, on x86 this does not build. The SaveVMHandlers type is unknown.

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 110

Quoted 6 lines
target/i386/mshv/mshv-all.c +6 -0
+static SaveVMHandlers savevm_mshv = {
+    .load_cleanup = mshv_load_cleanup,
+};
+
+int mshv_arch_migration_init(MshvState *s)
+{

Also, this function here is seen as implicit declaration. I think there's a missing include in this file.

Quoted 7 lines
target/i386/mshv/mshv-all.c +4 -0
+    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
+    mshv_clock_init();
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#8 lore raw

Re: [PATCH v6 03/14] accel/mshv: make MSHV migration initialization arch-specific

Quoted 111 lines · Doru Blânzeanu
Quoted 110 lines · Aastha Rawat

x86 specific migration handlers currently live in common MSHV code. Move these handlers into x86 backend and add a generic architecture migration initialization hook. ARM MSHV live migration support is not added yet, this will allow ARM to add an empty initialization hook.

---
 accel/mshv/mshv-all.c       | 35 ++++-------------------------------
 include/system/mshv_int.h   |  1 +
 target/i386/mshv/mshv-all.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 31 deletions(-)
accel/mshv/mshv-all.c +4 -31
index 860f511fcba..efe19a39140 100644
@@ -60,30 +60,6 @@ static int init_mshv(int *mshv_fd)
     return 0;
 }
 
-static int mshv_load_cleanup(void *opaque)
-{
-    CPUState *cpu;
-    int ret;
-
-    ret = mshv_arch_set_partition_msrs(first_cpu);
-    if (ret < 0) {
-        error_report("Failed to set partition MSRs: %s", strerror(-ret));
-        return -1;
-    }
-
-    CPU_FOREACH(cpu) {
-        ret = mshv_arch_set_mp_state(cpu);
-        if (ret < 0) {
-            error_report("Failed to set mp state for vCPU %d: %s",
-                         cpu->cpu_index, strerror(-ret));
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int get_host_partition_property(int mshv_fd, uint32_t property_code,
                                        uint64_t *value)
 {
@@ -519,10 +495,6 @@ static int mshv_init_vcpu(CPUState *cpu)
     return 0;
 }
 
-static SaveVMHandlers savevm_mshv = {
-    .load_cleanup = mshv_load_cleanup,
-};
-
 static int mshv_init(AccelState *as, MachineState *ms)
 {
     MshvState *s;
@@ -574,9 +546,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
                                   0, "mshv-memory");
     memory_listener_register(&mshv_io_listener, &address_space_io);
 
-    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
-
-    mshv_clock_init();
+    ret = mshv_arch_migration_init(s);
+    if (ret < 0) {
+        return ret;
+    }
 
     return 0;
 }
include/system/mshv_int.h +1 -0
index 0e68365c63f..31d2f4381c7 100644
@@ -109,6 +109,7 @@ void mshv_arch_disable_partition_proc_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);
+int mshv_arch_migration_init(MshvState *s);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-all.c +18 -0
index f0b43aa86f1..968fe2a4494 100644
@@ -78,3 +78,37 @@ int mshv_arch_post_init_vm(int vm_fd)
 
     return ret;
 }
+
+static int mshv_load_cleanup(void *opaque)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = mshv_arch_set_partition_msrs(first_cpu);
+    if (ret < 0) {
+        error_report("Failed to set partition MSRs: %s", strerror(-ret));
+        return -1;
+    }
+
+    CPU_FOREACH(cpu) {
+        ret = mshv_arch_set_mp_state(cpu);
+        if (ret < 0) {
+            error_report("Failed to set mp state for vCPU %d: %s",
+                         cpu->cpu_index, strerror(-ret));
+            return -1;
Quoted 8 lines

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 104

Quoted 6 lines
target/i386/mshv/mshv-all.c +6 -0
+        }
+    }
+
+    return 0;
+}
+

For me, on x86 this does not build. The SaveVMHandlers type is unknown.

It's defined by the migration include which is missing. I'll add that and going forward I'll test patches for x86 as well.

Quoted 9 lines

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 110

Quoted 6 lines
target/i386/mshv/mshv-all.c +6 -0
+static SaveVMHandlers savevm_mshv = {
+    .load_cleanup = mshv_load_cleanup,
+};
+
+int mshv_arch_migration_init(MshvState *s)
+{

Also, this function here is seen as implicit declaration. I think there's a missing include in this file.

That's probably because of the mshv_clock_init() called inside this function. Fixing the naming mismatch will fix this too. Along with the migration include, it should build successfully.

Quoted 7 lines
Quoted 7 lines
target/i386/mshv/mshv-all.c +4 -0
+    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
+    mshv_clock_init();
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#9 lore raw

Re: [PATCH v6 03/14] accel/mshv: make MSHV migration initialization arch-specific

In reply to Aastha Rawat
Quoted 111 lines · Aastha Rawat

x86 specific migration handlers currently live in common MSHV code. Move these handlers into x86 backend and add a generic architecture migration initialization hook. ARM MSHV live migration support is not added yet, this will allow ARM to add an empty initialization hook.

---
 accel/mshv/mshv-all.c       | 35 ++++-------------------------------
 include/system/mshv_int.h   |  1 +
 target/i386/mshv/mshv-all.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 31 deletions(-)
accel/mshv/mshv-all.c +4 -31
index 860f511fcba..efe19a39140 100644
@@ -60,30 +60,6 @@ static int init_mshv(int *mshv_fd)
     return 0;
 }
 
-static int mshv_load_cleanup(void *opaque)
-{
-    CPUState *cpu;
-    int ret;
-
-    ret = mshv_arch_set_partition_msrs(first_cpu);
-    if (ret < 0) {
-        error_report("Failed to set partition MSRs: %s", strerror(-ret));
-        return -1;
-    }
-
-    CPU_FOREACH(cpu) {
-        ret = mshv_arch_set_mp_state(cpu);
-        if (ret < 0) {
-            error_report("Failed to set mp state for vCPU %d: %s",
-                         cpu->cpu_index, strerror(-ret));
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int get_host_partition_property(int mshv_fd, uint32_t property_code,
                                        uint64_t *value)
 {
@@ -519,10 +495,6 @@ static int mshv_init_vcpu(CPUState *cpu)
     return 0;
 }
 
-static SaveVMHandlers savevm_mshv = {
-    .load_cleanup = mshv_load_cleanup,
-};
-
 static int mshv_init(AccelState *as, MachineState *ms)
 {
     MshvState *s;
@@ -574,9 +546,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
                                   0, "mshv-memory");
     memory_listener_register(&mshv_io_listener, &address_space_io);
 
-    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
-
-    mshv_clock_init();
+    ret = mshv_arch_migration_init(s);
+    if (ret < 0) {
+        return ret;
+    }
 
     return 0;
 }
include/system/mshv_int.h +1 -0
index 0e68365c63f..31d2f4381c7 100644
@@ -109,6 +109,7 @@ void mshv_arch_disable_partition_proc_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);
+int mshv_arch_migration_init(MshvState *s);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-all.c +19 -0
index f0b43aa86f1..968fe2a4494 100644
@@ -78,3 +78,37 @@ int mshv_arch_post_init_vm(int vm_fd)
 
     return ret;
 }
+
+static int mshv_load_cleanup(void *opaque)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = mshv_arch_set_partition_msrs(first_cpu);
+    if (ret < 0) {
+        error_report("Failed to set partition MSRs: %s", strerror(-ret));
+        return -1;
+    }
+
+    CPU_FOREACH(cpu) {
+        ret = mshv_arch_set_mp_state(cpu);
+        if (ret < 0) {
+            error_report("Failed to set mp state for vCPU %d: %s",
+                         cpu->cpu_index, strerror(-ret));
+            return -1;
+        }

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 109

Quoted 10 lines
target/i386/mshv/mshv-all.c +10 -0
+    }
+
+    return 0;
+}
+
+static SaveVMHandlers savevm_mshv = {
+    .load_cleanup = mshv_load_cleanup,
+};
+
+int mshv_arch_migration_init(MshvState *s)

mshv_clock_init() is also relevant for pause/resume handling, so maybe "mshv_arch_vm_state_init()"?

Quoted 8 lines
target/i386/mshv/mshv-all.c +5 -0
+{
+    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
+    mshv_clock_init();
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#10 lore raw

Re: [PATCH v6 03/14] accel/mshv: make MSHV migration initialization arch-specific

Quoted 117 lines · Magnus Kulke
Quoted 116 lines · Aastha Rawat

x86 specific migration handlers currently live in common MSHV code. Move these handlers into x86 backend and add a generic architecture migration initialization hook. ARM MSHV live migration support is not added yet, this will allow ARM to add an empty initialization hook.

---
 accel/mshv/mshv-all.c       | 35 ++++-------------------------------
 include/system/mshv_int.h   |  1 +
 target/i386/mshv/mshv-all.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 31 deletions(-)
accel/mshv/mshv-all.c +4 -31
index 860f511fcba..efe19a39140 100644
@@ -60,30 +60,6 @@ static int init_mshv(int *mshv_fd)
     return 0;
 }
 
-static int mshv_load_cleanup(void *opaque)
-{
-    CPUState *cpu;
-    int ret;
-
-    ret = mshv_arch_set_partition_msrs(first_cpu);
-    if (ret < 0) {
-        error_report("Failed to set partition MSRs: %s", strerror(-ret));
-        return -1;
-    }
-
-    CPU_FOREACH(cpu) {
-        ret = mshv_arch_set_mp_state(cpu);
-        if (ret < 0) {
-            error_report("Failed to set mp state for vCPU %d: %s",
-                         cpu->cpu_index, strerror(-ret));
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int get_host_partition_property(int mshv_fd, uint32_t property_code,
                                        uint64_t *value)
 {
@@ -519,10 +495,6 @@ static int mshv_init_vcpu(CPUState *cpu)
     return 0;
 }
 
-static SaveVMHandlers savevm_mshv = {
-    .load_cleanup = mshv_load_cleanup,
-};
-
 static int mshv_init(AccelState *as, MachineState *ms)
 {
     MshvState *s;
@@ -574,9 +546,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
                                   0, "mshv-memory");
     memory_listener_register(&mshv_io_listener, &address_space_io);
 
-    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
-
-    mshv_clock_init();
+    ret = mshv_arch_migration_init(s);
+    if (ret < 0) {
+        return ret;
+    }
 
     return 0;
 }
include/system/mshv_int.h +1 -0
index 0e68365c63f..31d2f4381c7 100644
@@ -109,6 +109,7 @@ void mshv_arch_disable_partition_proc_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);
+int mshv_arch_migration_init(MshvState *s);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-all.c +24 -0
index f0b43aa86f1..968fe2a4494 100644
@@ -78,3 +78,37 @@ int mshv_arch_post_init_vm(int vm_fd)
 
     return ret;
 }
+
+static int mshv_load_cleanup(void *opaque)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = mshv_arch_set_partition_msrs(first_cpu);
+    if (ret < 0) {
+        error_report("Failed to set partition MSRs: %s", strerror(-ret));
+        return -1;
+    }
+
+    CPU_FOREACH(cpu) {
+        ret = mshv_arch_set_mp_state(cpu);
+        if (ret < 0) {
+            error_report("Failed to set mp state for vCPU %d: %s",
+                         cpu->cpu_index, strerror(-ret));
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
Quoted 8 lines

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 109

Quoted 5 lines
target/i386/mshv/mshv-all.c +5 -0
+static SaveVMHandlers savevm_mshv = {
+    .load_cleanup = mshv_load_cleanup,
+};
+
+int mshv_arch_migration_init(MshvState *s)

mshv_clock_init() is also relevant for pause/resume handling, so maybe "mshv_arch_vm_state_init()"?

Sure, that makes sense.

Quoted 8 lines
Quoted 8 lines
target/i386/mshv/mshv-all.c +5 -0
+{
+    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
+    mshv_clock_init();
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#11 lore raw

Re: [PATCH v6 03/14] accel/mshv: make MSHV migration initialization arch-specific

In reply to Aastha Rawat
Quoted 59 lines · Aastha Rawat

x86 specific migration handlers currently live in common MSHV code. Move these handlers into x86 backend and add a generic architecture migration initialization hook. ARM MSHV live migration support is not added yet, this will allow ARM to add an empty initialization hook.

---
 accel/mshv/mshv-all.c       | 35 ++++-------------------------------
 include/system/mshv_int.h   |  1 +
 target/i386/mshv/mshv-all.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 31 deletions(-)
accel/mshv/mshv-all.c +0 -28
index 860f511fcba..efe19a39140 100644
@@ -60,30 +60,6 @@ static int init_mshv(int *mshv_fd)
     return 0;
 }
 
-static int mshv_load_cleanup(void *opaque)
-{
-    CPUState *cpu;
-    int ret;
-
-    ret = mshv_arch_set_partition_msrs(first_cpu);
-    if (ret < 0) {
-        error_report("Failed to set partition MSRs: %s", strerror(-ret));
-        return -1;
-    }
-
-    CPU_FOREACH(cpu) {
-        ret = mshv_arch_set_mp_state(cpu);
-        if (ret < 0) {
-            error_report("Failed to set mp state for vCPU %d: %s",
-                         cpu->cpu_index, strerror(-ret));
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int get_host_partition_property(int mshv_fd, uint32_t property_code,
                                        uint64_t *value)
 {
@@ -519,10 +495,6 @@ static int mshv_init_vcpu(CPUState *cpu)
     return 0;
 }
 
-static SaveVMHandlers savevm_mshv = {
-    .load_cleanup = mshv_load_cleanup,
-};
-
 static int mshv_init(AccelState *as, MachineState *ms)
 {
     MshvState *s;

accel/mshv/mshv-all.c static int mshv_init(AccelState *as, MachineState *ms) · line 550

Quoted 8 lines
accel/mshv/mshv-all.c +2 -3
@@ -574,9 +546,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
                                   0, "mshv-memory");
     memory_listener_register(&mshv_io_listener, &address_space_io);
 
-    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
-
-    mshv_clock_init();
+    ret = mshv_arch_migration_init(s);
+    if (ret < 0) {

if this is fallible now, do we need to unwind the previous steps?

Quoted 44 lines
accel/mshv/mshv-all.c +2 -0
+        return ret;
+    }
 
     return 0;
 }
include/system/mshv_int.h +1 -0
index 0e68365c63f..31d2f4381c7 100644
@@ -109,6 +109,7 @@ void mshv_arch_disable_partition_proc_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);
+int mshv_arch_migration_init(MshvState *s);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-all.c +19 -0
index f0b43aa86f1..968fe2a4494 100644
@@ -78,3 +78,37 @@ int mshv_arch_post_init_vm(int vm_fd)
 
     return ret;
 }
+
+static int mshv_load_cleanup(void *opaque)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = mshv_arch_set_partition_msrs(first_cpu);
+    if (ret < 0) {
+        error_report("Failed to set partition MSRs: %s", strerror(-ret));
+        return -1;
+    }
+
+    CPU_FOREACH(cpu) {
+        ret = mshv_arch_set_mp_state(cpu);
+        if (ret < 0) {
+            error_report("Failed to set mp state for vCPU %d: %s",
+                         cpu->cpu_index, strerror(-ret));
+            return -1;
+        }

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 109

Quoted 10 lines
target/i386/mshv/mshv-all.c +10 -0
+    }
+
+    return 0;
+}
+
+static SaveVMHandlers savevm_mshv = {
+    .load_cleanup = mshv_load_cleanup,
+};
+
+int mshv_arch_migration_init(MshvState *s)

mshv_clock_init() also covers pause/resume, so maybe "mshv_arch_vm_state_init()" would be a better name?

Quoted 8 lines
target/i386/mshv/mshv-all.c +5 -0
+{
+    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
+    mshv_clock_init();
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#12 lore raw

Re: [PATCH v6 03/14] accel/mshv: make MSHV migration initialization arch-specific

Quoted 63 lines · Magnus Kulke
Quoted 62 lines · Aastha Rawat

x86 specific migration handlers currently live in common MSHV code. Move these handlers into x86 backend and add a generic architecture migration initialization hook. ARM MSHV live migration support is not added yet, this will allow ARM to add an empty initialization hook.

---
 accel/mshv/mshv-all.c       | 35 ++++-------------------------------
 include/system/mshv_int.h   |  1 +
 target/i386/mshv/mshv-all.c | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 31 deletions(-)
accel/mshv/mshv-all.c +0 -28
index 860f511fcba..efe19a39140 100644
@@ -60,30 +60,6 @@ static int init_mshv(int *mshv_fd)
     return 0;
 }
 
-static int mshv_load_cleanup(void *opaque)
-{
-    CPUState *cpu;
-    int ret;
-
-    ret = mshv_arch_set_partition_msrs(first_cpu);
-    if (ret < 0) {
-        error_report("Failed to set partition MSRs: %s", strerror(-ret));
-        return -1;
-    }
-
-    CPU_FOREACH(cpu) {
-        ret = mshv_arch_set_mp_state(cpu);
-        if (ret < 0) {
-            error_report("Failed to set mp state for vCPU %d: %s",
-                         cpu->cpu_index, strerror(-ret));
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int get_host_partition_property(int mshv_fd, uint32_t property_code,
                                        uint64_t *value)
 {
@@ -519,10 +495,6 @@ static int mshv_init_vcpu(CPUState *cpu)
     return 0;
 }
 
-static SaveVMHandlers savevm_mshv = {
-    .load_cleanup = mshv_load_cleanup,
-};
-
 static int mshv_init(AccelState *as, MachineState *ms)
 {
     MshvState *s;
@@ -574,9 +546,10 @@ static int mshv_init(AccelState *as, MachineState *ms)
                                   0, "mshv-memory");
     memory_listener_register(&mshv_io_listener, &address_space_io);
 
Quoted 7 lines

accel/mshv/mshv-all.c static int mshv_init(AccelState *as, MachineState *ms) · line 550

Quoted 5 lines
accel/mshv/mshv-all.c +2 -3
-    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
-
-    mshv_clock_init();
+    ret = mshv_arch_migration_init(s);
+    if (ret < 0) {

if this is fallible now, do we need to unwind the previous steps?

The existing initialization was infallible and ARM does nothing yet (currently a no-op). I will keep this hook returning void. If it becomes fallible later, then we will add a proper cleanup.

Quoted 49 lines
Quoted 49 lines
accel/mshv/mshv-all.c +2 -0
+        return ret;
+    }
 
     return 0;
 }
include/system/mshv_int.h +1 -0
index 0e68365c63f..31d2f4381c7 100644
@@ -109,6 +109,7 @@ void mshv_arch_disable_partition_proc_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);
+int mshv_arch_migration_init(MshvState *s);
 int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
 int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
 typedef struct mshv_root_hvcall mshv_root_hvcall;
target/i386/mshv/mshv-all.c +24 -0
index f0b43aa86f1..968fe2a4494 100644
@@ -78,3 +78,37 @@ int mshv_arch_post_init_vm(int vm_fd)
 
     return ret;
 }
+
+static int mshv_load_cleanup(void *opaque)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = mshv_arch_set_partition_msrs(first_cpu);
+    if (ret < 0) {
+        error_report("Failed to set partition MSRs: %s", strerror(-ret));
+        return -1;
+    }
+
+    CPU_FOREACH(cpu) {
+        ret = mshv_arch_set_mp_state(cpu);
+        if (ret < 0) {
+            error_report("Failed to set mp state for vCPU %d: %s",
+                         cpu->cpu_index, strerror(-ret));
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
Quoted 8 lines

target/i386/mshv/mshv-all.c int mshv_arch_post_init_vm(int vm_fd) · line 109

Quoted 5 lines
target/i386/mshv/mshv-all.c +5 -0
+static SaveVMHandlers savevm_mshv = {
+    .load_cleanup = mshv_load_cleanup,
+};
+
+int mshv_arch_migration_init(MshvState *s)

mshv_clock_init() also covers pause/resume, so maybe "mshv_arch_vm_state_init()" would be a better name?

Definitely.

Quoted 8 lines
Quoted 8 lines
target/i386/mshv/mshv-all.c +5 -0
+{
+    register_savevm_live("mshv", 0, 1, &savevm_mshv, s);
+    mshv_clock_init();
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#13 lore raw

PATCH v6 4/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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +8 -2
index efe19a3914..f3f1825b0b 100644
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif
 
     struct mshv_create_partition_v2 args = {0};
 
@@ -155,7 +157,8 @@ static int create_partition(int mshv_fd, int *vm_fd)
                (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES) |
                (1ULL << MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES);
 
-    /* enable all */
+#if defined(__x86_64__)
+    /* enable all xsave features (0 = enabled) */
     disabled_xsave_features.as_uint64 = 0;
     /*
      * AMX TILE XSAVE state (XTILE_DATA) is 8KB, which exceeds the
@@ -165,6 +168,7 @@ static int create_partition(int mshv_fd, int *vm_fd)
     disabled_xsave_features.amx_bf16_support = 1;
     disabled_xsave_features.amx_int8_support = 1;
     disabled_xsave_features.amx_fp16_support = 1;
+#endif
 
     /*
      * query host for supported processor features and disable unsupported
@@ -196,7 +200,9 @@ static int create_partition(int mshv_fd, int *vm_fd)
     /* populate args structure */
     args.pt_flags = pt_flags;
     args.pt_isolation = MSHV_PT_ISOLATION_NONE;
+#if defined(__x86_64__)
     args.pt_disabled_xsave = disabled_xsave_features.as_uint64;
+#endif
     args.pt_num_cpu_fbanks = MSHV_NUM_CPU_FEATURES_BANKS;
 
     ret = ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args);
hw/i386/mshv/clock.c +1 -1
index 02c586503a..b7db382752 100644
@@ -182,7 +182,7 @@ static const VMStateDescription vmstate_mshv_clock = {
     },
 };
 
-void mshv_clock_init(void)
+void mshv_arch_clock_init(void)
 {
     vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock);
     qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clock);
include/system/mshv.h +1 -1
index f98ddff9b5..114f065db5 100644
@@ -58,7 +58,7 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE,
 extern MshvState *mshv_state;
 
 /* clock (partition reference time) */
-void mshv_clock_init(void);
+void mshv_arch_clock_init(void);
 
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
meson.build +4 -4
index cae1591cd2..6a90a394a2 100644
@@ -304,7 +304,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 += {
@@ -884,10 +885,9 @@ 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 host_machine.cpu() in [ 'x86_64', 'aarch64' ]
+    accelerators += 'CONFIG_MSHV'
   endif
-  accelerators += 'CONFIG_MSHV'
 endif
 
 if get_option('whpx').allowed() and host_os == 'windows'
target/arm/meson.build +1 -0
index 0369f96b4c..1ac2cef26b 100644
@@ -63,6 +63,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 +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.45.4
#14 lore raw

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

Quoted 21 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +0 -0
index efe19a3914..f3f1825b0b 100644

accel/mshv/mshv-all.c static int create_partition(int mshv_fd, int *vm_fd) · line 148

Quoted 8 lines
accel/mshv/mshv-all.c +3 -1
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif

This is the common mshv file; if you are adding __x86_64__ ifdefs to it then something is in the wrong place and needs to be moved to an architecture-specific file/function.

thanks
-- PMM

#15 lore raw

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

Quoted 27 lines · Peter Maydell
Quoted 25 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +0 -1
index efe19a3914..f3f1825b0b 100644
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
Quoted 8 lines

accel/mshv/mshv-all.c static int create_partition(int mshv_fd, int *vm_fd) · line 148

Quoted 4 lines
accel/mshv/mshv-all.c +3 -0
     union hv_partition_processor_features disabled_partition_features = {0};
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif

This is the common mshv file; if you are adding __x86_64__ ifdefs to it then something is in the wrong place and needs to be moved to an architecture-specific file/function.

Yes, I plan to introduce an ARM variant of this function. This will be moved there.

Thanks,
Aastha

#16 lore raw

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

In reply to Aastha Rawat
Quoted 20 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +0 -0
index efe19a3914..f3f1825b0b 100644

accel/mshv/mshv-all.c static int create_partition(int mshv_fd, int *vm_fd) · line 145

Quoted 5 lines
accel/mshv/mshv-all.c +0 -1
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};

I think this should reside in an arch specific variant of create_partition.

Quoted 202 lines
accel/mshv/mshv-all.c +8 -1
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif
 
     struct mshv_create_partition_v2 args = {0};
 
@@ -155,7 +157,8 @@ static int create_partition(int mshv_fd, int *vm_fd)
                (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES) |
                (1ULL << MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES);
 
-    /* enable all */
+#if defined(__x86_64__)
+    /* enable all xsave features (0 = enabled) */
     disabled_xsave_features.as_uint64 = 0;
     /*
      * AMX TILE XSAVE state (XTILE_DATA) is 8KB, which exceeds the
@@ -165,6 +168,7 @@ static int create_partition(int mshv_fd, int *vm_fd)
     disabled_xsave_features.amx_bf16_support = 1;
     disabled_xsave_features.amx_int8_support = 1;
     disabled_xsave_features.amx_fp16_support = 1;
+#endif
 
     /*
      * query host for supported processor features and disable unsupported
@@ -196,7 +200,9 @@ static int create_partition(int mshv_fd, int *vm_fd)
     /* populate args structure */
     args.pt_flags = pt_flags;
     args.pt_isolation = MSHV_PT_ISOLATION_NONE;
+#if defined(__x86_64__)
     args.pt_disabled_xsave = disabled_xsave_features.as_uint64;
+#endif
     args.pt_num_cpu_fbanks = MSHV_NUM_CPU_FEATURES_BANKS;
 
     ret = ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args);
hw/i386/mshv/clock.c +1 -1
index 02c586503a..b7db382752 100644
@@ -182,7 +182,7 @@ static const VMStateDescription vmstate_mshv_clock = {
     },
 };
 
-void mshv_clock_init(void)
+void mshv_arch_clock_init(void)
 {
     vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock);
     qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clock);
include/system/mshv.h +1 -1
index f98ddff9b5..114f065db5 100644
@@ -58,7 +58,7 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE,
 extern MshvState *mshv_state;
 
 /* clock (partition reference time) */
-void mshv_clock_init(void);
+void mshv_arch_clock_init(void);
 
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
meson.build +4 -4
index cae1591cd2..6a90a394a2 100644
@@ -304,7 +304,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 += {
@@ -884,10 +885,9 @@ 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 host_machine.cpu() in [ 'x86_64', 'aarch64' ]
+    accelerators += 'CONFIG_MSHV'
   endif
-  accelerators += 'CONFIG_MSHV'
 endif
 
 if get_option('whpx').allowed() and host_os == 'windows'
target/arm/meson.build +1 -0
index 0369f96b4c..1ac2cef26b 100644
@@ -63,6 +63,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 +81 -0
new file mode 100644
index 0000000000..00c54f5d66
@@ -0,0 +1,81 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat          <[email protected]>
+ *          Anirudh Rayabharam    <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/arm/virt.h"
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+
+static int load_regs(CPUState *cpu)
+{
+    return 0;
+}
+
+static int store_regs(const CPUState *cpu)
+{
+    return 0;
+}
+
+int mshv_arch_load_vcpu_state(CPUState *cpu)
+{
+    return load_regs(cpu);
+}
+
+int mshv_arch_store_vcpu_state(const CPUState *cpu)
+{
+    return store_regs(cpu);
+}
+
+int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
+{
+    return 0;
+}
+
+void mshv_arch_init_vcpu(CPUState *cpu)
+{
+
+}
+
+void mshv_arch_destroy_vcpu(CPUState *cpu)
+{
+
+}
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    return 0;
+}
+
+void mshv_arch_amend_proc_features(
+    union hv_partition_synthetic_processor_features *features)
+{
+    /*
+     * TLB-flush hypercalls are an x86 enlightenment. ARM guests use
+     * broadcast TLBI instructions instead.
+     */
+    features->tb_flush_hypercalls = 0;
+}
+
+void mshv_arch_disable_partition_proc_features(
+    union hv_partition_processor_features *disabled_features)
+{
+    /* No processor features to disable on ARM */
+}
+
+int mshv_arch_post_init_vm(int vm_fd)
+{
+    return 0;
+}
+
+int mshv_arch_migration_init(MshvState *s)
+{
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#17 lore raw

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

Quoted 16 lines · Doru Blânzeanu
Quoted 15 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
Quoted 12 lines

accel/mshv/mshv-all.c static int create_partition(int mshv_fd, int *vm_fd) · line 145

Quoted 10 lines
accel/mshv/mshv-all.c +0 -1
index efe19a3914..f3f1825b0b 100644
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};

I think this should reside in an arch specific variant of create_partition.

Agreed, I'll introduce it in the next revision and move arm specific bits there. As discussed, it will also help us get rid of the checkpatch.pl warnings.

Quoted 202 lines
Quoted 202 lines
accel/mshv/mshv-all.c +8 -1
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif
 
     struct mshv_create_partition_v2 args = {0};
 
@@ -155,7 +157,8 @@ static int create_partition(int mshv_fd, int *vm_fd)
                (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES) |
                (1ULL << MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES);
 
-    /* enable all */
+#if defined(__x86_64__)
+    /* enable all xsave features (0 = enabled) */
     disabled_xsave_features.as_uint64 = 0;
     /*
      * AMX TILE XSAVE state (XTILE_DATA) is 8KB, which exceeds the
@@ -165,6 +168,7 @@ static int create_partition(int mshv_fd, int *vm_fd)
     disabled_xsave_features.amx_bf16_support = 1;
     disabled_xsave_features.amx_int8_support = 1;
     disabled_xsave_features.amx_fp16_support = 1;
+#endif
 
     /*
      * query host for supported processor features and disable unsupported
@@ -196,7 +200,9 @@ static int create_partition(int mshv_fd, int *vm_fd)
     /* populate args structure */
     args.pt_flags = pt_flags;
     args.pt_isolation = MSHV_PT_ISOLATION_NONE;
+#if defined(__x86_64__)
     args.pt_disabled_xsave = disabled_xsave_features.as_uint64;
+#endif
     args.pt_num_cpu_fbanks = MSHV_NUM_CPU_FEATURES_BANKS;
 
     ret = ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args);
hw/i386/mshv/clock.c +1 -1
index 02c586503a..b7db382752 100644
@@ -182,7 +182,7 @@ static const VMStateDescription vmstate_mshv_clock = {
     },
 };
 
-void mshv_clock_init(void)
+void mshv_arch_clock_init(void)
 {
     vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock);
     qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clock);
include/system/mshv.h +1 -1
index f98ddff9b5..114f065db5 100644
@@ -58,7 +58,7 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE,
 extern MshvState *mshv_state;
 
 /* clock (partition reference time) */
-void mshv_clock_init(void);
+void mshv_arch_clock_init(void);
 
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
meson.build +4 -4
index cae1591cd2..6a90a394a2 100644
@@ -304,7 +304,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 += {
@@ -884,10 +885,9 @@ 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 host_machine.cpu() in [ 'x86_64', 'aarch64' ]
+    accelerators += 'CONFIG_MSHV'
   endif
-  accelerators += 'CONFIG_MSHV'
 endif
 
 if get_option('whpx').allowed() and host_os == 'windows'
target/arm/meson.build +1 -0
index 0369f96b4c..1ac2cef26b 100644
@@ -63,6 +63,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 +81 -0
new file mode 100644
index 0000000000..00c54f5d66
@@ -0,0 +1,81 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat          <[email protected]>
+ *          Anirudh Rayabharam    <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/arm/virt.h"
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+
+static int load_regs(CPUState *cpu)
+{
+    return 0;
+}
+
+static int store_regs(const CPUState *cpu)
+{
+    return 0;
+}
+
+int mshv_arch_load_vcpu_state(CPUState *cpu)
+{
+    return load_regs(cpu);
+}
+
+int mshv_arch_store_vcpu_state(const CPUState *cpu)
+{
+    return store_regs(cpu);
+}
+
+int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
+{
+    return 0;
+}
+
+void mshv_arch_init_vcpu(CPUState *cpu)
+{
+
+}
+
+void mshv_arch_destroy_vcpu(CPUState *cpu)
+{
+
+}
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    return 0;
+}
+
+void mshv_arch_amend_proc_features(
+    union hv_partition_synthetic_processor_features *features)
+{
+    /*
+     * TLB-flush hypercalls are an x86 enlightenment. ARM guests use
+     * broadcast TLBI instructions instead.
+     */
+    features->tb_flush_hypercalls = 0;
+}
+
+void mshv_arch_disable_partition_proc_features(
+    union hv_partition_processor_features *disabled_features)
+{
+    /* No processor features to disable on ARM */
+}
+
+int mshv_arch_post_init_vm(int vm_fd)
+{
+    return 0;
+}
+
+int mshv_arch_migration_init(MshvState *s)
+{
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#18 lore raw

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

In reply to Aastha Rawat
Quoted 77 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +8 -2
index efe19a3914..f3f1825b0b 100644
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif
 
     struct mshv_create_partition_v2 args = {0};
 
@@ -155,7 +157,8 @@ static int create_partition(int mshv_fd, int *vm_fd)
                (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES) |
                (1ULL << MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES);
 
-    /* enable all */
+#if defined(__x86_64__)
+    /* enable all xsave features (0 = enabled) */
     disabled_xsave_features.as_uint64 = 0;
     /*
      * AMX TILE XSAVE state (XTILE_DATA) is 8KB, which exceeds the
@@ -165,6 +168,7 @@ static int create_partition(int mshv_fd, int *vm_fd)
     disabled_xsave_features.amx_bf16_support = 1;
     disabled_xsave_features.amx_int8_support = 1;
     disabled_xsave_features.amx_fp16_support = 1;
+#endif
 
     /*
      * query host for supported processor features and disable unsupported
@@ -196,7 +200,9 @@ static int create_partition(int mshv_fd, int *vm_fd)
     /* populate args structure */
     args.pt_flags = pt_flags;
     args.pt_isolation = MSHV_PT_ISOLATION_NONE;
+#if defined(__x86_64__)
     args.pt_disabled_xsave = disabled_xsave_features.as_uint64;
+#endif
     args.pt_num_cpu_fbanks = MSHV_NUM_CPU_FEATURES_BANKS;
 
     ret = ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args);
hw/i386/mshv/clock.c +1 -1
index 02c586503a..b7db382752 100644
@@ -182,7 +182,7 @@ static const VMStateDescription vmstate_mshv_clock = {
     },
 };
 
-void mshv_clock_init(void)
+void mshv_arch_clock_init(void)
 {
     vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock);
     qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clock);
include/system/mshv.h +0 -0
index f98ddff9b5..114f065db5 100644

include/system/mshv.h DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE, · line 59

Quoted 2 lines
include/system/mshv.h +0 -0
@@ -58,7 +58,7 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE,
 extern MshvState *mshv_state;
 

This renames the mshv_clock_init, but the place where it is called is not changed in this commit, which leads to a compilation issue on the x86 arch.

Quoted 148 lines
include/system/mshv.h +1 -1
 /* clock (partition reference time) */
-void mshv_clock_init(void);
+void mshv_arch_clock_init(void);
 
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
meson.build +4 -4
index cae1591cd2..6a90a394a2 100644
@@ -304,7 +304,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 += {
@@ -884,10 +885,9 @@ 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 host_machine.cpu() in [ 'x86_64', 'aarch64' ]
+    accelerators += 'CONFIG_MSHV'
   endif
-  accelerators += 'CONFIG_MSHV'
 endif
 
 if get_option('whpx').allowed() and host_os == 'windows'
target/arm/meson.build +1 -0
index 0369f96b4c..1ac2cef26b 100644
@@ -63,6 +63,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 +81 -0
new file mode 100644
index 0000000000..00c54f5d66
@@ -0,0 +1,81 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat          <[email protected]>
+ *          Anirudh Rayabharam    <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/arm/virt.h"
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+
+static int load_regs(CPUState *cpu)
+{
+    return 0;
+}
+
+static int store_regs(const CPUState *cpu)
+{
+    return 0;
+}
+
+int mshv_arch_load_vcpu_state(CPUState *cpu)
+{
+    return load_regs(cpu);
+}
+
+int mshv_arch_store_vcpu_state(const CPUState *cpu)
+{
+    return store_regs(cpu);
+}
+
+int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
+{
+    return 0;
+}
+
+void mshv_arch_init_vcpu(CPUState *cpu)
+{
+
+}
+
+void mshv_arch_destroy_vcpu(CPUState *cpu)
+{
+
+}
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    return 0;
+}
+
+void mshv_arch_amend_proc_features(
+    union hv_partition_synthetic_processor_features *features)
+{
+    /*
+     * TLB-flush hypercalls are an x86 enlightenment. ARM guests use
+     * broadcast TLBI instructions instead.
+     */
+    features->tb_flush_hypercalls = 0;
+}
+
+void mshv_arch_disable_partition_proc_features(
+    union hv_partition_processor_features *disabled_features)
+{
+    /* No processor features to disable on ARM */
+}
+
+int mshv_arch_post_init_vm(int vm_fd)
+{
+    return 0;
+}
+
+int mshv_arch_migration_init(MshvState *s)
+{
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#19 lore raw

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

Quoted 68 lines · Doru Blânzeanu
Quoted 67 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +8 -2
index efe19a3914..f3f1825b0b 100644
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif
 
     struct mshv_create_partition_v2 args = {0};
 
@@ -155,7 +157,8 @@ static int create_partition(int mshv_fd, int *vm_fd)
                (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES) |
                (1ULL << MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES);
 
-    /* enable all */
+#if defined(__x86_64__)
+    /* enable all xsave features (0 = enabled) */
     disabled_xsave_features.as_uint64 = 0;
     /*
      * AMX TILE XSAVE state (XTILE_DATA) is 8KB, which exceeds the
@@ -165,6 +168,7 @@ static int create_partition(int mshv_fd, int *vm_fd)
     disabled_xsave_features.amx_bf16_support = 1;
     disabled_xsave_features.amx_int8_support = 1;
     disabled_xsave_features.amx_fp16_support = 1;
+#endif
 
     /*
      * query host for supported processor features and disable unsupported
@@ -196,7 +200,9 @@ static int create_partition(int mshv_fd, int *vm_fd)
     /* populate args structure */
     args.pt_flags = pt_flags;
     args.pt_isolation = MSHV_PT_ISOLATION_NONE;
+#if defined(__x86_64__)
     args.pt_disabled_xsave = disabled_xsave_features.as_uint64;
+#endif
     args.pt_num_cpu_fbanks = MSHV_NUM_CPU_FEATURES_BANKS;
 
     ret = ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args);
hw/i386/mshv/clock.c +0 -0
index 02c586503a..b7db382752 100644
@@ -182,7 +182,7 @@ static const VMStateDescription vmstate_mshv_clock = {
     },
 };
 
Quoted 15 lines

include/system/mshv.h DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE, · line 59

Quoted 12 lines
hw/i386/mshv/clock.c +1 -1
-void mshv_clock_init(void)
+void mshv_arch_clock_init(void)
 {
     vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock);
     qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clock);
include/system/mshv.h +0 -0
index f98ddff9b5..114f065db5 100644
@@ -58,7 +58,7 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE,
 extern MshvState *mshv_state;
 

This renames the mshv_clock_init, but the place where it is called is not changed in this commit, which leads to a compilation issue on the x86 arch.

Right. There is a naming mismatch which leads to this, but I want to re-consider making this function arch neutral now because for ARM, architectural timer migration happens per-vcpu. I will wire it into ARM migration later if partition wide handling is needed. Having "mshv_arch_vm_state_init()" seems to be enough for now.

Quoted 148 lines
Quoted 148 lines
include/system/mshv.h +1 -1
 /* clock (partition reference time) */
-void mshv_clock_init(void);
+void mshv_arch_clock_init(void);
 
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
meson.build +4 -4
index cae1591cd2..6a90a394a2 100644
@@ -304,7 +304,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 += {
@@ -884,10 +885,9 @@ 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 host_machine.cpu() in [ 'x86_64', 'aarch64' ]
+    accelerators += 'CONFIG_MSHV'
   endif
-  accelerators += 'CONFIG_MSHV'
 endif
 
 if get_option('whpx').allowed() and host_os == 'windows'
target/arm/meson.build +1 -0
index 0369f96b4c..1ac2cef26b 100644
@@ -63,6 +63,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 +81 -0
new file mode 100644
index 0000000000..00c54f5d66
@@ -0,0 +1,81 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat          <[email protected]>
+ *          Anirudh Rayabharam    <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/arm/virt.h"
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+
+static int load_regs(CPUState *cpu)
+{
+    return 0;
+}
+
+static int store_regs(const CPUState *cpu)
+{
+    return 0;
+}
+
+int mshv_arch_load_vcpu_state(CPUState *cpu)
+{
+    return load_regs(cpu);
+}
+
+int mshv_arch_store_vcpu_state(const CPUState *cpu)
+{
+    return store_regs(cpu);
+}
+
+int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
+{
+    return 0;
+}
+
+void mshv_arch_init_vcpu(CPUState *cpu)
+{
+
+}
+
+void mshv_arch_destroy_vcpu(CPUState *cpu)
+{
+
+}
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    return 0;
+}
+
+void mshv_arch_amend_proc_features(
+    union hv_partition_synthetic_processor_features *features)
+{
+    /*
+     * TLB-flush hypercalls are an x86 enlightenment. ARM guests use
+     * broadcast TLBI instructions instead.
+     */
+    features->tb_flush_hypercalls = 0;
+}
+
+void mshv_arch_disable_partition_proc_features(
+    union hv_partition_processor_features *disabled_features)
+{
+    /* No processor features to disable on ARM */
+}
+
+int mshv_arch_post_init_vm(int vm_fd)
+{
+    return 0;
+}
+
+int mshv_arch_migration_init(MshvState *s)
+{
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#20 lore raw

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

In reply to Aastha Rawat
Quoted 77 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +8 -2
index efe19a3914..f3f1825b0b 100644
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif
 
     struct mshv_create_partition_v2 args = {0};
 
@@ -155,7 +157,8 @@ static int create_partition(int mshv_fd, int *vm_fd)
                (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES) |
                (1ULL << MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES);
 
-    /* enable all */
+#if defined(__x86_64__)
+    /* enable all xsave features (0 = enabled) */
     disabled_xsave_features.as_uint64 = 0;
     /*
      * AMX TILE XSAVE state (XTILE_DATA) is 8KB, which exceeds the
@@ -165,6 +168,7 @@ static int create_partition(int mshv_fd, int *vm_fd)
     disabled_xsave_features.amx_bf16_support = 1;
     disabled_xsave_features.amx_int8_support = 1;
     disabled_xsave_features.amx_fp16_support = 1;
+#endif
 
     /*
      * query host for supported processor features and disable unsupported
@@ -196,7 +200,9 @@ static int create_partition(int mshv_fd, int *vm_fd)
     /* populate args structure */
     args.pt_flags = pt_flags;
     args.pt_isolation = MSHV_PT_ISOLATION_NONE;
+#if defined(__x86_64__)
     args.pt_disabled_xsave = disabled_xsave_features.as_uint64;
+#endif
     args.pt_num_cpu_fbanks = MSHV_NUM_CPU_FEATURES_BANKS;
 
     ret = ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args);
hw/i386/mshv/clock.c +1 -1
index 02c586503a..b7db382752 100644
@@ -182,7 +182,7 @@ static const VMStateDescription vmstate_mshv_clock = {
     },
 };
 
-void mshv_clock_init(void)
+void mshv_arch_clock_init(void)
 {
     vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock);
     qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clock);
include/system/mshv.h +0 -0
index f98ddff9b5..114f065db5 100644

include/system/mshv.h DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE, · line 61

Quoted 5 lines
include/system/mshv.h +1 -1
@@ -58,7 +58,7 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE,
 extern MshvState *mshv_state;
 
 /* clock (partition reference time) */
-void mshv_clock_init(void);
+void mshv_arch_clock_init(void);

the (x86) call site also needs to be changed to mshv_arch_clock_init()

Quoted 70 lines
include/system/mshv.h +0 -0
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
meson.build +4 -4
index cae1591cd2..6a90a394a2 100644
@@ -304,7 +304,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 += {
@@ -884,10 +885,9 @@ 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 host_machine.cpu() in [ 'x86_64', 'aarch64' ]
+    accelerators += 'CONFIG_MSHV'
   endif
-  accelerators += 'CONFIG_MSHV'
 endif
 
 if get_option('whpx').allowed() and host_os == 'windows'
target/arm/meson.build +1 -0
index 0369f96b4c..1ac2cef26b 100644
@@ -63,6 +63,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 +9 -0
new file mode 100644
index 0000000000..00c54f5d66
@@ -0,0 +1,81 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat          <[email protected]>
+ *          Anirudh Rayabharam    <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later

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

Quoted 10 lines
target/arm/mshv/mshv-all.c new file +10 -0
+ */
+
+#include "qemu/osdep.h"
+#include "hw/arm/virt.h"
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+
+static int load_regs(CPUState *cpu)
+{
+    return 0;

maybe the stubs should abort with an "umimplemented" message instead of returning to avoid silent failures (or g_assert_not_reached())

Quoted 65 lines
target/arm/mshv/mshv-all.c new file +62 -0
+}
+
+static int store_regs(const CPUState *cpu)
+{
+    return 0;
+}
+
+int mshv_arch_load_vcpu_state(CPUState *cpu)
+{
+    return load_regs(cpu);
+}
+
+int mshv_arch_store_vcpu_state(const CPUState *cpu)
+{
+    return store_regs(cpu);
+}
+
+int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
+{
+    return 0;
+}
+
+void mshv_arch_init_vcpu(CPUState *cpu)
+{
+
+}
+
+void mshv_arch_destroy_vcpu(CPUState *cpu)
+{
+
+}
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    return 0;
+}
+
+void mshv_arch_amend_proc_features(
+    union hv_partition_synthetic_processor_features *features)
+{
+    /*
+     * TLB-flush hypercalls are an x86 enlightenment. ARM guests use
+     * broadcast TLBI instructions instead.
+     */
+    features->tb_flush_hypercalls = 0;
+}
+
+void mshv_arch_disable_partition_proc_features(
+    union hv_partition_processor_features *disabled_features)
+{
+    /* No processor features to disable on ARM */
+}
+
+int mshv_arch_post_init_vm(int vm_fd)
+{
+    return 0;
+}
+
+int mshv_arch_migration_init(MshvState *s)
+{
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#21 lore raw

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

Quoted 80 lines · Magnus Kulke
Quoted 79 lines · 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.

---
 accel/mshv/mshv-all.c       | 10 ++++--
 hw/i386/mshv/clock.c        |  2 +-
 include/system/mshv.h       |  2 +-
 meson.build                 |  8 ++---
 target/arm/meson.build      |  1 +
 target/arm/mshv/meson.build |  7 ++++
 target/arm/mshv/mshv-all.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 8 deletions(-)
accel/mshv/mshv-all.c +8 -2
index efe19a3914..f3f1825b0b 100644
@@ -142,8 +142,10 @@ static int create_partition(int mshv_fd, int *vm_fd)
 {
     int ret;
     uint64_t pt_flags, host_proc_features;
-    union hv_partition_processor_xsave_features disabled_xsave_features;
     union hv_partition_processor_features disabled_partition_features = {0};
+#if defined(__x86_64__)
+    union hv_partition_processor_xsave_features disabled_xsave_features;
+#endif
 
     struct mshv_create_partition_v2 args = {0};
 
@@ -155,7 +157,8 @@ static int create_partition(int mshv_fd, int *vm_fd)
                (1ULL << MSHV_PT_BIT_GPA_SUPER_PAGES) |
                (1ULL << MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES);
 
-    /* enable all */
+#if defined(__x86_64__)
+    /* enable all xsave features (0 = enabled) */
     disabled_xsave_features.as_uint64 = 0;
     /*
      * AMX TILE XSAVE state (XTILE_DATA) is 8KB, which exceeds the
@@ -165,6 +168,7 @@ static int create_partition(int mshv_fd, int *vm_fd)
     disabled_xsave_features.amx_bf16_support = 1;
     disabled_xsave_features.amx_int8_support = 1;
     disabled_xsave_features.amx_fp16_support = 1;
+#endif
 
     /*
      * query host for supported processor features and disable unsupported
@@ -196,7 +200,9 @@ static int create_partition(int mshv_fd, int *vm_fd)
     /* populate args structure */
     args.pt_flags = pt_flags;
     args.pt_isolation = MSHV_PT_ISOLATION_NONE;
+#if defined(__x86_64__)
     args.pt_disabled_xsave = disabled_xsave_features.as_uint64;
+#endif
     args.pt_num_cpu_fbanks = MSHV_NUM_CPU_FEATURES_BANKS;
 
     ret = ioctl(mshv_fd, MSHV_CREATE_PARTITION, &args);
hw/i386/mshv/clock.c +1 -1
index 02c586503a..b7db382752 100644
@@ -182,7 +182,7 @@ static const VMStateDescription vmstate_mshv_clock = {
     },
 };
 
-void mshv_clock_init(void)
+void mshv_arch_clock_init(void)
 {
     vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock);
     qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clock);
include/system/mshv.h +0 -0
index f98ddff9b5..114f065db5 100644
@@ -58,7 +58,7 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE,
 extern MshvState *mshv_state;
 
Quoted 5 lines

include/system/mshv.h DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE, · line 61

Quoted 3 lines
include/system/mshv.h +1 -1
 /* clock (partition reference time) */
-void mshv_clock_init(void);
+void mshv_arch_clock_init(void);

the (x86) call site also needs to be changed to mshv_arch_clock_init()

I want to re-consider making this function arch neutral at this point. For ARM, architectural timer migration happens per-vcpu. I can wire it into ARM migration later if partition wide handling is needed. Having "mshv_arch_vm_state_init()" as a preparatory step seems to be sufficient for now.

Quoted 149 lines
Quoted 70 lines
include/system/mshv.h +0 -0
 /* interrupt */
 int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint32_t vector,
meson.build +4 -4
index cae1591cd2..6a90a394a2 100644
@@ -304,7 +304,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 += {
@@ -884,10 +885,9 @@ 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 host_machine.cpu() in [ 'x86_64', 'aarch64' ]
+    accelerators += 'CONFIG_MSHV'
   endif
-  accelerators += 'CONFIG_MSHV'
 endif
 
 if get_option('whpx').allowed() and host_os == 'windows'
target/arm/meson.build +1 -0
index 0369f96b4c..1ac2cef26b 100644
@@ -63,6 +63,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 +9 -0
new file mode 100644
index 0000000000..00c54f5d66
@@ -0,0 +1,81 @@
+/*
+ * QEMU MSHV support
+ *
+ * Copyright Microsoft, Corp. 2026
+ *
+ * Authors: Aastha Rawat          <[email protected]>
+ *          Anirudh Rayabharam    <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later

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

Quoted 10 lines
target/arm/mshv/mshv-all.c new file +10 -0
+ */
+
+#include "qemu/osdep.h"
+#include "hw/arm/virt.h"
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+
+static int load_regs(CPUState *cpu)
+{
+    return 0;

maybe the stubs should abort with an "umimplemented" message instead of returning to avoid silent failures (or g_assert_not_reached())

Quoted 65 lines
target/arm/mshv/mshv-all.c new file +62 -0
+}
+
+static int store_regs(const CPUState *cpu)
+{
+    return 0;
+}
+
+int mshv_arch_load_vcpu_state(CPUState *cpu)
+{
+    return load_regs(cpu);
+}
+
+int mshv_arch_store_vcpu_state(const CPUState *cpu)
+{
+    return store_regs(cpu);
+}
+
+int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
+{
+    return 0;
+}
+
+void mshv_arch_init_vcpu(CPUState *cpu)
+{
+
+}
+
+void mshv_arch_destroy_vcpu(CPUState *cpu)
+{
+
+}
+
+int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
+{
+    return 0;
+}
+
+void mshv_arch_amend_proc_features(
+    union hv_partition_synthetic_processor_features *features)
+{
+    /*
+     * TLB-flush hypercalls are an x86 enlightenment. ARM guests use
+     * broadcast TLBI instructions instead.
+     */
+    features->tb_flush_hypercalls = 0;
+}
+
+void mshv_arch_disable_partition_proc_features(
+    union hv_partition_processor_features *disabled_features)
+{
+    /* No processor features to disable on ARM */
+}
+
+int mshv_arch_post_init_vm(int vm_fd)
+{
+    return 0;
+}
+
+int mshv_arch_migration_init(MshvState *s)
+{
+    return 0;
+}
Signature · 2 lines
-- 
2.45.4
#22 lore raw

PATCH v6 5/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     | 119 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+)
include/hw/hyperv/hvgdk_mini.h +42 -0
index a5527d49cf..b5bcea353a 100644
@@ -32,6 +32,46 @@ typedef enum hv_register_name {
     HV_REGISTER_PENDING_EVENT0       = 0x00010004,
     HV_REGISTER_PENDING_EVENT1       = 0x00010005,
 
+#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,
@@ -188,6 +228,8 @@ typedef enum hv_register_name {
     /* Other MSRs */
     HV_X64_REGISTER_MSR_IA32_MISC_ENABLE = 0x000800A0,
 
+#endif
+
     /* Misc */
     HV_X64_REGISTER_HYPERCALL       = 0x00090001,
     HV_REGISTER_GUEST_OS_ID         = 0x00090002,
target/arm/mshv/mshv-all.c +119 -0

148 lines not on this page · raw message

Signature · 1 line
2.45.4
#23 lore raw

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

Quoted 181 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     | 119 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+)
include/hw/hyperv/hvgdk_mini.h +42 -0
index a5527d49cf..b5bcea353a 100644
@@ -32,6 +32,46 @@ typedef enum hv_register_name {
     HV_REGISTER_PENDING_EVENT0       = 0x00010004,
     HV_REGISTER_PENDING_EVENT1       = 0x00010005,
 
+#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,
@@ -188,6 +228,8 @@ typedef enum hv_register_name {
     /* Other MSRs */
     HV_X64_REGISTER_MSR_IA32_MISC_ENABLE = 0x000800A0,
 
+#endif
+
     /* Misc */
     HV_X64_REGISTER_HYPERCALL       = 0x00090001,
     HV_REGISTER_GUEST_OS_ID         = 0x00090002,
target/arm/mshv/mshv-all.c +96 -0
index 00c54f5d66..29dd45a123 100644
@@ -10,17 +10,128 @@
  */
 
 #include "qemu/osdep.h"
+#include <sys/ioctl.h>
+
+#include "qemu/error-report.h"
+#include "qemu/memalign.h"
 #include "hw/arm/virt.h"
+
+#include "system/cpus.h"
+#include "target/arm/cpu.h"
+
 #include "system/mshv.h"
 #include "system/mshv_int.h"
+#include "hw/hyperv/hvgdk_mini.h"
+
+static const enum hv_register_name STANDARD_REGISTER_NAMES[32] = {
+    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,
+    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,
+};
+
+static int set_standard_regs(const CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+        assocs[i].value.reg64 = env->xregs[i];
+    }
+
+    /* Last register is the program counter */
+    assocs[n_regs - 1].name = STANDARD_REGISTER_NAMES[n_regs - 1];
+    assocs[n_regs - 1].value.reg64 = env->pc;
+
+    ret = mshv_set_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to set standard registers");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int get_standard_regs(CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+    }
+    ret = mshv_get_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to get standard registers");
+        return -1;
+    }
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        env->xregs[i] = assocs[i].value.reg64;
+    }
+    env->pc = assocs[n_regs - 1].value.reg64;
+
+    return 0;
+}
 
 static int load_regs(CPUState *cpu)
 {
+    int ret;

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

Quoted 10 lines
target/arm/mshv/mshv-all.c +7 -0
+
+    ret = get_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to load standard registers");
+        return -1;
+    }
+
     return 0;
 }
 

I see that only these general purpose registers are synchronized after an exit, reset or init.
At a glance, KVM also handles registers such as PSTATE and SP_EL0/SP_EL1. Should this implementation handle those as well?

Quoted 37 lines
target/arm/mshv/mshv-all.c +16 -0
 static int store_regs(const CPUState *cpu)
 {
+    int ret;
+
+    ret = set_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to store standard registers");
+        return -1;
+    }
+
     return 0;
 }
 
@@ -41,12 +152,20 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
 void mshv_arch_init_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
 
+    mshv_setup_hvcall_args(state);
 }
 
 void mshv_arch_destroy_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
+
+    if (state->hvcall_args.base) {
+        qemu_vfree(state->hvcall_args.base);
+    }
 
+    state->hvcall_args = (MshvHvCallArgs){0};
 }
 
 int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
Signature · 2 lines
-- 
2.45.4
#24 lore raw

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

Quoted 189 lines · Doru Blânzeanu
Quoted 188 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     | 119 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+)
include/hw/hyperv/hvgdk_mini.h +42 -0
index a5527d49cf..b5bcea353a 100644
@@ -32,6 +32,46 @@ typedef enum hv_register_name {
     HV_REGISTER_PENDING_EVENT0       = 0x00010004,
     HV_REGISTER_PENDING_EVENT1       = 0x00010005,
 
+#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,
@@ -188,6 +228,8 @@ typedef enum hv_register_name {
     /* Other MSRs */
     HV_X64_REGISTER_MSR_IA32_MISC_ENABLE = 0x000800A0,
 
+#endif
+
     /* Misc */
     HV_X64_REGISTER_HYPERCALL       = 0x00090001,
     HV_REGISTER_GUEST_OS_ID         = 0x00090002,
target/arm/mshv/mshv-all.c +103 -0
index 00c54f5d66..29dd45a123 100644
@@ -10,17 +10,128 @@
  */
 
 #include "qemu/osdep.h"
+#include <sys/ioctl.h>
+
+#include "qemu/error-report.h"
+#include "qemu/memalign.h"
 #include "hw/arm/virt.h"
+
+#include "system/cpus.h"
+#include "target/arm/cpu.h"
+
 #include "system/mshv.h"
 #include "system/mshv_int.h"
+#include "hw/hyperv/hvgdk_mini.h"
+
+static const enum hv_register_name STANDARD_REGISTER_NAMES[32] = {
+    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,
+    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,
+};
+
+static int set_standard_regs(const CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+        assocs[i].value.reg64 = env->xregs[i];
+    }
+
+    /* Last register is the program counter */
+    assocs[n_regs - 1].name = STANDARD_REGISTER_NAMES[n_regs - 1];
+    assocs[n_regs - 1].value.reg64 = env->pc;
+
+    ret = mshv_set_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to set standard registers");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int get_standard_regs(CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+    }
+    ret = mshv_get_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to get standard registers");
+        return -1;
+    }
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        env->xregs[i] = assocs[i].value.reg64;
+    }
+    env->pc = assocs[n_regs - 1].value.reg64;
+
+    return 0;
+}
 
 static int load_regs(CPUState *cpu)
 {
+    int ret;
+
+    ret = get_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to load standard registers");
+        return -1;
+    }
+
Quoted 8 lines

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

Quoted 3 lines
target/arm/mshv/mshv-all.c +0 -0
     return 0;
 }
 

I see that only these general purpose registers are synchronized after an exit, reset or init.
At a glance, KVM also handles registers such as PSTATE and SP_EL0/SP_EL1. Should this implementation handle those as well?

For the current enablement scope, handling PSTATE and SP_EL0/SP_EL1 isn't required. KVM provides full vCPU synchronization. I plan to introduce these with migration support when we actually need complete state transfer.

Quoted 37 lines
Quoted 37 lines
target/arm/mshv/mshv-all.c +16 -0
 static int store_regs(const CPUState *cpu)
 {
+    int ret;
+
+    ret = set_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to store standard registers");
+        return -1;
+    }
+
     return 0;
 }
 
@@ -41,12 +152,20 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
 void mshv_arch_init_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
 
+    mshv_setup_hvcall_args(state);
 }
 
 void mshv_arch_destroy_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
+
+    if (state->hvcall_args.base) {
+        qemu_vfree(state->hvcall_args.base);
+    }
 
+    state->hvcall_args = (MshvHvCallArgs){0};
 }
 
 int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
Signature · 2 lines
-- 
2.45.4
#25 lore raw

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

In reply to Aastha Rawat
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     | 119 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+)
include/hw/hyperv/hvgdk_mini.h +0 -0
index a5527d49cf..b5bcea353a 100644

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

Quoted 4 lines
include/hw/hyperv/hvgdk_mini.h +1 -0
@@ -32,6 +32,46 @@ typedef enum hv_register_name {
     HV_REGISTER_PENDING_EVENT0       = 0x00010004,
     HV_REGISTER_PENDING_EVENT1       = 0x00010005,
 
+#if defined(__aarch64__)

do we need those ifdefs here (they create a checkpatch warning)? Since they have a clear prefix and all values are explicitly assigned we might not run into a conflict.

Quoted 208 lines
include/hw/hyperv/hvgdk_mini.h +41 -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,
+    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,
@@ -188,6 +228,8 @@ typedef enum hv_register_name {
     /* Other MSRs */
     HV_X64_REGISTER_MSR_IA32_MISC_ENABLE = 0x000800A0,
 
+#endif
+
     /* Misc */
     HV_X64_REGISTER_HYPERCALL       = 0x00090001,
     HV_REGISTER_GUEST_OS_ID         = 0x00090002,
target/arm/mshv/mshv-all.c +119 -0
index 00c54f5d66..29dd45a123 100644
@@ -10,17 +10,128 @@
  */
 
 #include "qemu/osdep.h"
+#include <sys/ioctl.h>
+
+#include "qemu/error-report.h"
+#include "qemu/memalign.h"
 #include "hw/arm/virt.h"
+
+#include "system/cpus.h"
+#include "target/arm/cpu.h"
+
 #include "system/mshv.h"
 #include "system/mshv_int.h"
+#include "hw/hyperv/hvgdk_mini.h"
+
+static const enum hv_register_name STANDARD_REGISTER_NAMES[32] = {
+    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,
+    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,
+};
+
+static int set_standard_regs(const CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+        assocs[i].value.reg64 = env->xregs[i];
+    }
+
+    /* Last register is the program counter */
+    assocs[n_regs - 1].name = STANDARD_REGISTER_NAMES[n_regs - 1];
+    assocs[n_regs - 1].value.reg64 = env->pc;
+
+    ret = mshv_set_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to set standard registers");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int get_standard_regs(CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+    }
+    ret = mshv_get_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to get standard registers");
+        return -1;
+    }
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        env->xregs[i] = assocs[i].value.reg64;
+    }
+    env->pc = assocs[n_regs - 1].value.reg64;
+
+    return 0;
+}
 
 static int load_regs(CPUState *cpu)
 {
+    int ret;
+
+    ret = get_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to load standard registers");
+        return -1;
+    }
+
     return 0;
 }
 
 static int store_regs(const CPUState *cpu)
 {
+    int ret;
+
+    ret = set_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to store standard registers");
+        return -1;
+    }
+
     return 0;
 }
 
@@ -41,12 +152,20 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
 void mshv_arch_init_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
 
+    mshv_setup_hvcall_args(state);
 }
 
 void mshv_arch_destroy_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
+
+    if (state->hvcall_args.base) {
+        qemu_vfree(state->hvcall_args.base);
+    }
 
+    state->hvcall_args = (MshvHvCallArgs){0};
 }
 
 int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
Signature · 2 lines
-- 
2.45.4
#26 lore raw

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

Quoted 20 lines · Magnus Kulke
Quoted 19 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     | 119 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+)
include/hw/hyperv/hvgdk_mini.h +0 -0
index a5527d49cf..b5bcea353a 100644
@@ -32,6 +32,46 @@ typedef enum hv_register_name {
     HV_REGISTER_PENDING_EVENT0       = 0x00010004,
     HV_REGISTER_PENDING_EVENT1       = 0x00010005,
 
Quoted 5 lines

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

Quoted 1 line
include/hw/hyperv/hvgdk_mini.h +1 -0
+#if defined(__aarch64__)

do we need those ifdefs here (they create a checkpatch warning)? Since they have a clear prefix and all values are explicitly assigned we might not run into a conflict.

I believe it should be fine to get rid of the ifdefs, we won't run into any problems as long as we're writing architecture-specific code.

Quoted 208 lines
Quoted 208 lines
include/hw/hyperv/hvgdk_mini.h +41 -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,
+    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,
@@ -188,6 +228,8 @@ typedef enum hv_register_name {
     /* Other MSRs */
     HV_X64_REGISTER_MSR_IA32_MISC_ENABLE = 0x000800A0,
 
+#endif
+
     /* Misc */
     HV_X64_REGISTER_HYPERCALL       = 0x00090001,
     HV_REGISTER_GUEST_OS_ID         = 0x00090002,
target/arm/mshv/mshv-all.c +119 -0
index 00c54f5d66..29dd45a123 100644
@@ -10,17 +10,128 @@
  */
 
 #include "qemu/osdep.h"
+#include <sys/ioctl.h>
+
+#include "qemu/error-report.h"
+#include "qemu/memalign.h"
 #include "hw/arm/virt.h"
+
+#include "system/cpus.h"
+#include "target/arm/cpu.h"
+
 #include "system/mshv.h"
 #include "system/mshv_int.h"
+#include "hw/hyperv/hvgdk_mini.h"
+
+static const enum hv_register_name STANDARD_REGISTER_NAMES[32] = {
+    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,
+    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,
+};
+
+static int set_standard_regs(const CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+        assocs[i].value.reg64 = env->xregs[i];
+    }
+
+    /* Last register is the program counter */
+    assocs[n_regs - 1].name = STANDARD_REGISTER_NAMES[n_regs - 1];
+    assocs[n_regs - 1].value.reg64 = env->pc;
+
+    ret = mshv_set_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to set standard registers");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int get_standard_regs(CPUState *cpu)
+{
+    size_t n_regs = ARRAY_SIZE(STANDARD_REGISTER_NAMES);
+    struct hv_register_assoc assocs[ARRAY_SIZE(STANDARD_REGISTER_NAMES)] = {};
+    int ret;
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+
+    for (size_t i = 0; i < n_regs; i++) {
+        assocs[i].name = STANDARD_REGISTER_NAMES[i];
+    }
+    ret = mshv_get_generic_regs(cpu, assocs, n_regs);
+    if (ret < 0) {
+        error_report("failed to get standard registers");
+        return -1;
+    }
+
+    for (size_t i = 0; i < n_regs - 1; i++) {
+        env->xregs[i] = assocs[i].value.reg64;
+    }
+    env->pc = assocs[n_regs - 1].value.reg64;
+
+    return 0;
+}
 
 static int load_regs(CPUState *cpu)
 {
+    int ret;
+
+    ret = get_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to load standard registers");
+        return -1;
+    }
+
     return 0;
 }
 
 static int store_regs(const CPUState *cpu)
 {
+    int ret;
+
+    ret = set_standard_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to store standard registers");
+        return -1;
+    }
+
     return 0;
 }
 
@@ -41,12 +152,20 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
 void mshv_arch_init_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
 
+    mshv_setup_hvcall_args(state);
 }
 
 void mshv_arch_destroy_vcpu(CPUState *cpu)
 {
+    AccelCPUState *state = cpu->accel;
+
+    if (state->hvcall_args.base) {
+        qemu_vfree(state->hvcall_args.base);
+    }
 
+    state->hvcall_args = (MshvHvCallArgs){0};
 }
 
 int mshv_arch_accel_init(AccelState *as, MachineState *ms, int mshv_fd)
Signature · 2 lines
-- 
2.45.4
#27 lore raw

PATCH v6 6/14 target/arm: convert host CPU feature ifdef ladder to runtime checks

In reply to Aastha Rawat

Replace the compile-time #if/#elif preprocessor ladder in aarch64_host_initfn() with runtime calls and add mshv_enabled() to it. This ensures the correct accelerator is selected at runtime when multiple accelerators are compiled in.

Add CONFIG_MSHV to the aarch64_cpus[] guard so that mshv accelerator can register the "host" CPU type.

---
 target/arm/cpu64.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
target/arm/cpu64.c +15 -10
index dcd0d698b3..a9e346049d 100644
@@ -811,17 +811,21 @@ void aarch64_host_initfn(Object *obj)
     }
 #endif
 
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);
+    } else if (kvm_enabled()) {
 #if defined(CONFIG_KVM)
-    kvm_arm_set_cpreg_mig_tolerances(cpu);
-    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();
+        kvm_arm_set_cpreg_mig_tolerances(cpu);
 #endif
+        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);
     }
@@ -830,7 +834,8 @@ void aarch64_host_initfn(Object *obj)
 static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "cortex-a57",         .initfn = aarch64_a57_initfn },
     { .name = "cortex-a53",         .initfn = aarch64_a53_initfn },
-#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || defined(CONFIG_WHPX)
+#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || \
+    defined(CONFIG_WHPX) || defined(CONFIG_MSHV)
     { .name = "host",               .initfn = aarch64_host_initfn },
 #endif
 };
Signature · 1 line
2.45.4
#28 lore raw

Re: [PATCH v6 06/14] target/arm: convert host CPU feature ifdef ladder to runtime checks

Quoted 8 lines · Aastha Rawat

Replace the compile-time #if/#elif preprocessor ladder in aarch64_host_initfn() with runtime calls and add mshv_enabled() to it. This ensures the correct accelerator is selected at runtime when multiple accelerators are compiled in.

Add CONFIG_MSHV to the aarch64_cpus[] guard so that mshv accelerator can register the "host" CPU type.

Don't do two things in one commit, please.

Quoted 27 lines
---
 target/arm/cpu64.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
target/arm/cpu64.c +4 -9
index dcd0d698b3..a9e346049d 100644
@@ -811,17 +811,21 @@ void aarch64_host_initfn(Object *obj)
     }
 #endif
 
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);
+    } else if (kvm_enabled()) {
 #if defined(CONFIG_KVM)
-    kvm_arm_set_cpreg_mig_tolerances(cpu);
-    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();
+        kvm_arm_set_cpreg_mig_tolerances(cpu);

target/arm/cpu64.c void aarch64_host_initfn(Object *obj) · line 828

Quoted 10 lines
target/arm/cpu64.c +9 -0
 #endif
+        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();
+    }

The reason for the ifdefs is that if CONFIG_HVF etc aren't set then the functions hvf_arm_set_cpu_features_from_host() don't exist...

thanks
-- PMM

#29 lore raw

Re: [PATCH v6 06/14] target/arm: convert host CPU feature ifdef ladder to runtime checks

Quoted 8 lines · Peter Maydell
Quoted 6 lines · Aastha Rawat

Replace the compile-time #if/#elif preprocessor ladder in aarch64_host_initfn() with runtime calls and add mshv_enabled() to it. This ensures the correct accelerator is selected at runtime when multiple accelerators are compiled in.

Quoted 4 lines
Quoted 2 lines

Add CONFIG_MSHV to the aarch64_cpus[] guard so that mshv accelerator can register the "host" CPU type.

Don't do two things in one commit, please.

Sure, this will be split into two commits in the next revision.

Quoted 33 lines
Quoted 33 lines
---
 target/arm/cpu64.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
target/arm/cpu64.c +9 -9
index dcd0d698b3..a9e346049d 100644
@@ -811,17 +811,21 @@ void aarch64_host_initfn(Object *obj)
     }
 #endif
 
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);
+    } else if (kvm_enabled()) {
 #if defined(CONFIG_KVM)
-    kvm_arm_set_cpreg_mig_tolerances(cpu);
-    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();
+        kvm_arm_set_cpreg_mig_tolerances(cpu);
 #endif
+        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()) {
Quoted 8 lines

target/arm/cpu64.c void aarch64_host_initfn(Object *obj) · line 828

Quoted 4 lines
target/arm/cpu64.c +4 -0
+        whpx_arm_set_cpu_features_from_host(cpu);
+    } else {
+        g_assert_not_reached();
+    }

The reason for the ifdefs is that if CONFIG_HVF etc aren't set then the functions hvf_arm_set_cpu_features_from_host() don't exist...

Yeah, the compile guards should continue to exist. The old ladder was valid for one relevant accelerator backend per host build. MSHV can be built alongside KVM on Linux, so this can be something like:

#if defined(CONFIG_MSHV)
    if (mshv_enabled()) {
        mshv_arm_set_cpu_features_from_host(cpu);
        goto add_common_properties;
    }

#endif

#if defined(CONFIG_KVM)
    if (kvm_enabled()) {
        kvm_arm_set_cpreg_mig_tolerances(cpu);
        kvm_arm_set_cpu_features_from_host(cpu);
        aarch64_add_sve_properties(obj);
        goto add_common_properties;
    }

#endif

#if defined(CONFIG_HVF)
    if (hvf_enabled()) {
        hvf_arm_set_cpu_features_from_host(cpu);
        goto add_common_properties;
    }

#endif

#if defined(CONFIG_WHPX)
    if (whpx_enabled()) {
        whpx_arm_set_cpu_features_from_host(cpu);
        goto add_common_properties;
    }

#endif

    g_assert_not_reached();
#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || \
    defined(CONFIG_WHPX) || defined(CONFIG_MSHV)
add_common_properties:
#endif
    if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
        aarch64_add_pauth_properties(obj);
    }

The runtime checks will select the accelerator actually chosen for the VM when multiple accelerators, such as KVM and MSHV, are compiled together.

Thanks,
Aastha

#30 lore raw

Re: [PATCH v6 06/14] target/arm: convert host CPU feature ifdef ladder to runtime checks

In reply to Aastha Rawat
Quoted 18 lines · Aastha Rawat

Replace the compile-time #if/#elif preprocessor ladder in aarch64_host_initfn() with runtime calls and add mshv_enabled() to it. This ensures the correct accelerator is selected at runtime when multiple accelerators are compiled in.

Add CONFIG_MSHV to the aarch64_cpus[] guard so that mshv accelerator can register the "host" CPU type.

---
 target/arm/cpu64.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
target/arm/cpu64.c +0 -0
index dcd0d698b3..a9e346049d 100644

target/arm/cpu64.c void aarch64_host_initfn(Object *obj) · line 815

Quoted 5 lines
target/arm/cpu64.c +2 -0
@@ -811,17 +811,21 @@ void aarch64_host_initfn(Object *obj)
     }
 #endif
 
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);

as pointed out by Peter, we probably want to keep the defines, but this fn is also not present in this commit yet, so it might need a stub or reordering.

Quoted 38 lines
target/arm/cpu64.c +13 -10
+    } else if (kvm_enabled()) {
 #if defined(CONFIG_KVM)
-    kvm_arm_set_cpreg_mig_tolerances(cpu);
-    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();
+        kvm_arm_set_cpreg_mig_tolerances(cpu);
 #endif
+        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);
     }
@@ -830,7 +834,8 @@ void aarch64_host_initfn(Object *obj)
 static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "cortex-a57",         .initfn = aarch64_a57_initfn },
     { .name = "cortex-a53",         .initfn = aarch64_a53_initfn },
-#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || defined(CONFIG_WHPX)
+#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || \
+    defined(CONFIG_WHPX) || defined(CONFIG_MSHV)
     { .name = "host",               .initfn = aarch64_host_initfn },
 #endif
 };
Signature · 2 lines
-- 
2.45.4
#31 lore raw

Re: [PATCH v6 06/14] target/arm: convert host CPU feature ifdef ladder to runtime checks

Quoted 22 lines · Magnus Kulke
Quoted 21 lines · Aastha Rawat

Replace the compile-time #if/#elif preprocessor ladder in aarch64_host_initfn() with runtime calls and add mshv_enabled() to it. This ensures the correct accelerator is selected at runtime when multiple accelerators are compiled in.

Add CONFIG_MSHV to the aarch64_cpus[] guard so that mshv accelerator can register the "host" CPU type.

---
 target/arm/cpu64.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
target/arm/cpu64.c +0 -0
index dcd0d698b3..a9e346049d 100644
@@ -811,17 +811,21 @@ void aarch64_host_initfn(Object *obj)
     }
 #endif
 
Quoted 6 lines

target/arm/cpu64.c void aarch64_host_initfn(Object *obj) · line 815

Quoted 2 lines
target/arm/cpu64.c +2 -0
+    if (mshv_enabled()) {
+        mshv_arm_set_cpu_features_from_host(cpu);

as pointed out by Peter, we probably want to keep the defines, but this fn is also not present in this commit yet, so it might need a stub or reordering.

I'll move this call to the commit that introduces it.

Quoted 38 lines
Quoted 38 lines
target/arm/cpu64.c +13 -10
+    } else if (kvm_enabled()) {
 #if defined(CONFIG_KVM)
-    kvm_arm_set_cpreg_mig_tolerances(cpu);
-    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();
+        kvm_arm_set_cpreg_mig_tolerances(cpu);
 #endif
+        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);
     }
@@ -830,7 +834,8 @@ void aarch64_host_initfn(Object *obj)
 static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "cortex-a57",         .initfn = aarch64_a57_initfn },
     { .name = "cortex-a53",         .initfn = aarch64_a53_initfn },
-#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || defined(CONFIG_WHPX)
+#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || \
+    defined(CONFIG_WHPX) || defined(CONFIG_MSHV)
     { .name = "host",               .initfn = aarch64_host_initfn },
 #endif
 };
Signature · 2 lines
-- 
2.45.4
#32 lore raw

PATCH v6 7/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.

---
 hw/arm/virt.c                  |   3 +-
 include/hw/hyperv/hvgdk_mini.h |  17 ++++
 include/system/hw_accel.h      |   3 +-
 target/arm/cpu.c               |   6 +-
 target/arm/cpu64.c             |   2 +
 target/arm/mshv/mshv-all.c     | 217 +++++++++++++++++++++++++++++++++++++++++
 target/arm/mshv_arm.h          |  18 ++++
 7 files changed, 262 insertions(+), 4 deletions(-)
hw/arm/virt.c +2 -1
index bdac33a1de8..ee03a85f482 100644
@@ -48,6 +48,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"
@@ -4143,7 +4144,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() || whpx_enabled()) {
+        if (kvm_enabled() || hvf_enabled() || whpx_enabled() || mshv_enabled()) {
             g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("host")));
         }
     }
include/hw/hyperv/hvgdk_mini.h +17 -0
index b5bcea353aa..33c9e4f3e08 100644
@@ -67,6 +67,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,
@@ -988,6 +1002,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/system/hw_accel.h +2 -1
index f0c10b6d805..614ea60be3f 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 10ac6c98318..8ad5e346d1a 100644
@@ -47,6 +47,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"
@@ -1843,8 +1844,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() && !whpx_enabled()) {
-            error_setg(errp, "The 'host' CPU type can only be used with KVM, HVF or WHPX");
+        if (!kvm_enabled() && !hvf_enabled() && !whpx_enabled() && !mshv_enabled()) {
+            error_setg(errp,
+                       "The 'host' CPU type can only be used with KVM, HVF, WHPX, or MSHV");
         } else {
             error_setg(errp, "Failed to retrieve host CPU features");
         }
target/arm/cpu64.c +2 -0
index a9e346049df..71fc6824188 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"
target/arm/mshv/mshv-all.c +217 -0

238 lines not on this page · raw message

target/arm/mshv_arm.h new file +18 -0
new file mode 100644
index 00000000000..a03f9a4405e
@@ -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
#33 lore raw

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

Quoted 29 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.

---
 hw/arm/virt.c                  |   3 +-
 include/hw/hyperv/hvgdk_mini.h |  17 ++++
 include/system/hw_accel.h      |   3 +-
 target/arm/cpu.c               |   6 +-
 target/arm/cpu64.c             |   2 +
 target/arm/mshv/mshv-all.c     | 217 +++++++++++++++++++++++++++++++++++++++++
 target/arm/mshv_arm.h          |  18 ++++
 7 files changed, 262 insertions(+), 4 deletions(-)
hw/arm/virt.c +1 -0
index bdac33a1de8..ee03a85f482 100644
@@ -48,6 +48,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"

hw/arm/virt.c static GPtrArray *virt_get_valid_cpu_types(const MachineState *ms) · line 4147

Quoted 5 lines
hw/arm/virt.c +1 -1
@@ -4143,7 +4144,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() || whpx_enabled()) {
+        if (kvm_enabled() || hvf_enabled() || whpx_enabled() || mshv_enabled()) {

checkpatch complains here

Quoted 65 lines
hw/arm/virt.c +0 -0
             g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("host")));
         }
     }
include/hw/hyperv/hvgdk_mini.h +17 -0
index b5bcea353aa..33c9e4f3e08 100644
@@ -67,6 +67,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,
@@ -988,6 +1002,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/system/hw_accel.h +2 -1
index f0c10b6d805..614ea60be3f 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 +1 -0
index 10ac6c98318..8ad5e346d1a 100644
@@ -47,6 +47,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"

target/arm/cpu.c static void arm_cpu_realizefn(DeviceState *dev, Error **errp) · line 1849

Quoted 8 lines
target/arm/cpu.c +3 -2
@@ -1843,8 +1844,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() && !whpx_enabled()) {
-            error_setg(errp, "The 'host' CPU type can only be used with KVM, HVF or WHPX");
+        if (!kvm_enabled() && !hvf_enabled() && !whpx_enabled() && !mshv_enabled()) {
+            error_setg(errp,
+                       "The 'host' CPU type can only be used with KVM, HVF, WHPX, or MSHV");

checkpatch complains here

Quoted 69 lines
target/arm/cpu.c +0 -0
         } else {
             error_setg(errp, "Failed to retrieve host CPU features");
         }
target/arm/cpu64.c +2 -0
index a9e346049df..71fc6824188 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"
target/arm/mshv/mshv-all.c +28 -0
index 29dd45a1231..d835614fd59 100644
@@ -18,10 +18,23 @@
 
 #include "system/cpus.h"
 #include "target/arm/cpu.h"
+#include "target/arm/internals.h"
+#include "target/arm/mshv_arm.h"
 
 #include "system/mshv.h"
 #include "system/mshv_int.h"
 #include "hw/hyperv/hvgdk_mini.h"
+#include "hw/hyperv/hvhdk_mini.h"
+
+typedef struct ARMHostCPUFeatures {
+    ARMISARegisters isar;
+    uint64_t features;
+    uint64_t midr;
+    uint32_t reset_sctlr;
+    const char *dtb_compatible;
+} ARMHostCPUFeatures;
+
+static ARMHostCPUFeatures arm_host_cpu_features;
 
 static const enum hv_register_name STANDARD_REGISTER_NAMES[32] = {
     HV_ARM64_REGISTER_X0,
@@ -168,8 +181,46 @@ void mshv_arch_destroy_vcpu(CPUState *cpu)
     state->hvcall_args = (MshvHvCallArgs){0};
 }
 
+
+static uint32_t mshv_arm_get_ipa_bit_size(int mshv_fd)
+{
+    int ret;
+    struct hv_input_get_partition_property in = {0};
+    struct hv_output_get_partition_property out = {0};
+    struct mshv_root_hvcall args = {0};
+
+    in.property_code = HV_PARTITION_PROPERTY_PHYSICAL_ADDRESS_WIDTH;
+
+    args.code = HVCALL_GET_PARTITION_PROPERTY;
+    args.in_sz = sizeof(in);
+    args.in_ptr = (uint64_t)&in;
+    args.out_sz = sizeof(out);
+    args.out_ptr = (uint64_t)&out;

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

Quoted 10 lines
target/arm/mshv/mshv-all.c +10 -0
+
+    ret = mshv_hvcall(mshv_fd, &args);
+
+    if (ret < 0) {
+        error_report("Failed to get IPA size");
+        exit(1);
+    }
+
+    return out.property_value;
+}

for consistency we might want to return the propery_value as an out_param (int mshv_fd, uint32_t *ipa_size) and propagate the error in the return value instead of aborting here.

Quoted 63 lines
target/arm/mshv/mshv-all.c +54 -0
+
 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;
 }
 
@@ -198,3 +249,169 @@ int mshv_arch_migration_init(MshvState *s)
 {
     return 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 = FIELD_DP64(id_aa64mmfr0, ID_AA64MMFR0, PARANGE, index);
+    SET_IDREG(isar, ID_AA64MMFR0, id_aa64mmfr0);
+}
+
+static int mshv_get_partition_regs(int vm_fd, hv_register_name *names,
+                             hv_register_value *values, size_t n_regs)
+{
+    int ret = 0;
+    size_t in_sz, names_sz, values_sz;
+    void *in_buffer = qemu_memalign(HV_HYP_PAGE_SIZE, HV_HYP_PAGE_SIZE);
+    void *out_buffer = qemu_memalign(HV_HYP_PAGE_SIZE, HV_HYP_PAGE_SIZE);
+    hv_input_get_vp_registers *in = in_buffer;
+
+    struct mshv_root_hvcall args = {0};
+
+    names_sz = n_regs * sizeof(hv_register_name);
+    in_sz = sizeof(hv_input_get_vp_registers) + names_sz;
+
+    memset(in, 0, HV_HYP_PAGE_SIZE);
+
+    in->vp_index = HV_ANY_VP;
+    in->input_vtl.target_vtl = HV_VTL_ALL;
+    in->input_vtl.use_target_vtl = 1;
+
+    for (int i = 0; i < n_regs; i++) {
+        in->names[i] = names[i];
+    }
+
+    values_sz = n_regs * sizeof(hv_register_value);
+
+    args.code = HVCALL_GET_VP_REGISTERS;

target/arm/mshv/mshv-all.c int mshv_arch_migration_init(MshvState *s) · line 302

Quoted 10 lines
target/arm/mshv/mshv-all.c +10 -0
+    args.in_sz = in_sz;
+    args.in_ptr = (uintptr_t)in_buffer;
+    args.out_sz = values_sz;
+    args.out_ptr = (uintptr_t)out_buffer;
+    args.reps = n_regs;
+
+    ret = mshv_hvcall(vm_fd, &args);
+
+    if (ret == 0) {
+        memcpy(values, out_buffer, values_sz);

we might want to assert that args.reps == n_regs here, since the hypercall might return fewer registers than requested.

Quoted 142 lines
target/arm/mshv/mshv-all.c +115 -0
+    }
+
+    qemu_vfree(in_buffer);
+    qemu_vfree(out_buffer);
+
+    return ret;
+}
+
+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;
+
+    static const struct {
+        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 },
+    };
+
+    size_t n_regs = ARRAY_SIZE(regs);
+    hv_register_name *reg_names = g_new(hv_register_name, n_regs);
+    hv_register_value *reg_values = g_new(hv_register_value, n_regs);
+
+    for (i = 0; i < n_regs; i++) {
+        reg_names[i] = regs[i].name;
+    }
+
+    ret = mshv_get_partition_regs(vm_fd, reg_names, reg_values, n_regs);
+
+    if (ret < 0) {
+        error_report("Failed to get host ID registers");
+        success = false;
+        goto out;
+    }
+
+    for (i = 0; i < n_regs; i++) {
+        ahcf->isar.idregs[regs[i].isar_idx] = reg_values[i].reg64;
+    }
+
+    /* Read MIDR_EL1 from sysfs */
+    if (g_file_get_contents(
+            "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1",
+            &contents, NULL, NULL)) {
+        ahcf->midr = g_ascii_strtoull(contents, NULL, 0);
+    } else {
+        error_report("Failed to read MIDR_EL1 from sysfs");
+        success = false;
+        goto out;
+    }
+
+    ahcf->dtb_compatible = "arm,armv8";
+    ahcf->features = (1ULL << ARM_FEATURE_V8) |
+                     (1ULL << ARM_FEATURE_AARCH64) |
+                     (1ULL << ARM_FEATURE_PMU) |
+                     (1ULL << ARM_FEATURE_GENERIC_TIMER) |
+                     (1ULL << ARM_FEATURE_NEON);
+
+    clamp_id_aa64mmfr0_parange_to_ipa_size(mshv_fd, &ahcf->isar);
+
+    /*
+     * SVE (Scalable Vector Extension) and SME (Scalable Matrix Extension)
+     * require specific context switch logic in the accelerator.
+     * Mask them out for now to ensure stability.
+     */
+    /* Mask SVE in PFR0 */
+    pfr0 = GET_IDREG(&ahcf->isar, ID_AA64PFR0);
+    pfr0 &= ~R_ID_AA64PFR0_SVE_MASK;
+    SET_IDREG(&ahcf->isar, ID_AA64PFR0, pfr0);
+
+    /* Mask SME in PFR1 */
+    pfr1 = GET_IDREG(&ahcf->isar, ID_AA64PFR1);
+    pfr1 &= ~R_ID_AA64PFR1_SME_MASK;
+    SET_IDREG(&ahcf->isar, ID_AA64PFR1, pfr1);
+
+out:
+    g_free(contents);
+    g_free(reg_names);
+    g_free(reg_values);
+    return success;
+}
+
+void mshv_arm_set_cpu_features_from_host(ARMCPU *cpu)
+{
+    if (!arm_host_cpu_features.dtb_compatible) {
+        if (!mshv_enabled() ||
+            !mshv_arm_get_host_cpu_features(&arm_host_cpu_features)) {
+            /*
+             * We can't report this error yet, so flag that we need to
+             * in arm_cpu_realizefn().
+             */
+            cpu->host_cpu_probe_failed = true;
+            return;
+        }
+    }
+
+    cpu->dtb_compatible = arm_host_cpu_features.dtb_compatible;
+    cpu->isar = arm_host_cpu_features.isar;
+    cpu->env.features = arm_host_cpu_features.features;
+    cpu->midr = arm_host_cpu_features.midr;
+    cpu->reset_sctlr = arm_host_cpu_features.reset_sctlr;
+}
target/arm/mshv_arm.h new file +18 -0
new file mode 100644
index 00000000000..a03f9a4405e
@@ -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 · 2 lines
-- 
2.45.4
#34 lore raw

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

Quoted 28 lines · Magnus Kulke
Quoted 27 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.

---
 hw/arm/virt.c                  |   3 +-
 include/hw/hyperv/hvgdk_mini.h |  17 ++++
 include/system/hw_accel.h      |   3 +-
 target/arm/cpu.c               |   6 +-
 target/arm/cpu64.c             |   2 +
 target/arm/mshv/mshv-all.c     | 217 +++++++++++++++++++++++++++++++++++++++++
 target/arm/mshv_arm.h          |  18 ++++
 7 files changed, 262 insertions(+), 4 deletions(-)
hw/arm/virt.c +1 -0
index bdac33a1de8..ee03a85f482 100644
@@ -48,6 +48,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"
Quoted 8 lines

hw/arm/virt.c static GPtrArray *virt_get_valid_cpu_types(const MachineState *ms) · line 4147

Quoted 7 lines
hw/arm/virt.c +1 -1
 #include "hw/core/loader.h"
@@ -4143,7 +4144,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() || whpx_enabled()) {
+        if (kvm_enabled() || hvf_enabled() || whpx_enabled() || mshv_enabled()) {

checkpatch complains here

Ack, will fix.

Quoted 67 lines
Quoted 67 lines
hw/arm/virt.c +0 -0
             g_ptr_array_add(vct, g_strdup(ARM_CPU_TYPE_NAME("host")));
         }
     }
include/hw/hyperv/hvgdk_mini.h +17 -0
index b5bcea353aa..33c9e4f3e08 100644
@@ -67,6 +67,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,
@@ -988,6 +1002,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/system/hw_accel.h +2 -1
index f0c10b6d805..614ea60be3f 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 +1 -0
index 10ac6c98318..8ad5e346d1a 100644
@@ -47,6 +47,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"
@@ -1843,8 +1844,9 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
      * this is the first point where we can report it.
      */
Quoted 8 lines

target/arm/cpu.c static void arm_cpu_realizefn(DeviceState *dev, Error **errp) · line 1849

Quoted 6 lines
target/arm/cpu.c +3 -2
     if (cpu->host_cpu_probe_failed) {
-        if (!kvm_enabled() && !hvf_enabled() && !whpx_enabled()) {
-            error_setg(errp, "The 'host' CPU type can only be used with KVM, HVF or WHPX");
+        if (!kvm_enabled() && !hvf_enabled() && !whpx_enabled() && !mshv_enabled()) {
+            error_setg(errp,
+                       "The 'host' CPU type can only be used with KVM, HVF, WHPX, or MSHV");

checkpatch complains here

Ack, will fix.

Quoted 75 lines
Quoted 75 lines
target/arm/cpu.c +0 -0
         } else {
             error_setg(errp, "Failed to retrieve host CPU features");
         }
target/arm/cpu64.c +2 -0
index a9e346049df..71fc6824188 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"
target/arm/mshv/mshv-all.c +34 -0
index 29dd45a1231..d835614fd59 100644
@@ -18,10 +18,23 @@
 
 #include "system/cpus.h"
 #include "target/arm/cpu.h"
+#include "target/arm/internals.h"
+#include "target/arm/mshv_arm.h"
 
 #include "system/mshv.h"
 #include "system/mshv_int.h"
 #include "hw/hyperv/hvgdk_mini.h"
+#include "hw/hyperv/hvhdk_mini.h"
+
+typedef struct ARMHostCPUFeatures {
+    ARMISARegisters isar;
+    uint64_t features;
+    uint64_t midr;
+    uint32_t reset_sctlr;
+    const char *dtb_compatible;
+} ARMHostCPUFeatures;
+
+static ARMHostCPUFeatures arm_host_cpu_features;
 
 static const enum hv_register_name STANDARD_REGISTER_NAMES[32] = {
     HV_ARM64_REGISTER_X0,
@@ -168,8 +181,46 @@ void mshv_arch_destroy_vcpu(CPUState *cpu)
     state->hvcall_args = (MshvHvCallArgs){0};
 }
 
+
+static uint32_t mshv_arm_get_ipa_bit_size(int mshv_fd)
+{
+    int ret;
+    struct hv_input_get_partition_property in = {0};
+    struct hv_output_get_partition_property out = {0};
+    struct mshv_root_hvcall args = {0};
+
+    in.property_code = HV_PARTITION_PROPERTY_PHYSICAL_ADDRESS_WIDTH;
+
+    args.code = HVCALL_GET_PARTITION_PROPERTY;
+    args.in_sz = sizeof(in);
+    args.in_ptr = (uint64_t)&in;
+    args.out_sz = sizeof(out);
+    args.out_ptr = (uint64_t)&out;
+
+    ret = mshv_hvcall(mshv_fd, &args);
+
+    if (ret < 0) {
+        error_report("Failed to get IPA size");
+        exit(1);
Quoted 8 lines

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

Quoted 4 lines
target/arm/mshv/mshv-all.c +4 -0
+    }
+
+    return out.property_value;
+}

for consistency we might want to return the propery_value as an out_param (int mshv_fd, uint32_t *ipa_size) and propagate the error in the return value instead of aborting here.

Agreed.

Quoted 68 lines
Quoted 68 lines
target/arm/mshv/mshv-all.c +59 -0
+
 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;
 }
 
@@ -198,3 +249,169 @@ int mshv_arch_migration_init(MshvState *s)
 {
     return 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 = FIELD_DP64(id_aa64mmfr0, ID_AA64MMFR0, PARANGE, index);
+    SET_IDREG(isar, ID_AA64MMFR0, id_aa64mmfr0);
+}
+
+static int mshv_get_partition_regs(int vm_fd, hv_register_name *names,
+                             hv_register_value *values, size_t n_regs)
+{
+    int ret = 0;
+    size_t in_sz, names_sz, values_sz;
+    void *in_buffer = qemu_memalign(HV_HYP_PAGE_SIZE, HV_HYP_PAGE_SIZE);
+    void *out_buffer = qemu_memalign(HV_HYP_PAGE_SIZE, HV_HYP_PAGE_SIZE);
+    hv_input_get_vp_registers *in = in_buffer;
+
+    struct mshv_root_hvcall args = {0};
+
+    names_sz = n_regs * sizeof(hv_register_name);
+    in_sz = sizeof(hv_input_get_vp_registers) + names_sz;
+
+    memset(in, 0, HV_HYP_PAGE_SIZE);
+
+    in->vp_index = HV_ANY_VP;
+    in->input_vtl.target_vtl = HV_VTL_ALL;
+    in->input_vtl.use_target_vtl = 1;
+
+    for (int i = 0; i < n_regs; i++) {
+        in->names[i] = names[i];
+    }
+
+    values_sz = n_regs * sizeof(hv_register_value);
+
+    args.code = HVCALL_GET_VP_REGISTERS;
+    args.in_sz = in_sz;
+    args.in_ptr = (uintptr_t)in_buffer;
+    args.out_sz = values_sz;
+    args.out_ptr = (uintptr_t)out_buffer;
+    args.reps = n_regs;
Quoted 8 lines

target/arm/mshv/mshv-all.c int mshv_arch_migration_init(MshvState *s) · line 302

Quoted 5 lines
target/arm/mshv/mshv-all.c +5 -0
+
+    ret = mshv_hvcall(vm_fd, &args);
+
+    if (ret == 0) {
+        memcpy(values, out_buffer, values_sz);

we might want to assert that args.reps == n_regs here, since the hypercall might return fewer registers than requested.

Right, will add this.

Quoted 142 lines
Quoted 142 lines
target/arm/mshv/mshv-all.c +115 -0
+    }
+
+    qemu_vfree(in_buffer);
+    qemu_vfree(out_buffer);
+
+    return ret;
+}
+
+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;
+
+    static const struct {
+        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 },
+    };
+
+    size_t n_regs = ARRAY_SIZE(regs);
+    hv_register_name *reg_names = g_new(hv_register_name, n_regs);
+    hv_register_value *reg_values = g_new(hv_register_value, n_regs);
+
+    for (i = 0; i < n_regs; i++) {
+        reg_names[i] = regs[i].name;
+    }
+
+    ret = mshv_get_partition_regs(vm_fd, reg_names, reg_values, n_regs);
+
+    if (ret < 0) {
+        error_report("Failed to get host ID registers");
+        success = false;
+        goto out;
+    }
+
+    for (i = 0; i < n_regs; i++) {
+        ahcf->isar.idregs[regs[i].isar_idx] = reg_values[i].reg64;
+    }
+
+    /* Read MIDR_EL1 from sysfs */
+    if (g_file_get_contents(
+            "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1",
+            &contents, NULL, NULL)) {
+        ahcf->midr = g_ascii_strtoull(contents, NULL, 0);
+    } else {
+        error_report("Failed to read MIDR_EL1 from sysfs");
+        success = false;
+        goto out;
+    }
+
+    ahcf->dtb_compatible = "arm,armv8";
+    ahcf->features = (1ULL << ARM_FEATURE_V8) |
+                     (1ULL << ARM_FEATURE_AARCH64) |
+                     (1ULL << ARM_FEATURE_PMU) |
+                     (1ULL << ARM_FEATURE_GENERIC_TIMER) |
+                     (1ULL << ARM_FEATURE_NEON);
+
+    clamp_id_aa64mmfr0_parange_to_ipa_size(mshv_fd, &ahcf->isar);
+
+    /*
+     * SVE (Scalable Vector Extension) and SME (Scalable Matrix Extension)
+     * require specific context switch logic in the accelerator.
+     * Mask them out for now to ensure stability.
+     */
+    /* Mask SVE in PFR0 */
+    pfr0 = GET_IDREG(&ahcf->isar, ID_AA64PFR0);
+    pfr0 &= ~R_ID_AA64PFR0_SVE_MASK;
+    SET_IDREG(&ahcf->isar, ID_AA64PFR0, pfr0);
+
+    /* Mask SME in PFR1 */
+    pfr1 = GET_IDREG(&ahcf->isar, ID_AA64PFR1);
+    pfr1 &= ~R_ID_AA64PFR1_SME_MASK;
+    SET_IDREG(&ahcf->isar, ID_AA64PFR1, pfr1);
+
+out:
+    g_free(contents);
+    g_free(reg_names);
+    g_free(reg_values);
+    return success;
+}
+
+void mshv_arm_set_cpu_features_from_host(ARMCPU *cpu)
+{
+    if (!arm_host_cpu_features.dtb_compatible) {
+        if (!mshv_enabled() ||
+            !mshv_arm_get_host_cpu_features(&arm_host_cpu_features)) {
+            /*
+             * We can't report this error yet, so flag that we need to
+             * in arm_cpu_realizefn().
+             */
+            cpu->host_cpu_probe_failed = true;
+            return;
+        }
+    }
+
+    cpu->dtb_compatible = arm_host_cpu_features.dtb_compatible;
+    cpu->isar = arm_host_cpu_features.isar;
+    cpu->env.features = arm_host_cpu_features.features;
+    cpu->midr = arm_host_cpu_features.midr;
+    cpu->reset_sctlr = arm_host_cpu_features.reset_sctlr;
+}
target/arm/mshv_arm.h new file +18 -0
new file mode 100644
index 00000000000..a03f9a4405e
@@ -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 · 2 lines
-- 
2.45.4
#35 lore raw

PATCH v6 8/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 f3f1825b0b..9603f0360e 100644
@@ -235,6 +235,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
#36 lore raw

Re: [PATCH v6 08/14] accel/mshv: Add access_vp_regs synthetic proc features

Quoted 16 lines · 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 +0 -0
index f3f1825b0b..9603f0360e 100644

accel/mshv/mshv-all.c static int set_synthetic_proc_features(int vm_fd) · line 238

Quoted 4 lines
accel/mshv/mshv-all.c +1 -0
@@ -235,6 +235,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;

is this safe to use on x86_64? if not/unsure we can use the mshv_arch_amend_proc_features to only set this bit on arm64

include/hw/hyperv/hvhdk.h union hv_partition_synthetic_processor_features { · line 97

Quoted 12 lines
accel/mshv/mshv-all.c +0 -0
     mshv_arch_amend_proc_features(&features);
 
include/hw/hyperv/hvhdk.h +1 -0
index 9e6dcb22f6..d0adbea0c7 100644
@@ -94,11 +94,16 @@ union hv_partition_synthetic_processor_features {
          */
         uint64_t access_partition_reference_tsc:1;
 
+#if defined(__x86_64__)

Maybe we can use the HOST_x86_64/AARCH64 qemu equivalent here.

Quoted 126 lines
include/hw/hyperv/hvhdk.h +89 -1
+
         /*
          * Partition has access to the guest idle reg. Corresponds to
          * access_guest_idle_reg privilege.
          */
         uint64_t access_guest_idle_reg:1;
+#else
+        uint64_t reserved_z10:1;
+#endif
 
         /*
          * Partition has access to frequency regs. corresponds to
@@ -110,11 +115,16 @@ union hv_partition_synthetic_processor_features {
         uint64_t reserved_z13:1; /* Reserved for access_root_scheduler_reg */
         uint64_t reserved_z14:1; /* Reserved for access_tsc_invariant_controls */
 
+#if defined(__x86_64__)
+
         /*
          * Extended GVA ranges for HvCallFlushVirtualAddressList hypercall.
          * Corresponds to privilege.
          */
         uint64_t enable_extended_gva_ranges_for_flush_virtual_address_list:1;
+#else
+        uint64_t reserved_z15:1;
+#endif
 
         uint64_t reserved_z16:1; /* Reserved for access_vsm. */
         uint64_t reserved_z17:1; /* Reserved for access_vp_registers. */
@@ -161,13 +171,92 @@ union hv_partition_synthetic_processor_features {
         /* HvCallRetargetDeviceInterrupt is supported. */
         uint64_t retarget_device_interrupt:1;
 
+#if defined(__x86_64__)
         /* HvCallRestorePartitionTime is supported. */
         uint64_t restore_time:1;
 
         /* EnlightenedVmcs nested enlightenment is supported. */
         uint64_t enlightened_vmcs:1;
 
-        uint64_t reserved:30;
+        uint64_t nested_debug_ctl:1;
+        uint64_t synthetic_time_unhalted_timer:1;
+        uint64_t idle_spec_ctrl:1;
+
+#else
+        uint64_t reserved_z31:1;
+        uint64_t reserved_z32:1;
+        uint64_t reserved_z33:1;
+        uint64_t reserved_z34:1;
+        uint64_t reserved_z35:1;
+#endif
+
+#if defined(__aarch64__)
+        /*
+         * Register intercepts supported in V1. As more registers are supported
+         * in future releases, new bits will be added here to prevent migration
+         * between incompatible hosts.
+         *
+         * List of registers supported in V1:
+         * 1. TPIDRRO_EL0
+         * 2. TPIDR_EL1
+         * 3. SCTLR_EL1 - Supports write intercept mask.
+         * 4. VBAR_EL1
+         * 5. TCR_EL1 - Supports write intercept mask.
+         * 6. MAIR_EL1 - Supports write intercept mask.
+         * 7. CPACR_EL1 - Supports write intercept mask.
+         * 8. CONTEXTIDR_EL1
+         * 9. PAuth keys (total 10 registers)
+         * 10. HvArm64RegisterSyntheticException
+         */
+        uint64_t register_intercepts_v1:1;
+#else
+        uint64_t reserved_z36:1;
+#endif
+
+        /* HvCallWakeVps is supported */
+        uint64_t wake_vps:1;
+
+        /*
+         * HvCallGet/SetVpRegisters is supported.
+         * Corresponds to AccessVpRegisters privilege.
+         * This feature only affects exo partitions.
+         */
+        uint64_t access_vp_regs:1;
+
+#if defined(__aarch64__)
+        /* HvCallSyncContext/Ex is supported. */
+        uint64_t sync_context:1;
+#else
+        uint64_t reserved_z39:1;
+#endif /* __aarch64__ */
+
+        /*
+         * Management VTL synic support is allowed.
+         * Corresponds to the ManagementVtlSynicSupport privilege.
+         */
+        uint64_t management_vtl_synic_support:1;
+
+#if defined(__x86_64__)
+        /*
+         * Hypervisor supports guest mechanism to signal pending interrupts to
+         * paravisor.
+         */
+        uint64_t proxy_interrupt_doorbell_support:1;
+#else
+        uint64_t reserved_z41:1;
+#endif
+
+#if defined(__aarch64__)
+        /* InterceptSystemResetAvailable is exposed. */
+        uint64_t intercept_system_reset:1;
+#else
+        uint64_t reserved_z42:1;
+#endif
+
+        /* Hypercalls for host MMIO operations are available. */
+        uint64_t mmio_hypercalls:1;
+
+        uint64_t reserved:20;
     };
 };
 
Signature · 2 lines
-- 
2.45.4
#37 lore raw

Re: [PATCH v6 08/14] accel/mshv: Add access_vp_regs synthetic proc features

Quoted 12 lines · Magnus Kulke
Quoted 11 lines · 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(-)
Quoted 12 lines

accel/mshv/mshv-all.c static int set_synthetic_proc_features(int vm_fd) · line 238

Quoted 9 lines
accel/mshv/mshv-all.c +1 -0
index f3f1825b0b..9603f0360e 100644
@@ -235,6 +235,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;

is this safe to use on x86_64? if not/unsure we can use the mshv_arch_amend_proc_features to only set this bit on arm64

This is unconditionally enabled for MSHV in cloud-hypervisor so I believe it to be safe for x86.

Quoted 141 lines

include/hw/hyperv/hvhdk.h union hv_partition_synthetic_processor_features { · line 97

Quoted 12 lines
accel/mshv/mshv-all.c +0 -0
     mshv_arch_amend_proc_features(&features);
 
include/hw/hyperv/hvhdk.h +1 -0
index 9e6dcb22f6..d0adbea0c7 100644
@@ -94,11 +94,16 @@ union hv_partition_synthetic_processor_features {
          */
         uint64_t access_partition_reference_tsc:1;
 
+#if defined(__x86_64__)

Maybe we can use the HOST_x86_64/AARCH64 qemu equivalent here.

Quoted 126 lines
include/hw/hyperv/hvhdk.h +89 -1
+
         /*
          * Partition has access to the guest idle reg. Corresponds to
          * access_guest_idle_reg privilege.
          */
         uint64_t access_guest_idle_reg:1;
+#else
+        uint64_t reserved_z10:1;
+#endif
 
         /*
          * Partition has access to frequency regs. corresponds to
@@ -110,11 +115,16 @@ union hv_partition_synthetic_processor_features {
         uint64_t reserved_z13:1; /* Reserved for access_root_scheduler_reg */
         uint64_t reserved_z14:1; /* Reserved for access_tsc_invariant_controls */
 
+#if defined(__x86_64__)
+
         /*
          * Extended GVA ranges for HvCallFlushVirtualAddressList hypercall.
          * Corresponds to privilege.
          */
         uint64_t enable_extended_gva_ranges_for_flush_virtual_address_list:1;
+#else
+        uint64_t reserved_z15:1;
+#endif
 
         uint64_t reserved_z16:1; /* Reserved for access_vsm. */
         uint64_t reserved_z17:1; /* Reserved for access_vp_registers. */
@@ -161,13 +171,92 @@ union hv_partition_synthetic_processor_features {
         /* HvCallRetargetDeviceInterrupt is supported. */
         uint64_t retarget_device_interrupt:1;
 
+#if defined(__x86_64__)
         /* HvCallRestorePartitionTime is supported. */
         uint64_t restore_time:1;
 
         /* EnlightenedVmcs nested enlightenment is supported. */
         uint64_t enlightened_vmcs:1;
 
-        uint64_t reserved:30;
+        uint64_t nested_debug_ctl:1;
+        uint64_t synthetic_time_unhalted_timer:1;
+        uint64_t idle_spec_ctrl:1;
+
+#else
+        uint64_t reserved_z31:1;
+        uint64_t reserved_z32:1;
+        uint64_t reserved_z33:1;
+        uint64_t reserved_z34:1;
+        uint64_t reserved_z35:1;
+#endif
+
+#if defined(__aarch64__)
+        /*
+         * Register intercepts supported in V1. As more registers are supported
+         * in future releases, new bits will be added here to prevent migration
+         * between incompatible hosts.
+         *
+         * List of registers supported in V1:
+         * 1. TPIDRRO_EL0
+         * 2. TPIDR_EL1
+         * 3. SCTLR_EL1 - Supports write intercept mask.
+         * 4. VBAR_EL1
+         * 5. TCR_EL1 - Supports write intercept mask.
+         * 6. MAIR_EL1 - Supports write intercept mask.
+         * 7. CPACR_EL1 - Supports write intercept mask.
+         * 8. CONTEXTIDR_EL1
+         * 9. PAuth keys (total 10 registers)
+         * 10. HvArm64RegisterSyntheticException
+         */
+        uint64_t register_intercepts_v1:1;
+#else
+        uint64_t reserved_z36:1;
+#endif
+
+        /* HvCallWakeVps is supported */
+        uint64_t wake_vps:1;
+
+        /*
+         * HvCallGet/SetVpRegisters is supported.
+         * Corresponds to AccessVpRegisters privilege.
+         * This feature only affects exo partitions.
+         */
+        uint64_t access_vp_regs:1;
+
+#if defined(__aarch64__)
+        /* HvCallSyncContext/Ex is supported. */
+        uint64_t sync_context:1;
+#else
+        uint64_t reserved_z39:1;
+#endif /* __aarch64__ */
+
+        /*
+         * Management VTL synic support is allowed.
+         * Corresponds to the ManagementVtlSynicSupport privilege.
+         */
+        uint64_t management_vtl_synic_support:1;
+
+#if defined(__x86_64__)
+        /*
+         * Hypervisor supports guest mechanism to signal pending interrupts to
+         * paravisor.
+         */
+        uint64_t proxy_interrupt_doorbell_support:1;
+#else
+        uint64_t reserved_z41:1;
+#endif
+
+#if defined(__aarch64__)
+        /* InterceptSystemResetAvailable is exposed. */
+        uint64_t intercept_system_reset:1;
+#else
+        uint64_t reserved_z42:1;
+#endif
+
+        /* Hypercalls for host MMIO operations are available. */
+        uint64_t mmio_hypercalls:1;
+
+        uint64_t reserved:20;
     };
 };
 
Signature · 2 lines
-- 
2.45.4
#38 lore raw

Re: [PATCH v6 08/14] accel/mshv: Add access_vp_regs synthetic proc features

Quoted 26 lines · Aastha Rawat
Quoted 12 lines · Magnus Kulke
Quoted 11 lines · 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(-)
Quoted 12 lines

accel/mshv/mshv-all.c static int set_synthetic_proc_features(int vm_fd) · line 238

Quoted 9 lines
accel/mshv/mshv-all.c +1 -0
index f3f1825b0b..9603f0360e 100644
@@ -235,6 +235,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;

is this safe to use on x86_64? if not/unsure we can use the mshv_arch_amend_proc_features to only set this bit on arm64

Quoted 17 lines

This is unconditionally enabled for MSHV in cloud-hypervisor so I believe it to be safe for x86.

Quoted 14 lines

include/hw/hyperv/hvhdk.h union hv_partition_synthetic_processor_features { · line 97

Quoted 12 lines
accel/mshv/mshv-all.c +0 -0
     mshv_arch_amend_proc_features(&features);
 
include/hw/hyperv/hvhdk.h +1 -0
index 9e6dcb22f6..d0adbea0c7 100644
@@ -94,11 +94,16 @@ union hv_partition_synthetic_processor_features {
          */
         uint64_t access_partition_reference_tsc:1;
 
+#if defined(__x86_64__)

Maybe we can use the HOST_x86_64/AARCH64 qemu equivalent here.

Replying to the second point I missed in my last message...

Yes, using HOST_X86_64/HOST_AARCH64 also removes the checkpatch.pl warnings. Will do.

Quoted 126 lines
Quoted 126 lines
Quoted 126 lines
include/hw/hyperv/hvhdk.h +89 -1
+
         /*
          * Partition has access to the guest idle reg. Corresponds to
          * access_guest_idle_reg privilege.
          */
         uint64_t access_guest_idle_reg:1;
+#else
+        uint64_t reserved_z10:1;
+#endif
 
         /*
          * Partition has access to frequency regs. corresponds to
@@ -110,11 +115,16 @@ union hv_partition_synthetic_processor_features {
         uint64_t reserved_z13:1; /* Reserved for access_root_scheduler_reg */
         uint64_t reserved_z14:1; /* Reserved for access_tsc_invariant_controls */
 
+#if defined(__x86_64__)
+
         /*
          * Extended GVA ranges for HvCallFlushVirtualAddressList hypercall.
          * Corresponds to privilege.
          */
         uint64_t enable_extended_gva_ranges_for_flush_virtual_address_list:1;
+#else
+        uint64_t reserved_z15:1;
+#endif
 
         uint64_t reserved_z16:1; /* Reserved for access_vsm. */
         uint64_t reserved_z17:1; /* Reserved for access_vp_registers. */
@@ -161,13 +171,92 @@ union hv_partition_synthetic_processor_features {
         /* HvCallRetargetDeviceInterrupt is supported. */
         uint64_t retarget_device_interrupt:1;
 
+#if defined(__x86_64__)
         /* HvCallRestorePartitionTime is supported. */
         uint64_t restore_time:1;
 
         /* EnlightenedVmcs nested enlightenment is supported. */
         uint64_t enlightened_vmcs:1;
 
-        uint64_t reserved:30;
+        uint64_t nested_debug_ctl:1;
+        uint64_t synthetic_time_unhalted_timer:1;
+        uint64_t idle_spec_ctrl:1;
+
+#else
+        uint64_t reserved_z31:1;
+        uint64_t reserved_z32:1;
+        uint64_t reserved_z33:1;
+        uint64_t reserved_z34:1;
+        uint64_t reserved_z35:1;
+#endif
+
+#if defined(__aarch64__)
+        /*
+         * Register intercepts supported in V1. As more registers are supported
+         * in future releases, new bits will be added here to prevent migration
+         * between incompatible hosts.
+         *
+         * List of registers supported in V1:
+         * 1. TPIDRRO_EL0
+         * 2. TPIDR_EL1
+         * 3. SCTLR_EL1 - Supports write intercept mask.
+         * 4. VBAR_EL1
+         * 5. TCR_EL1 - Supports write intercept mask.
+         * 6. MAIR_EL1 - Supports write intercept mask.
+         * 7. CPACR_EL1 - Supports write intercept mask.
+         * 8. CONTEXTIDR_EL1
+         * 9. PAuth keys (total 10 registers)
+         * 10. HvArm64RegisterSyntheticException
+         */
+        uint64_t register_intercepts_v1:1;
+#else
+        uint64_t reserved_z36:1;
+#endif
+
+        /* HvCallWakeVps is supported */
+        uint64_t wake_vps:1;
+
+        /*
+         * HvCallGet/SetVpRegisters is supported.
+         * Corresponds to AccessVpRegisters privilege.
+         * This feature only affects exo partitions.
+         */
+        uint64_t access_vp_regs:1;
+
+#if defined(__aarch64__)
+        /* HvCallSyncContext/Ex is supported. */
+        uint64_t sync_context:1;
+#else
+        uint64_t reserved_z39:1;
+#endif /* __aarch64__ */
+
+        /*
+         * Management VTL synic support is allowed.
+         * Corresponds to the ManagementVtlSynicSupport privilege.
+         */
+        uint64_t management_vtl_synic_support:1;
+
+#if defined(__x86_64__)
+        /*
+         * Hypervisor supports guest mechanism to signal pending interrupts to
+         * paravisor.
+         */
+        uint64_t proxy_interrupt_doorbell_support:1;
+#else
+        uint64_t reserved_z41:1;
+#endif
+
+#if defined(__aarch64__)
+        /* InterceptSystemResetAvailable is exposed. */
+        uint64_t intercept_system_reset:1;
+#else
+        uint64_t reserved_z42:1;
+#endif
+
+        /* Hypercalls for host MMIO operations are available. */
+        uint64_t mmio_hypercalls:1;
+
+        uint64_t reserved:20;
     };
 };
 
Signature · 2 lines
-- 
2.45.4
#39 lore raw

PATCH v6 9/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 8ad5e346d1..624cb72610 100644
@@ -1295,7 +1295,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
Reviewed-by on v6 9/14
#40 lore raw

Re: [PATCH v6 09/14] target/arm: cpu: Mark MSHV supporting PSCI 1.3

Quoted 24 lines · 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 8ad5e346d1..624cb72610 100644
@@ -1295,7 +1295,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 · 2 lines
-- 
2.45.4
#41 lore raw

PATCH v6 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        | 62 ++++++++++++++++++++++++++++++++++++++++++++++
 target/arm/helper.h        | 16 ++++++++++++
 target/arm/hvf/hvf.c       | 57 +++++++++++++++++++-----------------------
 target/arm/syndrome.h      | 47 +++++++++++++++++++++++++++++++++++
 target/arm/whpx/whpx-all.c | 45 ++++++++++++++-------------------
 5 files changed, 169 insertions(+), 58 deletions(-)
target/arm/helper.c +62 -0
index ff0b766b73d..5b14f0bff72 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);
@@ -9455,6 +9457,66 @@ 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,
+                     const struct arm_emul_ops *ops)
+{
+    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 ? ops->get_reg(cpu, 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;
+        }
+        if (reg_index < 31) {
+            ops->set_reg(cpu, reg_index, val);
+        }
+    }
+
+    return 0;
+}
+
 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
 {
     /*
target/arm/helper.h +16 -0
index b1c26c180ea..503404112d4 100644
@@ -3,8 +3,24 @@
 #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"
+
+/*
+ * Callback ops for per-register access during MMIO emulation.
+ * Each hypervisor backend provides its own implementation to avoid
+ * syncing the full CPU state for a single-register MMIO access.
+ */
+struct arm_emul_ops {
+    uint64_t (*get_reg)(CPUState *cpu, int reg);
+    void (*set_reg)(CPUState *cpu, int reg, uint64_t val);
+};
+
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa,
+                     const struct arm_emul_ops *ops);
 
 #define HELPER_H "tcg/helper-defs.h"
 #include "exec/helper-proto.h.inc"
target/arm/hvf/hvf.c +26 -31

107 lines not on this page · raw message

target/arm/syndrome.h +47 -0

53 lines not on this page · raw message

target/arm/whpx/whpx-all.c +18 -27

58 lines not on this page · raw message

Signature · 1 line
2.45.4
#42 lore raw

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

Quoted 9 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.

---

target/arm/helper.c static void arm_cpu_do_interrupt_aarch32(CPUState *cs) · line 9464

Quoted 5 lines
target/arm/helper.c +5 -0
+/*
+ * Emulate a data abort syndrome for MMIO/guest memory access
+ */
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa,
+                     const struct arm_emul_ops *ops)

Please don't put more stuff into helper.c. It is big enough as it is.

target/arm/helper.h line 7

Quoted 10 lines
target/arm/helper.h +2 -0
index b1c26c180ea..503404112d4 100644
@@ -3,8 +3,24 @@
 #ifndef HELPER__H
 #define HELPER__H
 
+#include <stdint.h>
+#include <stdbool.h>

osdep.h includes stdint and stdbool for you, you don't need to include them manually anywhere.

Quoted 6 lines
target/arm/helper.h +4 -0
 #include "exec/helper-proto-common.h"
 #include "exec/helper-gen-common.h"
+#include "target/arm/syndrome.h"
+
+/*
+ * Callback ops for per-register access during MMIO emulation.

target/arm/helper.h line 23

Quoted 10 lines
target/arm/helper.h +10 -0
+ * Each hypervisor backend provides its own implementation to avoid
+ * syncing the full CPU state for a single-register MMIO access.
+ */
+struct arm_emul_ops {
+    uint64_t (*get_reg)(CPUState *cpu, int reg);
+    void (*set_reg)(CPUState *cpu, int reg, uint64_t val);
+};
+
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa,
+                     const struct arm_emul_ops *ops);

helper.h is structly for the autogenerated prototypes for TCG helper functions (hence its name). Put your prototype somewhere else.

Quoted 8 lines
target/arm/syndrome.h +0 -0
index 2a6fa903f27..8b4cc7f7b21 100644
@@ -106,6 +106,53 @@ typedef enum {
     GCS_IT_GCSPOPX = 9,
 } GCSInstructionType;
 

target/arm/syndrome.h typedef enum { · line 118

Quoted 10 lines
target/arm/syndrome.h +10 -0
+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;

Do not use bitfields. They are not portable (notably, they have endianness issues). syndrome. already sets up FIELD() definitions that you can use with FIELD_EX32 to extract subfields of the syndrome.

target/arm/syndrome.h typedef enum { · line 144

Quoted 4 lines
target/arm/syndrome.h +4 -0
+typedef enum {
+    data_abort_lower = 36,
+    data_abort = 37,
+} ExceptionClass;

Don't define new versions of what we already have either. These are EC_DATAABORT and EC_DATAABORT_SAME_EL.

target/arm/syndrome.h typedef enum { · line 151

Quoted 7 lines
target/arm/syndrome.h +7 -0
+
+#define ARM_EL_EC_LENGTH 6
+#define ARM_EL_EC_SHIFT 26
+#define ARM_EL_IL_SHIFT 25
+#define ARM_EL_ISV_SHIFT 24
+#define ARM_EL_IL (1 << ARM_EL_IL_SHIFT)
+#define ARM_EL_ISV (1 << ARM_EL_ISV_SHIFT)

This also is reimplementing what the header already gives you.

-- PMM

#43 lore raw

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

Quoted 11 lines · Peter Maydell
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.

---

Quoted 8 lines

target/arm/helper.c static void arm_cpu_do_interrupt_aarch32(CPUState *cs) · line 9464

Quoted 5 lines
target/arm/helper.c +5 -0
+/*
+ * Emulate a data abort syndrome for MMIO/guest memory access
+ */
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa,
+                     const struct arm_emul_ops *ops)

Please don't put more stuff into helper.c. It is big enough as it is.

I plan to introduce target/arm/hyp-mmio.c for this extraction in that case.

Quoted 8 lines
Quoted 8 lines
target/arm/helper.h +0 -0
index b1c26c180ea..503404112d4 100644
@@ -3,8 +3,24 @@
 #ifndef HELPER__H
 #define HELPER__H
 
Quoted 5 lines

target/arm/helper.h line 7

Quoted 2 lines
target/arm/helper.h +2 -0
+#include <stdint.h>
+#include <stdbool.h>

osdep.h includes stdint and stdbool for you, you don't need to include them manually anywhere.

Got it.

Quoted 11 lines
Quoted 11 lines
target/arm/helper.h +9 -0
 #include "exec/helper-proto-common.h"
 #include "exec/helper-gen-common.h"
+#include "target/arm/syndrome.h"
+
+/*
+ * Callback ops for per-register access during MMIO emulation.
+ * Each hypervisor backend provides its own implementation to avoid
+ * syncing the full CPU state for a single-register MMIO access.
+ */
+struct arm_emul_ops {
+    uint64_t (*get_reg)(CPUState *cpu, int reg);
Quoted 8 lines

target/arm/helper.h line 23

Quoted 5 lines
target/arm/helper.h +5 -0
+    void (*set_reg)(CPUState *cpu, int reg, uint64_t val);
+};
+
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa,
+                     const struct arm_emul_ops *ops);

helper.h is structly for the autogenerated prototypes for TCG helper functions (hence its name). Put your prototype somewhere else.

Will move to target/arm/hyp-mmio.h, paired with hyp-mmio.c.

Quoted 8 lines
Quoted 8 lines
target/arm/syndrome.h +0 -0
index 2a6fa903f27..8b4cc7f7b21 100644
@@ -106,6 +106,53 @@ typedef enum {
     GCS_IT_GCSPOPX = 9,
 } GCSInstructionType;
 
Quoted 14 lines

target/arm/syndrome.h typedef enum { · line 118

Quoted 10 lines
target/arm/syndrome.h +10 -0
+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;

Do not use bitfields. They are not portable (notably, they have endianness issues). syndrome. already sets up FIELD() definitions that you can use with FIELD_EX32 to extract subfields of the syndrome.

Sure, will use the existing SYNDROME and DABORT_ISS definitions.

Quoted 8 lines

target/arm/syndrome.h typedef enum { · line 144

Quoted 4 lines
target/arm/syndrome.h +4 -0
+typedef enum {
+    data_abort_lower = 36,
+    data_abort = 37,
+} ExceptionClass;

Don't define new versions of what we already have either. These are EC_DATAABORT and EC_DATAABORT_SAME_EL.

Quoted 9 lines

target/arm/syndrome.h typedef enum { · line 151

Quoted 7 lines
target/arm/syndrome.h +7 -0
+
+#define ARM_EL_EC_LENGTH 6
+#define ARM_EL_EC_SHIFT 26
+#define ARM_EL_IL_SHIFT 25
+#define ARM_EL_ISV_SHIFT 24
+#define ARM_EL_IL (1 << ARM_EL_IL_SHIFT)
+#define ARM_EL_ISV (1 << ARM_EL_ISV_SHIFT)

This also is reimplementing what the header already gives you.

Ack.

Thanks,
Aastha

#44 lore raw

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

In reply to Aastha Rawat
Quoted 4 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.

Generally, this is a patch with some potential for undesired fallout and we need testing on the consolidated platforms (WHPX and HVF) and/or close scrutiny from the maintainers. Maybe we can send this one as an individual patch?

Quoted 14 lines
---
 target/arm/helper.c        | 62 ++++++++++++++++++++++++++++++++++++++++++++++
 target/arm/helper.h        | 16 ++++++++++++
 target/arm/hvf/hvf.c       | 57 +++++++++++++++++++-----------------------
 target/arm/syndrome.h      | 47 +++++++++++++++++++++++++++++++++++
 target/arm/whpx/whpx-all.c | 45 ++++++++++++++-------------------
 5 files changed, 169 insertions(+), 58 deletions(-)
target/arm/helper.c +0 -0
index ff0b766b73d..5b14f0bff72 100644

target/arm/helper.c line 37

Quoted 4 lines
target/arm/helper.c +1 -0
@@ -34,6 +34,8 @@
 #endif
 #include "cpregs.h"
 #include "target/arm/gtimer.h"
+#include "target/arm/helper.h"

This will introduce a dependency on TCG, which might be undesirable:

target/arm/helper.h -> exec->helper-gen.h.inc -> tcg/tcg.h

Quoted 19 lines
target/arm/helper.c +12 -0
+#include "target/arm/syndrome.h"
 #include "qemu/plugin.h"
 
 static void switch_mode(CPUARMState *env, int mode);
@@ -9455,6 +9457,66 @@ 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,
+                     const struct arm_emul_ops *ops)
+{
+    int ret;
+    IssDataAbort iss = { 0 };
+    iss.raw = syndrome.iss;
+
+    if (!(syndrome.ec == data_abort_lower || syndrome.ec == data_abort)) {

target/arm/helper.c static void arm_cpu_do_interrupt_aarch32(CPUState *cs) · line 9480

Quoted 10 lines
target/arm/helper.c +10 -0
+        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);

in this patch we have a lot of variable declerations outside of blocks/ fn top.

Quoted 172 lines
target/arm/helper.c +39 -0
+    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 ? ops->get_reg(cpu, 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;
+        }
+        if (reg_index < 31) {
+            ops->set_reg(cpu, reg_index, val);
+        }
+    }
+
+    return 0;
+}
+
 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
 {
     /*
target/arm/helper.h +16 -0
index b1c26c180ea..503404112d4 100644
@@ -3,8 +3,24 @@
 #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"
+
+/*
+ * Callback ops for per-register access during MMIO emulation.
+ * Each hypervisor backend provides its own implementation to avoid
+ * syncing the full CPU state for a single-register MMIO access.
+ */
+struct arm_emul_ops {
+    uint64_t (*get_reg)(CPUState *cpu, int reg);
+    void (*set_reg)(CPUState *cpu, int reg, uint64_t val);
+};
+
+int arm_emulate_mmio(CPUState *cpu, EsrEl2 syndrome, uint64_t gpa,
+                     const struct arm_emul_ops *ops);
 
 #define HELPER_H "tcg/helper-defs.h"
 #include "exec/helper-proto.h.inc"
target/arm/hvf/hvf.c +15 -31
index b00a7e1a845..99a66a68812 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"
@@ -37,6 +38,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"
 
@@ -2338,12 +2340,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;
@@ -2389,29 +2394,22 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
         break;
     }
     case EC_DATAABORT: {
-        bool isv = FIELD_EX32(syndrome, DABORT_ISS, ISV);
-        bool iswrite = FIELD_EX32(syndrome, DABORT_ISS, WNR);
-        bool s1ptw = FIELD_EX32(syndrome, DABORT_ISS, S1PTW);
-        bool sse = FIELD_EX32(syndrome, DABORT_ISS, SSE);
-        uint32_t sas = FIELD_EX32(syndrome, DABORT_ISS, SAS);
-        uint32_t len = 1 << sas;
-        uint32_t srt = FIELD_EX32(syndrome, DABORT_ISS, SRT);
-        uint32_t cm = FIELD_EX32(syndrome, DABORT_ISS, CM);
-        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,
@@ -2438,28 +2436,25 @@ 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);
+

target/arm/hvf/hvf.c static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp) · line 2455

Quoted 10 lines
target/arm/hvf/hvf.c +10 -0
+        static const struct arm_emul_ops hvf_arm_emul_ops = {
+            .get_reg = hvf_get_reg,
+            .set_reg = hvf_set_reg,
+        };
+
+        ret = arm_emulate_mmio(cpu, esr_el2, ipa, &hvf_arm_emul_ops);
+        if (ret < 0) {
+            error_report("Failed to emulate MMIO, syndrome=0x%llx, gpa=0x%llx",
+                         esr_el2.raw, ipa);
+            return -1;

I think this introduces a subtle behaviour change for HVF. we return -1 here, and do not advance the PC. I think MMIO emulation can fail e.g. for probes (non x86 UEFI we see this with TPM afair).

Quoted 130 lines
target/arm/hvf/hvf.c +1 -0
         }
+
         advance_pc = true;
         break;
     }
target/arm/syndrome.h +47 -0
index 2a6fa903f27..8b4cc7f7b21 100644
@@ -106,6 +106,53 @@ 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
+#define ARM_EL_ISV_SHIFT 24
+#define ARM_EL_IL (1 << ARM_EL_IL_SHIFT)
+#define ARM_EL_ISV (1 << ARM_EL_ISV_SHIFT)
+
+/* In the Data Abort syndrome */
+#define ARM_EL_VNCR (1 << 13)
+
 static inline uint32_t syn_get_ec(uint32_t syn)
 {
     return FIELD_EX32(syn, SYNDROME, EC);
target/arm/whpx/whpx-all.c +18 -27
index 46f0b9524e0..93d89f79ffe 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"
@@ -352,34 +354,23 @@ static void whpx_set_gp_reg(CPUState *cpu, int rt, uint64_t val)
 
 static int whpx_handle_mmio(CPUState *cpu, WHV_MEMORY_ACCESS_CONTEXT *ctx)
 {
-    uint64_t syndrome = ctx->Syndrome;
-
-    bool isv = FIELD_EX32(syndrome, DABORT_ISS, ISV);
-    bool iswrite = FIELD_EX32(syndrome, DABORT_ISS, WNR);
-    bool sse = FIELD_EX32(syndrome, DABORT_ISS, SSE);
-    uint32_t sas = FIELD_EX32(syndrome, DABORT_ISS, SAS);
-    uint32_t len = 1 << sas;
-    uint32_t srt = FIELD_EX32(syndrome, DABORT_ISS, SRT);
-    uint32_t cm = FIELD_EX32(syndrome, DABORT_ISS, CM);
-    uint64_t val = 0;
-
-    assert(syn_get_ec(syndrome) == EC_DATAABORT);
+    int ret = 0;
+    EsrEl2 esr = { .raw = ctx->Syndrome };
+    uint32_t cm = (esr.iss >> 8) & 0x1;
+
+    assert(syn_get_ec(esr.raw) == EC_DATAABORT);
     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);
+
+    static const struct arm_emul_ops whpx_arm_emul_ops = {
+        .get_reg = whpx_get_gp_reg,
+        .set_reg = whpx_set_gp_reg,
+    };
+
+    ret = arm_emulate_mmio(cpu, esr, ctx->Gpa, &whpx_arm_emul_ops);
+    if (ret < 0) {
+        error_report("WHPX: Failed to handle MMIO, syndrome=0x%llx, gpa=0x%llx",
+                     esr.raw, ctx->Gpa);
+        return -1;
     }
 
     return 0;
Signature · 2 lines
-- 
2.45.4
#45 lore raw

PATCH v6 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     | 107 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
include/hw/hyperv/hvgdk_mini.h +44 -0
index 33c9e4f3e0..53dc58813e 100644
@@ -896,6 +896,50 @@ struct hv_x64_memory_intercept_message {
     uint8_t instruction_bytes[16];
 } QEMU_PACKED;
 
+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 +107 -0

121 lines not on this page · raw message

Signature · 1 line
2.45.4
#46 lore raw

Re: [PATCH v6 11/14] target/arm/mshv: add vCPU run loop

Quoted 23 lines · 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     | 107 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
include/hw/hyperv/hvgdk_mini.h +1 -0
index 33c9e4f3e0..53dc58813e 100644
@@ -896,6 +896,50 @@ struct hv_x64_memory_intercept_message {
     uint8_t instruction_bytes[16];
 } QEMU_PACKED;
 
+union hv_arm64_vp_execution_state {

include/hw/hyperv/hvgdk_mini.h struct hv_x64_memory_intercept_message { · line 909

Quoted 10 lines
include/hw/hyperv/hvgdk_mini.h +10 -0
+    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;
+    };
+};

This is an mshv specific file, but I would still recommend against bitfields-in-a-union as generally bad style.

thanks
-- PMM

#47 lore raw

Re: [PATCH v6 11/14] target/arm/mshv: add vCPU run loop

Quoted 24 lines · Peter Maydell
Quoted 22 lines · 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     | 107 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
include/hw/hyperv/hvgdk_mini.h +0 -0
index 33c9e4f3e0..53dc58813e 100644
@@ -896,6 +896,50 @@ struct hv_x64_memory_intercept_message {
     uint8_t instruction_bytes[16];
 } QEMU_PACKED;
 
Quoted 14 lines

include/hw/hyperv/hvgdk_mini.h struct hv_x64_memory_intercept_message { · line 909

Quoted 11 lines
include/hw/hyperv/hvgdk_mini.h +11 -0
+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;
+    };
+};

This is an mshv specific file, but I would still recommend against bitfields-in-a-union as generally bad style.

Hi Peter,

We source these definitions pretty much verbatim from the hypervisor headers. Having them as close as possible to the hypervisor headers would it make it easier to maintain. We do make some cosmetic changes w.r.t naming style and QEMU_PACKED instead of __packed__ etc but that's about it.

Alsoe, there are existing definitions in this file using the same style.

Thanks,
Anirudh.

#48 lore raw

Re: [PATCH v6 11/14] target/arm/mshv: add vCPU run loop

In reply to Aastha Rawat
Quoted 161 lines · 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     | 107 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
include/hw/hyperv/hvgdk_mini.h +44 -0
index 33c9e4f3e0..53dc58813e 100644
@@ -896,6 +896,50 @@ struct hv_x64_memory_intercept_message {
     uint8_t instruction_bytes[16];
 } QEMU_PACKED;
 
+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 +78 -0
index d835614fd5..786d815312 100644
@@ -20,6 +20,7 @@
 #include "target/arm/cpu.h"
 #include "target/arm/internals.h"
 #include "target/arm/mshv_arm.h"
+#include "target/arm/helper.h"
 
 #include "system/mshv.h"
 #include "system/mshv_int.h"
@@ -158,8 +159,114 @@ int mshv_arch_store_vcpu_state(const CPUState *cpu)
     return store_regs(cpu);
 }
 
+static int set_memory_info(const struct hyperv_message *msg,
+                           struct hv_arm64_memory_intercept_message *info)
+{
+    if (msg->header.message_type != HVMSG_GPA_INTERCEPT
+            && msg->header.message_type != HVMSG_UNMAPPED_GPA
+            && msg->header.message_type != HVMSG_UNACCEPTED_GPA) {
+        error_report("invalid message type");
+        return -1;
+    }
+    memcpy(info, msg->payload, sizeof(*info));
+
+    return 0;
+}
+
+static uint64_t mshv_mmio_get_reg(CPUState *cpu, int reg_index)
+{
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+    return reg_index < 31 ? env->xregs[reg_index] : 0ULL;
+}
+
+static void mshv_mmio_set_reg(CPUState *cpu, int reg_index, uint64_t val)
+{
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+    if (reg_index < 31) {
+        env->xregs[reg_index] = val;
+    }
+}
+
+static int handle_unmapped_mem(int vm_fd, CPUState *cpu,
+                               const struct hyperv_message *msg,
+                               MshvVmExit *exit_reason)
+{
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
+    CPUARMState *env = &arm_cpu->env;
+    struct hv_arm64_memory_intercept_message info = { 0 };
+    int ret;
+    EsrEl2 syndrome;
+
+    ret = set_memory_info(msg, &info);
+    if (ret < 0) {
+        error_report("failed to convert message to memory info");
+        return -1;
+    }
+
+    syndrome.raw = info.syndrome;
+
+    ret = load_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to load registers");
+        return -1;
+    }
+
+    static const struct arm_emul_ops mshv_arm_emul_ops = {
+        .get_reg = mshv_mmio_get_reg,
+        .set_reg = mshv_mmio_set_reg,
+    };
+
+    ret = arm_emulate_mmio(cpu, syndrome, info.guest_physical_address,
+                           &mshv_arm_emul_ops);
+    if (ret < 0) {
+        error_report("Failed to emulate with syndrome");
+        return -1;
+    }
+
+    /* Advance PC past the faulting instruction */
+    env->pc += (syndrome.il == 1) ? 4 : 2;
+
+    ret = store_regs(cpu);
+    if (ret < 0) {
+        error_report("Failed to store registers");
+        return -1;
+    }
+
+    *exit_reason = MshvVmExitIgnore;
+

target/arm/mshv/mshv-all.c int mshv_arch_store_vcpu_state(const CPUState *cpu) · line 248

Quoted 10 lines
target/arm/mshv/mshv-all.c +8 -0
+    return 0;
+}
+
 int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 {
+    int ret;
+    int cpu_fd = mshv_vcpufd(cpu);
+
+    ret = ioctl(cpu_fd, MSHV_RUN_VP, msg);
+    if (ret < 0) {

the ioctl can return EINTR after a userland kick, for x86 on master we handle this with a special exit:

    if (ret < 0) {
        if (errno == EINTR) {
            *exit = MshvVmExitSpecial;
            return 0;
        }

target/arm/mshv/mshv-all.c int mshv_arch_store_vcpu_state(const CPUState *cpu) · line 258

Quoted 10 lines
target/arm/mshv/mshv-all.c +10 -0
+        *exit = MshvVmExitShutdown;
+        return ret;
+    }
+
+    switch (msg->header.message_type) {
+    case HVMSG_UNRECOVERABLE_EXCEPTION:
+        *exit = MshvVmExitShutdown;
+        break;
+    case HVMSG_GPA_INTERCEPT:
+    case HVMSG_UNMAPPED_GPA:

the whpx code handles a cache maintainance exit, do we need the same here?

	advance_pc = true;

	if (vcpu->exit_ctx.MemoryAccess.Syndrome & BIT(8)) {
		error_report("WHPX: cached access to unmapped memory"
		"Pc = 0x%llx Gva = 0x%llx Gpa = 0x%llx",
		vcpu->exit_ctx.MemoryAccess.Header.Pc,
		vcpu->exit_ctx.MemoryAccess.Gpa,
		vcpu->exit_ctx.MemoryAccess.Gva);
		break;
	}
Quoted 17 lines
target/arm/mshv/mshv-all.c +11 -0
+        ret = handle_unmapped_mem(vm_fd, cpu, msg, exit);
+        if (ret < 0) {
+            error_report("failed to handle mmio");
+            return -1;
+        }
+        break;
+    default:
+        error_report("Unhandled message type: 0x%x", msg->header.message_type);
+        return -1;
+    }
+
     return 0;
 }
 
Signature · 2 lines
-- 
2.45.4
#49 lore raw

PATCH v6 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 | 24 ++++++++++++++++++++++++
 include/system/mshv.h          |  2 ++
 3 files changed, 28 insertions(+)
accel/mshv/irq.c +2 -0
index 4828ac51ac..f18e6a0f66 100644
@@ -259,6 +259,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)
@@ -295,6 +296,7 @@ int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint3
     }
     return 0;
 }
+#endif
 
 void mshv_irqchip_commit_routes(MshvState *s)
 {
include/hw/hyperv/hvgdk_mini.h +24 -0
index 53dc58813e..d81c9816e7 100644
@@ -543,14 +543,38 @@ typedef struct hv_input_set_vp_registers {
 } QEMU_PACKED hv_input_set_vp_registers;
 
 #define MSHV_VP_MAX_REGISTERS   128
+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 114f065db5..80b6712af9 100644
@@ -60,10 +60,12 @@ extern MshvState *mshv_state;
 /* clock (partition reference time) */
 void mshv_arch_clock_init(void);
 
+#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(AccelRouteChange *c, int vector, PCIDevice *dev);
 int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev);
Signature · 1 line
2.45.4
#50 lore raw

Re: [PATCH v6 12/14] include/hw/hyperv: adjust hv_interrupt_control structure for arm64

Quoted 18 lines · 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 | 24 ++++++++++++++++++++++++
 include/system/mshv.h          |  2 ++
 3 files changed, 28 insertions(+)
accel/mshv/irq.c +0 -0
index 4828ac51ac..f18e6a0f66 100644

accel/mshv/irq.c int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev) · line 262

Quoted 4 lines
accel/mshv/irq.c +1 -0
@@ -259,6 +259,7 @@ int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev)
     return 0;
 }
 
+#if defined(__x86_64__)

if it's arch-specific then we probably want to move this to target/i386/mshv

Quoted 16 lines
accel/mshv/irq.c +1 -0
 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)
@@ -295,6 +296,7 @@ int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type, uint3
     }
     return 0;
 }
+#endif
 
 void mshv_irqchip_commit_routes(MshvState *s)
 {
include/hw/hyperv/hvgdk_mini.h +0 -0
index 53dc58813e..d81c9816e7 100644

include/hw/hyperv/hvgdk_mini.h typedef struct hv_input_set_vp_registers { · line 546

Quoted 4 lines
include/hw/hyperv/hvgdk_mini.h +1 -0
@@ -543,14 +543,38 @@ typedef struct hv_input_set_vp_registers {
 } QEMU_PACKED hv_input_set_vp_registers;
 
 #define MSHV_VP_MAX_REGISTERS   128
+enum hv_interrupt_type {

maybe we don't need those defines, since the values are prefixed with the arch name?

Quoted 54 lines
include/hw/hyperv/hvgdk_mini.h +23 -0
+#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 114f065db5..80b6712af9 100644
@@ -60,10 +60,12 @@ extern MshvState *mshv_state;
 /* clock (partition reference time) */
 void mshv_arch_clock_init(void);
 
+#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(AccelRouteChange *c, int vector, PCIDevice *dev);
 int mshv_irqchip_update_msi_route(int virq, MSIMessage msg, PCIDevice *dev);
Signature · 2 lines
-- 
2.45.4
#51 lore raw

PATCH v6 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                |   2 +
 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         |  61 +++++++++++++
 target/i386/mshv/mshv-all.c        |   5 +
 11 files changed, 273 insertions(+), 2 deletions(-)
accel/mshv/mshv-all.c +5 -0
index 9603f0360e1..e1264952873 100644
@@ -279,6 +279,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 +6 -2
index ee03a85f482..67a1f19190b 100644
@@ -2706,7 +2706,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 (hvf_enabled()) {
         if (!hvf_irqchip_in_kernel()) {
@@ -2760,7 +2760,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 if (hvf_enabled() && hvf_irqchip_in_kernel()) {
             vms->msi_controller = VIRT_MSI_CTRL_GICV2M;
@@ -2790,6 +2790,10 @@ static void finalize_msi_controller(VirtMachineState *vms)
             error_report("ITS not supported on WHPX.");
             exit(1);
         }
+        if (mshv_enabled()) {
+            error_report("ITS not supported on MSHV.");
+            exit(1);
+        }
         if (hvf_enabled() && hvf_irqchip_in_kernel()) {
             error_report("ITS not supported on HVF when using the hardware vGIC.");
             exit(1);
hw/intc/arm_gicv3_common.c +3 -0
index f7ba74e6d52..fb34b6a942d 100644
@@ -34,6 +34,7 @@
 #include "system/kvm.h"
 #include "system/whpx.h"
 #include "system/hvf.h"
+#include "system/mshv.h"
 
 
 static void gicv3_gicd_no_migration_shift_bug_post_load(GICv3State *cs)
@@ -662,6 +663,8 @@ const char *gicv3_class_name(void)
         return TYPE_WHPX_GICV3;
     } else if (hvf_enabled() && hvf_irqchip_in_kernel()) {
         return TYPE_HVF_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 +2 -0
index 0620b0792a1..e0047ab1c0f 100644
@@ -49,6 +49,8 @@ specific_ss.add(when: ['CONFIG_WHPX', 'CONFIG_ARM_GICV3'], if_true: files('arm_g
 specific_ss.add(when: ['CONFIG_HVF', 'CONFIG_ARM_GICV3'], if_true: files('arm_gicv3_hvf.c'))
 stub_ss.add(files('arm_gicv3_hvf_stub.c'))
 specific_ss.add(when: 'CONFIG_ARM_GIC_KVM', if_true: files('arm_gicv3_kvm.c', 'arm_gicv3_its_kvm.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 d81c9816e7f..2b4b1406ab0 100644
@@ -85,6 +85,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 9c2f3cf5aee..4d78fa06777 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 03ab3e8f2ff..2ed456e654a 100644
@@ -316,6 +316,7 @@ DECLARE_OBJ_CHECKERS(GICv3State, ARMGICv3CommonClass,
 /* Types for GICv3 kernel-irqchip */
 #define TYPE_WHPX_GICV3 "whpx-arm-gicv3"
 #define TYPE_HVF_GICV3 "hvf-arm-gicv3"
+#define TYPE_MSHV_GICV3 "mshv-arm-gicv3"
 
 struct ARMGICv3CommonClass {
     /*< private >*/
include/system/mshv_int.h +1 -0
index 31d2f4381c7..2dcfd11d8df 100644
@@ -107,6 +107,7 @@ void mshv_arch_amend_proc_features(
 void mshv_arch_disable_partition_proc_features(
      union hv_partition_processor_features *disabled_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);
 int mshv_arch_migration_init(MshvState *s);
target/arm/mshv/mshv-all.c +61 -0

67 lines not on this page · raw message

target/i386/mshv/mshv-all.c +5 -0
index 968fe2a4494..85dbacf98ff 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
#52 lore raw

Re: [PATCH v6 13/14] hw/intc,target/arm/mshv: add MSHV vGICv3 implementation

hw/intc/arm_gicv3_mshv.c

Quoted 104 lines · 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                |   2 +
 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         |  61 +++++++++++++
 target/i386/mshv/mshv-all.c        |   5 +
 11 files changed, 273 insertions(+), 2 deletions(-)
accel/mshv/mshv-all.c +5 -0
index 9603f0360e1..e1264952873 100644
@@ -279,6 +279,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 +6 -2
index ee03a85f482..67a1f19190b 100644
@@ -2706,7 +2706,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 (hvf_enabled()) {
         if (!hvf_irqchip_in_kernel()) {
@@ -2760,7 +2760,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 if (hvf_enabled() && hvf_irqchip_in_kernel()) {
             vms->msi_controller = VIRT_MSI_CTRL_GICV2M;
@@ -2790,6 +2790,10 @@ static void finalize_msi_controller(VirtMachineState *vms)
             error_report("ITS not supported on WHPX.");
             exit(1);
         }
+        if (mshv_enabled()) {
+            error_report("ITS not supported on MSHV.");
+            exit(1);
+        }
         if (hvf_enabled() && hvf_irqchip_in_kernel()) {
             error_report("ITS not supported on HVF when using the hardware vGIC.");
             exit(1);
hw/intc/arm_gicv3_common.c +3 -0
index f7ba74e6d52..fb34b6a942d 100644
@@ -34,6 +34,7 @@
 #include "system/kvm.h"
 #include "system/whpx.h"
 #include "system/hvf.h"
+#include "system/mshv.h"
 
 
 static void gicv3_gicd_no_migration_shift_bug_post_load(GICv3State *cs)
@@ -662,6 +663,8 @@ const char *gicv3_class_name(void)
         return TYPE_WHPX_GICV3;
     } else if (hvf_enabled() && hvf_irqchip_in_kernel()) {
         return TYPE_HVF_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 +0 -0
new file mode 100644
index 00000000000..a87819a5ba3

this is a new file that we should add to MAINTAINERS

Quoted 57 lines
hw/intc/arm_gicv3_mshv.c new file +56 -0
@@ -0,0 +1,181 @@
+/*
+ * ARM Generic Interrupt Controller using MSHV in-kernel support
+ *
+ * Copyright Microsoft, Corp. 2026
+ * Based on vGICv3 KVM code by Pavel Fedin
+ *
+ * Authors:
+ *      Aastha Rawat <[email protected]>
+ *      Anirudh Rayabharam (Microsoft) <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/error-report.h"
+#include "hw/intc/arm_gicv3_common.h"
+#include "migration/blocker.h"
+#include "target/arm/cpregs.h"
+#include "hw/hyperv/hvgdk_mini.h"
+#include "system/mshv.h"
+#include "system/mshv_int.h"
+
+struct MSHVARMGICv3Class {
+    ARMGICv3CommonClass parent_class;
+    DeviceRealize parent_realize;
+    ResettablePhases parent_phases;
+};
+
+OBJECT_DECLARE_TYPE(GICv3State, MSHVARMGICv3Class, MSHV_GICV3)
+
+static void mshv_gicv3_get(GICv3State *s)
+{
+}
+
+static void mshv_gicv3_put(GICv3State *s)
+{
+}
+
+static void mshv_gicv3_reset_hold(Object *obj, ResetType type)
+{
+    GICv3State *s = ARM_GICV3_COMMON(obj);
+    MSHVARMGICv3Class *mgc = MSHV_GICV3_GET_CLASS(s);
+
+    if (mgc->parent_phases.hold) {
+        mgc->parent_phases.hold(obj, type);
+    }
+
+    mshv_gicv3_put(s);
+}
+
+static void mshv_gicv3_set_irq(void *opaque, int irq, int level)
+{
+    int ret;
+    GICv3State *s = (GICv3State *)opaque;
+    int vm_fd = mshv_state->vm;

hw/intc/arm_gicv3_mshv.c line 66

Quoted 10 lines
hw/intc/arm_gicv3_mshv.c new file +10 -0
+    struct hv_input_assert_virtual_interrupt arg = {0};
+    struct mshv_root_hvcall args = {0};
+    union hv_interrupt_control control = {
+        .interrupt_type = HV_ARM64_INTERRUPT_TYPE_FIXED,
+        .rsvd1 = 0,
+        .asserted = level,
+        .rsvd2 = 0
+    };
+
+    if (irq >= s->num_irq) {

I think we also need to consider the SGI + PPI range here:

if (irq >= s->num_irq - GIC_INTERNAL)

Quoted 72 lines
hw/intc/arm_gicv3_mshv.c new file +72 -0
+        return;
+    }
+
+    arg.control = control;
+    arg.vector = GIC_INTERNAL + irq;
+
+    args.code   = HVCALL_ASSERT_VIRTUAL_INTERRUPT;
+    args.in_sz  = sizeof(arg);
+    args.in_ptr = (uint64_t)&arg;
+
+    ret = mshv_hvcall(vm_fd, &args);
+    if (ret < 0) {
+        error_report("Failed to set GICv3 IRQ %d to level %d", irq, level);
+    }
+}
+
+static void mshv_gicv3_realize(DeviceState *dev, Error **errp)
+{
+    ERRP_GUARD();
+    GICv3State *s = MSHV_GICV3(dev);
+    MSHVARMGICv3Class *mgc = MSHV_GICV3_GET_CLASS(s);
+    int i, ret;
+
+    mgc->parent_realize(dev, errp);
+    if (*errp) {
+        return;
+    }
+
+    if (s->revision != 3) {
+        error_setg(errp, "unsupported GIC revision %d for platform GIC",
+                   s->revision);
+        return;
+    }
+
+    if (s->security_extn) {
+        error_setg(errp, "the platform vGICv3 does not implement the "
+                   "security extensions");
+        return;
+    }
+
+    if (s->nmi_support) {
+        error_setg(errp, "NMI is not supported with the platform GIC");
+        return;
+    }
+
+    if (s->nb_redist_regions > 1) {
+        error_setg(errp, "Multiple VGICv3 redistributor regions are not "
+                   "supported by MSHV");
+        error_append_hint(errp, "A maximum of %d VCPUs can be used",
+                          s->redist_region_count[0]);
+        return;
+    }
+
+    gicv3_init_irqs_and_mmio(s, mshv_gicv3_set_irq, NULL);
+
+    for (i = 0; i < s->num_cpu; i++) {
+        CPUState *cpu_state = qemu_get_cpu(i);
+
+        hv_register_assoc gicr_base = {
+            .name = HV_ARM64_REGISTER_GICR_BASE_GPA,
+            .value = {
+                .reg64 = 0x080A0000 + (GICV3_REDIST_SIZE * i)
+            }
+        };
+
+        ret = mshv_set_generic_regs(cpu_state, &gicr_base, 1);
+        if (ret < 0) {
+            error_setg(errp, "Failed to set GICR base for CPU %d", i);
+            return;
+        }
+    }
+

hw/intc/arm_gicv3_mshv.c line 148

Quoted 10 lines
hw/intc/arm_gicv3_mshv.c new file +10 -0
+    if (s->maint_irq) {
+        error_setg(errp,
+               "Nested virtualisation not currently supported by MSHV");
+        return;
+    }
+
+    error_setg(&s->migration_blocker,
+        "Live migration disabled because GIC state save/restore not supported on MSHV");
+    if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
+        error_report_err(*errp);

err_report_err() will consume/free errp. AFAIU the call site owns the error, and we can just return here. otherwise we will probably have a double free of the error somewhere up in the callchain.

Quoted 38 lines
hw/intc/arm_gicv3_mshv.c new file +33 -0
+    }
+}
+
+static void mshv_gicv3_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
+    MSHVARMGICv3Class *mgc = MSHV_GICV3_CLASS(klass);
+
+    agcc->pre_save = mshv_gicv3_get;
+    agcc->post_load = mshv_gicv3_put;
+
+    device_class_set_parent_realize(dc, mshv_gicv3_realize,
+                                    &mgc->parent_realize);
+    resettable_class_set_parent_phases(rc, NULL, mshv_gicv3_reset_hold, NULL,
+                                       &mgc->parent_phases);
+}
+
+static const TypeInfo mshv_arm_gicv3_info = {
+    .name = TYPE_MSHV_GICV3,
+    .parent = TYPE_ARM_GICV3_COMMON,
+    .instance_size = sizeof(GICv3State),
+    .class_init = mshv_gicv3_class_init,
+    .class_size = sizeof(MSHVARMGICv3Class),
+};
+
+static void mshv_gicv3_register_types(void)
+{
+    type_register_static(&mshv_arm_gicv3_info);
+}
+
+type_init(mshv_gicv3_register_types)
hw/intc/meson.build +0 -0
index 0620b0792a1..e0047ab1c0f 100644

hw/intc/meson.build specific_ss.add(when: ['CONFIG_WHPX', 'CONFIG_ARM_GICV3'], if_true: files('arm_g · line 53

Quoted 5 lines
hw/intc/meson.build +2 -0
@@ -49,6 +49,8 @@ specific_ss.add(when: ['CONFIG_WHPX', 'CONFIG_ARM_GICV3'], if_true: files('arm_g
 specific_ss.add(when: ['CONFIG_HVF', 'CONFIG_ARM_GICV3'], if_true: files('arm_gicv3_hvf.c'))
 stub_ss.add(files('arm_gicv3_hvf_stub.c'))
 specific_ss.add(when: 'CONFIG_ARM_GIC_KVM', if_true: files('arm_gicv3_kvm.c', 'arm_gicv3_its_kvm.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'))

Do we need 'CONFIG_ARM_GIC' here (like KVM)?

Quoted 148 lines
hw/intc/meson.build +0 -0
 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 d81c9816e7f..2b4b1406ab0 100644
@@ -85,6 +85,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 9c2f3cf5aee..4d78fa06777 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 03ab3e8f2ff..2ed456e654a 100644
@@ -316,6 +316,7 @@ DECLARE_OBJ_CHECKERS(GICv3State, ARMGICv3CommonClass,
 /* Types for GICv3 kernel-irqchip */
 #define TYPE_WHPX_GICV3 "whpx-arm-gicv3"
 #define TYPE_HVF_GICV3 "hvf-arm-gicv3"
+#define TYPE_MSHV_GICV3 "mshv-arm-gicv3"
 
 struct ARMGICv3CommonClass {
     /*< private >*/
include/system/mshv_int.h +1 -0
index 31d2f4381c7..2dcfd11d8df 100644
@@ -107,6 +107,7 @@ void mshv_arch_amend_proc_features(
 void mshv_arch_disable_partition_proc_features(
      union hv_partition_processor_features *disabled_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);
 int mshv_arch_migration_init(MshvState *s);
target/arm/mshv/mshv-all.c +61 -0
index 786d815312b..8fa497c9a89 100644
@@ -288,6 +288,67 @@ void mshv_arch_destroy_vcpu(CPUState *cpu)
     state->hvcall_args = (MshvHvCallArgs){0};
 }
 
+static int set_partition_prop(int vm_fd, uint32_t prop_code,
+                                uint64_t prop_value)
+{
+    int ret;
+    struct hv_input_set_partition_property in = {0};
+    in.property_code = prop_code;
+    in.property_value = prop_value;
+
+    struct mshv_root_hvcall args = {0};
+    args.code = HVCALL_SET_PARTITION_PROPERTY;
+    args.in_sz = sizeof(in);
+    args.in_ptr = (uint64_t)&in;
+
+    ret = mshv_hvcall(vm_fd, &args);
+    if (ret < 0) {
+        error_report("Failed to set partition property code %u", prop_code);
+        return -1;
+    }
+
+    return 0;
+}
+
+int mshv_arch_pre_init_vm(int vm_fd)
+{
+    int ret;
+    VirtMachineState *vms = VIRT_MACHINE(qdev_get_machine());
+
+    ret = set_partition_prop(vm_fd,
+                            HV_PARTITION_PROPERTY_GICD_BASE_ADDRESS,
+                            vms->memmap[VIRT_GIC_DIST].base);
+    if (ret < 0) {
+        return ret;
+    }
+
+    ret = set_partition_prop(vm_fd,
+                        HV_PARTITION_PROPERTY_GITS_TRANSLATER_BASE_ADDRESS,
+                        vms->memmap[VIRT_GIC_ITS].base);
+    if (ret < 0) {
+        return ret;
+    }
+
+    ret = set_partition_prop(vm_fd,
+                        HV_PARTITION_PROPERTY_GIC_LPI_INT_ID_BITS,
+                        0);
+    if (ret < 0) {
+        return ret;
+    }
+
+    ret = set_partition_prop(vm_fd,
+                        HV_PARTITION_PROPERTY_GIC_PPI_OVERFLOW_INTERRUPT_FROM_CNTV,
+                        ARCH_TIMER_VIRT_IRQ);
+    if (ret < 0) {
+        return ret;
+    }
+
+    ret = set_partition_prop(vm_fd,
+                        HV_PARTITION_PROPERTY_GIC_PPI_PERFORMANCE_MONITORS_INTERRUPT,
+                        VIRTUAL_PMU_IRQ);
+
+    return ret;
+}
 
 static uint32_t mshv_arm_get_ipa_bit_size(int mshv_fd)
 {
target/i386/mshv/mshv-all.c +5 -0
index 968fe2a4494..85dbacf98ff 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 · 2 lines
-- 
2.45.4
#53 lore raw

PATCH v6 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 1312bbd1bd..84a911ac26 100644
@@ -624,6 +624,14 @@ S: Supported
 F: target/i386/mshv/
 F: hw/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: Roman Bolshakov <[email protected]>
 R: Phil Dennis-Jordan <[email protected]>
Signature · 1 line
2.45.4
#54 lore raw

Re: [PATCH v6 14/14] MAINTAINERS: updates for MSHV arm64 code

Quoted 33 lines · 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 1312bbd1bd..84a911ac26 100644
@@ -624,6 +624,14 @@ S: Supported
 F: target/i386/mshv/
 F: hw/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: Roman Bolshakov <[email protected]>
 R: Phil Dennis-Jordan <[email protected]>
Signature · 2 lines
-- 
2.45.4

It might makes sense to add this earlier in the series and extend it as we go ahead. this should address the checkpatch warnings in the series.