[PATCH v2 12/17] KVM: arm64: Support async PF ioctl commands

From: Gavin Shan
Date: Tue Feb 09 2021 - 00:10:35 EST


This supports ioctl commands for configuration and migration:

KVM_ARM_ASYNC_PF_CMD_GET_VERSION
Return implementation version
KVM_ARM_ASYNC_PF_CMD_GET_SDEI
Return SDEI event number used for page-not-present notification
KVM_ARM_ASYNC_PF_CMD_GET_IRQ
Return IRQ number used for page-ready notification
KVM_ARM_ASYNC_PF_CMD_GET_CONTROL
Get control block when VM is migrated
KVM_ARM_ASYNC_PF_CMD_SET_SDEI
Set SDEI event number when VM is started or migrated
KVM_ARM_ASYNC_PF_CMD_SET_IRQ
Set IRQ number during when VM is started or migrated
KVM_ARM_ASYNC_PF_CMD_SET_CONTROL
Set control block when VM is migrated

Signed-off-by: Gavin Shan <gshan@xxxxxxxxxx>
---
arch/arm64/include/asm/kvm_host.h | 14 +++++++
arch/arm64/include/uapi/asm/kvm.h | 19 +++++++++
arch/arm64/kvm/arm.c | 6 +++
arch/arm64/kvm/async_pf.c | 64 +++++++++++++++++++++++++++++++
include/uapi/linux/kvm.h | 3 ++
5 files changed, 106 insertions(+)

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 6349920fd9ce..14b3d1505b15 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -778,6 +778,8 @@ void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
struct kvm_async_pf *work);
long kvm_arch_async_pf_hypercall(struct kvm_vcpu *vcpu,
long *r1, long *r2, long *r3);
+long kvm_arch_async_pf_vm_ioctl(struct kvm *kvm, unsigned long arg);
+long kvm_arch_async_pf_vcpu_ioctl(struct kvm_vcpu *vcpu, unsigned long arg);
void kvm_arch_async_pf_destroy_vcpu(struct kvm_vcpu *vcpu);
#else
static inline void kvm_arch_async_pf_create_vcpu(struct kvm_vcpu *vcpu) { }
@@ -799,6 +801,18 @@ static inline long kvm_arch_async_pf_hypercall(struct kvm_vcpu *vcpu,
{
return SMCCC_RET_NOT_SUPPORTED;
}
+
+static inline long kvm_arch_async_pf_vm_ioctl(struct kvm *kvm,
+ unsigned long arg)
+{
+ return -EPERM;
+}
+
+static inline long kvm_arch_async_pf_vcpu_ioctl(struct kvm_vcpu *vcpu,
+ unsigned long arg)
+{
+ return -EPERM;
+}
#endif

/* Guest/host FPSIMD coordination helpers */
diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h
index 15499751997d..a6124068bee6 100644
--- a/arch/arm64/include/uapi/asm/kvm.h
+++ b/arch/arm64/include/uapi/asm/kvm.h
@@ -403,6 +403,25 @@ struct kvm_vcpu_events {
#define KVM_PSCI_RET_INVAL PSCI_RET_INVALID_PARAMS
#define KVM_PSCI_RET_DENIED PSCI_RET_DENIED

+/* Asynchronous page fault */
+#define KVM_ARM_ASYNC_PF_CMD_GET_VERSION 0
+#define KVM_ARM_ASYNC_PF_CMD_GET_SDEI 1
+#define KVM_ARM_ASYNC_PF_CMD_GET_IRQ 2
+#define KVM_ARM_ASYNC_PF_CMD_GET_CONTROL 3
+#define KVM_ARM_ASYNC_PF_CMD_SET_SDEI 4
+#define KVM_ARM_ASYNC_PF_CMD_SET_IRQ 5
+#define KVM_ARM_ASYNC_PF_CMD_SET_CONTROL 6
+
+struct kvm_arm_async_pf_cmd {
+ __u32 cmd;
+ union {
+ __u32 version;
+ __u64 sdei;
+ __u32 irq;
+ __u64 control;
+ };
+};
+
#endif

#endif /* __ARM_KVM_H__ */
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index e34fca3fa0ff..be0e6c2db2a5 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1287,6 +1287,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
case KVM_ARM_SDEI_COMMAND: {
return kvm_sdei_vcpu_ioctl(vcpu, arg);
}
+ case KVM_ARM_ASYNC_PF_COMMAND: {
+ return kvm_arch_async_pf_vcpu_ioctl(vcpu, arg);
+ }
default:
r = -EINVAL;
}
@@ -1364,6 +1367,9 @@ long kvm_arch_vm_ioctl(struct file *filp,
case KVM_ARM_SDEI_COMMAND: {
return kvm_sdei_vm_ioctl(kvm, arg);
}
+ case KVM_ARM_ASYNC_PF_COMMAND: {
+ return kvm_arch_async_pf_vm_ioctl(kvm, arg);
+ }
default:
return -EINVAL;
}
diff --git a/arch/arm64/kvm/async_pf.c b/arch/arm64/kvm/async_pf.c
index 4734c5b26aa8..6f763edbe3a3 100644
--- a/arch/arm64/kvm/async_pf.c
+++ b/arch/arm64/kvm/async_pf.c
@@ -464,6 +464,70 @@ long kvm_arch_async_pf_hypercall(struct kvm_vcpu *vcpu,
return ret;
}

+long kvm_arch_async_pf_vm_ioctl(struct kvm *kvm, unsigned long arg)
+{
+ struct kvm_arm_async_pf_cmd cmd;
+ unsigned int version = 0x010000; /* v1.0.0 */
+ void __user *argp = (void __user *)arg;
+
+ if (copy_from_user(&cmd, argp, sizeof(cmd)))
+ return -EFAULT;
+
+ if (cmd.cmd != KVM_ARM_ASYNC_PF_CMD_GET_VERSION)
+ return -EINVAL;
+
+ cmd.version = version;
+ if (copy_to_user(argp, &cmd, sizeof(cmd)))
+ return -EFAULT;
+
+ return 0;
+}
+
+long kvm_arch_async_pf_vcpu_ioctl(struct kvm_vcpu *vcpu, unsigned long arg)
+{
+ struct kvm_arch_async_pf_control *apf = vcpu->arch.apf;
+ struct kvm_arm_async_pf_cmd cmd;
+ void __user *argp = (void __user *)arg;
+ long ret = 0;
+
+ if (!apf)
+ return -EPERM;
+
+ if (copy_from_user(&cmd, argp, sizeof(cmd)))
+ return -EFAULT;
+
+ switch (cmd.cmd) {
+ case KVM_ARM_ASYNC_PF_CMD_GET_SDEI:
+ cmd.sdei = apf->sdei_event_num;
+ break;
+ case KVM_ARM_ASYNC_PF_CMD_GET_IRQ:
+ cmd.irq = apf->irq;
+ break;
+ case KVM_ARM_ASYNC_PF_CMD_GET_CONTROL:
+ cmd.control = apf->control_block;
+ break;
+ case KVM_ARM_ASYNC_PF_CMD_SET_SDEI:
+ apf->sdei_event_num = cmd.sdei;
+ break;
+ case KVM_ARM_ASYNC_PF_CMD_SET_IRQ:
+ apf->irq = cmd.irq;
+ break;
+ case KVM_ARM_ASYNC_PF_CMD_SET_CONTROL:
+ if (kvm_arch_async_enable(vcpu, cmd.control) !=
+ SMCCC_RET_SUCCESS)
+ ret = -EIO;
+
+ break;
+ default:
+ ret = -EINVAL;
+ }
+
+ if (!ret && copy_to_user(argp, &cmd, sizeof(cmd)))
+ ret = -EFAULT;
+
+ return ret;
+}
+
void kvm_arch_async_pf_destroy_vcpu(struct kvm_vcpu *vcpu)
{
kfree(vcpu->arch.apf);
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 133128d45fcb..1f161156edb2 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -1569,6 +1569,9 @@ struct kvm_pv_cmd {
/* Available with KVM_CAP_ARM_SDEI */
#define KVM_ARM_SDEI_COMMAND _IOWR(KVMIO, 0xc8, struct kvm_sdei_cmd)

+/* Available with KVM_CAP_ASYNC_PF or KVM_CAP_ASYNC_PF_INT */
+#define KVM_ARM_ASYNC_PF_COMMAND _IOWR(KVMIO, 0xc9, struct kvm_arm_async_pf_cmd)
+
/* Secure Encrypted Virtualization command */
enum sev_cmd_id {
/* Guest initialization commands */
--
2.23.0