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 /*** copied from mips64 ***/
183 /*
184  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
185  * 64-bit unsigned longs.
186  */
187 
188 static inline int
189 get_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
190 {
191 	n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
192 	if (ufdset) {
193 		unsigned long odd;
194 
195 		if (!access_ok(VERIFY_WRITE, ufdset, n*sizeof(u32)))
196 			return -EFAULT;
197 
198 		odd = n & 1UL;
199 		n &= ~1UL;
200 		while (n) {
201 			unsigned long h, l;
202 			__get_user(l, ufdset);
203 			__get_user(h, ufdset+1);
204 			ufdset += 2;
205 			*fdset++ = h << 32 | l;
206 			n -= 2;
207 		}
208 		if (odd)
209 			__get_user(*fdset, ufdset);
210 	} else {
211 		/* Tricky, must clear full unsigned long in the
212 		 * kernel fdset at the end, this makes sure that
213 		 * actually happens.
214 		 */
215 		memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32));
216 	}
217 	return 0;
218 }
219 
220 static inline void
221 set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
222 {
223 	unsigned long odd;
224 	n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
225 
226 	if (!ufdset)
227 		return;
228 
229 	odd = n & 1UL;
230 	n &= ~1UL;
231 	while (n) {
232 		unsigned long h, l;
233 		l = *fdset++;
234 		h = l >> 32;
235 		__put_user(l, ufdset);
236 		__put_user(h, ufdset+1);
237 		ufdset += 2;
238 		n -= 2;
239 	}
240 	if (odd)
241 		__put_user(*fdset, ufdset);
242 }
243 
244 struct msgbuf32 {
245     int mtype;
246     char mtext[1];
247 };
248 
249 asmlinkage long sys32_msgsnd(int msqid,
250 				struct msgbuf32 __user *umsgp32,
251 				size_t msgsz, int msgflg)
252 {
253 	struct msgbuf *mb;
254 	struct msgbuf32 mb32;
255 	int err;
256 
257 	if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
258 		return -ENOMEM;
259 
260 	err = get_user(mb32.mtype, &umsgp32->mtype);
261 	mb->mtype = mb32.mtype;
262 	err |= copy_from_user(mb->mtext, &umsgp32->mtext, msgsz);
263 
264 	if (err)
265 		err = -EFAULT;
266 	else
267 		KERNEL_SYSCALL(err, sys_msgsnd, msqid, (struct msgbuf __user *)mb, msgsz, msgflg);
268 
269 	kfree(mb);
270 	return err;
271 }
272 
273 asmlinkage long sys32_msgrcv(int msqid,
274 				struct msgbuf32 __user *umsgp32,
275 				size_t msgsz, long msgtyp, int msgflg)
276 {
277 	struct msgbuf *mb;
278 	struct msgbuf32 mb32;
279 	int err, len;
280 
281 	if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
282 		return -ENOMEM;
283 
284 	KERNEL_SYSCALL(err, sys_msgrcv, msqid, (struct msgbuf __user *)mb, msgsz, msgtyp, msgflg);
285 
286 	if (err >= 0) {
287 		len = err;
288 		mb32.mtype = mb->mtype;
289 		err = put_user(mb32.mtype, &umsgp32->mtype);
290 		err |= copy_to_user(&umsgp32->mtext, mb->mtext, len);
291 		if (err)
292 			err = -EFAULT;
293 		else
294 			err = len;
295 	}
296 
297 	kfree(mb);
298 	return err;
299 }
300 
301 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
302 {
303         mm_segment_t old_fs = get_fs();
304         int ret;
305         off_t of;
306 
307         if (offset && get_user(of, offset))
308                 return -EFAULT;
309 
310         set_fs(KERNEL_DS);
311         ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
312         set_fs(old_fs);
313 
314         if (offset && put_user(of, offset))
315                 return -EFAULT;
316 
317         return ret;
318 }
319 
320 asmlinkage int sys32_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count)
321 {
322 	mm_segment_t old_fs = get_fs();
323 	int ret;
324 	loff_t lof;
325 
326 	if (offset && get_user(lof, offset))
327 		return -EFAULT;
328 
329 	set_fs(KERNEL_DS);
330 	ret = sys_sendfile64(out_fd, in_fd, offset ? (loff_t __user *)&lof : NULL, count);
331 	set_fs(old_fs);
332 
333 	if (offset && put_user(lof, offset))
334 		return -EFAULT;
335 
336 	return ret;
337 }
338 
339 
340 /* lseek() needs a wrapper because 'offset' can be negative, but the top
341  * half of the argument has been zeroed by syscall.S.
342  */
343 
344 asmlinkage int sys32_lseek(unsigned int fd, int offset, unsigned int origin)
345 {
346 	return sys_lseek(fd, offset, origin);
347 }
348 
349 asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg)
350 {
351         union semun u;
352 
353         if (cmd == SETVAL) {
354                 /* Ugh.  arg is a union of int,ptr,ptr,ptr, so is 8 bytes.
355                  * The int should be in the first 4, but our argument
356                  * frobbing has left it in the last 4.
357                  */
358                 u.val = *((int *)&arg + 1);
359                 return sys_semctl (semid, semnum, cmd, u);
360 	}
361 	return sys_semctl (semid, semnum, cmd, arg);
362 }
363 
364 long sys32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf,
365 			  size_t len)
366 {
367 	return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
368 				  buf, len);
369 }
370 
371 asmlinkage long compat_sys_fallocate(int fd, int mode, u32 offhi, u32 offlo,
372 				u32 lenhi, u32 lenlo)
373 {
374         return sys_fallocate(fd, mode, ((loff_t)offhi << 32) | offlo,
375                              ((loff_t)lenhi << 32) | lenlo);
376 }
377