Remove obsolete #include <linux/config.h>
[pandora-kernel.git] / arch / ia64 / kernel / sys_ia64.c
1 /*
2  * This file contains various system calls that have different calling
3  * conventions on different platforms.
4  *
5  * Copyright (C) 1999-2000, 2002-2003, 2005 Hewlett-Packard Co
6  *      David Mosberger-Tang <davidm@hpl.hp.com>
7  */
8 #include <linux/errno.h>
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/mman.h>
12 #include <linux/sched.h>
13 #include <linux/shm.h>
14 #include <linux/file.h>         /* doh, must come after sched.h... */
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/syscalls.h>
18 #include <linux/highuid.h>
19 #include <linux/hugetlb.h>
20
21 #include <asm/shmparam.h>
22 #include <asm/uaccess.h>
23
24 unsigned long
25 arch_get_unmapped_area (struct file *filp, unsigned long addr, unsigned long len,
26                         unsigned long pgoff, unsigned long flags)
27 {
28         long map_shared = (flags & MAP_SHARED);
29         unsigned long start_addr, align_mask = PAGE_SIZE - 1;
30         struct mm_struct *mm = current->mm;
31         struct vm_area_struct *vma;
32
33         if (len > RGN_MAP_LIMIT)
34                 return -ENOMEM;
35
36 #ifdef CONFIG_HUGETLB_PAGE
37         if (REGION_NUMBER(addr) == RGN_HPAGE)
38                 addr = 0;
39 #endif
40         if (!addr)
41                 addr = mm->free_area_cache;
42
43         if (map_shared && (TASK_SIZE > 0xfffffffful))
44                 /*
45                  * For 64-bit tasks, align shared segments to 1MB to avoid potential
46                  * performance penalty due to virtual aliasing (see ASDM).  For 32-bit
47                  * tasks, we prefer to avoid exhausting the address space too quickly by
48                  * limiting alignment to a single page.
49                  */
50                 align_mask = SHMLBA - 1;
51
52   full_search:
53         start_addr = addr = (addr + align_mask) & ~align_mask;
54
55         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
56                 /* At this point:  (!vma || addr < vma->vm_end). */
57                 if (TASK_SIZE - len < addr || RGN_MAP_LIMIT - len < REGION_OFFSET(addr)) {
58                         if (start_addr != TASK_UNMAPPED_BASE) {
59                                 /* Start a new search --- just in case we missed some holes.  */
60                                 addr = TASK_UNMAPPED_BASE;
61                                 goto full_search;
62                         }
63                         return -ENOMEM;
64                 }
65                 if (!vma || addr + len <= vma->vm_start) {
66                         /* Remember the address where we stopped this search:  */
67                         mm->free_area_cache = addr + len;
68                         return addr;
69                 }
70                 addr = (vma->vm_end + align_mask) & ~align_mask;
71         }
72 }
73
74 asmlinkage long
75 ia64_getpriority (int which, int who)
76 {
77         long prio;
78
79         prio = sys_getpriority(which, who);
80         if (prio >= 0) {
81                 force_successful_syscall_return();
82                 prio = 20 - prio;
83         }
84         return prio;
85 }
86
87 /* XXX obsolete, but leave it here until the old libc is gone... */
88 asmlinkage unsigned long
89 sys_getpagesize (void)
90 {
91         return PAGE_SIZE;
92 }
93
94 asmlinkage unsigned long
95 ia64_brk (unsigned long brk)
96 {
97         unsigned long rlim, retval, newbrk, oldbrk;
98         struct mm_struct *mm = current->mm;
99
100         /*
101          * Most of this replicates the code in sys_brk() except for an additional safety
102          * check and the clearing of r8.  However, we can't call sys_brk() because we need
103          * to acquire the mmap_sem before we can do the test...
104          */
105         down_write(&mm->mmap_sem);
106
107         if (brk < mm->end_code)
108                 goto out;
109         newbrk = PAGE_ALIGN(brk);
110         oldbrk = PAGE_ALIGN(mm->brk);
111         if (oldbrk == newbrk)
112                 goto set_brk;
113
114         /* Always allow shrinking brk. */
115         if (brk <= mm->brk) {
116                 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
117                         goto set_brk;
118                 goto out;
119         }
120
121         /* Check against unimplemented/unmapped addresses: */
122         if ((newbrk - oldbrk) > RGN_MAP_LIMIT || REGION_OFFSET(newbrk) > RGN_MAP_LIMIT)
123                 goto out;
124
125         /* Check against rlimit.. */
126         rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
127         if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
128                 goto out;
129
130         /* Check against existing mmap mappings. */
131         if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
132                 goto out;
133
134         /* Ok, looks good - let it rip. */
135         if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
136                 goto out;
137 set_brk:
138         mm->brk = brk;
139 out:
140         retval = mm->brk;
141         up_write(&mm->mmap_sem);
142         force_successful_syscall_return();
143         return retval;
144 }
145
146 /*
147  * On IA-64, we return the two file descriptors in ret0 and ret1 (r8
148  * and r9) as this is faster than doing a copy_to_user().
149  */
150 asmlinkage long
151 sys_pipe (void)
152 {
153         struct pt_regs *regs = task_pt_regs(current);
154         int fd[2];
155         int retval;
156
157         retval = do_pipe(fd);
158         if (retval)
159                 goto out;
160         retval = fd[0];
161         regs->r9 = fd[1];
162   out:
163         return retval;
164 }
165
166 static inline unsigned long
167 do_mmap2 (unsigned long addr, unsigned long len, int prot, int flags, int fd, unsigned long pgoff)
168 {
169         unsigned long roff;
170         struct file *file = NULL;
171
172         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
173         if (!(flags & MAP_ANONYMOUS)) {
174                 file = fget(fd);
175                 if (!file)
176                         return -EBADF;
177
178                 if (!file->f_op || !file->f_op->mmap) {
179                         addr = -ENODEV;
180                         goto out;
181                 }
182         }
183
184         /* Careful about overflows.. */
185         len = PAGE_ALIGN(len);
186         if (!len || len > TASK_SIZE) {
187                 addr = -EINVAL;
188                 goto out;
189         }
190
191         /*
192          * Don't permit mappings into unmapped space, the virtual page table of a region,
193          * or across a region boundary.  Note: RGN_MAP_LIMIT is equal to 2^n-PAGE_SIZE
194          * (for some integer n <= 61) and len > 0.
195          */
196         roff = REGION_OFFSET(addr);
197         if ((len > RGN_MAP_LIMIT) || (roff > (RGN_MAP_LIMIT - len))) {
198                 addr = -EINVAL;
199                 goto out;
200         }
201
202         down_write(&current->mm->mmap_sem);
203         addr = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
204         up_write(&current->mm->mmap_sem);
205
206 out:    if (file)
207                 fput(file);
208         return addr;
209 }
210
211 /*
212  * mmap2() is like mmap() except that the offset is expressed in units
213  * of PAGE_SIZE (instead of bytes).  This allows to mmap2() (pieces
214  * of) files that are larger than the address space of the CPU.
215  */
216 asmlinkage unsigned long
217 sys_mmap2 (unsigned long addr, unsigned long len, int prot, int flags, int fd, long pgoff)
218 {
219         addr = do_mmap2(addr, len, prot, flags, fd, pgoff);
220         if (!IS_ERR((void *) addr))
221                 force_successful_syscall_return();
222         return addr;
223 }
224
225 asmlinkage unsigned long
226 sys_mmap (unsigned long addr, unsigned long len, int prot, int flags, int fd, long off)
227 {
228         if (offset_in_page(off) != 0)
229                 return -EINVAL;
230
231         addr = do_mmap2(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
232         if (!IS_ERR((void *) addr))
233                 force_successful_syscall_return();
234         return addr;
235 }
236
237 asmlinkage unsigned long
238 ia64_mremap (unsigned long addr, unsigned long old_len, unsigned long new_len, unsigned long flags,
239              unsigned long new_addr)
240 {
241         extern unsigned long do_mremap (unsigned long addr,
242                                         unsigned long old_len,
243                                         unsigned long new_len,
244                                         unsigned long flags,
245                                         unsigned long new_addr);
246
247         down_write(&current->mm->mmap_sem);
248         {
249                 addr = do_mremap(addr, old_len, new_len, flags, new_addr);
250         }
251         up_write(&current->mm->mmap_sem);
252
253         if (IS_ERR((void *) addr))
254                 return addr;
255
256         force_successful_syscall_return();
257         return addr;
258 }
259
260 #ifndef CONFIG_PCI
261
262 asmlinkage long
263 sys_pciconfig_read (unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len,
264                     void *buf)
265 {
266         return -ENOSYS;
267 }
268
269 asmlinkage long
270 sys_pciconfig_write (unsigned long bus, unsigned long dfn, unsigned long off, unsigned long len,
271                      void *buf)
272 {
273         return -ENOSYS;
274 }
275
276 #endif /* CONFIG_PCI */