xref: /kvm-unit-tests/s390x/mvpg-sie.c (revision 0c259cf369540df8fed7e64d63998f92574966b9)
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 u8 *guest_instr;
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 _binary_s390x_snippets_c_mvpg_snippet_gbin_start[];
32 extern const char _binary_s390x_snippets_c_mvpg_snippet_gbin_end[];
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 = ((uintptr_t)_binary_s390x_snippets_c_mvpg_snippet_gbin_end -
82 			   (uintptr_t)_binary_s390x_snippets_c_mvpg_snippet_gbin_start);
83 
84 	memcpy(guest, _binary_s390x_snippets_c_mvpg_snippet_gbin_start, binary_size);
85 	memset(src, 0x42, PAGE_SIZE);
86 	memset(dst, 0x43, PAGE_SIZE);
87 	sie(&vm);
88 	report(!memcmp(src, dst, PAGE_SIZE) && *dst == 0x42, "Page moved");
89 }
90 
91 static void setup_guest(void)
92 {
93 	setup_vm();
94 
95 	/* Allocate 1MB as guest memory */
96 	guest = alloc_pages(8);
97 	/* The first two pages are the lowcore */
98 	guest_instr = guest + PAGE_SIZE * 2;
99 
100 	sie_guest_create(&vm, (uint64_t)guest, HPAGE_SIZE);
101 
102 	vm.sblk->gpsw.addr = PAGE_SIZE * 4;
103 	vm.sblk->gpsw.mask = PSW_MASK_64;
104 	vm.sblk->ictl = ICTL_OPEREXC | ICTL_PINT;
105 	/* Enable MVPG interpretation as we want to test KVM and not ourselves */
106 	vm.sblk->eca = ECA_MVPGI;
107 
108 	src = guest + PAGE_SIZE * 6;
109 	dst = guest + PAGE_SIZE * 5;
110 	cmp = alloc_page();
111 	memset(cmp, 0, PAGE_SIZE);
112 }
113 
114 int main(void)
115 {
116 	report_prefix_push("mvpg-sie");
117 	if (!sclp_facilities.has_sief2) {
118 		report_skip("SIEF2 facility unavailable");
119 		goto done;
120 	}
121 
122 	setup_guest();
123 	test_mvpg();
124 	test_mvpg_pei();
125 	sie_guest_destroy(&vm);
126 
127 done:
128 	report_prefix_pop();
129 	return report_summary();
130 
131 }
132