blob: e24126208614ba063b46e234227bc9e2577ad6a8 [file] [log] [blame]
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * Copyright IBM Corp. 2007, 2011
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02003 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
4 */
5
6#include <linux/sched.h>
7#include <linux/kernel.h>
8#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020010#include <linux/mm.h>
11#include <linux/swap.h>
12#include <linux/smp.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020013#include <linux/spinlock.h>
Martin Schwidefsky80217142010-10-25 16:10:11 +020014#include <linux/rcupdate.h>
Martin Schwidefskye5992f22011-07-24 10:48:20 +020015#include <linux/slab.h>
Konstantin Weitzb31288f2013-04-17 17:36:29 +020016#include <linux/swapops.h>
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +020017#include <linux/sysctl.h>
Dominik Dingel3ac8e382014-10-23 12:09:17 +020018#include <linux/ksm.h>
19#include <linux/mman.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020020
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020021#include <asm/pgtable.h>
22#include <asm/pgalloc.h>
23#include <asm/tlb.h>
24#include <asm/tlbflush.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010025#include <asm/mmu_context.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020026
Martin Schwidefsky043d0702011-05-23 10:24:23 +020027unsigned long *crst_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020028{
Martin Schwidefsky78fb9072015-08-14 14:58:50 +020029 struct page *page = alloc_pages(GFP_KERNEL, 2);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020030
31 if (!page)
32 return NULL;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020033 return (unsigned long *) page_to_phys(page);
34}
35
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010036void crst_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020037{
Martin Schwidefsky78fb9072015-08-14 14:58:50 +020038 free_pages((unsigned long) table, 2);
Martin Schwidefsky80217142010-10-25 16:10:11 +020039}
40
Martin Schwidefsky10607862013-10-28 14:48:30 +010041static void __crst_table_upgrade(void *arg)
42{
43 struct mm_struct *mm = arg;
44
Martin Schwidefskybeef5602014-04-14 15:11:26 +020045 if (current->active_mm == mm) {
46 clear_user_asce();
47 set_user_asce(mm);
48 }
Martin Schwidefsky10607862013-10-28 14:48:30 +010049 __tlb_flush_local();
50}
51
Martin Schwidefsky6252d702008-02-09 18:24:37 +010052int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
53{
54 unsigned long *table, *pgd;
55 unsigned long entry;
Martin Schwidefsky10607862013-10-28 14:48:30 +010056 int flush;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010057
Dominik Dingela9d7ab92016-01-11 11:47:12 +010058 BUG_ON(limit > TASK_MAX_SIZE);
Martin Schwidefsky10607862013-10-28 14:48:30 +010059 flush = 0;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010060repeat:
Martin Schwidefsky043d0702011-05-23 10:24:23 +020061 table = crst_table_alloc(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010062 if (!table)
63 return -ENOMEM;
Martin Schwidefsky80217142010-10-25 16:10:11 +020064 spin_lock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010065 if (mm->context.asce_limit < limit) {
66 pgd = (unsigned long *) mm->pgd;
67 if (mm->context.asce_limit <= (1UL << 31)) {
68 entry = _REGION3_ENTRY_EMPTY;
69 mm->context.asce_limit = 1UL << 42;
70 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
71 _ASCE_USER_BITS |
72 _ASCE_TYPE_REGION3;
73 } else {
74 entry = _REGION2_ENTRY_EMPTY;
75 mm->context.asce_limit = 1UL << 53;
76 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
77 _ASCE_USER_BITS |
78 _ASCE_TYPE_REGION2;
79 }
80 crst_table_init(table, entry);
81 pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
82 mm->pgd = (pgd_t *) table;
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +010083 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010084 table = NULL;
Martin Schwidefsky10607862013-10-28 14:48:30 +010085 flush = 1;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010086 }
Martin Schwidefsky80217142010-10-25 16:10:11 +020087 spin_unlock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010088 if (table)
89 crst_table_free(mm, table);
90 if (mm->context.asce_limit < limit)
91 goto repeat;
Martin Schwidefsky10607862013-10-28 14:48:30 +010092 if (flush)
93 on_each_cpu(__crst_table_upgrade, mm, 0);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010094 return 0;
95}
96
97void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
98{
99 pgd_t *pgd;
100
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200101 if (current->active_mm == mm) {
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200102 clear_user_asce();
Martin Schwidefsky10607862013-10-28 14:48:30 +0100103 __tlb_flush_mm(mm);
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200104 }
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100105 while (mm->context.asce_limit > limit) {
106 pgd = mm->pgd;
107 switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
108 case _REGION_ENTRY_TYPE_R2:
109 mm->context.asce_limit = 1UL << 42;
110 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
111 _ASCE_USER_BITS |
112 _ASCE_TYPE_REGION3;
113 break;
114 case _REGION_ENTRY_TYPE_R3:
115 mm->context.asce_limit = 1UL << 31;
116 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
117 _ASCE_USER_BITS |
118 _ASCE_TYPE_SEGMENT;
119 break;
120 default:
121 BUG();
122 }
123 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +0100124 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100125 crst_table_free(mm, (unsigned long *) pgd);
126 }
Martin Schwidefsky10607862013-10-28 14:48:30 +0100127 if (current->active_mm == mm)
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200128 set_user_asce(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100129}
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100130
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200131#ifdef CONFIG_PGSTE
132
133/**
134 * gmap_alloc - allocate a guest address space
135 * @mm: pointer to the parent mm_struct
Dominik Dingela3a92c32014-12-01 17:24:42 +0100136 * @limit: maximum address of the gmap address space
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200137 *
138 * Returns a guest address space structure.
139 */
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200140struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200141{
142 struct gmap *gmap;
143 struct page *page;
144 unsigned long *table;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200145 unsigned long etype, atype;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200146
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200147 if (limit < (1UL << 31)) {
148 limit = (1UL << 31) - 1;
149 atype = _ASCE_TYPE_SEGMENT;
150 etype = _SEGMENT_ENTRY_EMPTY;
151 } else if (limit < (1UL << 42)) {
152 limit = (1UL << 42) - 1;
153 atype = _ASCE_TYPE_REGION3;
154 etype = _REGION3_ENTRY_EMPTY;
155 } else if (limit < (1UL << 53)) {
156 limit = (1UL << 53) - 1;
157 atype = _ASCE_TYPE_REGION2;
158 etype = _REGION2_ENTRY_EMPTY;
159 } else {
160 limit = -1UL;
161 atype = _ASCE_TYPE_REGION1;
162 etype = _REGION1_ENTRY_EMPTY;
163 }
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200164 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
165 if (!gmap)
166 goto out;
167 INIT_LIST_HEAD(&gmap->crst_list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200168 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
169 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
170 spin_lock_init(&gmap->guest_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200171 gmap->mm = mm;
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200172 page = alloc_pages(GFP_KERNEL, 2);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200173 if (!page)
174 goto out_free;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200175 page->index = 0;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200176 list_add(&page->lru, &gmap->crst_list);
177 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200178 crst_table_init(table, etype);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200179 gmap->table = table;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200180 gmap->asce = atype | _ASCE_TABLE_LENGTH |
181 _ASCE_USER_BITS | __pa(table);
182 gmap->asce_end = limit;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200183 down_write(&mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200184 list_add(&gmap->list, &mm->context.gmap_list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200185 up_write(&mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200186 return gmap;
187
188out_free:
189 kfree(gmap);
190out:
191 return NULL;
192}
193EXPORT_SYMBOL_GPL(gmap_alloc);
194
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200195static void gmap_flush_tlb(struct gmap *gmap)
196{
197 if (MACHINE_HAS_IDTE)
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200198 __tlb_flush_asce(gmap->mm, gmap->asce);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200199 else
200 __tlb_flush_global();
201}
202
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200203static void gmap_radix_tree_free(struct radix_tree_root *root)
204{
205 struct radix_tree_iter iter;
206 unsigned long indices[16];
207 unsigned long index;
208 void **slot;
209 int i, nr;
210
211 /* A radix tree is freed by deleting all of its entries */
212 index = 0;
213 do {
214 nr = 0;
215 radix_tree_for_each_slot(slot, root, &iter, index) {
216 indices[nr] = iter.index;
217 if (++nr == 16)
218 break;
219 }
220 for (i = 0; i < nr; i++) {
221 index = indices[i];
222 radix_tree_delete(root, index);
223 }
224 } while (nr > 0);
225}
226
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200227/**
228 * gmap_free - free a guest address space
229 * @gmap: pointer to the guest address space structure
230 */
231void gmap_free(struct gmap *gmap)
232{
233 struct page *page, *next;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200234
235 /* Flush tlb. */
236 if (MACHINE_HAS_IDTE)
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200237 __tlb_flush_asce(gmap->mm, gmap->asce);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200238 else
239 __tlb_flush_global();
240
241 /* Free all segment & region tables. */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200242 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200243 __free_pages(page, 2);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200244 gmap_radix_tree_free(&gmap->guest_to_host);
245 gmap_radix_tree_free(&gmap->host_to_guest);
246 down_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200247 list_del(&gmap->list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200248 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200249 kfree(gmap);
250}
251EXPORT_SYMBOL_GPL(gmap_free);
252
253/**
254 * gmap_enable - switch primary space to the guest address space
255 * @gmap: pointer to the guest address space structure
256 */
257void gmap_enable(struct gmap *gmap)
258{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200259 S390_lowcore.gmap = (unsigned long) gmap;
260}
261EXPORT_SYMBOL_GPL(gmap_enable);
262
263/**
264 * gmap_disable - switch back to the standard primary address space
265 * @gmap: pointer to the guest address space structure
266 */
267void gmap_disable(struct gmap *gmap)
268{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200269 S390_lowcore.gmap = 0UL;
270}
271EXPORT_SYMBOL_GPL(gmap_disable);
272
Carsten Ottea9162f22011-10-30 15:17:00 +0100273/*
274 * gmap_alloc_table is assumed to be called with mmap_sem held
275 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200276static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
277 unsigned long init, unsigned long gaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200278{
279 struct page *page;
280 unsigned long *new;
281
Christian Borntraegerc86cce22011-12-27 11:25:47 +0100282 /* since we dont free the gmap table until gmap_free we can unlock */
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200283 page = alloc_pages(GFP_KERNEL, 2);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200284 if (!page)
285 return -ENOMEM;
286 new = (unsigned long *) page_to_phys(page);
287 crst_table_init(new, init);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200288 spin_lock(&gmap->mm->page_table_lock);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200289 if (*table & _REGION_ENTRY_INVALID) {
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200290 list_add(&page->lru, &gmap->crst_list);
291 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
292 (*table & _REGION_ENTRY_TYPE_MASK);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200293 page->index = gaddr;
294 page = NULL;
295 }
296 spin_unlock(&gmap->mm->page_table_lock);
297 if (page)
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200298 __free_pages(page, 2);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200299 return 0;
300}
301
302/**
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200303 * __gmap_segment_gaddr - find virtual address from segment pointer
304 * @entry: pointer to a segment table entry in the guest address space
305 *
306 * Returns the virtual address in the guest address space for the segment
307 */
308static unsigned long __gmap_segment_gaddr(unsigned long *entry)
309{
310 struct page *page;
Martin Schwidefskyfbc89c92015-01-07 11:00:02 +0100311 unsigned long offset, mask;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200312
313 offset = (unsigned long) entry / sizeof(unsigned long);
314 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
Martin Schwidefskyfbc89c92015-01-07 11:00:02 +0100315 mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
316 page = virt_to_page((void *)((unsigned long) entry & mask));
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200317 return page->index + offset;
318}
319
320/**
321 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
322 * @gmap: pointer to the guest address space structure
323 * @vmaddr: address in the host process address space
324 *
325 * Returns 1 if a TLB flush is required
326 */
327static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
328{
329 unsigned long *entry;
330 int flush = 0;
331
332 spin_lock(&gmap->guest_table_lock);
333 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
334 if (entry) {
335 flush = (*entry != _SEGMENT_ENTRY_INVALID);
336 *entry = _SEGMENT_ENTRY_INVALID;
337 }
338 spin_unlock(&gmap->guest_table_lock);
339 return flush;
340}
341
342/**
343 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
344 * @gmap: pointer to the guest address space structure
345 * @gaddr: address in the guest address space
346 *
347 * Returns 1 if a TLB flush is required
348 */
349static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
350{
351 unsigned long vmaddr;
352
353 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
354 gaddr >> PMD_SHIFT);
355 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
356}
357
358/**
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200359 * gmap_unmap_segment - unmap segment from the guest address space
360 * @gmap: pointer to the guest address space structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200361 * @to: address in the guest address space
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200362 * @len: length of the memory area to unmap
363 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100364 * Returns 0 if the unmap succeeded, -EINVAL if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200365 */
366int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
367{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200368 unsigned long off;
369 int flush;
370
371 if ((to | len) & (PMD_SIZE - 1))
372 return -EINVAL;
373 if (len == 0 || to + len < to)
374 return -EINVAL;
375
376 flush = 0;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200377 down_write(&gmap->mm->mmap_sem);
378 for (off = 0; off < len; off += PMD_SIZE)
379 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
380 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200381 if (flush)
382 gmap_flush_tlb(gmap);
383 return 0;
384}
385EXPORT_SYMBOL_GPL(gmap_unmap_segment);
386
387/**
388 * gmap_mmap_segment - map a segment to the guest address space
389 * @gmap: pointer to the guest address space structure
390 * @from: source address in the parent address space
391 * @to: target address in the guest address space
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200392 * @len: length of the memory area to map
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200393 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100394 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200395 */
396int gmap_map_segment(struct gmap *gmap, unsigned long from,
397 unsigned long to, unsigned long len)
398{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200399 unsigned long off;
400 int flush;
401
402 if ((from | to | len) & (PMD_SIZE - 1))
403 return -EINVAL;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200404 if (len == 0 || from + len < from || to + len < to ||
Dominik Dingela3a92c32014-12-01 17:24:42 +0100405 from + len - 1 > TASK_MAX_SIZE || to + len - 1 > gmap->asce_end)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200406 return -EINVAL;
407
408 flush = 0;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200409 down_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200410 for (off = 0; off < len; off += PMD_SIZE) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200411 /* Remove old translation */
412 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
413 /* Store new translation */
414 if (radix_tree_insert(&gmap->guest_to_host,
415 (to + off) >> PMD_SHIFT,
416 (void *) from + off))
417 break;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200418 }
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200419 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200420 if (flush)
421 gmap_flush_tlb(gmap);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200422 if (off >= len)
423 return 0;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200424 gmap_unmap_segment(gmap, to, len);
425 return -ENOMEM;
426}
427EXPORT_SYMBOL_GPL(gmap_map_segment);
428
Heiko Carstensc5034942012-09-10 16:14:33 +0200429/**
430 * __gmap_translate - translate a guest address to a user space address
Heiko Carstensc5034942012-09-10 16:14:33 +0200431 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200432 * @gaddr: guest address
Heiko Carstensc5034942012-09-10 16:14:33 +0200433 *
434 * Returns user space address which corresponds to the guest address or
435 * -EFAULT if no such mapping exists.
436 * This function does not establish potentially missing page table entries.
437 * The mmap_sem of the mm that belongs to the address space must be held
438 * when this function gets called.
439 */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200440unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
Heiko Carstensc5034942012-09-10 16:14:33 +0200441{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200442 unsigned long vmaddr;
Heiko Carstensc5034942012-09-10 16:14:33 +0200443
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200444 vmaddr = (unsigned long)
445 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
446 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
Heiko Carstensc5034942012-09-10 16:14:33 +0200447}
448EXPORT_SYMBOL_GPL(__gmap_translate);
449
450/**
451 * gmap_translate - translate a guest address to a user space address
Heiko Carstensc5034942012-09-10 16:14:33 +0200452 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200453 * @gaddr: guest address
Heiko Carstensc5034942012-09-10 16:14:33 +0200454 *
455 * Returns user space address which corresponds to the guest address or
456 * -EFAULT if no such mapping exists.
457 * This function does not establish potentially missing page table entries.
458 */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200459unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
Heiko Carstensc5034942012-09-10 16:14:33 +0200460{
461 unsigned long rc;
462
463 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200464 rc = __gmap_translate(gmap, gaddr);
Heiko Carstensc5034942012-09-10 16:14:33 +0200465 up_read(&gmap->mm->mmap_sem);
466 return rc;
467}
468EXPORT_SYMBOL_GPL(gmap_translate);
469
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200470/**
471 * gmap_unlink - disconnect a page table from the gmap shadow tables
472 * @gmap: pointer to guest mapping meta data structure
473 * @table: pointer to the host page table
474 * @vmaddr: vm address associated with the host page table
475 */
476static void gmap_unlink(struct mm_struct *mm, unsigned long *table,
477 unsigned long vmaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200478{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200479 struct gmap *gmap;
480 int flush;
481
482 list_for_each_entry(gmap, &mm->context.gmap_list, list) {
483 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
484 if (flush)
485 gmap_flush_tlb(gmap);
486 }
487}
488
489/**
490 * gmap_link - set up shadow page tables to connect a host to a guest address
491 * @gmap: pointer to guest mapping meta data structure
492 * @gaddr: guest address
493 * @vmaddr: vm address
494 *
495 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
496 * if the vm address is already mapped to a different guest segment.
497 * The mmap_sem of the mm that belongs to the address space must be held
498 * when this function gets called.
499 */
500int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
501{
Heiko Carstensc5034942012-09-10 16:14:33 +0200502 struct mm_struct *mm;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200503 unsigned long *table;
504 spinlock_t *ptl;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200505 pgd_t *pgd;
506 pud_t *pud;
507 pmd_t *pmd;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200508 int rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200509
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200510 /* Create higher level tables in the gmap page table */
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200511 table = gmap->table;
512 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
513 table += (gaddr >> 53) & 0x7ff;
514 if ((*table & _REGION_ENTRY_INVALID) &&
515 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
Heiko Carstens925dfc02014-12-12 13:04:21 +0100516 gaddr & 0xffe0000000000000UL))
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200517 return -ENOMEM;
518 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
519 }
520 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
521 table += (gaddr >> 42) & 0x7ff;
522 if ((*table & _REGION_ENTRY_INVALID) &&
523 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
Heiko Carstens925dfc02014-12-12 13:04:21 +0100524 gaddr & 0xfffffc0000000000UL))
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200525 return -ENOMEM;
526 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
527 }
528 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
529 table += (gaddr >> 31) & 0x7ff;
530 if ((*table & _REGION_ENTRY_INVALID) &&
531 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
Heiko Carstens925dfc02014-12-12 13:04:21 +0100532 gaddr & 0xffffffff80000000UL))
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200533 return -ENOMEM;
534 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
535 }
536 table += (gaddr >> 20) & 0x7ff;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200537 /* Walk the parent mm page table */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200538 mm = gmap->mm;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200539 pgd = pgd_offset(mm, vmaddr);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200540 VM_BUG_ON(pgd_none(*pgd));
541 pud = pud_offset(pgd, vmaddr);
542 VM_BUG_ON(pud_none(*pud));
543 pmd = pmd_offset(pud, vmaddr);
544 VM_BUG_ON(pmd_none(*pmd));
Alex Thorlton1e1836e2014-04-07 15:37:09 -0700545 /* large pmds cannot yet be handled */
546 if (pmd_large(*pmd))
547 return -EFAULT;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200548 /* Link gmap segment table entry location to page table. */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200549 rc = radix_tree_preload(GFP_KERNEL);
550 if (rc)
551 return rc;
552 ptl = pmd_lock(mm, pmd);
553 spin_lock(&gmap->guest_table_lock);
554 if (*table == _SEGMENT_ENTRY_INVALID) {
555 rc = radix_tree_insert(&gmap->host_to_guest,
556 vmaddr >> PMD_SHIFT, table);
557 if (!rc)
558 *table = pmd_val(*pmd);
559 } else
560 rc = 0;
561 spin_unlock(&gmap->guest_table_lock);
562 spin_unlock(ptl);
563 radix_tree_preload_end();
564 return rc;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200565}
566
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200567/**
568 * gmap_fault - resolve a fault on a guest address
569 * @gmap: pointer to guest mapping meta data structure
570 * @gaddr: guest address
571 * @fault_flags: flags to pass down to handle_mm_fault()
572 *
573 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
574 * if the vm address is already mapped to a different guest segment.
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200575 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200576int gmap_fault(struct gmap *gmap, unsigned long gaddr,
577 unsigned int fault_flags)
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200578{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200579 unsigned long vmaddr;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200580 int rc;
Dominik Dingelfef89532016-01-15 16:57:07 -0800581 bool unlocked;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200582
Carsten Otte499069e2011-10-30 15:17:02 +0100583 down_read(&gmap->mm->mmap_sem);
Dominik Dingelfef89532016-01-15 16:57:07 -0800584
585retry:
586 unlocked = false;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200587 vmaddr = __gmap_translate(gmap, gaddr);
588 if (IS_ERR_VALUE(vmaddr)) {
589 rc = vmaddr;
590 goto out_up;
591 }
Dominik Dingelfef89532016-01-15 16:57:07 -0800592 if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags,
593 &unlocked)) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200594 rc = -EFAULT;
595 goto out_up;
596 }
Dominik Dingelfef89532016-01-15 16:57:07 -0800597 /*
598 * In the case that fixup_user_fault unlocked the mmap_sem during
599 * faultin redo __gmap_translate to not race with a map/unmap_segment.
600 */
601 if (unlocked)
602 goto retry;
603
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200604 rc = __gmap_link(gmap, gaddr, vmaddr);
605out_up:
Carsten Otte499069e2011-10-30 15:17:02 +0100606 up_read(&gmap->mm->mmap_sem);
Carsten Otte499069e2011-10-30 15:17:02 +0100607 return rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200608}
609EXPORT_SYMBOL_GPL(gmap_fault);
610
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200611static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
612{
613 if (!non_swap_entry(entry))
614 dec_mm_counter(mm, MM_SWAPENTS);
615 else if (is_migration_entry(entry)) {
616 struct page *page = migration_entry_to_page(entry);
617
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800618 dec_mm_counter(mm, mm_counter(page));
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200619 }
620 free_swap_and_cache(entry);
621}
622
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200623/*
624 * this function is assumed to be called with mmap_sem held
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200625 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200626void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200627{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200628 unsigned long vmaddr, ptev, pgstev;
629 pte_t *ptep, pte;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200630 spinlock_t *ptl;
631 pgste_t pgste;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200632
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200633 /* Find the vm address for the guest address */
634 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
635 gaddr >> PMD_SHIFT);
636 if (!vmaddr)
637 return;
638 vmaddr |= gaddr & ~PMD_MASK;
639 /* Get pointer to the page table entry */
640 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200641 if (unlikely(!ptep))
642 return;
643 pte = *ptep;
644 if (!pte_swap(pte))
645 goto out_pte;
646 /* Zap unused and logically-zero pages */
647 pgste = pgste_get_lock(ptep);
648 pgstev = pgste_val(pgste);
649 ptev = pte_val(pte);
650 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
651 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200652 gmap_zap_swap_entry(pte_to_swp_entry(pte), gmap->mm);
653 pte_clear(gmap->mm, vmaddr, ptep);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200654 }
655 pgste_set_unlock(ptep, pgste);
656out_pte:
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200657 pte_unmap_unlock(ptep, ptl);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200658}
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200659EXPORT_SYMBOL_GPL(__gmap_zap);
660
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200661void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
Christian Borntraeger388186b2011-10-30 15:17:03 +0100662{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200663 unsigned long gaddr, vmaddr, size;
Christian Borntraeger388186b2011-10-30 15:17:03 +0100664 struct vm_area_struct *vma;
Christian Borntraeger388186b2011-10-30 15:17:03 +0100665
666 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200667 for (gaddr = from; gaddr < to;
668 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
669 /* Find the vm address for the guest address */
670 vmaddr = (unsigned long)
671 radix_tree_lookup(&gmap->guest_to_host,
672 gaddr >> PMD_SHIFT);
673 if (!vmaddr)
Christian Borntraeger388186b2011-10-30 15:17:03 +0100674 continue;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200675 vmaddr |= gaddr & ~PMD_MASK;
676 /* Find vma in the parent mm */
677 vma = find_vma(gmap->mm, vmaddr);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200678 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200679 zap_page_range(vma, vmaddr, size, NULL);
Christian Borntraeger388186b2011-10-30 15:17:03 +0100680 }
681 up_read(&gmap->mm->mmap_sem);
682}
683EXPORT_SYMBOL_GPL(gmap_discard);
684
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200685static LIST_HEAD(gmap_notifier_list);
686static DEFINE_SPINLOCK(gmap_notifier_lock);
687
688/**
689 * gmap_register_ipte_notifier - register a pte invalidation callback
690 * @nb: pointer to the gmap notifier block
691 */
692void gmap_register_ipte_notifier(struct gmap_notifier *nb)
693{
694 spin_lock(&gmap_notifier_lock);
695 list_add(&nb->list, &gmap_notifier_list);
696 spin_unlock(&gmap_notifier_lock);
697}
698EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
699
700/**
701 * gmap_unregister_ipte_notifier - remove a pte invalidation callback
702 * @nb: pointer to the gmap notifier block
703 */
704void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
705{
706 spin_lock(&gmap_notifier_lock);
707 list_del_init(&nb->list);
708 spin_unlock(&gmap_notifier_lock);
709}
710EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
711
712/**
713 * gmap_ipte_notify - mark a range of ptes for invalidation notification
714 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200715 * @gaddr: virtual address in the guest address space
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200716 * @len: size of area
717 *
718 * Returns 0 if for each page in the given range a gmap mapping exists and
719 * the invalidation notification could be set. If the gmap mapping is missing
720 * for one or more pages -EFAULT is returned. If no memory could be allocated
721 * -ENOMEM is returned. This function establishes missing page table entries.
722 */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200723int gmap_ipte_notify(struct gmap *gmap, unsigned long gaddr, unsigned long len)
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200724{
725 unsigned long addr;
726 spinlock_t *ptl;
727 pte_t *ptep, entry;
728 pgste_t pgste;
Dominik Dingelfef89532016-01-15 16:57:07 -0800729 bool unlocked;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200730 int rc = 0;
731
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200732 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK))
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200733 return -EINVAL;
734 down_read(&gmap->mm->mmap_sem);
735 while (len) {
Dominik Dingelfef89532016-01-15 16:57:07 -0800736 unlocked = false;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200737 /* Convert gmap address and connect the page tables */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200738 addr = __gmap_translate(gmap, gaddr);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200739 if (IS_ERR_VALUE(addr)) {
740 rc = addr;
741 break;
742 }
743 /* Get the page mapped */
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800744 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE,
Dominik Dingelfef89532016-01-15 16:57:07 -0800745 &unlocked)) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200746 rc = -EFAULT;
747 break;
748 }
Dominik Dingelfef89532016-01-15 16:57:07 -0800749 /* While trying to map mmap_sem got unlocked. Let us retry */
750 if (unlocked)
751 continue;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200752 rc = __gmap_link(gmap, gaddr, addr);
753 if (rc)
754 break;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200755 /* Walk the process page table, lock and get pte pointer */
756 ptep = get_locked_pte(gmap->mm, addr, &ptl);
Dominik Dingel6972cae2014-10-15 15:29:01 +0200757 VM_BUG_ON(!ptep);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200758 /* Set notification bit in the pgste of the pte */
759 entry = *ptep;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200760 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200761 pgste = pgste_get_lock(ptep);
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200762 pgste_val(pgste) |= PGSTE_IN_BIT;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200763 pgste_set_unlock(ptep, pgste);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200764 gaddr += PAGE_SIZE;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200765 len -= PAGE_SIZE;
766 }
Martin Schwidefskya697e052014-10-30 10:55:37 +0100767 pte_unmap_unlock(ptep, ptl);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200768 }
769 up_read(&gmap->mm->mmap_sem);
770 return rc;
771}
772EXPORT_SYMBOL_GPL(gmap_ipte_notify);
773
774/**
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100775 * ptep_ipte_notify - call all invalidation callbacks for a specific pte.
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200776 * @mm: pointer to the process mm_struct
Martin Schwidefsky9da4e382014-04-30 14:46:26 +0200777 * @addr: virtual address in the process address space
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200778 * @pte: pointer to the page table entry
779 *
780 * This function is assumed to be called with the page table lock held
781 * for the pte to notify.
782 */
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100783void ptep_ipte_notify(struct mm_struct *mm, unsigned long vmaddr, pte_t *pte)
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200784{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200785 unsigned long offset, gaddr;
786 unsigned long *table;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200787 struct gmap_notifier *nb;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200788 struct gmap *gmap;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200789
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200790 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
791 offset = offset * (4096 / sizeof(pte_t));
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200792 spin_lock(&gmap_notifier_lock);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200793 list_for_each_entry(gmap, &mm->context.gmap_list, list) {
794 table = radix_tree_lookup(&gmap->host_to_guest,
795 vmaddr >> PMD_SHIFT);
796 if (!table)
797 continue;
798 gaddr = __gmap_segment_gaddr(table) + offset;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200799 list_for_each_entry(nb, &gmap_notifier_list, list)
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200800 nb->notifier_call(gmap, gaddr);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200801 }
802 spin_unlock(&gmap_notifier_lock);
803}
Martin Schwidefskyebde7652016-03-08 11:08:09 +0100804EXPORT_SYMBOL_GPL(ptep_ipte_notify);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200805
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200806int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
807 unsigned long key, bool nq)
808{
809 spinlock_t *ptl;
810 pgste_t old, new;
811 pte_t *ptep;
812
813 down_read(&mm->mmap_sem);
Jason J. Herneedeb69e2014-10-07 13:31:37 -0400814 ptep = get_locked_pte(mm, addr, &ptl);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200815 if (unlikely(!ptep)) {
816 up_read(&mm->mmap_sem);
817 return -EFAULT;
818 }
819
820 new = old = pgste_get_lock(ptep);
821 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
822 PGSTE_ACC_BITS | PGSTE_FP_BIT);
823 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
824 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
825 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200826 unsigned long address, bits, skey;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200827
828 address = pte_val(*ptep) & PAGE_MASK;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200829 skey = (unsigned long) page_get_storage_key(address);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200830 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200831 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200832 /* Set storage key ACC and FP */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200833 page_set_storage_key(address, skey, !nq);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200834 /* Merge host changed & referenced into pgste */
835 pgste_val(new) |= bits << 52;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200836 }
837 /* changing the guest storage key is considered a change of the page */
838 if ((pgste_val(new) ^ pgste_val(old)) &
839 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200840 pgste_val(new) |= PGSTE_UC_BIT;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200841
842 pgste_set_unlock(ptep, new);
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200843 pte_unmap_unlock(ptep, ptl);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200844 up_read(&mm->mmap_sem);
845 return 0;
846}
847EXPORT_SYMBOL(set_guest_storage_key);
848
Jason J. Herne9fcf93b2014-09-23 09:18:57 -0400849unsigned long get_guest_storage_key(struct mm_struct *mm, unsigned long addr)
850{
851 spinlock_t *ptl;
852 pgste_t pgste;
853 pte_t *ptep;
854 uint64_t physaddr;
855 unsigned long key = 0;
856
857 down_read(&mm->mmap_sem);
858 ptep = get_locked_pte(mm, addr, &ptl);
859 if (unlikely(!ptep)) {
860 up_read(&mm->mmap_sem);
861 return -EFAULT;
862 }
863 pgste = pgste_get_lock(ptep);
864
865 if (pte_val(*ptep) & _PAGE_INVALID) {
866 key |= (pgste_val(pgste) & PGSTE_ACC_BITS) >> 56;
867 key |= (pgste_val(pgste) & PGSTE_FP_BIT) >> 56;
868 key |= (pgste_val(pgste) & PGSTE_GR_BIT) >> 48;
869 key |= (pgste_val(pgste) & PGSTE_GC_BIT) >> 48;
870 } else {
871 physaddr = pte_val(*ptep) & PAGE_MASK;
872 key = page_get_storage_key(physaddr);
873
874 /* Reflect guest's logical view, not physical */
875 if (pgste_val(pgste) & PGSTE_GR_BIT)
876 key |= _PAGE_REFERENCED;
877 if (pgste_val(pgste) & PGSTE_GC_BIT)
878 key |= _PAGE_CHANGED;
879 }
880
881 pgste_set_unlock(ptep, pgste);
882 pte_unmap_unlock(ptep, ptl);
883 up_read(&mm->mmap_sem);
884 return key;
885}
886EXPORT_SYMBOL(get_guest_storage_key);
887
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +0200888static int page_table_allocate_pgste_min = 0;
889static int page_table_allocate_pgste_max = 1;
890int page_table_allocate_pgste = 0;
891EXPORT_SYMBOL(page_table_allocate_pgste);
892
893static struct ctl_table page_table_sysctl[] = {
894 {
895 .procname = "allocate_pgste",
896 .data = &page_table_allocate_pgste,
897 .maxlen = sizeof(int),
898 .mode = S_IRUGO | S_IWUSR,
899 .proc_handler = proc_dointvec,
900 .extra1 = &page_table_allocate_pgste_min,
901 .extra2 = &page_table_allocate_pgste_max,
902 },
903 { }
904};
905
906static struct ctl_table page_table_sysctl_dir[] = {
907 {
908 .procname = "vm",
909 .maxlen = 0,
910 .mode = 0555,
911 .child = page_table_sysctl,
912 },
913 { }
914};
915
916static int __init page_table_register_sysctl(void)
917{
918 return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
919}
920__initcall(page_table_register_sysctl);
921
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200922#else /* CONFIG_PGSTE */
923
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200924static inline void gmap_unlink(struct mm_struct *mm, unsigned long *table,
925 unsigned long vmaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200926{
927}
928
929#endif /* CONFIG_PGSTE */
930
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200931static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
932{
933 unsigned int old, new;
934
935 do {
936 old = atomic_read(v);
937 new = old ^ bits;
938 } while (atomic_cmpxchg(v, old, new) != old);
939 return new;
940}
941
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200942/*
943 * page table entry allocation/free routines.
944 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200945unsigned long *page_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200946{
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200947 unsigned long *table;
948 struct page *page;
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200949 unsigned int mask, bit;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200950
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200951 /* Try to get a fragment of a 4K page as a 2K page table */
952 if (!mm_alloc_pgste(mm)) {
953 table = NULL;
954 spin_lock_bh(&mm->context.list_lock);
955 if (!list_empty(&mm->context.pgtable_list)) {
956 page = list_first_entry(&mm->context.pgtable_list,
957 struct page, lru);
958 mask = atomic_read(&page->_mapcount);
959 mask = (mask | (mask >> 4)) & 3;
960 if (mask != 3) {
961 table = (unsigned long *) page_to_phys(page);
962 bit = mask & 1; /* =1 -> second 2K */
963 if (bit)
964 table += PTRS_PER_PTE;
965 atomic_xor_bits(&page->_mapcount, 1U << bit);
966 list_del(&page->lru);
967 }
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -0800968 }
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200969 spin_unlock_bh(&mm->context.list_lock);
970 if (table)
971 return table;
972 }
973 /* Allocate a fresh page */
974 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
975 if (!page)
976 return NULL;
977 if (!pgtable_page_ctor(page)) {
978 __free_page(page);
979 return NULL;
980 }
981 /* Initialize page table */
982 table = (unsigned long *) page_to_phys(page);
983 if (mm_alloc_pgste(mm)) {
984 /* Return 4K page table with PGSTEs */
985 atomic_set(&page->_mapcount, 3);
986 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
987 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
988 } else {
989 /* Return the first 2K fragment of the page */
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200990 atomic_set(&page->_mapcount, 1);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200991 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200992 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100993 list_add(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky78fb9072015-08-14 14:58:50 +0200994 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100995 }
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200996 return table;
997}
998
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100999void page_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001000{
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001001 struct page *page;
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001002 unsigned int bit, mask;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001003
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001004 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001005 if (!mm_alloc_pgste(mm)) {
1006 /* Free 2K page table fragment of a 4K page */
1007 bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
1008 spin_lock_bh(&mm->context.list_lock);
1009 mask = atomic_xor_bits(&page->_mapcount, 1U << bit);
1010 if (mask & 3)
1011 list_add(&page->lru, &mm->context.pgtable_list);
1012 else
1013 list_del(&page->lru);
1014 spin_unlock_bh(&mm->context.list_lock);
1015 if (mask != 0)
1016 return;
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001017 }
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001018
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001019 pgtable_page_dtor(page);
1020 atomic_set(&page->_mapcount, -1);
1021 __free_page(page);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001022}
1023
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001024void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
1025 unsigned long vmaddr)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001026{
1027 struct mm_struct *mm;
1028 struct page *page;
1029 unsigned int bit, mask;
1030
1031 mm = tlb->mm;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001032 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001033 if (mm_alloc_pgste(mm)) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001034 gmap_unlink(mm, table, vmaddr);
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001035 table = (unsigned long *) (__pa(table) | 3);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001036 tlb_remove_table(tlb, table);
1037 return;
Martin Schwidefsky80217142010-10-25 16:10:11 +02001038 }
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001039 bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
Martin Schwidefsky80217142010-10-25 16:10:11 +02001040 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001041 mask = atomic_xor_bits(&page->_mapcount, 0x11U << bit);
1042 if (mask & 3)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001043 list_add_tail(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001044 else
1045 list_del(&page->lru);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001046 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001047 table = (unsigned long *) (__pa(table) | (1U << bit));
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001048 tlb_remove_table(tlb, table);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001049}
1050
Heiko Carstens63df41d62013-09-06 19:10:48 +02001051static void __tlb_remove_table(void *_table)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001052{
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001053 unsigned int mask = (unsigned long) _table & 3;
1054 void *table = (void *)((unsigned long) _table ^ mask);
1055 struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001056
Martin Schwidefsky78fb9072015-08-14 14:58:50 +02001057 switch (mask) {
1058 case 0: /* pmd or pud */
1059 free_pages((unsigned long) table, 2);
1060 break;
1061 case 1: /* lower 2K of a 4K page table */
1062 case 2: /* higher 2K of a 4K page table */
1063 if (atomic_xor_bits(&page->_mapcount, mask << 4) != 0)
1064 break;
1065 /* fallthrough */
1066 case 3: /* 4K page table with pgstes */
1067 pgtable_page_dtor(page);
1068 atomic_set(&page->_mapcount, -1);
1069 __free_page(page);
1070 break;
1071 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001072}
1073
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001074static void tlb_remove_table_smp_sync(void *arg)
1075{
1076 /* Simply deliver the interrupt */
1077}
1078
1079static void tlb_remove_table_one(void *table)
1080{
1081 /*
1082 * This isn't an RCU grace period and hence the page-tables cannot be
1083 * assumed to be actually RCU-freed.
1084 *
1085 * It is however sufficient for software page-table walkers that rely
1086 * on IRQ disabling. See the comment near struct mmu_table_batch.
1087 */
1088 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1089 __tlb_remove_table(table);
1090}
1091
1092static void tlb_remove_table_rcu(struct rcu_head *head)
1093{
1094 struct mmu_table_batch *batch;
1095 int i;
1096
1097 batch = container_of(head, struct mmu_table_batch, rcu);
1098
1099 for (i = 0; i < batch->nr; i++)
1100 __tlb_remove_table(batch->tables[i]);
1101
1102 free_page((unsigned long)batch);
1103}
1104
1105void tlb_table_flush(struct mmu_gather *tlb)
1106{
1107 struct mmu_table_batch **batch = &tlb->batch;
1108
1109 if (*batch) {
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001110 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1111 *batch = NULL;
1112 }
1113}
1114
1115void tlb_remove_table(struct mmu_gather *tlb, void *table)
1116{
1117 struct mmu_table_batch **batch = &tlb->batch;
1118
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001119 tlb->mm->context.flush_mm = 1;
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001120 if (*batch == NULL) {
1121 *batch = (struct mmu_table_batch *)
1122 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1123 if (*batch == NULL) {
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001124 __tlb_flush_mm_lazy(tlb->mm);
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001125 tlb_remove_table_one(table);
1126 return;
1127 }
1128 (*batch)->nr = 0;
1129 }
1130 (*batch)->tables[(*batch)->nr++] = table;
1131 if ((*batch)->nr == MAX_TABLE_BATCH)
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001132 tlb_flush_mmu(tlb);
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001133}
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001134
Gerald Schaefer274023d2012-10-08 16:30:21 -07001135#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001136static inline void thp_split_vma(struct vm_area_struct *vma)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001137{
1138 unsigned long addr;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001139
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001140 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1141 follow_page(vma, addr, FOLL_SPLIT);
Gerald Schaefer274023d2012-10-08 16:30:21 -07001142}
1143
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001144static inline void thp_split_mm(struct mm_struct *mm)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001145{
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001146 struct vm_area_struct *vma;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001147
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001148 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
Gerald Schaefer274023d2012-10-08 16:30:21 -07001149 thp_split_vma(vma);
1150 vma->vm_flags &= ~VM_HUGEPAGE;
1151 vma->vm_flags |= VM_NOHUGEPAGE;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001152 }
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001153 mm->def_flags |= VM_NOHUGEPAGE;
1154}
1155#else
1156static inline void thp_split_mm(struct mm_struct *mm)
1157{
Gerald Schaefer274023d2012-10-08 16:30:21 -07001158}
1159#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1160
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001161static inline pte_t ptep_flush_direct(struct mm_struct *mm,
1162 unsigned long addr, pte_t *ptep)
1163{
1164 int active, count;
1165 pte_t old;
1166
1167 old = *ptep;
1168 if (unlikely(pte_val(old) & _PAGE_INVALID))
1169 return old;
1170 active = (mm == current->active_mm) ? 1 : 0;
1171 count = atomic_add_return(0x10000, &mm->context.attach_count);
1172 if (MACHINE_HAS_TLB_LC && (count & 0xffff) <= active &&
1173 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
1174 __ptep_ipte_local(addr, ptep);
1175 else
1176 __ptep_ipte(addr, ptep);
1177 atomic_sub(0x10000, &mm->context.attach_count);
1178 return old;
1179}
1180
1181static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
1182 unsigned long addr, pte_t *ptep)
1183{
1184 int active, count;
1185 pte_t old;
1186
1187 old = *ptep;
1188 if (unlikely(pte_val(old) & _PAGE_INVALID))
1189 return old;
1190 active = (mm == current->active_mm) ? 1 : 0;
1191 count = atomic_add_return(0x10000, &mm->context.attach_count);
1192 if ((count & 0xffff) <= active) {
1193 pte_val(*ptep) |= _PAGE_INVALID;
1194 mm->context.flush_mm = 1;
1195 } else
1196 __ptep_ipte(addr, ptep);
1197 atomic_sub(0x10000, &mm->context.attach_count);
1198 return old;
1199}
1200
1201static inline pgste_t pgste_update_all(pte_t pte, pgste_t pgste,
1202 struct mm_struct *mm)
1203{
1204#ifdef CONFIG_PGSTE
1205 unsigned long address, bits, skey;
1206
1207 if (!mm_use_skey(mm) || pte_val(pte) & _PAGE_INVALID)
1208 return pgste;
1209 address = pte_val(pte) & PAGE_MASK;
1210 skey = (unsigned long) page_get_storage_key(address);
1211 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
1212 /* Transfer page changed & referenced bit to guest bits in pgste */
1213 pgste_val(pgste) |= bits << 48; /* GR bit & GC bit */
1214 /* Copy page access key and fetch protection bit to pgste */
1215 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
1216 pgste_val(pgste) |= (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
1217#endif
1218 return pgste;
1219
1220}
1221
1222static inline void pgste_set_key(pte_t *ptep, pgste_t pgste, pte_t entry,
1223 struct mm_struct *mm)
1224{
1225#ifdef CONFIG_PGSTE
1226 unsigned long address;
1227 unsigned long nkey;
1228
1229 if (!mm_use_skey(mm) || pte_val(entry) & _PAGE_INVALID)
1230 return;
1231 VM_BUG_ON(!(pte_val(*ptep) & _PAGE_INVALID));
1232 address = pte_val(entry) & PAGE_MASK;
1233 /*
1234 * Set page access key and fetch protection bit from pgste.
1235 * The guest C/R information is still in the PGSTE, set real
1236 * key C/R to 0.
1237 */
1238 nkey = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
1239 nkey |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
1240 page_set_storage_key(address, nkey, 0);
1241#endif
1242}
1243
1244static inline pgste_t pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
1245{
1246#ifdef CONFIG_PGSTE
1247 if ((pte_val(entry) & _PAGE_PRESENT) &&
1248 (pte_val(entry) & _PAGE_WRITE) &&
1249 !(pte_val(entry) & _PAGE_INVALID)) {
1250 if (!MACHINE_HAS_ESOP) {
1251 /*
1252 * Without enhanced suppression-on-protection force
1253 * the dirty bit on for all writable ptes.
1254 */
1255 pte_val(entry) |= _PAGE_DIRTY;
1256 pte_val(entry) &= ~_PAGE_PROTECT;
1257 }
1258 if (!(pte_val(entry) & _PAGE_PROTECT))
1259 /* This pte allows write access, set user-dirty */
1260 pgste_val(pgste) |= PGSTE_UC_BIT;
1261 }
1262#endif
1263 *ptep = entry;
1264 return pgste;
1265}
1266
1267static inline pgste_t pgste_ipte_notify(struct mm_struct *mm,
1268 unsigned long addr,
1269 pte_t *ptep, pgste_t pgste)
1270{
1271#ifdef CONFIG_PGSTE
1272 if (pgste_val(pgste) & PGSTE_IN_BIT) {
1273 pgste_val(pgste) &= ~PGSTE_IN_BIT;
1274 ptep_ipte_notify(mm, addr, ptep);
1275 }
1276#endif
1277 return pgste;
1278}
1279
1280#ifdef CONFIG_PGSTE
1281/*
1282 * Test and reset if a guest page is dirty
1283 */
1284bool pgste_test_and_clear_dirty(struct mm_struct *mm, unsigned long addr)
1285{
1286 spinlock_t *ptl;
1287 pgste_t pgste;
1288 pte_t *ptep;
1289 pte_t pte;
1290 bool dirty;
1291
1292 ptep = get_locked_pte(mm, addr, &ptl);
1293 if (unlikely(!ptep))
1294 return false;
1295
1296 pgste = pgste_get_lock(ptep);
1297 dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
1298 pgste_val(pgste) &= ~PGSTE_UC_BIT;
1299 pte = *ptep;
1300 if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
1301 pgste = pgste_ipte_notify(mm, addr, ptep, pgste);
1302 __ptep_ipte(addr, ptep);
1303 if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
1304 pte_val(pte) |= _PAGE_PROTECT;
1305 else
1306 pte_val(pte) |= _PAGE_INVALID;
1307 *ptep = pte;
1308 }
1309 pgste_set_unlock(ptep, pgste);
1310
1311 spin_unlock(ptl);
1312 return dirty;
1313}
1314EXPORT_SYMBOL_GPL(pgste_test_and_clear_dirty);
1315
1316void set_pte_pgste_at(struct mm_struct *mm, unsigned long addr,
1317 pte_t *ptep, pte_t entry)
1318{
1319 pgste_t pgste;
1320
1321 /* the mm_has_pgste() check is done in set_pte_at() */
1322 pgste = pgste_get_lock(ptep);
1323 pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
1324 pgste_set_key(ptep, pgste, entry, mm);
1325 pgste = pgste_set_pte(ptep, pgste, entry);
1326 pgste_set_unlock(ptep, pgste);
1327}
1328EXPORT_SYMBOL(set_pte_pgste_at);
1329#endif
1330
1331static inline pgste_t ptep_xchg_start(struct mm_struct *mm,
1332 unsigned long addr, pte_t *ptep)
1333{
1334 pgste_t pgste = __pgste(0);
1335
1336 if (mm_has_pgste(mm)) {
1337 pgste = pgste_get_lock(ptep);
1338 pgste = pgste_ipte_notify(mm, addr, ptep, pgste);
1339 }
1340 return pgste;
1341}
1342
1343static inline void ptep_xchg_commit(struct mm_struct *mm,
1344 unsigned long addr, pte_t *ptep,
1345 pgste_t pgste, pte_t old, pte_t new)
1346{
1347 if (mm_has_pgste(mm)) {
1348 if (pte_val(old) & _PAGE_INVALID)
1349 pgste_set_key(ptep, pgste, new, mm);
1350 if (pte_val(new) & _PAGE_INVALID) {
1351 pgste = pgste_update_all(old, pgste, mm);
1352 if ((pgste_val(pgste) & _PGSTE_GPS_USAGE_MASK) ==
1353 _PGSTE_GPS_USAGE_UNUSED)
1354 pte_val(old) |= _PAGE_UNUSED;
1355 }
1356 pgste = pgste_set_pte(ptep, pgste, new);
1357 pgste_set_unlock(ptep, pgste);
1358 } else {
1359 *ptep = new;
1360 }
1361}
1362
1363pte_t ptep_xchg_direct(struct mm_struct *mm, unsigned long addr,
1364 pte_t *ptep, pte_t new)
1365{
1366 pgste_t pgste;
1367 pte_t old;
1368
1369 pgste = ptep_xchg_start(mm, addr, ptep);
1370 old = ptep_flush_direct(mm, addr, ptep);
1371 ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
1372 return old;
1373}
1374EXPORT_SYMBOL(ptep_xchg_direct);
1375
1376pte_t ptep_xchg_lazy(struct mm_struct *mm, unsigned long addr,
1377 pte_t *ptep, pte_t new)
1378{
1379 pgste_t pgste;
1380 pte_t old;
1381
1382 pgste = ptep_xchg_start(mm, addr, ptep);
1383 old = ptep_flush_lazy(mm, addr, ptep);
1384 ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
1385 return old;
1386}
1387EXPORT_SYMBOL(ptep_xchg_lazy);
1388
1389pte_t ptep_modify_prot_start(struct mm_struct *mm, unsigned long addr,
1390 pte_t *ptep)
1391{
1392 pgste_t pgste;
1393 pte_t old;
1394
1395 pgste = ptep_xchg_start(mm, addr, ptep);
1396 old = ptep_flush_lazy(mm, addr, ptep);
1397 if (mm_has_pgste(mm)) {
1398 pgste = pgste_update_all(old, pgste, mm);
1399 pgste_set(ptep, pgste);
1400 }
1401 return old;
1402}
1403EXPORT_SYMBOL(ptep_modify_prot_start);
1404
1405void ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
1406 pte_t *ptep, pte_t pte)
1407{
1408 pgste_t pgste;
1409
1410 if (mm_has_pgste(mm)) {
1411 pgste = pgste_get(ptep);
1412 pgste_set_key(ptep, pgste, pte, mm);
1413 pgste = pgste_set_pte(ptep, pgste, pte);
1414 pgste_set_unlock(ptep, pgste);
1415 } else {
1416 *ptep = pte;
1417 }
1418}
1419EXPORT_SYMBOL(ptep_modify_prot_commit);
1420
Martin Schwidefsky227be792016-03-08 11:09:25 +01001421static inline pmd_t pmdp_flush_direct(struct mm_struct *mm,
1422 unsigned long addr, pmd_t *pmdp)
1423{
1424 int active, count;
1425 pmd_t old;
1426
1427 old = *pmdp;
1428 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
1429 return old;
1430 if (!MACHINE_HAS_IDTE) {
1431 __pmdp_csp(pmdp);
1432 return old;
1433 }
1434 active = (mm == current->active_mm) ? 1 : 0;
1435 count = atomic_add_return(0x10000, &mm->context.attach_count);
1436 if (MACHINE_HAS_TLB_LC && (count & 0xffff) <= active &&
1437 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
1438 __pmdp_idte_local(addr, pmdp);
1439 else
1440 __pmdp_idte(addr, pmdp);
1441 atomic_sub(0x10000, &mm->context.attach_count);
1442 return old;
1443}
1444
1445static inline pmd_t pmdp_flush_lazy(struct mm_struct *mm,
1446 unsigned long addr, pmd_t *pmdp)
1447{
1448 int active, count;
1449 pmd_t old;
1450
1451 old = *pmdp;
1452 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
1453 return old;
1454 active = (mm == current->active_mm) ? 1 : 0;
1455 count = atomic_add_return(0x10000, &mm->context.attach_count);
1456 if ((count & 0xffff) <= active) {
1457 pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID;
1458 mm->context.flush_mm = 1;
1459 } else if (MACHINE_HAS_IDTE)
1460 __pmdp_idte(addr, pmdp);
1461 else
1462 __pmdp_csp(pmdp);
1463 atomic_sub(0x10000, &mm->context.attach_count);
1464 return old;
1465}
1466
1467pmd_t pmdp_xchg_direct(struct mm_struct *mm, unsigned long addr,
1468 pmd_t *pmdp, pmd_t new)
1469{
1470 pmd_t old;
1471
1472 old = pmdp_flush_direct(mm, addr, pmdp);
1473 *pmdp = new;
1474 return old;
1475}
1476EXPORT_SYMBOL(pmdp_xchg_direct);
1477
1478pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
1479 pmd_t *pmdp, pmd_t new)
1480{
1481 pmd_t old;
1482
1483 old = pmdp_flush_lazy(mm, addr, pmdp);
1484 *pmdp = new;
1485 return old;
1486}
1487EXPORT_SYMBOL(pmdp_xchg_lazy);
1488
Carsten Otte402b0862008-03-25 18:47:10 +01001489/*
1490 * switch on pgstes for its userspace process (for kvm)
1491 */
1492int s390_enable_sie(void)
1493{
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001494 struct mm_struct *mm = current->mm;
Carsten Otte402b0862008-03-25 18:47:10 +01001495
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001496 /* Do we have pgstes? if yes, we are done */
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001497 if (mm_has_pgste(mm))
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001498 return 0;
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001499 /* Fail if the page tables are 2K */
1500 if (!mm_alloc_pgste(mm))
1501 return -EINVAL;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001502 down_write(&mm->mmap_sem);
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001503 mm->context.has_pgste = 1;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001504 /* split thp mappings and disable thp for future mappings */
1505 thp_split_mm(mm);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001506 up_write(&mm->mmap_sem);
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001507 return 0;
Carsten Otte402b0862008-03-25 18:47:10 +01001508}
1509EXPORT_SYMBOL_GPL(s390_enable_sie);
Hans-Joachim Picht7db11a32009-06-16 10:30:26 +02001510
Dominik Dingel934bc132014-01-14 18:10:17 +01001511/*
1512 * Enable storage key handling from now on and initialize the storage
1513 * keys with the default key.
1514 */
Dominik Dingela13cff32014-10-23 12:07:14 +02001515static int __s390_enable_skey(pte_t *pte, unsigned long addr,
1516 unsigned long next, struct mm_walk *walk)
1517{
1518 unsigned long ptev;
1519 pgste_t pgste;
1520
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001521 /*
1522 * Remove all zero page mappings,
1523 * after establishing a policy to forbid zero page mappings
1524 * following faults for that page will get fresh anonymous pages
1525 */
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001526 if (is_zero_pfn(pte_pfn(*pte)))
1527 ptep_xchg_direct(walk->mm, addr, pte, __pte(_PAGE_INVALID));
Dominik Dingela13cff32014-10-23 12:07:14 +02001528 /* Clear storage key */
Martin Schwidefskyebde7652016-03-08 11:08:09 +01001529 pgste = pgste_get_lock(pte);
Dominik Dingela13cff32014-10-23 12:07:14 +02001530 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
1531 PGSTE_GR_BIT | PGSTE_GC_BIT);
1532 ptev = pte_val(*pte);
1533 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
1534 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
1535 pgste_set_unlock(pte, pgste);
1536 return 0;
1537}
1538
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001539int s390_enable_skey(void)
Dominik Dingel934bc132014-01-14 18:10:17 +01001540{
Dominik Dingela13cff32014-10-23 12:07:14 +02001541 struct mm_walk walk = { .pte_entry = __s390_enable_skey };
1542 struct mm_struct *mm = current->mm;
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001543 struct vm_area_struct *vma;
1544 int rc = 0;
Dominik Dingela13cff32014-10-23 12:07:14 +02001545
1546 down_write(&mm->mmap_sem);
1547 if (mm_use_skey(mm))
1548 goto out_up;
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001549
1550 mm->context.use_skey = 1;
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001551 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1552 if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
1553 MADV_UNMERGEABLE, &vma->vm_flags)) {
1554 mm->context.use_skey = 0;
1555 rc = -ENOMEM;
1556 goto out_up;
1557 }
1558 }
1559 mm->def_flags &= ~VM_MERGEABLE;
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001560
Dominik Dingela13cff32014-10-23 12:07:14 +02001561 walk.mm = mm;
1562 walk_page_range(0, TASK_SIZE, &walk);
Dominik Dingela13cff32014-10-23 12:07:14 +02001563
1564out_up:
1565 up_write(&mm->mmap_sem);
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001566 return rc;
Dominik Dingel934bc132014-01-14 18:10:17 +01001567}
1568EXPORT_SYMBOL_GPL(s390_enable_skey);
1569
Dominik Dingela0bf4f12014-03-24 14:27:58 +01001570/*
Dominik Dingela13cff32014-10-23 12:07:14 +02001571 * Reset CMMA state, make all pages stable again.
1572 */
1573static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
1574 unsigned long next, struct mm_walk *walk)
1575{
1576 pgste_t pgste;
1577
1578 pgste = pgste_get_lock(pte);
1579 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
1580 pgste_set_unlock(pte, pgste);
1581 return 0;
1582}
1583
1584void s390_reset_cmma(struct mm_struct *mm)
1585{
1586 struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
1587
1588 down_write(&mm->mmap_sem);
1589 walk.mm = mm;
1590 walk_page_range(0, TASK_SIZE, &walk);
1591 up_write(&mm->mmap_sem);
1592}
1593EXPORT_SYMBOL_GPL(s390_reset_cmma);
1594
Gerald Schaefer75077af2012-10-08 16:30:15 -07001595#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001596void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1597 pgtable_t pgtable)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001598{
1599 struct list_head *lh = (struct list_head *) pgtable;
1600
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001601 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001602
1603 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001604 if (!pmd_huge_pte(mm, pmdp))
Gerald Schaefer9501d092012-10-08 16:30:18 -07001605 INIT_LIST_HEAD(lh);
1606 else
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001607 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1608 pmd_huge_pte(mm, pmdp) = pgtable;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001609}
1610
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001611pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001612{
1613 struct list_head *lh;
1614 pgtable_t pgtable;
1615 pte_t *ptep;
1616
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001617 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001618
1619 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001620 pgtable = pmd_huge_pte(mm, pmdp);
Gerald Schaefer9501d092012-10-08 16:30:18 -07001621 lh = (struct list_head *) pgtable;
1622 if (list_empty(lh))
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001623 pmd_huge_pte(mm, pmdp) = NULL;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001624 else {
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001625 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001626 list_del(lh);
1627 }
1628 ptep = (pte_t *) pgtable;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001629 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001630 ptep++;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001631 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001632 return pgtable;
1633}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001634#endif /* CONFIG_TRANSPARENT_HUGEPAGE */