linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gavin Shan <gshan@redhat.com>
To: kvmarm@lists.cs.columbia.edu
Cc: linux-kernel@vger.kernel.org, james.morse@arm.com,
	mark.rutland@arm.com, Jonathan.Cameron@huawei.com,
	will@kernel.org, maz@kernel.org, pbonzini@redhat.com
Subject: [PATCH v4 21/21] KVM: selftests: Add SDEI test case
Date: Sun, 15 Aug 2021 08:13:52 +0800	[thread overview]
Message-ID: <20210815001352.81927-22-gshan@redhat.com> (raw)
In-Reply-To: <20210815001352.81927-1-gshan@redhat.com>

This adds SDEI test case into selftests where the various hypercalls
are issued to kvm private event (0x40200000) and then ensure that's
completed without error. Note that two vCPUs are started up by default
to run same consequence. Actually, it's simulating what SDEI client
driver does and the following hypercalls are issued in sequence:

   SDEI_1_0_FN_SDEI_VERSION            (probing SDEI capability)
   SDEI_1_0_FN_SDEI_PE_UNMASK          (CPU online)
   SDEI_1_0_FN_SDEI_PRIVATE_RESET      (restart SDEI)
   SDEI_1_0_FN_SDEI_SHARED_RESET
   SDEI_1_0_FN_SDEI_EVENT_GET_INFO     (register event)
   SDEI_1_0_FN_SDEI_EVENT_GET_INFO
   SDEI_1_0_FN_SDEI_EVENT_GET_INFO
   SDEI_1_0_FN_SDEI_EVENT_REGISTER
   SDEI_1_0_FN_SDEI_EVENT_ENABLE       (enable event)
   SDEI_1_0_FN_SDEI_EVENT_DISABLE      (disable event)
   SDEI_1_0_FN_SDEI_EVENT_UNREGISTER   (unregister event)
   SDEI_1_0_FN_SDEI_PE_MASK            (CPU offline)

Signed-off-by: Gavin Shan <gshan@redhat.com>
---
 tools/testing/selftests/kvm/Makefile       |   1 +
 tools/testing/selftests/kvm/aarch64/sdei.c | 171 +++++++++++++++++++++
 2 files changed, 172 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/sdei.c

diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 5832f510a16c..33284c468b7d 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -87,6 +87,7 @@ TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
 TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
 TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
+TEST_GEN_PROGS_aarch64 += aarch64/sdei
 TEST_GEN_PROGS_aarch64 += demand_paging_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
 TEST_GEN_PROGS_aarch64 += dirty_log_perf_test
