Skip to contentSkip to conversation

hw/arm/virt: Move common vCPU properties in a function

PATCH In review Salil Mehta 2 participants 3 messages Nov 3, 2024 – Nov 5, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/arm/virt: Move common vCPU properties in a function

Refactor vCPU properties code from the machvirt_init() main loop with the following goals:

1. Enable code reuse in future patch sets.
2. Improve code readability.
3. Separate out the one-time initialization of (secure-)Tagged memory, handling potential failures early.

Note: This is a cosmetic change only; *no* functional changes are
intended here.

---
 hw/arm/virt.c         | 238 +++++++++++++++++++++++++-----------------
 include/hw/arm/virt.h |   4 +
 2 files changed, 147 insertions(+), 95 deletions(-)
hw/arm/virt.c +143 -95
index 1a381e9a2b..a0d3bef875 100644
@@ -2091,16 +2091,126 @@ static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)
     }
 }
 
+static void virt_cpu_set_properties(Object *cpuobj, Error **errp)
+{
+    MachineState *ms = MACHINE(qdev_get_machine());
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+    VirtMachineState *vms = VIRT_MACHINE(ms);
+    const CPUArchIdList *possible_cpus;
+    Error *local_err = NULL;
+    VirtMachineClass *vmc;
+
+    vmc = VIRT_MACHINE_GET_CLASS(ms);
+
+    possible_cpus = mc->possible_cpu_arch_ids(ms);
+    object_property_set_int(cpuobj, "mp-affinity",
+                            possible_cpus->cpus[CPU(cpuobj)->cpu_index].arch_id,
+                            NULL);
+
+    numa_cpu_pre_plug(&possible_cpus->cpus[CPU(cpuobj)->cpu_index],
+                      DEVICE(cpuobj), &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (!vms->secure) {
+        object_property_set_bool(cpuobj, "has_el3", false, NULL);
+    }
+
+    if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
+        object_property_set_bool(cpuobj, "has_el2", false, NULL);
+    }
+
+    if (vmc->kvm_no_adjvtime &&
+        object_property_find(cpuobj, "kvm-no-adjvtime")) {
+        object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL);
+    }
+
+    if (vmc->no_kvm_steal_time &&
+        object_property_find(cpuobj, "kvm-steal-time")) {
+        object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
+    }
+
+    if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
+        object_property_set_bool(cpuobj, "pmu", false, NULL);
+    }
+
+    if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
+        object_property_set_bool(cpuobj, "lpa2", false, NULL);
+    }
+
+    if (object_property_find(cpuobj, "reset-cbar")) {
+        object_property_set_int(cpuobj, "reset-cbar",
+                                vms->memmap[VIRT_CPUPERIPHS].base,
+                                &local_err);
+        if (local_err) {
+            goto out;
+        }
+    }
+
+    object_property_set_link(cpuobj, "memory", OBJECT(vms->sysmem), &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (vms->secure) {
+        object_property_set_link(cpuobj, "secure-memory",
+                                 OBJECT(vms->secure_sysmem), &local_err);
+        if (local_err) {
+            goto out;
+        }
+    }
+
+    if (vms->mte) {
+        if (tcg_enabled()) {
+            /*
+             * The property exists only if MemTag is supported.
+             * If it is, we must allocate the ram to back that up.
+             */
+            if (!object_property_find(cpuobj, "tag-memory")) {
+                error_report("MTE requested, but not supported "
+                             "by the guest CPU");
+                exit(1);
+            }
+
+            object_property_set_link(cpuobj, "tag-memory",
+                                     OBJECT(vms->tag_sysmem), &local_err);
+            if (local_err) {
+                goto out;
+            }
+
+            if (vms->secure) {
+                object_property_set_link(cpuobj, "secure-tag-memory",
+                                         OBJECT(vms->secure_tag_sysmem),
+                                         &local_err);
+                if (local_err) {
+                    goto out;
+                }
+            }
+        } else if (kvm_enabled()) {
+            kvm_arm_enable_mte(cpuobj, &local_err);
+            if (local_err) {
+                goto out;
+            }
+        }
+    }
+
+out:
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
 static void machvirt_init(MachineState *machine)
 {
     VirtMachineState *vms = VIRT_MACHINE(machine);
     VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
     MachineClass *mc = MACHINE_GET_CLASS(machine);
     const CPUArchIdList *possible_cpus;
-    MemoryRegion *sysmem = get_system_memory();
+    MemoryRegion *secure_tag_sysmem = NULL;
     MemoryRegion *secure_sysmem = NULL;
     MemoryRegion *tag_sysmem = NULL;
-    MemoryRegion *secure_tag_sysmem = NULL;
+    MemoryRegion *sysmem;
     int n, virt_max_cpus;
     bool firmware_loaded;
     bool aarch64 = true;
@@ -2140,6 +2250,8 @@ static void machvirt_init(MachineState *machine)
      */
     finalize_gic_version(vms);
 
+    sysmem = vms->sysmem = get_system_memory();
+
     if (vms->secure) {
         /*
          * The Secure view of the world is the same as the NonSecure,
@@ -2147,7 +2259,7 @@ static void machvirt_init(MachineState *machine)
          * containing the system memory at low priority; any secure-only
          * devices go in at higher priority and take precedence.
          */
-        secure_sysmem = g_new(MemoryRegion, 1);
+        secure_sysmem = vms->secure_sysmem = g_new(MemoryRegion, 1);
         memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
                            UINT64_MAX);
         memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
@@ -2220,6 +2332,33 @@ static void machvirt_init(MachineState *machine)
         exit(1);
     }
 
+    if (vms->mte && !kvm_enabled() && !tcg_enabled()) {
+        error_report("MTE requested, but not supported ");
+        exit(1);
+    }
+
+    if (vms->mte && kvm_enabled() && !kvm_arm_mte_supported()) {
+        error_report("MTE requested, but not supported by KVM");
+        exit(1);
+    }
+
+    if (vms->mte && tcg_enabled()) {
+        /* Create the memory region only once, but link to all cpus later */
+        tag_sysmem = vms->tag_sysmem = g_new(MemoryRegion, 1);
+        memory_region_init(tag_sysmem, OBJECT(machine),
+                           "tag-memory", UINT64_MAX / 32);
+
+        if (vms->secure) {
+            secure_tag_sysmem = vms->secure_tag_sysmem = g_new(MemoryRegion, 1);
+            memory_region_init(secure_tag_sysmem, OBJECT(machine),
+                               "secure-tag-memory", UINT64_MAX / 32);
+
+            /* As with ram, secure-tag takes precedence over tag.  */
+            memory_region_add_subregion_overlap(secure_tag_sysmem, 0,
+                                                tag_sysmem, -1);
+        }
+    }
+
     create_fdt(vms);
 
     assert(possible_cpus->len == max_cpus);
@@ -2232,104 +2371,13 @@ static void machvirt_init(MachineState *machine)
         }
 
         cpuobj = object_new(possible_cpus->cpus[n].type);
