[PATCH v6 07/18] KVM: arm64: Support SDEI_EVENT_UNREGISTER hypercall

From: Gavin Shan
Date: Sun Apr 03 2022 - 11:40:34 EST


This supports SDEI_EVENT_UNREGISTER hypercall. It's used by the
guest to unregister event. The event is disabled automatically
and won't be delivered to guest after unregistration.

If the event is being serviced or handled, we can't unregister
it immediately. Instead, the unregistration pending state is
set for the event and it's unregistered when the event handler
is to finish by calling SDEI_EVENT_{COMPLETE, COMPLETE_AND_RESUME}
hypercall.

Signed-off-by: Gavin Shan <gshan@xxxxxxxxxx>
---
arch/arm64/kvm/sdei.c | 79 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)

diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c
index af5d11b8eb2f..f774f2cf0ac7 100644
--- a/arch/arm64/kvm/sdei.c
+++ b/arch/arm64/kvm/sdei.c
@@ -45,6 +45,48 @@ static struct kvm_sdei_event *find_event(struct kvm_vcpu *vcpu,
return NULL;
}

+static int reset_event(struct kvm_vcpu *vcpu,
+ struct kvm_sdei_event *event)
+{
+ struct kvm_sdei_vcpu *vsdei = vcpu->arch.sdei;
+ struct kvm_sdei_vcpu_context *context;
+ struct kvm_sdei_exposed_event *exposed_event;
+
+ /* Check if the event has been pending for unregistration */
+ if (kvm_sdei_is_unregister_pending(event))
+ return -EAGAIN;
+
+ /*
+ * If the event is being handled, we should set the unregistration
+ * pending state for it. The event will be unregistered after the
+ * event handler is to finish.
+ */
+ exposed_event = event->exposed_event;
+ context = kvm_sdei_is_critical(exposed_event->priority) ?
+ &vsdei->context[SDEI_EVENT_PRIORITY_CRITICAL] :
+ &vsdei->context[SDEI_EVENT_PRIORITY_NORMAL];
+ if (context->event == event) {
+ kvm_sdei_set_unregister_pending(event);
+ return -EBUSY;
+ }
+
+ /*
+ * The event is ready to be unregistered. The event is disabled
+ * when it's unregistered. The pending events should be cancelled
+ * either.
+ */
+ if (kvm_sdei_is_critical(exposed_event->priority))
+ vsdei->critical_event_count -= event->event_count;
+ else
+ vsdei->normal_event_count -= event->event_count;
+
+ event->event_count = 0;
+ kvm_sdei_clear_enabled(event);
+ kvm_sdei_clear_registered(event);
+
+ return 0;
+}
+
static unsigned long hypercall_register(struct kvm_vcpu *vcpu)
{
struct kvm_sdei_vcpu *vsdei = vcpu->arch.sdei;
@@ -171,6 +213,40 @@ static unsigned long hypercall_context(struct kvm_vcpu *vcpu)
return ret;
}

+static unsigned long hypercall_unregister(struct kvm_vcpu *vcpu)
+{
+ struct kvm_sdei_vcpu *vsdei = vcpu->arch.sdei;
+ struct kvm_sdei_event *event;
+ unsigned int num = smccc_get_arg(vcpu, 1);
+ unsigned long ret = SDEI_SUCCESS;
+
+ if (!kvm_sdei_is_supported(num))
+ return SDEI_INVALID_PARAMETERS;
+
+ spin_lock(&vsdei->lock);
+
+ /* Check if the event exists */
+ event = find_event(vcpu, num);
+ if (!event) {
+ ret = SDEI_INVALID_PARAMETERS;
+ goto unlock;
+ }
+
+ /* Check if the event has been registered */
+ if (!kvm_sdei_is_registered(event)) {
+ ret = SDEI_DENIED;
+ goto unlock;
+ }
+
+ if (reset_event(vcpu, event))
+ ret = SDEI_PENDING;
+
+unlock:
+ spin_unlock(&vsdei->lock);
+
+ return ret;
+}
+
int kvm_sdei_call(struct kvm_vcpu *vcpu)
{
struct kvm_sdei_vcpu *vsdei = vcpu->arch.sdei;
@@ -204,6 +280,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu)
case SDEI_1_0_FN_SDEI_EVENT_CONTEXT:
ret = hypercall_context(vcpu);
break;
+ case SDEI_1_0_FN_SDEI_EVENT_UNREGISTER:
+ ret = hypercall_unregister(vcpu);
+ break;
default:
ret = SDEI_NOT_SUPPORTED;
}
--
2.23.0