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