1 /* 2 * sys_parisc32.c: Conversion between 32bit and 64bit native syscalls. 3 * 4 * Copyright (C) 2000-2001 Hewlett Packard Company 5 * Copyright (C) 2000 John Marvin 6 * Copyright (C) 2001 Matthew Wilcox 7 * 8 * These routines maintain argument size conversion between 32bit and 64bit 9 * environment. Based heavily on sys_ia32.c and sys_sparc32.c. 10 */ 11 12 #include <linux/compat.h> 13 #include <linux/kernel.h> 14 #include <linux/sched.h> 15 #include <linux/fs.h> 16 #include <linux/mm.h> 17 #include <linux/file.h> 18 #include <linux/signal.h> 19 #include <linux/resource.h> 20 #include <linux/times.h> 21 #include <linux/utsname.h> 22 #include <linux/time.h> 23 #include <linux/smp.h> 24 #include <linux/smp_lock.h> 25 #include <linux/sem.h> 26 #include <linux/msg.h> 27 #include <linux/shm.h> 28 #include <linux/slab.h> 29 #include <linux/uio.h> 30 #include <linux/nfs_fs.h> 31 #include <linux/ncp_fs.h> 32 #include <linux/sunrpc/svc.h> 33 #include <linux/nfsd/nfsd.h> 34 #include <linux/nfsd/cache.h> 35 #include <linux/nfsd/xdr.h> 36 #include <linux/nfsd/syscall.h> 37 #include <linux/poll.h> 38 #include <linux/personality.h> 39 #include <linux/stat.h> 40 #include <linux/highmem.h> 41 #include <linux/highuid.h> 42 #include <linux/mman.h> 43 #include <linux/binfmts.h> 44 #include <linux/namei.h> 45 #include <linux/vfs.h> 46 #include <linux/ptrace.h> 47 #include <linux/swap.h> 48 #include <linux/syscalls.h> 49 50 #include <asm/types.h> 51 #include <asm/uaccess.h> 52 #include <asm/mmu_context.h> 53 54 #include "sys32.h" 55 56 #undef DEBUG 57 58 #ifdef DEBUG 59 #define DBG(x) printk x 60 #else 61 #define DBG(x) 62 #endif 63 64 /* 65 * sys32_execve() executes a new program. 66 */ 67 68 asmlinkage int sys32_execve(struct pt_regs *regs) 69 { 70 int error; 71 char *filename; 72 73 DBG(("sys32_execve(%p) r26 = 0x%lx\n", regs, regs->gr[26])); 74 filename = getname((const char __user *) regs->gr[26]); 75 error = PTR_ERR(filename); 76 if (IS_ERR(filename)) 77 goto out; 78 error = compat_do_execve(filename, compat_ptr(regs->gr[25]), 79 compat_ptr(regs->gr[24]), regs); 80 putname(filename); 81 out: 82 83 return error; 84 } 85 86 asmlinkage long sys32_unimplemented(int r26, int r25, int r24, int r23, 87 int r22, int r21, int r20) 88 { 89 printk(KERN_ERR "%s(%d): Unimplemented 32 on 64 syscall #%d!\n", 90 current->comm, current->pid, r20); 91 return -ENOSYS; 92 } 93 94 #ifdef CONFIG_SYSCTL 95 96 struct __sysctl_args32 { 97 u32 name; 98 int nlen; 99 u32 oldval; 100 u32 oldlenp; 101 u32 newval; 102 u32 newlen; 103 u32 __unused[4]; 104 }; 105 106 asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args) 107 { 108 #ifndef CONFIG_SYSCTL_SYSCALL 109 return -ENOSYS; 110 #else 111 struct __sysctl_args32 tmp; 112 int error; 113 unsigned int oldlen32; 114 size_t oldlen, __user *oldlenp = NULL; 115 unsigned long addr = (((long __force)&args->__unused[0]) + 7) & ~7; 116 117 DBG(("sysctl32(%p)\n", args)); 118 119 if (copy_from_user(&tmp, args, sizeof(tmp))) 120 return -EFAULT; 121 122 if (tmp.oldval && tmp.oldlenp) { 123 /* Duh, this is ugly and might not work if sysctl_args 124 is in read-only memory, but do_sysctl does indirectly 125 a lot of uaccess in both directions and we'd have to 126 basically copy the whole sysctl.c here, and 127 glibc's __sysctl uses rw memory for the structure 128 anyway. */ 129 /* a possibly better hack than this, which will avoid the 130 * problem if the struct is read only, is to push the 131 * 'oldlen' value out to the user's stack instead. -PB 132 */ 133 if (get_user(oldlen32, (u32 *)(u64)tmp.oldlenp)) 134 return -EFAULT; 135 oldlen = oldlen32; 136 if (put_user(oldlen, (size_t *)addr)) 137 return -EFAULT; 138 oldlenp = (size_t *)addr; 139 } 140 141 lock_kernel(); 142 error = do_sysctl((int __user *)(u64)tmp.name, tmp.nlen, 143 (void __user *)(u64)tmp.oldval, oldlenp, 144 (void __user *)(u64)tmp.newval, tmp.newlen); 145 unlock_kernel(); 146 if (oldlenp) { 147 if (!error) { 148 if (get_user(oldlen, (size_t *)addr)) { 149 error = -EFAULT; 150 } else { 151 oldlen32 = oldlen; 152 if (put_user(oldlen32, (u32 *)(u64)tmp.oldlenp)) 153 error = -EFAULT; 154 } 155 } 156 if (copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused))) 157 error = -EFAULT; 158 } 159 return error; 160 #endif 161 } 162 163 #endif /* CONFIG_SYSCTL */ 164 165 asmlinkage long sys32_sched_rr_get_interval(pid_t pid, 166 struct compat_timespec __user *interval) 167 { 168 struct timespec t; 169 int ret; 170 171 KERNEL_SYSCALL(ret, sys_sched_rr_get_interval, pid, (struct timespec __user *)&t); 172 if (put_compat_timespec(&t, interval)) 173 return -EFAULT; 174 return ret; 175 } 176 177 /*** copied from mips64 ***/ 178 /* 179 * Ooo, nasty. We need here to frob 32-bit unsigned longs to 180 * 64-bit unsigned longs. 181 */ 182 183 static inline int 184 get_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset) 185 { 186 n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32)); 187 if (ufdset) { 188 unsigned long odd; 189 190 if (!access_ok(VERIFY_WRITE, ufdset, n*sizeof(u32))) 191 return -EFAULT; 192 193 odd = n & 1UL; 194 n &= ~1UL; 195 while (n) { 196 unsigned long h, l; 197 __get_user(l, ufdset); 198 __get_user(h, ufdset+1); 199 ufdset += 2; 200 *fdset++ = h << 32 | l; 201 n -= 2; 202 } 203 if (odd) 204 __get_user(*fdset, ufdset); 205 } else { 206 /* Tricky, must clear full unsigned long in the 207 * kernel fdset at the end, this makes sure that 208 * actually happens. 209 */ 210 memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32)); 211 } 212 return 0; 213 } 214 215 static inline void 216 set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset) 217 { 218 unsigned long odd; 219 n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32)); 220 221 if (!ufdset) 222 return; 223 224 odd = n & 1UL; 225 n &= ~1UL; 226 while (n) { 227 unsigned long h, l; 228 l = *fdset++; 229 h = l >> 32; 230 __put_user(l, ufdset); 231 __put_user(h, ufdset+1); 232 ufdset += 2; 233 n -= 2; 234 } 235 if (odd) 236 __put_user(*fdset, ufdset); 237 } 238 239 struct msgbuf32 { 240 int mtype; 241 char mtext[1]; 242 }; 243 244 asmlinkage long sys32_msgsnd(int msqid, 245 struct msgbuf32 __user *umsgp32, 246 size_t msgsz, int msgflg) 247 { 248 struct msgbuf *mb; 249 struct msgbuf32 mb32; 250 int err; 251 252 if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL) 253 return -ENOMEM; 254 255 err = get_user(mb32.mtype, &umsgp32->mtype); 256 mb->mtype = mb32.mtype; 257 err |= copy_from_user(mb->mtext, &umsgp32->mtext, msgsz); 258 259 if (err) 260 err = -EFAULT; 261 else 262 KERNEL_SYSCALL(err, sys_msgsnd, msqid, (struct msgbuf __user *)mb, msgsz, msgflg); 263 264 kfree(mb); 265 return err; 266 } 267 268 asmlinkage long sys32_msgrcv(int msqid, 269 struct msgbuf32 __user *umsgp32, 270 size_t msgsz, long msgtyp, int msgflg) 271 { 272 struct msgbuf *mb; 273 struct msgbuf32 mb32; 274 int err, len; 275 276 if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL) 277 return -ENOMEM; 278 279 KERNEL_SYSCALL(err, sys_msgrcv, msqid, (struct msgbuf __user *)mb, msgsz, msgtyp, msgflg); 280 281 if (err >= 0) { 282 len = err; 283 mb32.mtype = mb->mtype; 284 err = put_user(mb32.mtype, &umsgp32->mtype); 285 err |= copy_to_user(&umsgp32->mtext, mb->mtext, len); 286 if (err) 287 err = -EFAULT; 288 else 289 err = len; 290 } 291 292 kfree(mb); 293 return err; 294 } 295 296 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count) 297 { 298 mm_segment_t old_fs = get_fs(); 299 int ret; 300 off_t of; 301 302 if (offset && get_user(of, offset)) 303 return -EFAULT; 304 305 set_fs(KERNEL_DS); 306 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count); 307 set_fs(old_fs); 308 309 if (offset && put_user(of, offset)) 310 return -EFAULT; 311 312 return ret; 313 } 314 315 asmlinkage int sys32_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count) 316 { 317 mm_segment_t old_fs = get_fs(); 318 int ret; 319 loff_t lof; 320 321 if (offset && get_user(lof, offset)) 322 return -EFAULT; 323 324 set_fs(KERNEL_DS); 325 ret = sys_sendfile64(out_fd, in_fd, offset ? (loff_t __user *)&lof : NULL, count); 326 set_fs(old_fs); 327 328 if (offset && put_user(lof, offset)) 329 return -EFAULT; 330 331 return ret; 332 } 333 334 335 /* lseek() needs a wrapper because 'offset' can be negative, but the top 336 * half of the argument has been zeroed by syscall.S. 337 */ 338 339 asmlinkage int sys32_lseek(unsigned int fd, int offset, unsigned int origin) 340 { 341 return sys_lseek(fd, offset, origin); 342 } 343 344 asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg) 345 { 346 union semun u; 347 348 if (cmd == SETVAL) { 349 /* Ugh. arg is a union of int,ptr,ptr,ptr, so is 8 bytes. 350 * The int should be in the first 4, but our argument 351 * frobbing has left it in the last 4. 352 */ 353 u.val = *((int *)&arg + 1); 354 return sys_semctl (semid, semnum, cmd, u); 355 } 356 return sys_semctl (semid, semnum, cmd, arg); 357 } 358 359 long sys32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf, 360 size_t len) 361 { 362 return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low, 363 buf, len); 364 } 365 366 asmlinkage long compat_sys_fallocate(int fd, int mode, u32 offhi, u32 offlo, 367 u32 lenhi, u32 lenlo) 368 { 369 return sys_fallocate(fd, mode, ((loff_t)offhi << 32) | offlo, 370 ((loff_t)lenhi << 32) | lenlo); 371 } 372