Introduce a new capability and ioctl to expose CPU compatibility modes
supported by the host processor for nested guests.

On IBM POWER systems, newer processor generations (N) can operate in
compatibility modes corresponding to earlier generations, like (N-1) and
(N-2). This is particularly relevant for nested virtualization, where
nested KVM guests may need to run with a specific processor compatibility
level.

Introduce KVM_CAP_PPC_COMPAT_CAPS capability and the corresponding
KVM_PPC_GET_COMPAT_CAPS vm ioctl. The ioctl returns a bitmap describing
the compatibility modes supported by the host in respective bit numbers,
allowing userspace (e.g., QEMU) to select an appropriate compatibility
level when configuring nested KVM guests.

The ioctl handling is added in kvm_arch_vm_ioctl() and retrieves host
CPU compatibility capabilities via a PowerPC-specific backend
implementation when available. The implementation validates the structure
size from userspace to ensure forward compatibility and returns
appropriate error codes (EINVAL for invalid size, EFAULT for copy
failures, ENOTTY if backend is not implemented). The struct
kvm_ppc_compat_caps includes a size field to support future ABI
extensions.

Suggested-by: Vaibhav Jain <[email protected]>
Signed-off-by: Amit Machhiwal <[email protected]>
---
 arch/powerpc/include/asm/kvm_ppc.h  |  1 +
 arch/powerpc/include/uapi/asm/kvm.h |  7 ++++++
 arch/powerpc/kvm/powerpc.c          | 35 +++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h            |  4 ++++
 4 files changed, 47 insertions(+)

diff --git a/arch/powerpc/include/asm/kvm_ppc.h 
b/arch/powerpc/include/asm/kvm_ppc.h
index 0953f2daa466..169ea6a7fbad 100644
--- a/arch/powerpc/include/asm/kvm_ppc.h
+++ b/arch/powerpc/include/asm/kvm_ppc.h
@@ -319,6 +319,7 @@ struct kvmppc_ops {
        bool (*hash_v3_possible)(void);
        int (*create_vm_debugfs)(struct kvm *kvm);
        int (*create_vcpu_debugfs)(struct kvm_vcpu *vcpu, struct dentry 
*debugfs_dentry);
+       int (*get_compat_caps)(struct kvm_ppc_compat_caps *host_caps);
 };
 
 extern struct kvmppc_ops *kvmppc_hv_ops;
diff --git a/arch/powerpc/include/uapi/asm/kvm.h 
b/arch/powerpc/include/uapi/asm/kvm.h
index 077c5437f521..8a38be6c3b03 100644
--- a/arch/powerpc/include/uapi/asm/kvm.h
+++ b/arch/powerpc/include/uapi/asm/kvm.h
@@ -437,6 +437,13 @@ 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 */
+};
+
 /*
  * Values for character and character_mask.
  * These are identical to the values used by H_GET_CPU_CHARACTERISTICS.
diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
index 98de68379b18..9153b0034b45 100644
--- a/arch/powerpc/kvm/powerpc.c
+++ b/arch/powerpc/kvm/powerpc.c
@@ -701,6 +701,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
                        }
                }
                break;
+#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
+       case KVM_CAP_PPC_COMPAT_CAPS:
+               r = 0;
+               if (kvmhv_on_pseries())
+                       r = 1;
+               break;
+#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
        default:
                r = 0;
                break;
@@ -2467,6 +2474,34 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int 
ioctl, unsigned long arg)
                r = kvm->arch.kvm_ops->svm_off(kvm);
                break;
        }
+       case KVM_PPC_GET_COMPAT_CAPS: {
+               struct kvm_ppc_compat_caps host_caps;
+               u64 user_size;
+
+               r = -EFAULT;
+               /* First, get the size field from userspace to validate */
+               if (copy_from_user(&user_size, &((struct kvm_ppc_compat_caps
+                    __user *)argp)->size, sizeof(user_size))) {
+                       goto out;
+               }
+
+               /* Validate size - must be at least the current structure size 
*/
+               r = -EINVAL;
+               if (user_size < sizeof(host_caps))
+                       goto out;
+
+               r = -ENOTTY;
+               memset(&host_caps, 0, sizeof(host_caps));
+               if (!kvm->arch.kvm_ops->get_compat_caps)
+                       goto out;
+
+               r = kvm->arch.kvm_ops->get_compat_caps(&host_caps);
+               /* Set the actual size of the structure we're returning */
+               host_caps.size = sizeof(host_caps);
+               if (!r && copy_to_user(argp, &host_caps, sizeof(host_caps)))
+                       r = -EFAULT;
+               break;
+       }
        default: {
                struct kvm *kvm = filp->private_data;
                r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 6c8afa2047bf..1788a0068662 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -996,6 +996,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 249
 
 struct kvm_irq_routing_irqchip {
        __u32 irqchip;
@@ -1349,6 +1350,9 @@ struct kvm_s390_keyop {
 #define KVM_GET_DEVICE_ATTR      _IOW(KVMIO,  0xe2, struct kvm_device_attr)
 #define KVM_HAS_DEVICE_ATTR      _IOW(KVMIO,  0xe3, struct kvm_device_attr)
 
+/* Available with KVM_CAP_PPC_COMPAT_CAPS */
+#define KVM_PPC_GET_COMPAT_CAPS        _IOR(KVMIO,  0xe4, struct 
kvm_ppc_compat_caps)
+
 /*
  * ioctls for vcpu fds
  */
-- 
2.50.1 (Apple Git-155)


Reply via email to