linux32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. asmlinkage unsigned long
  62. sys32_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  63. unsigned long flags, unsigned long fd, 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. asmlinkage long sys32_truncate64(const char __user * path,
  110. unsigned long __dummy, int a2, int a3)
  111. {
  112. return sys_truncate(path, merge_64(a2, a3));
  113. }
  114. asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long __dummy,
  115. int a2, int a3)
  116. {
  117. return sys_ftruncate(fd, merge_64(a2, a3));
  118. }
  119. asmlinkage int sys32_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. asmlinkage ssize_t sys32_pread(unsigned int fd, char __user * buf,
  129. size_t count, u32 unused, u64 a4, u64 a5)
  130. {
  131. return sys_pread64(fd, buf, count, merge_64(a4, a5));
  132. }
  133. asmlinkage ssize_t sys32_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. asmlinkage int sys32_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. asmlinkage long
  154. sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 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. asmlinkage long
  208. sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
  209. {
  210. return -ENOSYS;
  211. }
  212. #endif /* CONFIG_SYSVIPC */
  213. #ifdef CONFIG_MIPS32_N32
  214. asmlinkage long sysn32_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. asmlinkage long sysn32_msgsnd(int msqid, u32 msgp, unsigned msgsz, int msgflg)
  223. {
  224. return compat_sys_msgsnd(msqid, msgsz, msgflg, compat_ptr(msgp));
  225. }
  226. asmlinkage long sysn32_msgrcv(int msqid, u32 msgp, size_t msgsz, int msgtyp,
  227. int msgflg)
  228. {
  229. return compat_sys_msgrcv(msqid, msgsz, msgtyp, msgflg, IPC_64,
  230. compat_ptr(msgp));
  231. }
  232. #endif
  233. struct sysctl_args32
  234. {
  235. compat_caddr_t name;
  236. int nlen;
  237. compat_caddr_t oldval;
  238. compat_caddr_t oldlenp;
  239. compat_caddr_t newval;
  240. compat_size_t newlen;
  241. unsigned int __unused[4];
  242. };
  243. #ifdef CONFIG_SYSCTL_SYSCALL
  244. asmlinkage long sys32_sysctl(struct sysctl_args32 __user *args)
  245. {
  246. struct sysctl_args32 tmp;
  247. int error;
  248. size_t oldlen;
  249. size_t __user *oldlenp = NULL;
  250. unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
  251. if (copy_from_user(&tmp, args, sizeof(tmp)))
  252. return -EFAULT;
  253. if (tmp.oldval && tmp.oldlenp) {
  254. /* Duh, this is ugly and might not work if sysctl_args
  255. is in read-only memory, but do_sysctl does indirectly
  256. a lot of uaccess in both directions and we'd have to
  257. basically copy the whole sysctl.c here, and
  258. glibc's __sysctl uses rw memory for the structure
  259. anyway. */
  260. if (get_user(oldlen, (u32 __user *)A(tmp.oldlenp)) ||
  261. put_user(oldlen, (size_t __user *)addr))
  262. return -EFAULT;
  263. oldlenp = (size_t __user *)addr;
  264. }
  265. lock_kernel();
  266. error = do_sysctl((int __user *)A(tmp.name), tmp.nlen, (void __user *)A(tmp.oldval),
  267. oldlenp, (void __user *)A(tmp.newval), tmp.newlen);
  268. unlock_kernel();
  269. if (oldlenp) {
  270. if (!error) {
  271. if (get_user(oldlen, (size_t __user *)addr) ||
  272. put_user(oldlen, (u32 __user *)A(tmp.oldlenp)))
  273. error = -EFAULT;
  274. }
  275. copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
  276. }
  277. return error;
  278. }
  279. #endif /* CONFIG_SYSCTL_SYSCALL */
  280. asmlinkage long sys32_newuname(struct new_utsname __user * name)
  281. {
  282. int ret = 0;
  283. down_read(&uts_sem);
  284. if (copy_to_user(name, utsname(), sizeof *name))
  285. ret = -EFAULT;
  286. up_read(&uts_sem);
  287. if (current->personality == PER_LINUX32 && !ret)
  288. if (copy_to_user(name->machine, "mips\0\0\0", 8))
  289. ret = -EFAULT;
  290. return ret;
  291. }
  292. asmlinkage int sys32_personality(unsigned long personality)
  293. {
  294. int ret;
  295. personality &= 0xffffffff;
  296. if (personality(current->personality) == PER_LINUX32 &&
  297. personality == PER_LINUX)
  298. personality = PER_LINUX32;
  299. ret = sys_personality(personality);
  300. if (ret == PER_LINUX32)
  301. ret = PER_LINUX;
  302. return ret;
  303. }
  304. /* ustat compatibility */
  305. struct ustat32 {
  306. compat_daddr_t f_tfree;
  307. compat_ino_t f_tinode;
  308. char f_fname[6];
  309. char f_fpack[6];
  310. };
  311. extern asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf);
  312. asmlinkage int sys32_ustat(dev_t dev, struct ustat32 __user * ubuf32)
  313. {
  314. int err;
  315. struct ustat tmp;
  316. struct ustat32 tmp32;
  317. mm_segment_t old_fs = get_fs();
  318. set_fs(KERNEL_DS);
  319. err = sys_ustat(dev, (struct ustat __user *)&tmp);
  320. set_fs(old_fs);
  321. if (err)
  322. goto out;
  323. memset(&tmp32, 0, sizeof(struct ustat32));
  324. tmp32.f_tfree = tmp.f_tfree;
  325. tmp32.f_tinode = tmp.f_tinode;
  326. err = copy_to_user(ubuf32, &tmp32, sizeof(struct ustat32)) ? -EFAULT : 0;
  327. out:
  328. return err;
  329. }
  330. asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset,
  331. s32 count)
  332. {
  333. mm_segment_t old_fs = get_fs();
  334. int ret;
  335. off_t of;
  336. if (offset && get_user(of, offset))
  337. return -EFAULT;
  338. set_fs(KERNEL_DS);
  339. ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
  340. set_fs(old_fs);
  341. if (offset && put_user(of, offset))
  342. return -EFAULT;
  343. return ret;
  344. }
  345. asmlinkage ssize_t sys32_readahead(int fd, u32 pad0, u64 a2, u64 a3,
  346. size_t count)
  347. {
  348. return sys_readahead(fd, merge_64(a2, a3), count);
  349. }
  350. asmlinkage long sys32_sync_file_range(int fd, int __pad,
  351. unsigned long a2, unsigned long a3,
  352. unsigned long a4, unsigned long a5,
  353. int flags)
  354. {
  355. return sys_sync_file_range(fd,
  356. merge_64(a2, a3), merge_64(a4, a5),
  357. flags);
  358. }
  359. asmlinkage long sys32_fadvise64_64(int fd, int __pad,
  360. unsigned long a2, unsigned long a3,
  361. unsigned long a4, unsigned long a5,
  362. int flags)
  363. {
  364. return sys_fadvise64_64(fd,
  365. merge_64(a2, a3), merge_64(a4, a5),
  366. flags);
  367. }
  368. asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_a2,
  369. unsigned offset_a3, unsigned len_a4, unsigned len_a5)
  370. {
  371. return sys_fallocate(fd, mode, merge_64(offset_a2, offset_a3),
  372. merge_64(len_a4, len_a5));
  373. }
  374. save_static_function(sys32_clone);
  375. static int noinline __used
  376. _sys32_clone(nabi_no_regargs struct pt_regs regs)
  377. {
  378. unsigned long clone_flags;
  379. unsigned long newsp;
  380. int __user *parent_tidptr, *child_tidptr;
  381. clone_flags = regs.regs[4];
  382. newsp = regs.regs[5];
  383. if (!newsp)
  384. newsp = regs.regs[29];
  385. parent_tidptr = (int __user *) regs.regs[6];
  386. /* Use __dummy4 instead of getting it off the stack, so that
  387. syscall() works. */
  388. child_tidptr = (int __user *) __dummy4;
  389. return do_fork(clone_flags, newsp, &regs, 0,
  390. parent_tidptr, child_tidptr);
  391. }