Skip to contentSkip to conversation

hvf: arm: fixes for QEMU 11.2

PATCH In review Mohamed Mediouni 3 participants 18 messages Oct 27 – Nov 16

lore
Share & tools
lore
#0 lore raw

v2 cover hvf: arm: fixes for QEMU 11.2

v2:
- remove GIC_ISPENDR workaround: currently under further investigation
- simplify SME save/restore patch
- rework the CPU reset strategy, with also taking care of the GIC CPU interface registers.

v1:

- workaround for presumed Hypervisor.framework issue
- save/restore SME state only when SME is on for the CPU
- add ID_AA64PFR0_EL1.GIC sync to advertise the system register interface

Mohamed Mediouni (6):
hvf: arm: save/restore SME state only when SME on
hvf: arm: remove broken ID_AA64PFR0_EL1.GIC sync
hvf: arm: advertise ID_AA64PFR0_EL1.GIC
hvf: arm: fetch more features
hvf: gic: sync the CPU interface during a vCPU reset
hvf: arm: sync vCPU state on PSCI reset

 hw/intc/arm_gicv3_hvf.c | 93 ++++++++++++++++++++++++-----------------
 target/arm/hvf/hvf.c    | 45 +++++++++++++++++---
 2 files changed, 93 insertions(+), 45 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH v2 1/6 hvf: arm: save/restore SME state only when SME on

When SME is disabled as part of the CPU configuration, do not restore SME state.

---
 target/arm/hvf/hvf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +2 -2
index b00a7e1a84..6085ac2284 100644
@@ -1394,7 +1394,7 @@ int hvf_arch_init_vcpu(CPUState *cpu)
     int i;
 
     if (__builtin_available(macOS 15.2, *)) {
-        if (hvf_arm_sme2_supported()) {
+        if (cpu_isar_feature(aa64_sme, arm_cpu)) {
             sregs_match_len += ARRAY_SIZE(hvf_sreg_list_sme2) + 1;
         }
 
@@ -1444,7 +1444,7 @@ int hvf_arch_init_vcpu(CPUState *cpu)
         }
     }
     if (__builtin_available(macOS 15.2, *)) {
-        if (hvf_arm_sme2_supported()) {
+        if (cpu_isar_feature(aa64_sme, arm_cpu)) {
             for (i = 0; i < ARRAY_SIZE(hvf_sreg_list_sme2); i++) {
                 hv_sys_reg_t hvf_id = hvf_sreg_list_sme2[i].sreg;
                 uint64_t kvm_id = HVF_TO_KVMID(hvf_id);
Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw

PATCH v2 2/6 hvf: arm: remove broken ID_AA64PFR0_EL1.GIC sync

In reply to Mohamed Mediouni

The GIC is only realised after this executes
in arm_gicv3_common_realize.

---
 target/arm/hvf/hvf.c | 1 -
 1 file changed, 1 deletion(-)
target/arm/hvf/hvf.c +0 -1
index 6085ac2284..5213842d2d 100644
@@ -1480,7 +1480,6 @@ int hvf_arch_init_vcpu(CPUState *cpu)
     assert_hvf_ok(ret);
 
     pfr = GET_IDREG(&arm_cpu->isar, ID_AA64PFR0);
-    pfr |= env->gicv3state ? (1 << 24) : 0;
     ret = hv_vcpu_set_sys_reg(cpu->accel->fd, HV_SYS_REG_ID_AA64PFR0_EL1, pfr);
     assert_hvf_ok(ret);
 
Signature · 1 line
2.54.0 (Apple Git-157)
#4 lore raw

PATCH v2 3/6 hvf: arm: advertise ID_AA64PFR0_EL1.GIC

In reply to Mohamed Mediouni

For the kernel-irqchip=on case, advertise ID_AA64PFR0_EL1.GIC.

---
 target/arm/hvf/hvf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/arm/hvf/hvf.c +12 -0
index 5213842d2d..acbc257091 100644
@@ -1230,6 +1230,18 @@ static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
         FIELD_DP64_IDREG(&host_isar, ID_AA64PFR1, SME, 0);
     }
 
+    /*
+     * On HVF, kernel-irqchip implies GICv3.
+     * The kernel-irqchip=off,gic-version=3 legacy case also supports
+     * the host CPU interface but advertising it in ID_AA64PFR0_EL1
+     * is not implemented yet.
+     */
+
+    if (hvf_irqchip_in_kernel()) {
+        /* Advertise the GIC system register interface */
+        FIELD_DP64_IDREG(&host_isar, ID_AA64PFR0, GIC, 1);
+    }
+
     ahcf->isar = host_isar;
 
     /*
Signature · 1 line
2.54.0 (Apple Git-157)
#5 lore raw

PATCH v2 4/6 hvf: arm: fetch more features

In reply to Mohamed Mediouni

macOS 27.0 has some more feature registers available. Fetch them (but not using the values yet).

Also fetch DCZID_EL0 at the same time.

---
 target/arm/hvf/hvf.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
target/arm/hvf/hvf.c +17 -3
index acbc257091..e1ddfe14c7 100644
@@ -1142,16 +1142,14 @@ static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
     } regs[] = {
         { HV_FEATURE_REG_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_IDX },
         { HV_FEATURE_REG_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_IDX },
-        /* Add ID_AA64PFR2_EL1 here when HVF supports it */
         { HV_FEATURE_REG_ID_AA64DFR0_EL1, ID_AA64DFR0_EL1_IDX },
         { HV_FEATURE_REG_ID_AA64DFR1_EL1, ID_AA64DFR1_EL1_IDX },
         { HV_FEATURE_REG_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_IDX },
         { HV_FEATURE_REG_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_IDX },
-        /* Add ID_AA64ISAR2_EL1 here when HVF supports it */
         { HV_FEATURE_REG_ID_AA64MMFR0_EL1, ID_AA64MMFR0_EL1_IDX },
         { HV_FEATURE_REG_ID_AA64MMFR1_EL1, ID_AA64MMFR1_EL1_IDX },
         { HV_FEATURE_REG_ID_AA64MMFR2_EL1, ID_AA64MMFR2_EL1_IDX },
-        /* Add ID_AA64MMFR3_EL1 here when HVF supports it */
+        { HV_FEATURE_REG_DCZID_EL0, DCZID_EL0_IDX},
     };
     hv_return_t r = HV_SUCCESS;
     hv_vcpu_config_t config = hv_vcpu_config_create();
@@ -1191,6 +1189,22 @@ static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
         }
     }
 
