compat_linux.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * arch/s390x/kernel/linux32.c
  3. *
  4. * S390 version
  5. * Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7. * Gerhard Tonn (ton@de.ibm.com)
  8. * Thomas Spatzier (tspat@de.ibm.com)
  9. *
  10. * Conversion between 31bit and 64bit native syscalls.
  11. *
  12. * Heavily inspired by the 32-bit Sparc compat code which is
  13. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  14. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/file.h>
  22. #include <linux/signal.h>
  23. #include <linux/resource.h>
  24. #include <linux/times.h>
  25. #include <linux/utsname.h>
  26. #include <linux/smp.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/sem.h>
  29. #include <linux/msg.h>
  30. #include <linux/shm.h>
  31. #include <linux/slab.h>
  32. #include <linux/uio.h>
  33. #include <linux/nfs_fs.h>
  34. #include <linux/quota.h>
  35. #include <linux/module.h>
  36. #include <linux/sunrpc/svc.h>
  37. #include <linux/nfsd/nfsd.h>
  38. #include <linux/nfsd/cache.h>
  39. #include <linux/nfsd/xdr.h>
  40. #include <linux/nfsd/syscall.h>
  41. #include <linux/poll.h>
  42. #include <linux/personality.h>
  43. #include <linux/stat.h>
  44. #include <linux/filter.h>
  45. #include <linux/highmem.h>
  46. #include <linux/highuid.h>
  47. #include <linux/mman.h>
  48. #include <linux/ipv6.h>
  49. #include <linux/in.h>
  50. #include <linux/icmpv6.h>
  51. #include <linux/syscalls.h>
  52. #include <linux/sysctl.h>
  53. #include <linux/binfmts.h>
  54. #include <linux/capability.h>
  55. #include <linux/compat.h>
  56. #include <linux/vfs.h>
  57. #include <linux/ptrace.h>
  58. #include <linux/fadvise.h>
  59. #include <linux/ipc.h>
  60. #include <asm/types.h>
  61. #include <asm/uaccess.h>
  62. #include <net/scm.h>
  63. #include <net/sock.h>
  64. #include "compat_linux.h"
  65. long psw_user32_bits = (PSW_BASE32_BITS | PSW_MASK_DAT | PSW_ASC_HOME |
  66. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK |
  67. PSW_MASK_PSTATE | PSW_DEFAULT_KEY);
  68. long psw32_user_bits = (PSW32_BASE_BITS | PSW32_MASK_DAT | PSW32_ASC_HOME |
  69. PSW32_MASK_IO | PSW32_MASK_EXT | PSW32_MASK_MCHECK |
  70. PSW32_MASK_PSTATE);
  71. /* For this source file, we want overflow handling. */
  72. #undef high2lowuid
  73. #undef high2lowgid
  74. #undef low2highuid
  75. #undef low2highgid
  76. #undef SET_UID16
  77. #undef SET_GID16
  78. #undef NEW_TO_OLD_UID
  79. #undef NEW_TO_OLD_GID
  80. #undef SET_OLDSTAT_UID
  81. #undef SET_OLDSTAT_GID
  82. #undef SET_STAT_UID
  83. #undef SET_STAT_GID
  84. #define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
  85. #define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
  86. #define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
  87. #define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
  88. #define SET_UID16(var, uid) var = high2lowuid(uid)
  89. #define SET_GID16(var, gid) var = high2lowgid(gid)
  90. #define NEW_TO_OLD_UID(uid) high2lowuid(uid)
  91. #define NEW_TO_OLD_GID(gid) high2lowgid(gid)
  92. #define SET_OLDSTAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  93. #define SET_OLDSTAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  94. #define SET_STAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  95. #define SET_STAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  96. asmlinkage long sys32_chown16(const char __user * filename, u16 user, u16 group)
  97. {
  98. return sys_chown(filename, low2highuid(user), low2highgid(group));
  99. }
  100. asmlinkage long sys32_lchown16(const char __user * filename, u16 user, u16 group)
  101. {
  102. return sys_lchown(filename, low2highuid(user), low2highgid(group));
  103. }
  104. asmlinkage long sys32_fchown16(unsigned int fd, u16 user, u16 group)
  105. {
  106. return sys_fchown(fd, low2highuid(user), low2highgid(group));
  107. }
  108. asmlinkage long sys32_setregid16(u16 rgid, u16 egid)
  109. {
  110. return sys_setregid(low2highgid(rgid), low2highgid(egid));
  111. }
  112. asmlinkage long sys32_setgid16(u16 gid)
  113. {
  114. return sys_setgid((gid_t)gid);
  115. }
  116. asmlinkage long sys32_setreuid16(u16 ruid, u16 euid)
  117. {
  118. return sys_setreuid(low2highuid(ruid), low2highuid(euid));
  119. }
  120. asmlinkage long sys32_setuid16(u16 uid)
  121. {
  122. return sys_setuid((uid_t)uid);
  123. }
  124. asmlinkage long sys32_setresuid16(u16 ruid, u16 euid, u16 suid)
  125. {
  126. return sys_setresuid(low2highuid(ruid), low2highuid(euid),
  127. low2highuid(suid));
  128. }
  129. asmlinkage long sys32_getresuid16(u16 __user *ruid, u16 __user *euid, u16 __user *suid)
  130. {
  131. int retval;
  132. if (!(retval = put_user(high2lowuid(current->cred->uid), ruid)) &&
  133. !(retval = put_user(high2lowuid(current->cred->euid), euid)))
  134. retval = put_user(high2lowuid(current->cred->suid), suid);
  135. return retval;
  136. }
  137. asmlinkage long sys32_setresgid16(u16 rgid, u16 egid, u16 sgid)
  138. {
  139. return sys_setresgid(low2highgid(rgid), low2highgid(egid),
  140. low2highgid(sgid));
  141. }
  142. asmlinkage long sys32_getresgid16(u16 __user *rgid, u16 __user *egid, u16 __user *sgid)
  143. {
  144. int retval;
  145. if (!(retval = put_user(high2lowgid(current->cred->gid), rgid)) &&
  146. !(retval = put_user(high2lowgid(current->cred->egid), egid)))
  147. retval = put_user(high2lowgid(current->cred->sgid), sgid);
  148. return retval;
  149. }
  150. asmlinkage long sys32_setfsuid16(u16 uid)
  151. {
  152. return sys_setfsuid((uid_t)uid);
  153. }
  154. asmlinkage long sys32_setfsgid16(u16 gid)
  155. {
  156. return sys_setfsgid((gid_t)gid);
  157. }
  158. static int groups16_to_user(u16 __user *grouplist, struct group_info *group_info)
  159. {
  160. int i;
  161. u16 group;
  162. for (i = 0; i < group_info->ngroups; i++) {
  163. group = (u16)GROUP_AT(group_info, i);
  164. if (put_user(group, grouplist+i))
  165. return -EFAULT;
  166. }
  167. return 0;
  168. }
  169. static int groups16_from_user(struct group_info *group_info, u16 __user *grouplist)
  170. {
  171. int i;
  172. u16 group;
  173. for (i = 0; i < group_info->ngroups; i++) {
  174. if (get_user(group, grouplist+i))
  175. return -EFAULT;
  176. GROUP_AT(group_info, i) = (gid_t)group;
  177. }
  178. return 0;
  179. }
  180. asmlinkage long sys32_getgroups16(int gidsetsize, u16 __user *grouplist)
  181. {
  182. int i;
  183. if (gidsetsize < 0)
  184. return -EINVAL;
  185. get_group_info(current->cred->group_info);
  186. i = current->cred->group_info->ngroups;
  187. if (gidsetsize) {
  188. if (i > gidsetsize) {
  189. i = -EINVAL;
  190. goto out;
  191. }
  192. if (groups16_to_user(grouplist, current->cred->group_info)) {
  193. i = -EFAULT;
  194. goto out;
  195. }
  196. }
  197. out:
  198. put_group_info(current->cred->group_info);
  199. return i;
  200. }
  201. asmlinkage long sys32_setgroups16(int gidsetsize, u16 __user *grouplist)
  202. {
  203. struct group_info *group_info;
  204. int retval;
  205. if (!capable(CAP_SETGID))
  206. return -EPERM;
  207. if ((unsigned)gidsetsize > NGROUPS_MAX)
  208. return -EINVAL;
  209. group_info = groups_alloc(gidsetsize);
  210. if (!group_info)
  211. return -ENOMEM;
  212. retval = groups16_from_user(group_info, grouplist);
  213. if (retval) {
  214. put_group_info(group_info);
  215. return retval;
  216. }
  217. retval = set_current_groups(group_info);
  218. put_group_info(group_info);
  219. return retval;
  220. }
  221. asmlinkage long sys32_getuid16(void)
  222. {
  223. return high2lowuid(current->cred->uid);
  224. }
  225. asmlinkage long sys32_geteuid16(void)
  226. {
  227. return high2lowuid(current->cred->euid);
  228. }
  229. asmlinkage long sys32_getgid16(void)
  230. {
  231. return high2lowgid(current->cred->gid);
  232. }
  233. asmlinkage long sys32_getegid16(void)
  234. {
  235. return high2lowgid(current->cred->egid);
  236. }
  237. /*
  238. * sys32_ipc() is the de-multiplexer for the SysV IPC calls in 32bit emulation.
  239. *
  240. * This is really horribly ugly.
  241. */
  242. #ifdef CONFIG_SYSVIPC
  243. asmlinkage long sys32_ipc(u32 call, int first, int second, int third, u32 ptr)
  244. {
  245. if (call >> 16) /* hack for backward compatibility */
  246. return -EINVAL;
  247. call &= 0xffff;
  248. switch (call) {
  249. case SEMTIMEDOP:
  250. return compat_sys_semtimedop(first, compat_ptr(ptr),
  251. second, compat_ptr(third));
  252. case SEMOP:
  253. /* struct sembuf is the same on 32 and 64bit :)) */
  254. return sys_semtimedop(first, compat_ptr(ptr),
  255. second, NULL);
  256. case SEMGET:
  257. return sys_semget(first, second, third);
  258. case SEMCTL:
  259. return compat_sys_semctl(first, second, third,
  260. compat_ptr(ptr));
  261. case MSGSND:
  262. return compat_sys_msgsnd(first, second, third,
  263. compat_ptr(ptr));
  264. case MSGRCV:
  265. return compat_sys_msgrcv(first, second, 0, third,
  266. 0, compat_ptr(ptr));
  267. case MSGGET:
  268. return sys_msgget((key_t) first, second);
  269. case MSGCTL:
  270. return compat_sys_msgctl(first, second, compat_ptr(ptr));
  271. case SHMAT:
  272. return compat_sys_shmat(first, second, third,
  273. 0, compat_ptr(ptr));
  274. case SHMDT:
  275. return sys_shmdt(compat_ptr(ptr));
  276. case SHMGET:
  277. return sys_shmget(first, (unsigned)second, third);
  278. case SHMCTL:
  279. return compat_sys_shmctl(first, second, compat_ptr(ptr));
  280. }
  281. return -ENOSYS;
  282. }
  283. #endif
  284. asmlinkage long sys32_truncate64(const char __user * path, unsigned long high, unsigned long low)
  285. {
  286. if ((int)high < 0)
  287. return -EINVAL;
  288. else
  289. return sys_truncate(path, (high << 32) | low);
  290. }
  291. asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
  292. {
  293. if ((int)high < 0)
  294. return -EINVAL;
  295. else
  296. return sys_ftruncate(fd, (high << 32) | low);
  297. }
  298. asmlinkage long sys32_sched_rr_get_interval(compat_pid_t pid,
  299. struct compat_timespec __user *interval)
  300. {
  301. struct timespec t;
  302. int ret;
  303. mm_segment_t old_fs = get_fs ();
  304. set_fs (KERNEL_DS);
  305. ret = sys_sched_rr_get_interval(pid,
  306. (struct timespec __force __user *) &t);
  307. set_fs (old_fs);
  308. if (put_compat_timespec(&t, interval))
  309. return -EFAULT;
  310. return ret;
  311. }
  312. asmlinkage long sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
  313. compat_sigset_t __user *oset, size_t sigsetsize)
  314. {
  315. sigset_t s;
  316. compat_sigset_t s32;
  317. int ret;
  318. mm_segment_t old_fs = get_fs();
  319. if (set) {
  320. if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
  321. return -EFAULT;
  322. switch (_NSIG_WORDS) {
  323. case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
  324. case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
  325. case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
  326. case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  327. }
  328. }
  329. set_fs (KERNEL_DS);
  330. ret = sys_rt_sigprocmask(how,
  331. set ? (sigset_t __force __user *) &s : NULL,
  332. oset ? (sigset_t __force __user *) &s : NULL,
  333. sigsetsize);
  334. set_fs (old_fs);
  335. if (ret) return ret;
  336. if (oset) {
  337. switch (_NSIG_WORDS) {
  338. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  339. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  340. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  341. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  342. }
  343. if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
  344. return -EFAULT;
  345. }
  346. return 0;
  347. }
  348. asmlinkage long sys32_rt_sigpending(compat_sigset_t __user *set,
  349. size_t sigsetsize)
  350. {
  351. sigset_t s;
  352. compat_sigset_t s32;
  353. int ret;
  354. mm_segment_t old_fs = get_fs();
  355. set_fs (KERNEL_DS);
  356. ret = sys_rt_sigpending((sigset_t __force __user *) &s, sigsetsize);
  357. set_fs (old_fs);
  358. if (!ret) {
  359. switch (_NSIG_WORDS) {
  360. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  361. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  362. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  363. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  364. }
  365. if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
  366. return -EFAULT;
  367. }
  368. return ret;
  369. }
  370. asmlinkage long
  371. sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
  372. {
  373. siginfo_t info;
  374. int ret;
  375. mm_segment_t old_fs = get_fs();
  376. if (copy_siginfo_from_user32(&info, uinfo))
  377. return -EFAULT;
  378. set_fs (KERNEL_DS);
  379. ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __force __user *) &info);
  380. set_fs (old_fs);
  381. return ret;
  382. }
  383. /*
  384. * sys32_execve() executes a new program after the asm stub has set
  385. * things up for us. This should basically do what I want it to.
  386. */
  387. asmlinkage long sys32_execve(void)
  388. {
  389. struct pt_regs *regs = task_pt_regs(current);
  390. char *filename;
  391. unsigned long result;
  392. int rc;
  393. filename = getname(compat_ptr(regs->orig_gpr2));
  394. if (IS_ERR(filename)) {
  395. result = PTR_ERR(filename);
  396. goto out;
  397. }
  398. rc = compat_do_execve(filename, compat_ptr(regs->gprs[3]),
  399. compat_ptr(regs->gprs[4]), regs);
  400. if (rc) {
  401. result = rc;
  402. goto out_putname;
  403. }
  404. task_lock(current);
  405. current->ptrace &= ~PT_DTRACE;
  406. task_unlock(current);
  407. current->thread.fp_regs.fpc=0;
  408. asm volatile("sfpc %0,0" : : "d" (0));
  409. result = regs->gprs[2];
  410. out_putname:
  411. putname(filename);
  412. out:
  413. return result;
  414. }
  415. #ifdef CONFIG_MODULES
  416. asmlinkage long
  417. sys32_init_module(void __user *umod, unsigned long len,
  418. const char __user *uargs)
  419. {
  420. return sys_init_module(umod, len, uargs);
  421. }
  422. asmlinkage long
  423. sys32_delete_module(const char __user *name_user, unsigned int flags)
  424. {
  425. return sys_delete_module(name_user, flags);
  426. }
  427. #else /* CONFIG_MODULES */
  428. asmlinkage long
  429. sys32_init_module(void __user *umod, unsigned long len,
  430. const char __user *uargs)
  431. {
  432. return -ENOSYS;
  433. }
  434. asmlinkage long
  435. sys32_delete_module(const char __user *name_user, unsigned int flags)
  436. {
  437. return -ENOSYS;
  438. }
  439. #endif /* CONFIG_MODULES */
  440. asmlinkage long sys32_pread64(unsigned int fd, char __user *ubuf,
  441. size_t count, u32 poshi, u32 poslo)
  442. {
  443. if ((compat_ssize_t) count < 0)
  444. return -EINVAL;
  445. return sys_pread64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  446. }
  447. asmlinkage long sys32_pwrite64(unsigned int fd, const char __user *ubuf,
  448. size_t count, u32 poshi, u32 poslo)
  449. {
  450. if ((compat_ssize_t) count < 0)
  451. return -EINVAL;
  452. return sys_pwrite64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  453. }
  454. asmlinkage compat_ssize_t sys32_readahead(int fd, u32 offhi, u32 offlo, s32 count)
  455. {
  456. return sys_readahead(fd, ((loff_t)AA(offhi) << 32) | AA(offlo), count);
  457. }
  458. asmlinkage long sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, size_t count)
  459. {
  460. mm_segment_t old_fs = get_fs();
  461. int ret;
  462. off_t of;
  463. if (offset && get_user(of, offset))
  464. return -EFAULT;
  465. set_fs(KERNEL_DS);
  466. ret = sys_sendfile(out_fd, in_fd,
  467. offset ? (off_t __force __user *) &of : NULL, count);
  468. set_fs(old_fs);
  469. if (offset && put_user(of, offset))
  470. return -EFAULT;
  471. return ret;
  472. }
  473. asmlinkage long sys32_sendfile64(int out_fd, int in_fd,
  474. compat_loff_t __user *offset, s32 count)
  475. {
  476. mm_segment_t old_fs = get_fs();
  477. int ret;
  478. loff_t lof;
  479. if (offset && get_user(lof, offset))
  480. return -EFAULT;
  481. set_fs(KERNEL_DS);
  482. ret = sys_sendfile64(out_fd, in_fd,
  483. offset ? (loff_t __force __user *) &lof : NULL,
  484. count);
  485. set_fs(old_fs);
  486. if (offset && put_user(lof, offset))
  487. return -EFAULT;
  488. return ret;
  489. }
  490. #ifdef CONFIG_SYSCTL_SYSCALL
  491. struct __sysctl_args32 {
  492. u32 name;
  493. int nlen;
  494. u32 oldval;
  495. u32 oldlenp;
  496. u32 newval;
  497. u32 newlen;
  498. u32 __unused[4];
  499. };
  500. asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
  501. {
  502. struct __sysctl_args32 tmp;
  503. int error;
  504. size_t oldlen;
  505. size_t __user *oldlenp = NULL;
  506. unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
  507. if (copy_from_user(&tmp, args, sizeof(tmp)))
  508. return -EFAULT;
  509. if (tmp.oldval && tmp.oldlenp) {
  510. /* Duh, this is ugly and might not work if sysctl_args
  511. is in read-only memory, but do_sysctl does indirectly
  512. a lot of uaccess in both directions and we'd have to
  513. basically copy the whole sysctl.c here, and
  514. glibc's __sysctl uses rw memory for the structure
  515. anyway. */
  516. if (get_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)) ||
  517. put_user(oldlen, (size_t __user *)addr))
  518. return -EFAULT;
  519. oldlenp = (size_t __user *)addr;
  520. }
  521. lock_kernel();
  522. error = do_sysctl(compat_ptr(tmp.name), tmp.nlen, compat_ptr(tmp.oldval),
  523. oldlenp, compat_ptr(tmp.newval), tmp.newlen);
  524. unlock_kernel();
  525. if (oldlenp) {
  526. if (!error) {
  527. if (get_user(oldlen, (size_t __user *)addr) ||
  528. put_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)))
  529. error = -EFAULT;
  530. }
  531. if (copy_to_user(args->__unused, tmp.__unused,
  532. sizeof(tmp.__unused)))
  533. error = -EFAULT;
  534. }
  535. return error;
  536. }
  537. #endif
  538. struct stat64_emu31 {
  539. unsigned long long st_dev;
  540. unsigned int __pad1;
  541. #define STAT64_HAS_BROKEN_ST_INO 1
  542. u32 __st_ino;
  543. unsigned int st_mode;
  544. unsigned int st_nlink;
  545. u32 st_uid;
  546. u32 st_gid;
  547. unsigned long long st_rdev;
  548. unsigned int __pad3;
  549. long st_size;
  550. u32 st_blksize;
  551. unsigned char __pad4[4];
  552. u32 __pad5; /* future possible st_blocks high bits */
  553. u32 st_blocks; /* Number 512-byte blocks allocated. */
  554. u32 st_atime;
  555. u32 __pad6;
  556. u32 st_mtime;
  557. u32 __pad7;
  558. u32 st_ctime;
  559. u32 __pad8; /* will be high 32 bits of ctime someday */
  560. unsigned long st_ino;
  561. };
  562. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  563. {
  564. struct stat64_emu31 tmp;
  565. memset(&tmp, 0, sizeof(tmp));
  566. tmp.st_dev = huge_encode_dev(stat->dev);
  567. tmp.st_ino = stat->ino;
  568. tmp.__st_ino = (u32)stat->ino;
  569. tmp.st_mode = stat->mode;
  570. tmp.st_nlink = (unsigned int)stat->nlink;
  571. tmp.st_uid = stat->uid;
  572. tmp.st_gid = stat->gid;
  573. tmp.st_rdev = huge_encode_dev(stat->rdev);
  574. tmp.st_size = stat->size;
  575. tmp.st_blksize = (u32)stat->blksize;
  576. tmp.st_blocks = (u32)stat->blocks;
  577. tmp.st_atime = (u32)stat->atime.tv_sec;
  578. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  579. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  580. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  581. }
  582. asmlinkage long sys32_stat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  583. {
  584. struct kstat stat;
  585. int ret = vfs_stat(filename, &stat);
  586. if (!ret)
  587. ret = cp_stat64(statbuf, &stat);
  588. return ret;
  589. }
  590. asmlinkage long sys32_lstat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  591. {
  592. struct kstat stat;
  593. int ret = vfs_lstat(filename, &stat);
  594. if (!ret)
  595. ret = cp_stat64(statbuf, &stat);
  596. return ret;
  597. }
  598. asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 __user * statbuf)
  599. {
  600. struct kstat stat;
  601. int ret = vfs_fstat(fd, &stat);
  602. if (!ret)
  603. ret = cp_stat64(statbuf, &stat);
  604. return ret;
  605. }
  606. asmlinkage long sys32_fstatat64(unsigned int dfd, char __user *filename,
  607. struct stat64_emu31 __user* statbuf, int flag)
  608. {
  609. struct kstat stat;
  610. int error = -EINVAL;
  611. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  612. goto out;
  613. if (flag & AT_SYMLINK_NOFOLLOW)
  614. error = vfs_lstat_fd(dfd, filename, &stat);
  615. else
  616. error = vfs_stat_fd(dfd, filename, &stat);
  617. if (!error)
  618. error = cp_stat64(statbuf, &stat);
  619. out:
  620. return error;
  621. }
  622. /*
  623. * Linux/i386 didn't use to be able to handle more than
  624. * 4 system call parameters, so these system calls used a memory
  625. * block for parameter passing..
  626. */
  627. struct mmap_arg_struct_emu31 {
  628. u32 addr;
  629. u32 len;
  630. u32 prot;
  631. u32 flags;
  632. u32 fd;
  633. u32 offset;
  634. };
  635. /* common code for old and new mmaps */
  636. static inline long do_mmap2(
  637. unsigned long addr, unsigned long len,
  638. unsigned long prot, unsigned long flags,
  639. unsigned long fd, unsigned long pgoff)
  640. {
  641. struct file * file = NULL;
  642. unsigned long error = -EBADF;
  643. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  644. if (!(flags & MAP_ANONYMOUS)) {
  645. file = fget(fd);
  646. if (!file)
  647. goto out;
  648. }
  649. down_write(&current->mm->mmap_sem);
  650. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  651. if (!IS_ERR((void *) error) && error + len >= 0x80000000ULL) {
  652. /* Result is out of bounds. */
  653. do_munmap(current->mm, addr, len);
  654. error = -ENOMEM;
  655. }
  656. up_write(&current->mm->mmap_sem);
  657. if (file)
  658. fput(file);
  659. out:
  660. return error;
  661. }
  662. asmlinkage unsigned long
  663. old32_mmap(struct mmap_arg_struct_emu31 __user *arg)
  664. {
  665. struct mmap_arg_struct_emu31 a;
  666. int error = -EFAULT;
  667. if (copy_from_user(&a, arg, sizeof(a)))
  668. goto out;
  669. error = -EINVAL;
  670. if (a.offset & ~PAGE_MASK)
  671. goto out;
  672. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
  673. out:
  674. return error;
  675. }
  676. asmlinkage long
  677. sys32_mmap2(struct mmap_arg_struct_emu31 __user *arg)
  678. {
  679. struct mmap_arg_struct_emu31 a;
  680. int error = -EFAULT;
  681. if (copy_from_user(&a, arg, sizeof(a)))
  682. goto out;
  683. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  684. out:
  685. return error;
  686. }
  687. asmlinkage long sys32_read(unsigned int fd, char __user * buf, size_t count)
  688. {
  689. if ((compat_ssize_t) count < 0)
  690. return -EINVAL;
  691. return sys_read(fd, buf, count);
  692. }
  693. asmlinkage long sys32_write(unsigned int fd, char __user * buf, size_t count)
  694. {
  695. if ((compat_ssize_t) count < 0)
  696. return -EINVAL;
  697. return sys_write(fd, buf, count);
  698. }
  699. asmlinkage long sys32_clone(void)
  700. {
  701. struct pt_regs *regs = task_pt_regs(current);
  702. unsigned long clone_flags;
  703. unsigned long newsp;
  704. int __user *parent_tidptr, *child_tidptr;
  705. clone_flags = regs->gprs[3] & 0xffffffffUL;
  706. newsp = regs->orig_gpr2 & 0x7fffffffUL;
  707. parent_tidptr = compat_ptr(regs->gprs[4]);
  708. child_tidptr = compat_ptr(regs->gprs[5]);
  709. if (!newsp)
  710. newsp = regs->gprs[15];
  711. return do_fork(clone_flags, newsp, regs, 0,
  712. parent_tidptr, child_tidptr);
  713. }
  714. /*
  715. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  716. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  717. * because the 31 bit values differ from the 64 bit values.
  718. */
  719. asmlinkage long
  720. sys32_fadvise64(int fd, loff_t offset, size_t len, int advise)
  721. {
  722. if (advise == 4)
  723. advise = POSIX_FADV_DONTNEED;
  724. else if (advise == 5)
  725. advise = POSIX_FADV_NOREUSE;
  726. return sys_fadvise64(fd, offset, len, advise);
  727. }
  728. struct fadvise64_64_args {
  729. int fd;
  730. long long offset;
  731. long long len;
  732. int advice;
  733. };
  734. asmlinkage long
  735. sys32_fadvise64_64(struct fadvise64_64_args __user *args)
  736. {
  737. struct fadvise64_64_args a;
  738. if ( copy_from_user(&a, args, sizeof(a)) )
  739. return -EFAULT;
  740. if (a.advice == 4)
  741. a.advice = POSIX_FADV_DONTNEED;
  742. else if (a.advice == 5)
  743. a.advice = POSIX_FADV_NOREUSE;
  744. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  745. }