Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 1 | /* |
| 2 | * address space "slices" (meta-segments) support |
| 3 | * |
| 4 | * Copyright (C) 2007 Benjamin Herrenschmidt, IBM Corporation. |
| 5 | * |
| 6 | * Based on hugetlb implementation |
| 7 | * |
| 8 | * Copyright (C) 2003 David Gibson, IBM Corporation. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #undef DEBUG |
| 26 | |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/pagemap.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/spinlock.h> |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 32 | #include <linux/export.h> |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 33 | #include <asm/mman.h> |
| 34 | #include <asm/mmu.h> |
| 35 | #include <asm/spu.h> |
| 36 | |
Roel Kluin | f7a75f0 | 2007-10-16 23:30:25 -0700 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(slice_convert_lock); |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | #ifdef DEBUG |
| 41 | int _slice_debug = 1; |
| 42 | |
| 43 | static void slice_print_mask(const char *label, struct slice_mask mask) |
| 44 | { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 45 | char *p, buf[16 + 3 + 64 + 1]; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 46 | int i; |
| 47 | |
| 48 | if (!_slice_debug) |
| 49 | return; |
| 50 | p = buf; |
| 51 | for (i = 0; i < SLICE_NUM_LOW; i++) |
| 52 | *(p++) = (mask.low_slices & (1 << i)) ? '1' : '0'; |
| 53 | *(p++) = ' '; |
| 54 | *(p++) = '-'; |
| 55 | *(p++) = ' '; |
| 56 | for (i = 0; i < SLICE_NUM_HIGH; i++) |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 57 | *(p++) = (mask.high_slices & (1ul << i)) ? '1' : '0'; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 58 | *(p++) = 0; |
| 59 | |
| 60 | printk(KERN_DEBUG "%s:%s\n", label, buf); |
| 61 | } |
| 62 | |
| 63 | #define slice_dbg(fmt...) do { if (_slice_debug) pr_debug(fmt); } while(0) |
| 64 | |
| 65 | #else |
| 66 | |
| 67 | static void slice_print_mask(const char *label, struct slice_mask mask) {} |
| 68 | #define slice_dbg(fmt...) |
| 69 | |
| 70 | #endif |
| 71 | |
| 72 | static struct slice_mask slice_range_to_mask(unsigned long start, |
| 73 | unsigned long len) |
| 74 | { |
| 75 | unsigned long end = start + len - 1; |
| 76 | struct slice_mask ret = { 0, 0 }; |
| 77 | |
| 78 | if (start < SLICE_LOW_TOP) { |
| 79 | unsigned long mend = min(end, SLICE_LOW_TOP); |
| 80 | unsigned long mstart = min(start, SLICE_LOW_TOP); |
| 81 | |
| 82 | ret.low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1)) |
| 83 | - (1u << GET_LOW_SLICE_INDEX(mstart)); |
| 84 | } |
| 85 | |
| 86 | if ((start + len) > SLICE_LOW_TOP) |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 87 | ret.high_slices = (1ul << (GET_HIGH_SLICE_INDEX(end) + 1)) |
| 88 | - (1ul << GET_HIGH_SLICE_INDEX(start)); |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static int slice_area_is_free(struct mm_struct *mm, unsigned long addr, |
| 94 | unsigned long len) |
| 95 | { |
| 96 | struct vm_area_struct *vma; |
| 97 | |
| 98 | if ((mm->task_size - len) < addr) |
| 99 | return 0; |
| 100 | vma = find_vma(mm, addr); |
| 101 | return (!vma || (addr + len) <= vma->vm_start); |
| 102 | } |
| 103 | |
| 104 | static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice) |
| 105 | { |
| 106 | return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT, |
| 107 | 1ul << SLICE_LOW_SHIFT); |
| 108 | } |
| 109 | |
| 110 | static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice) |
| 111 | { |
| 112 | unsigned long start = slice << SLICE_HIGH_SHIFT; |
| 113 | unsigned long end = start + (1ul << SLICE_HIGH_SHIFT); |
| 114 | |
| 115 | /* Hack, so that each addresses is controlled by exactly one |
| 116 | * of the high or low area bitmaps, the first high area starts |
| 117 | * at 4GB, not 0 */ |
| 118 | if (start == 0) |
| 119 | start = SLICE_LOW_TOP; |
| 120 | |
| 121 | return !slice_area_is_free(mm, start, end - start); |
| 122 | } |
| 123 | |
| 124 | static struct slice_mask slice_mask_for_free(struct mm_struct *mm) |
| 125 | { |
| 126 | struct slice_mask ret = { 0, 0 }; |
| 127 | unsigned long i; |
| 128 | |
| 129 | for (i = 0; i < SLICE_NUM_LOW; i++) |
| 130 | if (!slice_low_has_vma(mm, i)) |
| 131 | ret.low_slices |= 1u << i; |
| 132 | |
| 133 | if (mm->task_size <= SLICE_LOW_TOP) |
| 134 | return ret; |
| 135 | |
| 136 | for (i = 0; i < SLICE_NUM_HIGH; i++) |
| 137 | if (!slice_high_has_vma(mm, i)) |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 138 | ret.high_slices |= 1ul << i; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static struct slice_mask slice_mask_for_size(struct mm_struct *mm, int psize) |
| 144 | { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 145 | unsigned char *hpsizes; |
| 146 | int index, mask_index; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 147 | struct slice_mask ret = { 0, 0 }; |
| 148 | unsigned long i; |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 149 | u64 lpsizes; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 150 | |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 151 | lpsizes = mm->context.low_slices_psize; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 152 | for (i = 0; i < SLICE_NUM_LOW; i++) |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 153 | if (((lpsizes >> (i * 4)) & 0xf) == psize) |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 154 | ret.low_slices |= 1u << i; |
| 155 | |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 156 | hpsizes = mm->context.high_slices_psize; |
| 157 | for (i = 0; i < SLICE_NUM_HIGH; i++) { |
| 158 | mask_index = i & 0x1; |
| 159 | index = i >> 1; |
| 160 | if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize) |
| 161 | ret.high_slices |= 1ul << i; |
| 162 | } |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static int slice_check_fit(struct slice_mask mask, struct slice_mask available) |
| 168 | { |
| 169 | return (mask.low_slices & available.low_slices) == mask.low_slices && |
| 170 | (mask.high_slices & available.high_slices) == mask.high_slices; |
| 171 | } |
| 172 | |
| 173 | static void slice_flush_segments(void *parm) |
| 174 | { |
| 175 | struct mm_struct *mm = parm; |
| 176 | unsigned long flags; |
| 177 | |
| 178 | if (mm != current->active_mm) |
| 179 | return; |
| 180 | |
| 181 | /* update the paca copy of the context struct */ |
| 182 | get_paca()->context = current->active_mm->context; |
| 183 | |
| 184 | local_irq_save(flags); |
| 185 | slb_flush_and_rebolt(); |
| 186 | local_irq_restore(flags); |
| 187 | } |
| 188 | |
| 189 | static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize) |
| 190 | { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 191 | int index, mask_index; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 192 | /* Write the new slice psize bits */ |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 193 | unsigned char *hpsizes; |
| 194 | u64 lpsizes; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 195 | unsigned long i, flags; |
| 196 | |
| 197 | slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize); |
| 198 | slice_print_mask(" mask", mask); |
| 199 | |
| 200 | /* We need to use a spinlock here to protect against |
| 201 | * concurrent 64k -> 4k demotion ... |
| 202 | */ |
| 203 | spin_lock_irqsave(&slice_convert_lock, flags); |
| 204 | |
| 205 | lpsizes = mm->context.low_slices_psize; |
| 206 | for (i = 0; i < SLICE_NUM_LOW; i++) |
| 207 | if (mask.low_slices & (1u << i)) |
| 208 | lpsizes = (lpsizes & ~(0xful << (i * 4))) | |
| 209 | (((unsigned long)psize) << (i * 4)); |
| 210 | |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 211 | /* Assign the value back */ |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 212 | mm->context.low_slices_psize = lpsizes; |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 213 | |
| 214 | hpsizes = mm->context.high_slices_psize; |
| 215 | for (i = 0; i < SLICE_NUM_HIGH; i++) { |
| 216 | mask_index = i & 0x1; |
| 217 | index = i >> 1; |
| 218 | if (mask.high_slices & (1ul << i)) |
| 219 | hpsizes[index] = (hpsizes[index] & |
| 220 | ~(0xf << (mask_index * 4))) | |
| 221 | (((unsigned long)psize) << (mask_index * 4)); |
| 222 | } |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 223 | |
| 224 | slice_dbg(" lsps=%lx, hsps=%lx\n", |
| 225 | mm->context.low_slices_psize, |
| 226 | mm->context.high_slices_psize); |
| 227 | |
| 228 | spin_unlock_irqrestore(&slice_convert_lock, flags); |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 229 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 230 | #ifdef CONFIG_SPU_BASE |
| 231 | spu_flush_all_slbs(mm); |
| 232 | #endif |
| 233 | } |
| 234 | |
| 235 | static unsigned long slice_find_area_bottomup(struct mm_struct *mm, |
| 236 | unsigned long len, |
| 237 | struct slice_mask available, |
| 238 | int psize, int use_cache) |
| 239 | { |
| 240 | struct vm_area_struct *vma; |
| 241 | unsigned long start_addr, addr; |
| 242 | struct slice_mask mask; |
| 243 | int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); |
| 244 | |
| 245 | if (use_cache) { |
| 246 | if (len <= mm->cached_hole_size) { |
| 247 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 248 | mm->cached_hole_size = 0; |
| 249 | } else |
| 250 | start_addr = addr = mm->free_area_cache; |
| 251 | } else |
| 252 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 253 | |
| 254 | full_search: |
| 255 | for (;;) { |
| 256 | addr = _ALIGN_UP(addr, 1ul << pshift); |
| 257 | if ((TASK_SIZE - len) < addr) |
| 258 | break; |
| 259 | vma = find_vma(mm, addr); |
| 260 | BUG_ON(vma && (addr >= vma->vm_end)); |
| 261 | |
| 262 | mask = slice_range_to_mask(addr, len); |
| 263 | if (!slice_check_fit(mask, available)) { |
| 264 | if (addr < SLICE_LOW_TOP) |
| 265 | addr = _ALIGN_UP(addr + 1, 1ul << SLICE_LOW_SHIFT); |
| 266 | else |
| 267 | addr = _ALIGN_UP(addr + 1, 1ul << SLICE_HIGH_SHIFT); |
| 268 | continue; |
| 269 | } |
| 270 | if (!vma || addr + len <= vma->vm_start) { |
| 271 | /* |
| 272 | * Remember the place where we stopped the search: |
| 273 | */ |
| 274 | if (use_cache) |
| 275 | mm->free_area_cache = addr + len; |
| 276 | return addr; |
| 277 | } |
| 278 | if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start) |
| 279 | mm->cached_hole_size = vma->vm_start - addr; |
| 280 | addr = vma->vm_end; |
| 281 | } |
| 282 | |
| 283 | /* Make sure we didn't miss any holes */ |
| 284 | if (use_cache && start_addr != TASK_UNMAPPED_BASE) { |
| 285 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 286 | mm->cached_hole_size = 0; |
| 287 | goto full_search; |
| 288 | } |
| 289 | return -ENOMEM; |
| 290 | } |
| 291 | |
| 292 | static unsigned long slice_find_area_topdown(struct mm_struct *mm, |
| 293 | unsigned long len, |
| 294 | struct slice_mask available, |
| 295 | int psize, int use_cache) |
| 296 | { |
| 297 | struct vm_area_struct *vma; |
| 298 | unsigned long addr; |
| 299 | struct slice_mask mask; |
| 300 | int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); |
| 301 | |
| 302 | /* check if free_area_cache is useful for us */ |
| 303 | if (use_cache) { |
| 304 | if (len <= mm->cached_hole_size) { |
| 305 | mm->cached_hole_size = 0; |
| 306 | mm->free_area_cache = mm->mmap_base; |
| 307 | } |
| 308 | |
| 309 | /* either no address requested or can't fit in requested |
| 310 | * address hole |
| 311 | */ |
| 312 | addr = mm->free_area_cache; |
| 313 | |
| 314 | /* make sure it can fit in the remaining address space */ |
| 315 | if (addr > len) { |
| 316 | addr = _ALIGN_DOWN(addr - len, 1ul << pshift); |
| 317 | mask = slice_range_to_mask(addr, len); |
| 318 | if (slice_check_fit(mask, available) && |
| 319 | slice_area_is_free(mm, addr, len)) |
| 320 | /* remember the address as a hint for |
| 321 | * next time |
| 322 | */ |
| 323 | return (mm->free_area_cache = addr); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | addr = mm->mmap_base; |
| 328 | while (addr > len) { |
| 329 | /* Go down by chunk size */ |
| 330 | addr = _ALIGN_DOWN(addr - len, 1ul << pshift); |
| 331 | |
| 332 | /* Check for hit with different page size */ |
| 333 | mask = slice_range_to_mask(addr, len); |
| 334 | if (!slice_check_fit(mask, available)) { |
| 335 | if (addr < SLICE_LOW_TOP) |
| 336 | addr = _ALIGN_DOWN(addr, 1ul << SLICE_LOW_SHIFT); |
| 337 | else if (addr < (1ul << SLICE_HIGH_SHIFT)) |
| 338 | addr = SLICE_LOW_TOP; |
| 339 | else |
| 340 | addr = _ALIGN_DOWN(addr, 1ul << SLICE_HIGH_SHIFT); |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Lookup failure means no vma is above this address, |
| 346 | * else if new region fits below vma->vm_start, |
| 347 | * return with success: |
| 348 | */ |
| 349 | vma = find_vma(mm, addr); |
| 350 | if (!vma || (addr + len) <= vma->vm_start) { |
| 351 | /* remember the address as a hint for next time */ |
| 352 | if (use_cache) |
| 353 | mm->free_area_cache = addr; |
| 354 | return addr; |
| 355 | } |
| 356 | |
| 357 | /* remember the largest hole we saw so far */ |
| 358 | if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start) |
| 359 | mm->cached_hole_size = vma->vm_start - addr; |
| 360 | |
| 361 | /* try just below the current vma->vm_start */ |
| 362 | addr = vma->vm_start; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * A failed mmap() very likely causes application failure, |
| 367 | * so fall back to the bottom-up function here. This scenario |
| 368 | * can happen with large stack limits and large mmap() |
| 369 | * allocations. |
| 370 | */ |
| 371 | addr = slice_find_area_bottomup(mm, len, available, psize, 0); |
| 372 | |
| 373 | /* |
| 374 | * Restore the topdown base: |
| 375 | */ |
| 376 | if (use_cache) { |
| 377 | mm->free_area_cache = mm->mmap_base; |
| 378 | mm->cached_hole_size = ~0UL; |
| 379 | } |
| 380 | |
| 381 | return addr; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len, |
| 386 | struct slice_mask mask, int psize, |
| 387 | int topdown, int use_cache) |
| 388 | { |
| 389 | if (topdown) |
| 390 | return slice_find_area_topdown(mm, len, mask, psize, use_cache); |
| 391 | else |
| 392 | return slice_find_area_bottomup(mm, len, mask, psize, use_cache); |
| 393 | } |
| 394 | |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 395 | #define or_mask(dst, src) do { \ |
| 396 | (dst).low_slices |= (src).low_slices; \ |
| 397 | (dst).high_slices |= (src).high_slices; \ |
| 398 | } while (0) |
| 399 | |
| 400 | #define andnot_mask(dst, src) do { \ |
| 401 | (dst).low_slices &= ~(src).low_slices; \ |
| 402 | (dst).high_slices &= ~(src).high_slices; \ |
| 403 | } while (0) |
| 404 | |
| 405 | #ifdef CONFIG_PPC_64K_PAGES |
| 406 | #define MMU_PAGE_BASE MMU_PAGE_64K |
| 407 | #else |
| 408 | #define MMU_PAGE_BASE MMU_PAGE_4K |
| 409 | #endif |
| 410 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 411 | unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len, |
| 412 | unsigned long flags, unsigned int psize, |
| 413 | int topdown, int use_cache) |
| 414 | { |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 415 | struct slice_mask mask = {0, 0}; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 416 | struct slice_mask good_mask; |
| 417 | struct slice_mask potential_mask = {0,0} /* silence stupid warning */; |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 418 | struct slice_mask compat_mask = {0, 0}; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 419 | int fixed = (flags & MAP_FIXED); |
| 420 | int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); |
| 421 | struct mm_struct *mm = current->mm; |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 422 | unsigned long newaddr; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 423 | |
| 424 | /* Sanity checks */ |
| 425 | BUG_ON(mm->task_size == 0); |
| 426 | |
| 427 | slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize); |
| 428 | slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d, use_cache=%d\n", |
| 429 | addr, len, flags, topdown, use_cache); |
| 430 | |
| 431 | if (len > mm->task_size) |
| 432 | return -ENOMEM; |
Benjamin Herrenschmidt | d1f5a77 | 2007-08-08 15:44:15 +1000 | [diff] [blame] | 433 | if (len & ((1ul << pshift) - 1)) |
| 434 | return -EINVAL; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 435 | if (fixed && (addr & ((1ul << pshift) - 1))) |
| 436 | return -EINVAL; |
| 437 | if (fixed && addr > (mm->task_size - len)) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | /* If hint, make sure it matches our alignment restrictions */ |
| 441 | if (!fixed && addr) { |
| 442 | addr = _ALIGN_UP(addr, 1ul << pshift); |
| 443 | slice_dbg(" aligned addr=%lx\n", addr); |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 444 | /* Ignore hint if it's too large or overlaps a VMA */ |
| 445 | if (addr > mm->task_size - len || |
| 446 | !slice_area_is_free(mm, addr, len)) |
| 447 | addr = 0; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 448 | } |
| 449 | |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 450 | /* First make up a "good" mask of slices that have the right size |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 451 | * already |
| 452 | */ |
| 453 | good_mask = slice_mask_for_size(mm, psize); |
| 454 | slice_print_mask(" good_mask", good_mask); |
| 455 | |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 456 | /* |
| 457 | * Here "good" means slices that are already the right page size, |
| 458 | * "compat" means slices that have a compatible page size (i.e. |
| 459 | * 4k in a 64k pagesize kernel), and "free" means slices without |
| 460 | * any VMAs. |
| 461 | * |
| 462 | * If MAP_FIXED: |
| 463 | * check if fits in good | compat => OK |
| 464 | * check if fits in good | compat | free => convert free |
| 465 | * else bad |
| 466 | * If have hint: |
| 467 | * check if hint fits in good => OK |
| 468 | * check if hint fits in good | free => convert free |
| 469 | * Otherwise: |
| 470 | * search in good, found => OK |
| 471 | * search in good | free, found => convert free |
| 472 | * search in good | compat | free, found => convert free. |
| 473 | */ |
| 474 | |
| 475 | #ifdef CONFIG_PPC_64K_PAGES |
| 476 | /* If we support combo pages, we can allow 64k pages in 4k slices */ |
| 477 | if (psize == MMU_PAGE_64K) { |
| 478 | compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K); |
| 479 | if (fixed) |
| 480 | or_mask(good_mask, compat_mask); |
| 481 | } |
| 482 | #endif |
| 483 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 484 | /* First check hint if it's valid or if we have MAP_FIXED */ |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 485 | if (addr != 0 || fixed) { |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 486 | /* Build a mask for the requested range */ |
| 487 | mask = slice_range_to_mask(addr, len); |
| 488 | slice_print_mask(" mask", mask); |
| 489 | |
| 490 | /* Check if we fit in the good mask. If we do, we just return, |
| 491 | * nothing else to do |
| 492 | */ |
| 493 | if (slice_check_fit(mask, good_mask)) { |
| 494 | slice_dbg(" fits good !\n"); |
| 495 | return addr; |
| 496 | } |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 497 | } else { |
| 498 | /* Now let's see if we can find something in the existing |
| 499 | * slices for that size |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 500 | */ |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 501 | newaddr = slice_find_area(mm, len, good_mask, psize, topdown, |
| 502 | use_cache); |
| 503 | if (newaddr != -ENOMEM) { |
| 504 | /* Found within the good mask, we don't have to setup, |
| 505 | * we thus return directly |
| 506 | */ |
| 507 | slice_dbg(" found area at 0x%lx\n", newaddr); |
| 508 | return newaddr; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 512 | /* We don't fit in the good mask, check what other slices are |
| 513 | * empty and thus can be converted |
| 514 | */ |
| 515 | potential_mask = slice_mask_for_free(mm); |
| 516 | or_mask(potential_mask, good_mask); |
| 517 | slice_print_mask(" potential", potential_mask); |
| 518 | |
| 519 | if ((addr != 0 || fixed) && slice_check_fit(mask, potential_mask)) { |
| 520 | slice_dbg(" fits potential !\n"); |
| 521 | goto convert; |
| 522 | } |
| 523 | |
| 524 | /* If we have MAP_FIXED and failed the above steps, then error out */ |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 525 | if (fixed) |
| 526 | return -EBUSY; |
| 527 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 528 | slice_dbg(" search...\n"); |
| 529 | |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 530 | /* If we had a hint that didn't work out, see if we can fit |
| 531 | * anywhere in the good area. |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 532 | */ |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 533 | if (addr) { |
| 534 | addr = slice_find_area(mm, len, good_mask, psize, topdown, |
| 535 | use_cache); |
| 536 | if (addr != -ENOMEM) { |
| 537 | slice_dbg(" found area at 0x%lx\n", addr); |
| 538 | return addr; |
| 539 | } |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* Now let's see if we can find something in the existing slices |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 543 | * for that size plus free slices |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 544 | */ |
| 545 | addr = slice_find_area(mm, len, potential_mask, psize, topdown, |
| 546 | use_cache); |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 547 | |
| 548 | #ifdef CONFIG_PPC_64K_PAGES |
| 549 | if (addr == -ENOMEM && psize == MMU_PAGE_64K) { |
| 550 | /* retry the search with 4k-page slices included */ |
| 551 | or_mask(potential_mask, compat_mask); |
| 552 | addr = slice_find_area(mm, len, potential_mask, psize, |
| 553 | topdown, use_cache); |
| 554 | } |
| 555 | #endif |
| 556 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 557 | if (addr == -ENOMEM) |
| 558 | return -ENOMEM; |
| 559 | |
| 560 | mask = slice_range_to_mask(addr, len); |
| 561 | slice_dbg(" found potential area at 0x%lx\n", addr); |
| 562 | slice_print_mask(" mask", mask); |
| 563 | |
| 564 | convert: |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 565 | andnot_mask(mask, good_mask); |
| 566 | andnot_mask(mask, compat_mask); |
| 567 | if (mask.low_slices || mask.high_slices) { |
| 568 | slice_convert(mm, mask, psize); |
| 569 | if (psize > MMU_PAGE_BASE) |
Benjamin Herrenschmidt | 84c3d4a | 2008-07-16 11:07:59 +1000 | [diff] [blame] | 570 | on_each_cpu(slice_flush_segments, mm, 1); |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 571 | } |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 572 | return addr; |
| 573 | |
| 574 | } |
| 575 | EXPORT_SYMBOL_GPL(slice_get_unmapped_area); |
| 576 | |
| 577 | unsigned long arch_get_unmapped_area(struct file *filp, |
| 578 | unsigned long addr, |
| 579 | unsigned long len, |
| 580 | unsigned long pgoff, |
| 581 | unsigned long flags) |
| 582 | { |
| 583 | return slice_get_unmapped_area(addr, len, flags, |
| 584 | current->mm->context.user_psize, |
| 585 | 0, 1); |
| 586 | } |
| 587 | |
| 588 | unsigned long arch_get_unmapped_area_topdown(struct file *filp, |
| 589 | const unsigned long addr0, |
| 590 | const unsigned long len, |
| 591 | const unsigned long pgoff, |
| 592 | const unsigned long flags) |
| 593 | { |
| 594 | return slice_get_unmapped_area(addr0, len, flags, |
| 595 | current->mm->context.user_psize, |
| 596 | 1, 1); |
| 597 | } |
| 598 | |
| 599 | unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr) |
| 600 | { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 601 | unsigned char *hpsizes; |
| 602 | int index, mask_index; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 603 | |
| 604 | if (addr < SLICE_LOW_TOP) { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 605 | u64 lpsizes; |
| 606 | lpsizes = mm->context.low_slices_psize; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 607 | index = GET_LOW_SLICE_INDEX(addr); |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 608 | return (lpsizes >> (index * 4)) & 0xf; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 609 | } |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 610 | hpsizes = mm->context.high_slices_psize; |
| 611 | index = GET_HIGH_SLICE_INDEX(addr); |
| 612 | mask_index = index & 0x1; |
| 613 | return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xf; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 614 | } |
| 615 | EXPORT_SYMBOL_GPL(get_slice_psize); |
| 616 | |
| 617 | /* |
| 618 | * This is called by hash_page when it needs to do a lazy conversion of |
| 619 | * an address space from real 64K pages to combo 4K pages (typically |
| 620 | * when hitting a non cacheable mapping on a processor or hypervisor |
| 621 | * that won't allow them for 64K pages). |
| 622 | * |
| 623 | * This is also called in init_new_context() to change back the user |
| 624 | * psize from whatever the parent context had it set to |
Stephen Rothwell | 9dfe5c53 | 2007-08-15 16:33:55 +1000 | [diff] [blame] | 625 | * N.B. This may be called before mm->context.id has been set. |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 626 | * |
| 627 | * This function will only change the content of the {low,high)_slice_psize |
| 628 | * masks, it will not flush SLBs as this shall be handled lazily by the |
| 629 | * caller. |
| 630 | */ |
| 631 | void slice_set_user_psize(struct mm_struct *mm, unsigned int psize) |
| 632 | { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 633 | int index, mask_index; |
| 634 | unsigned char *hpsizes; |
| 635 | unsigned long flags, lpsizes; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 636 | unsigned int old_psize; |
| 637 | int i; |
| 638 | |
| 639 | slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize); |
| 640 | |
| 641 | spin_lock_irqsave(&slice_convert_lock, flags); |
| 642 | |
| 643 | old_psize = mm->context.user_psize; |
| 644 | slice_dbg(" old_psize=%d\n", old_psize); |
| 645 | if (old_psize == psize) |
| 646 | goto bail; |
| 647 | |
| 648 | mm->context.user_psize = psize; |
| 649 | wmb(); |
| 650 | |
| 651 | lpsizes = mm->context.low_slices_psize; |
| 652 | for (i = 0; i < SLICE_NUM_LOW; i++) |
| 653 | if (((lpsizes >> (i * 4)) & 0xf) == old_psize) |
| 654 | lpsizes = (lpsizes & ~(0xful << (i * 4))) | |
| 655 | (((unsigned long)psize) << (i * 4)); |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 656 | /* Assign the value back */ |
| 657 | mm->context.low_slices_psize = lpsizes; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 658 | |
| 659 | hpsizes = mm->context.high_slices_psize; |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 660 | for (i = 0; i < SLICE_NUM_HIGH; i++) { |
| 661 | mask_index = i & 0x1; |
| 662 | index = i >> 1; |
| 663 | if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == old_psize) |
| 664 | hpsizes[index] = (hpsizes[index] & |
| 665 | ~(0xf << (mask_index * 4))) | |
| 666 | (((unsigned long)psize) << (mask_index * 4)); |
| 667 | } |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 668 | |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 669 | |
| 670 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 671 | |
| 672 | slice_dbg(" lsps=%lx, hsps=%lx\n", |
| 673 | mm->context.low_slices_psize, |
| 674 | mm->context.high_slices_psize); |
| 675 | |
| 676 | bail: |
| 677 | spin_unlock_irqrestore(&slice_convert_lock, flags); |
| 678 | } |
| 679 | |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 680 | void slice_set_psize(struct mm_struct *mm, unsigned long address, |
| 681 | unsigned int psize) |
| 682 | { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 683 | unsigned char *hpsizes; |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 684 | unsigned long i, flags; |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 685 | u64 *lpsizes; |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 686 | |
| 687 | spin_lock_irqsave(&slice_convert_lock, flags); |
| 688 | if (address < SLICE_LOW_TOP) { |
| 689 | i = GET_LOW_SLICE_INDEX(address); |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 690 | lpsizes = &mm->context.low_slices_psize; |
| 691 | *lpsizes = (*lpsizes & ~(0xful << (i * 4))) | |
| 692 | ((unsigned long) psize << (i * 4)); |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 693 | } else { |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 694 | int index, mask_index; |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 695 | i = GET_HIGH_SLICE_INDEX(address); |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 696 | hpsizes = mm->context.high_slices_psize; |
| 697 | mask_index = i & 0x1; |
| 698 | index = i >> 1; |
| 699 | hpsizes[index] = (hpsizes[index] & |
| 700 | ~(0xf << (mask_index * 4))) | |
| 701 | (((unsigned long)psize) << (mask_index * 4)); |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 702 | } |
Aneesh Kumar K.V | 7aa0727 | 2012-09-10 02:52:52 +0000 | [diff] [blame^] | 703 | |
Paul Mackerras | 3a8247c | 2008-06-18 15:29:12 +1000 | [diff] [blame] | 704 | spin_unlock_irqrestore(&slice_convert_lock, flags); |
| 705 | |
| 706 | #ifdef CONFIG_SPU_BASE |
| 707 | spu_flush_all_slbs(mm); |
| 708 | #endif |
| 709 | } |
| 710 | |
| 711 | void slice_set_range_psize(struct mm_struct *mm, unsigned long start, |
| 712 | unsigned long len, unsigned int psize) |
| 713 | { |
| 714 | struct slice_mask mask = slice_range_to_mask(start, len); |
| 715 | |
| 716 | slice_convert(mm, mask, psize); |
| 717 | } |
| 718 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 719 | /* |
| 720 | * is_hugepage_only_range() is used by generic code to verify wether |
| 721 | * a normal mmap mapping (non hugetlbfs) is valid on a given area. |
| 722 | * |
| 723 | * until the generic code provides a more generic hook and/or starts |
| 724 | * calling arch get_unmapped_area for MAP_FIXED (which our implementation |
| 725 | * here knows how to deal with), we hijack it to keep standard mappings |
| 726 | * away from us. |
| 727 | * |
| 728 | * because of that generic code limitation, MAP_FIXED mapping cannot |
| 729 | * "convert" back a slice with no VMAs to the standard page size, only |
| 730 | * get_unmapped_area() can. It would be possible to fix it here but I |
| 731 | * prefer working on fixing the generic code instead. |
| 732 | * |
| 733 | * WARNING: This will not work if hugetlbfs isn't enabled since the |
| 734 | * generic code will redefine that function as 0 in that. This is ok |
| 735 | * for now as we only use slices with hugetlbfs enabled. This should |
| 736 | * be fixed as the generic code gets fixed. |
| 737 | */ |
| 738 | int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr, |
| 739 | unsigned long len) |
| 740 | { |
| 741 | struct slice_mask mask, available; |
Dave Kleikamp | 9ba0fdb | 2009-01-14 09:09:34 +0000 | [diff] [blame] | 742 | unsigned int psize = mm->context.user_psize; |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 743 | |
| 744 | mask = slice_range_to_mask(addr, len); |
Dave Kleikamp | 9ba0fdb | 2009-01-14 09:09:34 +0000 | [diff] [blame] | 745 | available = slice_mask_for_size(mm, psize); |
| 746 | #ifdef CONFIG_PPC_64K_PAGES |
| 747 | /* We need to account for 4k slices too */ |
| 748 | if (psize == MMU_PAGE_64K) { |
| 749 | struct slice_mask compat_mask; |
| 750 | compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K); |
| 751 | or_mask(available, compat_mask); |
| 752 | } |
| 753 | #endif |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 754 | |
| 755 | #if 0 /* too verbose */ |
| 756 | slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n", |
| 757 | mm, addr, len); |
| 758 | slice_print_mask(" mask", mask); |
| 759 | slice_print_mask(" available", available); |
| 760 | #endif |
| 761 | return !slice_check_fit(mask, available); |
| 762 | } |
| 763 | |