-        object_property_set_int(cpuobj, "mp-affinity",
-                                possible_cpus->cpus[n].arch_id, NULL);
 
         cs = CPU(cpuobj);
         cs->cpu_index = n;
 
-        numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
-                          &error_fatal);
-
         aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
 
-        if (!vms->secure) {
-            object_property_set_bool(cpuobj, "has_el3", false, NULL);
-        }
-
-        if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
-            object_property_set_bool(cpuobj, "has_el2", false, NULL);
-        }
-
-        if (vmc->kvm_no_adjvtime &&
-            object_property_find(cpuobj, "kvm-no-adjvtime")) {
-            object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL);
-        }
-
-        if (vmc->no_kvm_steal_time &&
-            object_property_find(cpuobj, "kvm-steal-time")) {
-            object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
-        }
-
-        if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
-            object_property_set_bool(cpuobj, "pmu", false, NULL);
-        }
-
-        if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
-            object_property_set_bool(cpuobj, "lpa2", false, NULL);
-        }
-
-        if (object_property_find(cpuobj, "reset-cbar")) {
-            object_property_set_int(cpuobj, "reset-cbar",
-                                    vms->memmap[VIRT_CPUPERIPHS].base,
-                                    &error_abort);
-        }
-
-        object_property_set_link(cpuobj, "memory", OBJECT(sysmem),
-                                 &error_abort);
-        if (vms->secure) {
-            object_property_set_link(cpuobj, "secure-memory",
-                                     OBJECT(secure_sysmem), &error_abort);
-        }
-
-        if (vms->mte) {
-            if (tcg_enabled()) {
-                /* Create the memory region only once, but link to all cpus. */
-                if (!tag_sysmem) {
-                    /*
-                     * The property exists only if MemTag is supported.
-                     * If it is, we must allocate the ram to back that up.
-                     */
-                    if (!object_property_find(cpuobj, "tag-memory")) {
-                        error_report("MTE requested, but not supported "
-                                     "by the guest CPU");
-                        exit(1);
-                    }
-
-                    tag_sysmem = g_new(MemoryRegion, 1);
-                    memory_region_init(tag_sysmem, OBJECT(machine),
-                                       "tag-memory", UINT64_MAX / 32);
-
-                    if (vms->secure) {
-                        secure_tag_sysmem = g_new(MemoryRegion, 1);
-                        memory_region_init(secure_tag_sysmem, OBJECT(machine),
-                                           "secure-tag-memory",
-                                           UINT64_MAX / 32);
-
-                        /* As with ram, secure-tag takes precedence over tag. */
-                        memory_region_add_subregion_overlap(secure_tag_sysmem,
-                                                            0, tag_sysmem, -1);
-                    }
-                }
-
-                object_property_set_link(cpuobj, "tag-memory",
-                                         OBJECT(tag_sysmem), &error_abort);
-                if (vms->secure) {
-                    object_property_set_link(cpuobj, "secure-tag-memory",
-                                             OBJECT(secure_tag_sysmem),
-                                             &error_abort);
-                }
-            } else if (kvm_enabled()) {
-                if (!kvm_arm_mte_supported()) {
-                    error_report("MTE requested, but not supported by KVM");
-                    exit(1);
-                }
-                kvm_arm_enable_mte(cpuobj, &error_abort);
-            } else {
-                    error_report("MTE requested, but not supported ");
-                    exit(1);
-            }
-        }
+        virt_cpu_set_properties(cpuobj, &error_abort);
 
         qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
         object_unref(cpuobj);
