sys_parisc32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. #include <linux/compat.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/fs.h>
  15. #include <linux/mm.h>
  16. #include <linux/file.h>
  17. #include <linux/signal.h>
  18. #include <linux/resource.h>
  19. #include <linux/times.h>
  20. #include <linux/utsname.h>
  21. #include <linux/time.h>
  22. #include <linux/smp.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/sem.h>
  25. #include <linux/msg.h>
  26. #include <linux/shm.h>
  27. #include <linux/slab.h>
  28. #include <linux/uio.h>
  29. #include <linux/nfs_fs.h>
  30. #include <linux/ncp_fs.h>
  31. #include <linux/sunrpc/svc.h>
  32. #include <linux/nfsd/nfsd.h>
  33. #include <linux/nfsd/cache.h>
  34. #include <linux/nfsd/xdr.h>
  35. #include <linux/nfsd/syscall.h>
  36. #include <linux/poll.h>
  37. #include <linux/personality.h>
  38. #include <linux/stat.h>
  39. #include <linux/highmem.h>
  40. #include <linux/highuid.h>
  41. #include <linux/mman.h>
  42. #include <linux/binfmts.h>
  43. #include <linux/namei.h>
  44. #include <linux/vfs.h>
  45. #include <linux/ptrace.h>
  46. #include <linux/swap.h>
  47. #include <linux/syscalls.h>
  48. #include <asm/types.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/semaphore.h>
  51. #include <asm/mmu_context.h>
  52. #include "sys32.h"
  53. #undef DEBUG
  54. #ifdef DEBUG
  55. #define DBG(x) printk x
  56. #else
  57. #define DBG(x)
  58. #endif
  59. /*
  60. * sys32_execve() executes a new program.
  61. */
  62. asmlinkage int sys32_execve(struct pt_regs *regs)
  63. {
  64. int error;
  65. char *filename;
  66. DBG(("sys32_execve(%p) r26 = 0x%lx\n", regs, regs->gr[26]));
  67. filename = getname((const char __user *) regs->gr[26]);
  68. error = PTR_ERR(filename);
  69. if (IS_ERR(filename))
  70. goto out;
  71. error = compat_do_execve(filename, compat_ptr(regs->gr[25]),
  72. compat_ptr(regs->gr[24]), regs);
  73. if (error == 0) {
  74. task_lock(current);
  75. current->ptrace &= ~PT_DTRACE;
  76. task_unlock(current);
  77. }
  78. putname(filename);
  79. out:
  80. return error;
  81. }
  82. asmlinkage long sys32_unimplemented(int r26, int r25, int r24, int r23,
  83. int r22, int r21, int r20)
  84. {
  85. printk(KERN_ERR "%s(%d): Unimplemented 32 on 64 syscall #%d!\n",
  86. current->comm, current->pid, r20);
  87. return -ENOSYS;
  88. }
  89. #ifdef CONFIG_SYSCTL
  90. struct __sysctl_args32 {
  91. u32 name;
  92. int nlen;
  93. u32 oldval;
  94. u32 oldlenp;
  95. u32 newval;
  96. u32 newlen;
  97. u32 __unused[4];
  98. };
  99. asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
  100. {
  101. #ifndef CONFIG_SYSCTL_SYSCALL
  102. return -ENOSYS;
  103. #else
  104. struct __sysctl_args32 tmp;
  105. int error;
  106. unsigned int oldlen32;
  107. size_t oldlen, __user *oldlenp = NULL;
  108. unsigned long addr = (((long __force)&args->__unused[0]) + 7) & ~7;
  109. DBG(("sysctl32(%p)\n", args));
  110. if (copy_from_user(&tmp, args, sizeof(tmp)))
  111. return -EFAULT;
  112. if (tmp.oldval && tmp.oldlenp) {
  113. /* Duh, this is ugly and might not work if sysctl_args
  114. is in read-only memory, but do_sysctl does indirectly
  115. a lot of uaccess in both directions and we'd have to
  116. basically copy the whole sysctl.c here, and
  117. glibc's __sysctl uses rw memory for the structure
  118. anyway. */
  119. /* a possibly better hack than this, which will avoid the
  120. * problem if the struct is read only, is to push the
  121. * 'oldlen' value out to the user's stack instead. -PB
  122. */
  123. if (get_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
  124. return -EFAULT;
  125. oldlen = oldlen32;
  126. if (put_user(oldlen, (size_t *)addr))
  127. return -EFAULT;
  128. oldlenp = (size_t *)addr;
  129. }
  130. lock_kernel();
  131. error = do_sysctl((int __user *)(u64)tmp.name, tmp.nlen,
  132. (void __user *)(u64)tmp.oldval, oldlenp,
  133. (void __user *)(u64)tmp.newval, tmp.newlen);
  134. unlock_kernel();
  135. if (oldlenp) {
  136. if (!error) {
  137. if (get_user(oldlen, (size_t *)addr)) {
  138. error = -EFAULT;
  139. } else {
  140. oldlen32 = oldlen;
  141. if (put_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
  142. error = -EFAULT;
  143. }
  144. }
  145. if (copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused)))
  146. error = -EFAULT;
  147. }
  148. return error;
  149. #endif
  150. }
  151. #endif /* CONFIG_SYSCTL */
  152. asmlinkage long sys32_sched_rr_get_interval(pid_t pid,
  153. struct compat_timespec __user *interval)
  154. {
  155. struct timespec t;
  156. int ret;
  157. KERNEL_SYSCALL(ret, sys_sched_rr_get_interval, pid, (struct timespec __user *)&t);
  158. if (put_compat_timespec(&t, interval))
  159. return -EFAULT;
  160. return ret;
  161. }
  162. static int
  163. put_compat_timeval(struct compat_timeval __user *u, struct timeval *t)
  164. {
  165. struct compat_timeval t32;
  166. t32.tv_sec = t->tv_sec;
  167. t32.tv_usec = t->tv_usec;
  168. return copy_to_user(u, &t32, sizeof t32);
  169. }
  170. static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
  171. {
  172. long usec;
  173. if (__get_user(o->tv_sec, &i->tv_sec))
  174. return -EFAULT;
  175. if (__get_user(usec, &i->tv_usec))
  176. return -EFAULT;
  177. o->tv_nsec = usec * 1000;
  178. return 0;
  179. }
  180. asmlinkage int
  181. sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  182. {
  183. extern void do_gettimeofday(struct timeval *tv);
  184. if (tv) {
  185. struct timeval ktv;
  186. do_gettimeofday(&ktv);
  187. if (put_compat_timeval(tv, &ktv))
  188. return -EFAULT;
  189. }
  190. if (tz) {
  191. extern struct timezone sys_tz;
  192. if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  193. return -EFAULT;
  194. }
  195. return 0;
  196. }
  197. asmlinkage
  198. int sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  199. {
  200. struct timespec kts;
  201. struct timezone ktz;
  202. if (tv) {
  203. if (get_ts32(&kts, tv))
  204. return -EFAULT;
  205. }
  206. if (tz) {
  207. if (copy_from_user(&ktz, tz, sizeof(ktz)))
  208. return -EFAULT;
  209. }
  210. return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
  211. }
  212. int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
  213. {
  214. compat_ino_t ino;
  215. int err;
  216. if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
  217. !new_valid_dev(stat->rdev))
  218. return -EOVERFLOW;
  219. ino = stat->ino;
  220. if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
  221. return -EOVERFLOW;
  222. err = put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
  223. err |= put_user(ino, &statbuf->st_ino);
  224. err |= put_user(stat->mode, &statbuf->st_mode);
  225. err |= put_user(stat->nlink, &statbuf->st_nlink);
  226. err |= put_user(0, &statbuf->st_reserved1);
  227. err |= put_user(0, &statbuf->st_reserved2);
  228. err |= put_user(new_encode_dev(stat->rdev), &statbuf->st_rdev);
  229. err |= put_user(stat->size, &statbuf->st_size);
  230. err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
  231. err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
  232. err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
  233. err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
  234. err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
  235. err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
  236. err |= put_user(stat->blksize, &statbuf->st_blksize);
  237. err |= put_user(stat->blocks, &statbuf->st_blocks);
  238. err |= put_user(0, &statbuf->__unused1);
  239. err |= put_user(0, &statbuf->__unused2);
  240. err |= put_user(0, &statbuf->__unused3);
  241. err |= put_user(0, &statbuf->__unused4);
  242. err |= put_user(0, &statbuf->__unused5);
  243. err |= put_user(0, &statbuf->st_fstype); /* not avail */
  244. err |= put_user(0, &statbuf->st_realdev); /* not avail */
  245. err |= put_user(0, &statbuf->st_basemode); /* not avail */
  246. err |= put_user(0, &statbuf->st_spareshort);
  247. err |= put_user(stat->uid, &statbuf->st_uid);
  248. err |= put_user(stat->gid, &statbuf->st_gid);
  249. err |= put_user(0, &statbuf->st_spare4[0]);
  250. err |= put_user(0, &statbuf->st_spare4[1]);
  251. err |= put_user(0, &statbuf->st_spare4[2]);
  252. return err;
  253. }
  254. struct linux32_dirent {
  255. u32 d_ino;
  256. compat_off_t d_off;
  257. u16 d_reclen;
  258. char d_name[1];
  259. };
  260. struct old_linux32_dirent {
  261. u32 d_ino;
  262. u32 d_offset;
  263. u16 d_namlen;
  264. char d_name[1];
  265. };
  266. struct getdents32_callback {
  267. struct linux32_dirent __user * current_dir;
  268. struct linux32_dirent __user * previous;
  269. int count;
  270. int error;
  271. };
  272. struct readdir32_callback {
  273. struct old_linux32_dirent __user * dirent;
  274. int count;
  275. };
  276. #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
  277. static int filldir32 (void *__buf, const char *name, int namlen,
  278. loff_t offset, u64 ino, unsigned int d_type)
  279. {
  280. struct linux32_dirent __user * dirent;
  281. struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
  282. int reclen = ALIGN(NAME_OFFSET(dirent) + namlen + 1, 4);
  283. u32 d_ino;
  284. buf->error = -EINVAL; /* only used if we fail.. */
  285. if (reclen > buf->count)
  286. return -EINVAL;
  287. d_ino = ino;
  288. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
  289. return -EOVERFLOW;
  290. dirent = buf->previous;
  291. if (dirent)
  292. put_user(offset, &dirent->d_off);
  293. dirent = buf->current_dir;
  294. buf->previous = dirent;
  295. put_user(d_ino, &dirent->d_ino);
  296. put_user(reclen, &dirent->d_reclen);
  297. copy_to_user(dirent->d_name, name, namlen);
  298. put_user(0, dirent->d_name + namlen);
  299. dirent = ((void __user *)dirent) + reclen;
  300. buf->current_dir = dirent;
  301. buf->count -= reclen;
  302. return 0;
  303. }
  304. asmlinkage long
  305. sys32_getdents (unsigned int fd, void __user * dirent, unsigned int count)
  306. {
  307. struct file * file;
  308. struct linux32_dirent __user * lastdirent;
  309. struct getdents32_callback buf;
  310. int error;
  311. error = -EFAULT;
  312. if (!access_ok(VERIFY_WRITE, dirent, count))
  313. goto out;
  314. error = -EBADF;
  315. file = fget(fd);
  316. if (!file)
  317. goto out;
  318. buf.current_dir = (struct linux32_dirent __user *) dirent;
  319. buf.previous = NULL;
  320. buf.count = count;
  321. buf.error = 0;
  322. error = vfs_readdir(file, filldir32, &buf);
  323. if (error < 0)
  324. goto out_putf;
  325. error = buf.error;
  326. lastdirent = buf.previous;
  327. if (lastdirent) {
  328. if (put_user(file->f_pos, &lastdirent->d_off))
  329. error = -EFAULT;
  330. else
  331. error = count - buf.count;
  332. }
  333. out_putf:
  334. fput(file);
  335. out:
  336. return error;
  337. }
  338. static int fillonedir32(void * __buf, const char * name, int namlen,
  339. loff_t offset, u64 ino, unsigned int d_type)
  340. {
  341. struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
  342. struct old_linux32_dirent __user * dirent;
  343. u32 d_ino;
  344. if (buf->count)
  345. return -EINVAL;
  346. d_ino = ino;
  347. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
  348. return -EOVERFLOW;
  349. buf->count++;
  350. dirent = buf->dirent;
  351. put_user(d_ino, &dirent->d_ino);
  352. put_user(offset, &dirent->d_offset);
  353. put_user(namlen, &dirent->d_namlen);
  354. copy_to_user(dirent->d_name, name, namlen);
  355. put_user(0, dirent->d_name + namlen);
  356. return 0;
  357. }
  358. asmlinkage long
  359. sys32_readdir (unsigned int fd, void __user * dirent, unsigned int count)
  360. {
  361. int error;
  362. struct file * file;
  363. struct readdir32_callback buf;
  364. error = -EBADF;
  365. file = fget(fd);
  366. if (!file)
  367. goto out;
  368. buf.count = 0;
  369. buf.dirent = dirent;
  370. error = vfs_readdir(file, fillonedir32, &buf);
  371. if (error >= 0)
  372. error = buf.count;
  373. fput(file);
  374. out:
  375. return error;
  376. }
  377. /*** copied from mips64 ***/
  378. /*
  379. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  380. * 64-bit unsigned longs.
  381. */
  382. static inline int
  383. get_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
  384. {
  385. n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
  386. if (ufdset) {
  387. unsigned long odd;
  388. if (!access_ok(VERIFY_WRITE, ufdset, n*sizeof(u32)))
  389. return -EFAULT;
  390. odd = n & 1UL;
  391. n &= ~1UL;
  392. while (n) {
  393. unsigned long h, l;
  394. __get_user(l, ufdset);
  395. __get_user(h, ufdset+1);
  396. ufdset += 2;
  397. *fdset++ = h << 32 | l;
  398. n -= 2;
  399. }
  400. if (odd)
  401. __get_user(*fdset, ufdset);
  402. } else {
  403. /* Tricky, must clear full unsigned long in the
  404. * kernel fdset at the end, this makes sure that
  405. * actually happens.
  406. */
  407. memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32));
  408. }
  409. return 0;
  410. }
  411. static inline void
  412. set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
  413. {
  414. unsigned long odd;
  415. n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
  416. if (!ufdset)
  417. return;
  418. odd = n & 1UL;
  419. n &= ~1UL;
  420. while (n) {
  421. unsigned long h, l;
  422. l = *fdset++;
  423. h = l >> 32;
  424. __put_user(l, ufdset);
  425. __put_user(h, ufdset+1);
  426. ufdset += 2;
  427. n -= 2;
  428. }
  429. if (odd)
  430. __put_user(*fdset, ufdset);
  431. }
  432. struct msgbuf32 {
  433. int mtype;
  434. char mtext[1];
  435. };
  436. asmlinkage long sys32_msgsnd(int msqid,
  437. struct msgbuf32 __user *umsgp32,
  438. size_t msgsz, int msgflg)
  439. {
  440. struct msgbuf *mb;
  441. struct msgbuf32 mb32;
  442. int err;
  443. if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
  444. return -ENOMEM;
  445. err = get_user(mb32.mtype, &umsgp32->mtype);
  446. mb->mtype = mb32.mtype;
  447. err |= copy_from_user(mb->mtext, &umsgp32->mtext, msgsz);
  448. if (err)
  449. err = -EFAULT;
  450. else
  451. KERNEL_SYSCALL(err, sys_msgsnd, msqid, (struct msgbuf __user *)mb, msgsz, msgflg);
  452. kfree(mb);
  453. return err;
  454. }
  455. asmlinkage long sys32_msgrcv(int msqid,
  456. struct msgbuf32 __user *umsgp32,
  457. size_t msgsz, long msgtyp, int msgflg)
  458. {
  459. struct msgbuf *mb;
  460. struct msgbuf32 mb32;
  461. int err, len;
  462. if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
  463. return -ENOMEM;
  464. KERNEL_SYSCALL(err, sys_msgrcv, msqid, (struct msgbuf __user *)mb, msgsz, msgtyp, msgflg);
  465. if (err >= 0) {
  466. len = err;
  467. mb32.mtype = mb->mtype;
  468. err = put_user(mb32.mtype, &umsgp32->mtype);
  469. err |= copy_to_user(&umsgp32->mtext, mb->mtext, len);
  470. if (err)
  471. err = -EFAULT;
  472. else
  473. err = len;
  474. }
  475. kfree(mb);
  476. return err;
  477. }
  478. asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
  479. {
  480. mm_segment_t old_fs = get_fs();
  481. int ret;
  482. off_t of;
  483. if (offset && get_user(of, offset))
  484. return -EFAULT;
  485. set_fs(KERNEL_DS);
  486. ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
  487. set_fs(old_fs);
  488. if (offset && put_user(of, offset))
  489. return -EFAULT;
  490. return ret;
  491. }
  492. asmlinkage int sys32_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count)
  493. {
  494. mm_segment_t old_fs = get_fs();
  495. int ret;
  496. loff_t lof;
  497. if (offset && get_user(lof, offset))
  498. return -EFAULT;
  499. set_fs(KERNEL_DS);
  500. ret = sys_sendfile64(out_fd, in_fd, offset ? (loff_t __user *)&lof : NULL, count);
  501. set_fs(old_fs);
  502. if (offset && put_user(lof, offset))
  503. return -EFAULT;
  504. return ret;
  505. }
  506. /* lseek() needs a wrapper because 'offset' can be negative, but the top
  507. * half of the argument has been zeroed by syscall.S.
  508. */
  509. asmlinkage int sys32_lseek(unsigned int fd, int offset, unsigned int origin)
  510. {
  511. return sys_lseek(fd, offset, origin);
  512. }
  513. asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg)
  514. {
  515. union semun u;
  516. if (cmd == SETVAL) {
  517. /* Ugh. arg is a union of int,ptr,ptr,ptr, so is 8 bytes.
  518. * The int should be in the first 4, but our argument
  519. * frobbing has left it in the last 4.
  520. */
  521. u.val = *((int *)&arg + 1);
  522. return sys_semctl (semid, semnum, cmd, u);
  523. }
  524. return sys_semctl (semid, semnum, cmd, arg);
  525. }
  526. long sys32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf,
  527. size_t len)
  528. {
  529. return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
  530. buf, len);
  531. }