xref: /kvm-unit-tests/s390x/mvpg-sie.c (revision 877821b4f5d190aae4868f5898a4b5d9fff92f02)
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 
23 static u8 *guest;
24 static struct vm vm;
25 
26 static uint8_t *src;
27 static uint8_t *dst;
28 static uint8_t *cmp;
29 
30 extern const char _binary_s390x_snippets_c_mvpg_snippet_gbin_start[];
31 extern const char _binary_s390x_snippets_c_mvpg_snippet_gbin_end[];
32 int binary_size;
33 
34 static void test_mvpg_pei(void)
35 {
36 	uint64_t **pei_dst = (uint64_t **)((uintptr_t) vm.sblk + 0xc0);
37 	uint64_t **pei_src = (uint64_t **)((uintptr_t) vm.sblk + 0xc8);
38 
39 	report_prefix_push("pei");
40 
41 	report_prefix_push("src");
42 	memset(dst, 0, PAGE_SIZE);
43 	protect_page(src, PAGE_ENTRY_I);
44 	sie(&vm);
45 	report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution");
46 	report((uintptr_t)**pei_src == (uintptr_t)src + PAGE_ENTRY_I, "PEI_SRC correct");
47 	report((uintptr_t)**pei_dst == (uintptr_t)dst, "PEI_DST correct");
48 	unprotect_page(src, PAGE_ENTRY_I);
49 	report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact");
50 	/*
51 	 * We need to execute the diag44 which is used as a blocker
52 	 * behind the mvpg. It makes sure we fail the tests above if
53 	 * the mvpg wouldn't have intercepted.
54 	 */
55 	sie(&vm);
56 	/* Make sure we intercepted for the diag44 and nothing else */
57 	assert(vm.sblk->icptcode == ICPT_INST &&
58 	       vm.sblk->ipa == 0x8300 && vm.sblk->ipb == 0x440000);
59 	report_prefix_pop();
60 
61 	/* Clear PEI data for next check */
62 	report_prefix_push("dst");
63 	memset((uint64_t *)((uintptr_t) vm.sblk + 0xc0), 0, 16);
64 	memset(dst, 0, PAGE_SIZE);
65 	protect_page(dst, PAGE_ENTRY_I);
66 	sie(&vm);
67 	report(vm.sblk->icptcode == ICPT_PARTEXEC, "Partial execution");
68 	report((uintptr_t)**pei_src == (uintptr_t)src, "PEI_SRC correct");
69 	report((uintptr_t)**pei_dst == (uintptr_t)dst + PAGE_ENTRY_I, "PEI_DST correct");
70 	/* Needed for the memcmp and general cleanup */
71 	unprotect_page(dst, PAGE_ENTRY_I);
72 	report(!memcmp(cmp, dst, PAGE_SIZE), "Destination intact");
73 	report_prefix_pop();
74 
75 	report_prefix_pop();
76 }
77 
78 static void test_mvpg(void)
79 {
80 	int binary_size = ((uintptr_t)_binary_s390x_snippets_c_mvpg_snippet_gbin_end -
81 			   (uintptr_t)_binary_s390x_snippets_c_mvpg_snippet_gbin_start);
82 
83 	memcpy(guest, _binary_s390x_snippets_c_mvpg_snippet_gbin_start, 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.addr = PAGE_SIZE * 4;
100 	vm.sblk->gpsw.mask = PSW_MASK_64;
101 	vm.sblk->ictl = ICTL_OPEREXC | ICTL_PINT;
102 	/* Enable MVPG interpretation as we want to test KVM and not ourselves */
103 	vm.sblk->eca = ECA_MVPGI;
104 
105 	src = guest + PAGE_SIZE * 6;
106 	dst = guest + PAGE_SIZE * 5;
107 	cmp = alloc_page();
108 	memset(cmp, 0, PAGE_SIZE);
109 }
110 
111 int main(void)
112 {
113 	report_prefix_push("mvpg-sie");
114 	if (!sclp_facilities.has_sief2) {
115 		report_skip("SIEF2 facility unavailable");
116 		goto done;
117 	}
118 
119 	setup_guest();
120 	test_mvpg();
121 	test_mvpg_pei();
122 	sie_guest_destroy(&vm);
123 
124 done:
125 	report_prefix_pop();
126 	return report_summary();
127 
128 }
129