Skip to contentSkip to conversation

target/arm: Needn't validate the target in scratch host

PATCH In review Gavin Shan 2 participants 4 messages Apr 7, 2021 – Apr 12, 2021

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH 1/2 target/arm: Needn't validate the target in scratch host

@init->target is always -1 and preferred target is retrieved from host when @init isn't NULL in kvm_arm_create_scratch_host_vcpu(). So we can have the assumption that preferred target retrived from host is tried prior to the specified target list.

---
 target/arm/kvm.c   | 15 ++++-----------
 target/arm/kvm64.c | 12 +++++-------
 2 files changed, 9 insertions(+), 18 deletions(-)
target/arm/kvm.c +4 -11
index d8381ba224..3141d5d313 100644
@@ -69,6 +69,7 @@ bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
                                       int *fdarray,
                                       struct kvm_vcpu_init *init)
 {
+    struct kvm_vcpu_init try;
     int ret = 0, kvmfd = -1, vmfd = -1, cpufd = -1;
 
     kvmfd = qemu_open_old("/dev/kvm", O_RDWR);
@@ -89,15 +90,9 @@ bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
         goto finish;
     }
 
-    if (init->target == -1) {
-        struct kvm_vcpu_init preferred;
-
-        ret = ioctl(vmfd, KVM_ARM_PREFERRED_TARGET, &preferred);
-        if (!ret) {
-            init->target = preferred.target;
-        }
-    }
-    if (ret >= 0) {
+    ret = ioctl(vmfd, KVM_ARM_PREFERRED_TARGET, &try);
+    if (!ret) {
+        init->target = try.target;
         ret = ioctl(cpufd, KVM_ARM_VCPU_INIT, init);
         if (ret < 0) {
             goto err;
@@ -108,8 +103,6 @@ bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try,
          * creating one kind of guest CPU which is its preferred
          * CPU type.
          */
-        struct kvm_vcpu_init try;
-
         while (*cpus_to_try != QEMU_KVM_ARM_TARGET_NONE) {
             try.target = *cpus_to_try++;
             memcpy(try.features, init->features, sizeof(init->features));
target/arm/kvm64.c +5 -7
index 581335e49d..79800f00a7 100644
@@ -515,11 +515,9 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
         KVM_ARM_TARGET_CORTEX_A57,
         QEMU_KVM_ARM_TARGET_NONE
     };
-    /*
-     * target = -1 informs kvm_arm_create_scratch_host_vcpu()
-     * to use the preferred target
-     */
-    struct kvm_vcpu_init init = { .target = -1, };
+    struct kvm_vcpu_init init = {
+        .features[0] = 0;
+    };
 
     if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
         return false;
@@ -742,8 +740,7 @@ void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map)
      */
     if (!probed) {
         struct kvm_vcpu_init init = {
-            .target = -1,
-            .features[0] = (1 << KVM_ARM_VCPU_SVE),
+            .features[0] = 0,
         };
         struct kvm_one_reg reg = {
             .id = KVM_REG_ARM64_SVE_VLS,
@@ -753,6 +750,7 @@ void kvm_arm_sve_get_vls(CPUState *cs, unsigned long *map)
 
         probed = true;
 
+        init.features[0] = (1 << KVM_ARM_VCPU_SVE);
         if (!kvm_arm_create_scratch_host_vcpu(NULL, fdarray, &init)) {
             error_report("failed to create scratch VCPU with SVE enabled");
             abort();
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 2/2 target/arm: Initlaize PMU feature for scratch vcpu

If the scratch vCPU is initialized without PMU feature, we receive error on reading PMCR_EL0 as it's invisible in this case. It leads to host probing failure.

This fixes the issue by initializing the scratch vcpu with the PMU feature enabled and reading PMCR_EL0 from host. Otherwise, its value is set according to the detected target.

Fixes f7fb73b8cdd3 ("target/arm: Make number of counters in PMCR follow the CPU")
---
 target/arm/kvm64.c | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)
target/arm/kvm64.c +27 -5
index 79800f00a7..7311e86d1d 100644
@@ -516,11 +516,14 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
         QEMU_KVM_ARM_TARGET_NONE
     };
     struct kvm_vcpu_init init = {
-        .features[0] = 0;
+        .features[0] = (1 << KVM_ARM_VCPU_PMU_V3),
     };
 
-    if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
-        return false;
+    if (!kvm_arm_create_scratch_host_vcpu(NULL, fdarray, &init)) {
+        init.features[0] = 0;
+        if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
+            return false;
+        }
     }
 
     ahcf->target = init.target;
@@ -564,8 +567,27 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
                               ARM64_SYS_REG(3, 0, 0, 7, 1));
         err |= read_sys_reg64(fdarray[2], &ahcf->isar.id_aa64mmfr2,
                               ARM64_SYS_REG(3, 0, 0, 7, 2));
-        err |= read_sys_reg64(fdarray[2], &ahcf->isar.reset_pmcr_el0,
-                              ARM64_SYS_REG(3, 3, 9, 12, 0));
+
+        /*
+         * Read PMCR_EL0 from host if PMU feature has been enabled
+         * successfully. Otherwise, the value is set according to
+         * the detected target.
+         */
+        if (init.features[0] & (1 << KVM_ARM_VCPU_PMU_V3)) {
+            err |= read_sys_reg64(fdarray[2], &ahcf->isar.reset_pmcr_el0,
+                                  ARM64_SYS_REG(3, 3, 9, 12, 0));
+        } else {
+            switch (init.target) {
+            case KVM_ARM_TARGET_CORTEX_A53:
+                ahcf->isar.reset_pmcr_el0 = 0x41033000;
+                break;
+            case KVM_ARM_TARGET_CORTEX_A57:
+                ahcf->isar.reset_pmcr_el0 = 0x41013000;
+                break;
+            default:
+                ahcf->isar.reset_pmcr_el0 = 0x41023000;
+            }
+        }
 
         /*
          * Note that if AArch32 support is not present in the host,
Signature · 1 line
2.23.0
#2 lore raw

Re: [PATCH 2/2] target/arm: Initlaize PMU feature for scratch vcpu

Quoted 10 lines · Gavin Shan

If the scratch vCPU is initialized without PMU feature, we receive error on reading PMCR_EL0 as it's invisible in this case. It leads to host probing failure.

This fixes the issue by initializing the scratch vcpu with the PMU feature enabled and reading PMCR_EL0 from host. Otherwise, its value is set according to the detected target.

Fixes f7fb73b8cdd3 ("target/arm: Make number of counters in PMCR follow the CPU")

This commit has been reverted...

I couldn't find a cover letter for these patches, so it's hard to tell what you're aiming to do with them. Could you make sure you always send a cover letter with a multiple-patch series, please ? This also helps with our automated tooling.

thanks
-- PMM

author
#3 lore raw

Re: [PATCH 2/2] target/arm: Initlaize PMU feature for scratch vcpu

Hi Peter,

Quoted 10 lines · Peter Maydell
Quoted 9 lines · Gavin Shan

If the scratch vCPU is initialized without PMU feature, we receive error on reading PMCR_EL0 as it's invisible in this case. It leads to host probing failure.

This fixes the issue by initializing the scratch vcpu with the PMU feature enabled and reading PMCR_EL0 from host. Otherwise, its value is set according to the detected target.

Quoted 9 lines
Quoted 1 line
Fixes f7fb73b8cdd3 ("target/arm: Make number of counters in PMCR follow the CPU")

This commit has been reverted...

I couldn't find a cover letter for these patches, so it's hard to tell what you're aiming to do with them. Could you make sure you always send a cover letter with a multiple-patch series, please ? This also helps with our automated tooling.

Sorry for the delay. Yep, I will always include cover letter for a series. For this particular series, it's invalid since f7fb73b8cdd3 has been reverted. So please ignore this series.

Thanks,
Gavin