Skip to contentSkip to conversation

target/arm: Allow aarch64=off for TCG

PATCH In review Peter Maydell 3 participants 9 messages Apr 16 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover target/arm: Allow aarch64=off for TCG

This patchset relaxes our current constraint that we only permit -cpu foo,aarch64=off on KVM CPUs, so that you can also use this to run a TCG CPU with aarch64 disabled. This is useful because currently if you want a 32-bit TCG CPU you're limited to either 'max' in qemu-system-arm or else the old v7-only CPUs like a15.

I sent out v1 back in October last year. The main changes here are:
- as I said we ought to do in a follow up to those patches
https://lore.kernel.org/qemu-devel/CAFEAcA9KeO2r+jNont_2Jpvi5MgViH3nXO7TcbANe+3nS_PF-A@mail.gmail.com/
I've switched to only clearing the AArch64 ID regs with TCG; we leave the KVM aarch64=off behaviour alone. (Clearing the regs didn't break KVM, but did leave us in the odd situation that QEMU and KVM didn't have the same idea about what the ID reg values were, which seems like it might lead to subtle problems later.)
- I've added a simple functional test which boots one of the kernels we use for a 32-bit functional test using qemu-system-aarch64 and -cpu max,aarch64=off

I also made a tiny tweak to the docs wording.

Patches 1 and 3 need review.

thanks
-- PMM

Peter Maydell (3):
target/arm: Clear AArch64 ID regs from ARMISARegisters if AArch64
disabled
target/arm: Allow 'aarch64=off' to be set for TCG CPUs tests/functional/aarch64: Add basic test of TCG aarch64=off

 docs/system/arm/cpu-features.rst              | 10 +--
 target/arm/cpu-features.h                     |  5 ++
 target/arm/cpu.c                              | 71 +++++++++++++++++--
 target/arm/cpu.h                              |  3 +-
 tests/functional/aarch64/meson.build          |  1 +
 .../aarch64/test_virt_aarch64_off.py          | 37 ++++++++++
 tests/qtest/arm-cpu-features.c                |  8 +--
 7 files changed, 120 insertions(+), 15 deletions(-)
 create mode 100755 tests/functional/aarch64/test_virt_aarch64_off.py
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 target/arm: Clear AArch64 ID regs from ARMISARegisters if AArch64 disabled

If we create a normally-AArch64 CPU and configure it with aarch64=off, this will by default leave all the AArch64 ID register values in its ARMISARegisters struct untouched. That in turn means that tests of cpu_isar_feature(aa64_something, cpu) will return true.

Until now we have had a design policy that you shouldn't check an aa64_ feature unless you know that the CPU has AArch64; but this is quite fragile as it's easy to forget and only causes a problem in the corner case where AArch64 was turned off. In particular, when we extend the ability to disable AArch64 from only KVM to also TCG there are many more aa64 feature check points which we would otherwise have to audit for whether they needed to be guarded with a check on ARM_FEATURE_AARCH64.

Instead, make the CPU realize function zero out all the 64-bit ID registers if a TCG CPU doesn't have AArch64; this will make aa64_ feature tests generally return false.

We only do this for TCG because only TCG really needs it, and for KVM it might be confusing to have QEMU's idea of the ID registers be different from KVM's.

---
v1->v2: limit scope of register clearing to TCG, as I suggested after posting and testing the v1 version:
https://patchew.org/QEMU/[email protected]/[email protected]/

---
 target/arm/cpu.c | 35 +++++++++++++++++++++++++++++++++++
 target/arm/cpu.h |  3 ++-
 2 files changed, 37 insertions(+), 1 deletion(-)
target/arm/cpu.c +35 -0
index ccc47c8a9a..11d3437843 100644
@@ -1610,6 +1610,27 @@ void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
     }
 }
 
