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 | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 30 | #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT) |
| 31 | #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT) |
| 32 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 33 | #ifdef CONFIG_PPC_64K_PAGES |
| 34 | #define HUGEPTE_INDEX_SIZE (PMD_SHIFT-HPAGE_SHIFT) |
| 35 | #else |
| 36 | #define HUGEPTE_INDEX_SIZE (PUD_SHIFT-HPAGE_SHIFT) |
| 37 | #endif |
| 38 | #define PTRS_PER_HUGEPTE (1 << HUGEPTE_INDEX_SIZE) |
| 39 | #define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << HUGEPTE_INDEX_SIZE) |
| 40 | |
| 41 | #define HUGEPD_SHIFT (HPAGE_SHIFT + HUGEPTE_INDEX_SIZE) |
| 42 | #define HUGEPD_SIZE (1UL << HUGEPD_SHIFT) |
| 43 | #define HUGEPD_MASK (~(HUGEPD_SIZE-1)) |
| 44 | |
| 45 | #define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM]) |
| 46 | |
| 47 | /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad() |
| 48 | * will choke on pointers to hugepte tables, which is handy for |
| 49 | * catching screwups early. */ |
| 50 | #define HUGEPD_OK 0x1 |
| 51 | |
| 52 | typedef struct { unsigned long pd; } hugepd_t; |
| 53 | |
| 54 | #define hugepd_none(hpd) ((hpd).pd == 0) |
| 55 | |
| 56 | static inline pte_t *hugepd_page(hugepd_t hpd) |
| 57 | { |
| 58 | BUG_ON(!(hpd.pd & HUGEPD_OK)); |
| 59 | return (pte_t *)(hpd.pd & ~HUGEPD_OK); |
| 60 | } |
| 61 | |
| 62 | static inline pte_t *hugepte_offset(hugepd_t *hpdp, unsigned long addr) |
| 63 | { |
| 64 | unsigned long idx = ((addr >> HPAGE_SHIFT) & (PTRS_PER_HUGEPTE-1)); |
| 65 | pte_t *dir = hugepd_page(*hpdp); |
| 66 | |
| 67 | return dir + idx; |
| 68 | } |
| 69 | |
| 70 | static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp, |
| 71 | unsigned long address) |
| 72 | { |
| 73 | pte_t *new = kmem_cache_alloc(huge_pgtable_cache, |
| 74 | GFP_KERNEL|__GFP_REPEAT); |
| 75 | |
| 76 | if (! new) |
| 77 | return -ENOMEM; |
| 78 | |
| 79 | spin_lock(&mm->page_table_lock); |
| 80 | if (!hugepd_none(*hpdp)) |
| 81 | kmem_cache_free(huge_pgtable_cache, new); |
| 82 | else |
| 83 | hpdp->pd = (unsigned long)new | HUGEPD_OK; |
| 84 | spin_unlock(&mm->page_table_lock); |
| 85 | return 0; |
| 86 | } |
| 87 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 88 | /* Modelled after find_linux_pte() */ |
| 89 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 91 | pgd_t *pg; |
| 92 | pud_t *pu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | BUG_ON(! in_hugepage_area(mm->context, addr)); |
| 95 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 96 | addr &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 98 | pg = pgd_offset(mm, addr); |
| 99 | if (!pgd_none(*pg)) { |
| 100 | pu = pud_offset(pg, addr); |
| 101 | if (!pud_none(*pu)) { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 102 | #ifdef CONFIG_PPC_64K_PAGES |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 103 | pmd_t *pm; |
| 104 | pm = pmd_offset(pu, addr); |
| 105 | if (!pmd_none(*pm)) |
| 106 | return hugepte_offset((hugepd_t *)pm, addr); |
| 107 | #else |
| 108 | return hugepte_offset((hugepd_t *)pu, addr); |
| 109 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 113 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 116 | pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 118 | pgd_t *pg; |
| 119 | pud_t *pu; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 120 | hugepd_t *hpdp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | BUG_ON(! in_hugepage_area(mm->context, addr)); |
| 123 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 124 | addr &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 126 | pg = pgd_offset(mm, addr); |
| 127 | pu = pud_alloc(mm, pg, addr); |
| 128 | |
| 129 | if (pu) { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 130 | #ifdef CONFIG_PPC_64K_PAGES |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 131 | pmd_t *pm; |
| 132 | pm = pmd_alloc(mm, pu, addr); |
| 133 | if (pm) |
| 134 | hpdp = (hugepd_t *)pm; |
| 135 | #else |
| 136 | hpdp = (hugepd_t *)pu; |
| 137 | #endif |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 138 | } |
| 139 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 140 | if (! hpdp) |
| 141 | return NULL; |
| 142 | |
| 143 | if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr)) |
| 144 | return NULL; |
| 145 | |
| 146 | return hugepte_offset(hpdp, addr); |
| 147 | } |
| 148 | |
| 149 | static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp) |
| 150 | { |
| 151 | pte_t *hugepte = hugepd_page(*hpdp); |
| 152 | |
| 153 | hpdp->pd = 0; |
| 154 | tlb->need_flush = 1; |
| 155 | pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM, |
Adam Litke | c9169f8 | 2006-08-18 11:22:21 -0700 | [diff] [blame] | 156 | PGF_CACHENUM_MASK)); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | #ifdef CONFIG_PPC_64K_PAGES |
| 160 | static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud, |
| 161 | unsigned long addr, unsigned long end, |
| 162 | unsigned long floor, unsigned long ceiling) |
| 163 | { |
| 164 | pmd_t *pmd; |
| 165 | unsigned long next; |
| 166 | unsigned long start; |
| 167 | |
| 168 | start = addr; |
| 169 | pmd = pmd_offset(pud, addr); |
| 170 | do { |
| 171 | next = pmd_addr_end(addr, end); |
| 172 | if (pmd_none(*pmd)) |
| 173 | continue; |
| 174 | free_hugepte_range(tlb, (hugepd_t *)pmd); |
| 175 | } while (pmd++, addr = next, addr != end); |
| 176 | |
| 177 | start &= PUD_MASK; |
| 178 | if (start < floor) |
| 179 | return; |
| 180 | if (ceiling) { |
| 181 | ceiling &= PUD_MASK; |
| 182 | if (!ceiling) |
| 183 | return; |
| 184 | } |
| 185 | if (end - 1 > ceiling - 1) |
| 186 | return; |
| 187 | |
| 188 | pmd = pmd_offset(pud, start); |
| 189 | pud_clear(pud); |
| 190 | pmd_free_tlb(tlb, pmd); |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, |
| 195 | unsigned long addr, unsigned long end, |
| 196 | unsigned long floor, unsigned long ceiling) |
| 197 | { |
| 198 | pud_t *pud; |
| 199 | unsigned long next; |
| 200 | unsigned long start; |
| 201 | |
| 202 | start = addr; |
| 203 | pud = pud_offset(pgd, addr); |
| 204 | do { |
| 205 | next = pud_addr_end(addr, end); |
| 206 | #ifdef CONFIG_PPC_64K_PAGES |
| 207 | if (pud_none_or_clear_bad(pud)) |
| 208 | continue; |
| 209 | hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling); |
| 210 | #else |
| 211 | if (pud_none(*pud)) |
| 212 | continue; |
| 213 | free_hugepte_range(tlb, (hugepd_t *)pud); |
| 214 | #endif |
| 215 | } while (pud++, addr = next, addr != end); |
| 216 | |
| 217 | start &= PGDIR_MASK; |
| 218 | if (start < floor) |
| 219 | return; |
| 220 | if (ceiling) { |
| 221 | ceiling &= PGDIR_MASK; |
| 222 | if (!ceiling) |
| 223 | return; |
| 224 | } |
| 225 | if (end - 1 > ceiling - 1) |
| 226 | return; |
| 227 | |
| 228 | pud = pud_offset(pgd, start); |
| 229 | pgd_clear(pgd); |
| 230 | pud_free_tlb(tlb, pud); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * This function frees user-level page tables of a process. |
| 235 | * |
| 236 | * Must be called with pagetable lock held. |
| 237 | */ |
| 238 | void hugetlb_free_pgd_range(struct mmu_gather **tlb, |
| 239 | unsigned long addr, unsigned long end, |
| 240 | unsigned long floor, unsigned long ceiling) |
| 241 | { |
| 242 | pgd_t *pgd; |
| 243 | unsigned long next; |
| 244 | unsigned long start; |
| 245 | |
| 246 | /* |
| 247 | * Comments below take from the normal free_pgd_range(). They |
| 248 | * apply here too. The tests against HUGEPD_MASK below are |
| 249 | * essential, because we *don't* test for this at the bottom |
| 250 | * level. Without them we'll attempt to free a hugepte table |
| 251 | * when we unmap just part of it, even if there are other |
| 252 | * active mappings using it. |
| 253 | * |
| 254 | * The next few lines have given us lots of grief... |
| 255 | * |
| 256 | * Why are we testing HUGEPD* at this top level? Because |
| 257 | * often there will be no work to do at all, and we'd prefer |
| 258 | * not to go all the way down to the bottom just to discover |
| 259 | * that. |
| 260 | * |
| 261 | * Why all these "- 1"s? Because 0 represents both the bottom |
| 262 | * of the address space and the top of it (using -1 for the |
| 263 | * top wouldn't help much: the masks would do the wrong thing). |
| 264 | * The rule is that addr 0 and floor 0 refer to the bottom of |
| 265 | * the address space, but end 0 and ceiling 0 refer to the top |
| 266 | * Comparisons need to use "end - 1" and "ceiling - 1" (though |
| 267 | * that end 0 case should be mythical). |
| 268 | * |
| 269 | * Wherever addr is brought up or ceiling brought down, we |
| 270 | * must be careful to reject "the opposite 0" before it |
| 271 | * confuses the subsequent tests. But what about where end is |
| 272 | * brought down by HUGEPD_SIZE below? no, end can't go down to |
| 273 | * 0 there. |
| 274 | * |
| 275 | * Whereas we round start (addr) and ceiling down, by different |
| 276 | * masks at different levels, in order to test whether a table |
| 277 | * now has no other vmas using it, so can be freed, we don't |
| 278 | * bother to round floor or end up - the tests don't need that. |
| 279 | */ |
| 280 | |
| 281 | addr &= HUGEPD_MASK; |
| 282 | if (addr < floor) { |
| 283 | addr += HUGEPD_SIZE; |
| 284 | if (!addr) |
| 285 | return; |
| 286 | } |
| 287 | if (ceiling) { |
| 288 | ceiling &= HUGEPD_MASK; |
| 289 | if (!ceiling) |
| 290 | return; |
| 291 | } |
| 292 | if (end - 1 > ceiling - 1) |
| 293 | end -= HUGEPD_SIZE; |
| 294 | if (addr > end - 1) |
| 295 | return; |
| 296 | |
| 297 | start = addr; |
| 298 | pgd = pgd_offset((*tlb)->mm, addr); |
| 299 | do { |
| 300 | BUG_ON(! in_hugepage_area((*tlb)->mm->context, addr)); |
| 301 | next = pgd_addr_end(addr, end); |
| 302 | if (pgd_none_or_clear_bad(pgd)) |
| 303 | continue; |
| 304 | hugetlb_free_pud_range(*tlb, pgd, addr, next, floor, ceiling); |
| 305 | } while (pgd++, addr = next, addr != end); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 306 | } |
| 307 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 308 | void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, |
| 309 | pte_t *ptep, pte_t pte) |
| 310 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 311 | if (pte_present(*ptep)) { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 312 | /* We open-code pte_clear because we need to pass the right |
| 313 | * argument to hpte_update (huge / !huge) |
| 314 | */ |
| 315 | unsigned long old = pte_update(ptep, ~0UL); |
| 316 | if (old & _PAGE_HASHPTE) |
| 317 | hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 318 | flush_tlb_pending(); |
| 319 | } |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 320 | *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, |
| 324 | pte_t *ptep) |
| 325 | { |
| 326 | unsigned long old = pte_update(ptep, ~0UL); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 327 | |
| 328 | if (old & _PAGE_HASHPTE) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 329 | hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1); |
| 330 | *ptep = __pte(0); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 331 | |
| 332 | return __pte(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 335 | struct slb_flush_info { |
| 336 | struct mm_struct *mm; |
| 337 | u16 newareas; |
| 338 | }; |
| 339 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 340 | static void flush_low_segments(void *parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 342 | struct slb_flush_info *fi = parm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | unsigned long i; |
| 344 | |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 345 | BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_LOW_AREAS); |
| 346 | |
| 347 | if (current->active_mm != fi->mm) |
| 348 | return; |
| 349 | |
| 350 | /* Only need to do anything if this CPU is working in the same |
| 351 | * mm as the one which has changed */ |
| 352 | |
| 353 | /* update the paca copy of the context struct */ |
| 354 | get_paca()->context = current->active_mm->context; |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | asm volatile("isync" : : : "memory"); |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 357 | for (i = 0; i < NUM_LOW_AREAS; i++) { |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 358 | if (! (fi->newareas & (1U << i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | continue; |
David Gibson | 14b3466 | 2005-09-06 14:59:47 +1000 | [diff] [blame] | 360 | asm volatile("slbie %0" |
| 361 | : : "r" ((i << SID_SHIFT) | SLBIE_C)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | asm volatile("isync" : : : "memory"); |
| 364 | } |
| 365 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 366 | static void flush_high_segments(void *parm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 368 | struct slb_flush_info *fi = parm; |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 369 | unsigned long i, j; |
| 370 | |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 371 | |
| 372 | BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_HIGH_AREAS); |
| 373 | |
| 374 | if (current->active_mm != fi->mm) |
| 375 | return; |
| 376 | |
| 377 | /* Only need to do anything if this CPU is working in the same |
| 378 | * mm as the one which has changed */ |
| 379 | |
| 380 | /* update the paca copy of the context struct */ |
| 381 | get_paca()->context = current->active_mm->context; |
| 382 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 383 | asm volatile("isync" : : : "memory"); |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 384 | for (i = 0; i < NUM_HIGH_AREAS; i++) { |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 385 | if (! (fi->newareas & (1U << i))) |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 386 | continue; |
| 387 | for (j = 0; j < (1UL << (HTLB_AREA_SHIFT-SID_SHIFT)); j++) |
| 388 | asm volatile("slbie %0" |
David Gibson | 14b3466 | 2005-09-06 14:59:47 +1000 | [diff] [blame] | 389 | :: "r" (((i << HTLB_AREA_SHIFT) |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 390 | + (j << SID_SHIFT)) | SLBIE_C)); |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 391 | } |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 392 | asm volatile("isync" : : : "memory"); |
| 393 | } |
| 394 | |
| 395 | static int prepare_low_area_for_htlb(struct mm_struct *mm, unsigned long area) |
| 396 | { |
| 397 | unsigned long start = area << SID_SHIFT; |
| 398 | unsigned long end = (area+1) << SID_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 401 | BUG_ON(area >= NUM_LOW_AREAS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
| 403 | /* Check no VMAs are in the region */ |
| 404 | vma = find_vma(mm, start); |
| 405 | if (vma && (vma->vm_start < end)) |
| 406 | return -EBUSY; |
| 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 411 | static int prepare_high_area_for_htlb(struct mm_struct *mm, unsigned long area) |
| 412 | { |
| 413 | unsigned long start = area << HTLB_AREA_SHIFT; |
| 414 | unsigned long end = (area+1) << HTLB_AREA_SHIFT; |
| 415 | struct vm_area_struct *vma; |
| 416 | |
| 417 | BUG_ON(area >= NUM_HIGH_AREAS); |
| 418 | |
David Gibson | 7d24f0b | 2005-11-07 00:57:52 -0800 | [diff] [blame] | 419 | /* Hack, so that each addresses is controlled by exactly one |
| 420 | * of the high or low area bitmaps, the first high area starts |
| 421 | * at 4GB, not 0 */ |
| 422 | if (start == 0) |
| 423 | start = 0x100000000UL; |
| 424 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 425 | /* Check no VMAs are in the region */ |
| 426 | vma = find_vma(mm, start); |
| 427 | if (vma && (vma->vm_start < end)) |
| 428 | return -EBUSY; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int open_low_hpage_areas(struct mm_struct *mm, u16 newareas) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | unsigned long i; |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 436 | struct slb_flush_info fi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 438 | BUILD_BUG_ON((sizeof(newareas)*8) != NUM_LOW_AREAS); |
| 439 | BUILD_BUG_ON((sizeof(mm->context.low_htlb_areas)*8) != NUM_LOW_AREAS); |
| 440 | |
| 441 | newareas &= ~(mm->context.low_htlb_areas); |
| 442 | if (! newareas) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return 0; /* The segments we want are already open */ |
| 444 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 445 | for (i = 0; i < NUM_LOW_AREAS; i++) |
| 446 | if ((1 << i) & newareas) |
| 447 | if (prepare_low_area_for_htlb(mm, i) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return -EBUSY; |
| 449 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 450 | mm->context.low_htlb_areas |= newareas; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* the context change must make it to memory before the flush, |
| 453 | * so that further SLB misses do the right thing. */ |
| 454 | mb(); |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 455 | |
| 456 | fi.mm = mm; |
| 457 | fi.newareas = newareas; |
| 458 | on_each_cpu(flush_low_segments, &fi, 0, 1); |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static int open_high_hpage_areas(struct mm_struct *mm, u16 newareas) |
| 464 | { |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 465 | struct slb_flush_info fi; |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 466 | unsigned long i; |
| 467 | |
| 468 | BUILD_BUG_ON((sizeof(newareas)*8) != NUM_HIGH_AREAS); |
| 469 | BUILD_BUG_ON((sizeof(mm->context.high_htlb_areas)*8) |
| 470 | != NUM_HIGH_AREAS); |
| 471 | |
| 472 | newareas &= ~(mm->context.high_htlb_areas); |
| 473 | if (! newareas) |
| 474 | return 0; /* The areas we want are already open */ |
| 475 | |
| 476 | for (i = 0; i < NUM_HIGH_AREAS; i++) |
| 477 | if ((1 << i) & newareas) |
| 478 | if (prepare_high_area_for_htlb(mm, i) != 0) |
| 479 | return -EBUSY; |
| 480 | |
| 481 | mm->context.high_htlb_areas |= newareas; |
| 482 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 483 | /* the context change must make it to memory before the flush, |
| 484 | * so that further SLB misses do the right thing. */ |
| 485 | mb(); |
David Gibson | 23ed6cb | 2005-12-09 16:45:17 +1100 | [diff] [blame] | 486 | |
| 487 | fi.mm = mm; |
| 488 | fi.newareas = newareas; |
| 489 | on_each_cpu(flush_high_segments, &fi, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Hugh Dickins | 68589bc | 2006-11-14 02:03:32 -0800 | [diff] [blame] | 494 | int prepare_hugepage_range(unsigned long addr, unsigned long len, pgoff_t pgoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
David Gibson | 5e391dc | 2005-11-23 13:37:45 -0800 | [diff] [blame] | 496 | int err = 0; |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 497 | |
Hugh Dickins | 68589bc | 2006-11-14 02:03:32 -0800 | [diff] [blame] | 498 | if (pgoff & (~HPAGE_MASK >> PAGE_SHIFT)) |
| 499 | return -EINVAL; |
| 500 | if (len & ~HPAGE_MASK) |
| 501 | return -EINVAL; |
| 502 | if (addr & ~HPAGE_MASK) |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 503 | return -EINVAL; |
| 504 | |
David Gibson | 5e391dc | 2005-11-23 13:37:45 -0800 | [diff] [blame] | 505 | if (addr < 0x100000000UL) |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 506 | err = open_low_hpage_areas(current->mm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | LOW_ESID_MASK(addr, len)); |
David Gibson | 9a94c57 | 2005-11-24 13:34:56 +1100 | [diff] [blame] | 508 | if ((addr + len) > 0x100000000UL) |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 509 | err = open_high_hpage_areas(current->mm, |
| 510 | HTLB_AREA_MASK(addr, len)); |
| 511 | if (err) { |
| 512 | printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)" |
| 513 | " failed (lowmask: 0x%04hx, highmask: 0x%04hx)\n", |
| 514 | addr, len, |
| 515 | LOW_ESID_MASK(addr, len), HTLB_AREA_MASK(addr, len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | return err; |
| 517 | } |
| 518 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 519 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | struct page * |
| 523 | follow_huge_addr(struct mm_struct *mm, unsigned long address, int write) |
| 524 | { |
| 525 | pte_t *ptep; |
| 526 | struct page *page; |
| 527 | |
| 528 | if (! in_hugepage_area(mm->context, address)) |
| 529 | return ERR_PTR(-EINVAL); |
| 530 | |
| 531 | ptep = huge_pte_offset(mm, address); |
| 532 | page = pte_page(*ptep); |
| 533 | if (page) |
| 534 | page += (address % HPAGE_SIZE) / PAGE_SIZE; |
| 535 | |
| 536 | return page; |
| 537 | } |
| 538 | |
| 539 | int pmd_huge(pmd_t pmd) |
| 540 | { |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | struct page * |
| 545 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 546 | pmd_t *pmd, int write) |
| 547 | { |
| 548 | BUG(); |
| 549 | return NULL; |
| 550 | } |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* Because we have an exclusive hugepage region which lies within the |
| 553 | * normal user address space, we have to take special measures to make |
| 554 | * non-huge mmap()s evade the hugepage reserved regions. */ |
| 555 | unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 556 | unsigned long len, unsigned long pgoff, |
| 557 | unsigned long flags) |
| 558 | { |
| 559 | struct mm_struct *mm = current->mm; |
| 560 | struct vm_area_struct *vma; |
| 561 | unsigned long start_addr; |
| 562 | |
| 563 | if (len > TASK_SIZE) |
| 564 | return -ENOMEM; |
| 565 | |
| 566 | if (addr) { |
| 567 | addr = PAGE_ALIGN(addr); |
| 568 | vma = find_vma(mm, addr); |
| 569 | if (((TASK_SIZE - len) >= addr) |
| 570 | && (!vma || (addr+len) <= vma->vm_start) |
| 571 | && !is_hugepage_only_range(mm, addr,len)) |
| 572 | return addr; |
| 573 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 574 | if (len > mm->cached_hole_size) { |
| 575 | start_addr = addr = mm->free_area_cache; |
| 576 | } else { |
| 577 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 578 | mm->cached_hole_size = 0; |
| 579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | full_search: |
| 582 | vma = find_vma(mm, addr); |
| 583 | while (TASK_SIZE - len >= addr) { |
| 584 | BUG_ON(vma && (addr >= vma->vm_end)); |
| 585 | |
| 586 | if (touches_hugepage_low_range(mm, addr, len)) { |
| 587 | addr = ALIGN(addr+1, 1<<SID_SHIFT); |
| 588 | vma = find_vma(mm, addr); |
| 589 | continue; |
| 590 | } |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 591 | if (touches_hugepage_high_range(mm, addr, len)) { |
| 592 | addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | vma = find_vma(mm, addr); |
| 594 | continue; |
| 595 | } |
| 596 | if (!vma || addr + len <= vma->vm_start) { |
| 597 | /* |
| 598 | * Remember the place where we stopped the search: |
| 599 | */ |
| 600 | mm->free_area_cache = addr + len; |
| 601 | return addr; |
| 602 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 603 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 604 | mm->cached_hole_size = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | addr = vma->vm_end; |
| 606 | vma = vma->vm_next; |
| 607 | } |
| 608 | |
| 609 | /* Make sure we didn't miss any holes */ |
| 610 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 611 | start_addr = addr = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 612 | mm->cached_hole_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | goto full_search; |
| 614 | } |
| 615 | return -ENOMEM; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * This mmap-allocator allocates new areas top-down from below the |
| 620 | * stack's low limit (the base): |
| 621 | * |
| 622 | * Because we have an exclusive hugepage region which lies within the |
| 623 | * normal user address space, we have to take special measures to make |
| 624 | * non-huge mmap()s evade the hugepage reserved regions. |
| 625 | */ |
| 626 | unsigned long |
| 627 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 628 | const unsigned long len, const unsigned long pgoff, |
| 629 | const unsigned long flags) |
| 630 | { |
| 631 | struct vm_area_struct *vma, *prev_vma; |
| 632 | struct mm_struct *mm = current->mm; |
| 633 | unsigned long base = mm->mmap_base, addr = addr0; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 634 | unsigned long largest_hole = mm->cached_hole_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | int first_time = 1; |
| 636 | |
| 637 | /* requested length too big for entire address space */ |
| 638 | if (len > TASK_SIZE) |
| 639 | return -ENOMEM; |
| 640 | |
| 641 | /* dont allow allocations above current base */ |
| 642 | if (mm->free_area_cache > base) |
| 643 | mm->free_area_cache = base; |
| 644 | |
| 645 | /* requesting a specific address */ |
| 646 | if (addr) { |
| 647 | addr = PAGE_ALIGN(addr); |
| 648 | vma = find_vma(mm, addr); |
| 649 | if (TASK_SIZE - len >= addr && |
| 650 | (!vma || addr + len <= vma->vm_start) |
| 651 | && !is_hugepage_only_range(mm, addr,len)) |
| 652 | return addr; |
| 653 | } |
| 654 | |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 655 | if (len <= largest_hole) { |
| 656 | largest_hole = 0; |
| 657 | mm->free_area_cache = base; |
| 658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | try_again: |
| 660 | /* make sure it can fit in the remaining address space */ |
| 661 | if (mm->free_area_cache < len) |
| 662 | goto fail; |
| 663 | |
| 664 | /* either no address requested or cant fit in requested address hole */ |
| 665 | addr = (mm->free_area_cache - len) & PAGE_MASK; |
| 666 | do { |
| 667 | hugepage_recheck: |
| 668 | if (touches_hugepage_low_range(mm, addr, len)) { |
| 669 | addr = (addr & ((~0) << SID_SHIFT)) - len; |
| 670 | goto hugepage_recheck; |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 671 | } else if (touches_hugepage_high_range(mm, addr, len)) { |
| 672 | addr = (addr & ((~0UL) << HTLB_AREA_SHIFT)) - len; |
| 673 | goto hugepage_recheck; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Lookup failure means no vma is above this address, |
| 678 | * i.e. return with success: |
| 679 | */ |
| 680 | if (!(vma = find_vma_prev(mm, addr, &prev_vma))) |
| 681 | return addr; |
| 682 | |
| 683 | /* |
| 684 | * new region fits between prev_vma->vm_end and |
| 685 | * vma->vm_start, use it: |
| 686 | */ |
| 687 | if (addr+len <= vma->vm_start && |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 688 | (!prev_vma || (addr >= prev_vma->vm_end))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | /* remember the address as a hint for next time */ |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 690 | mm->cached_hole_size = largest_hole; |
| 691 | return (mm->free_area_cache = addr); |
| 692 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | /* pull free_area_cache down to the first hole */ |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 694 | if (mm->free_area_cache == vma->vm_end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | mm->free_area_cache = vma->vm_start; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 696 | mm->cached_hole_size = largest_hole; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* remember the largest hole we saw so far */ |
| 701 | if (addr + largest_hole < vma->vm_start) |
| 702 | largest_hole = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | /* try just below the current vma->vm_start */ |
| 705 | addr = vma->vm_start-len; |
| 706 | } while (len <= vma->vm_start); |
| 707 | |
| 708 | fail: |
| 709 | /* |
| 710 | * if hint left us with no space for the requested |
| 711 | * mapping then try again: |
| 712 | */ |
| 713 | if (first_time) { |
| 714 | mm->free_area_cache = base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 715 | largest_hole = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | first_time = 0; |
| 717 | goto try_again; |
| 718 | } |
| 719 | /* |
| 720 | * A failed mmap() very likely causes application failure, |
| 721 | * so fall back to the bottom-up function here. This scenario |
| 722 | * can happen with large stack limits and large mmap() |
| 723 | * allocations. |
| 724 | */ |
| 725 | mm->free_area_cache = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 726 | mm->cached_hole_size = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags); |
| 728 | /* |
| 729 | * Restore the topdown base: |
| 730 | */ |
| 731 | mm->free_area_cache = base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 732 | mm->cached_hole_size = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
| 734 | return addr; |
| 735 | } |
| 736 | |
David Gibson | 456752f | 2005-11-24 14:16:15 +1100 | [diff] [blame] | 737 | static int htlb_check_hinted_area(unsigned long addr, unsigned long len) |
| 738 | { |
| 739 | struct vm_area_struct *vma; |
| 740 | |
| 741 | vma = find_vma(current->mm, addr); |
| 742 | if (!vma || ((addr + len) <= vma->vm_start)) |
| 743 | return 0; |
| 744 | |
| 745 | return -ENOMEM; |
| 746 | } |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | static unsigned long htlb_get_low_area(unsigned long len, u16 segmask) |
| 749 | { |
| 750 | unsigned long addr = 0; |
| 751 | struct vm_area_struct *vma; |
| 752 | |
| 753 | vma = find_vma(current->mm, addr); |
| 754 | while (addr + len <= 0x100000000UL) { |
| 755 | BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */ |
| 756 | |
| 757 | if (! __within_hugepage_low_range(addr, len, segmask)) { |
| 758 | addr = ALIGN(addr+1, 1<<SID_SHIFT); |
| 759 | vma = find_vma(current->mm, addr); |
| 760 | continue; |
| 761 | } |
| 762 | |
| 763 | if (!vma || (addr + len) <= vma->vm_start) |
| 764 | return addr; |
| 765 | addr = ALIGN(vma->vm_end, HPAGE_SIZE); |
| 766 | /* Depending on segmask this might not be a confirmed |
| 767 | * hugepage region, so the ALIGN could have skipped |
| 768 | * some VMAs */ |
| 769 | vma = find_vma(current->mm, addr); |
| 770 | } |
| 771 | |
| 772 | return -ENOMEM; |
| 773 | } |
| 774 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 775 | static unsigned long htlb_get_high_area(unsigned long len, u16 areamask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 777 | unsigned long addr = 0x100000000UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | struct vm_area_struct *vma; |
| 779 | |
| 780 | vma = find_vma(current->mm, addr); |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 781 | while (addr + len <= TASK_SIZE_USER64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */ |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 783 | |
| 784 | if (! __within_hugepage_high_range(addr, len, areamask)) { |
| 785 | addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT); |
| 786 | vma = find_vma(current->mm, addr); |
| 787 | continue; |
| 788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
| 790 | if (!vma || (addr + len) <= vma->vm_start) |
| 791 | return addr; |
| 792 | addr = ALIGN(vma->vm_end, HPAGE_SIZE); |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 793 | /* Depending on segmask this might not be a confirmed |
| 794 | * hugepage region, so the ALIGN could have skipped |
| 795 | * some VMAs */ |
| 796 | vma = find_vma(current->mm, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | return -ENOMEM; |
| 800 | } |
| 801 | |
| 802 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 803 | unsigned long len, unsigned long pgoff, |
| 804 | unsigned long flags) |
| 805 | { |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 806 | int lastshift; |
| 807 | u16 areamask, curareas; |
| 808 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 809 | if (HPAGE_SHIFT == 0) |
| 810 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | if (len & ~HPAGE_MASK) |
| 812 | return -EINVAL; |
| 813 | |
| 814 | if (!cpu_has_feature(CPU_FTR_16M_PAGE)) |
| 815 | return -EINVAL; |
| 816 | |
David Gibson | 456752f | 2005-11-24 14:16:15 +1100 | [diff] [blame] | 817 | /* Paranoia, caller should have dealt with this */ |
| 818 | BUG_ON((addr + len) < addr); |
| 819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | if (test_thread_flag(TIF_32BIT)) { |
David Gibson | 456752f | 2005-11-24 14:16:15 +1100 | [diff] [blame] | 821 | /* Paranoia, caller should have dealt with this */ |
| 822 | BUG_ON((addr + len) > 0x100000000UL); |
| 823 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 824 | curareas = current->mm->context.low_htlb_areas; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
David Gibson | 456752f | 2005-11-24 14:16:15 +1100 | [diff] [blame] | 826 | /* First see if we can use the hint address */ |
| 827 | if (addr && (htlb_check_hinted_area(addr, len) == 0)) { |
| 828 | areamask = LOW_ESID_MASK(addr, len); |
| 829 | if (open_low_hpage_areas(current->mm, areamask) == 0) |
| 830 | return addr; |
| 831 | } |
| 832 | |
| 833 | /* Next see if we can map in the existing low areas */ |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 834 | addr = htlb_get_low_area(len, curareas); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (addr != -ENOMEM) |
| 836 | return addr; |
| 837 | |
David Gibson | 456752f | 2005-11-24 14:16:15 +1100 | [diff] [blame] | 838 | /* Finally go looking for areas to open */ |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 839 | lastshift = 0; |
| 840 | for (areamask = LOW_ESID_MASK(0x100000000UL-len, len); |
| 841 | ! lastshift; areamask >>=1) { |
| 842 | if (areamask & 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | lastshift = 1; |
| 844 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 845 | addr = htlb_get_low_area(len, curareas | areamask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | if ((addr != -ENOMEM) |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 847 | && open_low_hpage_areas(current->mm, areamask) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | return addr; |
| 849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } else { |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 851 | curareas = current->mm->context.high_htlb_areas; |
| 852 | |
David Gibson | 456752f | 2005-11-24 14:16:15 +1100 | [diff] [blame] | 853 | /* First see if we can use the hint address */ |
| 854 | /* We discourage 64-bit processes from doing hugepage |
| 855 | * mappings below 4GB (must use MAP_FIXED) */ |
| 856 | if ((addr >= 0x100000000UL) |
| 857 | && (htlb_check_hinted_area(addr, len) == 0)) { |
| 858 | areamask = HTLB_AREA_MASK(addr, len); |
| 859 | if (open_high_hpage_areas(current->mm, areamask) == 0) |
| 860 | return addr; |
| 861 | } |
| 862 | |
| 863 | /* Next see if we can map in the existing high areas */ |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 864 | addr = htlb_get_high_area(len, curareas); |
| 865 | if (addr != -ENOMEM) |
| 866 | return addr; |
| 867 | |
David Gibson | 456752f | 2005-11-24 14:16:15 +1100 | [diff] [blame] | 868 | /* Finally go looking for areas to open */ |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 869 | lastshift = 0; |
| 870 | for (areamask = HTLB_AREA_MASK(TASK_SIZE_USER64-len, len); |
| 871 | ! lastshift; areamask >>=1) { |
| 872 | if (areamask & 1) |
| 873 | lastshift = 1; |
| 874 | |
| 875 | addr = htlb_get_high_area(len, curareas | areamask); |
| 876 | if ((addr != -ENOMEM) |
| 877 | && open_high_hpage_areas(current->mm, areamask) == 0) |
| 878 | return addr; |
| 879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 881 | printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open" |
| 882 | " enough areas\n"); |
| 883 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
David Gibson | cbf52af | 2005-12-09 14:20:52 +1100 | [diff] [blame] | 886 | /* |
| 887 | * Called by asm hashtable.S for doing lazy icache flush |
| 888 | */ |
| 889 | static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags, |
| 890 | pte_t pte, int trap) |
| 891 | { |
| 892 | struct page *page; |
| 893 | int i; |
| 894 | |
| 895 | if (!pfn_valid(pte_pfn(pte))) |
| 896 | return rflags; |
| 897 | |
| 898 | page = pte_page(pte); |
| 899 | |
| 900 | /* page is dirty */ |
| 901 | if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) { |
| 902 | if (trap == 0x400) { |
| 903 | for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) |
| 904 | __flush_dcache_icache(page_address(page+i)); |
| 905 | set_bit(PG_arch_1, &page->flags); |
| 906 | } else { |
| 907 | rflags |= HPTE_R_N; |
| 908 | } |
| 909 | } |
| 910 | return rflags; |
| 911 | } |
| 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | int hash_huge_page(struct mm_struct *mm, unsigned long access, |
David Gibson | cbf52af | 2005-12-09 14:20:52 +1100 | [diff] [blame] | 914 | unsigned long ea, unsigned long vsid, int local, |
| 915 | unsigned long trap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { |
| 917 | pte_t *ptep; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 918 | unsigned long old_pte, new_pte; |
| 919 | unsigned long va, rflags, pa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | long slot; |
| 921 | int err = 1; |
| 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | ptep = huge_pte_offset(mm, ea); |
| 924 | |
| 925 | /* Search the Linux page table for a match with va */ |
| 926 | va = (vsid << 28) | (ea & 0x0fffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
| 928 | /* |
| 929 | * If no pte found or not present, send the problem up to |
| 930 | * do_page_fault |
| 931 | */ |
| 932 | if (unlikely(!ptep || pte_none(*ptep))) |
| 933 | goto out; |
| 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | /* |
| 936 | * Check the user's access rights to the page. If access should be |
| 937 | * prevented then send the problem up to do_page_fault. |
| 938 | */ |
| 939 | if (unlikely(access & ~pte_val(*ptep))) |
| 940 | goto out; |
| 941 | /* |
| 942 | * At this point, we have a pte (old_pte) which can be used to build |
| 943 | * or update an HPTE. There are 2 cases: |
| 944 | * |
| 945 | * 1. There is a valid (present) pte with no associated HPTE (this is |
| 946 | * the most common case) |
| 947 | * 2. There is a valid (present) pte with an associated HPTE. The |
| 948 | * current values of the pp bits in the HPTE prevent access |
| 949 | * because we are doing software DIRTY bit management and the |
| 950 | * page is currently not DIRTY. |
| 951 | */ |
| 952 | |
| 953 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 954 | do { |
| 955 | old_pte = pte_val(*ptep); |
| 956 | if (old_pte & _PAGE_BUSY) |
| 957 | goto out; |
| 958 | new_pte = old_pte | _PAGE_BUSY | |
| 959 | _PAGE_ACCESSED | _PAGE_HASHPTE; |
| 960 | } while(old_pte != __cmpxchg_u64((unsigned long *)ptep, |
| 961 | old_pte, new_pte)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 963 | rflags = 0x2 | (!(new_pte & _PAGE_RW)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 965 | rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N); |
David Gibson | cbf52af | 2005-12-09 14:20:52 +1100 | [diff] [blame] | 966 | if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) |
| 967 | /* No CPU has hugepages but lacks no execute, so we |
| 968 | * don't need to worry about that case */ |
| 969 | rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte), |
| 970 | trap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
| 972 | /* Check if pte already has an hpte (case 2) */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 973 | if (unlikely(old_pte & _PAGE_HASHPTE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | /* There MIGHT be an HPTE for this pte */ |
| 975 | unsigned long hash, slot; |
| 976 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 977 | hash = hpt_hash(va, HPAGE_SHIFT); |
| 978 | if (old_pte & _PAGE_F_SECOND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | hash = ~hash; |
| 980 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 981 | slot += (old_pte & _PAGE_F_GIX) >> 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
Benjamin Herrenschmidt | 325c82a | 2005-12-08 16:51:44 +1100 | [diff] [blame] | 983 | if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize, |
| 984 | local) == -1) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 985 | old_pte &= ~_PAGE_HPTEFLAGS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 988 | if (likely(!(old_pte & _PAGE_HASHPTE))) { |
| 989 | unsigned long hash = hpt_hash(va, HPAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | unsigned long hpte_group; |
| 991 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 992 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | repeat: |
| 995 | hpte_group = ((hash & htab_hash_mask) * |
| 996 | HPTES_PER_GROUP) & ~0x7UL; |
| 997 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 998 | /* clear HPTE slot informations in new PTE */ |
| 999 | new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
| 1001 | /* Add in WIMG bits */ |
| 1002 | /* XXX We should store these in the pte */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1003 | /* --BenH: I think they are ... */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 1004 | rflags |= _PAGE_COHERENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1006 | /* Insert into the hash table, primary slot */ |
| 1007 | slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0, |
| 1008 | mmu_huge_psize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
| 1010 | /* Primary is full, try the secondary */ |
| 1011 | if (unlikely(slot == -1)) { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1012 | new_pte |= _PAGE_F_SECOND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | hpte_group = ((~hash & htab_hash_mask) * |
| 1014 | HPTES_PER_GROUP) & ~0x7UL; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1015 | slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, |
Benjamin Herrenschmidt | 67b1081 | 2005-09-23 13:24:07 -0700 | [diff] [blame] | 1016 | HPTE_V_SECONDARY, |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1017 | mmu_huge_psize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | if (slot == -1) { |
| 1019 | if (mftb() & 0x1) |
Benjamin Herrenschmidt | 67b1081 | 2005-09-23 13:24:07 -0700 | [diff] [blame] | 1020 | hpte_group = ((hash & htab_hash_mask) * |
| 1021 | HPTES_PER_GROUP)&~0x7UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | |
| 1023 | ppc_md.hpte_remove(hpte_group); |
| 1024 | goto repeat; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | if (unlikely(slot == -2)) |
| 1029 | panic("hash_huge_page: pte_insert failed\n"); |
| 1030 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1031 | new_pte |= (slot << 12) & _PAGE_F_GIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1034 | /* |
Hugh Dickins | 01edcd8 | 2005-11-23 13:37:39 -0800 | [diff] [blame] | 1035 | * No need to use ldarx/stdcx here |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1036 | */ |
| 1037 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 1038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | err = 0; |
| 1040 | |
| 1041 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | return err; |
| 1043 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 1044 | |
| 1045 | static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags) |
| 1046 | { |
| 1047 | memset(addr, 0, kmem_cache_size(cache)); |
| 1048 | } |
| 1049 | |
| 1050 | static int __init hugetlbpage_init(void) |
| 1051 | { |
| 1052 | if (!cpu_has_feature(CPU_FTR_16M_PAGE)) |
| 1053 | return -ENODEV; |
| 1054 | |
| 1055 | huge_pgtable_cache = kmem_cache_create("hugepte_cache", |
| 1056 | HUGEPTE_TABLE_SIZE, |
| 1057 | HUGEPTE_TABLE_SIZE, |
| 1058 | SLAB_HWCACHE_ALIGN | |
| 1059 | SLAB_MUST_HWCACHE_ALIGN, |
| 1060 | zero_ctor, NULL); |
| 1061 | if (! huge_pgtable_cache) |
| 1062 | panic("hugetlbpage_init(): could not create hugepte cache\n"); |
| 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | module_init(hugetlbpage_init); |