xref: /linux/arch/s390/kernel/uv.c (revision 22f74df7ac575c43c4dd1e2d4cbae65ff84a3595)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common Ultravisor functions and initialization
4  *
5  * Copyright IBM Corp. 2019, 2024
6  */
7 #define pr_fmt(fmt) "prot_virt: " fmt
8 
9 #include <linux/export.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/sizes.h>
13 #include <linux/bitmap.h>
14 #include <linux/memblock.h>
15 #include <linux/pagemap.h>
16 #include <linux/swap.h>
17 #include <linux/pagewalk.h>
18 #include <linux/backing-dev.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <asm/facility.h>
22 #include <asm/sections.h>
23 #include <asm/uv.h>
24 
25 /* the bootdata_preserved fields come from ones in arch/s390/boot/uv.c */
26 int __bootdata_preserved(prot_virt_guest);
27 EXPORT_SYMBOL(prot_virt_guest);
28 
29 /*
30  * uv_info contains both host and guest information but it's currently only
31  * expected to be used within modules if it's the KVM module or for
32  * any PV guest module.
33  *
34  * The kernel itself will write these values once in uv_query_info()
35  * and then make some of them readable via a sysfs interface.
36  */
37 struct uv_info __bootdata_preserved(uv_info);
38 EXPORT_SYMBOL(uv_info);
39 
40 int __bootdata_preserved(prot_virt_host);
41 EXPORT_SYMBOL(prot_virt_host);
42 
43 static int __init uv_init(phys_addr_t stor_base, unsigned long stor_len)
44 {
45 	struct uv_cb_init uvcb = {
46 		.header.cmd = UVC_CMD_INIT_UV,
47 		.header.len = sizeof(uvcb),
48 		.stor_origin = stor_base,
49 		.stor_len = stor_len,
50 	};
51 
52 	if (uv_call(0, (uint64_t)&uvcb)) {
53 		pr_err("Ultravisor init failed with rc: 0x%x rrc: 0%x\n",
54 		       uvcb.header.rc, uvcb.header.rrc);
55 		return -1;
56 	}
57 	return 0;
58 }
59 
60 void __init setup_uv(void)
61 {
62 	void *uv_stor_base;
63 
64 	if (!is_prot_virt_host())
65 		return;
66 
67 	uv_stor_base = memblock_alloc_try_nid(
68 		uv_info.uv_base_stor_len, SZ_1M, SZ_2G,
69 		MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
70 	if (!uv_stor_base) {
71 		pr_warn("Failed to reserve %lu bytes for ultravisor base storage\n",
72 			uv_info.uv_base_stor_len);
73 		goto fail;
74 	}
75 
76 	if (uv_init(__pa(uv_stor_base), uv_info.uv_base_stor_len)) {
77 		memblock_free(uv_stor_base, uv_info.uv_base_stor_len);
78 		goto fail;
79 	}
80 
81 	pr_info("Reserving %luMB as ultravisor base storage\n",
82 		uv_info.uv_base_stor_len >> 20);
83 	return;
84 fail:
85 	pr_info("Disabling support for protected virtualization");
86 	prot_virt_host = 0;
87 }
88 
89 /*
90  * Requests the Ultravisor to pin the page in the shared state. This will
91  * cause an intercept when the guest attempts to unshare the pinned page.
92  */
93 int uv_pin_shared(unsigned long paddr)
94 {
95 	struct uv_cb_cfs uvcb = {
96 		.header.cmd = UVC_CMD_PIN_PAGE_SHARED,
97 		.header.len = sizeof(uvcb),
98 		.paddr = paddr,
99 	};
100 
101 	if (uv_call(0, (u64)&uvcb))
102 		return -EINVAL;
103 	return 0;
104 }
105 EXPORT_SYMBOL_GPL(uv_pin_shared);
106 
107 /*
108  * Requests the Ultravisor to destroy a guest page and make it
109  * accessible to the host. The destroy clears the page instead of
110  * exporting.
111  *
112  * @paddr: Absolute host address of page to be destroyed
113  */
114 static int uv_destroy(unsigned long paddr)
115 {
116 	struct uv_cb_cfs uvcb = {
117 		.header.cmd = UVC_CMD_DESTR_SEC_STOR,
118 		.header.len = sizeof(uvcb),
119 		.paddr = paddr
120 	};
121 
122 	if (uv_call(0, (u64)&uvcb)) {
123 		/*
124 		 * Older firmware uses 107/d as an indication of a non secure
125 		 * page. Let us emulate the newer variant (no-op).
126 		 */
127 		if (uvcb.header.rc == 0x107 && uvcb.header.rrc == 0xd)
128 			return 0;
129 		return -EINVAL;
130 	}
131 	return 0;
132 }
133 
134 /*
135  * The caller must already hold a reference to the folio
136  */
137 int uv_destroy_folio(struct folio *folio)
138 {
139 	unsigned long i;
140 	int rc;
141 
142 	folio_get(folio);
143 	for (i = 0; i < (1 << folio_order(folio)); i++) {
144 		rc = uv_destroy(folio_to_phys(folio) + i * PAGE_SIZE);
145 		if (rc)
146 			break;
147 	}
148 	if (!rc)
149 		clear_bit(PG_arch_1, &folio->flags.f);
150 	folio_put(folio);
151 	return rc;
152 }
153 EXPORT_SYMBOL(uv_destroy_folio);
154 
155 /*
156  * The present PTE still indirectly holds a folio reference through the mapping.
157  */
158 int uv_destroy_pte(pte_t pte)
159 {
160 	VM_WARN_ON(!pte_present(pte));
161 	return uv_destroy_folio(pfn_folio(pte_pfn(pte)));
162 }
163 
164 /*
165  * Requests the Ultravisor to encrypt a guest page and make it
166  * accessible to the host for paging (export).
167  *
168  * @paddr: Absolute host address of page to be exported
169  */
170 int uv_convert_from_secure(unsigned long paddr)
171 {
172 	struct uv_cb_cfs uvcb = {
173 		.header.cmd = UVC_CMD_CONV_FROM_SEC_STOR,
174 		.header.len = sizeof(uvcb),
175 		.paddr = paddr
176 	};
177 
178 	if (uv_call(0, (u64)&uvcb))
179 		return -EINVAL;
180 	return 0;
181 }
182 EXPORT_SYMBOL_GPL(uv_convert_from_secure);
183 
184 /*
185  * The caller must already hold a reference to the folio.
186  */
187 int uv_convert_from_secure_folio(struct folio *folio)
188 {
189 	unsigned long i;
190 	int rc;
191 
192 	folio_get(folio);
193 	for (i = 0; i < (1 << folio_order(folio)); i++) {
194 		rc = uv_convert_from_secure(folio_to_phys(folio) + i * PAGE_SIZE);
195 		if (rc)
196 			break;
197 	}
198 	if (!rc)
199 		clear_bit(PG_arch_1, &folio->flags.f);
200 	folio_put(folio);
201 	return rc;
202 }
203 EXPORT_SYMBOL_GPL(uv_convert_from_secure_folio);
204 
205 /*
206  * The present PTE still indirectly holds a folio reference through the mapping.
207  */
208 int uv_convert_from_secure_pte(pte_t pte)
209 {
210 	VM_WARN_ON(!pte_present(pte));
211 	return uv_convert_from_secure_folio(pfn_folio(pte_pfn(pte)));
212 }
213 
214 static int uv_free_range_cb(pte_t *ptep, unsigned long addr, void *data)
215 {
216 	pte_t pte = ptep_get(ptep);
217 
218 	if (!pte_present(pte))
219 		return 0;
220 	/*
221 	 * Note: do not update the pte here, since there is no code which
222 	 * accesses the memory range, besides bugs. The invalidation of ptes
223 	 * and TLB flushing is deferred like for regular vfree() calls.
224 	 */
225 	__free_page(pte_page(pte));
226 	return 0;
227 }
228 
229 void uv_free_stor_var(void *stor_var)
230 {
231 	unsigned long addr, size;
232 	struct vm_struct *area;
233 
234 	if (!stor_var)
235 		return;
236 	area = find_vm_area(stor_var);
237 	if (WARN_ON_ONCE(!area || !(area->flags & VM_SPARSE)))
238 		return;
239 	size = get_vm_area_size(area);
240 	addr = (unsigned long)area->addr;
241 	apply_to_existing_page_range(&init_mm, addr, size, uv_free_range_cb, NULL);
242 	free_vm_area(area);
243 }
244 EXPORT_SYMBOL_FOR_MODULES(uv_free_stor_var, "kvm");
245 
246 void *uv_alloc_stor_var(unsigned long size)
247 {
248 	unsigned long i, nr_pages, addr;
249 	struct vm_struct *area;
250 	struct page **pages;
251 
252 	size = PAGE_ALIGN(size);
253 	nr_pages = size >> PAGE_SHIFT;
254 	area = get_vm_area(size, VM_SPARSE);
255 	if (!area)
256 		return NULL;
257 	pages = kvcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL_ACCOUNT);
258 	if (!pages)
259 		goto free_area;
260 	for (i = 0; i < nr_pages; i++) {
261 		pages[i] = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
262 		if (!pages[i])
263 			goto free_pages;
264 	}
265 	addr = (unsigned long)area->addr;
266 	if (vm_area_map_pages(area, addr, addr + size, pages))
267 		goto free_pages;
268 	kvfree(pages);
269 	return area->addr;
270 free_pages:
271 	for (i = 0; i < nr_pages; i++) {
272 		if (pages[i])
273 			__free_page(pages[i]);
274 	}
275 	kvfree(pages);
276 free_area:
277 	free_vm_area(area);
278 	return NULL;
279 }
280 EXPORT_SYMBOL_FOR_MODULES(uv_alloc_stor_var, "kvm");
281 
282 /*
283  * Calculate the expected ref_count for a folio that would otherwise have no
284  * further pins. This was cribbed from similar functions in other places in
285  * the kernel, but with some slight modifications. We know that a secure
286  * folio can not be a large folio, for example.
287  */
288 static int expected_folio_refs(struct folio *folio)
289 {
290 	int res;
291 
292 	res = folio_mapcount(folio);
293 	if (folio_test_swapcache(folio)) {
294 		res++;
295 	} else if (folio_mapping(folio)) {
296 		res++;
297 		if (folio->private)
298 			res++;
299 	}
300 	return res;
301 }
302 
303 /**
304  * __make_folio_secure() - make a folio secure
305  * @folio: the folio to make secure
306  * @uvcb: the uvcb that describes the UVC to be used
307  *
308  * The folio @folio will be made secure if possible, @uvcb will be passed
309  * as-is to the UVC.
310  *
311  * Return: 0 on success;
312  *         -EBUSY if the folio is in writeback or has too many references;
313  *         -EAGAIN if the UVC needs to be attempted again;
314  *         -ENXIO if the address is not mapped;
315  *         -EINVAL if the UVC failed for other reasons.
316  *
317  * Context: The caller must hold exactly one extra reference on the folio
318  *          (it's the same logic as split_folio()), and the folio must be
319  *          locked.
320  */
321 int __make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)
322 {
323 	int expected, cc = 0;
324 
325 	if (folio_test_writeback(folio))
326 		return -EBUSY;
327 	expected = expected_folio_refs(folio) + 1;
328 	if (!folio_ref_freeze(folio, expected))
329 		return -EBUSY;
330 	set_bit(PG_arch_1, &folio->flags.f);
331 	/*
332 	 * If the UVC does not succeed or fail immediately, we don't want to
333 	 * loop for long, or we might get stall notifications.
334 	 * On the other hand, this is a complex scenario and we are holding a lot of
335 	 * locks, so we can't easily sleep and reschedule. We try only once,
336 	 * and if the UVC returned busy or partial completion, we return
337 	 * -EAGAIN and we let the callers deal with it.
338 	 */
339 	cc = __uv_call(0, (u64)uvcb);
340 	folio_ref_unfreeze(folio, expected);
341 	/*
342 	 * Return -ENXIO if the folio was not mapped, -EINVAL for other errors.
343 	 * If busy or partially completed, return -EAGAIN.
344 	 */
345 	if (cc == UVC_CC_OK)
346 		return 0;
347 	else if (cc == UVC_CC_BUSY || cc == UVC_CC_PARTIAL)
348 		return -EAGAIN;
349 	return uvcb->rc == 0x10a ? -ENXIO : -EINVAL;
350 }
351 EXPORT_SYMBOL(__make_folio_secure);
352 
353 /**
354  * s390_wiggle_split_folio() - try to drain extra references to a folio and
355  *			       split the folio if it is large.
356  * @mm:    the mm containing the folio to work on
357  * @folio: the folio
358  *
359  * Context: Must be called while holding an extra reference to the folio;
360  *          the mm lock should not be held.
361  * Return: 0 if the operation was successful;
362  *	   -EAGAIN if splitting the large folio was not successful,
363  *		   but another attempt can be made;
364  *	   -EINVAL in case of other folio splitting errors. See split_folio().
365  */
366 int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio)
367 {
368 	int rc, tried_splits;
369 
370 	lockdep_assert_not_held(&mm->mmap_lock);
371 	folio_wait_writeback(folio);
372 	lru_add_drain_all();
373 
374 	if (!folio_test_large(folio))
375 		return 0;
376 
377 	for (tried_splits = 0; tried_splits < 2; tried_splits++) {
378 		struct address_space *mapping;
379 		loff_t lstart, lend;
380 		struct inode *inode;
381 
382 		folio_lock(folio);
383 		rc = split_folio(folio);
384 		if (rc != -EBUSY) {
385 			folio_unlock(folio);
386 			return rc;
387 		}
388 
389 		/*
390 		 * Splitting with -EBUSY can fail for various reasons, but we
391 		 * have to handle one case explicitly for now: some mappings
392 		 * don't allow for splitting dirty folios; writeback will
393 		 * mark them clean again, including marking all page table
394 		 * entries mapping the folio read-only, to catch future write
395 		 * attempts.
396 		 *
397 		 * While the system should be writing back dirty folios in the
398 		 * background, we obtained this folio by looking up a writable
399 		 * page table entry. On these problematic mappings, writable
400 		 * page table entries imply dirty folios, preventing the
401 		 * split in the first place.
402 		 *
403 		 * To prevent a livelock when trigger writeback manually and
404 		 * letting the caller look up the folio again in the page
405 		 * table (turning it dirty), immediately try to split again.
406 		 *
407 		 * This is only a problem for some mappings (e.g., XFS);
408 		 * mappings that do not support writeback (e.g., shmem) do not
409 		 * apply.
410 		 */
411 		if (!folio_test_dirty(folio) || folio_test_anon(folio) ||
412 		    !folio->mapping || !mapping_can_writeback(folio->mapping)) {
413 			folio_unlock(folio);
414 			break;
415 		}
416 
417 		/*
418 		 * Ideally, we'd only trigger writeback on this exact folio. But
419 		 * there is no easy way to do that, so we'll stabilize the
420 		 * mapping while we still hold the folio lock, so we can drop
421 		 * the folio lock to trigger writeback on the range currently
422 		 * covered by the folio instead.
423 		 */
424 		mapping = folio->mapping;
425 		lstart = folio_pos(folio);
426 		lend = lstart + folio_size(folio) - 1;
427 		inode = igrab(mapping->host);
428 		folio_unlock(folio);
429 
430 		if (unlikely(!inode))
431 			break;
432 
433 		filemap_write_and_wait_range(mapping, lstart, lend);
434 		iput(mapping->host);
435 	}
436 	return -EAGAIN;
437 }
438 EXPORT_SYMBOL_GPL(s390_wiggle_split_folio);
439 
440 /*
441  * To be called with the folio locked or with an extra reference! This will
442  * prevent kvm_s390_pv_make_secure() from touching the folio concurrently.
443  * Having 2 parallel arch_make_folio_accessible is fine, as the UV calls will
444  * become a no-op if the folio is already exported.
445  */
446 int arch_make_folio_accessible(struct folio *folio)
447 {
448 	int rc = 0;
449 
450 	/*
451 	 * PG_arch_1 is used as an indication that this small folio might be
452 	 * secure. This can overindicate, e.g. we set the bit before calling
453 	 * convert_to_secure.
454 	 */
455 	if (!test_bit(PG_arch_1, &folio->flags.f))
456 		return 0;
457 
458 	/* Large folios cannot be secure. */
459 	if (WARN_ON_ONCE(folio_test_large(folio)))
460 		return -EFAULT;
461 
462 	rc = uv_pin_shared(folio_to_phys(folio));
463 	if (!rc) {
464 		clear_bit(PG_arch_1, &folio->flags.f);
465 		return 0;
466 	}
467 
468 	rc = uv_convert_from_secure(folio_to_phys(folio));
469 	if (!rc) {
470 		clear_bit(PG_arch_1, &folio->flags.f);
471 		return 0;
472 	}
473 
474 	return rc;
475 }
476 EXPORT_SYMBOL_GPL(arch_make_folio_accessible);
477 
478 static ssize_t uv_query_facilities(struct kobject *kobj,
479 				   struct kobj_attribute *attr, char *buf)
480 {
481 	return sysfs_emit(buf, "%lx\n%lx\n%lx\n%lx\n",
482 			  uv_info.inst_calls_list[0],
483 			  uv_info.inst_calls_list[1],
484 			  uv_info.inst_calls_list[2],
485 			  uv_info.inst_calls_list[3]);
486 }
487 
488 static struct kobj_attribute uv_query_facilities_attr =
489 	__ATTR(facilities, 0444, uv_query_facilities, NULL);
490 
491 static ssize_t uv_query_supp_se_hdr_ver(struct kobject *kobj,
492 					struct kobj_attribute *attr, char *buf)
493 {
494 	return sysfs_emit(buf, "%lx\n", uv_info.supp_se_hdr_ver);
495 }
496 
497 static struct kobj_attribute uv_query_supp_se_hdr_ver_attr =
498 	__ATTR(supp_se_hdr_ver, 0444, uv_query_supp_se_hdr_ver, NULL);
499 
500 static ssize_t uv_query_supp_se_hdr_pcf(struct kobject *kobj,
501 					struct kobj_attribute *attr, char *buf)
502 {
503 	return sysfs_emit(buf, "%lx\n", uv_info.supp_se_hdr_pcf);
504 }
505 
506 static struct kobj_attribute uv_query_supp_se_hdr_pcf_attr =
507 	__ATTR(supp_se_hdr_pcf, 0444, uv_query_supp_se_hdr_pcf, NULL);
508 
509 static ssize_t uv_query_dump_cpu_len(struct kobject *kobj,
510 				     struct kobj_attribute *attr, char *buf)
511 {
512 	return sysfs_emit(buf, "%lx\n", uv_info.guest_cpu_stor_len);
513 }
514 
515 static struct kobj_attribute uv_query_dump_cpu_len_attr =
516 	__ATTR(uv_query_dump_cpu_len, 0444, uv_query_dump_cpu_len, NULL);
517 
518 static ssize_t uv_query_dump_storage_state_len(struct kobject *kobj,
519 					       struct kobj_attribute *attr, char *buf)
520 {
521 	return sysfs_emit(buf, "%lx\n", uv_info.conf_dump_storage_state_len);
522 }
523 
524 static struct kobj_attribute uv_query_dump_storage_state_len_attr =
525 	__ATTR(dump_storage_state_len, 0444, uv_query_dump_storage_state_len, NULL);
526 
527 static ssize_t uv_query_dump_finalize_len(struct kobject *kobj,
528 					  struct kobj_attribute *attr, char *buf)
529 {
530 	return sysfs_emit(buf, "%lx\n", uv_info.conf_dump_finalize_len);
531 }
532 
533 static struct kobj_attribute uv_query_dump_finalize_len_attr =
534 	__ATTR(dump_finalize_len, 0444, uv_query_dump_finalize_len, NULL);
535 
536 static ssize_t uv_query_feature_indications(struct kobject *kobj,
537 					    struct kobj_attribute *attr, char *buf)
538 {
539 	return sysfs_emit(buf, "%lx\n", uv_info.uv_feature_indications);
540 }
541 
542 static struct kobj_attribute uv_query_feature_indications_attr =
543 	__ATTR(feature_indications, 0444, uv_query_feature_indications, NULL);
544 
545 static ssize_t uv_query_max_guest_cpus(struct kobject *kobj,
546 				       struct kobj_attribute *attr, char *buf)
547 {
548 	return sysfs_emit(buf, "%d\n", uv_info.max_guest_cpu_id + 1);
549 }
550 
551 static struct kobj_attribute uv_query_max_guest_cpus_attr =
552 	__ATTR(max_cpus, 0444, uv_query_max_guest_cpus, NULL);
553 
554 static ssize_t uv_query_max_guest_vms(struct kobject *kobj,
555 				      struct kobj_attribute *attr, char *buf)
556 {
557 	return sysfs_emit(buf, "%d\n", uv_info.max_num_sec_conf);
558 }
559 
560 static struct kobj_attribute uv_query_max_guest_vms_attr =
561 	__ATTR(max_guests, 0444, uv_query_max_guest_vms, NULL);
562 
563 static ssize_t uv_query_max_guest_addr(struct kobject *kobj,
564 				       struct kobj_attribute *attr, char *buf)
565 {
566 	return sysfs_emit(buf, "%lx\n", uv_info.max_sec_stor_addr);
567 }
568 
569 static struct kobj_attribute uv_query_max_guest_addr_attr =
570 	__ATTR(max_address, 0444, uv_query_max_guest_addr, NULL);
571 
572 static ssize_t uv_query_supp_att_req_hdr_ver(struct kobject *kobj,
573 					     struct kobj_attribute *attr, char *buf)
574 {
575 	return sysfs_emit(buf, "%lx\n", uv_info.supp_att_req_hdr_ver);
576 }
577 
578 static struct kobj_attribute uv_query_supp_att_req_hdr_ver_attr =
579 	__ATTR(supp_att_req_hdr_ver, 0444, uv_query_supp_att_req_hdr_ver, NULL);
580 
581 static ssize_t uv_query_supp_att_pflags(struct kobject *kobj,
582 					struct kobj_attribute *attr, char *buf)
583 {
584 	return sysfs_emit(buf, "%lx\n", uv_info.supp_att_pflags);
585 }
586 
587 static struct kobj_attribute uv_query_supp_att_pflags_attr =
588 	__ATTR(supp_att_pflags, 0444, uv_query_supp_att_pflags, NULL);
589 
590 static ssize_t uv_query_supp_add_secret_req_ver(struct kobject *kobj,
591 						struct kobj_attribute *attr, char *buf)
592 {
593 	return sysfs_emit(buf, "%lx\n", uv_info.supp_add_secret_req_ver);
594 }
595 
596 static struct kobj_attribute uv_query_supp_add_secret_req_ver_attr =
597 	__ATTR(supp_add_secret_req_ver, 0444, uv_query_supp_add_secret_req_ver, NULL);
598 
599 static ssize_t uv_query_supp_add_secret_pcf(struct kobject *kobj,
600 					    struct kobj_attribute *attr, char *buf)
601 {
602 	return sysfs_emit(buf, "%lx\n", uv_info.supp_add_secret_pcf);
603 }
604 
605 static struct kobj_attribute uv_query_supp_add_secret_pcf_attr =
606 	__ATTR(supp_add_secret_pcf, 0444, uv_query_supp_add_secret_pcf, NULL);
607 
608 static ssize_t uv_query_supp_secret_types(struct kobject *kobj,
609 					  struct kobj_attribute *attr, char *buf)
610 {
611 	return sysfs_emit(buf, "%lx\n", uv_info.supp_secret_types);
612 }
613 
614 static struct kobj_attribute uv_query_supp_secret_types_attr =
615 	__ATTR(supp_secret_types, 0444, uv_query_supp_secret_types, NULL);
616 
617 static ssize_t uv_query_max_secrets(struct kobject *kobj,
618 				    struct kobj_attribute *attr, char *buf)
619 {
620 	return sysfs_emit(buf, "%d\n",
621 			  uv_info.max_assoc_secrets + uv_info.max_retr_secrets);
622 }
623 
624 static struct kobj_attribute uv_query_max_secrets_attr =
625 	__ATTR(max_secrets, 0444, uv_query_max_secrets, NULL);
626 
627 static ssize_t uv_query_max_retr_secrets(struct kobject *kobj,
628 					 struct kobj_attribute *attr, char *buf)
629 {
630 	return sysfs_emit(buf, "%d\n", uv_info.max_retr_secrets);
631 }
632 
633 static struct kobj_attribute uv_query_max_retr_secrets_attr =
634 	__ATTR(max_retr_secrets, 0444, uv_query_max_retr_secrets, NULL);
635 
636 static ssize_t uv_query_max_assoc_secrets(struct kobject *kobj,
637 					  struct kobj_attribute *attr,
638 					  char *buf)
639 {
640 	return sysfs_emit(buf, "%d\n", uv_info.max_assoc_secrets);
641 }
642 
643 static struct kobj_attribute uv_query_max_assoc_secrets_attr =
644 	__ATTR(max_assoc_secrets, 0444, uv_query_max_assoc_secrets, NULL);
645 
646 static struct attribute *uv_query_attrs[] = {
647 	&uv_query_facilities_attr.attr,
648 	&uv_query_feature_indications_attr.attr,
649 	&uv_query_max_guest_cpus_attr.attr,
650 	&uv_query_max_guest_vms_attr.attr,
651 	&uv_query_max_guest_addr_attr.attr,
652 	&uv_query_supp_se_hdr_ver_attr.attr,
653 	&uv_query_supp_se_hdr_pcf_attr.attr,
654 	&uv_query_dump_storage_state_len_attr.attr,
655 	&uv_query_dump_finalize_len_attr.attr,
656 	&uv_query_dump_cpu_len_attr.attr,
657 	&uv_query_supp_att_req_hdr_ver_attr.attr,
658 	&uv_query_supp_att_pflags_attr.attr,
659 	&uv_query_supp_add_secret_req_ver_attr.attr,
660 	&uv_query_supp_add_secret_pcf_attr.attr,
661 	&uv_query_supp_secret_types_attr.attr,
662 	&uv_query_max_secrets_attr.attr,
663 	&uv_query_max_assoc_secrets_attr.attr,
664 	&uv_query_max_retr_secrets_attr.attr,
665 	NULL,
666 };
667 
668 static inline struct uv_cb_query_keys uv_query_keys(void)
669 {
670 	struct uv_cb_query_keys uvcb = {
671 		.header.cmd = UVC_CMD_QUERY_KEYS,
672 		.header.len = sizeof(uvcb)
673 	};
674 
675 	uv_call(0, (uint64_t)&uvcb);
676 	return uvcb;
677 }
678 
679 static inline ssize_t emit_hash(struct uv_key_hash *hash, char *buf, int at)
680 {
681 	return sysfs_emit_at(buf, at, "%016llx%016llx%016llx%016llx\n",
682 			    hash->dword[0], hash->dword[1], hash->dword[2], hash->dword[3]);
683 }
684 
685 static ssize_t uv_keys_host_key(struct kobject *kobj,
686 				struct kobj_attribute *attr, char *buf)
687 {
688 	struct uv_cb_query_keys uvcb = uv_query_keys();
689 
690 	return emit_hash(&uvcb.key_hashes[UVC_QUERY_KEYS_IDX_HK], buf, 0);
691 }
692 
693 static struct kobj_attribute uv_keys_host_key_attr =
694 	__ATTR(host_key, 0444, uv_keys_host_key, NULL);
695 
696 static ssize_t uv_keys_backup_host_key(struct kobject *kobj,
697 				       struct kobj_attribute *attr, char *buf)
698 {
699 	struct uv_cb_query_keys uvcb = uv_query_keys();
700 
701 	return emit_hash(&uvcb.key_hashes[UVC_QUERY_KEYS_IDX_BACK_HK], buf, 0);
702 }
703 
704 static struct kobj_attribute uv_keys_backup_host_key_attr =
705 	__ATTR(backup_host_key, 0444, uv_keys_backup_host_key, NULL);
706 
707 static ssize_t uv_keys_all(struct kobject *kobj,
708 			   struct kobj_attribute *attr, char *buf)
709 {
710 	struct uv_cb_query_keys uvcb = uv_query_keys();
711 	ssize_t len = 0;
712 	int i;
713 
714 	for (i = 0; i < ARRAY_SIZE(uvcb.key_hashes); i++)
715 		len += emit_hash(uvcb.key_hashes + i, buf, len);
716 
717 	return len;
718 }
719 
720 static struct kobj_attribute uv_keys_all_attr =
721 	__ATTR(all, 0444, uv_keys_all, NULL);
722 
723 static struct attribute_group uv_query_attr_group = {
724 	.attrs = uv_query_attrs,
725 };
726 
727 static struct attribute *uv_keys_attrs[] = {
728 	&uv_keys_host_key_attr.attr,
729 	&uv_keys_backup_host_key_attr.attr,
730 	&uv_keys_all_attr.attr,
731 	NULL,
732 };
733 
734 static struct attribute_group uv_keys_attr_group = {
735 	.attrs = uv_keys_attrs,
736 };
737 
738 static ssize_t uv_is_prot_virt_guest(struct kobject *kobj,
739 				     struct kobj_attribute *attr, char *buf)
740 {
741 	return sysfs_emit(buf, "%d\n", prot_virt_guest);
742 }
743 
744 static ssize_t uv_is_prot_virt_host(struct kobject *kobj,
745 				    struct kobj_attribute *attr, char *buf)
746 {
747 	return sysfs_emit(buf, "%d\n", prot_virt_host);
748 }
749 
750 static struct kobj_attribute uv_prot_virt_guest =
751 	__ATTR(prot_virt_guest, 0444, uv_is_prot_virt_guest, NULL);
752 
753 static struct kobj_attribute uv_prot_virt_host =
754 	__ATTR(prot_virt_host, 0444, uv_is_prot_virt_host, NULL);
755 
756 static const struct attribute *uv_prot_virt_attrs[] = {
757 	&uv_prot_virt_guest.attr,
758 	&uv_prot_virt_host.attr,
759 	NULL,
760 };
761 
762 static struct kset *uv_query_kset;
763 static struct kset *uv_keys_kset;
764 static struct kobject *uv_kobj;
765 
766 static int __init uv_sysfs_dir_init(const struct attribute_group *grp,
767 				    struct kset **uv_dir_kset, const char *name)
768 {
769 	struct kset *kset;
770 	int rc;
771 
772 	kset = kset_create_and_add(name, NULL, uv_kobj);
773 	if (!kset)
774 		return -ENOMEM;
775 	*uv_dir_kset = kset;
776 
777 	rc = sysfs_create_group(&kset->kobj, grp);
778 	if (rc)
779 		kset_unregister(kset);
780 	return rc;
781 }
782 
783 static int __init uv_sysfs_init(void)
784 {
785 	int rc = -ENOMEM;
786 
787 	if (!test_facility(158))
788 		return 0;
789 
790 	uv_kobj = kobject_create_and_add("uv", firmware_kobj);
791 	if (!uv_kobj)
792 		return -ENOMEM;
793 
794 	rc = sysfs_create_files(uv_kobj, uv_prot_virt_attrs);
795 	if (rc)
796 		goto out_kobj;
797 
798 	rc = uv_sysfs_dir_init(&uv_query_attr_group, &uv_query_kset, "query");
799 	if (rc)
800 		goto out_ind_files;
801 
802 	/* Get installed key hashes if available, ignore any errors */
803 	if (test_bit_inv(BIT_UVC_CMD_QUERY_KEYS, uv_info.inst_calls_list))
804 		uv_sysfs_dir_init(&uv_keys_attr_group, &uv_keys_kset, "keys");
805 
806 	return 0;
807 
808 out_ind_files:
809 	sysfs_remove_files(uv_kobj, uv_prot_virt_attrs);
810 out_kobj:
811 	kobject_del(uv_kobj);
812 	kobject_put(uv_kobj);
813 	return rc;
814 }
815 device_initcall(uv_sysfs_init);
816 
817 /*
818  * Locate a secret in the list by its id.
819  * @secret_id: search pattern.
820  * @list: ephemeral buffer space
821  * @secret: output data, containing the secret's metadata.
822  *
823  * Search for a secret with the given secret_id in the Ultravisor secret store.
824  *
825  * Context: might sleep.
826  */
827 static int find_secret_in_page(const u8 secret_id[UV_SECRET_ID_LEN],
828 			       const struct uv_secret_list *list,
829 			       struct uv_secret_list_item_hdr *secret)
830 {
831 	u16 i;
832 
833 	for (i = 0; i < list->num_secr_stored; i++) {
834 		if (memcmp(secret_id, list->secrets[i].id, UV_SECRET_ID_LEN) == 0) {
835 			*secret = list->secrets[i].hdr;
836 			return 0;
837 		}
838 	}
839 	return -ENOENT;
840 }
841 
842 /**
843  * uv_find_secret() - search secret metadata for a given secret id.
844  * @secret_id: search pattern.
845  * @list: ephemeral buffer space
846  * @secret: output data, containing the secret's metadata.
847  *
848  * Context: might sleep.
849  */
850 int uv_find_secret(const u8 secret_id[UV_SECRET_ID_LEN],
851 		   struct uv_secret_list *list,
852 		   struct uv_secret_list_item_hdr *secret)
853 {
854 	u16 start_idx;
855 	u16 list_rc;
856 	int ret;
857 
858 	list->next_secret_idx = 0;
859 
860 	do {
861 		start_idx = list->next_secret_idx;
862 		uv_list_secrets(list, start_idx, &list_rc, NULL);
863 		if (list_rc != UVC_RC_EXECUTED && list_rc != UVC_RC_MORE_DATA) {
864 			if (list_rc == UVC_RC_INV_CMD)
865 				return -ENODEV;
866 			else
867 				return -EIO;
868 		}
869 		ret = find_secret_in_page(secret_id, list, secret);
870 		if (ret == 0)
871 			return ret;
872 	} while (list_rc == UVC_RC_MORE_DATA && start_idx < list->next_secret_idx);
873 
874 	return -ENOENT;
875 }
876 EXPORT_SYMBOL_GPL(uv_find_secret);
877 
878 /**
879  * uv_retrieve_secret() - get the secret value for the secret index.
880  * @secret_idx: Secret index for which the secret should be retrieved.
881  * @buf: Buffer to store retrieved secret.
882  * @buf_size: Size of the buffer. The correct buffer size is reported as part of
883  * the result from `uv_get_secret_metadata`.
884  *
885  * Calls the Retrieve Secret UVC and translates the UV return code into an errno.
886  *
887  * Context: might sleep.
888  *
889  * Return:
890  * * %0		- Entry found; buffer contains a valid secret.
891  * * %ENOENT:	- No entry found or secret at the index is non-retrievable.
892  * * %ENODEV:	- Not supported: UV not available or command not available.
893  * * %EINVAL:	- Buffer too small for content.
894  * * %EIO:	- Other unexpected UV error.
895  */
896 int uv_retrieve_secret(u16 secret_idx, u8 *buf, size_t buf_size)
897 {
898 	struct uv_cb_retr_secr uvcb = {
899 		.header.len = sizeof(uvcb),
900 		.header.cmd = UVC_CMD_RETR_SECRET,
901 		.secret_idx = secret_idx,
902 		.buf_addr = (u64)buf,
903 		.buf_size = buf_size,
904 	};
905 
906 	uv_call(0, (u64)&uvcb);
907 
908 	switch (uvcb.header.rc) {
909 	case UVC_RC_EXECUTED:
910 		return 0;
911 	case UVC_RC_INV_CMD:
912 		return -ENODEV;
913 	case UVC_RC_RETR_SECR_STORE_EMPTY:
914 	case UVC_RC_RETR_SECR_INV_SECRET:
915 	case UVC_RC_RETR_SECR_INV_IDX:
916 		return -ENOENT;
917 	case UVC_RC_RETR_SECR_BUF_SMALL:
918 		return -EINVAL;
919 	default:
920 		return -EIO;
921 	}
922 }
923 EXPORT_SYMBOL_GPL(uv_retrieve_secret);
924