+static void arm_clear_aarch64_idregs(ARMCPU *cpu)
+{
+    /* Zero out all the AArch64 ID registers in ARMISARegisters */
+    SET_IDREG(&cpu->isar, ID_AA64ISAR0, 0);
+    SET_IDREG(&cpu->isar, ID_AA64ISAR1, 0);
+    SET_IDREG(&cpu->isar, ID_AA64ISAR2, 0);
+    SET_IDREG(&cpu->isar, ID_AA64PFR0, 0);
+    SET_IDREG(&cpu->isar, ID_AA64PFR1, 0);
+    SET_IDREG(&cpu->isar, ID_AA64PFR2, 0);
+    SET_IDREG(&cpu->isar, ID_AA64MMFR0, 0);
+    SET_IDREG(&cpu->isar, ID_AA64MMFR1, 0);
+    SET_IDREG(&cpu->isar, ID_AA64MMFR2, 0);
+    SET_IDREG(&cpu->isar, ID_AA64MMFR3, 0);
+    SET_IDREG(&cpu->isar, ID_AA64DFR0, 0);
+    SET_IDREG(&cpu->isar, ID_AA64DFR1, 0);
+    SET_IDREG(&cpu->isar, ID_AA64AFR0, 0);
+    SET_IDREG(&cpu->isar, ID_AA64AFR1, 0);
+    SET_IDREG(&cpu->isar, ID_AA64ZFR0, 0);
+    SET_IDREG(&cpu->isar, ID_AA64SMFR0, 0);
+}
+
 static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
@@ -1737,6 +1758,20 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 #endif
 
+    /*
+     * A TCG aarch64=off CPU has no AArch64 at all, so we clear out the
+     * ID registers to avoid cpu_isar_feature(aa64_something, cpu) tests
+     * incorrectly returning true. We don't do this for other accelerators
+     * (which in practice means "for KVM", since no others have AArch32
+     * guest support) because from KVM's point of view the AArch64 ID
+     * registers still exist and must have their correct values. So we
+     * avoid clearing them out so that we don't have QEMU and KVM with
+     * different ideas of the ID registers.
+     */
+    if (tcg_enabled() && !arm_feature(env, ARM_FEATURE_AARCH64)) {
+        arm_clear_aarch64_idregs(cpu);
+    }
+
 #ifdef CONFIG_USER_ONLY
     /*
      * User mode relies on IC IVAU instructions to catch modification of
target/arm/cpu.h +2 -1
index 657ff4ab20..ab6bacf4aa 100644
@@ -1080,7 +1080,8 @@ struct ArchCPU {
      * Note that if you add an ID register to the ARMISARegisters struct
      * you need to also update the 32-bit and 64-bit versions of the
      * kvm_arm_get_host_cpu_features() function to correctly populate the
-     * field by reading the value from the KVM vCPU.
+     * field by reading the value from the KVM vCPU. If it is an AArch64
+     * ID register then you also must update arm_clear_aarch64_idregs().
      */
     struct ARMISARegisters {
         uint32_t mvfr0;
Signature · 1 line
2.43.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] target/arm: Clear AArch64 ID regs from ARMISARegisters if AArch64 disabled

Quoted 23 lines · Peter Maydell

If we create a normally-AArch64 CPU and configure it with aarch64=off, this will by default leave all the AArch64 ID register values in its ARMISARegisters struct untouched. That in turn means that tests of cpu_isar_feature(aa64_something, cpu) will return true.

Until now we have had a design policy that you shouldn't check an aa64_ feature unless you know that the CPU has AArch64; but this is quite fragile as it's easy to forget and only causes a problem in the corner case where AArch64 was turned off. In particular, when we extend the ability to disable AArch64 from only KVM to also TCG there are many more aa64 feature check points which we would otherwise have to audit for whether they needed to be guarded with a check on ARM_FEATURE_AARCH64.

Instead, make the CPU realize function zero out all the 64-bit ID registers if a TCG CPU doesn't have AArch64; this will make aa64_ feature tests generally return false.

We only do this for TCG because only TCG really needs it, and for KVM it might be confusing to have QEMU's idea of the ID registers be different from KVM's.

Quoted 8 lines

---
v1->v2: limit scope of register clearing to TCG, as I suggested after posting and testing the v1 version:
https://patchew.org/QEMU/[email protected]/[email protected]/

