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