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/elf-randomize.h>
11 #include <linux/errno.h>
13 #include <linux/mman.h>
14 #include <linux/export.h>
15 #include <linux/personality.h>
16 #include <linux/random.h>
17 #include <linux/sched/signal.h>
18 #include <linux/sched/mm.h>
20 unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
21 EXPORT_SYMBOL(shm_align_mask);
23 /* gap between mmap and stack */
24 #define MIN_GAP (128*1024*1024UL)
25 #define MAX_GAP ((TASK_SIZE)/6*5)
26 #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12))
28 static int mmap_is_legacy(struct rlimit *rlim_stack)
30 if (current->personality & ADDR_COMPAT_LAYOUT)
33 if (rlim_stack->rlim_cur == RLIM_INFINITY)
36 return sysctl_legacy_va_layout;
39 static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
41 unsigned long gap = rlim_stack->rlim_cur;
42 unsigned long pad = stack_guard_gap;
44 /* Account for stack randomization if necessary */
45 if (current->flags & PF_RANDOMIZE)
46 pad += (STACK_RND_MASK << PAGE_SHIFT);
48 /* Values close to RLIM_INFINITY can overflow. */
54 else if (gap > MAX_GAP)
57 return PAGE_ALIGN(TASK_SIZE - gap - rnd);
60 #define COLOUR_ALIGN(addr, pgoff) \
61 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
62 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
64 enum mmap_allocation_direction {UP, DOWN};
66 static unsigned long arch_get_unmapped_area_common(struct file *filp,
67 unsigned long addr0, unsigned long len, unsigned long pgoff,
68 unsigned long flags, enum mmap_allocation_direction dir)
70 struct mm_struct *mm = current->mm;
71 struct vm_area_struct *vma;
72 unsigned long addr = addr0;
74 struct vm_unmapped_area_info info;
76 if (unlikely(len > TASK_SIZE))
79 if (flags & MAP_FIXED) {
80 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
81 if (TASK_SIZE - len < addr)
85 * We do not accept a shared mapping if it would violate
86 * cache aliasing constraints.
88 if ((flags & MAP_SHARED) &&
89 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
95 if (filp || (flags & MAP_SHARED))
98 /* requesting a specific address */
101 addr = COLOUR_ALIGN(addr, pgoff);
103 addr = PAGE_ALIGN(addr);
105 vma = find_vma(mm, addr);
106 if (TASK_SIZE - len >= addr &&
107 (!vma || addr + len <= vm_start_gap(vma)))
112 info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
113 info.align_offset = pgoff << PAGE_SHIFT;
116 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
117 info.low_limit = PAGE_SIZE;
118 info.high_limit = mm->mmap_base;
119 addr = vm_unmapped_area(&info);
121 if (!(addr & ~PAGE_MASK))
125 * A failed mmap() very likely causes application failure,
126 * so fall back to the bottom-up function here. This scenario
127 * can happen with large stack limits and large mmap()
133 info.low_limit = mm->mmap_base;
134 info.high_limit = TASK_SIZE;
135 return vm_unmapped_area(&info);
138 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
139 unsigned long len, unsigned long pgoff, unsigned long flags)
141 return arch_get_unmapped_area_common(filp,
142 addr0, len, pgoff, flags, UP);
146 * There is no need to export this but sched.h declares the function as
147 * extern so making it static here results in an error.
149 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
150 unsigned long addr0, unsigned long len, unsigned long pgoff,
153 return arch_get_unmapped_area_common(filp,
154 addr0, len, pgoff, flags, DOWN);
157 unsigned long arch_mmap_rnd(void)
162 if (TASK_IS_32BIT_ADDR)
163 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
165 #endif /* CONFIG_COMPAT */
166 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
168 return rnd << PAGE_SHIFT;
171 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
173 unsigned long random_factor = 0UL;
175 if (current->flags & PF_RANDOMIZE)
176 random_factor = arch_mmap_rnd();
178 if (mmap_is_legacy(rlim_stack)) {
179 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
180 mm->get_unmapped_area = arch_get_unmapped_area;
182 mm->mmap_base = mmap_base(random_factor, rlim_stack);
183 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
187 static inline unsigned long brk_rnd(void)
189 unsigned long rnd = get_random_long();
191 rnd = rnd << PAGE_SHIFT;
192 /* 8MB for 32bit, 256MB for 64bit */
193 if (TASK_IS_32BIT_ADDR)
194 rnd = rnd & 0x7ffffful;
196 rnd = rnd & 0xffffffful;
201 unsigned long arch_randomize_brk(struct mm_struct *mm)
203 unsigned long base = mm->brk;
206 ret = PAGE_ALIGN(base + brk_rnd());
214 int __virt_addr_valid(const volatile void *kaddr)
216 unsigned long vaddr = (unsigned long)kaddr;
218 if ((vaddr < PAGE_OFFSET) || (vaddr >= MAP_BASE))
221 return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
223 EXPORT_SYMBOL_GPL(__virt_addr_valid);