public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Janosch Frank <frankja@linux.ibm.com>
To: kvm@vger.kernel.org
Cc: linux-s390@vger.kernel.org, imbrenda@linux.ibm.com,
	david@redhat.com, thuth@redhat.com, seiden@linux.ibm.com
Subject: [kvm-unit-tests PATCH v2 08/10] lib: s390x: Introduce snippet helpers
Date: Tue,  7 Dec 2021 16:00:03 +0000	[thread overview]
Message-ID: <20211207160005.1586-9-frankja@linux.ibm.com> (raw)
In-Reply-To: <20211207160005.1586-1-frankja@linux.ibm.com>

These helpers reduce code duplication for PV snippet tests.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
---
 lib/s390x/snippet.h | 103 ++++++++++++++++++++++++++++++++++++++++++++
 lib/s390x/uv.h      |  21 +++++++++
 2 files changed, 124 insertions(+)

diff --git a/lib/s390x/snippet.h b/lib/s390x/snippet.h
index 6b77a8a9..b17b2a4c 100644
--- a/lib/s390x/snippet.h
+++ b/lib/s390x/snippet.h
@@ -9,6 +9,10 @@
 #ifndef _S390X_SNIPPET_H_
 #define _S390X_SNIPPET_H_
 
+#include <sie.h>
+#include <uv.h>
+#include <asm/uv.h>
+
 /* This macro cuts down the length of the pointers to snippets */
 #define SNIPPET_NAME_START(type, file) \
 	_binary_s390x_snippets_##type##_##file##_gbin_start
@@ -26,6 +30,12 @@
 #define SNIPPET_HDR_LEN(type, file) \
 	((uintptr_t)SNIPPET_HDR_END(type, file) - (uintptr_t)SNIPPET_HDR_START(type, file))
 
+#define SNIPPET_PV_TWEAK0	0x42UL
+#define SNIPPET_PV_TWEAK1	0UL
+#define SNIPPET_OFF_C		0
+#define SNIPPET_OFF_ASM		0x4000
+
+
 /*
  * C snippet instructions start at 0x4000 due to the prefix and the
  * stack being before that. ASM snippets don't strictly need a stack
@@ -38,4 +48,97 @@ static const struct psw snippet_psw = {
 	.mask = PSW_MASK_64,
 	.addr = SNIPPET_ENTRY_ADDR,
 };
+
+/*
+ * Sets up a snippet guest on top of an existing and initialized SIE
+ * vm struct.
+ * Once this function has finished without errors the guest can be started.
+ *
+ * @vm: VM that this function will populated, has to be initialized already
+ * @gbin: Snippet gbin data pointer
+ * @gbin_len: Length of the gbin data
+ * @off: Offset from guest absolute 0x0 where snippet is copied to
+ */
+static inline void snippet_init(struct vm *vm, const char *gbin,
+				uint64_t gbin_len, uint64_t off)
+{
+	uint64_t mso = vm->sblk->mso;
+
+	/* Copy test image to guest memory */
+	memcpy((void *)mso + off, gbin, gbin_len);
+
+	/* Setup guest PSW */
+	vm->sblk->gpsw = snippet_psw;
+
+	/*
+	 * We want to exit on PGM exceptions so we don't need
+	 * exception handlers in the guest.
+	 */
+	vm->sblk->ictl = ICTL_OPEREXC | ICTL_PINT;
+}
+
+/*
+ * Sets up a snippet UV/PV guest on top of an existing and initialized
+ * SIE vm struct.
+ * Once this function has finished without errors the guest can be started.
+ *
+ * @vm: VM that this function will populated, has to be initialized already
+ * @gbin: Snippet gbin data pointer
+ * @hdr: Snippet SE header data pointer
+ * @gbin_len: Length of the gbin data
+ * @hdr_len: Length of the hdr data
+ * @off: Offset from guest absolute 0x0 where snippet is copied to
+ */
+static inline void snippet_pv_init(struct vm *vm, const char *gbin,
+				   const char *hdr, uint64_t gbin_len,
+				   uint64_t hdr_len, uint64_t off)
+{
+	uint64_t tweak[2] = {SNIPPET_PV_TWEAK0, SNIPPET_PV_TWEAK1};
+	uint64_t mso = vm->sblk->mso;
+	int i;
+
+	snippet_init(vm, gbin, gbin_len, off);
+
+	uv_create_guest(vm);
+	uv_set_se_hdr(vm->uv.vm_handle, (void *)hdr, hdr_len);
+
+	/* Unpack works on guest addresses so we only need off */
+	uv_unpack(vm, off, gbin_len, tweak[0]);
+	uv_verify_load(vm);
+
+	/*
+	 * Manually import:
+	 * - lowcore 0x0 - 0x1000 (asm)
+	 * - stack 0x3000 (C)
+	 */
+	for (i = 0; i < 4; i++) {
+		uv_import(vm->uv.vm_handle, mso + PAGE_SIZE * i);
+	}
+}
+
+/* Allocates and sets up a snippet based guest */
+static inline void snippet_setup_guest(struct vm *vm, bool is_pv)
+{
+	u8 *guest;
+
+	/* Allocate 1MB as guest memory */
+	guest = alloc_pages(8);
+	memset(guest, 0, HPAGE_SIZE);
+
+	/* Initialize the vm struct and allocate control blocks */
+	sie_guest_create(vm, (uint64_t)guest, HPAGE_SIZE);
+
+	if (is_pv) {
+		/* FMT4 needs a ESCA */
+		sie_guest_sca_create(vm);
+
+		/*
+		 * Initialize UV and setup the address spaces needed
+		 * to run a PV guest.
+		 */
+		uv_init();
+		uv_setup_asces();
+	}
+}
+
 #endif
