xref: /linux/include/linux/compat.h (revision 687b7edb477e4ae106158c4e10ae938a024a2298)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_COMPAT_H
3 #define _LINUX_COMPAT_H
4 /*
5  * These are the type definitions for the architecture specific
6  * syscall compatibility layer.
7  */
8 
9 #include <linux/types.h>
10 #include <linux/time.h>
11 
12 #include <linux/stat.h>
13 #include <linux/param.h>	/* for HZ */
14 #include <linux/sem.h>
15 #include <linux/socket.h>
16 #include <linux/if.h>
17 #include <linux/fs.h>
18 #include <linux/aio_abi.h>	/* for aio_context_t */
19 #include <linux/uaccess.h>
20 #include <linux/unistd.h>
21 #include <linux/error-injection.h>
22 
23 #include <asm/compat.h>
24 #include <asm/siginfo.h>
25 #include <asm/signal.h>
26 
27 #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
28 /*
29  * It may be useful for an architecture to override the definitions of the
30  * COMPAT_SYSCALL_DEFINE0 and COMPAT_SYSCALL_DEFINEx() macros, in particular
31  * to use a different calling convention for syscalls. To allow for that,
32  + the prototypes for the compat_sys_*() functions below will *not* be included
33  * if CONFIG_ARCH_HAS_SYSCALL_WRAPPER is enabled.
34  */
35 #include <asm/syscall_wrapper.h>
36 #endif /* CONFIG_ARCH_HAS_SYSCALL_WRAPPER */
37 
38 #ifndef COMPAT_USE_64BIT_TIME
39 #define COMPAT_USE_64BIT_TIME 0
40 #endif
41 
42 #ifndef __SC_DELOUSE
43 #define __SC_DELOUSE(t,v) ((__force t)(unsigned long)(v))
44 #endif
45 
46 #ifndef COMPAT_SYSCALL_DEFINE0
47 #define COMPAT_SYSCALL_DEFINE0(name) \
48 	asmlinkage long compat_sys_##name(void); \
49 	ALLOW_ERROR_INJECTION(compat_sys_##name, ERRNO); \
50 	asmlinkage long compat_sys_##name(void)
51 #endif /* COMPAT_SYSCALL_DEFINE0 */
52 
53 #define COMPAT_SYSCALL_DEFINE1(name, ...) \
54         COMPAT_SYSCALL_DEFINEx(1, _##name, __VA_ARGS__)
55 #define COMPAT_SYSCALL_DEFINE2(name, ...) \
56 	COMPAT_SYSCALL_DEFINEx(2, _##name, __VA_ARGS__)
57 #define COMPAT_SYSCALL_DEFINE3(name, ...) \
58 	COMPAT_SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)
59 #define COMPAT_SYSCALL_DEFINE4(name, ...) \
60 	COMPAT_SYSCALL_DEFINEx(4, _##name, __VA_ARGS__)
61 #define COMPAT_SYSCALL_DEFINE5(name, ...) \
62 	COMPAT_SYSCALL_DEFINEx(5, _##name, __VA_ARGS__)
63 #define COMPAT_SYSCALL_DEFINE6(name, ...) \
64 	COMPAT_SYSCALL_DEFINEx(6, _##name, __VA_ARGS__)
65 
66 /*
67  * The asmlinkage stub is aliased to a function named __se_compat_sys_*() which
68  * sign-extends 32-bit ints to longs whenever needed. The actual work is
69  * done within __do_compat_sys_*().
70  */
71 #ifndef COMPAT_SYSCALL_DEFINEx
72 #define COMPAT_SYSCALL_DEFINEx(x, name, ...)					\
73 	__diag_push();								\
74 	__diag_ignore(GCC, 8, "-Wattribute-alias",				\
75 		      "Type aliasing is used to sanitize syscall arguments");\
76 	__diag_ignore(clang, 23, "-Wunknown-warning-option",			\
77 		      "Avoid breaking versions without -Wattribute-alias");	\
78 	__diag_ignore(clang, 23, "-Wattribute-alias",				\
79 		      "Type aliasing is used to sanitize syscall arguments");	\
80 	asmlinkage long compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))	\
81 		__attribute__((alias(__stringify(__se_compat_sys##name))));	\
82 	ALLOW_ERROR_INJECTION(compat_sys##name, ERRNO);				\
83 	static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
84 	asmlinkage long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__));	\
85 	asmlinkage long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
86 	{									\
87 		long ret = __do_compat_sys##name(__MAP(x,__SC_DELOUSE,__VA_ARGS__));\
88 		__MAP(x,__SC_TEST,__VA_ARGS__);					\
89 		return ret;							\
90 	}									\
91 	__diag_pop();								\
92 	static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
93 #endif /* COMPAT_SYSCALL_DEFINEx */
94 
95 struct compat_iovec {
96 	compat_uptr_t	iov_base;
97 	compat_size_t	iov_len;
98 };
99 
100 #ifndef compat_user_stack_pointer
101 #define compat_user_stack_pointer() current_user_stack_pointer()
102 #endif
103 #ifndef compat_sigaltstack	/* we'll need that for MIPS */
104 typedef struct compat_sigaltstack {
105 	compat_uptr_t			ss_sp;
106 	int				ss_flags;
107 	compat_size_t			ss_size;
108 } compat_stack_t;
109 #endif
110 #ifndef COMPAT_MINSIGSTKSZ
111 #define COMPAT_MINSIGSTKSZ	MINSIGSTKSZ
112 #endif
113 
114 #define compat_jiffies_to_clock_t(x)	\
115 		(((unsigned long)(x) * COMPAT_USER_HZ) / HZ)
116 
117 typedef __compat_uid32_t	compat_uid_t;
118 typedef __compat_gid32_t	compat_gid_t;
119 
120 struct compat_sel_arg_struct;
121 struct rusage;
122 
123 struct old_itimerval32;
124 
125 struct compat_tms {
126 	compat_clock_t		tms_utime;
127 	compat_clock_t		tms_stime;
128 	compat_clock_t		tms_cutime;
129 	compat_clock_t		tms_cstime;
130 };
131 
132 #define _COMPAT_NSIG_WORDS	(_COMPAT_NSIG / _COMPAT_NSIG_BPW)
133 
134 typedef struct {
135 	compat_sigset_word	sig[_COMPAT_NSIG_WORDS];
136 } compat_sigset_t;
137 
138 int set_compat_user_sigmask(const compat_sigset_t __user *umask,
139 			    size_t sigsetsize);
140 
141 struct compat_sigaction {
142 #ifndef __ARCH_HAS_IRIX_SIGACTION
143 	compat_uptr_t			sa_handler;
144 	compat_ulong_t			sa_flags;
145 #else
146 	compat_uint_t			sa_flags;
147 	compat_uptr_t			sa_handler;
148 #endif
149 #ifdef __ARCH_HAS_SA_RESTORER
150 	compat_uptr_t			sa_restorer;
151 #endif
152 	compat_sigset_t			sa_mask __packed;
153 };
154 
155 typedef union compat_sigval {
156 	compat_int_t	sival_int;
157 	compat_uptr_t	sival_ptr;
158 } compat_sigval_t;
159 
160 typedef struct compat_siginfo {
161 	int si_signo;
162 #ifndef __ARCH_HAS_SWAPPED_SIGINFO
163 	int si_errno;
164 	int si_code;
165 #else
166 	int si_code;
167 	int si_errno;
168 #endif
169 
170 	union {
171 		int _pad[128/sizeof(int) - 3];
172 
173 		/* kill() */
174 		struct {
175 			compat_pid_t _pid;	/* sender's pid */
176 			__compat_uid32_t _uid;	/* sender's uid */
177 		} _kill;
178 
179 		/* POSIX.1b timers */
180 		struct {
181 			compat_timer_t _tid;	/* timer id */
182 			int _overrun;		/* overrun count */
183 			compat_sigval_t _sigval;	/* same as below */
184 		} _timer;
185 
186 		/* POSIX.1b signals */
187 		struct {
188 			compat_pid_t _pid;	/* sender's pid */
189 			__compat_uid32_t _uid;	/* sender's uid */
190 			compat_sigval_t _sigval;
191 		} _rt;
192 
193 		/* SIGCHLD */
194 		struct {
195 			compat_pid_t _pid;	/* which child */
196 			__compat_uid32_t _uid;	/* sender's uid */
197 			int _status;		/* exit code */
198 			compat_clock_t _utime;
199 			compat_clock_t _stime;
200 		} _sigchld;
201 
202 #ifdef CONFIG_X86_X32_ABI
203 		/* SIGCHLD (x32 version) */
204 		struct {
205 			compat_pid_t _pid;	/* which child */
206 			__compat_uid32_t _uid;	/* sender's uid */
207 			int _status;		/* exit code */
208 			compat_s64 _utime;
209 			compat_s64 _stime;
210 		} _sigchld_x32;
211 #endif
212 
213 		/* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGTRAP, SIGEMT */
214 		struct {
215 			compat_uptr_t _addr;	/* faulting insn/memory ref. */
216 #define __COMPAT_ADDR_BND_PKEY_PAD  (__alignof__(compat_uptr_t) < sizeof(short) ? \
217 				     sizeof(short) : __alignof__(compat_uptr_t))
218 			union {
219 				/* used on alpha and sparc */
220 				int _trapno;	/* TRAP # which caused the signal */
221 				/*
222 				 * used when si_code=BUS_MCEERR_AR or
223 				 * used when si_code=BUS_MCEERR_AO
224 				 */
225 				short int _addr_lsb;	/* Valid LSB of the reported address. */
226 				/* used when si_code=SEGV_BNDERR */
227 				struct {
228 					char _dummy_bnd[__COMPAT_ADDR_BND_PKEY_PAD];
229 					compat_uptr_t _lower;
230 					compat_uptr_t _upper;
231 				} _addr_bnd;
232 				/* used when si_code=SEGV_PKUERR */
233 				struct {
234 					char _dummy_pkey[__COMPAT_ADDR_BND_PKEY_PAD];
235 					u32 _pkey;
236 				} _addr_pkey;
237 				/* used when si_code=TRAP_PERF */
238 				struct {
239 					compat_ulong_t _data;
240 					u32 _type;
241 					u32 _flags;
242 				} _perf;
243 			};
244 		} _sigfault;
245 
246 		/* SIGPOLL */
247 		struct {
248 			compat_long_t _band;	/* POLL_IN, POLL_OUT, POLL_MSG */
249 			int _fd;
250 		} _sigpoll;
251 
252 		struct {
253 			compat_uptr_t _call_addr; /* calling user insn */
254 			int _syscall;	/* triggering system call number */
255 			unsigned int _arch;	/* AUDIT_ARCH_* of syscall */
256 		} _sigsys;
257 	} _sifields;
258 } compat_siginfo_t;
259 
260 struct compat_rlimit {
261 	compat_ulong_t	rlim_cur;
262 	compat_ulong_t	rlim_max;
263 };
264 
265 #ifdef __ARCH_NEED_COMPAT_FLOCK64_PACKED
266 #define __ARCH_COMPAT_FLOCK64_PACK	__attribute__((packed))
267 #else
268 #define __ARCH_COMPAT_FLOCK64_PACK
269 #endif
270 
271 struct compat_flock {
272 	short			l_type;
273 	short			l_whence;
274 	compat_off_t		l_start;
275 	compat_off_t		l_len;
276 #ifdef __ARCH_COMPAT_FLOCK_EXTRA_SYSID
277 	__ARCH_COMPAT_FLOCK_EXTRA_SYSID
278 #endif
279 	compat_pid_t		l_pid;
280 #ifdef __ARCH_COMPAT_FLOCK_PAD
281 	__ARCH_COMPAT_FLOCK_PAD
282 #endif
283 };
284 
285 struct compat_flock64 {
286 	short		l_type;
287 	short		l_whence;
288 	compat_loff_t	l_start;
289 	compat_loff_t	l_len;
290 	compat_pid_t	l_pid;
291 #ifdef __ARCH_COMPAT_FLOCK64_PAD
292 	__ARCH_COMPAT_FLOCK64_PAD
293 #endif
294 } __ARCH_COMPAT_FLOCK64_PACK;
295 
296 struct compat_rusage {
297 	struct old_timeval32 ru_utime;
298 	struct old_timeval32 ru_stime;
299 	compat_long_t	ru_maxrss;
300 	compat_long_t	ru_ixrss;
301 	compat_long_t	ru_idrss;
302 	compat_long_t	ru_isrss;
303 	compat_long_t	ru_minflt;
304 	compat_long_t	ru_majflt;
305 	compat_long_t	ru_nswap;
306 	compat_long_t	ru_inblock;
307 	compat_long_t	ru_oublock;
308 	compat_long_t	ru_msgsnd;
309 	compat_long_t	ru_msgrcv;
310 	compat_long_t	ru_nsignals;
311 	compat_long_t	ru_nvcsw;
312 	compat_long_t	ru_nivcsw;
313 };
314 
315 extern int put_compat_rusage(const struct rusage *,
316 			     struct compat_rusage __user *);
317 
318 struct compat_siginfo;
319 struct __compat_aio_sigset;
320 
321 struct compat_dirent {
322 	u32		d_ino;
323 	compat_off_t	d_off;
324 	u16		d_reclen;
325 	char		d_name[256];
326 };
327 
328 struct compat_ustat {
329 	compat_daddr_t		f_tfree;
330 	compat_ino_t		f_tinode;
331 	char			f_fname[6];
332 	char			f_fpack[6];
333 };
334 
335 #define COMPAT_SIGEV_PAD_SIZE	((SIGEV_MAX_SIZE/sizeof(int)) - 3)
336 
337 typedef struct compat_sigevent {
338 	compat_sigval_t sigev_value;
339 	compat_int_t sigev_signo;
340 	compat_int_t sigev_notify;
341 	union {
342 		compat_int_t _pad[COMPAT_SIGEV_PAD_SIZE];
343 		compat_int_t _tid;
344 
345 		struct {
346 			compat_uptr_t _function;
347 			compat_uptr_t _attribute;
348 		} _sigev_thread;
349 	} _sigev_un;
350 } compat_sigevent_t;
351 
352 struct compat_ifmap {
353 	compat_ulong_t mem_start;
354 	compat_ulong_t mem_end;
355 	unsigned short base_addr;
356 	unsigned char irq;
357 	unsigned char dma;
358 	unsigned char port;
359 };
360 
361 struct compat_if_settings {
362 	unsigned int type;	/* Type of physical device or protocol */
363 	unsigned int size;	/* Size of the data allocated by the caller */
364 	compat_uptr_t ifs_ifsu;	/* union of pointers */
365 };
366 
367 struct compat_ifreq {
368 	union {
369 		char	ifrn_name[IFNAMSIZ];    /* if name, e.g. "en0" */
370 	} ifr_ifrn;
371 	union {
372 		struct	sockaddr ifru_addr;
373 		struct	sockaddr ifru_dstaddr;
374 		struct	sockaddr ifru_broadaddr;
375 		struct	sockaddr ifru_netmask;
376 		struct	sockaddr ifru_hwaddr;
377 		short	ifru_flags;
378 		compat_int_t	ifru_ivalue;
379 		compat_int_t	ifru_mtu;
380 		struct	compat_ifmap ifru_map;
381 		char	ifru_slave[IFNAMSIZ];   /* Just fits the size */
382 		char	ifru_newname[IFNAMSIZ];
383 		compat_caddr_t	ifru_data;
384 		struct	compat_if_settings ifru_settings;
385 	} ifr_ifru;
386 };
387 
388 struct compat_ifconf {
389 	compat_int_t	ifc_len;                /* size of buffer */
390 	compat_caddr_t  ifcbuf;
391 };
392 
393 struct compat_robust_list {
394 	compat_uptr_t			next;
395 };
396 
397 struct compat_robust_list_head {
398 	struct compat_robust_list	list;
399 	compat_long_t			futex_offset;
400 	compat_uptr_t			list_op_pending;
401 };
402 
403 #ifdef CONFIG_COMPAT_OLD_SIGACTION
404 struct compat_old_sigaction {
405 	compat_uptr_t			sa_handler;
406 	compat_old_sigset_t		sa_mask;
407 	compat_ulong_t			sa_flags;
408 	compat_uptr_t			sa_restorer;
409 };
410 #endif
411 
412 struct compat_keyctl_kdf_params {
413 	compat_uptr_t hashname;
414 	compat_uptr_t otherinfo;
415 	__u32 otherinfolen;
416 	__u32 __spare[8];
417 };
418 
419 struct compat_stat;
420 struct compat_statfs;
421 struct compat_statfs64;
422 struct compat_old_linux_dirent;
423 struct compat_linux_dirent;
424 struct linux_dirent64;
425 struct compat_msghdr;
426 struct compat_mmsghdr;
427 struct compat_sysinfo;
428 struct compat_sysctl_args;
429 struct compat_kexec_segment;
430 struct compat_mq_attr;
431 struct compat_msgbuf;
432 
433 void copy_siginfo_to_external32(struct compat_siginfo *to,
434 		const struct kernel_siginfo *from);
435 int copy_siginfo_from_user32(kernel_siginfo_t *to,
436 		const struct compat_siginfo __user *from);
437 int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
438 		const kernel_siginfo_t *from);
439 #ifndef copy_siginfo_to_user32
440 #define copy_siginfo_to_user32 __copy_siginfo_to_user32
441 #endif
442 int get_compat_sigevent(struct sigevent *event,
443 		const struct compat_sigevent __user *u_event);
444 
445 extern int get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat);
446 
447 /*
448  * Defined inline such that size can be compile time constant, which avoids
449  * CONFIG_HARDENED_USERCOPY complaining about copies from task_struct
450  */
451 static inline int
452 put_compat_sigset(compat_sigset_t __user *compat, const sigset_t *set,
453 		  unsigned int size)
454 {
455 	/* size <= sizeof(compat_sigset_t) <= sizeof(sigset_t) */
456 #if defined(__BIG_ENDIAN) && defined(CONFIG_64BIT)
457 	compat_sigset_t v;
458 	switch (_NSIG_WORDS) {
459 	case 4: v.sig[7] = (set->sig[3] >> 32); v.sig[6] = set->sig[3];
460 		fallthrough;
461 	case 3: v.sig[5] = (set->sig[2] >> 32); v.sig[4] = set->sig[2];
462 		fallthrough;
463 	case 2: v.sig[3] = (set->sig[1] >> 32); v.sig[2] = set->sig[1];
464 		fallthrough;
465 	case 1: v.sig[1] = (set->sig[0] >> 32); v.sig[0] = set->sig[0];
466 	}
467 	return copy_to_user(compat, &v, size) ? -EFAULT : 0;
468 #else
469 	return copy_to_user(compat, set, size) ? -EFAULT : 0;
470 #endif
471 }
472 
473 #ifdef CONFIG_CPU_BIG_ENDIAN
474 #define unsafe_put_compat_sigset(compat, set, label) do {		\
475 	compat_sigset_t __user *__c = compat;				\
476 	const sigset_t *__s = set;					\
477 									\
478 	switch (_NSIG_WORDS) {						\
479 	case 4:								\
480 		unsafe_put_user(__s->sig[3] >> 32, &__c->sig[7], label);	\
481 		unsafe_put_user(__s->sig[3], &__c->sig[6], label);	\
482 		fallthrough;						\
483 	case 3:								\
484 		unsafe_put_user(__s->sig[2] >> 32, &__c->sig[5], label);	\
485 		unsafe_put_user(__s->sig[2], &__c->sig[4], label);	\
486 		fallthrough;						\
487 	case 2:								\
488 		unsafe_put_user(__s->sig[1] >> 32, &__c->sig[3], label);	\
489 		unsafe_put_user(__s->sig[1], &__c->sig[2], label);	\
490 		fallthrough;						\
491 	case 1:								\
492 		unsafe_put_user(__s->sig[0] >> 32, &__c->sig[1], label);	\
493 		unsafe_put_user(__s->sig[0], &__c->sig[0], label);	\
494 	}								\
495 } while (0)
496 
497 #define unsafe_get_compat_sigset(set, compat, label) do {		\
498 	const compat_sigset_t __user *__c = compat;			\
499 	compat_sigset_word hi, lo;					\
500 	sigset_t *__s = set;						\
501 									\
502 	switch (_NSIG_WORDS) {						\
503 	case 4:								\
504 		unsafe_get_user(lo, &__c->sig[7], label);		\
505 		unsafe_get_user(hi, &__c->sig[6], label);		\
506 		__s->sig[3] = hi | (((long)lo) << 32);			\
507 		fallthrough;						\
508 	case 3:								\
509 		unsafe_get_user(lo, &__c->sig[5], label);		\
510 		unsafe_get_user(hi, &__c->sig[4], label);		\
511 		__s->sig[2] = hi | (((long)lo) << 32);			\
512 		fallthrough;						\
513 	case 2:								\
514 		unsafe_get_user(lo, &__c->sig[3], label);		\
515 		unsafe_get_user(hi, &__c->sig[2], label);		\
516 		__s->sig[1] = hi | (((long)lo) << 32);			\
517 		fallthrough;						\
518 	case 1:								\
519 		unsafe_get_user(lo, &__c->sig[1], label);		\
520 		unsafe_get_user(hi, &__c->sig[0], label);		\
521 		__s->sig[0] = hi | (((long)lo) << 32);			\
522 	}								\
523 } while (0)
524 #else
525 #define unsafe_put_compat_sigset(compat, set, label) do {		\
526 	compat_sigset_t __user *__c = compat;				\
527 	const sigset_t *__s = set;					\
528 									\
529 	unsafe_copy_to_user(__c, __s, sizeof(*__c), label);		\
530 } while (0)
531 
532 #define unsafe_get_compat_sigset(set, compat, label) do {		\
533 	const compat_sigset_t __user *__c = compat;			\
534 	sigset_t *__s = set;						\
535 									\
536 	unsafe_copy_from_user(__s, __c, sizeof(*__c), label);		\
537 } while (0)
538 #endif
539 
540 extern int compat_ptrace_request(struct task_struct *child,
541 				 compat_long_t request,
542 				 compat_ulong_t addr, compat_ulong_t data);
543 
544 extern long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
545 			       compat_ulong_t addr, compat_ulong_t data);
546 
547 struct epoll_event;	/* fortunately, this one is fixed-layout */
548 
549 int compat_restore_altstack(const compat_stack_t __user *uss);
550 int __compat_save_altstack(compat_stack_t __user *, unsigned long);
551 #define unsafe_compat_save_altstack(uss, sp, label) do { \
552 	compat_stack_t __user *__uss = uss; \
553 	struct task_struct *t = current; \
554 	unsafe_put_user(ptr_to_compat((void __user *)t->sas_ss_sp), \
555 			&__uss->ss_sp, label); \
556 	unsafe_put_user(t->sas_ss_flags, &__uss->ss_flags, label); \
557 	unsafe_put_user(t->sas_ss_size, &__uss->ss_size, label); \
558 } while (0);
559 
560 /*
561  * These syscall function prototypes are kept in the same order as
562  * include/uapi/asm-generic/unistd.h. Deprecated or obsolete system calls
563  * go below.
564  *
565  * Please note that these prototypes here are only provided for information
566  * purposes, for static analysis, and for linking from the syscall table.
567  * These functions should not be called elsewhere from kernel code.
568  *
569  * As the syscall calling convention may be different from the default
570  * for architectures overriding the syscall calling convention, do not
571  * include the prototypes if CONFIG_ARCH_HAS_SYSCALL_WRAPPER is enabled.
572  */
573 #ifndef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
574 asmlinkage long compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p);
575 asmlinkage long compat_sys_io_submit(compat_aio_context_t ctx_id, int nr,
576 				     u32 __user *iocb);
577 asmlinkage long compat_sys_io_pgetevents(compat_aio_context_t ctx_id,
578 					compat_long_t min_nr,
579 					compat_long_t nr,
580 					struct io_event __user *events,
581 					struct old_timespec32 __user *timeout,
582 					const struct __compat_aio_sigset __user *usig);
583 asmlinkage long compat_sys_io_pgetevents_time64(compat_aio_context_t ctx_id,
584 					compat_long_t min_nr,
585 					compat_long_t nr,
586 					struct io_event __user *events,
587 					struct __kernel_timespec __user *timeout,
588 					const struct __compat_aio_sigset __user *usig);
589 asmlinkage long compat_sys_epoll_pwait(int epfd,
590 			struct epoll_event __user *events,
591 			int maxevents, int timeout,
592 			const compat_sigset_t __user *sigmask,
593 			compat_size_t sigsetsize);
594 asmlinkage long compat_sys_epoll_pwait2(int epfd,
595 			struct epoll_event __user *events,
596 			int maxevents,
597 			const struct __kernel_timespec __user *timeout,
598 			const compat_sigset_t __user *sigmask,
599 			compat_size_t sigsetsize);
600 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
601 				 compat_ulong_t arg);
602 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
603 				   compat_ulong_t arg);
604 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
605 				 compat_ulong_t arg);
606 asmlinkage long compat_sys_statfs(const char __user *pathname,
607 				  struct compat_statfs __user *buf);
608 asmlinkage long compat_sys_statfs64(const char __user *pathname,
609 				    compat_size_t sz,
610 				    struct compat_statfs64 __user *buf);
611 asmlinkage long compat_sys_fstatfs(unsigned int fd,
612 				   struct compat_statfs __user *buf);
613 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz,
614 				     struct compat_statfs64 __user *buf);
615 asmlinkage long compat_sys_truncate(const char __user *, compat_off_t);
616 asmlinkage long compat_sys_ftruncate(unsigned int, compat_off_t);
617 /* No generic prototype for truncate64, ftruncate64, fallocate */
618 asmlinkage long compat_sys_openat(int dfd, const char __user *filename,
619 				  int flags, umode_t mode);
620 asmlinkage long compat_sys_getdents(unsigned int fd,
621 				    struct compat_linux_dirent __user *dirent,
622 				    unsigned int count);
623 asmlinkage long compat_sys_lseek(unsigned int, compat_off_t, unsigned int);
624 /* No generic prototype for pread64 and pwrite64 */
625 asmlinkage ssize_t compat_sys_preadv(compat_ulong_t fd,
626 		const struct iovec __user *vec,
627 		compat_ulong_t vlen, u32 pos_low, u32 pos_high);
628 asmlinkage ssize_t compat_sys_pwritev(compat_ulong_t fd,
629 		const struct iovec __user *vec,
630 		compat_ulong_t vlen, u32 pos_low, u32 pos_high);
631 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
632 asmlinkage long compat_sys_preadv64(unsigned long fd,
633 		const struct iovec __user *vec,
634 		unsigned long vlen, loff_t pos);
635 #endif
636 
637 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
638 asmlinkage long compat_sys_pwritev64(unsigned long fd,
639 		const struct iovec __user *vec,
640 		unsigned long vlen, loff_t pos);
641 #endif
642 asmlinkage long compat_sys_sendfile(int out_fd, int in_fd,
643 				    compat_off_t __user *offset, compat_size_t count);
644 asmlinkage long compat_sys_sendfile64(int out_fd, int in_fd,
645 				    compat_loff_t __user *offset, compat_size_t count);
646 asmlinkage long compat_sys_pselect6_time32(int n, compat_ulong_t __user *inp,
647 				    compat_ulong_t __user *outp,
648 				    compat_ulong_t __user *exp,
649 				    struct old_timespec32 __user *tsp,
650 				    void __user *sig);
651 asmlinkage long compat_sys_pselect6_time64(int n, compat_ulong_t __user *inp,
652 				    compat_ulong_t __user *outp,
653 				    compat_ulong_t __user *exp,
654 				    struct __kernel_timespec __user *tsp,
655 				    void __user *sig);
656 asmlinkage long compat_sys_ppoll_time32(struct pollfd __user *ufds,
657 				 unsigned int nfds,
658 				 struct old_timespec32 __user *tsp,
659 				 const compat_sigset_t __user *sigmask,
660 				 compat_size_t sigsetsize);
661 asmlinkage long compat_sys_ppoll_time64(struct pollfd __user *ufds,
662 				 unsigned int nfds,
663 				 struct __kernel_timespec __user *tsp,
664 				 const compat_sigset_t __user *sigmask,
665 				 compat_size_t sigsetsize);
666 asmlinkage long compat_sys_signalfd4(int ufd,
667 				     const compat_sigset_t __user *sigmask,
668 				     compat_size_t sigsetsize, int flags);
669 asmlinkage long compat_sys_newfstatat(unsigned int dfd,
670 				      const char __user *filename,
671 				      struct compat_stat __user *statbuf,
672 				      int flag);
673 asmlinkage long compat_sys_newfstat(unsigned int fd,
674 				    struct compat_stat __user *statbuf);
675 /* No generic prototype for sync_file_range and sync_file_range2 */
676 asmlinkage long compat_sys_waitid(int, compat_pid_t,
677 		struct compat_siginfo __user *, int,
678 		struct compat_rusage __user *);
679 asmlinkage long
680 compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
681 			   compat_size_t len);
682 asmlinkage long
683 compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
684 			   compat_size_t __user *len_ptr);
685 asmlinkage long compat_sys_getitimer(int which,
686 				     struct old_itimerval32 __user *it);
687 asmlinkage long compat_sys_setitimer(int which,
688 				     struct old_itimerval32 __user *in,
689 				     struct old_itimerval32 __user *out);
690 asmlinkage long compat_sys_kexec_load(compat_ulong_t entry,
691 				      compat_ulong_t nr_segments,
692 				      struct compat_kexec_segment __user *,
693 				      compat_ulong_t flags);
694 asmlinkage long compat_sys_timer_create(clockid_t which_clock,
695 			struct compat_sigevent __user *timer_event_spec,
696 			timer_t __user *created_timer_id);
697 asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
698 				  compat_long_t addr, compat_long_t data);
699 asmlinkage long compat_sys_sched_setaffinity(compat_pid_t pid,
700 				     unsigned int len,
701 				     compat_ulong_t __user *user_mask_ptr);
702 asmlinkage long compat_sys_sched_getaffinity(compat_pid_t pid,
703 				     unsigned int len,
704 				     compat_ulong_t __user *user_mask_ptr);
705 asmlinkage long compat_sys_sigaltstack(const compat_stack_t __user *uss_ptr,
706 				       compat_stack_t __user *uoss_ptr);
707 asmlinkage long compat_sys_rt_sigsuspend(compat_sigset_t __user *unewset,
708 					 compat_size_t sigsetsize);
709 #ifndef CONFIG_ODD_RT_SIGACTION
710 asmlinkage long compat_sys_rt_sigaction(int,
711 				 const struct compat_sigaction __user *,
712 				 struct compat_sigaction __user *,
713 				 compat_size_t);
714 #endif
715 asmlinkage long compat_sys_rt_sigprocmask(int how, compat_sigset_t __user *set,
716 					  compat_sigset_t __user *oset,
717 					  compat_size_t sigsetsize);
718 asmlinkage long compat_sys_rt_sigpending(compat_sigset_t __user *uset,
719 					 compat_size_t sigsetsize);
720 asmlinkage long compat_sys_rt_sigtimedwait_time32(compat_sigset_t __user *uthese,
721 		struct compat_siginfo __user *uinfo,
722 		struct old_timespec32 __user *uts, compat_size_t sigsetsize);
723 asmlinkage long compat_sys_rt_sigtimedwait_time64(compat_sigset_t __user *uthese,
724 		struct compat_siginfo __user *uinfo,
725 		struct __kernel_timespec __user *uts, compat_size_t sigsetsize);
726 asmlinkage long compat_sys_rt_sigqueueinfo(compat_pid_t pid, int sig,
727 				struct compat_siginfo __user *uinfo);
728 /* No generic prototype for rt_sigreturn */
729 asmlinkage long compat_sys_times(struct compat_tms __user *tbuf);
730 asmlinkage long compat_sys_getrlimit(unsigned int resource,
731 				     struct compat_rlimit __user *rlim);
732 asmlinkage long compat_sys_setrlimit(unsigned int resource,
733 				     struct compat_rlimit __user *rlim);
734 asmlinkage long compat_sys_getrusage(int who, struct compat_rusage __user *ru);
735 asmlinkage long compat_sys_gettimeofday(struct old_timeval32 __user *tv,
736 		struct timezone __user *tz);
737 asmlinkage long compat_sys_settimeofday(struct old_timeval32 __user *tv,
738 		struct timezone __user *tz);
739 asmlinkage long compat_sys_sysinfo(struct compat_sysinfo __user *info);
740 asmlinkage long compat_sys_mq_open(const char __user *u_name,
741 			int oflag, compat_mode_t mode,
742 			struct compat_mq_attr __user *u_attr);
743 asmlinkage long compat_sys_mq_notify(mqd_t mqdes,
744 			const struct compat_sigevent __user *u_notification);
745 asmlinkage long compat_sys_mq_getsetattr(mqd_t mqdes,
746 			const struct compat_mq_attr __user *u_mqstat,
747 			struct compat_mq_attr __user *u_omqstat);
748 asmlinkage long compat_sys_msgctl(int first, int second, void __user *uptr);
749 asmlinkage long compat_sys_msgrcv(int msqid, compat_uptr_t msgp,
750 		compat_ssize_t msgsz, compat_long_t msgtyp, int msgflg);
751 asmlinkage long compat_sys_msgsnd(int msqid, compat_uptr_t msgp,
752 		compat_ssize_t msgsz, int msgflg);
753 asmlinkage long compat_sys_semctl(int semid, int semnum, int cmd, int arg);
754 asmlinkage long compat_sys_shmctl(int first, int second, void __user *uptr);
755 asmlinkage long compat_sys_shmat(int shmid, compat_uptr_t shmaddr, int shmflg);
756 asmlinkage long compat_sys_recvfrom(int fd, void __user *buf, compat_size_t len,
757 			    unsigned flags, struct sockaddr __user *addr,
758 			    int __user *addrlen);
759 asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg,
760 				   unsigned flags);
761 asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg,
762 				   unsigned int flags);
763 /* No generic prototype for readahead */
764 asmlinkage long compat_sys_keyctl(u32 option,
765 			      u32 arg2, u32 arg3, u32 arg4, u32 arg5);
766 asmlinkage long compat_sys_execve(const char __user *filename, const compat_uptr_t __user *argv,
767 		     const compat_uptr_t __user *envp);
768 /* No generic prototype for fadvise64_64 */
769 /* CONFIG_MMU only */
770 asmlinkage long compat_sys_rt_tgsigqueueinfo(compat_pid_t tgid,
771 					compat_pid_t pid, int sig,
772 					struct compat_siginfo __user *uinfo);
773 asmlinkage long compat_sys_recvmmsg_time64(int fd, struct compat_mmsghdr __user *mmsg,
774 				    unsigned vlen, unsigned int flags,
775 				    struct __kernel_timespec __user *timeout);
776 asmlinkage long compat_sys_recvmmsg_time32(int fd, struct compat_mmsghdr __user *mmsg,
777 				    unsigned vlen, unsigned int flags,
778 				    struct old_timespec32 __user *timeout);
779 asmlinkage long compat_sys_wait4(compat_pid_t pid,
780 				 compat_uint_t __user *stat_addr, int options,
781 				 struct compat_rusage __user *ru);
782 asmlinkage long compat_sys_fanotify_mark(int, unsigned int, __u32, __u32,
783 					    int, const char __user *);
784 asmlinkage long compat_sys_open_by_handle_at(int mountdirfd,
785 					     struct file_handle __user *handle,
786 					     int flags);
787 asmlinkage long compat_sys_sendmmsg(int fd, struct compat_mmsghdr __user *mmsg,
788 				    unsigned vlen, unsigned int flags);
789 asmlinkage long compat_sys_execveat(int dfd, const char __user *filename,
790 		     const compat_uptr_t __user *argv,
791 		     const compat_uptr_t __user *envp, int flags);
792 asmlinkage ssize_t compat_sys_preadv2(compat_ulong_t fd,
793 		const struct iovec __user *vec,
794 		compat_ulong_t vlen, u32 pos_low, u32 pos_high, rwf_t flags);
795 asmlinkage ssize_t compat_sys_pwritev2(compat_ulong_t fd,
796 		const struct iovec __user *vec,
797 		compat_ulong_t vlen, u32 pos_low, u32 pos_high, rwf_t flags);
798 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
799 asmlinkage long  compat_sys_preadv64v2(unsigned long fd,
800 		const struct iovec __user *vec,
801 		unsigned long vlen, loff_t pos, rwf_t flags);
802 #endif
803 
804 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
805 asmlinkage long compat_sys_pwritev64v2(unsigned long fd,
806 		const struct iovec __user *vec,
807 		unsigned long vlen, loff_t pos, rwf_t flags);
808 #endif
809 
810 
811 /*
812  * Deprecated system calls which are still defined in
813  * include/uapi/asm-generic/unistd.h and wanted by >= 1 arch
814  */
815 
816 /* __ARCH_WANT_SYSCALL_NO_AT */
817 asmlinkage long compat_sys_open(const char __user *filename, int flags,
818 				umode_t mode);
819 
820 /* __ARCH_WANT_SYSCALL_NO_FLAGS */
821 asmlinkage long compat_sys_signalfd(int ufd,
822 				    const compat_sigset_t __user *sigmask,
823 				    compat_size_t sigsetsize);
824 
825 /* __ARCH_WANT_SYSCALL_OFF_T */
826 asmlinkage long compat_sys_newstat(const char __user *filename,
827 				   struct compat_stat __user *statbuf);
828 asmlinkage long compat_sys_newlstat(const char __user *filename,
829 				    struct compat_stat __user *statbuf);
830 
831 /* __ARCH_WANT_SYSCALL_DEPRECATED */
832 asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
833 		compat_ulong_t __user *outp, compat_ulong_t __user *exp,
834 		struct old_timeval32 __user *tvp);
835 asmlinkage long compat_sys_ustat(unsigned dev, struct compat_ustat __user *u32);
836 asmlinkage long compat_sys_recv(int fd, void __user *buf, compat_size_t len,
837 				unsigned flags);
838 
839 /* obsolete */
840 asmlinkage long compat_sys_old_readdir(unsigned int fd,
841 				       struct compat_old_linux_dirent __user *,
842 				       unsigned int count);
843 
844 /* obsolete */
845 asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg);
846 
847 /* obsolete */
848 asmlinkage long compat_sys_ipc(u32, int, int, u32, compat_uptr_t, u32);
849 
850 /* obsolete */
851 #ifdef __ARCH_WANT_SYS_SIGPENDING
852 asmlinkage long compat_sys_sigpending(compat_old_sigset_t __user *set);
853 #endif
854 
855 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
856 asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *nset,
857 				       compat_old_sigset_t __user *oset);
858 #endif
859 #ifdef CONFIG_COMPAT_OLD_SIGACTION
860 asmlinkage long compat_sys_sigaction(int sig,
861                                    const struct compat_old_sigaction __user *act,
862                                    struct compat_old_sigaction __user *oact);
863 #endif
864 
865 /* obsolete */
866 asmlinkage long compat_sys_socketcall(int call, u32 __user *args);
867 
868 #ifdef __ARCH_WANT_COMPAT_TRUNCATE64
869 asmlinkage long compat_sys_truncate64(const char __user *pathname, compat_arg_u64(len));
870 #endif
871 
872 #ifdef __ARCH_WANT_COMPAT_FTRUNCATE64
873 asmlinkage long compat_sys_ftruncate64(unsigned int fd, compat_arg_u64(len));
874 #endif
875 
876 #ifdef __ARCH_WANT_COMPAT_FALLOCATE
877 asmlinkage long compat_sys_fallocate(int fd, int mode, compat_arg_u64(offset),
878 				     compat_arg_u64(len));
879 #endif
880 
881 #ifdef __ARCH_WANT_COMPAT_PREAD64
882 asmlinkage long compat_sys_pread64(unsigned int fd, char __user *buf, size_t count,
883 				   compat_arg_u64(pos));
884 #endif
885 
886 #ifdef __ARCH_WANT_COMPAT_PWRITE64
887 asmlinkage long compat_sys_pwrite64(unsigned int fd, const char __user *buf, size_t count,
888 				    compat_arg_u64(pos));
889 #endif
890 
891 #ifdef __ARCH_WANT_COMPAT_SYNC_FILE_RANGE
892 asmlinkage long compat_sys_sync_file_range(int fd, compat_arg_u64(pos),
893 					   compat_arg_u64(nbytes), unsigned int flags);
894 #endif
895 
896 #ifdef __ARCH_WANT_COMPAT_FADVISE64_64
897 asmlinkage long compat_sys_fadvise64_64(int fd, compat_arg_u64(pos),
898 					compat_arg_u64(len), int advice);
899 #endif
900 
901 #ifdef __ARCH_WANT_COMPAT_READAHEAD
902 asmlinkage long compat_sys_readahead(int fd, compat_arg_u64(offset), size_t count);
903 #endif
904 
905 #endif /* CONFIG_ARCH_HAS_SYSCALL_WRAPPER */
906 
907 /**
908  * ns_to_old_timeval32 - Compat version of ns_to_timeval
909  * @nsec:	the nanoseconds value to be converted
910  *
911  * Returns the old_timeval32 representation of the nsec parameter.
912  */
913 static inline struct old_timeval32 ns_to_old_timeval32(s64 nsec)
914 {
915 	struct __kernel_old_timeval tv;
916 	struct old_timeval32 ctv;
917 
918 	tv = ns_to_kernel_old_timeval(nsec);
919 	ctv.tv_sec = tv.tv_sec;
920 	ctv.tv_usec = tv.tv_usec;
921 
922 	return ctv;
923 }
924 
925 /*
926  * Kernel code should not call compat syscalls (i.e., compat_sys_xyzyyz())
927  * directly.  Instead, use one of the functions which work equivalently, such
928  * as the kcompat_sys_xyzyyz() functions prototyped below.
929  */
930 
931 int kcompat_sys_statfs64(const char __user * pathname, compat_size_t sz,
932 		     struct compat_statfs64 __user * buf);
933 int kcompat_sys_fstatfs64(unsigned int fd, compat_size_t sz,
934 			  struct compat_statfs64 __user * buf);
935 
936 #ifdef CONFIG_COMPAT
937 
938 /*
939  * For most but not all architectures, "am I in a compat syscall?" and
940  * "am I a compat task?" are the same question.  For architectures on which
941  * they aren't the same question, arch code can override in_compat_syscall.
942  */
943 #ifndef in_compat_syscall
944 static inline bool in_compat_syscall(void) { return is_compat_task(); }
945 #endif
946 
947 #else /* !CONFIG_COMPAT */
948 
949 #define is_compat_task() (0)
950 /* Ensure no one redefines in_compat_syscall() under !CONFIG_COMPAT */
951 #define in_compat_syscall in_compat_syscall
952 static inline bool in_compat_syscall(void) { return false; }
953 
954 #endif /* CONFIG_COMPAT */
955 
956 #define BITS_PER_COMPAT_LONG    (8*sizeof(compat_long_t))
957 
958 #define BITS_TO_COMPAT_LONGS(bits) DIV_ROUND_UP(bits, BITS_PER_COMPAT_LONG)
959 
960 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
961 		       unsigned long bitmap_size);
962 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
963 		       unsigned long bitmap_size);
964 
965 /*
966  * Some legacy ABIs like the i386 one use less than natural alignment for 64-bit
967  * types, and will need special compat treatment for that.  Most architectures
968  * don't need that special handling even for compat syscalls.
969  */
970 #ifndef compat_need_64bit_alignment_fixup
971 #define compat_need_64bit_alignment_fixup()		false
972 #endif
973 
974 /*
975  * A pointer passed in from user mode. This should not
976  * be used for syscall parameters, just declare them
977  * as pointers because the syscall entry code will have
978  * appropriately converted them already.
979  */
980 #ifndef compat_ptr
981 static inline void __user *compat_ptr(compat_uptr_t uptr)
982 {
983 	return (void __user *)(unsigned long)uptr;
984 }
985 #endif
986 
987 static inline compat_uptr_t ptr_to_compat(void __user *uptr)
988 {
989 	return (u32)(unsigned long)uptr;
990 }
991 
992 #endif /* _LINUX_COMPAT_H */
993