1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Ultravisor Interfaces
5 * Copyright IBM Corp. 2019, 2024
11 #ifndef _ASM_S390_UV_H
12 #define _ASM_S390_UV_H
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/sched.h>
23 #define UVC_CC_ERROR 1
25 #define UVC_CC_PARTIAL 3
27 #define UVC_RC_EXECUTED 0x0001
28 #define UVC_RC_INV_CMD 0x0002
29 #define UVC_RC_INV_STATE 0x0003
30 #define UVC_RC_INV_LEN 0x0005
31 #define UVC_RC_NO_RESUME 0x0007
32 #define UVC_RC_MORE_DATA 0x0100
33 #define UVC_RC_NEED_DESTROY 0x8000
35 #define UVC_CMD_QUI 0x0001
36 #define UVC_CMD_QUERY_KEYS 0x0002
37 #define UVC_CMD_INIT_UV 0x000f
38 #define UVC_CMD_CREATE_SEC_CONF 0x0100
39 #define UVC_CMD_DESTROY_SEC_CONF 0x0101
40 #define UVC_CMD_DESTROY_SEC_CONF_FAST 0x0102
41 #define UVC_CMD_CREATE_SEC_CPU 0x0120
42 #define UVC_CMD_DESTROY_SEC_CPU 0x0121
43 #define UVC_CMD_CONV_TO_SEC_STOR 0x0200
44 #define UVC_CMD_CONV_FROM_SEC_STOR 0x0201
45 #define UVC_CMD_DESTR_SEC_STOR 0x0202
46 #define UVC_CMD_SET_SEC_CONF_PARAMS 0x0300
47 #define UVC_CMD_UNPACK_IMG 0x0301
48 #define UVC_CMD_VERIFY_IMG 0x0302
49 #define UVC_CMD_CPU_RESET 0x0310
50 #define UVC_CMD_CPU_RESET_INITIAL 0x0311
51 #define UVC_CMD_PREPARE_RESET 0x0320
52 #define UVC_CMD_CPU_RESET_CLEAR 0x0321
53 #define UVC_CMD_CPU_SET_STATE 0x0330
54 #define UVC_CMD_SET_UNSHARE_ALL 0x0340
55 #define UVC_CMD_PIN_PAGE_SHARED 0x0341
56 #define UVC_CMD_UNPIN_PAGE_SHARED 0x0342
57 #define UVC_CMD_DUMP_INIT 0x0400
58 #define UVC_CMD_DUMP_CONF_STOR_STATE 0x0401
59 #define UVC_CMD_DUMP_CPU 0x0402
60 #define UVC_CMD_DUMP_COMPLETE 0x0403
61 #define UVC_CMD_SET_SHARED_ACCESS 0x1000
62 #define UVC_CMD_REMOVE_SHARED_ACCESS 0x1001
63 #define UVC_CMD_RETR_ATTEST 0x1020
64 #define UVC_CMD_ADD_SECRET 0x1031
65 #define UVC_CMD_LIST_SECRETS 0x1033
66 #define UVC_CMD_LOCK_SECRETS 0x1034
67 #define UVC_CMD_RETR_SECRET 0x1035
69 /* Bits in installed uv calls */
72 BIT_UVC_CMD_INIT_UV = 1,
73 BIT_UVC_CMD_CREATE_SEC_CONF = 2,
74 BIT_UVC_CMD_DESTROY_SEC_CONF = 3,
75 BIT_UVC_CMD_CREATE_SEC_CPU = 4,
76 BIT_UVC_CMD_DESTROY_SEC_CPU = 5,
77 BIT_UVC_CMD_CONV_TO_SEC_STOR = 6,
78 BIT_UVC_CMD_CONV_FROM_SEC_STOR = 7,
79 BIT_UVC_CMD_SET_SHARED_ACCESS = 8,
80 BIT_UVC_CMD_REMOVE_SHARED_ACCESS = 9,
81 BIT_UVC_CMD_SET_SEC_PARMS = 11,
82 BIT_UVC_CMD_UNPACK_IMG = 13,
83 BIT_UVC_CMD_VERIFY_IMG = 14,
84 BIT_UVC_CMD_CPU_RESET = 15,
85 BIT_UVC_CMD_CPU_RESET_INITIAL = 16,
86 BIT_UVC_CMD_CPU_SET_STATE = 17,
87 BIT_UVC_CMD_PREPARE_RESET = 18,
88 BIT_UVC_CMD_CPU_PERFORM_CLEAR_RESET = 19,
89 BIT_UVC_CMD_UNSHARE_ALL = 20,
90 BIT_UVC_CMD_PIN_PAGE_SHARED = 21,
91 BIT_UVC_CMD_UNPIN_PAGE_SHARED = 22,
92 BIT_UVC_CMD_DESTROY_SEC_CONF_FAST = 23,
93 BIT_UVC_CMD_DUMP_INIT = 24,
94 BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE = 25,
95 BIT_UVC_CMD_DUMP_CPU = 26,
96 BIT_UVC_CMD_DUMP_COMPLETE = 27,
97 BIT_UVC_CMD_RETR_ATTEST = 28,
98 BIT_UVC_CMD_ADD_SECRET = 29,
99 BIT_UVC_CMD_LIST_SECRETS = 30,
100 BIT_UVC_CMD_LOCK_SECRETS = 31,
101 BIT_UVC_CMD_RETR_SECRET = 33,
102 BIT_UVC_CMD_QUERY_KEYS = 34,
106 BIT_UV_FEAT_MISC = 0,
109 BIT_UV_FEAT_AP_INTR = 5,
112 struct uv_cb_header {
114 u16 cmd; /* Command Code */
115 u16 rc; /* Response Code */
116 u16 rrc; /* Return Reason Code */
117 } __packed __aligned(8);
119 /* Query Ultravisor Information */
121 struct uv_cb_header header; /* 0x0000 */
122 u64 reserved08; /* 0x0008 */
123 u64 inst_calls_list[4]; /* 0x0010 */
124 u64 reserved30[2]; /* 0x0030 */
125 u64 uv_base_stor_len; /* 0x0040 */
126 u64 reserved48; /* 0x0048 */
127 u64 conf_base_phys_stor_len; /* 0x0050 */
128 u64 conf_base_virt_stor_len; /* 0x0058 */
129 u64 conf_virt_var_stor_len; /* 0x0060 */
130 u64 cpu_stor_len; /* 0x0068 */
131 u32 reserved70[3]; /* 0x0070 */
132 u32 max_num_sec_conf; /* 0x007c */
133 u64 max_guest_stor_addr; /* 0x0080 */
134 u8 reserved88[0x9e - 0x88]; /* 0x0088 */
135 u16 max_guest_cpu_id; /* 0x009e */
136 u64 uv_feature_indications; /* 0x00a0 */
137 u64 reserveda8; /* 0x00a8 */
138 u64 supp_se_hdr_versions; /* 0x00b0 */
139 u64 supp_se_hdr_pcf; /* 0x00b8 */
140 u64 reservedc0; /* 0x00c0 */
141 u64 conf_dump_storage_state_len; /* 0x00c8 */
142 u64 conf_dump_finalize_len; /* 0x00d0 */
143 u64 reservedd8; /* 0x00d8 */
144 u64 supp_att_req_hdr_ver; /* 0x00e0 */
145 u64 supp_att_pflags; /* 0x00e8 */
146 u64 reservedf0; /* 0x00f0 */
147 u64 supp_add_secret_req_ver; /* 0x00f8 */
148 u64 supp_add_secret_pcf; /* 0x0100 */
149 u64 supp_secret_types; /* 0x0108 */
150 u16 max_assoc_secrets; /* 0x0110 */
151 u16 max_retr_secrets; /* 0x0112 */
152 u8 reserved114[0x120 - 0x114]; /* 0x0114 */
153 } __packed __aligned(8);
157 } __packed __aligned(8);
159 #define UVC_QUERY_KEYS_IDX_HK 0
160 #define UVC_QUERY_KEYS_IDX_BACK_HK 1
162 /* Query Ultravisor Keys */
163 struct uv_cb_query_keys {
164 struct uv_cb_header header; /* 0x0000 */
165 u64 reserved08[3]; /* 0x0008 */
166 struct uv_key_hash key_hashes[15]; /* 0x0020 */
167 } __packed __aligned(8);
168 static_assert(sizeof(struct uv_cb_query_keys) == 0x200);
170 /* Initialize Ultravisor */
172 struct uv_cb_header header;
177 } __packed __aligned(8);
179 /* Create Guest Configuration */
181 struct uv_cb_header header;
184 u64 conf_base_stor_origin;
185 u64 conf_virt_stor_origin;
190 u16 ap_instr_intr : 1;
191 u16 ap_allow_instr : 1;
195 u64 guest_stor_origin;
200 } __packed __aligned(8);
202 /* Create Secure CPU */
204 struct uv_cb_header header;
213 } __packed __aligned(8);
215 /* Convert to Secure */
217 struct uv_cb_header header;
221 } __packed __aligned(8);
223 /* Convert from Secure / Pin Page Shared */
225 struct uv_cb_header header;
228 } __packed __aligned(8);
230 /* Set Secure Config Parameter */
232 struct uv_cb_header header;
235 u64 sec_header_origin;
239 } __packed __aligned(8);
243 struct uv_cb_header header;
249 } __packed __aligned(8);
251 #define PV_CPU_STATE_OPR 1
252 #define PV_CPU_STATE_STP 2
253 #define PV_CPU_STATE_CHKSTP 3
254 #define PV_CPU_STATE_OPR_LOAD 5
256 struct uv_cb_cpu_set_state {
257 struct uv_cb_header header;
266 * A common UV call struct for calls that take no payload
271 struct uv_cb_nodata {
272 struct uv_cb_header header;
276 } __packed __aligned(8);
278 /* Destroy Configuration Fast */
279 struct uv_cb_destroy_fast {
280 struct uv_cb_header header;
284 } __packed __aligned(8);
286 /* Set Shared Access */
288 struct uv_cb_header header;
292 } __packed __aligned(8);
294 /* Retrieve Attestation Measurement */
295 struct uv_cb_attest {
296 struct uv_cb_header header; /* 0x0000 */
297 u64 reserved08[2]; /* 0x0008 */
298 u64 arcb_addr; /* 0x0018 */
299 u64 cont_token; /* 0x0020 */
300 u8 reserved28[6]; /* 0x0028 */
301 u16 user_data_len; /* 0x002e */
302 u8 user_data[256]; /* 0x0030 */
303 u32 reserved130[3]; /* 0x0130 */
304 u32 meas_len; /* 0x013c */
305 u64 meas_addr; /* 0x0140 */
306 u8 config_uid[16]; /* 0x0148 */
307 u32 reserved158; /* 0x0158 */
308 u32 add_data_len; /* 0x015c */
309 u64 add_data_addr; /* 0x0160 */
310 u64 reserved168[4]; /* 0x0168 */
311 } __packed __aligned(8);
313 struct uv_cb_dump_cpu {
314 struct uv_cb_header header;
317 u64 dump_area_origin;
319 } __packed __aligned(8);
321 struct uv_cb_dump_stor_state {
322 struct uv_cb_header header;
325 u64 dump_area_origin;
328 } __packed __aligned(8);
330 struct uv_cb_dump_complete {
331 struct uv_cb_header header;
334 u64 dump_area_origin;
336 } __packed __aligned(8);
339 * A common UV call struct for pv guests that contains a single address
343 struct uv_cb_guest_addr {
344 struct uv_cb_header header;
348 } __packed __aligned(8);
350 #define UVC_RC_RETR_SECR_BUF_SMALL 0x0109
351 #define UVC_RC_RETR_SECR_STORE_EMPTY 0x010f
352 #define UVC_RC_RETR_SECR_INV_IDX 0x0110
353 #define UVC_RC_RETR_SECR_INV_SECRET 0x0111
355 struct uv_cb_retr_secr {
356 struct uv_cb_header header;
363 } __packed __aligned(8);
365 struct uv_cb_list_secrets {
366 struct uv_cb_header header;
372 } __packed __aligned(8);
374 enum uv_secret_types {
375 UV_SECRET_INVAL = 0x0,
376 UV_SECRET_NULL = 0x1,
377 UV_SECRET_ASSOCIATION = 0x2,
378 UV_SECRET_PLAIN = 0x3,
379 UV_SECRET_AES_128 = 0x4,
380 UV_SECRET_AES_192 = 0x5,
381 UV_SECRET_AES_256 = 0x6,
382 UV_SECRET_AES_XTS_128 = 0x7,
383 UV_SECRET_AES_XTS_256 = 0x8,
384 UV_SECRET_HMAC_SHA_256 = 0x9,
385 UV_SECRET_HMAC_SHA_512 = 0xa,
386 /* 0x0b - 0x10 reserved */
387 UV_SECRET_ECDSA_P256 = 0x11,
388 UV_SECRET_ECDSA_P384 = 0x12,
389 UV_SECRET_ECDSA_P521 = 0x13,
390 UV_SECRET_ECDSA_ED25519 = 0x14,
391 UV_SECRET_ECDSA_ED448 = 0x15,
395 * uv_secret_list_item_hdr - UV secret metadata.
396 * @index: Index of the secret in the secret list.
397 * @type: Type of the secret. See `enum uv_secret_types`.
398 * @length: Length of the stored secret.
400 struct uv_secret_list_item_hdr {
404 } __packed __aligned(8);
406 #define UV_SECRET_ID_LEN 32
408 * uv_secret_list_item - UV secret entry.
409 * @hdr: The metadata of this secret.
410 * @id: The ID of this secret, not the secret itself.
412 struct uv_secret_list_item {
413 struct uv_secret_list_item_hdr hdr;
415 u8 id[UV_SECRET_ID_LEN];
416 } __packed __aligned(8);
419 * uv_secret_list - UV secret-metadata list.
420 * @num_secr_stored: Number of secrets stored in this list.
421 * @total_num_secrets: Number of secrets stored in the UV for this guest.
422 * @next_secret_idx: positive number if there are more secrets available or zero.
423 * @secrets: Up to 85 UV-secret metadata entries.
425 struct uv_secret_list {
427 u16 total_num_secrets;
431 struct uv_secret_list_item secrets[85];
432 } __packed __aligned(8);
433 static_assert(sizeof(struct uv_secret_list) == PAGE_SIZE);
435 static inline int __uv_call(unsigned long r1, unsigned long r2)
440 " .insn rrf,0xb9a40000,%[r1],%[r2],0,0\n"
443 : [r1] "a" (r1), [r2] "a" (r2)
444 : CC_CLOBBER_LIST("memory"));
445 return CC_TRANSFORM(cc);
448 static inline int uv_call(unsigned long r1, unsigned long r2)
453 cc = __uv_call(r1, r2);
458 /* Low level uv_call that avoids stalls for long running busy conditions */
459 static inline int uv_call_sched(unsigned long r1, unsigned long r2)
464 cc = __uv_call(r1, r2);
471 * special variant of uv_call that only transports the cpu or guest
472 * handle and the command, like destroy or verify.
474 static inline int uv_cmd_nodata(u64 handle, u16 cmd, u16 *rc, u16 *rrc)
476 struct uv_cb_nodata uvcb = {
478 .header.len = sizeof(uvcb),
483 WARN(!handle, "No handle provided to Ultravisor call cmd %x\n", cmd);
484 cc = uv_call_sched(0, (u64)&uvcb);
485 *rc = uvcb.header.rc;
486 *rrc = uvcb.header.rrc;
487 return cc ? -EINVAL : 0;
491 * uv_list_secrets() - Do a List Secrets UVC.
493 * @buf: Buffer to write list into; size of one page.
494 * @start_idx: The smallest index that should be included in the list.
495 * For the fist invocation use 0.
496 * @rc: Pointer to store the return code or NULL.
497 * @rrc: Pointer to store the return reason code or NULL.
499 * This function calls the List Secrets UVC. The result is written into `buf`,
500 * that needs to be at least one page of writable memory.
502 * * %struct uv_secret_list_hdr
503 * * %struct uv_secret_list_item (multiple)
505 * For `start_idx` use _0_ for the first call. If there are more secrets available
506 * but could not fit into the page then `rc` is `UVC_RC_MORE_DATA`.
507 * In this case use `uv_secret_list_hdr.next_secret_idx` for `start_idx`.
509 * Context: might sleep.
511 * Return: The UVC condition code.
513 static inline int uv_list_secrets(struct uv_secret_list *buf, u16 start_idx,
516 struct uv_cb_list_secrets uvcb = {
517 .header.len = sizeof(uvcb),
518 .header.cmd = UVC_CMD_LIST_SECRETS,
519 .start_idx = start_idx,
520 .list_addr = (u64)buf,
522 int cc = uv_call_sched(0, (u64)&uvcb);
525 *rc = uvcb.header.rc;
527 *rrc = uvcb.header.rrc;
533 unsigned long inst_calls_list[4];
534 unsigned long uv_base_stor_len;
535 unsigned long guest_base_stor_len;
536 unsigned long guest_virt_base_stor_len;
537 unsigned long guest_virt_var_stor_len;
538 unsigned long guest_cpu_stor_len;
539 unsigned long max_sec_stor_addr;
540 unsigned int max_num_sec_conf;
541 unsigned short max_guest_cpu_id;
542 unsigned long uv_feature_indications;
543 unsigned long supp_se_hdr_ver;
544 unsigned long supp_se_hdr_pcf;
545 unsigned long conf_dump_storage_state_len;
546 unsigned long conf_dump_finalize_len;
547 unsigned long supp_att_req_hdr_ver;
548 unsigned long supp_att_pflags;
549 unsigned long supp_add_secret_req_ver;
550 unsigned long supp_add_secret_pcf;
551 unsigned long supp_secret_types;
552 unsigned short max_assoc_secrets;
553 unsigned short max_retr_secrets;
556 extern struct uv_info uv_info;
558 static inline bool uv_has_feature(u8 feature_bit)
560 if (feature_bit >= sizeof(uv_info.uv_feature_indications) * 8)
562 return test_bit_inv(feature_bit, &uv_info.uv_feature_indications);
565 extern int prot_virt_guest;
567 static inline int is_prot_virt_guest(void)
569 return prot_virt_guest;
572 static inline int share(unsigned long addr, u16 cmd)
574 struct uv_cb_share uvcb = {
576 .header.len = sizeof(uvcb),
580 if (!is_prot_virt_guest())
583 * Sharing is page wise, if we encounter addresses that are
584 * not page aligned, we assume something went wrong. If
585 * malloced structs are passed to this function, we could leak
586 * data to the hypervisor.
588 BUG_ON(addr & ~PAGE_MASK);
590 if (!uv_call(0, (u64)&uvcb))
592 pr_err("%s UVC failed (rc: 0x%x, rrc: 0x%x), possible hypervisor bug.\n",
593 uvcb.header.cmd == UVC_CMD_SET_SHARED_ACCESS ? "Share" : "Unshare",
594 uvcb.header.rc, uvcb.header.rrc);
595 panic("System security cannot be guaranteed unless the system panics now.\n");
599 * Guest 2 request to the Ultravisor to make a page shared with the
602 * @addr: Real or absolute address of the page to be shared
604 static inline int uv_set_shared(unsigned long addr)
606 return share(addr, UVC_CMD_SET_SHARED_ACCESS);
610 * Guest 2 request to the Ultravisor to make a page unshared.
612 * @addr: Real or absolute address of the page to be unshared
614 static inline int uv_remove_shared(unsigned long addr)
616 return share(addr, UVC_CMD_REMOVE_SHARED_ACCESS);
619 int uv_get_secret_metadata(const u8 secret_id[UV_SECRET_ID_LEN],
620 struct uv_secret_list_item_hdr *secret);
621 int uv_retrieve_secret(u16 secret_idx, u8 *buf, size_t buf_size);
623 extern int prot_virt_host;
625 static inline int is_prot_virt_host(void)
627 return prot_virt_host;
630 int uv_pin_shared(unsigned long paddr);
631 int uv_destroy_folio(struct folio *folio);
632 int uv_destroy_pte(pte_t pte);
633 int uv_convert_from_secure_pte(pte_t pte);
634 int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb);
635 int uv_convert_from_secure(unsigned long paddr);
636 int uv_convert_from_secure_folio(struct folio *folio);
640 #endif /* _ASM_S390_UV_H */