Merge git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6] / 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/sem.h>
16 #include <linux/msg.h>
17 #include <linux/shm.h>
18 #include <linux/stat.h>
19 #include <linux/syscalls.h>
20 #include <linux/mman.h>
21 #include <linux/file.h>
22 #include <linux/utsname.h>
23 #include <linux/module.h>
24 #include <asm/cacheflush.h>
25 #include <asm/uaccess.h>
26 #include <asm/ipc.h>
27 #include <asm/unistd.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         struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
38         int fd[2];
39         int error;
40
41         error = do_pipe(fd);
42         if (!error) {
43                 regs->regs[1] = fd[1];
44                 return fd[0];
45         }
46         return error;
47 }
48
49 unsigned long shm_align_mask = PAGE_SIZE - 1;   /* Sane caches */
50
51 EXPORT_SYMBOL(shm_align_mask);
52
53 #ifdef CONFIG_MMU
54 /*
55  * To avoid cache aliases, we map the shared page with same color.
56  */
57 #define COLOUR_ALIGN(addr, pgoff)                               \
58         ((((addr) + shm_align_mask) & ~shm_align_mask) +        \
59          (((pgoff) << PAGE_SHIFT) & shm_align_mask))
60
61 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
62         unsigned long len, unsigned long pgoff, unsigned long flags)
63 {
64         struct mm_struct *mm = current->mm;
65         struct vm_area_struct *vma;
66         unsigned long start_addr;
67         int do_colour_align;
68
69         if (flags & MAP_FIXED) {
70                 /* We do not accept a shared mapping if it would violate
71                  * cache aliasing constraints.
72                  */
73                 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
74                         return -EINVAL;
75                 return addr;
76         }
77
78         if (unlikely(len > TASK_SIZE))
79                 return -ENOMEM;
80
81         do_colour_align = 0;
82         if (filp || (flags & MAP_SHARED))
83                 do_colour_align = 1;
84
85         if (addr) {
86                 if (do_colour_align)
87                         addr = COLOUR_ALIGN(addr, pgoff);
88                 else
89                         addr = PAGE_ALIGN(addr);
90
91                 vma = find_vma(mm, addr);
92                 if (TASK_SIZE - len >= addr &&
93                     (!vma || addr + len <= vma->vm_start))
94                         return addr;
95         }
96
97         if (len > mm->cached_hole_size) {
98                 start_addr = addr = mm->free_area_cache;
99         } else {
100                 mm->cached_hole_size = 0;
101                 start_addr = addr = TASK_UNMAPPED_BASE;
102         }
103
104 full_search:
105         if (do_colour_align)
106                 addr = COLOUR_ALIGN(addr, pgoff);
107         else
108                 addr = PAGE_ALIGN(mm->free_area_cache);
109
110         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
111                 /* At this point:  (!vma || addr < vma->vm_end). */
112                 if (unlikely(TASK_SIZE - len < addr)) {
113                         /*
114                          * Start a new search - just in case we missed
115                          * some holes.
116                          */
117                         if (start_addr != TASK_UNMAPPED_BASE) {
118                                 start_addr = addr = TASK_UNMAPPED_BASE;
119                                 mm->cached_hole_size = 0;
120                                 goto full_search;
121                         }
122                         return -ENOMEM;
123                 }
124                 if (likely(!vma || addr + len <= vma->vm_start)) {
125                         /*
126                          * Remember the place where we stopped the search:
127                          */
128                         mm->free_area_cache = addr + len;
129                         return addr;
130                 }
131                 if (addr + mm->cached_hole_size < vma->vm_start)
132                         mm->cached_hole_size = vma->vm_start - addr;
133
134                 addr = vma->vm_end;
135                 if (do_colour_align)
136                         addr = COLOUR_ALIGN(addr, pgoff);
137         }
138 }
139 #endif /* CONFIG_MMU */
140
141 static inline long
142 do_mmap2(unsigned long addr, unsigned long len, unsigned long prot, 
143          unsigned long flags, int fd, unsigned long pgoff)
144 {
145         int error = -EBADF;
146         struct file *file = NULL;
147
148         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
149         if (!(flags & MAP_ANONYMOUS)) {
150                 file = fget(fd);
151                 if (!file)
152                         goto out;
153         }
154
155         down_write(&current->mm->mmap_sem);
156         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
157         up_write(&current->mm->mmap_sem);
158
159         if (file)
160                 fput(file);
161 out:
162         return error;
163 }
164
165 asmlinkage int old_mmap(unsigned long addr, unsigned long len,
166         unsigned long prot, unsigned long flags,
167         int fd, unsigned long off)
168 {
169         if (off & ~PAGE_MASK)
170                 return -EINVAL;
171         return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
172 }
173
174 asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
175         unsigned long prot, unsigned long flags,
176         unsigned long fd, unsigned long pgoff)
177 {
178         return do_mmap2(addr, len, prot, flags, fd, pgoff);
179 }
180
181 /*
182  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
183  *
184  * This is really horribly ugly.
185  */
186 asmlinkage int sys_ipc(uint call, int first, int second,
187                        int third, void __user *ptr, long fifth)
188 {
189         int version, ret;
190
191         version = call >> 16; /* hack for backward compatibility */
192         call &= 0xffff;
193
194         if (call <= SEMCTL)
195                 switch (call) {
196                 case SEMOP:
197                         return sys_semtimedop(first, (struct sembuf __user *)ptr,
198                                               second, NULL);
199                 case SEMTIMEDOP:
200                         return sys_semtimedop(first, (struct sembuf __user *)ptr,
201                                               second,
202                                               (const struct timespec __user *)fifth);
203                 case SEMGET:
204                         return sys_semget (first, second, third);
205                 case SEMCTL: {
206                         union semun fourth;
207                         if (!ptr)
208                                 return -EINVAL;
209                         if (get_user(fourth.__pad, (void * __user *) ptr))
210                                 return -EFAULT;
211                         return sys_semctl (first, second, third, fourth);
212                         }
213                 default:
214                         return -EINVAL;
215                 }
216
217         if (call <= MSGCTL) 
218                 switch (call) {
219                 case MSGSND:
220                         return sys_msgsnd (first, (struct msgbuf __user *) ptr, 
221                                           second, third);
222                 case MSGRCV:
223                         switch (version) {
224                         case 0: {
225                                 struct ipc_kludge tmp;
226                                 if (!ptr)
227                                         return -EINVAL;
228                                 
229                                 if (copy_from_user(&tmp,
230                                                    (struct ipc_kludge __user *) ptr, 
231                                                    sizeof (tmp)))
232                                         return -EFAULT;
233                                 return sys_msgrcv (first, tmp.msgp, second,
234                                                    tmp.msgtyp, third);
235                                 }
236                         default:
237                                 return sys_msgrcv (first,
238                                                    (struct msgbuf __user *) ptr,
239                                                    second, fifth, third);
240                         }
241                 case MSGGET:
242                         return sys_msgget ((key_t) first, second);
243                 case MSGCTL:
244                         return sys_msgctl (first, second,
245                                            (struct msqid_ds __user *) ptr);
246                 default:
247                         return -EINVAL;
248                 }
249         if (call <= SHMCTL) 
250                 switch (call) {
251                 case SHMAT:
252                         switch (version) {
253                         default: {
254                                 ulong raddr;
255                                 ret = do_shmat (first, (char __user *) ptr,
256                                                  second, &raddr);
257                                 if (ret)
258                                         return ret;
259                                 return put_user (raddr, (ulong __user *) third);
260                         }
261                         case 1: /* iBCS2 emulator entry point */
262                                 if (!segment_eq(get_fs(), get_ds()))
263                                         return -EINVAL;
264                                 return do_shmat (first, (char __user *) ptr,
265                                                   second, (ulong *) third);
266                         }
267                 case SHMDT: 
268                         return sys_shmdt ((char __user *)ptr);
269                 case SHMGET:
270                         return sys_shmget (first, second, third);
271                 case SHMCTL:
272                         return sys_shmctl (first, second,
273                                            (struct shmid_ds __user *) ptr);
274                 default:
275                         return -EINVAL;
276                 }
277         
278         return -EINVAL;
279 }
280
281 asmlinkage int sys_uname(struct old_utsname * name)
282 {
283         int err;
284         if (!name)
285                 return -EFAULT;
286         down_read(&uts_sem);
287         err = copy_to_user(name, utsname(), sizeof (*name));
288         up_read(&uts_sem);
289         return err?-EFAULT:0;
290 }
291
292 asmlinkage ssize_t sys_pread_wrapper(unsigned int fd, char * buf,
293                              size_t count, long dummy, loff_t pos)
294 {
295         return sys_pread64(fd, buf, count, pos);
296 }
297
298 asmlinkage ssize_t sys_pwrite_wrapper(unsigned int fd, const char * buf,
299                               size_t count, long dummy, loff_t pos)
300 {
301         return sys_pwrite64(fd, buf, count, pos);
302 }
303
304 asmlinkage int sys_fadvise64_64_wrapper(int fd, u32 offset0, u32 offset1,
305                                 u32 len0, u32 len1, int advice)
306 {
307 #ifdef  __LITTLE_ENDIAN__
308         return sys_fadvise64_64(fd, (u64)offset1 << 32 | offset0,
309                                 (u64)len1 << 32 | len0, advice);
310 #else
311         return sys_fadvise64_64(fd, (u64)offset0 << 32 | offset1,
312                                 (u64)len0 << 32 | len1, advice);
313 #endif
314 }
315
316 #if defined(CONFIG_CPU_SH2) || defined(CONFIG_CPU_SH2A)
317 #define SYSCALL_ARG3    "trapa #0x23"
318 #else
319 #define SYSCALL_ARG3    "trapa #0x13"
320 #endif
321
322 /*
323  * Do a system call from kernel instead of calling sys_execve so we
324  * end up with proper pt_regs.
325  */
326 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
327 {
328         register long __sc0 __asm__ ("r3") = __NR_execve;
329         register long __sc4 __asm__ ("r4") = (long) filename;
330         register long __sc5 __asm__ ("r5") = (long) argv;
331         register long __sc6 __asm__ ("r6") = (long) envp;
332         __asm__ __volatile__ (SYSCALL_ARG3 : "=z" (__sc0)       
333                         : "0" (__sc0), "r" (__sc4), "r" (__sc5), "r" (__sc6)
334                         : "memory");
335         return __sc0;
336 }