---
  target/arm/cpu.c | 35 +++++++++++++++++++++++++++++++++++
  target/arm/cpu.h |  3 ++-
  2 files changed, 37 insertions(+), 1 deletion(-)
Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] target/arm: Clear AArch64 ID regs from ARMISARegisters if AArch64 disabled

In reply to Peter Maydell
Quoted 28 lines · Peter Maydell

If we create a normally-AArch64 CPU and configure it with aarch64=off, this will by default leave all the AArch64 ID register values in its ARMISARegisters struct untouched. That in turn means that tests of cpu_isar_feature(aa64_something, cpu) will return true.

Until now we have had a design policy that you shouldn't check an aa64_ feature unless you know that the CPU has AArch64; but this is quite fragile as it's easy to forget and only causes a problem in the corner case where AArch64 was turned off. In particular, when we extend the ability to disable AArch64 from only KVM to also TCG there are many more aa64 feature check points which we would otherwise have to audit for whether they needed to be guarded with a check on ARM_FEATURE_AARCH64.

Instead, make the CPU realize function zero out all the 64-bit ID registers if a TCG CPU doesn't have AArch64; this will make aa64_ feature tests generally return false.

We only do this for TCG because only TCG really needs it, and for KVM it might be confusing to have QEMU's idea of the ID registers be different from KVM's.

---
v1->v2: limit scope of register clearing to TCG, as I suggested after posting and testing the v1 version:
https://patchew.org/QEMU/20251002101648.2455374-1-
[email protected]/[email protected]/

Quoted 4 lines
---
  target/arm/cpu.c | 35 +++++++++++++++++++++++++++++++++++
  target/arm/cpu.h |  3 ++-
  2 files changed, 37 insertions(+), 1 deletion(-)

r~

#4 lore raw

PATCH v2 2/3 target/arm: Allow 'aarch64=off' to be set for TCG CPUs

In reply to Peter Maydell

Allow the 'aarch64=off' property, which is currently KVM-only, to be set for TCG CPUs also.

Note that we don't permit it on the qemu-aarch64 user-mode binary:
this makes no sense as that executable can only handle AArch64 syscalls (and it would also assert at startup since it doesn't compile in the A32-specific GDB xml files like arm-neon.xml).

---
v1->v2: slight tweak to the docs wording

---
 docs/system/arm/cpu-features.rst | 10 +++++----
 target/arm/cpu-features.h        |  5 +++++
 target/arm/cpu.c                 | 36 ++++++++++++++++++++++++++++----
 tests/qtest/arm-cpu-features.c   |  8 ++-----
 4 files changed, 45 insertions(+), 14 deletions(-)
docs/system/arm/cpu-features.rst +6 -4
index ce19ae6a04..10b0eff27e 100644
@@ -23,10 +23,12 @@ not implement ARMv8-A, will not have the ``aarch64`` CPU property.
 QEMU's support may be limited for some CPU features, only partially
 supporting the feature or only supporting the feature under certain
 configurations.  For example, the ``aarch64`` CPU feature, which, when
-disabled, enables the optional AArch32 CPU feature, is only supported
-when using the KVM accelerator and when running on a host CPU type that
-supports the feature.  While ``aarch64`` currently only works with KVM,
-it could work with TCG.  CPU features that are specific to KVM are
+disabled, enables the optional AArch32 CPU feature, can only be set to
+``off`` on the TCG and KVM accelerators, and it cannot be set to
+``off`` under KVM unless running on a host CPU type that supports
+running guests in AArch32.
+
+CPU features that are inherently specific to KVM are
 prefixed with "kvm-" and are described in "KVM VCPU Features".
 
 CPU Feature Probing
target/arm/cpu-features.h +5 -0
index b683c9551a..6e5212ff6c 100644
@@ -1071,6 +1071,11 @@ static inline bool isar_feature_aa64_aa32_el2(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64PFR0, EL2) >= 2;
 }
 
