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