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