xref: /openbmc/linux/arch/s390/include/asm/uv.h (revision 2a12187d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Ultravisor Interfaces
4  *
5  * Copyright IBM Corp. 2019, 2022
6  *
7  * Author(s):
8  *	Vasily Gorbik <gor@linux.ibm.com>
9  *	Janosch Frank <frankja@linux.ibm.com>
10  */
11 #ifndef _ASM_S390_UV_H
12 #define _ASM_S390_UV_H
13 
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/sched.h>
18 #include <asm/page.h>
19 #include <asm/gmap.h>
20 
21 #define UVC_CC_OK	0
22 #define UVC_CC_ERROR	1
23 #define UVC_CC_BUSY	2
24 #define UVC_CC_PARTIAL	3
25 
26 #define UVC_RC_EXECUTED		0x0001
27 #define UVC_RC_INV_CMD		0x0002
28 #define UVC_RC_INV_STATE	0x0003
29 #define UVC_RC_INV_LEN		0x0005
30 #define UVC_RC_NO_RESUME	0x0007
31 #define UVC_RC_NEED_DESTROY	0x8000
32 
33 #define UVC_CMD_QUI			0x0001
34 #define UVC_CMD_INIT_UV			0x000f
35 #define UVC_CMD_CREATE_SEC_CONF		0x0100
36 #define UVC_CMD_DESTROY_SEC_CONF	0x0101
37 #define UVC_CMD_DESTROY_SEC_CONF_FAST	0x0102
38 #define UVC_CMD_CREATE_SEC_CPU		0x0120
39 #define UVC_CMD_DESTROY_SEC_CPU		0x0121
40 #define UVC_CMD_CONV_TO_SEC_STOR	0x0200
41 #define UVC_CMD_CONV_FROM_SEC_STOR	0x0201
42 #define UVC_CMD_DESTR_SEC_STOR		0x0202
43 #define UVC_CMD_SET_SEC_CONF_PARAMS	0x0300
44 #define UVC_CMD_UNPACK_IMG		0x0301
45 #define UVC_CMD_VERIFY_IMG		0x0302
46 #define UVC_CMD_CPU_RESET		0x0310
47 #define UVC_CMD_CPU_RESET_INITIAL	0x0311
48 #define UVC_CMD_PREPARE_RESET		0x0320
49 #define UVC_CMD_CPU_RESET_CLEAR		0x0321
50 #define UVC_CMD_CPU_SET_STATE		0x0330
51 #define UVC_CMD_SET_UNSHARE_ALL		0x0340
52 #define UVC_CMD_PIN_PAGE_SHARED		0x0341
53 #define UVC_CMD_UNPIN_PAGE_SHARED	0x0342
54 #define UVC_CMD_DUMP_INIT		0x0400
55 #define UVC_CMD_DUMP_CONF_STOR_STATE	0x0401
56 #define UVC_CMD_DUMP_CPU		0x0402
57 #define UVC_CMD_DUMP_COMPLETE		0x0403
58 #define UVC_CMD_SET_SHARED_ACCESS	0x1000
59 #define UVC_CMD_REMOVE_SHARED_ACCESS	0x1001
60 #define UVC_CMD_RETR_ATTEST		0x1020
61 
62 /* Bits in installed uv calls */
63 enum uv_cmds_inst {
64 	BIT_UVC_CMD_QUI = 0,
65 	BIT_UVC_CMD_INIT_UV = 1,
66 	BIT_UVC_CMD_CREATE_SEC_CONF = 2,
67 	BIT_UVC_CMD_DESTROY_SEC_CONF = 3,
68 	BIT_UVC_CMD_CREATE_SEC_CPU = 4,
69 	BIT_UVC_CMD_DESTROY_SEC_CPU = 5,
70 	BIT_UVC_CMD_CONV_TO_SEC_STOR = 6,
71 	BIT_UVC_CMD_CONV_FROM_SEC_STOR = 7,
72 	BIT_UVC_CMD_SET_SHARED_ACCESS = 8,
73 	BIT_UVC_CMD_REMOVE_SHARED_ACCESS = 9,
74 	BIT_UVC_CMD_SET_SEC_PARMS = 11,
75 	BIT_UVC_CMD_UNPACK_IMG = 13,
76 	BIT_UVC_CMD_VERIFY_IMG = 14,
77 	BIT_UVC_CMD_CPU_RESET = 15,
78 	BIT_UVC_CMD_CPU_RESET_INITIAL = 16,
79 	BIT_UVC_CMD_CPU_SET_STATE = 17,
80 	BIT_UVC_CMD_PREPARE_RESET = 18,
81 	BIT_UVC_CMD_CPU_PERFORM_CLEAR_RESET = 19,
82 	BIT_UVC_CMD_UNSHARE_ALL = 20,
83 	BIT_UVC_CMD_PIN_PAGE_SHARED = 21,
84 	BIT_UVC_CMD_UNPIN_PAGE_SHARED = 22,
85 	BIT_UVC_CMD_DESTROY_SEC_CONF_FAST = 23,
86 	BIT_UVC_CMD_DUMP_INIT = 24,
87 	BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE = 25,
88 	BIT_UVC_CMD_DUMP_CPU = 26,
89 	BIT_UVC_CMD_DUMP_COMPLETE = 27,
90 	BIT_UVC_CMD_RETR_ATTEST = 28,
91 };
92 
93 enum uv_feat_ind {
94 	BIT_UV_FEAT_MISC = 0,
95 	BIT_UV_FEAT_AIV = 1,
96 };
97 
98 struct uv_cb_header {
99 	u16 len;
100 	u16 cmd;	/* Command Code */
101 	u16 rc;		/* Response Code */
102 	u16 rrc;	/* Return Reason Code */
103 } __packed __aligned(8);
104 
105 /* Query Ultravisor Information */
106 struct uv_cb_qui {
107 	struct uv_cb_header header;		/* 0x0000 */
108 	u64 reserved08;				/* 0x0008 */
109 	u64 inst_calls_list[4];			/* 0x0010 */
110 	u64 reserved30[2];			/* 0x0030 */
111 	u64 uv_base_stor_len;			/* 0x0040 */
112 	u64 reserved48;				/* 0x0048 */
113 	u64 conf_base_phys_stor_len;		/* 0x0050 */
114 	u64 conf_base_virt_stor_len;		/* 0x0058 */
115 	u64 conf_virt_var_stor_len;		/* 0x0060 */
116 	u64 cpu_stor_len;			/* 0x0068 */
117 	u32 reserved70[3];			/* 0x0070 */
118 	u32 max_num_sec_conf;			/* 0x007c */
119 	u64 max_guest_stor_addr;		/* 0x0080 */
120 	u8  reserved88[158 - 136];		/* 0x0088 */
121 	u16 max_guest_cpu_id;			/* 0x009e */
122 	u64 uv_feature_indications;		/* 0x00a0 */
123 	u64 reserveda8;				/* 0x00a8 */
124 	u64 supp_se_hdr_versions;		/* 0x00b0 */
125 	u64 supp_se_hdr_pcf;			/* 0x00b8 */
126 	u64 reservedc0;				/* 0x00c0 */
127 	u64 conf_dump_storage_state_len;	/* 0x00c8 */
128 	u64 conf_dump_finalize_len;		/* 0x00d0 */
129 	u64 reservedd8;				/* 0x00d8 */
130 	u64 supp_att_req_hdr_ver;		/* 0x00e0 */
131 	u64 supp_att_pflags;			/* 0x00e8 */
132 	u8 reservedf0[256 - 240];		/* 0x00f0 */
133 } __packed __aligned(8);
134 
135 /* Initialize Ultravisor */
136 struct uv_cb_init {
137 	struct uv_cb_header header;
138 	u64 reserved08[2];
139 	u64 stor_origin;
140 	u64 stor_len;
141 	u64 reserved28[4];
142 } __packed __aligned(8);
143 
144 /* Create Guest Configuration */
145 struct uv_cb_cgc {
146 	struct uv_cb_header header;
147 	u64 reserved08[2];
148 	u64 guest_handle;
149 	u64 conf_base_stor_origin;
150 	u64 conf_virt_stor_origin;
151 	u64 reserved30;
152 	u64 guest_stor_origin;
153 	u64 guest_stor_len;
154 	u64 guest_sca;
155 	u64 guest_asce;
156 	u64 reserved58[5];
157 } __packed __aligned(8);
158 
159 /* Create Secure CPU */
160 struct uv_cb_csc {
161 	struct uv_cb_header header;
162 	u64 reserved08[2];
163 	u64 cpu_handle;
164 	u64 guest_handle;
165 	u64 stor_origin;
166 	u8  reserved30[6];
167 	u16 num;
168 	u64 state_origin;
169 	u64 reserved40[4];
170 } __packed __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 } __packed __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 } __packed __aligned(8);
186 
187 /* Set Secure Config Parameter */
188 struct uv_cb_ssc {
189 	struct uv_cb_header header;
190 	u64 reserved08[2];
191 	u64 guest_handle;
192 	u64 sec_header_origin;
193 	u32 sec_header_len;
194 	u32 reserved2c;
195 	u64 reserved30[4];
196 } __packed __aligned(8);
197 
198 /* Unpack */
199 struct uv_cb_unp {
200 	struct uv_cb_header header;
201 	u64 reserved08[2];
202 	u64 guest_handle;
203 	u64 gaddr;
204 	u64 tweak[2];
205 	u64 reserved38[3];
206 } __packed __aligned(8);
207 
208 #define PV_CPU_STATE_OPR	1
209 #define PV_CPU_STATE_STP	2
210 #define PV_CPU_STATE_CHKSTP	3
211 #define PV_CPU_STATE_OPR_LOAD	5
212 
213 struct uv_cb_cpu_set_state {
214 	struct uv_cb_header header;
215 	u64 reserved08[2];
216 	u64 cpu_handle;
217 	u8  reserved20[7];
218 	u8  state;
219 	u64 reserved28[5];
220 };
221 
222 /*
223  * A common UV call struct for calls that take no payload
224  * Examples:
225  * Destroy cpu/config
226  * Verify
227  */
228 struct uv_cb_nodata {
229 	struct uv_cb_header header;
230 	u64 reserved08[2];
231 	u64 handle;
232 	u64 reserved20[4];
233 } __packed __aligned(8);
234 
235 /* Destroy Configuration Fast */
236 struct uv_cb_destroy_fast {
237 	struct uv_cb_header header;
238 	u64 reserved08[2];
239 	u64 handle;
240 	u64 reserved20[5];
241 } __packed __aligned(8);
242 
243 /* Set Shared Access */
244 struct uv_cb_share {
245 	struct uv_cb_header header;
246 	u64 reserved08[3];
247 	u64 paddr;
248 	u64 reserved28;
249 } __packed __aligned(8);
250 
251 /* Retrieve Attestation Measurement */
252 struct uv_cb_attest {
253 	struct uv_cb_header header;	/* 0x0000 */
254 	u64 reserved08[2];		/* 0x0008 */
255 	u64 arcb_addr;			/* 0x0018 */
256 	u64 cont_token;			/* 0x0020 */
257 	u8  reserved28[6];		/* 0x0028 */
258 	u16 user_data_len;		/* 0x002e */
259 	u8  user_data[256];		/* 0x0030 */
260 	u32 reserved130[3];		/* 0x0130 */
261 	u32 meas_len;			/* 0x013c */
262 	u64 meas_addr;			/* 0x0140 */
263 	u8  config_uid[16];		/* 0x0148 */
264 	u32 reserved158;		/* 0x0158 */
265 	u32 add_data_len;		/* 0x015c */
266 	u64 add_data_addr;		/* 0x0160 */
267 	u64 reserved168[4];		/* 0x0168 */
268 } __packed __aligned(8);
269 
270 struct uv_cb_dump_cpu {
271 	struct uv_cb_header header;
272 	u64 reserved08[2];
273 	u64 cpu_handle;
274 	u64 dump_area_origin;
275 	u64 reserved28[5];
276 } __packed __aligned(8);
277 
278 struct uv_cb_dump_stor_state {
279 	struct uv_cb_header header;
280 	u64 reserved08[2];
281 	u64 config_handle;
282 	u64 dump_area_origin;
283 	u64 gaddr;
284 	u64 reserved28[4];
285 } __packed __aligned(8);
286 
287 struct uv_cb_dump_complete {
288 	struct uv_cb_header header;
289 	u64 reserved08[2];
290 	u64 config_handle;
291 	u64 dump_area_origin;
292 	u64 reserved30[5];
293 } __packed __aligned(8);
294 
295 static inline int __uv_call(unsigned long r1, unsigned long r2)
296 {
297 	int cc;
298 
299 	asm volatile(
300 		"	.insn rrf,0xB9A40000,%[r1],%[r2],0,0\n"
301 		"	ipm	%[cc]\n"
302 		"	srl	%[cc],28\n"
303 		: [cc] "=d" (cc)
304 		: [r1] "a" (r1), [r2] "a" (r2)
305 		: "memory", "cc");
306 	return cc;
307 }
308 
309 static inline int uv_call(unsigned long r1, unsigned long r2)
310 {
311 	int cc;
312 
313 	do {
314 		cc = __uv_call(r1, r2);
315 	} while (cc > 1);
316 	return cc;
317 }
318 
319 /* Low level uv_call that avoids stalls for long running busy conditions  */
320 static inline int uv_call_sched(unsigned long r1, unsigned long r2)
321 {
322 	int cc;
323 
324 	do {
325 		cc = __uv_call(r1, r2);
326 		cond_resched();
327 	} while (cc > 1);
328 	return cc;
329 }
330 
331 /*
332  * special variant of uv_call that only transports the cpu or guest
333  * handle and the command, like destroy or verify.
334  */
335 static inline int uv_cmd_nodata(u64 handle, u16 cmd, u16 *rc, u16 *rrc)
336 {
337 	struct uv_cb_nodata uvcb = {
338 		.header.cmd = cmd,
339 		.header.len = sizeof(uvcb),
340 		.handle = handle,
341 	};
342 	int cc;
343 
344 	WARN(!handle, "No handle provided to Ultravisor call cmd %x\n", cmd);
345 	cc = uv_call_sched(0, (u64)&uvcb);
346 	*rc = uvcb.header.rc;
347 	*rrc = uvcb.header.rrc;
348 	return cc ? -EINVAL : 0;
349 }
350 
351 struct uv_info {
352 	unsigned long inst_calls_list[4];
353 	unsigned long uv_base_stor_len;
354 	unsigned long guest_base_stor_len;
355 	unsigned long guest_virt_base_stor_len;
356 	unsigned long guest_virt_var_stor_len;
357 	unsigned long guest_cpu_stor_len;
358 	unsigned long max_sec_stor_addr;
359 	unsigned int max_num_sec_conf;
360 	unsigned short max_guest_cpu_id;
361 	unsigned long uv_feature_indications;
362 	unsigned long supp_se_hdr_ver;
363 	unsigned long supp_se_hdr_pcf;
364 	unsigned long conf_dump_storage_state_len;
365 	unsigned long conf_dump_finalize_len;
366 	unsigned long supp_att_req_hdr_ver;
367 	unsigned long supp_att_pflags;
368 };
369 
370 extern struct uv_info uv_info;
371 
372 #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST
373 extern int prot_virt_guest;
374 
375 static inline int is_prot_virt_guest(void)
376 {
377 	return prot_virt_guest;
378 }
379 
380 static inline int share(unsigned long addr, u16 cmd)
381 {
382 	struct uv_cb_share uvcb = {
383 		.header.cmd = cmd,
384 		.header.len = sizeof(uvcb),
385 		.paddr = addr
386 	};
387 
388 	if (!is_prot_virt_guest())
389 		return -EOPNOTSUPP;
390 	/*
391 	 * Sharing is page wise, if we encounter addresses that are
392 	 * not page aligned, we assume something went wrong. If
393 	 * malloced structs are passed to this function, we could leak
394 	 * data to the hypervisor.
395 	 */
396 	BUG_ON(addr & ~PAGE_MASK);
397 
398 	if (!uv_call(0, (u64)&uvcb))
399 		return 0;
400 	return -EINVAL;
401 }
402 
403 /*
404  * Guest 2 request to the Ultravisor to make a page shared with the
405  * hypervisor for IO.
406  *
407  * @addr: Real or absolute address of the page to be shared
408  */
409 static inline int uv_set_shared(unsigned long addr)
410 {
411 	return share(addr, UVC_CMD_SET_SHARED_ACCESS);
412 }
413 
414 /*
415  * Guest 2 request to the Ultravisor to make a page unshared.
416  *
417  * @addr: Real or absolute address of the page to be unshared
418  */
419 static inline int uv_remove_shared(unsigned long addr)
420 {
421 	return share(addr, UVC_CMD_REMOVE_SHARED_ACCESS);
422 }
423 
424 #else
425 #define is_prot_virt_guest() 0
426 static inline int uv_set_shared(unsigned long addr) { return 0; }
427 static inline int uv_remove_shared(unsigned long addr) { return 0; }
428 #endif
429 
430 #if IS_ENABLED(CONFIG_KVM)
431 extern int prot_virt_host;
432 
433 static inline int is_prot_virt_host(void)
434 {
435 	return prot_virt_host;
436 }
437 
438 int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb);
439 int gmap_destroy_page(struct gmap *gmap, unsigned long gaddr);
440 int uv_destroy_owned_page(unsigned long paddr);
441 int uv_convert_from_secure(unsigned long paddr);
442 int uv_convert_owned_from_secure(unsigned long paddr);
443 int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr);
444 
445 void setup_uv(void);
446 #else
447 #define is_prot_virt_host() 0
448 static inline void setup_uv(void) {}
449 
450 static inline int uv_destroy_owned_page(unsigned long paddr)
451 {
452 	return 0;
453 }
454 
455 static inline int uv_convert_from_secure(unsigned long paddr)
456 {
457 	return 0;
458 }
459 
460 static inline int uv_convert_owned_from_secure(unsigned long paddr)
461 {
462 	return 0;
463 }
464 #endif
465 
466 #endif /* _ASM_S390_UV_H */
467