xref: /kvm-unit-tests/powerpc/spapr_hcall.c (revision c20e1e54076863136b1e818d364778209fb7e4bb)
1 /*
2  * Test sPAPR hypervisor calls (aka. h-calls)
3  *
4  * Copyright 2016  Thomas Huth, Red Hat Inc.
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.
7  */
8 #include <libcflat.h>
9 #include <util.h>
10 #include <alloc.h>
11 #include <asm/hcall.h>
12 
13 #define PAGE_SIZE 4096
14 
15 #define H_ZERO_PAGE	(1UL << (63-48))
16 #define H_COPY_PAGE	(1UL << (63-49))
17 
18 #define mfspr(nr) ({ \
19 	uint64_t ret; \
20 	asm volatile("mfspr %0,%1" : "=r"(ret) : "i"(nr)); \
21 	ret; \
22 })
23 
24 #define SPR_SPRG0	0x110
25 
26 /**
27  * Test the H_SET_SPRG0 h-call by setting some values and checking whether
28  * the SPRG0 register contains the correct values afterwards
29  */
30 static void test_h_set_sprg0(int argc, char **argv)
31 {
32 	uint64_t sprg0, sprg0_orig;
33 	int rc;
34 
35 	if (argc > 1)
36 		report_abort("Unsupported argument: '%s'", argv[1]);
37 
38 	sprg0_orig = mfspr(SPR_SPRG0);
39 
40 	rc = hcall(H_SET_SPRG0, 0xcafebabedeadbeefULL);
41 	sprg0 = mfspr(SPR_SPRG0);
42 	report("sprg0 = 0xcafebabedeadbeef",
43 		rc == H_SUCCESS && sprg0 == 0xcafebabedeadbeefULL);
44 
45 	rc = hcall(H_SET_SPRG0, 0xaaaaaaaa55555555ULL);
46 	sprg0 = mfspr(SPR_SPRG0);
47 	report("sprg0 = 0xaaaaaaaa55555555",
48 		rc == H_SUCCESS && sprg0 == 0xaaaaaaaa55555555ULL);
49 
50 	rc = hcall(H_SET_SPRG0, sprg0_orig);
51 	sprg0 = mfspr(SPR_SPRG0);
52 	report("sprg0 = 0x%llx",
53 		rc == H_SUCCESS && sprg0 == sprg0_orig, sprg0_orig);
54 }
55 
56 /**
57  * Test the H_PAGE_INIT h-call by using it to clear and to copy a page, and
58  * by checking for the correct values in the destination page afterwards
59  */
60 static void test_h_page_init(int argc, char **argv)
61 {
62 	u8 *dst, *src;
63 	int rc;
64 
65 	if (argc > 1)
66 		report_abort("Unsupported argument: '%s'", argv[1]);
67 
68 	dst = memalign(PAGE_SIZE, PAGE_SIZE);
69 	src = memalign(PAGE_SIZE, PAGE_SIZE);
70 	if (!dst || !src)
71 		report_abort("Failed to alloc memory");
72 
73 	memset(dst, 0xaa, PAGE_SIZE);
74 	rc = hcall(H_PAGE_INIT, H_ZERO_PAGE, dst, src);
75 	report("h_zero_page", rc == H_SUCCESS && *(uint64_t*)dst == 0);
76 
77 	*(uint64_t*)src = 0xbeefc0dedeadcafeULL;
78 	rc = hcall(H_PAGE_INIT, H_COPY_PAGE, dst, src);
79 	report("h_copy_page",
80 		rc == H_SUCCESS && *(uint64_t*)dst == 0xbeefc0dedeadcafeULL);
81 
82 	*(uint64_t*)src = 0x9abcdef012345678ULL;
83 	rc = hcall(H_PAGE_INIT, H_COPY_PAGE|H_ZERO_PAGE, dst, src);
84 	report("h_copy_page+h_zero_page",
85 		rc == H_SUCCESS &&  *(uint64_t*)dst == 0x9abcdef012345678ULL);
86 
87 	rc = hcall(H_PAGE_INIT, H_ZERO_PAGE, dst + 0x123, src);
88 	report("h_zero_page unaligned dst", rc == H_PARAMETER);
89 
90 	rc = hcall(H_PAGE_INIT, H_COPY_PAGE, dst, src + 0x123);
91 	report("h_copy_page unaligned src", rc == H_PARAMETER);
92 }
93 
94 static int h_random(uint64_t *val)
95 {
96 	register uint64_t r3 asm("r3") = H_RANDOM;
97 	register uint64_t r4 asm("r4");
98 
99 	asm volatile (" sc 1 " : "+r"(r3), "=r"(r4) : "r"(r3));
100 	*val = r4;
101 
102 	return r3;
103 }
104 
105 /**
106  * Test H_RANDOM by calling it a couple of times to check whether all bit
107  * positions really toggle (there should be no "stuck" bits in the output)
108  */
109 static void test_h_random(int argc, char **argv)
110 {
111 	uint64_t rval, val0, val1;
112 	int rc, i;
113 
114 	if (argc > 1)
115 		report_abort("Unsupported argument: '%s'", argv[1]);
116 
117 	/* H_RANDOM is optional - so check for sane return values first */
118 	rc = h_random(&rval);
119 	report_xfail("h-call available", rc == H_FUNCTION, rc == H_SUCCESS);
120 	if (rc != H_SUCCESS)
121 		return;
122 
123 	val0 = 0ULL;
124 	val1 = ~0ULL;
125 
126 	i = 100;
127 	do {
128 		rc = h_random(&rval);
129 		if (rc != H_SUCCESS)
130 			break;
131 		val0 |= rval;
132 		val1 &= rval;
133 	} while (i-- > 0 && (val0 != ~0ULL || val1 != 0ULL));
134 
135 	report("no stuck bits", rc == H_SUCCESS && val0 == ~0ULL && val1 == 0);
136 }
137 
138 struct {
139 	const char *name;
140 	void (*func)(int argc, char **argv);
141 } hctests[] = {
142 	{ "h_set_sprg0", test_h_set_sprg0 },
143 	{ "h_page_init", test_h_page_init },
144 	{ "h_random", test_h_random },
145 	{ NULL, NULL }
146 };
147 
148 int main(int argc, char **argv)
149 {
150 	int all = 0;
151 	int i;
152 
153 	report_prefix_push("hypercall");
154 
155 	if (!argc || (argc == 1 && !strcmp(argv[0], "all")))
156 		all = 1;
157 
158 	for (i = 0; hctests[i].name != NULL; i++) {
159 		report_prefix_push(hctests[i].name);
160 		if (all || strcmp(argv[0], hctests[i].name) == 0) {
161 			hctests[i].func(argc, argv);
162 		}
163 		report_prefix_pop();
164 	}
165 
166 	return report_summary();
167 }
168