include/hw/arm/virt.h +4 -0
index aca4f8061b..239f3678af 100644
@@ -143,6 +143,10 @@ struct VirtMachineState {
     DeviceState *platform_bus_dev;
     FWCfgState *fw_cfg;
     PFlashCFI01 *flash[2];
+    MemoryRegion *sysmem;
+    MemoryRegion *secure_sysmem;
+    MemoryRegion *tag_sysmem;
+    MemoryRegion *secure_tag_sysmem;
     bool secure;
     bool highmem;
     bool highmem_compact;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Salil Mehta

Refactor vCPU properties code from the machvirt_init() main loop with the following goals:

1. Enable code reuse in future patch sets.
2. Improve code readability.
3. Separate out the one-time initialization of (secure-)Tagged memory, handling potential failures early.

Quoted 9 lines

Note: This is a cosmetic change only; *no* functional changes are
intended here.

---
  hw/arm/virt.c         | 238 +++++++++++++++++++++++++-----------------
  include/hw/arm/virt.h |   4 +
  2 files changed, 147 insertions(+), 95 deletions(-)

With the following nitpicks addressed:

Quoted 10 lines
hw/arm/virt.c +2 -0
index 1a381e9a2b..a0d3bef875 100644
@@ -2091,16 +2091,126 @@ static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)
      }
  }
 
+static void virt_cpu_set_properties(Object *cpuobj, Error **errp)
+{

hw/arm/virt.c static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem) · line 2105

Quoted 10 lines
hw/arm/virt.c +10 -0
+    MachineState *ms = MACHINE(qdev_get_machine());
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+    VirtMachineState *vms = VIRT_MACHINE(ms);
+    const CPUArchIdList *possible_cpus;
+    Error *local_err = NULL;
+    VirtMachineClass *vmc;
+
+    vmc = VIRT_MACHINE_GET_CLASS(ms);
+
+    possible_cpus = mc->possible_cpu_arch_ids(ms);

virt_cpu_set_properties() is only called by machvirt_init() where MachineState is passed in as parameter. So MachineState can be passed down as a parameter instead of retriving it by qdev_get_machine(). Besides, the chunk of code can be further compact.

static void virt_cpu_set_properties(MachineState *ms, Object *cpuobj, Error **errp)

{
    VirtMachineState *vms = VIRT_MACHINE(ms);
    MachineClass *mc = MACHINE_GET_CLASS(ms);
    VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(ms);
    const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
    Error *local_err = NULL;
      :

}

