Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / sh / kernel / sys_sh.c
1 /*
2  * linux/arch/sh/kernel/sys_sh.c
3  *
4  * This file contains various random system calls that
5  * have a non-standard calling sequence on the Linux/SuperH
6  * platform.
7  *
8  * Taken from i386 version.
9  */
10
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
16 #include <linux/sem.h>
17 #include <linux/msg.h>
18 #include <linux/shm.h>
19 #include <linux/stat.h>
20 #include <linux/syscalls.h>
21 #include <linux/mman.h>
22 #include <linux/file.h>
23 #include <linux/utsname.h>
24 #include <linux/vs_cvirt.h>
25
26 #include <asm/uaccess.h>
27 #include <asm/ipc.h>
28
29 /*
30  * sys_pipe() is the normal C calling standard for creating
31  * a pipe. It's not the way Unix traditionally does this, though.
32  */
33 asmlinkage int sys_pipe(unsigned long r4, unsigned long r5,
34         unsigned long r6, unsigned long r7,
35         struct pt_regs regs)
36 {
37         int fd[2];
38         int error;
39
40         error = do_pipe(fd);
41         if (!error) {
42                 regs.regs[1] = fd[1];
43                 return fd[0];
44         }
45         return error;
46 }
47
48 #if defined(HAVE_ARCH_UNMAPPED_AREA)
49 /*
50  * To avoid cache alias, we map the shard page with same color.
51  */
52 #define COLOUR_ALIGN(addr)      (((addr)+SHMLBA-1)&~(SHMLBA-1))
53
54 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
55         unsigned long len, unsigned long pgoff, unsigned long flags)
56 {
57         struct mm_struct *mm = current->mm;
58         struct vm_area_struct *vma;
59         unsigned long start_addr;
60
61         if (flags & MAP_FIXED) {
62                 /* We do not accept a shared mapping if it would violate
63                  * cache aliasing constraints.
64                  */
65                 if ((flags & MAP_SHARED) && (addr & (SHMLBA - 1)))
66                         return -EINVAL;
67                 return addr;
68         }
69
70         if (len > TASK_SIZE)
71                 return -ENOMEM;
72
73         if (addr) {
74                 if (flags & MAP_PRIVATE)
75                         addr = PAGE_ALIGN(addr);
76                 else
77                         addr = COLOUR_ALIGN(addr);
78                 vma = find_vma(mm, addr);
79                 if (TASK_SIZE - len >= addr &&
80                     (!vma || addr + len <= vma->vm_start))
81                         return addr;
82         }
83         if (len <= mm->cached_hole_size) {
84                 mm->cached_hole_size = 0;
85                 mm->free_area_cache = TASK_UNMAPPED_BASE;
86         }
87         if (flags & MAP_PRIVATE)
88                 addr = PAGE_ALIGN(mm->free_area_cache);
89         else
90                 addr = COLOUR_ALIGN(mm->free_area_cache);
91         start_addr = addr;
92
93 full_search:
94         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
95                 /* At this point:  (!vma || addr < vma->vm_end). */
96                 if (TASK_SIZE - len < addr) {
97                         /*
98                          * Start a new search - just in case we missed
99                          * some holes.
100                          */
101                         if (start_addr != TASK_UNMAPPED_BASE) {
102                                 start_addr = addr = TASK_UNMAPPED_BASE;
103                                 mm->cached_hole_size = 0;
104                                 goto full_search;
105                         }
106                         return -ENOMEM;
107                 }
108                 if (!vma || addr + len <= vma->vm_start) {
109                         /*
110                          * Remember the place where we stopped the search:
111                          */
112                         mm->free_area_cache = addr + len;
113                         return addr;
114                 }
115                 if (addr + mm->cached_hole_size < vma->vm_start)
116                         mm->cached_hole_size = vma->vm_start - addr;
117
118                 addr = vma->vm_end;
119                 if (!(flags & MAP_PRIVATE))
120                         addr = COLOUR_ALIGN(addr);
121         }
122 }
123 #endif
124
125 static inline long
126 do_mmap2(unsigned long addr, unsigned long len, unsigned long prot, 
127          unsigned long flags, int fd, unsigned long pgoff)
128 {
129         int error = -EBADF;
130         struct file *file = NULL;
131
132         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
133         if (!(flags & MAP_ANONYMOUS)) {
134                 file = fget(fd);
135                 if (!file)
136                         goto out;
137         }
138
139         down_write(&current->mm->mmap_sem);
140         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
141         up_write(&current->mm->mmap_sem);
142
143         if (file)
144                 fput(file);
145 out:
146         return error;
147 }
148
149 asmlinkage int old_mmap(unsigned long addr, unsigned long len,
150         unsigned long prot, unsigned long flags,
151         int fd, unsigned long off)
152 {
153         if (off & ~PAGE_MASK)
154                 return -EINVAL;
155         return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
156 }
157
158 asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
159         unsigned long prot, unsigned long flags,
160         unsigned long fd, unsigned long pgoff)
161 {
162         return do_mmap2(addr, len, prot, flags, fd, pgoff);
163 }
164
165 /*
166  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
167  *
168  * This is really horribly ugly.
169  */
170 asmlinkage int sys_ipc(uint call, int first, int second,
171                        int third, void __user *ptr, long fifth)
172 {
173         int version, ret;
174
175         version = call >> 16; /* hack for backward compatibility */
176         call &= 0xffff;
177
178         if (call <= SEMCTL)
179                 switch (call) {
180                 case SEMOP:
181                         return sys_semtimedop(first, (struct sembuf __user *)ptr,
182                                               second, NULL);
183                 case SEMTIMEDOP:
184                         return sys_semtimedop(first, (struct sembuf __user *)ptr,
185                                               second,
186                                               (const struct timespec __user *)fifth);
187                 case SEMGET:
188                         return sys_semget (first, second, third);
189                 case SEMCTL: {
190                         union semun fourth;
191                         if (!ptr)
192                                 return -EINVAL;
193                         if (get_user(fourth.__pad, (void * __user *) ptr))
194                                 return -EFAULT;
195                         return sys_semctl (first, second, third, fourth);
196                         }
197                 default:
198                         return -EINVAL;
199                 }
200
201         if (call <= MSGCTL) 
202                 switch (call) {
203                 case MSGSND:
204                         return sys_msgsnd (first, (struct msgbuf __user *) ptr, 
205                                           second, third);
206                 case MSGRCV:
207                         switch (version) {
208                         case 0: {
209                                 struct ipc_kludge tmp;
210                                 if (!ptr)
211                                         return -EINVAL;
212                                 
213                                 if (copy_from_user(&tmp,
214                                                    (struct ipc_kludge __user *) ptr, 
215                                                    sizeof (tmp)))
216                                         return -EFAULT;
217                                 return sys_msgrcv (first, tmp.msgp, second,
218                                                    tmp.msgtyp, third);
219                                 }
220                         default:
221                                 return sys_msgrcv (first,
222                                                    (struct msgbuf __user *) ptr,
223                                                    second, fifth, third);
224                         }
225                 case MSGGET:
226                         return sys_msgget ((key_t) first, second);
227                 case MSGCTL:
228                         return sys_msgctl (first, second,
229                                            (struct msqid_ds __user *) ptr);
230                 default:
231                         return -EINVAL;
232                 }
233         if (call <= SHMCTL) 
234                 switch (call) {
235                 case SHMAT:
236                         switch (version) {
237                         default: {
238                                 ulong raddr;
239                                 ret = do_shmat (first, (char __user *) ptr,
240                                                  second, &raddr);
241                                 if (ret)
242                                         return ret;
243                                 return put_user (raddr, (ulong __user *) third);
244                         }
245                         case 1: /* iBCS2 emulator entry point */
246                                 if (!segment_eq(get_fs(), get_ds()))
247                                         return -EINVAL;
248                                 return do_shmat (first, (char __user *) ptr,
249                                                   second, (ulong *) third);
250                         }
251                 case SHMDT: 
252                         return sys_shmdt ((char __user *)ptr);
253                 case SHMGET:
254                         return sys_shmget (first, second, third);
255                 case SHMCTL:
256                         return sys_shmctl (first, second,
257                                            (struct shmid_ds __user *) ptr);
258                 default:
259                         return -EINVAL;
260                 }
261         
262         return -EINVAL;
263 }
264
265 asmlinkage int sys_uname(struct old_utsname * name)
266 {
267         int err;
268         if (!name)
269                 return -EFAULT;
270         down_read(&uts_sem);
271         err=copy_to_user(name, vx_new_utsname(), sizeof (*name));
272         up_read(&uts_sem);
273         return err?-EFAULT:0;
274 }
275
276 asmlinkage ssize_t sys_pread_wrapper(unsigned int fd, char * buf,
277                              size_t count, long dummy, loff_t pos)
278 {
279         return sys_pread64(fd, buf, count, pos);
280 }
281
282 asmlinkage ssize_t sys_pwrite_wrapper(unsigned int fd, const char * buf,
283                               size_t count, long dummy, loff_t pos)
284 {
285         return sys_pwrite64(fd, buf, count, pos);
286 }
287
288 asmlinkage int sys_fadvise64_64_wrapper(int fd, u32 offset0, u32 offset1,
289                                 u32 len0, u32 len1, int advice)
290 {
291 #ifdef  __LITTLE_ENDIAN__
292         return sys_fadvise64_64(fd, (u64)offset1 << 32 | offset0,
293                                 (u64)len1 << 32 | len0, advice);
294 #else
295         return sys_fadvise64_64(fd, (u64)offset0 << 32 | offset1,
296                                 (u64)len0 << 32 | len1, advice);
297 #endif
298 }