+    if (__builtin_available(macOS 27.0, *)) {
+        static const struct isar_regs {
+            hv_feature_reg_t reg;
+            ARMIDRegisterIdx index;
+        } new_regs[] = {
+        { HV_FEATURE_REG_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_IDX },
+        { HV_FEATURE_REG_ID_AA64PFR2_EL1, ID_AA64PFR2_EL1_IDX },
+        { HV_FEATURE_REG_ID_AA64MMFR3_EL1, ID_AA64MMFR3_EL1_IDX },
+        { HV_FEATURE_REG_ID_AA64MMFR4_EL1, ID_AA64MMFR4_EL1_IDX },
+        };
+        for (i = 0; i < ARRAY_SIZE(new_regs); i++) {
+            r |= hv_vcpu_config_get_feature_reg(config, new_regs[i].reg,
+                                            &host_isar.idregs[new_regs[i].index]);
+        }
+    }
+
     os_release(config);
 
     /*
Signature · 1 line
2.54.0 (Apple Git-157)
#6 lore raw

Re: [PATCH v2 4/6] hvf: arm: fetch more features

Quoted 7 lines · Mohamed Mediouni

macOS 27.0 has some more feature registers available. Fetch them (but not using the values yet).

Also fetch DCZID_EL0 at the same time.

This patch failed on the macos CI jobs, so I've dropped it from target-arm:

https://gitlab.com/qemu-project/qemu/-/jobs/17671032296#L4708
https://gitlab.com/qemu-project/qemu/-/jobs/17671063089#L4702

Quoted 15 lines

../target/arm/hvf/hvf.c:1197:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64ISAR2_EL1'
1197 | { HV_FEATURE_REG_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_IDX },

      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1198:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64PFR2_EL1'
 1198 |         { HV_FEATURE_REG_ID_AA64PFR2_EL1, ID_AA64PFR2_EL1_IDX },
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1199:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64MMFR3_EL1'
 1199 |         { HV_FEATURE_REG_ID_AA64MMFR3_EL1, ID_AA64MMFR3_EL1_IDX },
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1200:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64MMFR4_EL1'
 1200 |         { HV_FEATURE_REG_ID_AA64MMFR4_EL1, ID_AA64MMFR4_EL1_IDX },
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1202:25: error: invalid application of 'sizeof' to an incomplete type 'const struct isar_regs[]'
 1202 |         for (i = 0; i < ARRAY_SIZE(new_regs); i++) {
      |                         ^~~~~~~~~~~~~~~~~~~~
Quoted 4 lines

/Users/gitlab/builds/qemu-project/qemu/include/qemu/osdep.h:541:31: note: expanded from macro 'ARRAY_SIZE'
541 | #define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \
| ^~~
5 errors generated.

thanks
-- PMM

#7 lore raw

Re: [PATCH v2 4/6] hvf: arm: fetch more features

Quoted 10 lines · Peter Maydell
Quoted 5 lines · Mohamed Mediouni

macOS 27.0 has some more feature registers available. Fetch them (but not using the values yet).

Also fetch DCZID_EL0 at the same time.

Quoted 8 lines
Quoted 2 lines

This patch failed on the macos CI jobs, so I've dropped it from target-arm:

https://gitlab.com/qemu-project/qemu/-/jobs/17671032296#L4708
https://gitlab.com/qemu-project/qemu/-/jobs/17671063089#L4702

Hi,

Thank you, sounds like an SDK versioning issue again…

Will look more at how to decouple the SDK situation (or we’ll have to have #ifdef everywhere for everything new)...

The latest SDK provided by the CLTs can back-deploy to older macOS versions all the way back to macOS 11. But that still supposes having the newer SDK available in the first place.

From the CI logs, looks like they are at macOS 15.0 SDK which is pretty old by now...

Quoted 23 lines
Quoted 16 lines

../target/arm/hvf/hvf.c:1197:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64ISAR2_EL1'
1197 | { HV_FEATURE_REG_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_IDX },

     |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1198:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64PFR2_EL1'
1198 |         { HV_FEATURE_REG_ID_AA64PFR2_EL1, ID_AA64PFR2_EL1_IDX },
     |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1199:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64MMFR3_EL1'
1199 |         { HV_FEATURE_REG_ID_AA64MMFR3_EL1, ID_AA64MMFR3_EL1_IDX },
     |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1200:11: error: use of undeclared identifier 'HV_FEATURE_REG_ID_AA64MMFR4_EL1'
1200 |         { HV_FEATURE_REG_ID_AA64MMFR4_EL1, ID_AA64MMFR4_EL1_IDX },
     |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/arm/hvf/hvf.c:1202:25: error: invalid application of 'sizeof' to an incomplete type 'const struct isar_regs[]'
1202 |         for (i = 0; i < ARRAY_SIZE(new_regs); i++) {
     |                         ^~~~~~~~~~~~~~~~~~~~
Quoted 4 lines

/Users/gitlab/builds/qemu-project/qemu/include/qemu/osdep.h:541:31: note: expanded from macro 'ARRAY_SIZE'
541 | #define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \
| ^~~
5 errors generated.

thanks
-- PMM

#8 lore raw

PATCH v2 5/6 hvf: gic: sync the CPU interface during a vCPU reset

In reply to Mohamed Mediouni

When a vCPU reset happens, we should also
reset the state of the GIC CPU interface.

Otherwise, reboots might not work as expected
during some circumstances.

---
 hw/intc/arm_gicv3_hvf.c | 93 ++++++++++++++++++++++++-----------------
 1 file changed, 54 insertions(+), 39 deletions(-)
hw/intc/arm_gicv3_hvf.c +54 -39
index 4876446189..ad6e09b4c9 100644
@@ -257,50 +257,14 @@ static void hvf_gicv3_put_cpu_el2(CPUState *cpu_state, run_on_cpu_data arg)
     }
 }
 
-static void hvf_gicv3_put_cpu(CPUState *cpu_state, run_on_cpu_data arg)
+static void hvf_gicv3_put_cpuif(CPUState *cpu_state, run_on_cpu_data arg)
 {
-    uint32_t reg;
     uint64_t reg64;
-    int i, num_pri_bits;
-
+    int num_pri_bits;
     /* Redistributor state */
     GICv3CPUState *c = arg.host_ptr;
     hv_vcpu_t vcpu = c->cpu->accel->fd;
 
