xref: /openbmc/linux/arch/x86/include/asm/compat.h (revision 527c4125)
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 
15fb373975SArnd Bergmann #include <asm-generic/compat.h>
16fb373975SArnd Bergmann 
17bb898558SAl Viro #define COMPAT_USER_HZ		100
18e28cbf22SChristoph Hellwig #define COMPAT_UTS_MACHINE	"i686\0\0"
19bb898558SAl Viro 
20bb898558SAl Viro typedef u16		__compat_uid_t;
21bb898558SAl Viro typedef u16		__compat_gid_t;
22bb898558SAl Viro typedef u32		__compat_uid32_t;
23bb898558SAl Viro typedef u32		__compat_gid32_t;
24bb898558SAl Viro typedef u16		compat_mode_t;
25bb898558SAl Viro typedef u16		compat_dev_t;
26bb898558SAl Viro typedef u16		compat_nlink_t;
27bb898558SAl Viro typedef u16		compat_ipc_pid_t;
28bb898558SAl Viro typedef u32		compat_caddr_t;
29bb898558SAl Viro typedef __kernel_fsid_t	compat_fsid_t;
30bb898558SAl Viro 
31bb898558SAl Viro struct compat_stat {
32bb898558SAl Viro 	compat_dev_t	st_dev;
33bb898558SAl Viro 	u16		__pad1;
34bb898558SAl Viro 	compat_ino_t	st_ino;
35bb898558SAl Viro 	compat_mode_t	st_mode;
36bb898558SAl Viro 	compat_nlink_t	st_nlink;
37bb898558SAl Viro 	__compat_uid_t	st_uid;
38bb898558SAl Viro 	__compat_gid_t	st_gid;
39bb898558SAl Viro 	compat_dev_t	st_rdev;
40bb898558SAl Viro 	u16		__pad2;
41bb898558SAl Viro 	u32		st_size;
42bb898558SAl Viro 	u32		st_blksize;
43bb898558SAl Viro 	u32		st_blocks;
44bb898558SAl Viro 	u32		st_atime;
45bb898558SAl Viro 	u32		st_atime_nsec;
46bb898558SAl Viro 	u32		st_mtime;
47bb898558SAl Viro 	u32		st_mtime_nsec;
48bb898558SAl Viro 	u32		st_ctime;
49bb898558SAl Viro 	u32		st_ctime_nsec;
50bb898558SAl Viro 	u32		__unused4;
51bb898558SAl Viro 	u32		__unused5;
52bb898558SAl Viro };
53bb898558SAl Viro 
54bb898558SAl Viro struct compat_flock {
55bb898558SAl Viro 	short		l_type;
56bb898558SAl Viro 	short		l_whence;
57bb898558SAl Viro 	compat_off_t	l_start;
58bb898558SAl Viro 	compat_off_t	l_len;
59bb898558SAl Viro 	compat_pid_t	l_pid;
60bb898558SAl Viro };
61bb898558SAl Viro 
62bb898558SAl Viro #define F_GETLK64	12	/*  using 'struct flock64' */
63bb898558SAl Viro #define F_SETLK64	13
64bb898558SAl Viro #define F_SETLKW64	14
65bb898558SAl Viro 
66bb898558SAl Viro /*
67bb898558SAl Viro  * IA32 uses 4 byte alignment for 64 bit quantities,
68bb898558SAl Viro  * so we need to pack this structure.
69bb898558SAl Viro  */
70bb898558SAl Viro struct compat_flock64 {
71bb898558SAl Viro 	short		l_type;
72bb898558SAl Viro 	short		l_whence;
73bb898558SAl Viro 	compat_loff_t	l_start;
74bb898558SAl Viro 	compat_loff_t	l_len;
75bb898558SAl Viro 	compat_pid_t	l_pid;
76bb898558SAl Viro } __attribute__((packed));
77bb898558SAl Viro 
78bb898558SAl Viro struct compat_statfs {
79bb898558SAl Viro 	int		f_type;
80bb898558SAl Viro 	int		f_bsize;
81bb898558SAl Viro 	int		f_blocks;
82bb898558SAl Viro 	int		f_bfree;
83bb898558SAl Viro 	int		f_bavail;
84bb898558SAl Viro 	int		f_files;
85bb898558SAl Viro 	int		f_ffree;
86bb898558SAl Viro 	compat_fsid_t	f_fsid;
87bb898558SAl Viro 	int		f_namelen;	/* SunOS ignores this field. */
88bb898558SAl Viro 	int		f_frsize;
891448c721SEric W. Biederman 	int		f_flags;
901448c721SEric W. Biederman 	int		f_spare[4];
91bb898558SAl Viro };
92bb898558SAl Viro 
93bb898558SAl Viro #define COMPAT_RLIM_INFINITY		0xffffffff
94bb898558SAl Viro 
95bb898558SAl Viro typedef u32		compat_old_sigset_t;	/* at least 32 bits */
96bb898558SAl Viro 
97bb898558SAl Viro #define _COMPAT_NSIG		64
98bb898558SAl Viro #define _COMPAT_NSIG_BPW	32
99bb898558SAl Viro 
100bb898558SAl Viro typedef u32               compat_sigset_word;
101bb898558SAl Viro 
102bb898558SAl Viro #define COMPAT_OFF_T_MAX	0x7fffffff
103bb898558SAl Viro 
104bb898558SAl Viro struct compat_ipc64_perm {
105bb898558SAl Viro 	compat_key_t key;
106bb898558SAl Viro 	__compat_uid32_t uid;
107bb898558SAl Viro 	__compat_gid32_t gid;
108bb898558SAl Viro 	__compat_uid32_t cuid;
109bb898558SAl Viro 	__compat_gid32_t cgid;
110bb898558SAl Viro 	unsigned short mode;
111bb898558SAl Viro 	unsigned short __pad1;
112bb898558SAl Viro 	unsigned short seq;
113bb898558SAl Viro 	unsigned short __pad2;
114bb898558SAl Viro 	compat_ulong_t unused1;
115bb898558SAl Viro 	compat_ulong_t unused2;
116bb898558SAl Viro };
117bb898558SAl Viro 
118bb898558SAl Viro struct compat_semid64_ds {
119bb898558SAl Viro 	struct compat_ipc64_perm sem_perm;
120c039dbd5SArnd Bergmann 	compat_ulong_t sem_otime;
121c039dbd5SArnd Bergmann 	compat_ulong_t sem_otime_high;
122c039dbd5SArnd Bergmann 	compat_ulong_t sem_ctime;
123c039dbd5SArnd Bergmann 	compat_ulong_t sem_ctime_high;
124bb898558SAl Viro 	compat_ulong_t sem_nsems;
125bb898558SAl Viro 	compat_ulong_t __unused3;
126bb898558SAl Viro 	compat_ulong_t __unused4;
127bb898558SAl Viro };
128bb898558SAl Viro 
129bb898558SAl Viro struct compat_msqid64_ds {
130bb898558SAl Viro 	struct compat_ipc64_perm msg_perm;
131c039dbd5SArnd Bergmann 	compat_ulong_t msg_stime;
132c039dbd5SArnd Bergmann 	compat_ulong_t msg_stime_high;
133c039dbd5SArnd Bergmann 	compat_ulong_t msg_rtime;
134c039dbd5SArnd Bergmann 	compat_ulong_t msg_rtime_high;
135c039dbd5SArnd Bergmann 	compat_ulong_t msg_ctime;
136c039dbd5SArnd Bergmann 	compat_ulong_t msg_ctime_high;
137bb898558SAl Viro 	compat_ulong_t msg_cbytes;
138bb898558SAl Viro 	compat_ulong_t msg_qnum;
139bb898558SAl Viro 	compat_ulong_t msg_qbytes;
140bb898558SAl Viro 	compat_pid_t   msg_lspid;
141bb898558SAl Viro 	compat_pid_t   msg_lrpid;
142bb898558SAl Viro 	compat_ulong_t __unused4;
143bb898558SAl Viro 	compat_ulong_t __unused5;
144bb898558SAl Viro };
145bb898558SAl Viro 
146bb898558SAl Viro struct compat_shmid64_ds {
147bb898558SAl Viro 	struct compat_ipc64_perm shm_perm;
148bb898558SAl Viro 	compat_size_t  shm_segsz;
149c039dbd5SArnd Bergmann 	compat_ulong_t shm_atime;
150c039dbd5SArnd Bergmann 	compat_ulong_t shm_atime_high;
151c039dbd5SArnd Bergmann 	compat_ulong_t shm_dtime;
152c039dbd5SArnd Bergmann 	compat_ulong_t shm_dtime_high;
153c039dbd5SArnd Bergmann 	compat_ulong_t shm_ctime;
154c039dbd5SArnd Bergmann 	compat_ulong_t shm_ctime_high;
155bb898558SAl Viro 	compat_pid_t   shm_cpid;
156bb898558SAl Viro 	compat_pid_t   shm_lpid;
157bb898558SAl Viro 	compat_ulong_t shm_nattch;
158bb898558SAl Viro 	compat_ulong_t __unused4;
159bb898558SAl Viro 	compat_ulong_t __unused5;
160bb898558SAl Viro };
161bb898558SAl Viro 
162bb898558SAl Viro /*
163bb898558SAl Viro  * The type of struct elf_prstatus.pr_reg in compatible core dumps.
164bb898558SAl Viro  */
165d1a797f3SH. Peter Anvin typedef struct user_regs_struct compat_elf_gregset_t;
166d1a797f3SH. Peter Anvin 
16790954e7bSDmitry Safonov /* Full regset -- prstatus on x32, otherwise on ia32 */
16890954e7bSDmitry Safonov #define PRSTATUS_SIZE(S, R) (R != sizeof(S.pr_reg) ? 144 : 296)
16990954e7bSDmitry Safonov #define SET_PR_FPVALID(S, V, R) \
17090954e7bSDmitry Safonov   do { *(int *) (((void *) &((S)->pr_reg)) + R) = (V); } \
171d1a797f3SH. Peter Anvin   while (0)
172d1a797f3SH. Peter Anvin 
1737b2dd368SDmitry Safonov #ifdef CONFIG_X86_X32_ABI
174d1a797f3SH. Peter Anvin #define COMPAT_USE_64BIT_TIME \
175d1a797f3SH. Peter Anvin 	(!!(task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT))
176d1a797f3SH. Peter Anvin #endif
177bb898558SAl Viro 
178c41d68a5SH. Peter Anvin static inline void __user *arch_compat_alloc_user_space(long len)
179bb898558SAl Viro {
180d1a797f3SH. Peter Anvin 	compat_uptr_t sp;
181d1a797f3SH. Peter Anvin 
182d1a797f3SH. Peter Anvin 	if (test_thread_flag(TIF_IA32)) {
183d1a797f3SH. Peter Anvin 		sp = task_pt_regs(current)->sp;
184d1a797f3SH. Peter Anvin 	} else {
185d1a797f3SH. Peter Anvin 		/* -128 for the x32 ABI redzone */
186263042e4SDenys Vlasenko 		sp = task_pt_regs(current)->sp - 128;
187d1a797f3SH. Peter Anvin 	}
188d1a797f3SH. Peter Anvin 
189d1a797f3SH. Peter Anvin 	return (void __user *)round_down(sp - len, 16);
190bb898558SAl Viro }
191bb898558SAl Viro 
192abfb9498SDmitry Safonov static inline bool in_x32_syscall(void)
193a628b684SH. Peter Anvin {
194fca460f9SH. Peter Anvin #ifdef CONFIG_X86_X32_ABI
195fca460f9SH. Peter Anvin 	if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
196fca460f9SH. Peter Anvin 		return true;
197fca460f9SH. Peter Anvin #endif
198fca460f9SH. Peter Anvin 	return false;
199bb898558SAl Viro }
200bb898558SAl Viro 
201a846446bSDmitry Safonov static inline bool in_32bit_syscall(void)
202a628b684SH. Peter Anvin {
203abfb9498SDmitry Safonov 	return in_ia32_syscall() || in_x32_syscall();
204a628b684SH. Peter Anvin }
205a846446bSDmitry Safonov 
206a846446bSDmitry Safonov #ifdef CONFIG_COMPAT
207a846446bSDmitry Safonov static inline bool in_compat_syscall(void)
208a846446bSDmitry Safonov {
209a846446bSDmitry Safonov 	return in_32bit_syscall();
210a846446bSDmitry Safonov }
211f970165bSAndy Lutomirski #define in_compat_syscall in_compat_syscall	/* override the generic impl */
212527c4125SChristoph Hellwig #define compat_need_64bit_alignment_fixup in_ia32_syscall
213a846446bSDmitry Safonov #endif
214a628b684SH. Peter Anvin 
215ea64d5acSEric W. Biederman struct compat_siginfo;
216c3b3f524SChristoph Hellwig 
217c3b3f524SChristoph Hellwig #ifdef CONFIG_X86_X32_ABI
218c3b3f524SChristoph Hellwig int copy_siginfo_to_user32(struct compat_siginfo __user *to,
219c3b3f524SChristoph Hellwig 		const kernel_siginfo_t *from);
220c3b3f524SChristoph Hellwig #define copy_siginfo_to_user32 copy_siginfo_to_user32
221c3b3f524SChristoph Hellwig #endif /* CONFIG_X86_X32_ABI */
222ea64d5acSEric W. Biederman 
2231965aae3SH. Peter Anvin #endif /* _ASM_X86_COMPAT_H */
224