xref: /openbmc/linux/arch/sparc/kernel/sys_sparc32.c (revision 7dd65feb)
1 /* sys_sparc32.c: Conversion between 32bit and 64bit native syscalls.
2  *
3  * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
4  * Copyright (C) 1997, 2007 David S. Miller (davem@davemloft.net)
5  *
6  * These routines maintain argument size conversion between 32bit and 64bit
7  * environment.
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/capability.h>
13 #include <linux/fs.h>
14 #include <linux/mm.h>
15 #include <linux/file.h>
16 #include <linux/signal.h>
17 #include <linux/resource.h>
18 #include <linux/times.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/sem.h>
22 #include <linux/msg.h>
23 #include <linux/shm.h>
24 #include <linux/slab.h>
25 #include <linux/uio.h>
26 #include <linux/nfs_fs.h>
27 #include <linux/quota.h>
28 #include <linux/module.h>
29 #include <linux/poll.h>
30 #include <linux/personality.h>
31 #include <linux/stat.h>
32 #include <linux/filter.h>
33 #include <linux/highmem.h>
34 #include <linux/highuid.h>
35 #include <linux/mman.h>
36 #include <linux/ipv6.h>
37 #include <linux/in.h>
38 #include <linux/icmpv6.h>
39 #include <linux/syscalls.h>
40 #include <linux/sysctl.h>
41 #include <linux/binfmts.h>
42 #include <linux/dnotify.h>
43 #include <linux/security.h>
44 #include <linux/compat.h>
45 #include <linux/vfs.h>
46 #include <linux/netfilter_ipv4/ip_tables.h>
47 #include <linux/ptrace.h>
48 
49 #include <asm/types.h>
50 #include <asm/uaccess.h>
51 #include <asm/fpumacro.h>
52 #include <asm/mmu_context.h>
53 #include <asm/compat_signal.h>
54 
55 #ifdef CONFIG_SYSVIPC
56 asmlinkage long compat_sys_ipc(u32 call, u32 first, u32 second, u32 third, compat_uptr_t ptr, u32 fifth)
57 {
58 	int version;
59 
60 	version = call >> 16; /* hack for backward compatibility */
61 	call &= 0xffff;
62 
63 	switch (call) {
64 	case SEMTIMEDOP:
65 		if (fifth)
66 			/* sign extend semid */
67 			return compat_sys_semtimedop((int)first,
68 						     compat_ptr(ptr), second,
69 						     compat_ptr(fifth));
70 		/* else fall through for normal semop() */
71 	case SEMOP:
72 		/* struct sembuf is the same on 32 and 64bit :)) */
73 		/* sign extend semid */
74 		return sys_semtimedop((int)first, compat_ptr(ptr), second,
75 				      NULL);
76 	case SEMGET:
77 		/* sign extend key, nsems */
78 		return sys_semget((int)first, (int)second, third);
79 	case SEMCTL:
80 		/* sign extend semid, semnum */
81 		return compat_sys_semctl((int)first, (int)second, third,
82 					 compat_ptr(ptr));
83 
84 	case MSGSND:
85 		/* sign extend msqid */
86 		return compat_sys_msgsnd((int)first, (int)second, third,
87 					 compat_ptr(ptr));
88 	case MSGRCV:
89 		/* sign extend msqid, msgtyp */
90 		return compat_sys_msgrcv((int)first, second, (int)fifth,
91 					 third, version, compat_ptr(ptr));
92 	case MSGGET:
93 		/* sign extend key */
94 		return sys_msgget((int)first, second);
95 	case MSGCTL:
96 		/* sign extend msqid */
97 		return compat_sys_msgctl((int)first, second, compat_ptr(ptr));
98 
99 	case SHMAT:
100 		/* sign extend shmid */
101 		return compat_sys_shmat((int)first, second, third, version,
102 					compat_ptr(ptr));
103 	case SHMDT:
104 		return sys_shmdt(compat_ptr(ptr));
105 	case SHMGET:
106 		/* sign extend key_t */
107 		return sys_shmget((int)first, second, third);
108 	case SHMCTL:
109 		/* sign extend shmid */
110 		return compat_sys_shmctl((int)first, second, compat_ptr(ptr));
111 
112 	default:
113 		return -ENOSYS;
114 	};
115 
116 	return -ENOSYS;
117 }
118 #endif
119 
120 asmlinkage long sys32_truncate64(const char __user * path, unsigned long high, unsigned long low)
121 {
122 	if ((int)high < 0)
123 		return -EINVAL;
124 	else
125 		return sys_truncate(path, (high << 32) | low);
126 }
127 
128 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
129 {
130 	if ((int)high < 0)
131 		return -EINVAL;
132 	else
133 		return sys_ftruncate(fd, (high << 32) | low);
134 }
135 
136 static int cp_compat_stat64(struct kstat *stat,
137 			    struct compat_stat64 __user *statbuf)
138 {
139 	int err;
140 
141 	err  = put_user(huge_encode_dev(stat->dev), &statbuf->st_dev);
142 	err |= put_user(stat->ino, &statbuf->st_ino);
143 	err |= put_user(stat->mode, &statbuf->st_mode);
144 	err |= put_user(stat->nlink, &statbuf->st_nlink);
145 	err |= put_user(stat->uid, &statbuf->st_uid);
146 	err |= put_user(stat->gid, &statbuf->st_gid);
147 	err |= put_user(huge_encode_dev(stat->rdev), &statbuf->st_rdev);
148 	err |= put_user(0, (unsigned long __user *) &statbuf->__pad3[0]);
149 	err |= put_user(stat->size, &statbuf->st_size);
150 	err |= put_user(stat->blksize, &statbuf->st_blksize);
151 	err |= put_user(0, (unsigned int __user *) &statbuf->__pad4[0]);
152 	err |= put_user(0, (unsigned int __user *) &statbuf->__pad4[4]);
153 	err |= put_user(stat->blocks, &statbuf->st_blocks);
154 	err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
155 	err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
156 	err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
157 	err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
158 	err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
159 	err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
160 	err |= put_user(0, &statbuf->__unused4);
161 	err |= put_user(0, &statbuf->__unused5);
162 
163 	return err;
164 }
165 
166 asmlinkage long compat_sys_stat64(char __user * filename,
167 		struct compat_stat64 __user *statbuf)
168 {
169 	struct kstat stat;
170 	int error = vfs_stat(filename, &stat);
171 
172 	if (!error)
173 		error = cp_compat_stat64(&stat, statbuf);
174 	return error;
175 }
176 
177 asmlinkage long compat_sys_lstat64(char __user * filename,
178 		struct compat_stat64 __user *statbuf)
179 {
180 	struct kstat stat;
181 	int error = vfs_lstat(filename, &stat);
182 
183 	if (!error)
184 		error = cp_compat_stat64(&stat, statbuf);
185 	return error;
186 }
187 
188 asmlinkage long compat_sys_fstat64(unsigned int fd,
189 		struct compat_stat64 __user * statbuf)
190 {
191 	struct kstat stat;
192 	int error = vfs_fstat(fd, &stat);
193 
194 	if (!error)
195 		error = cp_compat_stat64(&stat, statbuf);
196 	return error;
197 }
198 
199 asmlinkage long compat_sys_fstatat64(unsigned int dfd, char __user *filename,
200 		struct compat_stat64 __user * statbuf, int flag)
201 {
202 	struct kstat stat;
203 	int error;
204 
205 	error = vfs_fstatat(dfd, filename, &stat, flag);
206 	if (error)
207 		return error;
208 	return cp_compat_stat64(&stat, statbuf);
209 }
210 
211 asmlinkage long compat_sys_sysfs(int option, u32 arg1, u32 arg2)
212 {
213 	return sys_sysfs(option, arg1, arg2);
214 }
215 
216 asmlinkage long compat_sys_sched_rr_get_interval(compat_pid_t pid, struct compat_timespec __user *interval)
217 {
218 	struct timespec t;
219 	int ret;
220 	mm_segment_t old_fs = get_fs ();
221 
222 	set_fs (KERNEL_DS);
223 	ret = sys_sched_rr_get_interval(pid, (struct timespec __user *) &t);
224 	set_fs (old_fs);
225 	if (put_compat_timespec(&t, interval))
226 		return -EFAULT;
227 	return ret;
228 }
229 
230 asmlinkage long compat_sys_rt_sigprocmask(int how,
231 					  compat_sigset_t __user *set,
232 					  compat_sigset_t __user *oset,
233 					  compat_size_t sigsetsize)
234 {
235 	sigset_t s;
236 	compat_sigset_t s32;
237 	int ret;
238 	mm_segment_t old_fs = get_fs();
239 
240 	if (set) {
241 		if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
242 			return -EFAULT;
243 		switch (_NSIG_WORDS) {
244 		case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
245 		case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
246 		case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
247 		case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
248 		}
249 	}
250 	set_fs (KERNEL_DS);
251 	ret = sys_rt_sigprocmask(how,
252 				 set ? (sigset_t __user *) &s : NULL,
253 				 oset ? (sigset_t __user *) &s : NULL,
254 				 sigsetsize);
255 	set_fs (old_fs);
256 	if (ret) return ret;
257 	if (oset) {
258 		switch (_NSIG_WORDS) {
259 		case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
260 		case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
261 		case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
262 		case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
263 		}
264 		if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
265 			return -EFAULT;
266 	}
267 	return 0;
268 }
269 
270 asmlinkage long sys32_rt_sigpending(compat_sigset_t __user *set,
271 				    compat_size_t sigsetsize)
272 {
273 	sigset_t s;
274 	compat_sigset_t s32;
275 	int ret;
276 	mm_segment_t old_fs = get_fs();
277 
278 	set_fs (KERNEL_DS);
279 	ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
280 	set_fs (old_fs);
281 	if (!ret) {
282 		switch (_NSIG_WORDS) {
283 		case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
284 		case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
285 		case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
286 		case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
287 		}
288 		if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
289 			return -EFAULT;
290 	}
291 	return ret;
292 }
293 
294 asmlinkage long compat_sys_rt_sigqueueinfo(int pid, int sig,
295 					   struct compat_siginfo __user *uinfo)
296 {
297 	siginfo_t info;
298 	int ret;
299 	mm_segment_t old_fs = get_fs();
300 
301 	if (copy_siginfo_from_user32(&info, uinfo))
302 		return -EFAULT;
303 
304 	set_fs (KERNEL_DS);
305 	ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *) &info);
306 	set_fs (old_fs);
307 	return ret;
308 }
309 
310 asmlinkage long compat_sys_sigaction(int sig, struct old_sigaction32 __user *act,
311 				     struct old_sigaction32 __user *oact)
312 {
313         struct k_sigaction new_ka, old_ka;
314         int ret;
315 
316 	WARN_ON_ONCE(sig >= 0);
317 	sig = -sig;
318 
319         if (act) {
320 		compat_old_sigset_t mask;
321 		u32 u_handler, u_restorer;
322 
323 		ret = get_user(u_handler, &act->sa_handler);
324 		new_ka.sa.sa_handler =  compat_ptr(u_handler);
325 		ret |= __get_user(u_restorer, &act->sa_restorer);
326 		new_ka.sa.sa_restorer = compat_ptr(u_restorer);
327 		ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
328 		ret |= __get_user(mask, &act->sa_mask);
329 		if (ret)
330 			return ret;
331 		new_ka.ka_restorer = NULL;
332 		siginitset(&new_ka.sa.sa_mask, mask);
333         }
334 
335         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
336 
337 	if (!ret && oact) {
338 		ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler);
339 		ret |= __put_user(ptr_to_compat(old_ka.sa.sa_restorer), &oact->sa_restorer);
340 		ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
341 		ret |= __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
342         }
343 
344 	return ret;
345 }
346 
347 asmlinkage long compat_sys_rt_sigaction(int sig,
348 					struct sigaction32 __user *act,
349 					struct sigaction32 __user *oact,
350 					void __user *restorer,
351 					compat_size_t sigsetsize)
352 {
353         struct k_sigaction new_ka, old_ka;
354         int ret;
355 	compat_sigset_t set32;
356 
357         /* XXX: Don't preclude handling different sized sigset_t's.  */
358         if (sigsetsize != sizeof(compat_sigset_t))
359                 return -EINVAL;
360 
361         if (act) {
362 		u32 u_handler, u_restorer;
363 
364 		new_ka.ka_restorer = restorer;
365 		ret = get_user(u_handler, &act->sa_handler);
366 		new_ka.sa.sa_handler =  compat_ptr(u_handler);
367 		ret |= __copy_from_user(&set32, &act->sa_mask, sizeof(compat_sigset_t));
368 		switch (_NSIG_WORDS) {
369 		case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6] | (((long)set32.sig[7]) << 32);
370 		case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4] | (((long)set32.sig[5]) << 32);
371 		case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2] | (((long)set32.sig[3]) << 32);
372 		case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0] | (((long)set32.sig[1]) << 32);
373 		}
374 		ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
375 		ret |= __get_user(u_restorer, &act->sa_restorer);
376 		new_ka.sa.sa_restorer = compat_ptr(u_restorer);
377                 if (ret)
378                 	return -EFAULT;
379 	}
380 
381 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
382 
383 	if (!ret && oact) {
384 		switch (_NSIG_WORDS) {
385 		case 4: set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32); set32.sig[6] = old_ka.sa.sa_mask.sig[3];
386 		case 3: set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32); set32.sig[4] = old_ka.sa.sa_mask.sig[2];
387 		case 2: set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32); set32.sig[2] = old_ka.sa.sa_mask.sig[1];
388 		case 1: set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32); set32.sig[0] = old_ka.sa.sa_mask.sig[0];
389 		}
390 		ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler);
391 		ret |= __copy_to_user(&oact->sa_mask, &set32, sizeof(compat_sigset_t));
392 		ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
393 		ret |= __put_user(ptr_to_compat(old_ka.sa.sa_restorer), &oact->sa_restorer);
394 		if (ret)
395 			ret = -EFAULT;
396         }
397 
398         return ret;
399 }
400 
401 /*
402  * sparc32_execve() executes a new program after the asm stub has set
403  * things up for us.  This should basically do what I want it to.
404  */
405 asmlinkage long sparc32_execve(struct pt_regs *regs)
406 {
407 	int error, base = 0;
408 	char *filename;
409 
410 	/* User register window flush is done by entry.S */
411 
412 	/* Check for indirect call. */
413 	if ((u32)regs->u_regs[UREG_G1] == 0)
414 		base = 1;
415 
416 	filename = getname(compat_ptr(regs->u_regs[base + UREG_I0]));
417 	error = PTR_ERR(filename);
418 	if (IS_ERR(filename))
419 		goto out;
420 
421 	error = compat_do_execve(filename,
422 				 compat_ptr(regs->u_regs[base + UREG_I1]),
423 				 compat_ptr(regs->u_regs[base + UREG_I2]), regs);
424 
425 	putname(filename);
426 
427 	if (!error) {
428 		fprs_write(0);
429 		current_thread_info()->xfsr[0] = 0;
430 		current_thread_info()->fpsaved[0] = 0;
431 		regs->tstate &= ~TSTATE_PEF;
432 	}
433 out:
434 	return error;
435 }
436 
437 #ifdef CONFIG_MODULES
438 
439 asmlinkage long sys32_init_module(void __user *umod, u32 len,
440 				  const char __user *uargs)
441 {
442 	return sys_init_module(umod, len, uargs);
443 }
444 
445 asmlinkage long sys32_delete_module(const char __user *name_user,
446 				    unsigned int flags)
447 {
448 	return sys_delete_module(name_user, flags);
449 }
450 
451 #else /* CONFIG_MODULES */
452 
453 asmlinkage long sys32_init_module(const char __user *name_user,
454 				  struct module __user *mod_user)
455 {
456 	return -ENOSYS;
457 }
458 
459 asmlinkage long sys32_delete_module(const char __user *name_user)
460 {
461 	return -ENOSYS;
462 }
463 
464 #endif  /* CONFIG_MODULES */
465 
466 asmlinkage compat_ssize_t sys32_pread64(unsigned int fd,
467 					char __user *ubuf,
468 					compat_size_t count,
469 					unsigned long poshi,
470 					unsigned long poslo)
471 {
472 	return sys_pread64(fd, ubuf, count, (poshi << 32) | poslo);
473 }
474 
475 asmlinkage compat_ssize_t sys32_pwrite64(unsigned int fd,
476 					 char __user *ubuf,
477 					 compat_size_t count,
478 					 unsigned long poshi,
479 					 unsigned long poslo)
480 {
481 	return sys_pwrite64(fd, ubuf, count, (poshi << 32) | poslo);
482 }
483 
484 asmlinkage long compat_sys_readahead(int fd,
485 				     unsigned long offhi,
486 				     unsigned long offlo,
487 				     compat_size_t count)
488 {
489 	return sys_readahead(fd, (offhi << 32) | offlo, count);
490 }
491 
492 long compat_sys_fadvise64(int fd,
493 			  unsigned long offhi,
494 			  unsigned long offlo,
495 			  compat_size_t len, int advice)
496 {
497 	return sys_fadvise64_64(fd, (offhi << 32) | offlo, len, advice);
498 }
499 
500 long compat_sys_fadvise64_64(int fd,
501 			     unsigned long offhi, unsigned long offlo,
502 			     unsigned long lenhi, unsigned long lenlo,
503 			     int advice)
504 {
505 	return sys_fadvise64_64(fd,
506 				(offhi << 32) | offlo,
507 				(lenhi << 32) | lenlo,
508 				advice);
509 }
510 
511 asmlinkage long compat_sys_sendfile(int out_fd, int in_fd,
512 				    compat_off_t __user *offset,
513 				    compat_size_t count)
514 {
515 	mm_segment_t old_fs = get_fs();
516 	int ret;
517 	off_t of;
518 
519 	if (offset && get_user(of, offset))
520 		return -EFAULT;
521 
522 	set_fs(KERNEL_DS);
523 	ret = sys_sendfile(out_fd, in_fd,
524 			   offset ? (off_t __user *) &of : NULL,
525 			   count);
526 	set_fs(old_fs);
527 
528 	if (offset && put_user(of, offset))
529 		return -EFAULT;
530 
531 	return ret;
532 }
533 
534 asmlinkage long compat_sys_sendfile64(int out_fd, int in_fd,
535 				      compat_loff_t __user *offset,
536 				      compat_size_t count)
537 {
538 	mm_segment_t old_fs = get_fs();
539 	int ret;
540 	loff_t lof;
541 
542 	if (offset && get_user(lof, offset))
543 		return -EFAULT;
544 
545 	set_fs(KERNEL_DS);
546 	ret = sys_sendfile64(out_fd, in_fd,
547 			     offset ? (loff_t __user *) &lof : NULL,
548 			     count);
549 	set_fs(old_fs);
550 
551 	if (offset && put_user(lof, offset))
552 		return -EFAULT;
553 
554 	return ret;
555 }
556 
557 /* This is just a version for 32-bit applications which does
558  * not force O_LARGEFILE on.
559  */
560 
561 asmlinkage long sparc32_open(const char __user *filename,
562 			     int flags, int mode)
563 {
564 	return do_sys_open(AT_FDCWD, filename, flags, mode);
565 }
566 
567 long sys32_lookup_dcookie(unsigned long cookie_high,
568 			  unsigned long cookie_low,
569 			  char __user *buf, size_t len)
570 {
571 	return sys_lookup_dcookie((cookie_high << 32) | cookie_low,
572 				  buf, len);
573 }
574 
575 long compat_sync_file_range(int fd, unsigned long off_high, unsigned long off_low, unsigned long nb_high, unsigned long nb_low, int flags)
576 {
577 	return sys_sync_file_range(fd,
578 				   (off_high << 32) | off_low,
579 				   (nb_high << 32) | nb_low,
580 				   flags);
581 }
582 
583 asmlinkage long compat_sys_fallocate(int fd, int mode, u32 offhi, u32 offlo,
584 				     u32 lenhi, u32 lenlo)
585 {
586 	return sys_fallocate(fd, mode, ((loff_t)offhi << 32) | offlo,
587 			     ((loff_t)lenhi << 32) | lenlo);
588 }
589