Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / arch / powerpc / mm / mmap.c
blobd503f344e476e497912def22e544d56a88b65567
1 /*
2 * flexible mmap layout support
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5 * All Rights Reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Started by Ingo Molnar <mingo@elte.hu>
25 #include <linux/personality.h>
26 #include <linux/mm.h>
27 #include <linux/random.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sched/mm.h>
30 #include <linux/elf-randomize.h>
31 #include <linux/security.h>
32 #include <linux/mman.h>
35 * Top of mmap area (just below the process stack).
37 * Leave at least a ~128 MB hole.
39 #define MIN_GAP (128*1024*1024)
40 #define MAX_GAP (TASK_SIZE/6*5)
42 static inline int mmap_is_legacy(void)
44 if (current->personality & ADDR_COMPAT_LAYOUT)
45 return 1;
47 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
48 return 1;
50 return sysctl_legacy_va_layout;
53 unsigned long arch_mmap_rnd(void)
55 unsigned long shift, rnd;
57 shift = mmap_rnd_bits;
58 #ifdef CONFIG_COMPAT
59 if (is_32bit_task())
60 shift = mmap_rnd_compat_bits;
61 #endif
62 rnd = get_random_long() % (1ul << shift);
64 return rnd << PAGE_SHIFT;
67 static inline unsigned long stack_maxrandom_size(void)
69 if (!(current->flags & PF_RANDOMIZE))
70 return 0;
72 /* 8MB for 32bit, 1GB for 64bit */
73 if (is_32bit_task())
74 return (1<<23);
75 else
76 return (1<<30);
79 static inline unsigned long mmap_base(unsigned long rnd)
81 unsigned long gap = rlimit(RLIMIT_STACK);
82 unsigned long pad = stack_maxrandom_size() + stack_guard_gap;
84 /* Values close to RLIM_INFINITY can overflow. */
85 if (gap + pad > gap)
86 gap += pad;
88 if (gap < MIN_GAP)
89 gap = MIN_GAP;
90 else if (gap > MAX_GAP)
91 gap = MAX_GAP;
93 return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd);
96 #ifdef CONFIG_PPC_RADIX_MMU
98 * Same function as generic code used only for radix, because we don't need to overload
99 * the generic one. But we will have to duplicate, because hash select
100 * HAVE_ARCH_UNMAPPED_AREA
102 static unsigned long
103 radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
104 unsigned long len, unsigned long pgoff,
105 unsigned long flags)
107 struct mm_struct *mm = current->mm;
108 struct vm_area_struct *vma;
109 int fixed = (flags & MAP_FIXED);
110 unsigned long high_limit;
111 struct vm_unmapped_area_info info;
113 high_limit = DEFAULT_MAP_WINDOW;
114 if (addr >= high_limit || (fixed && (addr + len > high_limit)))
115 high_limit = TASK_SIZE;
117 if (len > high_limit)
118 return -ENOMEM;
120 if (fixed) {
121 if (addr > high_limit - len)
122 return -ENOMEM;
123 return addr;
126 if (addr) {
127 addr = PAGE_ALIGN(addr);
128 vma = find_vma(mm, addr);
129 if (high_limit - len >= addr && addr >= mmap_min_addr &&
130 (!vma || addr + len <= vm_start_gap(vma)))
131 return addr;
134 info.flags = 0;
135 info.length = len;
136 info.low_limit = mm->mmap_base;
137 info.high_limit = high_limit;
138 info.align_mask = 0;
140 return vm_unmapped_area(&info);
143 static unsigned long
144 radix__arch_get_unmapped_area_topdown(struct file *filp,
145 const unsigned long addr0,
146 const unsigned long len,
147 const unsigned long pgoff,
148 const unsigned long flags)
150 struct vm_area_struct *vma;
151 struct mm_struct *mm = current->mm;
152 unsigned long addr = addr0;
153 int fixed = (flags & MAP_FIXED);
154 unsigned long high_limit;
155 struct vm_unmapped_area_info info;
157 high_limit = DEFAULT_MAP_WINDOW;
158 if (addr >= high_limit || (fixed && (addr + len > high_limit)))
159 high_limit = TASK_SIZE;
161 if (len > high_limit)
162 return -ENOMEM;
164 if (fixed) {
165 if (addr > high_limit - len)
166 return -ENOMEM;
167 return addr;
170 if (addr) {
171 addr = PAGE_ALIGN(addr);
172 vma = find_vma(mm, addr);
173 if (high_limit - len >= addr && addr >= mmap_min_addr &&
174 (!vma || addr + len <= vm_start_gap(vma)))
175 return addr;
178 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
179 info.length = len;
180 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
181 info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
182 info.align_mask = 0;
184 addr = vm_unmapped_area(&info);
185 if (!(addr & ~PAGE_MASK))
186 return addr;
187 VM_BUG_ON(addr != -ENOMEM);
190 * A failed mmap() very likely causes application failure,
191 * so fall back to the bottom-up function here. This scenario
192 * can happen with large stack limits and large mmap()
193 * allocations.
195 return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
198 static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
199 unsigned long random_factor)
201 if (mmap_is_legacy()) {
202 mm->mmap_base = TASK_UNMAPPED_BASE;
203 mm->get_unmapped_area = radix__arch_get_unmapped_area;
204 } else {
205 mm->mmap_base = mmap_base(random_factor);
206 mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
209 #else
210 /* dummy */
211 extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
212 unsigned long random_factor);
213 #endif
215 * This function, called very early during the creation of a new
216 * process VM image, sets up which VM layout function to use:
218 void arch_pick_mmap_layout(struct mm_struct *mm)
220 unsigned long random_factor = 0UL;
222 if (current->flags & PF_RANDOMIZE)
223 random_factor = arch_mmap_rnd();
225 if (radix_enabled())
226 return radix__arch_pick_mmap_layout(mm, random_factor);
228 * Fall back to the standard layout if the personality
229 * bit is set, or if the expected stack growth is unlimited:
231 if (mmap_is_legacy()) {
232 mm->mmap_base = TASK_UNMAPPED_BASE;
233 mm->get_unmapped_area = arch_get_unmapped_area;
234 } else {
235 mm->mmap_base = mmap_base(random_factor);
236 mm->get_unmapped_area = arch_get_unmapped_area_topdown;