1 #include <linux/errno.h>
2 #include <linux/sched.h>
3 #include <linux/syscalls.h>
10 #include <linux/stat.h>
11 #include <linux/mman.h>
12 #include <linux/file.h>
13 #include <linux/utsname.h>
14 #include <linux/personality.h>
15 #include <linux/random.h>
16 #include <linux/uaccess.h>
19 #include <asm/syscalls.h>
22 * Align a virtual address to avoid aliasing in the I$ on AMD F15h.
24 * @flags denotes the allocation direction - bottomup or topdown -
25 * or vDSO; see call sites below.
27 unsigned long align_addr(unsigned long addr
, struct file
*filp
,
28 enum align_flags flags
)
30 unsigned long tmp_addr
;
32 /* handle 32- and 64-bit case with a single conditional */
33 if (va_align
.flags
< 0 || !(va_align
.flags
& (2 - mmap_is_ia32())))
36 if (!(current
->flags
& PF_RANDOMIZE
))
39 if (!((flags
& ALIGN_VDSO
) || filp
))
45 * We need an address which is <= than the original
46 * one only when in topdown direction.
48 if (!(flags
& ALIGN_TOPDOWN
))
49 tmp_addr
+= va_align
.mask
;
51 tmp_addr
&= ~va_align
.mask
;
56 static int __init
control_va_addr_alignment(char *str
)
58 /* guard against enabling this on other CPU families */
59 if (va_align
.flags
< 0)
68 if (!strcmp(str
, "32"))
69 va_align
.flags
= ALIGN_VA_32
;
70 else if (!strcmp(str
, "64"))
71 va_align
.flags
= ALIGN_VA_64
;
72 else if (!strcmp(str
, "off"))
74 else if (!strcmp(str
, "on"))
75 va_align
.flags
= ALIGN_VA_32
| ALIGN_VA_64
;
81 __setup("align_va_addr", control_va_addr_alignment
);
83 SYSCALL_DEFINE6(mmap
, unsigned long, addr
, unsigned long, len
,
84 unsigned long, prot
, unsigned long, flags
,
85 unsigned long, fd
, unsigned long, off
)
92 error
= sys_mmap_pgoff(addr
, len
, prot
, flags
, fd
, off
>> PAGE_SHIFT
);
97 static void find_start_end(unsigned long flags
, unsigned long *begin
,
100 if (!test_thread_flag(TIF_IA32
) && (flags
& MAP_32BIT
)) {
101 unsigned long new_begin
;
102 /* This is usually used needed to map code in small
103 model, so it needs to be in the first 31bit. Limit
104 it to that. This means we need to move the
105 unmapped base down for this case. This can give
106 conflicts with the heap, but we assume that glibc
107 malloc knows how to fall back to mmap. Give it 1GB
108 of playground for now. -AK */
111 if (current
->flags
& PF_RANDOMIZE
) {
112 new_begin
= randomize_range(*begin
, *begin
+ 0x02000000, 0);
117 *begin
= TASK_UNMAPPED_BASE
;
123 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
124 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
126 struct mm_struct
*mm
= current
->mm
;
127 struct vm_area_struct
*vma
;
128 unsigned long start_addr
;
129 unsigned long begin
, end
;
131 if (flags
& MAP_FIXED
)
134 find_start_end(flags
, &begin
, &end
);
140 addr
= PAGE_ALIGN(addr
);
141 vma
= find_vma(mm
, addr
);
142 if (end
- len
>= addr
&&
143 (!vma
|| addr
+ len
<= vma
->vm_start
))
146 if (((flags
& MAP_32BIT
) || test_thread_flag(TIF_IA32
))
147 && len
<= mm
->cached_hole_size
) {
148 mm
->cached_hole_size
= 0;
149 mm
->free_area_cache
= begin
;
151 addr
= mm
->free_area_cache
;
158 addr
= align_addr(addr
, filp
, 0);
160 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
161 /* At this point: (!vma || addr < vma->vm_end). */
162 if (end
- len
< addr
) {
164 * Start a new search - just in case we missed
167 if (start_addr
!= begin
) {
168 start_addr
= addr
= begin
;
169 mm
->cached_hole_size
= 0;
174 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
176 * Remember the place where we stopped the search:
178 mm
->free_area_cache
= addr
+ len
;
181 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
182 mm
->cached_hole_size
= vma
->vm_start
- addr
;
185 addr
= align_addr(addr
, filp
, 0);
191 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
192 const unsigned long len
, const unsigned long pgoff
,
193 const unsigned long flags
)
195 struct vm_area_struct
*vma
;
196 struct mm_struct
*mm
= current
->mm
;
197 unsigned long addr
= addr0
;
199 /* requested length too big for entire address space */
203 if (flags
& MAP_FIXED
)
206 /* for MAP_32BIT mappings we force the legact mmap base */
207 if (!test_thread_flag(TIF_IA32
) && (flags
& MAP_32BIT
))
210 /* requesting a specific address */
212 addr
= PAGE_ALIGN(addr
);
213 vma
= find_vma(mm
, addr
);
214 if (TASK_SIZE
- len
>= addr
&&
215 (!vma
|| addr
+ len
<= vma
->vm_start
))
219 /* check if free_area_cache is useful for us */
220 if (len
<= mm
->cached_hole_size
) {
221 mm
->cached_hole_size
= 0;
222 mm
->free_area_cache
= mm
->mmap_base
;
225 /* either no address requested or can't fit in requested address hole */
226 addr
= mm
->free_area_cache
;
228 /* make sure it can fit in the remaining address space */
230 unsigned long tmp_addr
= align_addr(addr
- len
, filp
,
233 vma
= find_vma(mm
, tmp_addr
);
234 if (!vma
|| tmp_addr
+ len
<= vma
->vm_start
)
235 /* remember the address as a hint for next time */
236 return mm
->free_area_cache
= tmp_addr
;
239 if (mm
->mmap_base
< len
)
242 addr
= mm
->mmap_base
-len
;
245 addr
= align_addr(addr
, filp
, ALIGN_TOPDOWN
);
248 * Lookup failure means no vma is above this address,
249 * else if new region fits below vma->vm_start,
250 * return with success:
252 vma
= find_vma(mm
, addr
);
253 if (!vma
|| addr
+len
<= vma
->vm_start
)
254 /* remember the address as a hint for next time */
255 return mm
->free_area_cache
= addr
;
257 /* remember the largest hole we saw so far */
258 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
259 mm
->cached_hole_size
= vma
->vm_start
- addr
;
261 /* try just below the current vma->vm_start */
262 addr
= vma
->vm_start
-len
;
263 } while (len
< vma
->vm_start
);
267 * A failed mmap() very likely causes application failure,
268 * so fall back to the bottom-up function here. This scenario
269 * can happen with large stack limits and large mmap()
272 mm
->cached_hole_size
= ~0UL;
273 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
274 addr
= arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
276 * Restore the topdown base:
278 mm
->free_area_cache
= mm
->mmap_base
;
279 mm
->cached_hole_size
= ~0UL;