Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * KVM guest address space mapping code
4 *
5 * Copyright IBM Corp. 2007, 2020
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * David Hildenbrand <david@redhat.com>
8 * Janosch Frank <frankja@linux.vnet.ibm.com>
9 */
10
11#include <linux/kernel.h>
12#include <linux/pagewalk.h>
13#include <linux/swap.h>
14#include <linux/smp.h>
15#include <linux/spinlock.h>
16#include <linux/slab.h>
17#include <linux/swapops.h>
18#include <linux/ksm.h>
19#include <linux/mman.h>
20#include <linux/pgtable.h>
21#include <asm/page-states.h>
22#include <asm/pgalloc.h>
23#include <asm/gmap.h>
24#include <asm/page.h>
25#include <asm/tlb.h>
26
27#define GMAP_SHADOW_FAKE_TABLE 1ULL
28
29static struct page *gmap_alloc_crst(void)
30{
31 struct page *page;
32
33 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
34 if (!page)
35 return NULL;
36 __arch_set_page_dat(page_to_virt(page), 1UL << CRST_ALLOC_ORDER);
37 return page;
38}
39
40/**
41 * gmap_alloc - allocate and initialize a guest address space
42 * @limit: maximum address of the gmap address space
43 *
44 * Returns a guest address space structure.
45 */
46static struct gmap *gmap_alloc(unsigned long limit)
47{
48 struct gmap *gmap;
49 struct page *page;
50 unsigned long *table;
51 unsigned long etype, atype;
52
53 if (limit < _REGION3_SIZE) {
54 limit = _REGION3_SIZE - 1;
55 atype = _ASCE_TYPE_SEGMENT;
56 etype = _SEGMENT_ENTRY_EMPTY;
57 } else if (limit < _REGION2_SIZE) {
58 limit = _REGION2_SIZE - 1;
59 atype = _ASCE_TYPE_REGION3;
60 etype = _REGION3_ENTRY_EMPTY;
61 } else if (limit < _REGION1_SIZE) {
62 limit = _REGION1_SIZE - 1;
63 atype = _ASCE_TYPE_REGION2;
64 etype = _REGION2_ENTRY_EMPTY;
65 } else {
66 limit = -1UL;
67 atype = _ASCE_TYPE_REGION1;
68 etype = _REGION1_ENTRY_EMPTY;
69 }
70 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
71 if (!gmap)
72 goto out;
73 INIT_LIST_HEAD(&gmap->crst_list);
74 INIT_LIST_HEAD(&gmap->children);
75 INIT_LIST_HEAD(&gmap->pt_list);
76 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL_ACCOUNT);
77 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC | __GFP_ACCOUNT);
78 INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC | __GFP_ACCOUNT);
79 spin_lock_init(&gmap->guest_table_lock);
80 spin_lock_init(&gmap->shadow_lock);
81 refcount_set(&gmap->ref_count, 1);
82 page = gmap_alloc_crst();
83 if (!page)
84 goto out_free;
85 page->index = 0;
86 list_add(&page->lru, &gmap->crst_list);
87 table = page_to_virt(page);
88 crst_table_init(table, etype);
89 gmap->table = table;
90 gmap->asce = atype | _ASCE_TABLE_LENGTH |
91 _ASCE_USER_BITS | __pa(table);
92 gmap->asce_end = limit;
93 return gmap;
94
95out_free:
96 kfree(gmap);
97out:
98 return NULL;
99}
100
101/**
102 * gmap_create - create a guest address space
103 * @mm: pointer to the parent mm_struct
104 * @limit: maximum size of the gmap address space
105 *
106 * Returns a guest address space structure.
107 */
108struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
109{
110 struct gmap *gmap;
111 unsigned long gmap_asce;
112
113 gmap = gmap_alloc(limit);
114 if (!gmap)
115 return NULL;
116 gmap->mm = mm;
117 spin_lock(&mm->context.lock);
118 list_add_rcu(&gmap->list, &mm->context.gmap_list);
119 if (list_is_singular(&mm->context.gmap_list))
120 gmap_asce = gmap->asce;
121 else
122 gmap_asce = -1UL;
123 WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
124 spin_unlock(&mm->context.lock);
125 return gmap;
126}
127EXPORT_SYMBOL_GPL(gmap_create);
128
129static void gmap_flush_tlb(struct gmap *gmap)
130{
131 if (MACHINE_HAS_IDTE)
132 __tlb_flush_idte(gmap->asce);
133 else
134 __tlb_flush_global();
135}
136
137static void gmap_radix_tree_free(struct radix_tree_root *root)
138{
139 struct radix_tree_iter iter;
140 unsigned long indices[16];
141 unsigned long index;
142 void __rcu **slot;
143 int i, nr;
144
145 /* A radix tree is freed by deleting all of its entries */
146 index = 0;
147 do {
148 nr = 0;
149 radix_tree_for_each_slot(slot, root, &iter, index) {
150 indices[nr] = iter.index;
151 if (++nr == 16)
152 break;
153 }
154 for (i = 0; i < nr; i++) {
155 index = indices[i];
156 radix_tree_delete(root, index);
157 }
158 } while (nr > 0);
159}
160
161static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
162{
163 struct gmap_rmap *rmap, *rnext, *head;
164 struct radix_tree_iter iter;
165 unsigned long indices[16];
166 unsigned long index;
167 void __rcu **slot;
168 int i, nr;
169
170 /* A radix tree is freed by deleting all of its entries */
171 index = 0;
172 do {
173 nr = 0;
174 radix_tree_for_each_slot(slot, root, &iter, index) {
175 indices[nr] = iter.index;
176 if (++nr == 16)
177 break;
178 }
179 for (i = 0; i < nr; i++) {
180 index = indices[i];
181 head = radix_tree_delete(root, index);
182 gmap_for_each_rmap_safe(rmap, rnext, head)
183 kfree(rmap);
184 }
185 } while (nr > 0);
186}
187
188/**
189 * gmap_free - free a guest address space
190 * @gmap: pointer to the guest address space structure
191 *
192 * No locks required. There are no references to this gmap anymore.
193 */
194static void gmap_free(struct gmap *gmap)
195{
196 struct page *page, *next;
197
198 /* Flush tlb of all gmaps (if not already done for shadows) */
199 if (!(gmap_is_shadow(gmap) && gmap->removed))
200 gmap_flush_tlb(gmap);
201 /* Free all segment & region tables. */
202 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
203 __free_pages(page, CRST_ALLOC_ORDER);
204 gmap_radix_tree_free(&gmap->guest_to_host);
205 gmap_radix_tree_free(&gmap->host_to_guest);
206
207 /* Free additional data for a shadow gmap */
208 if (gmap_is_shadow(gmap)) {
209 struct ptdesc *ptdesc, *n;
210
211 /* Free all page tables. */
212 list_for_each_entry_safe(ptdesc, n, &gmap->pt_list, pt_list)
213 page_table_free_pgste(ptdesc);
214 gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
215 /* Release reference to the parent */
216 gmap_put(gmap->parent);
217 }
218
219 kfree(gmap);
220}
221
222/**
223 * gmap_get - increase reference counter for guest address space
224 * @gmap: pointer to the guest address space structure
225 *
226 * Returns the gmap pointer
227 */
228struct gmap *gmap_get(struct gmap *gmap)
229{
230 refcount_inc(&gmap->ref_count);
231 return gmap;
232}
233EXPORT_SYMBOL_GPL(gmap_get);
234
235/**
236 * gmap_put - decrease reference counter for guest address space
237 * @gmap: pointer to the guest address space structure
238 *
239 * If the reference counter reaches zero the guest address space is freed.
240 */
241void gmap_put(struct gmap *gmap)
242{
243 if (refcount_dec_and_test(&gmap->ref_count))
244 gmap_free(gmap);
245}
246EXPORT_SYMBOL_GPL(gmap_put);
247
248/**
249 * gmap_remove - remove a guest address space but do not free it yet
250 * @gmap: pointer to the guest address space structure
251 */
252void gmap_remove(struct gmap *gmap)
253{
254 struct gmap *sg, *next;
255 unsigned long gmap_asce;
256
257 /* Remove all shadow gmaps linked to this gmap */
258 if (!list_empty(&gmap->children)) {
259 spin_lock(&gmap->shadow_lock);
260 list_for_each_entry_safe(sg, next, &gmap->children, list) {
261 list_del(&sg->list);
262 gmap_put(sg);
263 }
264 spin_unlock(&gmap->shadow_lock);
265 }
266 /* Remove gmap from the pre-mm list */
267 spin_lock(&gmap->mm->context.lock);
268 list_del_rcu(&gmap->list);
269 if (list_empty(&gmap->mm->context.gmap_list))
270 gmap_asce = 0;
271 else if (list_is_singular(&gmap->mm->context.gmap_list))
272 gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
273 struct gmap, list)->asce;
274 else
275 gmap_asce = -1UL;
276 WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
277 spin_unlock(&gmap->mm->context.lock);
278 synchronize_rcu();
279 /* Put reference */
280 gmap_put(gmap);
281}
282EXPORT_SYMBOL_GPL(gmap_remove);
283
284/*
285 * gmap_alloc_table is assumed to be called with mmap_lock held
286 */
287static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
288 unsigned long init, unsigned long gaddr)
289{
290 struct page *page;
291 unsigned long *new;
292
293 /* since we dont free the gmap table until gmap_free we can unlock */
294 page = gmap_alloc_crst();
295 if (!page)
296 return -ENOMEM;
297 new = page_to_virt(page);
298 crst_table_init(new, init);
299 spin_lock(&gmap->guest_table_lock);
300 if (*table & _REGION_ENTRY_INVALID) {
301 list_add(&page->lru, &gmap->crst_list);
302 *table = __pa(new) | _REGION_ENTRY_LENGTH |
303 (*table & _REGION_ENTRY_TYPE_MASK);
304 page->index = gaddr;
305 page = NULL;
306 }
307 spin_unlock(&gmap->guest_table_lock);
308 if (page)
309 __free_pages(page, CRST_ALLOC_ORDER);
310 return 0;
311}
312
313/**
314 * __gmap_segment_gaddr - find virtual address from segment pointer
315 * @entry: pointer to a segment table entry in the guest address space
316 *
317 * Returns the virtual address in the guest address space for the segment
318 */
319static unsigned long __gmap_segment_gaddr(unsigned long *entry)
320{
321 struct page *page;
322 unsigned long offset;
323
324 offset = (unsigned long) entry / sizeof(unsigned long);
325 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
326 page = pmd_pgtable_page((pmd_t *) entry);
327 return page->index + offset;
328}
329
330/**
331 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
332 * @gmap: pointer to the guest address space structure
333 * @vmaddr: address in the host process address space
334 *
335 * Returns 1 if a TLB flush is required
336 */
337static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
338{
339 unsigned long *entry;
340 int flush = 0;
341
342 BUG_ON(gmap_is_shadow(gmap));
343 spin_lock(&gmap->guest_table_lock);
344 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
345 if (entry) {
346 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
347 *entry = _SEGMENT_ENTRY_EMPTY;
348 }
349 spin_unlock(&gmap->guest_table_lock);
350 return flush;
351}
352
353/**
354 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
355 * @gmap: pointer to the guest address space structure
356 * @gaddr: address in the guest address space
357 *
358 * Returns 1 if a TLB flush is required
359 */
360static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
361{
362 unsigned long vmaddr;
363
364 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
365 gaddr >> PMD_SHIFT);
366 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
367}
368
369/**
370 * gmap_unmap_segment - unmap segment from the guest address space
371 * @gmap: pointer to the guest address space structure
372 * @to: address in the guest address space
373 * @len: length of the memory area to unmap
374 *
375 * Returns 0 if the unmap succeeded, -EINVAL if not.
376 */
377int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
378{
379 unsigned long off;
380 int flush;
381
382 BUG_ON(gmap_is_shadow(gmap));
383 if ((to | len) & (PMD_SIZE - 1))
384 return -EINVAL;
385 if (len == 0 || to + len < to)
386 return -EINVAL;
387
388 flush = 0;
389 mmap_write_lock(gmap->mm);
390 for (off = 0; off < len; off += PMD_SIZE)
391 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
392 mmap_write_unlock(gmap->mm);
393 if (flush)
394 gmap_flush_tlb(gmap);
395 return 0;
396}
397EXPORT_SYMBOL_GPL(gmap_unmap_segment);
398
399/**
400 * gmap_map_segment - map a segment to the guest address space
401 * @gmap: pointer to the guest address space structure
402 * @from: source address in the parent address space
403 * @to: target address in the guest address space
404 * @len: length of the memory area to map
405 *
406 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
407 */
408int gmap_map_segment(struct gmap *gmap, unsigned long from,
409 unsigned long to, unsigned long len)
410{
411 unsigned long off;
412 int flush;
413
414 BUG_ON(gmap_is_shadow(gmap));
415 if ((from | to | len) & (PMD_SIZE - 1))
416 return -EINVAL;
417 if (len == 0 || from + len < from || to + len < to ||
418 from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
419 return -EINVAL;
420
421 flush = 0;
422 mmap_write_lock(gmap->mm);
423 for (off = 0; off < len; off += PMD_SIZE) {
424 /* Remove old translation */
425 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
426 /* Store new translation */
427 if (radix_tree_insert(&gmap->guest_to_host,
428 (to + off) >> PMD_SHIFT,
429 (void *) from + off))
430 break;
431 }
432 mmap_write_unlock(gmap->mm);
433 if (flush)
434 gmap_flush_tlb(gmap);
435 if (off >= len)
436 return 0;
437 gmap_unmap_segment(gmap, to, len);
438 return -ENOMEM;
439}
440EXPORT_SYMBOL_GPL(gmap_map_segment);
441
442/**
443 * __gmap_translate - translate a guest address to a user space address
444 * @gmap: pointer to guest mapping meta data structure
445 * @gaddr: guest address
446 *
447 * Returns user space address which corresponds to the guest address or
448 * -EFAULT if no such mapping exists.
449 * This function does not establish potentially missing page table entries.
450 * The mmap_lock of the mm that belongs to the address space must be held
451 * when this function gets called.
452 *
453 * Note: Can also be called for shadow gmaps.
454 */
455unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
456{
457 unsigned long vmaddr;
458
459 vmaddr = (unsigned long)
460 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
461 /* Note: guest_to_host is empty for a shadow gmap */
462 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
463}
464EXPORT_SYMBOL_GPL(__gmap_translate);
465
466/**
467 * gmap_translate - translate a guest address to a user space address
468 * @gmap: pointer to guest mapping meta data structure
469 * @gaddr: guest address
470 *
471 * Returns user space address which corresponds to the guest address or
472 * -EFAULT if no such mapping exists.
473 * This function does not establish potentially missing page table entries.
474 */
475unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
476{
477 unsigned long rc;
478
479 mmap_read_lock(gmap->mm);
480 rc = __gmap_translate(gmap, gaddr);
481 mmap_read_unlock(gmap->mm);
482 return rc;
483}
484EXPORT_SYMBOL_GPL(gmap_translate);
485
486/**
487 * gmap_unlink - disconnect a page table from the gmap shadow tables
488 * @mm: pointer to the parent mm_struct
489 * @table: pointer to the host page table
490 * @vmaddr: vm address associated with the host page table
491 */
492void gmap_unlink(struct mm_struct *mm, unsigned long *table,
493 unsigned long vmaddr)
494{
495 struct gmap *gmap;
496 int flush;
497
498 rcu_read_lock();
499 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
500 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
501 if (flush)
502 gmap_flush_tlb(gmap);
503 }
504 rcu_read_unlock();
505}
506
507static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
508 unsigned long gaddr);
509
510/**
511 * __gmap_link - set up shadow page tables to connect a host to a guest address
512 * @gmap: pointer to guest mapping meta data structure
513 * @gaddr: guest address
514 * @vmaddr: vm address
515 *
516 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
517 * if the vm address is already mapped to a different guest segment.
518 * The mmap_lock of the mm that belongs to the address space must be held
519 * when this function gets called.
520 */
521int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
522{
523 struct mm_struct *mm;
524 unsigned long *table;
525 spinlock_t *ptl;
526 pgd_t *pgd;
527 p4d_t *p4d;
528 pud_t *pud;
529 pmd_t *pmd;
530 u64 unprot;
531 int rc;
532
533 BUG_ON(gmap_is_shadow(gmap));
534 /* Create higher level tables in the gmap page table */
535 table = gmap->table;
536 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
537 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
538 if ((*table & _REGION_ENTRY_INVALID) &&
539 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
540 gaddr & _REGION1_MASK))
541 return -ENOMEM;
542 table = __va(*table & _REGION_ENTRY_ORIGIN);
543 }
544 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
545 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
546 if ((*table & _REGION_ENTRY_INVALID) &&
547 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
548 gaddr & _REGION2_MASK))
549 return -ENOMEM;
550 table = __va(*table & _REGION_ENTRY_ORIGIN);
551 }
552 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
553 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
554 if ((*table & _REGION_ENTRY_INVALID) &&
555 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
556 gaddr & _REGION3_MASK))
557 return -ENOMEM;
558 table = __va(*table & _REGION_ENTRY_ORIGIN);
559 }
560 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
561 /* Walk the parent mm page table */
562 mm = gmap->mm;
563 pgd = pgd_offset(mm, vmaddr);
564 VM_BUG_ON(pgd_none(*pgd));
565 p4d = p4d_offset(pgd, vmaddr);
566 VM_BUG_ON(p4d_none(*p4d));
567 pud = pud_offset(p4d, vmaddr);
568 VM_BUG_ON(pud_none(*pud));
569 /* large puds cannot yet be handled */
570 if (pud_leaf(*pud))
571 return -EFAULT;
572 pmd = pmd_offset(pud, vmaddr);
573 VM_BUG_ON(pmd_none(*pmd));
574 /* Are we allowed to use huge pages? */
575 if (pmd_leaf(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
576 return -EFAULT;
577 /* Link gmap segment table entry location to page table. */
578 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
579 if (rc)
580 return rc;
581 ptl = pmd_lock(mm, pmd);
582 spin_lock(&gmap->guest_table_lock);
583 if (*table == _SEGMENT_ENTRY_EMPTY) {
584 rc = radix_tree_insert(&gmap->host_to_guest,
585 vmaddr >> PMD_SHIFT, table);
586 if (!rc) {
587 if (pmd_leaf(*pmd)) {
588 *table = (pmd_val(*pmd) &
589 _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
590 | _SEGMENT_ENTRY_GMAP_UC
591 | _SEGMENT_ENTRY;
592 } else
593 *table = pmd_val(*pmd) &
594 _SEGMENT_ENTRY_HARDWARE_BITS;
595 }
596 } else if (*table & _SEGMENT_ENTRY_PROTECT &&
597 !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
598 unprot = (u64)*table;
599 unprot &= ~_SEGMENT_ENTRY_PROTECT;
600 unprot |= _SEGMENT_ENTRY_GMAP_UC;
601 gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
602 }
603 spin_unlock(&gmap->guest_table_lock);
604 spin_unlock(ptl);
605 radix_tree_preload_end();
606 return rc;
607}
608
609/**
610 * fixup_user_fault_nowait - manually resolve a user page fault without waiting
611 * @mm: mm_struct of target mm
612 * @address: user address
613 * @fault_flags:flags to pass down to handle_mm_fault()
614 * @unlocked: did we unlock the mmap_lock while retrying
615 *
616 * This function behaves similarly to fixup_user_fault(), but it guarantees
617 * that the fault will be resolved without waiting. The function might drop
618 * and re-acquire the mm lock, in which case @unlocked will be set to true.
619 *
620 * The guarantee is that the fault is handled without waiting, but the
621 * function itself might sleep, due to the lock.
622 *
623 * Context: Needs to be called with mm->mmap_lock held in read mode, and will
624 * return with the lock held in read mode; @unlocked will indicate whether
625 * the lock has been dropped and re-acquired. This is the same behaviour as
626 * fixup_user_fault().
627 *
628 * Return: 0 on success, -EAGAIN if the fault cannot be resolved without
629 * waiting, -EFAULT if the fault cannot be resolved, -ENOMEM if out of
630 * memory.
631 */
632static int fixup_user_fault_nowait(struct mm_struct *mm, unsigned long address,
633 unsigned int fault_flags, bool *unlocked)
634{
635 struct vm_area_struct *vma;
636 unsigned int test_flags;
637 vm_fault_t fault;
638 int rc;
639
640 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
641 test_flags = fault_flags & FAULT_FLAG_WRITE ? VM_WRITE : VM_READ;
642
643 vma = find_vma(mm, address);
644 if (unlikely(!vma || address < vma->vm_start))
645 return -EFAULT;
646 if (unlikely(!(vma->vm_flags & test_flags)))
647 return -EFAULT;
648
649 fault = handle_mm_fault(vma, address, fault_flags, NULL);
650 /* the mm lock has been dropped, take it again */
651 if (fault & VM_FAULT_COMPLETED) {
652 *unlocked = true;
653 mmap_read_lock(mm);
654 return 0;
655 }
656 /* the mm lock has not been dropped */
657 if (fault & VM_FAULT_ERROR) {
658 rc = vm_fault_to_errno(fault, 0);
659 BUG_ON(!rc);
660 return rc;
661 }
662 /* the mm lock has not been dropped because of FAULT_FLAG_RETRY_NOWAIT */
663 if (fault & VM_FAULT_RETRY)
664 return -EAGAIN;
665 /* nothing needed to be done and the mm lock has not been dropped */
666 return 0;
667}
668
669/**
670 * __gmap_fault - resolve a fault on a guest address
671 * @gmap: pointer to guest mapping meta data structure
672 * @gaddr: guest address
673 * @fault_flags: flags to pass down to handle_mm_fault()
674 *
675 * Context: Needs to be called with mm->mmap_lock held in read mode. Might
676 * drop and re-acquire the lock. Will always return with the lock held.
677 */
678static int __gmap_fault(struct gmap *gmap, unsigned long gaddr, unsigned int fault_flags)
679{
680 unsigned long vmaddr;
681 bool unlocked;
682 int rc = 0;
683
684retry:
685 unlocked = false;
686
687 vmaddr = __gmap_translate(gmap, gaddr);
688 if (IS_ERR_VALUE(vmaddr))
689 return vmaddr;
690
691 if (fault_flags & FAULT_FLAG_RETRY_NOWAIT)
692 rc = fixup_user_fault_nowait(gmap->mm, vmaddr, fault_flags, &unlocked);
693 else
694 rc = fixup_user_fault(gmap->mm, vmaddr, fault_flags, &unlocked);
695 if (rc)
696 return rc;
697 /*
698 * In the case that fixup_user_fault unlocked the mmap_lock during
699 * fault-in, redo __gmap_translate() to avoid racing with a
700 * map/unmap_segment.
701 * In particular, __gmap_translate(), fixup_user_fault{,_nowait}(),
702 * and __gmap_link() must all be called atomically in one go; if the
703 * lock had been dropped in between, a retry is needed.
704 */
705 if (unlocked)
706 goto retry;
707
708 return __gmap_link(gmap, gaddr, vmaddr);
709}
710
711/**
712 * gmap_fault - resolve a fault on a guest address
713 * @gmap: pointer to guest mapping meta data structure
714 * @gaddr: guest address
715 * @fault_flags: flags to pass down to handle_mm_fault()
716 *
717 * Returns 0 on success, -ENOMEM for out of memory conditions, -EFAULT if the
718 * vm address is already mapped to a different guest segment, and -EAGAIN if
719 * FAULT_FLAG_RETRY_NOWAIT was specified and the fault could not be processed
720 * immediately.
721 */
722int gmap_fault(struct gmap *gmap, unsigned long gaddr, unsigned int fault_flags)
723{
724 int rc;
725
726 mmap_read_lock(gmap->mm);
727 rc = __gmap_fault(gmap, gaddr, fault_flags);
728 mmap_read_unlock(gmap->mm);
729 return rc;
730}
731EXPORT_SYMBOL_GPL(gmap_fault);
732
733/*
734 * this function is assumed to be called with mmap_lock held
735 */
736void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
737{
738 struct vm_area_struct *vma;
739 unsigned long vmaddr;
740 spinlock_t *ptl;
741 pte_t *ptep;
742
743 /* Find the vm address for the guest address */
744 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
745 gaddr >> PMD_SHIFT);
746 if (vmaddr) {
747 vmaddr |= gaddr & ~PMD_MASK;
748
749 vma = vma_lookup(gmap->mm, vmaddr);
750 if (!vma || is_vm_hugetlb_page(vma))
751 return;
752
753 /* Get pointer to the page table entry */
754 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
755 if (likely(ptep)) {
756 ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
757 pte_unmap_unlock(ptep, ptl);
758 }
759 }
760}
761EXPORT_SYMBOL_GPL(__gmap_zap);
762
763void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
764{
765 unsigned long gaddr, vmaddr, size;
766 struct vm_area_struct *vma;
767
768 mmap_read_lock(gmap->mm);
769 for (gaddr = from; gaddr < to;
770 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
771 /* Find the vm address for the guest address */
772 vmaddr = (unsigned long)
773 radix_tree_lookup(&gmap->guest_to_host,
774 gaddr >> PMD_SHIFT);
775 if (!vmaddr)
776 continue;
777 vmaddr |= gaddr & ~PMD_MASK;
778 /* Find vma in the parent mm */
779 vma = find_vma(gmap->mm, vmaddr);
780 if (!vma)
781 continue;
782 /*
783 * We do not discard pages that are backed by
784 * hugetlbfs, so we don't have to refault them.
785 */
786 if (is_vm_hugetlb_page(vma))
787 continue;
788 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
789 zap_page_range_single(vma, vmaddr, size, NULL);
790 }
791 mmap_read_unlock(gmap->mm);
792}
793EXPORT_SYMBOL_GPL(gmap_discard);
794
795static LIST_HEAD(gmap_notifier_list);
796static DEFINE_SPINLOCK(gmap_notifier_lock);
797
798/**
799 * gmap_register_pte_notifier - register a pte invalidation callback
800 * @nb: pointer to the gmap notifier block
801 */
802void gmap_register_pte_notifier(struct gmap_notifier *nb)
803{
804 spin_lock(&gmap_notifier_lock);
805 list_add_rcu(&nb->list, &gmap_notifier_list);
806 spin_unlock(&gmap_notifier_lock);
807}
808EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
809
810/**
811 * gmap_unregister_pte_notifier - remove a pte invalidation callback
812 * @nb: pointer to the gmap notifier block
813 */
814void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
815{
816 spin_lock(&gmap_notifier_lock);
817 list_del_rcu(&nb->list);
818 spin_unlock(&gmap_notifier_lock);
819 synchronize_rcu();
820}
821EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
822
823/**
824 * gmap_call_notifier - call all registered invalidation callbacks
825 * @gmap: pointer to guest mapping meta data structure
826 * @start: start virtual address in the guest address space
827 * @end: end virtual address in the guest address space
828 */
829static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
830 unsigned long end)
831{
832 struct gmap_notifier *nb;
833
834 list_for_each_entry(nb, &gmap_notifier_list, list)
835 nb->notifier_call(gmap, start, end);
836}
837
838/**
839 * gmap_table_walk - walk the gmap page tables
840 * @gmap: pointer to guest mapping meta data structure
841 * @gaddr: virtual address in the guest address space
842 * @level: page table level to stop at
843 *
844 * Returns a table entry pointer for the given guest address and @level
845 * @level=0 : returns a pointer to a page table table entry (or NULL)
846 * @level=1 : returns a pointer to a segment table entry (or NULL)
847 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
848 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
849 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
850 *
851 * Returns NULL if the gmap page tables could not be walked to the
852 * requested level.
853 *
854 * Note: Can also be called for shadow gmaps.
855 */
856static inline unsigned long *gmap_table_walk(struct gmap *gmap,
857 unsigned long gaddr, int level)
858{
859 const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
860 unsigned long *table = gmap->table;
861
862 if (gmap_is_shadow(gmap) && gmap->removed)
863 return NULL;
864
865 if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
866 return NULL;
867
868 if (asce_type != _ASCE_TYPE_REGION1 &&
869 gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
870 return NULL;
871
872 switch (asce_type) {
873 case _ASCE_TYPE_REGION1:
874 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
875 if (level == 4)
876 break;
877 if (*table & _REGION_ENTRY_INVALID)
878 return NULL;
879 table = __va(*table & _REGION_ENTRY_ORIGIN);
880 fallthrough;
881 case _ASCE_TYPE_REGION2:
882 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
883 if (level == 3)
884 break;
885 if (*table & _REGION_ENTRY_INVALID)
886 return NULL;
887 table = __va(*table & _REGION_ENTRY_ORIGIN);
888 fallthrough;
889 case _ASCE_TYPE_REGION3:
890 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
891 if (level == 2)
892 break;
893 if (*table & _REGION_ENTRY_INVALID)
894 return NULL;
895 table = __va(*table & _REGION_ENTRY_ORIGIN);
896 fallthrough;
897 case _ASCE_TYPE_SEGMENT:
898 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
899 if (level == 1)
900 break;
901 if (*table & _REGION_ENTRY_INVALID)
902 return NULL;
903 table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
904 table += (gaddr & _PAGE_INDEX) >> PAGE_SHIFT;
905 }
906 return table;
907}
908
909/**
910 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
911 * and return the pte pointer
912 * @gmap: pointer to guest mapping meta data structure
913 * @gaddr: virtual address in the guest address space
914 * @ptl: pointer to the spinlock pointer
915 *
916 * Returns a pointer to the locked pte for a guest address, or NULL
917 */
918static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
919 spinlock_t **ptl)
920{
921 unsigned long *table;
922
923 BUG_ON(gmap_is_shadow(gmap));
924 /* Walk the gmap page table, lock and get pte pointer */
925 table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
926 if (!table || *table & _SEGMENT_ENTRY_INVALID)
927 return NULL;
928 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
929}
930
931/**
932 * gmap_pte_op_fixup - force a page in and connect the gmap page table
933 * @gmap: pointer to guest mapping meta data structure
934 * @gaddr: virtual address in the guest address space
935 * @vmaddr: address in the host process address space
936 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
937 *
938 * Returns 0 if the caller can retry __gmap_translate (might fail again),
939 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
940 * up or connecting the gmap page table.
941 */
942static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
943 unsigned long vmaddr, int prot)
944{
945 struct mm_struct *mm = gmap->mm;
946 unsigned int fault_flags;
947 bool unlocked = false;
948
949 BUG_ON(gmap_is_shadow(gmap));
950 fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
951 if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
952 return -EFAULT;
953 if (unlocked)
954 /* lost mmap_lock, caller has to retry __gmap_translate */
955 return 0;
956 /* Connect the page tables */
957 return __gmap_link(gmap, gaddr, vmaddr);
958}
959
960/**
961 * gmap_pte_op_end - release the page table lock
962 * @ptep: pointer to the locked pte
963 * @ptl: pointer to the page table spinlock
964 */
965static void gmap_pte_op_end(pte_t *ptep, spinlock_t *ptl)
966{
967 pte_unmap_unlock(ptep, ptl);
968}
969
970/**
971 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
972 * and return the pmd pointer
973 * @gmap: pointer to guest mapping meta data structure
974 * @gaddr: virtual address in the guest address space
975 *
976 * Returns a pointer to the pmd for a guest address, or NULL
977 */
978static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
979{
980 pmd_t *pmdp;
981
982 BUG_ON(gmap_is_shadow(gmap));
983 pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
984 if (!pmdp)
985 return NULL;
986
987 /* without huge pages, there is no need to take the table lock */
988 if (!gmap->mm->context.allow_gmap_hpage_1m)
989 return pmd_none(*pmdp) ? NULL : pmdp;
990
991 spin_lock(&gmap->guest_table_lock);
992 if (pmd_none(*pmdp)) {
993 spin_unlock(&gmap->guest_table_lock);
994 return NULL;
995 }
996
997 /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
998 if (!pmd_leaf(*pmdp))
999 spin_unlock(&gmap->guest_table_lock);
1000 return pmdp;
1001}
1002
1003/**
1004 * gmap_pmd_op_end - release the guest_table_lock if needed
1005 * @gmap: pointer to the guest mapping meta data structure
1006 * @pmdp: pointer to the pmd
1007 */
1008static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
1009{
1010 if (pmd_leaf(*pmdp))
1011 spin_unlock(&gmap->guest_table_lock);
1012}
1013
1014/*
1015 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
1016 * @pmdp: pointer to the pmd to be protected
1017 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1018 * @bits: notification bits to set
1019 *
1020 * Returns:
1021 * 0 if successfully protected
1022 * -EAGAIN if a fixup is needed
1023 * -EINVAL if unsupported notifier bits have been specified
1024 *
1025 * Expected to be called with sg->mm->mmap_lock in read and
1026 * guest_table_lock held.
1027 */
1028static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
1029 pmd_t *pmdp, int prot, unsigned long bits)
1030{
1031 int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
1032 int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
1033 pmd_t new = *pmdp;
1034
1035 /* Fixup needed */
1036 if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
1037 return -EAGAIN;
1038
1039 if (prot == PROT_NONE && !pmd_i) {
1040 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
1041 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
1042 }
1043
1044 if (prot == PROT_READ && !pmd_p) {
1045 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
1046 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_PROTECT));
1047 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
1048 }
1049
1050 if (bits & GMAP_NOTIFY_MPROT)
1051 set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
1052
1053 /* Shadow GMAP protection needs split PMDs */
1054 if (bits & GMAP_NOTIFY_SHADOW)
1055 return -EINVAL;
1056
1057 return 0;
1058}
1059
1060/*
1061 * gmap_protect_pte - remove access rights to memory and set pgste bits
1062 * @gmap: pointer to guest mapping meta data structure
1063 * @gaddr: virtual address in the guest address space
1064 * @pmdp: pointer to the pmd associated with the pte
1065 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1066 * @bits: notification bits to set
1067 *
1068 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1069 * -EAGAIN if a fixup is needed.
1070 *
1071 * Expected to be called with sg->mm->mmap_lock in read
1072 */
1073static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
1074 pmd_t *pmdp, int prot, unsigned long bits)
1075{
1076 int rc;
1077 pte_t *ptep;
1078 spinlock_t *ptl;
1079 unsigned long pbits = 0;
1080
1081 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1082 return -EAGAIN;
1083
1084 ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1085 if (!ptep)
1086 return -ENOMEM;
1087
1088 pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1089 pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
1090 /* Protect and unlock. */
1091 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
1092 gmap_pte_op_end(ptep, ptl);
1093 return rc;
1094}
1095
1096/*
1097 * gmap_protect_range - remove access rights to memory and set pgste bits
1098 * @gmap: pointer to guest mapping meta data structure
1099 * @gaddr: virtual address in the guest address space
1100 * @len: size of area
1101 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1102 * @bits: pgste notification bits to set
1103 *
1104 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1105 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1106 *
1107 * Called with sg->mm->mmap_lock in read.
1108 */
1109static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1110 unsigned long len, int prot, unsigned long bits)
1111{
1112 unsigned long vmaddr, dist;
1113 pmd_t *pmdp;
1114 int rc;
1115
1116 BUG_ON(gmap_is_shadow(gmap));
1117 while (len) {
1118 rc = -EAGAIN;
1119 pmdp = gmap_pmd_op_walk(gmap, gaddr);
1120 if (pmdp) {
1121 if (!pmd_leaf(*pmdp)) {
1122 rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1123 bits);
1124 if (!rc) {
1125 len -= PAGE_SIZE;
1126 gaddr += PAGE_SIZE;
1127 }
1128 } else {
1129 rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1130 bits);
1131 if (!rc) {
1132 dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1133 len = len < dist ? 0 : len - dist;
1134 gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1135 }
1136 }
1137 gmap_pmd_op_end(gmap, pmdp);
1138 }
1139 if (rc) {
1140 if (rc == -EINVAL)
1141 return rc;
1142
1143 /* -EAGAIN, fixup of userspace mm and gmap */
1144 vmaddr = __gmap_translate(gmap, gaddr);
1145 if (IS_ERR_VALUE(vmaddr))
1146 return vmaddr;
1147 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
1148 if (rc)
1149 return rc;
1150 }
1151 }
1152 return 0;
1153}
1154
1155/**
1156 * gmap_mprotect_notify - change access rights for a range of ptes and
1157 * call the notifier if any pte changes again
1158 * @gmap: pointer to guest mapping meta data structure
1159 * @gaddr: virtual address in the guest address space
1160 * @len: size of area
1161 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1162 *
1163 * Returns 0 if for each page in the given range a gmap mapping exists,
1164 * the new access rights could be set and the notifier could be armed.
1165 * If the gmap mapping is missing for one or more pages -EFAULT is
1166 * returned. If no memory could be allocated -ENOMEM is returned.
1167 * This function establishes missing page table entries.
1168 */
1169int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1170 unsigned long len, int prot)
1171{
1172 int rc;
1173
1174 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1175 return -EINVAL;
1176 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1177 return -EINVAL;
1178 mmap_read_lock(gmap->mm);
1179 rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
1180 mmap_read_unlock(gmap->mm);
1181 return rc;
1182}
1183EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
1184
1185/**
1186 * gmap_read_table - get an unsigned long value from a guest page table using
1187 * absolute addressing, without marking the page referenced.
1188 * @gmap: pointer to guest mapping meta data structure
1189 * @gaddr: virtual address in the guest address space
1190 * @val: pointer to the unsigned long value to return
1191 *
1192 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
1193 * if reading using the virtual address failed. -EINVAL if called on a gmap
1194 * shadow.
1195 *
1196 * Called with gmap->mm->mmap_lock in read.
1197 */
1198int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1199{
1200 unsigned long address, vmaddr;
1201 spinlock_t *ptl;
1202 pte_t *ptep, pte;
1203 int rc;
1204
1205 if (gmap_is_shadow(gmap))
1206 return -EINVAL;
1207
1208 while (1) {
1209 rc = -EAGAIN;
1210 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1211 if (ptep) {
1212 pte = *ptep;
1213 if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1214 address = pte_val(pte) & PAGE_MASK;
1215 address += gaddr & ~PAGE_MASK;
1216 *val = *(unsigned long *)__va(address);
1217 set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_YOUNG)));
1218 /* Do *NOT* clear the _PAGE_INVALID bit! */
1219 rc = 0;
1220 }
1221 gmap_pte_op_end(ptep, ptl);
1222 }
1223 if (!rc)
1224 break;
1225 vmaddr = __gmap_translate(gmap, gaddr);
1226 if (IS_ERR_VALUE(vmaddr)) {
1227 rc = vmaddr;
1228 break;
1229 }
1230 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1231 if (rc)
1232 break;
1233 }
1234 return rc;
1235}
1236EXPORT_SYMBOL_GPL(gmap_read_table);
1237
1238/**
1239 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1240 * @sg: pointer to the shadow guest address space structure
1241 * @vmaddr: vm address associated with the rmap
1242 * @rmap: pointer to the rmap structure
1243 *
1244 * Called with the sg->guest_table_lock
1245 */
1246static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1247 struct gmap_rmap *rmap)
1248{
1249 struct gmap_rmap *temp;
1250 void __rcu **slot;
1251
1252 BUG_ON(!gmap_is_shadow(sg));
1253 slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1254 if (slot) {
1255 rmap->next = radix_tree_deref_slot_protected(slot,
1256 &sg->guest_table_lock);
1257 for (temp = rmap->next; temp; temp = temp->next) {
1258 if (temp->raddr == rmap->raddr) {
1259 kfree(rmap);
1260 return;
1261 }
1262 }
1263 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1264 } else {
1265 rmap->next = NULL;
1266 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1267 rmap);
1268 }
1269}
1270
1271/**
1272 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
1273 * @sg: pointer to the shadow guest address space structure
1274 * @raddr: rmap address in the shadow gmap
1275 * @paddr: address in the parent guest address space
1276 * @len: length of the memory area to protect
1277 *
1278 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1279 * if out of memory and -EFAULT if paddr is invalid.
1280 */
1281static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1282 unsigned long paddr, unsigned long len)
1283{
1284 struct gmap *parent;
1285 struct gmap_rmap *rmap;
1286 unsigned long vmaddr;
1287 spinlock_t *ptl;
1288 pte_t *ptep;
1289 int rc;
1290
1291 BUG_ON(!gmap_is_shadow(sg));
1292 parent = sg->parent;
1293 while (len) {
1294 vmaddr = __gmap_translate(parent, paddr);
1295 if (IS_ERR_VALUE(vmaddr))
1296 return vmaddr;
1297 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
1298 if (!rmap)
1299 return -ENOMEM;
1300 rmap->raddr = raddr;
1301 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
1302 if (rc) {
1303 kfree(rmap);
1304 return rc;
1305 }
1306 rc = -EAGAIN;
1307 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1308 if (ptep) {
1309 spin_lock(&sg->guest_table_lock);
1310 rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
1311 PGSTE_VSIE_BIT);
1312 if (!rc)
1313 gmap_insert_rmap(sg, vmaddr, rmap);
1314 spin_unlock(&sg->guest_table_lock);
1315 gmap_pte_op_end(ptep, ptl);
1316 }
1317 radix_tree_preload_end();
1318 if (rc) {
1319 kfree(rmap);
1320 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
1321 if (rc)
1322 return rc;
1323 continue;
1324 }
1325 paddr += PAGE_SIZE;
1326 len -= PAGE_SIZE;
1327 }
1328 return 0;
1329}
1330
1331#define _SHADOW_RMAP_MASK 0x7
1332#define _SHADOW_RMAP_REGION1 0x5
1333#define _SHADOW_RMAP_REGION2 0x4
1334#define _SHADOW_RMAP_REGION3 0x3
1335#define _SHADOW_RMAP_SEGMENT 0x2
1336#define _SHADOW_RMAP_PGTABLE 0x1
1337
1338/**
1339 * gmap_idte_one - invalidate a single region or segment table entry
1340 * @asce: region or segment table *origin* + table-type bits
1341 * @vaddr: virtual address to identify the table entry to flush
1342 *
1343 * The invalid bit of a single region or segment table entry is set
1344 * and the associated TLB entries depending on the entry are flushed.
1345 * The table-type of the @asce identifies the portion of the @vaddr
1346 * that is used as the invalidation index.
1347 */
1348static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1349{
1350 asm volatile(
1351 " idte %0,0,%1"
1352 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1353}
1354
1355/**
1356 * gmap_unshadow_page - remove a page from a shadow page table
1357 * @sg: pointer to the shadow guest address space structure
1358 * @raddr: rmap address in the shadow guest address space
1359 *
1360 * Called with the sg->guest_table_lock
1361 */
1362static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1363{
1364 unsigned long *table;
1365
1366 BUG_ON(!gmap_is_shadow(sg));
1367 table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1368 if (!table || *table & _PAGE_INVALID)
1369 return;
1370 gmap_call_notifier(sg, raddr, raddr + PAGE_SIZE - 1);
1371 ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1372}
1373
1374/**
1375 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1376 * @sg: pointer to the shadow guest address space structure
1377 * @raddr: rmap address in the shadow guest address space
1378 * @pgt: pointer to the start of a shadow page table
1379 *
1380 * Called with the sg->guest_table_lock
1381 */
1382static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1383 unsigned long *pgt)
1384{
1385 int i;
1386
1387 BUG_ON(!gmap_is_shadow(sg));
1388 for (i = 0; i < _PAGE_ENTRIES; i++, raddr += PAGE_SIZE)
1389 pgt[i] = _PAGE_INVALID;
1390}
1391
1392/**
1393 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1394 * @sg: pointer to the shadow guest address space structure
1395 * @raddr: address in the shadow guest address space
1396 *
1397 * Called with the sg->guest_table_lock
1398 */
1399static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1400{
1401 unsigned long *ste;
1402 phys_addr_t sto, pgt;
1403 struct ptdesc *ptdesc;
1404
1405 BUG_ON(!gmap_is_shadow(sg));
1406 ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1407 if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1408 return;
1409 gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1410 sto = __pa(ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1411 gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1412 pgt = *ste & _SEGMENT_ENTRY_ORIGIN;
1413 *ste = _SEGMENT_ENTRY_EMPTY;
1414 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
1415 /* Free page table */
1416 ptdesc = page_ptdesc(phys_to_page(pgt));
1417 list_del(&ptdesc->pt_list);
1418 page_table_free_pgste(ptdesc);
1419}
1420
1421/**
1422 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1423 * @sg: pointer to the shadow guest address space structure
1424 * @raddr: rmap address in the shadow guest address space
1425 * @sgt: pointer to the start of a shadow segment table
1426 *
1427 * Called with the sg->guest_table_lock
1428 */
1429static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1430 unsigned long *sgt)
1431{
1432 struct ptdesc *ptdesc;
1433 phys_addr_t pgt;
1434 int i;
1435
1436 BUG_ON(!gmap_is_shadow(sg));
1437 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
1438 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1439 continue;
1440 pgt = sgt[i] & _REGION_ENTRY_ORIGIN;
1441 sgt[i] = _SEGMENT_ENTRY_EMPTY;
1442 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
1443 /* Free page table */
1444 ptdesc = page_ptdesc(phys_to_page(pgt));
1445 list_del(&ptdesc->pt_list);
1446 page_table_free_pgste(ptdesc);
1447 }
1448}
1449
1450/**
1451 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1452 * @sg: pointer to the shadow guest address space structure
1453 * @raddr: rmap address in the shadow guest address space
1454 *
1455 * Called with the shadow->guest_table_lock
1456 */
1457static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1458{
1459 unsigned long r3o, *r3e;
1460 phys_addr_t sgt;
1461 struct page *page;
1462
1463 BUG_ON(!gmap_is_shadow(sg));
1464 r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1465 if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1466 return;
1467 gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1468 r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1469 gmap_idte_one(__pa(r3o) | _ASCE_TYPE_REGION3, raddr);
1470 sgt = *r3e & _REGION_ENTRY_ORIGIN;
1471 *r3e = _REGION3_ENTRY_EMPTY;
1472 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
1473 /* Free segment table */
1474 page = phys_to_page(sgt);
1475 list_del(&page->lru);
1476 __free_pages(page, CRST_ALLOC_ORDER);
1477}
1478
1479/**
1480 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1481 * @sg: pointer to the shadow guest address space structure
1482 * @raddr: address in the shadow guest address space
1483 * @r3t: pointer to the start of a shadow region-3 table
1484 *
1485 * Called with the sg->guest_table_lock
1486 */
1487static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1488 unsigned long *r3t)
1489{
1490 struct page *page;
1491 phys_addr_t sgt;
1492 int i;
1493
1494 BUG_ON(!gmap_is_shadow(sg));
1495 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
1496 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1497 continue;
1498 sgt = r3t[i] & _REGION_ENTRY_ORIGIN;
1499 r3t[i] = _REGION3_ENTRY_EMPTY;
1500 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
1501 /* Free segment table */
1502 page = phys_to_page(sgt);
1503 list_del(&page->lru);
1504 __free_pages(page, CRST_ALLOC_ORDER);
1505 }
1506}
1507
1508/**
1509 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1510 * @sg: pointer to the shadow guest address space structure
1511 * @raddr: rmap address in the shadow guest address space
1512 *
1513 * Called with the sg->guest_table_lock
1514 */
1515static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1516{
1517 unsigned long r2o, *r2e;
1518 phys_addr_t r3t;
1519 struct page *page;
1520
1521 BUG_ON(!gmap_is_shadow(sg));
1522 r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1523 if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1524 return;
1525 gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1526 r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1527 gmap_idte_one(__pa(r2o) | _ASCE_TYPE_REGION2, raddr);
1528 r3t = *r2e & _REGION_ENTRY_ORIGIN;
1529 *r2e = _REGION2_ENTRY_EMPTY;
1530 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
1531 /* Free region 3 table */
1532 page = phys_to_page(r3t);
1533 list_del(&page->lru);
1534 __free_pages(page, CRST_ALLOC_ORDER);
1535}
1536
1537/**
1538 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1539 * @sg: pointer to the shadow guest address space structure
1540 * @raddr: rmap address in the shadow guest address space
1541 * @r2t: pointer to the start of a shadow region-2 table
1542 *
1543 * Called with the sg->guest_table_lock
1544 */
1545static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1546 unsigned long *r2t)
1547{
1548 phys_addr_t r3t;
1549 struct page *page;
1550 int i;
1551
1552 BUG_ON(!gmap_is_shadow(sg));
1553 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
1554 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1555 continue;
1556 r3t = r2t[i] & _REGION_ENTRY_ORIGIN;
1557 r2t[i] = _REGION2_ENTRY_EMPTY;
1558 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
1559 /* Free region 3 table */
1560 page = phys_to_page(r3t);
1561 list_del(&page->lru);
1562 __free_pages(page, CRST_ALLOC_ORDER);
1563 }
1564}
1565
1566/**
1567 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1568 * @sg: pointer to the shadow guest address space structure
1569 * @raddr: rmap address in the shadow guest address space
1570 *
1571 * Called with the sg->guest_table_lock
1572 */
1573static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1574{
1575 unsigned long r1o, *r1e;
1576 struct page *page;
1577 phys_addr_t r2t;
1578
1579 BUG_ON(!gmap_is_shadow(sg));
1580 r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1581 if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1582 return;
1583 gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1584 r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1585 gmap_idte_one(__pa(r1o) | _ASCE_TYPE_REGION1, raddr);
1586 r2t = *r1e & _REGION_ENTRY_ORIGIN;
1587 *r1e = _REGION1_ENTRY_EMPTY;
1588 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
1589 /* Free region 2 table */
1590 page = phys_to_page(r2t);
1591 list_del(&page->lru);
1592 __free_pages(page, CRST_ALLOC_ORDER);
1593}
1594
1595/**
1596 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1597 * @sg: pointer to the shadow guest address space structure
1598 * @raddr: rmap address in the shadow guest address space
1599 * @r1t: pointer to the start of a shadow region-1 table
1600 *
1601 * Called with the shadow->guest_table_lock
1602 */
1603static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1604 unsigned long *r1t)
1605{
1606 unsigned long asce;
1607 struct page *page;
1608 phys_addr_t r2t;
1609 int i;
1610
1611 BUG_ON(!gmap_is_shadow(sg));
1612 asce = __pa(r1t) | _ASCE_TYPE_REGION1;
1613 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1614 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1615 continue;
1616 r2t = r1t[i] & _REGION_ENTRY_ORIGIN;
1617 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
1618 /* Clear entry and flush translation r1t -> r2t */
1619 gmap_idte_one(asce, raddr);
1620 r1t[i] = _REGION1_ENTRY_EMPTY;
1621 /* Free region 2 table */
1622 page = phys_to_page(r2t);
1623 list_del(&page->lru);
1624 __free_pages(page, CRST_ALLOC_ORDER);
1625 }
1626}
1627
1628/**
1629 * gmap_unshadow - remove a shadow page table completely
1630 * @sg: pointer to the shadow guest address space structure
1631 *
1632 * Called with sg->guest_table_lock
1633 */
1634static void gmap_unshadow(struct gmap *sg)
1635{
1636 unsigned long *table;
1637
1638 BUG_ON(!gmap_is_shadow(sg));
1639 if (sg->removed)
1640 return;
1641 sg->removed = 1;
1642 gmap_call_notifier(sg, 0, -1UL);
1643 gmap_flush_tlb(sg);
1644 table = __va(sg->asce & _ASCE_ORIGIN);
1645 switch (sg->asce & _ASCE_TYPE_MASK) {
1646 case _ASCE_TYPE_REGION1:
1647 __gmap_unshadow_r1t(sg, 0, table);
1648 break;
1649 case _ASCE_TYPE_REGION2:
1650 __gmap_unshadow_r2t(sg, 0, table);
1651 break;
1652 case _ASCE_TYPE_REGION3:
1653 __gmap_unshadow_r3t(sg, 0, table);
1654 break;
1655 case _ASCE_TYPE_SEGMENT:
1656 __gmap_unshadow_sgt(sg, 0, table);
1657 break;
1658 }
1659}
1660
1661/**
1662 * gmap_find_shadow - find a specific asce in the list of shadow tables
1663 * @parent: pointer to the parent gmap
1664 * @asce: ASCE for which the shadow table is created
1665 * @edat_level: edat level to be used for the shadow translation
1666 *
1667 * Returns the pointer to a gmap if a shadow table with the given asce is
1668 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1669 * otherwise NULL
1670 */
1671static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1672 int edat_level)
1673{
1674 struct gmap *sg;
1675
1676 list_for_each_entry(sg, &parent->children, list) {
1677 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1678 sg->removed)
1679 continue;
1680 if (!sg->initialized)
1681 return ERR_PTR(-EAGAIN);
1682 refcount_inc(&sg->ref_count);
1683 return sg;
1684 }
1685 return NULL;
1686}
1687
1688/**
1689 * gmap_shadow_valid - check if a shadow guest address space matches the
1690 * given properties and is still valid
1691 * @sg: pointer to the shadow guest address space structure
1692 * @asce: ASCE for which the shadow table is requested
1693 * @edat_level: edat level to be used for the shadow translation
1694 *
1695 * Returns 1 if the gmap shadow is still valid and matches the given
1696 * properties, the caller can continue using it. Returns 0 otherwise, the
1697 * caller has to request a new shadow gmap in this case.
1698 *
1699 */
1700int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1701{
1702 if (sg->removed)
1703 return 0;
1704 return sg->orig_asce == asce && sg->edat_level == edat_level;
1705}
1706EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1707
1708/**
1709 * gmap_shadow - create/find a shadow guest address space
1710 * @parent: pointer to the parent gmap
1711 * @asce: ASCE for which the shadow table is created
1712 * @edat_level: edat level to be used for the shadow translation
1713 *
1714 * The pages of the top level page table referred by the asce parameter
1715 * will be set to read-only and marked in the PGSTEs of the kvm process.
1716 * The shadow table will be removed automatically on any change to the
1717 * PTE mapping for the source table.
1718 *
1719 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1720 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1721 * parent gmap table could not be protected.
1722 */
1723struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1724 int edat_level)
1725{
1726 struct gmap *sg, *new;
1727 unsigned long limit;
1728 int rc;
1729
1730 BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
1731 BUG_ON(gmap_is_shadow(parent));
1732 spin_lock(&parent->shadow_lock);
1733 sg = gmap_find_shadow(parent, asce, edat_level);
1734 spin_unlock(&parent->shadow_lock);
1735 if (sg)
1736 return sg;
1737 /* Create a new shadow gmap */
1738 limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1739 if (asce & _ASCE_REAL_SPACE)
1740 limit = -1UL;
1741 new = gmap_alloc(limit);
1742 if (!new)
1743 return ERR_PTR(-ENOMEM);
1744 new->mm = parent->mm;
1745 new->parent = gmap_get(parent);
1746 new->private = parent->private;
1747 new->orig_asce = asce;
1748 new->edat_level = edat_level;
1749 new->initialized = false;
1750 spin_lock(&parent->shadow_lock);
1751 /* Recheck if another CPU created the same shadow */
1752 sg = gmap_find_shadow(parent, asce, edat_level);
1753 if (sg) {
1754 spin_unlock(&parent->shadow_lock);
1755 gmap_free(new);
1756 return sg;
1757 }
1758 if (asce & _ASCE_REAL_SPACE) {
1759 /* only allow one real-space gmap shadow */
1760 list_for_each_entry(sg, &parent->children, list) {
1761 if (sg->orig_asce & _ASCE_REAL_SPACE) {
1762 spin_lock(&sg->guest_table_lock);
1763 gmap_unshadow(sg);
1764 spin_unlock(&sg->guest_table_lock);
1765 list_del(&sg->list);
1766 gmap_put(sg);
1767 break;
1768 }
1769 }
1770 }
1771 refcount_set(&new->ref_count, 2);
1772 list_add(&new->list, &parent->children);
1773 if (asce & _ASCE_REAL_SPACE) {
1774 /* nothing to protect, return right away */
1775 new->initialized = true;
1776 spin_unlock(&parent->shadow_lock);
1777 return new;
1778 }
1779 spin_unlock(&parent->shadow_lock);
1780 /* protect after insertion, so it will get properly invalidated */
1781 mmap_read_lock(parent->mm);
1782 rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1783 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1784 PROT_READ, GMAP_NOTIFY_SHADOW);
1785 mmap_read_unlock(parent->mm);
1786 spin_lock(&parent->shadow_lock);
1787 new->initialized = true;
1788 if (rc) {
1789 list_del(&new->list);
1790 gmap_free(new);
1791 new = ERR_PTR(rc);
1792 }
1793 spin_unlock(&parent->shadow_lock);
1794 return new;
1795}
1796EXPORT_SYMBOL_GPL(gmap_shadow);
1797
1798/**
1799 * gmap_shadow_r2t - create an empty shadow region 2 table
1800 * @sg: pointer to the shadow guest address space structure
1801 * @saddr: faulting address in the shadow gmap
1802 * @r2t: parent gmap address of the region 2 table to get shadowed
1803 * @fake: r2t references contiguous guest memory block, not a r2t
1804 *
1805 * The r2t parameter specifies the address of the source table. The
1806 * four pages of the source table are made read-only in the parent gmap
1807 * address space. A write to the source table area @r2t will automatically
1808 * remove the shadow r2 table and all of its descendants.
1809 *
1810 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1811 * shadow table structure is incomplete, -ENOMEM if out of memory and
1812 * -EFAULT if an address in the parent gmap could not be resolved.
1813 *
1814 * Called with sg->mm->mmap_lock in read.
1815 */
1816int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1817 int fake)
1818{
1819 unsigned long raddr, origin, offset, len;
1820 unsigned long *table;
1821 phys_addr_t s_r2t;
1822 struct page *page;
1823 int rc;
1824
1825 BUG_ON(!gmap_is_shadow(sg));
1826 /* Allocate a shadow region second table */
1827 page = gmap_alloc_crst();
1828 if (!page)
1829 return -ENOMEM;
1830 page->index = r2t & _REGION_ENTRY_ORIGIN;
1831 if (fake)
1832 page->index |= GMAP_SHADOW_FAKE_TABLE;
1833 s_r2t = page_to_phys(page);
1834 /* Install shadow region second table */
1835 spin_lock(&sg->guest_table_lock);
1836 table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1837 if (!table) {
1838 rc = -EAGAIN; /* Race with unshadow */
1839 goto out_free;
1840 }
1841 if (!(*table & _REGION_ENTRY_INVALID)) {
1842 rc = 0; /* Already established */
1843 goto out_free;
1844 } else if (*table & _REGION_ENTRY_ORIGIN) {
1845 rc = -EAGAIN; /* Race with shadow */
1846 goto out_free;
1847 }
1848 crst_table_init(__va(s_r2t), _REGION2_ENTRY_EMPTY);
1849 /* mark as invalid as long as the parent table is not protected */
1850 *table = s_r2t | _REGION_ENTRY_LENGTH |
1851 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1852 if (sg->edat_level >= 1)
1853 *table |= (r2t & _REGION_ENTRY_PROTECT);
1854 list_add(&page->lru, &sg->crst_list);
1855 if (fake) {
1856 /* nothing to protect for fake tables */
1857 *table &= ~_REGION_ENTRY_INVALID;
1858 spin_unlock(&sg->guest_table_lock);
1859 return 0;
1860 }
1861 spin_unlock(&sg->guest_table_lock);
1862 /* Make r2t read-only in parent gmap page table */
1863 raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1864 origin = r2t & _REGION_ENTRY_ORIGIN;
1865 offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1866 len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1867 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1868 spin_lock(&sg->guest_table_lock);
1869 if (!rc) {
1870 table = gmap_table_walk(sg, saddr, 4);
1871 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r2t)
1872 rc = -EAGAIN; /* Race with unshadow */
1873 else
1874 *table &= ~_REGION_ENTRY_INVALID;
1875 } else {
1876 gmap_unshadow_r2t(sg, raddr);
1877 }
1878 spin_unlock(&sg->guest_table_lock);
1879 return rc;
1880out_free:
1881 spin_unlock(&sg->guest_table_lock);
1882 __free_pages(page, CRST_ALLOC_ORDER);
1883 return rc;
1884}
1885EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1886
1887/**
1888 * gmap_shadow_r3t - create a shadow region 3 table
1889 * @sg: pointer to the shadow guest address space structure
1890 * @saddr: faulting address in the shadow gmap
1891 * @r3t: parent gmap address of the region 3 table to get shadowed
1892 * @fake: r3t references contiguous guest memory block, not a r3t
1893 *
1894 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1895 * shadow table structure is incomplete, -ENOMEM if out of memory and
1896 * -EFAULT if an address in the parent gmap could not be resolved.
1897 *
1898 * Called with sg->mm->mmap_lock in read.
1899 */
1900int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1901 int fake)
1902{
1903 unsigned long raddr, origin, offset, len;
1904 unsigned long *table;
1905 phys_addr_t s_r3t;
1906 struct page *page;
1907 int rc;
1908
1909 BUG_ON(!gmap_is_shadow(sg));
1910 /* Allocate a shadow region second table */
1911 page = gmap_alloc_crst();
1912 if (!page)
1913 return -ENOMEM;
1914 page->index = r3t & _REGION_ENTRY_ORIGIN;
1915 if (fake)
1916 page->index |= GMAP_SHADOW_FAKE_TABLE;
1917 s_r3t = page_to_phys(page);
1918 /* Install shadow region second table */
1919 spin_lock(&sg->guest_table_lock);
1920 table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1921 if (!table) {
1922 rc = -EAGAIN; /* Race with unshadow */
1923 goto out_free;
1924 }
1925 if (!(*table & _REGION_ENTRY_INVALID)) {
1926 rc = 0; /* Already established */
1927 goto out_free;
1928 } else if (*table & _REGION_ENTRY_ORIGIN) {
1929 rc = -EAGAIN; /* Race with shadow */
1930 goto out_free;
1931 }
1932 crst_table_init(__va(s_r3t), _REGION3_ENTRY_EMPTY);
1933 /* mark as invalid as long as the parent table is not protected */
1934 *table = s_r3t | _REGION_ENTRY_LENGTH |
1935 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1936 if (sg->edat_level >= 1)
1937 *table |= (r3t & _REGION_ENTRY_PROTECT);
1938 list_add(&page->lru, &sg->crst_list);
1939 if (fake) {
1940 /* nothing to protect for fake tables */
1941 *table &= ~_REGION_ENTRY_INVALID;
1942 spin_unlock(&sg->guest_table_lock);
1943 return 0;
1944 }
1945 spin_unlock(&sg->guest_table_lock);
1946 /* Make r3t read-only in parent gmap page table */
1947 raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1948 origin = r3t & _REGION_ENTRY_ORIGIN;
1949 offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1950 len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1951 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1952 spin_lock(&sg->guest_table_lock);
1953 if (!rc) {
1954 table = gmap_table_walk(sg, saddr, 3);
1955 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r3t)
1956 rc = -EAGAIN; /* Race with unshadow */
1957 else
1958 *table &= ~_REGION_ENTRY_INVALID;
1959 } else {
1960 gmap_unshadow_r3t(sg, raddr);
1961 }
1962 spin_unlock(&sg->guest_table_lock);
1963 return rc;
1964out_free:
1965 spin_unlock(&sg->guest_table_lock);
1966 __free_pages(page, CRST_ALLOC_ORDER);
1967 return rc;
1968}
1969EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1970
1971/**
1972 * gmap_shadow_sgt - create a shadow segment table
1973 * @sg: pointer to the shadow guest address space structure
1974 * @saddr: faulting address in the shadow gmap
1975 * @sgt: parent gmap address of the segment table to get shadowed
1976 * @fake: sgt references contiguous guest memory block, not a sgt
1977 *
1978 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1979 * shadow table structure is incomplete, -ENOMEM if out of memory and
1980 * -EFAULT if an address in the parent gmap could not be resolved.
1981 *
1982 * Called with sg->mm->mmap_lock in read.
1983 */
1984int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1985 int fake)
1986{
1987 unsigned long raddr, origin, offset, len;
1988 unsigned long *table;
1989 phys_addr_t s_sgt;
1990 struct page *page;
1991 int rc;
1992
1993 BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1994 /* Allocate a shadow segment table */
1995 page = gmap_alloc_crst();
1996 if (!page)
1997 return -ENOMEM;
1998 page->index = sgt & _REGION_ENTRY_ORIGIN;
1999 if (fake)
2000 page->index |= GMAP_SHADOW_FAKE_TABLE;
2001 s_sgt = page_to_phys(page);
2002 /* Install shadow region second table */
2003 spin_lock(&sg->guest_table_lock);
2004 table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
2005 if (!table) {
2006 rc = -EAGAIN; /* Race with unshadow */
2007 goto out_free;
2008 }
2009 if (!(*table & _REGION_ENTRY_INVALID)) {
2010 rc = 0; /* Already established */
2011 goto out_free;
2012 } else if (*table & _REGION_ENTRY_ORIGIN) {
2013 rc = -EAGAIN; /* Race with shadow */
2014 goto out_free;
2015 }
2016 crst_table_init(__va(s_sgt), _SEGMENT_ENTRY_EMPTY);
2017 /* mark as invalid as long as the parent table is not protected */
2018 *table = s_sgt | _REGION_ENTRY_LENGTH |
2019 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
2020 if (sg->edat_level >= 1)
2021 *table |= sgt & _REGION_ENTRY_PROTECT;
2022 list_add(&page->lru, &sg->crst_list);
2023 if (fake) {
2024 /* nothing to protect for fake tables */
2025 *table &= ~_REGION_ENTRY_INVALID;
2026 spin_unlock(&sg->guest_table_lock);
2027 return 0;
2028 }
2029 spin_unlock(&sg->guest_table_lock);
2030 /* Make sgt read-only in parent gmap page table */
2031 raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
2032 origin = sgt & _REGION_ENTRY_ORIGIN;
2033 offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
2034 len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
2035 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
2036 spin_lock(&sg->guest_table_lock);
2037 if (!rc) {
2038 table = gmap_table_walk(sg, saddr, 2);
2039 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_sgt)
2040 rc = -EAGAIN; /* Race with unshadow */
2041 else
2042 *table &= ~_REGION_ENTRY_INVALID;
2043 } else {
2044 gmap_unshadow_sgt(sg, raddr);
2045 }
2046 spin_unlock(&sg->guest_table_lock);
2047 return rc;
2048out_free:
2049 spin_unlock(&sg->guest_table_lock);
2050 __free_pages(page, CRST_ALLOC_ORDER);
2051 return rc;
2052}
2053EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
2054
2055/**
2056 * gmap_shadow_pgt_lookup - find a shadow page table
2057 * @sg: pointer to the shadow guest address space structure
2058 * @saddr: the address in the shadow aguest address space
2059 * @pgt: parent gmap address of the page table to get shadowed
2060 * @dat_protection: if the pgtable is marked as protected by dat
2061 * @fake: pgt references contiguous guest memory block, not a pgtable
2062 *
2063 * Returns 0 if the shadow page table was found and -EAGAIN if the page
2064 * table was not found.
2065 *
2066 * Called with sg->mm->mmap_lock in read.
2067 */
2068int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
2069 unsigned long *pgt, int *dat_protection,
2070 int *fake)
2071{
2072 unsigned long *table;
2073 struct page *page;
2074 int rc;
2075
2076 BUG_ON(!gmap_is_shadow(sg));
2077 spin_lock(&sg->guest_table_lock);
2078 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2079 if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
2080 /* Shadow page tables are full pages (pte+pgste) */
2081 page = pfn_to_page(*table >> PAGE_SHIFT);
2082 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
2083 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
2084 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
2085 rc = 0;
2086 } else {
2087 rc = -EAGAIN;
2088 }
2089 spin_unlock(&sg->guest_table_lock);
2090 return rc;
2091
2092}
2093EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2094
2095/**
2096 * gmap_shadow_pgt - instantiate a shadow page table
2097 * @sg: pointer to the shadow guest address space structure
2098 * @saddr: faulting address in the shadow gmap
2099 * @pgt: parent gmap address of the page table to get shadowed
2100 * @fake: pgt references contiguous guest memory block, not a pgtable
2101 *
2102 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2103 * shadow table structure is incomplete, -ENOMEM if out of memory,
2104 * -EFAULT if an address in the parent gmap could not be resolved and
2105 *
2106 * Called with gmap->mm->mmap_lock in read
2107 */
2108int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2109 int fake)
2110{
2111 unsigned long raddr, origin;
2112 unsigned long *table;
2113 struct ptdesc *ptdesc;
2114 phys_addr_t s_pgt;
2115 int rc;
2116
2117 BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
2118 /* Allocate a shadow page table */
2119 ptdesc = page_table_alloc_pgste(sg->mm);
2120 if (!ptdesc)
2121 return -ENOMEM;
2122 ptdesc->pt_index = pgt & _SEGMENT_ENTRY_ORIGIN;
2123 if (fake)
2124 ptdesc->pt_index |= GMAP_SHADOW_FAKE_TABLE;
2125 s_pgt = page_to_phys(ptdesc_page(ptdesc));
2126 /* Install shadow page table */
2127 spin_lock(&sg->guest_table_lock);
2128 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2129 if (!table) {
2130 rc = -EAGAIN; /* Race with unshadow */
2131 goto out_free;
2132 }
2133 if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2134 rc = 0; /* Already established */
2135 goto out_free;
2136 } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2137 rc = -EAGAIN; /* Race with shadow */
2138 goto out_free;
2139 }
2140 /* mark as invalid as long as the parent table is not protected */
2141 *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
2142 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
2143 list_add(&ptdesc->pt_list, &sg->pt_list);
2144 if (fake) {
2145 /* nothing to protect for fake tables */
2146 *table &= ~_SEGMENT_ENTRY_INVALID;
2147 spin_unlock(&sg->guest_table_lock);
2148 return 0;
2149 }
2150 spin_unlock(&sg->guest_table_lock);
2151 /* Make pgt read-only in parent gmap page table (not the pgste) */
2152 raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
2153 origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
2154 rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
2155 spin_lock(&sg->guest_table_lock);
2156 if (!rc) {
2157 table = gmap_table_walk(sg, saddr, 1);
2158 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) != s_pgt)
2159 rc = -EAGAIN; /* Race with unshadow */
2160 else
2161 *table &= ~_SEGMENT_ENTRY_INVALID;
2162 } else {
2163 gmap_unshadow_pgt(sg, raddr);
2164 }
2165 spin_unlock(&sg->guest_table_lock);
2166 return rc;
2167out_free:
2168 spin_unlock(&sg->guest_table_lock);
2169 page_table_free_pgste(ptdesc);
2170 return rc;
2171
2172}
2173EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2174
2175/**
2176 * gmap_shadow_page - create a shadow page mapping
2177 * @sg: pointer to the shadow guest address space structure
2178 * @saddr: faulting address in the shadow gmap
2179 * @pte: pte in parent gmap address space to get shadowed
2180 *
2181 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2182 * shadow table structure is incomplete, -ENOMEM if out of memory and
2183 * -EFAULT if an address in the parent gmap could not be resolved.
2184 *
2185 * Called with sg->mm->mmap_lock in read.
2186 */
2187int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
2188{
2189 struct gmap *parent;
2190 struct gmap_rmap *rmap;
2191 unsigned long vmaddr, paddr;
2192 spinlock_t *ptl;
2193 pte_t *sptep, *tptep;
2194 int prot;
2195 int rc;
2196
2197 BUG_ON(!gmap_is_shadow(sg));
2198 parent = sg->parent;
2199 prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
2200
2201 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
2202 if (!rmap)
2203 return -ENOMEM;
2204 rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2205
2206 while (1) {
2207 paddr = pte_val(pte) & PAGE_MASK;
2208 vmaddr = __gmap_translate(parent, paddr);
2209 if (IS_ERR_VALUE(vmaddr)) {
2210 rc = vmaddr;
2211 break;
2212 }
2213 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
2214 if (rc)
2215 break;
2216 rc = -EAGAIN;
2217 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2218 if (sptep) {
2219 spin_lock(&sg->guest_table_lock);
2220 /* Get page table pointer */
2221 tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2222 if (!tptep) {
2223 spin_unlock(&sg->guest_table_lock);
2224 gmap_pte_op_end(sptep, ptl);
2225 radix_tree_preload_end();
2226 break;
2227 }
2228 rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
2229 if (rc > 0) {
2230 /* Success and a new mapping */
2231 gmap_insert_rmap(sg, vmaddr, rmap);
2232 rmap = NULL;
2233 rc = 0;
2234 }
2235 gmap_pte_op_end(sptep, ptl);
2236 spin_unlock(&sg->guest_table_lock);
2237 }
2238 radix_tree_preload_end();
2239 if (!rc)
2240 break;
2241 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2242 if (rc)
2243 break;
2244 }
2245 kfree(rmap);
2246 return rc;
2247}
2248EXPORT_SYMBOL_GPL(gmap_shadow_page);
2249
2250/*
2251 * gmap_shadow_notify - handle notifications for shadow gmap
2252 *
2253 * Called with sg->parent->shadow_lock.
2254 */
2255static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2256 unsigned long gaddr)
2257{
2258 struct gmap_rmap *rmap, *rnext, *head;
2259 unsigned long start, end, bits, raddr;
2260
2261 BUG_ON(!gmap_is_shadow(sg));
2262
2263 spin_lock(&sg->guest_table_lock);
2264 if (sg->removed) {
2265 spin_unlock(&sg->guest_table_lock);
2266 return;
2267 }
2268 /* Check for top level table */
2269 start = sg->orig_asce & _ASCE_ORIGIN;
2270 end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2271 if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2272 gaddr < end) {
2273 /* The complete shadow table has to go */
2274 gmap_unshadow(sg);
2275 spin_unlock(&sg->guest_table_lock);
2276 list_del(&sg->list);
2277 gmap_put(sg);
2278 return;
2279 }
2280 /* Remove the page table tree from on specific entry */
2281 head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2282 gmap_for_each_rmap_safe(rmap, rnext, head) {
2283 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2284 raddr = rmap->raddr ^ bits;
2285 switch (bits) {
2286 case _SHADOW_RMAP_REGION1:
2287 gmap_unshadow_r2t(sg, raddr);
2288 break;
2289 case _SHADOW_RMAP_REGION2:
2290 gmap_unshadow_r3t(sg, raddr);
2291 break;
2292 case _SHADOW_RMAP_REGION3:
2293 gmap_unshadow_sgt(sg, raddr);
2294 break;
2295 case _SHADOW_RMAP_SEGMENT:
2296 gmap_unshadow_pgt(sg, raddr);
2297 break;
2298 case _SHADOW_RMAP_PGTABLE:
2299 gmap_unshadow_page(sg, raddr);
2300 break;
2301 }
2302 kfree(rmap);
2303 }
2304 spin_unlock(&sg->guest_table_lock);
2305}
2306
2307/**
2308 * ptep_notify - call all invalidation callbacks for a specific pte.
2309 * @mm: pointer to the process mm_struct
2310 * @vmaddr: virtual address in the process address space
2311 * @pte: pointer to the page table entry
2312 * @bits: bits from the pgste that caused the notify call
2313 *
2314 * This function is assumed to be called with the page table lock held
2315 * for the pte to notify.
2316 */
2317void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2318 pte_t *pte, unsigned long bits)
2319{
2320 unsigned long offset, gaddr = 0;
2321 unsigned long *table;
2322 struct gmap *gmap, *sg, *next;
2323
2324 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2325 offset = offset * (PAGE_SIZE / sizeof(pte_t));
2326 rcu_read_lock();
2327 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2328 spin_lock(&gmap->guest_table_lock);
2329 table = radix_tree_lookup(&gmap->host_to_guest,
2330 vmaddr >> PMD_SHIFT);
2331 if (table)
2332 gaddr = __gmap_segment_gaddr(table) + offset;
2333 spin_unlock(&gmap->guest_table_lock);
2334 if (!table)
2335 continue;
2336
2337 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2338 spin_lock(&gmap->shadow_lock);
2339 list_for_each_entry_safe(sg, next,
2340 &gmap->children, list)
2341 gmap_shadow_notify(sg, vmaddr, gaddr);
2342 spin_unlock(&gmap->shadow_lock);
2343 }
2344 if (bits & PGSTE_IN_BIT)
2345 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2346 }
2347 rcu_read_unlock();
2348}
2349EXPORT_SYMBOL_GPL(ptep_notify);
2350
2351static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2352 unsigned long gaddr)
2353{
2354 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
2355 gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2356}
2357
2358/**
2359 * gmap_pmdp_xchg - exchange a gmap pmd with another
2360 * @gmap: pointer to the guest address space structure
2361 * @pmdp: pointer to the pmd entry
2362 * @new: replacement entry
2363 * @gaddr: the affected guest address
2364 *
2365 * This function is assumed to be called with the guest_table_lock
2366 * held.
2367 */
2368static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2369 unsigned long gaddr)
2370{
2371 gaddr &= HPAGE_MASK;
2372 pmdp_notify_gmap(gmap, pmdp, gaddr);
2373 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_GMAP_IN));
2374 if (MACHINE_HAS_TLB_GUEST)
2375 __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2376 IDTE_GLOBAL);
2377 else if (MACHINE_HAS_IDTE)
2378 __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2379 else
2380 __pmdp_csp(pmdp);
2381 set_pmd(pmdp, new);
2382}
2383
2384static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2385 int purge)
2386{
2387 pmd_t *pmdp;
2388 struct gmap *gmap;
2389 unsigned long gaddr;
2390
2391 rcu_read_lock();
2392 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2393 spin_lock(&gmap->guest_table_lock);
2394 pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2395 vmaddr >> PMD_SHIFT);
2396 if (pmdp) {
2397 gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2398 pmdp_notify_gmap(gmap, pmdp, gaddr);
2399 WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2400 _SEGMENT_ENTRY_GMAP_UC |
2401 _SEGMENT_ENTRY));
2402 if (purge)
2403 __pmdp_csp(pmdp);
2404 set_pmd(pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
2405 }
2406 spin_unlock(&gmap->guest_table_lock);
2407 }
2408 rcu_read_unlock();
2409}
2410
2411/**
2412 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2413 * flushing
2414 * @mm: pointer to the process mm_struct
2415 * @vmaddr: virtual address in the process address space
2416 */
2417void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2418{
2419 gmap_pmdp_clear(mm, vmaddr, 0);
2420}
2421EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2422
2423/**
2424 * gmap_pmdp_csp - csp all affected guest pmd entries
2425 * @mm: pointer to the process mm_struct
2426 * @vmaddr: virtual address in the process address space
2427 */
2428void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2429{
2430 gmap_pmdp_clear(mm, vmaddr, 1);
2431}
2432EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2433
2434/**
2435 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2436 * @mm: pointer to the process mm_struct
2437 * @vmaddr: virtual address in the process address space
2438 */
2439void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2440{
2441 unsigned long *entry, gaddr;
2442 struct gmap *gmap;
2443 pmd_t *pmdp;
2444
2445 rcu_read_lock();
2446 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2447 spin_lock(&gmap->guest_table_lock);
2448 entry = radix_tree_delete(&gmap->host_to_guest,
2449 vmaddr >> PMD_SHIFT);
2450 if (entry) {
2451 pmdp = (pmd_t *)entry;
2452 gaddr = __gmap_segment_gaddr(entry);
2453 pmdp_notify_gmap(gmap, pmdp, gaddr);
2454 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2455 _SEGMENT_ENTRY_GMAP_UC |
2456 _SEGMENT_ENTRY));
2457 if (MACHINE_HAS_TLB_GUEST)
2458 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2459 gmap->asce, IDTE_LOCAL);
2460 else if (MACHINE_HAS_IDTE)
2461 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2462 *entry = _SEGMENT_ENTRY_EMPTY;
2463 }
2464 spin_unlock(&gmap->guest_table_lock);
2465 }
2466 rcu_read_unlock();
2467}
2468EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2469
2470/**
2471 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2472 * @mm: pointer to the process mm_struct
2473 * @vmaddr: virtual address in the process address space
2474 */
2475void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2476{
2477 unsigned long *entry, gaddr;
2478 struct gmap *gmap;
2479 pmd_t *pmdp;
2480
2481 rcu_read_lock();
2482 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2483 spin_lock(&gmap->guest_table_lock);
2484 entry = radix_tree_delete(&gmap->host_to_guest,
2485 vmaddr >> PMD_SHIFT);
2486 if (entry) {
2487 pmdp = (pmd_t *)entry;
2488 gaddr = __gmap_segment_gaddr(entry);
2489 pmdp_notify_gmap(gmap, pmdp, gaddr);
2490 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2491 _SEGMENT_ENTRY_GMAP_UC |
2492 _SEGMENT_ENTRY));
2493 if (MACHINE_HAS_TLB_GUEST)
2494 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2495 gmap->asce, IDTE_GLOBAL);
2496 else if (MACHINE_HAS_IDTE)
2497 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2498 else
2499 __pmdp_csp(pmdp);
2500 *entry = _SEGMENT_ENTRY_EMPTY;
2501 }
2502 spin_unlock(&gmap->guest_table_lock);
2503 }
2504 rcu_read_unlock();
2505}
2506EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2507
2508/**
2509 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2510 * @gmap: pointer to guest address space
2511 * @pmdp: pointer to the pmd to be tested
2512 * @gaddr: virtual address in the guest address space
2513 *
2514 * This function is assumed to be called with the guest_table_lock
2515 * held.
2516 */
2517static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2518 unsigned long gaddr)
2519{
2520 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2521 return false;
2522
2523 /* Already protected memory, which did not change is clean */
2524 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2525 !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2526 return false;
2527
2528 /* Clear UC indication and reset protection */
2529 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_UC)));
2530 gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2531 return true;
2532}
2533
2534/**
2535 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2536 * @gmap: pointer to guest address space
2537 * @bitmap: dirty bitmap for this pmd
2538 * @gaddr: virtual address in the guest address space
2539 * @vmaddr: virtual address in the host address space
2540 *
2541 * This function is assumed to be called with the guest_table_lock
2542 * held.
2543 */
2544void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2545 unsigned long gaddr, unsigned long vmaddr)
2546{
2547 int i;
2548 pmd_t *pmdp;
2549 pte_t *ptep;
2550 spinlock_t *ptl;
2551
2552 pmdp = gmap_pmd_op_walk(gmap, gaddr);
2553 if (!pmdp)
2554 return;
2555
2556 if (pmd_leaf(*pmdp)) {
2557 if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2558 bitmap_fill(bitmap, _PAGE_ENTRIES);
2559 } else {
2560 for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2561 ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2562 if (!ptep)
2563 continue;
2564 if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2565 set_bit(i, bitmap);
2566 pte_unmap_unlock(ptep, ptl);
2567 }
2568 }
2569 gmap_pmd_op_end(gmap, pmdp);
2570}
2571EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2572
2573#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2574static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
2575 unsigned long end, struct mm_walk *walk)
2576{
2577 struct vm_area_struct *vma = walk->vma;
2578
2579 split_huge_pmd(vma, pmd, addr);
2580 return 0;
2581}
2582
2583static const struct mm_walk_ops thp_split_walk_ops = {
2584 .pmd_entry = thp_split_walk_pmd_entry,
2585 .walk_lock = PGWALK_WRLOCK_VERIFY,
2586};
2587
2588static inline void thp_split_mm(struct mm_struct *mm)
2589{
2590 struct vm_area_struct *vma;
2591 VMA_ITERATOR(vmi, mm, 0);
2592
2593 for_each_vma(vmi, vma) {
2594 vm_flags_mod(vma, VM_NOHUGEPAGE, VM_HUGEPAGE);
2595 walk_page_vma(vma, &thp_split_walk_ops, NULL);
2596 }
2597 mm->def_flags |= VM_NOHUGEPAGE;
2598}
2599#else
2600static inline void thp_split_mm(struct mm_struct *mm)
2601{
2602}
2603#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2604
2605/*
2606 * switch on pgstes for its userspace process (for kvm)
2607 */
2608int s390_enable_sie(void)
2609{
2610 struct mm_struct *mm = current->mm;
2611
2612 /* Do we have pgstes? if yes, we are done */
2613 if (mm_has_pgste(mm))
2614 return 0;
2615 /* Fail if the page tables are 2K */
2616 if (!mm_alloc_pgste(mm))
2617 return -EINVAL;
2618 mmap_write_lock(mm);
2619 mm->context.has_pgste = 1;
2620 /* split thp mappings and disable thp for future mappings */
2621 thp_split_mm(mm);
2622 mmap_write_unlock(mm);
2623 return 0;
2624}
2625EXPORT_SYMBOL_GPL(s390_enable_sie);
2626
2627static int find_zeropage_pte_entry(pte_t *pte, unsigned long addr,
2628 unsigned long end, struct mm_walk *walk)
2629{
2630 unsigned long *found_addr = walk->private;
2631
2632 /* Return 1 of the page is a zeropage. */
2633 if (is_zero_pfn(pte_pfn(*pte))) {
2634 /*
2635 * Shared zeropage in e.g., a FS DAX mapping? We cannot do the
2636 * right thing and likely don't care: FAULT_FLAG_UNSHARE
2637 * currently only works in COW mappings, which is also where
2638 * mm_forbids_zeropage() is checked.
2639 */
2640 if (!is_cow_mapping(walk->vma->vm_flags))
2641 return -EFAULT;
2642
2643 *found_addr = addr;
2644 return 1;
2645 }
2646 return 0;
2647}
2648
2649static const struct mm_walk_ops find_zeropage_ops = {
2650 .pte_entry = find_zeropage_pte_entry,
2651 .walk_lock = PGWALK_WRLOCK,
2652};
2653
2654/*
2655 * Unshare all shared zeropages, replacing them by anonymous pages. Note that
2656 * we cannot simply zap all shared zeropages, because this could later
2657 * trigger unexpected userfaultfd missing events.
2658 *
2659 * This must be called after mm->context.allow_cow_sharing was
2660 * set to 0, to avoid future mappings of shared zeropages.
2661 *
2662 * mm contracts with s390, that even if mm were to remove a page table,
2663 * and racing with walk_page_range_vma() calling pte_offset_map_lock()
2664 * would fail, it will never insert a page table containing empty zero
2665 * pages once mm_forbids_zeropage(mm) i.e.
2666 * mm->context.allow_cow_sharing is set to 0.
2667 */
2668static int __s390_unshare_zeropages(struct mm_struct *mm)
2669{
2670 struct vm_area_struct *vma;
2671 VMA_ITERATOR(vmi, mm, 0);
2672 unsigned long addr;
2673 vm_fault_t fault;
2674 int rc;
2675
2676 for_each_vma(vmi, vma) {
2677 /*
2678 * We could only look at COW mappings, but it's more future
2679 * proof to catch unexpected zeropages in other mappings and
2680 * fail.
2681 */
2682 if ((vma->vm_flags & VM_PFNMAP) || is_vm_hugetlb_page(vma))
2683 continue;
2684 addr = vma->vm_start;
2685
2686retry:
2687 rc = walk_page_range_vma(vma, addr, vma->vm_end,
2688 &find_zeropage_ops, &addr);
2689 if (rc < 0)
2690 return rc;
2691 else if (!rc)
2692 continue;
2693
2694 /* addr was updated by find_zeropage_pte_entry() */
2695 fault = handle_mm_fault(vma, addr,
2696 FAULT_FLAG_UNSHARE | FAULT_FLAG_REMOTE,
2697 NULL);
2698 if (fault & VM_FAULT_OOM)
2699 return -ENOMEM;
2700 /*
2701 * See break_ksm(): even after handle_mm_fault() returned 0, we
2702 * must start the lookup from the current address, because
2703 * handle_mm_fault() may back out if there's any difficulty.
2704 *
2705 * VM_FAULT_SIGBUS and VM_FAULT_SIGSEGV are unexpected but
2706 * maybe they could trigger in the future on concurrent
2707 * truncation. In that case, the shared zeropage would be gone
2708 * and we can simply retry and make progress.
2709 */
2710 cond_resched();
2711 goto retry;
2712 }
2713
2714 return 0;
2715}
2716
2717static int __s390_disable_cow_sharing(struct mm_struct *mm)
2718{
2719 int rc;
2720
2721 if (!mm->context.allow_cow_sharing)
2722 return 0;
2723
2724 mm->context.allow_cow_sharing = 0;
2725
2726 /* Replace all shared zeropages by anonymous pages. */
2727 rc = __s390_unshare_zeropages(mm);
2728 /*
2729 * Make sure to disable KSM (if enabled for the whole process or
2730 * individual VMAs). Note that nothing currently hinders user space
2731 * from re-enabling it.
2732 */
2733 if (!rc)
2734 rc = ksm_disable(mm);
2735 if (rc)
2736 mm->context.allow_cow_sharing = 1;
2737 return rc;
2738}
2739
2740/*
2741 * Disable most COW-sharing of memory pages for the whole process:
2742 * (1) Disable KSM and unmerge/unshare any KSM pages.
2743 * (2) Disallow shared zeropages and unshare any zerpages that are mapped.
2744 *
2745 * Not that we currently don't bother with COW-shared pages that are shared
2746 * with parent/child processes due to fork().
2747 */
2748int s390_disable_cow_sharing(void)
2749{
2750 int rc;
2751
2752 mmap_write_lock(current->mm);
2753 rc = __s390_disable_cow_sharing(current->mm);
2754 mmap_write_unlock(current->mm);
2755 return rc;
2756}
2757EXPORT_SYMBOL_GPL(s390_disable_cow_sharing);
2758
2759/*
2760 * Enable storage key handling from now on and initialize the storage
2761 * keys with the default key.
2762 */
2763static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2764 unsigned long next, struct mm_walk *walk)
2765{
2766 /* Clear storage key */
2767 ptep_zap_key(walk->mm, addr, pte);
2768 return 0;
2769}
2770
2771/*
2772 * Give a chance to schedule after setting a key to 256 pages.
2773 * We only hold the mm lock, which is a rwsem and the kvm srcu.
2774 * Both can sleep.
2775 */
2776static int __s390_enable_skey_pmd(pmd_t *pmd, unsigned long addr,
2777 unsigned long next, struct mm_walk *walk)
2778{
2779 cond_resched();
2780 return 0;
2781}
2782
2783static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2784 unsigned long hmask, unsigned long next,
2785 struct mm_walk *walk)
2786{
2787 pmd_t *pmd = (pmd_t *)pte;
2788 unsigned long start, end;
2789 struct folio *folio = page_folio(pmd_page(*pmd));
2790
2791 /*
2792 * The write check makes sure we do not set a key on shared
2793 * memory. This is needed as the walker does not differentiate
2794 * between actual guest memory and the process executable or
2795 * shared libraries.
2796 */
2797 if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2798 !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2799 return 0;
2800
2801 start = pmd_val(*pmd) & HPAGE_MASK;
2802 end = start + HPAGE_SIZE;
2803 __storage_key_init_range(start, end);
2804 set_bit(PG_arch_1, &folio->flags);
2805 cond_resched();
2806 return 0;
2807}
2808
2809static const struct mm_walk_ops enable_skey_walk_ops = {
2810 .hugetlb_entry = __s390_enable_skey_hugetlb,
2811 .pte_entry = __s390_enable_skey_pte,
2812 .pmd_entry = __s390_enable_skey_pmd,
2813 .walk_lock = PGWALK_WRLOCK,
2814};
2815
2816int s390_enable_skey(void)
2817{
2818 struct mm_struct *mm = current->mm;
2819 int rc = 0;
2820
2821 mmap_write_lock(mm);
2822 if (mm_uses_skeys(mm))
2823 goto out_up;
2824
2825 mm->context.uses_skeys = 1;
2826 rc = __s390_disable_cow_sharing(mm);
2827 if (rc) {
2828 mm->context.uses_skeys = 0;
2829 goto out_up;
2830 }
2831 walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
2832
2833out_up:
2834 mmap_write_unlock(mm);
2835 return rc;
2836}
2837EXPORT_SYMBOL_GPL(s390_enable_skey);
2838
2839/*
2840 * Reset CMMA state, make all pages stable again.
2841 */
2842static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2843 unsigned long next, struct mm_walk *walk)
2844{
2845 ptep_zap_unused(walk->mm, addr, pte, 1);
2846 return 0;
2847}
2848
2849static const struct mm_walk_ops reset_cmma_walk_ops = {
2850 .pte_entry = __s390_reset_cmma,
2851 .walk_lock = PGWALK_WRLOCK,
2852};
2853
2854void s390_reset_cmma(struct mm_struct *mm)
2855{
2856 mmap_write_lock(mm);
2857 walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
2858 mmap_write_unlock(mm);
2859}
2860EXPORT_SYMBOL_GPL(s390_reset_cmma);
2861
2862#define GATHER_GET_PAGES 32
2863
2864struct reset_walk_state {
2865 unsigned long next;
2866 unsigned long count;
2867 unsigned long pfns[GATHER_GET_PAGES];
2868};
2869
2870static int s390_gather_pages(pte_t *ptep, unsigned long addr,
2871 unsigned long next, struct mm_walk *walk)
2872{
2873 struct reset_walk_state *p = walk->private;
2874 pte_t pte = READ_ONCE(*ptep);
2875
2876 if (pte_present(pte)) {
2877 /* we have a reference from the mapping, take an extra one */
2878 get_page(phys_to_page(pte_val(pte)));
2879 p->pfns[p->count] = phys_to_pfn(pte_val(pte));
2880 p->next = next;
2881 p->count++;
2882 }
2883 return p->count >= GATHER_GET_PAGES;
2884}
2885
2886static const struct mm_walk_ops gather_pages_ops = {
2887 .pte_entry = s390_gather_pages,
2888 .walk_lock = PGWALK_RDLOCK,
2889};
2890
2891/*
2892 * Call the Destroy secure page UVC on each page in the given array of PFNs.
2893 * Each page needs to have an extra reference, which will be released here.
2894 */
2895void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
2896{
2897 struct folio *folio;
2898 unsigned long i;
2899
2900 for (i = 0; i < count; i++) {
2901 folio = pfn_folio(pfns[i]);
2902 /* we always have an extra reference */
2903 uv_destroy_folio(folio);
2904 /* get rid of the extra reference */
2905 folio_put(folio);
2906 cond_resched();
2907 }
2908}
2909EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
2910
2911/**
2912 * __s390_uv_destroy_range - Call the destroy secure page UVC on each page
2913 * in the given range of the given address space.
2914 * @mm: the mm to operate on
2915 * @start: the start of the range
2916 * @end: the end of the range
2917 * @interruptible: if not 0, stop when a fatal signal is received
2918 *
2919 * Walk the given range of the given address space and call the destroy
2920 * secure page UVC on each page. Optionally exit early if a fatal signal is
2921 * pending.
2922 *
2923 * Return: 0 on success, -EINTR if the function stopped before completing
2924 */
2925int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
2926 unsigned long end, bool interruptible)
2927{
2928 struct reset_walk_state state = { .next = start };
2929 int r = 1;
2930
2931 while (r > 0) {
2932 state.count = 0;
2933 mmap_read_lock(mm);
2934 r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
2935 mmap_read_unlock(mm);
2936 cond_resched();
2937 s390_uv_destroy_pfns(state.count, state.pfns);
2938 if (interruptible && fatal_signal_pending(current))
2939 return -EINTR;
2940 }
2941 return 0;
2942}
2943EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
2944
2945/**
2946 * s390_unlist_old_asce - Remove the topmost level of page tables from the
2947 * list of page tables of the gmap.
2948 * @gmap: the gmap whose table is to be removed
2949 *
2950 * On s390x, KVM keeps a list of all pages containing the page tables of the
2951 * gmap (the CRST list). This list is used at tear down time to free all
2952 * pages that are now not needed anymore.
2953 *
2954 * This function removes the topmost page of the tree (the one pointed to by
2955 * the ASCE) from the CRST list.
2956 *
2957 * This means that it will not be freed when the VM is torn down, and needs
2958 * to be handled separately by the caller, unless a leak is actually
2959 * intended. Notice that this function will only remove the page from the
2960 * list, the page will still be used as a top level page table (and ASCE).
2961 */
2962void s390_unlist_old_asce(struct gmap *gmap)
2963{
2964 struct page *old;
2965
2966 old = virt_to_page(gmap->table);
2967 spin_lock(&gmap->guest_table_lock);
2968 list_del(&old->lru);
2969 /*
2970 * Sometimes the topmost page might need to be "removed" multiple
2971 * times, for example if the VM is rebooted into secure mode several
2972 * times concurrently, or if s390_replace_asce fails after calling
2973 * s390_remove_old_asce and is attempted again later. In that case
2974 * the old asce has been removed from the list, and therefore it
2975 * will not be freed when the VM terminates, but the ASCE is still
2976 * in use and still pointed to.
2977 * A subsequent call to replace_asce will follow the pointer and try
2978 * to remove the same page from the list again.
2979 * Therefore it's necessary that the page of the ASCE has valid
2980 * pointers, so list_del can work (and do nothing) without
2981 * dereferencing stale or invalid pointers.
2982 */
2983 INIT_LIST_HEAD(&old->lru);
2984 spin_unlock(&gmap->guest_table_lock);
2985}
2986EXPORT_SYMBOL_GPL(s390_unlist_old_asce);
2987
2988/**
2989 * s390_replace_asce - Try to replace the current ASCE of a gmap with a copy
2990 * @gmap: the gmap whose ASCE needs to be replaced
2991 *
2992 * If the ASCE is a SEGMENT type then this function will return -EINVAL,
2993 * otherwise the pointers in the host_to_guest radix tree will keep pointing
2994 * to the wrong pages, causing use-after-free and memory corruption.
2995 * If the allocation of the new top level page table fails, the ASCE is not
2996 * replaced.
2997 * In any case, the old ASCE is always removed from the gmap CRST list.
2998 * Therefore the caller has to make sure to save a pointer to it
2999 * beforehand, unless a leak is actually intended.
3000 */
3001int s390_replace_asce(struct gmap *gmap)
3002{
3003 unsigned long asce;
3004 struct page *page;
3005 void *table;
3006
3007 s390_unlist_old_asce(gmap);
3008
3009 /* Replacing segment type ASCEs would cause serious issues */
3010 if ((gmap->asce & _ASCE_TYPE_MASK) == _ASCE_TYPE_SEGMENT)
3011 return -EINVAL;
3012
3013 page = gmap_alloc_crst();
3014 if (!page)
3015 return -ENOMEM;
3016 page->index = 0;
3017 table = page_to_virt(page);
3018 memcpy(table, gmap->table, 1UL << (CRST_ALLOC_ORDER + PAGE_SHIFT));
3019
3020 /*
3021 * The caller has to deal with the old ASCE, but here we make sure
3022 * the new one is properly added to the CRST list, so that
3023 * it will be freed when the VM is torn down.
3024 */
3025 spin_lock(&gmap->guest_table_lock);
3026 list_add(&page->lru, &gmap->crst_list);
3027 spin_unlock(&gmap->guest_table_lock);
3028
3029 /* Set new table origin while preserving existing ASCE control bits */
3030 asce = (gmap->asce & ~_ASCE_ORIGIN) | __pa(table);
3031 WRITE_ONCE(gmap->asce, asce);
3032 WRITE_ONCE(gmap->mm->context.gmap_asce, asce);
3033 WRITE_ONCE(gmap->table, table);
3034
3035 return 0;
3036}
3037EXPORT_SYMBOL_GPL(s390_replace_asce);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * KVM guest address space mapping code
4 *
5 * Copyright IBM Corp. 2007, 2016, 2018
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * David Hildenbrand <david@redhat.com>
8 * Janosch Frank <frankja@linux.vnet.ibm.com>
9 */
10
11#include <linux/kernel.h>
12#include <linux/pagewalk.h>
13#include <linux/swap.h>
14#include <linux/smp.h>
15#include <linux/spinlock.h>
16#include <linux/slab.h>
17#include <linux/swapops.h>
18#include <linux/ksm.h>
19#include <linux/mman.h>
20#include <linux/pgtable.h>
21
22#include <asm/pgalloc.h>
23#include <asm/gmap.h>
24#include <asm/tlb.h>
25
26#define GMAP_SHADOW_FAKE_TABLE 1ULL
27
28/**
29 * gmap_alloc - allocate and initialize a guest address space
30 * @mm: pointer to the parent mm_struct
31 * @limit: maximum address of the gmap address space
32 *
33 * Returns a guest address space structure.
34 */
35static struct gmap *gmap_alloc(unsigned long limit)
36{
37 struct gmap *gmap;
38 struct page *page;
39 unsigned long *table;
40 unsigned long etype, atype;
41
42 if (limit < _REGION3_SIZE) {
43 limit = _REGION3_SIZE - 1;
44 atype = _ASCE_TYPE_SEGMENT;
45 etype = _SEGMENT_ENTRY_EMPTY;
46 } else if (limit < _REGION2_SIZE) {
47 limit = _REGION2_SIZE - 1;
48 atype = _ASCE_TYPE_REGION3;
49 etype = _REGION3_ENTRY_EMPTY;
50 } else if (limit < _REGION1_SIZE) {
51 limit = _REGION1_SIZE - 1;
52 atype = _ASCE_TYPE_REGION2;
53 etype = _REGION2_ENTRY_EMPTY;
54 } else {
55 limit = -1UL;
56 atype = _ASCE_TYPE_REGION1;
57 etype = _REGION1_ENTRY_EMPTY;
58 }
59 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
60 if (!gmap)
61 goto out;
62 INIT_LIST_HEAD(&gmap->crst_list);
63 INIT_LIST_HEAD(&gmap->children);
64 INIT_LIST_HEAD(&gmap->pt_list);
65 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
66 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
67 INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC);
68 spin_lock_init(&gmap->guest_table_lock);
69 spin_lock_init(&gmap->shadow_lock);
70 refcount_set(&gmap->ref_count, 1);
71 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
72 if (!page)
73 goto out_free;
74 page->index = 0;
75 list_add(&page->lru, &gmap->crst_list);
76 table = (unsigned long *) page_to_phys(page);
77 crst_table_init(table, etype);
78 gmap->table = table;
79 gmap->asce = atype | _ASCE_TABLE_LENGTH |
80 _ASCE_USER_BITS | __pa(table);
81 gmap->asce_end = limit;
82 return gmap;
83
84out_free:
85 kfree(gmap);
86out:
87 return NULL;
88}
89
90/**
91 * gmap_create - create a guest address space
92 * @mm: pointer to the parent mm_struct
93 * @limit: maximum size of the gmap address space
94 *
95 * Returns a guest address space structure.
96 */
97struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
98{
99 struct gmap *gmap;
100 unsigned long gmap_asce;
101
102 gmap = gmap_alloc(limit);
103 if (!gmap)
104 return NULL;
105 gmap->mm = mm;
106 spin_lock(&mm->context.lock);
107 list_add_rcu(&gmap->list, &mm->context.gmap_list);
108 if (list_is_singular(&mm->context.gmap_list))
109 gmap_asce = gmap->asce;
110 else
111 gmap_asce = -1UL;
112 WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
113 spin_unlock(&mm->context.lock);
114 return gmap;
115}
116EXPORT_SYMBOL_GPL(gmap_create);
117
118static void gmap_flush_tlb(struct gmap *gmap)
119{
120 if (MACHINE_HAS_IDTE)
121 __tlb_flush_idte(gmap->asce);
122 else
123 __tlb_flush_global();
124}
125
126static void gmap_radix_tree_free(struct radix_tree_root *root)
127{
128 struct radix_tree_iter iter;
129 unsigned long indices[16];
130 unsigned long index;
131 void __rcu **slot;
132 int i, nr;
133
134 /* A radix tree is freed by deleting all of its entries */
135 index = 0;
136 do {
137 nr = 0;
138 radix_tree_for_each_slot(slot, root, &iter, index) {
139 indices[nr] = iter.index;
140 if (++nr == 16)
141 break;
142 }
143 for (i = 0; i < nr; i++) {
144 index = indices[i];
145 radix_tree_delete(root, index);
146 }
147 } while (nr > 0);
148}
149
150static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
151{
152 struct gmap_rmap *rmap, *rnext, *head;
153 struct radix_tree_iter iter;
154 unsigned long indices[16];
155 unsigned long index;
156 void __rcu **slot;
157 int i, nr;
158
159 /* A radix tree is freed by deleting all of its entries */
160 index = 0;
161 do {
162 nr = 0;
163 radix_tree_for_each_slot(slot, root, &iter, index) {
164 indices[nr] = iter.index;
165 if (++nr == 16)
166 break;
167 }
168 for (i = 0; i < nr; i++) {
169 index = indices[i];
170 head = radix_tree_delete(root, index);
171 gmap_for_each_rmap_safe(rmap, rnext, head)
172 kfree(rmap);
173 }
174 } while (nr > 0);
175}
176
177/**
178 * gmap_free - free a guest address space
179 * @gmap: pointer to the guest address space structure
180 *
181 * No locks required. There are no references to this gmap anymore.
182 */
183static void gmap_free(struct gmap *gmap)
184{
185 struct page *page, *next;
186
187 /* Flush tlb of all gmaps (if not already done for shadows) */
188 if (!(gmap_is_shadow(gmap) && gmap->removed))
189 gmap_flush_tlb(gmap);
190 /* Free all segment & region tables. */
191 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
192 __free_pages(page, CRST_ALLOC_ORDER);
193 gmap_radix_tree_free(&gmap->guest_to_host);
194 gmap_radix_tree_free(&gmap->host_to_guest);
195
196 /* Free additional data for a shadow gmap */
197 if (gmap_is_shadow(gmap)) {
198 /* Free all page tables. */
199 list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
200 page_table_free_pgste(page);
201 gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
202 /* Release reference to the parent */
203 gmap_put(gmap->parent);
204 }
205
206 kfree(gmap);
207}
208
209/**
210 * gmap_get - increase reference counter for guest address space
211 * @gmap: pointer to the guest address space structure
212 *
213 * Returns the gmap pointer
214 */
215struct gmap *gmap_get(struct gmap *gmap)
216{
217 refcount_inc(&gmap->ref_count);
218 return gmap;
219}
220EXPORT_SYMBOL_GPL(gmap_get);
221
222/**
223 * gmap_put - decrease reference counter for guest address space
224 * @gmap: pointer to the guest address space structure
225 *
226 * If the reference counter reaches zero the guest address space is freed.
227 */
228void gmap_put(struct gmap *gmap)
229{
230 if (refcount_dec_and_test(&gmap->ref_count))
231 gmap_free(gmap);
232}
233EXPORT_SYMBOL_GPL(gmap_put);
234
235/**
236 * gmap_remove - remove a guest address space but do not free it yet
237 * @gmap: pointer to the guest address space structure
238 */
239void gmap_remove(struct gmap *gmap)
240{
241 struct gmap *sg, *next;
242 unsigned long gmap_asce;
243
244 /* Remove all shadow gmaps linked to this gmap */
245 if (!list_empty(&gmap->children)) {
246 spin_lock(&gmap->shadow_lock);
247 list_for_each_entry_safe(sg, next, &gmap->children, list) {
248 list_del(&sg->list);
249 gmap_put(sg);
250 }
251 spin_unlock(&gmap->shadow_lock);
252 }
253 /* Remove gmap from the pre-mm list */
254 spin_lock(&gmap->mm->context.lock);
255 list_del_rcu(&gmap->list);
256 if (list_empty(&gmap->mm->context.gmap_list))
257 gmap_asce = 0;
258 else if (list_is_singular(&gmap->mm->context.gmap_list))
259 gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
260 struct gmap, list)->asce;
261 else
262 gmap_asce = -1UL;
263 WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
264 spin_unlock(&gmap->mm->context.lock);
265 synchronize_rcu();
266 /* Put reference */
267 gmap_put(gmap);
268}
269EXPORT_SYMBOL_GPL(gmap_remove);
270
271/**
272 * gmap_enable - switch primary space to the guest address space
273 * @gmap: pointer to the guest address space structure
274 */
275void gmap_enable(struct gmap *gmap)
276{
277 S390_lowcore.gmap = (unsigned long) gmap;
278}
279EXPORT_SYMBOL_GPL(gmap_enable);
280
281/**
282 * gmap_disable - switch back to the standard primary address space
283 * @gmap: pointer to the guest address space structure
284 */
285void gmap_disable(struct gmap *gmap)
286{
287 S390_lowcore.gmap = 0UL;
288}
289EXPORT_SYMBOL_GPL(gmap_disable);
290
291/**
292 * gmap_get_enabled - get a pointer to the currently enabled gmap
293 *
294 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
295 */
296struct gmap *gmap_get_enabled(void)
297{
298 return (struct gmap *) S390_lowcore.gmap;
299}
300EXPORT_SYMBOL_GPL(gmap_get_enabled);
301
302/*
303 * gmap_alloc_table is assumed to be called with mmap_lock held
304 */
305static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
306 unsigned long init, unsigned long gaddr)
307{
308 struct page *page;
309 unsigned long *new;
310
311 /* since we dont free the gmap table until gmap_free we can unlock */
312 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
313 if (!page)
314 return -ENOMEM;
315 new = (unsigned long *) page_to_phys(page);
316 crst_table_init(new, init);
317 spin_lock(&gmap->guest_table_lock);
318 if (*table & _REGION_ENTRY_INVALID) {
319 list_add(&page->lru, &gmap->crst_list);
320 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
321 (*table & _REGION_ENTRY_TYPE_MASK);
322 page->index = gaddr;
323 page = NULL;
324 }
325 spin_unlock(&gmap->guest_table_lock);
326 if (page)
327 __free_pages(page, CRST_ALLOC_ORDER);
328 return 0;
329}
330
331/**
332 * __gmap_segment_gaddr - find virtual address from segment pointer
333 * @entry: pointer to a segment table entry in the guest address space
334 *
335 * Returns the virtual address in the guest address space for the segment
336 */
337static unsigned long __gmap_segment_gaddr(unsigned long *entry)
338{
339 struct page *page;
340 unsigned long offset, mask;
341
342 offset = (unsigned long) entry / sizeof(unsigned long);
343 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
344 mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
345 page = virt_to_page((void *)((unsigned long) entry & mask));
346 return page->index + offset;
347}
348
349/**
350 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
351 * @gmap: pointer to the guest address space structure
352 * @vmaddr: address in the host process address space
353 *
354 * Returns 1 if a TLB flush is required
355 */
356static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
357{
358 unsigned long *entry;
359 int flush = 0;
360
361 BUG_ON(gmap_is_shadow(gmap));
362 spin_lock(&gmap->guest_table_lock);
363 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
364 if (entry) {
365 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
366 *entry = _SEGMENT_ENTRY_EMPTY;
367 }
368 spin_unlock(&gmap->guest_table_lock);
369 return flush;
370}
371
372/**
373 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
374 * @gmap: pointer to the guest address space structure
375 * @gaddr: address in the guest address space
376 *
377 * Returns 1 if a TLB flush is required
378 */
379static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
380{
381 unsigned long vmaddr;
382
383 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
384 gaddr >> PMD_SHIFT);
385 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
386}
387
388/**
389 * gmap_unmap_segment - unmap segment from the guest address space
390 * @gmap: pointer to the guest address space structure
391 * @to: address in the guest address space
392 * @len: length of the memory area to unmap
393 *
394 * Returns 0 if the unmap succeeded, -EINVAL if not.
395 */
396int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
397{
398 unsigned long off;
399 int flush;
400
401 BUG_ON(gmap_is_shadow(gmap));
402 if ((to | len) & (PMD_SIZE - 1))
403 return -EINVAL;
404 if (len == 0 || to + len < to)
405 return -EINVAL;
406
407 flush = 0;
408 mmap_write_lock(gmap->mm);
409 for (off = 0; off < len; off += PMD_SIZE)
410 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
411 mmap_write_unlock(gmap->mm);
412 if (flush)
413 gmap_flush_tlb(gmap);
414 return 0;
415}
416EXPORT_SYMBOL_GPL(gmap_unmap_segment);
417
418/**
419 * gmap_map_segment - map a segment to the guest address space
420 * @gmap: pointer to the guest address space structure
421 * @from: source address in the parent address space
422 * @to: target address in the guest address space
423 * @len: length of the memory area to map
424 *
425 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
426 */
427int gmap_map_segment(struct gmap *gmap, unsigned long from,
428 unsigned long to, unsigned long len)
429{
430 unsigned long off;
431 int flush;
432
433 BUG_ON(gmap_is_shadow(gmap));
434 if ((from | to | len) & (PMD_SIZE - 1))
435 return -EINVAL;
436 if (len == 0 || from + len < from || to + len < to ||
437 from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
438 return -EINVAL;
439
440 flush = 0;
441 mmap_write_lock(gmap->mm);
442 for (off = 0; off < len; off += PMD_SIZE) {
443 /* Remove old translation */
444 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
445 /* Store new translation */
446 if (radix_tree_insert(&gmap->guest_to_host,
447 (to + off) >> PMD_SHIFT,
448 (void *) from + off))
449 break;
450 }
451 mmap_write_unlock(gmap->mm);
452 if (flush)
453 gmap_flush_tlb(gmap);
454 if (off >= len)
455 return 0;
456 gmap_unmap_segment(gmap, to, len);
457 return -ENOMEM;
458}
459EXPORT_SYMBOL_GPL(gmap_map_segment);
460
461/**
462 * __gmap_translate - translate a guest address to a user space address
463 * @gmap: pointer to guest mapping meta data structure
464 * @gaddr: guest address
465 *
466 * Returns user space address which corresponds to the guest address or
467 * -EFAULT if no such mapping exists.
468 * This function does not establish potentially missing page table entries.
469 * The mmap_lock of the mm that belongs to the address space must be held
470 * when this function gets called.
471 *
472 * Note: Can also be called for shadow gmaps.
473 */
474unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
475{
476 unsigned long vmaddr;
477
478 vmaddr = (unsigned long)
479 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
480 /* Note: guest_to_host is empty for a shadow gmap */
481 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
482}
483EXPORT_SYMBOL_GPL(__gmap_translate);
484
485/**
486 * gmap_translate - translate a guest address to a user space address
487 * @gmap: pointer to guest mapping meta data structure
488 * @gaddr: guest address
489 *
490 * Returns user space address which corresponds to the guest address or
491 * -EFAULT if no such mapping exists.
492 * This function does not establish potentially missing page table entries.
493 */
494unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
495{
496 unsigned long rc;
497
498 mmap_read_lock(gmap->mm);
499 rc = __gmap_translate(gmap, gaddr);
500 mmap_read_unlock(gmap->mm);
501 return rc;
502}
503EXPORT_SYMBOL_GPL(gmap_translate);
504
505/**
506 * gmap_unlink - disconnect a page table from the gmap shadow tables
507 * @gmap: pointer to guest mapping meta data structure
508 * @table: pointer to the host page table
509 * @vmaddr: vm address associated with the host page table
510 */
511void gmap_unlink(struct mm_struct *mm, unsigned long *table,
512 unsigned long vmaddr)
513{
514 struct gmap *gmap;
515 int flush;
516
517 rcu_read_lock();
518 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
519 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
520 if (flush)
521 gmap_flush_tlb(gmap);
522 }
523 rcu_read_unlock();
524}
525
526static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
527 unsigned long gaddr);
528
529/**
530 * gmap_link - set up shadow page tables to connect a host to a guest address
531 * @gmap: pointer to guest mapping meta data structure
532 * @gaddr: guest address
533 * @vmaddr: vm address
534 *
535 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
536 * if the vm address is already mapped to a different guest segment.
537 * The mmap_lock of the mm that belongs to the address space must be held
538 * when this function gets called.
539 */
540int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
541{
542 struct mm_struct *mm;
543 unsigned long *table;
544 spinlock_t *ptl;
545 pgd_t *pgd;
546 p4d_t *p4d;
547 pud_t *pud;
548 pmd_t *pmd;
549 u64 unprot;
550 int rc;
551
552 BUG_ON(gmap_is_shadow(gmap));
553 /* Create higher level tables in the gmap page table */
554 table = gmap->table;
555 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
556 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
557 if ((*table & _REGION_ENTRY_INVALID) &&
558 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
559 gaddr & _REGION1_MASK))
560 return -ENOMEM;
561 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
562 }
563 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
564 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
565 if ((*table & _REGION_ENTRY_INVALID) &&
566 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
567 gaddr & _REGION2_MASK))
568 return -ENOMEM;
569 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
570 }
571 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
572 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
573 if ((*table & _REGION_ENTRY_INVALID) &&
574 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
575 gaddr & _REGION3_MASK))
576 return -ENOMEM;
577 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
578 }
579 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
580 /* Walk the parent mm page table */
581 mm = gmap->mm;
582 pgd = pgd_offset(mm, vmaddr);
583 VM_BUG_ON(pgd_none(*pgd));
584 p4d = p4d_offset(pgd, vmaddr);
585 VM_BUG_ON(p4d_none(*p4d));
586 pud = pud_offset(p4d, vmaddr);
587 VM_BUG_ON(pud_none(*pud));
588 /* large puds cannot yet be handled */
589 if (pud_large(*pud))
590 return -EFAULT;
591 pmd = pmd_offset(pud, vmaddr);
592 VM_BUG_ON(pmd_none(*pmd));
593 /* Are we allowed to use huge pages? */
594 if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
595 return -EFAULT;
596 /* Link gmap segment table entry location to page table. */
597 rc = radix_tree_preload(GFP_KERNEL);
598 if (rc)
599 return rc;
600 ptl = pmd_lock(mm, pmd);
601 spin_lock(&gmap->guest_table_lock);
602 if (*table == _SEGMENT_ENTRY_EMPTY) {
603 rc = radix_tree_insert(&gmap->host_to_guest,
604 vmaddr >> PMD_SHIFT, table);
605 if (!rc) {
606 if (pmd_large(*pmd)) {
607 *table = (pmd_val(*pmd) &
608 _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
609 | _SEGMENT_ENTRY_GMAP_UC;
610 } else
611 *table = pmd_val(*pmd) &
612 _SEGMENT_ENTRY_HARDWARE_BITS;
613 }
614 } else if (*table & _SEGMENT_ENTRY_PROTECT &&
615 !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
616 unprot = (u64)*table;
617 unprot &= ~_SEGMENT_ENTRY_PROTECT;
618 unprot |= _SEGMENT_ENTRY_GMAP_UC;
619 gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
620 }
621 spin_unlock(&gmap->guest_table_lock);
622 spin_unlock(ptl);
623 radix_tree_preload_end();
624 return rc;
625}
626
627/**
628 * gmap_fault - resolve a fault on a guest address
629 * @gmap: pointer to guest mapping meta data structure
630 * @gaddr: guest address
631 * @fault_flags: flags to pass down to handle_mm_fault()
632 *
633 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
634 * if the vm address is already mapped to a different guest segment.
635 */
636int gmap_fault(struct gmap *gmap, unsigned long gaddr,
637 unsigned int fault_flags)
638{
639 unsigned long vmaddr;
640 int rc;
641 bool unlocked;
642
643 mmap_read_lock(gmap->mm);
644
645retry:
646 unlocked = false;
647 vmaddr = __gmap_translate(gmap, gaddr);
648 if (IS_ERR_VALUE(vmaddr)) {
649 rc = vmaddr;
650 goto out_up;
651 }
652 if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
653 &unlocked)) {
654 rc = -EFAULT;
655 goto out_up;
656 }
657 /*
658 * In the case that fixup_user_fault unlocked the mmap_lock during
659 * faultin redo __gmap_translate to not race with a map/unmap_segment.
660 */
661 if (unlocked)
662 goto retry;
663
664 rc = __gmap_link(gmap, gaddr, vmaddr);
665out_up:
666 mmap_read_unlock(gmap->mm);
667 return rc;
668}
669EXPORT_SYMBOL_GPL(gmap_fault);
670
671/*
672 * this function is assumed to be called with mmap_lock held
673 */
674void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
675{
676 unsigned long vmaddr;
677 spinlock_t *ptl;
678 pte_t *ptep;
679
680 /* Find the vm address for the guest address */
681 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
682 gaddr >> PMD_SHIFT);
683 if (vmaddr) {
684 vmaddr |= gaddr & ~PMD_MASK;
685 /* Get pointer to the page table entry */
686 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
687 if (likely(ptep))
688 ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
689 pte_unmap_unlock(ptep, ptl);
690 }
691}
692EXPORT_SYMBOL_GPL(__gmap_zap);
693
694void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
695{
696 unsigned long gaddr, vmaddr, size;
697 struct vm_area_struct *vma;
698
699 mmap_read_lock(gmap->mm);
700 for (gaddr = from; gaddr < to;
701 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
702 /* Find the vm address for the guest address */
703 vmaddr = (unsigned long)
704 radix_tree_lookup(&gmap->guest_to_host,
705 gaddr >> PMD_SHIFT);
706 if (!vmaddr)
707 continue;
708 vmaddr |= gaddr & ~PMD_MASK;
709 /* Find vma in the parent mm */
710 vma = find_vma(gmap->mm, vmaddr);
711 if (!vma)
712 continue;
713 /*
714 * We do not discard pages that are backed by
715 * hugetlbfs, so we don't have to refault them.
716 */
717 if (is_vm_hugetlb_page(vma))
718 continue;
719 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
720 zap_page_range(vma, vmaddr, size);
721 }
722 mmap_read_unlock(gmap->mm);
723}
724EXPORT_SYMBOL_GPL(gmap_discard);
725
726static LIST_HEAD(gmap_notifier_list);
727static DEFINE_SPINLOCK(gmap_notifier_lock);
728
729/**
730 * gmap_register_pte_notifier - register a pte invalidation callback
731 * @nb: pointer to the gmap notifier block
732 */
733void gmap_register_pte_notifier(struct gmap_notifier *nb)
734{
735 spin_lock(&gmap_notifier_lock);
736 list_add_rcu(&nb->list, &gmap_notifier_list);
737 spin_unlock(&gmap_notifier_lock);
738}
739EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
740
741/**
742 * gmap_unregister_pte_notifier - remove a pte invalidation callback
743 * @nb: pointer to the gmap notifier block
744 */
745void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
746{
747 spin_lock(&gmap_notifier_lock);
748 list_del_rcu(&nb->list);
749 spin_unlock(&gmap_notifier_lock);
750 synchronize_rcu();
751}
752EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
753
754/**
755 * gmap_call_notifier - call all registered invalidation callbacks
756 * @gmap: pointer to guest mapping meta data structure
757 * @start: start virtual address in the guest address space
758 * @end: end virtual address in the guest address space
759 */
760static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
761 unsigned long end)
762{
763 struct gmap_notifier *nb;
764
765 list_for_each_entry(nb, &gmap_notifier_list, list)
766 nb->notifier_call(gmap, start, end);
767}
768
769/**
770 * gmap_table_walk - walk the gmap page tables
771 * @gmap: pointer to guest mapping meta data structure
772 * @gaddr: virtual address in the guest address space
773 * @level: page table level to stop at
774 *
775 * Returns a table entry pointer for the given guest address and @level
776 * @level=0 : returns a pointer to a page table table entry (or NULL)
777 * @level=1 : returns a pointer to a segment table entry (or NULL)
778 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
779 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
780 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
781 *
782 * Returns NULL if the gmap page tables could not be walked to the
783 * requested level.
784 *
785 * Note: Can also be called for shadow gmaps.
786 */
787static inline unsigned long *gmap_table_walk(struct gmap *gmap,
788 unsigned long gaddr, int level)
789{
790 const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
791 unsigned long *table = gmap->table;
792
793 if (gmap_is_shadow(gmap) && gmap->removed)
794 return NULL;
795
796 if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
797 return NULL;
798
799 if (asce_type != _ASCE_TYPE_REGION1 &&
800 gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
801 return NULL;
802
803 switch (asce_type) {
804 case _ASCE_TYPE_REGION1:
805 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
806 if (level == 4)
807 break;
808 if (*table & _REGION_ENTRY_INVALID)
809 return NULL;
810 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
811 fallthrough;
812 case _ASCE_TYPE_REGION2:
813 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
814 if (level == 3)
815 break;
816 if (*table & _REGION_ENTRY_INVALID)
817 return NULL;
818 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
819 fallthrough;
820 case _ASCE_TYPE_REGION3:
821 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
822 if (level == 2)
823 break;
824 if (*table & _REGION_ENTRY_INVALID)
825 return NULL;
826 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
827 fallthrough;
828 case _ASCE_TYPE_SEGMENT:
829 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
830 if (level == 1)
831 break;
832 if (*table & _REGION_ENTRY_INVALID)
833 return NULL;
834 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
835 table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
836 }
837 return table;
838}
839
840/**
841 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
842 * and return the pte pointer
843 * @gmap: pointer to guest mapping meta data structure
844 * @gaddr: virtual address in the guest address space
845 * @ptl: pointer to the spinlock pointer
846 *
847 * Returns a pointer to the locked pte for a guest address, or NULL
848 */
849static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
850 spinlock_t **ptl)
851{
852 unsigned long *table;
853
854 BUG_ON(gmap_is_shadow(gmap));
855 /* Walk the gmap page table, lock and get pte pointer */
856 table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
857 if (!table || *table & _SEGMENT_ENTRY_INVALID)
858 return NULL;
859 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
860}
861
862/**
863 * gmap_pte_op_fixup - force a page in and connect the gmap page table
864 * @gmap: pointer to guest mapping meta data structure
865 * @gaddr: virtual address in the guest address space
866 * @vmaddr: address in the host process address space
867 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
868 *
869 * Returns 0 if the caller can retry __gmap_translate (might fail again),
870 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
871 * up or connecting the gmap page table.
872 */
873static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
874 unsigned long vmaddr, int prot)
875{
876 struct mm_struct *mm = gmap->mm;
877 unsigned int fault_flags;
878 bool unlocked = false;
879
880 BUG_ON(gmap_is_shadow(gmap));
881 fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
882 if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
883 return -EFAULT;
884 if (unlocked)
885 /* lost mmap_lock, caller has to retry __gmap_translate */
886 return 0;
887 /* Connect the page tables */
888 return __gmap_link(gmap, gaddr, vmaddr);
889}
890
891/**
892 * gmap_pte_op_end - release the page table lock
893 * @ptl: pointer to the spinlock pointer
894 */
895static void gmap_pte_op_end(spinlock_t *ptl)
896{
897 if (ptl)
898 spin_unlock(ptl);
899}
900
901/**
902 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
903 * and return the pmd pointer
904 * @gmap: pointer to guest mapping meta data structure
905 * @gaddr: virtual address in the guest address space
906 *
907 * Returns a pointer to the pmd for a guest address, or NULL
908 */
909static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
910{
911 pmd_t *pmdp;
912
913 BUG_ON(gmap_is_shadow(gmap));
914 pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
915 if (!pmdp)
916 return NULL;
917
918 /* without huge pages, there is no need to take the table lock */
919 if (!gmap->mm->context.allow_gmap_hpage_1m)
920 return pmd_none(*pmdp) ? NULL : pmdp;
921
922 spin_lock(&gmap->guest_table_lock);
923 if (pmd_none(*pmdp)) {
924 spin_unlock(&gmap->guest_table_lock);
925 return NULL;
926 }
927
928 /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
929 if (!pmd_large(*pmdp))
930 spin_unlock(&gmap->guest_table_lock);
931 return pmdp;
932}
933
934/**
935 * gmap_pmd_op_end - release the guest_table_lock if needed
936 * @gmap: pointer to the guest mapping meta data structure
937 * @pmdp: pointer to the pmd
938 */
939static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
940{
941 if (pmd_large(*pmdp))
942 spin_unlock(&gmap->guest_table_lock);
943}
944
945/*
946 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
947 * @pmdp: pointer to the pmd to be protected
948 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
949 * @bits: notification bits to set
950 *
951 * Returns:
952 * 0 if successfully protected
953 * -EAGAIN if a fixup is needed
954 * -EINVAL if unsupported notifier bits have been specified
955 *
956 * Expected to be called with sg->mm->mmap_lock in read and
957 * guest_table_lock held.
958 */
959static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
960 pmd_t *pmdp, int prot, unsigned long bits)
961{
962 int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
963 int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
964 pmd_t new = *pmdp;
965
966 /* Fixup needed */
967 if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
968 return -EAGAIN;
969
970 if (prot == PROT_NONE && !pmd_i) {
971 pmd_val(new) |= _SEGMENT_ENTRY_INVALID;
972 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
973 }
974
975 if (prot == PROT_READ && !pmd_p) {
976 pmd_val(new) &= ~_SEGMENT_ENTRY_INVALID;
977 pmd_val(new) |= _SEGMENT_ENTRY_PROTECT;
978 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
979 }
980
981 if (bits & GMAP_NOTIFY_MPROT)
982 pmd_val(*pmdp) |= _SEGMENT_ENTRY_GMAP_IN;
983
984 /* Shadow GMAP protection needs split PMDs */
985 if (bits & GMAP_NOTIFY_SHADOW)
986 return -EINVAL;
987
988 return 0;
989}
990
991/*
992 * gmap_protect_pte - remove access rights to memory and set pgste bits
993 * @gmap: pointer to guest mapping meta data structure
994 * @gaddr: virtual address in the guest address space
995 * @pmdp: pointer to the pmd associated with the pte
996 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
997 * @bits: notification bits to set
998 *
999 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1000 * -EAGAIN if a fixup is needed.
1001 *
1002 * Expected to be called with sg->mm->mmap_lock in read
1003 */
1004static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
1005 pmd_t *pmdp, int prot, unsigned long bits)
1006{
1007 int rc;
1008 pte_t *ptep;
1009 spinlock_t *ptl = NULL;
1010 unsigned long pbits = 0;
1011
1012 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1013 return -EAGAIN;
1014
1015 ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1016 if (!ptep)
1017 return -ENOMEM;
1018
1019 pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1020 pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
1021 /* Protect and unlock. */
1022 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
1023 gmap_pte_op_end(ptl);
1024 return rc;
1025}
1026
1027/*
1028 * gmap_protect_range - remove access rights to memory and set pgste bits
1029 * @gmap: pointer to guest mapping meta data structure
1030 * @gaddr: virtual address in the guest address space
1031 * @len: size of area
1032 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1033 * @bits: pgste notification bits to set
1034 *
1035 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1036 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1037 *
1038 * Called with sg->mm->mmap_lock in read.
1039 */
1040static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1041 unsigned long len, int prot, unsigned long bits)
1042{
1043 unsigned long vmaddr, dist;
1044 pmd_t *pmdp;
1045 int rc;
1046
1047 BUG_ON(gmap_is_shadow(gmap));
1048 while (len) {
1049 rc = -EAGAIN;
1050 pmdp = gmap_pmd_op_walk(gmap, gaddr);
1051 if (pmdp) {
1052 if (!pmd_large(*pmdp)) {
1053 rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1054 bits);
1055 if (!rc) {
1056 len -= PAGE_SIZE;
1057 gaddr += PAGE_SIZE;
1058 }
1059 } else {
1060 rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1061 bits);
1062 if (!rc) {
1063 dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1064 len = len < dist ? 0 : len - dist;
1065 gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1066 }
1067 }
1068 gmap_pmd_op_end(gmap, pmdp);
1069 }
1070 if (rc) {
1071 if (rc == -EINVAL)
1072 return rc;
1073
1074 /* -EAGAIN, fixup of userspace mm and gmap */
1075 vmaddr = __gmap_translate(gmap, gaddr);
1076 if (IS_ERR_VALUE(vmaddr))
1077 return vmaddr;
1078 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
1079 if (rc)
1080 return rc;
1081 }
1082 }
1083 return 0;
1084}
1085
1086/**
1087 * gmap_mprotect_notify - change access rights for a range of ptes and
1088 * call the notifier if any pte changes again
1089 * @gmap: pointer to guest mapping meta data structure
1090 * @gaddr: virtual address in the guest address space
1091 * @len: size of area
1092 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1093 *
1094 * Returns 0 if for each page in the given range a gmap mapping exists,
1095 * the new access rights could be set and the notifier could be armed.
1096 * If the gmap mapping is missing for one or more pages -EFAULT is
1097 * returned. If no memory could be allocated -ENOMEM is returned.
1098 * This function establishes missing page table entries.
1099 */
1100int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1101 unsigned long len, int prot)
1102{
1103 int rc;
1104
1105 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1106 return -EINVAL;
1107 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1108 return -EINVAL;
1109 mmap_read_lock(gmap->mm);
1110 rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
1111 mmap_read_unlock(gmap->mm);
1112 return rc;
1113}
1114EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
1115
1116/**
1117 * gmap_read_table - get an unsigned long value from a guest page table using
1118 * absolute addressing, without marking the page referenced.
1119 * @gmap: pointer to guest mapping meta data structure
1120 * @gaddr: virtual address in the guest address space
1121 * @val: pointer to the unsigned long value to return
1122 *
1123 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
1124 * if reading using the virtual address failed. -EINVAL if called on a gmap
1125 * shadow.
1126 *
1127 * Called with gmap->mm->mmap_lock in read.
1128 */
1129int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1130{
1131 unsigned long address, vmaddr;
1132 spinlock_t *ptl;
1133 pte_t *ptep, pte;
1134 int rc;
1135
1136 if (gmap_is_shadow(gmap))
1137 return -EINVAL;
1138
1139 while (1) {
1140 rc = -EAGAIN;
1141 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1142 if (ptep) {
1143 pte = *ptep;
1144 if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1145 address = pte_val(pte) & PAGE_MASK;
1146 address += gaddr & ~PAGE_MASK;
1147 *val = *(unsigned long *) address;
1148 pte_val(*ptep) |= _PAGE_YOUNG;
1149 /* Do *NOT* clear the _PAGE_INVALID bit! */
1150 rc = 0;
1151 }
1152 gmap_pte_op_end(ptl);
1153 }
1154 if (!rc)
1155 break;
1156 vmaddr = __gmap_translate(gmap, gaddr);
1157 if (IS_ERR_VALUE(vmaddr)) {
1158 rc = vmaddr;
1159 break;
1160 }
1161 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1162 if (rc)
1163 break;
1164 }
1165 return rc;
1166}
1167EXPORT_SYMBOL_GPL(gmap_read_table);
1168
1169/**
1170 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1171 * @sg: pointer to the shadow guest address space structure
1172 * @vmaddr: vm address associated with the rmap
1173 * @rmap: pointer to the rmap structure
1174 *
1175 * Called with the sg->guest_table_lock
1176 */
1177static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1178 struct gmap_rmap *rmap)
1179{
1180 void __rcu **slot;
1181
1182 BUG_ON(!gmap_is_shadow(sg));
1183 slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1184 if (slot) {
1185 rmap->next = radix_tree_deref_slot_protected(slot,
1186 &sg->guest_table_lock);
1187 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1188 } else {
1189 rmap->next = NULL;
1190 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1191 rmap);
1192 }
1193}
1194
1195/**
1196 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
1197 * @sg: pointer to the shadow guest address space structure
1198 * @raddr: rmap address in the shadow gmap
1199 * @paddr: address in the parent guest address space
1200 * @len: length of the memory area to protect
1201 *
1202 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1203 * if out of memory and -EFAULT if paddr is invalid.
1204 */
1205static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1206 unsigned long paddr, unsigned long len)
1207{
1208 struct gmap *parent;
1209 struct gmap_rmap *rmap;
1210 unsigned long vmaddr;
1211 spinlock_t *ptl;
1212 pte_t *ptep;
1213 int rc;
1214
1215 BUG_ON(!gmap_is_shadow(sg));
1216 parent = sg->parent;
1217 while (len) {
1218 vmaddr = __gmap_translate(parent, paddr);
1219 if (IS_ERR_VALUE(vmaddr))
1220 return vmaddr;
1221 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
1222 if (!rmap)
1223 return -ENOMEM;
1224 rmap->raddr = raddr;
1225 rc = radix_tree_preload(GFP_KERNEL);
1226 if (rc) {
1227 kfree(rmap);
1228 return rc;
1229 }
1230 rc = -EAGAIN;
1231 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1232 if (ptep) {
1233 spin_lock(&sg->guest_table_lock);
1234 rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
1235 PGSTE_VSIE_BIT);
1236 if (!rc)
1237 gmap_insert_rmap(sg, vmaddr, rmap);
1238 spin_unlock(&sg->guest_table_lock);
1239 gmap_pte_op_end(ptl);
1240 }
1241 radix_tree_preload_end();
1242 if (rc) {
1243 kfree(rmap);
1244 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
1245 if (rc)
1246 return rc;
1247 continue;
1248 }
1249 paddr += PAGE_SIZE;
1250 len -= PAGE_SIZE;
1251 }
1252 return 0;
1253}
1254
1255#define _SHADOW_RMAP_MASK 0x7
1256#define _SHADOW_RMAP_REGION1 0x5
1257#define _SHADOW_RMAP_REGION2 0x4
1258#define _SHADOW_RMAP_REGION3 0x3
1259#define _SHADOW_RMAP_SEGMENT 0x2
1260#define _SHADOW_RMAP_PGTABLE 0x1
1261
1262/**
1263 * gmap_idte_one - invalidate a single region or segment table entry
1264 * @asce: region or segment table *origin* + table-type bits
1265 * @vaddr: virtual address to identify the table entry to flush
1266 *
1267 * The invalid bit of a single region or segment table entry is set
1268 * and the associated TLB entries depending on the entry are flushed.
1269 * The table-type of the @asce identifies the portion of the @vaddr
1270 * that is used as the invalidation index.
1271 */
1272static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1273{
1274 asm volatile(
1275 " .insn rrf,0xb98e0000,%0,%1,0,0"
1276 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1277}
1278
1279/**
1280 * gmap_unshadow_page - remove a page from a shadow page table
1281 * @sg: pointer to the shadow guest address space structure
1282 * @raddr: rmap address in the shadow guest address space
1283 *
1284 * Called with the sg->guest_table_lock
1285 */
1286static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1287{
1288 unsigned long *table;
1289
1290 BUG_ON(!gmap_is_shadow(sg));
1291 table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1292 if (!table || *table & _PAGE_INVALID)
1293 return;
1294 gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
1295 ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1296}
1297
1298/**
1299 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1300 * @sg: pointer to the shadow guest address space structure
1301 * @raddr: rmap address in the shadow guest address space
1302 * @pgt: pointer to the start of a shadow page table
1303 *
1304 * Called with the sg->guest_table_lock
1305 */
1306static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1307 unsigned long *pgt)
1308{
1309 int i;
1310
1311 BUG_ON(!gmap_is_shadow(sg));
1312 for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
1313 pgt[i] = _PAGE_INVALID;
1314}
1315
1316/**
1317 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1318 * @sg: pointer to the shadow guest address space structure
1319 * @raddr: address in the shadow guest address space
1320 *
1321 * Called with the sg->guest_table_lock
1322 */
1323static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1324{
1325 unsigned long sto, *ste, *pgt;
1326 struct page *page;
1327
1328 BUG_ON(!gmap_is_shadow(sg));
1329 ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1330 if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1331 return;
1332 gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1333 sto = (unsigned long) (ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1334 gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1335 pgt = (unsigned long *)(*ste & _SEGMENT_ENTRY_ORIGIN);
1336 *ste = _SEGMENT_ENTRY_EMPTY;
1337 __gmap_unshadow_pgt(sg, raddr, pgt);
1338 /* Free page table */
1339 page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1340 list_del(&page->lru);
1341 page_table_free_pgste(page);
1342}
1343
1344/**
1345 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1346 * @sg: pointer to the shadow guest address space structure
1347 * @raddr: rmap address in the shadow guest address space
1348 * @sgt: pointer to the start of a shadow segment table
1349 *
1350 * Called with the sg->guest_table_lock
1351 */
1352static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1353 unsigned long *sgt)
1354{
1355 unsigned long *pgt;
1356 struct page *page;
1357 int i;
1358
1359 BUG_ON(!gmap_is_shadow(sg));
1360 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
1361 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1362 continue;
1363 pgt = (unsigned long *)(sgt[i] & _REGION_ENTRY_ORIGIN);
1364 sgt[i] = _SEGMENT_ENTRY_EMPTY;
1365 __gmap_unshadow_pgt(sg, raddr, pgt);
1366 /* Free page table */
1367 page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1368 list_del(&page->lru);
1369 page_table_free_pgste(page);
1370 }
1371}
1372
1373/**
1374 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1375 * @sg: pointer to the shadow guest address space structure
1376 * @raddr: rmap address in the shadow guest address space
1377 *
1378 * Called with the shadow->guest_table_lock
1379 */
1380static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1381{
1382 unsigned long r3o, *r3e, *sgt;
1383 struct page *page;
1384
1385 BUG_ON(!gmap_is_shadow(sg));
1386 r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1387 if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1388 return;
1389 gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1390 r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1391 gmap_idte_one(r3o | _ASCE_TYPE_REGION3, raddr);
1392 sgt = (unsigned long *)(*r3e & _REGION_ENTRY_ORIGIN);
1393 *r3e = _REGION3_ENTRY_EMPTY;
1394 __gmap_unshadow_sgt(sg, raddr, sgt);
1395 /* Free segment table */
1396 page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1397 list_del(&page->lru);
1398 __free_pages(page, CRST_ALLOC_ORDER);
1399}
1400
1401/**
1402 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1403 * @sg: pointer to the shadow guest address space structure
1404 * @raddr: address in the shadow guest address space
1405 * @r3t: pointer to the start of a shadow region-3 table
1406 *
1407 * Called with the sg->guest_table_lock
1408 */
1409static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1410 unsigned long *r3t)
1411{
1412 unsigned long *sgt;
1413 struct page *page;
1414 int i;
1415
1416 BUG_ON(!gmap_is_shadow(sg));
1417 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
1418 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1419 continue;
1420 sgt = (unsigned long *)(r3t[i] & _REGION_ENTRY_ORIGIN);
1421 r3t[i] = _REGION3_ENTRY_EMPTY;
1422 __gmap_unshadow_sgt(sg, raddr, sgt);
1423 /* Free segment table */
1424 page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1425 list_del(&page->lru);
1426 __free_pages(page, CRST_ALLOC_ORDER);
1427 }
1428}
1429
1430/**
1431 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1432 * @sg: pointer to the shadow guest address space structure
1433 * @raddr: rmap address in the shadow guest address space
1434 *
1435 * Called with the sg->guest_table_lock
1436 */
1437static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1438{
1439 unsigned long r2o, *r2e, *r3t;
1440 struct page *page;
1441
1442 BUG_ON(!gmap_is_shadow(sg));
1443 r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1444 if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1445 return;
1446 gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1447 r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1448 gmap_idte_one(r2o | _ASCE_TYPE_REGION2, raddr);
1449 r3t = (unsigned long *)(*r2e & _REGION_ENTRY_ORIGIN);
1450 *r2e = _REGION2_ENTRY_EMPTY;
1451 __gmap_unshadow_r3t(sg, raddr, r3t);
1452 /* Free region 3 table */
1453 page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1454 list_del(&page->lru);
1455 __free_pages(page, CRST_ALLOC_ORDER);
1456}
1457
1458/**
1459 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1460 * @sg: pointer to the shadow guest address space structure
1461 * @raddr: rmap address in the shadow guest address space
1462 * @r2t: pointer to the start of a shadow region-2 table
1463 *
1464 * Called with the sg->guest_table_lock
1465 */
1466static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1467 unsigned long *r2t)
1468{
1469 unsigned long *r3t;
1470 struct page *page;
1471 int i;
1472
1473 BUG_ON(!gmap_is_shadow(sg));
1474 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
1475 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1476 continue;
1477 r3t = (unsigned long *)(r2t[i] & _REGION_ENTRY_ORIGIN);
1478 r2t[i] = _REGION2_ENTRY_EMPTY;
1479 __gmap_unshadow_r3t(sg, raddr, r3t);
1480 /* Free region 3 table */
1481 page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1482 list_del(&page->lru);
1483 __free_pages(page, CRST_ALLOC_ORDER);
1484 }
1485}
1486
1487/**
1488 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1489 * @sg: pointer to the shadow guest address space structure
1490 * @raddr: rmap address in the shadow guest address space
1491 *
1492 * Called with the sg->guest_table_lock
1493 */
1494static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1495{
1496 unsigned long r1o, *r1e, *r2t;
1497 struct page *page;
1498
1499 BUG_ON(!gmap_is_shadow(sg));
1500 r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1501 if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1502 return;
1503 gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1504 r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1505 gmap_idte_one(r1o | _ASCE_TYPE_REGION1, raddr);
1506 r2t = (unsigned long *)(*r1e & _REGION_ENTRY_ORIGIN);
1507 *r1e = _REGION1_ENTRY_EMPTY;
1508 __gmap_unshadow_r2t(sg, raddr, r2t);
1509 /* Free region 2 table */
1510 page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1511 list_del(&page->lru);
1512 __free_pages(page, CRST_ALLOC_ORDER);
1513}
1514
1515/**
1516 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1517 * @sg: pointer to the shadow guest address space structure
1518 * @raddr: rmap address in the shadow guest address space
1519 * @r1t: pointer to the start of a shadow region-1 table
1520 *
1521 * Called with the shadow->guest_table_lock
1522 */
1523static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1524 unsigned long *r1t)
1525{
1526 unsigned long asce, *r2t;
1527 struct page *page;
1528 int i;
1529
1530 BUG_ON(!gmap_is_shadow(sg));
1531 asce = (unsigned long) r1t | _ASCE_TYPE_REGION1;
1532 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1533 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1534 continue;
1535 r2t = (unsigned long *)(r1t[i] & _REGION_ENTRY_ORIGIN);
1536 __gmap_unshadow_r2t(sg, raddr, r2t);
1537 /* Clear entry and flush translation r1t -> r2t */
1538 gmap_idte_one(asce, raddr);
1539 r1t[i] = _REGION1_ENTRY_EMPTY;
1540 /* Free region 2 table */
1541 page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1542 list_del(&page->lru);
1543 __free_pages(page, CRST_ALLOC_ORDER);
1544 }
1545}
1546
1547/**
1548 * gmap_unshadow - remove a shadow page table completely
1549 * @sg: pointer to the shadow guest address space structure
1550 *
1551 * Called with sg->guest_table_lock
1552 */
1553static void gmap_unshadow(struct gmap *sg)
1554{
1555 unsigned long *table;
1556
1557 BUG_ON(!gmap_is_shadow(sg));
1558 if (sg->removed)
1559 return;
1560 sg->removed = 1;
1561 gmap_call_notifier(sg, 0, -1UL);
1562 gmap_flush_tlb(sg);
1563 table = (unsigned long *)(sg->asce & _ASCE_ORIGIN);
1564 switch (sg->asce & _ASCE_TYPE_MASK) {
1565 case _ASCE_TYPE_REGION1:
1566 __gmap_unshadow_r1t(sg, 0, table);
1567 break;
1568 case _ASCE_TYPE_REGION2:
1569 __gmap_unshadow_r2t(sg, 0, table);
1570 break;
1571 case _ASCE_TYPE_REGION3:
1572 __gmap_unshadow_r3t(sg, 0, table);
1573 break;
1574 case _ASCE_TYPE_SEGMENT:
1575 __gmap_unshadow_sgt(sg, 0, table);
1576 break;
1577 }
1578}
1579
1580/**
1581 * gmap_find_shadow - find a specific asce in the list of shadow tables
1582 * @parent: pointer to the parent gmap
1583 * @asce: ASCE for which the shadow table is created
1584 * @edat_level: edat level to be used for the shadow translation
1585 *
1586 * Returns the pointer to a gmap if a shadow table with the given asce is
1587 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1588 * otherwise NULL
1589 */
1590static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1591 int edat_level)
1592{
1593 struct gmap *sg;
1594
1595 list_for_each_entry(sg, &parent->children, list) {
1596 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1597 sg->removed)
1598 continue;
1599 if (!sg->initialized)
1600 return ERR_PTR(-EAGAIN);
1601 refcount_inc(&sg->ref_count);
1602 return sg;
1603 }
1604 return NULL;
1605}
1606
1607/**
1608 * gmap_shadow_valid - check if a shadow guest address space matches the
1609 * given properties and is still valid
1610 * @sg: pointer to the shadow guest address space structure
1611 * @asce: ASCE for which the shadow table is requested
1612 * @edat_level: edat level to be used for the shadow translation
1613 *
1614 * Returns 1 if the gmap shadow is still valid and matches the given
1615 * properties, the caller can continue using it. Returns 0 otherwise, the
1616 * caller has to request a new shadow gmap in this case.
1617 *
1618 */
1619int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1620{
1621 if (sg->removed)
1622 return 0;
1623 return sg->orig_asce == asce && sg->edat_level == edat_level;
1624}
1625EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1626
1627/**
1628 * gmap_shadow - create/find a shadow guest address space
1629 * @parent: pointer to the parent gmap
1630 * @asce: ASCE for which the shadow table is created
1631 * @edat_level: edat level to be used for the shadow translation
1632 *
1633 * The pages of the top level page table referred by the asce parameter
1634 * will be set to read-only and marked in the PGSTEs of the kvm process.
1635 * The shadow table will be removed automatically on any change to the
1636 * PTE mapping for the source table.
1637 *
1638 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1639 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1640 * parent gmap table could not be protected.
1641 */
1642struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1643 int edat_level)
1644{
1645 struct gmap *sg, *new;
1646 unsigned long limit;
1647 int rc;
1648
1649 BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
1650 BUG_ON(gmap_is_shadow(parent));
1651 spin_lock(&parent->shadow_lock);
1652 sg = gmap_find_shadow(parent, asce, edat_level);
1653 spin_unlock(&parent->shadow_lock);
1654 if (sg)
1655 return sg;
1656 /* Create a new shadow gmap */
1657 limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1658 if (asce & _ASCE_REAL_SPACE)
1659 limit = -1UL;
1660 new = gmap_alloc(limit);
1661 if (!new)
1662 return ERR_PTR(-ENOMEM);
1663 new->mm = parent->mm;
1664 new->parent = gmap_get(parent);
1665 new->orig_asce = asce;
1666 new->edat_level = edat_level;
1667 new->initialized = false;
1668 spin_lock(&parent->shadow_lock);
1669 /* Recheck if another CPU created the same shadow */
1670 sg = gmap_find_shadow(parent, asce, edat_level);
1671 if (sg) {
1672 spin_unlock(&parent->shadow_lock);
1673 gmap_free(new);
1674 return sg;
1675 }
1676 if (asce & _ASCE_REAL_SPACE) {
1677 /* only allow one real-space gmap shadow */
1678 list_for_each_entry(sg, &parent->children, list) {
1679 if (sg->orig_asce & _ASCE_REAL_SPACE) {
1680 spin_lock(&sg->guest_table_lock);
1681 gmap_unshadow(sg);
1682 spin_unlock(&sg->guest_table_lock);
1683 list_del(&sg->list);
1684 gmap_put(sg);
1685 break;
1686 }
1687 }
1688 }
1689 refcount_set(&new->ref_count, 2);
1690 list_add(&new->list, &parent->children);
1691 if (asce & _ASCE_REAL_SPACE) {
1692 /* nothing to protect, return right away */
1693 new->initialized = true;
1694 spin_unlock(&parent->shadow_lock);
1695 return new;
1696 }
1697 spin_unlock(&parent->shadow_lock);
1698 /* protect after insertion, so it will get properly invalidated */
1699 mmap_read_lock(parent->mm);
1700 rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1701 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1702 PROT_READ, GMAP_NOTIFY_SHADOW);
1703 mmap_read_unlock(parent->mm);
1704 spin_lock(&parent->shadow_lock);
1705 new->initialized = true;
1706 if (rc) {
1707 list_del(&new->list);
1708 gmap_free(new);
1709 new = ERR_PTR(rc);
1710 }
1711 spin_unlock(&parent->shadow_lock);
1712 return new;
1713}
1714EXPORT_SYMBOL_GPL(gmap_shadow);
1715
1716/**
1717 * gmap_shadow_r2t - create an empty shadow region 2 table
1718 * @sg: pointer to the shadow guest address space structure
1719 * @saddr: faulting address in the shadow gmap
1720 * @r2t: parent gmap address of the region 2 table to get shadowed
1721 * @fake: r2t references contiguous guest memory block, not a r2t
1722 *
1723 * The r2t parameter specifies the address of the source table. The
1724 * four pages of the source table are made read-only in the parent gmap
1725 * address space. A write to the source table area @r2t will automatically
1726 * remove the shadow r2 table and all of its decendents.
1727 *
1728 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1729 * shadow table structure is incomplete, -ENOMEM if out of memory and
1730 * -EFAULT if an address in the parent gmap could not be resolved.
1731 *
1732 * Called with sg->mm->mmap_lock in read.
1733 */
1734int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1735 int fake)
1736{
1737 unsigned long raddr, origin, offset, len;
1738 unsigned long *s_r2t, *table;
1739 struct page *page;
1740 int rc;
1741
1742 BUG_ON(!gmap_is_shadow(sg));
1743 /* Allocate a shadow region second table */
1744 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1745 if (!page)
1746 return -ENOMEM;
1747 page->index = r2t & _REGION_ENTRY_ORIGIN;
1748 if (fake)
1749 page->index |= GMAP_SHADOW_FAKE_TABLE;
1750 s_r2t = (unsigned long *) page_to_phys(page);
1751 /* Install shadow region second table */
1752 spin_lock(&sg->guest_table_lock);
1753 table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1754 if (!table) {
1755 rc = -EAGAIN; /* Race with unshadow */
1756 goto out_free;
1757 }
1758 if (!(*table & _REGION_ENTRY_INVALID)) {
1759 rc = 0; /* Already established */
1760 goto out_free;
1761 } else if (*table & _REGION_ENTRY_ORIGIN) {
1762 rc = -EAGAIN; /* Race with shadow */
1763 goto out_free;
1764 }
1765 crst_table_init(s_r2t, _REGION2_ENTRY_EMPTY);
1766 /* mark as invalid as long as the parent table is not protected */
1767 *table = (unsigned long) s_r2t | _REGION_ENTRY_LENGTH |
1768 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1769 if (sg->edat_level >= 1)
1770 *table |= (r2t & _REGION_ENTRY_PROTECT);
1771 list_add(&page->lru, &sg->crst_list);
1772 if (fake) {
1773 /* nothing to protect for fake tables */
1774 *table &= ~_REGION_ENTRY_INVALID;
1775 spin_unlock(&sg->guest_table_lock);
1776 return 0;
1777 }
1778 spin_unlock(&sg->guest_table_lock);
1779 /* Make r2t read-only in parent gmap page table */
1780 raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1781 origin = r2t & _REGION_ENTRY_ORIGIN;
1782 offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1783 len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1784 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1785 spin_lock(&sg->guest_table_lock);
1786 if (!rc) {
1787 table = gmap_table_walk(sg, saddr, 4);
1788 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1789 (unsigned long) s_r2t)
1790 rc = -EAGAIN; /* Race with unshadow */
1791 else
1792 *table &= ~_REGION_ENTRY_INVALID;
1793 } else {
1794 gmap_unshadow_r2t(sg, raddr);
1795 }
1796 spin_unlock(&sg->guest_table_lock);
1797 return rc;
1798out_free:
1799 spin_unlock(&sg->guest_table_lock);
1800 __free_pages(page, CRST_ALLOC_ORDER);
1801 return rc;
1802}
1803EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1804
1805/**
1806 * gmap_shadow_r3t - create a shadow region 3 table
1807 * @sg: pointer to the shadow guest address space structure
1808 * @saddr: faulting address in the shadow gmap
1809 * @r3t: parent gmap address of the region 3 table to get shadowed
1810 * @fake: r3t references contiguous guest memory block, not a r3t
1811 *
1812 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1813 * shadow table structure is incomplete, -ENOMEM if out of memory and
1814 * -EFAULT if an address in the parent gmap could not be resolved.
1815 *
1816 * Called with sg->mm->mmap_lock in read.
1817 */
1818int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1819 int fake)
1820{
1821 unsigned long raddr, origin, offset, len;
1822 unsigned long *s_r3t, *table;
1823 struct page *page;
1824 int rc;
1825
1826 BUG_ON(!gmap_is_shadow(sg));
1827 /* Allocate a shadow region second table */
1828 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1829 if (!page)
1830 return -ENOMEM;
1831 page->index = r3t & _REGION_ENTRY_ORIGIN;
1832 if (fake)
1833 page->index |= GMAP_SHADOW_FAKE_TABLE;
1834 s_r3t = (unsigned long *) page_to_phys(page);
1835 /* Install shadow region second table */
1836 spin_lock(&sg->guest_table_lock);
1837 table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1838 if (!table) {
1839 rc = -EAGAIN; /* Race with unshadow */
1840 goto out_free;
1841 }
1842 if (!(*table & _REGION_ENTRY_INVALID)) {
1843 rc = 0; /* Already established */
1844 goto out_free;
1845 } else if (*table & _REGION_ENTRY_ORIGIN) {
1846 rc = -EAGAIN; /* Race with shadow */
1847 goto out_free;
1848 }
1849 crst_table_init(s_r3t, _REGION3_ENTRY_EMPTY);
1850 /* mark as invalid as long as the parent table is not protected */
1851 *table = (unsigned long) s_r3t | _REGION_ENTRY_LENGTH |
1852 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1853 if (sg->edat_level >= 1)
1854 *table |= (r3t & _REGION_ENTRY_PROTECT);
1855 list_add(&page->lru, &sg->crst_list);
1856 if (fake) {
1857 /* nothing to protect for fake tables */
1858 *table &= ~_REGION_ENTRY_INVALID;
1859 spin_unlock(&sg->guest_table_lock);
1860 return 0;
1861 }
1862 spin_unlock(&sg->guest_table_lock);
1863 /* Make r3t read-only in parent gmap page table */
1864 raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1865 origin = r3t & _REGION_ENTRY_ORIGIN;
1866 offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1867 len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1868 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1869 spin_lock(&sg->guest_table_lock);
1870 if (!rc) {
1871 table = gmap_table_walk(sg, saddr, 3);
1872 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1873 (unsigned long) s_r3t)
1874 rc = -EAGAIN; /* Race with unshadow */
1875 else
1876 *table &= ~_REGION_ENTRY_INVALID;
1877 } else {
1878 gmap_unshadow_r3t(sg, raddr);
1879 }
1880 spin_unlock(&sg->guest_table_lock);
1881 return rc;
1882out_free:
1883 spin_unlock(&sg->guest_table_lock);
1884 __free_pages(page, CRST_ALLOC_ORDER);
1885 return rc;
1886}
1887EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1888
1889/**
1890 * gmap_shadow_sgt - create a shadow segment table
1891 * @sg: pointer to the shadow guest address space structure
1892 * @saddr: faulting address in the shadow gmap
1893 * @sgt: parent gmap address of the segment table to get shadowed
1894 * @fake: sgt references contiguous guest memory block, not a sgt
1895 *
1896 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1897 * shadow table structure is incomplete, -ENOMEM if out of memory and
1898 * -EFAULT if an address in the parent gmap could not be resolved.
1899 *
1900 * Called with sg->mm->mmap_lock in read.
1901 */
1902int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1903 int fake)
1904{
1905 unsigned long raddr, origin, offset, len;
1906 unsigned long *s_sgt, *table;
1907 struct page *page;
1908 int rc;
1909
1910 BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1911 /* Allocate a shadow segment table */
1912 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1913 if (!page)
1914 return -ENOMEM;
1915 page->index = sgt & _REGION_ENTRY_ORIGIN;
1916 if (fake)
1917 page->index |= GMAP_SHADOW_FAKE_TABLE;
1918 s_sgt = (unsigned long *) page_to_phys(page);
1919 /* Install shadow region second table */
1920 spin_lock(&sg->guest_table_lock);
1921 table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1922 if (!table) {
1923 rc = -EAGAIN; /* Race with unshadow */
1924 goto out_free;
1925 }
1926 if (!(*table & _REGION_ENTRY_INVALID)) {
1927 rc = 0; /* Already established */
1928 goto out_free;
1929 } else if (*table & _REGION_ENTRY_ORIGIN) {
1930 rc = -EAGAIN; /* Race with shadow */
1931 goto out_free;
1932 }
1933 crst_table_init(s_sgt, _SEGMENT_ENTRY_EMPTY);
1934 /* mark as invalid as long as the parent table is not protected */
1935 *table = (unsigned long) s_sgt | _REGION_ENTRY_LENGTH |
1936 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1937 if (sg->edat_level >= 1)
1938 *table |= sgt & _REGION_ENTRY_PROTECT;
1939 list_add(&page->lru, &sg->crst_list);
1940 if (fake) {
1941 /* nothing to protect for fake tables */
1942 *table &= ~_REGION_ENTRY_INVALID;
1943 spin_unlock(&sg->guest_table_lock);
1944 return 0;
1945 }
1946 spin_unlock(&sg->guest_table_lock);
1947 /* Make sgt read-only in parent gmap page table */
1948 raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
1949 origin = sgt & _REGION_ENTRY_ORIGIN;
1950 offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1951 len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1952 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1953 spin_lock(&sg->guest_table_lock);
1954 if (!rc) {
1955 table = gmap_table_walk(sg, saddr, 2);
1956 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1957 (unsigned long) s_sgt)
1958 rc = -EAGAIN; /* Race with unshadow */
1959 else
1960 *table &= ~_REGION_ENTRY_INVALID;
1961 } else {
1962 gmap_unshadow_sgt(sg, raddr);
1963 }
1964 spin_unlock(&sg->guest_table_lock);
1965 return rc;
1966out_free:
1967 spin_unlock(&sg->guest_table_lock);
1968 __free_pages(page, CRST_ALLOC_ORDER);
1969 return rc;
1970}
1971EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
1972
1973/**
1974 * gmap_shadow_lookup_pgtable - find a shadow page table
1975 * @sg: pointer to the shadow guest address space structure
1976 * @saddr: the address in the shadow aguest address space
1977 * @pgt: parent gmap address of the page table to get shadowed
1978 * @dat_protection: if the pgtable is marked as protected by dat
1979 * @fake: pgt references contiguous guest memory block, not a pgtable
1980 *
1981 * Returns 0 if the shadow page table was found and -EAGAIN if the page
1982 * table was not found.
1983 *
1984 * Called with sg->mm->mmap_lock in read.
1985 */
1986int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
1987 unsigned long *pgt, int *dat_protection,
1988 int *fake)
1989{
1990 unsigned long *table;
1991 struct page *page;
1992 int rc;
1993
1994 BUG_ON(!gmap_is_shadow(sg));
1995 spin_lock(&sg->guest_table_lock);
1996 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
1997 if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
1998 /* Shadow page tables are full pages (pte+pgste) */
1999 page = pfn_to_page(*table >> PAGE_SHIFT);
2000 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
2001 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
2002 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
2003 rc = 0;
2004 } else {
2005 rc = -EAGAIN;
2006 }
2007 spin_unlock(&sg->guest_table_lock);
2008 return rc;
2009
2010}
2011EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2012
2013/**
2014 * gmap_shadow_pgt - instantiate a shadow page table
2015 * @sg: pointer to the shadow guest address space structure
2016 * @saddr: faulting address in the shadow gmap
2017 * @pgt: parent gmap address of the page table to get shadowed
2018 * @fake: pgt references contiguous guest memory block, not a pgtable
2019 *
2020 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2021 * shadow table structure is incomplete, -ENOMEM if out of memory,
2022 * -EFAULT if an address in the parent gmap could not be resolved and
2023 *
2024 * Called with gmap->mm->mmap_lock in read
2025 */
2026int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2027 int fake)
2028{
2029 unsigned long raddr, origin;
2030 unsigned long *s_pgt, *table;
2031 struct page *page;
2032 int rc;
2033
2034 BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
2035 /* Allocate a shadow page table */
2036 page = page_table_alloc_pgste(sg->mm);
2037 if (!page)
2038 return -ENOMEM;
2039 page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
2040 if (fake)
2041 page->index |= GMAP_SHADOW_FAKE_TABLE;
2042 s_pgt = (unsigned long *) page_to_phys(page);
2043 /* Install shadow page table */
2044 spin_lock(&sg->guest_table_lock);
2045 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2046 if (!table) {
2047 rc = -EAGAIN; /* Race with unshadow */
2048 goto out_free;
2049 }
2050 if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2051 rc = 0; /* Already established */
2052 goto out_free;
2053 } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2054 rc = -EAGAIN; /* Race with shadow */
2055 goto out_free;
2056 }
2057 /* mark as invalid as long as the parent table is not protected */
2058 *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
2059 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
2060 list_add(&page->lru, &sg->pt_list);
2061 if (fake) {
2062 /* nothing to protect for fake tables */
2063 *table &= ~_SEGMENT_ENTRY_INVALID;
2064 spin_unlock(&sg->guest_table_lock);
2065 return 0;
2066 }
2067 spin_unlock(&sg->guest_table_lock);
2068 /* Make pgt read-only in parent gmap page table (not the pgste) */
2069 raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
2070 origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
2071 rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
2072 spin_lock(&sg->guest_table_lock);
2073 if (!rc) {
2074 table = gmap_table_walk(sg, saddr, 1);
2075 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) !=
2076 (unsigned long) s_pgt)
2077 rc = -EAGAIN; /* Race with unshadow */
2078 else
2079 *table &= ~_SEGMENT_ENTRY_INVALID;
2080 } else {
2081 gmap_unshadow_pgt(sg, raddr);
2082 }
2083 spin_unlock(&sg->guest_table_lock);
2084 return rc;
2085out_free:
2086 spin_unlock(&sg->guest_table_lock);
2087 page_table_free_pgste(page);
2088 return rc;
2089
2090}
2091EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2092
2093/**
2094 * gmap_shadow_page - create a shadow page mapping
2095 * @sg: pointer to the shadow guest address space structure
2096 * @saddr: faulting address in the shadow gmap
2097 * @pte: pte in parent gmap address space to get shadowed
2098 *
2099 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2100 * shadow table structure is incomplete, -ENOMEM if out of memory and
2101 * -EFAULT if an address in the parent gmap could not be resolved.
2102 *
2103 * Called with sg->mm->mmap_lock in read.
2104 */
2105int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
2106{
2107 struct gmap *parent;
2108 struct gmap_rmap *rmap;
2109 unsigned long vmaddr, paddr;
2110 spinlock_t *ptl;
2111 pte_t *sptep, *tptep;
2112 int prot;
2113 int rc;
2114
2115 BUG_ON(!gmap_is_shadow(sg));
2116 parent = sg->parent;
2117 prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
2118
2119 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
2120 if (!rmap)
2121 return -ENOMEM;
2122 rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2123
2124 while (1) {
2125 paddr = pte_val(pte) & PAGE_MASK;
2126 vmaddr = __gmap_translate(parent, paddr);
2127 if (IS_ERR_VALUE(vmaddr)) {
2128 rc = vmaddr;
2129 break;
2130 }
2131 rc = radix_tree_preload(GFP_KERNEL);
2132 if (rc)
2133 break;
2134 rc = -EAGAIN;
2135 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2136 if (sptep) {
2137 spin_lock(&sg->guest_table_lock);
2138 /* Get page table pointer */
2139 tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2140 if (!tptep) {
2141 spin_unlock(&sg->guest_table_lock);
2142 gmap_pte_op_end(ptl);
2143 radix_tree_preload_end();
2144 break;
2145 }
2146 rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
2147 if (rc > 0) {
2148 /* Success and a new mapping */
2149 gmap_insert_rmap(sg, vmaddr, rmap);
2150 rmap = NULL;
2151 rc = 0;
2152 }
2153 gmap_pte_op_end(ptl);
2154 spin_unlock(&sg->guest_table_lock);
2155 }
2156 radix_tree_preload_end();
2157 if (!rc)
2158 break;
2159 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2160 if (rc)
2161 break;
2162 }
2163 kfree(rmap);
2164 return rc;
2165}
2166EXPORT_SYMBOL_GPL(gmap_shadow_page);
2167
2168/**
2169 * gmap_shadow_notify - handle notifications for shadow gmap
2170 *
2171 * Called with sg->parent->shadow_lock.
2172 */
2173static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2174 unsigned long gaddr)
2175{
2176 struct gmap_rmap *rmap, *rnext, *head;
2177 unsigned long start, end, bits, raddr;
2178
2179 BUG_ON(!gmap_is_shadow(sg));
2180
2181 spin_lock(&sg->guest_table_lock);
2182 if (sg->removed) {
2183 spin_unlock(&sg->guest_table_lock);
2184 return;
2185 }
2186 /* Check for top level table */
2187 start = sg->orig_asce & _ASCE_ORIGIN;
2188 end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2189 if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2190 gaddr < end) {
2191 /* The complete shadow table has to go */
2192 gmap_unshadow(sg);
2193 spin_unlock(&sg->guest_table_lock);
2194 list_del(&sg->list);
2195 gmap_put(sg);
2196 return;
2197 }
2198 /* Remove the page table tree from on specific entry */
2199 head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2200 gmap_for_each_rmap_safe(rmap, rnext, head) {
2201 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2202 raddr = rmap->raddr ^ bits;
2203 switch (bits) {
2204 case _SHADOW_RMAP_REGION1:
2205 gmap_unshadow_r2t(sg, raddr);
2206 break;
2207 case _SHADOW_RMAP_REGION2:
2208 gmap_unshadow_r3t(sg, raddr);
2209 break;
2210 case _SHADOW_RMAP_REGION3:
2211 gmap_unshadow_sgt(sg, raddr);
2212 break;
2213 case _SHADOW_RMAP_SEGMENT:
2214 gmap_unshadow_pgt(sg, raddr);
2215 break;
2216 case _SHADOW_RMAP_PGTABLE:
2217 gmap_unshadow_page(sg, raddr);
2218 break;
2219 }
2220 kfree(rmap);
2221 }
2222 spin_unlock(&sg->guest_table_lock);
2223}
2224
2225/**
2226 * ptep_notify - call all invalidation callbacks for a specific pte.
2227 * @mm: pointer to the process mm_struct
2228 * @addr: virtual address in the process address space
2229 * @pte: pointer to the page table entry
2230 * @bits: bits from the pgste that caused the notify call
2231 *
2232 * This function is assumed to be called with the page table lock held
2233 * for the pte to notify.
2234 */
2235void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2236 pte_t *pte, unsigned long bits)
2237{
2238 unsigned long offset, gaddr = 0;
2239 unsigned long *table;
2240 struct gmap *gmap, *sg, *next;
2241
2242 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2243 offset = offset * (PAGE_SIZE / sizeof(pte_t));
2244 rcu_read_lock();
2245 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2246 spin_lock(&gmap->guest_table_lock);
2247 table = radix_tree_lookup(&gmap->host_to_guest,
2248 vmaddr >> PMD_SHIFT);
2249 if (table)
2250 gaddr = __gmap_segment_gaddr(table) + offset;
2251 spin_unlock(&gmap->guest_table_lock);
2252 if (!table)
2253 continue;
2254
2255 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2256 spin_lock(&gmap->shadow_lock);
2257 list_for_each_entry_safe(sg, next,
2258 &gmap->children, list)
2259 gmap_shadow_notify(sg, vmaddr, gaddr);
2260 spin_unlock(&gmap->shadow_lock);
2261 }
2262 if (bits & PGSTE_IN_BIT)
2263 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2264 }
2265 rcu_read_unlock();
2266}
2267EXPORT_SYMBOL_GPL(ptep_notify);
2268
2269static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2270 unsigned long gaddr)
2271{
2272 pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_IN;
2273 gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2274}
2275
2276/**
2277 * gmap_pmdp_xchg - exchange a gmap pmd with another
2278 * @gmap: pointer to the guest address space structure
2279 * @pmdp: pointer to the pmd entry
2280 * @new: replacement entry
2281 * @gaddr: the affected guest address
2282 *
2283 * This function is assumed to be called with the guest_table_lock
2284 * held.
2285 */
2286static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2287 unsigned long gaddr)
2288{
2289 gaddr &= HPAGE_MASK;
2290 pmdp_notify_gmap(gmap, pmdp, gaddr);
2291 pmd_val(new) &= ~_SEGMENT_ENTRY_GMAP_IN;
2292 if (MACHINE_HAS_TLB_GUEST)
2293 __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2294 IDTE_GLOBAL);
2295 else if (MACHINE_HAS_IDTE)
2296 __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2297 else
2298 __pmdp_csp(pmdp);
2299 *pmdp = new;
2300}
2301
2302static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2303 int purge)
2304{
2305 pmd_t *pmdp;
2306 struct gmap *gmap;
2307 unsigned long gaddr;
2308
2309 rcu_read_lock();
2310 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2311 spin_lock(&gmap->guest_table_lock);
2312 pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2313 vmaddr >> PMD_SHIFT);
2314 if (pmdp) {
2315 gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2316 pmdp_notify_gmap(gmap, pmdp, gaddr);
2317 WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2318 _SEGMENT_ENTRY_GMAP_UC));
2319 if (purge)
2320 __pmdp_csp(pmdp);
2321 pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
2322 }
2323 spin_unlock(&gmap->guest_table_lock);
2324 }
2325 rcu_read_unlock();
2326}
2327
2328/**
2329 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2330 * flushing
2331 * @mm: pointer to the process mm_struct
2332 * @vmaddr: virtual address in the process address space
2333 */
2334void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2335{
2336 gmap_pmdp_clear(mm, vmaddr, 0);
2337}
2338EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2339
2340/**
2341 * gmap_pmdp_csp - csp all affected guest pmd entries
2342 * @mm: pointer to the process mm_struct
2343 * @vmaddr: virtual address in the process address space
2344 */
2345void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2346{
2347 gmap_pmdp_clear(mm, vmaddr, 1);
2348}
2349EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2350
2351/**
2352 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2353 * @mm: pointer to the process mm_struct
2354 * @vmaddr: virtual address in the process address space
2355 */
2356void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2357{
2358 unsigned long *entry, gaddr;
2359 struct gmap *gmap;
2360 pmd_t *pmdp;
2361
2362 rcu_read_lock();
2363 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2364 spin_lock(&gmap->guest_table_lock);
2365 entry = radix_tree_delete(&gmap->host_to_guest,
2366 vmaddr >> PMD_SHIFT);
2367 if (entry) {
2368 pmdp = (pmd_t *)entry;
2369 gaddr = __gmap_segment_gaddr(entry);
2370 pmdp_notify_gmap(gmap, pmdp, gaddr);
2371 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2372 _SEGMENT_ENTRY_GMAP_UC));
2373 if (MACHINE_HAS_TLB_GUEST)
2374 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2375 gmap->asce, IDTE_LOCAL);
2376 else if (MACHINE_HAS_IDTE)
2377 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2378 *entry = _SEGMENT_ENTRY_EMPTY;
2379 }
2380 spin_unlock(&gmap->guest_table_lock);
2381 }
2382 rcu_read_unlock();
2383}
2384EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2385
2386/**
2387 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2388 * @mm: pointer to the process mm_struct
2389 * @vmaddr: virtual address in the process address space
2390 */
2391void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2392{
2393 unsigned long *entry, gaddr;
2394 struct gmap *gmap;
2395 pmd_t *pmdp;
2396
2397 rcu_read_lock();
2398 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2399 spin_lock(&gmap->guest_table_lock);
2400 entry = radix_tree_delete(&gmap->host_to_guest,
2401 vmaddr >> PMD_SHIFT);
2402 if (entry) {
2403 pmdp = (pmd_t *)entry;
2404 gaddr = __gmap_segment_gaddr(entry);
2405 pmdp_notify_gmap(gmap, pmdp, gaddr);
2406 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2407 _SEGMENT_ENTRY_GMAP_UC));
2408 if (MACHINE_HAS_TLB_GUEST)
2409 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2410 gmap->asce, IDTE_GLOBAL);
2411 else if (MACHINE_HAS_IDTE)
2412 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2413 else
2414 __pmdp_csp(pmdp);
2415 *entry = _SEGMENT_ENTRY_EMPTY;
2416 }
2417 spin_unlock(&gmap->guest_table_lock);
2418 }
2419 rcu_read_unlock();
2420}
2421EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2422
2423/**
2424 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2425 * @gmap: pointer to guest address space
2426 * @pmdp: pointer to the pmd to be tested
2427 * @gaddr: virtual address in the guest address space
2428 *
2429 * This function is assumed to be called with the guest_table_lock
2430 * held.
2431 */
2432static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2433 unsigned long gaddr)
2434{
2435 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2436 return false;
2437
2438 /* Already protected memory, which did not change is clean */
2439 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2440 !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2441 return false;
2442
2443 /* Clear UC indication and reset protection */
2444 pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_UC;
2445 gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2446 return true;
2447}
2448
2449/**
2450 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2451 * @gmap: pointer to guest address space
2452 * @bitmap: dirty bitmap for this pmd
2453 * @gaddr: virtual address in the guest address space
2454 * @vmaddr: virtual address in the host address space
2455 *
2456 * This function is assumed to be called with the guest_table_lock
2457 * held.
2458 */
2459void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2460 unsigned long gaddr, unsigned long vmaddr)
2461{
2462 int i;
2463 pmd_t *pmdp;
2464 pte_t *ptep;
2465 spinlock_t *ptl;
2466
2467 pmdp = gmap_pmd_op_walk(gmap, gaddr);
2468 if (!pmdp)
2469 return;
2470
2471 if (pmd_large(*pmdp)) {
2472 if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2473 bitmap_fill(bitmap, _PAGE_ENTRIES);
2474 } else {
2475 for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2476 ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2477 if (!ptep)
2478 continue;
2479 if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2480 set_bit(i, bitmap);
2481 spin_unlock(ptl);
2482 }
2483 }
2484 gmap_pmd_op_end(gmap, pmdp);
2485}
2486EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2487
2488#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2489static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
2490 unsigned long end, struct mm_walk *walk)
2491{
2492 struct vm_area_struct *vma = walk->vma;
2493
2494 split_huge_pmd(vma, pmd, addr);
2495 return 0;
2496}
2497
2498static const struct mm_walk_ops thp_split_walk_ops = {
2499 .pmd_entry = thp_split_walk_pmd_entry,
2500};
2501
2502static inline void thp_split_mm(struct mm_struct *mm)
2503{
2504 struct vm_area_struct *vma;
2505
2506 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
2507 vma->vm_flags &= ~VM_HUGEPAGE;
2508 vma->vm_flags |= VM_NOHUGEPAGE;
2509 walk_page_vma(vma, &thp_split_walk_ops, NULL);
2510 }
2511 mm->def_flags |= VM_NOHUGEPAGE;
2512}
2513#else
2514static inline void thp_split_mm(struct mm_struct *mm)
2515{
2516}
2517#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2518
2519/*
2520 * Remove all empty zero pages from the mapping for lazy refaulting
2521 * - This must be called after mm->context.has_pgste is set, to avoid
2522 * future creation of zero pages
2523 * - This must be called after THP was enabled
2524 */
2525static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
2526 unsigned long end, struct mm_walk *walk)
2527{
2528 unsigned long addr;
2529
2530 for (addr = start; addr != end; addr += PAGE_SIZE) {
2531 pte_t *ptep;
2532 spinlock_t *ptl;
2533
2534 ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
2535 if (is_zero_pfn(pte_pfn(*ptep)))
2536 ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
2537 pte_unmap_unlock(ptep, ptl);
2538 }
2539 return 0;
2540}
2541
2542static const struct mm_walk_ops zap_zero_walk_ops = {
2543 .pmd_entry = __zap_zero_pages,
2544};
2545
2546/*
2547 * switch on pgstes for its userspace process (for kvm)
2548 */
2549int s390_enable_sie(void)
2550{
2551 struct mm_struct *mm = current->mm;
2552
2553 /* Do we have pgstes? if yes, we are done */
2554 if (mm_has_pgste(mm))
2555 return 0;
2556 /* Fail if the page tables are 2K */
2557 if (!mm_alloc_pgste(mm))
2558 return -EINVAL;
2559 mmap_write_lock(mm);
2560 mm->context.has_pgste = 1;
2561 /* split thp mappings and disable thp for future mappings */
2562 thp_split_mm(mm);
2563 walk_page_range(mm, 0, TASK_SIZE, &zap_zero_walk_ops, NULL);
2564 mmap_write_unlock(mm);
2565 return 0;
2566}
2567EXPORT_SYMBOL_GPL(s390_enable_sie);
2568
2569int gmap_mark_unmergeable(void)
2570{
2571 struct mm_struct *mm = current->mm;
2572 struct vm_area_struct *vma;
2573 int ret;
2574
2575 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2576 ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
2577 MADV_UNMERGEABLE, &vma->vm_flags);
2578 if (ret)
2579 return ret;
2580 }
2581 mm->def_flags &= ~VM_MERGEABLE;
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(gmap_mark_unmergeable);
2585
2586/*
2587 * Enable storage key handling from now on and initialize the storage
2588 * keys with the default key.
2589 */
2590static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2591 unsigned long next, struct mm_walk *walk)
2592{
2593 /* Clear storage key */
2594 ptep_zap_key(walk->mm, addr, pte);
2595 return 0;
2596}
2597
2598static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2599 unsigned long hmask, unsigned long next,
2600 struct mm_walk *walk)
2601{
2602 pmd_t *pmd = (pmd_t *)pte;
2603 unsigned long start, end;
2604 struct page *page = pmd_page(*pmd);
2605
2606 /*
2607 * The write check makes sure we do not set a key on shared
2608 * memory. This is needed as the walker does not differentiate
2609 * between actual guest memory and the process executable or
2610 * shared libraries.
2611 */
2612 if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2613 !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2614 return 0;
2615
2616 start = pmd_val(*pmd) & HPAGE_MASK;
2617 end = start + HPAGE_SIZE - 1;
2618 __storage_key_init_range(start, end);
2619 set_bit(PG_arch_1, &page->flags);
2620 return 0;
2621}
2622
2623static const struct mm_walk_ops enable_skey_walk_ops = {
2624 .hugetlb_entry = __s390_enable_skey_hugetlb,
2625 .pte_entry = __s390_enable_skey_pte,
2626};
2627
2628int s390_enable_skey(void)
2629{
2630 struct mm_struct *mm = current->mm;
2631 int rc = 0;
2632
2633 mmap_write_lock(mm);
2634 if (mm_uses_skeys(mm))
2635 goto out_up;
2636
2637 mm->context.uses_skeys = 1;
2638 rc = gmap_mark_unmergeable();
2639 if (rc) {
2640 mm->context.uses_skeys = 0;
2641 goto out_up;
2642 }
2643 walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
2644
2645out_up:
2646 mmap_write_unlock(mm);
2647 return rc;
2648}
2649EXPORT_SYMBOL_GPL(s390_enable_skey);
2650
2651/*
2652 * Reset CMMA state, make all pages stable again.
2653 */
2654static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2655 unsigned long next, struct mm_walk *walk)
2656{
2657 ptep_zap_unused(walk->mm, addr, pte, 1);
2658 return 0;
2659}
2660
2661static const struct mm_walk_ops reset_cmma_walk_ops = {
2662 .pte_entry = __s390_reset_cmma,
2663};
2664
2665void s390_reset_cmma(struct mm_struct *mm)
2666{
2667 mmap_write_lock(mm);
2668 walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
2669 mmap_write_unlock(mm);
2670}
2671EXPORT_SYMBOL_GPL(s390_reset_cmma);
2672
2673/*
2674 * make inaccessible pages accessible again
2675 */
2676static int __s390_reset_acc(pte_t *ptep, unsigned long addr,
2677 unsigned long next, struct mm_walk *walk)
2678{
2679 pte_t pte = READ_ONCE(*ptep);
2680
2681 if (pte_present(pte))
2682 WARN_ON_ONCE(uv_convert_from_secure(pte_val(pte) & PAGE_MASK));
2683 return 0;
2684}
2685
2686static const struct mm_walk_ops reset_acc_walk_ops = {
2687 .pte_entry = __s390_reset_acc,
2688};
2689
2690#include <linux/sched/mm.h>
2691void s390_reset_acc(struct mm_struct *mm)
2692{
2693 /*
2694 * we might be called during
2695 * reset: we walk the pages and clear
2696 * close of all kvm file descriptors: we walk the pages and clear
2697 * exit of process on fd closure: vma already gone, do nothing
2698 */
2699 if (!mmget_not_zero(mm))
2700 return;
2701 mmap_read_lock(mm);
2702 walk_page_range(mm, 0, TASK_SIZE, &reset_acc_walk_ops, NULL);
2703 mmap_read_unlock(mm);
2704 mmput(mm);
2705}
2706EXPORT_SYMBOL_GPL(s390_reset_acc);