KVM: s390: Add UV feature negotiation
authorSteffen Eiden <seiden@linux.ibm.com>
Tue, 15 Aug 2023 15:14:14 +0000 (17:14 +0200)
committerJanosch Frank <frankja@linux.ibm.com>
Mon, 28 Aug 2023 09:27:55 +0000 (09:27 +0000)
Add a uv_feature list for pv-guests to the KVM cpu-model.
The feature bits 'AP-interpretation for secure guests' and
'AP-interrupt for secure guests' are available.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Janosch Frank <frankja@linux.ibm.com>
Reviewed-by: Michael Mueller <mimu@linux.ibm.com>
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
Link: https://lore.kernel.org/r/20230815151415.379760-4-seiden@linux.ibm.com
Message-Id: <20230815151415.379760-4-seiden@linux.ibm.com>

arch/s390/include/asm/kvm_host.h
arch/s390/include/uapi/asm/kvm.h
arch/s390/kvm/kvm-s390.c

index 91bfecb..427f952 100644 (file)
@@ -817,6 +817,8 @@ struct kvm_s390_cpu_model {
        __u64 *fac_list;
        u64 cpuid;
        unsigned short ibc;
+       /* subset of available UV-features for pv-guests enabled by user space */
+       struct kvm_s390_vm_cpu_uv_feat uv_feat_guest;
 };
 
 typedef int (*crypto_hook)(struct kvm_vcpu *vcpu);
index a73cf01..abe926d 100644 (file)
@@ -159,6 +159,22 @@ struct kvm_s390_vm_cpu_subfunc {
        __u8 reserved[1728];
 };
 
+#define KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST        6
+#define KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST  7
+
+#define KVM_S390_VM_CPU_UV_FEAT_NR_BITS        64
+struct kvm_s390_vm_cpu_uv_feat {
+       union {
+               struct {
+                       __u64 : 4;
+                       __u64 ap : 1;           /* bit 4 */
+                       __u64 ap_intr : 1;      /* bit 5 */
+                       __u64 : 58;
+               };
+               __u64 feat;
+       };
+};
+
 /* kvm attributes for crypto */
 #define KVM_S390_VM_CRYPTO_ENABLE_AES_KW       0
 #define KVM_S390_VM_CRYPTO_ENABLE_DEA_KW       1
index 813cc3d..b3f17e0 100644 (file)
@@ -1531,6 +1531,39 @@ static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
        return 0;
 }
 
+#define KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK     \
+(                                              \
+       ((struct kvm_s390_vm_cpu_uv_feat){      \
+               .ap = 1,                        \
+               .ap_intr = 1,                   \
+       })                                      \
+       .feat                                   \
+)
+
+static int kvm_s390_set_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
+{
+       struct kvm_s390_vm_cpu_uv_feat __user *ptr = (void __user *)attr->addr;
+       unsigned long data, filter;
+
+       filter = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
+       if (get_user(data, &ptr->feat))
+               return -EFAULT;
+       if (!bitmap_subset(&data, &filter, KVM_S390_VM_CPU_UV_FEAT_NR_BITS))
+               return -EINVAL;
+
+       mutex_lock(&kvm->lock);
+       if (kvm->created_vcpus) {
+               mutex_unlock(&kvm->lock);
+               return -EBUSY;
+       }
+       kvm->arch.model.uv_feat_guest.feat = data;
+       mutex_unlock(&kvm->lock);
+
+       VM_EVENT(kvm, 3, "SET: guest UV-feat: 0x%16.16lx", data);
+
+       return 0;
+}
+
 static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
 {
        int ret = -ENXIO;
@@ -1545,6 +1578,9 @@ static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
        case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
                ret = kvm_s390_set_processor_subfunc(kvm, attr);
                break;
+       case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
+               ret = kvm_s390_set_uv_feat(kvm, attr);
+               break;
        }
        return ret;
 }
@@ -1777,6 +1813,33 @@ static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
        return 0;
 }
 
+static int kvm_s390_get_processor_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
+{
+       struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
+       unsigned long feat = kvm->arch.model.uv_feat_guest.feat;
+
+       if (put_user(feat, &dst->feat))
+               return -EFAULT;
+       VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
+
+       return 0;
+}
+
+static int kvm_s390_get_machine_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
+{
+       struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
+       unsigned long feat;
+
+       BUILD_BUG_ON(sizeof(*dst) != sizeof(uv_info.uv_feature_indications));
+
+       feat = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
+       if (put_user(feat, &dst->feat))
+               return -EFAULT;
+       VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
+
+       return 0;
+}
+
 static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
 {
        int ret = -ENXIO;
@@ -1800,6 +1863,12 @@ static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
        case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
                ret = kvm_s390_get_machine_subfunc(kvm, attr);
                break;
+       case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
+               ret = kvm_s390_get_processor_uv_feat(kvm, attr);
+               break;
+       case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
+               ret = kvm_s390_get_machine_uv_feat(kvm, attr);
+               break;
        }
        return ret;
 }
@@ -1952,6 +2021,8 @@ static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
                case KVM_S390_VM_CPU_MACHINE_FEAT:
                case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
                case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
+               case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
+               case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
                        ret = 0;
                        break;
                default:
@@ -3296,6 +3367,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
        kvm->arch.model.ibc = sclp.ibc & 0x0fff;
 
+       kvm->arch.model.uv_feat_guest.feat = 0;
+
        kvm_s390_crypto_init(kvm);
 
        if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {