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