Merge tag 'soc-fsl-fix-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/leo...
[linux-2.6-block.git] / arch / powerpc / mm / slice.c
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>
32 #include <linux/export.h>
33 #include <linux/hugetlb.h>
34 #include <linux/sched/mm.h>
35 #include <linux/security.h>
36 #include <asm/mman.h>
37 #include <asm/mmu.h>
38 #include <asm/copro.h>
39 #include <asm/hugetlb.h>
40 #include <asm/mmu_context.h>
41
42 static DEFINE_SPINLOCK(slice_convert_lock);
43
44 #ifdef DEBUG
45 int _slice_debug = 1;
46
47 static void slice_print_mask(const char *label, const struct slice_mask *mask)
48 {
49         if (!_slice_debug)
50                 return;
51         pr_devel("%s low_slice: %*pbl\n", label,
52                         (int)SLICE_NUM_LOW, &mask->low_slices);
53         pr_devel("%s high_slice: %*pbl\n", label,
54                         (int)SLICE_NUM_HIGH, mask->high_slices);
55 }
56
57 #define slice_dbg(fmt...) do { if (_slice_debug) pr_devel(fmt); } while (0)
58
59 #else
60
61 static void slice_print_mask(const char *label, const struct slice_mask *mask) {}
62 #define slice_dbg(fmt...)
63
64 #endif
65
66 static inline bool slice_addr_is_low(unsigned long addr)
67 {
68         u64 tmp = (u64)addr;
69
70         return tmp < SLICE_LOW_TOP;
71 }
72
73 static void slice_range_to_mask(unsigned long start, unsigned long len,
74                                 struct slice_mask *ret)
75 {
76         unsigned long end = start + len - 1;
77
78         ret->low_slices = 0;
79         if (SLICE_NUM_HIGH)
80                 bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
81
82         if (slice_addr_is_low(start)) {
83                 unsigned long mend = min(end,
84                                          (unsigned long)(SLICE_LOW_TOP - 1));
85
86                 ret->low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
87                         - (1u << GET_LOW_SLICE_INDEX(start));
88         }
89
90         if (SLICE_NUM_HIGH && !slice_addr_is_low(end)) {
91                 unsigned long start_index = GET_HIGH_SLICE_INDEX(start);
92                 unsigned long align_end = ALIGN(end, (1UL << SLICE_HIGH_SHIFT));
93                 unsigned long count = GET_HIGH_SLICE_INDEX(align_end) - start_index;
94
95                 bitmap_set(ret->high_slices, start_index, count);
96         }
97 }
98
99 static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
100                               unsigned long len)
101 {
102         struct vm_area_struct *vma;
103
104         if ((mm_ctx_slb_addr_limit(&mm->context) - len) < addr)
105                 return 0;
106         vma = find_vma(mm, addr);
107         return (!vma || (addr + len) <= vm_start_gap(vma));
108 }
109
110 static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
111 {
112         return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
113                                    1ul << SLICE_LOW_SHIFT);
114 }
115
116 static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
117 {
118         unsigned long start = slice << SLICE_HIGH_SHIFT;
119         unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
120
121         /* Hack, so that each addresses is controlled by exactly one
122          * of the high or low area bitmaps, the first high area starts
123          * at 4GB, not 0 */
124         if (start == 0)
125                 start = (unsigned long)SLICE_LOW_TOP;
126
127         return !slice_area_is_free(mm, start, end - start);
128 }
129
130 static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret,
131                                 unsigned long high_limit)
132 {
133         unsigned long i;
134
135         ret->low_slices = 0;
136         if (SLICE_NUM_HIGH)
137                 bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
138
139         for (i = 0; i < SLICE_NUM_LOW; i++)
140                 if (!slice_low_has_vma(mm, i))
141                         ret->low_slices |= 1u << i;
142
143         if (slice_addr_is_low(high_limit - 1))
144                 return;
145
146         for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++)
147                 if (!slice_high_has_vma(mm, i))
148                         __set_bit(i, ret->high_slices);
149 }
150
151 static bool slice_check_range_fits(struct mm_struct *mm,
152                            const struct slice_mask *available,
153                            unsigned long start, unsigned long len)
154 {
155         unsigned long end = start + len - 1;
156         u64 low_slices = 0;
157
158         if (slice_addr_is_low(start)) {
159                 unsigned long mend = min(end,
160                                          (unsigned long)(SLICE_LOW_TOP - 1));
161
162                 low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
163                                 - (1u << GET_LOW_SLICE_INDEX(start));
164         }
165         if ((low_slices & available->low_slices) != low_slices)
166                 return false;
167
168         if (SLICE_NUM_HIGH && !slice_addr_is_low(end)) {
169                 unsigned long start_index = GET_HIGH_SLICE_INDEX(start);
170                 unsigned long align_end = ALIGN(end, (1UL << SLICE_HIGH_SHIFT));
171                 unsigned long count = GET_HIGH_SLICE_INDEX(align_end) - start_index;
172                 unsigned long i;
173
174                 for (i = start_index; i < start_index + count; i++) {
175                         if (!test_bit(i, available->high_slices))
176                                 return false;
177                 }
178         }
179
180         return true;
181 }
182
183 static void slice_flush_segments(void *parm)
184 {
185 #ifdef CONFIG_PPC64
186         struct mm_struct *mm = parm;
187         unsigned long flags;
188
189         if (mm != current->active_mm)
190                 return;
191
192         copy_mm_to_paca(current->active_mm);
193
194         local_irq_save(flags);
195         slb_flush_and_restore_bolted();
196         local_irq_restore(flags);
197 #endif
198 }
199
200 static void slice_convert(struct mm_struct *mm,
201                                 const struct slice_mask *mask, int psize)
202 {
203         int index, mask_index;
204         /* Write the new slice psize bits */
205         unsigned char *hpsizes, *lpsizes;
206         struct slice_mask *psize_mask, *old_mask;
207         unsigned long i, flags;
208         int old_psize;
209
210         slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
211         slice_print_mask(" mask", mask);
212
213         psize_mask = slice_mask_for_size(&mm->context, psize);
214
215         /* We need to use a spinlock here to protect against
216          * concurrent 64k -> 4k demotion ...
217          */
218         spin_lock_irqsave(&slice_convert_lock, flags);
219
220         lpsizes = mm_ctx_low_slices(&mm->context);
221         for (i = 0; i < SLICE_NUM_LOW; i++) {
222                 if (!(mask->low_slices & (1u << i)))
223                         continue;
224
225                 mask_index = i & 0x1;
226                 index = i >> 1;
227
228                 /* Update the slice_mask */
229                 old_psize = (lpsizes[index] >> (mask_index * 4)) & 0xf;
230                 old_mask = slice_mask_for_size(&mm->context, old_psize);
231                 old_mask->low_slices &= ~(1u << i);
232                 psize_mask->low_slices |= 1u << i;
233
234                 /* Update the sizes array */
235                 lpsizes[index] = (lpsizes[index] & ~(0xf << (mask_index * 4))) |
236                                 (((unsigned long)psize) << (mask_index * 4));
237         }
238
239         hpsizes = mm_ctx_high_slices(&mm->context);
240         for (i = 0; i < GET_HIGH_SLICE_INDEX(mm_ctx_slb_addr_limit(&mm->context)); i++) {
241                 if (!test_bit(i, mask->high_slices))
242                         continue;
243
244                 mask_index = i & 0x1;
245                 index = i >> 1;
246
247                 /* Update the slice_mask */
248                 old_psize = (hpsizes[index] >> (mask_index * 4)) & 0xf;
249                 old_mask = slice_mask_for_size(&mm->context, old_psize);
250                 __clear_bit(i, old_mask->high_slices);
251                 __set_bit(i, psize_mask->high_slices);
252
253                 /* Update the sizes array */
254                 hpsizes[index] = (hpsizes[index] & ~(0xf << (mask_index * 4))) |
255                                 (((unsigned long)psize) << (mask_index * 4));
256         }
257
258         slice_dbg(" lsps=%lx, hsps=%lx\n",
259                   (unsigned long)mm_ctx_low_slices(&mm->context),
260                   (unsigned long)mm_ctx_high_slices(&mm->context));
261
262         spin_unlock_irqrestore(&slice_convert_lock, flags);
263
264         copro_flush_all_slbs(mm);
265 }
266
267 /*
268  * Compute which slice addr is part of;
269  * set *boundary_addr to the start or end boundary of that slice
270  * (depending on 'end' parameter);
271  * return boolean indicating if the slice is marked as available in the
272  * 'available' slice_mark.
273  */
274 static bool slice_scan_available(unsigned long addr,
275                                  const struct slice_mask *available,
276                                  int end, unsigned long *boundary_addr)
277 {
278         unsigned long slice;
279         if (slice_addr_is_low(addr)) {
280                 slice = GET_LOW_SLICE_INDEX(addr);
281                 *boundary_addr = (slice + end) << SLICE_LOW_SHIFT;
282                 return !!(available->low_slices & (1u << slice));
283         } else {
284                 slice = GET_HIGH_SLICE_INDEX(addr);
285                 *boundary_addr = (slice + end) ?
286                         ((slice + end) << SLICE_HIGH_SHIFT) : SLICE_LOW_TOP;
287                 return !!test_bit(slice, available->high_slices);
288         }
289 }
290
291 static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
292                                               unsigned long len,
293                                               const struct slice_mask *available,
294                                               int psize, unsigned long high_limit)
295 {
296         int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
297         unsigned long addr, found, next_end;
298         struct vm_unmapped_area_info info;
299
300         info.flags = 0;
301         info.length = len;
302         info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
303         info.align_offset = 0;
304
305         addr = TASK_UNMAPPED_BASE;
306         /*
307          * Check till the allow max value for this mmap request
308          */
309         while (addr < high_limit) {
310                 info.low_limit = addr;
311                 if (!slice_scan_available(addr, available, 1, &addr))
312                         continue;
313
314  next_slice:
315                 /*
316                  * At this point [info.low_limit; addr) covers
317                  * available slices only and ends at a slice boundary.
318                  * Check if we need to reduce the range, or if we can
319                  * extend it to cover the next available slice.
320                  */
321                 if (addr >= high_limit)
322                         addr = high_limit;
323                 else if (slice_scan_available(addr, available, 1, &next_end)) {
324                         addr = next_end;
325                         goto next_slice;
326                 }
327                 info.high_limit = addr;
328
329                 found = vm_unmapped_area(&info);
330                 if (!(found & ~PAGE_MASK))
331                         return found;
332         }
333
334         return -ENOMEM;
335 }
336
337 static unsigned long slice_find_area_topdown(struct mm_struct *mm,
338                                              unsigned long len,
339                                              const struct slice_mask *available,
340                                              int psize, unsigned long high_limit)
341 {
342         int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
343         unsigned long addr, found, prev;
344         struct vm_unmapped_area_info info;
345         unsigned long min_addr = max(PAGE_SIZE, mmap_min_addr);
346
347         info.flags = VM_UNMAPPED_AREA_TOPDOWN;
348         info.length = len;
349         info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
350         info.align_offset = 0;
351
352         addr = mm->mmap_base;
353         /*
354          * If we are trying to allocate above DEFAULT_MAP_WINDOW
355          * Add the different to the mmap_base.
356          * Only for that request for which high_limit is above
357          * DEFAULT_MAP_WINDOW we should apply this.
358          */
359         if (high_limit > DEFAULT_MAP_WINDOW)
360                 addr += mm_ctx_slb_addr_limit(&mm->context) - DEFAULT_MAP_WINDOW;
361
362         while (addr > min_addr) {
363                 info.high_limit = addr;
364                 if (!slice_scan_available(addr - 1, available, 0, &addr))
365                         continue;
366
367  prev_slice:
368                 /*
369                  * At this point [addr; info.high_limit) covers
370                  * available slices only and starts at a slice boundary.
371                  * Check if we need to reduce the range, or if we can
372                  * extend it to cover the previous available slice.
373                  */
374                 if (addr < min_addr)
375                         addr = min_addr;
376                 else if (slice_scan_available(addr - 1, available, 0, &prev)) {
377                         addr = prev;
378                         goto prev_slice;
379                 }
380                 info.low_limit = addr;
381
382                 found = vm_unmapped_area(&info);
383                 if (!(found & ~PAGE_MASK))
384                         return found;
385         }
386
387         /*
388          * A failed mmap() very likely causes application failure,
389          * so fall back to the bottom-up function here. This scenario
390          * can happen with large stack limits and large mmap()
391          * allocations.
392          */
393         return slice_find_area_bottomup(mm, len, available, psize, high_limit);
394 }
395
396
397 static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
398                                      const struct slice_mask *mask, int psize,
399                                      int topdown, unsigned long high_limit)
400 {
401         if (topdown)
402                 return slice_find_area_topdown(mm, len, mask, psize, high_limit);
403         else
404                 return slice_find_area_bottomup(mm, len, mask, psize, high_limit);
405 }
406
407 static inline void slice_copy_mask(struct slice_mask *dst,
408                                         const struct slice_mask *src)
409 {
410         dst->low_slices = src->low_slices;
411         if (!SLICE_NUM_HIGH)
412                 return;
413         bitmap_copy(dst->high_slices, src->high_slices, SLICE_NUM_HIGH);
414 }
415
416 static inline void slice_or_mask(struct slice_mask *dst,
417                                         const struct slice_mask *src1,
418                                         const struct slice_mask *src2)
419 {
420         dst->low_slices = src1->low_slices | src2->low_slices;
421         if (!SLICE_NUM_HIGH)
422                 return;
423         bitmap_or(dst->high_slices, src1->high_slices, src2->high_slices, SLICE_NUM_HIGH);
424 }
425
426 static inline void slice_andnot_mask(struct slice_mask *dst,
427                                         const struct slice_mask *src1,
428                                         const struct slice_mask *src2)
429 {
430         dst->low_slices = src1->low_slices & ~src2->low_slices;
431         if (!SLICE_NUM_HIGH)
432                 return;
433         bitmap_andnot(dst->high_slices, src1->high_slices, src2->high_slices, SLICE_NUM_HIGH);
434 }
435
436 #ifdef CONFIG_PPC_64K_PAGES
437 #define MMU_PAGE_BASE   MMU_PAGE_64K
438 #else
439 #define MMU_PAGE_BASE   MMU_PAGE_4K
440 #endif
441
442 unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
443                                       unsigned long flags, unsigned int psize,
444                                       int topdown)
445 {
446         struct slice_mask good_mask;
447         struct slice_mask potential_mask;
448         const struct slice_mask *maskp;
449         const struct slice_mask *compat_maskp = NULL;
450         int fixed = (flags & MAP_FIXED);
451         int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
452         unsigned long page_size = 1UL << pshift;
453         struct mm_struct *mm = current->mm;
454         unsigned long newaddr;
455         unsigned long high_limit;
456
457         high_limit = DEFAULT_MAP_WINDOW;
458         if (addr >= high_limit || (fixed && (addr + len > high_limit)))
459                 high_limit = TASK_SIZE;
460
461         if (len > high_limit)
462                 return -ENOMEM;
463         if (len & (page_size - 1))
464                 return -EINVAL;
465         if (fixed) {
466                 if (addr & (page_size - 1))
467                         return -EINVAL;
468                 if (addr > high_limit - len)
469                         return -ENOMEM;
470         }
471
472         if (high_limit > mm_ctx_slb_addr_limit(&mm->context)) {
473                 /*
474                  * Increasing the slb_addr_limit does not require
475                  * slice mask cache to be recalculated because it should
476                  * be already initialised beyond the old address limit.
477                  */
478                 mm_ctx_set_slb_addr_limit(&mm->context, high_limit);
479
480                 on_each_cpu(slice_flush_segments, mm, 1);
481         }
482
483         /* Sanity checks */
484         BUG_ON(mm->task_size == 0);
485         BUG_ON(mm_ctx_slb_addr_limit(&mm->context) == 0);
486         VM_BUG_ON(radix_enabled());
487
488         slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
489         slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
490                   addr, len, flags, topdown);
491
492         /* If hint, make sure it matches our alignment restrictions */
493         if (!fixed && addr) {
494                 addr = _ALIGN_UP(addr, page_size);
495                 slice_dbg(" aligned addr=%lx\n", addr);
496                 /* Ignore hint if it's too large or overlaps a VMA */
497                 if (addr > high_limit - len || addr < mmap_min_addr ||
498                     !slice_area_is_free(mm, addr, len))
499                         addr = 0;
500         }
501
502         /* First make up a "good" mask of slices that have the right size
503          * already
504          */
505         maskp = slice_mask_for_size(&mm->context, psize);
506
507         /*
508          * Here "good" means slices that are already the right page size,
509          * "compat" means slices that have a compatible page size (i.e.
510          * 4k in a 64k pagesize kernel), and "free" means slices without
511          * any VMAs.
512          *
513          * If MAP_FIXED:
514          *      check if fits in good | compat => OK
515          *      check if fits in good | compat | free => convert free
516          *      else bad
517          * If have hint:
518          *      check if hint fits in good => OK
519          *      check if hint fits in good | free => convert free
520          * Otherwise:
521          *      search in good, found => OK
522          *      search in good | free, found => convert free
523          *      search in good | compat | free, found => convert free.
524          */
525
526         /*
527          * If we support combo pages, we can allow 64k pages in 4k slices
528          * The mask copies could be avoided in most cases here if we had
529          * a pointer to good mask for the next code to use.
530          */
531         if (IS_ENABLED(CONFIG_PPC_64K_PAGES) && psize == MMU_PAGE_64K) {
532                 compat_maskp = slice_mask_for_size(&mm->context, MMU_PAGE_4K);
533                 if (fixed)
534                         slice_or_mask(&good_mask, maskp, compat_maskp);
535                 else
536                         slice_copy_mask(&good_mask, maskp);
537         } else {
538                 slice_copy_mask(&good_mask, maskp);
539         }
540
541         slice_print_mask(" good_mask", &good_mask);
542         if (compat_maskp)
543                 slice_print_mask(" compat_mask", compat_maskp);
544
545         /* First check hint if it's valid or if we have MAP_FIXED */
546         if (addr != 0 || fixed) {
547                 /* Check if we fit in the good mask. If we do, we just return,
548                  * nothing else to do
549                  */
550                 if (slice_check_range_fits(mm, &good_mask, addr, len)) {
551                         slice_dbg(" fits good !\n");
552                         newaddr = addr;
553                         goto return_addr;
554                 }
555         } else {
556                 /* Now let's see if we can find something in the existing
557                  * slices for that size
558                  */
559                 newaddr = slice_find_area(mm, len, &good_mask,
560                                           psize, topdown, high_limit);
561                 if (newaddr != -ENOMEM) {
562                         /* Found within the good mask, we don't have to setup,
563                          * we thus return directly
564                          */
565                         slice_dbg(" found area at 0x%lx\n", newaddr);
566                         goto return_addr;
567                 }
568         }
569         /*
570          * We don't fit in the good mask, check what other slices are
571          * empty and thus can be converted
572          */
573         slice_mask_for_free(mm, &potential_mask, high_limit);
574         slice_or_mask(&potential_mask, &potential_mask, &good_mask);
575         slice_print_mask(" potential", &potential_mask);
576
577         if (addr != 0 || fixed) {
578                 if (slice_check_range_fits(mm, &potential_mask, addr, len)) {
579                         slice_dbg(" fits potential !\n");
580                         newaddr = addr;
581                         goto convert;
582                 }
583         }
584
585         /* If we have MAP_FIXED and failed the above steps, then error out */
586         if (fixed)
587                 return -EBUSY;
588
589         slice_dbg(" search...\n");
590
591         /* If we had a hint that didn't work out, see if we can fit
592          * anywhere in the good area.
593          */
594         if (addr) {
595                 newaddr = slice_find_area(mm, len, &good_mask,
596                                           psize, topdown, high_limit);
597                 if (newaddr != -ENOMEM) {
598                         slice_dbg(" found area at 0x%lx\n", newaddr);
599                         goto return_addr;
600                 }
601         }
602
603         /* Now let's see if we can find something in the existing slices
604          * for that size plus free slices
605          */
606         newaddr = slice_find_area(mm, len, &potential_mask,
607                                   psize, topdown, high_limit);
608
609         if (IS_ENABLED(CONFIG_PPC_64K_PAGES) && newaddr == -ENOMEM &&
610             psize == MMU_PAGE_64K) {
611                 /* retry the search with 4k-page slices included */
612                 slice_or_mask(&potential_mask, &potential_mask, compat_maskp);
613                 newaddr = slice_find_area(mm, len, &potential_mask,
614                                           psize, topdown, high_limit);
615         }
616
617         if (newaddr == -ENOMEM)
618                 return -ENOMEM;
619
620         slice_range_to_mask(newaddr, len, &potential_mask);
621         slice_dbg(" found potential area at 0x%lx\n", newaddr);
622         slice_print_mask(" mask", &potential_mask);
623
624  convert:
625         /*
626          * Try to allocate the context before we do slice convert
627          * so that we handle the context allocation failure gracefully.
628          */
629         if (need_extra_context(mm, newaddr)) {
630                 if (alloc_extended_context(mm, newaddr) < 0)
631                         return -ENOMEM;
632         }
633
634         slice_andnot_mask(&potential_mask, &potential_mask, &good_mask);
635         if (compat_maskp && !fixed)
636                 slice_andnot_mask(&potential_mask, &potential_mask, compat_maskp);
637         if (potential_mask.low_slices ||
638                 (SLICE_NUM_HIGH &&
639                  !bitmap_empty(potential_mask.high_slices, SLICE_NUM_HIGH))) {
640                 slice_convert(mm, &potential_mask, psize);
641                 if (psize > MMU_PAGE_BASE)
642                         on_each_cpu(slice_flush_segments, mm, 1);
643         }
644         return newaddr;
645
646 return_addr:
647         if (need_extra_context(mm, newaddr)) {
648                 if (alloc_extended_context(mm, newaddr) < 0)
649                         return -ENOMEM;
650         }
651         return newaddr;
652 }
653 EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
654
655 unsigned long arch_get_unmapped_area(struct file *filp,
656                                      unsigned long addr,
657                                      unsigned long len,
658                                      unsigned long pgoff,
659                                      unsigned long flags)
660 {
661         return slice_get_unmapped_area(addr, len, flags,
662                                        mm_ctx_user_psize(&current->mm->context), 0);
663 }
664
665 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
666                                              const unsigned long addr0,
667                                              const unsigned long len,
668                                              const unsigned long pgoff,
669                                              const unsigned long flags)
670 {
671         return slice_get_unmapped_area(addr0, len, flags,
672                                        mm_ctx_user_psize(&current->mm->context), 1);
673 }
674
675 unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
676 {
677         unsigned char *psizes;
678         int index, mask_index;
679
680         VM_BUG_ON(radix_enabled());
681
682         if (slice_addr_is_low(addr)) {
683                 psizes = mm_ctx_low_slices(&mm->context);
684                 index = GET_LOW_SLICE_INDEX(addr);
685         } else {
686                 psizes = mm_ctx_high_slices(&mm->context);
687                 index = GET_HIGH_SLICE_INDEX(addr);
688         }
689         mask_index = index & 0x1;
690         return (psizes[index >> 1] >> (mask_index * 4)) & 0xf;
691 }
692 EXPORT_SYMBOL_GPL(get_slice_psize);
693
694 void slice_init_new_context_exec(struct mm_struct *mm)
695 {
696         unsigned char *hpsizes, *lpsizes;
697         struct slice_mask *mask;
698         unsigned int psize = mmu_virtual_psize;
699
700         slice_dbg("slice_init_new_context_exec(mm=%p)\n", mm);
701
702         /*
703          * In the case of exec, use the default limit. In the
704          * case of fork it is just inherited from the mm being
705          * duplicated.
706          */
707         mm_ctx_set_slb_addr_limit(&mm->context, SLB_ADDR_LIMIT_DEFAULT);
708         mm_ctx_set_user_psize(&mm->context, psize);
709
710         /*
711          * Set all slice psizes to the default.
712          */
713         lpsizes = mm_ctx_low_slices(&mm->context);
714         memset(lpsizes, (psize << 4) | psize, SLICE_NUM_LOW >> 1);
715
716         hpsizes = mm_ctx_high_slices(&mm->context);
717         memset(hpsizes, (psize << 4) | psize, SLICE_NUM_HIGH >> 1);
718
719         /*
720          * Slice mask cache starts zeroed, fill the default size cache.
721          */
722         mask = slice_mask_for_size(&mm->context, psize);
723         mask->low_slices = ~0UL;
724         if (SLICE_NUM_HIGH)
725                 bitmap_fill(mask->high_slices, SLICE_NUM_HIGH);
726 }
727
728 #ifdef CONFIG_PPC_BOOK3S_64
729 void slice_setup_new_exec(void)
730 {
731         struct mm_struct *mm = current->mm;
732
733         slice_dbg("slice_setup_new_exec(mm=%p)\n", mm);
734
735         if (!is_32bit_task())
736                 return;
737
738         mm_ctx_set_slb_addr_limit(&mm->context, DEFAULT_MAP_WINDOW);
739 }
740 #endif
741
742 void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
743                            unsigned long len, unsigned int psize)
744 {
745         struct slice_mask mask;
746
747         VM_BUG_ON(radix_enabled());
748
749         slice_range_to_mask(start, len, &mask);
750         slice_convert(mm, &mask, psize);
751 }
752
753 #ifdef CONFIG_HUGETLB_PAGE
754 /*
755  * is_hugepage_only_range() is used by generic code to verify whether
756  * a normal mmap mapping (non hugetlbfs) is valid on a given area.
757  *
758  * until the generic code provides a more generic hook and/or starts
759  * calling arch get_unmapped_area for MAP_FIXED (which our implementation
760  * here knows how to deal with), we hijack it to keep standard mappings
761  * away from us.
762  *
763  * because of that generic code limitation, MAP_FIXED mapping cannot
764  * "convert" back a slice with no VMAs to the standard page size, only
765  * get_unmapped_area() can. It would be possible to fix it here but I
766  * prefer working on fixing the generic code instead.
767  *
768  * WARNING: This will not work if hugetlbfs isn't enabled since the
769  * generic code will redefine that function as 0 in that. This is ok
770  * for now as we only use slices with hugetlbfs enabled. This should
771  * be fixed as the generic code gets fixed.
772  */
773 int slice_is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
774                            unsigned long len)
775 {
776         const struct slice_mask *maskp;
777         unsigned int psize = mm_ctx_user_psize(&mm->context);
778
779         VM_BUG_ON(radix_enabled());
780
781         maskp = slice_mask_for_size(&mm->context, psize);
782
783         /* We need to account for 4k slices too */
784         if (IS_ENABLED(CONFIG_PPC_64K_PAGES) && psize == MMU_PAGE_64K) {
785                 const struct slice_mask *compat_maskp;
786                 struct slice_mask available;
787
788                 compat_maskp = slice_mask_for_size(&mm->context, MMU_PAGE_4K);
789                 slice_or_mask(&available, maskp, compat_maskp);
790                 return !slice_check_range_fits(mm, &available, addr, len);
791         }
792
793         return !slice_check_range_fits(mm, maskp, addr, len);
794 }
795 #endif