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_time_t; 21 typedef s32 compat_clock_t; 22 typedef s32 compat_pid_t; 23 typedef u16 __compat_uid_t; 24 typedef u16 __compat_gid_t; 25 typedef u32 __compat_uid32_t; 26 typedef u32 __compat_gid32_t; 27 typedef u16 compat_mode_t; 28 typedef u32 compat_ino_t; 29 typedef u16 compat_dev_t; 30 typedef s32 compat_off_t; 31 typedef s64 compat_loff_t; 32 typedef u16 compat_nlink_t; 33 typedef u16 compat_ipc_pid_t; 34 typedef s32 compat_daddr_t; 35 typedef u32 compat_caddr_t; 36 typedef __kernel_fsid_t compat_fsid_t; 37 typedef s32 compat_timer_t; 38 typedef s32 compat_key_t; 39 40 typedef s32 compat_int_t; 41 typedef s32 compat_long_t; 42 typedef s64 __attribute__((aligned(4))) compat_s64; 43 typedef u32 compat_uint_t; 44 typedef u32 compat_ulong_t; 45 typedef u32 compat_u32; 46 typedef u64 __attribute__((aligned(4))) compat_u64; 47 typedef u32 compat_uptr_t; 48 49 struct compat_timespec { 50 compat_time_t tv_sec; 51 s32 tv_nsec; 52 }; 53 54 struct compat_timeval { 55 compat_time_t tv_sec; 56 s32 tv_usec; 57 }; 58 59 struct 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; 65 __compat_uid_t st_uid; 66 __compat_gid_t st_gid; 67 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 82 struct 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 */ 98 struct 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 106 struct 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; 117 int f_flags; 118 int f_spare[4]; 119 }; 120 121 #define COMPAT_RLIM_INFINITY 0xffffffff 122 123 typedef u32 compat_old_sigset_t; /* at least 32 bits */ 124 125 #define _COMPAT_NSIG 64 126 #define _COMPAT_NSIG_BPW 32 127 128 typedef u32 compat_sigset_word; 129 130 typedef union compat_sigval { 131 compat_int_t sival_int; 132 compat_uptr_t sival_ptr; 133 } compat_sigval_t; 134 135 typedef 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 } _timer; 155 156 /* POSIX.1b signals */ 157 struct { 158 unsigned int _pid; /* sender's pid */ 159 unsigned int _uid; /* sender's uid */ 160 compat_sigval_t _sigval; 161 } _rt; 162 163 /* SIGCHLD */ 164 struct { 165 unsigned int _pid; /* which child */ 166 unsigned int _uid; /* sender's uid */ 167 int _status; /* exit code */ 168 compat_clock_t _utime; 169 compat_clock_t _stime; 170 } _sigchld; 171 172 /* SIGCHLD (x32 version) */ 173 struct { 174 unsigned int _pid; /* which child */ 175 unsigned int _uid; /* sender's uid */ 176 int _status; /* exit code */ 177 compat_s64 _utime; 178 compat_s64 _stime; 179 } _sigchld_x32; 180 181 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */ 182 struct { 183 unsigned int _addr; /* faulting insn/memory ref. */ 184 short int _addr_lsb; /* Valid LSB of the reported address. */ 185 union { 186 /* used when si_code=SEGV_BNDERR */ 187 struct { 188 compat_uptr_t _lower; 189 compat_uptr_t _upper; 190 } _addr_bnd; 191 /* used when si_code=SEGV_PKUERR */ 192 compat_u32 _pkey; 193 }; 194 } _sigfault; 195 196 /* SIGPOLL */ 197 struct { 198 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */ 199 int _fd; 200 } _sigpoll; 201 202 struct { 203 unsigned int _call_addr; /* calling insn */ 204 int _syscall; /* triggering system call number */ 205 unsigned int _arch; /* AUDIT_ARCH_* of syscall */ 206 } _sigsys; 207 } _sifields; 208 } compat_siginfo_t; 209 210 #define COMPAT_OFF_T_MAX 0x7fffffff 211 212 struct compat_ipc64_perm { 213 compat_key_t key; 214 __compat_uid32_t uid; 215 __compat_gid32_t gid; 216 __compat_uid32_t cuid; 217 __compat_gid32_t cgid; 218 unsigned short mode; 219 unsigned short __pad1; 220 unsigned short seq; 221 unsigned short __pad2; 222 compat_ulong_t unused1; 223 compat_ulong_t unused2; 224 }; 225 226 struct compat_semid64_ds { 227 struct compat_ipc64_perm sem_perm; 228 compat_time_t sem_otime; 229 compat_ulong_t __unused1; 230 compat_time_t sem_ctime; 231 compat_ulong_t __unused2; 232 compat_ulong_t sem_nsems; 233 compat_ulong_t __unused3; 234 compat_ulong_t __unused4; 235 }; 236 237 struct compat_msqid64_ds { 238 struct compat_ipc64_perm msg_perm; 239 compat_time_t msg_stime; 240 compat_ulong_t __unused1; 241 compat_time_t msg_rtime; 242 compat_ulong_t __unused2; 243 compat_time_t msg_ctime; 244 compat_ulong_t __unused3; 245 compat_ulong_t msg_cbytes; 246 compat_ulong_t msg_qnum; 247 compat_ulong_t msg_qbytes; 248 compat_pid_t msg_lspid; 249 compat_pid_t msg_lrpid; 250 compat_ulong_t __unused4; 251 compat_ulong_t __unused5; 252 }; 253 254 struct compat_shmid64_ds { 255 struct compat_ipc64_perm shm_perm; 256 compat_size_t shm_segsz; 257 compat_time_t shm_atime; 258 compat_ulong_t __unused1; 259 compat_time_t shm_dtime; 260 compat_ulong_t __unused2; 261 compat_time_t shm_ctime; 262 compat_ulong_t __unused3; 263 compat_pid_t shm_cpid; 264 compat_pid_t shm_lpid; 265 compat_ulong_t shm_nattch; 266 compat_ulong_t __unused4; 267 compat_ulong_t __unused5; 268 }; 269 270 /* 271 * The type of struct elf_prstatus.pr_reg in compatible core dumps. 272 */ 273 typedef struct user_regs_struct compat_elf_gregset_t; 274 275 /* Full regset -- prstatus on x32, otherwise on ia32 */ 276 #define PRSTATUS_SIZE(S, R) (R != sizeof(S.pr_reg) ? 144 : 296) 277 #define SET_PR_FPVALID(S, V, R) \ 278 do { *(int *) (((void *) &((S)->pr_reg)) + R) = (V); } \ 279 while (0) 280 281 #ifdef CONFIG_X86_X32_ABI 282 #define COMPAT_USE_64BIT_TIME \ 283 (!!(task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)) 284 #endif 285 286 /* 287 * A pointer passed in from user mode. This should not 288 * be used for syscall parameters, just declare them 289 * as pointers because the syscall entry code will have 290 * appropriately converted them already. 291 */ 292 293 static inline void __user *compat_ptr(compat_uptr_t uptr) 294 { 295 return (void __user *)(unsigned long)uptr; 296 } 297 298 static inline compat_uptr_t ptr_to_compat(void __user *uptr) 299 { 300 return (u32)(unsigned long)uptr; 301 } 302 303 static inline void __user *arch_compat_alloc_user_space(long len) 304 { 305 compat_uptr_t sp; 306 307 if (test_thread_flag(TIF_IA32)) { 308 sp = task_pt_regs(current)->sp; 309 } else { 310 /* -128 for the x32 ABI redzone */ 311 sp = task_pt_regs(current)->sp - 128; 312 } 313 314 return (void __user *)round_down(sp - len, 16); 315 } 316 317 static inline bool in_x32_syscall(void) 318 { 319 #ifdef CONFIG_X86_X32_ABI 320 if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT) 321 return true; 322 #endif 323 return false; 324 } 325 326 static inline bool in_compat_syscall(void) 327 { 328 return in_ia32_syscall() || in_x32_syscall(); 329 } 330 #define in_compat_syscall in_compat_syscall /* override the generic impl */ 331 332 #endif /* _ASM_X86_COMPAT_H */ 333