xref: /openbmc/linux/arch/powerpc/kernel/sys_ppc32.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /*
2  * sys_ppc32.c: Conversion between 32bit and 64bit native syscalls.
3  *
4  * Copyright (C) 2001 IBM
5  * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
7  *
8  * These routines maintain argument size conversion between 32bit and 64bit
9  * environment.
10  *
11  *      This program is free software; you can redistribute it and/or
12  *      modify it under the terms of the GNU General Public License
13  *      as published by the Free Software Foundation; either version
14  *      2 of the License, or (at your option) any later version.
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 #include <linux/file.h>
22 #include <linux/signal.h>
23 #include <linux/resource.h>
24 #include <linux/times.h>
25 #include <linux/smp.h>
26 #include <linux/sem.h>
27 #include <linux/msg.h>
28 #include <linux/shm.h>
29 #include <linux/poll.h>
30 #include <linux/personality.h>
31 #include <linux/stat.h>
32 #include <linux/mman.h>
33 #include <linux/in.h>
34 #include <linux/syscalls.h>
35 #include <linux/unistd.h>
36 #include <linux/sysctl.h>
37 #include <linux/binfmts.h>
38 #include <linux/security.h>
39 #include <linux/compat.h>
40 #include <linux/ptrace.h>
41 #include <linux/elf.h>
42 #include <linux/ipc.h>
43 #include <linux/slab.h>
44 
45 #include <asm/ptrace.h>
46 #include <asm/types.h>
47 #include <asm/uaccess.h>
48 #include <asm/unistd.h>
49 #include <asm/time.h>
50 #include <asm/mmu_context.h>
51 #include <asm/ppc-pci.h>
52 #include <asm/syscalls.h>
53 
54 
55 asmlinkage long ppc32_select(u32 n, compat_ulong_t __user *inp,
56 		compat_ulong_t __user *outp, compat_ulong_t __user *exp,
57 		compat_uptr_t tvp_x)
58 {
59 	/* sign extend n */
60 	return compat_sys_select((int)n, inp, outp, exp, compat_ptr(tvp_x));
61 }
62 
63 /* Note: it is necessary to treat option as an unsigned int,
64  * with the corresponding cast to a signed int to insure that the
65  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
66  * and the register representation of a signed int (msr in 64-bit mode) is performed.
67  */
68 asmlinkage long compat_sys_sysfs(u32 option, u32 arg1, u32 arg2)
69 {
70 	return sys_sysfs((int)option, arg1, arg2);
71 }
72 
73 #ifdef CONFIG_SYSVIPC
74 long compat_sys_ipc(u32 call, u32 first, u32 second, u32 third, compat_uptr_t ptr,
75 	       u32 fifth)
76 {
77 	int version;
78 
79 	version = call >> 16; /* hack for backward compatibility */
80 	call &= 0xffff;
81 
82 	switch (call) {
83 
84 	case SEMTIMEDOP:
85 		if (fifth)
86 			/* sign extend semid */
87 			return compat_sys_semtimedop((int)first,
88 						     compat_ptr(ptr), second,
89 						     compat_ptr(fifth));
90 		/* else fall through for normal semop() */
91 	case SEMOP:
92 		/* struct sembuf is the same on 32 and 64bit :)) */
93 		/* sign extend semid */
94 		return sys_semtimedop((int)first, compat_ptr(ptr), second,
95 				      NULL);
96 	case SEMGET:
97 		/* sign extend key, nsems */
98 		return sys_semget((int)first, (int)second, third);
99 	case SEMCTL:
100 		/* sign extend semid, semnum */
101 		return compat_sys_semctl((int)first, (int)second, third,
102 					 compat_ptr(ptr));
103 
104 	case MSGSND:
105 		/* sign extend msqid */
106 		return compat_sys_msgsnd((int)first, (int)second, third,
107 					 compat_ptr(ptr));
108 	case MSGRCV:
109 		/* sign extend msqid, msgtyp */
110 		return compat_sys_msgrcv((int)first, second, (int)fifth,
111 					 third, version, compat_ptr(ptr));
112 	case MSGGET:
113 		/* sign extend key */
114 		return sys_msgget((int)first, second);
115 	case MSGCTL:
116 		/* sign extend msqid */
117 		return compat_sys_msgctl((int)first, second, compat_ptr(ptr));
118 
119 	case SHMAT:
120 		/* sign extend shmid */
121 		return compat_sys_shmat((int)first, second, third, version,
122 					compat_ptr(ptr));
123 	case SHMDT:
124 		return sys_shmdt(compat_ptr(ptr));
125 	case SHMGET:
126 		/* sign extend key_t */
127 		return sys_shmget((int)first, second, third);
128 	case SHMCTL:
129 		/* sign extend shmid */
130 		return compat_sys_shmctl((int)first, second, compat_ptr(ptr));
131 
132 	default:
133 		return -ENOSYS;
134 	}
135 
136 	return -ENOSYS;
137 }
138 #endif
139 
140 /* Note: it is necessary to treat out_fd and in_fd as unsigned ints,
141  * with the corresponding cast to a signed int to insure that the
142  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
143  * and the register representation of a signed int (msr in 64-bit mode) is performed.
144  */
145 asmlinkage long compat_sys_sendfile(u32 out_fd, u32 in_fd, compat_off_t __user * offset, u32 count)
146 {
147 	mm_segment_t old_fs = get_fs();
148 	int ret;
149 	off_t of;
150 	off_t __user *up;
151 
152 	if (offset && get_user(of, offset))
153 		return -EFAULT;
154 
155 	/* The __user pointer cast is valid because of the set_fs() */
156 	set_fs(KERNEL_DS);
157 	up = offset ? (off_t __user *) &of : NULL;
158 	ret = sys_sendfile((int)out_fd, (int)in_fd, up, count);
159 	set_fs(old_fs);
160 
161 	if (offset && put_user(of, offset))
162 		return -EFAULT;
163 
164 	return ret;
165 }
166 
167 asmlinkage int compat_sys_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count)
168 {
169 	mm_segment_t old_fs = get_fs();
170 	int ret;
171 	loff_t lof;
172 	loff_t __user *up;
173 
174 	if (offset && get_user(lof, offset))
175 		return -EFAULT;
176 
177 	/* The __user pointer cast is valid because of the set_fs() */
178 	set_fs(KERNEL_DS);
179 	up = offset ? (loff_t __user *) &lof : NULL;
180 	ret = sys_sendfile64(out_fd, in_fd, up, count);
181 	set_fs(old_fs);
182 
183 	if (offset && put_user(lof, offset))
184 		return -EFAULT;
185 
186 	return ret;
187 }
188 
189 long compat_sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
190 		  unsigned long a3, unsigned long a4, unsigned long a5,
191 		  struct pt_regs *regs)
192 {
193 	int error;
194 	char * filename;
195 
196 	filename = getname((char __user *) a0);
197 	error = PTR_ERR(filename);
198 	if (IS_ERR(filename))
199 		goto out;
200 	flush_fp_to_thread(current);
201 	flush_altivec_to_thread(current);
202 
203 	error = compat_do_execve(filename, compat_ptr(a1), compat_ptr(a2), regs);
204 
205 	putname(filename);
206 
207 out:
208 	return error;
209 }
210 
211 /* Note: it is necessary to treat option as an unsigned int,
212  * with the corresponding cast to a signed int to insure that the
213  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
214  * and the register representation of a signed int (msr in 64-bit mode) is performed.
215  */
216 asmlinkage long compat_sys_prctl(u32 option, u32 arg2, u32 arg3, u32 arg4, u32 arg5)
217 {
218 	return sys_prctl((int)option,
219 			 (unsigned long) arg2,
220 			 (unsigned long) arg3,
221 			 (unsigned long) arg4,
222 			 (unsigned long) arg5);
223 }
224 
225 /* Note: it is necessary to treat pid as an unsigned int,
226  * with the corresponding cast to a signed int to insure that the
227  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
228  * and the register representation of a signed int (msr in 64-bit mode) is performed.
229  */
230 asmlinkage long compat_sys_sched_rr_get_interval(u32 pid, struct compat_timespec __user *interval)
231 {
232 	struct timespec t;
233 	int ret;
234 	mm_segment_t old_fs = get_fs ();
235 
236 	/* The __user pointer cast is valid because of the set_fs() */
237 	set_fs (KERNEL_DS);
238 	ret = sys_sched_rr_get_interval((int)pid, (struct timespec __user *) &t);
239 	set_fs (old_fs);
240 	if (put_compat_timespec(&t, interval))
241 		return -EFAULT;
242 	return ret;
243 }
244 
245 /* Note: it is necessary to treat mode as an unsigned int,
246  * with the corresponding cast to a signed int to insure that the
247  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
248  * and the register representation of a signed int (msr in 64-bit mode) is performed.
249  */
250 asmlinkage long compat_sys_access(const char __user * filename, u32 mode)
251 {
252 	return sys_access(filename, (int)mode);
253 }
254 
255 
256 /* Note: it is necessary to treat mode as an unsigned int,
257  * with the corresponding cast to a signed int to insure that the
258  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
259  * and the register representation of a signed int (msr in 64-bit mode) is performed.
260  */
261 asmlinkage long compat_sys_creat(const char __user * pathname, u32 mode)
262 {
263 	return sys_creat(pathname, (int)mode);
264 }
265 
266 
267 /* Note: it is necessary to treat pid and options as unsigned ints,
268  * with the corresponding cast to a signed int to insure that the
269  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
270  * and the register representation of a signed int (msr in 64-bit mode) is performed.
271  */
272 asmlinkage long compat_sys_waitpid(u32 pid, unsigned int __user * stat_addr, u32 options)
273 {
274 	return sys_waitpid((int)pid, stat_addr, (int)options);
275 }
276 
277 
278 /* Note: it is necessary to treat gidsetsize as an unsigned int,
279  * with the corresponding cast to a signed int to insure that the
280  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
281  * and the register representation of a signed int (msr in 64-bit mode) is performed.
282  */
283 asmlinkage long compat_sys_getgroups(u32 gidsetsize, gid_t __user *grouplist)
284 {
285 	return sys_getgroups((int)gidsetsize, grouplist);
286 }
287 
288 
289 /* Note: it is necessary to treat pid as an unsigned int,
290  * with the corresponding cast to a signed int to insure that the
291  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
292  * and the register representation of a signed int (msr in 64-bit mode) is performed.
293  */
294 asmlinkage long compat_sys_getpgid(u32 pid)
295 {
296 	return sys_getpgid((int)pid);
297 }
298 
299 
300 
301 /* Note: it is necessary to treat pid as an unsigned int,
302  * with the corresponding cast to a signed int to insure that the
303  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
304  * and the register representation of a signed int (msr in 64-bit mode) is performed.
305  */
306 asmlinkage long compat_sys_getsid(u32 pid)
307 {
308 	return sys_getsid((int)pid);
309 }
310 
311 
312 /* Note: it is necessary to treat pid and sig as unsigned ints,
313  * with the corresponding cast to a signed int to insure that the
314  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
315  * and the register representation of a signed int (msr in 64-bit mode) is performed.
316  */
317 asmlinkage long compat_sys_kill(u32 pid, u32 sig)
318 {
319 	return sys_kill((int)pid, (int)sig);
320 }
321 
322 
323 /* Note: it is necessary to treat mode as an unsigned int,
324  * with the corresponding cast to a signed int to insure that the
325  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
326  * and the register representation of a signed int (msr in 64-bit mode) is performed.
327  */
328 asmlinkage long compat_sys_mkdir(const char __user * pathname, u32 mode)
329 {
330 	return sys_mkdir(pathname, (int)mode);
331 }
332 
333 long compat_sys_nice(u32 increment)
334 {
335 	/* sign extend increment */
336 	return sys_nice((int)increment);
337 }
338 
339 off_t ppc32_lseek(unsigned int fd, u32 offset, unsigned int origin)
340 {
341 	/* sign extend n */
342 	return sys_lseek(fd, (int)offset, origin);
343 }
344 
345 long compat_sys_truncate(const char __user * path, u32 length)
346 {
347 	/* sign extend length */
348 	return sys_truncate(path, (int)length);
349 }
350 
351 long compat_sys_ftruncate(int fd, u32 length)
352 {
353 	/* sign extend length */
354 	return sys_ftruncate(fd, (int)length);
355 }
356 
357 /* Note: it is necessary to treat bufsiz as an unsigned int,
358  * with the corresponding cast to a signed int to insure that the
359  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
360  * and the register representation of a signed int (msr in 64-bit mode) is performed.
361  */
362 asmlinkage long compat_sys_readlink(const char __user * path, char __user * buf, u32 bufsiz)
363 {
364 	return sys_readlink(path, buf, (int)bufsiz);
365 }
366 
367 /* Note: it is necessary to treat option as an unsigned int,
368  * with the corresponding cast to a signed int to insure that the
369  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
370  * and the register representation of a signed int (msr in 64-bit mode) is performed.
371  */
372 asmlinkage long compat_sys_sched_get_priority_max(u32 policy)
373 {
374 	return sys_sched_get_priority_max((int)policy);
375 }
376 
377 
378 /* Note: it is necessary to treat policy as an unsigned int,
379  * with the corresponding cast to a signed int to insure that the
380  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
381  * and the register representation of a signed int (msr in 64-bit mode) is performed.
382  */
383 asmlinkage long compat_sys_sched_get_priority_min(u32 policy)
384 {
385 	return sys_sched_get_priority_min((int)policy);
386 }
387 
388 
389 /* Note: it is necessary to treat pid as an unsigned int,
390  * with the corresponding cast to a signed int to insure that the
391  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
392  * and the register representation of a signed int (msr in 64-bit mode) is performed.
393  */
394 asmlinkage long compat_sys_sched_getparam(u32 pid, struct sched_param __user *param)
395 {
396 	return sys_sched_getparam((int)pid, param);
397 }
398 
399 
400 /* Note: it is necessary to treat pid as an unsigned int,
401  * with the corresponding cast to a signed int to insure that the
402  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
403  * and the register representation of a signed int (msr in 64-bit mode) is performed.
404  */
405 asmlinkage long compat_sys_sched_getscheduler(u32 pid)
406 {
407 	return sys_sched_getscheduler((int)pid);
408 }
409 
410 
411 /* Note: it is necessary to treat pid as an unsigned int,
412  * with the corresponding cast to a signed int to insure that the
413  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
414  * and the register representation of a signed int (msr in 64-bit mode) is performed.
415  */
416 asmlinkage long compat_sys_sched_setparam(u32 pid, struct sched_param __user *param)
417 {
418 	return sys_sched_setparam((int)pid, param);
419 }
420 
421 
422 /* Note: it is necessary to treat pid and policy as unsigned ints,
423  * with the corresponding cast to a signed int to insure that the
424  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
425  * and the register representation of a signed int (msr in 64-bit mode) is performed.
426  */
427 asmlinkage long compat_sys_sched_setscheduler(u32 pid, u32 policy, struct sched_param __user *param)
428 {
429 	return sys_sched_setscheduler((int)pid, (int)policy, param);
430 }
431 
432 
433 /* Note: it is necessary to treat len as an unsigned int,
434  * with the corresponding cast to a signed int to insure that the
435  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
436  * and the register representation of a signed int (msr in 64-bit mode) is performed.
437  */
438 asmlinkage long compat_sys_setdomainname(char __user *name, u32 len)
439 {
440 	return sys_setdomainname(name, (int)len);
441 }
442 
443 
444 /* Note: it is necessary to treat gidsetsize as an unsigned int,
445  * with the corresponding cast to a signed int to insure that the
446  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
447  * and the register representation of a signed int (msr in 64-bit mode) is performed.
448  */
449 asmlinkage long compat_sys_setgroups(u32 gidsetsize, gid_t __user *grouplist)
450 {
451 	return sys_setgroups((int)gidsetsize, grouplist);
452 }
453 
454 
455 asmlinkage long compat_sys_sethostname(char __user *name, u32 len)
456 {
457 	/* sign extend len */
458 	return sys_sethostname(name, (int)len);
459 }
460 
461 
462 /* Note: it is necessary to treat pid and pgid as unsigned ints,
463  * with the corresponding cast to a signed int to insure that the
464  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
465  * and the register representation of a signed int (msr in 64-bit mode) is performed.
466  */
467 asmlinkage long compat_sys_setpgid(u32 pid, u32 pgid)
468 {
469 	return sys_setpgid((int)pid, (int)pgid);
470 }
471 
472 long compat_sys_getpriority(u32 which, u32 who)
473 {
474 	/* sign extend which and who */
475 	return sys_getpriority((int)which, (int)who);
476 }
477 
478 long compat_sys_setpriority(u32 which, u32 who, u32 niceval)
479 {
480 	/* sign extend which, who and niceval */
481 	return sys_setpriority((int)which, (int)who, (int)niceval);
482 }
483 
484 long compat_sys_ioprio_get(u32 which, u32 who)
485 {
486 	/* sign extend which and who */
487 	return sys_ioprio_get((int)which, (int)who);
488 }
489 
490 long compat_sys_ioprio_set(u32 which, u32 who, u32 ioprio)
491 {
492 	/* sign extend which, who and ioprio */
493 	return sys_ioprio_set((int)which, (int)who, (int)ioprio);
494 }
495 
496 /* Note: it is necessary to treat newmask as an unsigned int,
497  * with the corresponding cast to a signed int to insure that the
498  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
499  * and the register representation of a signed int (msr in 64-bit mode) is performed.
500  */
501 asmlinkage long compat_sys_ssetmask(u32 newmask)
502 {
503 	return sys_ssetmask((int) newmask);
504 }
505 
506 asmlinkage long compat_sys_syslog(u32 type, char __user * buf, u32 len)
507 {
508 	/* sign extend len */
509 	return sys_syslog(type, buf, (int)len);
510 }
511 
512 
513 /* Note: it is necessary to treat mask as an unsigned int,
514  * with the corresponding cast to a signed int to insure that the
515  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
516  * and the register representation of a signed int (msr in 64-bit mode) is performed.
517  */
518 asmlinkage long compat_sys_umask(u32 mask)
519 {
520 	return sys_umask((int)mask);
521 }
522 
523 unsigned long compat_sys_mmap2(unsigned long addr, size_t len,
524 			  unsigned long prot, unsigned long flags,
525 			  unsigned long fd, unsigned long pgoff)
526 {
527 	/* This should remain 12 even if PAGE_SIZE changes */
528 	return sys_mmap(addr, len, prot, flags, fd, pgoff << 12);
529 }
530 
531 long compat_sys_tgkill(u32 tgid, u32 pid, int sig)
532 {
533 	/* sign extend tgid, pid */
534 	return sys_tgkill((int)tgid, (int)pid, sig);
535 }
536 
537 /*
538  * long long munging:
539  * The 32 bit ABI passes long longs in an odd even register pair.
540  */
541 
542 compat_ssize_t compat_sys_pread64(unsigned int fd, char __user *ubuf, compat_size_t count,
543 			     u32 reg6, u32 poshi, u32 poslo)
544 {
545 	return sys_pread64(fd, ubuf, count, ((loff_t)poshi << 32) | poslo);
546 }
547 
548 compat_ssize_t compat_sys_pwrite64(unsigned int fd, const char __user *ubuf, compat_size_t count,
549 			      u32 reg6, u32 poshi, u32 poslo)
550 {
551 	return sys_pwrite64(fd, ubuf, count, ((loff_t)poshi << 32) | poslo);
552 }
553 
554 compat_ssize_t compat_sys_readahead(int fd, u32 r4, u32 offhi, u32 offlo, u32 count)
555 {
556 	return sys_readahead(fd, ((loff_t)offhi << 32) | offlo, count);
557 }
558 
559 asmlinkage int compat_sys_truncate64(const char __user * path, u32 reg4,
560 				unsigned long high, unsigned long low)
561 {
562 	return sys_truncate(path, (high << 32) | low);
563 }
564 
565 asmlinkage long compat_sys_fallocate(int fd, int mode, u32 offhi, u32 offlo,
566 				     u32 lenhi, u32 lenlo)
567 {
568 	return sys_fallocate(fd, mode, ((loff_t)offhi << 32) | offlo,
569 			     ((loff_t)lenhi << 32) | lenlo);
570 }
571 
572 asmlinkage int compat_sys_ftruncate64(unsigned int fd, u32 reg4, unsigned long high,
573 				 unsigned long low)
574 {
575 	return sys_ftruncate(fd, (high << 32) | low);
576 }
577 
578 long ppc32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf,
579 			  size_t len)
580 {
581 	return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
582 				  buf, len);
583 }
584 
585 long ppc32_fadvise64(int fd, u32 unused, u32 offset_high, u32 offset_low,
586 		     size_t len, int advice)
587 {
588 	return sys_fadvise64(fd, (u64)offset_high << 32 | offset_low, len,
589 			     advice);
590 }
591 
592 asmlinkage long compat_sys_add_key(const char __user *_type,
593 			      const char __user *_description,
594 			      const void __user *_payload,
595 			      u32 plen,
596 			      u32 ringid)
597 {
598 	return sys_add_key(_type, _description, _payload, plen, ringid);
599 }
600 
601 asmlinkage long compat_sys_request_key(const char __user *_type,
602 				  const char __user *_description,
603 				  const char __user *_callout_info,
604 				  u32 destringid)
605 {
606 	return sys_request_key(_type, _description, _callout_info, destringid);
607 }
608 
609 asmlinkage long compat_sys_sync_file_range2(int fd, unsigned int flags,
610 				   unsigned offset_hi, unsigned offset_lo,
611 				   unsigned nbytes_hi, unsigned nbytes_lo)
612 {
613 	loff_t offset = ((loff_t)offset_hi << 32) | offset_lo;
614 	loff_t nbytes = ((loff_t)nbytes_hi << 32) | nbytes_lo;
615 
616 	return sys_sync_file_range(fd, offset, nbytes, flags);
617 }
618 
619 asmlinkage long compat_sys_fanotify_mark(int fanotify_fd, unsigned int flags,
620 					 unsigned mask_hi, unsigned mask_lo,
621 					 int dfd, const char __user *pathname)
622 {
623 	u64 mask = ((u64)mask_hi << 32) | mask_lo;
624 	return sys_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
625 }
626