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