Quoted 103 lines
hw/arm/virt.c +98 -0
+    object_property_set_int(cpuobj, "mp-affinity",
+                            possible_cpus->cpus[CPU(cpuobj)->cpu_index].arch_id,
+                            NULL);
+
+    numa_cpu_pre_plug(&possible_cpus->cpus[CPU(cpuobj)->cpu_index],
+                      DEVICE(cpuobj), &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (!vms->secure) {
+        object_property_set_bool(cpuobj, "has_el3", false, NULL);
+    }
+
+    if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
+        object_property_set_bool(cpuobj, "has_el2", false, NULL);
+    }
+
+    if (vmc->kvm_no_adjvtime &&
+        object_property_find(cpuobj, "kvm-no-adjvtime")) {
+        object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL);
+    }
+
+    if (vmc->no_kvm_steal_time &&
+        object_property_find(cpuobj, "kvm-steal-time")) {
+        object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
+    }
+
+    if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
+        object_property_set_bool(cpuobj, "pmu", false, NULL);
+    }
+
+    if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
+        object_property_set_bool(cpuobj, "lpa2", false, NULL);
+    }
+
+    if (object_property_find(cpuobj, "reset-cbar")) {
+        object_property_set_int(cpuobj, "reset-cbar",
+                                vms->memmap[VIRT_CPUPERIPHS].base,
+                                &local_err);
+        if (local_err) {
+            goto out;
+        }
+    }
+
+    object_property_set_link(cpuobj, "memory", OBJECT(vms->sysmem), &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (vms->secure) {
+        object_property_set_link(cpuobj, "secure-memory",
+                                 OBJECT(vms->secure_sysmem), &local_err);
+        if (local_err) {
+            goto out;
+        }
+    }
+
+    if (vms->mte) {
+        if (tcg_enabled()) {
+            /*
+             * The property exists only if MemTag is supported.
+             * If it is, we must allocate the ram to back that up.
+             */
+            if (!object_property_find(cpuobj, "tag-memory")) {
+                error_report("MTE requested, but not supported "
+                             "by the guest CPU");
+                exit(1);
+            }
+
+            object_property_set_link(cpuobj, "tag-memory",
+                                     OBJECT(vms->tag_sysmem), &local_err);
+            if (local_err) {
+                goto out;
+            }
+
+            if (vms->secure) {
+                object_property_set_link(cpuobj, "secure-tag-memory",
+                                         OBJECT(vms->secure_tag_sysmem),
+                                         &local_err);
+                if (local_err) {
+                    goto out;
+                }
+            }
+        } else if (kvm_enabled()) {
+            kvm_arm_enable_mte(cpuobj, &local_err);
+            if (local_err) {
+                goto out;
+            }
+        }
+    }
+
+out:
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
  static void machvirt_init(MachineState *machine)
  {
      VirtMachineState *vms = VIRT_MACHINE(machine);
      VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
      MachineClass *mc = MACHINE_GET_CLASS(machine);

hw/arm/virt.c static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem) · line 2216

Quoted 10 lines
hw/arm/virt.c +2 -2
      const CPUArchIdList *possible_cpus;
-    MemoryRegion *sysmem = get_system_memory();
+    MemoryRegion *secure_tag_sysmem = NULL;
      MemoryRegion *secure_sysmem = NULL;
      MemoryRegion *tag_sysmem = NULL;
-    MemoryRegion *secure_tag_sysmem = NULL;
+    MemoryRegion *sysmem;
      int n, virt_max_cpus;
      bool firmware_loaded;
      bool aarch64 = true;

Unnecessary change caused after the defination of @secure_tag_sysmem is moved around. Also, I personally prefer to keep the order to define @sysmem, @secure_sysmem, @tag_sysmem and @secure_tag_system. So this chunk of code would be something like below:

    MemoryRegion *sysmem;
    MemoryRegion *secure_sysmem = NULL;
    MemoryRegion *tag_sysmem = NULL;
    MemoryRegion *secure_tag_sysmem = NULL;
Quoted 163 lines
hw/arm/virt.c +31 -93
@@ -2140,6 +2250,8 @@ static void machvirt_init(MachineState *machine)
       */
      finalize_gic_version(vms);
 
+    sysmem = vms->sysmem = get_system_memory();
+
      if (vms->secure) {
          /*
           * The Secure view of the world is the same as the NonSecure,
@@ -2147,7 +2259,7 @@ static void machvirt_init(MachineState *machine)
           * containing the system memory at low priority; any secure-only
           * devices go in at higher priority and take precedence.
           */
-        secure_sysmem = g_new(MemoryRegion, 1);
+        secure_sysmem = vms->secure_sysmem = g_new(MemoryRegion, 1);
          memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
                             UINT64_MAX);
          memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
@@ -2220,6 +2332,33 @@ static void machvirt_init(MachineState *machine)
          exit(1);
      }
 
+    if (vms->mte && !kvm_enabled() && !tcg_enabled()) {
+        error_report("MTE requested, but not supported ");
+        exit(1);
+    }
+
+    if (vms->mte && kvm_enabled() && !kvm_arm_mte_supported()) {
+        error_report("MTE requested, but not supported by KVM");
+        exit(1);
+    }
+
+    if (vms->mte && tcg_enabled()) {
+        /* Create the memory region only once, but link to all cpus later */
+        tag_sysmem = vms->tag_sysmem = g_new(MemoryRegion, 1);
+        memory_region_init(tag_sysmem, OBJECT(machine),
+                           "tag-memory", UINT64_MAX / 32);
+
+        if (vms->secure) {
+            secure_tag_sysmem = vms->secure_tag_sysmem = g_new(MemoryRegion, 1);
+            memory_region_init(secure_tag_sysmem, OBJECT(machine),
+                               "secure-tag-memory", UINT64_MAX / 32);
+
+            /* As with ram, secure-tag takes precedence over tag.  */
+            memory_region_add_subregion_overlap(secure_tag_sysmem, 0,
+                                                tag_sysmem, -1);
+        }
+    }
+
      create_fdt(vms);
 
      assert(possible_cpus->len == max_cpus);
@@ -2232,104 +2371,13 @@ static void machvirt_init(MachineState *machine)
          }
 
          cpuobj = object_new(possible_cpus->cpus[n].type);
