xref: /kvm-unit-tests/s390x/mvpg-sie.c (revision 3ac97f8fc847d05d0a5555aefd34e2cac26fdc0c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Tests mvpg SIE partial execution intercepts.
4  *
5  * Copyright 2021 IBM Corp.
6  *
7  * Authors:
8  *    Janosch Frank <frankja@linux.ibm.com>
9  */
10 #include <libcflat.h>
11 #include <asm/asm-offsets.h>
12 #include <asm-generic/barrier.h>
13 #include <asm/pgtable.h>
14 #include <mmu.h>
15 #include <asm/page.h>
16 #include <asm/facility.h>
17 #include <asm/mem.h>
18 #include <alloc_page.h>
19 #include <vm.h>
20 #include <sclp.h>
21 #include <sie.h>
22 #include <snippet.h>
23 
24 static u8 *guest;
25 static struct vm vm;
26 
27 static uint8_t *src;
28 static uint8_t *dst;
29 static uint8_t *cmp;
30 
31 extern const char SNIPPET_NAME_START(c, mvpg_snippet)[];
32 extern const char SNIPPET_NAME_END(c, mvpg_snippet)[];
33 int binary_size;
34 
35 static void test_mvpg_pei(void)
36 {
37 	uint64_t **pei_dst = (uint64_t **)((uintptr_t) vm.sblk + 0xc0);
38 	uint64_t **pei_src = (uint64_t **)((uintptr_t) vm.sblk + 0xc8);
39 
40 	report_prefix_push("pei");
41 
42 	report_prefix_push("src");
43 	memset(dst, 0, PAGE_SIZE);
44 	protect_page(src, PAGE_ENTRY_I);
45 	sie(&vm);
46 	report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution");
47 	report((uintptr_t)**pei_src == (uintptr_t)src + PAGE_ENTRY_I, "PEI_SRC correct");
48 	report((uintptr_t)**pei_dst == (uintptr_t)dst, "PEI_DST correct");
49 	unprotect_page(src, PAGE_ENTRY_I);
50 	report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact");
51 	/*
52 	 * We need to execute the diag44 which is used as a blocker
53 	 * behind the mvpg. It makes sure we fail the tests above if
54 	 * the mvpg wouldn't have intercepted.
55 	 */
56 	sie(&vm);
57 	/* Make sure we intercepted for the diag44 and nothing else */
58 	assert(vm.sblk->icptcode == ICPT_INST &&
59 	       vm.sblk->ipa == 0x8300 && vm.sblk->ipb == 0x440000);
60 	report_prefix_pop();
61 
62 	/* Clear PEI data for next check */
63 	report_prefix_push("dst");
64 	memset((uint64_t *)((uintptr_t) vm.sblk + 0xc0), 0, 16);
65 	memset(dst, 0, PAGE_SIZE);
66 	protect_page(dst, PAGE_ENTRY_I);
67 	sie(&vm);
68 	report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution");
69 	report((uintptr_t)**pei_src == (uintptr_t)src, "PEI_SRC correct");
70 	report((uintptr_t)**pei_dst == (uintptr_t)dst + PAGE_ENTRY_I, "PEI_DST correct");
71 	/* Needed for the memcmp and general cleanup */
72 	unprotect_page(dst, PAGE_ENTRY_I);
73 	report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact");
74 	report_prefix_pop();
75 
76 	report_prefix_pop();
77 }
78 
79 static void test_mvpg(void)
80 {
81 	int binary_size = SNIPPET_LEN(c, mvpg_snippet);
82 
83 	memcpy(guest, SNIPPET_NAME_START(c, mvpg_snippet), binary_size);
84 	memset(src, 0x42, PAGE_SIZE);
85 	memset(dst, 0x43, PAGE_SIZE);
86 	sie(&vm);
87 	report(!memcmp(src, dst, PAGE_SIZE) && *dst == 0x42, "Page moved");
88 }
89 
90 static void setup_guest(void)
91 {
92 	setup_vm();
93 
94 	/* Allocate 1MB as guest memory */
95 	guest = alloc_pages(8);
96 
97 	sie_guest_create(&vm, (uint64_t)guest, HPAGE_SIZE);
98 
99 	vm.sblk->gpsw = snippet_psw;
100 	vm.sblk->ictl = ICTL_OPEREXC | ICTL_PINT;
101 	/* Enable MVPG interpretation as we want to test KVM and not ourselves */
102 	vm.sblk->eca = ECA_MVPGI;
103 
104 	src = guest + PAGE_SIZE * 6;
105 	dst = guest + PAGE_SIZE * 5;
106 	cmp = alloc_page();
107 	memset(cmp, 0, PAGE_SIZE);
108 }
109 
110 int main(void)
111 {
112 	report_prefix_push("mvpg-sie");
113 	if (!sclp_facilities.has_sief2) {
114 		report_skip("SIEF2 facility unavailable");
115 		goto done;
116 	}
117 
118 	setup_guest();
119 	test_mvpg();
120 	test_mvpg_pei();
121 	sie_guest_destroy(&vm);
122 
123 done:
124 	report_prefix_pop();
125 	return report_summary();
126 
127 }
128