compat_linux.c 19 KB

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