-        object_property_set_int(cpuobj, "mp-affinity",
-                                possible_cpus->cpus[n].arch_id, NULL);
 
          cs = CPU(cpuobj);
          cs->cpu_index = n;
 
-        numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
-                          &error_fatal);
-
          aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
 
-        if (!vms->secure) {
-            object_property_set_bool(cpuobj, "has_el3", false, NULL);
-        }
-
-        if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
-            object_property_set_bool(cpuobj, "has_el2", false, NULL);
-        }
-
-        if (vmc->kvm_no_adjvtime &&
-            object_property_find(cpuobj, "kvm-no-adjvtime")) {
-            object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL);
-        }
-
-        if (vmc->no_kvm_steal_time &&
-            object_property_find(cpuobj, "kvm-steal-time")) {
-            object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
-        }
-
-        if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
-            object_property_set_bool(cpuobj, "pmu", false, NULL);
-        }
-
-        if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
-            object_property_set_bool(cpuobj, "lpa2", false, NULL);
-        }
-
-        if (object_property_find(cpuobj, "reset-cbar")) {
-            object_property_set_int(cpuobj, "reset-cbar",
-                                    vms->memmap[VIRT_CPUPERIPHS].base,
-                                    &error_abort);
-        }
-
-        object_property_set_link(cpuobj, "memory", OBJECT(sysmem),
-                                 &error_abort);
-        if (vms->secure) {
-            object_property_set_link(cpuobj, "secure-memory",
-                                     OBJECT(secure_sysmem), &error_abort);
-        }
-
-        if (vms->mte) {
-            if (tcg_enabled()) {
-                /* Create the memory region only once, but link to all cpus. */
-                if (!tag_sysmem) {
-                    /*
-                     * The property exists only if MemTag is supported.
-                     * If it is, we must allocate the ram to back that up.
-                     */
-                    if (!object_property_find(cpuobj, "tag-memory")) {
-                        error_report("MTE requested, but not supported "
-                                     "by the guest CPU");
-                        exit(1);
-                    }
-
-                    tag_sysmem = g_new(MemoryRegion, 1);
-                    memory_region_init(tag_sysmem, OBJECT(machine),
-                                       "tag-memory", UINT64_MAX / 32);
-
-                    if (vms->secure) {
-                        secure_tag_sysmem = g_new(MemoryRegion, 1);
-                        memory_region_init(secure_tag_sysmem, OBJECT(machine),
-                                           "secure-tag-memory",
-                                           UINT64_MAX / 32);
-
-                        /* As with ram, secure-tag takes precedence over tag. */
-                        memory_region_add_subregion_overlap(secure_tag_sysmem,
-                                                            0, tag_sysmem, -1);
-                    }
-                }
-
-                object_property_set_link(cpuobj, "tag-memory",
-                                         OBJECT(tag_sysmem), &error_abort);
-                if (vms->secure) {
-                    object_property_set_link(cpuobj, "secure-tag-memory",
-                                             OBJECT(secure_tag_sysmem),
-                                             &error_abort);
-                }
-            } else if (kvm_enabled()) {
-                if (!kvm_arm_mte_supported()) {
-                    error_report("MTE requested, but not supported by KVM");
-                    exit(1);
-                }
-                kvm_arm_enable_mte(cpuobj, &error_abort);
-            } else {
-                    error_report("MTE requested, but not supported ");
-                    exit(1);
-            }
-        }
+        virt_cpu_set_properties(cpuobj, &error_abort);
 
          qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
          object_unref(cpuobj);
include/hw/arm/virt.h +0 -0
index aca4f8061b..239f3678af 100644

include/hw/arm/virt.h struct VirtMachineState { · line 152

Quoted 10 lines
include/hw/arm/virt.h +4 -0
@@ -143,6 +143,10 @@ struct VirtMachineState {
      DeviceState *platform_bus_dev;
      FWCfgState *fw_cfg;
      PFlashCFI01 *flash[2];
+    MemoryRegion *sysmem;
+    MemoryRegion *secure_sysmem;
+    MemoryRegion *tag_sysmem;
+    MemoryRegion *secure_tag_sysmem;
      bool secure;
      bool highmem;
      bool highmem_compact;

Thanks,
Gavin

#2 lore raw

Hi Gavin,

Thanks for the valuable comments. I will address them but it looks peter has already pulled ARM changes for this cycle?

Thanks
Salil.

Quoted 398 lines

From: Gavin Shan <[email protected]>
Sent: Tuesday, November 5, 2024 12:01 AM
To: Salil Mehta <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jean- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; zhukeqian <[email protected]>; wangxiongfeng (C) <[email protected]>; wangyanan (Y) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Linuxarm <[email protected]>; [email protected]
Subject: Re: [PATCH] hw/arm/virt: Move common vCPU properties in a function

Quoted 8 lines · Salil Mehta

Refactor vCPU properties code from the machvirt_init() main loop with the following goals:

1. Enable code reuse in future patch sets.
2. Improve code readability.
3. Separate out the one-time initialization of (secure-)Tagged memory, handling potential failures early.

Quoted 9 lines

Note: This is a cosmetic change only; *no* functional changes are
intended here.

---
  hw/arm/virt.c         | 238 +++++++++++++++++++++++++-----------------
  include/hw/arm/virt.h |   4 +
  2 files changed, 147 insertions(+), 95 deletions(-)

With the following nitpicks addressed:

hw/arm/virt.c static void

Quoted 5 lines
hw/arm/virt.c index renamed from hw/arm/virt.c +0 -0

1a381e9a2b..a0d3bef875 100644

hw/arm/virt.c +0 -0
@@ -2091,16 +2091,126 @@ static void

virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)

Quoted 4 lines
hw/arm/virt.c +1 -0
      }
  }
 
