ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / parisc / kernel / sys_parisc.c
1
2 /*
3  *    PARISC specific syscalls
4  *
5  *    Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
6  *    Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
7  *    Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
8  *
9  *
10  *    This program is free software; you can redistribute it and/or modify
11  *    it under the terms of the GNU General Public License as published by
12  *    the Free Software Foundation; either version 2 of the License, or
13  *    (at your option) any later version.
14  *
15  *    This program is distributed in the hope that it will be useful,
16  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *    GNU General Public License for more details.
19  *
20  *    You should have received a copy of the GNU General Public License
21  *    along with this program; if not, write to the Free Software
22  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include <asm/uaccess.h>
26 #include <linux/file.h>
27 #include <linux/fs.h>
28 #include <linux/linkage.h>
29 #include <linux/mm.h>
30 #include <linux/mman.h>
31 #include <linux/shm.h>
32 #include <linux/smp_lock.h>
33 #include <linux/syscalls.h>
34
35 int sys_pipe(int *fildes)
36 {
37         int fd[2];
38         int error;
39
40         error = do_pipe(fd);
41         if (!error) {
42                 if (copy_to_user(fildes, fd, 2*sizeof(int)))
43                         error = -EFAULT;
44         }
45         return error;
46 }
47
48 static unsigned long get_unshared_area(unsigned long addr, unsigned long len)
49 {
50         struct vm_area_struct *vma;
51
52         addr = PAGE_ALIGN(addr);
53
54         for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
55                 /* At this point:  (!vma || addr < vma->vm_end). */
56                 if (TASK_SIZE - len < addr)
57                         return -ENOMEM;
58                 if (!vma || addr + len <= vma->vm_start)
59                         return addr;
60                 addr = vma->vm_end;
61         }
62 }
63
64 #define DCACHE_ALIGN(addr) (((addr) + (SHMLBA - 1)) &~ (SHMLBA - 1))
65
66 /*
67  * We need to know the offset to use.  Old scheme was to look for
68  * existing mapping and use the same offset.  New scheme is to use the
69  * address of the kernel data structure as the seed for the offset.
70  * We'll see how that works...
71  */
72 #if 0
73 static int get_offset(struct address_space *mapping)
74 {
75         struct vm_area_struct *vma = list_entry(mapping->i_mmap_shared.next,
76                         struct vm_area_struct, shared);
77         return (vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT)) &
78                 (SHMLBA - 1);
79 }
80 #else
81 /* The mapping is cacheline aligned, so there's no information in the bottom
82  * few bits of the address.  We're looking for 10 bits (4MB / 4k), so let's
83  * drop the bottom 8 bits and use bits 8-17.  
84  */
85 static int get_offset(struct address_space *mapping)
86 {
87         int offset = (unsigned long) mapping << (PAGE_SHIFT - 8);
88         return offset & 0x3FF000;
89 }
90 #endif
91
92 static unsigned long get_shared_area(struct address_space *mapping,
93                 unsigned long addr, unsigned long len, unsigned long pgoff)
94 {
95         struct vm_area_struct *vma;
96         int offset = get_offset(mapping);
97
98         addr = DCACHE_ALIGN(addr - offset) + offset;
99
100         for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
101                 /* At this point:  (!vma || addr < vma->vm_end). */
102                 if (TASK_SIZE - len < addr)
103                         return -ENOMEM;
104                 if (!vma || addr + len <= vma->vm_start)
105                         return addr;
106                 addr = DCACHE_ALIGN(vma->vm_end - offset) + offset;
107                 if (addr < vma->vm_end) /* handle wraparound */
108                         return -ENOMEM;
109         }
110 }
111
112 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
113                 unsigned long len, unsigned long pgoff, unsigned long flags)
114 {
115         if (len > TASK_SIZE)
116                 return -ENOMEM;
117         if (!addr)
118                 addr = TASK_UNMAPPED_BASE;
119
120         if (filp && (flags & MAP_SHARED)) {
121                 addr = get_shared_area(filp->f_mapping, addr, len, pgoff);
122         } else {
123                 addr = get_unshared_area(addr, len);
124         }
125         return addr;
126 }
127
128 static unsigned long do_mmap2(unsigned long addr, unsigned long len,
129         unsigned long prot, unsigned long flags, unsigned long fd,
130         unsigned long pgoff)
131 {
132         struct file * file = NULL;
133         unsigned long error = -EBADF;
134         if (!(flags & MAP_ANONYMOUS)) {
135                 file = fget(fd);
136                 if (!file)
137                         goto out;
138         }
139
140         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
141
142         down_write(&current->mm->mmap_sem);
143         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
144         up_write(&current->mm->mmap_sem);
145
146         if (file != NULL)
147                 fput(file);
148 out:
149         return error;
150 }
151
152 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
153         unsigned long prot, unsigned long flags, unsigned long fd,
154         unsigned long pgoff)
155 {
156         /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
157            we have. */
158         return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT - 12));
159 }
160
161 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
162                 unsigned long prot, unsigned long flags, unsigned long fd,
163                 unsigned long offset)
164 {
165         if (!(offset & ~PAGE_MASK)) {
166                 return do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
167         } else {
168                 return -EINVAL;
169         }
170 }
171
172 long sys_shmat_wrapper(int shmid, char *shmaddr, int shmflag)
173 {
174         unsigned long raddr;
175         int r;
176
177         r = do_shmat(shmid, shmaddr, shmflag, &raddr);
178         if (r < 0)
179                 return r;
180         return raddr;
181 }
182
183 /* Fucking broken ABI */
184
185 #ifdef CONFIG_PARISC64
186 asmlinkage long parisc_truncate64(const char * path,
187                                         unsigned int high, unsigned int low)
188 {
189         return sys_truncate(path, (long)high << 32 | low);
190 }
191
192 asmlinkage long parisc_ftruncate64(unsigned int fd,
193                                         unsigned int high, unsigned int low)
194 {
195         return sys_ftruncate(fd, (long)high << 32 | low);
196 }
197
198 /* stubs for the benefit of the syscall_table since truncate64 and truncate 
199  * are identical on LP64 */
200 asmlinkage long sys_truncate64(const char * path, unsigned long length)
201 {
202         return sys_truncate(path, length);
203 }
204 asmlinkage long sys_ftruncate64(unsigned int fd, unsigned long length)
205 {
206         return sys_ftruncate(fd, length);
207 }
208 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
209 {
210         return sys_fcntl(fd, cmd, arg);
211 }
212 #else
213
214 asmlinkage long parisc_truncate64(const char * path,
215                                         unsigned int high, unsigned int low)
216 {
217         return sys_truncate64(path, (loff_t)high << 32 | low);
218 }
219
220 asmlinkage long parisc_ftruncate64(unsigned int fd,
221                                         unsigned int high, unsigned int low)
222 {
223         return sys_ftruncate64(fd, (loff_t)high << 32 | low);
224 }
225 #endif
226
227 asmlinkage ssize_t parisc_pread64(unsigned int fd, char *buf, size_t count,
228                                         unsigned int high, unsigned int low)
229 {
230         return sys_pread64(fd, buf, count, (loff_t)high << 32 | low);
231 }
232
233 asmlinkage ssize_t parisc_pwrite64(unsigned int fd, const char *buf,
234                         size_t count, unsigned int high, unsigned int low)
235 {
236         return sys_pwrite64(fd, buf, count, (loff_t)high << 32 | low);
237 }
238
239 asmlinkage ssize_t parisc_readahead(int fd, unsigned int high, unsigned int low,
240                                     size_t count)
241 {
242         return sys_readahead(fd, (loff_t)high << 32 | low, count);
243 }
244
245 asmlinkage unsigned long sys_alloc_hugepages(int key, unsigned long addr, unsigned long len, int prot, int flag)
246 {
247         return -ENOMEM;
248 }
249
250 asmlinkage int sys_free_hugepages(unsigned long addr)
251 {
252         return -EINVAL;
253 }