GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / s390 / mm / gmap.c
1 /*
2  *  KVM guest address space mapping code
3  *
4  *    Copyright IBM Corp. 2007, 2016
5  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/swap.h>
11 #include <linux/smp.h>
12 #include <linux/spinlock.h>
13 #include <linux/slab.h>
14 #include <linux/swapops.h>
15 #include <linux/ksm.h>
16 #include <linux/mman.h>
17
18 #include <asm/pgtable.h>
19 #include <asm/pgalloc.h>
20 #include <asm/gmap.h>
21 #include <asm/tlb.h>
22
23 #define GMAP_SHADOW_FAKE_TABLE 1ULL
24
25 /**
26  * gmap_alloc - allocate and initialize a guest address space
27  * @mm: pointer to the parent mm_struct
28  * @limit: maximum address of the gmap address space
29  *
30  * Returns a guest address space structure.
31  */
32 static struct gmap *gmap_alloc(unsigned long limit)
33 {
34         struct gmap *gmap;
35         struct page *page;
36         unsigned long *table;
37         unsigned long etype, atype;
38
39         if (limit < _REGION3_SIZE) {
40                 limit = _REGION3_SIZE - 1;
41                 atype = _ASCE_TYPE_SEGMENT;
42                 etype = _SEGMENT_ENTRY_EMPTY;
43         } else if (limit < _REGION2_SIZE) {
44                 limit = _REGION2_SIZE - 1;
45                 atype = _ASCE_TYPE_REGION3;
46                 etype = _REGION3_ENTRY_EMPTY;
47         } else if (limit < _REGION1_SIZE) {
48                 limit = _REGION1_SIZE - 1;
49                 atype = _ASCE_TYPE_REGION2;
50                 etype = _REGION2_ENTRY_EMPTY;
51         } else {
52                 limit = -1UL;
53                 atype = _ASCE_TYPE_REGION1;
54                 etype = _REGION1_ENTRY_EMPTY;
55         }
56         gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
57         if (!gmap)
58                 goto out;
59         INIT_LIST_HEAD(&gmap->crst_list);
60         INIT_LIST_HEAD(&gmap->children);
61         INIT_LIST_HEAD(&gmap->pt_list);
62         INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
63         INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
64         INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC);
65         spin_lock_init(&gmap->guest_table_lock);
66         spin_lock_init(&gmap->shadow_lock);
67         atomic_set(&gmap->ref_count, 1);
68         page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
69         if (!page)
70                 goto out_free;
71         page->index = 0;
72         list_add(&page->lru, &gmap->crst_list);
73         table = (unsigned long *) page_to_phys(page);
74         crst_table_init(table, etype);
75         gmap->table = table;
76         gmap->asce = atype | _ASCE_TABLE_LENGTH |
77                 _ASCE_USER_BITS | __pa(table);
78         gmap->asce_end = limit;
79         return gmap;
80
81 out_free:
82         kfree(gmap);
83 out:
84         return NULL;
85 }
86
87 /**
88  * gmap_create - create a guest address space
89  * @mm: pointer to the parent mm_struct
90  * @limit: maximum size of the gmap address space
91  *
92  * Returns a guest address space structure.
93  */
94 struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
95 {
96         struct gmap *gmap;
97         unsigned long gmap_asce;
98
99         gmap = gmap_alloc(limit);
100         if (!gmap)
101                 return NULL;
102         gmap->mm = mm;
103         spin_lock(&mm->context.lock);
104         list_add_rcu(&gmap->list, &mm->context.gmap_list);
105         if (list_is_singular(&mm->context.gmap_list))
106                 gmap_asce = gmap->asce;
107         else
108                 gmap_asce = -1UL;
109         WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
110         spin_unlock(&mm->context.lock);
111         return gmap;
112 }
113 EXPORT_SYMBOL_GPL(gmap_create);
114
115 static void gmap_flush_tlb(struct gmap *gmap)
116 {
117         if (MACHINE_HAS_IDTE)
118                 __tlb_flush_idte(gmap->asce);
119         else
120                 __tlb_flush_global();
121 }
122
123 static void gmap_radix_tree_free(struct radix_tree_root *root)
124 {
125         struct radix_tree_iter iter;
126         unsigned long indices[16];
127         unsigned long index;
128         void __rcu **slot;
129         int i, nr;
130
131         /* A radix tree is freed by deleting all of its entries */
132         index = 0;
133         do {
134                 nr = 0;
135                 radix_tree_for_each_slot(slot, root, &iter, index) {
136                         indices[nr] = iter.index;
137                         if (++nr == 16)
138                                 break;
139                 }
140                 for (i = 0; i < nr; i++) {
141                         index = indices[i];
142                         radix_tree_delete(root, index);
143                 }
144         } while (nr > 0);
145 }
146
147 static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
148 {
149         struct gmap_rmap *rmap, *rnext, *head;
150         struct radix_tree_iter iter;
151         unsigned long indices[16];
152         unsigned long index;
153         void __rcu **slot;
154         int i, nr;
155
156         /* A radix tree is freed by deleting all of its entries */
157         index = 0;
158         do {
159                 nr = 0;
160                 radix_tree_for_each_slot(slot, root, &iter, index) {
161                         indices[nr] = iter.index;
162                         if (++nr == 16)
163                                 break;
164                 }
165                 for (i = 0; i < nr; i++) {
166                         index = indices[i];
167                         head = radix_tree_delete(root, index);
168                         gmap_for_each_rmap_safe(rmap, rnext, head)
169                                 kfree(rmap);
170                 }
171         } while (nr > 0);
172 }
173
174 /**
175  * gmap_free - free a guest address space
176  * @gmap: pointer to the guest address space structure
177  *
178  * No locks required. There are no references to this gmap anymore.
179  */
180 static void gmap_free(struct gmap *gmap)
181 {
182         struct page *page, *next;
183
184         /* Flush tlb of all gmaps (if not already done for shadows) */
185         if (!(gmap_is_shadow(gmap) && gmap->removed))
186                 gmap_flush_tlb(gmap);
187         /* Free all segment & region tables. */
188         list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
189                 __free_pages(page, CRST_ALLOC_ORDER);
190         gmap_radix_tree_free(&gmap->guest_to_host);
191         gmap_radix_tree_free(&gmap->host_to_guest);
192
193         /* Free additional data for a shadow gmap */
194         if (gmap_is_shadow(gmap)) {
195                 /* Free all page tables. */
196                 list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
197                         page_table_free_pgste(page);
198                 gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
199                 /* Release reference to the parent */
200                 gmap_put(gmap->parent);
201         }
202
203         kfree(gmap);
204 }
205
206 /**
207  * gmap_get - increase reference counter for guest address space
208  * @gmap: pointer to the guest address space structure
209  *
210  * Returns the gmap pointer
211  */
212 struct gmap *gmap_get(struct gmap *gmap)
213 {
214         atomic_inc(&gmap->ref_count);
215         return gmap;
216 }
217 EXPORT_SYMBOL_GPL(gmap_get);
218
219 /**
220  * gmap_put - decrease reference counter for guest address space
221  * @gmap: pointer to the guest address space structure
222  *
223  * If the reference counter reaches zero the guest address space is freed.
224  */
225 void gmap_put(struct gmap *gmap)
226 {
227         if (atomic_dec_return(&gmap->ref_count) == 0)
228                 gmap_free(gmap);
229 }
230 EXPORT_SYMBOL_GPL(gmap_put);
231
232 /**
233  * gmap_remove - remove a guest address space but do not free it yet
234  * @gmap: pointer to the guest address space structure
235  */
236 void gmap_remove(struct gmap *gmap)
237 {
238         struct gmap *sg, *next;
239         unsigned long gmap_asce;
240
241         /* Remove all shadow gmaps linked to this gmap */
242         if (!list_empty(&gmap->children)) {
243                 spin_lock(&gmap->shadow_lock);
244                 list_for_each_entry_safe(sg, next, &gmap->children, list) {
245                         list_del(&sg->list);
246                         gmap_put(sg);
247                 }
248                 spin_unlock(&gmap->shadow_lock);
249         }
250         /* Remove gmap from the pre-mm list */
251         spin_lock(&gmap->mm->context.lock);
252         list_del_rcu(&gmap->list);
253         if (list_empty(&gmap->mm->context.gmap_list))
254                 gmap_asce = 0;
255         else if (list_is_singular(&gmap->mm->context.gmap_list))
256                 gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
257                                              struct gmap, list)->asce;
258         else
259                 gmap_asce = -1UL;
260         WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
261         spin_unlock(&gmap->mm->context.lock);
262         synchronize_rcu();
263         /* Put reference */
264         gmap_put(gmap);
265 }
266 EXPORT_SYMBOL_GPL(gmap_remove);
267
268 /**
269  * gmap_enable - switch primary space to the guest address space
270  * @gmap: pointer to the guest address space structure
271  */
272 void gmap_enable(struct gmap *gmap)
273 {
274         S390_lowcore.gmap = (unsigned long) gmap;
275 }
276 EXPORT_SYMBOL_GPL(gmap_enable);
277
278 /**
279  * gmap_disable - switch back to the standard primary address space
280  * @gmap: pointer to the guest address space structure
281  */
282 void gmap_disable(struct gmap *gmap)
283 {
284         S390_lowcore.gmap = 0UL;
285 }
286 EXPORT_SYMBOL_GPL(gmap_disable);
287
288 /**
289  * gmap_get_enabled - get a pointer to the currently enabled gmap
290  *
291  * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
292  */
293 struct gmap *gmap_get_enabled(void)
294 {
295         return (struct gmap *) S390_lowcore.gmap;
296 }
297 EXPORT_SYMBOL_GPL(gmap_get_enabled);
298
299 /*
300  * gmap_alloc_table is assumed to be called with mmap_sem held
301  */
302 static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
303                             unsigned long init, unsigned long gaddr)
304 {
305         struct page *page;
306         unsigned long *new;
307
308         /* since we dont free the gmap table until gmap_free we can unlock */
309         page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
310         if (!page)
311                 return -ENOMEM;
312         new = (unsigned long *) page_to_phys(page);
313         crst_table_init(new, init);
314         spin_lock(&gmap->guest_table_lock);
315         if (*table & _REGION_ENTRY_INVALID) {
316                 list_add(&page->lru, &gmap->crst_list);
317                 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
318                         (*table & _REGION_ENTRY_TYPE_MASK);
319                 page->index = gaddr;
320                 page = NULL;
321         }
322         spin_unlock(&gmap->guest_table_lock);
323         if (page)
324                 __free_pages(page, CRST_ALLOC_ORDER);
325         return 0;
326 }
327
328 /**
329  * __gmap_segment_gaddr - find virtual address from segment pointer
330  * @entry: pointer to a segment table entry in the guest address space
331  *
332  * Returns the virtual address in the guest address space for the segment
333  */
334 static unsigned long __gmap_segment_gaddr(unsigned long *entry)
335 {
336         struct page *page;
337         unsigned long offset, mask;
338
339         offset = (unsigned long) entry / sizeof(unsigned long);
340         offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
341         mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
342         page = virt_to_page((void *)((unsigned long) entry & mask));
343         return page->index + offset;
344 }
345
346 /**
347  * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
348  * @gmap: pointer to the guest address space structure
349  * @vmaddr: address in the host process address space
350  *
351  * Returns 1 if a TLB flush is required
352  */
353 static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
354 {
355         unsigned long *entry;
356         int flush = 0;
357
358         BUG_ON(gmap_is_shadow(gmap));
359         spin_lock(&gmap->guest_table_lock);
360         entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
361         if (entry) {
362                 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
363                 *entry = _SEGMENT_ENTRY_EMPTY;
364         }
365         spin_unlock(&gmap->guest_table_lock);
366         return flush;
367 }
368
369 /**
370  * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
371  * @gmap: pointer to the guest address space structure
372  * @gaddr: address in the guest address space
373  *
374  * Returns 1 if a TLB flush is required
375  */
376 static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
377 {
378         unsigned long vmaddr;
379
380         vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
381                                                    gaddr >> PMD_SHIFT);
382         return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
383 }
384
385 /**
386  * gmap_unmap_segment - unmap segment from the guest address space
387  * @gmap: pointer to the guest address space structure
388  * @to: address in the guest address space
389  * @len: length of the memory area to unmap
390  *
391  * Returns 0 if the unmap succeeded, -EINVAL if not.
392  */
393 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
394 {
395         unsigned long off;
396         int flush;
397
398         BUG_ON(gmap_is_shadow(gmap));
399         if ((to | len) & (PMD_SIZE - 1))
400                 return -EINVAL;
401         if (len == 0 || to + len < to)
402                 return -EINVAL;
403
404         flush = 0;
405         down_write(&gmap->mm->mmap_sem);
406         for (off = 0; off < len; off += PMD_SIZE)
407                 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
408         up_write(&gmap->mm->mmap_sem);
409         if (flush)
410                 gmap_flush_tlb(gmap);
411         return 0;
412 }
413 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
414
415 /**
416  * gmap_map_segment - map a segment to the guest address space
417  * @gmap: pointer to the guest address space structure
418  * @from: source address in the parent address space
419  * @to: target address in the guest address space
420  * @len: length of the memory area to map
421  *
422  * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
423  */
424 int gmap_map_segment(struct gmap *gmap, unsigned long from,
425                      unsigned long to, unsigned long len)
426 {
427         unsigned long off;
428         int flush;
429
430         BUG_ON(gmap_is_shadow(gmap));
431         if ((from | to | len) & (PMD_SIZE - 1))
432                 return -EINVAL;
433         if (len == 0 || from + len < from || to + len < to ||
434             from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
435                 return -EINVAL;
436
437         flush = 0;
438         down_write(&gmap->mm->mmap_sem);
439         for (off = 0; off < len; off += PMD_SIZE) {
440                 /* Remove old translation */
441                 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
442                 /* Store new translation */
443                 if (radix_tree_insert(&gmap->guest_to_host,
444                                       (to + off) >> PMD_SHIFT,
445                                       (void *) from + off))
446                         break;
447         }
448         up_write(&gmap->mm->mmap_sem);
449         if (flush)
450                 gmap_flush_tlb(gmap);
451         if (off >= len)
452                 return 0;
453         gmap_unmap_segment(gmap, to, len);
454         return -ENOMEM;
455 }
456 EXPORT_SYMBOL_GPL(gmap_map_segment);
457
458 /**
459  * __gmap_translate - translate a guest address to a user space address
460  * @gmap: pointer to guest mapping meta data structure
461  * @gaddr: guest address
462  *
463  * Returns user space address which corresponds to the guest address or
464  * -EFAULT if no such mapping exists.
465  * This function does not establish potentially missing page table entries.
466  * The mmap_sem of the mm that belongs to the address space must be held
467  * when this function gets called.
468  *
469  * Note: Can also be called for shadow gmaps.
470  */
471 unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
472 {
473         unsigned long vmaddr;
474
475         vmaddr = (unsigned long)
476                 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
477         /* Note: guest_to_host is empty for a shadow gmap */
478         return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
479 }
480 EXPORT_SYMBOL_GPL(__gmap_translate);
481
482 /**
483  * gmap_translate - translate a guest address to a user space address
484  * @gmap: pointer to guest mapping meta data structure
485  * @gaddr: guest address
486  *
487  * Returns user space address which corresponds to the guest address or
488  * -EFAULT if no such mapping exists.
489  * This function does not establish potentially missing page table entries.
490  */
491 unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
492 {
493         unsigned long rc;
494
495         down_read(&gmap->mm->mmap_sem);
496         rc = __gmap_translate(gmap, gaddr);
497         up_read(&gmap->mm->mmap_sem);
498         return rc;
499 }
500 EXPORT_SYMBOL_GPL(gmap_translate);
501
502 /**
503  * gmap_unlink - disconnect a page table from the gmap shadow tables
504  * @gmap: pointer to guest mapping meta data structure
505  * @table: pointer to the host page table
506  * @vmaddr: vm address associated with the host page table
507  */
508 void gmap_unlink(struct mm_struct *mm, unsigned long *table,
509                  unsigned long vmaddr)
510 {
511         struct gmap *gmap;
512         int flush;
513
514         rcu_read_lock();
515         list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
516                 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
517                 if (flush)
518                         gmap_flush_tlb(gmap);
519         }
520         rcu_read_unlock();
521 }
522
523 /**
524  * gmap_link - set up shadow page tables to connect a host to a guest address
525  * @gmap: pointer to guest mapping meta data structure
526  * @gaddr: guest address
527  * @vmaddr: vm address
528  *
529  * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
530  * if the vm address is already mapped to a different guest segment.
531  * The mmap_sem of the mm that belongs to the address space must be held
532  * when this function gets called.
533  */
534 int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
535 {
536         struct mm_struct *mm;
537         unsigned long *table;
538         spinlock_t *ptl;
539         pgd_t *pgd;
540         p4d_t *p4d;
541         pud_t *pud;
542         pmd_t *pmd;
543         int rc;
544
545         BUG_ON(gmap_is_shadow(gmap));
546         /* Create higher level tables in the gmap page table */
547         table = gmap->table;
548         if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
549                 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
550                 if ((*table & _REGION_ENTRY_INVALID) &&
551                     gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
552                                      gaddr & _REGION1_MASK))
553                         return -ENOMEM;
554                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
555         }
556         if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
557                 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
558                 if ((*table & _REGION_ENTRY_INVALID) &&
559                     gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
560                                      gaddr & _REGION2_MASK))
561                         return -ENOMEM;
562                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
563         }
564         if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
565                 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
566                 if ((*table & _REGION_ENTRY_INVALID) &&
567                     gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
568                                      gaddr & _REGION3_MASK))
569                         return -ENOMEM;
570                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
571         }
572         table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
573         /* Walk the parent mm page table */
574         mm = gmap->mm;
575         pgd = pgd_offset(mm, vmaddr);
576         VM_BUG_ON(pgd_none(*pgd));
577         p4d = p4d_offset(pgd, vmaddr);
578         VM_BUG_ON(p4d_none(*p4d));
579         pud = pud_offset(p4d, vmaddr);
580         VM_BUG_ON(pud_none(*pud));
581         /* large puds cannot yet be handled */
582         if (pud_large(*pud))
583                 return -EFAULT;
584         pmd = pmd_offset(pud, vmaddr);
585         VM_BUG_ON(pmd_none(*pmd));
586         /* large pmds cannot yet be handled */
587         if (pmd_large(*pmd))
588                 return -EFAULT;
589         /* Link gmap segment table entry location to page table. */
590         rc = radix_tree_preload(GFP_KERNEL);
591         if (rc)
592                 return rc;
593         ptl = pmd_lock(mm, pmd);
594         spin_lock(&gmap->guest_table_lock);
595         if (*table == _SEGMENT_ENTRY_EMPTY) {
596                 rc = radix_tree_insert(&gmap->host_to_guest,
597                                        vmaddr >> PMD_SHIFT, table);
598                 if (!rc)
599                         *table = pmd_val(*pmd);
600         } else
601                 rc = 0;
602         spin_unlock(&gmap->guest_table_lock);
603         spin_unlock(ptl);
604         radix_tree_preload_end();
605         return rc;
606 }
607
608 /**
609  * gmap_fault - resolve a fault on a guest address
610  * @gmap: pointer to guest mapping meta data structure
611  * @gaddr: guest address
612  * @fault_flags: flags to pass down to handle_mm_fault()
613  *
614  * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
615  * if the vm address is already mapped to a different guest segment.
616  */
617 int gmap_fault(struct gmap *gmap, unsigned long gaddr,
618                unsigned int fault_flags)
619 {
620         unsigned long vmaddr;
621         int rc;
622         bool unlocked;
623
624         down_read(&gmap->mm->mmap_sem);
625
626 retry:
627         unlocked = false;
628         vmaddr = __gmap_translate(gmap, gaddr);
629         if (IS_ERR_VALUE(vmaddr)) {
630                 rc = vmaddr;
631                 goto out_up;
632         }
633         if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags,
634                              &unlocked)) {
635                 rc = -EFAULT;
636                 goto out_up;
637         }
638         /*
639          * In the case that fixup_user_fault unlocked the mmap_sem during
640          * faultin redo __gmap_translate to not race with a map/unmap_segment.
641          */
642         if (unlocked)
643                 goto retry;
644
645         rc = __gmap_link(gmap, gaddr, vmaddr);
646 out_up:
647         up_read(&gmap->mm->mmap_sem);
648         return rc;
649 }
650 EXPORT_SYMBOL_GPL(gmap_fault);
651
652 /*
653  * this function is assumed to be called with mmap_sem held
654  */
655 void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
656 {
657         unsigned long vmaddr;
658         spinlock_t *ptl;
659         pte_t *ptep;
660
661         /* Find the vm address for the guest address */
662         vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
663                                                    gaddr >> PMD_SHIFT);
664         if (vmaddr) {
665                 vmaddr |= gaddr & ~PMD_MASK;
666                 /* Get pointer to the page table entry */
667                 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
668                 if (likely(ptep)) {
669                         ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
670                         pte_unmap_unlock(ptep, ptl);
671                 }
672         }
673 }
674 EXPORT_SYMBOL_GPL(__gmap_zap);
675
676 void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
677 {
678         unsigned long gaddr, vmaddr, size;
679         struct vm_area_struct *vma;
680
681         down_read(&gmap->mm->mmap_sem);
682         for (gaddr = from; gaddr < to;
683              gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
684                 /* Find the vm address for the guest address */
685                 vmaddr = (unsigned long)
686                         radix_tree_lookup(&gmap->guest_to_host,
687                                           gaddr >> PMD_SHIFT);
688                 if (!vmaddr)
689                         continue;
690                 vmaddr |= gaddr & ~PMD_MASK;
691                 /* Find vma in the parent mm */
692                 vma = find_vma(gmap->mm, vmaddr);
693                 if (!vma)
694                         continue;
695                 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
696                 zap_page_range(vma, vmaddr, size);
697         }
698         up_read(&gmap->mm->mmap_sem);
699 }
700 EXPORT_SYMBOL_GPL(gmap_discard);
701
702 static LIST_HEAD(gmap_notifier_list);
703 static DEFINE_SPINLOCK(gmap_notifier_lock);
704
705 /**
706  * gmap_register_pte_notifier - register a pte invalidation callback
707  * @nb: pointer to the gmap notifier block
708  */
709 void gmap_register_pte_notifier(struct gmap_notifier *nb)
710 {
711         spin_lock(&gmap_notifier_lock);
712         list_add_rcu(&nb->list, &gmap_notifier_list);
713         spin_unlock(&gmap_notifier_lock);
714 }
715 EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
716
717 /**
718  * gmap_unregister_pte_notifier - remove a pte invalidation callback
719  * @nb: pointer to the gmap notifier block
720  */
721 void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
722 {
723         spin_lock(&gmap_notifier_lock);
724         list_del_rcu(&nb->list);
725         spin_unlock(&gmap_notifier_lock);
726         synchronize_rcu();
727 }
728 EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
729
730 /**
731  * gmap_call_notifier - call all registered invalidation callbacks
732  * @gmap: pointer to guest mapping meta data structure
733  * @start: start virtual address in the guest address space
734  * @end: end virtual address in the guest address space
735  */
736 static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
737                                unsigned long end)
738 {
739         struct gmap_notifier *nb;
740
741         list_for_each_entry(nb, &gmap_notifier_list, list)
742                 nb->notifier_call(gmap, start, end);
743 }
744
745 /**
746  * gmap_table_walk - walk the gmap page tables
747  * @gmap: pointer to guest mapping meta data structure
748  * @gaddr: virtual address in the guest address space
749  * @level: page table level to stop at
750  *
751  * Returns a table entry pointer for the given guest address and @level
752  * @level=0 : returns a pointer to a page table table entry (or NULL)
753  * @level=1 : returns a pointer to a segment table entry (or NULL)
754  * @level=2 : returns a pointer to a region-3 table entry (or NULL)
755  * @level=3 : returns a pointer to a region-2 table entry (or NULL)
756  * @level=4 : returns a pointer to a region-1 table entry (or NULL)
757  *
758  * Returns NULL if the gmap page tables could not be walked to the
759  * requested level.
760  *
761  * Note: Can also be called for shadow gmaps.
762  */
763 static inline unsigned long *gmap_table_walk(struct gmap *gmap,
764                                              unsigned long gaddr, int level)
765 {
766         const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
767         unsigned long *table;
768
769         if ((gmap->asce & _ASCE_TYPE_MASK) + 4 < (level * 4))
770                 return NULL;
771         if (gmap_is_shadow(gmap) && gmap->removed)
772                 return NULL;
773
774         if (asce_type != _ASCE_TYPE_REGION1 &&
775             gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
776                 return NULL;
777
778         table = gmap->table;
779         switch (gmap->asce & _ASCE_TYPE_MASK) {
780         case _ASCE_TYPE_REGION1:
781                 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
782                 if (level == 4)
783                         break;
784                 if (*table & _REGION_ENTRY_INVALID)
785                         return NULL;
786                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
787                 /* Fallthrough */
788         case _ASCE_TYPE_REGION2:
789                 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
790                 if (level == 3)
791                         break;
792                 if (*table & _REGION_ENTRY_INVALID)
793                         return NULL;
794                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
795                 /* Fallthrough */
796         case _ASCE_TYPE_REGION3:
797                 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
798                 if (level == 2)
799                         break;
800                 if (*table & _REGION_ENTRY_INVALID)
801                         return NULL;
802                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
803                 /* Fallthrough */
804         case _ASCE_TYPE_SEGMENT:
805                 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
806                 if (level == 1)
807                         break;
808                 if (*table & _REGION_ENTRY_INVALID)
809                         return NULL;
810                 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
811                 table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
812         }
813         return table;
814 }
815
816 /**
817  * gmap_pte_op_walk - walk the gmap page table, get the page table lock
818  *                    and return the pte pointer
819  * @gmap: pointer to guest mapping meta data structure
820  * @gaddr: virtual address in the guest address space
821  * @ptl: pointer to the spinlock pointer
822  *
823  * Returns a pointer to the locked pte for a guest address, or NULL
824  *
825  * Note: Can also be called for shadow gmaps.
826  */
827 static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
828                                spinlock_t **ptl)
829 {
830         unsigned long *table;
831
832         if (gmap_is_shadow(gmap))
833                 spin_lock(&gmap->guest_table_lock);
834         /* Walk the gmap page table, lock and get pte pointer */
835         table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
836         if (!table || *table & _SEGMENT_ENTRY_INVALID) {
837                 if (gmap_is_shadow(gmap))
838                         spin_unlock(&gmap->guest_table_lock);
839                 return NULL;
840         }
841         if (gmap_is_shadow(gmap)) {
842                 *ptl = &gmap->guest_table_lock;
843                 return pte_offset_map((pmd_t *) table, gaddr);
844         }
845         return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
846 }
847
848 /**
849  * gmap_pte_op_fixup - force a page in and connect the gmap page table
850  * @gmap: pointer to guest mapping meta data structure
851  * @gaddr: virtual address in the guest address space
852  * @vmaddr: address in the host process address space
853  * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
854  *
855  * Returns 0 if the caller can retry __gmap_translate (might fail again),
856  * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
857  * up or connecting the gmap page table.
858  */
859 static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
860                              unsigned long vmaddr, int prot)
861 {
862         struct mm_struct *mm = gmap->mm;
863         unsigned int fault_flags;
864         bool unlocked = false;
865
866         BUG_ON(gmap_is_shadow(gmap));
867         fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
868         if (fixup_user_fault(current, mm, vmaddr, fault_flags, &unlocked))
869                 return -EFAULT;
870         if (unlocked)
871                 /* lost mmap_sem, caller has to retry __gmap_translate */
872                 return 0;
873         /* Connect the page tables */
874         return __gmap_link(gmap, gaddr, vmaddr);
875 }
876
877 /**
878  * gmap_pte_op_end - release the page table lock
879  * @ptl: pointer to the spinlock pointer
880  */
881 static void gmap_pte_op_end(spinlock_t *ptl)
882 {
883         spin_unlock(ptl);
884 }
885
886 /*
887  * gmap_protect_range - remove access rights to memory and set pgste bits
888  * @gmap: pointer to guest mapping meta data structure
889  * @gaddr: virtual address in the guest address space
890  * @len: size of area
891  * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
892  * @bits: pgste notification bits to set
893  *
894  * Returns 0 if successfully protected, -ENOMEM if out of memory and
895  * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
896  *
897  * Called with sg->mm->mmap_sem in read.
898  *
899  * Note: Can also be called for shadow gmaps.
900  */
901 static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
902                               unsigned long len, int prot, unsigned long bits)
903 {
904         unsigned long vmaddr;
905         spinlock_t *ptl;
906         pte_t *ptep;
907         int rc;
908
909         while (len) {
910                 rc = -EAGAIN;
911                 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
912                 if (ptep) {
913                         rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, bits);
914                         gmap_pte_op_end(ptl);
915                 }
916                 if (rc) {
917                         vmaddr = __gmap_translate(gmap, gaddr);
918                         if (IS_ERR_VALUE(vmaddr))
919                                 return vmaddr;
920                         rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
921                         if (rc)
922                                 return rc;
923                         continue;
924                 }
925                 gaddr += PAGE_SIZE;
926                 len -= PAGE_SIZE;
927         }
928         return 0;
929 }
930
931 /**
932  * gmap_mprotect_notify - change access rights for a range of ptes and
933  *                        call the notifier if any pte changes again
934  * @gmap: pointer to guest mapping meta data structure
935  * @gaddr: virtual address in the guest address space
936  * @len: size of area
937  * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
938  *
939  * Returns 0 if for each page in the given range a gmap mapping exists,
940  * the new access rights could be set and the notifier could be armed.
941  * If the gmap mapping is missing for one or more pages -EFAULT is
942  * returned. If no memory could be allocated -ENOMEM is returned.
943  * This function establishes missing page table entries.
944  */
945 int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
946                          unsigned long len, int prot)
947 {
948         int rc;
949
950         if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
951                 return -EINVAL;
952         if (!MACHINE_HAS_ESOP && prot == PROT_READ)
953                 return -EINVAL;
954         down_read(&gmap->mm->mmap_sem);
955         rc = gmap_protect_range(gmap, gaddr, len, prot, PGSTE_IN_BIT);
956         up_read(&gmap->mm->mmap_sem);
957         return rc;
958 }
959 EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
960
961 /**
962  * gmap_read_table - get an unsigned long value from a guest page table using
963  *                   absolute addressing, without marking the page referenced.
964  * @gmap: pointer to guest mapping meta data structure
965  * @gaddr: virtual address in the guest address space
966  * @val: pointer to the unsigned long value to return
967  *
968  * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
969  * if reading using the virtual address failed.
970  *
971  * Called with gmap->mm->mmap_sem in read.
972  */
973 int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
974 {
975         unsigned long address, vmaddr;
976         spinlock_t *ptl;
977         pte_t *ptep, pte;
978         int rc;
979
980         while (1) {
981                 rc = -EAGAIN;
982                 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
983                 if (ptep) {
984                         pte = *ptep;
985                         if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
986                                 address = pte_val(pte) & PAGE_MASK;
987                                 address += gaddr & ~PAGE_MASK;
988                                 *val = *(unsigned long *) address;
989                                 pte_val(*ptep) |= _PAGE_YOUNG;
990                                 /* Do *NOT* clear the _PAGE_INVALID bit! */
991                                 rc = 0;
992                         }
993                         gmap_pte_op_end(ptl);
994                 }
995                 if (!rc)
996                         break;
997                 vmaddr = __gmap_translate(gmap, gaddr);
998                 if (IS_ERR_VALUE(vmaddr)) {
999                         rc = vmaddr;
1000                         break;
1001                 }
1002                 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1003                 if (rc)
1004                         break;
1005         }
1006         return rc;
1007 }
1008 EXPORT_SYMBOL_GPL(gmap_read_table);
1009
1010 /**
1011  * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1012  * @sg: pointer to the shadow guest address space structure
1013  * @vmaddr: vm address associated with the rmap
1014  * @rmap: pointer to the rmap structure
1015  *
1016  * Called with the sg->guest_table_lock
1017  */
1018 static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1019                                     struct gmap_rmap *rmap)
1020 {
1021         void __rcu **slot;
1022
1023         BUG_ON(!gmap_is_shadow(sg));
1024         slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1025         if (slot) {
1026                 rmap->next = radix_tree_deref_slot_protected(slot,
1027                                                         &sg->guest_table_lock);
1028                 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1029         } else {
1030                 rmap->next = NULL;
1031                 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1032                                   rmap);
1033         }
1034 }
1035
1036 /**
1037  * gmap_protect_rmap - modify access rights to memory and create an rmap
1038  * @sg: pointer to the shadow guest address space structure
1039  * @raddr: rmap address in the shadow gmap
1040  * @paddr: address in the parent guest address space
1041  * @len: length of the memory area to protect
1042  * @prot: indicates access rights: none, read-only or read-write
1043  *
1044  * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1045  * if out of memory and -EFAULT if paddr is invalid.
1046  */
1047 static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1048                              unsigned long paddr, unsigned long len, int prot)
1049 {
1050         struct gmap *parent;
1051         struct gmap_rmap *rmap;
1052         unsigned long vmaddr;
1053         spinlock_t *ptl;
1054         pte_t *ptep;
1055         int rc;
1056
1057         BUG_ON(!gmap_is_shadow(sg));
1058         parent = sg->parent;
1059         while (len) {
1060                 vmaddr = __gmap_translate(parent, paddr);
1061                 if (IS_ERR_VALUE(vmaddr))
1062                         return vmaddr;
1063                 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
1064                 if (!rmap)
1065                         return -ENOMEM;
1066                 rmap->raddr = raddr;
1067                 rc = radix_tree_preload(GFP_KERNEL);
1068                 if (rc) {
1069                         kfree(rmap);
1070                         return rc;
1071                 }
1072                 rc = -EAGAIN;
1073                 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1074                 if (ptep) {
1075                         spin_lock(&sg->guest_table_lock);
1076                         rc = ptep_force_prot(parent->mm, paddr, ptep, prot,
1077                                              PGSTE_VSIE_BIT);
1078                         if (!rc)
1079                                 gmap_insert_rmap(sg, vmaddr, rmap);
1080                         spin_unlock(&sg->guest_table_lock);
1081                         gmap_pte_op_end(ptl);
1082                 }
1083                 radix_tree_preload_end();
1084                 if (rc) {
1085                         kfree(rmap);
1086                         rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
1087                         if (rc)
1088                                 return rc;
1089                         continue;
1090                 }
1091                 paddr += PAGE_SIZE;
1092                 len -= PAGE_SIZE;
1093         }
1094         return 0;
1095 }
1096
1097 #define _SHADOW_RMAP_MASK       0x7
1098 #define _SHADOW_RMAP_REGION1    0x5
1099 #define _SHADOW_RMAP_REGION2    0x4
1100 #define _SHADOW_RMAP_REGION3    0x3
1101 #define _SHADOW_RMAP_SEGMENT    0x2
1102 #define _SHADOW_RMAP_PGTABLE    0x1
1103
1104 /**
1105  * gmap_idte_one - invalidate a single region or segment table entry
1106  * @asce: region or segment table *origin* + table-type bits
1107  * @vaddr: virtual address to identify the table entry to flush
1108  *
1109  * The invalid bit of a single region or segment table entry is set
1110  * and the associated TLB entries depending on the entry are flushed.
1111  * The table-type of the @asce identifies the portion of the @vaddr
1112  * that is used as the invalidation index.
1113  */
1114 static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1115 {
1116         asm volatile(
1117                 "       .insn   rrf,0xb98e0000,%0,%1,0,0"
1118                 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1119 }
1120
1121 /**
1122  * gmap_unshadow_page - remove a page from a shadow page table
1123  * @sg: pointer to the shadow guest address space structure
1124  * @raddr: rmap address in the shadow guest address space
1125  *
1126  * Called with the sg->guest_table_lock
1127  */
1128 static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1129 {
1130         unsigned long *table;
1131
1132         BUG_ON(!gmap_is_shadow(sg));
1133         table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1134         if (!table || *table & _PAGE_INVALID)
1135                 return;
1136         gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
1137         ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1138 }
1139
1140 /**
1141  * __gmap_unshadow_pgt - remove all entries from a shadow page table
1142  * @sg: pointer to the shadow guest address space structure
1143  * @raddr: rmap address in the shadow guest address space
1144  * @pgt: pointer to the start of a shadow page table
1145  *
1146  * Called with the sg->guest_table_lock
1147  */
1148 static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1149                                 unsigned long *pgt)
1150 {
1151         int i;
1152
1153         BUG_ON(!gmap_is_shadow(sg));
1154         for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
1155                 pgt[i] = _PAGE_INVALID;
1156 }
1157
1158 /**
1159  * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1160  * @sg: pointer to the shadow guest address space structure
1161  * @raddr: address in the shadow guest address space
1162  *
1163  * Called with the sg->guest_table_lock
1164  */
1165 static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1166 {
1167         unsigned long sto, *ste, *pgt;
1168         struct page *page;
1169
1170         BUG_ON(!gmap_is_shadow(sg));
1171         ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1172         if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1173                 return;
1174         gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1175         sto = (unsigned long) (ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1176         gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1177         pgt = (unsigned long *)(*ste & _SEGMENT_ENTRY_ORIGIN);
1178         *ste = _SEGMENT_ENTRY_EMPTY;
1179         __gmap_unshadow_pgt(sg, raddr, pgt);
1180         /* Free page table */
1181         page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1182         list_del(&page->lru);
1183         page_table_free_pgste(page);
1184 }
1185
1186 /**
1187  * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1188  * @sg: pointer to the shadow guest address space structure
1189  * @raddr: rmap address in the shadow guest address space
1190  * @sgt: pointer to the start of a shadow segment table
1191  *
1192  * Called with the sg->guest_table_lock
1193  */
1194 static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1195                                 unsigned long *sgt)
1196 {
1197         unsigned long asce, *pgt;
1198         struct page *page;
1199         int i;
1200
1201         BUG_ON(!gmap_is_shadow(sg));
1202         asce = (unsigned long) sgt | _ASCE_TYPE_SEGMENT;
1203         for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
1204                 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1205                         continue;
1206                 pgt = (unsigned long *)(sgt[i] & _REGION_ENTRY_ORIGIN);
1207                 sgt[i] = _SEGMENT_ENTRY_EMPTY;
1208                 __gmap_unshadow_pgt(sg, raddr, pgt);
1209                 /* Free page table */
1210                 page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1211                 list_del(&page->lru);
1212                 page_table_free_pgste(page);
1213         }
1214 }
1215
1216 /**
1217  * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1218  * @sg: pointer to the shadow guest address space structure
1219  * @raddr: rmap address in the shadow guest address space
1220  *
1221  * Called with the shadow->guest_table_lock
1222  */
1223 static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1224 {
1225         unsigned long r3o, *r3e, *sgt;
1226         struct page *page;
1227
1228         BUG_ON(!gmap_is_shadow(sg));
1229         r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1230         if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1231                 return;
1232         gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1233         r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1234         gmap_idte_one(r3o | _ASCE_TYPE_REGION3, raddr);
1235         sgt = (unsigned long *)(*r3e & _REGION_ENTRY_ORIGIN);
1236         *r3e = _REGION3_ENTRY_EMPTY;
1237         __gmap_unshadow_sgt(sg, raddr, sgt);
1238         /* Free segment table */
1239         page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1240         list_del(&page->lru);
1241         __free_pages(page, CRST_ALLOC_ORDER);
1242 }
1243
1244 /**
1245  * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1246  * @sg: pointer to the shadow guest address space structure
1247  * @raddr: address in the shadow guest address space
1248  * @r3t: pointer to the start of a shadow region-3 table
1249  *
1250  * Called with the sg->guest_table_lock
1251  */
1252 static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1253                                 unsigned long *r3t)
1254 {
1255         unsigned long asce, *sgt;
1256         struct page *page;
1257         int i;
1258
1259         BUG_ON(!gmap_is_shadow(sg));
1260         asce = (unsigned long) r3t | _ASCE_TYPE_REGION3;
1261         for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
1262                 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1263                         continue;
1264                 sgt = (unsigned long *)(r3t[i] & _REGION_ENTRY_ORIGIN);
1265                 r3t[i] = _REGION3_ENTRY_EMPTY;
1266                 __gmap_unshadow_sgt(sg, raddr, sgt);
1267                 /* Free segment table */
1268                 page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1269                 list_del(&page->lru);
1270                 __free_pages(page, CRST_ALLOC_ORDER);
1271         }
1272 }
1273
1274 /**
1275  * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1276  * @sg: pointer to the shadow guest address space structure
1277  * @raddr: rmap address in the shadow guest address space
1278  *
1279  * Called with the sg->guest_table_lock
1280  */
1281 static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1282 {
1283         unsigned long r2o, *r2e, *r3t;
1284         struct page *page;
1285
1286         BUG_ON(!gmap_is_shadow(sg));
1287         r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1288         if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1289                 return;
1290         gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1291         r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1292         gmap_idte_one(r2o | _ASCE_TYPE_REGION2, raddr);
1293         r3t = (unsigned long *)(*r2e & _REGION_ENTRY_ORIGIN);
1294         *r2e = _REGION2_ENTRY_EMPTY;
1295         __gmap_unshadow_r3t(sg, raddr, r3t);
1296         /* Free region 3 table */
1297         page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1298         list_del(&page->lru);
1299         __free_pages(page, CRST_ALLOC_ORDER);
1300 }
1301
1302 /**
1303  * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1304  * @sg: pointer to the shadow guest address space structure
1305  * @raddr: rmap address in the shadow guest address space
1306  * @r2t: pointer to the start of a shadow region-2 table
1307  *
1308  * Called with the sg->guest_table_lock
1309  */
1310 static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1311                                 unsigned long *r2t)
1312 {
1313         unsigned long asce, *r3t;
1314         struct page *page;
1315         int i;
1316
1317         BUG_ON(!gmap_is_shadow(sg));
1318         asce = (unsigned long) r2t | _ASCE_TYPE_REGION2;
1319         for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
1320                 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1321                         continue;
1322                 r3t = (unsigned long *)(r2t[i] & _REGION_ENTRY_ORIGIN);
1323                 r2t[i] = _REGION2_ENTRY_EMPTY;
1324                 __gmap_unshadow_r3t(sg, raddr, r3t);
1325                 /* Free region 3 table */
1326                 page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1327                 list_del(&page->lru);
1328                 __free_pages(page, CRST_ALLOC_ORDER);
1329         }
1330 }
1331
1332 /**
1333  * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1334  * @sg: pointer to the shadow guest address space structure
1335  * @raddr: rmap address in the shadow guest address space
1336  *
1337  * Called with the sg->guest_table_lock
1338  */
1339 static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1340 {
1341         unsigned long r1o, *r1e, *r2t;
1342         struct page *page;
1343
1344         BUG_ON(!gmap_is_shadow(sg));
1345         r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1346         if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1347                 return;
1348         gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1349         r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1350         gmap_idte_one(r1o | _ASCE_TYPE_REGION1, raddr);
1351         r2t = (unsigned long *)(*r1e & _REGION_ENTRY_ORIGIN);
1352         *r1e = _REGION1_ENTRY_EMPTY;
1353         __gmap_unshadow_r2t(sg, raddr, r2t);
1354         /* Free region 2 table */
1355         page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1356         list_del(&page->lru);
1357         __free_pages(page, CRST_ALLOC_ORDER);
1358 }
1359
1360 /**
1361  * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1362  * @sg: pointer to the shadow guest address space structure
1363  * @raddr: rmap address in the shadow guest address space
1364  * @r1t: pointer to the start of a shadow region-1 table
1365  *
1366  * Called with the shadow->guest_table_lock
1367  */
1368 static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1369                                 unsigned long *r1t)
1370 {
1371         unsigned long asce, *r2t;
1372         struct page *page;
1373         int i;
1374
1375         BUG_ON(!gmap_is_shadow(sg));
1376         asce = (unsigned long) r1t | _ASCE_TYPE_REGION1;
1377         for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1378                 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1379                         continue;
1380                 r2t = (unsigned long *)(r1t[i] & _REGION_ENTRY_ORIGIN);
1381                 __gmap_unshadow_r2t(sg, raddr, r2t);
1382                 /* Clear entry and flush translation r1t -> r2t */
1383                 gmap_idte_one(asce, raddr);
1384                 r1t[i] = _REGION1_ENTRY_EMPTY;
1385                 /* Free region 2 table */
1386                 page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1387                 list_del(&page->lru);
1388                 __free_pages(page, CRST_ALLOC_ORDER);
1389         }
1390 }
1391
1392 /**
1393  * gmap_unshadow - remove a shadow page table completely
1394  * @sg: pointer to the shadow guest address space structure
1395  *
1396  * Called with sg->guest_table_lock
1397  */
1398 static void gmap_unshadow(struct gmap *sg)
1399 {
1400         unsigned long *table;
1401
1402         BUG_ON(!gmap_is_shadow(sg));
1403         if (sg->removed)
1404                 return;
1405         sg->removed = 1;
1406         gmap_call_notifier(sg, 0, -1UL);
1407         gmap_flush_tlb(sg);
1408         table = (unsigned long *)(sg->asce & _ASCE_ORIGIN);
1409         switch (sg->asce & _ASCE_TYPE_MASK) {
1410         case _ASCE_TYPE_REGION1:
1411                 __gmap_unshadow_r1t(sg, 0, table);
1412                 break;
1413         case _ASCE_TYPE_REGION2:
1414                 __gmap_unshadow_r2t(sg, 0, table);
1415                 break;
1416         case _ASCE_TYPE_REGION3:
1417                 __gmap_unshadow_r3t(sg, 0, table);
1418                 break;
1419         case _ASCE_TYPE_SEGMENT:
1420                 __gmap_unshadow_sgt(sg, 0, table);
1421                 break;
1422         }
1423 }
1424
1425 /**
1426  * gmap_find_shadow - find a specific asce in the list of shadow tables
1427  * @parent: pointer to the parent gmap
1428  * @asce: ASCE for which the shadow table is created
1429  * @edat_level: edat level to be used for the shadow translation
1430  *
1431  * Returns the pointer to a gmap if a shadow table with the given asce is
1432  * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1433  * otherwise NULL
1434  */
1435 static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1436                                      int edat_level)
1437 {
1438         struct gmap *sg;
1439
1440         list_for_each_entry(sg, &parent->children, list) {
1441                 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1442                     sg->removed)
1443                         continue;
1444                 if (!sg->initialized)
1445                         return ERR_PTR(-EAGAIN);
1446                 atomic_inc(&sg->ref_count);
1447                 return sg;
1448         }
1449         return NULL;
1450 }
1451
1452 /**
1453  * gmap_shadow_valid - check if a shadow guest address space matches the
1454  *                     given properties and is still valid
1455  * @sg: pointer to the shadow guest address space structure
1456  * @asce: ASCE for which the shadow table is requested
1457  * @edat_level: edat level to be used for the shadow translation
1458  *
1459  * Returns 1 if the gmap shadow is still valid and matches the given
1460  * properties, the caller can continue using it. Returns 0 otherwise, the
1461  * caller has to request a new shadow gmap in this case.
1462  *
1463  */
1464 int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1465 {
1466         if (sg->removed)
1467                 return 0;
1468         return sg->orig_asce == asce && sg->edat_level == edat_level;
1469 }
1470 EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1471
1472 /**
1473  * gmap_shadow - create/find a shadow guest address space
1474  * @parent: pointer to the parent gmap
1475  * @asce: ASCE for which the shadow table is created
1476  * @edat_level: edat level to be used for the shadow translation
1477  *
1478  * The pages of the top level page table referred by the asce parameter
1479  * will be set to read-only and marked in the PGSTEs of the kvm process.
1480  * The shadow table will be removed automatically on any change to the
1481  * PTE mapping for the source table.
1482  *
1483  * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1484  * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1485  * parent gmap table could not be protected.
1486  */
1487 struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1488                          int edat_level)
1489 {
1490         struct gmap *sg, *new;
1491         unsigned long limit;
1492         int rc;
1493
1494         BUG_ON(gmap_is_shadow(parent));
1495         spin_lock(&parent->shadow_lock);
1496         sg = gmap_find_shadow(parent, asce, edat_level);
1497         spin_unlock(&parent->shadow_lock);
1498         if (sg)
1499                 return sg;
1500         /* Create a new shadow gmap */
1501         limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1502         if (asce & _ASCE_REAL_SPACE)
1503                 limit = -1UL;
1504         new = gmap_alloc(limit);
1505         if (!new)
1506                 return ERR_PTR(-ENOMEM);
1507         new->mm = parent->mm;
1508         new->parent = gmap_get(parent);
1509         new->orig_asce = asce;
1510         new->edat_level = edat_level;
1511         new->initialized = false;
1512         spin_lock(&parent->shadow_lock);
1513         /* Recheck if another CPU created the same shadow */
1514         sg = gmap_find_shadow(parent, asce, edat_level);
1515         if (sg) {
1516                 spin_unlock(&parent->shadow_lock);
1517                 gmap_free(new);
1518                 return sg;
1519         }
1520         if (asce & _ASCE_REAL_SPACE) {
1521                 /* only allow one real-space gmap shadow */
1522                 list_for_each_entry(sg, &parent->children, list) {
1523                         if (sg->orig_asce & _ASCE_REAL_SPACE) {
1524                                 spin_lock(&sg->guest_table_lock);
1525                                 gmap_unshadow(sg);
1526                                 spin_unlock(&sg->guest_table_lock);
1527                                 list_del(&sg->list);
1528                                 gmap_put(sg);
1529                                 break;
1530                         }
1531                 }
1532         }
1533         atomic_set(&new->ref_count, 2);
1534         list_add(&new->list, &parent->children);
1535         if (asce & _ASCE_REAL_SPACE) {
1536                 /* nothing to protect, return right away */
1537                 new->initialized = true;
1538                 spin_unlock(&parent->shadow_lock);
1539                 return new;
1540         }
1541         spin_unlock(&parent->shadow_lock);
1542         /* protect after insertion, so it will get properly invalidated */
1543         down_read(&parent->mm->mmap_sem);
1544         rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1545                                 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1546                                 PROT_READ, PGSTE_VSIE_BIT);
1547         up_read(&parent->mm->mmap_sem);
1548         spin_lock(&parent->shadow_lock);
1549         new->initialized = true;
1550         if (rc) {
1551                 list_del(&new->list);
1552                 gmap_free(new);
1553                 new = ERR_PTR(rc);
1554         }
1555         spin_unlock(&parent->shadow_lock);
1556         return new;
1557 }
1558 EXPORT_SYMBOL_GPL(gmap_shadow);
1559
1560 /**
1561  * gmap_shadow_r2t - create an empty shadow region 2 table
1562  * @sg: pointer to the shadow guest address space structure
1563  * @saddr: faulting address in the shadow gmap
1564  * @r2t: parent gmap address of the region 2 table to get shadowed
1565  * @fake: r2t references contiguous guest memory block, not a r2t
1566  *
1567  * The r2t parameter specifies the address of the source table. The
1568  * four pages of the source table are made read-only in the parent gmap
1569  * address space. A write to the source table area @r2t will automatically
1570  * remove the shadow r2 table and all of its decendents.
1571  *
1572  * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1573  * shadow table structure is incomplete, -ENOMEM if out of memory and
1574  * -EFAULT if an address in the parent gmap could not be resolved.
1575  *
1576  * Called with sg->mm->mmap_sem in read.
1577  */
1578 int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1579                     int fake)
1580 {
1581         unsigned long raddr, origin, offset, len;
1582         unsigned long *s_r2t, *table;
1583         struct page *page;
1584         int rc;
1585
1586         BUG_ON(!gmap_is_shadow(sg));
1587         /* Allocate a shadow region second table */
1588         page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1589         if (!page)
1590                 return -ENOMEM;
1591         page->index = r2t & _REGION_ENTRY_ORIGIN;
1592         if (fake)
1593                 page->index |= GMAP_SHADOW_FAKE_TABLE;
1594         s_r2t = (unsigned long *) page_to_phys(page);
1595         /* Install shadow region second table */
1596         spin_lock(&sg->guest_table_lock);
1597         table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1598         if (!table) {
1599                 rc = -EAGAIN;           /* Race with unshadow */
1600                 goto out_free;
1601         }
1602         if (!(*table & _REGION_ENTRY_INVALID)) {
1603                 rc = 0;                 /* Already established */
1604                 goto out_free;
1605         } else if (*table & _REGION_ENTRY_ORIGIN) {
1606                 rc = -EAGAIN;           /* Race with shadow */
1607                 goto out_free;
1608         }
1609         crst_table_init(s_r2t, _REGION2_ENTRY_EMPTY);
1610         /* mark as invalid as long as the parent table is not protected */
1611         *table = (unsigned long) s_r2t | _REGION_ENTRY_LENGTH |
1612                  _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1613         if (sg->edat_level >= 1)
1614                 *table |= (r2t & _REGION_ENTRY_PROTECT);
1615         list_add(&page->lru, &sg->crst_list);
1616         if (fake) {
1617                 /* nothing to protect for fake tables */
1618                 *table &= ~_REGION_ENTRY_INVALID;
1619                 spin_unlock(&sg->guest_table_lock);
1620                 return 0;
1621         }
1622         spin_unlock(&sg->guest_table_lock);
1623         /* Make r2t read-only in parent gmap page table */
1624         raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1625         origin = r2t & _REGION_ENTRY_ORIGIN;
1626         offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1627         len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1628         rc = gmap_protect_rmap(sg, raddr, origin + offset, len, PROT_READ);
1629         spin_lock(&sg->guest_table_lock);
1630         if (!rc) {
1631                 table = gmap_table_walk(sg, saddr, 4);
1632                 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1633                               (unsigned long) s_r2t)
1634                         rc = -EAGAIN;           /* Race with unshadow */
1635                 else
1636                         *table &= ~_REGION_ENTRY_INVALID;
1637         } else {
1638                 gmap_unshadow_r2t(sg, raddr);
1639         }
1640         spin_unlock(&sg->guest_table_lock);
1641         return rc;
1642 out_free:
1643         spin_unlock(&sg->guest_table_lock);
1644         __free_pages(page, CRST_ALLOC_ORDER);
1645         return rc;
1646 }
1647 EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1648
1649 /**
1650  * gmap_shadow_r3t - create a shadow region 3 table
1651  * @sg: pointer to the shadow guest address space structure
1652  * @saddr: faulting address in the shadow gmap
1653  * @r3t: parent gmap address of the region 3 table to get shadowed
1654  * @fake: r3t references contiguous guest memory block, not a r3t
1655  *
1656  * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1657  * shadow table structure is incomplete, -ENOMEM if out of memory and
1658  * -EFAULT if an address in the parent gmap could not be resolved.
1659  *
1660  * Called with sg->mm->mmap_sem in read.
1661  */
1662 int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1663                     int fake)
1664 {
1665         unsigned long raddr, origin, offset, len;
1666         unsigned long *s_r3t, *table;
1667         struct page *page;
1668         int rc;
1669
1670         BUG_ON(!gmap_is_shadow(sg));
1671         /* Allocate a shadow region second table */
1672         page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1673         if (!page)
1674                 return -ENOMEM;
1675         page->index = r3t & _REGION_ENTRY_ORIGIN;
1676         if (fake)
1677                 page->index |= GMAP_SHADOW_FAKE_TABLE;
1678         s_r3t = (unsigned long *) page_to_phys(page);
1679         /* Install shadow region second table */
1680         spin_lock(&sg->guest_table_lock);
1681         table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1682         if (!table) {
1683                 rc = -EAGAIN;           /* Race with unshadow */
1684                 goto out_free;
1685         }
1686         if (!(*table & _REGION_ENTRY_INVALID)) {
1687                 rc = 0;                 /* Already established */
1688                 goto out_free;
1689         } else if (*table & _REGION_ENTRY_ORIGIN) {
1690                 rc = -EAGAIN;           /* Race with shadow */
1691                 goto out_free;
1692         }
1693         crst_table_init(s_r3t, _REGION3_ENTRY_EMPTY);
1694         /* mark as invalid as long as the parent table is not protected */
1695         *table = (unsigned long) s_r3t | _REGION_ENTRY_LENGTH |
1696                  _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1697         if (sg->edat_level >= 1)
1698                 *table |= (r3t & _REGION_ENTRY_PROTECT);
1699         list_add(&page->lru, &sg->crst_list);
1700         if (fake) {
1701                 /* nothing to protect for fake tables */
1702                 *table &= ~_REGION_ENTRY_INVALID;
1703                 spin_unlock(&sg->guest_table_lock);
1704                 return 0;
1705         }
1706         spin_unlock(&sg->guest_table_lock);
1707         /* Make r3t read-only in parent gmap page table */
1708         raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1709         origin = r3t & _REGION_ENTRY_ORIGIN;
1710         offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1711         len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1712         rc = gmap_protect_rmap(sg, raddr, origin + offset, len, PROT_READ);
1713         spin_lock(&sg->guest_table_lock);
1714         if (!rc) {
1715                 table = gmap_table_walk(sg, saddr, 3);
1716                 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1717                               (unsigned long) s_r3t)
1718                         rc = -EAGAIN;           /* Race with unshadow */
1719                 else
1720                         *table &= ~_REGION_ENTRY_INVALID;
1721         } else {
1722                 gmap_unshadow_r3t(sg, raddr);
1723         }
1724         spin_unlock(&sg->guest_table_lock);
1725         return rc;
1726 out_free:
1727         spin_unlock(&sg->guest_table_lock);
1728         __free_pages(page, CRST_ALLOC_ORDER);
1729         return rc;
1730 }
1731 EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1732
1733 /**
1734  * gmap_shadow_sgt - create a shadow segment table
1735  * @sg: pointer to the shadow guest address space structure
1736  * @saddr: faulting address in the shadow gmap
1737  * @sgt: parent gmap address of the segment table to get shadowed
1738  * @fake: sgt references contiguous guest memory block, not a sgt
1739  *
1740  * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1741  * shadow table structure is incomplete, -ENOMEM if out of memory and
1742  * -EFAULT if an address in the parent gmap could not be resolved.
1743  *
1744  * Called with sg->mm->mmap_sem in read.
1745  */
1746 int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1747                     int fake)
1748 {
1749         unsigned long raddr, origin, offset, len;
1750         unsigned long *s_sgt, *table;
1751         struct page *page;
1752         int rc;
1753
1754         BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1755         /* Allocate a shadow segment table */
1756         page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1757         if (!page)
1758                 return -ENOMEM;
1759         page->index = sgt & _REGION_ENTRY_ORIGIN;
1760         if (fake)
1761                 page->index |= GMAP_SHADOW_FAKE_TABLE;
1762         s_sgt = (unsigned long *) page_to_phys(page);
1763         /* Install shadow region second table */
1764         spin_lock(&sg->guest_table_lock);
1765         table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1766         if (!table) {
1767                 rc = -EAGAIN;           /* Race with unshadow */
1768                 goto out_free;
1769         }
1770         if (!(*table & _REGION_ENTRY_INVALID)) {
1771                 rc = 0;                 /* Already established */
1772                 goto out_free;
1773         } else if (*table & _REGION_ENTRY_ORIGIN) {
1774                 rc = -EAGAIN;           /* Race with shadow */
1775                 goto out_free;
1776         }
1777         crst_table_init(s_sgt, _SEGMENT_ENTRY_EMPTY);
1778         /* mark as invalid as long as the parent table is not protected */
1779         *table = (unsigned long) s_sgt | _REGION_ENTRY_LENGTH |
1780                  _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1781         if (sg->edat_level >= 1)
1782                 *table |= sgt & _REGION_ENTRY_PROTECT;
1783         list_add(&page->lru, &sg->crst_list);
1784         if (fake) {
1785                 /* nothing to protect for fake tables */
1786                 *table &= ~_REGION_ENTRY_INVALID;
1787                 spin_unlock(&sg->guest_table_lock);
1788                 return 0;
1789         }
1790         spin_unlock(&sg->guest_table_lock);
1791         /* Make sgt read-only in parent gmap page table */
1792         raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
1793         origin = sgt & _REGION_ENTRY_ORIGIN;
1794         offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1795         len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1796         rc = gmap_protect_rmap(sg, raddr, origin + offset, len, PROT_READ);
1797         spin_lock(&sg->guest_table_lock);
1798         if (!rc) {
1799                 table = gmap_table_walk(sg, saddr, 2);
1800                 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1801                               (unsigned long) s_sgt)
1802                         rc = -EAGAIN;           /* Race with unshadow */
1803                 else
1804                         *table &= ~_REGION_ENTRY_INVALID;
1805         } else {
1806                 gmap_unshadow_sgt(sg, raddr);
1807         }
1808         spin_unlock(&sg->guest_table_lock);
1809         return rc;
1810 out_free:
1811         spin_unlock(&sg->guest_table_lock);
1812         __free_pages(page, CRST_ALLOC_ORDER);
1813         return rc;
1814 }
1815 EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
1816
1817 /**
1818  * gmap_shadow_lookup_pgtable - find a shadow page table
1819  * @sg: pointer to the shadow guest address space structure
1820  * @saddr: the address in the shadow aguest address space
1821  * @pgt: parent gmap address of the page table to get shadowed
1822  * @dat_protection: if the pgtable is marked as protected by dat
1823  * @fake: pgt references contiguous guest memory block, not a pgtable
1824  *
1825  * Returns 0 if the shadow page table was found and -EAGAIN if the page
1826  * table was not found.
1827  *
1828  * Called with sg->mm->mmap_sem in read.
1829  */
1830 int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
1831                            unsigned long *pgt, int *dat_protection,
1832                            int *fake)
1833 {
1834         unsigned long *table;
1835         struct page *page;
1836         int rc;
1837
1838         BUG_ON(!gmap_is_shadow(sg));
1839         spin_lock(&sg->guest_table_lock);
1840         table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
1841         if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
1842                 /* Shadow page tables are full pages (pte+pgste) */
1843                 page = pfn_to_page(*table >> PAGE_SHIFT);
1844                 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
1845                 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
1846                 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
1847                 rc = 0;
1848         } else  {
1849                 rc = -EAGAIN;
1850         }
1851         spin_unlock(&sg->guest_table_lock);
1852         return rc;
1853
1854 }
1855 EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
1856
1857 /**
1858  * gmap_shadow_pgt - instantiate a shadow page table
1859  * @sg: pointer to the shadow guest address space structure
1860  * @saddr: faulting address in the shadow gmap
1861  * @pgt: parent gmap address of the page table to get shadowed
1862  * @fake: pgt references contiguous guest memory block, not a pgtable
1863  *
1864  * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1865  * shadow table structure is incomplete, -ENOMEM if out of memory,
1866  * -EFAULT if an address in the parent gmap could not be resolved and
1867  *
1868  * Called with gmap->mm->mmap_sem in read
1869  */
1870 int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
1871                     int fake)
1872 {
1873         unsigned long raddr, origin;
1874         unsigned long *s_pgt, *table;
1875         struct page *page;
1876         int rc;
1877
1878         BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
1879         /* Allocate a shadow page table */
1880         page = page_table_alloc_pgste(sg->mm);
1881         if (!page)
1882                 return -ENOMEM;
1883         page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
1884         if (fake)
1885                 page->index |= GMAP_SHADOW_FAKE_TABLE;
1886         s_pgt = (unsigned long *) page_to_phys(page);
1887         /* Install shadow page table */
1888         spin_lock(&sg->guest_table_lock);
1889         table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
1890         if (!table) {
1891                 rc = -EAGAIN;           /* Race with unshadow */
1892                 goto out_free;
1893         }
1894         if (!(*table & _SEGMENT_ENTRY_INVALID)) {
1895                 rc = 0;                 /* Already established */
1896                 goto out_free;
1897         } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
1898                 rc = -EAGAIN;           /* Race with shadow */
1899                 goto out_free;
1900         }
1901         /* mark as invalid as long as the parent table is not protected */
1902         *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
1903                  (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
1904         list_add(&page->lru, &sg->pt_list);
1905         if (fake) {
1906                 /* nothing to protect for fake tables */
1907                 *table &= ~_SEGMENT_ENTRY_INVALID;
1908                 spin_unlock(&sg->guest_table_lock);
1909                 return 0;
1910         }
1911         spin_unlock(&sg->guest_table_lock);
1912         /* Make pgt read-only in parent gmap page table (not the pgste) */
1913         raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
1914         origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
1915         rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE, PROT_READ);
1916         spin_lock(&sg->guest_table_lock);
1917         if (!rc) {
1918                 table = gmap_table_walk(sg, saddr, 1);
1919                 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) !=
1920                               (unsigned long) s_pgt)
1921                         rc = -EAGAIN;           /* Race with unshadow */
1922                 else
1923                         *table &= ~_SEGMENT_ENTRY_INVALID;
1924         } else {
1925                 gmap_unshadow_pgt(sg, raddr);
1926         }
1927         spin_unlock(&sg->guest_table_lock);
1928         return rc;
1929 out_free:
1930         spin_unlock(&sg->guest_table_lock);
1931         page_table_free_pgste(page);
1932         return rc;
1933
1934 }
1935 EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
1936
1937 /**
1938  * gmap_shadow_page - create a shadow page mapping
1939  * @sg: pointer to the shadow guest address space structure
1940  * @saddr: faulting address in the shadow gmap
1941  * @pte: pte in parent gmap address space to get shadowed
1942  *
1943  * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1944  * shadow table structure is incomplete, -ENOMEM if out of memory and
1945  * -EFAULT if an address in the parent gmap could not be resolved.
1946  *
1947  * Called with sg->mm->mmap_sem in read.
1948  */
1949 int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
1950 {
1951         struct gmap *parent;
1952         struct gmap_rmap *rmap;
1953         unsigned long vmaddr, paddr;
1954         spinlock_t *ptl;
1955         pte_t *sptep, *tptep;
1956         int prot;
1957         int rc;
1958
1959         BUG_ON(!gmap_is_shadow(sg));
1960         parent = sg->parent;
1961         prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
1962
1963         rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
1964         if (!rmap)
1965                 return -ENOMEM;
1966         rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
1967
1968         while (1) {
1969                 paddr = pte_val(pte) & PAGE_MASK;
1970                 vmaddr = __gmap_translate(parent, paddr);
1971                 if (IS_ERR_VALUE(vmaddr)) {
1972                         rc = vmaddr;
1973                         break;
1974                 }
1975                 rc = radix_tree_preload(GFP_KERNEL);
1976                 if (rc)
1977                         break;
1978                 rc = -EAGAIN;
1979                 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
1980                 if (sptep) {
1981                         spin_lock(&sg->guest_table_lock);
1982                         /* Get page table pointer */
1983                         tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
1984                         if (!tptep) {
1985                                 spin_unlock(&sg->guest_table_lock);
1986                                 gmap_pte_op_end(ptl);
1987                                 radix_tree_preload_end();
1988                                 break;
1989                         }
1990                         rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
1991                         if (rc > 0) {
1992                                 /* Success and a new mapping */
1993                                 gmap_insert_rmap(sg, vmaddr, rmap);
1994                                 rmap = NULL;
1995                                 rc = 0;
1996                         }
1997                         gmap_pte_op_end(ptl);
1998                         spin_unlock(&sg->guest_table_lock);
1999                 }
2000                 radix_tree_preload_end();
2001                 if (!rc)
2002                         break;
2003                 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2004                 if (rc)
2005                         break;
2006         }
2007         kfree(rmap);
2008         return rc;
2009 }
2010 EXPORT_SYMBOL_GPL(gmap_shadow_page);
2011
2012 /**
2013  * gmap_shadow_notify - handle notifications for shadow gmap
2014  *
2015  * Called with sg->parent->shadow_lock.
2016  */
2017 static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2018                                unsigned long gaddr, pte_t *pte)
2019 {
2020         struct gmap_rmap *rmap, *rnext, *head;
2021         unsigned long start, end, bits, raddr;
2022
2023         BUG_ON(!gmap_is_shadow(sg));
2024
2025         spin_lock(&sg->guest_table_lock);
2026         if (sg->removed) {
2027                 spin_unlock(&sg->guest_table_lock);
2028                 return;
2029         }
2030         /* Check for top level table */
2031         start = sg->orig_asce & _ASCE_ORIGIN;
2032         end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2033         if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2034             gaddr < end) {
2035                 /* The complete shadow table has to go */
2036                 gmap_unshadow(sg);
2037                 spin_unlock(&sg->guest_table_lock);
2038                 list_del(&sg->list);
2039                 gmap_put(sg);
2040                 return;
2041         }
2042         /* Remove the page table tree from on specific entry */
2043         head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2044         gmap_for_each_rmap_safe(rmap, rnext, head) {
2045                 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2046                 raddr = rmap->raddr ^ bits;
2047                 switch (bits) {
2048                 case _SHADOW_RMAP_REGION1:
2049                         gmap_unshadow_r2t(sg, raddr);
2050                         break;
2051                 case _SHADOW_RMAP_REGION2:
2052                         gmap_unshadow_r3t(sg, raddr);
2053                         break;
2054                 case _SHADOW_RMAP_REGION3:
2055                         gmap_unshadow_sgt(sg, raddr);
2056                         break;
2057                 case _SHADOW_RMAP_SEGMENT:
2058                         gmap_unshadow_pgt(sg, raddr);
2059                         break;
2060                 case _SHADOW_RMAP_PGTABLE:
2061                         gmap_unshadow_page(sg, raddr);
2062                         break;
2063                 }
2064                 kfree(rmap);
2065         }
2066         spin_unlock(&sg->guest_table_lock);
2067 }
2068
2069 /**
2070  * ptep_notify - call all invalidation callbacks for a specific pte.
2071  * @mm: pointer to the process mm_struct
2072  * @addr: virtual address in the process address space
2073  * @pte: pointer to the page table entry
2074  * @bits: bits from the pgste that caused the notify call
2075  *
2076  * This function is assumed to be called with the page table lock held
2077  * for the pte to notify.
2078  */
2079 void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2080                  pte_t *pte, unsigned long bits)
2081 {
2082         unsigned long offset, gaddr = 0;
2083         unsigned long *table;
2084         struct gmap *gmap, *sg, *next;
2085
2086         offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2087         offset = offset * (PAGE_SIZE / sizeof(pte_t));
2088         rcu_read_lock();
2089         list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2090                 spin_lock(&gmap->guest_table_lock);
2091                 table = radix_tree_lookup(&gmap->host_to_guest,
2092                                           vmaddr >> PMD_SHIFT);
2093                 if (table)
2094                         gaddr = __gmap_segment_gaddr(table) + offset;
2095                 spin_unlock(&gmap->guest_table_lock);
2096                 if (!table)
2097                         continue;
2098
2099                 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2100                         spin_lock(&gmap->shadow_lock);
2101                         list_for_each_entry_safe(sg, next,
2102                                                  &gmap->children, list)
2103                                 gmap_shadow_notify(sg, vmaddr, gaddr, pte);
2104                         spin_unlock(&gmap->shadow_lock);
2105                 }
2106                 if (bits & PGSTE_IN_BIT)
2107                         gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2108         }
2109         rcu_read_unlock();
2110 }
2111 EXPORT_SYMBOL_GPL(ptep_notify);
2112
2113 static inline void thp_split_mm(struct mm_struct *mm)
2114 {
2115 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2116         struct vm_area_struct *vma;
2117         unsigned long addr;
2118
2119         for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
2120                 for (addr = vma->vm_start;
2121                      addr < vma->vm_end;
2122                      addr += PAGE_SIZE)
2123                         follow_page(vma, addr, FOLL_SPLIT);
2124                 vma->vm_flags &= ~VM_HUGEPAGE;
2125                 vma->vm_flags |= VM_NOHUGEPAGE;
2126         }
2127         mm->def_flags |= VM_NOHUGEPAGE;
2128 #endif
2129 }
2130
2131 /*
2132  * Remove all empty zero pages from the mapping for lazy refaulting
2133  * - This must be called after mm->context.has_pgste is set, to avoid
2134  *   future creation of zero pages
2135  * - This must be called after THP was enabled
2136  */
2137 static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
2138                            unsigned long end, struct mm_walk *walk)
2139 {
2140         unsigned long addr;
2141
2142         for (addr = start; addr != end; addr += PAGE_SIZE) {
2143                 pte_t *ptep;
2144                 spinlock_t *ptl;
2145
2146                 ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
2147                 if (is_zero_pfn(pte_pfn(*ptep)))
2148                         ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
2149                 pte_unmap_unlock(ptep, ptl);
2150         }
2151         return 0;
2152 }
2153
2154 static inline void zap_zero_pages(struct mm_struct *mm)
2155 {
2156         struct mm_walk walk = { .pmd_entry = __zap_zero_pages };
2157
2158         walk.mm = mm;
2159         walk_page_range(0, TASK_SIZE, &walk);
2160 }
2161
2162 /*
2163  * switch on pgstes for its userspace process (for kvm)
2164  */
2165 int s390_enable_sie(void)
2166 {
2167         struct mm_struct *mm = current->mm;
2168
2169         /* Do we have pgstes? if yes, we are done */
2170         if (mm_has_pgste(mm))
2171                 return 0;
2172         /* Fail if the page tables are 2K */
2173         if (!mm_alloc_pgste(mm))
2174                 return -EINVAL;
2175         down_write(&mm->mmap_sem);
2176         mm->context.has_pgste = 1;
2177         /* split thp mappings and disable thp for future mappings */
2178         thp_split_mm(mm);
2179         zap_zero_pages(mm);
2180         up_write(&mm->mmap_sem);
2181         return 0;
2182 }
2183 EXPORT_SYMBOL_GPL(s390_enable_sie);
2184
2185 /*
2186  * Enable storage key handling from now on and initialize the storage
2187  * keys with the default key.
2188  */
2189 static int __s390_enable_skey(pte_t *pte, unsigned long addr,
2190                               unsigned long next, struct mm_walk *walk)
2191 {
2192         /* Clear storage key */
2193         ptep_zap_key(walk->mm, addr, pte);
2194         return 0;
2195 }
2196
2197 int s390_enable_skey(void)
2198 {
2199         struct mm_walk walk = { .pte_entry = __s390_enable_skey };
2200         struct mm_struct *mm = current->mm;
2201         struct vm_area_struct *vma;
2202         int rc = 0;
2203
2204         down_write(&mm->mmap_sem);
2205         if (mm_use_skey(mm))
2206                 goto out_up;
2207
2208         mm->context.use_skey = 1;
2209         for (vma = mm->mmap; vma; vma = vma->vm_next) {
2210                 if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
2211                                 MADV_UNMERGEABLE, &vma->vm_flags)) {
2212                         mm->context.use_skey = 0;
2213                         rc = -ENOMEM;
2214                         goto out_up;
2215                 }
2216         }
2217         mm->def_flags &= ~VM_MERGEABLE;
2218
2219         walk.mm = mm;
2220         walk_page_range(0, TASK_SIZE, &walk);
2221
2222 out_up:
2223         up_write(&mm->mmap_sem);
2224         return rc;
2225 }
2226 EXPORT_SYMBOL_GPL(s390_enable_skey);
2227
2228 /*
2229  * Reset CMMA state, make all pages stable again.
2230  */
2231 static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2232                              unsigned long next, struct mm_walk *walk)
2233 {
2234         ptep_zap_unused(walk->mm, addr, pte, 1);
2235         return 0;
2236 }
2237
2238 void s390_reset_cmma(struct mm_struct *mm)
2239 {
2240         struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
2241
2242         down_write(&mm->mmap_sem);
2243         walk.mm = mm;
2244         walk_page_range(0, TASK_SIZE, &walk);
2245         up_write(&mm->mmap_sem);
2246 }
2247 EXPORT_SYMBOL_GPL(s390_reset_cmma);