2 * flexible mmap layout support
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
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>
27 #include <linux/mman.h>
28 #include <linux/module.h>
29 #include <linux/random.h>
30 #include <linux/compat.h>
31 #include <linux/security.h>
32 #include <asm/pgalloc.h>
34 unsigned long mmap_rnd_mask
;
35 static unsigned long mmap_align_mask
;
37 static unsigned long stack_maxrandom_size(void)
39 if (!(current
->flags
& PF_RANDOMIZE
))
41 if (current
->personality
& ADDR_NO_RANDOMIZE
)
43 return STACK_RND_MASK
<< PAGE_SHIFT
;
47 * Top of mmap area (just below the process stack).
49 * Leave at least a ~32 MB hole.
51 #define MIN_GAP (32*1024*1024)
52 #define MAX_GAP (STACK_TOP/6*5)
54 static inline int mmap_is_legacy(void)
56 if (current
->personality
& ADDR_COMPAT_LAYOUT
)
58 if (rlimit(RLIMIT_STACK
) == RLIM_INFINITY
)
60 return sysctl_legacy_va_layout
;
63 unsigned long arch_mmap_rnd(void)
66 return (get_random_int() & 0x7ff) << PAGE_SHIFT
;
68 return (get_random_int() & mmap_rnd_mask
) << PAGE_SHIFT
;
71 static unsigned long mmap_base_legacy(unsigned long rnd
)
73 return TASK_UNMAPPED_BASE
+ rnd
;
76 static inline unsigned long mmap_base(unsigned long rnd
)
78 unsigned long gap
= rlimit(RLIMIT_STACK
);
82 else if (gap
> MAX_GAP
)
85 return STACK_TOP
- stack_maxrandom_size() - rnd
- gap
;
89 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
90 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
92 struct mm_struct
*mm
= current
->mm
;
93 struct vm_area_struct
*vma
;
94 struct vm_unmapped_area_info info
;
97 if (len
> TASK_SIZE
- mmap_min_addr
)
100 if (flags
& MAP_FIXED
)
104 addr
= PAGE_ALIGN(addr
);
105 vma
= find_vma(mm
, addr
);
106 if (TASK_SIZE
- len
>= addr
&& addr
>= mmap_min_addr
&&
107 (!vma
|| addr
+ len
<= vma
->vm_start
))
112 if (filp
|| (flags
& MAP_SHARED
))
113 do_color_align
= !is_32bit_task();
117 info
.low_limit
= mm
->mmap_base
;
118 info
.high_limit
= TASK_SIZE
;
119 info
.align_mask
= do_color_align
? (mmap_align_mask
<< PAGE_SHIFT
) : 0;
120 info
.align_offset
= pgoff
<< PAGE_SHIFT
;
121 return vm_unmapped_area(&info
);
125 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
126 const unsigned long len
, const unsigned long pgoff
,
127 const unsigned long flags
)
129 struct vm_area_struct
*vma
;
130 struct mm_struct
*mm
= current
->mm
;
131 unsigned long addr
= addr0
;
132 struct vm_unmapped_area_info info
;
135 /* requested length too big for entire address space */
136 if (len
> TASK_SIZE
- mmap_min_addr
)
139 if (flags
& MAP_FIXED
)
142 /* requesting a specific address */
144 addr
= PAGE_ALIGN(addr
);
145 vma
= find_vma(mm
, addr
);
146 if (TASK_SIZE
- len
>= addr
&& addr
>= mmap_min_addr
&&
147 (!vma
|| addr
+ len
<= vma
->vm_start
))
152 if (filp
|| (flags
& MAP_SHARED
))
153 do_color_align
= !is_32bit_task();
155 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
157 info
.low_limit
= max(PAGE_SIZE
, mmap_min_addr
);
158 info
.high_limit
= mm
->mmap_base
;
159 info
.align_mask
= do_color_align
? (mmap_align_mask
<< PAGE_SHIFT
) : 0;
160 info
.align_offset
= pgoff
<< PAGE_SHIFT
;
161 addr
= vm_unmapped_area(&info
);
164 * A failed mmap() very likely causes application failure,
165 * so fall back to the bottom-up function here. This scenario
166 * can happen with large stack limits and large mmap()
169 if (addr
& ~PAGE_MASK
) {
170 VM_BUG_ON(addr
!= -ENOMEM
);
172 info
.low_limit
= TASK_UNMAPPED_BASE
;
173 info
.high_limit
= TASK_SIZE
;
174 addr
= vm_unmapped_area(&info
);
180 int s390_mmap_check(unsigned long addr
, unsigned long len
, unsigned long flags
)
182 if (is_compat_task() || (TASK_SIZE
>= (1UL << 53)))
184 if (!(flags
& MAP_FIXED
))
186 if ((addr
+ len
) >= TASK_SIZE
)
187 return crst_table_upgrade(current
->mm
, 1UL << 53);
192 s390_get_unmapped_area(struct file
*filp
, unsigned long addr
,
193 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
195 struct mm_struct
*mm
= current
->mm
;
199 area
= arch_get_unmapped_area(filp
, addr
, len
, pgoff
, flags
);
200 if (!(area
& ~PAGE_MASK
))
202 if (area
== -ENOMEM
&& !is_compat_task() && TASK_SIZE
< (1UL << 53)) {
203 /* Upgrade the page table to 4 levels and retry. */
204 rc
= crst_table_upgrade(mm
, 1UL << 53);
206 return (unsigned long) rc
;
207 area
= arch_get_unmapped_area(filp
, addr
, len
, pgoff
, flags
);
213 s390_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr
,
214 const unsigned long len
, const unsigned long pgoff
,
215 const unsigned long flags
)
217 struct mm_struct
*mm
= current
->mm
;
221 area
= arch_get_unmapped_area_topdown(filp
, addr
, len
, pgoff
, flags
);
222 if (!(area
& ~PAGE_MASK
))
224 if (area
== -ENOMEM
&& !is_compat_task() && TASK_SIZE
< (1UL << 53)) {
225 /* Upgrade the page table to 4 levels and retry. */
226 rc
= crst_table_upgrade(mm
, 1UL << 53);
228 return (unsigned long) rc
;
229 area
= arch_get_unmapped_area_topdown(filp
, addr
, len
,
235 * This function, called very early during the creation of a new
236 * process VM image, sets up which VM layout function to use:
238 void arch_pick_mmap_layout(struct mm_struct
*mm
)
240 unsigned long random_factor
= 0UL;
242 if (current
->flags
& PF_RANDOMIZE
)
243 random_factor
= arch_mmap_rnd();
246 * Fall back to the standard layout if the personality
247 * bit is set, or if the expected stack growth is unlimited:
249 if (mmap_is_legacy()) {
250 mm
->mmap_base
= mmap_base_legacy(random_factor
);
251 mm
->get_unmapped_area
= s390_get_unmapped_area
;
253 mm
->mmap_base
= mmap_base(random_factor
);
254 mm
->get_unmapped_area
= s390_get_unmapped_area_topdown
;
258 static int __init
setup_mmap_rnd(void)
263 switch (cpu_id
.machine
) {
277 mmap_rnd_mask
= 0x7ffUL
;
278 mmap_align_mask
= 0UL;
280 case 0x2964: /* z13 */
282 mmap_rnd_mask
= 0x3ff80UL
;
283 mmap_align_mask
= 0x7fUL
;
288 early_initcall(setup_mmap_rnd
);