blob: 2cbd75dd2fd3542b9cb993fb6fd4f436e605c062 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_COMPAT_H
3#define _ASM_X86_COMPAT_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5/*
6 * Architecture specific compatibility types
7 */
8#include <linux/types.h>
9#include <linux/sched.h>
Andy Lutomirskid375cf12017-11-02 00:59:16 -070010#include <linux/sched/task_stack.h>
H. Peter Anvind1a797f2012-02-19 10:06:34 -080011#include <asm/processor.h>
Roland McGrath95d1b8f2007-10-19 20:35:02 +020012#include <asm/user32.h>
H. Peter Anvinfca460f2012-02-19 07:56:26 -080013#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Christoph Hellwige28cbf22010-03-10 15:21:19 -080015#define COMPAT_USER_HZ 100
16#define COMPAT_UTS_MACHINE "i686\0\0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18typedef u32 compat_size_t;
19typedef s32 compat_ssize_t;
20typedef s32 compat_time_t;
21typedef s32 compat_clock_t;
22typedef s32 compat_pid_t;
Stephen Rothwell202e5972005-09-06 15:16:40 -070023typedef u16 __compat_uid_t;
24typedef u16 __compat_gid_t;
25typedef u32 __compat_uid32_t;
26typedef u32 __compat_gid32_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027typedef u16 compat_mode_t;
28typedef u32 compat_ino_t;
29typedef u16 compat_dev_t;
30typedef s32 compat_off_t;
31typedef s64 compat_loff_t;
32typedef u16 compat_nlink_t;
33typedef u16 compat_ipc_pid_t;
34typedef s32 compat_daddr_t;
35typedef u32 compat_caddr_t;
36typedef __kernel_fsid_t compat_fsid_t;
37typedef s32 compat_timer_t;
38typedef s32 compat_key_t;
39
40typedef s32 compat_int_t;
41typedef s32 compat_long_t;
Arnd Bergmann4b777582007-07-15 23:41:11 -070042typedef s64 __attribute__((aligned(4))) compat_s64;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043typedef u32 compat_uint_t;
44typedef u32 compat_ulong_t;
Dave Hansena4455082016-06-08 10:25:33 -070045typedef u32 compat_u32;
Arnd Bergmann4b777582007-07-15 23:41:11 -070046typedef u64 __attribute__((aligned(4))) compat_u64;
Denys Vlasenko751f4092012-10-04 17:15:31 -070047typedef u32 compat_uptr_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49struct compat_timespec {
50 compat_time_t tv_sec;
51 s32 tv_nsec;
52};
53
54struct compat_timeval {
55 compat_time_t tv_sec;
56 s32 tv_usec;
57};
58
59struct compat_stat {
60 compat_dev_t st_dev;
61 u16 __pad1;
62 compat_ino_t st_ino;
63 compat_mode_t st_mode;
64 compat_nlink_t st_nlink;
Stephen Rothwell202e5972005-09-06 15:16:40 -070065 __compat_uid_t st_uid;
66 __compat_gid_t st_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 compat_dev_t st_rdev;
68 u16 __pad2;
69 u32 st_size;
70 u32 st_blksize;
71 u32 st_blocks;
72 u32 st_atime;
73 u32 st_atime_nsec;
74 u32 st_mtime;
75 u32 st_mtime_nsec;
76 u32 st_ctime;
77 u32 st_ctime_nsec;
78 u32 __unused4;
79 u32 __unused5;
80};
81
82struct compat_flock {
83 short l_type;
84 short l_whence;
85 compat_off_t l_start;
86 compat_off_t l_len;
87 compat_pid_t l_pid;
88};
89
90#define F_GETLK64 12 /* using 'struct flock64' */
91#define F_SETLK64 13
92#define F_SETLKW64 14
93
94/*
95 * IA32 uses 4 byte alignment for 64 bit quantities,
96 * so we need to pack this structure.
97 */
98struct compat_flock64 {
99 short l_type;
100 short l_whence;
101 compat_loff_t l_start;
102 compat_loff_t l_len;
103 compat_pid_t l_pid;
104} __attribute__((packed));
105
106struct compat_statfs {
107 int f_type;
108 int f_bsize;
109 int f_blocks;
110 int f_bfree;
111 int f_bavail;
112 int f_files;
113 int f_ffree;
114 compat_fsid_t f_fsid;
115 int f_namelen; /* SunOS ignores this field. */
116 int f_frsize;
Eric W. Biederman1448c722011-10-17 13:40:02 -0700117 int f_flags;
118 int f_spare[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define COMPAT_RLIM_INFINITY 0xffffffff
122
123typedef u32 compat_old_sigset_t; /* at least 32 bits */
124
125#define _COMPAT_NSIG 64
126#define _COMPAT_NSIG_BPW 32
127
128typedef u32 compat_sigset_word;
129
Denys Vlasenko751f4092012-10-04 17:15:31 -0700130typedef union compat_sigval {
131 compat_int_t sival_int;
132 compat_uptr_t sival_ptr;
133} compat_sigval_t;
134
135typedef struct compat_siginfo {
136 int si_signo;
137 int si_errno;
138 int si_code;
139
140 union {
141 int _pad[128/sizeof(int) - 3];
142
143 /* kill() */
144 struct {
145 unsigned int _pid; /* sender's pid */
146 unsigned int _uid; /* sender's uid */
147 } _kill;
148
149 /* POSIX.1b timers */
150 struct {
151 compat_timer_t _tid; /* timer id */
152 int _overrun; /* overrun count */
153 compat_sigval_t _sigval; /* same as below */
154 int _sys_private; /* not to be passed to user */
155 int _overrun_incr; /* amount to add to overrun */
156 } _timer;
157
158 /* POSIX.1b signals */
159 struct {
160 unsigned int _pid; /* sender's pid */
161 unsigned int _uid; /* sender's uid */
162 compat_sigval_t _sigval;
163 } _rt;
164
165 /* SIGCHLD */
166 struct {
167 unsigned int _pid; /* which child */
168 unsigned int _uid; /* sender's uid */
169 int _status; /* exit code */
170 compat_clock_t _utime;
171 compat_clock_t _stime;
172 } _sigchld;
173
174 /* SIGCHLD (x32 version) */
175 struct {
176 unsigned int _pid; /* which child */
177 unsigned int _uid; /* sender's uid */
178 int _status; /* exit code */
179 compat_s64 _utime;
180 compat_s64 _stime;
181 } _sigchld_x32;
182
183 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
184 struct {
185 unsigned int _addr; /* faulting insn/memory ref. */
Dave Hansena4455082016-06-08 10:25:33 -0700186 short int _addr_lsb; /* Valid LSB of the reported address. */
187 union {
188 /* used when si_code=SEGV_BNDERR */
189 struct {
190 compat_uptr_t _lower;
191 compat_uptr_t _upper;
192 } _addr_bnd;
193 /* used when si_code=SEGV_PKUERR */
194 compat_u32 _pkey;
195 };
Denys Vlasenko751f4092012-10-04 17:15:31 -0700196 } _sigfault;
197
198 /* SIGPOLL */
199 struct {
200 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
201 int _fd;
202 } _sigpoll;
203
204 struct {
205 unsigned int _call_addr; /* calling insn */
206 int _syscall; /* triggering system call number */
207 unsigned int _arch; /* AUDIT_ARCH_* of syscall */
208 } _sigsys;
209 } _sifields;
210} compat_siginfo_t;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define COMPAT_OFF_T_MAX 0x7fffffff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214struct compat_ipc64_perm {
215 compat_key_t key;
Stephen Rothwell202e5972005-09-06 15:16:40 -0700216 __compat_uid32_t uid;
217 __compat_gid32_t gid;
218 __compat_uid32_t cuid;
219 __compat_gid32_t cgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned short mode;
221 unsigned short __pad1;
222 unsigned short seq;
223 unsigned short __pad2;
224 compat_ulong_t unused1;
225 compat_ulong_t unused2;
226};
227
228struct compat_semid64_ds {
229 struct compat_ipc64_perm sem_perm;
230 compat_time_t sem_otime;
231 compat_ulong_t __unused1;
232 compat_time_t sem_ctime;
233 compat_ulong_t __unused2;
234 compat_ulong_t sem_nsems;
235 compat_ulong_t __unused3;
236 compat_ulong_t __unused4;
237};
238
239struct compat_msqid64_ds {
240 struct compat_ipc64_perm msg_perm;
241 compat_time_t msg_stime;
242 compat_ulong_t __unused1;
243 compat_time_t msg_rtime;
244 compat_ulong_t __unused2;
245 compat_time_t msg_ctime;
246 compat_ulong_t __unused3;
247 compat_ulong_t msg_cbytes;
248 compat_ulong_t msg_qnum;
249 compat_ulong_t msg_qbytes;
250 compat_pid_t msg_lspid;
251 compat_pid_t msg_lrpid;
252 compat_ulong_t __unused4;
253 compat_ulong_t __unused5;
254};
255
256struct compat_shmid64_ds {
257 struct compat_ipc64_perm shm_perm;
258 compat_size_t shm_segsz;
259 compat_time_t shm_atime;
260 compat_ulong_t __unused1;
261 compat_time_t shm_dtime;
262 compat_ulong_t __unused2;
263 compat_time_t shm_ctime;
264 compat_ulong_t __unused3;
265 compat_pid_t shm_cpid;
266 compat_pid_t shm_lpid;
267 compat_ulong_t shm_nattch;
268 compat_ulong_t __unused4;
269 compat_ulong_t __unused5;
270};
271
272/*
Roland McGrath95d1b8f2007-10-19 20:35:02 +0200273 * The type of struct elf_prstatus.pr_reg in compatible core dumps.
274 */
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800275typedef struct user_regs_struct compat_elf_gregset_t;
276
Dmitry Safonov90954e72016-09-05 16:33:06 +0300277/* Full regset -- prstatus on x32, otherwise on ia32 */
278#define PRSTATUS_SIZE(S, R) (R != sizeof(S.pr_reg) ? 144 : 296)
279#define SET_PR_FPVALID(S, V, R) \
280 do { *(int *) (((void *) &((S)->pr_reg)) + R) = (V); } \
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800281 while (0)
282
Dmitry Safonov7b2dd362016-11-23 21:13:30 +0300283#ifdef CONFIG_X86_X32_ABI
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800284#define COMPAT_USE_64BIT_TIME \
285 (!!(task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT))
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800286#endif
Roland McGrath95d1b8f2007-10-19 20:35:02 +0200287
288/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * A pointer passed in from user mode. This should not
290 * be used for syscall parameters, just declare them
291 * as pointers because the syscall entry code will have
Marcin Ĺšlusarz8b3de0d2008-02-03 16:32:51 +0200292 * appropriately converted them already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295static inline void __user *compat_ptr(compat_uptr_t uptr)
296{
297 return (void __user *)(unsigned long)uptr;
298}
299
300static inline compat_uptr_t ptr_to_compat(void __user *uptr)
301{
302 return (u32)(unsigned long)uptr;
303}
304
H. Peter Anvinc41d68a2010-09-07 16:16:18 -0700305static inline void __user *arch_compat_alloc_user_space(long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800307 compat_uptr_t sp;
308
309 if (test_thread_flag(TIF_IA32)) {
310 sp = task_pt_regs(current)->sp;
311 } else {
312 /* -128 for the x32 ABI redzone */
Denys Vlasenko263042e2015-03-09 19:39:23 +0100313 sp = task_pt_regs(current)->sp - 128;
H. Peter Anvind1a797f2012-02-19 10:06:34 -0800314 }
315
316 return (void __user *)round_down(sp - len, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Dmitry Safonovabfb9492016-04-18 16:43:43 +0300319static inline bool in_x32_syscall(void)
H. Peter Anvina628b682012-03-05 13:39:29 -0800320{
H. Peter Anvinfca460f2012-02-19 07:56:26 -0800321#ifdef CONFIG_X86_X32_ABI
322 if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
323 return true;
324#endif
325 return false;
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100326}
327
Andy Lutomirskif9701652016-03-22 14:25:27 -0700328static inline bool in_compat_syscall(void)
H. Peter Anvina628b682012-03-05 13:39:29 -0800329{
Dmitry Safonovabfb9492016-04-18 16:43:43 +0300330 return in_ia32_syscall() || in_x32_syscall();
H. Peter Anvina628b682012-03-05 13:39:29 -0800331}
Andy Lutomirskif9701652016-03-22 14:25:27 -0700332#define in_compat_syscall in_compat_syscall /* override the generic impl */
H. Peter Anvina628b682012-03-05 13:39:29 -0800333
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700334#endif /* _ASM_X86_COMPAT_H */