linux32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/syscalls.h>
  36. #include <linux/compat.h>
  37. #include <linux/vfs.h>
  38. #include <linux/ipc.h>
  39. #include <net/sock.h>
  40. #include <net/scm.h>
  41. #include <asm/compat-signal.h>
  42. #include <asm/sim.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/mmu_context.h>
  45. #include <asm/mman.h>
  46. /* Use this to get at 32-bit user passed pointers. */
  47. /* A() macro should be used for places where you e.g.
  48. have some internal variable u32 and just want to get
  49. rid of a compiler warning. AA() has to be used in
  50. places where you want to convert a function argument
  51. to 32bit pointer or when you e.g. access pt_regs
  52. structure and want to consider 32bit registers only.
  53. */
  54. #define A(__x) ((unsigned long)(__x))
  55. #define AA(__x) ((unsigned long)((int)__x))
  56. #ifdef __MIPSEB__
  57. #define merge_64(r1, r2) ((((r1) & 0xffffffffUL) << 32) + ((r2) & 0xffffffffUL))
  58. #endif
  59. #ifdef __MIPSEL__
  60. #define merge_64(r1, r2) ((((r2) & 0xffffffffUL) << 32) + ((r1) & 0xffffffffUL))
  61. #endif
  62. SYSCALL_DEFINE6(32_mmap2, unsigned long, addr, unsigned long, len,
  63. unsigned long, prot, unsigned long, flags, unsigned long, fd,
  64. unsigned long, pgoff)
  65. {
  66. struct file * file = NULL;
  67. unsigned long error;
  68. error = -EINVAL;
  69. if (pgoff & (~PAGE_MASK >> 12))
  70. goto out;
  71. pgoff >>= PAGE_SHIFT-12;
  72. if (!(flags & MAP_ANONYMOUS)) {
  73. error = -EBADF;
  74. file = fget(fd);
  75. if (!file)
  76. goto out;
  77. }
  78. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  79. down_write(&current->mm->mmap_sem);
  80. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  81. up_write(&current->mm->mmap_sem);
  82. if (file)
  83. fput(file);
  84. out:
  85. return error;
  86. }
  87. /*
  88. * sys_execve() executes a new program.
  89. */
  90. asmlinkage int sys32_execve(nabi_no_regargs struct pt_regs regs)
  91. {
  92. int error;
  93. char * filename;
  94. filename = getname(compat_ptr(regs.regs[4]));
  95. error = PTR_ERR(filename);
  96. if (IS_ERR(filename))
  97. goto out;
  98. error = compat_do_execve(filename, compat_ptr(regs.regs[5]),
  99. compat_ptr(regs.regs[6]), &regs);
  100. putname(filename);
  101. out:
  102. return error;
  103. }
  104. #define RLIM_INFINITY32 0x7fffffff
  105. #define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)
  106. struct rlimit32 {
  107. int rlim_cur;
  108. int rlim_max;
  109. };
  110. SYSCALL_DEFINE4(32_truncate64, const char __user *, path,
  111. unsigned long, __dummy, unsigned long, a2, unsigned long, a3)
  112. {
  113. return sys_truncate(path, merge_64(a2, a3));
  114. }
  115. SYSCALL_DEFINE4(32_ftruncate64, unsigned long, fd, unsigned long, __dummy,
  116. unsigned long, a2, unsigned long, a3)
  117. {
  118. return sys_ftruncate(fd, merge_64(a2, a3));
  119. }
  120. SYSCALL_DEFINE5(32_llseek, unsigned long, fd, unsigned long, offset_high,
  121. unsigned long, offset_low, loff_t __user *, result,
  122. unsigned long, origin)
  123. {
  124. return sys_llseek(fd, offset_high, offset_low, result, origin);
  125. }
  126. /* From the Single Unix Spec: pread & pwrite act like lseek to pos + op +
  127. lseek back to original location. They fail just like lseek does on
  128. non-seekable files. */
  129. SYSCALL_DEFINE6(32_pread, unsigned long, fd, char __user *, buf, size_t, count,
  130. unsigned long, unused, unsigned long, a4, unsigned long, a5)
  131. {
  132. return sys_pread64(fd, buf, count, merge_64(a4, a5));
  133. }
  134. SYSCALL_DEFINE6(32_pwrite, unsigned int, fd, const char __user *, buf,
  135. size_t, count, u32, unused, u64, a4, u64, a5)
  136. {
  137. return sys_pwrite64(fd, buf, count, merge_64(a4, a5));
  138. }
  139. SYSCALL_DEFINE2(32_sched_rr_get_interval, compat_pid_t, pid,
  140. struct compat_timespec __user *, interval)
  141. {
  142. struct timespec t;
  143. int ret;
  144. mm_segment_t old_fs = get_fs();
  145. set_fs(KERNEL_DS);
  146. ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
  147. set_fs(old_fs);
  148. if (put_user (t.tv_sec, &interval->tv_sec) ||
  149. __put_user(t.tv_nsec, &interval->tv_nsec))
  150. return -EFAULT;
  151. return ret;
  152. }
  153. #ifdef CONFIG_SYSVIPC
  154. SYSCALL_DEFINE6(32_ipc, u32, call, long, first, long, second, long, third,
  155. unsigned long, ptr, unsigned long, fifth)
  156. {
  157. int version, err;
  158. version = call >> 16; /* hack for backward compatibility */
  159. call &= 0xffff;
  160. switch (call) {
  161. case SEMOP:
  162. /* struct sembuf is the same on 32 and 64bit :)) */
  163. err = sys_semtimedop(first, compat_ptr(ptr), second, NULL);
  164. break;
  165. case SEMTIMEDOP:
  166. err = compat_sys_semtimedop(first, compat_ptr(ptr), second,
  167. compat_ptr(fifth));
  168. break;
  169. case SEMGET:
  170. err = sys_semget(first, second, third);
  171. break;
  172. case SEMCTL:
  173. err = compat_sys_semctl(first, second, third, compat_ptr(ptr));
  174. break;
  175. case MSGSND:
  176. err = compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
  177. break;
  178. case MSGRCV:
  179. err = compat_sys_msgrcv(first, second, fifth, third,
  180. version, compat_ptr(ptr));
  181. break;
  182. case MSGGET:
  183. err = sys_msgget((key_t) first, second);
  184. break;
  185. case MSGCTL:
  186. err = compat_sys_msgctl(first, second, compat_ptr(ptr));
  187. break;
  188. case SHMAT:
  189. err = compat_sys_shmat(first, second, third, version,
  190. compat_ptr(ptr));
  191. break;
  192. case SHMDT:
  193. err = sys_shmdt(compat_ptr(ptr));
  194. break;
  195. case SHMGET:
  196. err = sys_shmget(first, (unsigned)second, third);
  197. break;
  198. case SHMCTL:
  199. err = compat_sys_shmctl(first, second, compat_ptr(ptr));
  200. break;
  201. default:
  202. err = -EINVAL;
  203. break;
  204. }
  205. return err;
  206. }
  207. #else
  208. SYSCALL_DEFINE6(32_ipc, u32, call, int, first, int, second, int, third,
  209. u32, ptr, u32, fifth)
  210. {
  211. return -ENOSYS;
  212. }
  213. #endif /* CONFIG_SYSVIPC */
  214. #ifdef CONFIG_MIPS32_N32
  215. SYSCALL_DEFINE4(n32_semctl, int, semid, int, semnum, int, cmd, u32, arg)
  216. {
  217. /* compat_sys_semctl expects a pointer to union semun */
  218. u32 __user *uptr = compat_alloc_user_space(sizeof(u32));
  219. if (put_user(arg, uptr))
  220. return -EFAULT;
  221. return compat_sys_semctl(semid, semnum, cmd, uptr);
  222. }
  223. SYSCALL_DEFINE4(n32_msgsnd, int, msqid, u32, msgp, unsigned int, msgsz,
  224. int, msgflg)
  225. {
  226. return compat_sys_msgsnd(msqid, msgsz, msgflg, compat_ptr(msgp));
  227. }
  228. SYSCALL_DEFINE5(n32_msgrcv, int, msqid, u32, msgp, size_t, msgsz,
  229. int, msgtyp, int, msgflg)
  230. {
  231. return compat_sys_msgrcv(msqid, msgsz, msgtyp, msgflg, IPC_64,
  232. compat_ptr(msgp));
  233. }
  234. #endif
  235. struct sysctl_args32
  236. {
  237. compat_caddr_t name;
  238. int nlen;
  239. compat_caddr_t oldval;
  240. compat_caddr_t oldlenp;
  241. compat_caddr_t newval;
  242. compat_size_t newlen;
  243. unsigned int __unused[4];
  244. };
  245. #ifdef CONFIG_SYSCTL_SYSCALL
  246. SYSCALL_DEFINE1(32_sysctl, struct sysctl_args32 __user *, args)
  247. {
  248. struct sysctl_args32 tmp;
  249. int error;
  250. size_t oldlen;
  251. size_t __user *oldlenp = NULL;
  252. unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
  253. if (copy_from_user(&tmp, args, sizeof(tmp)))
  254. return -EFAULT;
  255. if (tmp.oldval && tmp.oldlenp) {
  256. /* Duh, this is ugly and might not work if sysctl_args
  257. is in read-only memory, but do_sysctl does indirectly
  258. a lot of uaccess in both directions and we'd have to
  259. basically copy the whole sysctl.c here, and
  260. glibc's __sysctl uses rw memory for the structure
  261. anyway. */
  262. if (get_user(oldlen, (u32 __user *)A(tmp.oldlenp)) ||
  263. put_user(oldlen, (size_t __user *)addr))
  264. return -EFAULT;
  265. oldlenp = (size_t __user *)addr;
  266. }
  267. lock_kernel();
  268. error = do_sysctl((int __user *)A(tmp.name), tmp.nlen, (void __user *)A(tmp.oldval),
  269. oldlenp, (void __user *)A(tmp.newval), tmp.newlen);
  270. unlock_kernel();
  271. if (oldlenp) {
  272. if (!error) {
  273. if (get_user(oldlen, (size_t __user *)addr) ||
  274. put_user(oldlen, (u32 __user *)A(tmp.oldlenp)))
  275. error = -EFAULT;
  276. }
  277. copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
  278. }
  279. return error;
  280. }
  281. #else
  282. SYSCALL_DEFINE1(32_sysctl, struct sysctl_args32 __user *, args)
  283. {
  284. return -ENOSYS;
  285. }
  286. #endif /* CONFIG_SYSCTL_SYSCALL */
  287. SYSCALL_DEFINE1(32_newuname, struct new_utsname __user *, name)
  288. {
  289. int ret = 0;
  290. down_read(&uts_sem);
  291. if (copy_to_user(name, utsname(), sizeof *name))
  292. ret = -EFAULT;
  293. up_read(&uts_sem);
  294. if (current->personality == PER_LINUX32 && !ret)
  295. if (copy_to_user(name->machine, "mips\0\0\0", 8))
  296. ret = -EFAULT;
  297. return ret;
  298. }
  299. SYSCALL_DEFINE1(32_personality, unsigned long, personality)
  300. {
  301. int ret;
  302. personality &= 0xffffffff;
  303. if (personality(current->personality) == PER_LINUX32 &&
  304. personality == PER_LINUX)
  305. personality = PER_LINUX32;
  306. ret = sys_personality(personality);
  307. if (ret == PER_LINUX32)
  308. ret = PER_LINUX;
  309. return ret;
  310. }
  311. /* ustat compatibility */
  312. struct ustat32 {
  313. compat_daddr_t f_tfree;
  314. compat_ino_t f_tinode;
  315. char f_fname[6];
  316. char f_fpack[6];
  317. };
  318. extern asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf);
  319. SYSCALL_DEFINE2(32_ustat, dev_t, dev, struct ustat32 __user *, ubuf32)
  320. {
  321. int err;
  322. struct ustat tmp;
  323. struct ustat32 tmp32;
  324. mm_segment_t old_fs = get_fs();
  325. set_fs(KERNEL_DS);
  326. err = sys_ustat(dev, (struct ustat __user *)&tmp);
  327. set_fs(old_fs);
  328. if (err)
  329. goto out;
  330. memset(&tmp32, 0, sizeof(struct ustat32));
  331. tmp32.f_tfree = tmp.f_tfree;
  332. tmp32.f_tinode = tmp.f_tinode;
  333. err = copy_to_user(ubuf32, &tmp32, sizeof(struct ustat32)) ? -EFAULT : 0;
  334. out:
  335. return err;
  336. }
  337. SYSCALL_DEFINE4(32_sendfile, long, out_fd, long, in_fd,
  338. compat_off_t __user *, offset, s32, count)
  339. {
  340. mm_segment_t old_fs = get_fs();
  341. int ret;
  342. off_t of;
  343. if (offset && get_user(of, offset))
  344. return -EFAULT;
  345. set_fs(KERNEL_DS);
  346. ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
  347. set_fs(old_fs);
  348. if (offset && put_user(of, offset))
  349. return -EFAULT;
  350. return ret;
  351. }
  352. asmlinkage ssize_t sys32_readahead(int fd, u32 pad0, u64 a2, u64 a3,
  353. size_t count)
  354. {
  355. return sys_readahead(fd, merge_64(a2, a3), count);
  356. }
  357. asmlinkage long sys32_sync_file_range(int fd, int __pad,
  358. unsigned long a2, unsigned long a3,
  359. unsigned long a4, unsigned long a5,
  360. int flags)
  361. {
  362. return sys_sync_file_range(fd,
  363. merge_64(a2, a3), merge_64(a4, a5),
  364. flags);
  365. }
  366. asmlinkage long sys32_fadvise64_64(int fd, int __pad,
  367. unsigned long a2, unsigned long a3,
  368. unsigned long a4, unsigned long a5,
  369. int flags)
  370. {
  371. return sys_fadvise64_64(fd,
  372. merge_64(a2, a3), merge_64(a4, a5),
  373. flags);
  374. }
  375. asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_a2,
  376. unsigned offset_a3, unsigned len_a4, unsigned len_a5)
  377. {
  378. return sys_fallocate(fd, mode, merge_64(offset_a2, offset_a3),
  379. merge_64(len_a4, len_a5));
  380. }
  381. save_static_function(sys32_clone);
  382. static int noinline __used
  383. _sys32_clone(nabi_no_regargs struct pt_regs regs)
  384. {
  385. unsigned long clone_flags;
  386. unsigned long newsp;
  387. int __user *parent_tidptr, *child_tidptr;
  388. clone_flags = regs.regs[4];
  389. newsp = regs.regs[5];
  390. if (!newsp)
  391. newsp = regs.regs[29];
  392. parent_tidptr = (int __user *) regs.regs[6];
  393. /* Use __dummy4 instead of getting it off the stack, so that
  394. syscall() works. */
  395. child_tidptr = (int __user *) __dummy4;
  396. return do_fork(clone_flags, newsp, &regs, 0,
  397. parent_tidptr, child_tidptr);
  398. }