[PATCH v5 3/3] target/ppc/kvm: Use host compatibility mode for nested guests

Amit Machhiwal posted 3 patches 1 month, 1 week ago
Maintainers: Nicholas Piggin <npiggin@gmail.com>, Harsh Prateek Bora <harshpb@linux.ibm.com>, "Michael S. Tsirkin" <mst@redhat.com>, Cornelia Huck <cohuck@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Chinmay Rath <rathc@linux.ibm.com>, Glenn Miles <milesg@linux.ibm.com>
There is a newer version of this series
[PATCH v5 3/3] target/ppc/kvm: Use host compatibility mode for nested guests
Posted by Amit Machhiwal 1 month, 1 week ago
On POWER systems, the host CPU may run in a compatibility mode (e.g.,
a Power11 processor operating in Power10 compatibility mode). When
running nested KVM guests, QEMU currently derives the host CPU type
using mfpvr(), which reflects the physical processor version. This can
result in a mismatch between the CPU model used by QEMU and the
compatibility mode enforced by the host, leading to guest boot failures
such as "KVM-NESTEDv2: couldn't set guest wide elements".

Update kvm_ppc_get_host_cpu_class() to check if the host is running in
a compatibility mode using kvm_ppc_host_compat_pvr(). When available,
use the compatibility PVR instead of the raw hardware PVR when selecting
the CPU model. This ensures that QEMU selects a CPU model consistent
with the host compatibility mode, allowing nested guests to boot
correctly.

The guard uses #if defined(TARGET_PPC64) to prevent build breakage on
ppc32 targets where the POWER9/10/11 PVR constants are not defined.

Tested-by: Gautam Menghani <gautam@linux.ibm.com>
Reviewed-by: Gautam Menghani <gautam@linux.ibm.com>
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
No changes in this version.

 target/ppc/kvm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
index d4c5601a00c4..aa8269186a1a 100644
--- a/target/ppc/kvm.c
+++ b/target/ppc/kvm.c
@@ -2682,6 +2682,18 @@ PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
     uint32_t host_pvr = mfpvr();
     PowerPCCPUClass *pvr_pcc;
 