+static inline bool isar_feature_aa64_aa32_el3(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR0, EL3) >= 2;
+}
+
 static inline bool isar_feature_aa64_ras(const ARMISARegisters *id)
 {
     return FIELD_EX64_IDREG(id, ID_AA64PFR0, RAS) != 0;
target/arm/cpu.c +32 -4
index 11d3437843..347616fa5a 100644
@@ -1248,10 +1248,38 @@ static void aarch64_cpu_set_aarch64(Object *obj, bool value, Error **errp)
      * uniform execution state like do_interrupt.
      */
     if (value == false) {
-        if (!kvm_enabled() || !kvm_arm_aarch32_supported()) {
-            error_setg(errp, "'aarch64' feature cannot be disabled "
-                             "unless KVM is enabled and 32-bit EL1 "
-                             "is supported");
+        if (kvm_enabled()) {
+            if (!kvm_arm_aarch32_supported()) {
+                error_setg(errp, "'aarch64' feature cannot be disabled for KVM "
+                           "because this host does not support 32-bit EL1");
+                return;
+            }
+        } else if (tcg_enabled()) {
+#ifdef CONFIG_USER_ONLY
+            error_setg(errp, "'aarch64' feature cannot be disabled for "
+                       "usermode emulator qemu-aarch64; use qemu-arm instead");
+            return;
+#else
+            bool aa32_at_highest_el;
+            if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
+                aa32_at_highest_el = cpu_isar_feature(aa64_aa32_el3, cpu);
+            } else if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
+                aa32_at_highest_el = cpu_isar_feature(aa64_aa32_el2, cpu);
+            } else {
+                aa32_at_highest_el = cpu_isar_feature(aa64_aa32_el1, cpu);
+            }
+
+            if (!aa32_at_highest_el) {
+                error_setg(errp, "'aarch64' feature cannot be disabled for "
+                           "this TCG CPU because it does not support 32-bit "
+                           "execution at its highest implemented exception "
+                           "level");
+                return;
+            }
+#endif
+        } else {
+            error_setg(errp, "'aarch64' feature cannot be disabled for "
+                       "this accelerator");
             return;
         }
         unset_feature(&cpu->env, ARM_FEATURE_AARCH64);
tests/qtest/arm-cpu-features.c +2 -6
index bbdd89a81d..cb4d01fd46 100644
@@ -493,12 +493,8 @@ static void test_query_cpu_model_expansion(const void *data)
         sve_tests_default(qts, "max");
         pauth_tests_default(qts, "max");
 
-        /* Test that features that depend on KVM generate errors without. */
-        assert_error(qts, "max",
-                     "'aarch64' feature cannot be disabled "
-                     "unless KVM is enabled and 32-bit EL1 "
-                     "is supported",
-                     "{ 'aarch64': false }");
+        /* TCG allows us to turn off AArch64 on the 'max' CPU type */
+        assert_set_feature(qts, "max", "aarch64", false);
     }
 
     qtest_quit(qts);
Signature · 1 line
2.43.0
Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] target/arm: Allow 'aarch64=off' to be set for TCG CPUs

Quoted 27 lines · Peter Maydell

Allow the 'aarch64=off' property, which is currently KVM-only, to be set for TCG CPUs also.

Note that we don't permit it on the qemu-aarch64 user-mode binary:
this makes no sense as that executable can only handle AArch64 syscalls (and it would also assert at startup since it doesn't compile in the A32-specific GDB xml files like arm-neon.xml).

---
v1->v2: slight tweak to the docs wording

---
  docs/system/arm/cpu-features.rst | 10 +++++----
  target/arm/cpu-features.h        |  5 +++++
  target/arm/cpu.c                 | 36 ++++++++++++++++++++++++++++----
  tests/qtest/arm-cpu-features.c   |  8 ++-----
  4 files changed, 45 insertions(+), 14 deletions(-)
target/arm/cpu.c +6 -0
+            bool aa32_at_highest_el;
+            if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) {
+                aa32_at_highest_el = cpu_isar_feature(aa64_aa32_el3, cpu);
+            } else if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
+                aa32_at_highest_el = cpu_isar_feature(aa64_aa32_el2, cpu);
+            } else {

target/arm/cpu.c static void aarch64_cpu_set_aarch64(Object *obj, bool value, Error **errp) · line 1278

Quoted 10 lines
target/arm/cpu.c +10 -0
+                aa32_at_highest_el = cpu_isar_feature(aa64_aa32_el1, cpu);
+            }
+
+            if (!aa32_at_highest_el) {
+                error_setg(errp, "'aarch64' feature cannot be disabled for "
+                           "this TCG CPU because it does not support 32-bit "
+                           "execution at its highest implemented exception "
+                           "level");
+                return;
+            }

