compat: move cp_compat_stat to common code
[safe/jmp/linux-2.6] / arch / mips / kernel / linux32.c
1 /*
2  * Conversion between 32-bit and 64-bit native system calls.
3  *
4  * Copyright (C) 2000 Silicon Graphics, Inc.
5  * Written by Ulf Carlsson (ulfc@engr.sgi.com)
6  * sys32_execve from ia64/ia32 code, Feb 2000, Kanoj Sarcar (kanoj@sgi.com)
7  */
8 #include <linux/compiler.h>
9 #include <linux/mm.h>
10 #include <linux/errno.h>
11 #include <linux/file.h>
12 #include <linux/smp_lock.h>
13 #include <linux/highuid.h>
14 #include <linux/resource.h>
15 #include <linux/highmem.h>
16 #include <linux/time.h>
17 #include <linux/times.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/skbuff.h>
21 #include <linux/filter.h>
22 #include <linux/shm.h>
23 #include <linux/sem.h>
24 #include <linux/msg.h>
25 #include <linux/icmpv6.h>
26 #include <linux/syscalls.h>
27 #include <linux/sysctl.h>
28 #include <linux/utime.h>
29 #include <linux/utsname.h>
30 #include <linux/personality.h>
31 #include <linux/dnotify.h>
32 #include <linux/module.h>
33 #include <linux/binfmts.h>
34 #include <linux/security.h>
35 #include <linux/compat.h>
36 #include <linux/vfs.h>
37 #include <linux/ipc.h>
38
39 #include <net/sock.h>
40 #include <net/scm.h>
41
42 #include <asm/compat-signal.h>
43 #include <asm/sim.h>
44 #include <asm/uaccess.h>
45 #include <asm/mmu_context.h>
46 #include <asm/mman.h>
47
48 /* Use this to get at 32-bit user passed pointers. */
49 /* A() macro should be used for places where you e.g.
50    have some internal variable u32 and just want to get
51    rid of a compiler warning. AA() has to be used in
52    places where you want to convert a function argument
53    to 32bit pointer or when you e.g. access pt_regs
54    structure and want to consider 32bit registers only.
55  */
56 #define A(__x) ((unsigned long)(__x))
57 #define AA(__x) ((unsigned long)((int)__x))
58
59 #ifdef __MIPSEB__
60 #define merge_64(r1, r2) ((((r1) & 0xffffffffUL) << 32) + ((r2) & 0xffffffffUL))
61 #endif
62 #ifdef __MIPSEL__
63 #define merge_64(r1, r2) ((((r2) & 0xffffffffUL) << 32) + ((r1) & 0xffffffffUL))
64 #endif
65
66 asmlinkage unsigned long
67 sys32_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
68          unsigned long flags, unsigned long fd, unsigned long pgoff)
69 {
70         struct file * file = NULL;
71         unsigned long error;
72
73         error = -EINVAL;
74         if (pgoff & (~PAGE_MASK >> 12))
75                 goto out;
76         pgoff >>= PAGE_SHIFT-12;
77
78         if (!(flags & MAP_ANONYMOUS)) {
79                 error = -EBADF;
80                 file = fget(fd);
81                 if (!file)
82                         goto out;
83         }
84         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
85
86         down_write(&current->mm->mmap_sem);
87         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
88         up_write(&current->mm->mmap_sem);
89         if (file)
90                 fput(file);
91
92 out:
93         return error;
94 }
95
96 /*
97  * sys_execve() executes a new program.
98  */
99 asmlinkage int sys32_execve(nabi_no_regargs struct pt_regs regs)
100 {
101         int error;
102         char * filename;
103
104         filename = getname(compat_ptr(regs.regs[4]));
105         error = PTR_ERR(filename);
106         if (IS_ERR(filename))
107                 goto out;
108         error = compat_do_execve(filename, compat_ptr(regs.regs[5]),
109                                  compat_ptr(regs.regs[6]), &regs);
110         putname(filename);
111
112 out:
113         return error;
114 }
115
116 #define RLIM_INFINITY32 0x7fffffff
117 #define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)
118
119 struct rlimit32 {
120         int     rlim_cur;
121         int     rlim_max;
122 };
123
124 asmlinkage long sys32_truncate64(const char __user * path,
125         unsigned long __dummy, int a2, int a3)
126 {
127         return sys_truncate(path, merge_64(a2, a3));
128 }
129
130 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long __dummy,
131         int a2, int a3)
132 {
133         return sys_ftruncate(fd, merge_64(a2, a3));
134 }
135
136 static inline long
137 get_tv32(struct timeval *o, struct compat_timeval __user *i)
138 {
139         return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
140                 (__get_user(o->tv_sec, &i->tv_sec) |
141                  __get_user(o->tv_usec, &i->tv_usec)));
142 }
143
144 static inline long
145 put_tv32(struct compat_timeval __user *o, struct timeval *i)
146 {
147         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
148                 (__put_user(i->tv_sec, &o->tv_sec) |
149                  __put_user(i->tv_usec, &o->tv_usec)));
150 }
151
152 extern struct timezone sys_tz;
153
154 asmlinkage int
155 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
156 {
157         if (tv) {
158                 struct timeval ktv;
159                 do_gettimeofday(&ktv);
160                 if (put_tv32(tv, &ktv))
161                         return -EFAULT;
162         }
163         if (tz) {
164                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
165                         return -EFAULT;
166         }
167         return 0;
168 }
169
170 static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
171 {
172         long usec;
173
174         if (!access_ok(VERIFY_READ, i, sizeof(*i)))
175                 return -EFAULT;
176         if (__get_user(o->tv_sec, &i->tv_sec))
177                 return -EFAULT;
178         if (__get_user(usec, &i->tv_usec))
179                 return -EFAULT;
180         o->tv_nsec = usec * 1000;
181                 return 0;
182 }
183
184 asmlinkage int
185 sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
186 {
187         struct timespec kts;
188         struct timezone ktz;
189
190         if (tv) {
191                 if (get_ts32(&kts, tv))
192                         return -EFAULT;
193         }
194         if (tz) {
195                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
196                         return -EFAULT;
197         }
198
199         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
200 }
201
202 asmlinkage int sys32_llseek(unsigned int fd, unsigned int offset_high,
203                             unsigned int offset_low, loff_t __user * result,
204                             unsigned int origin)
205 {
206         return sys_llseek(fd, offset_high, offset_low, result, origin);
207 }
208
209 /* From the Single Unix Spec: pread & pwrite act like lseek to pos + op +
210    lseek back to original location.  They fail just like lseek does on
211    non-seekable files.  */
212
213 asmlinkage ssize_t sys32_pread(unsigned int fd, char __user * buf,
214                                size_t count, u32 unused, u64 a4, u64 a5)
215 {
216         return sys_pread64(fd, buf, count, merge_64(a4, a5));
217 }
218
219 asmlinkage ssize_t sys32_pwrite(unsigned int fd, const char __user * buf,
220                                 size_t count, u32 unused, u64 a4, u64 a5)
221 {
222         return sys_pwrite64(fd, buf, count, merge_64(a4, a5));
223 }
224
225 asmlinkage int sys32_sched_rr_get_interval(compat_pid_t pid,
226         struct compat_timespec __user *interval)
227 {
228         struct timespec t;
229         int ret;
230         mm_segment_t old_fs = get_fs();
231
232         set_fs(KERNEL_DS);
233         ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
234         set_fs(old_fs);
235         if (put_user (t.tv_sec, &interval->tv_sec) ||
236             __put_user(t.tv_nsec, &interval->tv_nsec))
237                 return -EFAULT;
238         return ret;
239 }
240
241 #ifdef CONFIG_SYSVIPC
242
243 asmlinkage long
244 sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
245 {
246         int version, err;
247
248         version = call >> 16; /* hack for backward compatibility */
249         call &= 0xffff;
250
251         switch (call) {
252         case SEMOP:
253                 /* struct sembuf is the same on 32 and 64bit :)) */
254                 err = sys_semtimedop(first, compat_ptr(ptr), second, NULL);
255                 break;
256         case SEMTIMEDOP:
257                 err = compat_sys_semtimedop(first, compat_ptr(ptr), second,
258                                             compat_ptr(fifth));
259                 break;
260         case SEMGET:
261                 err = sys_semget(first, second, third);
262                 break;
263         case SEMCTL:
264                 err = compat_sys_semctl(first, second, third, compat_ptr(ptr));
265                 break;
266         case MSGSND:
267                 err = compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
268                 break;
269         case MSGRCV:
270                 err = compat_sys_msgrcv(first, second, fifth, third,
271                                         version, compat_ptr(ptr));
272                 break;
273         case MSGGET:
274                 err = sys_msgget((key_t) first, second);
275                 break;
276         case MSGCTL:
277                 err = compat_sys_msgctl(first, second, compat_ptr(ptr));
278                 break;
279         case SHMAT:
280                 err = compat_sys_shmat(first, second, third, version,
281                                        compat_ptr(ptr));
282                 break;
283         case SHMDT:
284                 err = sys_shmdt(compat_ptr(ptr));
285                 break;
286         case SHMGET:
287                 err = sys_shmget(first, (unsigned)second, third);
288                 break;
289         case SHMCTL:
290                 err = compat_sys_shmctl(first, second, compat_ptr(ptr));
291                 break;
292         default:
293                 err = -EINVAL;
294                 break;
295         }
296
297         return err;
298 }
299
300 #else
301
302 asmlinkage long
303 sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
304 {
305         return -ENOSYS;
306 }
307
308 #endif /* CONFIG_SYSVIPC */
309
310 #ifdef CONFIG_MIPS32_N32
311 asmlinkage long sysn32_semctl(int semid, int semnum, int cmd, u32 arg)
312 {
313         /* compat_sys_semctl expects a pointer to union semun */
314         u32 __user *uptr = compat_alloc_user_space(sizeof(u32));
315         if (put_user(arg, uptr))
316                 return -EFAULT;
317         return compat_sys_semctl(semid, semnum, cmd, uptr);
318 }
319
320 asmlinkage long sysn32_msgsnd(int msqid, u32 msgp, unsigned msgsz, int msgflg)
321 {
322         return compat_sys_msgsnd(msqid, msgsz, msgflg, compat_ptr(msgp));
323 }
324
325 asmlinkage long sysn32_msgrcv(int msqid, u32 msgp, size_t msgsz, int msgtyp,
326                               int msgflg)
327 {
328         return compat_sys_msgrcv(msqid, msgsz, msgtyp, msgflg, IPC_64,
329                                  compat_ptr(msgp));
330 }
331 #endif
332
333 struct sysctl_args32
334 {
335         compat_caddr_t name;
336         int nlen;
337         compat_caddr_t oldval;
338         compat_caddr_t oldlenp;
339         compat_caddr_t newval;
340         compat_size_t newlen;
341         unsigned int __unused[4];
342 };
343
344 #ifdef CONFIG_SYSCTL_SYSCALL
345
346 asmlinkage long sys32_sysctl(struct sysctl_args32 __user *args)
347 {
348         struct sysctl_args32 tmp;
349         int error;
350         size_t oldlen;
351         size_t __user *oldlenp = NULL;
352         unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
353
354         if (copy_from_user(&tmp, args, sizeof(tmp)))
355                 return -EFAULT;
356
357         if (tmp.oldval && tmp.oldlenp) {
358                 /* Duh, this is ugly and might not work if sysctl_args
359                    is in read-only memory, but do_sysctl does indirectly
360                    a lot of uaccess in both directions and we'd have to
361                    basically copy the whole sysctl.c here, and
362                    glibc's __sysctl uses rw memory for the structure
363                    anyway.  */
364                 if (get_user(oldlen, (u32 __user *)A(tmp.oldlenp)) ||
365                     put_user(oldlen, (size_t __user *)addr))
366                         return -EFAULT;
367                 oldlenp = (size_t __user *)addr;
368         }
369
370         lock_kernel();
371         error = do_sysctl((int __user *)A(tmp.name), tmp.nlen, (void __user *)A(tmp.oldval),
372                           oldlenp, (void __user *)A(tmp.newval), tmp.newlen);
373         unlock_kernel();
374         if (oldlenp) {
375                 if (!error) {
376                         if (get_user(oldlen, (size_t __user *)addr) ||
377                             put_user(oldlen, (u32 __user *)A(tmp.oldlenp)))
378                                 error = -EFAULT;
379                 }
380                 copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
381         }
382         return error;
383 }
384
385 #endif /* CONFIG_SYSCTL_SYSCALL */
386
387 asmlinkage long sys32_newuname(struct new_utsname __user * name)
388 {
389         int ret = 0;
390
391         down_read(&uts_sem);
392         if (copy_to_user(name, utsname(), sizeof *name))
393                 ret = -EFAULT;
394         up_read(&uts_sem);
395
396         if (current->personality == PER_LINUX32 && !ret)
397                 if (copy_to_user(name->machine, "mips\0\0\0", 8))
398                         ret = -EFAULT;
399
400         return ret;
401 }
402
403 asmlinkage int sys32_personality(unsigned long personality)
404 {
405         int ret;
406         personality &= 0xffffffff;
407         if (personality(current->personality) == PER_LINUX32 &&
408             personality == PER_LINUX)
409                 personality = PER_LINUX32;
410         ret = sys_personality(personality);
411         if (ret == PER_LINUX32)
412                 ret = PER_LINUX;
413         return ret;
414 }
415
416 /* ustat compatibility */
417 struct ustat32 {
418         compat_daddr_t  f_tfree;
419         compat_ino_t    f_tinode;
420         char            f_fname[6];
421         char            f_fpack[6];
422 };
423
424 extern asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf);
425
426 asmlinkage int sys32_ustat(dev_t dev, struct ustat32 __user * ubuf32)
427 {
428         int err;
429         struct ustat tmp;
430         struct ustat32 tmp32;
431         mm_segment_t old_fs = get_fs();
432
433         set_fs(KERNEL_DS);
434         err = sys_ustat(dev, (struct ustat __user *)&tmp);
435         set_fs(old_fs);
436
437         if (err)
438                 goto out;
439
440         memset(&tmp32, 0, sizeof(struct ustat32));
441         tmp32.f_tfree = tmp.f_tfree;
442         tmp32.f_tinode = tmp.f_tinode;
443
444         err = copy_to_user(ubuf32, &tmp32, sizeof(struct ustat32)) ? -EFAULT : 0;
445
446 out:
447         return err;
448 }
449
450 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset,
451         s32 count)
452 {
453         mm_segment_t old_fs = get_fs();
454         int ret;
455         off_t of;
456
457         if (offset && get_user(of, offset))
458                 return -EFAULT;
459
460         set_fs(KERNEL_DS);
461         ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
462         set_fs(old_fs);
463
464         if (offset && put_user(of, offset))
465                 return -EFAULT;
466
467         return ret;
468 }
469
470 asmlinkage ssize_t sys32_readahead(int fd, u32 pad0, u64 a2, u64 a3,
471                                    size_t count)
472 {
473         return sys_readahead(fd, merge_64(a2, a3), count);
474 }
475
476 asmlinkage long sys32_sync_file_range(int fd, int __pad,
477         unsigned long a2, unsigned long a3,
478         unsigned long a4, unsigned long a5,
479         int flags)
480 {
481         return sys_sync_file_range(fd,
482                         merge_64(a2, a3), merge_64(a4, a5),
483                         flags);
484 }
485
486 asmlinkage long sys32_fadvise64_64(int fd, int __pad,
487         unsigned long a2, unsigned long a3,
488         unsigned long a4, unsigned long a5,
489         int flags)
490 {
491         return sys_fadvise64_64(fd,
492                         merge_64(a2, a3), merge_64(a4, a5),
493                         flags);
494 }
495
496 asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_a2,
497         unsigned offset_a3, unsigned len_a4, unsigned len_a5)
498 {
499         return sys_fallocate(fd, mode, merge_64(offset_a2, offset_a3),
500                              merge_64(len_a4, len_a5));
501 }
502
503 save_static_function(sys32_clone);
504 static int noinline __used
505 _sys32_clone(nabi_no_regargs struct pt_regs regs)
506 {
507         unsigned long clone_flags;
508         unsigned long newsp;
509         int __user *parent_tidptr, *child_tidptr;
510
511         clone_flags = regs.regs[4];
512         newsp = regs.regs[5];
513         if (!newsp)
514                 newsp = regs.regs[29];
515         parent_tidptr = (int __user *) regs.regs[6];
516
517         /* Use __dummy4 instead of getting it off the stack, so that
518            syscall() works.  */
519         child_tidptr = (int __user *) __dummy4;
520         return do_fork(clone_flags, newsp, &regs, 0,
521                        parent_tidptr, child_tidptr);
522 }