+static void virt_cpu_set_properties(Object *cpuobj, Error **errp) {

hw/arm/virt.c static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem) · line 2105

Quoted 10 lines
hw/arm/virt.c +10 -0
+    MachineState *ms = MACHINE(qdev_get_machine());
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+    VirtMachineState *vms = VIRT_MACHINE(ms);
+    const CPUArchIdList *possible_cpus;
+    Error *local_err = NULL;
+    VirtMachineClass *vmc;
+
+    vmc = VIRT_MACHINE_GET_CLASS(ms);
+
+    possible_cpus = mc->possible_cpu_arch_ids(ms);

virt_cpu_set_properties() is only called by machvirt_init() where MachineState is passed in as parameter. So MachineState can be passed down as a parameter instead of retriving it by qdev_get_machine(). Besides, the chunk of code can be further compact.

static void virt_cpu_set_properties(MachineState *ms, Object *cpuobj,

 Error **errp) {
      VirtMachineState *vms = VIRT_MACHINE(ms);
      MachineClass *mc = MACHINE_GET_CLASS(ms);
      VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(ms);
      const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
      Error *local_err = NULL;
        :

}

Quoted 37 lines
hw/arm/virt.c +36 -0
+    object_property_set_int(cpuobj, "mp-affinity",
+                            possible_cpus->cpus[CPU(cpuobj)->cpu_index].arch_id,
+                            NULL);
+
+    numa_cpu_pre_plug(&possible_cpus->cpus[CPU(cpuobj)-
 cpu_index],
+                      DEVICE(cpuobj), &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (!vms->secure) {
+        object_property_set_bool(cpuobj, "has_el3", false, NULL);
+    }
+
+    if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
+        object_property_set_bool(cpuobj, "has_el2", false, NULL);
+    }
+
+    if (vmc->kvm_no_adjvtime &&
+        object_property_find(cpuobj, "kvm-no-adjvtime")) {
+        object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL);
+    }
+
+    if (vmc->no_kvm_steal_time &&
+        object_property_find(cpuobj, "kvm-steal-time")) {
+        object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
+    }
+
+    if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
+        object_property_set_bool(cpuobj, "pmu", false, NULL);
+    }
+
+    if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
+        object_property_set_bool(cpuobj, "lpa2", false, NULL);
+    }
+

hw/arm/virt.c static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)

Quoted 10 lines
hw/arm/virt.c +10 -0
+    if (object_property_find(cpuobj, "reset-cbar")) {
+        object_property_set_int(cpuobj, "reset-cbar",
+                                vms->memmap[VIRT_CPUPERIPHS].base,
+                                &local_err);
+        if (local_err) {
+            goto out;
+        }
+    }
+
+    object_property_set_link(cpuobj, "memory", OBJECT(vms->sysmem),

&local_err);

Quoted 57 lines
hw/arm/virt.c +52 -0
+    if (local_err) {
+        goto out;
+    }
+
+    if (vms->secure) {
+        object_property_set_link(cpuobj, "secure-memory",
+                                 OBJECT(vms->secure_sysmem), &local_err);
+        if (local_err) {
+            goto out;
+        }
+    }
+
+    if (vms->mte) {
+        if (tcg_enabled()) {
+            /*
+             * The property exists only if MemTag is supported.
+             * If it is, we must allocate the ram to back that up.
+             */
+            if (!object_property_find(cpuobj, "tag-memory")) {
+                error_report("MTE requested, but not supported "
+                             "by the guest CPU");
+                exit(1);
+            }
+
+            object_property_set_link(cpuobj, "tag-memory",
+                                     OBJECT(vms->tag_sysmem), &local_err);
+            if (local_err) {
+                goto out;
+            }
+
+            if (vms->secure) {
+                object_property_set_link(cpuobj, "secure-tag-memory",
+                                         OBJECT(vms->secure_tag_sysmem),
+                                         &local_err);
+                if (local_err) {
+                    goto out;
+                }
+            }
+        } else if (kvm_enabled()) {
+            kvm_arm_enable_mte(cpuobj, &local_err);
+            if (local_err) {
+                goto out;
+            }
+        }
+    }
+
+out:
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
  static void machvirt_init(MachineState *machine)
  {
      VirtMachineState *vms = VIRT_MACHINE(machine);
      VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
      MachineClass *mc = MACHINE_GET_CLASS(machine);

hw/arm/virt.c static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem) · line 2216

