linux32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Conversion between 32-bit and 64-bit native system calls.
  3. *
  4. * Copyright (C) 2000 Silicon Graphics, Inc.
  5. * Written by Ulf Carlsson (ulfc@engr.sgi.com)
  6. * sys32_execve from ia64/ia32 code, Feb 2000, Kanoj Sarcar (kanoj@sgi.com)
  7. */
  8. #include <linux/compiler.h>
  9. #include <linux/mm.h>
  10. #include <linux/errno.h>
  11. #include <linux/file.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/highuid.h>
  14. #include <linux/resource.h>
  15. #include <linux/highmem.h>
  16. #include <linux/time.h>
  17. #include <linux/times.h>
  18. #include <linux/poll.h>
  19. #include <linux/slab.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/filter.h>
  22. #include <linux/shm.h>
  23. #include <linux/sem.h>
  24. #include <linux/msg.h>
  25. #include <linux/icmpv6.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/utime.h>
  29. #include <linux/utsname.h>
  30. #include <linux/personality.h>
  31. #include <linux/dnotify.h>
  32. #include <linux/module.h>
  33. #include <linux/binfmts.h>
  34. #include <linux/security.h>
  35. #include <linux/compat.h>
  36. #include <linux/vfs.h>
  37. #include <linux/ipc.h>
  38. #include <net/sock.h>
  39. #include <net/scm.h>
  40. #include <asm/compat-signal.h>
  41. #include <asm/sim.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/mman.h>
  45. /* Use this to get at 32-bit user passed pointers. */
  46. /* A() macro should be used for places where you e.g.
  47. have some internal variable u32 and just want to get
  48. rid of a compiler warning. AA() has to be used in
  49. places where you want to convert a function argument
  50. to 32bit pointer or when you e.g. access pt_regs
  51. structure and want to consider 32bit registers only.
  52. */
  53. #define A(__x) ((unsigned long)(__x))
  54. #define AA(__x) ((unsigned long)((int)__x))
  55. #ifdef __MIPSEB__
  56. #define merge_64(r1, r2) ((((r1) & 0xffffffffUL) << 32) + ((r2) & 0xffffffffUL))
  57. #endif
  58. #ifdef __MIPSEL__
  59. #define merge_64(r1, r2) ((((r2) & 0xffffffffUL) << 32) + ((r1) & 0xffffffffUL))
  60. #endif
  61. /*
  62. * Revalidate the inode. This is required for proper NFS attribute caching.
  63. */
  64. int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
  65. {
  66. struct compat_stat tmp;
  67. if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
  68. return -EOVERFLOW;
  69. memset(&tmp, 0, sizeof(tmp));
  70. tmp.st_dev = new_encode_dev(stat->dev);
  71. tmp.st_ino = stat->ino;
  72. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  73. return -EOVERFLOW;
  74. tmp.st_mode = stat->mode;
  75. tmp.st_nlink = stat->nlink;
  76. SET_UID(tmp.st_uid, stat->uid);
  77. SET_GID(tmp.st_gid, stat->gid);
  78. tmp.st_rdev = new_encode_dev(stat->rdev);
  79. tmp.st_size = stat->size;
  80. tmp.st_atime = stat->atime.tv_sec;
  81. tmp.st_mtime = stat->mtime.tv_sec;
  82. tmp.st_ctime = stat->ctime.tv_sec;
  83. #ifdef STAT_HAVE_NSEC
  84. tmp.st_atime_nsec = stat->atime.tv_nsec;
  85. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  86. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  87. #endif
  88. tmp.st_blocks = stat->blocks;
  89. tmp.st_blksize = stat->blksize;
  90. return copy_to_user(statbuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  91. }
  92. asmlinkage unsigned long
  93. sys32_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  94. unsigned long flags, unsigned long fd, unsigned long pgoff)
  95. {
  96. struct file * file = NULL;
  97. unsigned long error;
  98. error = -EINVAL;
  99. if (pgoff & (~PAGE_MASK >> 12))
  100. goto out;
  101. pgoff >>= PAGE_SHIFT-12;
  102. if (!(flags & MAP_ANONYMOUS)) {
  103. error = -EBADF;
  104. file = fget(fd);
  105. if (!file)
  106. goto out;
  107. }
  108. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  109. down_write(&current->mm->mmap_sem);
  110. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  111. up_write(&current->mm->mmap_sem);
  112. if (file)
  113. fput(file);
  114. out:
  115. return error;
  116. }
  117. /*
  118. * sys_execve() executes a new program.
  119. */
  120. asmlinkage int sys32_execve(nabi_no_regargs struct pt_regs regs)
  121. {
  122. int error;
  123. char * filename;
  124. filename = getname(compat_ptr(regs.regs[4]));
  125. error = PTR_ERR(filename);
  126. if (IS_ERR(filename))
  127. goto out;
  128. error = compat_do_execve(filename, compat_ptr(regs.regs[5]),
  129. compat_ptr(regs.regs[6]), &regs);
  130. putname(filename);
  131. out:
  132. return error;
  133. }
  134. #define RLIM_INFINITY32 0x7fffffff
  135. #define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)
  136. struct rlimit32 {
  137. int rlim_cur;
  138. int rlim_max;
  139. };
  140. asmlinkage long sys32_truncate64(const char __user * path,
  141. unsigned long __dummy, int a2, int a3)
  142. {
  143. return sys_truncate(path, merge_64(a2, a3));
  144. }
  145. asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long __dummy,
  146. int a2, int a3)
  147. {
  148. return sys_ftruncate(fd, merge_64(a2, a3));
  149. }
  150. static inline long
  151. get_tv32(struct timeval *o, struct compat_timeval __user *i)
  152. {
  153. return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
  154. (__get_user(o->tv_sec, &i->tv_sec) |
  155. __get_user(o->tv_usec, &i->tv_usec)));
  156. }
  157. static inline long
  158. put_tv32(struct compat_timeval __user *o, struct timeval *i)
  159. {
  160. return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
  161. (__put_user(i->tv_sec, &o->tv_sec) |
  162. __put_user(i->tv_usec, &o->tv_usec)));
  163. }
  164. extern struct timezone sys_tz;
  165. asmlinkage int
  166. sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  167. {
  168. if (tv) {
  169. struct timeval ktv;
  170. do_gettimeofday(&ktv);
  171. if (put_tv32(tv, &ktv))
  172. return -EFAULT;
  173. }
  174. if (tz) {
  175. if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  176. return -EFAULT;
  177. }
  178. return 0;
  179. }
  180. static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
  181. {
  182. long usec;
  183. if (!access_ok(VERIFY_READ, i, sizeof(*i)))
  184. return -EFAULT;
  185. if (__get_user(o->tv_sec, &i->tv_sec))
  186. return -EFAULT;
  187. if (__get_user(usec, &i->tv_usec))
  188. return -EFAULT;
  189. o->tv_nsec = usec * 1000;
  190. return 0;
  191. }
  192. asmlinkage int
  193. sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  194. {
  195. struct timespec kts;
  196. struct timezone ktz;
  197. if (tv) {
  198. if (get_ts32(&kts, tv))
  199. return -EFAULT;
  200. }
  201. if (tz) {
  202. if (copy_from_user(&ktz, tz, sizeof(ktz)))
  203. return -EFAULT;
  204. }
  205. return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
  206. }
  207. asmlinkage int sys32_llseek(unsigned int fd, unsigned int offset_high,
  208. unsigned int offset_low, loff_t __user * result,
  209. unsigned int origin)
  210. {
  211. return sys_llseek(fd, offset_high, offset_low, result, origin);
  212. }
  213. /* From the Single Unix Spec: pread & pwrite act like lseek to pos + op +
  214. lseek back to original location. They fail just like lseek does on
  215. non-seekable files. */
  216. asmlinkage ssize_t sys32_pread(unsigned int fd, char __user * buf,
  217. size_t count, u32 unused, u64 a4, u64 a5)
  218. {
  219. return sys_pread64(fd, buf, count, merge_64(a4, a5));
  220. }
  221. asmlinkage ssize_t sys32_pwrite(unsigned int fd, const char __user * buf,
  222. size_t count, u32 unused, u64 a4, u64 a5)
  223. {
  224. return sys_pwrite64(fd, buf, count, merge_64(a4, a5));
  225. }
  226. asmlinkage int sys32_sched_rr_get_interval(compat_pid_t pid,
  227. struct compat_timespec __user *interval)
  228. {
  229. struct timespec t;
  230. int ret;
  231. mm_segment_t old_fs = get_fs();
  232. set_fs(KERNEL_DS);
  233. ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
  234. set_fs(old_fs);
  235. if (put_user (t.tv_sec, &interval->tv_sec) ||
  236. __put_user(t.tv_nsec, &interval->tv_nsec))
  237. return -EFAULT;
  238. return ret;
  239. }
  240. #ifdef CONFIG_SYSVIPC
  241. asmlinkage long
  242. sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
  243. {
  244. int version, err;
  245. version = call >> 16; /* hack for backward compatibility */
  246. call &= 0xffff;
  247. switch (call) {
  248. case SEMOP:
  249. /* struct sembuf is the same on 32 and 64bit :)) */
  250. err = sys_semtimedop(first, compat_ptr(ptr), second, NULL);
  251. break;
  252. case SEMTIMEDOP:
  253. err = compat_sys_semtimedop(first, compat_ptr(ptr), second,
  254. compat_ptr(fifth));
  255. break;
  256. case SEMGET:
  257. err = sys_semget(first, second, third);
  258. break;
  259. case SEMCTL:
  260. err = compat_sys_semctl(first, second, third, compat_ptr(ptr));
  261. break;
  262. case MSGSND:
  263. err = compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
  264. break;
  265. case MSGRCV:
  266. err = compat_sys_msgrcv(first, second, fifth, third,
  267. version, compat_ptr(ptr));
  268. break;
  269. case MSGGET:
  270. err = sys_msgget((key_t) first, second);
  271. break;
  272. case MSGCTL:
  273. err = compat_sys_msgctl(first, second, compat_ptr(ptr));
  274. break;
  275. case SHMAT:
  276. err = compat_sys_shmat(first, second, third, version,
  277. compat_ptr(ptr));
  278. break;
  279. case SHMDT:
  280. err = sys_shmdt(compat_ptr(ptr));
  281. break;
  282. case SHMGET:
  283. err = sys_shmget(first, (unsigned)second, third);
  284. break;
  285. case SHMCTL:
  286. err = compat_sys_shmctl(first, second, compat_ptr(ptr));
  287. break;
  288. default:
  289. err = -EINVAL;
  290. break;
  291. }
  292. return err;
  293. }
  294. #else
  295. asmlinkage long
  296. sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
  297. {
  298. return -ENOSYS;
  299. }
  300. #endif /* CONFIG_SYSVIPC */
  301. #ifdef CONFIG_MIPS32_N32
  302. asmlinkage long sysn32_semctl(int semid, int semnum, int cmd, u32 arg)
  303. {
  304. /* compat_sys_semctl expects a pointer to union semun */
  305. u32 __user *uptr = compat_alloc_user_space(sizeof(u32));
  306. if (put_user(arg, uptr))
  307. return -EFAULT;
  308. return compat_sys_semctl(semid, semnum, cmd, uptr);
  309. }
  310. asmlinkage long sysn32_msgsnd(int msqid, u32 msgp, unsigned msgsz, int msgflg)
  311. {
  312. return compat_sys_msgsnd(msqid, msgsz, msgflg, compat_ptr(msgp));
  313. }
  314. asmlinkage long sysn32_msgrcv(int msqid, u32 msgp, size_t msgsz, int msgtyp,
  315. int msgflg)
  316. {
  317. return compat_sys_msgrcv(msqid, msgsz, msgtyp, msgflg, IPC_64,
  318. compat_ptr(msgp));
  319. }
  320. #endif
  321. struct sysctl_args32
  322. {
  323. compat_caddr_t name;
  324. int nlen;
  325. compat_caddr_t oldval;
  326. compat_caddr_t oldlenp;
  327. compat_caddr_t newval;
  328. compat_size_t newlen;
  329. unsigned int __unused[4];
  330. };
  331. #ifdef CONFIG_SYSCTL_SYSCALL
  332. asmlinkage long sys32_sysctl(struct sysctl_args32 __user *args)
  333. {
  334. struct sysctl_args32 tmp;
  335. int error;
  336. size_t oldlen;
  337. size_t __user *oldlenp = NULL;
  338. unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
  339. if (copy_from_user(&tmp, args, sizeof(tmp)))
  340. return -EFAULT;
  341. if (tmp.oldval && tmp.oldlenp) {
  342. /* Duh, this is ugly and might not work if sysctl_args
  343. is in read-only memory, but do_sysctl does indirectly
  344. a lot of uaccess in both directions and we'd have to
  345. basically copy the whole sysctl.c here, and
  346. glibc's __sysctl uses rw memory for the structure
  347. anyway. */
  348. if (get_user(oldlen, (u32 __user *)A(tmp.oldlenp)) ||
  349. put_user(oldlen, (size_t __user *)addr))
  350. return -EFAULT;
  351. oldlenp = (size_t __user *)addr;
  352. }
  353. lock_kernel();
  354. error = do_sysctl((int __user *)A(tmp.name), tmp.nlen, (void __user *)A(tmp.oldval),
  355. oldlenp, (void __user *)A(tmp.newval), tmp.newlen);
  356. unlock_kernel();
  357. if (oldlenp) {
  358. if (!error) {
  359. if (get_user(oldlen, (size_t __user *)addr) ||
  360. put_user(oldlen, (u32 __user *)A(tmp.oldlenp)))
  361. error = -EFAULT;
  362. }
  363. copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
  364. }
  365. return error;
  366. }
  367. #endif /* CONFIG_SYSCTL_SYSCALL */
  368. asmlinkage long sys32_newuname(struct new_utsname __user * name)
  369. {
  370. int ret = 0;
  371. down_read(&uts_sem);
  372. if (copy_to_user(name, utsname(), sizeof *name))
  373. ret = -EFAULT;
  374. up_read(&uts_sem);
  375. if (current->personality == PER_LINUX32 && !ret)
  376. if (copy_to_user(name->machine, "mips\0\0\0", 8))
  377. ret = -EFAULT;
  378. return ret;
  379. }
  380. asmlinkage int sys32_personality(unsigned long personality)
  381. {
  382. int ret;
  383. personality &= 0xffffffff;
  384. if (personality(current->personality) == PER_LINUX32 &&
  385. personality == PER_LINUX)
  386. personality = PER_LINUX32;
  387. ret = sys_personality(personality);
  388. if (ret == PER_LINUX32)
  389. ret = PER_LINUX;
  390. return ret;
  391. }
  392. /* ustat compatibility */
  393. struct ustat32 {
  394. compat_daddr_t f_tfree;
  395. compat_ino_t f_tinode;
  396. char f_fname[6];
  397. char f_fpack[6];
  398. };
  399. extern asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf);
  400. asmlinkage int sys32_ustat(dev_t dev, struct ustat32 __user * ubuf32)
  401. {
  402. int err;
  403. struct ustat tmp;
  404. struct ustat32 tmp32;
  405. mm_segment_t old_fs = get_fs();
  406. set_fs(KERNEL_DS);
  407. err = sys_ustat(dev, (struct ustat __user *)&tmp);
  408. set_fs(old_fs);
  409. if (err)
  410. goto out;
  411. memset(&tmp32, 0, sizeof(struct ustat32));
  412. tmp32.f_tfree = tmp.f_tfree;
  413. tmp32.f_tinode = tmp.f_tinode;
  414. err = copy_to_user(ubuf32, &tmp32, sizeof(struct ustat32)) ? -EFAULT : 0;
  415. out:
  416. return err;
  417. }
  418. asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset,
  419. s32 count)
  420. {
  421. mm_segment_t old_fs = get_fs();
  422. int ret;
  423. off_t of;
  424. if (offset && get_user(of, offset))
  425. return -EFAULT;
  426. set_fs(KERNEL_DS);
  427. ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
  428. set_fs(old_fs);
  429. if (offset && put_user(of, offset))
  430. return -EFAULT;
  431. return ret;
  432. }
  433. asmlinkage ssize_t sys32_readahead(int fd, u32 pad0, u64 a2, u64 a3,
  434. size_t count)
  435. {
  436. return sys_readahead(fd, merge_64(a2, a3), count);
  437. }
  438. asmlinkage long sys32_sync_file_range(int fd, int __pad,
  439. unsigned long a2, unsigned long a3,
  440. unsigned long a4, unsigned long a5,
  441. int flags)
  442. {
  443. return sys_sync_file_range(fd,
  444. merge_64(a2, a3), merge_64(a4, a5),
  445. flags);
  446. }
  447. asmlinkage long sys32_fadvise64_64(int fd, int __pad,
  448. unsigned long a2, unsigned long a3,
  449. unsigned long a4, unsigned long a5,
  450. int flags)
  451. {
  452. return sys_fadvise64_64(fd,
  453. merge_64(a2, a3), merge_64(a4, a5),
  454. flags);
  455. }
  456. asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_a2,
  457. unsigned offset_a3, unsigned len_a4, unsigned len_a5)
  458. {
  459. return sys_fallocate(fd, mode, merge_64(offset_a2, offset_a3),
  460. merge_64(len_a4, len_a5));
  461. }
  462. save_static_function(sys32_clone);
  463. static int noinline __used
  464. _sys32_clone(nabi_no_regargs struct pt_regs regs)
  465. {
  466. unsigned long clone_flags;
  467. unsigned long newsp;
  468. int __user *parent_tidptr, *child_tidptr;
  469. clone_flags = regs.regs[4];
  470. newsp = regs.regs[5];
  471. if (!newsp)
  472. newsp = regs.regs[29];
  473. parent_tidptr = (int __user *) regs.regs[6];
  474. /* Use __dummy4 instead of getting it off the stack, so that
  475. syscall() works. */
  476. child_tidptr = (int __user *) __dummy4;
  477. return do_fork(clone_flags, newsp, &regs, 0,
  478. parent_tidptr, child_tidptr);
  479. }