slice.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. #undef DEBUG
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/err.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/export.h>
  31. #include <linux/hugetlb.h>
  32. #include <asm/mman.h>
  33. #include <asm/mmu.h>
  34. #include <asm/copro.h>
  35. #include <asm/hugetlb.h>
  36. static DEFINE_SPINLOCK(slice_convert_lock);
  37. /*
  38. * One bit per slice. We have lower slices which cover 256MB segments
  39. * upto 4G range. That gets us 16 low slices. For the rest we track slices
  40. * in 1TB size.
  41. */
  42. struct slice_mask {
  43. u64 low_slices;
  44. DECLARE_BITMAP(high_slices, SLICE_NUM_HIGH);
  45. };
  46. #ifdef DEBUG
  47. int _slice_debug = 1;
  48. static void slice_print_mask(const char *label, struct slice_mask mask)
  49. {
  50. if (!_slice_debug)
  51. return;
  52. pr_devel("%s low_slice: %*pbl\n", label, (int)SLICE_NUM_LOW, &mask.low_slices);
  53. pr_devel("%s high_slice: %*pbl\n", label, (int)SLICE_NUM_HIGH, mask.high_slices);
  54. }
  55. #define slice_dbg(fmt...) do { if (_slice_debug) pr_devel(fmt); } while (0)
  56. #else
  57. static void slice_print_mask(const char *label, struct slice_mask mask) {}
  58. #define slice_dbg(fmt...)
  59. #endif
  60. static void slice_range_to_mask(unsigned long start, unsigned long len,
  61. struct slice_mask *ret)
  62. {
  63. unsigned long end = start + len - 1;
  64. ret->low_slices = 0;
  65. bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
  66. if (start < SLICE_LOW_TOP) {
  67. unsigned long mend = min(end, (SLICE_LOW_TOP - 1));
  68. ret->low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
  69. - (1u << GET_LOW_SLICE_INDEX(start));
  70. }
  71. if ((start + len) > SLICE_LOW_TOP) {
  72. unsigned long start_index = GET_HIGH_SLICE_INDEX(start);
  73. unsigned long align_end = ALIGN(end, (1UL << SLICE_HIGH_SHIFT));
  74. unsigned long count = GET_HIGH_SLICE_INDEX(align_end) - start_index;
  75. bitmap_set(ret->high_slices, start_index, count);
  76. }
  77. }
  78. static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
  79. unsigned long len)
  80. {
  81. struct vm_area_struct *vma;
  82. if ((mm->context.slb_addr_limit - len) < addr)
  83. return 0;
  84. vma = find_vma(mm, addr);
  85. return (!vma || (addr + len) <= vm_start_gap(vma));
  86. }
  87. static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
  88. {
  89. return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
  90. 1ul << SLICE_LOW_SHIFT);
  91. }
  92. static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
  93. {
  94. unsigned long start = slice << SLICE_HIGH_SHIFT;
  95. unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
  96. /* Hack, so that each addresses is controlled by exactly one
  97. * of the high or low area bitmaps, the first high area starts
  98. * at 4GB, not 0 */
  99. if (start == 0)
  100. start = SLICE_LOW_TOP;
  101. return !slice_area_is_free(mm, start, end - start);
  102. }
  103. static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret,
  104. unsigned long high_limit)
  105. {
  106. unsigned long i;
  107. ret->low_slices = 0;
  108. bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
  109. for (i = 0; i < SLICE_NUM_LOW; i++)
  110. if (!slice_low_has_vma(mm, i))
  111. ret->low_slices |= 1u << i;
  112. if (high_limit <= SLICE_LOW_TOP)
  113. return;
  114. for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++)
  115. if (!slice_high_has_vma(mm, i))
  116. __set_bit(i, ret->high_slices);
  117. }
  118. static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret,
  119. unsigned long high_limit)
  120. {
  121. unsigned char *hpsizes;
  122. int index, mask_index;
  123. unsigned long i;
  124. u64 lpsizes;
  125. ret->low_slices = 0;
  126. bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
  127. lpsizes = mm->context.low_slices_psize;
  128. for (i = 0; i < SLICE_NUM_LOW; i++)
  129. if (((lpsizes >> (i * 4)) & 0xf) == psize)
  130. ret->low_slices |= 1u << i;
  131. if (high_limit <= SLICE_LOW_TOP)
  132. return;
  133. hpsizes = mm->context.high_slices_psize;
  134. for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++) {
  135. mask_index = i & 0x1;
  136. index = i >> 1;
  137. if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
  138. __set_bit(i, ret->high_slices);
  139. }
  140. }
  141. static int slice_check_fit(struct mm_struct *mm,
  142. struct slice_mask mask, struct slice_mask available)
  143. {
  144. DECLARE_BITMAP(result, SLICE_NUM_HIGH);
  145. /*
  146. * Make sure we just do bit compare only to the max
  147. * addr limit and not the full bit map size.
  148. */
  149. unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit);
  150. bitmap_and(result, mask.high_slices,
  151. available.high_slices, slice_count);
  152. return (mask.low_slices & available.low_slices) == mask.low_slices &&
  153. bitmap_equal(result, mask.high_slices, slice_count);
  154. }
  155. static void slice_flush_segments(void *parm)
  156. {
  157. struct mm_struct *mm = parm;
  158. unsigned long flags;
  159. if (mm != current->active_mm)
  160. return;
  161. copy_mm_to_paca(current->active_mm);
  162. local_irq_save(flags);
  163. slb_flush_and_rebolt();
  164. local_irq_restore(flags);
  165. }
  166. static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize)
  167. {
  168. int index, mask_index;
  169. /* Write the new slice psize bits */
  170. unsigned char *hpsizes;
  171. u64 lpsizes;
  172. unsigned long i, flags;
  173. slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
  174. slice_print_mask(" mask", mask);
  175. /* We need to use a spinlock here to protect against
  176. * concurrent 64k -> 4k demotion ...
  177. */
  178. spin_lock_irqsave(&slice_convert_lock, flags);
  179. lpsizes = mm->context.low_slices_psize;
  180. for (i = 0; i < SLICE_NUM_LOW; i++)
  181. if (mask.low_slices & (1u << i))
  182. lpsizes = (lpsizes & ~(0xful << (i * 4))) |
  183. (((unsigned long)psize) << (i * 4));
  184. /* Assign the value back */
  185. mm->context.low_slices_psize = lpsizes;
  186. hpsizes = mm->context.high_slices_psize;
  187. for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++) {
  188. mask_index = i & 0x1;
  189. index = i >> 1;
  190. if (test_bit(i, mask.high_slices))
  191. hpsizes[index] = (hpsizes[index] &
  192. ~(0xf << (mask_index * 4))) |
  193. (((unsigned long)psize) << (mask_index * 4));
  194. }
  195. slice_dbg(" lsps=%lx, hsps=%lx\n",
  196. (unsigned long)mm->context.low_slices_psize,
  197. (unsigned long)mm->context.high_slices_psize);
  198. spin_unlock_irqrestore(&slice_convert_lock, flags);
  199. copro_flush_all_slbs(mm);
  200. }
  201. /*
  202. * Compute which slice addr is part of;
  203. * set *boundary_addr to the start or end boundary of that slice
  204. * (depending on 'end' parameter);
  205. * return boolean indicating if the slice is marked as available in the
  206. * 'available' slice_mark.
  207. */
  208. static bool slice_scan_available(unsigned long addr,
  209. struct slice_mask available,
  210. int end,
  211. unsigned long *boundary_addr)
  212. {
  213. unsigned long slice;
  214. if (addr < SLICE_LOW_TOP) {
  215. slice = GET_LOW_SLICE_INDEX(addr);
  216. *boundary_addr = (slice + end) << SLICE_LOW_SHIFT;
  217. return !!(available.low_slices & (1u << slice));
  218. } else {
  219. slice = GET_HIGH_SLICE_INDEX(addr);
  220. *boundary_addr = (slice + end) ?
  221. ((slice + end) << SLICE_HIGH_SHIFT) : SLICE_LOW_TOP;
  222. return !!test_bit(slice, available.high_slices);
  223. }
  224. }
  225. static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
  226. unsigned long len,
  227. struct slice_mask available,
  228. int psize, unsigned long high_limit)
  229. {
  230. int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
  231. unsigned long addr, found, next_end;
  232. struct vm_unmapped_area_info info;
  233. info.flags = 0;
  234. info.length = len;
  235. info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
  236. info.align_offset = 0;
  237. addr = TASK_UNMAPPED_BASE;
  238. /*
  239. * Check till the allow max value for this mmap request
  240. */
  241. while (addr < high_limit) {
  242. info.low_limit = addr;
  243. if (!slice_scan_available(addr, available, 1, &addr))
  244. continue;
  245. next_slice:
  246. /*
  247. * At this point [info.low_limit; addr) covers
  248. * available slices only and ends at a slice boundary.
  249. * Check if we need to reduce the range, or if we can
  250. * extend it to cover the next available slice.
  251. */
  252. if (addr >= high_limit)
  253. addr = high_limit;
  254. else if (slice_scan_available(addr, available, 1, &next_end)) {
  255. addr = next_end;
  256. goto next_slice;
  257. }
  258. info.high_limit = addr;
  259. found = vm_unmapped_area(&info);
  260. if (!(found & ~PAGE_MASK))
  261. return found;
  262. }
  263. return -ENOMEM;
  264. }
  265. static unsigned long slice_find_area_topdown(struct mm_struct *mm,
  266. unsigned long len,
  267. struct slice_mask available,
  268. int psize, unsigned long high_limit)
  269. {
  270. int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
  271. unsigned long addr, found, prev;
  272. struct vm_unmapped_area_info info;
  273. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  274. info.length = len;
  275. info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
  276. info.align_offset = 0;
  277. addr = mm->mmap_base;
  278. /*
  279. * If we are trying to allocate above DEFAULT_MAP_WINDOW
  280. * Add the different to the mmap_base.
  281. * Only for that request for which high_limit is above
  282. * DEFAULT_MAP_WINDOW we should apply this.
  283. */
  284. if (high_limit > DEFAULT_MAP_WINDOW)
  285. addr += mm->context.slb_addr_limit - DEFAULT_MAP_WINDOW;
  286. while (addr > PAGE_SIZE) {
  287. info.high_limit = addr;
  288. if (!slice_scan_available(addr - 1, available, 0, &addr))
  289. continue;
  290. prev_slice:
  291. /*
  292. * At this point [addr; info.high_limit) covers
  293. * available slices only and starts at a slice boundary.
  294. * Check if we need to reduce the range, or if we can
  295. * extend it to cover the previous available slice.
  296. */
  297. if (addr < PAGE_SIZE)
  298. addr = PAGE_SIZE;
  299. else if (slice_scan_available(addr - 1, available, 0, &prev)) {
  300. addr = prev;
  301. goto prev_slice;
  302. }
  303. info.low_limit = addr;
  304. found = vm_unmapped_area(&info);
  305. if (!(found & ~PAGE_MASK))
  306. return found;
  307. }
  308. /*
  309. * A failed mmap() very likely causes application failure,
  310. * so fall back to the bottom-up function here. This scenario
  311. * can happen with large stack limits and large mmap()
  312. * allocations.
  313. */
  314. return slice_find_area_bottomup(mm, len, available, psize, high_limit);
  315. }
  316. static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
  317. struct slice_mask mask, int psize,
  318. int topdown, unsigned long high_limit)
  319. {
  320. if (topdown)
  321. return slice_find_area_topdown(mm, len, mask, psize, high_limit);
  322. else
  323. return slice_find_area_bottomup(mm, len, mask, psize, high_limit);
  324. }
  325. static inline void slice_or_mask(struct slice_mask *dst, struct slice_mask *src)
  326. {
  327. DECLARE_BITMAP(result, SLICE_NUM_HIGH);
  328. dst->low_slices |= src->low_slices;
  329. bitmap_or(result, dst->high_slices, src->high_slices, SLICE_NUM_HIGH);
  330. bitmap_copy(dst->high_slices, result, SLICE_NUM_HIGH);
  331. }
  332. static inline void slice_andnot_mask(struct slice_mask *dst, struct slice_mask *src)
  333. {
  334. DECLARE_BITMAP(result, SLICE_NUM_HIGH);
  335. dst->low_slices &= ~src->low_slices;
  336. bitmap_andnot(result, dst->high_slices, src->high_slices, SLICE_NUM_HIGH);
  337. bitmap_copy(dst->high_slices, result, SLICE_NUM_HIGH);
  338. }
  339. #ifdef CONFIG_PPC_64K_PAGES
  340. #define MMU_PAGE_BASE MMU_PAGE_64K
  341. #else
  342. #define MMU_PAGE_BASE MMU_PAGE_4K
  343. #endif
  344. unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
  345. unsigned long flags, unsigned int psize,
  346. int topdown)
  347. {
  348. struct slice_mask mask;
  349. struct slice_mask good_mask;
  350. struct slice_mask potential_mask;
  351. struct slice_mask compat_mask;
  352. int fixed = (flags & MAP_FIXED);
  353. int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
  354. unsigned long page_size = 1UL << pshift;
  355. struct mm_struct *mm = current->mm;
  356. unsigned long newaddr;
  357. unsigned long high_limit;
  358. high_limit = DEFAULT_MAP_WINDOW;
  359. if (addr >= high_limit || (fixed && (addr + len > high_limit)))
  360. high_limit = TASK_SIZE;
  361. if (len > high_limit)
  362. return -ENOMEM;
  363. if (len & (page_size - 1))
  364. return -EINVAL;
  365. if (fixed) {
  366. if (addr & (page_size - 1))
  367. return -EINVAL;
  368. if (addr > high_limit - len)
  369. return -ENOMEM;
  370. }
  371. if (high_limit > mm->context.slb_addr_limit) {
  372. mm->context.slb_addr_limit = high_limit;
  373. on_each_cpu(slice_flush_segments, mm, 1);
  374. }
  375. /*
  376. * init different masks
  377. */
  378. mask.low_slices = 0;
  379. bitmap_zero(mask.high_slices, SLICE_NUM_HIGH);
  380. /* silence stupid warning */;
  381. potential_mask.low_slices = 0;
  382. bitmap_zero(potential_mask.high_slices, SLICE_NUM_HIGH);
  383. compat_mask.low_slices = 0;
  384. bitmap_zero(compat_mask.high_slices, SLICE_NUM_HIGH);
  385. /* Sanity checks */
  386. BUG_ON(mm->task_size == 0);
  387. BUG_ON(mm->context.slb_addr_limit == 0);
  388. VM_BUG_ON(radix_enabled());
  389. slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
  390. slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
  391. addr, len, flags, topdown);
  392. /* If hint, make sure it matches our alignment restrictions */
  393. if (!fixed && addr) {
  394. addr = _ALIGN_UP(addr, page_size);
  395. slice_dbg(" aligned addr=%lx\n", addr);
  396. /* Ignore hint if it's too large or overlaps a VMA */
  397. if (addr > high_limit - len ||
  398. !slice_area_is_free(mm, addr, len))
  399. addr = 0;
  400. }
  401. /* First make up a "good" mask of slices that have the right size
  402. * already
  403. */
  404. slice_mask_for_size(mm, psize, &good_mask, high_limit);
  405. slice_print_mask(" good_mask", good_mask);
  406. /*
  407. * Here "good" means slices that are already the right page size,
  408. * "compat" means slices that have a compatible page size (i.e.
  409. * 4k in a 64k pagesize kernel), and "free" means slices without
  410. * any VMAs.
  411. *
  412. * If MAP_FIXED:
  413. * check if fits in good | compat => OK
  414. * check if fits in good | compat | free => convert free
  415. * else bad
  416. * If have hint:
  417. * check if hint fits in good => OK
  418. * check if hint fits in good | free => convert free
  419. * Otherwise:
  420. * search in good, found => OK
  421. * search in good | free, found => convert free
  422. * search in good | compat | free, found => convert free.
  423. */
  424. #ifdef CONFIG_PPC_64K_PAGES
  425. /* If we support combo pages, we can allow 64k pages in 4k slices */
  426. if (psize == MMU_PAGE_64K) {
  427. slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
  428. if (fixed)
  429. slice_or_mask(&good_mask, &compat_mask);
  430. }
  431. #endif
  432. /* First check hint if it's valid or if we have MAP_FIXED */
  433. if (addr != 0 || fixed) {
  434. /* Build a mask for the requested range */
  435. slice_range_to_mask(addr, len, &mask);
  436. slice_print_mask(" mask", mask);
  437. /* Check if we fit in the good mask. If we do, we just return,
  438. * nothing else to do
  439. */
  440. if (slice_check_fit(mm, mask, good_mask)) {
  441. slice_dbg(" fits good !\n");
  442. return addr;
  443. }
  444. } else {
  445. /* Now let's see if we can find something in the existing
  446. * slices for that size
  447. */
  448. newaddr = slice_find_area(mm, len, good_mask,
  449. psize, topdown, high_limit);
  450. if (newaddr != -ENOMEM) {
  451. /* Found within the good mask, we don't have to setup,
  452. * we thus return directly
  453. */
  454. slice_dbg(" found area at 0x%lx\n", newaddr);
  455. return newaddr;
  456. }
  457. }
  458. /*
  459. * We don't fit in the good mask, check what other slices are
  460. * empty and thus can be converted
  461. */
  462. slice_mask_for_free(mm, &potential_mask, high_limit);
  463. slice_or_mask(&potential_mask, &good_mask);
  464. slice_print_mask(" potential", potential_mask);
  465. if ((addr != 0 || fixed) && slice_check_fit(mm, mask, potential_mask)) {
  466. slice_dbg(" fits potential !\n");
  467. goto convert;
  468. }
  469. /* If we have MAP_FIXED and failed the above steps, then error out */
  470. if (fixed)
  471. return -EBUSY;
  472. slice_dbg(" search...\n");
  473. /* If we had a hint that didn't work out, see if we can fit
  474. * anywhere in the good area.
  475. */
  476. if (addr) {
  477. addr = slice_find_area(mm, len, good_mask,
  478. psize, topdown, high_limit);
  479. if (addr != -ENOMEM) {
  480. slice_dbg(" found area at 0x%lx\n", addr);
  481. return addr;
  482. }
  483. }
  484. /* Now let's see if we can find something in the existing slices
  485. * for that size plus free slices
  486. */
  487. addr = slice_find_area(mm, len, potential_mask,
  488. psize, topdown, high_limit);
  489. #ifdef CONFIG_PPC_64K_PAGES
  490. if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
  491. /* retry the search with 4k-page slices included */
  492. slice_or_mask(&potential_mask, &compat_mask);
  493. addr = slice_find_area(mm, len, potential_mask,
  494. psize, topdown, high_limit);
  495. }
  496. #endif
  497. if (addr == -ENOMEM)
  498. return -ENOMEM;
  499. slice_range_to_mask(addr, len, &mask);
  500. slice_dbg(" found potential area at 0x%lx\n", addr);
  501. slice_print_mask(" mask", mask);
  502. convert:
  503. slice_andnot_mask(&mask, &good_mask);
  504. slice_andnot_mask(&mask, &compat_mask);
  505. if (mask.low_slices || !bitmap_empty(mask.high_slices, SLICE_NUM_HIGH)) {
  506. slice_convert(mm, mask, psize);
  507. if (psize > MMU_PAGE_BASE)
  508. on_each_cpu(slice_flush_segments, mm, 1);
  509. }
  510. return addr;
  511. }
  512. EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
  513. unsigned long arch_get_unmapped_area(struct file *filp,
  514. unsigned long addr,
  515. unsigned long len,
  516. unsigned long pgoff,
  517. unsigned long flags)
  518. {
  519. return slice_get_unmapped_area(addr, len, flags,
  520. current->mm->context.user_psize, 0);
  521. }
  522. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  523. const unsigned long addr0,
  524. const unsigned long len,
  525. const unsigned long pgoff,
  526. const unsigned long flags)
  527. {
  528. return slice_get_unmapped_area(addr0, len, flags,
  529. current->mm->context.user_psize, 1);
  530. }
  531. unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
  532. {
  533. unsigned char *hpsizes;
  534. int index, mask_index;
  535. /*
  536. * Radix doesn't use slice, but can get enabled along with MMU_SLICE
  537. */
  538. if (radix_enabled()) {
  539. #ifdef CONFIG_PPC_64K_PAGES
  540. return MMU_PAGE_64K;
  541. #else
  542. return MMU_PAGE_4K;
  543. #endif
  544. }
  545. if (addr < SLICE_LOW_TOP) {
  546. u64 lpsizes;
  547. lpsizes = mm->context.low_slices_psize;
  548. index = GET_LOW_SLICE_INDEX(addr);
  549. return (lpsizes >> (index * 4)) & 0xf;
  550. }
  551. hpsizes = mm->context.high_slices_psize;
  552. index = GET_HIGH_SLICE_INDEX(addr);
  553. mask_index = index & 0x1;
  554. return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xf;
  555. }
  556. EXPORT_SYMBOL_GPL(get_slice_psize);
  557. /*
  558. * This is called by hash_page when it needs to do a lazy conversion of
  559. * an address space from real 64K pages to combo 4K pages (typically
  560. * when hitting a non cacheable mapping on a processor or hypervisor
  561. * that won't allow them for 64K pages).
  562. *
  563. * This is also called in init_new_context() to change back the user
  564. * psize from whatever the parent context had it set to
  565. * N.B. This may be called before mm->context.id has been set.
  566. *
  567. * This function will only change the content of the {low,high)_slice_psize
  568. * masks, it will not flush SLBs as this shall be handled lazily by the
  569. * caller.
  570. */
  571. void slice_set_user_psize(struct mm_struct *mm, unsigned int psize)
  572. {
  573. int index, mask_index;
  574. unsigned char *hpsizes;
  575. unsigned long flags, lpsizes;
  576. unsigned int old_psize;
  577. int i;
  578. slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize);
  579. VM_BUG_ON(radix_enabled());
  580. spin_lock_irqsave(&slice_convert_lock, flags);
  581. old_psize = mm->context.user_psize;
  582. slice_dbg(" old_psize=%d\n", old_psize);
  583. if (old_psize == psize)
  584. goto bail;
  585. mm->context.user_psize = psize;
  586. wmb();
  587. lpsizes = mm->context.low_slices_psize;
  588. for (i = 0; i < SLICE_NUM_LOW; i++)
  589. if (((lpsizes >> (i * 4)) & 0xf) == old_psize)
  590. lpsizes = (lpsizes & ~(0xful << (i * 4))) |
  591. (((unsigned long)psize) << (i * 4));
  592. /* Assign the value back */
  593. mm->context.low_slices_psize = lpsizes;
  594. hpsizes = mm->context.high_slices_psize;
  595. for (i = 0; i < SLICE_NUM_HIGH; i++) {
  596. mask_index = i & 0x1;
  597. index = i >> 1;
  598. if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == old_psize)
  599. hpsizes[index] = (hpsizes[index] &
  600. ~(0xf << (mask_index * 4))) |
  601. (((unsigned long)psize) << (mask_index * 4));
  602. }
  603. slice_dbg(" lsps=%lx, hsps=%lx\n",
  604. (unsigned long)mm->context.low_slices_psize,
  605. (unsigned long)mm->context.high_slices_psize);
  606. bail:
  607. spin_unlock_irqrestore(&slice_convert_lock, flags);
  608. }
  609. void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
  610. unsigned long len, unsigned int psize)
  611. {
  612. struct slice_mask mask;
  613. VM_BUG_ON(radix_enabled());
  614. slice_range_to_mask(start, len, &mask);
  615. slice_convert(mm, mask, psize);
  616. }
  617. #ifdef CONFIG_HUGETLB_PAGE
  618. /*
  619. * is_hugepage_only_range() is used by generic code to verify whether
  620. * a normal mmap mapping (non hugetlbfs) is valid on a given area.
  621. *
  622. * until the generic code provides a more generic hook and/or starts
  623. * calling arch get_unmapped_area for MAP_FIXED (which our implementation
  624. * here knows how to deal with), we hijack it to keep standard mappings
  625. * away from us.
  626. *
  627. * because of that generic code limitation, MAP_FIXED mapping cannot
  628. * "convert" back a slice with no VMAs to the standard page size, only
  629. * get_unmapped_area() can. It would be possible to fix it here but I
  630. * prefer working on fixing the generic code instead.
  631. *
  632. * WARNING: This will not work if hugetlbfs isn't enabled since the
  633. * generic code will redefine that function as 0 in that. This is ok
  634. * for now as we only use slices with hugetlbfs enabled. This should
  635. * be fixed as the generic code gets fixed.
  636. */
  637. int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
  638. unsigned long len)
  639. {
  640. struct slice_mask mask, available;
  641. unsigned int psize = mm->context.user_psize;
  642. unsigned long high_limit = mm->context.slb_addr_limit;
  643. if (radix_enabled())
  644. return 0;
  645. slice_range_to_mask(addr, len, &mask);
  646. slice_mask_for_size(mm, psize, &available, high_limit);
  647. #ifdef CONFIG_PPC_64K_PAGES
  648. /* We need to account for 4k slices too */
  649. if (psize == MMU_PAGE_64K) {
  650. struct slice_mask compat_mask;
  651. slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
  652. slice_or_mask(&available, &compat_mask);
  653. }
  654. #endif
  655. #if 0 /* too verbose */
  656. slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
  657. mm, addr, len);
  658. slice_print_mask(" mask", mask);
  659. slice_print_mask(" available", available);
  660. #endif
  661. return !slice_check_fit(mm, mask, available);
  662. }
  663. #endif