1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Common Ultravisor functions and initialization
4 *
5 * Copyright IBM Corp. 2019, 2020
6 */
7 #define KMSG_COMPONENT "prot_virt"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
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 <asm/facility.h>
18 #include <asm/sections.h>
19 #include <asm/uv.h>
20
21 /* the bootdata_preserved fields come from ones in arch/s390/boot/uv.c */
22 #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST
23 int __bootdata_preserved(prot_virt_guest);
24 #endif
25
26 struct uv_info __bootdata_preserved(uv_info);
27
28 #if IS_ENABLED(CONFIG_KVM)
29 int __bootdata_preserved(prot_virt_host);
30 EXPORT_SYMBOL(prot_virt_host);
31 EXPORT_SYMBOL(uv_info);
32
uv_init(phys_addr_t stor_base,unsigned long stor_len)33 static int __init uv_init(phys_addr_t stor_base, unsigned long stor_len)
34 {
35 struct uv_cb_init uvcb = {
36 .header.cmd = UVC_CMD_INIT_UV,
37 .header.len = sizeof(uvcb),
38 .stor_origin = stor_base,
39 .stor_len = stor_len,
40 };
41
42 if (uv_call(0, (uint64_t)&uvcb)) {
43 pr_err("Ultravisor init failed with rc: 0x%x rrc: 0%x\n",
44 uvcb.header.rc, uvcb.header.rrc);
45 return -1;
46 }
47 return 0;
48 }
49
setup_uv(void)50 void __init setup_uv(void)
51 {
52 void *uv_stor_base;
53
54 if (!is_prot_virt_host())
55 return;
56
57 uv_stor_base = memblock_alloc_try_nid(
58 uv_info.uv_base_stor_len, SZ_1M, SZ_2G,
59 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
60 if (!uv_stor_base) {
61 pr_warn("Failed to reserve %lu bytes for ultravisor base storage\n",
62 uv_info.uv_base_stor_len);
63 goto fail;
64 }
65
66 if (uv_init(__pa(uv_stor_base), uv_info.uv_base_stor_len)) {
67 memblock_free(uv_stor_base, uv_info.uv_base_stor_len);
68 goto fail;
69 }
70
71 pr_info("Reserving %luMB as ultravisor base storage\n",
72 uv_info.uv_base_stor_len >> 20);
73 return;
74 fail:
75 pr_info("Disabling support for protected virtualization");
76 prot_virt_host = 0;
77 }
78
79 /*
80 * Requests the Ultravisor to pin the page in the shared state. This will
81 * cause an intercept when the guest attempts to unshare the pinned page.
82 */
uv_pin_shared(unsigned long paddr)83 static int uv_pin_shared(unsigned long paddr)
84 {
85 struct uv_cb_cfs uvcb = {
86 .header.cmd = UVC_CMD_PIN_PAGE_SHARED,
87 .header.len = sizeof(uvcb),
88 .paddr = paddr,
89 };
90
91 if (uv_call(0, (u64)&uvcb))
92 return -EINVAL;
93 return 0;
94 }
95
96 /*
97 * Requests the Ultravisor to destroy a guest page and make it
98 * accessible to the host. The destroy clears the page instead of
99 * exporting.
100 *
101 * @paddr: Absolute host address of page to be destroyed
102 */
uv_destroy_page(unsigned long paddr)103 static int uv_destroy_page(unsigned long paddr)
104 {
105 struct uv_cb_cfs uvcb = {
106 .header.cmd = UVC_CMD_DESTR_SEC_STOR,
107 .header.len = sizeof(uvcb),
108 .paddr = paddr
109 };
110
111 if (uv_call(0, (u64)&uvcb)) {
112 /*
113 * Older firmware uses 107/d as an indication of a non secure
114 * page. Let us emulate the newer variant (no-op).
115 */
116 if (uvcb.header.rc == 0x107 && uvcb.header.rrc == 0xd)
117 return 0;
118 return -EINVAL;
119 }
120 return 0;
121 }
122
123 /*
124 * The caller must already hold a reference to the page
125 */
uv_destroy_owned_page(unsigned long paddr)126 int uv_destroy_owned_page(unsigned long paddr)
127 {
128 struct page *page = phys_to_page(paddr);
129 int rc;
130
131 get_page(page);
132 rc = uv_destroy_page(paddr);
133 if (!rc)
134 clear_bit(PG_arch_1, &page->flags);
135 put_page(page);
136 return rc;
137 }
138
139 /*
140 * Requests the Ultravisor to encrypt a guest page and make it
141 * accessible to the host for paging (export).
142 *
143 * @paddr: Absolute host address of page to be exported
144 */
uv_convert_from_secure(unsigned long paddr)145 int uv_convert_from_secure(unsigned long paddr)
146 {
147 struct uv_cb_cfs uvcb = {
148 .header.cmd = UVC_CMD_CONV_FROM_SEC_STOR,
149 .header.len = sizeof(uvcb),
150 .paddr = paddr
151 };
152
153 if (uv_call(0, (u64)&uvcb))
154 return -EINVAL;
155 return 0;
156 }
157
158 /*
159 * The caller must already hold a reference to the page
160 */
uv_convert_owned_from_secure(unsigned long paddr)161 int uv_convert_owned_from_secure(unsigned long paddr)
162 {
163 struct page *page = phys_to_page(paddr);
164 int rc;
165
166 get_page(page);
167 rc = uv_convert_from_secure(paddr);
168 if (!rc)
169 clear_bit(PG_arch_1, &page->flags);
170 put_page(page);
171 return rc;
172 }
173
174 /*
175 * Calculate the expected ref_count for a page that would otherwise have no
176 * further pins. This was cribbed from similar functions in other places in
177 * the kernel, but with some slight modifications. We know that a secure
178 * page can not be a huge page for example.
179 */
expected_page_refs(struct page * page)180 static int expected_page_refs(struct page *page)
181 {
182 int res;
183
184 res = page_mapcount(page);
185 if (PageSwapCache(page)) {
186 res++;
187 } else if (page_mapping(page)) {
188 res++;
189 if (page_has_private(page))
190 res++;
191 }
192 return res;
193 }
194
make_secure_pte(pte_t * ptep,unsigned long addr,struct page * exp_page,struct uv_cb_header * uvcb)195 static int make_secure_pte(pte_t *ptep, unsigned long addr,
196 struct page *exp_page, struct uv_cb_header *uvcb)
197 {
198 pte_t entry = READ_ONCE(*ptep);
199 struct page *page;
200 int expected, cc = 0;
201
202 if (!pte_present(entry))
203 return -ENXIO;
204 if (pte_val(entry) & _PAGE_INVALID)
205 return -ENXIO;
206
207 page = pte_page(entry);
208 if (page != exp_page)
209 return -ENXIO;
210 if (PageWriteback(page))
211 return -EAGAIN;
212 expected = expected_page_refs(page);
213 if (!page_ref_freeze(page, expected))
214 return -EBUSY;
215 set_bit(PG_arch_1, &page->flags);
216 /*
217 * If the UVC does not succeed or fail immediately, we don't want to
218 * loop for long, or we might get stall notifications.
219 * On the other hand, this is a complex scenario and we are holding a lot of
220 * locks, so we can't easily sleep and reschedule. We try only once,
221 * and if the UVC returned busy or partial completion, we return
222 * -EAGAIN and we let the callers deal with it.
223 */
224 cc = __uv_call(0, (u64)uvcb);
225 page_ref_unfreeze(page, expected);
226 /*
227 * Return -ENXIO if the page was not mapped, -EINVAL for other errors.
228 * If busy or partially completed, return -EAGAIN.
229 */
230 if (cc == UVC_CC_OK)
231 return 0;
232 else if (cc == UVC_CC_BUSY || cc == UVC_CC_PARTIAL)
233 return -EAGAIN;
234 return uvcb->rc == 0x10a ? -ENXIO : -EINVAL;
235 }
236
237 /*
238 * Requests the Ultravisor to make a page accessible to a guest.
239 * If it's brought in the first time, it will be cleared. If
240 * it has been exported before, it will be decrypted and integrity
241 * checked.
242 */
gmap_make_secure(struct gmap * gmap,unsigned long gaddr,void * uvcb)243 int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
244 {
245 struct vm_area_struct *vma;
246 bool local_drain = false;
247 spinlock_t *ptelock;
248 unsigned long uaddr;
249 struct page *page;
250 pte_t *ptep;
251 int rc;
252
253 again:
254 rc = -EFAULT;
255 mmap_read_lock(gmap->mm);
256
257 uaddr = __gmap_translate(gmap, gaddr);
258 if (IS_ERR_VALUE(uaddr))
259 goto out;
260 vma = vma_lookup(gmap->mm, uaddr);
261 if (!vma)
262 goto out;
263 /*
264 * Secure pages cannot be huge and userspace should not combine both.
265 * In case userspace does it anyway this will result in an -EFAULT for
266 * the unpack. The guest is thus never reaching secure mode. If
267 * userspace is playing dirty tricky with mapping huge pages later
268 * on this will result in a segmentation fault.
269 */
270 if (is_vm_hugetlb_page(vma))
271 goto out;
272
273 rc = -ENXIO;
274 page = follow_page(vma, uaddr, FOLL_WRITE);
275 if (IS_ERR_OR_NULL(page))
276 goto out;
277
278 lock_page(page);
279 ptep = get_locked_pte(gmap->mm, uaddr, &ptelock);
280 rc = make_secure_pte(ptep, uaddr, page, uvcb);
281 pte_unmap_unlock(ptep, ptelock);
282 unlock_page(page);
283 out:
284 mmap_read_unlock(gmap->mm);
285
286 if (rc == -EAGAIN) {
287 /*
288 * If we are here because the UVC returned busy or partial
289 * completion, this is just a useless check, but it is safe.
290 */
291 wait_on_page_writeback(page);
292 } else if (rc == -EBUSY) {
293 /*
294 * If we have tried a local drain and the page refcount
295 * still does not match our expected safe value, try with a
296 * system wide drain. This is needed if the pagevecs holding
297 * the page are on a different CPU.
298 */
299 if (local_drain) {
300 lru_add_drain_all();
301 /* We give up here, and let the caller try again */
302 return -EAGAIN;
303 }
304 /*
305 * We are here if the page refcount does not match the
306 * expected safe value. The main culprits are usually
307 * pagevecs. With lru_add_drain() we drain the pagevecs
308 * on the local CPU so that hopefully the refcount will
309 * reach the expected safe value.
310 */
311 lru_add_drain();
312 local_drain = true;
313 /* And now we try again immediately after draining */
314 goto again;
315 } else if (rc == -ENXIO) {
316 if (gmap_fault(gmap, gaddr, FAULT_FLAG_WRITE))
317 return -EFAULT;
318 return -EAGAIN;
319 }
320 return rc;
321 }
322 EXPORT_SYMBOL_GPL(gmap_make_secure);
323
gmap_convert_to_secure(struct gmap * gmap,unsigned long gaddr)324 int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr)
325 {
326 struct uv_cb_cts uvcb = {
327 .header.cmd = UVC_CMD_CONV_TO_SEC_STOR,
328 .header.len = sizeof(uvcb),
329 .guest_handle = gmap->guest_handle,
330 .gaddr = gaddr,
331 };
332
333 return gmap_make_secure(gmap, gaddr, &uvcb);
334 }
335 EXPORT_SYMBOL_GPL(gmap_convert_to_secure);
336
337 /*
338 * To be called with the page locked or with an extra reference! This will
339 * prevent gmap_make_secure from touching the page concurrently. Having 2
340 * parallel make_page_accessible is fine, as the UV calls will become a
341 * no-op if the page is already exported.
342 */
arch_make_page_accessible(struct page * page)343 int arch_make_page_accessible(struct page *page)
344 {
345 int rc = 0;
346
347 /* Hugepage cannot be protected, so nothing to do */
348 if (PageHuge(page))
349 return 0;
350
351 /*
352 * PG_arch_1 is used in 3 places:
353 * 1. for kernel page tables during early boot
354 * 2. for storage keys of huge pages and KVM
355 * 3. As an indication that this page might be secure. This can
356 * overindicate, e.g. we set the bit before calling
357 * convert_to_secure.
358 * As secure pages are never huge, all 3 variants can co-exists.
359 */
360 if (!test_bit(PG_arch_1, &page->flags))
361 return 0;
362
363 rc = uv_pin_shared(page_to_phys(page));
364 if (!rc) {
365 clear_bit(PG_arch_1, &page->flags);
366 return 0;
367 }
368
369 rc = uv_convert_from_secure(page_to_phys(page));
370 if (!rc) {
371 clear_bit(PG_arch_1, &page->flags);
372 return 0;
373 }
374
375 return rc;
376 }
377 EXPORT_SYMBOL_GPL(arch_make_page_accessible);
378
379 #endif
380
381 #if defined(CONFIG_PROTECTED_VIRTUALIZATION_GUEST) || IS_ENABLED(CONFIG_KVM)
uv_query_facilities(struct kobject * kobj,struct kobj_attribute * attr,char * page)382 static ssize_t uv_query_facilities(struct kobject *kobj,
383 struct kobj_attribute *attr, char *page)
384 {
385 return scnprintf(page, PAGE_SIZE, "%lx\n%lx\n%lx\n%lx\n",
386 uv_info.inst_calls_list[0],
387 uv_info.inst_calls_list[1],
388 uv_info.inst_calls_list[2],
389 uv_info.inst_calls_list[3]);
390 }
391
392 static struct kobj_attribute uv_query_facilities_attr =
393 __ATTR(facilities, 0444, uv_query_facilities, NULL);
394
uv_query_feature_indications(struct kobject * kobj,struct kobj_attribute * attr,char * buf)395 static ssize_t uv_query_feature_indications(struct kobject *kobj,
396 struct kobj_attribute *attr, char *buf)
397 {
398 return sysfs_emit(buf, "%lx\n", uv_info.uv_feature_indications);
399 }
400
401 static struct kobj_attribute uv_query_feature_indications_attr =
402 __ATTR(feature_indications, 0444, uv_query_feature_indications, NULL);
403
uv_query_max_guest_cpus(struct kobject * kobj,struct kobj_attribute * attr,char * page)404 static ssize_t uv_query_max_guest_cpus(struct kobject *kobj,
405 struct kobj_attribute *attr, char *page)
406 {
407 return scnprintf(page, PAGE_SIZE, "%d\n",
408 uv_info.max_guest_cpu_id + 1);
409 }
410
411 static struct kobj_attribute uv_query_max_guest_cpus_attr =
412 __ATTR(max_cpus, 0444, uv_query_max_guest_cpus, NULL);
413
uv_query_max_guest_vms(struct kobject * kobj,struct kobj_attribute * attr,char * page)414 static ssize_t uv_query_max_guest_vms(struct kobject *kobj,
415 struct kobj_attribute *attr, char *page)
416 {
417 return scnprintf(page, PAGE_SIZE, "%d\n",
418 uv_info.max_num_sec_conf);
419 }
420
421 static struct kobj_attribute uv_query_max_guest_vms_attr =
422 __ATTR(max_guests, 0444, uv_query_max_guest_vms, NULL);
423
uv_query_max_guest_addr(struct kobject * kobj,struct kobj_attribute * attr,char * page)424 static ssize_t uv_query_max_guest_addr(struct kobject *kobj,
425 struct kobj_attribute *attr, char *page)
426 {
427 return scnprintf(page, PAGE_SIZE, "%lx\n",
428 uv_info.max_sec_stor_addr);
429 }
430
431 static struct kobj_attribute uv_query_max_guest_addr_attr =
432 __ATTR(max_address, 0444, uv_query_max_guest_addr, NULL);
433
434 static struct attribute *uv_query_attrs[] = {
435 &uv_query_facilities_attr.attr,
436 &uv_query_feature_indications_attr.attr,
437 &uv_query_max_guest_cpus_attr.attr,
438 &uv_query_max_guest_vms_attr.attr,
439 &uv_query_max_guest_addr_attr.attr,
440 NULL,
441 };
442
443 static struct attribute_group uv_query_attr_group = {
444 .attrs = uv_query_attrs,
445 };
446
uv_is_prot_virt_guest(struct kobject * kobj,struct kobj_attribute * attr,char * page)447 static ssize_t uv_is_prot_virt_guest(struct kobject *kobj,
448 struct kobj_attribute *attr, char *page)
449 {
450 int val = 0;
451
452 #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST
453 val = prot_virt_guest;
454 #endif
455 return scnprintf(page, PAGE_SIZE, "%d\n", val);
456 }
457
uv_is_prot_virt_host(struct kobject * kobj,struct kobj_attribute * attr,char * page)458 static ssize_t uv_is_prot_virt_host(struct kobject *kobj,
459 struct kobj_attribute *attr, char *page)
460 {
461 int val = 0;
462
463 #if IS_ENABLED(CONFIG_KVM)
464 val = prot_virt_host;
465 #endif
466
467 return scnprintf(page, PAGE_SIZE, "%d\n", val);
468 }
469
470 static struct kobj_attribute uv_prot_virt_guest =
471 __ATTR(prot_virt_guest, 0444, uv_is_prot_virt_guest, NULL);
472
473 static struct kobj_attribute uv_prot_virt_host =
474 __ATTR(prot_virt_host, 0444, uv_is_prot_virt_host, NULL);
475
476 static const struct attribute *uv_prot_virt_attrs[] = {
477 &uv_prot_virt_guest.attr,
478 &uv_prot_virt_host.attr,
479 NULL,
480 };
481
482 static struct kset *uv_query_kset;
483 static struct kobject *uv_kobj;
484
uv_info_init(void)485 static int __init uv_info_init(void)
486 {
487 int rc = -ENOMEM;
488
489 if (!test_facility(158))
490 return 0;
491
492 uv_kobj = kobject_create_and_add("uv", firmware_kobj);
493 if (!uv_kobj)
494 return -ENOMEM;
495
496 rc = sysfs_create_files(uv_kobj, uv_prot_virt_attrs);
497 if (rc)
498 goto out_kobj;
499
500 uv_query_kset = kset_create_and_add("query", NULL, uv_kobj);
501 if (!uv_query_kset) {
502 rc = -ENOMEM;
503 goto out_ind_files;
504 }
505
506 rc = sysfs_create_group(&uv_query_kset->kobj, &uv_query_attr_group);
507 if (!rc)
508 return 0;
509
510 kset_unregister(uv_query_kset);
511 out_ind_files:
512 sysfs_remove_files(uv_kobj, uv_prot_virt_attrs);
513 out_kobj:
514 kobject_del(uv_kobj);
515 kobject_put(uv_kobj);
516 return rc;
517 }
518 device_initcall(uv_info_init);
519 #endif
520