Quoted 10 lines
hw/arm/virt.c +2 -2
      const CPUArchIdList *possible_cpus;
-    MemoryRegion *sysmem = get_system_memory();
+    MemoryRegion *secure_tag_sysmem = NULL;
      MemoryRegion *secure_sysmem = NULL;
      MemoryRegion *tag_sysmem = NULL;
-    MemoryRegion *secure_tag_sysmem = NULL;
+    MemoryRegion *sysmem;
      int n, virt_max_cpus;
      bool firmware_loaded;
      bool aarch64 = true;

Unnecessary change caused after the defination of @secure_tag_sysmem is moved around. Also, I personally prefer to keep the order to define @sysmem, @secure_sysmem, @tag_sysmem and @secure_tag_system. So this chunk of code would be something like below:

      MemoryRegion *sysmem;
      MemoryRegion *secure_sysmem = NULL;
      MemoryRegion *tag_sysmem = NULL;
      MemoryRegion *secure_tag_sysmem = NULL;

hw/arm/virt.c static void machvirt_init(MachineState

Quoted 1 line
hw/arm/virt.c +0 -0
@@ -2140,6 +2250,8 @@ static void machvirt_init(MachineState

*machine)

Quoted 9 lines
hw/arm/virt.c +2 -0
       */
      finalize_gic_version(vms);
 
+    sysmem = vms->sysmem = get_system_memory();
+
      if (vms->secure) {
          /*
           * The Secure view of the world is the same as the

NonSecure, @@ -2147,7 +2259,7 @@ static void

machvirt_init(MachineState *machine)

hw/arm/virt.c

Quoted 5 lines

* containing the system memory at low priority; any secure-only
* devices go in at higher priority and take precedence. */

hw/arm/virt.c +1 -1
-        secure_sysmem = g_new(MemoryRegion, 1);
+        secure_sysmem = vms->secure_sysmem = g_new(MemoryRegion,

1);

Quoted 1 line

memory_region_init(secure_sysmem, OBJECT(machine), "secure-

memory",

Quoted 3 lines
                             UINT64_MAX);
          memory_region_add_subregion_overlap(secure_sysmem, 0,

sysmem, -1); @@ -2220,6 +2332,33 @@ static void

machvirt_init(MachineState *machine)

Quoted 11 lines
          exit(1);
      }
hw/arm/virt.c +8 -0
+    if (vms->mte && !kvm_enabled() && !tcg_enabled()) {
+        error_report("MTE requested, but not supported ");
+        exit(1);
+    }
+
+    if (vms->mte && kvm_enabled() && !kvm_arm_mte_supported()) {
+        error_report("MTE requested, but not supported by KVM");
+        exit(1);

hw/arm/virt.c static void machvirt_init(MachineState *machine)

Quoted 10 lines
hw/arm/virt.c +10 -0
+    }
+
+    if (vms->mte && tcg_enabled()) {
+        /* Create the memory region only once, but link to all cpus later */
+        tag_sysmem = vms->tag_sysmem = g_new(MemoryRegion, 1);
+        memory_region_init(tag_sysmem, OBJECT(machine),
+                           "tag-memory", UINT64_MAX / 32);
+
+        if (vms->secure) {
+            secure_tag_sysmem = vms->secure_tag_sysmem =

g_new(MemoryRegion, 1);

Quoted 16 lines
hw/arm/virt.c +9 -0
+            memory_region_init(secure_tag_sysmem, OBJECT(machine),
+                               "secure-tag-memory", UINT64_MAX / 32);
+
+            /* As with ram, secure-tag takes precedence over tag.  */
+            memory_region_add_subregion_overlap(secure_tag_sysmem, 0,
+                                                tag_sysmem, -1);
+        }
+    }
+
      create_fdt(vms);
 
      assert(possible_cpus->len == max_cpus); @@ -2232,104 +2371,13 @@

static void machvirt_init(MachineState *machine)

          }

          cpuobj = object_new(possible_cpus->cpus[n].type);

hw/arm/virt.c static void machvirt_init(MachineState *machine)

Quoted 7 lines
hw/arm/virt.c +0 -3
-        object_property_set_int(cpuobj, "mp-affinity",
-                                possible_cpus->cpus[n].arch_id, NULL);
 
          cs = CPU(cpuobj);
          cs->cpu_index = n;
 
