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>
21 * sys_pipe() is the normal C calling standard for creating
22 * a pipe. It's not the way Unix traditionally does this, though.
24 asmlinkage
long sys_pipe(int __user
*fildes
)
31 if (copy_to_user(fildes
, fd
, 2*sizeof(int)))
37 asmlinkage
long sys_mmap(unsigned long addr
, unsigned long len
, unsigned long prot
, unsigned long flags
,
38 unsigned long fd
, unsigned long off
)
49 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
50 if (!(flags
& MAP_ANONYMOUS
)) {
55 down_write(¤t
->mm
->mmap_sem
);
56 error
= do_mmap_pgoff(file
, addr
, len
, prot
, flags
, off
>> PAGE_SHIFT
);
57 up_write(¤t
->mm
->mmap_sem
);
65 static void find_start_end(unsigned long flags
, unsigned long *begin
,
68 if (!test_thread_flag(TIF_IA32
) && (flags
& MAP_32BIT
)) {
69 unsigned long new_begin
;
70 /* This is usually used needed to map code in small
71 model, so it needs to be in the first 31bit. Limit
72 it to that. This means we need to move the
73 unmapped base down for this case. This can give
74 conflicts with the heap, but we assume that glibc
75 malloc knows how to fall back to mmap. Give it 1GB
76 of playground for now. -AK */
79 if (current
->flags
& PF_RANDOMIZE
) {
80 new_begin
= randomize_range(*begin
, *begin
+ 0x02000000, 0);
85 *begin
= TASK_UNMAPPED_BASE
;
91 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
92 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
94 struct mm_struct
*mm
= current
->mm
;
95 struct vm_area_struct
*vma
;
96 unsigned long start_addr
;
97 unsigned long begin
, end
;
99 if (flags
& MAP_FIXED
)
102 find_start_end(flags
, &begin
, &end
);
108 addr
= PAGE_ALIGN(addr
);
109 vma
= find_vma(mm
, addr
);
110 if (end
- len
>= addr
&&
111 (!vma
|| addr
+ len
<= vma
->vm_start
))
114 if (((flags
& MAP_32BIT
) || test_thread_flag(TIF_IA32
))
115 && len
<= mm
->cached_hole_size
) {
116 mm
->cached_hole_size
= 0;
117 mm
->free_area_cache
= begin
;
119 addr
= mm
->free_area_cache
;
125 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
126 /* At this point: (!vma || addr < vma->vm_end). */
127 if (end
- len
< addr
) {
129 * Start a new search - just in case we missed
132 if (start_addr
!= begin
) {
133 start_addr
= addr
= begin
;
134 mm
->cached_hole_size
= 0;
139 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
141 * Remember the place where we stopped the search:
143 mm
->free_area_cache
= addr
+ len
;
146 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
147 mm
->cached_hole_size
= vma
->vm_start
- addr
;
155 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
156 const unsigned long len
, const unsigned long pgoff
,
157 const unsigned long flags
)
159 struct vm_area_struct
*vma
;
160 struct mm_struct
*mm
= current
->mm
;
161 unsigned long addr
= addr0
;
163 /* requested length too big for entire address space */
167 if (flags
& MAP_FIXED
)
170 /* for MAP_32BIT mappings we force the legact mmap base */
171 if (!test_thread_flag(TIF_IA32
) && (flags
& MAP_32BIT
))
174 /* requesting a specific address */
176 addr
= PAGE_ALIGN(addr
);
177 vma
= find_vma(mm
, addr
);
178 if (TASK_SIZE
- len
>= addr
&&
179 (!vma
|| addr
+ len
<= vma
->vm_start
))
183 /* check if free_area_cache is useful for us */
184 if (len
<= mm
->cached_hole_size
) {
185 mm
->cached_hole_size
= 0;
186 mm
->free_area_cache
= mm
->mmap_base
;
189 /* either no address requested or can't fit in requested address hole */
190 addr
= mm
->free_area_cache
;
192 /* make sure it can fit in the remaining address space */
194 vma
= find_vma(mm
, addr
-len
);
195 if (!vma
|| addr
<= vma
->vm_start
)
196 /* remember the address as a hint for next time */
197 return (mm
->free_area_cache
= addr
-len
);
200 if (mm
->mmap_base
< len
)
203 addr
= mm
->mmap_base
-len
;
207 * Lookup failure means no vma is above this address,
208 * else if new region fits below vma->vm_start,
209 * return with success:
211 vma
= find_vma(mm
, addr
);
212 if (!vma
|| addr
+len
<= vma
->vm_start
)
213 /* remember the address as a hint for next time */
214 return (mm
->free_area_cache
= addr
);
216 /* remember the largest hole we saw so far */
217 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
218 mm
->cached_hole_size
= vma
->vm_start
- addr
;
220 /* try just below the current vma->vm_start */
221 addr
= vma
->vm_start
-len
;
222 } while (len
< vma
->vm_start
);
226 * A failed mmap() very likely causes application failure,
227 * so fall back to the bottom-up function here. This scenario
228 * can happen with large stack limits and large mmap()
231 mm
->cached_hole_size
= ~0UL;
232 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
233 addr
= arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
235 * Restore the topdown base:
237 mm
->free_area_cache
= mm
->mmap_base
;
238 mm
->cached_hole_size
= ~0UL;
244 asmlinkage
long sys_uname(struct new_utsname __user
* name
)
248 err
= copy_to_user(name
, utsname(), sizeof (*name
));
250 if (personality(current
->personality
) == PER_LINUX32
)
251 err
|= copy_to_user(&name
->machine
, "i686", 5);
252 return err
? -EFAULT
: 0;