-    reg = c->gicr_waker;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_IGROUPR0, reg);
-
-    reg = c->gicr_igroupr0;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_IGROUPR0, reg);
-
-    reg = ~0;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICENABLER0, reg);
-    reg = c->gicr_ienabler0;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ISENABLER0, reg);
-
-    /* Restore config before pending so we treat level/edge correctly */
-    reg = half_shuffle32(c->edge_trigger >> 16) << 1;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICFGR1, reg);
-
-    reg = ~0;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICPENDR0, reg);
-    reg = c->gicr_ipendr0;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ISPENDR0, reg);
-
-    reg = ~0;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICACTIVER0, reg);
-    reg = c->gicr_iactiver0;
-    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ISACTIVER0, reg);
-
-    for (i = 0; i < GIC_INTERNAL; i += 4) {
-        reg = c->gicr_ipriorityr[i] |
-            (c->gicr_ipriorityr[i + 1] << 8) |
-            (c->gicr_ipriorityr[i + 2] << 16) |
-            (c->gicr_ipriorityr[i + 3] << 24);
-        hv_gic_set_redistributor_reg(vcpu,
-            HV_GIC_REDISTRIBUTOR_REG_GICR_IPRIORITYR0 + i, reg);
-    }
-
     /* CPU interface state */
     hv_gic_set_icc_reg(vcpu, HV_GIC_ICC_REG_SRE_EL1, c->icc_sre_el1);
 
