2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2011 Wind River Systems,
7 * written by Ralf Baechle <ralf@linux-mips.org>
9 #include <linux/compiler.h>
10 #include <linux/errno.h>
12 #include <linux/mman.h>
13 #include <linux/export.h>
14 #include <linux/personality.h>
15 #include <linux/random.h>
16 #include <linux/sched/signal.h>
17 #include <linux/sched/mm.h>
19 unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
20 EXPORT_SYMBOL(shm_align_mask);
22 /* gap between mmap and stack */
23 #define MIN_GAP (128*1024*1024UL)
24 #define MAX_GAP ((TASK_SIZE)/6*5)
26 static int mmap_is_legacy(void)
28 if (current->personality & ADDR_COMPAT_LAYOUT)
31 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
34 return sysctl_legacy_va_layout;
37 static unsigned long mmap_base(unsigned long rnd)
39 unsigned long gap = rlimit(RLIMIT_STACK);
43 else if (gap > MAX_GAP)
46 return PAGE_ALIGN(TASK_SIZE - gap - rnd);
49 #define COLOUR_ALIGN(addr, pgoff) \
50 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
51 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
53 enum mmap_allocation_direction {UP, DOWN};
55 static unsigned long arch_get_unmapped_area_common(struct file *filp,
56 unsigned long addr0, unsigned long len, unsigned long pgoff,
57 unsigned long flags, enum mmap_allocation_direction dir)
59 struct mm_struct *mm = current->mm;
60 struct vm_area_struct *vma;
61 unsigned long addr = addr0;
63 struct vm_unmapped_area_info info;
65 if (unlikely(len > TASK_SIZE))
68 if (flags & MAP_FIXED) {
69 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
70 if (TASK_SIZE - len < addr)
74 * We do not accept a shared mapping if it would violate
75 * cache aliasing constraints.
77 if ((flags & MAP_SHARED) &&
78 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
84 if (filp || (flags & MAP_SHARED))
87 /* requesting a specific address */
90 addr = COLOUR_ALIGN(addr, pgoff);
92 addr = PAGE_ALIGN(addr);
94 vma = find_vma(mm, addr);
95 if (TASK_SIZE - len >= addr &&
96 (!vma || addr + len <= vm_start_gap(vma)))
101 info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
102 info.align_offset = pgoff << PAGE_SHIFT;
105 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
106 info.low_limit = PAGE_SIZE;
107 info.high_limit = mm->mmap_base;
108 addr = vm_unmapped_area(&info);
110 if (!(addr & ~PAGE_MASK))
114 * A failed mmap() very likely causes application failure,
115 * so fall back to the bottom-up function here. This scenario
116 * can happen with large stack limits and large mmap()
122 info.low_limit = mm->mmap_base;
123 info.high_limit = TASK_SIZE;
124 return vm_unmapped_area(&info);
127 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
128 unsigned long len, unsigned long pgoff, unsigned long flags)
130 return arch_get_unmapped_area_common(filp,
131 addr0, len, pgoff, flags, UP);
135 * There is no need to export this but sched.h declares the function as
136 * extern so making it static here results in an error.
138 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
139 unsigned long addr0, unsigned long len, unsigned long pgoff,
142 return arch_get_unmapped_area_common(filp,
143 addr0, len, pgoff, flags, DOWN);
146 unsigned long arch_mmap_rnd(void)
151 if (TASK_IS_32BIT_ADDR)
152 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
154 #endif /* CONFIG_COMPAT */
155 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
157 return rnd << PAGE_SHIFT;
160 void arch_pick_mmap_layout(struct mm_struct *mm)
162 unsigned long random_factor = 0UL;
164 if (current->flags & PF_RANDOMIZE)
165 random_factor = arch_mmap_rnd();
167 if (mmap_is_legacy()) {
168 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
169 mm->get_unmapped_area = arch_get_unmapped_area;
171 mm->mmap_base = mmap_base(random_factor);
172 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
176 static inline unsigned long brk_rnd(void)
178 unsigned long rnd = get_random_long();
180 rnd = rnd << PAGE_SHIFT;
181 /* 8MB for 32bit, 256MB for 64bit */
182 if (TASK_IS_32BIT_ADDR)
183 rnd = rnd & 0x7ffffful;
185 rnd = rnd & 0xffffffful;
190 unsigned long arch_randomize_brk(struct mm_struct *mm)
192 unsigned long base = mm->brk;
195 ret = PAGE_ALIGN(base + brk_rnd());
203 int __virt_addr_valid(const volatile void *kaddr)
205 return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
207 EXPORT_SYMBOL_GPL(__virt_addr_valid);