Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-block.git] / arch / s390 / mm / gmap.c
CommitLineData
ac41aaee 1// SPDX-License-Identifier: GPL-2.0
1e133ab2
MS
2/*
3 * KVM guest address space mapping code
4 *
a9e00d83 5 * Copyright IBM Corp. 2007, 2016, 2018
1e133ab2 6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
a9e00d83
JF
7 * David Hildenbrand <david@redhat.com>
8 * Janosch Frank <frankja@linux.vnet.ibm.com>
1e133ab2
MS
9 */
10
11#include <linux/kernel.h>
12#include <linux/mm.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
21#include <asm/pgtable.h>
22#include <asm/pgalloc.h>
23#include <asm/gmap.h>
24#include <asm/tlb.h>
25
fd8d4e3a
DH
26#define GMAP_SHADOW_FAKE_TABLE 1ULL
27
1e133ab2 28/**
6ea427bb 29 * gmap_alloc - allocate and initialize a guest address space
1e133ab2 30 * @mm: pointer to the parent mm_struct
9c650d09 31 * @limit: maximum address of the gmap address space
1e133ab2
MS
32 *
33 * Returns a guest address space structure.
34 */
6ea427bb 35static struct gmap *gmap_alloc(unsigned long limit)
1e133ab2
MS
36{
37 struct gmap *gmap;
38 struct page *page;
39 unsigned long *table;
40 unsigned long etype, atype;
41
f1c1174f
HC
42 if (limit < _REGION3_SIZE) {
43 limit = _REGION3_SIZE - 1;
1e133ab2
MS
44 atype = _ASCE_TYPE_SEGMENT;
45 etype = _SEGMENT_ENTRY_EMPTY;
f1c1174f
HC
46 } else if (limit < _REGION2_SIZE) {
47 limit = _REGION2_SIZE - 1;
1e133ab2
MS
48 atype = _ASCE_TYPE_REGION3;
49 etype = _REGION3_ENTRY_EMPTY;
f1c1174f
HC
50 } else if (limit < _REGION1_SIZE) {
51 limit = _REGION1_SIZE - 1;
1e133ab2
MS
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);
4be130a0
MS
63 INIT_LIST_HEAD(&gmap->children);
64 INIT_LIST_HEAD(&gmap->pt_list);
1e133ab2
MS
65 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
66 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
4be130a0 67 INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC);
1e133ab2 68 spin_lock_init(&gmap->guest_table_lock);
4be130a0 69 spin_lock_init(&gmap->shadow_lock);
6ea427bb 70 atomic_set(&gmap->ref_count, 1);
f1c1174f 71 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1e133ab2
MS
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;
1e133ab2
MS
82 return gmap;
83
84out_free:
85 kfree(gmap);
86out:
87 return NULL;
88}
6ea427bb
MS
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;
44b6cc81 100 unsigned long gmap_asce;
6ea427bb
MS
101
102 gmap = gmap_alloc(limit);
103 if (!gmap)
104 return NULL;
105 gmap->mm = mm;
f28a4b4d 106 spin_lock(&mm->context.lock);
6ea427bb 107 list_add_rcu(&gmap->list, &mm->context.gmap_list);
44b6cc81
MS
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);
f28a4b4d 113 spin_unlock(&mm->context.lock);
6ea427bb
MS
114 return gmap;
115}
116EXPORT_SYMBOL_GPL(gmap_create);
1e133ab2
MS
117
118static void gmap_flush_tlb(struct gmap *gmap)
119{
120 if (MACHINE_HAS_IDTE)
f0454029 121 __tlb_flush_idte(gmap->asce);
1e133ab2
MS
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;
d12a3d60 131 void __rcu **slot;
1e133ab2
MS
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
4be130a0
MS
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;
d12a3d60 156 void __rcu **slot;
4be130a0
MS
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
1e133ab2
MS
177/**
178 * gmap_free - free a guest address space
179 * @gmap: pointer to the guest address space structure
4be130a0
MS
180 *
181 * No locks required. There are no references to this gmap anymore.
1e133ab2 182 */
6ea427bb 183static void gmap_free(struct gmap *gmap)
1e133ab2
MS
184{
185 struct page *page, *next;
186
eea3678d
DH
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);
1e133ab2
MS
190 /* Free all segment & region tables. */
191 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
f1c1174f 192 __free_pages(page, CRST_ALLOC_ORDER);
1e133ab2
MS
193 gmap_radix_tree_free(&gmap->guest_to_host);
194 gmap_radix_tree_free(&gmap->host_to_guest);
4be130a0
MS
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
1e133ab2
MS
206 kfree(gmap);
207}
6ea427bb
MS
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 atomic_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 (atomic_dec_return(&gmap->ref_count) == 0)
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{
4be130a0 241 struct gmap *sg, *next;
44b6cc81 242 unsigned long gmap_asce;
4be130a0 243
4be130a0
MS
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) {
4be130a0
MS
248 list_del(&sg->list);
249 gmap_put(sg);
250 }
251 spin_unlock(&gmap->shadow_lock);
252 }
6ea427bb 253 /* Remove gmap from the pre-mm list */
f28a4b4d 254 spin_lock(&gmap->mm->context.lock);
6ea427bb 255 list_del_rcu(&gmap->list);
44b6cc81
MS
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);
f28a4b4d 264 spin_unlock(&gmap->mm->context.lock);
6ea427bb
MS
265 synchronize_rcu();
266 /* Put reference */
267 gmap_put(gmap);
268}
269EXPORT_SYMBOL_GPL(gmap_remove);
1e133ab2
MS
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
37d9df98
DH
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
1e133ab2
MS
302/*
303 * gmap_alloc_table is assumed to be called with mmap_sem 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 */
f1c1174f 312 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
1e133ab2
MS
313 if (!page)
314 return -ENOMEM;
315 new = (unsigned long *) page_to_phys(page);
316 crst_table_init(new, init);
4be130a0 317 spin_lock(&gmap->guest_table_lock);
1e133ab2
MS
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 }
4be130a0 325 spin_unlock(&gmap->guest_table_lock);
1e133ab2 326 if (page)
f1c1174f 327 __free_pages(page, CRST_ALLOC_ORDER);
1e133ab2
MS
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
4be130a0 361 BUG_ON(gmap_is_shadow(gmap));
1e133ab2
MS
362 spin_lock(&gmap->guest_table_lock);
363 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
364 if (entry) {
54397bb0
DD
365 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
366 *entry = _SEGMENT_ENTRY_EMPTY;
1e133ab2
MS
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
4be130a0 401 BUG_ON(gmap_is_shadow(gmap));
1e133ab2
MS
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 down_write(&gmap->mm->mmap_sem);
409 for (off = 0; off < len; off += PMD_SIZE)
410 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
411 up_write(&gmap->mm->mmap_sem);
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
4be130a0 433 BUG_ON(gmap_is_shadow(gmap));
1e133ab2
MS
434 if ((from | to | len) & (PMD_SIZE - 1))
435 return -EINVAL;
436 if (len == 0 || from + len < from || to + len < to ||
ee71d16d 437 from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
1e133ab2
MS
438 return -EINVAL;
439
440 flush = 0;
441 down_write(&gmap->mm->mmap_sem);
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 up_write(&gmap->mm->mmap_sem);
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_sem of the mm that belongs to the address space must be held
470 * when this function gets called.
4be130a0
MS
471 *
472 * Note: Can also be called for shadow gmaps.
1e133ab2
MS
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);
4be130a0 480 /* Note: guest_to_host is empty for a shadow gmap */
1e133ab2
MS
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 down_read(&gmap->mm->mmap_sem);
499 rc = __gmap_translate(gmap, gaddr);
500 up_read(&gmap->mm->mmap_sem);
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
8ecb1a59
MS
517 rcu_read_lock();
518 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
1e133ab2
MS
519 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
520 if (flush)
521 gmap_flush_tlb(gmap);
522 }
8ecb1a59 523 rcu_read_unlock();
1e133ab2
MS
524}
525
0959e168
JF
526static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
527 unsigned long gaddr);
528
1e133ab2
MS
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_sem 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;
1aea9b3f 546 p4d_t *p4d;
1e133ab2
MS
547 pud_t *pud;
548 pmd_t *pmd;
0959e168 549 u64 unprot;
1e133ab2
MS
550 int rc;
551
4be130a0 552 BUG_ON(gmap_is_shadow(gmap));
1e133ab2
MS
553 /* Create higher level tables in the gmap page table */
554 table = gmap->table;
555 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
f1c1174f 556 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
1e133ab2
MS
557 if ((*table & _REGION_ENTRY_INVALID) &&
558 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
f1c1174f 559 gaddr & _REGION1_MASK))
1e133ab2
MS
560 return -ENOMEM;
561 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
562 }
563 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
f1c1174f 564 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
1e133ab2
MS
565 if ((*table & _REGION_ENTRY_INVALID) &&
566 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
f1c1174f 567 gaddr & _REGION2_MASK))
1e133ab2
MS
568 return -ENOMEM;
569 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
570 }
571 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
f1c1174f 572 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
1e133ab2
MS
573 if ((*table & _REGION_ENTRY_INVALID) &&
574 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
f1c1174f 575 gaddr & _REGION3_MASK))
1e133ab2
MS
576 return -ENOMEM;
577 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
578 }
f1c1174f 579 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
1e133ab2
MS
580 /* Walk the parent mm page table */
581 mm = gmap->mm;
582 pgd = pgd_offset(mm, vmaddr);
583 VM_BUG_ON(pgd_none(*pgd));
1aea9b3f
MS
584 p4d = p4d_offset(pgd, vmaddr);
585 VM_BUG_ON(p4d_none(*p4d));
586 pud = pud_offset(p4d, vmaddr);
1e133ab2 587 VM_BUG_ON(pud_none(*pud));
d08de8e2
GS
588 /* large puds cannot yet be handled */
589 if (pud_large(*pud))
590 return -EFAULT;
1e133ab2
MS
591 pmd = pmd_offset(pud, vmaddr);
592 VM_BUG_ON(pmd_none(*pmd));
a9e00d83
JF
593 /* Are we allowed to use huge pages? */
594 if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
1e133ab2
MS
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);
54397bb0 602 if (*table == _SEGMENT_ENTRY_EMPTY) {
1e133ab2
MS
603 rc = radix_tree_insert(&gmap->host_to_guest,
604 vmaddr >> PMD_SHIFT, table);
58b7e200
JF
605 if (!rc) {
606 if (pmd_large(*pmd)) {
0959e168
JF
607 *table = (pmd_val(*pmd) &
608 _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
609 | _SEGMENT_ENTRY_GMAP_UC;
58b7e200
JF
610 } else
611 *table = pmd_val(*pmd) &
612 _SEGMENT_ENTRY_HARDWARE_BITS;
613 }
0959e168
JF
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);
58b7e200 620 }
1e133ab2
MS
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 down_read(&gmap->mm->mmap_sem);
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(current, 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_sem 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 up_read(&gmap->mm->mmap_sem);
667 return rc;
668}
669EXPORT_SYMBOL_GPL(gmap_fault);
670
671/*
672 * this function is assumed to be called with mmap_sem 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 down_read(&gmap->mm->mmap_sem);
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);
1843abd0
JF
711 if (!vma)
712 continue;
7d735b9a
DD
713 /*
714 * We do not discard pages that are backed by
715 * hugetlbfs, so we don't have to refault them.
716 */
1843abd0 717 if (is_vm_hugetlb_page(vma))
7d735b9a 718 continue;
1e133ab2 719 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
ecf1385d 720 zap_page_range(vma, vmaddr, size);
1e133ab2
MS
721 }
722 up_read(&gmap->mm->mmap_sem);
723}
724EXPORT_SYMBOL_GPL(gmap_discard);
725
726static LIST_HEAD(gmap_notifier_list);
727static DEFINE_SPINLOCK(gmap_notifier_lock);
728
729/**
b2d73b2a 730 * gmap_register_pte_notifier - register a pte invalidation callback
1e133ab2
MS
731 * @nb: pointer to the gmap notifier block
732 */
b2d73b2a 733void gmap_register_pte_notifier(struct gmap_notifier *nb)
1e133ab2
MS
734{
735 spin_lock(&gmap_notifier_lock);
8ecb1a59 736 list_add_rcu(&nb->list, &gmap_notifier_list);
1e133ab2
MS
737 spin_unlock(&gmap_notifier_lock);
738}
b2d73b2a 739EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
1e133ab2
MS
740
741/**
b2d73b2a 742 * gmap_unregister_pte_notifier - remove a pte invalidation callback
1e133ab2
MS
743 * @nb: pointer to the gmap notifier block
744 */
b2d73b2a 745void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
1e133ab2
MS
746{
747 spin_lock(&gmap_notifier_lock);
8ecb1a59 748 list_del_rcu(&nb->list);
1e133ab2 749 spin_unlock(&gmap_notifier_lock);
8ecb1a59 750 synchronize_rcu();
1e133ab2 751}
b2d73b2a 752EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
1e133ab2 753
414d3b07
MS
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
1e133ab2 769/**
b2d73b2a
MS
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
4be130a0
MS
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.
b2d73b2a 784 *
4be130a0 785 * Note: Can also be called for shadow gmaps.
b2d73b2a
MS
786 */
787static inline unsigned long *gmap_table_walk(struct gmap *gmap,
4be130a0 788 unsigned long gaddr, int level)
b2d73b2a
MS
789{
790 unsigned long *table;
791
4be130a0
MS
792 if ((gmap->asce & _ASCE_TYPE_MASK) + 4 < (level * 4))
793 return NULL;
794 if (gmap_is_shadow(gmap) && gmap->removed)
795 return NULL;
796 if (gaddr & (-1UL << (31 + ((gmap->asce & _ASCE_TYPE_MASK) >> 2)*11)))
797 return NULL;
b2d73b2a
MS
798 table = gmap->table;
799 switch (gmap->asce & _ASCE_TYPE_MASK) {
800 case _ASCE_TYPE_REGION1:
f1c1174f 801 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
4be130a0
MS
802 if (level == 4)
803 break;
b2d73b2a
MS
804 if (*table & _REGION_ENTRY_INVALID)
805 return NULL;
806 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
807 /* Fallthrough */
808 case _ASCE_TYPE_REGION2:
f1c1174f 809 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
4be130a0
MS
810 if (level == 3)
811 break;
b2d73b2a
MS
812 if (*table & _REGION_ENTRY_INVALID)
813 return NULL;
814 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
815 /* Fallthrough */
816 case _ASCE_TYPE_REGION3:
f1c1174f 817 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
4be130a0
MS
818 if (level == 2)
819 break;
b2d73b2a
MS
820 if (*table & _REGION_ENTRY_INVALID)
821 return NULL;
822 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
823 /* Fallthrough */
824 case _ASCE_TYPE_SEGMENT:
f1c1174f 825 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
4be130a0
MS
826 if (level == 1)
827 break;
828 if (*table & _REGION_ENTRY_INVALID)
829 return NULL;
830 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
f1c1174f 831 table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
b2d73b2a
MS
832 }
833 return table;
834}
835
836/**
837 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
838 * and return the pte pointer
839 * @gmap: pointer to guest mapping meta data structure
840 * @gaddr: virtual address in the guest address space
841 * @ptl: pointer to the spinlock pointer
842 *
843 * Returns a pointer to the locked pte for a guest address, or NULL
844 */
845static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
846 spinlock_t **ptl)
847{
848 unsigned long *table;
849
96965941 850 BUG_ON(gmap_is_shadow(gmap));
b2d73b2a 851 /* Walk the gmap page table, lock and get pte pointer */
4be130a0 852 table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
96965941 853 if (!table || *table & _SEGMENT_ENTRY_INVALID)
b2d73b2a
MS
854 return NULL;
855 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
856}
857
858/**
859 * gmap_pte_op_fixup - force a page in and connect the gmap page table
860 * @gmap: pointer to guest mapping meta data structure
861 * @gaddr: virtual address in the guest address space
862 * @vmaddr: address in the host process address space
01f71917 863 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
b2d73b2a
MS
864 *
865 * Returns 0 if the caller can retry __gmap_translate (might fail again),
866 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
867 * up or connecting the gmap page table.
868 */
869static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
01f71917 870 unsigned long vmaddr, int prot)
b2d73b2a
MS
871{
872 struct mm_struct *mm = gmap->mm;
01f71917 873 unsigned int fault_flags;
b2d73b2a
MS
874 bool unlocked = false;
875
4be130a0 876 BUG_ON(gmap_is_shadow(gmap));
01f71917
DH
877 fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
878 if (fixup_user_fault(current, mm, vmaddr, fault_flags, &unlocked))
b2d73b2a
MS
879 return -EFAULT;
880 if (unlocked)
881 /* lost mmap_sem, caller has to retry __gmap_translate */
882 return 0;
883 /* Connect the page tables */
884 return __gmap_link(gmap, gaddr, vmaddr);
1e133ab2 885}
1e133ab2
MS
886
887/**
b2d73b2a
MS
888 * gmap_pte_op_end - release the page table lock
889 * @ptl: pointer to the spinlock pointer
890 */
891static void gmap_pte_op_end(spinlock_t *ptl)
892{
5a045bb9
JF
893 if (ptl)
894 spin_unlock(ptl);
895}
896
897/**
898 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
899 * and return the pmd pointer
900 * @gmap: pointer to guest mapping meta data structure
901 * @gaddr: virtual address in the guest address space
902 *
903 * Returns a pointer to the pmd for a guest address, or NULL
904 */
905static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
906{
907 pmd_t *pmdp;
908
909 BUG_ON(gmap_is_shadow(gmap));
910 spin_lock(&gmap->guest_table_lock);
911 pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
912
913 if (!pmdp || pmd_none(*pmdp)) {
914 spin_unlock(&gmap->guest_table_lock);
915 return NULL;
916 }
917
918 /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
919 if (!pmd_large(*pmdp))
920 spin_unlock(&gmap->guest_table_lock);
921 return pmdp;
922}
923
924/**
925 * gmap_pmd_op_end - release the guest_table_lock if needed
926 * @gmap: pointer to the guest mapping meta data structure
927 * @pmdp: pointer to the pmd
928 */
929static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
930{
931 if (pmd_large(*pmdp))
932 spin_unlock(&gmap->guest_table_lock);
933}
934
7c4b13a7
JF
935/*
936 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
937 * @pmdp: pointer to the pmd to be protected
938 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
939 * @bits: notification bits to set
940 *
941 * Returns:
942 * 0 if successfully protected
943 * -EAGAIN if a fixup is needed
944 * -EINVAL if unsupported notifier bits have been specified
945 *
946 * Expected to be called with sg->mm->mmap_sem in read and
947 * guest_table_lock held.
948 */
949static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
950 pmd_t *pmdp, int prot, unsigned long bits)
951{
952 int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
953 int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
0959e168 954 pmd_t new = *pmdp;
7c4b13a7
JF
955
956 /* Fixup needed */
957 if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
958 return -EAGAIN;
959
0959e168
JF
960 if (prot == PROT_NONE && !pmd_i) {
961 pmd_val(new) |= _SEGMENT_ENTRY_INVALID;
962 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
963 }
964
965 if (prot == PROT_READ && !pmd_p) {
966 pmd_val(new) &= ~_SEGMENT_ENTRY_INVALID;
967 pmd_val(new) |= _SEGMENT_ENTRY_PROTECT;
968 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
969 }
970
7c4b13a7
JF
971 if (bits & GMAP_NOTIFY_MPROT)
972 pmd_val(*pmdp) |= _SEGMENT_ENTRY_GMAP_IN;
973
974 /* Shadow GMAP protection needs split PMDs */
975 if (bits & GMAP_NOTIFY_SHADOW)
976 return -EINVAL;
977
978 return 0;
979}
980
5a045bb9
JF
981/*
982 * gmap_protect_pte - remove access rights to memory and set pgste bits
983 * @gmap: pointer to guest mapping meta data structure
984 * @gaddr: virtual address in the guest address space
985 * @pmdp: pointer to the pmd associated with the pte
986 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
2c46e974 987 * @bits: notification bits to set
5a045bb9
JF
988 *
989 * Returns 0 if successfully protected, -ENOMEM if out of memory and
990 * -EAGAIN if a fixup is needed.
991 *
992 * Expected to be called with sg->mm->mmap_sem in read
993 */
994static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
995 pmd_t *pmdp, int prot, unsigned long bits)
996{
997 int rc;
998 pte_t *ptep;
999 spinlock_t *ptl = NULL;
2c46e974 1000 unsigned long pbits = 0;
5a045bb9
JF
1001
1002 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1003 return -EAGAIN;
1004
1005 ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1006 if (!ptep)
1007 return -ENOMEM;
1008
2c46e974
JF
1009 pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1010 pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
5a045bb9 1011 /* Protect and unlock. */
2c46e974 1012 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
5a045bb9
JF
1013 gmap_pte_op_end(ptl);
1014 return rc;
b2d73b2a
MS
1015}
1016
4be130a0
MS
1017/*
1018 * gmap_protect_range - remove access rights to memory and set pgste bits
1e133ab2
MS
1019 * @gmap: pointer to guest mapping meta data structure
1020 * @gaddr: virtual address in the guest address space
1021 * @len: size of area
4be130a0
MS
1022 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1023 * @bits: pgste notification bits to set
1024 *
1025 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1026 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1027 *
1028 * Called with sg->mm->mmap_sem in read.
1e133ab2 1029 */
4be130a0
MS
1030static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1031 unsigned long len, int prot, unsigned long bits)
1e133ab2 1032{
7c4b13a7 1033 unsigned long vmaddr, dist;
5a045bb9 1034 pmd_t *pmdp;
4be130a0
MS
1035 int rc;
1036
96965941 1037 BUG_ON(gmap_is_shadow(gmap));
4be130a0
MS
1038 while (len) {
1039 rc = -EAGAIN;
5a045bb9
JF
1040 pmdp = gmap_pmd_op_walk(gmap, gaddr);
1041 if (pmdp) {
7c4b13a7
JF
1042 if (!pmd_large(*pmdp)) {
1043 rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1044 bits);
1045 if (!rc) {
1046 len -= PAGE_SIZE;
1047 gaddr += PAGE_SIZE;
1048 }
1049 } else {
1050 rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1051 bits);
1052 if (!rc) {
1053 dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1054 len = len < dist ? 0 : len - dist;
1055 gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1056 }
5a045bb9
JF
1057 }
1058 gmap_pmd_op_end(gmap, pmdp);
4be130a0
MS
1059 }
1060 if (rc) {
7c4b13a7
JF
1061 if (rc == -EINVAL)
1062 return rc;
1063
1064 /* -EAGAIN, fixup of userspace mm and gmap */
4be130a0
MS
1065 vmaddr = __gmap_translate(gmap, gaddr);
1066 if (IS_ERR_VALUE(vmaddr))
1067 return vmaddr;
01f71917 1068 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
4be130a0
MS
1069 if (rc)
1070 return rc;
4be130a0 1071 }
4be130a0
MS
1072 }
1073 return 0;
1074}
1e133ab2 1075
b2d73b2a
MS
1076/**
1077 * gmap_mprotect_notify - change access rights for a range of ptes and
1078 * call the notifier if any pte changes again
1e133ab2
MS
1079 * @gmap: pointer to guest mapping meta data structure
1080 * @gaddr: virtual address in the guest address space
1081 * @len: size of area
b2d73b2a 1082 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1e133ab2 1083 *
b2d73b2a
MS
1084 * Returns 0 if for each page in the given range a gmap mapping exists,
1085 * the new access rights could be set and the notifier could be armed.
1086 * If the gmap mapping is missing for one or more pages -EFAULT is
1087 * returned. If no memory could be allocated -ENOMEM is returned.
1088 * This function establishes missing page table entries.
1e133ab2 1089 */
b2d73b2a
MS
1090int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1091 unsigned long len, int prot)
1e133ab2 1092{
4be130a0 1093 int rc;
1e133ab2 1094
4be130a0 1095 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1e133ab2 1096 return -EINVAL;
b2d73b2a 1097 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1e133ab2
MS
1098 return -EINVAL;
1099 down_read(&gmap->mm->mmap_sem);
2c46e974 1100 rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
4be130a0
MS
1101 up_read(&gmap->mm->mmap_sem);
1102 return rc;
1103}
1104EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
1105
1106/**
1107 * gmap_read_table - get an unsigned long value from a guest page table using
1108 * absolute addressing, without marking the page referenced.
1109 * @gmap: pointer to guest mapping meta data structure
1110 * @gaddr: virtual address in the guest address space
1111 * @val: pointer to the unsigned long value to return
1112 *
1113 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
96965941
DH
1114 * if reading using the virtual address failed. -EINVAL if called on a gmap
1115 * shadow.
4be130a0
MS
1116 *
1117 * Called with gmap->mm->mmap_sem in read.
1118 */
1119int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1120{
1121 unsigned long address, vmaddr;
1122 spinlock_t *ptl;
1123 pte_t *ptep, pte;
1124 int rc;
1125
96965941
DH
1126 if (gmap_is_shadow(gmap))
1127 return -EINVAL;
1128
4be130a0 1129 while (1) {
b2d73b2a
MS
1130 rc = -EAGAIN;
1131 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1132 if (ptep) {
4be130a0
MS
1133 pte = *ptep;
1134 if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1135 address = pte_val(pte) & PAGE_MASK;
1136 address += gaddr & ~PAGE_MASK;
1137 *val = *(unsigned long *) address;
1138 pte_val(*ptep) |= _PAGE_YOUNG;
1139 /* Do *NOT* clear the _PAGE_INVALID bit! */
1140 rc = 0;
1141 }
b2d73b2a 1142 gmap_pte_op_end(ptl);
1e133ab2 1143 }
4be130a0
MS
1144 if (!rc)
1145 break;
1146 vmaddr = __gmap_translate(gmap, gaddr);
1147 if (IS_ERR_VALUE(vmaddr)) {
1148 rc = vmaddr;
1e133ab2
MS
1149 break;
1150 }
01f71917 1151 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1e133ab2
MS
1152 if (rc)
1153 break;
1e133ab2 1154 }
1e133ab2
MS
1155 return rc;
1156}
4be130a0 1157EXPORT_SYMBOL_GPL(gmap_read_table);
1e133ab2
MS
1158
1159/**
4be130a0
MS
1160 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1161 * @sg: pointer to the shadow guest address space structure
1162 * @vmaddr: vm address associated with the rmap
1163 * @rmap: pointer to the rmap structure
1e133ab2 1164 *
4be130a0 1165 * Called with the sg->guest_table_lock
1e133ab2 1166 */
4be130a0
MS
1167static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1168 struct gmap_rmap *rmap)
1e133ab2 1169{
d12a3d60 1170 void __rcu **slot;
1e133ab2 1171
4be130a0
MS
1172 BUG_ON(!gmap_is_shadow(sg));
1173 slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1174 if (slot) {
1175 rmap->next = radix_tree_deref_slot_protected(slot,
1176 &sg->guest_table_lock);
6d75f366 1177 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
4be130a0
MS
1178 } else {
1179 rmap->next = NULL;
1180 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1181 rmap);
1182 }
1183}
1184
1185/**
5c528db0 1186 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
4be130a0
MS
1187 * @sg: pointer to the shadow guest address space structure
1188 * @raddr: rmap address in the shadow gmap
1189 * @paddr: address in the parent guest address space
1190 * @len: length of the memory area to protect
4be130a0
MS
1191 *
1192 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1193 * if out of memory and -EFAULT if paddr is invalid.
1194 */
1195static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
5c528db0 1196 unsigned long paddr, unsigned long len)
4be130a0
MS
1197{
1198 struct gmap *parent;
1199 struct gmap_rmap *rmap;
1200 unsigned long vmaddr;
1201 spinlock_t *ptl;
1202 pte_t *ptep;
1203 int rc;
1204
1205 BUG_ON(!gmap_is_shadow(sg));
1206 parent = sg->parent;
1207 while (len) {
1208 vmaddr = __gmap_translate(parent, paddr);
1209 if (IS_ERR_VALUE(vmaddr))
1210 return vmaddr;
1211 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
1212 if (!rmap)
1213 return -ENOMEM;
1214 rmap->raddr = raddr;
1215 rc = radix_tree_preload(GFP_KERNEL);
b2d73b2a 1216 if (rc) {
4be130a0
MS
1217 kfree(rmap);
1218 return rc;
1219 }
1220 rc = -EAGAIN;
1221 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1222 if (ptep) {
1223 spin_lock(&sg->guest_table_lock);
5c528db0 1224 rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
4be130a0
MS
1225 PGSTE_VSIE_BIT);
1226 if (!rc)
1227 gmap_insert_rmap(sg, vmaddr, rmap);
1228 spin_unlock(&sg->guest_table_lock);
1229 gmap_pte_op_end(ptl);
1230 }
1231 radix_tree_preload_end();
1232 if (rc) {
1233 kfree(rmap);
5c528db0 1234 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
b2d73b2a 1235 if (rc)
4be130a0 1236 return rc;
1e133ab2 1237 continue;
1e133ab2 1238 }
4be130a0 1239 paddr += PAGE_SIZE;
b2d73b2a 1240 len -= PAGE_SIZE;
1e133ab2 1241 }
4be130a0
MS
1242 return 0;
1243}
1244
1245#define _SHADOW_RMAP_MASK 0x7
1246#define _SHADOW_RMAP_REGION1 0x5
1247#define _SHADOW_RMAP_REGION2 0x4
1248#define _SHADOW_RMAP_REGION3 0x3
1249#define _SHADOW_RMAP_SEGMENT 0x2
1250#define _SHADOW_RMAP_PGTABLE 0x1
1251
1252/**
1253 * gmap_idte_one - invalidate a single region or segment table entry
1254 * @asce: region or segment table *origin* + table-type bits
1255 * @vaddr: virtual address to identify the table entry to flush
1256 *
1257 * The invalid bit of a single region or segment table entry is set
1258 * and the associated TLB entries depending on the entry are flushed.
1259 * The table-type of the @asce identifies the portion of the @vaddr
1260 * that is used as the invalidation index.
1261 */
1262static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1263{
1264 asm volatile(
1265 " .insn rrf,0xb98e0000,%0,%1,0,0"
1266 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1267}
1268
1269/**
1270 * gmap_unshadow_page - remove a page from a shadow page table
1271 * @sg: pointer to the shadow guest address space structure
1272 * @raddr: rmap address in the shadow guest address space
1273 *
1274 * Called with the sg->guest_table_lock
1275 */
1276static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1277{
1278 unsigned long *table;
1279
1280 BUG_ON(!gmap_is_shadow(sg));
1281 table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1282 if (!table || *table & _PAGE_INVALID)
1283 return;
f1c1174f 1284 gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
4be130a0
MS
1285 ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1286}
1287
1288/**
1289 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1290 * @sg: pointer to the shadow guest address space structure
1291 * @raddr: rmap address in the shadow guest address space
1292 * @pgt: pointer to the start of a shadow page table
1293 *
1294 * Called with the sg->guest_table_lock
1295 */
1296static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1297 unsigned long *pgt)
1298{
1299 int i;
1300
1301 BUG_ON(!gmap_is_shadow(sg));
f1c1174f 1302 for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
4be130a0
MS
1303 pgt[i] = _PAGE_INVALID;
1304}
1305
1306/**
1307 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1308 * @sg: pointer to the shadow guest address space structure
1309 * @raddr: address in the shadow guest address space
1310 *
1311 * Called with the sg->guest_table_lock
1312 */
1313static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1314{
1315 unsigned long sto, *ste, *pgt;
1316 struct page *page;
1317
1318 BUG_ON(!gmap_is_shadow(sg));
1319 ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
998f637c 1320 if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
4be130a0 1321 return;
f1c1174f
HC
1322 gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1323 sto = (unsigned long) (ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
4be130a0
MS
1324 gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1325 pgt = (unsigned long *)(*ste & _SEGMENT_ENTRY_ORIGIN);
1326 *ste = _SEGMENT_ENTRY_EMPTY;
1327 __gmap_unshadow_pgt(sg, raddr, pgt);
1328 /* Free page table */
1329 page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1330 list_del(&page->lru);
1331 page_table_free_pgste(page);
1332}
1333
1334/**
1335 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1336 * @sg: pointer to the shadow guest address space structure
1337 * @raddr: rmap address in the shadow guest address space
1338 * @sgt: pointer to the start of a shadow segment table
1339 *
1340 * Called with the sg->guest_table_lock
1341 */
1342static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1343 unsigned long *sgt)
1344{
2be1da8d 1345 unsigned long *pgt;
4be130a0
MS
1346 struct page *page;
1347 int i;
1348
1349 BUG_ON(!gmap_is_shadow(sg));
f1c1174f 1350 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
998f637c 1351 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
4be130a0
MS
1352 continue;
1353 pgt = (unsigned long *)(sgt[i] & _REGION_ENTRY_ORIGIN);
1354 sgt[i] = _SEGMENT_ENTRY_EMPTY;
1355 __gmap_unshadow_pgt(sg, raddr, pgt);
1356 /* Free page table */
1357 page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1358 list_del(&page->lru);
1359 page_table_free_pgste(page);
1360 }
1361}
1362
1363/**
1364 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1365 * @sg: pointer to the shadow guest address space structure
1366 * @raddr: rmap address in the shadow guest address space
1367 *
1368 * Called with the shadow->guest_table_lock
1369 */
1370static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1371{
1372 unsigned long r3o, *r3e, *sgt;
1373 struct page *page;
1374
1375 BUG_ON(!gmap_is_shadow(sg));
1376 r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
998f637c 1377 if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
4be130a0 1378 return;
f1c1174f
HC
1379 gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1380 r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
4be130a0
MS
1381 gmap_idte_one(r3o | _ASCE_TYPE_REGION3, raddr);
1382 sgt = (unsigned long *)(*r3e & _REGION_ENTRY_ORIGIN);
1383 *r3e = _REGION3_ENTRY_EMPTY;
1384 __gmap_unshadow_sgt(sg, raddr, sgt);
1385 /* Free segment table */
1386 page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1387 list_del(&page->lru);
f1c1174f 1388 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1389}
1390
1391/**
1392 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1393 * @sg: pointer to the shadow guest address space structure
1394 * @raddr: address in the shadow guest address space
1395 * @r3t: pointer to the start of a shadow region-3 table
1396 *
1397 * Called with the sg->guest_table_lock
1398 */
1399static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1400 unsigned long *r3t)
1401{
2be1da8d 1402 unsigned long *sgt;
4be130a0
MS
1403 struct page *page;
1404 int i;
1405
1406 BUG_ON(!gmap_is_shadow(sg));
f1c1174f 1407 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
998f637c 1408 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
4be130a0
MS
1409 continue;
1410 sgt = (unsigned long *)(r3t[i] & _REGION_ENTRY_ORIGIN);
1411 r3t[i] = _REGION3_ENTRY_EMPTY;
1412 __gmap_unshadow_sgt(sg, raddr, sgt);
1413 /* Free segment table */
1414 page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1415 list_del(&page->lru);
f1c1174f 1416 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1417 }
1418}
1419
1420/**
1421 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1422 * @sg: pointer to the shadow guest address space structure
1423 * @raddr: rmap address in the shadow guest address space
1424 *
1425 * Called with the sg->guest_table_lock
1426 */
1427static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1428{
1429 unsigned long r2o, *r2e, *r3t;
1430 struct page *page;
1431
1432 BUG_ON(!gmap_is_shadow(sg));
1433 r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
998f637c 1434 if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
4be130a0 1435 return;
f1c1174f
HC
1436 gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1437 r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
4be130a0
MS
1438 gmap_idte_one(r2o | _ASCE_TYPE_REGION2, raddr);
1439 r3t = (unsigned long *)(*r2e & _REGION_ENTRY_ORIGIN);
1440 *r2e = _REGION2_ENTRY_EMPTY;
1441 __gmap_unshadow_r3t(sg, raddr, r3t);
1442 /* Free region 3 table */
1443 page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1444 list_del(&page->lru);
f1c1174f 1445 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1446}
1447
1448/**
1449 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1450 * @sg: pointer to the shadow guest address space structure
1451 * @raddr: rmap address in the shadow guest address space
1452 * @r2t: pointer to the start of a shadow region-2 table
1453 *
1454 * Called with the sg->guest_table_lock
1455 */
1456static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1457 unsigned long *r2t)
1458{
2be1da8d 1459 unsigned long *r3t;
4be130a0
MS
1460 struct page *page;
1461 int i;
1462
1463 BUG_ON(!gmap_is_shadow(sg));
f1c1174f 1464 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
998f637c 1465 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
4be130a0
MS
1466 continue;
1467 r3t = (unsigned long *)(r2t[i] & _REGION_ENTRY_ORIGIN);
1468 r2t[i] = _REGION2_ENTRY_EMPTY;
1469 __gmap_unshadow_r3t(sg, raddr, r3t);
1470 /* Free region 3 table */
1471 page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1472 list_del(&page->lru);
f1c1174f 1473 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1474 }
1475}
1476
1477/**
1478 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1479 * @sg: pointer to the shadow guest address space structure
1480 * @raddr: rmap address in the shadow guest address space
1481 *
1482 * Called with the sg->guest_table_lock
1483 */
1484static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1485{
1486 unsigned long r1o, *r1e, *r2t;
1487 struct page *page;
1488
1489 BUG_ON(!gmap_is_shadow(sg));
1490 r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
998f637c 1491 if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
4be130a0 1492 return;
f1c1174f
HC
1493 gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1494 r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
4be130a0
MS
1495 gmap_idte_one(r1o | _ASCE_TYPE_REGION1, raddr);
1496 r2t = (unsigned long *)(*r1e & _REGION_ENTRY_ORIGIN);
1497 *r1e = _REGION1_ENTRY_EMPTY;
1498 __gmap_unshadow_r2t(sg, raddr, r2t);
1499 /* Free region 2 table */
1500 page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1501 list_del(&page->lru);
f1c1174f 1502 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1503}
1504
1505/**
1506 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1507 * @sg: pointer to the shadow guest address space structure
1508 * @raddr: rmap address in the shadow guest address space
1509 * @r1t: pointer to the start of a shadow region-1 table
1510 *
1511 * Called with the shadow->guest_table_lock
1512 */
1513static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1514 unsigned long *r1t)
1515{
1516 unsigned long asce, *r2t;
1517 struct page *page;
1518 int i;
1519
1520 BUG_ON(!gmap_is_shadow(sg));
1521 asce = (unsigned long) r1t | _ASCE_TYPE_REGION1;
f1c1174f 1522 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
998f637c 1523 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
4be130a0
MS
1524 continue;
1525 r2t = (unsigned long *)(r1t[i] & _REGION_ENTRY_ORIGIN);
1526 __gmap_unshadow_r2t(sg, raddr, r2t);
1527 /* Clear entry and flush translation r1t -> r2t */
1528 gmap_idte_one(asce, raddr);
1529 r1t[i] = _REGION1_ENTRY_EMPTY;
1530 /* Free region 2 table */
1531 page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1532 list_del(&page->lru);
f1c1174f 1533 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1534 }
1535}
1536
1537/**
1538 * gmap_unshadow - remove a shadow page table completely
1539 * @sg: pointer to the shadow guest address space structure
1540 *
1541 * Called with sg->guest_table_lock
1542 */
1543static void gmap_unshadow(struct gmap *sg)
1544{
1545 unsigned long *table;
1546
1547 BUG_ON(!gmap_is_shadow(sg));
1548 if (sg->removed)
1549 return;
1550 sg->removed = 1;
1551 gmap_call_notifier(sg, 0, -1UL);
eea3678d 1552 gmap_flush_tlb(sg);
4be130a0
MS
1553 table = (unsigned long *)(sg->asce & _ASCE_ORIGIN);
1554 switch (sg->asce & _ASCE_TYPE_MASK) {
1555 case _ASCE_TYPE_REGION1:
1556 __gmap_unshadow_r1t(sg, 0, table);
1557 break;
1558 case _ASCE_TYPE_REGION2:
1559 __gmap_unshadow_r2t(sg, 0, table);
1560 break;
1561 case _ASCE_TYPE_REGION3:
1562 __gmap_unshadow_r3t(sg, 0, table);
1563 break;
1564 case _ASCE_TYPE_SEGMENT:
1565 __gmap_unshadow_sgt(sg, 0, table);
1566 break;
1567 }
1568}
1569
1570/**
1571 * gmap_find_shadow - find a specific asce in the list of shadow tables
1572 * @parent: pointer to the parent gmap
1573 * @asce: ASCE for which the shadow table is created
5b062bd4 1574 * @edat_level: edat level to be used for the shadow translation
4be130a0
MS
1575 *
1576 * Returns the pointer to a gmap if a shadow table with the given asce is
0f7f8489
DH
1577 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1578 * otherwise NULL
4be130a0 1579 */
5b062bd4
DH
1580static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1581 int edat_level)
4be130a0
MS
1582{
1583 struct gmap *sg;
1584
1585 list_for_each_entry(sg, &parent->children, list) {
5b062bd4
DH
1586 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1587 sg->removed)
4be130a0 1588 continue;
0f7f8489
DH
1589 if (!sg->initialized)
1590 return ERR_PTR(-EAGAIN);
4be130a0
MS
1591 atomic_inc(&sg->ref_count);
1592 return sg;
1593 }
1594 return NULL;
1595}
1596
5b6c963b
DH
1597/**
1598 * gmap_shadow_valid - check if a shadow guest address space matches the
1599 * given properties and is still valid
1600 * @sg: pointer to the shadow guest address space structure
1601 * @asce: ASCE for which the shadow table is requested
1602 * @edat_level: edat level to be used for the shadow translation
1603 *
1604 * Returns 1 if the gmap shadow is still valid and matches the given
1605 * properties, the caller can continue using it. Returns 0 otherwise, the
1606 * caller has to request a new shadow gmap in this case.
1607 *
1608 */
1609int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1610{
1611 if (sg->removed)
1612 return 0;
1613 return sg->orig_asce == asce && sg->edat_level == edat_level;
1614}
1615EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1616
4be130a0
MS
1617/**
1618 * gmap_shadow - create/find a shadow guest address space
1619 * @parent: pointer to the parent gmap
1620 * @asce: ASCE for which the shadow table is created
5b062bd4 1621 * @edat_level: edat level to be used for the shadow translation
4be130a0
MS
1622 *
1623 * The pages of the top level page table referred by the asce parameter
1624 * will be set to read-only and marked in the PGSTEs of the kvm process.
1625 * The shadow table will be removed automatically on any change to the
1626 * PTE mapping for the source table.
1627 *
0f7f8489
DH
1628 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1629 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1630 * parent gmap table could not be protected.
4be130a0 1631 */
5b062bd4
DH
1632struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1633 int edat_level)
4be130a0
MS
1634{
1635 struct gmap *sg, *new;
1636 unsigned long limit;
1637 int rc;
1638
a9e00d83 1639 BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
4be130a0
MS
1640 BUG_ON(gmap_is_shadow(parent));
1641 spin_lock(&parent->shadow_lock);
5b062bd4 1642 sg = gmap_find_shadow(parent, asce, edat_level);
4be130a0
MS
1643 spin_unlock(&parent->shadow_lock);
1644 if (sg)
1645 return sg;
1646 /* Create a new shadow gmap */
1647 limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
3218f709
DH
1648 if (asce & _ASCE_REAL_SPACE)
1649 limit = -1UL;
4be130a0
MS
1650 new = gmap_alloc(limit);
1651 if (!new)
0f7f8489 1652 return ERR_PTR(-ENOMEM);
4be130a0
MS
1653 new->mm = parent->mm;
1654 new->parent = gmap_get(parent);
1655 new->orig_asce = asce;
5b062bd4 1656 new->edat_level = edat_level;
0f7f8489
DH
1657 new->initialized = false;
1658 spin_lock(&parent->shadow_lock);
1659 /* Recheck if another CPU created the same shadow */
5b062bd4 1660 sg = gmap_find_shadow(parent, asce, edat_level);
0f7f8489
DH
1661 if (sg) {
1662 spin_unlock(&parent->shadow_lock);
1663 gmap_free(new);
1664 return sg;
1665 }
717c0555
DH
1666 if (asce & _ASCE_REAL_SPACE) {
1667 /* only allow one real-space gmap shadow */
1668 list_for_each_entry(sg, &parent->children, list) {
1669 if (sg->orig_asce & _ASCE_REAL_SPACE) {
1670 spin_lock(&sg->guest_table_lock);
1671 gmap_unshadow(sg);
1672 spin_unlock(&sg->guest_table_lock);
1673 list_del(&sg->list);
1674 gmap_put(sg);
1675 break;
1676 }
1677 }
1678 }
0f7f8489
DH
1679 atomic_set(&new->ref_count, 2);
1680 list_add(&new->list, &parent->children);
3218f709
DH
1681 if (asce & _ASCE_REAL_SPACE) {
1682 /* nothing to protect, return right away */
1683 new->initialized = true;
1684 spin_unlock(&parent->shadow_lock);
1685 return new;
1686 }
0f7f8489
DH
1687 spin_unlock(&parent->shadow_lock);
1688 /* protect after insertion, so it will get properly invalidated */
4be130a0
MS
1689 down_read(&parent->mm->mmap_sem);
1690 rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
f1c1174f 1691 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
2c46e974 1692 PROT_READ, GMAP_NOTIFY_SHADOW);
4be130a0 1693 up_read(&parent->mm->mmap_sem);
0f7f8489
DH
1694 spin_lock(&parent->shadow_lock);
1695 new->initialized = true;
4be130a0 1696 if (rc) {
0f7f8489 1697 list_del(&new->list);
4be130a0 1698 gmap_free(new);
0f7f8489
DH
1699 new = ERR_PTR(rc);
1700 }
1701 spin_unlock(&parent->shadow_lock);
1702 return new;
4be130a0
MS
1703}
1704EXPORT_SYMBOL_GPL(gmap_shadow);
1705
1706/**
1707 * gmap_shadow_r2t - create an empty shadow region 2 table
1708 * @sg: pointer to the shadow guest address space structure
1709 * @saddr: faulting address in the shadow gmap
1710 * @r2t: parent gmap address of the region 2 table to get shadowed
3218f709 1711 * @fake: r2t references contiguous guest memory block, not a r2t
4be130a0
MS
1712 *
1713 * The r2t parameter specifies the address of the source table. The
1714 * four pages of the source table are made read-only in the parent gmap
1715 * address space. A write to the source table area @r2t will automatically
1716 * remove the shadow r2 table and all of its decendents.
1717 *
1718 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1719 * shadow table structure is incomplete, -ENOMEM if out of memory and
1720 * -EFAULT if an address in the parent gmap could not be resolved.
1721 *
1722 * Called with sg->mm->mmap_sem in read.
1723 */
3218f709
DH
1724int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1725 int fake)
4be130a0
MS
1726{
1727 unsigned long raddr, origin, offset, len;
1728 unsigned long *s_r2t, *table;
1729 struct page *page;
1730 int rc;
1731
1732 BUG_ON(!gmap_is_shadow(sg));
1733 /* Allocate a shadow region second table */
f1c1174f 1734 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
4be130a0
MS
1735 if (!page)
1736 return -ENOMEM;
1737 page->index = r2t & _REGION_ENTRY_ORIGIN;
3218f709
DH
1738 if (fake)
1739 page->index |= GMAP_SHADOW_FAKE_TABLE;
4be130a0
MS
1740 s_r2t = (unsigned long *) page_to_phys(page);
1741 /* Install shadow region second table */
1742 spin_lock(&sg->guest_table_lock);
1743 table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1744 if (!table) {
1745 rc = -EAGAIN; /* Race with unshadow */
1746 goto out_free;
1747 }
1748 if (!(*table & _REGION_ENTRY_INVALID)) {
1749 rc = 0; /* Already established */
1750 goto out_free;
998f637c
DH
1751 } else if (*table & _REGION_ENTRY_ORIGIN) {
1752 rc = -EAGAIN; /* Race with shadow */
1753 goto out_free;
4be130a0
MS
1754 }
1755 crst_table_init(s_r2t, _REGION2_ENTRY_EMPTY);
998f637c
DH
1756 /* mark as invalid as long as the parent table is not protected */
1757 *table = (unsigned long) s_r2t | _REGION_ENTRY_LENGTH |
1758 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
fd8d4e3a
DH
1759 if (sg->edat_level >= 1)
1760 *table |= (r2t & _REGION_ENTRY_PROTECT);
4be130a0 1761 list_add(&page->lru, &sg->crst_list);
3218f709
DH
1762 if (fake) {
1763 /* nothing to protect for fake tables */
1764 *table &= ~_REGION_ENTRY_INVALID;
1765 spin_unlock(&sg->guest_table_lock);
1766 return 0;
1767 }
4be130a0
MS
1768 spin_unlock(&sg->guest_table_lock);
1769 /* Make r2t read-only in parent gmap page table */
f1c1174f 1770 raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
4be130a0 1771 origin = r2t & _REGION_ENTRY_ORIGIN;
f1c1174f
HC
1772 offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1773 len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
5c528db0 1774 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
998f637c
DH
1775 spin_lock(&sg->guest_table_lock);
1776 if (!rc) {
1777 table = gmap_table_walk(sg, saddr, 4);
1778 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1779 (unsigned long) s_r2t)
1780 rc = -EAGAIN; /* Race with unshadow */
1781 else
1782 *table &= ~_REGION_ENTRY_INVALID;
1783 } else {
4be130a0 1784 gmap_unshadow_r2t(sg, raddr);
4be130a0 1785 }
998f637c 1786 spin_unlock(&sg->guest_table_lock);
4be130a0
MS
1787 return rc;
1788out_free:
1789 spin_unlock(&sg->guest_table_lock);
f1c1174f 1790 __free_pages(page, CRST_ALLOC_ORDER);
1e133ab2
MS
1791 return rc;
1792}
4be130a0
MS
1793EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1794
1795/**
1796 * gmap_shadow_r3t - create a shadow region 3 table
1797 * @sg: pointer to the shadow guest address space structure
1798 * @saddr: faulting address in the shadow gmap
1799 * @r3t: parent gmap address of the region 3 table to get shadowed
3218f709 1800 * @fake: r3t references contiguous guest memory block, not a r3t
4be130a0
MS
1801 *
1802 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1803 * shadow table structure is incomplete, -ENOMEM if out of memory and
1804 * -EFAULT if an address in the parent gmap could not be resolved.
1805 *
1806 * Called with sg->mm->mmap_sem in read.
1807 */
3218f709
DH
1808int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1809 int fake)
4be130a0
MS
1810{
1811 unsigned long raddr, origin, offset, len;
1812 unsigned long *s_r3t, *table;
1813 struct page *page;
1814 int rc;
1815
1816 BUG_ON(!gmap_is_shadow(sg));
1817 /* Allocate a shadow region second table */
f1c1174f 1818 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
4be130a0
MS
1819 if (!page)
1820 return -ENOMEM;
1821 page->index = r3t & _REGION_ENTRY_ORIGIN;
3218f709
DH
1822 if (fake)
1823 page->index |= GMAP_SHADOW_FAKE_TABLE;
4be130a0
MS
1824 s_r3t = (unsigned long *) page_to_phys(page);
1825 /* Install shadow region second table */
1826 spin_lock(&sg->guest_table_lock);
1827 table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1828 if (!table) {
1829 rc = -EAGAIN; /* Race with unshadow */
1830 goto out_free;
1831 }
1832 if (!(*table & _REGION_ENTRY_INVALID)) {
1833 rc = 0; /* Already established */
1834 goto out_free;
998f637c
DH
1835 } else if (*table & _REGION_ENTRY_ORIGIN) {
1836 rc = -EAGAIN; /* Race with shadow */
4be130a0
MS
1837 }
1838 crst_table_init(s_r3t, _REGION3_ENTRY_EMPTY);
998f637c
DH
1839 /* mark as invalid as long as the parent table is not protected */
1840 *table = (unsigned long) s_r3t | _REGION_ENTRY_LENGTH |
1841 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
fd8d4e3a
DH
1842 if (sg->edat_level >= 1)
1843 *table |= (r3t & _REGION_ENTRY_PROTECT);
4be130a0 1844 list_add(&page->lru, &sg->crst_list);
3218f709
DH
1845 if (fake) {
1846 /* nothing to protect for fake tables */
1847 *table &= ~_REGION_ENTRY_INVALID;
1848 spin_unlock(&sg->guest_table_lock);
1849 return 0;
1850 }
4be130a0
MS
1851 spin_unlock(&sg->guest_table_lock);
1852 /* Make r3t read-only in parent gmap page table */
f1c1174f 1853 raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
4be130a0 1854 origin = r3t & _REGION_ENTRY_ORIGIN;
f1c1174f
HC
1855 offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1856 len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
5c528db0 1857 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
998f637c
DH
1858 spin_lock(&sg->guest_table_lock);
1859 if (!rc) {
1860 table = gmap_table_walk(sg, saddr, 3);
1861 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1862 (unsigned long) s_r3t)
1863 rc = -EAGAIN; /* Race with unshadow */
1864 else
1865 *table &= ~_REGION_ENTRY_INVALID;
1866 } else {
4be130a0 1867 gmap_unshadow_r3t(sg, raddr);
4be130a0 1868 }
998f637c 1869 spin_unlock(&sg->guest_table_lock);
4be130a0
MS
1870 return rc;
1871out_free:
1872 spin_unlock(&sg->guest_table_lock);
f1c1174f 1873 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1874 return rc;
1875}
1876EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1877
1878/**
1879 * gmap_shadow_sgt - create a shadow segment table
1880 * @sg: pointer to the shadow guest address space structure
1881 * @saddr: faulting address in the shadow gmap
1882 * @sgt: parent gmap address of the segment table to get shadowed
18b89809 1883 * @fake: sgt references contiguous guest memory block, not a sgt
4be130a0
MS
1884 *
1885 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1886 * shadow table structure is incomplete, -ENOMEM if out of memory and
1887 * -EFAULT if an address in the parent gmap could not be resolved.
1888 *
1889 * Called with sg->mm->mmap_sem in read.
1890 */
18b89809
DH
1891int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1892 int fake)
4be130a0
MS
1893{
1894 unsigned long raddr, origin, offset, len;
1895 unsigned long *s_sgt, *table;
1896 struct page *page;
1897 int rc;
1898
18b89809 1899 BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
4be130a0 1900 /* Allocate a shadow segment table */
f1c1174f 1901 page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
4be130a0
MS
1902 if (!page)
1903 return -ENOMEM;
1904 page->index = sgt & _REGION_ENTRY_ORIGIN;
18b89809
DH
1905 if (fake)
1906 page->index |= GMAP_SHADOW_FAKE_TABLE;
4be130a0
MS
1907 s_sgt = (unsigned long *) page_to_phys(page);
1908 /* Install shadow region second table */
1909 spin_lock(&sg->guest_table_lock);
1910 table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1911 if (!table) {
1912 rc = -EAGAIN; /* Race with unshadow */
1913 goto out_free;
1914 }
1915 if (!(*table & _REGION_ENTRY_INVALID)) {
1916 rc = 0; /* Already established */
1917 goto out_free;
998f637c
DH
1918 } else if (*table & _REGION_ENTRY_ORIGIN) {
1919 rc = -EAGAIN; /* Race with shadow */
1920 goto out_free;
4be130a0
MS
1921 }
1922 crst_table_init(s_sgt, _SEGMENT_ENTRY_EMPTY);
998f637c
DH
1923 /* mark as invalid as long as the parent table is not protected */
1924 *table = (unsigned long) s_sgt | _REGION_ENTRY_LENGTH |
1925 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
fd8d4e3a
DH
1926 if (sg->edat_level >= 1)
1927 *table |= sgt & _REGION_ENTRY_PROTECT;
4be130a0 1928 list_add(&page->lru, &sg->crst_list);
18b89809
DH
1929 if (fake) {
1930 /* nothing to protect for fake tables */
1931 *table &= ~_REGION_ENTRY_INVALID;
1932 spin_unlock(&sg->guest_table_lock);
1933 return 0;
1934 }
4be130a0
MS
1935 spin_unlock(&sg->guest_table_lock);
1936 /* Make sgt read-only in parent gmap page table */
f1c1174f 1937 raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
4be130a0 1938 origin = sgt & _REGION_ENTRY_ORIGIN;
f1c1174f
HC
1939 offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1940 len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
5c528db0 1941 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
998f637c
DH
1942 spin_lock(&sg->guest_table_lock);
1943 if (!rc) {
1944 table = gmap_table_walk(sg, saddr, 2);
1945 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1946 (unsigned long) s_sgt)
1947 rc = -EAGAIN; /* Race with unshadow */
1948 else
1949 *table &= ~_REGION_ENTRY_INVALID;
1950 } else {
4be130a0 1951 gmap_unshadow_sgt(sg, raddr);
4be130a0 1952 }
998f637c 1953 spin_unlock(&sg->guest_table_lock);
4be130a0
MS
1954 return rc;
1955out_free:
1956 spin_unlock(&sg->guest_table_lock);
f1c1174f 1957 __free_pages(page, CRST_ALLOC_ORDER);
4be130a0
MS
1958 return rc;
1959}
1960EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
1961
1962/**
1963 * gmap_shadow_lookup_pgtable - find a shadow page table
1964 * @sg: pointer to the shadow guest address space structure
1965 * @saddr: the address in the shadow aguest address space
1966 * @pgt: parent gmap address of the page table to get shadowed
1967 * @dat_protection: if the pgtable is marked as protected by dat
fd8d4e3a 1968 * @fake: pgt references contiguous guest memory block, not a pgtable
4be130a0
MS
1969 *
1970 * Returns 0 if the shadow page table was found and -EAGAIN if the page
1971 * table was not found.
1972 *
1973 * Called with sg->mm->mmap_sem in read.
1974 */
1975int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
fd8d4e3a
DH
1976 unsigned long *pgt, int *dat_protection,
1977 int *fake)
4be130a0
MS
1978{
1979 unsigned long *table;
1980 struct page *page;
1981 int rc;
1982
1983 BUG_ON(!gmap_is_shadow(sg));
1984 spin_lock(&sg->guest_table_lock);
1985 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
1986 if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
1987 /* Shadow page tables are full pages (pte+pgste) */
1988 page = pfn_to_page(*table >> PAGE_SHIFT);
fd8d4e3a 1989 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
4be130a0 1990 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
fd8d4e3a 1991 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
4be130a0
MS
1992 rc = 0;
1993 } else {
1994 rc = -EAGAIN;
1995 }
1996 spin_unlock(&sg->guest_table_lock);
1997 return rc;
1998
1999}
2000EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2001
2002/**
2003 * gmap_shadow_pgt - instantiate a shadow page table
2004 * @sg: pointer to the shadow guest address space structure
2005 * @saddr: faulting address in the shadow gmap
2006 * @pgt: parent gmap address of the page table to get shadowed
fd8d4e3a 2007 * @fake: pgt references contiguous guest memory block, not a pgtable
4be130a0
MS
2008 *
2009 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2010 * shadow table structure is incomplete, -ENOMEM if out of memory,
2011 * -EFAULT if an address in the parent gmap could not be resolved and
2012 *
2013 * Called with gmap->mm->mmap_sem in read
2014 */
fd8d4e3a
DH
2015int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2016 int fake)
4be130a0
MS
2017{
2018 unsigned long raddr, origin;
2019 unsigned long *s_pgt, *table;
2020 struct page *page;
2021 int rc;
2022
fd8d4e3a 2023 BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
4be130a0
MS
2024 /* Allocate a shadow page table */
2025 page = page_table_alloc_pgste(sg->mm);
2026 if (!page)
2027 return -ENOMEM;
2028 page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
fd8d4e3a
DH
2029 if (fake)
2030 page->index |= GMAP_SHADOW_FAKE_TABLE;
4be130a0
MS
2031 s_pgt = (unsigned long *) page_to_phys(page);
2032 /* Install shadow page table */
2033 spin_lock(&sg->guest_table_lock);
2034 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2035 if (!table) {
2036 rc = -EAGAIN; /* Race with unshadow */
2037 goto out_free;
2038 }
2039 if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2040 rc = 0; /* Already established */
2041 goto out_free;
998f637c
DH
2042 } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2043 rc = -EAGAIN; /* Race with shadow */
2044 goto out_free;
4be130a0 2045 }
998f637c 2046 /* mark as invalid as long as the parent table is not protected */
4be130a0 2047 *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
998f637c 2048 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
4be130a0 2049 list_add(&page->lru, &sg->pt_list);
fd8d4e3a
DH
2050 if (fake) {
2051 /* nothing to protect for fake tables */
2052 *table &= ~_SEGMENT_ENTRY_INVALID;
2053 spin_unlock(&sg->guest_table_lock);
2054 return 0;
2055 }
4be130a0
MS
2056 spin_unlock(&sg->guest_table_lock);
2057 /* Make pgt read-only in parent gmap page table (not the pgste) */
f1c1174f 2058 raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
4be130a0 2059 origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
5c528db0 2060 rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
998f637c
DH
2061 spin_lock(&sg->guest_table_lock);
2062 if (!rc) {
2063 table = gmap_table_walk(sg, saddr, 1);
2064 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) !=
2065 (unsigned long) s_pgt)
2066 rc = -EAGAIN; /* Race with unshadow */
2067 else
2068 *table &= ~_SEGMENT_ENTRY_INVALID;
2069 } else {
4be130a0 2070 gmap_unshadow_pgt(sg, raddr);
4be130a0 2071 }
998f637c 2072 spin_unlock(&sg->guest_table_lock);
4be130a0
MS
2073 return rc;
2074out_free:
2075 spin_unlock(&sg->guest_table_lock);
2076 page_table_free_pgste(page);
2077 return rc;
2078
2079}
2080EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2081
2082/**
2083 * gmap_shadow_page - create a shadow page mapping
2084 * @sg: pointer to the shadow guest address space structure
2085 * @saddr: faulting address in the shadow gmap
a9d23e71 2086 * @pte: pte in parent gmap address space to get shadowed
4be130a0
MS
2087 *
2088 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2089 * shadow table structure is incomplete, -ENOMEM if out of memory and
2090 * -EFAULT if an address in the parent gmap could not be resolved.
2091 *
2092 * Called with sg->mm->mmap_sem in read.
2093 */
a9d23e71 2094int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
4be130a0
MS
2095{
2096 struct gmap *parent;
2097 struct gmap_rmap *rmap;
a9d23e71 2098 unsigned long vmaddr, paddr;
4be130a0
MS
2099 spinlock_t *ptl;
2100 pte_t *sptep, *tptep;
01f71917 2101 int prot;
4be130a0
MS
2102 int rc;
2103
2104 BUG_ON(!gmap_is_shadow(sg));
2105 parent = sg->parent;
01f71917 2106 prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
4be130a0
MS
2107
2108 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
2109 if (!rmap)
2110 return -ENOMEM;
2111 rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2112
2113 while (1) {
a9d23e71 2114 paddr = pte_val(pte) & PAGE_MASK;
4be130a0
MS
2115 vmaddr = __gmap_translate(parent, paddr);
2116 if (IS_ERR_VALUE(vmaddr)) {
2117 rc = vmaddr;
2118 break;
2119 }
2120 rc = radix_tree_preload(GFP_KERNEL);
2121 if (rc)
2122 break;
2123 rc = -EAGAIN;
2124 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2125 if (sptep) {
2126 spin_lock(&sg->guest_table_lock);
2127 /* Get page table pointer */
2128 tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2129 if (!tptep) {
2130 spin_unlock(&sg->guest_table_lock);
2131 gmap_pte_op_end(ptl);
2132 radix_tree_preload_end();
2133 break;
2134 }
a9d23e71 2135 rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
4be130a0
MS
2136 if (rc > 0) {
2137 /* Success and a new mapping */
2138 gmap_insert_rmap(sg, vmaddr, rmap);
2139 rmap = NULL;
2140 rc = 0;
2141 }
2142 gmap_pte_op_end(ptl);
2143 spin_unlock(&sg->guest_table_lock);
2144 }
2145 radix_tree_preload_end();
2146 if (!rc)
2147 break;
01f71917 2148 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
4be130a0
MS
2149 if (rc)
2150 break;
2151 }
2152 kfree(rmap);
2153 return rc;
2154}
2155EXPORT_SYMBOL_GPL(gmap_shadow_page);
2156
2157/**
2158 * gmap_shadow_notify - handle notifications for shadow gmap
2159 *
2160 * Called with sg->parent->shadow_lock.
2161 */
2162static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
c0b4bd21 2163 unsigned long gaddr)
4be130a0
MS
2164{
2165 struct gmap_rmap *rmap, *rnext, *head;
2fa5ed7d 2166 unsigned long start, end, bits, raddr;
4be130a0
MS
2167
2168 BUG_ON(!gmap_is_shadow(sg));
4be130a0
MS
2169
2170 spin_lock(&sg->guest_table_lock);
2171 if (sg->removed) {
2172 spin_unlock(&sg->guest_table_lock);
2173 return;
2174 }
2175 /* Check for top level table */
2176 start = sg->orig_asce & _ASCE_ORIGIN;
f1c1174f 2177 end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
3218f709
DH
2178 if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2179 gaddr < end) {
4be130a0
MS
2180 /* The complete shadow table has to go */
2181 gmap_unshadow(sg);
2182 spin_unlock(&sg->guest_table_lock);
2183 list_del(&sg->list);
2184 gmap_put(sg);
2185 return;
2186 }
2187 /* Remove the page table tree from on specific entry */
f1c1174f 2188 head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
4be130a0
MS
2189 gmap_for_each_rmap_safe(rmap, rnext, head) {
2190 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2191 raddr = rmap->raddr ^ bits;
2192 switch (bits) {
2193 case _SHADOW_RMAP_REGION1:
2194 gmap_unshadow_r2t(sg, raddr);
2195 break;
2196 case _SHADOW_RMAP_REGION2:
2197 gmap_unshadow_r3t(sg, raddr);
2198 break;
2199 case _SHADOW_RMAP_REGION3:
2200 gmap_unshadow_sgt(sg, raddr);
2201 break;
2202 case _SHADOW_RMAP_SEGMENT:
2203 gmap_unshadow_pgt(sg, raddr);
2204 break;
2205 case _SHADOW_RMAP_PGTABLE:
2206 gmap_unshadow_page(sg, raddr);
2207 break;
2208 }
2209 kfree(rmap);
2210 }
2211 spin_unlock(&sg->guest_table_lock);
2212}
1e133ab2
MS
2213
2214/**
2215 * ptep_notify - call all invalidation callbacks for a specific pte.
2216 * @mm: pointer to the process mm_struct
2217 * @addr: virtual address in the process address space
2218 * @pte: pointer to the page table entry
4be130a0 2219 * @bits: bits from the pgste that caused the notify call
1e133ab2
MS
2220 *
2221 * This function is assumed to be called with the page table lock held
2222 * for the pte to notify.
2223 */
4be130a0
MS
2224void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2225 pte_t *pte, unsigned long bits)
1e133ab2 2226{
2fa5ed7d 2227 unsigned long offset, gaddr = 0;
1e133ab2 2228 unsigned long *table;
4be130a0 2229 struct gmap *gmap, *sg, *next;
1e133ab2
MS
2230
2231 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
f1c1174f 2232 offset = offset * (PAGE_SIZE / sizeof(pte_t));
8ecb1a59
MS
2233 rcu_read_lock();
2234 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2235 spin_lock(&gmap->guest_table_lock);
1e133ab2
MS
2236 table = radix_tree_lookup(&gmap->host_to_guest,
2237 vmaddr >> PMD_SHIFT);
8ecb1a59
MS
2238 if (table)
2239 gaddr = __gmap_segment_gaddr(table) + offset;
2240 spin_unlock(&gmap->guest_table_lock);
2fa5ed7d
JF
2241 if (!table)
2242 continue;
2243
2244 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2245 spin_lock(&gmap->shadow_lock);
2246 list_for_each_entry_safe(sg, next,
2247 &gmap->children, list)
c0b4bd21 2248 gmap_shadow_notify(sg, vmaddr, gaddr);
2fa5ed7d
JF
2249 spin_unlock(&gmap->shadow_lock);
2250 }
2251 if (bits & PGSTE_IN_BIT)
8ecb1a59 2252 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
1e133ab2 2253 }
8ecb1a59 2254 rcu_read_unlock();
1e133ab2
MS
2255}
2256EXPORT_SYMBOL_GPL(ptep_notify);
2257
6a376277
JF
2258static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2259 unsigned long gaddr)
2260{
2261 pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_IN;
2262 gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2263}
2264
0959e168
JF
2265/**
2266 * gmap_pmdp_xchg - exchange a gmap pmd with another
2267 * @gmap: pointer to the guest address space structure
2268 * @pmdp: pointer to the pmd entry
2269 * @new: replacement entry
2270 * @gaddr: the affected guest address
2271 *
2272 * This function is assumed to be called with the guest_table_lock
2273 * held.
2274 */
2275static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2276 unsigned long gaddr)
2277{
2278 gaddr &= HPAGE_MASK;
2279 pmdp_notify_gmap(gmap, pmdp, gaddr);
2280 pmd_val(new) &= ~_SEGMENT_ENTRY_GMAP_IN;
2281 if (MACHINE_HAS_TLB_GUEST)
2282 __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2283 IDTE_GLOBAL);
2284 else if (MACHINE_HAS_IDTE)
2285 __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2286 else
2287 __pmdp_csp(pmdp);
2288 *pmdp = new;
2289}
2290
6a376277
JF
2291static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2292 int purge)
2293{
2294 pmd_t *pmdp;
2295 struct gmap *gmap;
2296 unsigned long gaddr;
2297
2298 rcu_read_lock();
2299 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2300 spin_lock(&gmap->guest_table_lock);
2301 pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2302 vmaddr >> PMD_SHIFT);
2303 if (pmdp) {
2304 gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2305 pmdp_notify_gmap(gmap, pmdp, gaddr);
0959e168
JF
2306 WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2307 _SEGMENT_ENTRY_GMAP_UC));
6a376277
JF
2308 if (purge)
2309 __pmdp_csp(pmdp);
2310 pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
2311 }
2312 spin_unlock(&gmap->guest_table_lock);
2313 }
2314 rcu_read_unlock();
2315}
2316
2317/**
2318 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2319 * flushing
2320 * @mm: pointer to the process mm_struct
2321 * @vmaddr: virtual address in the process address space
2322 */
2323void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2324{
2325 gmap_pmdp_clear(mm, vmaddr, 0);
2326}
2327EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2328
2329/**
2330 * gmap_pmdp_csp - csp all affected guest pmd entries
2331 * @mm: pointer to the process mm_struct
2332 * @vmaddr: virtual address in the process address space
2333 */
2334void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2335{
2336 gmap_pmdp_clear(mm, vmaddr, 1);
2337}
2338EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2339
2340/**
2341 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2342 * @mm: pointer to the process mm_struct
2343 * @vmaddr: virtual address in the process address space
2344 */
2345void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2346{
2347 unsigned long *entry, gaddr;
2348 struct gmap *gmap;
2349 pmd_t *pmdp;
2350
2351 rcu_read_lock();
2352 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2353 spin_lock(&gmap->guest_table_lock);
2354 entry = radix_tree_delete(&gmap->host_to_guest,
2355 vmaddr >> PMD_SHIFT);
2356 if (entry) {
2357 pmdp = (pmd_t *)entry;
2358 gaddr = __gmap_segment_gaddr(entry);
2359 pmdp_notify_gmap(gmap, pmdp, gaddr);
0959e168
JF
2360 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2361 _SEGMENT_ENTRY_GMAP_UC));
6a376277
JF
2362 if (MACHINE_HAS_TLB_GUEST)
2363 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2364 gmap->asce, IDTE_LOCAL);
2365 else if (MACHINE_HAS_IDTE)
2366 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2367 *entry = _SEGMENT_ENTRY_EMPTY;
2368 }
2369 spin_unlock(&gmap->guest_table_lock);
2370 }
2371 rcu_read_unlock();
2372}
2373EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2374
2375/**
2376 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2377 * @mm: pointer to the process mm_struct
2378 * @vmaddr: virtual address in the process address space
2379 */
2380void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2381{
2382 unsigned long *entry, gaddr;
2383 struct gmap *gmap;
2384 pmd_t *pmdp;
2385
2386 rcu_read_lock();
2387 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2388 spin_lock(&gmap->guest_table_lock);
2389 entry = radix_tree_delete(&gmap->host_to_guest,
2390 vmaddr >> PMD_SHIFT);
2391 if (entry) {
2392 pmdp = (pmd_t *)entry;
2393 gaddr = __gmap_segment_gaddr(entry);
2394 pmdp_notify_gmap(gmap, pmdp, gaddr);
0959e168
JF
2395 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2396 _SEGMENT_ENTRY_GMAP_UC));
6a376277
JF
2397 if (MACHINE_HAS_TLB_GUEST)
2398 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2399 gmap->asce, IDTE_GLOBAL);
2400 else if (MACHINE_HAS_IDTE)
2401 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2402 else
2403 __pmdp_csp(pmdp);
2404 *entry = _SEGMENT_ENTRY_EMPTY;
2405 }
2406 spin_unlock(&gmap->guest_table_lock);
2407 }
2408 rcu_read_unlock();
2409}
2410EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2411
0959e168
JF
2412/**
2413 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2414 * @gmap: pointer to guest address space
2415 * @pmdp: pointer to the pmd to be tested
2416 * @gaddr: virtual address in the guest address space
2417 *
2418 * This function is assumed to be called with the guest_table_lock
2419 * held.
2420 */
2421bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2422 unsigned long gaddr)
2423{
2424 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2425 return false;
2426
2427 /* Already protected memory, which did not change is clean */
2428 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2429 !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2430 return false;
2431
2432 /* Clear UC indication and reset protection */
2433 pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_UC;
2434 gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2435 return true;
2436}
2437
2438/**
2439 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2440 * @gmap: pointer to guest address space
2441 * @bitmap: dirty bitmap for this pmd
2442 * @gaddr: virtual address in the guest address space
2443 * @vmaddr: virtual address in the host address space
2444 *
2445 * This function is assumed to be called with the guest_table_lock
2446 * held.
2447 */
2448void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2449 unsigned long gaddr, unsigned long vmaddr)
2450{
2451 int i;
2452 pmd_t *pmdp;
2453 pte_t *ptep;
2454 spinlock_t *ptl;
2455
2456 pmdp = gmap_pmd_op_walk(gmap, gaddr);
2457 if (!pmdp)
2458 return;
2459
2460 if (pmd_large(*pmdp)) {
2461 if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2462 bitmap_fill(bitmap, _PAGE_ENTRIES);
2463 } else {
2464 for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2465 ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2466 if (!ptep)
2467 continue;
2468 if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2469 set_bit(i, bitmap);
2470 spin_unlock(ptl);
2471 }
2472 }
2473 gmap_pmd_op_end(gmap, pmdp);
2474}
2475EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2476
1e133ab2
MS
2477static inline void thp_split_mm(struct mm_struct *mm)
2478{
2479#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2480 struct vm_area_struct *vma;
2481 unsigned long addr;
2482
2483 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
2484 for (addr = vma->vm_start;
2485 addr < vma->vm_end;
2486 addr += PAGE_SIZE)
2487 follow_page(vma, addr, FOLL_SPLIT);
2488 vma->vm_flags &= ~VM_HUGEPAGE;
2489 vma->vm_flags |= VM_NOHUGEPAGE;
2490 }
2491 mm->def_flags |= VM_NOHUGEPAGE;
2492#endif
2493}
2494
fa41ba0d
CB
2495/*
2496 * Remove all empty zero pages from the mapping for lazy refaulting
2497 * - This must be called after mm->context.has_pgste is set, to avoid
2498 * future creation of zero pages
2499 * - This must be called after THP was enabled
2500 */
2501static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
2502 unsigned long end, struct mm_walk *walk)
2503{
2504 unsigned long addr;
2505
2506 for (addr = start; addr != end; addr += PAGE_SIZE) {
2507 pte_t *ptep;
2508 spinlock_t *ptl;
2509
2510 ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
2511 if (is_zero_pfn(pte_pfn(*ptep)))
2512 ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
2513 pte_unmap_unlock(ptep, ptl);
2514 }
2515 return 0;
2516}
2517
2518static inline void zap_zero_pages(struct mm_struct *mm)
2519{
2520 struct mm_walk walk = { .pmd_entry = __zap_zero_pages };
2521
2522 walk.mm = mm;
2523 walk_page_range(0, TASK_SIZE, &walk);
2524}
2525
1e133ab2
MS
2526/*
2527 * switch on pgstes for its userspace process (for kvm)
2528 */
2529int s390_enable_sie(void)
2530{
2531 struct mm_struct *mm = current->mm;
2532
2533 /* Do we have pgstes? if yes, we are done */
2534 if (mm_has_pgste(mm))
2535 return 0;
2536 /* Fail if the page tables are 2K */
2537 if (!mm_alloc_pgste(mm))
2538 return -EINVAL;
2539 down_write(&mm->mmap_sem);
2540 mm->context.has_pgste = 1;
2541 /* split thp mappings and disable thp for future mappings */
2542 thp_split_mm(mm);
fa41ba0d 2543 zap_zero_pages(mm);
1e133ab2
MS
2544 up_write(&mm->mmap_sem);
2545 return 0;
2546}
2547EXPORT_SYMBOL_GPL(s390_enable_sie);
2548
2549/*
2550 * Enable storage key handling from now on and initialize the storage
2551 * keys with the default key.
2552 */
964c2c05
DD
2553static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2554 unsigned long next, struct mm_walk *walk)
1e133ab2 2555{
1e133ab2
MS
2556 /* Clear storage key */
2557 ptep_zap_key(walk->mm, addr, pte);
2558 return 0;
2559}
2560
964c2c05
DD
2561static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2562 unsigned long hmask, unsigned long next,
2563 struct mm_walk *walk)
2564{
2565 pmd_t *pmd = (pmd_t *)pte;
2566 unsigned long start, end;
3afdfca6 2567 struct page *page = pmd_page(*pmd);
964c2c05
DD
2568
2569 /*
2570 * The write check makes sure we do not set a key on shared
2571 * memory. This is needed as the walker does not differentiate
2572 * between actual guest memory and the process executable or
2573 * shared libraries.
2574 */
2575 if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2576 !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2577 return 0;
2578
2579 start = pmd_val(*pmd) & HPAGE_MASK;
2580 end = start + HPAGE_SIZE - 1;
2581 __storage_key_init_range(start, end);
3afdfca6 2582 set_bit(PG_arch_1, &page->flags);
964c2c05
DD
2583 return 0;
2584}
2585
1e133ab2
MS
2586int s390_enable_skey(void)
2587{
964c2c05
DD
2588 struct mm_walk walk = {
2589 .hugetlb_entry = __s390_enable_skey_hugetlb,
2590 .pte_entry = __s390_enable_skey_pte,
2591 };
1e133ab2
MS
2592 struct mm_struct *mm = current->mm;
2593 struct vm_area_struct *vma;
2594 int rc = 0;
2595
2596 down_write(&mm->mmap_sem);
55531b74 2597 if (mm_uses_skeys(mm))
1e133ab2
MS
2598 goto out_up;
2599
55531b74 2600 mm->context.uses_skeys = 1;
1e133ab2
MS
2601 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2602 if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
2603 MADV_UNMERGEABLE, &vma->vm_flags)) {
55531b74 2604 mm->context.uses_skeys = 0;
1e133ab2
MS
2605 rc = -ENOMEM;
2606 goto out_up;
2607 }
2608 }
2609 mm->def_flags &= ~VM_MERGEABLE;
2610
2611 walk.mm = mm;
2612 walk_page_range(0, TASK_SIZE, &walk);
2613
2614out_up:
2615 up_write(&mm->mmap_sem);
2616 return rc;
2617}
2618EXPORT_SYMBOL_GPL(s390_enable_skey);
2619
2620/*
2621 * Reset CMMA state, make all pages stable again.
2622 */
2623static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2624 unsigned long next, struct mm_walk *walk)
2625{
2626 ptep_zap_unused(walk->mm, addr, pte, 1);
2627 return 0;
2628}
2629
2630void s390_reset_cmma(struct mm_struct *mm)
2631{
2632 struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
2633
2634 down_write(&mm->mmap_sem);
2635 walk.mm = mm;
2636 walk_page_range(0, TASK_SIZE, &walk);
2637 up_write(&mm->mmap_sem);
2638}
2639EXPORT_SYMBOL_GPL(s390_reset_cmma);