xref: /openbmc/linux/arch/x86/include/asm/compat.h (revision ea64d5ac)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_COMPAT_H
31965aae3SH. Peter Anvin #define _ASM_X86_COMPAT_H
4bb898558SAl Viro 
5bb898558SAl Viro /*
6bb898558SAl Viro  * Architecture specific compatibility types
7bb898558SAl Viro  */
8bb898558SAl Viro #include <linux/types.h>
9bb898558SAl Viro #include <linux/sched.h>
10d375cf15SAndy Lutomirski #include <linux/sched/task_stack.h>
11d1a797f3SH. Peter Anvin #include <asm/processor.h>
12bb898558SAl Viro #include <asm/user32.h>
13fca460f9SH. Peter Anvin #include <asm/unistd.h>
14bb898558SAl Viro 
15bb898558SAl Viro #define COMPAT_USER_HZ		100
16e28cbf22SChristoph Hellwig #define COMPAT_UTS_MACHINE	"i686\0\0"
17bb898558SAl Viro 
18bb898558SAl Viro typedef u32		compat_size_t;
19bb898558SAl Viro typedef s32		compat_ssize_t;
20bb898558SAl Viro typedef s32		compat_time_t;
21bb898558SAl Viro typedef s32		compat_clock_t;
22bb898558SAl Viro typedef s32		compat_pid_t;
23bb898558SAl Viro typedef u16		__compat_uid_t;
24bb898558SAl Viro typedef u16		__compat_gid_t;
25bb898558SAl Viro typedef u32		__compat_uid32_t;
26bb898558SAl Viro typedef u32		__compat_gid32_t;
27bb898558SAl Viro typedef u16		compat_mode_t;
28bb898558SAl Viro typedef u32		compat_ino_t;
29bb898558SAl Viro typedef u16		compat_dev_t;
30bb898558SAl Viro typedef s32		compat_off_t;
31bb898558SAl Viro typedef s64		compat_loff_t;
32bb898558SAl Viro typedef u16		compat_nlink_t;
33bb898558SAl Viro typedef u16		compat_ipc_pid_t;
34bb898558SAl Viro typedef s32		compat_daddr_t;
35bb898558SAl Viro typedef u32		compat_caddr_t;
36bb898558SAl Viro typedef __kernel_fsid_t	compat_fsid_t;
37bb898558SAl Viro typedef s32		compat_timer_t;
38bb898558SAl Viro typedef s32		compat_key_t;
39bb898558SAl Viro 
40bb898558SAl Viro typedef s32		compat_int_t;
41bb898558SAl Viro typedef s32		compat_long_t;
42bb898558SAl Viro typedef s64 __attribute__((aligned(4))) compat_s64;
43bb898558SAl Viro typedef u32		compat_uint_t;
44bb898558SAl Viro typedef u32		compat_ulong_t;
45a4455082SDave Hansen typedef u32		compat_u32;
46bb898558SAl Viro typedef u64 __attribute__((aligned(4))) compat_u64;
47751f409dSDenys Vlasenko typedef u32		compat_uptr_t;
48bb898558SAl Viro 
49bb898558SAl Viro struct compat_timespec {
50bb898558SAl Viro 	compat_time_t	tv_sec;
51bb898558SAl Viro 	s32		tv_nsec;
52bb898558SAl Viro };
53bb898558SAl Viro 
54bb898558SAl Viro struct compat_timeval {
55bb898558SAl Viro 	compat_time_t	tv_sec;
56bb898558SAl Viro 	s32		tv_usec;
57bb898558SAl Viro };
58bb898558SAl Viro 
59bb898558SAl Viro struct compat_stat {
60bb898558SAl Viro 	compat_dev_t	st_dev;
61bb898558SAl Viro 	u16		__pad1;
62bb898558SAl Viro 	compat_ino_t	st_ino;
63bb898558SAl Viro 	compat_mode_t	st_mode;
64bb898558SAl Viro 	compat_nlink_t	st_nlink;
65bb898558SAl Viro 	__compat_uid_t	st_uid;
66bb898558SAl Viro 	__compat_gid_t	st_gid;
67bb898558SAl Viro 	compat_dev_t	st_rdev;
68bb898558SAl Viro 	u16		__pad2;
69bb898558SAl Viro 	u32		st_size;
70bb898558SAl Viro 	u32		st_blksize;
71bb898558SAl Viro 	u32		st_blocks;
72bb898558SAl Viro 	u32		st_atime;
73bb898558SAl Viro 	u32		st_atime_nsec;
74bb898558SAl Viro 	u32		st_mtime;
75bb898558SAl Viro 	u32		st_mtime_nsec;
76bb898558SAl Viro 	u32		st_ctime;
77bb898558SAl Viro 	u32		st_ctime_nsec;
78bb898558SAl Viro 	u32		__unused4;
79bb898558SAl Viro 	u32		__unused5;
80bb898558SAl Viro };
81bb898558SAl Viro 
82bb898558SAl Viro struct compat_flock {
83bb898558SAl Viro 	short		l_type;
84bb898558SAl Viro 	short		l_whence;
85bb898558SAl Viro 	compat_off_t	l_start;
86bb898558SAl Viro 	compat_off_t	l_len;
87bb898558SAl Viro 	compat_pid_t	l_pid;
88bb898558SAl Viro };
89bb898558SAl Viro 
90bb898558SAl Viro #define F_GETLK64	12	/*  using 'struct flock64' */
91bb898558SAl Viro #define F_SETLK64	13
92bb898558SAl Viro #define F_SETLKW64	14
93bb898558SAl Viro 
94bb898558SAl Viro /*
95bb898558SAl Viro  * IA32 uses 4 byte alignment for 64 bit quantities,
96bb898558SAl Viro  * so we need to pack this structure.
97bb898558SAl Viro  */
98bb898558SAl Viro struct compat_flock64 {
99bb898558SAl Viro 	short		l_type;
100bb898558SAl Viro 	short		l_whence;
101bb898558SAl Viro 	compat_loff_t	l_start;
102bb898558SAl Viro 	compat_loff_t	l_len;
103bb898558SAl Viro 	compat_pid_t	l_pid;
104bb898558SAl Viro } __attribute__((packed));
105bb898558SAl Viro 
106bb898558SAl Viro struct compat_statfs {
107bb898558SAl Viro 	int		f_type;
108bb898558SAl Viro 	int		f_bsize;
109bb898558SAl Viro 	int		f_blocks;
110bb898558SAl Viro 	int		f_bfree;
111bb898558SAl Viro 	int		f_bavail;
112bb898558SAl Viro 	int		f_files;
113bb898558SAl Viro 	int		f_ffree;
114bb898558SAl Viro 	compat_fsid_t	f_fsid;
115bb898558SAl Viro 	int		f_namelen;	/* SunOS ignores this field. */
116bb898558SAl Viro 	int		f_frsize;
1171448c721SEric W. Biederman 	int		f_flags;
1181448c721SEric W. Biederman 	int		f_spare[4];
119bb898558SAl Viro };
120bb898558SAl Viro 
121bb898558SAl Viro #define COMPAT_RLIM_INFINITY		0xffffffff
122bb898558SAl Viro 
123bb898558SAl Viro typedef u32		compat_old_sigset_t;	/* at least 32 bits */
124bb898558SAl Viro 
125bb898558SAl Viro #define _COMPAT_NSIG		64
126bb898558SAl Viro #define _COMPAT_NSIG_BPW	32
127bb898558SAl Viro 
128bb898558SAl Viro typedef u32               compat_sigset_word;
129bb898558SAl Viro 
130bb898558SAl Viro #define COMPAT_OFF_T_MAX	0x7fffffff
131bb898558SAl Viro 
132bb898558SAl Viro struct compat_ipc64_perm {
133bb898558SAl Viro 	compat_key_t key;
134bb898558SAl Viro 	__compat_uid32_t uid;
135bb898558SAl Viro 	__compat_gid32_t gid;
136bb898558SAl Viro 	__compat_uid32_t cuid;
137bb898558SAl Viro 	__compat_gid32_t cgid;
138bb898558SAl Viro 	unsigned short mode;
139bb898558SAl Viro 	unsigned short __pad1;
140bb898558SAl Viro 	unsigned short seq;
141bb898558SAl Viro 	unsigned short __pad2;
142bb898558SAl Viro 	compat_ulong_t unused1;
143bb898558SAl Viro 	compat_ulong_t unused2;
144bb898558SAl Viro };
145bb898558SAl Viro 
146bb898558SAl Viro struct compat_semid64_ds {
147bb898558SAl Viro 	struct compat_ipc64_perm sem_perm;
148bb898558SAl Viro 	compat_time_t  sem_otime;
149bb898558SAl Viro 	compat_ulong_t __unused1;
150bb898558SAl Viro 	compat_time_t  sem_ctime;
151bb898558SAl Viro 	compat_ulong_t __unused2;
152bb898558SAl Viro 	compat_ulong_t sem_nsems;
153bb898558SAl Viro 	compat_ulong_t __unused3;
154bb898558SAl Viro 	compat_ulong_t __unused4;
155bb898558SAl Viro };
156bb898558SAl Viro 
157bb898558SAl Viro struct compat_msqid64_ds {
158bb898558SAl Viro 	struct compat_ipc64_perm msg_perm;
159bb898558SAl Viro 	compat_time_t  msg_stime;
160bb898558SAl Viro 	compat_ulong_t __unused1;
161bb898558SAl Viro 	compat_time_t  msg_rtime;
162bb898558SAl Viro 	compat_ulong_t __unused2;
163bb898558SAl Viro 	compat_time_t  msg_ctime;
164bb898558SAl Viro 	compat_ulong_t __unused3;
165bb898558SAl Viro 	compat_ulong_t msg_cbytes;
166bb898558SAl Viro 	compat_ulong_t msg_qnum;
167bb898558SAl Viro 	compat_ulong_t msg_qbytes;
168bb898558SAl Viro 	compat_pid_t   msg_lspid;
169bb898558SAl Viro 	compat_pid_t   msg_lrpid;
170bb898558SAl Viro 	compat_ulong_t __unused4;
171bb898558SAl Viro 	compat_ulong_t __unused5;
172bb898558SAl Viro };
173bb898558SAl Viro 
174bb898558SAl Viro struct compat_shmid64_ds {
175bb898558SAl Viro 	struct compat_ipc64_perm shm_perm;
176bb898558SAl Viro 	compat_size_t  shm_segsz;
177bb898558SAl Viro 	compat_time_t  shm_atime;
178bb898558SAl Viro 	compat_ulong_t __unused1;
179bb898558SAl Viro 	compat_time_t  shm_dtime;
180bb898558SAl Viro 	compat_ulong_t __unused2;
181bb898558SAl Viro 	compat_time_t  shm_ctime;
182bb898558SAl Viro 	compat_ulong_t __unused3;
183bb898558SAl Viro 	compat_pid_t   shm_cpid;
184bb898558SAl Viro 	compat_pid_t   shm_lpid;
185bb898558SAl Viro 	compat_ulong_t shm_nattch;
186bb898558SAl Viro 	compat_ulong_t __unused4;
187bb898558SAl Viro 	compat_ulong_t __unused5;
188bb898558SAl Viro };
189bb898558SAl Viro 
190bb898558SAl Viro /*
191bb898558SAl Viro  * The type of struct elf_prstatus.pr_reg in compatible core dumps.
192bb898558SAl Viro  */
193d1a797f3SH. Peter Anvin typedef struct user_regs_struct compat_elf_gregset_t;
194d1a797f3SH. Peter Anvin 
19590954e7bSDmitry Safonov /* Full regset -- prstatus on x32, otherwise on ia32 */
19690954e7bSDmitry Safonov #define PRSTATUS_SIZE(S, R) (R != sizeof(S.pr_reg) ? 144 : 296)
19790954e7bSDmitry Safonov #define SET_PR_FPVALID(S, V, R) \
19890954e7bSDmitry Safonov   do { *(int *) (((void *) &((S)->pr_reg)) + R) = (V); } \
199d1a797f3SH. Peter Anvin   while (0)
200d1a797f3SH. Peter Anvin 
2017b2dd368SDmitry Safonov #ifdef CONFIG_X86_X32_ABI
202d1a797f3SH. Peter Anvin #define COMPAT_USE_64BIT_TIME \
203d1a797f3SH. Peter Anvin 	(!!(task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT))
204d1a797f3SH. Peter Anvin #endif
205bb898558SAl Viro 
206bb898558SAl Viro /*
207bb898558SAl Viro  * A pointer passed in from user mode. This should not
208bb898558SAl Viro  * be used for syscall parameters, just declare them
209bb898558SAl Viro  * as pointers because the syscall entry code will have
210bb898558SAl Viro  * appropriately converted them already.
211bb898558SAl Viro  */
212bb898558SAl Viro 
213bb898558SAl Viro static inline void __user *compat_ptr(compat_uptr_t uptr)
214bb898558SAl Viro {
215bb898558SAl Viro 	return (void __user *)(unsigned long)uptr;
216bb898558SAl Viro }
217bb898558SAl Viro 
218bb898558SAl Viro static inline compat_uptr_t ptr_to_compat(void __user *uptr)
219bb898558SAl Viro {
220bb898558SAl Viro 	return (u32)(unsigned long)uptr;
221bb898558SAl Viro }
222bb898558SAl Viro 
223c41d68a5SH. Peter Anvin static inline void __user *arch_compat_alloc_user_space(long len)
224bb898558SAl Viro {
225d1a797f3SH. Peter Anvin 	compat_uptr_t sp;
226d1a797f3SH. Peter Anvin 
227d1a797f3SH. Peter Anvin 	if (test_thread_flag(TIF_IA32)) {
228d1a797f3SH. Peter Anvin 		sp = task_pt_regs(current)->sp;
229d1a797f3SH. Peter Anvin 	} else {
230d1a797f3SH. Peter Anvin 		/* -128 for the x32 ABI redzone */
231263042e4SDenys Vlasenko 		sp = task_pt_regs(current)->sp - 128;
232d1a797f3SH. Peter Anvin 	}
233d1a797f3SH. Peter Anvin 
234d1a797f3SH. Peter Anvin 	return (void __user *)round_down(sp - len, 16);
235bb898558SAl Viro }
236bb898558SAl Viro 
237abfb9498SDmitry Safonov static inline bool in_x32_syscall(void)
238a628b684SH. Peter Anvin {
239fca460f9SH. Peter Anvin #ifdef CONFIG_X86_X32_ABI
240fca460f9SH. Peter Anvin 	if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
241fca460f9SH. Peter Anvin 		return true;
242fca460f9SH. Peter Anvin #endif
243fca460f9SH. Peter Anvin 	return false;
244bb898558SAl Viro }
245bb898558SAl Viro 
246f970165bSAndy Lutomirski static inline bool in_compat_syscall(void)
247a628b684SH. Peter Anvin {
248abfb9498SDmitry Safonov 	return in_ia32_syscall() || in_x32_syscall();
249a628b684SH. Peter Anvin }
250f970165bSAndy Lutomirski #define in_compat_syscall in_compat_syscall	/* override the generic impl */
251a628b684SH. Peter Anvin 
252ea64d5acSEric W. Biederman struct compat_siginfo;
253ea64d5acSEric W. Biederman int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
254ea64d5acSEric W. Biederman 		const siginfo_t *from, bool x32_ABI);
255ea64d5acSEric W. Biederman 
2561965aae3SH. Peter Anvin #endif /* _ASM_X86_COMPAT_H */
257