Mention branches and keyring.
[releases.git] / s390 / kernel / uv.c
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
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
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  */
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  */
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  */
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  */
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  */
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  */
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
195 static int make_page_secure(struct page *page, struct uv_cb_header *uvcb)
196 {
197         int expected, cc = 0;
198
199         if (PageWriteback(page))
200                 return -EAGAIN;
201         expected = expected_page_refs(page);
202         if (!page_ref_freeze(page, expected))
203                 return -EBUSY;
204         set_bit(PG_arch_1, &page->flags);
205         /*
206          * If the UVC does not succeed or fail immediately, we don't want to
207          * loop for long, or we might get stall notifications.
208          * On the other hand, this is a complex scenario and we are holding a lot of
209          * locks, so we can't easily sleep and reschedule. We try only once,
210          * and if the UVC returned busy or partial completion, we return
211          * -EAGAIN and we let the callers deal with it.
212          */
213         cc = __uv_call(0, (u64)uvcb);
214         page_ref_unfreeze(page, expected);
215         /*
216          * Return -ENXIO if the page was not mapped, -EINVAL for other errors.
217          * If busy or partially completed, return -EAGAIN.
218          */
219         if (cc == UVC_CC_OK)
220                 return 0;
221         else if (cc == UVC_CC_BUSY || cc == UVC_CC_PARTIAL)
222                 return -EAGAIN;
223         return uvcb->rc == 0x10a ? -ENXIO : -EINVAL;
224 }
225
226 /**
227  * should_export_before_import - Determine whether an export is needed
228  * before an import-like operation
229  * @uvcb: the Ultravisor control block of the UVC to be performed
230  * @mm: the mm of the process
231  *
232  * Returns whether an export is needed before every import-like operation.
233  * This is needed for shared pages, which don't trigger a secure storage
234  * exception when accessed from a different guest.
235  *
236  * Although considered as one, the Unpin Page UVC is not an actual import,
237  * so it is not affected.
238  *
239  * No export is needed also when there is only one protected VM, because the
240  * page cannot belong to the wrong VM in that case (there is no "other VM"
241  * it can belong to).
242  *
243  * Return: true if an export is needed before every import, otherwise false.
244  */
245 static bool should_export_before_import(struct uv_cb_header *uvcb, struct mm_struct *mm)
246 {
247         if (uvcb->cmd == UVC_CMD_UNPIN_PAGE_SHARED)
248                 return false;
249         return atomic_read(&mm->context.protected_count) > 1;
250 }
251
252 /*
253  * Requests the Ultravisor to make a page accessible to a guest.
254  * If it's brought in the first time, it will be cleared. If
255  * it has been exported before, it will be decrypted and integrity
256  * checked.
257  */
258 int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
259 {
260         struct vm_area_struct *vma;
261         bool local_drain = false;
262         spinlock_t *ptelock;
263         unsigned long uaddr;
264         struct page *page;
265         pte_t *ptep;
266         int rc;
267
268 again:
269         rc = -EFAULT;
270         mmap_read_lock(gmap->mm);
271
272         uaddr = __gmap_translate(gmap, gaddr);
273         if (IS_ERR_VALUE(uaddr))
274                 goto out;
275         vma = vma_lookup(gmap->mm, uaddr);
276         if (!vma)
277                 goto out;
278         /*
279          * Secure pages cannot be huge and userspace should not combine both.
280          * In case userspace does it anyway this will result in an -EFAULT for
281          * the unpack. The guest is thus never reaching secure mode. If
282          * userspace is playing dirty tricky with mapping huge pages later
283          * on this will result in a segmentation fault.
284          */
285         if (is_vm_hugetlb_page(vma))
286                 goto out;
287
288         rc = -ENXIO;
289         ptep = get_locked_pte(gmap->mm, uaddr, &ptelock);
290         if (pte_present(*ptep) && !(pte_val(*ptep) & _PAGE_INVALID) && pte_write(*ptep)) {
291                 page = pte_page(*ptep);
292                 rc = -EAGAIN;
293                 if (trylock_page(page)) {
294                         if (should_export_before_import(uvcb, gmap->mm))
295                                 uv_convert_from_secure(page_to_phys(page));
296                         rc = make_page_secure(page, uvcb);
297                         unlock_page(page);
298                 }
299         }
300         pte_unmap_unlock(ptep, ptelock);
301 out:
302         mmap_read_unlock(gmap->mm);
303
304         if (rc == -EAGAIN) {
305                 /*
306                  * If we are here because the UVC returned busy or partial
307                  * completion, this is just a useless check, but it is safe.
308                  */
309                 wait_on_page_writeback(page);
310         } else if (rc == -EBUSY) {
311                 /*
312                  * If we have tried a local drain and the page refcount
313                  * still does not match our expected safe value, try with a
314                  * system wide drain. This is needed if the pagevecs holding
315                  * the page are on a different CPU.
316                  */
317                 if (local_drain) {
318                         lru_add_drain_all();
319                         /* We give up here, and let the caller try again */
320                         return -EAGAIN;
321                 }
322                 /*
323                  * We are here if the page refcount does not match the
324                  * expected safe value. The main culprits are usually
325                  * pagevecs. With lru_add_drain() we drain the pagevecs
326                  * on the local CPU so that hopefully the refcount will
327                  * reach the expected safe value.
328                  */
329                 lru_add_drain();
330                 local_drain = true;
331                 /* And now we try again immediately after draining */
332                 goto again;
333         } else if (rc == -ENXIO) {
334                 if (gmap_fault(gmap, gaddr, FAULT_FLAG_WRITE))
335                         return -EFAULT;
336                 return -EAGAIN;
337         }
338         return rc;
339 }
340 EXPORT_SYMBOL_GPL(gmap_make_secure);
341
342 int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr)
343 {
344         struct uv_cb_cts uvcb = {
345                 .header.cmd = UVC_CMD_CONV_TO_SEC_STOR,
346                 .header.len = sizeof(uvcb),
347                 .guest_handle = gmap->guest_handle,
348                 .gaddr = gaddr,
349         };
350
351         return gmap_make_secure(gmap, gaddr, &uvcb);
352 }
353 EXPORT_SYMBOL_GPL(gmap_convert_to_secure);
354
355 /**
356  * gmap_destroy_page - Destroy a guest page.
357  * @gmap: the gmap of the guest
358  * @gaddr: the guest address to destroy
359  *
360  * An attempt will be made to destroy the given guest page. If the attempt
361  * fails, an attempt is made to export the page. If both attempts fail, an
362  * appropriate error is returned.
363  */
364 int gmap_destroy_page(struct gmap *gmap, unsigned long gaddr)
365 {
366         struct vm_area_struct *vma;
367         unsigned long uaddr;
368         struct page *page;
369         int rc;
370
371         rc = -EFAULT;
372         mmap_read_lock(gmap->mm);
373
374         uaddr = __gmap_translate(gmap, gaddr);
375         if (IS_ERR_VALUE(uaddr))
376                 goto out;
377         vma = vma_lookup(gmap->mm, uaddr);
378         if (!vma)
379                 goto out;
380         /*
381          * Huge pages should not be able to become secure
382          */
383         if (is_vm_hugetlb_page(vma))
384                 goto out;
385
386         rc = 0;
387         /* we take an extra reference here */
388         page = follow_page(vma, uaddr, FOLL_WRITE | FOLL_GET);
389         if (IS_ERR_OR_NULL(page))
390                 goto out;
391         rc = uv_destroy_owned_page(page_to_phys(page));
392         /*
393          * Fault handlers can race; it is possible that two CPUs will fault
394          * on the same secure page. One CPU can destroy the page, reboot,
395          * re-enter secure mode and import it, while the second CPU was
396          * stuck at the beginning of the handler. At some point the second
397          * CPU will be able to progress, and it will not be able to destroy
398          * the page. In that case we do not want to terminate the process,
399          * we instead try to export the page.
400          */
401         if (rc)
402                 rc = uv_convert_owned_from_secure(page_to_phys(page));
403         put_page(page);
404 out:
405         mmap_read_unlock(gmap->mm);
406         return rc;
407 }
408 EXPORT_SYMBOL_GPL(gmap_destroy_page);
409
410 /*
411  * To be called with the page locked or with an extra reference! This will
412  * prevent gmap_make_secure from touching the page concurrently. Having 2
413  * parallel make_page_accessible is fine, as the UV calls will become a
414  * no-op if the page is already exported.
415  */
416 int arch_make_page_accessible(struct page *page)
417 {
418         int rc = 0;
419
420         /* Hugepage cannot be protected, so nothing to do */
421         if (PageHuge(page))
422                 return 0;
423
424         /*
425          * PG_arch_1 is used in 3 places:
426          * 1. for kernel page tables during early boot
427          * 2. for storage keys of huge pages and KVM
428          * 3. As an indication that this page might be secure. This can
429          *    overindicate, e.g. we set the bit before calling
430          *    convert_to_secure.
431          * As secure pages are never huge, all 3 variants can co-exists.
432          */
433         if (!test_bit(PG_arch_1, &page->flags))
434                 return 0;
435
436         rc = uv_pin_shared(page_to_phys(page));
437         if (!rc) {
438                 clear_bit(PG_arch_1, &page->flags);
439                 return 0;
440         }
441
442         rc = uv_convert_from_secure(page_to_phys(page));
443         if (!rc) {
444                 clear_bit(PG_arch_1, &page->flags);
445                 return 0;
446         }
447
448         return rc;
449 }
450 EXPORT_SYMBOL_GPL(arch_make_page_accessible);
451
452 #endif
453
454 #if defined(CONFIG_PROTECTED_VIRTUALIZATION_GUEST) || IS_ENABLED(CONFIG_KVM)
455 static ssize_t uv_query_facilities(struct kobject *kobj,
456                                    struct kobj_attribute *attr, char *page)
457 {
458         return scnprintf(page, PAGE_SIZE, "%lx\n%lx\n%lx\n%lx\n",
459                         uv_info.inst_calls_list[0],
460                         uv_info.inst_calls_list[1],
461                         uv_info.inst_calls_list[2],
462                         uv_info.inst_calls_list[3]);
463 }
464
465 static struct kobj_attribute uv_query_facilities_attr =
466         __ATTR(facilities, 0444, uv_query_facilities, NULL);
467
468 static ssize_t uv_query_supp_se_hdr_ver(struct kobject *kobj,
469                                         struct kobj_attribute *attr, char *buf)
470 {
471         return sysfs_emit(buf, "%lx\n", uv_info.supp_se_hdr_ver);
472 }
473
474 static struct kobj_attribute uv_query_supp_se_hdr_ver_attr =
475         __ATTR(supp_se_hdr_ver, 0444, uv_query_supp_se_hdr_ver, NULL);
476
477 static ssize_t uv_query_supp_se_hdr_pcf(struct kobject *kobj,
478                                         struct kobj_attribute *attr, char *buf)
479 {
480         return sysfs_emit(buf, "%lx\n", uv_info.supp_se_hdr_pcf);
481 }
482
483 static struct kobj_attribute uv_query_supp_se_hdr_pcf_attr =
484         __ATTR(supp_se_hdr_pcf, 0444, uv_query_supp_se_hdr_pcf, NULL);
485
486 static ssize_t uv_query_dump_cpu_len(struct kobject *kobj,
487                                      struct kobj_attribute *attr, char *page)
488 {
489         return scnprintf(page, PAGE_SIZE, "%lx\n",
490                         uv_info.guest_cpu_stor_len);
491 }
492
493 static struct kobj_attribute uv_query_dump_cpu_len_attr =
494         __ATTR(uv_query_dump_cpu_len, 0444, uv_query_dump_cpu_len, NULL);
495
496 static ssize_t uv_query_dump_storage_state_len(struct kobject *kobj,
497                                                struct kobj_attribute *attr, char *page)
498 {
499         return scnprintf(page, PAGE_SIZE, "%lx\n",
500                         uv_info.conf_dump_storage_state_len);
501 }
502
503 static struct kobj_attribute uv_query_dump_storage_state_len_attr =
504         __ATTR(dump_storage_state_len, 0444, uv_query_dump_storage_state_len, NULL);
505
506 static ssize_t uv_query_dump_finalize_len(struct kobject *kobj,
507                                           struct kobj_attribute *attr, char *page)
508 {
509         return scnprintf(page, PAGE_SIZE, "%lx\n",
510                         uv_info.conf_dump_finalize_len);
511 }
512
513 static struct kobj_attribute uv_query_dump_finalize_len_attr =
514         __ATTR(dump_finalize_len, 0444, uv_query_dump_finalize_len, NULL);
515
516 static ssize_t uv_query_feature_indications(struct kobject *kobj,
517                                             struct kobj_attribute *attr, char *buf)
518 {
519         return sysfs_emit(buf, "%lx\n", uv_info.uv_feature_indications);
520 }
521
522 static struct kobj_attribute uv_query_feature_indications_attr =
523         __ATTR(feature_indications, 0444, uv_query_feature_indications, NULL);
524
525 static ssize_t uv_query_max_guest_cpus(struct kobject *kobj,
526                                        struct kobj_attribute *attr, char *page)
527 {
528         return scnprintf(page, PAGE_SIZE, "%d\n",
529                         uv_info.max_guest_cpu_id + 1);
530 }
531
532 static struct kobj_attribute uv_query_max_guest_cpus_attr =
533         __ATTR(max_cpus, 0444, uv_query_max_guest_cpus, NULL);
534
535 static ssize_t uv_query_max_guest_vms(struct kobject *kobj,
536                                       struct kobj_attribute *attr, char *page)
537 {
538         return scnprintf(page, PAGE_SIZE, "%d\n",
539                         uv_info.max_num_sec_conf);
540 }
541
542 static struct kobj_attribute uv_query_max_guest_vms_attr =
543         __ATTR(max_guests, 0444, uv_query_max_guest_vms, NULL);
544
545 static ssize_t uv_query_max_guest_addr(struct kobject *kobj,
546                                        struct kobj_attribute *attr, char *page)
547 {
548         return scnprintf(page, PAGE_SIZE, "%lx\n",
549                         uv_info.max_sec_stor_addr);
550 }
551
552 static struct kobj_attribute uv_query_max_guest_addr_attr =
553         __ATTR(max_address, 0444, uv_query_max_guest_addr, NULL);
554
555 static ssize_t uv_query_supp_att_req_hdr_ver(struct kobject *kobj,
556                                              struct kobj_attribute *attr, char *page)
557 {
558         return scnprintf(page, PAGE_SIZE, "%lx\n", uv_info.supp_att_req_hdr_ver);
559 }
560
561 static struct kobj_attribute uv_query_supp_att_req_hdr_ver_attr =
562         __ATTR(supp_att_req_hdr_ver, 0444, uv_query_supp_att_req_hdr_ver, NULL);
563
564 static ssize_t uv_query_supp_att_pflags(struct kobject *kobj,
565                                         struct kobj_attribute *attr, char *page)
566 {
567         return scnprintf(page, PAGE_SIZE, "%lx\n", uv_info.supp_att_pflags);
568 }
569
570 static struct kobj_attribute uv_query_supp_att_pflags_attr =
571         __ATTR(supp_att_pflags, 0444, uv_query_supp_att_pflags, NULL);
572
573 static struct attribute *uv_query_attrs[] = {
574         &uv_query_facilities_attr.attr,
575         &uv_query_feature_indications_attr.attr,
576         &uv_query_max_guest_cpus_attr.attr,
577         &uv_query_max_guest_vms_attr.attr,
578         &uv_query_max_guest_addr_attr.attr,
579         &uv_query_supp_se_hdr_ver_attr.attr,
580         &uv_query_supp_se_hdr_pcf_attr.attr,
581         &uv_query_dump_storage_state_len_attr.attr,
582         &uv_query_dump_finalize_len_attr.attr,
583         &uv_query_dump_cpu_len_attr.attr,
584         &uv_query_supp_att_req_hdr_ver_attr.attr,
585         &uv_query_supp_att_pflags_attr.attr,
586         NULL,
587 };
588
589 static struct attribute_group uv_query_attr_group = {
590         .attrs = uv_query_attrs,
591 };
592
593 static ssize_t uv_is_prot_virt_guest(struct kobject *kobj,
594                                      struct kobj_attribute *attr, char *page)
595 {
596         int val = 0;
597
598 #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST
599         val = prot_virt_guest;
600 #endif
601         return scnprintf(page, PAGE_SIZE, "%d\n", val);
602 }
603
604 static ssize_t uv_is_prot_virt_host(struct kobject *kobj,
605                                     struct kobj_attribute *attr, char *page)
606 {
607         int val = 0;
608
609 #if IS_ENABLED(CONFIG_KVM)
610         val = prot_virt_host;
611 #endif
612
613         return scnprintf(page, PAGE_SIZE, "%d\n", val);
614 }
615
616 static struct kobj_attribute uv_prot_virt_guest =
617         __ATTR(prot_virt_guest, 0444, uv_is_prot_virt_guest, NULL);
618
619 static struct kobj_attribute uv_prot_virt_host =
620         __ATTR(prot_virt_host, 0444, uv_is_prot_virt_host, NULL);
621
622 static const struct attribute *uv_prot_virt_attrs[] = {
623         &uv_prot_virt_guest.attr,
624         &uv_prot_virt_host.attr,
625         NULL,
626 };
627
628 static struct kset *uv_query_kset;
629 static struct kobject *uv_kobj;
630
631 static int __init uv_info_init(void)
632 {
633         int rc = -ENOMEM;
634
635         if (!test_facility(158))
636                 return 0;
637
638         uv_kobj = kobject_create_and_add("uv", firmware_kobj);
639         if (!uv_kobj)
640                 return -ENOMEM;
641
642         rc = sysfs_create_files(uv_kobj, uv_prot_virt_attrs);
643         if (rc)
644                 goto out_kobj;
645
646         uv_query_kset = kset_create_and_add("query", NULL, uv_kobj);
647         if (!uv_query_kset) {
648                 rc = -ENOMEM;
649                 goto out_ind_files;
650         }
651
652         rc = sysfs_create_group(&uv_query_kset->kobj, &uv_query_attr_group);
653         if (!rc)
654                 return 0;
655
656         kset_unregister(uv_query_kset);
657 out_ind_files:
658         sysfs_remove_files(uv_kobj, uv_prot_virt_attrs);
659 out_kobj:
660         kobject_del(uv_kobj);
661         kobject_put(uv_kobj);
662         return rc;
663 }
664 device_initcall(uv_info_init);
665 #endif