@@ -320,7 +284,7 @@ static void hvf_gicv3_put_cpu(CPUState *cpu_state, run_on_cpu_data arg)
 
     switch (num_pri_bits) {
     case 7:
-           reg64 = c->icc_apr[GICV3_G0][3];
+        reg64 = c->icc_apr[GICV3_G0][3];
         hv_gic_set_icc_reg(vcpu, HV_GIC_ICC_REG_AP0R0_EL1 + 3, reg64);
         reg64 = c->icc_apr[GICV3_G0][2];
         hv_gic_set_icc_reg(vcpu, HV_GIC_ICC_REG_AP0R0_EL1 + 2, reg64);
@@ -349,6 +313,53 @@ static void hvf_gicv3_put_cpu(CPUState *cpu_state, run_on_cpu_data arg)
         reg64 = c->icc_apr[GICV3_G1NS][0];
         hv_gic_set_icc_reg(vcpu, HV_GIC_ICC_REG_AP1R0_EL1, reg64);
     }
+}
+
+static void hvf_gicv3_put_cpu(CPUState *cpu_state, run_on_cpu_data arg)
+{
+    uint32_t reg;
+    int i;
+
+    /* Redistributor state */
+    GICv3CPUState *c = arg.host_ptr;
+    hv_vcpu_t vcpu = c->cpu->accel->fd;
+
+    reg = c->gicr_waker;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_IGROUPR0, reg);
+
+    reg = c->gicr_igroupr0;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_IGROUPR0, reg);
+
+    reg = ~0;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICENABLER0, reg);
+    reg = c->gicr_ienabler0;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ISENABLER0, reg);
+
+    /* Restore config before pending so we treat level/edge correctly */
+    reg = half_shuffle32(c->edge_trigger >> 16) << 1;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICFGR1, reg);
+
+    reg = ~0;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICPENDR0, reg);
+    reg = c->gicr_ipendr0;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ISPENDR0, reg);
+
+    reg = ~0;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ICACTIVER0, reg);
+    reg = c->gicr_iactiver0;
+    hv_gic_set_redistributor_reg(vcpu, HV_GIC_REDISTRIBUTOR_REG_GICR_ISACTIVER0, reg);
+
+    for (i = 0; i < GIC_INTERNAL; i += 4) {
+        reg = c->gicr_ipriorityr[i] |
+            (c->gicr_ipriorityr[i + 1] << 8) |
+            (c->gicr_ipriorityr[i + 2] << 16) |
+            (c->gicr_ipriorityr[i + 3] << 24);
+        hv_gic_set_redistributor_reg(vcpu,
+            HV_GIC_REDISTRIBUTOR_REG_GICR_IPRIORITYR0 + i, reg);
+    }
+
+    /* CPU interface state */
+    hvf_gicv3_put_cpuif(cpu_state, arg);
 
     /* Registers beyond this point are with nested virt only */
     if (c->gic->maint_irq) {
@@ -629,6 +640,7 @@ static void hvf_gicv3_set_irq(void *opaque, int irq, int level)
 static void hvf_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
 {
     GICv3CPUState *c;
+    run_on_cpu_data data;
 
     c = env->gicv3state;
     c->icc_pmr_el1 = 0;
@@ -649,6 +661,9 @@ static void hvf_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
     c->icc_sre_el1 = 0x7;
     memset(c->icc_apr, 0, sizeof(c->icc_apr));
     memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
+
+    data.host_ptr = env->gicv3state;
+    run_on_cpu(env_cpu(env), hvf_gicv3_put_cpu, data);
 }
 
 static void hvf_gicv3_reset_hold(Object *obj, ResetType type)
Signature · 1 line
2.54.0 (Apple Git-157)
#9 lore raw

Re: [PATCH v2 5/6] hvf: gic: sync the CPU interface during a vCPU reset

Quoted 18 lines · Mohamed Mediouni

When a vCPU reset happens, we should also
reset the state of the GIC CPU interface.

Otherwise, reboots might not work as expected
during some circumstances.

hw/intc/arm_gicv3_hvf.c +1 -0
@@ -629,6 +640,7 @@ static void hvf_gicv3_set_irq(void *opaque, int irq, int level)
 static void hvf_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
 {
     GICv3CPUState *c;
+    run_on_cpu_data data;
 
     c = env->gicv3state;
     c->icc_pmr_el1 = 0;

hw/intc/arm_gicv3_hvf.c static void hvf_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri) · line 666

Quoted 6 lines
hw/intc/arm_gicv3_hvf.c +3 -0
@@ -649,6 +661,9 @@ static void hvf_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
     c->icc_sre_el1 = 0x7;
     memset(c->icc_apr, 0, sizeof(c->icc_apr));
     memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
+
+    data.host_ptr = env->gicv3state;
+    run_on_cpu(env_cpu(env), hvf_gicv3_put_cpu, data);

This isn't safe to call here. run_on_cpu() has an extremely nasty beartrap built in, which is that it will unlock the BQL while it is waiting for the queued work to complete. The code that calls these register reset functions doesn't expect to have the BQL dropped underneath it, and there might be various race conditions that happen if it does.

I think the existing use of run_on_cpu() in hvf_gicv3_put() is also broken for a similar reason.

Put another way, run_on_cpu() is extremely hard to use safely; I think you can only really use it if you're calling it directly from code that was itself called with the BQL initially not held, and which is therefore dealing with the BQL directly and can be careful not to assume that state that it looked at before the run_on_cpu() is still valid after run_on_cpu() returns; and if you do use it like that you probably want clear commentary warning about the potential problems.

thanks
-- PMM

#10 lore raw

PATCH v2 6/6 hvf: arm: sync vCPU state on PSCI reset

In reply to Mohamed Mediouni

In case of PSCI off followed by a PSCI on which is a scenario that happens notably during kexec, what happens today is that the state sync does not happen and the vCPU becomes in an unexpected state.

Address this via marking the vCPU as dirty during a PSCI power on.

---
 target/arm/hvf/hvf.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/arm/hvf/hvf.c +8 -0
index e1ddfe14c7..7ce2e38df1 100644
@@ -1651,6 +1651,14 @@ static bool hvf_handle_psci_call(CPUState *cpu, int *excp_ret)
         context_id = param[3];
         ret = arm_set_cpu_on(mpidr, entry, context_id,
                              hvf_psci_get_target_el(), target_aarch64);
+
+        /*
+         * Mark state as dirty for HVF to sync context
+         * when the vCPU runs. arm_set_cpu_on() does not
+         * do it by itself.
+         */
+        arm_get_cpu_by_id(mpidr)->vcpu_dirty = true;
+
         break;
     case QEMU_PSCI_0_1_FN_CPU_OFF:
     case QEMU_PSCI_0_2_FN_CPU_OFF:
Signature · 1 line
2.54.0 (Apple Git-157)
#11 lore raw

Re: [PATCH v2 6/6] hvf: arm: sync vCPU state on PSCI reset

Quoted 33 lines · Mohamed Mediouni

In case of PSCI off followed by a PSCI on which is a scenario that happens notably during kexec, what happens today is that the state sync does not happen and the vCPU becomes in an unexpected state.

Address this via marking the vCPU as dirty during a PSCI power on.

---
 target/arm/hvf/hvf.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/arm/hvf/hvf.c +8 -1
index e1ddfe14c7..7ce2e38df1 100644
@@ -1651,6 +1651,14 @@ static bool hvf_handle_psci_call(CPUState *cpu, int *excp_ret)
         context_id = param[3];
         ret = arm_set_cpu_on(mpidr, entry, context_id,
                              hvf_psci_get_target_el(), target_aarch64);
+
+        /*
+         * Mark state as dirty for HVF to sync context
+         * when the vCPU runs. arm_set_cpu_on() does not
+         * do it by itself.
+         */
+        arm_get_cpu_by_id(mpidr)->vcpu_dirty = true;
+
         break;
     case QEMU_PSCI_0_1_FN_CPU_OFF:
     case QEMU_PSCI_0_2_FN_CPU_OFF:
--
Quoted 1 line

2.54.0 (Apple Git-157)

I do feel like maybe this isn't the best way to do this, but unfortunately I don't have a good enough grasp on all the state sync stuff to be sure.

I think that calling cpu_synchronize_post_init() or cpu_synchronize_post_reset() here would be basically equivalent, and maybe one of those is the right thing? Except looking at their implementation they use run_on_cpu() and at this point in this code we hold the BQL, so (as noted in my comment on patch 5) a bit dubious to call here.

arm_set_cpu_on_async_work() could safely call one of those sync functions (because it always runs on the right CPU and run_on_cpu definitely doesn't drop the BQL if it's already on the right CPU). And we do do a cpu_reset() in that function, so that does kind of argue for a post_reset sync to happen. But obviously that's code shared with KVM so we'd need to check through the consequences.

thanks
-- PMM

Tested-by on v2 6/6
#12 lore raw

Re: [PATCH v2 6/6] hvf: arm: sync vCPU state on PSCI reset

In reply to Mohamed Mediouni

Hi Mohamed,

I gave this a try with the CPU hotplug reproducer from my earlier
mail: the Ubuntu 24.04 arm64 cloud image on an M2 Pro (macOS 15.7), "-M virt -accel hvf -cpu host -smp 2", then as root:

echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online

On current master (e9fa1f6) the guest still panics on the second command ("kernel BUG at arch/arm64/kernel/smp.c:388"), 3 out of 3 runs. With this patch on top, CPU1 comes back online and the guest carries on, 3 out of 3 runs.

I can't judge Peter's question about which sync call is the right one, but if it helps:

I'm happy to test a v3 too.

Thanks,
Adrien

Quoted 34 lines · Mohamed Mediouni

In case of PSCI off followed by a PSCI on which is a scenario that happens notably during kexec, what happens today is that the state sync does not happen and the vCPU becomes in an unexpected state.

Address this via marking the vCPU as dirty during a PSCI power on.

---
 target/arm/hvf/hvf.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/arm/hvf/hvf.c +8 -0
index e1ddfe14c7..7ce2e38df1 100644
@@ -1651,6 +1651,14 @@ static bool hvf_handle_psci_call(CPUState *cpu, 
 int *excp_ret)
         context_id = param[3];
         ret = arm_set_cpu_on(mpidr, entry, context_id,
                              hvf_psci_get_target_el(), target_aarch64);
+
+        /*
+         * Mark state as dirty for HVF to sync context
+         * when the vCPU runs. arm_set_cpu_on() does not
+         * do it by itself.
+         */
+        arm_get_cpu_by_id(mpidr)->vcpu_dirty = true;
+
         break;
     case QEMU_PSCI_0_1_FN_CPU_OFF:
     case QEMU_PSCI_0_2_FN_CPU_OFF:
Signature · 1 line
2.54.0 (Apple Git-157)
#13 lore raw
In reply to Mohamed Mediouni
Quoted 11 lines · Mohamed Mediouni

v2:
- remove GIC_ISPENDR workaround: currently under further investigation
- simplify SME save/restore patch
- rework the CPU reset strategy, with also taking care of the GIC CPU interface registers.

v1:

- workaround for presumed Hypervisor.framework issue
- save/restore SME state only when SME is on for the CPU
- add ID_AA64PFR0_EL1.GIC sync to advertise the system register interface

I've applied patches 1-4 to target-arm.next; 5 and 6 I left comments on.

What's the status with the apple-gfx compile failures on macos 27? (issue #4486)

thanks
-- PMM

#14 lore raw
Quoted 13 lines · Peter Maydell
Quoted 8 lines · Mohamed Mediouni

v2:
- remove GIC_ISPENDR workaround: currently under further investigation
- simplify SME save/restore patch
- rework the CPU reset strategy, with also taking care of the GIC CPU interface registers.

v1:

Quoted 8 lines
Quoted 3 lines

- workaround for presumed Hypervisor.framework issue
- save/restore SME state only when SME is on for the CPU
- add ID_AA64PFR0_EL1.GIC sync to advertise the system register interface

I've applied patches 1-4 to target-arm.next; 5 and 6 I left comments on.

What's the status with the apple-gfx compile failures on macos 27? (issue #4486)

Hi,

I think we should just disable building apple-gfx by default for now.

It’s private API as of macOS 27, and as long as a decision on whether to keep that reverse-engineered device model isn’t made yet...

Quoted 3 lines

thanks
-- PMM

#15 lore raw
Quoted 23 lines · Mohamed Mediouni
Quoted 14 lines · Peter Maydell
Quoted 8 lines · Mohamed Mediouni

v2:
- remove GIC_ISPENDR workaround: currently under further investigation
- simplify SME save/restore patch
- rework the CPU reset strategy, with also taking care of the GIC CPU interface registers.

v1:

Quoted 8 lines
Quoted 3 lines

- workaround for presumed Hypervisor.framework issue
- save/restore SME state only when SME is on for the CPU
- add ID_AA64PFR0_EL1.GIC sync to advertise the system register interface

I've applied patches 1-4 to target-arm.next; 5 and 6 I left comments on.

What's the status with the apple-gfx compile failures on macos 27? (issue #4486)

Quoted 6 lines

Hi,

I think we should just disable building apple-gfx by default for now.

It’s private API as of macOS 27, and as long as a decision on whether to keep that reverse-engineered device model isn’t made yet...

That makes sense to me; we've had the vmapple machine disabled by default for a long time, and that I'm guessing that was the biggest reason to use apple-gfx.

Do you think we should deprecate the "vmapple" machine type, or do you hope to resurrect it still?

thanks
-- PMM

#16 lore raw
Quoted 35 lines · Peter Maydell
Quoted 23 lines · Mohamed Mediouni
Quoted 14 lines · Peter Maydell
Quoted 8 lines · Mohamed Mediouni

v2:
- remove GIC_ISPENDR workaround: currently under further investigation
- simplify SME save/restore patch
- rework the CPU reset strategy, with also taking care of the GIC CPU interface registers.

v1:

Quoted 8 lines
Quoted 3 lines

- workaround for presumed Hypervisor.framework issue
- save/restore SME state only when SME is on for the CPU
- add ID_AA64PFR0_EL1.GIC sync to advertise the system register interface

I've applied patches 1-4 to target-arm.next; 5 and 6 I left comments on.

What's the status with the apple-gfx compile failures on macos 27? (issue #4486)

Quoted 6 lines

Hi,

I think we should just disable building apple-gfx by default for now.

It’s private API as of macOS 27, and as long as a decision on whether to keep that reverse-engineered device model isn’t made yet...

Quoted 6 lines

That makes sense to me; we've had the vmapple machine disabled by default for a long time, and that I'm guessing that was the biggest reason to use apple-gfx.

Do you think we should deprecate the "vmapple" machine type, or do you hope to resurrect it still?

Hi,

I do hope to get it resurrected as an optional feature still, but the issue is that it’ll stay (by design) a reverse-engineered model.

I need to spin a new revision of the patches for that though.

It will break from time to time and cannot come with an expectation of short-term (as in, anything past 6 months) stability/build-ability.

Quoted 3 lines

thanks
-- PMM

#17 lore raw
Quoted 11 lines · Mohamed Mediouni
Quoted 2 lines · Peter Maydell

Do you think we should deprecate the "vmapple" machine type, or do you hope to resurrect it still?

I do hope to get it resurrected as an optional feature still, but the issue is that it’ll stay (by design) a reverse-engineered model.

I need to spin a new revision of the patches for that though.

It will break from time to time and cannot come with an expectation of short-term (as in, anything past 6 months) stability/build-ability.

Hmm. If it's going to break every six months or so then I'm not sure it's really a good fit for upstream QEMU. As we've seen, we haven't really in practice been able to get updates to it into the tree particularly quickly, and most downstreams will be operating on a slower cadence. If we had a larger group of people interested in cooperating on maintaining it upstream then that would help, but we don't really seem to have that.

thanks
-- PMM