xref: /kvm-unit-tests/s390x/uv-guest.c (revision c865f654ffe4c5955038aaf74f702ba62f3eb014)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Guest Ultravisor Call tests
4  *
5  * Copyright (c) 2020 IBM Corp
6  *
7  * Authors:
8  *  Janosch Frank <frankja@linux.ibm.com>
9  */
10 
11 #include <libcflat.h>
12 #include <alloc_page.h>
13 #include <asm/page.h>
14 #include <asm/asm-offsets.h>
15 #include <asm/interrupt.h>
16 #include <asm/facility.h>
17 #include <asm/uv.h>
18 #include <sclp.h>
19 #include <uv.h>
20 
21 static unsigned long page;
22 
23 static void test_priv(void)
24 {
25 	struct uv_cb_header uvcb = {};
26 
27 	report_prefix_push("privileged");
28 
29 	report_prefix_push("query");
30 	uvcb.cmd = UVC_CMD_QUI;
31 	uvcb.len = sizeof(struct uv_cb_qui);
32 	expect_pgm_int();
33 	enter_pstate();
34 	uv_call_once(0, (u64)&uvcb);
35 	check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION);
36 	report_prefix_pop();
37 
38 	report_prefix_push("share");
39 	uvcb.cmd = UVC_CMD_SET_SHARED_ACCESS;
40 	uvcb.len = sizeof(struct uv_cb_share);
41 	expect_pgm_int();
42 	enter_pstate();
43 	uv_call_once(0, (u64)&uvcb);
44 	check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION);
45 	report_prefix_pop();
46 
47 	report_prefix_push("unshare");
48 	uvcb.cmd = UVC_CMD_REMOVE_SHARED_ACCESS;
49 	uvcb.len = sizeof(struct uv_cb_share);
50 	expect_pgm_int();
51 	enter_pstate();
52 	uv_call_once(0, (u64)&uvcb);
53 	check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION);
54 	report_prefix_pop();
55 
56 	report_prefix_pop();
57 }
58 
59 static void test_query(void)
60 {
61 	struct uv_cb_qui uvcb = {
62 		.header.cmd = UVC_CMD_QUI,
63 		/* A dword below the minimum length */
64 		.header.len = 0xa0,
65 	};
66 	int cc;
67 
68 	report_prefix_push("query");
69 	cc = uv_call(0, (u64)&uvcb);
70 	report(cc == 1 && uvcb.header.rc == UVC_RC_INV_LEN, "length");
71 
72 	uvcb.header.len = sizeof(uvcb);
73 	cc = uv_call(0, (u64)&uvcb);
74 	report(cc == 0 && uvcb.header.rc == UVC_RC_EXECUTED, "successful query");
75 
76 	/*
77 	 * These bits have been introduced with the very first
78 	 * Ultravisor version and are expected to always be available
79 	 * because they are basic building blocks.
80 	 */
81 	report(test_bit_inv(BIT_UVC_CMD_QUI, &uvcb.inst_calls_list[0]),
82 	       "query indicated");
83 	report(test_bit_inv(BIT_UVC_CMD_SET_SHARED_ACCESS, &uvcb.inst_calls_list[0]),
84 	       "share indicated");
85 	report(test_bit_inv(BIT_UVC_CMD_REMOVE_SHARED_ACCESS, &uvcb.inst_calls_list[0]),
86 	       "unshare indicated");
87 	report_prefix_pop();
88 }
89 
90 static void test_sharing(void)
91 {
92 	struct uv_cb_share uvcb = {
93 		.header.cmd = UVC_CMD_SET_SHARED_ACCESS,
94 		.header.len = sizeof(uvcb) - 8,
95 		.paddr = page,
96 	};
97 	int cc;
98 
99 	report_prefix_push("share");
100 	cc = uv_call(0, (u64)&uvcb);
101 	report(cc == 1 && uvcb.header.rc == UVC_RC_INV_LEN, "length");
102 	uvcb.header.len = sizeof(uvcb);
103 	cc = uv_call(0, (u64)&uvcb);
104 	report(cc == 0 && uvcb.header.rc == UVC_RC_EXECUTED, "share");
105 	uvcb.paddr = get_ram_size() + PAGE_SIZE;
106 	cc = uv_call(0, (u64)&uvcb);
107 	report(cc == 1 && uvcb.header.rc == 0x101, "invalid memory");
108 	uvcb.paddr = page;
109 	report_prefix_pop();
110 
111 	report_prefix_push("unshare");
112 	uvcb.header.cmd = UVC_CMD_REMOVE_SHARED_ACCESS;
113 	uvcb.header.len -= 8;
114 	cc = uv_call(0, (u64)&uvcb);
115 	report(cc == 1 && uvcb.header.rc == UVC_RC_INV_LEN, "length");
116 	uvcb.header.len = sizeof(uvcb);
117 	cc = uv_call(0, (u64)&uvcb);
118 	report(cc == 0 && uvcb.header.rc == UVC_RC_EXECUTED, "unshare");
119 	report_prefix_pop();
120 
121 	report_prefix_pop();
122 }
123 
124 static struct {
125 	const char *name;
126 	uint16_t cmd;
127 	uint16_t len;
128 	int call_bit;
129 } invalid_cmds[] = {
130 	{ "bogus", 0x4242, sizeof(struct uv_cb_header), -1 },
131 	{ "init", UVC_CMD_INIT_UV, sizeof(struct uv_cb_init), BIT_UVC_CMD_INIT_UV },
132 	{ "create conf", UVC_CMD_CREATE_SEC_CONF, sizeof(struct uv_cb_cgc), BIT_UVC_CMD_CREATE_SEC_CONF },
133 	{ "destroy conf", UVC_CMD_DESTROY_SEC_CONF, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_DESTROY_SEC_CONF },
134 	{ "create cpu", UVC_CMD_CREATE_SEC_CPU, sizeof(struct uv_cb_csc), BIT_UVC_CMD_CREATE_SEC_CPU },
135 	{ "destroy cpu", UVC_CMD_DESTROY_SEC_CPU, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_DESTROY_SEC_CPU },
136 	{ "conv to", UVC_CMD_CONV_TO_SEC_STOR, sizeof(struct uv_cb_cts), BIT_UVC_CMD_CONV_TO_SEC_STOR },
137 	{ "conv from", UVC_CMD_CONV_FROM_SEC_STOR, sizeof(struct uv_cb_cfs), BIT_UVC_CMD_CONV_FROM_SEC_STOR },
138 	{ "set sec conf", UVC_CMD_SET_SEC_CONF_PARAMS, sizeof(struct uv_cb_ssc), BIT_UVC_CMD_SET_SEC_PARMS },
139 	{ "unpack", UVC_CMD_UNPACK_IMG, sizeof(struct uv_cb_unp), BIT_UVC_CMD_UNPACK_IMG },
140 	{ "verify", UVC_CMD_VERIFY_IMG, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_VERIFY_IMG },
141 	{ "cpu reset", UVC_CMD_CPU_RESET, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_CPU_RESET },
142 	{ "cpu initial reset", UVC_CMD_CPU_RESET_INITIAL, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_CPU_RESET_INITIAL },
143 	{ "conf clear reset", UVC_CMD_PERF_CONF_CLEAR_RESET, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_PREPARE_CLEAR_RESET },
144 	{ "cpu clear reset", UVC_CMD_CPU_RESET_CLEAR, sizeof(struct uv_cb_nodata), BIT_UVC_CMD_CPU_PERFORM_CLEAR_RESET },
145 	{ "cpu set state", UVC_CMD_CPU_SET_STATE, sizeof(struct uv_cb_cpu_set_state), BIT_UVC_CMD_CPU_SET_STATE },
146 	{ "pin shared", UVC_CMD_PIN_PAGE_SHARED, sizeof(struct uv_cb_cfs), BIT_UVC_CMD_PIN_PAGE_SHARED },
147 	{ "unpin shared", UVC_CMD_UNPIN_PAGE_SHARED, sizeof(struct uv_cb_cts), BIT_UVC_CMD_UNPIN_PAGE_SHARED },
148 	{ NULL, 0, 0 },
149 };
150 
151 static void test_invalid(void)
152 {
153 	struct uv_cb_header *hdr = (void *)page;
154 	int cc, i;
155 
156 	report_prefix_push("invalid");
157 	for (i = 0; invalid_cmds[i].name; i++) {
158 		hdr->cmd = invalid_cmds[i].cmd;
159 		hdr->len = invalid_cmds[i].len;
160 		cc = uv_call(0, (u64)hdr);
161 		report(cc == 1 && hdr->rc == UVC_RC_INV_CMD &&
162 		       (invalid_cmds[i].call_bit == -1 || !uv_query_test_call(invalid_cmds[i].call_bit)),
163 		       "%s", invalid_cmds[i].name);
164 	}
165 	report_prefix_pop();
166 }
167 
168 int main(void)
169 {
170 	bool has_uvc = test_facility(158);
171 
172 	report_prefix_push("uvc");
173 	if (!has_uvc) {
174 		report_skip("Ultravisor call facility is not available");
175 		goto done;
176 	}
177 
178 	if (!uv_os_is_guest()) {
179 		report_skip("Not a protected guest");
180 		goto done;
181 	}
182 
183 	page = (unsigned long)alloc_page();
184 	test_priv();
185 	test_invalid();
186 	test_query();
187 	test_sharing();
188 	free_page((void *)page);
189 done:
190 	report_prefix_pop();
191 	return report_summary();
192 }
193