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