ppc/kvm: Handle CPU compatibility mode correctly for nested guests
v3 cover ppc/kvm: Handle CPU compatibility mode correctly for nested guests
On POWER systems, newer processor generations can operate in compatibility modes corresponding to earlier generations (e.g., a Power11 system running in Power10 compatibility mode). In such cases, the effective CPU level exposed to guests differs from the physical processor generation.
This creates issues for nested virtualization. When booting a nested KVM guest, QEMU may derive the CPU model from the raw hardware PVR and attempt to configure the guest accordingly. However, the host is constrained by the compatibility level negotiated with the hypervisor, and requests exceeding that level are rejected by KVM, leading to guest boot failures such as:
KVM-NESTEDv2: couldn't set guest wide elements
This series addresses the issue by preventing fallback to raw mode when the host itself is booted in a compatibility mode, and by querying the effective CPU compatibility modes supported by the host via KVM. The implementation includes size field validation for ABI compatibility between QEMU and the kernel. With these changes, QEMU ensures that nested guests are configured with CPU models consistent with the host compatibility mode, allowing them to boot correctly.
Patch summary:
[1/3] [DO_NOT_MERGE] linux-headers: Add uapi header changes [2/3] target/ppc/kvm: Add support for querying host compatibility mode [3/3] target/ppc/kvm: Use host compatibility mode for nested guests
Changes in v3 (based on review from Vaibhav):
- Patch 1: Moved compatibility mode check from do_client_architecture_support() to cas_check_pvr(). Instead of error handling when KVM rejects compat mode, now prevents raw mode fallback when host is booted in compatibility mode. This ensures guests cannot exceed the host's compatibility level.
- Patch 2: Added size field to kvm_ppc_compat_caps structure for ABI versioning. Changed flags field from __u32 to __u64. Added capability bit definitions (KVM_PPC_COMPAT_CAP_POWER9/10/11) and KVM_PPC_COMPAT_BITMASK.
- Patch 3: Added size field initialization and validation in kvmppc_get_compat_caps() to ensure ABI compatibility between QEMU and kernel. Changed from H_GUEST_CAP_* constants to KVM_PPC_COMPAT_CAP_* constants. Added capability masking with KVM_PPC_COMPAT_BITMASK.
- Dropped Tested-by from Anushree due to code changes in v3.
Changes in v2:
- Patch 3: Guard compatibility mode code with #if defined(TARGET_PPC64) to fix compilation for ppc32 targets. The POWER9/10/11 PVR constants are only defined for 64-bit builds, and compatibility modes are only relevant for 64-bit systems.
Testing (with kernel v4 patches):
KVM APIv1 Testing
=================
On P10 PowerNV machine (L0)
---------------------------
- P10 L1 KVM guest -> works
- P10 nested L2 KVM guest -> works
- P9 compat nested L2 KVM guest -> works
- P9 compat L1 KVM guest -> works
- P9 nested L2 KVM guest -> works
On Powernv11 TCG Guest (L0)
---------------------------
- P11 L1 KVM guest -> works
- P11 L2 KVM guest -> works
- P10 compat L1 KVM guest -> works
- P10 L2 KVM guest -> works
- P9 compat L1 KVM guest -> works
- P9 L2 KVM guest -> works
KVM APIv2 Testing
=================
On P11 PowerVM LPAR (L1)
------------------------
- P11 L2 KVM guest -> works
- P10 compat L2 KVM guest -> works
On P11 LPAR in P10 compat (L1)
------------------------------
- P10 (host compat) L2 KVM guest -> works
On P10 PowerVM LPAR (L1)
------------------------
- P10 L2 KVM guest -> works
CI test results: https://gitlab.com/amachhiw/qemu/-/pipelines/2604638350
Note: Patch 1 is marked DO_NOT_MERGE as it contains linux-headers updates that will be synced separately once the corresponding kernel patches are merged.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
The corresponding Linux kernel patches (v4) are being posted concurrently.
Previous kernel patch versions:
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Amit Machhiwal (3):
linux-headers: Add uapi header changes target/ppc/kvm: Add support for querying host compatibility mode target/ppc/kvm: Use host compatibility mode for nested guests
hw/ppc/spapr_hcall.c | 14 +++++++ linux-headers/asm-powerpc/kvm.h | 18 ++++++++ linux-headers/linux/kvm.h | 3 ++ target/ppc/kvm.c | 74 +++++++++++++++++++++++++++++++++ target/ppc/kvm_ppc.h | 16 +++++++ 5 files changed, 125 insertions(+)
base-commit: 2f28d34ea0aead9830478cd1d3d0dd9d9191d82e
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v3 1/3 linux-headers: Add uapi header changes
This is a temporary patch intended for review and testing purposes only. It syncs the QEMU linux-headers with the kernel changes that introduce the KVM_PPC_GET_COMPAT_CAPS ioctl for querying host CPU compatibility capabilities. This includes the kvm_ppc_compat_caps structure with a size field for ABI versioning, the KVM_CAP_PPC_COMPAT_CAPS capability definition, the KVM_PPC_GET_COMPAT_CAPS ioctl definition, and capability bit definitions for POWER9, POWER10, and POWER11 compatibility modes.
The actual header sync will be done via scripts/update-linux-headers.sh once the kernel changes [1] are merged upstream.
[1] https://lore.kernel.org/all/[email protected]/
--- linux-headers/asm-powerpc/kvm.h | 18 ++++++++++++++++++ linux-headers/linux/kvm.h | 3 +++ 2 files changed, 21 insertions(+)
linux-headers/asm-powerpc/kvm.h +18 -0
@@ -437,6 +437,24 @@ struct kvm_ppc_cpu_char {__u64 behaviour_mask;/* valid bits in behaviour */ }; +/* For KVM_PPC_GET_COMPAT_CAPS */ +struct kvm_ppc_compat_caps { +__u64 flags;/* Reserved for future use */ +__u64 size;/* Size of this structure */ +__u64 compat_capabilities;/* Capabilities supported by the host */ +}; +/* +* Capability bits for compat_capabilities field in kvm_ppc_compat_caps. +* These bits indicate which processor compatibility modes are supported. +*/ +#define KVM_PPC_COMPAT_CAP_POWER9 (1ULL << 62) +#define KVM_PPC_COMPAT_CAP_POWER10 (1ULL << 61) +#define KVM_PPC_COMPAT_CAP_POWER11 (1ULL << 60) +#define KVM_PPC_COMPAT_BITMASK (KVM_PPC_COMPAT_CAP_POWER9 | \ + KVM_PPC_COMPAT_CAP_POWER10 | \ + KVM_PPC_COMPAT_CAP_POWER11) + +/* * Values for character and character_mask. * These are identical to the values used by H_GET_CPU_CHARACTERISTICS.
linux-headers/linux/kvm.h +3 -0
@@ -985,6 +985,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_PPC_COMPAT_CAPS 249struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1329,6 +1330,8 @@ struct kvm_s390_keyop {/* Available with KVM_CAP_COUNTER_OFFSET */ #define KVM_ARM_SET_COUNTER_OFFSET _IOW(KVMIO, 0xb5, struct kvm_arm_counter_offset)#define KVM_ARM_GET_REG_WRITABLE_MASKS _IOR(KVMIO, 0xb6, struct reg_mask_range) +/* Available with KVM_CAP_PPC_COMPAT_CAPS */ +#define KVM_PPC_GET_COMPAT_CAPS _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps)/* ioctl for vm fd */ #define KVM_CREATE_DEVICE _IOWR(KVMIO, 0xe0, struct kvm_create_device)
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v3 2/3 target/ppc/kvm: Add support for querying host compatibility mode
In reply to Amit MachhiwalAdd infrastructure to query the host CPU compatibility mode via the KVM_PPC_GET_COMPAT_CAPS ioctl. This allows QEMU to determine if the host is running in a compatibility mode (e.g., a Power11 processor operating in Power10 compatibility mode).
The kvmppc_get_compat_caps() function queries KVM for host compatibility capabilities with size field validation for ABI compatibility between QEMU and the kernel. The kvm_ppc_host_compat_pvr() function derives the effective PVR based on the compatibility mode reported by KVM.
Additionally, cas_check_pvr() in hw/ppc/spapr_hcall.c is updated to prevent fallback to raw mode when the host is running in compatibility mode. This ensures that nested guests cannot exceed the host's compatibility level.
If the capability is not supported or the query fails, the functions return 0, allowing fallback to existing behavior.
--- hw/ppc/spapr_hcall.c | 14 ++++++++++ target/ppc/kvm.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ target/ppc/kvm_ppc.h | 16 +++++++++++ 3 files changed, 95 insertions(+)
hw/ppc/spapr_hcall.c +14 -0
@@ -1135,6 +1135,7 @@ static uint32_t cas_check_pvr(PowerPCCPU *cpu, uint32_t max_compat, {bool explicit_match = false;/* Matched the CPU's real PVR */ uint32_t best_compat =0 ; +uint32_t compat_host_pvr =0 ;int i;/*
@@ -1162,6 +1163,19 @@ static uint32_t cas_check_pvr(PowerPCCPU *cpu, uint32_t max_compat, } } +if (explicit_match) { + compat_host_pvr =kvm_ppc_host_compat_pvr (); +/* +* If the host is booted in a compatibility mode, do not try booting in +* the raw mode as it may allow KVM guests to boot with a higher CPU +* version compared to what host was booted with; which should not be +* allowed. +*/ +if (compat_host_pvr) { + explicit_match = false; + } + } + *raw_mode_supported = explicit_match;/* Parsing finished */
target/ppc/kvm.c +65 -0
71 lines not on this page · raw message
target/ppc/kvm_ppc.h +16 -0
@@ -81,6 +81,17 @@ bool kvmppc_supports_ail_3(void);int kvmppc_enable_hwrng (void );int kvmppc_put_books_sregs (PowerPCCPU *cpu);PowerPCCPUClass *kvm_ppc_get_host_cpu_class (void ); + +/* +* Bit position indices for KVM_PPC_COMPAT_CAP_* capabilities. +* These represent the result of (63 - bit_number) for each capability bit. +* Used for identifying which compatibility mode is supported by the host. +*/ +#define KVM_PPC_COMPAT_CAP_P9_IDX 1/* 63 - 62 */ +#define KVM_PPC_COMPAT_CAP_P10_IDX 2/* 63 - 61 */ +#define KVM_PPC_COMPAT_CAP_P11_IDX 3/* 63 - 60 */ + +uint32_t kvm_ppc_host_compat_pvr (void );void kvmppc_check_papr_resize_hpt (Error **errp);int kvmppc_resize_hpt_prepare (PowerPCCPU *cpu,target_ulong flags,int shift);int kvmppc_resize_hpt_commit (PowerPCCPU *cpu,target_ulong flags,int shift);
@@ -440,6 +451,11 @@ static inline PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)return NULL ; } +static inline uint32_t kvm_ppc_host_compat_pvr (void ) +{ +return 0 ; +} +static inline void kvmppc_check_papr_resize_hpt (Error **errp) { }
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v3 2/3] target/ppc/kvm: Add support for querying host compatibility mode
Quoted 62 lines · Amit Machhiwal
Add infrastructure to query the host CPU compatibility mode via the
KVM_PPC_GET_COMPAT_CAPSioctl. This allows QEMU to determine if the host is running in a compatibility mode (e.g., a Power11 processor operating in Power10 compatibility mode).The
kvmppc_get_compat_caps()function queries KVM for host compatibility capabilities with size field validation for ABI compatibility between QEMU and the kernel. Thekvm_ppc_host_compat_pvr()function derives the effective PVR based on the compatibility mode reported by KVM.Additionally,
cas_check_pvr()inhw/ppc/spapr_hcall.cis updated to prevent fallback to raw mode when the host is running in compatibility mode. This ensures that nested guests cannot exceed the host's compatibility level.If the capability is not supported or the query fails, the functions return 0, allowing fallback to existing behavior.
Signed-off-by Amit Machhiwal <[email protected]>--- hw/ppc/spapr_hcall.c | 14 ++++++++++ target/ppc/kvm.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ target/ppc/kvm_ppc.h | 16 +++++++++++ 3 files changed, 95 insertions(+)hw/ppc/spapr_hcall.c +14
-0@@ -1135,6 +1135,7 @@ static uint32_t cas_check_pvr(PowerPCCPU *cpu, uint32_t max_compat, {bool explicit_match = false;/* Matched the CPU's real PVR */ uint32_t best_compat =0 ; +uint32_t compat_host_pvr =0 ;int i;/* @@ -1162,6 +1163,19 @@ static uint32_t cas_check_pvr(PowerPCCPU *cpu, uint32_t max_compat, } } +if (explicit_match) { + compat_host_pvr =kvm_ppc_host_compat_pvr (); +/* +* If the host is booted in a compatibility mode, do not try booting in +* the raw mode as it may allow KVM guests to boot with a higher CPU +* version compared to what host was booted with; which should not be +* allowed. +*/ +if (compat_host_pvr) { + explicit_match = false; + } + } + *raw_mode_supported = explicit_match;/* Parsing finished */ target/ppc/kvm.c +0
-0
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2605
Quoted 4 lines
target/ppc/kvm.c +1
-0@@ -2602,6 +2602,71 @@ bool kvmppc_supports_ail_3(void)return cap_ail_mode_3; } +#if defined(TARGET_PPC64 )
This define would be implicit for this file. I think you should target these functions for CONFIG_KVM. Otherwise you will have multiple definitions of kvm_ppc_host_compat_pvr()
Quoted 7 lines
target/ppc/kvm.c +7
-0+static target_ulong kvmppc_get_compat_caps (void ) +{ +struct kvm_ppc_compat_caps host_compat; +target_ulong host_caps; +int ret; + +if (!kvm_check_extension (kvm_state,KVM_CAP_PPC_COMPAT_CAPS )) {
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2622
Quoted 10 lines
target/ppc/kvm.c +10
-0+return 0 ; + } + +/* Initialize the structure with size field for forward compatibility */ +memset (&host_compat,0 ,sizeof (host_compat)); + host_compat.size =sizeof (host_compat); + + ret =kvm_vm_ioctl (kvm_state,KVM_PPC_GET_COMPAT_CAPS , &host_compat); +if (ret <0 ) { +fprintf (stderr,"KVM: failed to get host capabilities\n" );
Should use error_report instead of fprintf(stderr,..
target/ppc/kvm.c bool kvmppc_supports_ail_3(void)
Quoted 10 lines
target/ppc/kvm.c +10
-0+return 0 ; + } + +/* +* Validate the returned size matches our structure size. +* The kernel validates that userspace provides sufficient size before +* the ioctl, and returns its own structure size. A mismatch indicates +* a version incompatibility between QEMU and the kernel. +*/ +if (host_compat.size !=sizeof (host_compat))
This error handling differs from the kernel code which can indeed return host_compat.size < sizeof(host_compat).
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2634
Quoted 2 lines
target/ppc/kvm.c +2
-0+fprintf (stderr,"KVM: host_compat size mismatch (expected %zu, got %lu)\n" , +sizeof (host_compat), host_compat.size);
Error message can be more user friendly. Also your should use error_report instead of fprintf(stderr,..
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2639
Quoted 6 lines
target/ppc/kvm.c +5
-0+return 0 ; + } + + host_caps = host_compat.compat_capabilities; +return host_caps;
Minor: Why not just return host_compat.compat_capabilities instead of this extra store
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2642
Quoted 2 lines
target/ppc/kvm.c +2
-0+} +
Minor: Minor comment about this function would be nice to have
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2650
Quoted 9 lines
target/ppc/kvm.c +9
-0+uint32_t kvm_ppc_host_compat_pvr (void ) +{ +uint32_t compat_host_pvr =0 ; +int cap_idx =0 ; +target_ulong host_caps =kvmppc_get_compat_caps (); + + host_caps = host_caps &KVM_PPC_COMPAT_BITMASK ; +if (host_caps) { + cap_idx =63 -__builtin_ctzll (host_caps);
you probably need to use clz64() instead of this.
Quoted 29 lines
target/ppc/kvm.c +19
-0+switch (cap_idx) { +case KVM_PPC_COMPAT_CAP_P9_IDX : + compat_host_pvr =CPU_POWERPC_POWER9_DD22 ; +break ; +case KVM_PPC_COMPAT_CAP_P10_IDX : + compat_host_pvr =CPU_POWERPC_POWER10_DD20 ; +break ; +case KVM_PPC_COMPAT_CAP_P11_IDX : + compat_host_pvr =CPU_POWERPC_POWER11_DD20 ; +break ; +default : +break ; + } + } + +return compat_host_pvr; +} +#endif /* TARGET_PPC64 */ +PowerPCCPUClass *kvm_ppc_get_host_cpu_class (void ) {uint32_t host_pvr = mfpvr();target/ppc/kvm_ppc.h +0
-0@@ -81,6 +81,17 @@ bool kvmppc_supports_ail_3(void);int kvmppc_enable_hwrng (void );int kvmppc_put_books_sregs (PowerPCCPU *cpu);
target/ppc/kvm_ppc.h bool kvmppc_supports_ail_3(void); · line 92
Quoted 10 lines
target/ppc/kvm_ppc.h +9
-0PowerPCCPUClass *kvm_ppc_get_host_cpu_class (void ); + +/* +* Bit position indices for KVM_PPC_COMPAT_CAP_* capabilities. +* These represent the result of (63 - bit_number) for each capability bit. +* Used for identifying which compatibility mode is supported by the host. +*/ +#define KVM_PPC_COMPAT_CAP_P9_IDX 1/* 63 - 62 */ +#define KVM_PPC_COMPAT_CAP_P10_IDX 2/* 63 - 61 */ +#define KVM_PPC_COMPAT_CAP_P11_IDX 3/* 63 - 60 */
Can these be derived from KVM_PPC_COMPAT_CAP_POWERXX
Also if these defines are only used in kvm_ppc_host_compat_pvr() then should be defined near to it.
Quoted 20 lines
target/ppc/kvm_ppc.h +7
-0+ +uint32_t kvm_ppc_host_compat_pvr (void );void kvmppc_check_papr_resize_hpt (Error **errp);int kvmppc_resize_hpt_prepare (PowerPCCPU *cpu,target_ulong flags,int shift);int kvmppc_resize_hpt_commit (PowerPCCPU *cpu,target_ulong flags,int shift);@@ -440,6 +451,11 @@ static inline PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)return NULL ; } +static inline uint32_t kvm_ppc_host_compat_pvr (void ) +{ +return 0 ; +} +static inline void kvmppc_check_papr_resize_hpt (Error **errp) { }Signature · 1 line
2.50.1 (Apple Git-155)
Signature · 2 lines
Cheers ~ Vaibhav
Re: [PATCH v3 2/3] target/ppc/kvm: Add support for querying host compatibility mode
Hi Vaibhav,
Thanks for reviewing the patch. Please find my reponse inline.
Quoted 67 lines · Vaibhav Jain
Quoted 65 lines · Amit Machhiwal
Add infrastructure to query the host CPU compatibility mode via the
KVM_PPC_GET_COMPAT_CAPSioctl. This allows QEMU to determine if the host is running in a compatibility mode (e.g., a Power11 processor operating in Power10 compatibility mode).The
kvmppc_get_compat_caps()function queries KVM for host compatibility capabilities with size field validation for ABI compatibility between QEMU and the kernel. Thekvm_ppc_host_compat_pvr()function derives the effective PVR based on the compatibility mode reported by KVM.Additionally,
cas_check_pvr()inhw/ppc/spapr_hcall.cis updated to prevent fallback to raw mode when the host is running in compatibility mode. This ensures that nested guests cannot exceed the host's compatibility level.If the capability is not supported or the query fails, the functions return 0, allowing fallback to existing behavior.
Signed-off-by Amit Machhiwal <[email protected]>--- hw/ppc/spapr_hcall.c | 14 ++++++++++ target/ppc/kvm.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ target/ppc/kvm_ppc.h | 16 +++++++++++ 3 files changed, 95 insertions(+)hw/ppc/spapr_hcall.c +14
-0@@ -1135,6 +1135,7 @@ static uint32_t cas_check_pvr(PowerPCCPU *cpu, uint32_t max_compat, {bool explicit_match = false;/* Matched the CPU's real PVR */ uint32_t best_compat =0 ; +uint32_t compat_host_pvr =0 ;int i;/* @@ -1162,6 +1163,19 @@ static uint32_t cas_check_pvr(PowerPCCPU *cpu, uint32_t max_compat, } } +if (explicit_match) { + compat_host_pvr =kvm_ppc_host_compat_pvr (); +/* +* If the host is booted in a compatibility mode, do not try booting in +* the raw mode as it may allow KVM guests to boot with a higher CPU +* version compared to what host was booted with; which should not be +* allowed. +*/ +if (compat_host_pvr) { + explicit_match = false; + } + } + *raw_mode_supported = explicit_match;/* Parsing finished */ target/ppc/kvm.c +0
-0@@ -2602,6 +2602,71 @@ bool kvmppc_supports_ail_3(void)return cap_ail_mode_3; }
Quoted 4 lines
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2605
Quoted 1 line
target/ppc/kvm.c +1
-0+#if defined(TARGET_PPC64 )This define would be implicit for this file. I think you should target these functions for
CONFIG_KVM. Otherwise you will have multiple definitions ofkvm_ppc_host_compat_pvr()
No, this is not implicit. I had fixed a ppc32 build breakage for ppc32 in v2 of this series with these guards (the same is also documented in the cover letter).
I believe explicit check for CONFIG_KVM is redundant as target/ppc/kvm.c is only compiled when CONFIG_KVM=y (please take a look at target/ppc/meson.build:54):
[...]
ppc_system_ss.add(when: 'CONFIG_KVM', if_true: files('kvm.c'))
Quoted 11 lines
Quoted 11 lines
target/ppc/kvm.c +10
-0+static target_ulong kvmppc_get_compat_caps (void ) +{ +struct kvm_ppc_compat_caps host_compat; +target_ulong host_caps; +int ret; + +if (!kvm_check_extension (kvm_state,KVM_CAP_PPC_COMPAT_CAPS )) { +return 0 ; + } +
Quoted 8 lines
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2622
Quoted 7 lines
target/ppc/kvm.c +7
-0+/* Initialize the structure with size field for forward compatibility */ +memset (&host_compat,0 ,sizeof (host_compat)); + host_compat.size =sizeof (host_compat); + + ret =kvm_vm_ioctl (kvm_state,KVM_PPC_GET_COMPAT_CAPS , &host_compat); +if (ret <0 ) { +fprintf (stderr,"KVM: failed to get host capabilities\n" );Should use
error_reportinstead of fprintf(stderr,..
Good catch and my bad. I intended to replace these with error_report() after my testing but missed to do so. I'll fix this in the next version.
Quoted 10 lines
target/ppc/kvm.c bool kvmppc_supports_ail_3(void)
Quoted 10 lines
target/ppc/kvm.c +10
-0+return 0 ; + } + +/* +* Validate the returned size matches our structure size. +* The kernel validates that userspace provides sufficient size before +* the ioctl, and returns its own structure size. A mismatch indicates +* a version incompatibility between QEMU and the kernel. +*/ +if (host_compat.size !=sizeof (host_compat))
Quoted 2 lines
This error handling differs from the kernel code which can indeed return host_compat.size <
sizeof(host_compat).
You are right. I Will unify this check.
Quoted 4 lines
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2634
Quoted 2 lines
target/ppc/kvm.c +2
-0+fprintf (stderr,"KVM: host_compat size mismatch (expected %zu, got %lu)\n" , +sizeof (host_compat), host_compat.size);Error message can be more user friendly. Also your should use
error_reportinstead of fprintf(stderr,..
Will fix this and think about a better error message.
Quoted 8 lines
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2639
Quoted 6 lines
target/ppc/kvm.c +5
-0+return 0 ; + } + + host_caps = host_compat.compat_capabilities; +return host_caps;Minor: Why not just return host_compat.compat_capabilities instead of this extra store
Yeah, removing this extra store makes sense. I'll fix this.
Quoted 3 lines
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2642
Quoted 2 lines
target/ppc/kvm.c +2
-0+} +Minor: Minor comment about this function would be nice to have
Sure, will do.
Quoted 10 lines
target/ppc/kvm.c bool kvmppc_supports_ail_3(void) · line 2650
Quoted 9 lines
target/ppc/kvm.c +9
-0+uint32_t kvm_ppc_host_compat_pvr (void ) +{ +uint32_t compat_host_pvr =0 ; +int cap_idx =0 ; +target_ulong host_caps =kvmppc_get_compat_caps (); + + host_caps = host_caps &KVM_PPC_COMPAT_BITMASK ; +if (host_caps) { + cap_idx =63 -__builtin_ctzll (host_caps);you probably need to use
clz64()instead of this.
I agree that __builtin_ctzll() should be replaced with QEMU's portable function for consistency and safety, but clz64() would be incorrect here. The code needs to find the lowest set bit (rightmost in PowerPC MSB-0 numbering), not the highest.
For example, with host_caps = 0x3000000000000000 which advertises the support for Power10 and Power11 via bit 2 and bit 3 respectively:
- Using clz64(): clz64(0x3000000000000000) = 2 (wrong - gives bit 2)
- Using ctz64(): 63 - ctz64(0x3000000000000000) = 63 - 60 = 3 (correct)
The correct fix is to use ctz64() instead:
cap_idx = 63 - ctz64(host_caps);
This maintains the logic while using QEMU's portable bit manipulation function from qemu/host-utils.h, which also handles the zero-input case safely (though we already check for non-zero host_caps).
Quoted 33 lines
Quoted 33 lines
target/ppc/kvm.c +19
-0+switch (cap_idx) { +case KVM_PPC_COMPAT_CAP_P9_IDX : + compat_host_pvr =CPU_POWERPC_POWER9_DD22 ; +break ; +case KVM_PPC_COMPAT_CAP_P10_IDX : + compat_host_pvr =CPU_POWERPC_POWER10_DD20 ; +break ; +case KVM_PPC_COMPAT_CAP_P11_IDX : + compat_host_pvr =CPU_POWERPC_POWER11_DD20 ; +break ; +default : +break ; + } + } + +return compat_host_pvr; +} +#endif /* TARGET_PPC64 */ +PowerPCCPUClass *kvm_ppc_get_host_cpu_class (void ) {uint32_t host_pvr = mfpvr();target/ppc/kvm_ppc.h +2
-0@@ -81,6 +81,17 @@ bool kvmppc_supports_ail_3(void);int kvmppc_enable_hwrng (void );int kvmppc_put_books_sregs (PowerPCCPU *cpu);PowerPCCPUClass *kvm_ppc_get_host_cpu_class (void ); + +/*
Quoted 8 lines
target/ppc/kvm_ppc.h bool kvmppc_supports_ail_3(void); · line 92
Quoted 5 lines
target/ppc/kvm_ppc.h +5
-0+* Bit position indices for KVM_PPC_COMPAT_CAP_* capabilities. +* These represent the result of (63 - bit_number) for each capability bit. +#define KVM_PPC_COMPAT_CAP_P9_IDX 1 /* 63 - 62 */ +#define KVM_PPC_COMPAT_CAP_P10_IDX 2/* 63 - 61 */ +#define KVM_PPC_COMPAT_CAP_P11_IDX 3/* 63 - 60 */ Can these be derived from
KVM_PPC_COMPAT_CAP_POWERXX
Also if these defines are only used inkvm_ppc_host_compat_pvr()then should be defined near to it.
Deriving these would require using ctz64() in the preprocessor, which isn't possible since it's a function, not a macro. I tested this:
#define KVM_PPC_COMPAT_CAP_P9_IDX (63 - ctz64(KVM_PPC_COMPAT_CAP_POWER9))
And got a compilation error:
error: case label does not reduce to an integer constant
This is because switch/case statements require compile-time constants, but ctz64() is a runtime function call.
Explicit bitmap index defines are already used elsewhere in the codebase. For example, H_GUEST_CAP_XX_MODE_BMAP in hw/ppc/spapr_nested.h.
Also, I prefer keeping these in the header as they represent the KVM compatibility capability bitmap layout and could be useful for other code in the future for any other KVM operations that need to interpret these capabilities. But I don't really have any strong feelings about this. Please let me know your views.
Thanks,
Amit
Quoted 25 lines
Quoted 21 lines
target/ppc/kvm_ppc.h +7
-0+ +uint32_t kvm_ppc_host_compat_pvr (void );void kvmppc_check_papr_resize_hpt (Error **errp);int kvmppc_resize_hpt_prepare (PowerPCCPU *cpu,target_ulong flags,int shift);int kvmppc_resize_hpt_commit (PowerPCCPU *cpu,target_ulong flags,int shift);@@ -440,6 +451,11 @@ static inline PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)return NULL ; } +static inline uint32_t kvm_ppc_host_compat_pvr (void ) +{ +return 0 ; +} +static inline void kvmppc_check_papr_resize_hpt (Error **errp) { }Signature · 1 line
2.50.1 (Apple Git-155)Signature · 2 lines
Cheers ~ Vaibhav
PATCH v3 3/3 target/ppc/kvm: Use host compatibility mode for nested guests
In reply to Amit MachhiwalOn 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.
--- target/ppc/kvm.c | 9 +++++++++ 1 file changed, 9 insertions(+)
target/ppc/kvm.c +9 -0
@@ -2672,6 +2672,15 @@ PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)uint32_t host_pvr =mfpvr ();PowerPCCPUClass *pvr_pcc; +#if defined(TARGET_PPC64 ) +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);
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v3 3/3] target/ppc/kvm: Use host compatibility mode for nested guests
Hi Amit,
Thanks for the patch. My review comments inline below:
Quoted 25 lines · Amit Machhiwal
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 usingkvm_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.Signed-off-by Amit Machhiwal <[email protected]>--- target/ppc/kvm.c | 9 +++++++++ 1 file changed, 9 insertions(+)target/ppc/kvm.c +0
-0
target/ppc/kvm.c PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void) · line 2675
Quoted 4 lines
target/ppc/kvm.c +1
-0@@ -2672,6 +2672,15 @@ PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)uint32_t host_pvr =mfpvr ();PowerPCCPUClass *pvr_pcc; +#if defined(TARGET_PPC64 )
Use CONFIG_KVM
Quoted 15 lines
target/ppc/kvm.c +8
-0+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);Signature · 1 line
2.50.1 (Apple Git-155)
Signature · 2 lines
Cheers ~ Vaibhav
Re: [PATCH v3 3/3] target/ppc/kvm: Use host compatibility mode for nested guests
Hi Vaibhav,
Thanks for reviewing the patch. Please find my response below.
Quoted 34 lines · Vaibhav Jain
Hi Amit,
Thanks for the patch. My review comments inline below:
Quoted 28 lines · Amit Machhiwal
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 usingkvm_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.Signed-off-by Amit Machhiwal <[email protected]>--- target/ppc/kvm.c | 9 +++++++++ 1 file changed, 9 insertions(+)target/ppc/kvm.c +0
-0@@ -2672,6 +2672,15 @@ PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)uint32_t host_pvr =mfpvr ();PowerPCCPUClass *pvr_pcc;
Quoted 2 lines
target/ppc/kvm.c PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void) · line 2675
Quoted 1 line
target/ppc/kvm.c +1
-0+#if defined(TARGET_PPC64 )Use
CONFIG_KVM
As I mentioned in the response to previous patch's review comments, the TARGET_PPC64 guard is needed to fix ppc32 build breakage, and CONFIG_KVM would be redundant since target/ppc/kvm.c is only compiled when CONFIG_KVM=y.
Thanks,
Amit
Quoted 20 lines
Quoted 16 lines
target/ppc/kvm.c +8
-0+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);Signature · 1 line
2.50.1 (Apple Git-155)Signature · 2 lines
Cheers ~ Vaibhav