linux32.c 11 KB

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