+#if defined(TARGET_PPC64)
+#ifndef CONFIG_KVM
+#error "CONFIG_KVM is not enabled"
+#endif
+    uint32_t compat_host_pvr;
+
+    compat_host_pvr = kvm_ppc_host_compat_pvr();
+    if (compat_host_pvr) {
+        host_pvr = compat_host_pvr;
+    }
+#endif /* TARGET_PPC64 */
+
     pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
     if (pvr_pcc == NULL) {
         pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
-- 
2.50.1 (Apple Git-155)
Re: [PATCH v5 3/3] target/ppc/kvm: Use host compatibility mode for nested guests
Posted by BALATON Zoltan 1 month, 1 week ago
On Tue, 4 Aug 2026, Amit Machhiwal wrote:
> On POWER systems, the host CPU may run in a compatibility mode (e.g.,
> a Power11 processor operating in Power10 compatibility mode). When
> running nested KVM guests, QEMU currently derives the host CPU type
> using mfpvr(), which reflects the physical processor version. This can
> result in a mismatch between the CPU model used by QEMU and the
> compatibility mode enforced by the host, leading to guest boot failures
> such as "KVM-NESTEDv2: couldn't set guest wide elements".
>
> Update kvm_ppc_get_host_cpu_class() to check if the host is running in
> a compatibility mode using kvm_ppc_host_compat_pvr(). When available,
> use the compatibility PVR instead of the raw hardware PVR when selecting
> the CPU model. This ensures that QEMU selects a CPU model consistent
> with the host compatibility mode, allowing nested guests to boot
> correctly.
>
> The guard uses #if defined(TARGET_PPC64) to prevent build breakage on
> ppc32 targets where the POWER9/10/11 PVR constants are not defined.
>
> Tested-by: Gautam Menghani <gautam@linux.ibm.com>
> Reviewed-by: Gautam Menghani <gautam@linux.ibm.com>
> Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
> ---
> No changes in this version.
>
> target/ppc/kvm.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
> index d4c5601a00c4..aa8269186a1a 100644
> --- a/target/ppc/kvm.c
> +++ b/target/ppc/kvm.c
> @@ -2682,6 +2682,18 @@ PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
>     uint32_t host_pvr = mfpvr();
>     PowerPCCPUClass *pvr_pcc;
>
> +#if defined(TARGET_PPC64)
> +#ifndef CONFIG_KVM
> +#error "CONFIG_KVM is not enabled"
> +#endif

In meson.build:

ppc_system_ss.add(when: 'CONFIG_KVM', if_true: files('kvm.c'))

so I think this check and #error does not make sense.

Regards,
BALATON Zoltan

> +    uint32_t compat_host_pvr;
> +
> +    compat_host_pvr = kvm_ppc_host_compat_pvr();
> +    if (compat_host_pvr) {
> +        host_pvr = compat_host_pvr;
> +    }
> +#endif /* TARGET_PPC64 */
> +
>     pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
>     if (pvr_pcc == NULL) {
>         pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
>
Re: [PATCH v5 3/3] target/ppc/kvm: Use host compatibility mode for nested guests
Posted by Amit Machhiwal 1 month, 1 week ago
Hi BALATON,

Thanks for reviewing this patch. Please find my response inline below.

On 2026/08/04 09:55 PM, BALATON Zoltan wrote:
> On Tue, 4 Aug 2026, Amit Machhiwal wrote:
> > On POWER systems, the host CPU may run in a compatibility mode (e.g.,
> > a Power11 processor operating in Power10 compatibility mode). When
> > running nested KVM guests, QEMU currently derives the host CPU type
> > using mfpvr(), which reflects the physical processor version. This can
> > result in a mismatch between the CPU model used by QEMU and the
> > compatibility mode enforced by the host, leading to guest boot failures
> > such as "KVM-NESTEDv2: couldn't set guest wide elements".
> > 
> > Update kvm_ppc_get_host_cpu_class() to check if the host is running in
> > a compatibility mode using kvm_ppc_host_compat_pvr(). When available,
> > use the compatibility PVR instead of the raw hardware PVR when selecting
> > the CPU model. This ensures that QEMU selects a CPU model consistent
> > with the host compatibility mode, allowing nested guests to boot
> > correctly.
> > 
> > The guard uses #if defined(TARGET_PPC64) to prevent build breakage on
> > ppc32 targets where the POWER9/10/11 PVR constants are not defined.
> > 
> > Tested-by: Gautam Menghani <gautam@linux.ibm.com>
> > Reviewed-by: Gautam Menghani <gautam@linux.ibm.com>
> > Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
> > ---
> > No changes in this version.
> > 
> > target/ppc/kvm.c | 12 ++++++++++++
> > 1 file changed, 12 insertions(+)
> > 
> > diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
> > index d4c5601a00c4..aa8269186a1a 100644
> > --- a/target/ppc/kvm.c
> > +++ b/target/ppc/kvm.c
> > @@ -2682,6 +2682,18 @@ PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
> >     uint32_t host_pvr = mfpvr();
> >     PowerPCCPUClass *pvr_pcc;
> > 
> > +#if defined(TARGET_PPC64)
> > +#ifndef CONFIG_KVM
> > +#error "CONFIG_KVM is not enabled"
> > +#endif
> 
> In meson.build:
> 
> ppc_system_ss.add(when: 'CONFIG_KVM', if_true: files('kvm.c'))
> 
> so I think this check and #error does not make sense.

You're right that kvm.c is always compiled with CONFIG_KVM enabled due
to the meson.build constraint, so the #ifndef CONFIG_KVM / #error check
can never fire. I added it as an explicit compile-time assertion to
document the assumption, but it is redundant given the build system
already enforces it. I'll drop it in the next version.

Thanks,
Amit

> 
> Regards,
> BALATON Zoltan
> 
> > +    uint32_t compat_host_pvr;
> > +
> > +    compat_host_pvr = kvm_ppc_host_compat_pvr();
> > +    if (compat_host_pvr) {
> > +        host_pvr = compat_host_pvr;
> > +    }
> > +#endif /* TARGET_PPC64 */
> > +
> >     pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
> >     if (pvr_pcc == NULL) {
> >         pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
> >