[PATCH v4 10/15] KVM: arm64: Support async PF ioctl commands

From: Gavin Shan
Date: Sat Aug 14 2021 - 21:02:56 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 bec95e263f93..8c91a5599081 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -786,6 +786,8 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
struct kvm_async_pf *work);
void kvm_arch_async_pf_hypercall(struct kvm_vcpu *vcpu, u64 *val);
+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) { }
@@ -806,6 +808,18 @@ static inline void kvm_arch_async_pf_hypercall(struct kvm_vcpu *vcpu, u64 *val)
{
val[0] = 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 e1b200bb6482..068d1e0c4e5b 100644
--- a/arch/arm64/include/uapi/asm/kvm.h
+++ b/arch/arm64/include/uapi/asm/kvm.h
@@ -414,6 +414,25 @@ struct kvm_arm_copy_mte_tags {
#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 84f11c6b790c..74ca5ec51e53 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -1335,6 +1335,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;
}
@@ -1419,6 +1422,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 3bc69a631996..3aaed516540f 100644
--- a/arch/arm64/kvm/async_pf.c
+++ b/arch/arm64/kvm/async_pf.c
@@ -462,6 +462,70 @@ void kvm_arch_async_pf_hypercall(struct kvm_vcpu *vcpu, u64 *val)
val[0] = 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 2aa748fd89c7..bb058bf73840 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -1683,6 +1683,9 @@ struct kvm_xen_vcpu_attr {
/* Available with KVM_CAP_ARM_SDEI */
#define KVM_ARM_SDEI_COMMAND _IOWR(KVMIO, 0xce, struct kvm_sdei_cmd)

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