xref: /kvm-unit-tests/lib/s390x/asm/uv.h (revision 4c8a99ca02252d4a2bee43f4558fe47ce5ab7ec0)
1 /*
2  * s390x Ultravisor related definitions
3  *
4  * Copyright IBM Corp. 2020, 2022
5  *
6  * Authors:
7  *  Janosch Frank <frankja@linux.ibm.com>
8  *
9  * This code is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2.
11  */
12 #ifndef _ASMS390X_UV_H_
13 #define _ASMS390X_UV_H_
14 
15 /* Enables printing of command code and return codes for failed UVCs */
16 #ifndef UVC_ERR_DEBUG
17 #define UVC_ERR_DEBUG	0
18 #endif
19 
20 #define UVC_RC_EXECUTED		0x0001
21 #define UVC_RC_INV_CMD		0x0002
22 #define UVC_RC_INV_STATE	0x0003
23 #define UVC_RC_INV_LEN		0x0005
24 #define UVC_RC_NO_RESUME	0x0007
25 #define UVC_RC_INV_GHANDLE	0x0020
26 #define UVC_RC_INV_CHANDLE	0x0021
27 
28 #define UVC_CMD_QUI			0x0001
29 #define UVC_CMD_INIT_UV			0x000f
30 #define UVC_CMD_CREATE_SEC_CONF		0x0100
31 #define UVC_CMD_DESTROY_SEC_CONF	0x0101
32 #define UVC_CMD_CREATE_SEC_CPU		0x0120
33 #define UVC_CMD_DESTROY_SEC_CPU		0x0121
34 #define UVC_CMD_CONV_TO_SEC_STOR	0x0200
35 #define UVC_CMD_CONV_FROM_SEC_STOR	0x0201
36 #define UVC_CMD_DESTR_SEC_STOR		0x0202
37 #define UVC_CMD_SET_SEC_CONF_PARAMS	0x0300
38 #define UVC_CMD_UNPACK_IMG		0x0301
39 #define UVC_CMD_VERIFY_IMG		0x0302
40 #define UVC_CMD_CPU_RESET		0x0310
41 #define UVC_CMD_CPU_RESET_INITIAL	0x0311
42 #define UVC_CMD_PREPARE_RESET		0x0320
43 #define UVC_CMD_CPU_RESET_CLEAR		0x0321
44 #define UVC_CMD_CPU_SET_STATE		0x0330
45 #define UVC_CMD_SET_UNSHARED_ALL	0x0340
46 #define UVC_CMD_PIN_PAGE_SHARED		0x0341
47 #define UVC_CMD_UNPIN_PAGE_SHARED	0x0342
48 #define UVC_CMD_SET_SHARED_ACCESS	0x1000
49 #define UVC_CMD_REMOVE_SHARED_ACCESS	0x1001
50 #define UVC_CMD_ATTESTATION		0x1020
51 
52 /* Bits in installed uv calls */
53 enum uv_cmds_inst {
54 	BIT_UVC_CMD_QUI = 0,
55 	BIT_UVC_CMD_INIT_UV = 1,
56 	BIT_UVC_CMD_CREATE_SEC_CONF = 2,
57 	BIT_UVC_CMD_DESTROY_SEC_CONF = 3,
58 	BIT_UVC_CMD_CREATE_SEC_CPU = 4,
59 	BIT_UVC_CMD_DESTROY_SEC_CPU = 5,
60 	BIT_UVC_CMD_CONV_TO_SEC_STOR = 6,
61 	BIT_UVC_CMD_CONV_FROM_SEC_STOR = 7,
62 	BIT_UVC_CMD_SET_SHARED_ACCESS = 8,
63 	BIT_UVC_CMD_REMOVE_SHARED_ACCESS = 9,
64 	BIT_UVC_CMD_SET_SEC_PARMS = 11,
65 	BIT_UVC_CMD_UNPACK_IMG = 13,
66 	BIT_UVC_CMD_VERIFY_IMG = 14,
67 	BIT_UVC_CMD_CPU_RESET = 15,
68 	BIT_UVC_CMD_CPU_RESET_INITIAL = 16,
69 	BIT_UVC_CMD_CPU_SET_STATE = 17,
70 	BIT_UVC_CMD_PREPARE_RESET = 18,
71 	BIT_UVC_CMD_CPU_PERFORM_CLEAR_RESET = 19,
72 	BIT_UVC_CMD_UNSHARE_ALL = 20,
73 	BIT_UVC_CMD_PIN_PAGE_SHARED = 21,
74 	BIT_UVC_CMD_UNPIN_PAGE_SHARED = 22,
75 	BIT_UVC_CMD_ATTESTATION = 28,
76 };
77 
78 struct uv_cb_header {
79 	u16 len;
80 	u16 cmd;	/* Command Code */
81 	u16 rc;		/* Response Code */
82 	u16 rrc;	/* Return Reason Code */
83 } __attribute__((packed))  __attribute__((aligned(8)));
84 
85 struct uv_cb_init {
86 	struct uv_cb_header header;
87 	u64 reserved08[2];
88 	u64 stor_origin;
89 	u64 stor_len;
90 	u64 reserved28[4];
91 
92 } __attribute__((packed))  __attribute__((aligned(8)));
93 
94 struct uv_cb_qui {
95 	struct uv_cb_header header;	/* 0x0000 */
96 	u64 reserved08;			/* 0x0008 */
97 	u64 inst_calls_list[4];		/* 0x0010 */
98 	u64 reserved30[2];		/* 0x0030 */
99 	u64 uv_base_stor_len;		/* 0x0040 */
100 	u64 reserved48;			/* 0x0048 */
101 	u64 conf_base_phys_stor_len;	/* 0x0050 */
102 	u64 conf_base_virt_stor_len;	/* 0x0058 */
103 	u64 conf_virt_var_stor_len;	/* 0x0060 */
104 	u64 cpu_stor_len;		/* 0x0068 */
105 	u32 reserved70[3];		/* 0x0070 */
106 	u32 max_num_sec_conf;		/* 0x007c */
107 	u64 max_guest_stor_addr;	/* 0x0080 */
108 	u8  reserved88[158 - 136];	/* 0x0088 */
109 	uint16_t max_guest_cpus;	/* 0x009e */
110 	u64 uv_feature_indications;	/* 0x00a0 */
111 	uint8_t  reserveda8[224 - 168];	/* 0x00a8 */
112 	uint64_t supp_att_hdr_ver;	/* 0x00e0 */
113 	uint64_t supp_paf;		/* 0x00e8 */
114 	uint8_t  reservedf0[256 - 240];	/* 0x00f0 */
115 }  __attribute__((packed))  __attribute__((aligned(8)));
116 
117 struct uv_cb_cgc {
118 	struct uv_cb_header header;
119 	u64 reserved08[2];
120 	u64 guest_handle;
121 	u64 conf_base_stor_origin;
122 	u64 conf_var_stor_origin;
123 	u64 reserved30;
124 	u64 guest_stor_origin;
125 	u64 guest_stor_len;
126 	u64 guest_sca;
127 	u64 guest_asce;
128 	u64 reserved60[5];
129 } __attribute__((packed))  __attribute__((aligned(8)));
130 
131 struct uv_cb_csc {
132 	struct uv_cb_header header;
133 	u64 reserved08[2];
134 	u64 cpu_handle;
135 	u64 guest_handle;
136 	u64 stor_origin;
137 	u8  reserved30[6];
138 	u16 num;
139 	u64 state_origin;
140 	u64 reserved[4];
141 } __attribute__((packed))  __attribute__((aligned(8)));
142 
143 struct uv_cb_unp {
144 	struct uv_cb_header header;
145 	u64 reserved08[2];
146 	u64 guest_handle;
147 	u64 gaddr;
148 	u64 tweak[2];
149 	u64 reserved38[3];
150 } __attribute__((packed))  __attribute__((aligned(8)));
151 
152 /*
153  * A common UV call struct for the following calls:
154  * Destroy cpu/config
155  * Verify
156  */
157 struct uv_cb_nodata {
158 	struct uv_cb_header header;
159 	u64 reserved08[2];
160 	u64 handle;
161 	u64 reserved20[4];
162 }  __attribute__((packed))  __attribute__((aligned(8)));
163 
164 struct uv_cb_share {
165 	struct uv_cb_header header;
166 	u64 reserved08[3];
167 	u64 paddr;
168 	u64 reserved28;
169 } __attribute__((packed))  __attribute__((aligned(8)));
170 
171 /* Convert to Secure */
172 struct uv_cb_cts {
173 	struct uv_cb_header header;
174 	u64 reserved08[2];
175 	u64 guest_handle;
176 	u64 gaddr;
177 }  __attribute__((packed))  __attribute__((aligned(8)));
178 
179 /* Convert from Secure / Pin Page Shared */
180 struct uv_cb_cfs {
181 	struct uv_cb_header header;
182 	u64 reserved08[2];
183 	u64 paddr;
184 }  __attribute__((packed))  __attribute__((aligned(8)));
185 
186 /* Retrieve Attestation Measurement */
187 struct uv_cb_attest {
188 	struct uv_cb_header header;	/* 0x0000 */
189 	uint64_t reserved08[2];		/* 0x0008 */
190 	uint64_t arcb_addr;		/* 0x0018 */
191 	uint64_t continuation_token;	/* 0x0020 */
192 	uint8_t  reserved28[6];		/* 0x0028 */
193 	uint16_t user_data_length;	/* 0x002e */
194 	uint8_t  user_data[256];	/* 0x0030 */
195 	uint32_t reserved130[3];	/* 0x0130 */
196 	uint32_t measurement_length;	/* 0x013c */
197 	uint64_t measurement_address;	/* 0x0140 */
198 	uint8_t  config_uid[16];	/* 0x0148 */
199 	uint32_t reserved158;		/* 0x0158 */
200 	uint32_t add_data_length;	/* 0x015c */
201 	uint64_t add_data_address;	/* 0x0160 */
202 	uint64_t reserved168[4];	/* 0x0168 */
203 }  __attribute__((packed))  __attribute__((aligned(8)));
204 
205 /* Set Secure Config Parameter */
206 struct uv_cb_ssc {
207 	struct uv_cb_header header;
208 	u64 reserved08[2];
209 	u64 guest_handle;
210 	u64 sec_header_origin;
211 	u32 sec_header_len;
212 	u32 reserved2c;
213 	u64 reserved30[4];
214 } __attribute__((packed))  __attribute__((aligned(8)));
215 
216 static inline int uv_call_once(unsigned long r1, unsigned long r2)
217 {
218 	int cc;
219 
220 	asm volatile(
221 		"0:	.insn rrf,0xB9A40000,%[r1],%[r2],0,0\n"
222 		"		ipm	%[cc]\n"
223 		"		srl	%[cc],28\n"
224 		: [cc] "=d" (cc)
225 		: [r1] "a" (r1), [r2] "a" (r2)
226 		: "memory", "cc");
227 
228 	if (UVC_ERR_DEBUG && cc == 1)
229 		printf("UV call error: call %x rc %x rrc %x\n",
230 		       ((struct uv_cb_header *)r2)->cmd,
231 		       ((struct uv_cb_header *)r2)->rc,
232 		       ((struct uv_cb_header *)r2)->rrc);
233 
234 	return cc;
235 }
236 
237 static inline int uv_call(unsigned long r1, unsigned long r2)
238 {
239 	int cc;
240 
241 	/*
242 	 * CC 2 and 3 tell us to re-execute because the instruction
243 	 * hasn't yet finished.
244 	 */
245 	do {
246 		cc = uv_call_once(r1, r2);
247 	} while (cc > 1);
248 
249 	return cc;
250 }
251 
252 static inline int share(unsigned long addr, u16 cmd)
253 {
254 	struct uv_cb_share uvcb = {
255 		.header.cmd = cmd,
256 		.header.len = sizeof(uvcb),
257 		.paddr = addr
258 	};
259 
260 	return uv_call(0, (u64)&uvcb);
261 }
262 
263 /*
264  * Guest 2 request to the Ultravisor to make a page shared with the
265  * hypervisor for IO.
266  *
267  * @addr: Real or absolute address of the page to be shared
268  */
269 static inline int uv_set_shared(unsigned long addr)
270 {
271 	return share(addr, UVC_CMD_SET_SHARED_ACCESS);
272 }
273 
274 /*
275  * Guest 2 request to the Ultravisor to make a page unshared.
276  *
277  * @addr: Real or absolute address of the page to be unshared
278  */
279 static inline int uv_remove_shared(unsigned long addr)
280 {
281 	return share(addr, UVC_CMD_REMOVE_SHARED_ACCESS);
282 }
283 
284 static inline int uv_cmd_nodata(uint64_t handle, uint16_t cmd, uint16_t *rc, uint16_t *rrc)
285 {
286 	struct uv_cb_nodata uvcb = {
287 		.header.cmd = cmd,
288 		.header.len = sizeof(uvcb),
289 		.handle = handle,
290 	};
291 	int cc;
292 
293 	assert(handle);
294 	cc = uv_call(0, (uint64_t)&uvcb);
295 	*rc = uvcb.header.rc;
296 	*rrc = uvcb.header.rrc;
297 	return cc;
298 }
299 
300 static inline int uv_import(uint64_t handle, unsigned long gaddr)
301 {
302 	struct uv_cb_cts uvcb = {
303 		.header.cmd = UVC_CMD_CONV_TO_SEC_STOR,
304 		.header.len = sizeof(uvcb),
305 		.guest_handle = handle,
306 		.gaddr = gaddr,
307 	};
308 
309 	return uv_call(0, (uint64_t)&uvcb);
310 }
311 
312 static inline int uv_export(unsigned long paddr)
313 {
314 	struct uv_cb_cfs uvcb = {
315 		.header.cmd = UVC_CMD_CONV_FROM_SEC_STOR,
316 		.header.len = sizeof(uvcb),
317 		.paddr = paddr
318 	};
319 
320 	return uv_call(0, (u64)&uvcb);
321 }
322 
323 /*
324  * Requests the Ultravisor to destroy a guest page and make it
325  * accessible to the host. The destroy clears the page instead of
326  * exporting.
327  *
328  * @paddr: Absolute host address of page to be destroyed
329  */
330 static inline int uv_destroy_page(unsigned long paddr)
331 {
332 	struct uv_cb_cfs uvcb = {
333 		.header.cmd = UVC_CMD_DESTR_SEC_STOR,
334 		.header.len = sizeof(uvcb),
335 		.paddr = paddr
336 	};
337 
338 	return uv_call(0, (uint64_t)&uvcb);
339 }
340 
341 struct uv_cb_cpu_set_state {
342 	struct uv_cb_header header;
343 	u64 reserved08[2];
344 	u64 cpu_handle;
345 	u8  reserved20[7];
346 	u8  state;
347 	u64 reserved28[5];
348 };
349 
350 #define PV_CPU_STATE_OPR	1
351 #define PV_CPU_STATE_STP	2
352 #define PV_CPU_STATE_CHKSTP	3
353 #define PV_CPU_STATE_OPR_LOAD	5
354 
355 static inline int uv_set_cpu_state(uint64_t handle, uint8_t state)
356 {
357 	struct uv_cb_cpu_set_state uvcb = {
358 		.header.cmd = UVC_CMD_CPU_SET_STATE,
359 		.header.len = sizeof(uvcb),
360 		.cpu_handle = handle,
361 		.state = state,
362 	};
363 
364 	assert(handle);
365 	return uv_call(0, (uint64_t)&uvcb);
366 }
367 
368 static inline int uv_set_se_hdr(uint64_t handle, void *hdr, size_t len)
369 {
370 	struct uv_cb_ssc uvcb = {
371 		.header.cmd = UVC_CMD_SET_SEC_CONF_PARAMS,
372 		.header.len = sizeof(uvcb),
373 		.sec_header_origin = (uint64_t)hdr,
374 		.sec_header_len = len,
375 		.guest_handle = handle,
376 	};
377 
378 	assert(handle);
379 	return uv_call(0, (uint64_t)&uvcb);
380 }
381 
382 static inline int uv_unp_page(uint64_t handle, uint64_t gaddr, uint64_t tweak1, uint64_t tweak2)
383 {
384 	struct uv_cb_unp uvcb = {
385 		.header.cmd = UVC_CMD_UNPACK_IMG,
386 		.header.len = sizeof(uvcb),
387 		.guest_handle = handle,
388 		.gaddr = gaddr,
389 		.tweak[0] = tweak1,
390 		.tweak[1] = tweak2,
391 	};
392 
393 	return uv_call(0, (uint64_t)&uvcb);
394 }
395 
396 #endif
397