Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PPC64 (POWER4) Huge TLB Page Support for Kernel. |
| 3 | * |
| 4 | * Copyright (C) 2003 David Gibson, IBM Corporation. |
| 5 | * |
| 6 | * Based on the IA-32 version: |
| 7 | * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/hugetlb.h> |
| 14 | #include <linux/pagemap.h> |
| 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/sysctl.h> |
| 19 | #include <asm/mman.h> |
| 20 | #include <asm/pgalloc.h> |
| 21 | #include <asm/tlb.h> |
| 22 | #include <asm/tlbflush.h> |
| 23 | #include <asm/mmu_context.h> |
| 24 | #include <asm/machdep.h> |
| 25 | #include <asm/cputable.h> |
| 26 | #include <asm/tlb.h> |
| 27 | |
| 28 | #include <linux/sysctl.h> |
| 29 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 30 | /* Modelled after find_linux_pte() */ |
| 31 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 33 | pgd_t *pg; |
| 34 | pud_t *pu; |
| 35 | pmd_t *pm; |
| 36 | pte_t *pt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | BUG_ON(! in_hugepage_area(mm->context, addr)); |
| 39 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 40 | addr &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 42 | pg = pgd_offset(mm, addr); |
| 43 | if (!pgd_none(*pg)) { |
| 44 | pu = pud_offset(pg, addr); |
| 45 | if (!pud_none(*pu)) { |
| 46 | pm = pmd_offset(pu, addr); |
| 47 | pt = (pte_t *)pm; |
| 48 | BUG_ON(!pmd_none(*pm) |
| 49 | && !(pte_present(*pt) && pte_huge(*pt))); |
| 50 | return pt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 54 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 57 | pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 59 | pgd_t *pg; |
| 60 | pud_t *pu; |
| 61 | pmd_t *pm; |
| 62 | pte_t *pt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | BUG_ON(! in_hugepage_area(mm->context, addr)); |
| 65 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 66 | addr &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame^] | 68 | pg = pgd_offset(mm, addr); |
| 69 | pu = pud_alloc(mm, pg, addr); |
| 70 | |
| 71 | if (pu) { |
| 72 | pm = pmd_alloc(mm, pu, addr); |
| 73 | if (pm) { |
| 74 | pt = (pte_t *)pm; |
| 75 | BUG_ON(!pmd_none(*pm) |
| 76 | && !(pte_present(*pt) && pte_huge(*pt))); |
| 77 | return pt; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | #define HUGEPTE_BATCH_SIZE (HPAGE_SIZE / PMD_SIZE) |
| 85 | |
| 86 | void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, |
| 87 | pte_t *ptep, pte_t pte) |
| 88 | { |
| 89 | int i; |
| 90 | |
| 91 | if (pte_present(*ptep)) { |
| 92 | pte_clear(mm, addr, ptep); |
| 93 | flush_tlb_pending(); |
| 94 | } |
| 95 | |
| 96 | for (i = 0; i < HUGEPTE_BATCH_SIZE; i++) { |
| 97 | *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS); |
| 98 | ptep++; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, |
| 103 | pte_t *ptep) |
| 104 | { |
| 105 | unsigned long old = pte_update(ptep, ~0UL); |
| 106 | int i; |
| 107 | |
| 108 | if (old & _PAGE_HASHPTE) |
| 109 | hpte_update(mm, addr, old, 0); |
| 110 | |
| 111 | for (i = 1; i < HUGEPTE_BATCH_SIZE; i++) |
| 112 | ptep[i] = __pte(0); |
| 113 | |
| 114 | return __pte(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* |
| 118 | * This function checks for proper alignment of input addr and len parameters. |
| 119 | */ |
| 120 | int is_aligned_hugepage_range(unsigned long addr, unsigned long len) |
| 121 | { |
| 122 | if (len & ~HPAGE_MASK) |
| 123 | return -EINVAL; |
| 124 | if (addr & ~HPAGE_MASK) |
| 125 | return -EINVAL; |
| 126 | if (! (within_hugepage_low_range(addr, len) |
| 127 | || within_hugepage_high_range(addr, len)) ) |
| 128 | return -EINVAL; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static void flush_segments(void *parm) |
| 133 | { |
| 134 | u16 segs = (unsigned long) parm; |
| 135 | unsigned long i; |
| 136 | |
| 137 | asm volatile("isync" : : : "memory"); |
| 138 | |
| 139 | for (i = 0; i < 16; i++) { |
| 140 | if (! (segs & (1U << i))) |
| 141 | continue; |
| 142 | asm volatile("slbie %0" : : "r" (i << SID_SHIFT)); |
| 143 | } |
| 144 | |
| 145 | asm volatile("isync" : : : "memory"); |
| 146 | } |
| 147 | |
| 148 | static int prepare_low_seg_for_htlb(struct mm_struct *mm, unsigned long seg) |
| 149 | { |
| 150 | unsigned long start = seg << SID_SHIFT; |
| 151 | unsigned long end = (seg+1) << SID_SHIFT; |
| 152 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | BUG_ON(seg >= 16); |
| 155 | |
| 156 | /* Check no VMAs are in the region */ |
| 157 | vma = find_vma(mm, start); |
| 158 | if (vma && (vma->vm_start < end)) |
| 159 | return -EBUSY; |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int open_low_hpage_segs(struct mm_struct *mm, u16 newsegs) |
| 165 | { |
| 166 | unsigned long i; |
| 167 | |
| 168 | newsegs &= ~(mm->context.htlb_segs); |
| 169 | if (! newsegs) |
| 170 | return 0; /* The segments we want are already open */ |
| 171 | |
| 172 | for (i = 0; i < 16; i++) |
| 173 | if ((1 << i) & newsegs) |
| 174 | if (prepare_low_seg_for_htlb(mm, i) != 0) |
| 175 | return -EBUSY; |
| 176 | |
| 177 | mm->context.htlb_segs |= newsegs; |
| 178 | |
| 179 | /* update the paca copy of the context struct */ |
| 180 | get_paca()->context = mm->context; |
| 181 | |
| 182 | /* the context change must make it to memory before the flush, |
| 183 | * so that further SLB misses do the right thing. */ |
| 184 | mb(); |
| 185 | on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int prepare_hugepage_range(unsigned long addr, unsigned long len) |
| 191 | { |
| 192 | if (within_hugepage_high_range(addr, len)) |
| 193 | return 0; |
| 194 | else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) { |
| 195 | int err; |
| 196 | /* Yes, we need both tests, in case addr+len overflows |
| 197 | * 64-bit arithmetic */ |
| 198 | err = open_low_hpage_segs(current->mm, |
| 199 | LOW_ESID_MASK(addr, len)); |
| 200 | if (err) |
| 201 | printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)" |
| 202 | " failed (segs: 0x%04hx)\n", addr, len, |
| 203 | LOW_ESID_MASK(addr, len)); |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | struct page * |
| 211 | follow_huge_addr(struct mm_struct *mm, unsigned long address, int write) |
| 212 | { |
| 213 | pte_t *ptep; |
| 214 | struct page *page; |
| 215 | |
| 216 | if (! in_hugepage_area(mm->context, address)) |
| 217 | return ERR_PTR(-EINVAL); |
| 218 | |
| 219 | ptep = huge_pte_offset(mm, address); |
| 220 | page = pte_page(*ptep); |
| 221 | if (page) |
| 222 | page += (address % HPAGE_SIZE) / PAGE_SIZE; |
| 223 | |
| 224 | return page; |
| 225 | } |
| 226 | |
| 227 | int pmd_huge(pmd_t pmd) |
| 228 | { |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | struct page * |
| 233 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 234 | pmd_t *pmd, int write) |
| 235 | { |
| 236 | BUG(); |
| 237 | return NULL; |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | /* Because we have an exclusive hugepage region which lies within the |
| 241 | * normal user address space, we have to take special measures to make |
| 242 | * non-huge mmap()s evade the hugepage reserved regions. */ |
| 243 | unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 244 | unsigned long len, unsigned long pgoff, |
| 245 | unsigned long flags) |
| 246 | { |
| 247 | struct mm_struct *mm = current->mm; |
| 248 | struct vm_area_struct *vma; |
| 249 | unsigned long start_addr; |
| 250 | |
| 251 | if (len > TASK_SIZE) |
| 252 | return -ENOMEM; |
| 253 | |
| 254 | if (addr) { |
| 255 | addr = PAGE_ALIGN(addr); |
| 256 | vma = find_vma(mm, addr); |
| 257 | if (((TASK_SIZE - len) >= addr) |
| 258 | && (!vma || (addr+len) <= vma->vm_start) |
| 259 | && !is_hugepage_only_range(mm, addr,len)) |
| 260 | return addr; |
| 261 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 262 | if (len > mm->cached_hole_size) { |
| 263 | start_addr = addr = mm->free_area_cache; |
| 264 | } else { |
| 265 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 266 | mm->cached_hole_size = 0; |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | full_search: |
| 270 | vma = find_vma(mm, addr); |
| 271 | while (TASK_SIZE - len >= addr) { |
| 272 | BUG_ON(vma && (addr >= vma->vm_end)); |
| 273 | |
| 274 | if (touches_hugepage_low_range(mm, addr, len)) { |
| 275 | addr = ALIGN(addr+1, 1<<SID_SHIFT); |
| 276 | vma = find_vma(mm, addr); |
| 277 | continue; |
| 278 | } |
| 279 | if (touches_hugepage_high_range(addr, len)) { |
| 280 | addr = TASK_HPAGE_END; |
| 281 | vma = find_vma(mm, addr); |
| 282 | continue; |
| 283 | } |
| 284 | if (!vma || addr + len <= vma->vm_start) { |
| 285 | /* |
| 286 | * Remember the place where we stopped the search: |
| 287 | */ |
| 288 | mm->free_area_cache = addr + len; |
| 289 | return addr; |
| 290 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 291 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 292 | mm->cached_hole_size = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | addr = vma->vm_end; |
| 294 | vma = vma->vm_next; |
| 295 | } |
| 296 | |
| 297 | /* Make sure we didn't miss any holes */ |
| 298 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 299 | start_addr = addr = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 300 | mm->cached_hole_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | goto full_search; |
| 302 | } |
| 303 | return -ENOMEM; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * This mmap-allocator allocates new areas top-down from below the |
| 308 | * stack's low limit (the base): |
| 309 | * |
| 310 | * Because we have an exclusive hugepage region which lies within the |
| 311 | * normal user address space, we have to take special measures to make |
| 312 | * non-huge mmap()s evade the hugepage reserved regions. |
| 313 | */ |
| 314 | unsigned long |
| 315 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 316 | const unsigned long len, const unsigned long pgoff, |
| 317 | const unsigned long flags) |
| 318 | { |
| 319 | struct vm_area_struct *vma, *prev_vma; |
| 320 | struct mm_struct *mm = current->mm; |
| 321 | unsigned long base = mm->mmap_base, addr = addr0; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 322 | unsigned long largest_hole = mm->cached_hole_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | int first_time = 1; |
| 324 | |
| 325 | /* requested length too big for entire address space */ |
| 326 | if (len > TASK_SIZE) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | /* dont allow allocations above current base */ |
| 330 | if (mm->free_area_cache > base) |
| 331 | mm->free_area_cache = base; |
| 332 | |
| 333 | /* requesting a specific address */ |
| 334 | if (addr) { |
| 335 | addr = PAGE_ALIGN(addr); |
| 336 | vma = find_vma(mm, addr); |
| 337 | if (TASK_SIZE - len >= addr && |
| 338 | (!vma || addr + len <= vma->vm_start) |
| 339 | && !is_hugepage_only_range(mm, addr,len)) |
| 340 | return addr; |
| 341 | } |
| 342 | |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 343 | if (len <= largest_hole) { |
| 344 | largest_hole = 0; |
| 345 | mm->free_area_cache = base; |
| 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | try_again: |
| 348 | /* make sure it can fit in the remaining address space */ |
| 349 | if (mm->free_area_cache < len) |
| 350 | goto fail; |
| 351 | |
| 352 | /* either no address requested or cant fit in requested address hole */ |
| 353 | addr = (mm->free_area_cache - len) & PAGE_MASK; |
| 354 | do { |
| 355 | hugepage_recheck: |
| 356 | if (touches_hugepage_low_range(mm, addr, len)) { |
| 357 | addr = (addr & ((~0) << SID_SHIFT)) - len; |
| 358 | goto hugepage_recheck; |
| 359 | } else if (touches_hugepage_high_range(addr, len)) { |
| 360 | addr = TASK_HPAGE_BASE - len; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Lookup failure means no vma is above this address, |
| 365 | * i.e. return with success: |
| 366 | */ |
| 367 | if (!(vma = find_vma_prev(mm, addr, &prev_vma))) |
| 368 | return addr; |
| 369 | |
| 370 | /* |
| 371 | * new region fits between prev_vma->vm_end and |
| 372 | * vma->vm_start, use it: |
| 373 | */ |
| 374 | if (addr+len <= vma->vm_start && |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 375 | (!prev_vma || (addr >= prev_vma->vm_end))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | /* remember the address as a hint for next time */ |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 377 | mm->cached_hole_size = largest_hole; |
| 378 | return (mm->free_area_cache = addr); |
| 379 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* pull free_area_cache down to the first hole */ |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 381 | if (mm->free_area_cache == vma->vm_end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | mm->free_area_cache = vma->vm_start; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 383 | mm->cached_hole_size = largest_hole; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* remember the largest hole we saw so far */ |
| 388 | if (addr + largest_hole < vma->vm_start) |
| 389 | largest_hole = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | /* try just below the current vma->vm_start */ |
| 392 | addr = vma->vm_start-len; |
| 393 | } while (len <= vma->vm_start); |
| 394 | |
| 395 | fail: |
| 396 | /* |
| 397 | * if hint left us with no space for the requested |
| 398 | * mapping then try again: |
| 399 | */ |
| 400 | if (first_time) { |
| 401 | mm->free_area_cache = base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 402 | largest_hole = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | first_time = 0; |
| 404 | goto try_again; |
| 405 | } |
| 406 | /* |
| 407 | * A failed mmap() very likely causes application failure, |
| 408 | * so fall back to the bottom-up function here. This scenario |
| 409 | * can happen with large stack limits and large mmap() |
| 410 | * allocations. |
| 411 | */ |
| 412 | mm->free_area_cache = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 413 | mm->cached_hole_size = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags); |
| 415 | /* |
| 416 | * Restore the topdown base: |
| 417 | */ |
| 418 | mm->free_area_cache = base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 419 | mm->cached_hole_size = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | return addr; |
| 422 | } |
| 423 | |
| 424 | static unsigned long htlb_get_low_area(unsigned long len, u16 segmask) |
| 425 | { |
| 426 | unsigned long addr = 0; |
| 427 | struct vm_area_struct *vma; |
| 428 | |
| 429 | vma = find_vma(current->mm, addr); |
| 430 | while (addr + len <= 0x100000000UL) { |
| 431 | BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */ |
| 432 | |
| 433 | if (! __within_hugepage_low_range(addr, len, segmask)) { |
| 434 | addr = ALIGN(addr+1, 1<<SID_SHIFT); |
| 435 | vma = find_vma(current->mm, addr); |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | if (!vma || (addr + len) <= vma->vm_start) |
| 440 | return addr; |
| 441 | addr = ALIGN(vma->vm_end, HPAGE_SIZE); |
| 442 | /* Depending on segmask this might not be a confirmed |
| 443 | * hugepage region, so the ALIGN could have skipped |
| 444 | * some VMAs */ |
| 445 | vma = find_vma(current->mm, addr); |
| 446 | } |
| 447 | |
| 448 | return -ENOMEM; |
| 449 | } |
| 450 | |
| 451 | static unsigned long htlb_get_high_area(unsigned long len) |
| 452 | { |
| 453 | unsigned long addr = TASK_HPAGE_BASE; |
| 454 | struct vm_area_struct *vma; |
| 455 | |
| 456 | vma = find_vma(current->mm, addr); |
| 457 | for (vma = find_vma(current->mm, addr); |
| 458 | addr + len <= TASK_HPAGE_END; |
| 459 | vma = vma->vm_next) { |
| 460 | BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */ |
| 461 | BUG_ON(! within_hugepage_high_range(addr, len)); |
| 462 | |
| 463 | if (!vma || (addr + len) <= vma->vm_start) |
| 464 | return addr; |
| 465 | addr = ALIGN(vma->vm_end, HPAGE_SIZE); |
| 466 | /* Because we're in a hugepage region, this alignment |
| 467 | * should not skip us over any VMAs */ |
| 468 | } |
| 469 | |
| 470 | return -ENOMEM; |
| 471 | } |
| 472 | |
| 473 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 474 | unsigned long len, unsigned long pgoff, |
| 475 | unsigned long flags) |
| 476 | { |
| 477 | if (len & ~HPAGE_MASK) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | if (!cpu_has_feature(CPU_FTR_16M_PAGE)) |
| 481 | return -EINVAL; |
| 482 | |
| 483 | if (test_thread_flag(TIF_32BIT)) { |
| 484 | int lastshift = 0; |
| 485 | u16 segmask, cursegs = current->mm->context.htlb_segs; |
| 486 | |
| 487 | /* First see if we can do the mapping in the existing |
| 488 | * low hpage segments */ |
| 489 | addr = htlb_get_low_area(len, cursegs); |
| 490 | if (addr != -ENOMEM) |
| 491 | return addr; |
| 492 | |
| 493 | for (segmask = LOW_ESID_MASK(0x100000000UL-len, len); |
| 494 | ! lastshift; segmask >>=1) { |
| 495 | if (segmask & 1) |
| 496 | lastshift = 1; |
| 497 | |
| 498 | addr = htlb_get_low_area(len, cursegs | segmask); |
| 499 | if ((addr != -ENOMEM) |
| 500 | && open_low_hpage_segs(current->mm, segmask) == 0) |
| 501 | return addr; |
| 502 | } |
| 503 | printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open" |
| 504 | " enough segments\n"); |
| 505 | return -ENOMEM; |
| 506 | } else { |
| 507 | return htlb_get_high_area(len); |
| 508 | } |
| 509 | } |
| 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | int hash_huge_page(struct mm_struct *mm, unsigned long access, |
| 512 | unsigned long ea, unsigned long vsid, int local) |
| 513 | { |
| 514 | pte_t *ptep; |
| 515 | unsigned long va, vpn; |
| 516 | pte_t old_pte, new_pte; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 517 | unsigned long rflags, prpn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | long slot; |
| 519 | int err = 1; |
| 520 | |
| 521 | spin_lock(&mm->page_table_lock); |
| 522 | |
| 523 | ptep = huge_pte_offset(mm, ea); |
| 524 | |
| 525 | /* Search the Linux page table for a match with va */ |
| 526 | va = (vsid << 28) | (ea & 0x0fffffff); |
| 527 | vpn = va >> HPAGE_SHIFT; |
| 528 | |
| 529 | /* |
| 530 | * If no pte found or not present, send the problem up to |
| 531 | * do_page_fault |
| 532 | */ |
| 533 | if (unlikely(!ptep || pte_none(*ptep))) |
| 534 | goto out; |
| 535 | |
| 536 | /* BUG_ON(pte_bad(*ptep)); */ |
| 537 | |
| 538 | /* |
| 539 | * Check the user's access rights to the page. If access should be |
| 540 | * prevented then send the problem up to do_page_fault. |
| 541 | */ |
| 542 | if (unlikely(access & ~pte_val(*ptep))) |
| 543 | goto out; |
| 544 | /* |
| 545 | * At this point, we have a pte (old_pte) which can be used to build |
| 546 | * or update an HPTE. There are 2 cases: |
| 547 | * |
| 548 | * 1. There is a valid (present) pte with no associated HPTE (this is |
| 549 | * the most common case) |
| 550 | * 2. There is a valid (present) pte with an associated HPTE. The |
| 551 | * current values of the pp bits in the HPTE prevent access |
| 552 | * because we are doing software DIRTY bit management and the |
| 553 | * page is currently not DIRTY. |
| 554 | */ |
| 555 | |
| 556 | |
| 557 | old_pte = *ptep; |
| 558 | new_pte = old_pte; |
| 559 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 560 | rflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 562 | rflags |= ((pte_val(new_pte) & _PAGE_EXEC) ? 0 : HW_NO_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | /* Check if pte already has an hpte (case 2) */ |
| 565 | if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) { |
| 566 | /* There MIGHT be an HPTE for this pte */ |
| 567 | unsigned long hash, slot; |
| 568 | |
| 569 | hash = hpt_hash(vpn, 1); |
| 570 | if (pte_val(old_pte) & _PAGE_SECONDARY) |
| 571 | hash = ~hash; |
| 572 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 573 | slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12; |
| 574 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 575 | if (ppc_md.hpte_updatepp(slot, rflags, va, 1, local) == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | pte_val(old_pte) &= ~_PAGE_HPTEFLAGS; |
| 577 | } |
| 578 | |
| 579 | if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) { |
| 580 | unsigned long hash = hpt_hash(vpn, 1); |
| 581 | unsigned long hpte_group; |
| 582 | |
| 583 | prpn = pte_pfn(old_pte); |
| 584 | |
| 585 | repeat: |
| 586 | hpte_group = ((hash & htab_hash_mask) * |
| 587 | HPTES_PER_GROUP) & ~0x7UL; |
| 588 | |
| 589 | /* Update the linux pte with the HPTE slot */ |
| 590 | pte_val(new_pte) &= ~_PAGE_HPTEFLAGS; |
| 591 | pte_val(new_pte) |= _PAGE_HASHPTE; |
| 592 | |
| 593 | /* Add in WIMG bits */ |
| 594 | /* XXX We should store these in the pte */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 595 | rflags |= _PAGE_COHERENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 597 | slot = ppc_md.hpte_insert(hpte_group, va, prpn, |
| 598 | HPTE_V_LARGE, rflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | /* Primary is full, try the secondary */ |
| 601 | if (unlikely(slot == -1)) { |
| 602 | pte_val(new_pte) |= _PAGE_SECONDARY; |
| 603 | hpte_group = ((~hash & htab_hash_mask) * |
| 604 | HPTES_PER_GROUP) & ~0x7UL; |
| 605 | slot = ppc_md.hpte_insert(hpte_group, va, prpn, |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 606 | HPTE_V_LARGE, rflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (slot == -1) { |
| 608 | if (mftb() & 0x1) |
| 609 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 610 | |
| 611 | ppc_md.hpte_remove(hpte_group); |
| 612 | goto repeat; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (unlikely(slot == -2)) |
| 617 | panic("hash_huge_page: pte_insert failed\n"); |
| 618 | |
| 619 | pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX; |
| 620 | |
| 621 | /* |
| 622 | * No need to use ldarx/stdcx here because all who |
| 623 | * might be updating the pte will hold the |
| 624 | * page_table_lock |
| 625 | */ |
| 626 | *ptep = new_pte; |
| 627 | } |
| 628 | |
| 629 | err = 0; |
| 630 | |
| 631 | out: |
| 632 | spin_unlock(&mm->page_table_lock); |
| 633 | |
| 634 | return err; |
| 635 | } |