ltp.lists.linux.it archive mirror
 help / color / mirror / Atom feed
From: Martin Doucha <mdoucha@suse.cz>
To: Nicolai Stange <nstange@suse.de>, ltp@lists.linux.it
Subject: [LTP] [PATCH 4/7] KVM: Add async communication helper functions
Date: Wed, 17 May 2023 17:36:39 +0200	[thread overview]
Message-ID: <20230517153642.26919-5-mdoucha@suse.cz> (raw)
In-Reply-To: <20230517153642.26919-1-mdoucha@suse.cz>

Some KVM tests need to communicate with VM controller program
without stopping guest execution. Add guest helper functions
for sending asynchronous guest signal to host and waiting for host
to acknowledge the signal. Add host helper functions for waiting
for guest signal and acknowledging it.

Test programs using these functions must have at least two host threads.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/kernel/kvm/include/kvm_common.h |  8 ++++++++
 testcases/kernel/kvm/include/kvm_guest.h  | 14 +++++++++++++
 testcases/kernel/kvm/include/kvm_host.h   | 15 ++++++++++++++
 testcases/kernel/kvm/lib_guest.c          | 16 +++++++++++++++
 testcases/kernel/kvm/lib_host.c           | 24 +++++++++++++++++++++++
 5 files changed, 77 insertions(+)

diff --git a/testcases/kernel/kvm/include/kvm_common.h b/testcases/kernel/kvm/include/kvm_common.h
index 4e81d8302..377e3f6aa 100644
--- a/testcases/kernel/kvm/include/kvm_common.h
+++ b/testcases/kernel/kvm/include/kvm_common.h
@@ -10,6 +10,14 @@
 
 #define KVM_TNONE	-1	/* "No result" status value */
 
+/*
+ * Result value for asynchronous notifications between guest and host.
+ * Do not use this value directly. Call tst_signal_host() or tst_wait_host()
+ * in guest code. The notification must be handled by another host thread
+ * and then the result value must be reset to KVM_TNONE.
+ */
+#define KVM_TSYNC	0xfe
+
 /*
  * Result value indicating end of test. If the test program exits using
  * the HLT instruction with any valid result value other than KVM_TEXIT or
diff --git a/testcases/kernel/kvm/include/kvm_guest.h b/testcases/kernel/kvm/include/kvm_guest.h
index ec13c5845..96f246155 100644
--- a/testcases/kernel/kvm/include/kvm_guest.h
+++ b/testcases/kernel/kvm/include/kvm_guest.h
@@ -64,6 +64,20 @@ void tst_brk_(const char *file, const int lineno, int result,
 	const char *message) __attribute__((noreturn));
 #define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))
 
+/*
+ * Send asynchronous notification to host without stopping VM execution and
+ * return immediately. The notification must be handled by another host thread.
+ * The data argument will be passed to host in test_result->file_addr and
+ * can be used to send additional data both ways.
+ */
+void tst_signal_host(void *data);
+
+/*
+ * Call tst_signal_host(data) and wait for host to call
+ * tst_kvm_clear_guest_signal().
+ */
+void tst_wait_host(void *data);
+
 void *tst_heap_alloc_aligned(size_t size, size_t align);
 void *tst_heap_alloc(size_t size);
 
diff --git a/testcases/kernel/kvm/include/kvm_host.h b/testcases/kernel/kvm/include/kvm_host.h
index 2359944fd..cbbdadc06 100644
--- a/testcases/kernel/kvm/include/kvm_host.h
+++ b/testcases/kernel/kvm/include/kvm_host.h
@@ -134,4 +134,19 @@ void tst_kvm_run_instance(struct tst_kvm_instance *inst);
  */
 void tst_kvm_destroy_instance(struct tst_kvm_instance *inst);
 
+/*
+ * Wait for given VM to call tst_signal_host() or tst_wait_host(). Timeout
+ * value is in seconds. Zero means no wait, negative value means wait forever.
+ * Note that the actual wait time may be up to 1 second shorter due to time()
+ * granularity. Returns 0 if signal was received, KVM_TEXIT if the VM exited
+ * without sending a signal, or -1 if timeout was reached.
+ */
+int tst_kvm_wait_guest(struct tst_kvm_instance *inst, int timeout);
+
+/*
+ * Clear VM signal sent by tst_signal_host(). If the VM is waiting
+ * in tst_wait_host(), this function will signal the VM to resume execution.
+ */
+void tst_kvm_clear_guest_signal(struct tst_kvm_instance *inst);
+
 #endif /* KVM_HOST_H_ */
