1 /*
2  * sys_parisc32.c: Conversion between 32bit and 64bit native syscalls.
3  *
4  * Copyright (C) 2000-2001 Hewlett Packard Company
5  * Copyright (C) 2000 John Marvin
6  * Copyright (C) 2001 Matthew Wilcox
7  *
8  * These routines maintain argument size conversion between 32bit and 64bit
9  * environment. Based heavily on sys_ia32.c and sys_sparc32.c.
10  */
11 
12 #include <linux/compat.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/fs.h>
16 #include <linux/mm.h>
17 #include <linux/file.h>
18 #include <linux/signal.h>
19 #include <linux/resource.h>
20 #include <linux/times.h>
21 #include <linux/utsname.h>
22 #include <linux/time.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/sem.h>
26 #include <linux/msg.h>
27 #include <linux/shm.h>
28 #include <linux/slab.h>
29 #include <linux/uio.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/ncp_fs.h>
32 #include <linux/sunrpc/svc.h>
33 #include <linux/nfsd/nfsd.h>
34 #include <linux/nfsd/cache.h>
35 #include <linux/nfsd/xdr.h>
36 #include <linux/nfsd/syscall.h>
37 #include <linux/poll.h>
38 #include <linux/personality.h>
39 #include <linux/stat.h>
40 #include <linux/highmem.h>
41 #include <linux/highuid.h>
42 #include <linux/mman.h>
43 #include <linux/binfmts.h>
44 #include <linux/namei.h>
45 #include <linux/vfs.h>
46 #include <linux/ptrace.h>
47 #include <linux/swap.h>
48 #include <linux/syscalls.h>
49 
50 #include <asm/types.h>
51 #include <asm/uaccess.h>
52 #include <asm/mmu_context.h>
53 
54 #include "sys32.h"
55 
56 #undef DEBUG
57 
58 #ifdef DEBUG
59 #define DBG(x)	printk x
60 #else
61 #define DBG(x)
62 #endif
63 
64 /*
65  * sys32_execve() executes a new program.
66  */
67 
68 asmlinkage int sys32_execve(struct pt_regs *regs)
69 {
70 	int error;
71 	char *filename;
72 
73 	DBG(("sys32_execve(%p) r26 = 0x%lx\n", regs, regs->gr[26]));
74 	filename = getname((const char __user *) regs->gr[26]);
75 	error = PTR_ERR(filename);
76 	if (IS_ERR(filename))
77 		goto out;
78 	error = compat_do_execve(filename, compat_ptr(regs->gr[25]),
79 				 compat_ptr(regs->gr[24]), regs);
80 	if (error == 0) {
81 		task_lock(current);
82 		current->ptrace &= ~PT_DTRACE;
83 		task_unlock(current);
84 	}
85 	putname(filename);
86 out:
87 
88 	return error;
89 }
90 
91 asmlinkage long sys32_unimplemented(int r26, int r25, int r24, int r23,
92 	int r22, int r21, int r20)
93 {
94     printk(KERN_ERR "%s(%d): Unimplemented 32 on 64 syscall #%d!\n",
95     	current->comm, current->pid, r20);
96     return -ENOSYS;
97 }
98 
99 #ifdef CONFIG_SYSCTL
100 
101 struct __sysctl_args32 {
102 	u32 name;
103 	int nlen;
104 	u32 oldval;
105 	u32 oldlenp;
106 	u32 newval;
107 	u32 newlen;
108 	u32 __unused[4];
109 };
110 
111 asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
112 {
113 #ifndef CONFIG_SYSCTL_SYSCALL
114 	return -ENOSYS;
115 #else
116 	struct __sysctl_args32 tmp;
117 	int error;
118 	unsigned int oldlen32;
119 	size_t oldlen, __user *oldlenp = NULL;
120 	unsigned long addr = (((long __force)&args->__unused[0]) + 7) & ~7;
121 
122 	DBG(("sysctl32(%p)\n", args));
123 
124 	if (copy_from_user(&tmp, args, sizeof(tmp)))
125 		return -EFAULT;
126 
127 	if (tmp.oldval && tmp.oldlenp) {
128 		/* Duh, this is ugly and might not work if sysctl_args
129 		   is in read-only memory, but do_sysctl does indirectly
130 		   a lot of uaccess in both directions and we'd have to
131 		   basically copy the whole sysctl.c here, and
132 		   glibc's __sysctl uses rw memory for the structure
133 		   anyway.  */
134 		/* a possibly better hack than this, which will avoid the
135 		 * problem if the struct is read only, is to push the
136 		 * 'oldlen' value out to the user's stack instead. -PB
137 		 */
138 		if (get_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
139 			return -EFAULT;
140 		oldlen = oldlen32;
141 		if (put_user(oldlen, (size_t *)addr))
142 			return -EFAULT;
143 		oldlenp = (size_t *)addr;
144 	}
145 
146 	lock_kernel();
147 	error = do_sysctl((int __user *)(u64)tmp.name, tmp.nlen,
148 			  (void __user *)(u64)tmp.oldval, oldlenp,
149 			  (void __user *)(u64)tmp.newval, tmp.newlen);
150 	unlock_kernel();
151 	if (oldlenp) {
152 		if (!error) {
153 			if (get_user(oldlen, (size_t *)addr)) {
154 				error = -EFAULT;
155 			} else {
156 				oldlen32 = oldlen;
157 				if (put_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
158 					error = -EFAULT;
159 			}
160 		}
161 		if (copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused)))
162 			error = -EFAULT;
163 	}
164 	return error;
165 #endif
166 }
167 
168 #endif /* CONFIG_SYSCTL */
169 
170 asmlinkage long sys32_sched_rr_get_interval(pid_t pid,
171 	struct compat_timespec __user *interval)
172 {
173 	struct timespec t;
174 	int ret;
175 
176 	KERNEL_SYSCALL(ret, sys_sched_rr_get_interval, pid, (struct timespec __user *)&t);
177 	if (put_compat_timespec(&t, interval))
178 		return -EFAULT;
179 	return ret;
180 }
181 
182 static int
183 put_compat_timeval(struct compat_timeval __user *u, struct timeval *t)
184 {
185 	struct compat_timeval t32;
186 	t32.tv_sec = t->tv_sec;
187 	t32.tv_usec = t->tv_usec;
188 	return copy_to_user(u, &t32, sizeof t32);
189 }
190 
191 static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
192 {
193 	long usec;
194 
195 	if (__get_user(o->tv_sec, &i->tv_sec))
196 		return -EFAULT;
197 	if (__get_user(usec, &i->tv_usec))
198 		return -EFAULT;
199 	o->tv_nsec = usec * 1000;
200 	return 0;
201 }
202 
203 asmlinkage int
204 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
205 {
206     extern void do_gettimeofday(struct timeval *tv);
207 
208     if (tv) {
209 	    struct timeval ktv;
210 	    do_gettimeofday(&ktv);
211 	    if (put_compat_timeval(tv, &ktv))
212 		    return -EFAULT;
213     }
214     if (tz) {
215 	    extern struct timezone sys_tz;
216 	    if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
217 		    return -EFAULT;
218     }
219     return 0;
220 }
221 
222 asmlinkage
223 int sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
224 {
225 	struct timespec kts;
226 	struct timezone ktz;
227 
228  	if (tv) {
229 		if (get_ts32(&kts, tv))
230 			return -EFAULT;
231 	}
232 	if (tz) {
233 		if (copy_from_user(&ktz, tz, sizeof(ktz)))
234 			return -EFAULT;
235 	}
236 
237 	return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
238 }
239 
240 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
241 {
242 	compat_ino_t ino;
243 	int err;
244 
245 	if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
246 	    !new_valid_dev(stat->rdev))
247 		return -EOVERFLOW;
248 
249 	ino = stat->ino;
250 	if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
251 		return -EOVERFLOW;
252 
253 	err  = put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
254 	err |= put_user(ino, &statbuf->st_ino);
255 	err |= put_user(stat->mode, &statbuf->st_mode);
256 	err |= put_user(stat->nlink, &statbuf->st_nlink);
257 	err |= put_user(0, &statbuf->st_reserved1);
258 	err |= put_user(0, &statbuf->st_reserved2);
259 	err |= put_user(new_encode_dev(stat->rdev), &statbuf->st_rdev);
260 	err |= put_user(stat->size, &statbuf->st_size);
261 	err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
262 	err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
263 	err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
264 	err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
265 	err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
266 	err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
267 	err |= put_user(stat->blksize, &statbuf->st_blksize);
268 	err |= put_user(stat->blocks, &statbuf->st_blocks);
269 	err |= put_user(0, &statbuf->__unused1);
270 	err |= put_user(0, &statbuf->__unused2);
271 	err |= put_user(0, &statbuf->__unused3);
272 	err |= put_user(0, &statbuf->__unused4);
273 	err |= put_user(0, &statbuf->__unused5);
274 	err |= put_user(0, &statbuf->st_fstype); /* not avail */
275 	err |= put_user(0, &statbuf->st_realdev); /* not avail */
276 	err |= put_user(0, &statbuf->st_basemode); /* not avail */
277 	err |= put_user(0, &statbuf->st_spareshort);
278 	err |= put_user(stat->uid, &statbuf->st_uid);
279 	err |= put_user(stat->gid, &statbuf->st_gid);
280 	err |= put_user(0, &statbuf->st_spare4[0]);
281 	err |= put_user(0, &statbuf->st_spare4[1]);
282 	err |= put_user(0, &statbuf->st_spare4[2]);
283 
284 	return err;
285 }
286 
287 /*** copied from mips64 ***/
288 /*
289  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
290  * 64-bit unsigned longs.
291  */
292 
293 static inline int
294 get_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
295 {
296 	n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
297 	if (ufdset) {
298 		unsigned long odd;
299 
300 		if (!access_ok(VERIFY_WRITE, ufdset, n*sizeof(u32)))
301 			return -EFAULT;
302 
303 		odd = n & 1UL;
304 		n &= ~1UL;
305 		while (n) {
306 			unsigned long h, l;
307 			__get_user(l, ufdset);
308 			__get_user(h, ufdset+1);
309 			ufdset += 2;
310 			*fdset++ = h << 32 | l;
311 			n -= 2;
312 		}
313 		if (odd)
314 			__get_user(*fdset, ufdset);
315 	} else {
316 		/* Tricky, must clear full unsigned long in the
317 		 * kernel fdset at the end, this makes sure that
318 		 * actually happens.
319 		 */
320 		memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32));
321 	}
322 	return 0;
323 }
324 
325 static inline void
326 set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
327 {
328 	unsigned long odd;
329 	n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
330 
331 	if (!ufdset)
332 		return;
333 
334 	odd = n & 1UL;
335 	n &= ~1UL;
336 	while (n) {
337 		unsigned long h, l;
338 		l = *fdset++;
339 		h = l >> 32;
340 		__put_user(l, ufdset);
341 		__put_user(h, ufdset+1);
342 		ufdset += 2;
343 		n -= 2;
344 	}
345 	if (odd)
346 		__put_user(*fdset, ufdset);
347 }
348 
349 struct msgbuf32 {
350     int mtype;
351     char mtext[1];
352 };
353 
354 asmlinkage long sys32_msgsnd(int msqid,
355 				struct msgbuf32 __user *umsgp32,
356 				size_t msgsz, int msgflg)
357 {
358 	struct msgbuf *mb;
359 	struct msgbuf32 mb32;
360 	int err;
361 
362 	if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
363 		return -ENOMEM;
364 
365 	err = get_user(mb32.mtype, &umsgp32->mtype);
366 	mb->mtype = mb32.mtype;
367 	err |= copy_from_user(mb->mtext, &umsgp32->mtext, msgsz);
368 
369 	if (err)
370 		err = -EFAULT;
371 	else
372 		KERNEL_SYSCALL(err, sys_msgsnd, msqid, (struct msgbuf __user *)mb, msgsz, msgflg);
373 
374 	kfree(mb);
375 	return err;
376 }
377 
378 asmlinkage long sys32_msgrcv(int msqid,
379 				struct msgbuf32 __user *umsgp32,
380 				size_t msgsz, long msgtyp, int msgflg)
381 {
382 	struct msgbuf *mb;
383 	struct msgbuf32 mb32;
384 	int err, len;
385 
386 	if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
387 		return -ENOMEM;
388 
389 	KERNEL_SYSCALL(err, sys_msgrcv, msqid, (struct msgbuf __user *)mb, msgsz, msgtyp, msgflg);
390 
391 	if (err >= 0) {
392 		len = err;
393 		mb32.mtype = mb->mtype;
394 		err = put_user(mb32.mtype, &umsgp32->mtype);
395 		err |= copy_to_user(&umsgp32->mtext, mb->mtext, len);
396 		if (err)
397 			err = -EFAULT;
398 		else
399 			err = len;
400 	}
401 
402 	kfree(mb);
403 	return err;
404 }
405 
406 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
407 {
408         mm_segment_t old_fs = get_fs();
409         int ret;
410         off_t of;
411 
412         if (offset && get_user(of, offset))
413                 return -EFAULT;
414 
415         set_fs(KERNEL_DS);
416         ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
417         set_fs(old_fs);
418 
419         if (offset && put_user(of, offset))
420                 return -EFAULT;
421 
422         return ret;
423 }
424 
425 asmlinkage int sys32_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count)
426 {
427 	mm_segment_t old_fs = get_fs();
428 	int ret;
429 	loff_t lof;
430 
431 	if (offset && get_user(lof, offset))
432 		return -EFAULT;
433 
434 	set_fs(KERNEL_DS);
435 	ret = sys_sendfile64(out_fd, in_fd, offset ? (loff_t __user *)&lof : NULL, count);
436 	set_fs(old_fs);
437 
438 	if (offset && put_user(lof, offset))
439 		return -EFAULT;
440 
441 	return ret;
442 }
443 
444 
445 /* lseek() needs a wrapper because 'offset' can be negative, but the top
446  * half of the argument has been zeroed by syscall.S.
447  */
448 
449 asmlinkage int sys32_lseek(unsigned int fd, int offset, unsigned int origin)
450 {
451 	return sys_lseek(fd, offset, origin);
452 }
453 
454 asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg)
455 {
456         union semun u;
457 
458         if (cmd == SETVAL) {
459                 /* Ugh.  arg is a union of int,ptr,ptr,ptr, so is 8 bytes.
460                  * The int should be in the first 4, but our argument
461                  * frobbing has left it in the last 4.
462                  */
463                 u.val = *((int *)&arg + 1);
464                 return sys_semctl (semid, semnum, cmd, u);
465 	}
466 	return sys_semctl (semid, semnum, cmd, arg);
467 }
468 
469 long sys32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf,
470 			  size_t len)
471 {
472 	return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
473 				  buf, len);
474 }
475 
476 asmlinkage long compat_sys_fallocate(int fd, int mode, u32 offhi, u32 offlo,
477 				u32 lenhi, u32 lenlo)
478 {
479         return sys_fallocate(fd, mode, ((loff_t)offhi << 32) | offlo,
480                              ((loff_t)lenhi << 32) | lenlo);
481 }
482