diff --git a/lib/s390x/uv.h b/lib/s390x/uv.h
index 6ffe537a..8175d9c6 100644
--- a/lib/s390x/uv.h
+++ b/lib/s390x/uv.h
@@ -3,6 +3,7 @@
 #define _S390X_UV_H_
 
 #include <sie.h>
+#include <asm/pgtable.h>
 
 bool uv_os_is_guest(void);
 bool uv_os_is_host(void);
@@ -14,4 +15,24 @@ void uv_destroy_guest(struct vm *vm);
 int uv_unpack(struct vm *vm, uint64_t addr, uint64_t len, uint64_t tweak);
 void uv_verify_load(struct vm *vm);
 
+/*
+ * To run PV guests we need to setup a few things:
+ * - A valid primary ASCE that contains the guest memory and has the P bit set.
+ * - A valid home space ASCE for the UV calls that use home space addresses.
+ */
+static inline void uv_setup_asces(void)
+{
+	uint64_t asce;
+
+	/* We need to have a valid primary ASCE to run guests. */
+	setup_vm();
+
+	/* Set P bit in ASCE as it is required for PV guests */
+	asce = stctg(1) | ASCE_P;
+	lctlg(1, asce);
+
+	/* Copy ASCE into home space CR */
+	lctlg(13, asce);
+}
+
 #endif /* UV_H */
-- 
2.32.0


  parent reply	other threads:[~2021-12-07 16:02 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-07 15:59 [kvm-unit-tests PATCH v2 00/10] s390x: sie: Add PV snippet support Janosch Frank
2021-12-07 15:59 ` [kvm-unit-tests PATCH v2 01/10] lib: s390x: sie: Add sca allocation and freeing Janosch Frank
2021-12-07 15:59 ` [kvm-unit-tests PATCH v2 02/10] s390x: sie: Add PV fields to SIE control block Janosch Frank
2021-12-07 15:59 ` [kvm-unit-tests PATCH v2 03/10] s390x: sie: Add UV information into VM struct Janosch Frank
2021-12-07 15:59 ` [kvm-unit-tests PATCH v2 04/10] s390x: uv: Add more UV call functions Janosch Frank
2021-12-07 16:00 ` [kvm-unit-tests PATCH v2 05/10] s390x: lib: Extend UV library with PV guest management Janosch Frank
2021-12-07 16:00 ` [kvm-unit-tests PATCH v2 06/10] lib: s390: sie: Add PV guest register handling Janosch Frank
2021-12-07 16:00 ` [kvm-unit-tests PATCH v2 07/10] s390x: snippets: Add PV support Janosch Frank
2021-12-08 12:57   ` Claudio Imbrenda
2021-12-07 16:00 ` Janosch Frank [this message]
2021-12-08 11:46   ` [kvm-unit-tests PATCH v2 08/10] lib: s390x: Introduce snippet helpers Claudio Imbrenda
2021-12-08 13:56     ` Janosch Frank
2021-12-07 16:00 ` [kvm-unit-tests PATCH v2 09/10] s390x: mvpg-sie: Use " Janosch Frank
2021-12-08 11:19   ` Claudio Imbrenda
2021-12-07 16:00 ` [kvm-unit-tests PATCH v2 10/10] s390x: sie: Add PV diag test Janosch Frank
2021-12-08 12:55   ` Claudio Imbrenda

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=20211207160005.1586-9-frankja@linux.ibm.com \
    --to=frankja@linux.ibm.com \
    --cc=david@redhat.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=seiden@linux.ibm.com \
    --cc=thuth@redhat.com \
    /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