Skip to contentSkip to conversation

arm: Don't remove EL3 exposure for SMC conduit

PATCH In review Alexander Graf 3 participants 8 messages Nov 14, 2021 – Nov 15, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm: Don't remove EL3 exposure for SMC conduit

When we expose an SMC conduit, we're implicitly telling the guest that there is EL3 available because it needs to call it. While that EL3 then is not backed by the emulated CPU, from the guest's EL2 point of view, it still means there is an EL3 to call into.

This is a problem for VMware ESXi, which validates EL3 availability before doing SMC calls. With this patch, VMware ESXi works with SMP in TCG.

---
 target/arm/cpu.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
target/arm/cpu.c +15 -5
index a211804fd3..21092c5242 100644
@@ -1782,11 +1782,21 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
          */
         unset_feature(env, ARM_FEATURE_EL3);
 
-        /* Disable the security extension feature bits in the processor feature
-         * registers as well. These are id_pfr1[7:4] and id_aa64pfr0[15:12].
-         */
-        cpu->isar.id_pfr1 &= ~0xf0;
-        cpu->isar.id_aa64pfr0 &= ~0xf000;
+        if (cpu->psci_conduit == QEMU_PSCI_CONDUIT_SMC) {
+            /*
+             * We tell the guest to use SMC calls into EL3 for PSCI calls, so
+             * there has to be EL3 available. We merely execute it on the host
+             * in QEMU rather than in actual EL3 inside the guest.
+             */
+        } else {
+            /*
+             * Disable the security extension feature bits in the processor
+             * feature registers as well. These are id_pfr1[7:4] and
+             * id_aa64pfr0[15:12].
+             */
+            cpu->isar.id_pfr1 &= ~0xf0;
+            cpu->isar.id_aa64pfr0 &= ~0xf000;
+        }
     }
 
     if (!cpu->has_el2) {
Signature · 1 line
2.30.1 (Apple Git-130)
#1 lore raw
Quoted 33 lines · Alexander Graf

When we expose an SMC conduit, we're implicitly telling the guest that there is EL3 available because it needs to call it. While that EL3 then is not backed by the emulated CPU, from the guest's EL2 point of view, it still means there is an EL3 to call into.

This is a problem for VMware ESXi, which validates EL3 availability before doing SMC calls. With this patch, VMware ESXi works with SMP in TCG.

---
 target/arm/cpu.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
target/arm/cpu.c +5 -5
index a211804fd3..21092c5242 100644
@@ -1782,11 +1782,21 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
          */
         unset_feature(env, ARM_FEATURE_EL3);
 
-        /* Disable the security extension feature bits in the processor feature
-         * registers as well. These are id_pfr1[7:4] and id_aa64pfr0[15:12].
-         */
-        cpu->isar.id_pfr1 &= ~0xf0;
-        cpu->isar.id_aa64pfr0 &= ~0xf000;
+        if (cpu->psci_conduit == QEMU_PSCI_CONDUIT_SMC) {
+            /*
+             * We tell the guest to use SMC calls into EL3 for PSCI calls, so
+             * there has to be EL3 available. We merely execute it on the host
+             * in QEMU rather than in actual EL3 inside the guest.

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

Quoted 10 lines
target/arm/cpu.c +10 -0
+             */
+        } else {
+            /*
+             * Disable the security extension feature bits in the processor
+             * feature registers as well. These are id_pfr1[7:4] and
+             * id_aa64pfr0[15:12].
+             */
+            cpu->isar.id_pfr1 &= ~0xf0;
+            cpu->isar.id_aa64pfr0 &= ~0xf000;
+        }

This is tricky, because we use the cpu->isar values to determine whether we should be emulating things. So this change means we now create an inconsistent CPU which in some ways claims to have EL3 (the ISAR ID bits say so) and in some ways does not (the ARM_FEATURE_EL3 flag is unset), and depending on which of the two "do we have EL3?" methods any bit of the TCG code is using will give different results...

-- PMM

#2 lore raw
Quoted 47 lines

Am 14.11.2021 um 18:20 schrieb Peter Maydell <[email protected]>:

Quoted 42 lines · Alexander Graf

When we expose an SMC conduit, we're implicitly telling the guest that there is EL3 available because it needs to call it. While that EL3 then is not backed by the emulated CPU, from the guest's EL2 point of view, it still means there is an EL3 to call into.

This is a problem for VMware ESXi, which validates EL3 availability before doing SMC calls. With this patch, VMware ESXi works with SMP in TCG.

---
target/arm/cpu.c | 20 +++++++++++++++-----

1 file changed, 15 insertions(+), 5 deletions(-)
target/arm/cpu.c +14 -5
index a211804fd3..21092c5242 100644
@@ -1782,11 +1782,21 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         */
        unset_feature(env, ARM_FEATURE_EL3);
 
-        /* Disable the security extension feature bits in the processor feature
-         * registers as well. These are id_pfr1[7:4] and id_aa64pfr0[15:12].
-         */
-        cpu->isar.id_pfr1 &= ~0xf0;
-        cpu->isar.id_aa64pfr0 &= ~0xf000;
+        if (cpu->psci_conduit == QEMU_PSCI_CONDUIT_SMC) {
+            /*
+             * We tell the guest to use SMC calls into EL3 for PSCI calls, so
+             * there has to be EL3 available. We merely execute it on the host
+             * in QEMU rather than in actual EL3 inside the guest.
+             */
+        } else {
+            /*
+             * Disable the security extension feature bits in the processor
+             * feature registers as well. These are id_pfr1[7:4] and
+             * id_aa64pfr0[15:12].
+             */
+            cpu->isar.id_pfr1 &= ~0xf0;
+            cpu->isar.id_aa64pfr0 &= ~0xf000;
Quoted 8 lines

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

Quoted 1 line
target/arm/cpu.c +1 -0
+        }

This is tricky, because we use the cpu->isar values to determine whether we should be emulating things. So this change means we now create an inconsistent CPU which in some ways claims to have EL3 (the ISAR ID bits say so) and in some ways does not (the ARM_FEATURE_EL3 flag is unset), and depending on which of the two "do we have EL3?" methods any bit of the TCG code is using will give different results...

Do you think it would be sufficient to go through all readers of the isar bits and guard them behind an ARM_FEATURE_EL3 check in addition? I'll be happy to do so then! :)

Alex

#3 lore raw
Quoted 52 lines · Alexander Graf
Quoted 44 lines

Am 14.11.2021 um 18:20 schrieb Peter Maydell <[email protected]>:

Quoted 40 lines · Alexander Graf

When we expose an SMC conduit, we're implicitly telling the guest that there is EL3 available because it needs to call it. While that EL3 then is not backed by the emulated CPU, from the guest's EL2 point of view, it still means there is an EL3 to call into.

This is a problem for VMware ESXi, which validates EL3 availability before doing SMC calls. With this patch, VMware ESXi works with SMP in TCG.

---
target/arm/cpu.c | 20 +++++++++++++++-----

1 file changed, 15 insertions(+), 5 deletions(-)
target/arm/cpu.c +13 -5
index a211804fd3..21092c5242 100644
@@ -1782,11 +1782,21 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
          */
         unset_feature(env, ARM_FEATURE_EL3);
 
-        /* Disable the security extension feature bits in the processor feature
-         * registers as well. These are id_pfr1[7:4] and id_aa64pfr0[15:12].
-         */
-        cpu->isar.id_pfr1 &= ~0xf0;
-        cpu->isar.id_aa64pfr0 &= ~0xf000;
+        if (cpu->psci_conduit == QEMU_PSCI_CONDUIT_SMC) {
+            /*
+             * We tell the guest to use SMC calls into EL3 for PSCI calls, so
+             * there has to be EL3 available. We merely execute it on the host
+             * in QEMU rather than in actual EL3 inside the guest.
+             */
+        } else {
+            /*
+             * Disable the security extension feature bits in the processor
+             * feature registers as well. These are id_pfr1[7:4] and
+             * id_aa64pfr0[15:12].
+             */
+            cpu->isar.id_pfr1 &= ~0xf0;
Quoted 8 lines

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

Quoted 2 lines
target/arm/cpu.c +2 -0
+            cpu->isar.id_aa64pfr0 &= ~0xf000;
+        }

This is tricky, because we use the cpu->isar values to determine whether we should be emulating things. So this change means we now create an inconsistent CPU which in some ways claims to have EL3 (the ISAR ID bits say so) and in some ways does not (the ARM_FEATURE_EL3 flag is unset), and depending on which of the two "do we have EL3?" methods any bit of the TCG code is using will give different results...

Quoted 1 line

Do you think it would be sufficient to go through all readers of the isar bits and guard them behind an ARM_FEATURE_EL3 check in addition? I'll be happy to do so then! :)

The aa32 pfr1 seems to have a single consumer:

static inline bool isar_feature_aa32_m_sec_state(const ARMISARegisters *id) {
    /*
     * Return true if M-profile state handling insns
     * (VSCCLRM, CLRM, FPCTX access insns) are implemented
     */
    return FIELD_EX32(id->id_pfr1, ID_PFR1, SECURITY) >= 3;
}

which is only called for M profile. For M profile however, the patch is irrelevant as it's patching inside a !arm_feature(env, ARM_FEATURE_M) branch.

The only consumer of the AA64PFR.EL3 field I could find was in KVM code when assembling -cpu host. It again is unaffected by this patch.

Alex

#4 lore raw
In reply to Alexander Graf
Quoted 11 lines · Alexander Graf
Quoted 10 lines

Am 14.11.2021 um 18:20 schrieb Peter Maydell <[email protected]>:
This is tricky, because we use the cpu->isar values to determine whether we should be emulating things. So this change means we now create an inconsistent CPU which in some ways claims to have EL3 (the ISAR ID bits say so) and in some ways does not (the ARM_FEATURE_EL3 flag is unset), and depending on which of the two "do we have EL3?" methods any bit of the TCG code is using will give different results...

Quoted 1 line

Do you think it would be sufficient to go through all readers of the isar bits and guard them behind an ARM_FEATURE_EL3 check in addition? I'll be happy to do so then! :)

That would be a big reverse-course on a design choice we made that the preference is to look at the ID registers and phase out the use of ARM_FEATURE bits where possible.

-- PMM

#5 lore raw
Quoted 10 lines · Peter Maydell
Quoted 9 lines · Alexander Graf
Quoted 9 lines

Am 14.11.2021 um 18:20 schrieb Peter Maydell <[email protected]>:
This is tricky, because we use the cpu->isar values to determine whether we should be emulating things. So this change means we now create an inconsistent CPU which in some ways claims to have EL3 (the ISAR ID bits say so) and in some ways does not (the ARM_FEATURE_EL3 flag is unset), and depending on which of the two "do we have EL3?" methods any bit of the TCG code is using will give different results...

Quoted 4 lines
Quoted 1 line

Do you think it would be sufficient to go through all readers of the isar bits and guard them behind an ARM_FEATURE_EL3 check in addition? I'll be happy to do so then! :)

That would be a big reverse-course on a design choice we made that the preference is to look at the ID registers and phase out the use of ARM_FEATURE bits where possible.

I'm open to alternatives. As it stands, we're lying to the guest because we tell it "SMC is not available" but ask it to call SMC for PSCI, which is bad too.

Alex

#6 lore raw
Quoted 14 lines · Alexander Graf
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Alexander Graf
Quoted 9 lines

Am 14.11.2021 um 18:20 schrieb Peter Maydell <[email protected]>:
This is tricky, because we use the cpu->isar values to determine whether we should be emulating things. So this change means we now create an inconsistent CPU which in some ways claims to have EL3 (the ISAR ID bits say so) and in some ways does not (the ARM_FEATURE_EL3 flag is unset), and depending on which of the two "do we have EL3?" methods any bit of the TCG code is using will give different results...

Do you think it would be sufficient to go through all readers of the isar bits and guard them behind an ARM_FEATURE_EL3 check in addition? I'll be happy to do so then! :)

Quoted 8 lines
Quoted 3 lines

That would be a big reverse-course on a design choice we made that the preference is to look at the ID registers and phase out the use of ARM_FEATURE bits where possible.

I'm open to alternatives. As it stands, we're lying to the guest because we tell it "SMC is not available" but ask it to call SMC for PSCI, which is bad too.

Is testing the ISAR bits actually telling a guest that SMC exists or just the CPU is capable of handling it? I guess -kernel only is a weird case because otherwise if EL3 is available some sort of firmware has to have gotten the CPU into a state a kernel can boot. It doesn't imply that firmware knows how to do a PSCI call though - surely there is some firmware configuration/probing mechanism you need to rely on for that?

Signature · 1 line
Alex Bennée
#7 lore raw
Quoted 19 lines · Alex Bennée
Quoted 17 lines · Alexander Graf
Quoted 15 lines · Peter Maydell
Quoted 11 lines · Alexander Graf
Quoted 8 lines

Am 14.11.2021 um 18:20 schrieb Peter Maydell <[email protected]>:
This is tricky, because we use the cpu->isar values to determine whether we should be emulating things. So this change means we now create an inconsistent CPU which in some ways claims to have EL3 (the ISAR ID bits say so) and in some ways does not (the ARM_FEATURE_EL3 flag is unset), and depending on which of the two "do we have EL3?" methods any bit of the TCG code is using will give different results...

Do you think it would be sufficient to go through all readers of the isar bits and guard them behind an ARM_FEATURE_EL3 check in addition? I'll be happy to do so then! :)

That would be a big reverse-course on a design choice we made that the preference is to look at the ID registers and phase out the use of ARM_FEATURE bits where possible.

Quoted 5 lines
Quoted 3 lines

I'm open to alternatives. As it stands, we're lying to the guest because we tell it "SMC is not available" but ask it to call SMC for PSCI, which is bad too.

Is testing the ISAR bits actually telling a guest that SMC exists or just the CPU is capable of handling it? I guess -kernel only is a weird

The way I understand it, it tells you whether SMC is a #UD or not. Whether that SMC call goes into happy PSCI land or into nirvana is a different question - but there's nothing you can do from EL<3 to figure that out I believe?

Quoted 4 lines

case because otherwise if EL3 is available some sort of firmware has to have gotten the CPU into a state a kernel can boot. It doesn't imply that firmware knows how to do a PSCI call though - surely there is some firmware configuration/probing mechanism you need to rely on for that?

Oh, absolutely! As an OS, you should look up in ACPI or DT whether to do an SMC call for PSCI operations or not.

The problem here is that we tell the OS in ACPI to do an SMC call, yet we tell it in AA64PFR0 that SMC is not implemented and thus would trigger a #UD.

Alex