xref: /linux/arch/parisc/kernel/sys_parisc32.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
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/config.h>
13 #include <linux/compat.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/fs.h>
17 #include <linux/mm.h>
18 #include <linux/file.h>
19 #include <linux/signal.h>
20 #include <linux/resource.h>
21 #include <linux/times.h>
22 #include <linux/utsname.h>
23 #include <linux/time.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/sem.h>
27 #include <linux/msg.h>
28 #include <linux/shm.h>
29 #include <linux/slab.h>
30 #include <linux/uio.h>
31 #include <linux/nfs_fs.h>
32 #include <linux/ncp_fs.h>
33 #include <linux/sunrpc/svc.h>
34 #include <linux/nfsd/nfsd.h>
35 #include <linux/nfsd/cache.h>
36 #include <linux/nfsd/xdr.h>
37 #include <linux/nfsd/syscall.h>
38 #include <linux/poll.h>
39 #include <linux/personality.h>
40 #include <linux/stat.h>
41 #include <linux/highmem.h>
42 #include <linux/highuid.h>
43 #include <linux/mman.h>
44 #include <linux/binfmts.h>
45 #include <linux/namei.h>
46 #include <linux/vfs.h>
47 #include <linux/ptrace.h>
48 #include <linux/swap.h>
49 #include <linux/syscalls.h>
50 
51 #include <asm/types.h>
52 #include <asm/uaccess.h>
53 #include <asm/semaphore.h>
54 #include <asm/mmu_context.h>
55 
56 #include "sys32.h"
57 
58 #undef DEBUG
59 
60 #ifdef DEBUG
61 #define DBG(x)	printk x
62 #else
63 #define DBG(x)
64 #endif
65 
66 /*
67  * sys32_execve() executes a new program.
68  */
69 
70 asmlinkage int sys32_execve(struct pt_regs *regs)
71 {
72 	int error;
73 	char *filename;
74 
75 	DBG(("sys32_execve(%p) r26 = 0x%lx\n", regs, regs->gr[26]));
76 	filename = getname((const char __user *) regs->gr[26]);
77 	error = PTR_ERR(filename);
78 	if (IS_ERR(filename))
79 		goto out;
80 	error = compat_do_execve(filename, compat_ptr(regs->gr[25]),
81 				 compat_ptr(regs->gr[24]), regs);
82 	if (error == 0) {
83 		task_lock(current);
84 		current->ptrace &= ~PT_DTRACE;
85 		task_unlock(current);
86 	}
87 	putname(filename);
88 out:
89 
90 	return error;
91 }
92 
93 asmlinkage long sys32_unimplemented(int r26, int r25, int r24, int r23,
94 	int r22, int r21, int r20)
95 {
96     printk(KERN_ERR "%s(%d): Unimplemented 32 on 64 syscall #%d!\n",
97     	current->comm, current->pid, r20);
98     return -ENOSYS;
99 }
100 
101 #ifdef CONFIG_SYSCTL
102 
103 struct __sysctl_args32 {
104 	u32 name;
105 	int nlen;
106 	u32 oldval;
107 	u32 oldlenp;
108 	u32 newval;
109 	u32 newlen;
110 	u32 __unused[4];
111 };
112 
113 asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
114 {
115 	struct __sysctl_args32 tmp;
116 	int error;
117 	unsigned int oldlen32;
118 	size_t oldlen, *oldlenp = NULL;
119 	unsigned long addr = (((long __force)&args->__unused[0]) + 7) & ~7;
120 	extern int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
121 	       void *newval, size_t newlen);
122 
123 	DBG(("sysctl32(%p)\n", args));
124 
125 	if (copy_from_user(&tmp, args, sizeof(tmp)))
126 		return -EFAULT;
127 
128 	if (tmp.oldval && tmp.oldlenp) {
129 		/* Duh, this is ugly and might not work if sysctl_args
130 		   is in read-only memory, but do_sysctl does indirectly
131 		   a lot of uaccess in both directions and we'd have to
132 		   basically copy the whole sysctl.c here, and
133 		   glibc's __sysctl uses rw memory for the structure
134 		   anyway.  */
135 		/* a possibly better hack than this, which will avoid the
136 		 * problem if the struct is read only, is to push the
137 		 * 'oldlen' value out to the user's stack instead. -PB
138 		 */
139 		if (get_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
140 			return -EFAULT;
141 		oldlen = oldlen32;
142 		if (put_user(oldlen, (size_t *)addr))
143 			return -EFAULT;
144 		oldlenp = (size_t *)addr;
145 	}
146 
147 	lock_kernel();
148 	error = do_sysctl((int *)(u64)tmp.name, tmp.nlen, (void *)(u64)tmp.oldval,
149 			  oldlenp, (void *)(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[0], tmp.__unused, sizeof(tmp.__unused)))
162 			error = -EFAULT;
163 	}
164 	return error;
165 }
166 
167 #endif /* CONFIG_SYSCTL */
168 
169 asmlinkage long sys32_sched_rr_get_interval(pid_t pid,
170 	struct compat_timespec __user *interval)
171 {
172 	struct timespec t;
173 	int ret;
174 
175 	KERNEL_SYSCALL(ret, sys_sched_rr_get_interval, pid, (struct timespec __user *)&t);
176 	if (put_compat_timespec(&t, interval))
177 		return -EFAULT;
178 	return ret;
179 }
180 
181 static int
182 put_compat_timeval(struct compat_timeval __user *u, struct timeval *t)
183 {
184 	struct compat_timeval t32;
185 	t32.tv_sec = t->tv_sec;
186 	t32.tv_usec = t->tv_usec;
187 	return copy_to_user(u, &t32, sizeof t32);
188 }
189 
190 static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
191 {
192 	long usec;
193 
194 	if (__get_user(o->tv_sec, &i->tv_sec))
195 		return -EFAULT;
196 	if (__get_user(usec, &i->tv_usec))
197 		return -EFAULT;
198 	o->tv_nsec = usec * 1000;
199 	return 0;
200 }
201 
202 asmlinkage int
203 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
204 {
205     extern void do_gettimeofday(struct timeval *tv);
206 
207     if (tv) {
208 	    struct timeval ktv;
209 	    do_gettimeofday(&ktv);
210 	    if (put_compat_timeval(tv, &ktv))
211 		    return -EFAULT;
212     }
213     if (tz) {
214 	    extern struct timezone sys_tz;
215 	    if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
216 		    return -EFAULT;
217     }
218     return 0;
219 }
220 
221 asmlinkage
222 int sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
223 {
224 	struct timespec kts;
225 	struct timezone ktz;
226 
227  	if (tv) {
228 		if (get_ts32(&kts, tv))
229 			return -EFAULT;
230 	}
231 	if (tz) {
232 		if (copy_from_user(&ktz, tz, sizeof(ktz)))
233 			return -EFAULT;
234 	}
235 
236 	return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
237 }
238 
239 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
240 {
241 	int err;
242 
243 	if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
244 	    !new_valid_dev(stat->rdev))
245 		return -EOVERFLOW;
246 
247 	err  = put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
248 	err |= put_user(stat->ino, &statbuf->st_ino);
249 	err |= put_user(stat->mode, &statbuf->st_mode);
250 	err |= put_user(stat->nlink, &statbuf->st_nlink);
251 	err |= put_user(0, &statbuf->st_reserved1);
252 	err |= put_user(0, &statbuf->st_reserved2);
253 	err |= put_user(new_encode_dev(stat->rdev), &statbuf->st_rdev);
254 	err |= put_user(stat->size, &statbuf->st_size);
255 	err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
256 	err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
257 	err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
258 	err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
259 	err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
260 	err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
261 	err |= put_user(stat->blksize, &statbuf->st_blksize);
262 	err |= put_user(stat->blocks, &statbuf->st_blocks);
263 	err |= put_user(0, &statbuf->__unused1);
264 	err |= put_user(0, &statbuf->__unused2);
265 	err |= put_user(0, &statbuf->__unused3);
266 	err |= put_user(0, &statbuf->__unused4);
267 	err |= put_user(0, &statbuf->__unused5);
268 	err |= put_user(0, &statbuf->st_fstype); /* not avail */
269 	err |= put_user(0, &statbuf->st_realdev); /* not avail */
270 	err |= put_user(0, &statbuf->st_basemode); /* not avail */
271 	err |= put_user(0, &statbuf->st_spareshort);
272 	err |= put_user(stat->uid, &statbuf->st_uid);
273 	err |= put_user(stat->gid, &statbuf->st_gid);
274 	err |= put_user(0, &statbuf->st_spare4[0]);
275 	err |= put_user(0, &statbuf->st_spare4[1]);
276 	err |= put_user(0, &statbuf->st_spare4[2]);
277 
278 	return err;
279 }
280 
281 struct linux32_dirent {
282 	u32		d_ino;
283 	compat_off_t	d_off;
284 	u16		d_reclen;
285 	char		d_name[1];
286 };
287 
288 struct old_linux32_dirent {
289 	u32	d_ino;
290 	u32	d_offset;
291 	u16	d_namlen;
292 	char	d_name[1];
293 };
294 
295 struct getdents32_callback {
296 	struct linux32_dirent __user * current_dir;
297 	struct linux32_dirent __user * previous;
298 	int count;
299 	int error;
300 };
301 
302 struct readdir32_callback {
303 	struct old_linux32_dirent __user * dirent;
304 	int count;
305 };
306 
307 #define ROUND_UP(x,a)	((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
308 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
309 static int
310 filldir32 (void *__buf, const char *name, int namlen, loff_t offset, ino_t ino,
311 	   unsigned int d_type)
312 {
313 	struct linux32_dirent __user * dirent;
314 	struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
315 	int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1, 4);
316 
317 	buf->error = -EINVAL;	/* only used if we fail.. */
318 	if (reclen > buf->count)
319 		return -EINVAL;
320 	dirent = buf->previous;
321 	if (dirent)
322 		put_user(offset, &dirent->d_off);
323 	dirent = buf->current_dir;
324 	buf->previous = dirent;
325 	put_user(ino, &dirent->d_ino);
326 	put_user(reclen, &dirent->d_reclen);
327 	copy_to_user(dirent->d_name, name, namlen);
328 	put_user(0, dirent->d_name + namlen);
329 	dirent = ((void __user *)dirent) + reclen;
330 	buf->current_dir = dirent;
331 	buf->count -= reclen;
332 	return 0;
333 }
334 
335 asmlinkage long
336 sys32_getdents (unsigned int fd, void __user * dirent, unsigned int count)
337 {
338 	struct file * file;
339 	struct linux32_dirent __user * lastdirent;
340 	struct getdents32_callback buf;
341 	int error;
342 
343 	error = -EBADF;
344 	file = fget(fd);
345 	if (!file)
346 		goto out;
347 
348 	buf.current_dir = (struct linux32_dirent __user *) dirent;
349 	buf.previous = NULL;
350 	buf.count = count;
351 	buf.error = 0;
352 
353 	error = vfs_readdir(file, filldir32, &buf);
354 	if (error < 0)
355 		goto out_putf;
356 	error = buf.error;
357 	lastdirent = buf.previous;
358 	if (lastdirent) {
359 		put_user(file->f_pos, &lastdirent->d_off);
360 		error = count - buf.count;
361 	}
362 
363 out_putf:
364 	fput(file);
365 out:
366 	return error;
367 }
368 
369 static int
370 fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, ino_t ino,
371 	      unsigned int d_type)
372 {
373 	struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
374 	struct old_linux32_dirent __user * dirent;
375 
376 	if (buf->count)
377 		return -EINVAL;
378 	buf->count++;
379 	dirent = buf->dirent;
380 	put_user(ino, &dirent->d_ino);
381 	put_user(offset, &dirent->d_offset);
382 	put_user(namlen, &dirent->d_namlen);
383 	copy_to_user(dirent->d_name, name, namlen);
384 	put_user(0, dirent->d_name + namlen);
385 	return 0;
386 }
387 
388 asmlinkage long
389 sys32_readdir (unsigned int fd, void __user * dirent, unsigned int count)
390 {
391 	int error;
392 	struct file * file;
393 	struct readdir32_callback buf;
394 
395 	error = -EBADF;
396 	file = fget(fd);
397 	if (!file)
398 		goto out;
399 
400 	buf.count = 0;
401 	buf.dirent = dirent;
402 
403 	error = vfs_readdir(file, fillonedir32, &buf);
404 	if (error >= 0)
405 		error = buf.count;
406 	fput(file);
407 out:
408 	return error;
409 }
410 
411 /*** copied from mips64 ***/
412 /*
413  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
414  * 64-bit unsigned longs.
415  */
416 
417 static inline int
418 get_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
419 {
420 	n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
421 	if (ufdset) {
422 		unsigned long odd;
423 
424 		if (!access_ok(VERIFY_WRITE, ufdset, n*sizeof(u32)))
425 			return -EFAULT;
426 
427 		odd = n & 1UL;
428 		n &= ~1UL;
429 		while (n) {
430 			unsigned long h, l;
431 			__get_user(l, ufdset);
432 			__get_user(h, ufdset+1);
433 			ufdset += 2;
434 			*fdset++ = h << 32 | l;
435 			n -= 2;
436 		}
437 		if (odd)
438 			__get_user(*fdset, ufdset);
439 	} else {
440 		/* Tricky, must clear full unsigned long in the
441 		 * kernel fdset at the end, this makes sure that
442 		 * actually happens.
443 		 */
444 		memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32));
445 	}
446 	return 0;
447 }
448 
449 static inline void
450 set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
451 {
452 	unsigned long odd;
453 	n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
454 
455 	if (!ufdset)
456 		return;
457 
458 	odd = n & 1UL;
459 	n &= ~1UL;
460 	while (n) {
461 		unsigned long h, l;
462 		l = *fdset++;
463 		h = l >> 32;
464 		__put_user(l, ufdset);
465 		__put_user(h, ufdset+1);
466 		ufdset += 2;
467 		n -= 2;
468 	}
469 	if (odd)
470 		__put_user(*fdset, ufdset);
471 }
472 
473 struct msgbuf32 {
474     int mtype;
475     char mtext[1];
476 };
477 
478 asmlinkage long sys32_msgsnd(int msqid,
479 				struct msgbuf32 __user *umsgp32,
480 				size_t msgsz, int msgflg)
481 {
482 	struct msgbuf *mb;
483 	struct msgbuf32 mb32;
484 	int err;
485 
486 	if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
487 		return -ENOMEM;
488 
489 	err = get_user(mb32.mtype, &umsgp32->mtype);
490 	mb->mtype = mb32.mtype;
491 	err |= copy_from_user(mb->mtext, &umsgp32->mtext, msgsz);
492 
493 	if (err)
494 		err = -EFAULT;
495 	else
496 		KERNEL_SYSCALL(err, sys_msgsnd, msqid, (struct msgbuf __user *)mb, msgsz, msgflg);
497 
498 	kfree(mb);
499 	return err;
500 }
501 
502 asmlinkage long sys32_msgrcv(int msqid,
503 				struct msgbuf32 __user *umsgp32,
504 				size_t msgsz, long msgtyp, int msgflg)
505 {
506 	struct msgbuf *mb;
507 	struct msgbuf32 mb32;
508 	int err, len;
509 
510 	if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
511 		return -ENOMEM;
512 
513 	KERNEL_SYSCALL(err, sys_msgrcv, msqid, (struct msgbuf __user *)mb, msgsz, msgtyp, msgflg);
514 
515 	if (err >= 0) {
516 		len = err;
517 		mb32.mtype = mb->mtype;
518 		err = put_user(mb32.mtype, &umsgp32->mtype);
519 		err |= copy_to_user(&umsgp32->mtext, mb->mtext, len);
520 		if (err)
521 			err = -EFAULT;
522 		else
523 			err = len;
524 	}
525 
526 	kfree(mb);
527 	return err;
528 }
529 
530 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
531 {
532         mm_segment_t old_fs = get_fs();
533         int ret;
534         off_t of;
535 
536         if (offset && get_user(of, offset))
537                 return -EFAULT;
538 
539         set_fs(KERNEL_DS);
540         ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
541         set_fs(old_fs);
542 
543         if (offset && put_user(of, offset))
544                 return -EFAULT;
545 
546         return ret;
547 }
548 
549 asmlinkage int sys32_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count)
550 {
551 	mm_segment_t old_fs = get_fs();
552 	int ret;
553 	loff_t lof;
554 
555 	if (offset && get_user(lof, offset))
556 		return -EFAULT;
557 
558 	set_fs(KERNEL_DS);
559 	ret = sys_sendfile64(out_fd, in_fd, offset ? (loff_t __user *)&lof : NULL, count);
560 	set_fs(old_fs);
561 
562 	if (offset && put_user(lof, offset))
563 		return -EFAULT;
564 
565 	return ret;
566 }
567 
568 
569 struct sysinfo32 {
570 	s32 uptime;
571 	u32 loads[3];
572 	u32 totalram;
573 	u32 freeram;
574 	u32 sharedram;
575 	u32 bufferram;
576 	u32 totalswap;
577 	u32 freeswap;
578 	unsigned short procs;
579 	u32 totalhigh;
580 	u32 freehigh;
581 	u32 mem_unit;
582 	char _f[12];
583 };
584 
585 /* We used to call sys_sysinfo and translate the result.  But sys_sysinfo
586  * undoes the good work done elsewhere, and rather than undoing the
587  * damage, I decided to just duplicate the code from sys_sysinfo here.
588  */
589 
590 asmlinkage int sys32_sysinfo(struct sysinfo32 __user *info)
591 {
592 	struct sysinfo val;
593 	int err;
594 	unsigned long seq;
595 
596 	/* We don't need a memset here because we copy the
597 	 * struct to userspace once element at a time.
598 	 */
599 
600 	do {
601 		seq = read_seqbegin(&xtime_lock);
602 		val.uptime = jiffies / HZ;
603 
604 		val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
605 		val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
606 		val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
607 
608 		val.procs = nr_threads;
609 	} while (read_seqretry(&xtime_lock, seq));
610 
611 
612 	si_meminfo(&val);
613 	si_swapinfo(&val);
614 
615 	err = put_user (val.uptime, &info->uptime);
616 	err |= __put_user (val.loads[0], &info->loads[0]);
617 	err |= __put_user (val.loads[1], &info->loads[1]);
618 	err |= __put_user (val.loads[2], &info->loads[2]);
619 	err |= __put_user (val.totalram, &info->totalram);
620 	err |= __put_user (val.freeram, &info->freeram);
621 	err |= __put_user (val.sharedram, &info->sharedram);
622 	err |= __put_user (val.bufferram, &info->bufferram);
623 	err |= __put_user (val.totalswap, &info->totalswap);
624 	err |= __put_user (val.freeswap, &info->freeswap);
625 	err |= __put_user (val.procs, &info->procs);
626 	err |= __put_user (val.totalhigh, &info->totalhigh);
627 	err |= __put_user (val.freehigh, &info->freehigh);
628 	err |= __put_user (val.mem_unit, &info->mem_unit);
629 	return err ? -EFAULT : 0;
630 }
631 
632 
633 /* lseek() needs a wrapper because 'offset' can be negative, but the top
634  * half of the argument has been zeroed by syscall.S.
635  */
636 
637 asmlinkage int sys32_lseek(unsigned int fd, int offset, unsigned int origin)
638 {
639 	return sys_lseek(fd, offset, origin);
640 }
641 
642 asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg)
643 {
644         union semun u;
645 
646         if (cmd == SETVAL) {
647                 /* Ugh.  arg is a union of int,ptr,ptr,ptr, so is 8 bytes.
648                  * The int should be in the first 4, but our argument
649                  * frobbing has left it in the last 4.
650                  */
651                 u.val = *((int *)&arg + 1);
652                 return sys_semctl (semid, semnum, cmd, u);
653 	}
654 	return sys_semctl (semid, semnum, cmd, arg);
655 }
656 
657 long sys32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf,
658 			  size_t len)
659 {
660 	return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
661 				  buf, len);
662 }
663