Nice.

#6 lore raw

PATCH v2 3/3 tests/functional/aarch64: Add basic test of TCG aarch64=off

In reply to Peter Maydell

Add a basic test of the TCG 'aarch64=off' functionality; this is the same as our existing arm/test_virt test, but it runs the AArch32 guest kernel on qemu-system-aarch64 with -cpu max,aarch64=off.

---
 tests/functional/aarch64/meson.build          |  1 +
 .../aarch64/test_virt_aarch64_off.py          | 37 +++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100755 tests/functional/aarch64/test_virt_aarch64_off.py
tests/functional/aarch64/meson.build +1 -0
index 7ea8c22b04..de6f3ad5e6 100644
@@ -45,6 +45,7 @@ tests_aarch64_system_thorough = [
   'tcg_plugins',
   'tuxrun',
   'virt',
+  'virt_aarch64_off',
   'virt_gpu',
   'virt_vbsa',
   'xen',
tests/functional/aarch64/test_virt_aarch64_off.py new file +37 -0
new file mode 100755
index 0000000000..13d8b73b0d
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+#
+# Functional test that boots an AArch32 Linux kernel and checks the console
+# on a TCG aarch64 CPU with aarch64=off. This is the same image etc
+# as we use in tests/functional/arm/test_virt.py.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+from qemu_test import LinuxKernelTest, Asset
+
+class ArmVirtMachine(LinuxKernelTest):
+
+    ASSET_KERNEL = Asset(
+        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/'
+         'releases/29/Everything/armhfp/os/images/pxeboot/vmlinuz'),
+        '18dd5f1a9a28bd539f9d047f7c0677211bae528e8712b40ca5a229a4ad8e2591')
+
+    def test_arm_virt(self):
+        self.set_machine('virt')
+        # KVM aarch64=off requires a host CPU that supports it, so
+        # restrict the test to TCG only
+        self.require_accelerator('tcg')
+        self.vm.add_args('-cpu', 'max,aarch64=off')
+
+        kernel_path = self.ASSET_KERNEL.fetch()
+
+        self.vm.set_console()
+        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
+                               'console=ttyAMA0')
+        self.vm.add_args('-kernel', kernel_path,
+                         '-append', kernel_command_line)
+        self.vm.launch()
+        console_pattern = 'Kernel command line: %s' % kernel_command_line
+        self.wait_for_console_pattern(console_pattern)
+
+if __name__ == '__main__':
+    LinuxKernelTest.main()
Signature · 1 line
2.43.0
Reviewed-by Tested-by on v2 3/3
#7 lore raw

Re: [PATCH v2 3/3] tests/functional/aarch64: Add basic test of TCG aarch64=off

Quoted 10 lines · Peter Maydell

Add a basic test of the TCG 'aarch64=off' functionality; this is the same as our existing arm/test_virt test, but it runs the AArch32 guest kernel on qemu-system-aarch64 with -cpu max,aarch64=off.

---
  tests/functional/aarch64/meson.build          |  1 +
  .../aarch64/test_virt_aarch64_off.py          | 37 +++++++++++++++++++
  2 files changed, 38 insertions(+)

create mode 100755 tests/functional/aarch64/test_virt_aarch64_off.py

Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] tests/functional/aarch64: Add basic test of TCG aarch64=off

In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

Add a basic test of the TCG 'aarch64=off' functionality; this is the same as our existing arm/test_virt test, but it runs the AArch32 guest kernel on qemu-system-aarch64 with -cpu max,aarch64=off.

---
  tests/functional/aarch64/meson.build          |  1 +
  .../aarch64/test_virt_aarch64_off.py          | 37 +++++++++++++++++++
  2 files changed, 38 insertions(+)

create mode 100755 tests/functional/aarch64/test_virt_aarch64_off.py

r~