[kvm-unit-tests PATCH v4 4/6] s390x: Add library functions for exiting from snippet

Nina Schoetterl-Glausch
2024-10-16T18:03:30Z

It is useful to be able to force an exit to the host from the snippet,
as well as do so while returning a value.
Add this functionality, also add helper functions for the host to check
for an exit and get or check the value.
Use diag 0x44 and 0x9c for this.
Add a guest specific snippet header file and rename snippet.h to reflect
that it is host specific.

Signed-off-by: Nina Schoetterl-Glausch <nsg@linux.ibm.com>
---
s390x/Makefile | 4 ++-
lib/s390x/asm/arch_def.h | 13 +++++++++
lib/s390x/snippet-exit.h | 47 +++++++++++++++++++++++++++++++
s390x/snippets/lib/snippet-exit.h | 28 ++++++++++++++++++
4 files changed, 91 insertions(+), 1 deletion(-)
create mode 100644 lib/s390x/snippet-exit.h
create mode 100644 s390x/snippets/lib/snippet-exit.h

diff --git a/s390x/Makefile b/s390x/Makefile
index 0ad8d021..1caf221d 100644
--- a/s390x/Makefile
+++ b/s390x/Makefile
@@ -70,7 +70,8 @@ test_cases: $(tests)
test_cases_binary: $(tests_binary)
test_cases_pv: $(tests_pv_binary)

-INCLUDE_PATHS = $(SRCDIR)/lib $(SRCDIR)/lib/s390x $(SRCDIR)/s390x
+SNIPPET_INCLUDE :=
+INCLUDE_PATHS = $(SNIPPET_INCLUDE) $(SRCDIR)/lib $(SRCDIR)/lib/s390x $(SRCDIR)/s390x
# Include generated header files (e.g. in case of out-of-source builds)
INCLUDE_PATHS += lib
CPPFLAGS = $(addprefix -I,$(INCLUDE_PATHS))
@@ -151,6 +152,7 @@ endif
$(SNIPPET_DIR)/asm/%.o: $(SNIPPET_DIR)/asm/%.S $(asm-offsets)
$(CC) $(CFLAGS) -c -nostdlib -o $@ $<

+$(SNIPPET_DIR)/c/%.o: SNIPPET_INCLUDE := $(SNIPPET_DIR)/lib
$(SNIPPET_DIR)/c/%.o: $(SNIPPET_DIR)/c/%.c $(asm-offsets)
$(CC) $(CFLAGS) -c -nostdlib -o $@ $<

diff --git a/lib/s390x/asm/arch_def.h b/lib/s390x/asm/arch_def.h
index 745a3387..db04deca 100644
--- a/lib/s390x/asm/arch_def.h
+++ b/lib/s390x/asm/arch_def.h
@@ -504,4 +504,17 @@ static inline uint32_t get_prefix(void)
return current_prefix;
}

+static inline void diag44(void)
+{
+ asm volatile("diag 0,0,0x44\n");
+}
+
+static inline void diag9c(uint64_t val)
+{
+ asm volatile("diag %[val],0,0x9c\n"
+ :
+ : [val] "d"(val)
+ );
+}
+
#endif
diff --git a/lib/s390x/snippet-exit.h b/lib/s390x/snippet-exit.h
new file mode 100644
index 00000000..f62f0068
--- /dev/null
+++ b/lib/s390x/snippet-exit.h
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Functionality handling snippet exits
+ *
+ * Copyright IBM Corp. 2024
+ */
+
+#ifndef _S390X_SNIPPET_EXIT_H_
+#define _S390X_SNIPPET_EXIT_H_
+
+#include <libcflat.h>
+#include <sie.h>
+#include <sie-icpt.h>
+
+static inline bool snippet_is_force_exit(struct vm *vm)
+{
+ return sie_is_diag_icpt(vm, 0x44);
+}
+
+static inline bool snippet_is_force_exit_value(struct vm *vm)
+{
+ return sie_is_diag_icpt(vm, 0x9c);
+}
+
+static inline uint64_t snippet_get_force_exit_value(struct vm *vm)
+{
+ struct kvm_s390_sie_block *sblk = vm->sblk;
+
+ assert(snippet_is_force_exit_value(vm));
+
+ return vm->save_area.guest.grs[sblk_ip_as_diag(sblk).r_1];
+}
+
+static inline void snippet_check_force_exit_value(struct vm *vm, uint64_t value_exp)
+{
+ uint64_t value;
+
+ if (snippet_is_force_exit_value(vm)) {
+ value = snippet_get_force_exit_value(vm);
+ report(value == value_exp, "guest forced exit with value (0x%lx == 0x%lx)",
+ value, value_exp);
+ } else {
+ report_fail("guest forced exit with value");
+ }
+}
+
+#endif /* _S390X_SNIPPET_EXIT_H_ */
diff --git a/s390x/snippets/lib/snippet-exit.h b/s390x/snippets/lib/snippet-exit.h
new file mode 100644
index 00000000..0b483366
--- /dev/null
+++ b/s390x/snippets/lib/snippet-exit.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Functionality for exiting the snippet.
+ *
+ * Copyright IBM Corp. 2023
+ */
+
+#ifndef _S390X_SNIPPET_LIB_EXIT_H_
+#define _S390X_SNIPPET_LIB_EXIT_H_
+
+#include <asm/arch_def.h>
+#include <asm/barrier.h>
+
+static inline void force_exit(void)
+{
+ mb(); /* host may read any memory written by the guest before */
+ diag44();
+ mb(); /* allow host to modify guest memory */
+}
+
+static inline void force_exit_value(uint64_t val)
+{
+ mb(); /* host may read any memory written by the guest before */
+ diag9c(val);
+ mb(); /* allow host to modify guest memory */
+}
+
+#endif /* _S390X_SNIPPET_LIB_EXIT_H_ */
--
2.44.0