diff --git a/tools/testing/selftests/kvm/aarch64/sdei.c b/tools/testing/selftests/kvm/aarch64/sdei.c
new file mode 100644
index 000000000000..fadccdfc009c
--- /dev/null
+++ b/tools/testing/selftests/kvm/aarch64/sdei.c
@@ -0,0 +1,171 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ARM SDEI test
+ *
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * Author(s): Gavin Shan <gshan@redhat.com>
+ */
+#define _GNU_SOURCE
+#include <stdio.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "asm/kvm_sdei.h"
+#include "linux/arm_sdei.h"
+
+#define NR_VCPUS	2
+
+struct sdei_event {
+	uint32_t	cpu;
+	uint64_t	version;
+	uint64_t	num;
+	uint64_t	type;
+	uint64_t	priority;
+	uint64_t	signaled;
+};
+
+static struct sdei_event sdei_events[NR_VCPUS];
+
+static int64_t smccc(uint32_t func, uint64_t arg0, uint64_t arg1,
+		     uint64_t arg2, uint64_t arg3, uint64_t arg4)
+{
+	int64_t ret;
+
+	asm volatile(
+		"mov    x0, %1\n"
+		"mov    x1, %2\n"
+		"mov    x2, %3\n"
+		"mov    x3, %4\n"
+		"mov    x4, %5\n"
+		"mov    x5, %6\n"
+		"hvc    #0\n"
+		"mov    %0, x0\n"
+	: "=r" (ret) : "r" (func), "r" (arg0), "r" (arg1),
+	"r" (arg2), "r" (arg3), "r" (arg4) :
+	"x0", "x1", "x2", "x3", "x4", "x5");
+
+	return ret;
+}
+
+static inline bool is_error(int64_t ret)
+{
+	if (ret == SDEI_NOT_SUPPORTED      ||
+	    ret == SDEI_INVALID_PARAMETERS ||
+	    ret == SDEI_DENIED             ||
+	    ret == SDEI_PENDING            ||
+	    ret == SDEI_OUT_OF_RESOURCE)
+		return true;
+
+	return false;
+}
+
+static void guest_code(int cpu)
+{
+	struct sdei_event *event = &sdei_events[cpu];
+	int64_t ret;
+
+	/* CPU */
+	event->cpu = cpu;
+	event->num = KVM_SDEI_DEFAULT_NUM;
+	GUEST_ASSERT(cpu < NR_VCPUS);
+
+	/* Version */
+	ret = smccc(SDEI_1_0_FN_SDEI_VERSION, 0, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+	GUEST_ASSERT(SDEI_VERSION_MAJOR(ret) == 1);
+	GUEST_ASSERT(SDEI_VERSION_MINOR(ret) == 0);
+	event->version = ret;
+
+	/* CPU unmasking */
+	ret = smccc(SDEI_1_0_FN_SDEI_PE_UNMASK, 0, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+
+	/* Reset */
+	ret = smccc(SDEI_1_0_FN_SDEI_PRIVATE_RESET, 0, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+	ret = smccc(SDEI_1_0_FN_SDEI_SHARED_RESET, 0, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+
+	/* Event properties */
+	ret = smccc(SDEI_1_0_FN_SDEI_EVENT_GET_INFO,
+		     event->num, SDEI_EVENT_INFO_EV_TYPE, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+	event->type = ret;
+
+	ret = smccc(SDEI_1_0_FN_SDEI_EVENT_GET_INFO,
+		    event->num, SDEI_EVENT_INFO_EV_PRIORITY, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+	event->priority = ret;
+
+	ret = smccc(SDEI_1_0_FN_SDEI_EVENT_GET_INFO,
+		    event->num, SDEI_EVENT_INFO_EV_SIGNALED, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+	event->signaled = ret;
+
+	/* Event registration */
+	ret = smccc(SDEI_1_0_FN_SDEI_EVENT_REGISTER,
+		    event->num, 0, 0, SDEI_EVENT_REGISTER_RM_ANY, 0);
+	GUEST_ASSERT(!is_error(ret));
+
+	/* Event enablement */
+	ret = smccc(SDEI_1_0_FN_SDEI_EVENT_ENABLE,
+		    event->num, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+
+	/* Event disablement */
+	ret = smccc(SDEI_1_0_FN_SDEI_EVENT_DISABLE,
+		    event->num, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+
+	/* Event unregistration */
+	ret = smccc(SDEI_1_0_FN_SDEI_EVENT_UNREGISTER,
+		    event->num, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+
+	/* CPU masking */
+	ret = smccc(SDEI_1_0_FN_SDEI_PE_MASK, 0, 0, 0, 0, 0);
+	GUEST_ASSERT(!is_error(ret));
+
+	GUEST_DONE();
+}
+
+int main(int argc, char **argv)
+{
+	struct kvm_vm *vm;
+	int i;
+
+	if (!kvm_check_cap(KVM_CAP_ARM_SDEI)) {
+		pr_info("SDEI not supported\n");
+		return 0;
+	}
+
+	vm = vm_create_default(0, 0, guest_code);
+	ucall_init(vm, NULL);
+
+	for (i = 1; i < NR_VCPUS; i++)
+		vm_vcpu_add_default(vm, i, guest_code);
+
+	for (i = 0; i < NR_VCPUS; i++) {
+		vcpu_args_set(vm, i, 1, i);
+		vcpu_run(vm, i);
+
+		sync_global_from_guest(vm, sdei_events[i]);
+		pr_info("--------------------------------\n");
+		pr_info("CPU:      %d\n",
+			sdei_events[i].cpu);
+		pr_info("Version:  %ld.%ld (0x%lx)\n",
+			SDEI_VERSION_MAJOR(sdei_events[i].version),
+			SDEI_VERSION_MINOR(sdei_events[i].version),
+			SDEI_VERSION_VENDOR(sdei_events[i].version));
+		pr_info("Event:    0x%08lx\n",
+			sdei_events[i].num);
+		pr_info("Type:     %s\n",
+			sdei_events[i].type ? "shared" : "private");
+		pr_info("Signaled: %s\n",
+			sdei_events[i].signaled ? "yes" : "no");
+	}
+
+	return 0;
+}
-- 
2.23.0


  parent reply	other threads:[~2021-08-15  0:17 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-15  0:13 [PATCH v4 00/21] Support SDEI Virtualization Gavin Shan
2021-08-15  0:13 ` [PATCH v4 01/21] KVM: arm64: Introduce template for inline functions Gavin Shan
2021-11-09 15:26   ` Eric Auger
2022-01-11  7:52     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 02/21] KVM: arm64: Add SDEI virtualization infrastructure Gavin Shan
2021-11-09 15:45   ` Eric Auger
2022-01-11  9:20     ` Gavin Shan
2022-01-27 13:17       ` Eric Auger
2022-01-11  9:40   ` Shannon Zhao
2022-01-13  7:09     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 03/21] KVM: arm64: Support SDEI_VERSION hypercall Gavin Shan
2021-11-09 15:26   ` Eric Auger
2022-01-11  9:25     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 04/21] KVM: arm64: Support SDEI_EVENT_REGISTER hypercall Gavin Shan
2021-11-09 15:50   ` Eric Auger
2022-01-12  2:19     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 05/21] KVM: arm64: Support SDEI_EVENT_{ENABLE, DISABLE} hypercall Gavin Shan
2021-11-09 16:02   ` Eric Auger
2022-01-12  2:29     ` Gavin Shan
2022-01-25 18:23       ` Eric Auger
2021-08-15  0:13 ` [PATCH v4 06/21] KVM: arm64: Support SDEI_EVENT_CONTEXT hypercall Gavin Shan
2021-11-10 11:16   ` Eric Auger
2022-01-12  2:33     ` Gavin Shan
2022-01-25 18:29       ` Eric Auger
2022-01-11  9:43   ` Shannon Zhao
2022-01-13  7:02     ` Gavin Shan
2022-01-13  7:13       ` Gavin Shan
2022-01-25 18:32         ` Eric Auger
2022-01-25 18:31       ` Eric Auger
2021-08-15  0:13 ` [PATCH v4 07/21] KVM: arm64: Support SDEI_EVENT_UNREGISTER hypercall Gavin Shan
2021-11-09 17:05   ` Eric Auger
2022-01-12  2:38     ` Gavin Shan
2022-01-25 18:42       ` Eric Auger
2021-08-15  0:13 ` [PATCH v4 08/21] KVM: arm64: Support SDEI_EVENT_STATUS hypercall Gavin Shan
2021-11-09 17:12   ` Eric Auger
2022-01-12  2:40     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 09/21] KVM: arm64: Support SDEI_EVENT_GET_INFO hypercall Gavin Shan
2021-11-09 17:19   ` Eric Auger
2022-01-12  2:46     ` Gavin Shan
2022-01-27 14:19       ` Eric Auger
2021-08-15  0:13 ` [PATCH v4 10/21] KVM: arm64: Support SDEI_EVENT_ROUTING_SET hypercall Gavin Shan
2021-11-09 18:47   ` Eric Auger
2022-01-12  2:54     ` Gavin Shan
2022-01-27 14:13       ` Eric Auger
2021-08-15  0:13 ` [PATCH v4 11/21] KVM: arm64: Support SDEI_PE_{MASK, UNMASK} hypercall Gavin Shan
2021-11-09 20:31   ` Eric Auger
2022-01-12  2:58     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 12/21] KVM: arm64: Support SDEI_{PRIVATE, SHARED}_RESET hypercall Gavin Shan
2021-11-09 20:37   ` Eric Auger
2022-01-12  3:01     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 13/21] KVM: arm64: Impment SDEI event delivery Gavin Shan
2021-11-10 10:58   ` Eric Auger
2022-01-12  6:34     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 14/21] KVM: arm64: Support SDEI_EVENT_{COMPLETE, COMPLETE_AND_RESUME} hypercall Gavin Shan
2021-11-10 10:58   ` Eric Auger
2022-01-12  6:43     ` Gavin Shan
2022-01-27 14:47       ` Eric Auger
2022-01-27 15:20       ` Eric Auger
2021-08-15  0:13 ` [PATCH v4 15/21] KVM: arm64: Support SDEI event notifier Gavin Shan
2021-11-10 11:35   ` Eric Auger
2022-01-12  6:48     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 16/21] KVM: arm64: Support SDEI ioctl commands on VM Gavin Shan
2021-11-10 13:48   ` Eric Auger
2022-01-12  7:03     ` Gavin Shan
2022-01-27 13:48       ` Eric Auger
2021-08-15  0:13 ` [PATCH v4 17/21] KVM: arm64: Support SDEI ioctl commands on vCPU Gavin Shan
2021-08-15  0:13 ` [PATCH v4 18/21] KVM: arm64: Support SDEI event injection Gavin Shan
2021-11-10 14:05   ` Eric Auger
2022-01-12  7:12     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 19/21] KVM: arm64: Support SDEI event cancellation Gavin Shan
2021-11-10 14:09   ` Eric Auger
2022-01-12  7:19     ` Gavin Shan
2021-08-15  0:13 ` [PATCH v4 20/21] KVM: arm64: Export SDEI capability Gavin Shan
2021-11-10 13:55   ` Eric Auger
2022-01-12  7:20     ` Gavin Shan
2021-08-15  0:13 ` Gavin Shan [this message]
2021-08-15  0:19 ` [PATCH v4 00/21] Support SDEI Virtualization Gavin Shan
2021-11-10 14:29   ` Eric Auger
2022-01-12  7:24     ` Gavin Shan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210815001352.81927-22-gshan@redhat.com \
    --to=gshan@redhat.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=james.morse@arm.com \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).