-        numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index],

DEVICE(cpuobj),

Quoted 6 lines
hw/arm/virt.c +0 -2
-                          &error_fatal);
-
          aarch64 &= object_property_get_bool(cpuobj, "aarch64",

NULL);

hw/arm/virt.c +0 -1
-        if (!vms->secure) {

hw/arm/virt.c static void machvirt_init(MachineState *machine)

Quoted 10 lines
hw/arm/virt.c +0 -10
-            object_property_set_bool(cpuobj, "has_el3", false, NULL);
-        }
-
-        if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
-            object_property_set_bool(cpuobj, "has_el2", false, NULL);
-        }
-
-        if (vmc->kvm_no_adjvtime &&
-            object_property_find(cpuobj, "kvm-no-adjvtime")) {
-            object_property_set_bool(cpuobj, "kvm-no-adjvtime", true,

NULL);

Quoted 44 lines
hw/arm/virt.c +0 -44
-        }
-
-        if (vmc->no_kvm_steal_time &&
-            object_property_find(cpuobj, "kvm-steal-time")) {
-            object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
-        }
-
-        if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
-            object_property_set_bool(cpuobj, "pmu", false, NULL);
-        }
-
-        if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
-            object_property_set_bool(cpuobj, "lpa2", false, NULL);
-        }
-
-        if (object_property_find(cpuobj, "reset-cbar")) {
-            object_property_set_int(cpuobj, "reset-cbar",
-                                    vms->memmap[VIRT_CPUPERIPHS].base,
-                                    &error_abort);
-        }
-
-        object_property_set_link(cpuobj, "memory", OBJECT(sysmem),
-                                 &error_abort);
-        if (vms->secure) {
-            object_property_set_link(cpuobj, "secure-memory",
-                                     OBJECT(secure_sysmem), &error_abort);
-        }
-
-        if (vms->mte) {
-            if (tcg_enabled()) {
-                /* Create the memory region only once, but link to all cpus. */
-                if (!tag_sysmem) {
-                    /*
-                     * The property exists only if MemTag is supported.
-                     * If it is, we must allocate the ram to back that up.
-                     */
-                    if (!object_property_find(cpuobj, "tag-memory")) {
-                        error_report("MTE requested, but not supported "
-                                     "by the guest CPU");
-                        exit(1);
-                    }
-
-                    tag_sysmem = g_new(MemoryRegion, 1);
-                    memory_region_init(tag_sysmem, OBJECT(machine),

hw/arm/virt.c static void machvirt_init(MachineState *machine) · line 2314

Quoted 10 lines
hw/arm/virt.c +0 -10
-                                       "tag-memory", UINT64_MAX / 32);
-
-                    if (vms->secure) {
-                        secure_tag_sysmem = g_new(MemoryRegion, 1);
-                        memory_region_init(secure_tag_sysmem, OBJECT(machine),
-                                           "secure-tag-memory",
-                                           UINT64_MAX / 32);
-
-                        /* As with ram, secure-tag takes precedence over tag. */
-

memory_region_add_subregion_overlap(secure_tag_sysmem,

Quoted 31 lines
hw/arm/virt.c +1 -22
-                                                            0, tag_sysmem, -1);
-                    }
-                }
-
-                object_property_set_link(cpuobj, "tag-memory",
-                                         OBJECT(tag_sysmem), &error_abort);
-                if (vms->secure) {
-                    object_property_set_link(cpuobj, "secure-tag-memory",
-                                             OBJECT(secure_tag_sysmem),
-                                             &error_abort);
-                }
-            } else if (kvm_enabled()) {
-                if (!kvm_arm_mte_supported()) {
-                    error_report("MTE requested, but not supported by KVM");
-                    exit(1);
-                }
-                kvm_arm_enable_mte(cpuobj, &error_abort);
-            } else {
-                    error_report("MTE requested, but not supported ");
-                    exit(1);
-            }
-        }
+        virt_cpu_set_properties(cpuobj, &error_abort);
 
          qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
          object_unref(cpuobj);
include/hw/arm/virt.h index renamed from include/hw/arm/virt.h +0 -0

aca4f8061b..239f3678af 100644

include/hw/arm/virt.h struct VirtMachineState { · line 152

Quoted 10 lines
include/hw/arm/virt.h +4 -0
@@ -143,6 +143,10 @@ struct VirtMachineState {
      DeviceState *platform_bus_dev;
      FWCfgState *fw_cfg;
      PFlashCFI01 *flash[2];
+    MemoryRegion *sysmem;
+    MemoryRegion *secure_sysmem;
+    MemoryRegion *tag_sysmem;
+    MemoryRegion *secure_tag_sysmem;
      bool secure;
      bool highmem;
      bool highmem_compact;

Thanks,
Gavin