diff --git a/testcases/kernel/kvm/lib_guest.c b/testcases/kernel/kvm/lib_guest.c
index d3b2ac3d5..f3e21d3d6 100644
--- a/testcases/kernel/kvm/lib_guest.c
+++ b/testcases/kernel/kvm/lib_guest.c
@@ -155,6 +155,22 @@ void tst_brk_(const char *file, const int lineno, int result,
 	kvm_exit();
 }
 
+void tst_signal_host(void *data)
+{
+	test_result->file_addr = (uintptr_t)data;
+	test_result->result = KVM_TSYNC;
+}
+
+void tst_wait_host(void *data)
+{
+	volatile int32_t *vres = &test_result->result;
+
+	tst_signal_host(data);
+
+	while (*vres != KVM_TNONE)
+		;
+}
+
 void tst_handle_interrupt(struct kvm_interrupt_frame *ifrm, long vector,
 	unsigned long errcode)
 {
diff --git a/testcases/kernel/kvm/lib_host.c b/testcases/kernel/kvm/lib_host.c
index 2782e68b0..a5f05449c 100644
--- a/testcases/kernel/kvm/lib_host.c
+++ b/testcases/kernel/kvm/lib_host.c
@@ -272,6 +272,30 @@ void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)
 	memset(inst->ram, 0, sizeof(inst->ram));
 }
 
+int tst_kvm_wait_guest(struct tst_kvm_instance *inst, int timeout)
+{
+	volatile struct tst_kvm_result *result = inst->result;
+	int32_t res;
+	time_t start = time(NULL);
+
+	while ((res = result->result) != KVM_TSYNC) {
+		if (res == KVM_TEXIT)
+			return res;
+
+		if (timeout >= 0 && start + timeout <= time(NULL))
+			return -1;
+
+		usleep(1000);
+	}
+
+	return 0;
+}
+
+void tst_kvm_clear_guest_signal(struct tst_kvm_instance *inst)
+{
+	inst->result->result = KVM_TNONE;
+}
+
 void tst_kvm_setup(void)
 {
 
-- 
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2023-05-17 15:37 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-17 15:36 [LTP] [PATCH 0/7] Two AMD SVM vulnerability tests Martin Doucha
2023-05-17 15:36 ` [LTP] [PATCH 1/7] KVM: Add VMSAVE/VMLOAD intercept constants Martin Doucha
2023-05-17 22:08   ` Petr Vorel
2023-05-18  8:27     ` Martin Doucha
2023-05-18  9:49       ` Petr Vorel
2023-05-17 15:36 ` [LTP] [PATCH 2/7] Add test for CVE 2021-3656 Martin Doucha
2023-05-17 22:32   ` Petr Vorel
2023-05-18  8:29     ` Martin Doucha
2023-05-18  9:56       ` Petr Vorel
2023-05-17 15:36 ` [LTP] [PATCH 3/7] lib: Add safe functions for pthread_kill() and mutexes Martin Doucha
2023-05-17 23:07   ` Petr Vorel
2023-05-23 12:02   ` Cyril Hrubis
2023-05-17 15:36 ` Martin Doucha [this message]
2023-05-17 22:47   ` [LTP] [PATCH 4/7] KVM: Add async communication helper functions Petr Vorel
2023-05-18  8:36     ` Martin Doucha
2023-05-18  9:55       ` Petr Vorel
2023-05-23 12:13   ` Cyril Hrubis
2023-05-23 12:18     ` Martin Doucha
2023-05-17 15:36 ` [LTP] [PATCH 5/7] KVM: Allow expected KVM_RUN errors in tst_kvm_run_instance() Martin Doucha
2023-05-18 10:03   ` Petr Vorel
2023-05-23 12:22   ` Cyril Hrubis
2023-05-17 15:36 ` [LTP] [PATCH 6/7] KVM: Add STGI/CLGI intercept constants Martin Doucha
2023-05-17 23:20   ` Petr Vorel
2023-05-18  8:38     ` Martin Doucha
2023-05-18  9:53       ` Petr Vorel
2023-05-17 15:36 ` [LTP] [PATCH 7/7] Add KVM test for CPU lockup through malicous SVM guest Martin Doucha
2023-05-18 10:08   ` Petr Vorel

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=20230517153642.26919-5-mdoucha@suse.cz \
    --to=mdoucha@suse.cz \
    --cc=ltp@lists.linux.it \
    --cc=nstange@suse.de \
    /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).