xref: /kvm-unit-tests/s390x/mvpg-sie.c (revision 90cd8340890105809862e7b3190403e2ad7a0acf)
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 struct vm vm;
25 
26 static uint8_t *src;
27 static uint8_t *dst;
28 static uint8_t *cmp;
29 
30 static void test_mvpg_pei(void)
31 {
32 	uint64_t **pei_dst = (uint64_t **)((uintptr_t) vm.sblk + 0xc0);
33 	uint64_t **pei_src = (uint64_t **)((uintptr_t) vm.sblk + 0xc8);
34 
35 	report_prefix_push("pei");
36 
37 	report_prefix_push("src");
38 	memset(dst, 0, PAGE_SIZE);
39 	protect_page(src, PAGE_ENTRY_I);
40 	sie(&vm);
41 	report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution");
42 	report((uintptr_t)**pei_src == (uintptr_t)src + PAGE_ENTRY_I, "PEI_SRC correct");
43 	report((uintptr_t)**pei_dst == (uintptr_t)dst, "PEI_DST correct");
44 	unprotect_page(src, PAGE_ENTRY_I);
45 	report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact");
46 	/*
47 	 * We need to execute the diag44 which is used as a blocker
48 	 * behind the mvpg. It makes sure we fail the tests above if
49 	 * the mvpg wouldn't have intercepted.
50 	 */
51 	sie(&vm);
52 	/* Make sure we intercepted for the diag44 and nothing else */
53 	assert(vm.sblk->icptcode == ICPT_INST &&
54 	       vm.sblk->ipa == 0x8300 && vm.sblk->ipb == 0x440000);
55 	report_prefix_pop();
56 
57 	/* Clear PEI data for next check */
58 	report_prefix_push("dst");
59 	memset((uint64_t *)((uintptr_t) vm.sblk + 0xc0), 0, 16);
60 	memset(dst, 0, PAGE_SIZE);
61 	protect_page(dst, PAGE_ENTRY_I);
62 	sie(&vm);
63 	report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution");
64 	report((uintptr_t)**pei_src == (uintptr_t)src, "PEI_SRC correct");
65 	report((uintptr_t)**pei_dst == (uintptr_t)dst + PAGE_ENTRY_I, "PEI_DST correct");
66 	/* Needed for the memcmp and general cleanup */
67 	unprotect_page(dst, PAGE_ENTRY_I);
68 	report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact");
69 	report_prefix_pop();
70 
71 	report_prefix_pop();
72 }
73 
74 static void test_mvpg(void)
75 {
76 	memset(src, 0x42, PAGE_SIZE);
77 	memset(dst, 0x43, PAGE_SIZE);
78 	sie(&vm);
79 	report(!memcmp(src, dst, PAGE_SIZE) && *dst == 0x42, "Page moved");
80 }
81 
82 static void setup_guest(void)
83 {
84 	extern const char SNIPPET_NAME_START(c, mvpg_snippet)[];
85 	extern const char SNIPPET_NAME_END(c, mvpg_snippet)[];
86 
87 	setup_vm();
88 
89 	snippet_setup_guest(&vm, false);
90 	snippet_init(&vm, SNIPPET_NAME_START(c, mvpg_snippet),
91 		     SNIPPET_LEN(c, mvpg_snippet), SNIPPET_OFF_C);
92 
93 	/* Enable MVPG interpretation as we want to test KVM and not ourselves */
94 	vm.sblk->eca = ECA_MVPGI;
95 
96 	src = (uint8_t *) vm.sblk->mso + PAGE_SIZE * 6;
97 	dst = (uint8_t *) vm.sblk->mso + PAGE_SIZE * 5;
98 	cmp = alloc_page();
99 	memset(cmp, 0, PAGE_SIZE);
100 }
101 
102 int main(void)
103 {
104 	report_prefix_push("mvpg-sie");
105 	if (!sclp_facilities.has_sief2) {
106 		report_skip("SIEF2 facility unavailable");
107 		goto done;
108 	}
109 
110 	setup_guest();
111 	test_mvpg();
112 	test_mvpg_pei();
113 	sie_guest_destroy(&vm);
114 
115 done:
116 	report_prefix_pop();
117 	return report_summary();
118 
119 }
120