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>
17 #include <asm/uaccess.h>
20 asmlinkage
long sys_mmap(unsigned long addr
, unsigned long len
, unsigned long prot
, unsigned long flags
,
21 unsigned long fd
, unsigned long off
)
32 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
33 if (!(flags
& MAP_ANONYMOUS
)) {
38 down_write(¤t
->mm
->mmap_sem
);
39 error
= do_mmap_pgoff(file
, addr
, len
, prot
, flags
, off
>> PAGE_SHIFT
);
40 up_write(¤t
->mm
->mmap_sem
);
48 static void find_start_end(unsigned long flags
, unsigned long *begin
,
51 if (!test_thread_flag(TIF_IA32
) && (flags
& MAP_32BIT
)) {
52 unsigned long new_begin
;
53 /* This is usually used needed to map code in small
54 model, so it needs to be in the first 31bit. Limit
55 it to that. This means we need to move the
56 unmapped base down for this case. This can give
57 conflicts with the heap, but we assume that glibc
58 malloc knows how to fall back to mmap. Give it 1GB
59 of playground for now. -AK */
62 if (current
->flags
& PF_RANDOMIZE
) {
63 new_begin
= randomize_range(*begin
, *begin
+ 0x02000000, 0);
68 *begin
= TASK_UNMAPPED_BASE
;
74 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
75 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
77 struct mm_struct
*mm
= current
->mm
;
78 struct vm_area_struct
*vma
;
79 unsigned long start_addr
;
80 unsigned long begin
, end
;
82 if (flags
& MAP_FIXED
)
85 find_start_end(flags
, &begin
, &end
);
91 addr
= PAGE_ALIGN(addr
);
92 vma
= find_vma(mm
, addr
);
93 if (end
- len
>= addr
&&
94 (!vma
|| addr
+ len
<= vma
->vm_start
))
97 if (((flags
& MAP_32BIT
) || test_thread_flag(TIF_IA32
))
98 && len
<= mm
->cached_hole_size
) {
99 mm
->cached_hole_size
= 0;
100 mm
->free_area_cache
= begin
;
102 addr
= mm
->free_area_cache
;
108 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
109 /* At this point: (!vma || addr < vma->vm_end). */
110 if (end
- len
< addr
) {
112 * Start a new search - just in case we missed
115 if (start_addr
!= begin
) {
116 start_addr
= addr
= begin
;
117 mm
->cached_hole_size
= 0;
122 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
124 * Remember the place where we stopped the search:
126 mm
->free_area_cache
= addr
+ len
;
129 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
130 mm
->cached_hole_size
= vma
->vm_start
- addr
;
138 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
139 const unsigned long len
, const unsigned long pgoff
,
140 const unsigned long flags
)
142 struct vm_area_struct
*vma
;
143 struct mm_struct
*mm
= current
->mm
;
144 unsigned long addr
= addr0
;
146 /* requested length too big for entire address space */
150 if (flags
& MAP_FIXED
)
153 /* for MAP_32BIT mappings we force the legact mmap base */
154 if (!test_thread_flag(TIF_IA32
) && (flags
& MAP_32BIT
))
157 /* requesting a specific address */
159 addr
= PAGE_ALIGN(addr
);
160 vma
= find_vma(mm
, addr
);
161 if (TASK_SIZE
- len
>= addr
&&
162 (!vma
|| addr
+ len
<= vma
->vm_start
))
166 /* check if free_area_cache is useful for us */
167 if (len
<= mm
->cached_hole_size
) {
168 mm
->cached_hole_size
= 0;
169 mm
->free_area_cache
= mm
->mmap_base
;
172 /* either no address requested or can't fit in requested address hole */
173 addr
= mm
->free_area_cache
;
175 /* make sure it can fit in the remaining address space */
177 vma
= find_vma(mm
, addr
-len
);
178 if (!vma
|| addr
<= vma
->vm_start
)
179 /* remember the address as a hint for next time */
180 return (mm
->free_area_cache
= addr
-len
);
183 if (mm
->mmap_base
< len
)
186 addr
= mm
->mmap_base
-len
;
190 * Lookup failure means no vma is above this address,
191 * else if new region fits below vma->vm_start,
192 * return with success:
194 vma
= find_vma(mm
, addr
);
195 if (!vma
|| addr
+len
<= vma
->vm_start
)
196 /* remember the address as a hint for next time */
197 return (mm
->free_area_cache
= addr
);
199 /* remember the largest hole we saw so far */
200 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
201 mm
->cached_hole_size
= vma
->vm_start
- addr
;
203 /* try just below the current vma->vm_start */
204 addr
= vma
->vm_start
-len
;
205 } while (len
< vma
->vm_start
);
209 * A failed mmap() very likely causes application failure,
210 * so fall back to the bottom-up function here. This scenario
211 * can happen with large stack limits and large mmap()
214 mm
->cached_hole_size
= ~0UL;
215 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
216 addr
= arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
218 * Restore the topdown base:
220 mm
->free_area_cache
= mm
->mmap_base
;
221 mm
->cached_hole_size
= ~0UL;
227 asmlinkage
long sys_uname(struct new_utsname __user
* name
)
231 err
= copy_to_user(name
, utsname(), sizeof (*name
));
233 if (personality(current
->personality
) == PER_LINUX32
)
234 err
|= copy_to_user(&name
->machine
, "i686", 5);
235 return err
? -EFAULT
: 0;