[PATCH v5 04/22] KVM: arm64: Support SDEI_EVENT_REGISTER hypercall
From: Gavin Shan
Date: Tue Mar 22 2022 - 04:08:50 EST
This supports SDEI_EVENT_REGISTER hypercall, which is used by guest
to register SDEI events. The SDEI event won't be raised until it's
registered and enabled explicitly.
Only the exposed events can be registered. For shared event, the
registered event instance is created. However, the instance may be
not created for the private events.
Signed-off-by: Gavin Shan <gshan@xxxxxxxxxx>
---
arch/arm64/kvm/sdei.c | 128 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 128 insertions(+)
diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c
index 5a3a64cd6e84..2458dc666445 100644
--- a/arch/arm64/kvm/sdei.c
+++ b/arch/arm64/kvm/sdei.c
@@ -14,6 +14,35 @@
#include <kvm/arm_hypercalls.h>
#include <asm/kvm_sdei.h>
+static struct kvm_sdei_exposed_event *
+find_exposed_event(struct kvm *kvm, unsigned long num)
+{
+ struct kvm_sdei_kvm *ksdei = kvm->arch.sdei;
+ struct kvm_sdei_exposed_event *exposed_event;
+
+ list_for_each_entry(exposed_event, &ksdei->exposed_events, link) {
+ if (exposed_event->state.num == num)
+ return exposed_event;
+ }
+
+ return NULL;
+}
+
+static struct kvm_sdei_registered_event *
+find_registered_event(struct kvm *kvm, unsigned long num)
+{
+ struct kvm_sdei_kvm *ksdei = kvm->arch.sdei;
+ struct kvm_sdei_registered_event *registered_event;
+
+ list_for_each_entry(registered_event,
+ &ksdei->registered_events, link) {
+ if (registered_event->state.num == num)
+ return registered_event;
+ }
+
+ return NULL;
+}
+
static void remove_all_exposed_events(struct kvm *kvm)
{
struct kvm_sdei_kvm *ksdei = kvm->arch.sdei;
@@ -126,6 +155,103 @@ static unsigned long hypercall_version(struct kvm_vcpu *vcpu)
0x4b564d;
}
+static unsigned long hypercall_register(struct kvm_vcpu *vcpu)
+{
+ struct kvm *kvm = vcpu->kvm;
+ struct kvm_sdei_kvm *ksdei = kvm->arch.sdei;
+ struct kvm_sdei_exposed_event *exposed_event;
+ struct kvm_sdei_registered_event *registered_event;
+ unsigned long event_num = smccc_get_arg1(vcpu);
+ unsigned long event_ep_address = smccc_get_arg2(vcpu);
+ unsigned long event_ep_arg = smccc_get_arg3(vcpu);
+ unsigned long route_mode = smccc_get_arg4(vcpu);
+ unsigned long route_affinity = smccc_get_arg5(vcpu);
+ int index;
+ unsigned long ret = SDEI_SUCCESS;
+
+ if (!kvm_sdei_is_supported(event_num)) {
+ ret = SDEI_INVALID_PARAMETERS;
+ goto out;
+ }
+
+ if (route_mode != SDEI_EVENT_REGISTER_RM_ANY &&
+ route_mode != SDEI_EVENT_REGISTER_RM_PE) {
+ ret = SDEI_INVALID_PARAMETERS;
+ goto out;
+ }
+
+ spin_lock(&ksdei->lock);
+
+ /*
+ * The registered event could have been existing if it's a private
+ * one. We needn't to create another registered event instance
+ * in this case.
+ */
+ registered_event = find_registered_event(kvm, event_num);
+ if (registered_event) {
+ exposed_event = registered_event->exposed_event;
+ index = kvm_sdei_vcpu_index(vcpu, exposed_event);
+ if (kvm_sdei_is_registered(registered_event, index) ||
+ kvm_sdei_is_unregister_pending(registered_event, index)) {
+ ret = SDEI_DENIED;
+ goto unlock;
+ }
+
+ registered_event->state.route_mode = route_mode;
+ registered_event->state.route_affinity = route_affinity;
+ registered_event->state.ep_address[index] = event_ep_address;
+ registered_event->state.ep_arg[index] = event_ep_arg;
+ kvm_sdei_set_registered(registered_event, index);
+ goto unlock;
+ }
+
+ /* Check if the exposed event exists */
+ exposed_event = find_exposed_event(kvm, event_num);
+ if (!exposed_event) {
+ ret = SDEI_INVALID_PARAMETERS;
+ goto unlock;
+ }
+
+ /*
+ * Check if the count of registered event instances exceeds
+ * the limit.
+ */
+ if (ksdei->registered_event_count >= KVM_SDEI_MAX_EVENTS) {
+ ret = SDEI_OUT_OF_RESOURCE;
+ goto unlock;
+ }
+
+ /* Allocate the registered event instance */
+ registered_event = kzalloc(sizeof(*registered_event),
+ GFP_KERNEL_ACCOUNT);
+ if (!registered_event) {
+ ret = SDEI_OUT_OF_RESOURCE;
+ goto unlock;
+ }
+
+ /* Initialize the registered event state */
+ index = kvm_sdei_vcpu_index(vcpu, exposed_event);
+ registered_event->state.num = event_num;
+ registered_event->state.route_mode = route_affinity;
+ registered_event->state.route_affinity = route_affinity;
+ registered_event->state.ep_address[index] = event_ep_address;
+ registered_event->state.ep_arg[index] = event_ep_arg;
+ registered_event->kvm = kvm;
+ registered_event->exposed_event = exposed_event;
+ registered_event->vcpu_event_count = 0;
+ kvm_sdei_set_registered(registered_event, index);
+
+ /* Add the registered event instance */
+ ksdei->registered_event_count++;
+ exposed_event->registered_event_count++;
+ list_add_tail(®istered_event->link, &ksdei->registered_events);
+
+unlock:
+ spin_unlock(&ksdei->lock);
+out:
+ return ret;
+}
+
int kvm_sdei_hypercall(struct kvm_vcpu *vcpu)
{
struct kvm *kvm = vcpu->kvm;
@@ -153,6 +279,8 @@ int kvm_sdei_hypercall(struct kvm_vcpu *vcpu)
ret = hypercall_version(vcpu);
break;
case SDEI_1_0_FN_SDEI_EVENT_REGISTER:
+ ret = hypercall_register(vcpu);
+ break;
case SDEI_1_0_FN_SDEI_EVENT_ENABLE:
case SDEI_1_0_FN_SDEI_EVENT_DISABLE:
case SDEI_1_0_FN_SDEI_EVENT_CONTEXT:
--
2.23.0