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