Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 2007, 2011 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 3 | * 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/gfp.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/swap.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/highmem.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 14 | #include <linux/pagemap.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/quicklist.h> |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 18 | #include <linux/rcupdate.h> |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 19 | #include <linux/slab.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 20 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 21 | #include <asm/pgtable.h> |
| 22 | #include <asm/pgalloc.h> |
| 23 | #include <asm/tlb.h> |
| 24 | #include <asm/tlbflush.h> |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 25 | #include <asm/mmu_context.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 26 | |
| 27 | #ifndef CONFIG_64BIT |
| 28 | #define ALLOC_ORDER 1 |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 29 | #define FRAG_MASK 0x0f |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 30 | #else |
| 31 | #define ALLOC_ORDER 2 |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 32 | #define FRAG_MASK 0x03 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 33 | #endif |
| 34 | |
Heiko Carstens | 239a6425 | 2009-06-12 10:26:33 +0200 | [diff] [blame] | 35 | |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 36 | unsigned long *crst_table_alloc(struct mm_struct *mm) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 37 | { |
| 38 | struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
| 39 | |
| 40 | if (!page) |
| 41 | return NULL; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 42 | return (unsigned long *) page_to_phys(page); |
| 43 | } |
| 44 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 45 | void crst_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 46 | { |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 47 | free_pages((unsigned long) table, ALLOC_ORDER); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 50 | #ifdef CONFIG_64BIT |
| 51 | int crst_table_upgrade(struct mm_struct *mm, unsigned long limit) |
| 52 | { |
| 53 | unsigned long *table, *pgd; |
| 54 | unsigned long entry; |
| 55 | |
| 56 | BUG_ON(limit > (1UL << 53)); |
| 57 | repeat: |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 58 | table = crst_table_alloc(mm); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 59 | if (!table) |
| 60 | return -ENOMEM; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 61 | spin_lock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 62 | if (mm->context.asce_limit < limit) { |
| 63 | pgd = (unsigned long *) mm->pgd; |
| 64 | if (mm->context.asce_limit <= (1UL << 31)) { |
| 65 | entry = _REGION3_ENTRY_EMPTY; |
| 66 | mm->context.asce_limit = 1UL << 42; |
| 67 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 68 | _ASCE_USER_BITS | |
| 69 | _ASCE_TYPE_REGION3; |
| 70 | } else { |
| 71 | entry = _REGION2_ENTRY_EMPTY; |
| 72 | mm->context.asce_limit = 1UL << 53; |
| 73 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 74 | _ASCE_USER_BITS | |
| 75 | _ASCE_TYPE_REGION2; |
| 76 | } |
| 77 | crst_table_init(table, entry); |
| 78 | pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd); |
| 79 | mm->pgd = (pgd_t *) table; |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 80 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 81 | table = NULL; |
| 82 | } |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 83 | spin_unlock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 84 | if (table) |
| 85 | crst_table_free(mm, table); |
| 86 | if (mm->context.asce_limit < limit) |
| 87 | goto repeat; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void crst_table_downgrade(struct mm_struct *mm, unsigned long limit) |
| 92 | { |
| 93 | pgd_t *pgd; |
| 94 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 95 | while (mm->context.asce_limit > limit) { |
| 96 | pgd = mm->pgd; |
| 97 | switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) { |
| 98 | case _REGION_ENTRY_TYPE_R2: |
| 99 | mm->context.asce_limit = 1UL << 42; |
| 100 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 101 | _ASCE_USER_BITS | |
| 102 | _ASCE_TYPE_REGION3; |
| 103 | break; |
| 104 | case _REGION_ENTRY_TYPE_R3: |
| 105 | mm->context.asce_limit = 1UL << 31; |
| 106 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 107 | _ASCE_USER_BITS | |
| 108 | _ASCE_TYPE_SEGMENT; |
| 109 | break; |
| 110 | default: |
| 111 | BUG(); |
| 112 | } |
| 113 | mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN); |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 114 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 115 | crst_table_free(mm, (unsigned long *) pgd); |
| 116 | } |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 117 | } |
| 118 | #endif |
| 119 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 120 | #ifdef CONFIG_PGSTE |
| 121 | |
| 122 | /** |
| 123 | * gmap_alloc - allocate a guest address space |
| 124 | * @mm: pointer to the parent mm_struct |
| 125 | * |
| 126 | * Returns a guest address space structure. |
| 127 | */ |
| 128 | struct gmap *gmap_alloc(struct mm_struct *mm) |
| 129 | { |
| 130 | struct gmap *gmap; |
| 131 | struct page *page; |
| 132 | unsigned long *table; |
| 133 | |
| 134 | gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL); |
| 135 | if (!gmap) |
| 136 | goto out; |
| 137 | INIT_LIST_HEAD(&gmap->crst_list); |
| 138 | gmap->mm = mm; |
| 139 | page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
| 140 | if (!page) |
| 141 | goto out_free; |
| 142 | list_add(&page->lru, &gmap->crst_list); |
| 143 | table = (unsigned long *) page_to_phys(page); |
| 144 | crst_table_init(table, _REGION1_ENTRY_EMPTY); |
| 145 | gmap->table = table; |
Christian Borntraeger | 480e592 | 2011-09-20 17:07:28 +0200 | [diff] [blame] | 146 | gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH | |
| 147 | _ASCE_USER_BITS | __pa(table); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 148 | list_add(&gmap->list, &mm->context.gmap_list); |
| 149 | return gmap; |
| 150 | |
| 151 | out_free: |
| 152 | kfree(gmap); |
| 153 | out: |
| 154 | return NULL; |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(gmap_alloc); |
| 157 | |
| 158 | static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table) |
| 159 | { |
| 160 | struct gmap_pgtable *mp; |
| 161 | struct gmap_rmap *rmap; |
| 162 | struct page *page; |
| 163 | |
| 164 | if (*table & _SEGMENT_ENTRY_INV) |
| 165 | return 0; |
| 166 | page = pfn_to_page(*table >> PAGE_SHIFT); |
| 167 | mp = (struct gmap_pgtable *) page->index; |
| 168 | list_for_each_entry(rmap, &mp->mapper, list) { |
| 169 | if (rmap->entry != table) |
| 170 | continue; |
| 171 | list_del(&rmap->list); |
| 172 | kfree(rmap); |
| 173 | break; |
| 174 | } |
| 175 | *table = _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO | mp->vmaddr; |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | static void gmap_flush_tlb(struct gmap *gmap) |
| 180 | { |
| 181 | if (MACHINE_HAS_IDTE) |
| 182 | __tlb_flush_idte((unsigned long) gmap->table | |
| 183 | _ASCE_TYPE_REGION1); |
| 184 | else |
| 185 | __tlb_flush_global(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * gmap_free - free a guest address space |
| 190 | * @gmap: pointer to the guest address space structure |
| 191 | */ |
| 192 | void gmap_free(struct gmap *gmap) |
| 193 | { |
| 194 | struct page *page, *next; |
| 195 | unsigned long *table; |
| 196 | int i; |
| 197 | |
| 198 | |
| 199 | /* Flush tlb. */ |
| 200 | if (MACHINE_HAS_IDTE) |
| 201 | __tlb_flush_idte((unsigned long) gmap->table | |
| 202 | _ASCE_TYPE_REGION1); |
| 203 | else |
| 204 | __tlb_flush_global(); |
| 205 | |
| 206 | /* Free all segment & region tables. */ |
| 207 | down_read(&gmap->mm->mmap_sem); |
Carsten Otte | cc77245 | 2011-10-30 15:17:01 +0100 | [diff] [blame] | 208 | spin_lock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 209 | list_for_each_entry_safe(page, next, &gmap->crst_list, lru) { |
| 210 | table = (unsigned long *) page_to_phys(page); |
| 211 | if ((*table & _REGION_ENTRY_TYPE_MASK) == 0) |
| 212 | /* Remove gmap rmap structures for segment table. */ |
| 213 | for (i = 0; i < PTRS_PER_PMD; i++, table++) |
| 214 | gmap_unlink_segment(gmap, table); |
| 215 | __free_pages(page, ALLOC_ORDER); |
| 216 | } |
Carsten Otte | cc77245 | 2011-10-30 15:17:01 +0100 | [diff] [blame] | 217 | spin_unlock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 218 | up_read(&gmap->mm->mmap_sem); |
| 219 | list_del(&gmap->list); |
| 220 | kfree(gmap); |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(gmap_free); |
| 223 | |
| 224 | /** |
| 225 | * gmap_enable - switch primary space to the guest address space |
| 226 | * @gmap: pointer to the guest address space structure |
| 227 | */ |
| 228 | void gmap_enable(struct gmap *gmap) |
| 229 | { |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 230 | S390_lowcore.gmap = (unsigned long) gmap; |
| 231 | } |
| 232 | EXPORT_SYMBOL_GPL(gmap_enable); |
| 233 | |
| 234 | /** |
| 235 | * gmap_disable - switch back to the standard primary address space |
| 236 | * @gmap: pointer to the guest address space structure |
| 237 | */ |
| 238 | void gmap_disable(struct gmap *gmap) |
| 239 | { |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 240 | S390_lowcore.gmap = 0UL; |
| 241 | } |
| 242 | EXPORT_SYMBOL_GPL(gmap_disable); |
| 243 | |
Carsten Otte | a9162f2 | 2011-10-30 15:17:00 +0100 | [diff] [blame] | 244 | /* |
| 245 | * gmap_alloc_table is assumed to be called with mmap_sem held |
| 246 | */ |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 247 | static int gmap_alloc_table(struct gmap *gmap, |
| 248 | unsigned long *table, unsigned long init) |
| 249 | { |
| 250 | struct page *page; |
| 251 | unsigned long *new; |
| 252 | |
Christian Borntraeger | c86cce2 | 2011-12-27 11:25:47 +0100 | [diff] [blame] | 253 | /* since we dont free the gmap table until gmap_free we can unlock */ |
| 254 | spin_unlock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 255 | page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
Christian Borntraeger | c86cce2 | 2011-12-27 11:25:47 +0100 | [diff] [blame] | 256 | spin_lock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 257 | if (!page) |
| 258 | return -ENOMEM; |
| 259 | new = (unsigned long *) page_to_phys(page); |
| 260 | crst_table_init(new, init); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 261 | if (*table & _REGION_ENTRY_INV) { |
| 262 | list_add(&page->lru, &gmap->crst_list); |
| 263 | *table = (unsigned long) new | _REGION_ENTRY_LENGTH | |
| 264 | (*table & _REGION_ENTRY_TYPE_MASK); |
| 265 | } else |
| 266 | __free_pages(page, ALLOC_ORDER); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * gmap_unmap_segment - unmap segment from the guest address space |
| 272 | * @gmap: pointer to the guest address space structure |
| 273 | * @addr: address in the guest address space |
| 274 | * @len: length of the memory area to unmap |
| 275 | * |
| 276 | * Returns 0 if the unmap succeded, -EINVAL if not. |
| 277 | */ |
| 278 | int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len) |
| 279 | { |
| 280 | unsigned long *table; |
| 281 | unsigned long off; |
| 282 | int flush; |
| 283 | |
| 284 | if ((to | len) & (PMD_SIZE - 1)) |
| 285 | return -EINVAL; |
| 286 | if (len == 0 || to + len < to) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | flush = 0; |
| 290 | down_read(&gmap->mm->mmap_sem); |
Carsten Otte | cc77245 | 2011-10-30 15:17:01 +0100 | [diff] [blame] | 291 | spin_lock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 292 | for (off = 0; off < len; off += PMD_SIZE) { |
| 293 | /* Walk the guest addr space page table */ |
| 294 | table = gmap->table + (((to + off) >> 53) & 0x7ff); |
| 295 | if (*table & _REGION_ENTRY_INV) |
Carsten Otte | 05873df | 2011-09-26 16:40:34 +0200 | [diff] [blame] | 296 | goto out; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 297 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 298 | table = table + (((to + off) >> 42) & 0x7ff); |
| 299 | if (*table & _REGION_ENTRY_INV) |
Carsten Otte | 05873df | 2011-09-26 16:40:34 +0200 | [diff] [blame] | 300 | goto out; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 301 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 302 | table = table + (((to + off) >> 31) & 0x7ff); |
| 303 | if (*table & _REGION_ENTRY_INV) |
Carsten Otte | 05873df | 2011-09-26 16:40:34 +0200 | [diff] [blame] | 304 | goto out; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 305 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 306 | table = table + (((to + off) >> 20) & 0x7ff); |
| 307 | |
| 308 | /* Clear segment table entry in guest address space. */ |
| 309 | flush |= gmap_unlink_segment(gmap, table); |
| 310 | *table = _SEGMENT_ENTRY_INV; |
| 311 | } |
Carsten Otte | 05873df | 2011-09-26 16:40:34 +0200 | [diff] [blame] | 312 | out: |
Carsten Otte | cc77245 | 2011-10-30 15:17:01 +0100 | [diff] [blame] | 313 | spin_unlock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 314 | up_read(&gmap->mm->mmap_sem); |
| 315 | if (flush) |
| 316 | gmap_flush_tlb(gmap); |
| 317 | return 0; |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(gmap_unmap_segment); |
| 320 | |
| 321 | /** |
| 322 | * gmap_mmap_segment - map a segment to the guest address space |
| 323 | * @gmap: pointer to the guest address space structure |
| 324 | * @from: source address in the parent address space |
| 325 | * @to: target address in the guest address space |
| 326 | * |
| 327 | * Returns 0 if the mmap succeded, -EINVAL or -ENOMEM if not. |
| 328 | */ |
| 329 | int gmap_map_segment(struct gmap *gmap, unsigned long from, |
| 330 | unsigned long to, unsigned long len) |
| 331 | { |
| 332 | unsigned long *table; |
| 333 | unsigned long off; |
| 334 | int flush; |
| 335 | |
| 336 | if ((from | to | len) & (PMD_SIZE - 1)) |
| 337 | return -EINVAL; |
| 338 | if (len == 0 || from + len > PGDIR_SIZE || |
| 339 | from + len < from || to + len < to) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | flush = 0; |
| 343 | down_read(&gmap->mm->mmap_sem); |
Carsten Otte | cc77245 | 2011-10-30 15:17:01 +0100 | [diff] [blame] | 344 | spin_lock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 345 | for (off = 0; off < len; off += PMD_SIZE) { |
| 346 | /* Walk the gmap address space page table */ |
| 347 | table = gmap->table + (((to + off) >> 53) & 0x7ff); |
| 348 | if ((*table & _REGION_ENTRY_INV) && |
| 349 | gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY)) |
| 350 | goto out_unmap; |
| 351 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 352 | table = table + (((to + off) >> 42) & 0x7ff); |
| 353 | if ((*table & _REGION_ENTRY_INV) && |
| 354 | gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY)) |
| 355 | goto out_unmap; |
| 356 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 357 | table = table + (((to + off) >> 31) & 0x7ff); |
| 358 | if ((*table & _REGION_ENTRY_INV) && |
| 359 | gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY)) |
| 360 | goto out_unmap; |
| 361 | table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN); |
| 362 | table = table + (((to + off) >> 20) & 0x7ff); |
| 363 | |
| 364 | /* Store 'from' address in an invalid segment table entry. */ |
| 365 | flush |= gmap_unlink_segment(gmap, table); |
| 366 | *table = _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO | (from + off); |
| 367 | } |
Carsten Otte | cc77245 | 2011-10-30 15:17:01 +0100 | [diff] [blame] | 368 | spin_unlock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 369 | up_read(&gmap->mm->mmap_sem); |
| 370 | if (flush) |
| 371 | gmap_flush_tlb(gmap); |
| 372 | return 0; |
| 373 | |
| 374 | out_unmap: |
Carsten Otte | cc77245 | 2011-10-30 15:17:01 +0100 | [diff] [blame] | 375 | spin_unlock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 376 | up_read(&gmap->mm->mmap_sem); |
| 377 | gmap_unmap_segment(gmap, to, len); |
| 378 | return -ENOMEM; |
| 379 | } |
| 380 | EXPORT_SYMBOL_GPL(gmap_map_segment); |
| 381 | |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 382 | static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap) |
| 383 | { |
| 384 | unsigned long *table; |
| 385 | |
| 386 | table = gmap->table + ((address >> 53) & 0x7ff); |
| 387 | if (unlikely(*table & _REGION_ENTRY_INV)) |
| 388 | return ERR_PTR(-EFAULT); |
| 389 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 390 | table = table + ((address >> 42) & 0x7ff); |
| 391 | if (unlikely(*table & _REGION_ENTRY_INV)) |
| 392 | return ERR_PTR(-EFAULT); |
| 393 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 394 | table = table + ((address >> 31) & 0x7ff); |
| 395 | if (unlikely(*table & _REGION_ENTRY_INV)) |
| 396 | return ERR_PTR(-EFAULT); |
| 397 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 398 | table = table + ((address >> 20) & 0x7ff); |
| 399 | return table; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * __gmap_translate - translate a guest address to a user space address |
| 404 | * @address: guest address |
| 405 | * @gmap: pointer to guest mapping meta data structure |
| 406 | * |
| 407 | * Returns user space address which corresponds to the guest address or |
| 408 | * -EFAULT if no such mapping exists. |
| 409 | * This function does not establish potentially missing page table entries. |
| 410 | * The mmap_sem of the mm that belongs to the address space must be held |
| 411 | * when this function gets called. |
| 412 | */ |
| 413 | unsigned long __gmap_translate(unsigned long address, struct gmap *gmap) |
| 414 | { |
| 415 | unsigned long *segment_ptr, vmaddr, segment; |
| 416 | struct gmap_pgtable *mp; |
| 417 | struct page *page; |
| 418 | |
| 419 | current->thread.gmap_addr = address; |
| 420 | segment_ptr = gmap_table_walk(address, gmap); |
| 421 | if (IS_ERR(segment_ptr)) |
| 422 | return PTR_ERR(segment_ptr); |
| 423 | /* Convert the gmap address to an mm address. */ |
| 424 | segment = *segment_ptr; |
| 425 | if (!(segment & _SEGMENT_ENTRY_INV)) { |
| 426 | page = pfn_to_page(segment >> PAGE_SHIFT); |
| 427 | mp = (struct gmap_pgtable *) page->index; |
| 428 | return mp->vmaddr | (address & ~PMD_MASK); |
| 429 | } else if (segment & _SEGMENT_ENTRY_RO) { |
| 430 | vmaddr = segment & _SEGMENT_ENTRY_ORIGIN; |
| 431 | return vmaddr | (address & ~PMD_MASK); |
| 432 | } |
| 433 | return -EFAULT; |
| 434 | } |
| 435 | EXPORT_SYMBOL_GPL(__gmap_translate); |
| 436 | |
| 437 | /** |
| 438 | * gmap_translate - translate a guest address to a user space address |
| 439 | * @address: guest address |
| 440 | * @gmap: pointer to guest mapping meta data structure |
| 441 | * |
| 442 | * Returns user space address which corresponds to the guest address or |
| 443 | * -EFAULT if no such mapping exists. |
| 444 | * This function does not establish potentially missing page table entries. |
| 445 | */ |
| 446 | unsigned long gmap_translate(unsigned long address, struct gmap *gmap) |
| 447 | { |
| 448 | unsigned long rc; |
| 449 | |
| 450 | down_read(&gmap->mm->mmap_sem); |
| 451 | rc = __gmap_translate(address, gmap); |
| 452 | up_read(&gmap->mm->mmap_sem); |
| 453 | return rc; |
| 454 | } |
| 455 | EXPORT_SYMBOL_GPL(gmap_translate); |
| 456 | |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 457 | static int gmap_connect_pgtable(unsigned long address, unsigned long segment, |
| 458 | unsigned long *segment_ptr, struct gmap *gmap) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 459 | { |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 460 | unsigned long vmaddr; |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 461 | struct vm_area_struct *vma; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 462 | struct gmap_pgtable *mp; |
| 463 | struct gmap_rmap *rmap; |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 464 | struct mm_struct *mm; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 465 | struct page *page; |
| 466 | pgd_t *pgd; |
| 467 | pud_t *pud; |
| 468 | pmd_t *pmd; |
| 469 | |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 470 | mm = gmap->mm; |
| 471 | vmaddr = segment & _SEGMENT_ENTRY_ORIGIN; |
| 472 | vma = find_vma(mm, vmaddr); |
| 473 | if (!vma || vma->vm_start > vmaddr) |
| 474 | return -EFAULT; |
| 475 | /* Walk the parent mm page table */ |
| 476 | pgd = pgd_offset(mm, vmaddr); |
| 477 | pud = pud_alloc(mm, pgd, vmaddr); |
| 478 | if (!pud) |
| 479 | return -ENOMEM; |
| 480 | pmd = pmd_alloc(mm, pud, vmaddr); |
| 481 | if (!pmd) |
| 482 | return -ENOMEM; |
| 483 | if (!pmd_present(*pmd) && |
| 484 | __pte_alloc(mm, vma, pmd, vmaddr)) |
| 485 | return -ENOMEM; |
| 486 | /* pmd now points to a valid segment table entry. */ |
| 487 | rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT); |
| 488 | if (!rmap) |
| 489 | return -ENOMEM; |
| 490 | /* Link gmap segment table entry location to page table. */ |
| 491 | page = pmd_page(*pmd); |
| 492 | mp = (struct gmap_pgtable *) page->index; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 493 | rmap->gmap = gmap; |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 494 | rmap->entry = segment_ptr; |
Christian Borntraeger | e86cbd8 | 2013-05-29 13:08:39 +0200 | [diff] [blame] | 495 | rmap->vmaddr = address & PMD_MASK; |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 496 | spin_lock(&mm->page_table_lock); |
| 497 | if (*segment_ptr == segment) { |
| 498 | list_add(&rmap->list, &mp->mapper); |
| 499 | /* Set gmap segment table entry to page table. */ |
| 500 | *segment_ptr = pmd_val(*pmd) & PAGE_MASK; |
| 501 | rmap = NULL; |
| 502 | } |
| 503 | spin_unlock(&mm->page_table_lock); |
| 504 | kfree(rmap); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table) |
| 509 | { |
| 510 | struct gmap_rmap *rmap, *next; |
| 511 | struct gmap_pgtable *mp; |
| 512 | struct page *page; |
| 513 | int flush; |
| 514 | |
| 515 | flush = 0; |
| 516 | spin_lock(&mm->page_table_lock); |
| 517 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 518 | mp = (struct gmap_pgtable *) page->index; |
| 519 | list_for_each_entry_safe(rmap, next, &mp->mapper, list) { |
| 520 | *rmap->entry = |
| 521 | _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO | mp->vmaddr; |
| 522 | list_del(&rmap->list); |
| 523 | kfree(rmap); |
| 524 | flush = 1; |
| 525 | } |
| 526 | spin_unlock(&mm->page_table_lock); |
| 527 | if (flush) |
| 528 | __tlb_flush_global(); |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * this function is assumed to be called with mmap_sem held |
| 533 | */ |
| 534 | unsigned long __gmap_fault(unsigned long address, struct gmap *gmap) |
| 535 | { |
| 536 | unsigned long *segment_ptr, segment; |
| 537 | struct gmap_pgtable *mp; |
| 538 | struct page *page; |
| 539 | int rc; |
| 540 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 541 | current->thread.gmap_addr = address; |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 542 | segment_ptr = gmap_table_walk(address, gmap); |
| 543 | if (IS_ERR(segment_ptr)) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 544 | return -EFAULT; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 545 | /* Convert the gmap address to an mm address. */ |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 546 | while (1) { |
| 547 | segment = *segment_ptr; |
| 548 | if (!(segment & _SEGMENT_ENTRY_INV)) { |
| 549 | /* Page table is present */ |
| 550 | page = pfn_to_page(segment >> PAGE_SHIFT); |
| 551 | mp = (struct gmap_pgtable *) page->index; |
| 552 | return mp->vmaddr | (address & ~PMD_MASK); |
| 553 | } |
| 554 | if (!(segment & _SEGMENT_ENTRY_RO)) |
| 555 | /* Nothing mapped in the gmap address space. */ |
| 556 | break; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 557 | rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap); |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 558 | if (rc) |
| 559 | return rc; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 560 | } |
| 561 | return -EFAULT; |
Carsten Otte | 499069e | 2011-10-30 15:17:02 +0100 | [diff] [blame] | 562 | } |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 563 | |
Carsten Otte | 499069e | 2011-10-30 15:17:02 +0100 | [diff] [blame] | 564 | unsigned long gmap_fault(unsigned long address, struct gmap *gmap) |
| 565 | { |
| 566 | unsigned long rc; |
| 567 | |
| 568 | down_read(&gmap->mm->mmap_sem); |
| 569 | rc = __gmap_fault(address, gmap); |
| 570 | up_read(&gmap->mm->mmap_sem); |
| 571 | |
| 572 | return rc; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 573 | } |
| 574 | EXPORT_SYMBOL_GPL(gmap_fault); |
| 575 | |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 576 | void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap) |
| 577 | { |
| 578 | |
| 579 | unsigned long *table, address, size; |
| 580 | struct vm_area_struct *vma; |
| 581 | struct gmap_pgtable *mp; |
| 582 | struct page *page; |
| 583 | |
| 584 | down_read(&gmap->mm->mmap_sem); |
| 585 | address = from; |
| 586 | while (address < to) { |
| 587 | /* Walk the gmap address space page table */ |
| 588 | table = gmap->table + ((address >> 53) & 0x7ff); |
| 589 | if (unlikely(*table & _REGION_ENTRY_INV)) { |
| 590 | address = (address + PMD_SIZE) & PMD_MASK; |
| 591 | continue; |
| 592 | } |
| 593 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 594 | table = table + ((address >> 42) & 0x7ff); |
| 595 | if (unlikely(*table & _REGION_ENTRY_INV)) { |
| 596 | address = (address + PMD_SIZE) & PMD_MASK; |
| 597 | continue; |
| 598 | } |
| 599 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 600 | table = table + ((address >> 31) & 0x7ff); |
| 601 | if (unlikely(*table & _REGION_ENTRY_INV)) { |
| 602 | address = (address + PMD_SIZE) & PMD_MASK; |
| 603 | continue; |
| 604 | } |
| 605 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 606 | table = table + ((address >> 20) & 0x7ff); |
| 607 | if (unlikely(*table & _SEGMENT_ENTRY_INV)) { |
| 608 | address = (address + PMD_SIZE) & PMD_MASK; |
| 609 | continue; |
| 610 | } |
| 611 | page = pfn_to_page(*table >> PAGE_SHIFT); |
| 612 | mp = (struct gmap_pgtable *) page->index; |
| 613 | vma = find_vma(gmap->mm, mp->vmaddr); |
| 614 | size = min(to - address, PMD_SIZE - (address & ~PMD_MASK)); |
| 615 | zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK), |
| 616 | size, NULL); |
| 617 | address = (address + PMD_SIZE) & PMD_MASK; |
| 618 | } |
| 619 | up_read(&gmap->mm->mmap_sem); |
| 620 | } |
| 621 | EXPORT_SYMBOL_GPL(gmap_discard); |
| 622 | |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 623 | static LIST_HEAD(gmap_notifier_list); |
| 624 | static DEFINE_SPINLOCK(gmap_notifier_lock); |
| 625 | |
| 626 | /** |
| 627 | * gmap_register_ipte_notifier - register a pte invalidation callback |
| 628 | * @nb: pointer to the gmap notifier block |
| 629 | */ |
| 630 | void gmap_register_ipte_notifier(struct gmap_notifier *nb) |
| 631 | { |
| 632 | spin_lock(&gmap_notifier_lock); |
| 633 | list_add(&nb->list, &gmap_notifier_list); |
| 634 | spin_unlock(&gmap_notifier_lock); |
| 635 | } |
| 636 | EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier); |
| 637 | |
| 638 | /** |
| 639 | * gmap_unregister_ipte_notifier - remove a pte invalidation callback |
| 640 | * @nb: pointer to the gmap notifier block |
| 641 | */ |
| 642 | void gmap_unregister_ipte_notifier(struct gmap_notifier *nb) |
| 643 | { |
| 644 | spin_lock(&gmap_notifier_lock); |
| 645 | list_del_init(&nb->list); |
| 646 | spin_unlock(&gmap_notifier_lock); |
| 647 | } |
| 648 | EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier); |
| 649 | |
| 650 | /** |
| 651 | * gmap_ipte_notify - mark a range of ptes for invalidation notification |
| 652 | * @gmap: pointer to guest mapping meta data structure |
| 653 | * @address: virtual address in the guest address space |
| 654 | * @len: size of area |
| 655 | * |
| 656 | * Returns 0 if for each page in the given range a gmap mapping exists and |
| 657 | * the invalidation notification could be set. If the gmap mapping is missing |
| 658 | * for one or more pages -EFAULT is returned. If no memory could be allocated |
| 659 | * -ENOMEM is returned. This function establishes missing page table entries. |
| 660 | */ |
| 661 | int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len) |
| 662 | { |
| 663 | unsigned long addr; |
| 664 | spinlock_t *ptl; |
| 665 | pte_t *ptep, entry; |
| 666 | pgste_t pgste; |
| 667 | int rc = 0; |
| 668 | |
| 669 | if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK)) |
| 670 | return -EINVAL; |
| 671 | down_read(&gmap->mm->mmap_sem); |
| 672 | while (len) { |
| 673 | /* Convert gmap address and connect the page tables */ |
| 674 | addr = __gmap_fault(start, gmap); |
| 675 | if (IS_ERR_VALUE(addr)) { |
| 676 | rc = addr; |
| 677 | break; |
| 678 | } |
| 679 | /* Get the page mapped */ |
Christian Borntraeger | bb4b42c | 2013-05-08 15:25:38 +0200 | [diff] [blame] | 680 | if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) { |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 681 | rc = -EFAULT; |
| 682 | break; |
| 683 | } |
| 684 | /* Walk the process page table, lock and get pte pointer */ |
| 685 | ptep = get_locked_pte(gmap->mm, addr, &ptl); |
| 686 | if (unlikely(!ptep)) |
| 687 | continue; |
| 688 | /* Set notification bit in the pgste of the pte */ |
| 689 | entry = *ptep; |
| 690 | if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_RO)) == 0) { |
| 691 | pgste = pgste_get_lock(ptep); |
Martin Schwidefsky | 0d0dafc | 2013-05-17 14:41:33 +0200 | [diff] [blame] | 692 | pgste_val(pgste) |= PGSTE_IN_BIT; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 693 | pgste_set_unlock(ptep, pgste); |
| 694 | start += PAGE_SIZE; |
| 695 | len -= PAGE_SIZE; |
| 696 | } |
| 697 | spin_unlock(ptl); |
| 698 | } |
| 699 | up_read(&gmap->mm->mmap_sem); |
| 700 | return rc; |
| 701 | } |
| 702 | EXPORT_SYMBOL_GPL(gmap_ipte_notify); |
| 703 | |
| 704 | /** |
| 705 | * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte. |
| 706 | * @mm: pointer to the process mm_struct |
| 707 | * @addr: virtual address in the process address space |
| 708 | * @pte: pointer to the page table entry |
| 709 | * |
| 710 | * This function is assumed to be called with the page table lock held |
| 711 | * for the pte to notify. |
| 712 | */ |
| 713 | void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long addr, pte_t *pte) |
| 714 | { |
| 715 | unsigned long segment_offset; |
| 716 | struct gmap_notifier *nb; |
| 717 | struct gmap_pgtable *mp; |
| 718 | struct gmap_rmap *rmap; |
| 719 | struct page *page; |
| 720 | |
| 721 | segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t)); |
| 722 | segment_offset = segment_offset * (4096 / sizeof(pte_t)); |
| 723 | page = pfn_to_page(__pa(pte) >> PAGE_SHIFT); |
| 724 | mp = (struct gmap_pgtable *) page->index; |
| 725 | spin_lock(&gmap_notifier_lock); |
| 726 | list_for_each_entry(rmap, &mp->mapper, list) { |
| 727 | list_for_each_entry(nb, &gmap_notifier_list, list) |
| 728 | nb->notifier_call(rmap->gmap, |
| 729 | rmap->vmaddr + segment_offset); |
| 730 | } |
| 731 | spin_unlock(&gmap_notifier_lock); |
| 732 | } |
| 733 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 734 | static inline int page_table_with_pgste(struct page *page) |
| 735 | { |
| 736 | return atomic_read(&page->_mapcount) == 0; |
| 737 | } |
| 738 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 739 | static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm, |
| 740 | unsigned long vmaddr) |
| 741 | { |
| 742 | struct page *page; |
| 743 | unsigned long *table; |
| 744 | struct gmap_pgtable *mp; |
| 745 | |
| 746 | page = alloc_page(GFP_KERNEL|__GFP_REPEAT); |
| 747 | if (!page) |
| 748 | return NULL; |
| 749 | mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT); |
| 750 | if (!mp) { |
| 751 | __free_page(page); |
| 752 | return NULL; |
| 753 | } |
| 754 | pgtable_page_ctor(page); |
| 755 | mp->vmaddr = vmaddr & PMD_MASK; |
| 756 | INIT_LIST_HEAD(&mp->mapper); |
| 757 | page->index = (unsigned long) mp; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 758 | atomic_set(&page->_mapcount, 0); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 759 | table = (unsigned long *) page_to_phys(page); |
| 760 | clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2); |
| 761 | clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2); |
| 762 | return table; |
| 763 | } |
| 764 | |
| 765 | static inline void page_table_free_pgste(unsigned long *table) |
| 766 | { |
| 767 | struct page *page; |
| 768 | struct gmap_pgtable *mp; |
| 769 | |
| 770 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 771 | mp = (struct gmap_pgtable *) page->index; |
| 772 | BUG_ON(!list_empty(&mp->mapper)); |
Martin Schwidefsky | 2320c57 | 2012-02-17 10:29:21 +0100 | [diff] [blame] | 773 | pgtable_page_dtor(page); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 774 | atomic_set(&page->_mapcount, -1); |
| 775 | kfree(mp); |
| 776 | __free_page(page); |
| 777 | } |
| 778 | |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 779 | int set_guest_storage_key(struct mm_struct *mm, unsigned long addr, |
| 780 | unsigned long key, bool nq) |
| 781 | { |
| 782 | spinlock_t *ptl; |
| 783 | pgste_t old, new; |
| 784 | pte_t *ptep; |
| 785 | |
| 786 | down_read(&mm->mmap_sem); |
| 787 | ptep = get_locked_pte(current->mm, addr, &ptl); |
| 788 | if (unlikely(!ptep)) { |
| 789 | up_read(&mm->mmap_sem); |
| 790 | return -EFAULT; |
| 791 | } |
| 792 | |
| 793 | new = old = pgste_get_lock(ptep); |
| 794 | pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT | |
| 795 | PGSTE_ACC_BITS | PGSTE_FP_BIT); |
| 796 | pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48; |
| 797 | pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56; |
| 798 | if (!(pte_val(*ptep) & _PAGE_INVALID)) { |
| 799 | unsigned long address, bits; |
| 800 | unsigned char skey; |
| 801 | |
| 802 | address = pte_val(*ptep) & PAGE_MASK; |
| 803 | skey = page_get_storage_key(address); |
| 804 | bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED); |
| 805 | /* Set storage key ACC and FP */ |
| 806 | page_set_storage_key(address, |
| 807 | (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)), |
| 808 | !nq); |
| 809 | |
| 810 | /* Merge host changed & referenced into pgste */ |
| 811 | pgste_val(new) |= bits << 52; |
| 812 | /* Transfer skey changed & referenced bit to kvm user bits */ |
| 813 | pgste_val(new) |= bits << 45; /* PGSTE_UR_BIT & PGSTE_UC_BIT */ |
| 814 | } |
| 815 | /* changing the guest storage key is considered a change of the page */ |
| 816 | if ((pgste_val(new) ^ pgste_val(old)) & |
| 817 | (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT)) |
| 818 | pgste_val(new) |= PGSTE_UC_BIT; |
| 819 | |
| 820 | pgste_set_unlock(ptep, new); |
| 821 | pte_unmap_unlock(*ptep, ptl); |
| 822 | up_read(&mm->mmap_sem); |
| 823 | return 0; |
| 824 | } |
| 825 | EXPORT_SYMBOL(set_guest_storage_key); |
| 826 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 827 | #else /* CONFIG_PGSTE */ |
| 828 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 829 | static inline int page_table_with_pgste(struct page *page) |
| 830 | { |
| 831 | return 0; |
| 832 | } |
| 833 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 834 | static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm, |
| 835 | unsigned long vmaddr) |
| 836 | { |
Jan Glauber | 944291d | 2011-08-03 16:44:18 +0200 | [diff] [blame] | 837 | return NULL; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static inline void page_table_free_pgste(unsigned long *table) |
| 841 | { |
| 842 | } |
| 843 | |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 844 | static inline void gmap_disconnect_pgtable(struct mm_struct *mm, |
| 845 | unsigned long *table) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 846 | { |
| 847 | } |
| 848 | |
| 849 | #endif /* CONFIG_PGSTE */ |
| 850 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 851 | static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits) |
| 852 | { |
| 853 | unsigned int old, new; |
| 854 | |
| 855 | do { |
| 856 | old = atomic_read(v); |
| 857 | new = old ^ bits; |
| 858 | } while (atomic_cmpxchg(v, old, new) != old); |
| 859 | return new; |
| 860 | } |
| 861 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 862 | /* |
| 863 | * page table entry allocation/free routines. |
| 864 | */ |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 865 | unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 866 | { |
Heiko Carstens | 41459d3 | 2012-09-14 11:09:52 +0200 | [diff] [blame] | 867 | unsigned long *uninitialized_var(table); |
| 868 | struct page *uninitialized_var(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 869 | unsigned int mask, bit; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 870 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 871 | if (mm_has_pgste(mm)) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 872 | return page_table_alloc_pgste(mm, vmaddr); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 873 | /* Allocate fragments of a 4K page as 1K/2K page table */ |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 874 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 875 | mask = FRAG_MASK; |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 876 | if (!list_empty(&mm->context.pgtable_list)) { |
| 877 | page = list_first_entry(&mm->context.pgtable_list, |
| 878 | struct page, lru); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 879 | table = (unsigned long *) page_to_phys(page); |
| 880 | mask = atomic_read(&page->_mapcount); |
| 881 | mask = mask | (mask >> 4); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 882 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 883 | if ((mask & FRAG_MASK) == FRAG_MASK) { |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 884 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 885 | page = alloc_page(GFP_KERNEL|__GFP_REPEAT); |
| 886 | if (!page) |
| 887 | return NULL; |
| 888 | pgtable_page_ctor(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 889 | atomic_set(&page->_mapcount, 1); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 890 | table = (unsigned long *) page_to_phys(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 891 | clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 892 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 893 | list_add(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 894 | } else { |
| 895 | for (bit = 1; mask & bit; bit <<= 1) |
| 896 | table += PTRS_PER_PTE; |
| 897 | mask = atomic_xor_bits(&page->_mapcount, bit); |
| 898 | if ((mask & FRAG_MASK) == FRAG_MASK) |
| 899 | list_del(&page->lru); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 900 | } |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 901 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 902 | return table; |
| 903 | } |
| 904 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 905 | void page_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 906 | { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 907 | struct page *page; |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 908 | unsigned int bit, mask; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 909 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 910 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 911 | if (page_table_with_pgste(page)) { |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 912 | gmap_disconnect_pgtable(mm, table); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 913 | return page_table_free_pgste(table); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 914 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 915 | /* Free 1K/2K page table fragment of a 4K page */ |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 916 | bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t))); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 917 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 918 | if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK) |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 919 | list_del(&page->lru); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 920 | mask = atomic_xor_bits(&page->_mapcount, bit); |
| 921 | if (mask & FRAG_MASK) |
| 922 | list_add(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 923 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 924 | if (mask == 0) { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 925 | pgtable_page_dtor(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 926 | atomic_set(&page->_mapcount, -1); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 927 | __free_page(page); |
| 928 | } |
| 929 | } |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 930 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 931 | static void __page_table_free_rcu(void *table, unsigned bit) |
| 932 | { |
| 933 | struct page *page; |
| 934 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 935 | if (bit == FRAG_MASK) |
| 936 | return page_table_free_pgste(table); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 937 | /* Free 1K/2K page table fragment of a 4K page */ |
| 938 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 939 | if (atomic_xor_bits(&page->_mapcount, bit) == 0) { |
| 940 | pgtable_page_dtor(page); |
| 941 | atomic_set(&page->_mapcount, -1); |
| 942 | __free_page(page); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 943 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table) |
| 947 | { |
| 948 | struct mm_struct *mm; |
| 949 | struct page *page; |
| 950 | unsigned int bit, mask; |
| 951 | |
| 952 | mm = tlb->mm; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 953 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 954 | if (page_table_with_pgste(page)) { |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 955 | gmap_disconnect_pgtable(mm, table); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 956 | table = (unsigned long *) (__pa(table) | FRAG_MASK); |
| 957 | tlb_remove_table(tlb, table); |
| 958 | return; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 959 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 960 | bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t))); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 961 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 962 | if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK) |
| 963 | list_del(&page->lru); |
| 964 | mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4)); |
| 965 | if (mask & FRAG_MASK) |
| 966 | list_add_tail(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 967 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 968 | table = (unsigned long *) (__pa(table) | (bit << 4)); |
| 969 | tlb_remove_table(tlb, table); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 972 | void __tlb_remove_table(void *_table) |
| 973 | { |
Martin Schwidefsky | e73b7ff | 2011-10-30 15:16:08 +0100 | [diff] [blame] | 974 | const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK; |
| 975 | void *table = (void *)((unsigned long) _table & ~mask); |
| 976 | unsigned type = (unsigned long) _table & mask; |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 977 | |
| 978 | if (type) |
| 979 | __page_table_free_rcu(table, type); |
| 980 | else |
| 981 | free_pages((unsigned long) table, ALLOC_ORDER); |
| 982 | } |
| 983 | |
Martin Schwidefsky | cd94154cc | 2012-04-11 14:28:07 +0200 | [diff] [blame] | 984 | static void tlb_remove_table_smp_sync(void *arg) |
| 985 | { |
| 986 | /* Simply deliver the interrupt */ |
| 987 | } |
| 988 | |
| 989 | static void tlb_remove_table_one(void *table) |
| 990 | { |
| 991 | /* |
| 992 | * This isn't an RCU grace period and hence the page-tables cannot be |
| 993 | * assumed to be actually RCU-freed. |
| 994 | * |
| 995 | * It is however sufficient for software page-table walkers that rely |
| 996 | * on IRQ disabling. See the comment near struct mmu_table_batch. |
| 997 | */ |
| 998 | smp_call_function(tlb_remove_table_smp_sync, NULL, 1); |
| 999 | __tlb_remove_table(table); |
| 1000 | } |
| 1001 | |
| 1002 | static void tlb_remove_table_rcu(struct rcu_head *head) |
| 1003 | { |
| 1004 | struct mmu_table_batch *batch; |
| 1005 | int i; |
| 1006 | |
| 1007 | batch = container_of(head, struct mmu_table_batch, rcu); |
| 1008 | |
| 1009 | for (i = 0; i < batch->nr; i++) |
| 1010 | __tlb_remove_table(batch->tables[i]); |
| 1011 | |
| 1012 | free_page((unsigned long)batch); |
| 1013 | } |
| 1014 | |
| 1015 | void tlb_table_flush(struct mmu_gather *tlb) |
| 1016 | { |
| 1017 | struct mmu_table_batch **batch = &tlb->batch; |
| 1018 | |
| 1019 | if (*batch) { |
| 1020 | __tlb_flush_mm(tlb->mm); |
| 1021 | call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); |
| 1022 | *batch = NULL; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | void tlb_remove_table(struct mmu_gather *tlb, void *table) |
| 1027 | { |
| 1028 | struct mmu_table_batch **batch = &tlb->batch; |
| 1029 | |
| 1030 | if (*batch == NULL) { |
| 1031 | *batch = (struct mmu_table_batch *) |
| 1032 | __get_free_page(GFP_NOWAIT | __GFP_NOWARN); |
| 1033 | if (*batch == NULL) { |
| 1034 | __tlb_flush_mm(tlb->mm); |
| 1035 | tlb_remove_table_one(table); |
| 1036 | return; |
| 1037 | } |
| 1038 | (*batch)->nr = 0; |
| 1039 | } |
| 1040 | (*batch)->tables[(*batch)->nr++] = table; |
| 1041 | if ((*batch)->nr == MAX_TABLE_BATCH) |
| 1042 | tlb_table_flush(tlb); |
| 1043 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1044 | |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1045 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1046 | static inline void thp_split_vma(struct vm_area_struct *vma) |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1047 | { |
| 1048 | unsigned long addr; |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1049 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1050 | for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE) |
| 1051 | follow_page(vma, addr, FOLL_SPLIT); |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1054 | static inline void thp_split_mm(struct mm_struct *mm) |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1055 | { |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1056 | struct vm_area_struct *vma; |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1057 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1058 | for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) { |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1059 | thp_split_vma(vma); |
| 1060 | vma->vm_flags &= ~VM_HUGEPAGE; |
| 1061 | vma->vm_flags |= VM_NOHUGEPAGE; |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1062 | } |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1063 | mm->def_flags |= VM_NOHUGEPAGE; |
| 1064 | } |
| 1065 | #else |
| 1066 | static inline void thp_split_mm(struct mm_struct *mm) |
| 1067 | { |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1068 | } |
| 1069 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 1070 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1071 | static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb, |
| 1072 | struct mm_struct *mm, pud_t *pud, |
| 1073 | unsigned long addr, unsigned long end) |
| 1074 | { |
| 1075 | unsigned long next, *table, *new; |
| 1076 | struct page *page; |
| 1077 | pmd_t *pmd; |
| 1078 | |
| 1079 | pmd = pmd_offset(pud, addr); |
| 1080 | do { |
| 1081 | next = pmd_addr_end(addr, end); |
| 1082 | again: |
| 1083 | if (pmd_none_or_clear_bad(pmd)) |
| 1084 | continue; |
| 1085 | table = (unsigned long *) pmd_deref(*pmd); |
| 1086 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 1087 | if (page_table_with_pgste(page)) |
| 1088 | continue; |
| 1089 | /* Allocate new page table with pgstes */ |
| 1090 | new = page_table_alloc_pgste(mm, addr); |
| 1091 | if (!new) { |
| 1092 | mm->context.has_pgste = 0; |
| 1093 | continue; |
| 1094 | } |
| 1095 | spin_lock(&mm->page_table_lock); |
| 1096 | if (likely((unsigned long *) pmd_deref(*pmd) == table)) { |
| 1097 | /* Nuke pmd entry pointing to the "short" page table */ |
| 1098 | pmdp_flush_lazy(mm, addr, pmd); |
| 1099 | pmd_clear(pmd); |
| 1100 | /* Copy ptes from old table to new table */ |
| 1101 | memcpy(new, table, PAGE_SIZE/2); |
| 1102 | clear_table(table, _PAGE_INVALID, PAGE_SIZE/2); |
| 1103 | /* Establish new table */ |
| 1104 | pmd_populate(mm, pmd, (pte_t *) new); |
| 1105 | /* Free old table with rcu, there might be a walker! */ |
| 1106 | page_table_free_rcu(tlb, table); |
| 1107 | new = NULL; |
| 1108 | } |
| 1109 | spin_unlock(&mm->page_table_lock); |
| 1110 | if (new) { |
| 1111 | page_table_free_pgste(new); |
| 1112 | goto again; |
| 1113 | } |
| 1114 | } while (pmd++, addr = next, addr != end); |
| 1115 | |
| 1116 | return addr; |
| 1117 | } |
| 1118 | |
| 1119 | static unsigned long page_table_realloc_pud(struct mmu_gather *tlb, |
| 1120 | struct mm_struct *mm, pgd_t *pgd, |
| 1121 | unsigned long addr, unsigned long end) |
| 1122 | { |
| 1123 | unsigned long next; |
| 1124 | pud_t *pud; |
| 1125 | |
| 1126 | pud = pud_offset(pgd, addr); |
| 1127 | do { |
| 1128 | next = pud_addr_end(addr, end); |
| 1129 | if (pud_none_or_clear_bad(pud)) |
| 1130 | continue; |
| 1131 | next = page_table_realloc_pmd(tlb, mm, pud, addr, next); |
| 1132 | } while (pud++, addr = next, addr != end); |
| 1133 | |
| 1134 | return addr; |
| 1135 | } |
| 1136 | |
| 1137 | static void page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm, |
| 1138 | unsigned long addr, unsigned long end) |
| 1139 | { |
| 1140 | unsigned long next; |
| 1141 | pgd_t *pgd; |
| 1142 | |
| 1143 | pgd = pgd_offset(mm, addr); |
| 1144 | do { |
| 1145 | next = pgd_addr_end(addr, end); |
| 1146 | if (pgd_none_or_clear_bad(pgd)) |
| 1147 | continue; |
| 1148 | next = page_table_realloc_pud(tlb, mm, pgd, addr, next); |
| 1149 | } while (pgd++, addr = next, addr != end); |
| 1150 | } |
| 1151 | |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1152 | /* |
| 1153 | * switch on pgstes for its userspace process (for kvm) |
| 1154 | */ |
| 1155 | int s390_enable_sie(void) |
| 1156 | { |
| 1157 | struct task_struct *tsk = current; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1158 | struct mm_struct *mm = tsk->mm; |
| 1159 | struct mmu_gather tlb; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1160 | |
Carsten Otte | 702d9e5 | 2009-03-26 15:23:57 +0100 | [diff] [blame] | 1161 | /* Do we have switched amode? If no, we cannot do sie */ |
Heiko Carstens | d1b0d84 | 2012-09-02 11:02:23 +0200 | [diff] [blame] | 1162 | if (s390_user_mode == HOME_SPACE_MODE) |
Carsten Otte | 702d9e5 | 2009-03-26 15:23:57 +0100 | [diff] [blame] | 1163 | return -EINVAL; |
| 1164 | |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 1165 | /* Do we have pgstes? if yes, we are done */ |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1166 | if (mm_has_pgste(tsk->mm)) |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 1167 | return 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1168 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1169 | down_write(&mm->mmap_sem); |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1170 | /* split thp mappings and disable thp for future mappings */ |
| 1171 | thp_split_mm(mm); |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame^] | 1172 | /* Reallocate the page tables with pgstes */ |
| 1173 | mm->context.has_pgste = 1; |
| 1174 | tlb_gather_mmu(&tlb, mm, 0); |
| 1175 | page_table_realloc(&tlb, mm, 0, TASK_SIZE); |
| 1176 | tlb_finish_mmu(&tlb, 0, -1); |
| 1177 | up_write(&mm->mmap_sem); |
| 1178 | return mm->context.has_pgste ? 0 : -ENOMEM; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1179 | } |
| 1180 | EXPORT_SYMBOL_GPL(s390_enable_sie); |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 1181 | |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 1182 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Gerald Schaefer | 1ae1c1d | 2012-10-08 16:30:24 -0700 | [diff] [blame] | 1183 | int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address, |
| 1184 | pmd_t *pmdp) |
| 1185 | { |
| 1186 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 1187 | /* No need to flush TLB |
| 1188 | * On s390 reference bits are in storage key and never in TLB */ |
| 1189 | return pmdp_test_and_clear_young(vma, address, pmdp); |
| 1190 | } |
| 1191 | |
| 1192 | int pmdp_set_access_flags(struct vm_area_struct *vma, |
| 1193 | unsigned long address, pmd_t *pmdp, |
| 1194 | pmd_t entry, int dirty) |
| 1195 | { |
| 1196 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 1197 | |
| 1198 | if (pmd_same(*pmdp, entry)) |
| 1199 | return 0; |
| 1200 | pmdp_invalidate(vma, address, pmdp); |
| 1201 | set_pmd_at(vma->vm_mm, address, pmdp, entry); |
| 1202 | return 1; |
| 1203 | } |
| 1204 | |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 1205 | static void pmdp_splitting_flush_sync(void *arg) |
| 1206 | { |
| 1207 | /* Simply deliver the interrupt */ |
| 1208 | } |
| 1209 | |
| 1210 | void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address, |
| 1211 | pmd_t *pmdp) |
| 1212 | { |
| 1213 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 1214 | if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT, |
| 1215 | (unsigned long *) pmdp)) { |
| 1216 | /* need to serialize against gup-fast (IRQ disabled) */ |
| 1217 | smp_call_function(pmdp_splitting_flush_sync, NULL, 1); |
| 1218 | } |
| 1219 | } |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1220 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1221 | void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp, |
| 1222 | pgtable_t pgtable) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1223 | { |
| 1224 | struct list_head *lh = (struct list_head *) pgtable; |
| 1225 | |
| 1226 | assert_spin_locked(&mm->page_table_lock); |
| 1227 | |
| 1228 | /* FIFO */ |
| 1229 | if (!mm->pmd_huge_pte) |
| 1230 | INIT_LIST_HEAD(lh); |
| 1231 | else |
| 1232 | list_add(lh, (struct list_head *) mm->pmd_huge_pte); |
| 1233 | mm->pmd_huge_pte = pgtable; |
| 1234 | } |
| 1235 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1236 | pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1237 | { |
| 1238 | struct list_head *lh; |
| 1239 | pgtable_t pgtable; |
| 1240 | pte_t *ptep; |
| 1241 | |
| 1242 | assert_spin_locked(&mm->page_table_lock); |
| 1243 | |
| 1244 | /* FIFO */ |
| 1245 | pgtable = mm->pmd_huge_pte; |
| 1246 | lh = (struct list_head *) pgtable; |
| 1247 | if (list_empty(lh)) |
| 1248 | mm->pmd_huge_pte = NULL; |
| 1249 | else { |
| 1250 | mm->pmd_huge_pte = (pgtable_t) lh->next; |
| 1251 | list_del(lh); |
| 1252 | } |
| 1253 | ptep = (pte_t *) pgtable; |
| 1254 | pte_val(*ptep) = _PAGE_TYPE_EMPTY; |
| 1255 | ptep++; |
| 1256 | pte_val(*ptep) = _PAGE_TYPE_EMPTY; |
| 1257 | return pgtable; |
| 1258 | } |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 1259 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |