compat_linux.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. current->thread.fp_regs.fpc=0;
  405. asm volatile("sfpc %0,0" : : "d" (0));
  406. result = regs->gprs[2];
  407. out_putname:
  408. putname(filename);
  409. out:
  410. return result;
  411. }
  412. #ifdef CONFIG_MODULES
  413. asmlinkage long
  414. sys32_init_module(void __user *umod, unsigned long len,
  415. const char __user *uargs)
  416. {
  417. return sys_init_module(umod, len, uargs);
  418. }
  419. asmlinkage long
  420. sys32_delete_module(const char __user *name_user, unsigned int flags)
  421. {
  422. return sys_delete_module(name_user, flags);
  423. }
  424. #else /* CONFIG_MODULES */
  425. asmlinkage long
  426. sys32_init_module(void __user *umod, unsigned long len,
  427. const char __user *uargs)
  428. {
  429. return -ENOSYS;
  430. }
  431. asmlinkage long
  432. sys32_delete_module(const char __user *name_user, unsigned int flags)
  433. {
  434. return -ENOSYS;
  435. }
  436. #endif /* CONFIG_MODULES */
  437. asmlinkage long sys32_pread64(unsigned int fd, char __user *ubuf,
  438. size_t count, u32 poshi, u32 poslo)
  439. {
  440. if ((compat_ssize_t) count < 0)
  441. return -EINVAL;
  442. return sys_pread64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  443. }
  444. asmlinkage long sys32_pwrite64(unsigned int fd, const char __user *ubuf,
  445. size_t count, u32 poshi, u32 poslo)
  446. {
  447. if ((compat_ssize_t) count < 0)
  448. return -EINVAL;
  449. return sys_pwrite64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  450. }
  451. asmlinkage compat_ssize_t sys32_readahead(int fd, u32 offhi, u32 offlo, s32 count)
  452. {
  453. return sys_readahead(fd, ((loff_t)AA(offhi) << 32) | AA(offlo), count);
  454. }
  455. asmlinkage long sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, size_t count)
  456. {
  457. mm_segment_t old_fs = get_fs();
  458. int ret;
  459. off_t of;
  460. if (offset && get_user(of, offset))
  461. return -EFAULT;
  462. set_fs(KERNEL_DS);
  463. ret = sys_sendfile(out_fd, in_fd,
  464. offset ? (off_t __force __user *) &of : NULL, count);
  465. set_fs(old_fs);
  466. if (offset && put_user(of, offset))
  467. return -EFAULT;
  468. return ret;
  469. }
  470. asmlinkage long sys32_sendfile64(int out_fd, int in_fd,
  471. compat_loff_t __user *offset, s32 count)
  472. {
  473. mm_segment_t old_fs = get_fs();
  474. int ret;
  475. loff_t lof;
  476. if (offset && get_user(lof, offset))
  477. return -EFAULT;
  478. set_fs(KERNEL_DS);
  479. ret = sys_sendfile64(out_fd, in_fd,
  480. offset ? (loff_t __force __user *) &lof : NULL,
  481. count);
  482. set_fs(old_fs);
  483. if (offset && put_user(lof, offset))
  484. return -EFAULT;
  485. return ret;
  486. }
  487. #ifdef CONFIG_SYSCTL_SYSCALL
  488. struct __sysctl_args32 {
  489. u32 name;
  490. int nlen;
  491. u32 oldval;
  492. u32 oldlenp;
  493. u32 newval;
  494. u32 newlen;
  495. u32 __unused[4];
  496. };
  497. asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
  498. {
  499. struct __sysctl_args32 tmp;
  500. int error;
  501. size_t oldlen;
  502. size_t __user *oldlenp = NULL;
  503. unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
  504. if (copy_from_user(&tmp, args, sizeof(tmp)))
  505. return -EFAULT;
  506. if (tmp.oldval && tmp.oldlenp) {
  507. /* Duh, this is ugly and might not work if sysctl_args
  508. is in read-only memory, but do_sysctl does indirectly
  509. a lot of uaccess in both directions and we'd have to
  510. basically copy the whole sysctl.c here, and
  511. glibc's __sysctl uses rw memory for the structure
  512. anyway. */
  513. if (get_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)) ||
  514. put_user(oldlen, (size_t __user *)addr))
  515. return -EFAULT;
  516. oldlenp = (size_t __user *)addr;
  517. }
  518. lock_kernel();
  519. error = do_sysctl(compat_ptr(tmp.name), tmp.nlen, compat_ptr(tmp.oldval),
  520. oldlenp, compat_ptr(tmp.newval), tmp.newlen);
  521. unlock_kernel();
  522. if (oldlenp) {
  523. if (!error) {
  524. if (get_user(oldlen, (size_t __user *)addr) ||
  525. put_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)))
  526. error = -EFAULT;
  527. }
  528. if (copy_to_user(args->__unused, tmp.__unused,
  529. sizeof(tmp.__unused)))
  530. error = -EFAULT;
  531. }
  532. return error;
  533. }
  534. #endif
  535. struct stat64_emu31 {
  536. unsigned long long st_dev;
  537. unsigned int __pad1;
  538. #define STAT64_HAS_BROKEN_ST_INO 1
  539. u32 __st_ino;
  540. unsigned int st_mode;
  541. unsigned int st_nlink;
  542. u32 st_uid;
  543. u32 st_gid;
  544. unsigned long long st_rdev;
  545. unsigned int __pad3;
  546. long st_size;
  547. u32 st_blksize;
  548. unsigned char __pad4[4];
  549. u32 __pad5; /* future possible st_blocks high bits */
  550. u32 st_blocks; /* Number 512-byte blocks allocated. */
  551. u32 st_atime;
  552. u32 __pad6;
  553. u32 st_mtime;
  554. u32 __pad7;
  555. u32 st_ctime;
  556. u32 __pad8; /* will be high 32 bits of ctime someday */
  557. unsigned long st_ino;
  558. };
  559. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  560. {
  561. struct stat64_emu31 tmp;
  562. memset(&tmp, 0, sizeof(tmp));
  563. tmp.st_dev = huge_encode_dev(stat->dev);
  564. tmp.st_ino = stat->ino;
  565. tmp.__st_ino = (u32)stat->ino;
  566. tmp.st_mode = stat->mode;
  567. tmp.st_nlink = (unsigned int)stat->nlink;
  568. tmp.st_uid = stat->uid;
  569. tmp.st_gid = stat->gid;
  570. tmp.st_rdev = huge_encode_dev(stat->rdev);
  571. tmp.st_size = stat->size;
  572. tmp.st_blksize = (u32)stat->blksize;
  573. tmp.st_blocks = (u32)stat->blocks;
  574. tmp.st_atime = (u32)stat->atime.tv_sec;
  575. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  576. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  577. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  578. }
  579. asmlinkage long sys32_stat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  580. {
  581. struct kstat stat;
  582. int ret = vfs_stat(filename, &stat);
  583. if (!ret)
  584. ret = cp_stat64(statbuf, &stat);
  585. return ret;
  586. }
  587. asmlinkage long sys32_lstat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  588. {
  589. struct kstat stat;
  590. int ret = vfs_lstat(filename, &stat);
  591. if (!ret)
  592. ret = cp_stat64(statbuf, &stat);
  593. return ret;
  594. }
  595. asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 __user * statbuf)
  596. {
  597. struct kstat stat;
  598. int ret = vfs_fstat(fd, &stat);
  599. if (!ret)
  600. ret = cp_stat64(statbuf, &stat);
  601. return ret;
  602. }
  603. asmlinkage long sys32_fstatat64(unsigned int dfd, char __user *filename,
  604. struct stat64_emu31 __user* statbuf, int flag)
  605. {
  606. struct kstat stat;
  607. int error;
  608. error = vfs_fstatat(dfd, filename, &stat, flag);
  609. if (error)
  610. return error;
  611. return cp_stat64(statbuf, &stat);
  612. }
  613. /*
  614. * Linux/i386 didn't use to be able to handle more than
  615. * 4 system call parameters, so these system calls used a memory
  616. * block for parameter passing..
  617. */
  618. struct mmap_arg_struct_emu31 {
  619. u32 addr;
  620. u32 len;
  621. u32 prot;
  622. u32 flags;
  623. u32 fd;
  624. u32 offset;
  625. };
  626. /* common code for old and new mmaps */
  627. static inline long do_mmap2(
  628. unsigned long addr, unsigned long len,
  629. unsigned long prot, unsigned long flags,
  630. unsigned long fd, unsigned long pgoff)
  631. {
  632. struct file * file = NULL;
  633. unsigned long error = -EBADF;
  634. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  635. if (!(flags & MAP_ANONYMOUS)) {
  636. file = fget(fd);
  637. if (!file)
  638. goto out;
  639. }
  640. down_write(&current->mm->mmap_sem);
  641. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  642. if (!IS_ERR((void *) error) && error + len >= 0x80000000ULL) {
  643. /* Result is out of bounds. */
  644. do_munmap(current->mm, addr, len);
  645. error = -ENOMEM;
  646. }
  647. up_write(&current->mm->mmap_sem);
  648. if (file)
  649. fput(file);
  650. out:
  651. return error;
  652. }
  653. asmlinkage unsigned long
  654. old32_mmap(struct mmap_arg_struct_emu31 __user *arg)
  655. {
  656. struct mmap_arg_struct_emu31 a;
  657. int error = -EFAULT;
  658. if (copy_from_user(&a, arg, sizeof(a)))
  659. goto out;
  660. error = -EINVAL;
  661. if (a.offset & ~PAGE_MASK)
  662. goto out;
  663. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
  664. out:
  665. return error;
  666. }
  667. asmlinkage long
  668. sys32_mmap2(struct mmap_arg_struct_emu31 __user *arg)
  669. {
  670. struct mmap_arg_struct_emu31 a;
  671. int error = -EFAULT;
  672. if (copy_from_user(&a, arg, sizeof(a)))
  673. goto out;
  674. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  675. out:
  676. return error;
  677. }
  678. asmlinkage long sys32_read(unsigned int fd, char __user * buf, size_t count)
  679. {
  680. if ((compat_ssize_t) count < 0)
  681. return -EINVAL;
  682. return sys_read(fd, buf, count);
  683. }
  684. asmlinkage long sys32_write(unsigned int fd, char __user * buf, size_t count)
  685. {
  686. if ((compat_ssize_t) count < 0)
  687. return -EINVAL;
  688. return sys_write(fd, buf, count);
  689. }
  690. asmlinkage long sys32_clone(void)
  691. {
  692. struct pt_regs *regs = task_pt_regs(current);
  693. unsigned long clone_flags;
  694. unsigned long newsp;
  695. int __user *parent_tidptr, *child_tidptr;
  696. clone_flags = regs->gprs[3] & 0xffffffffUL;
  697. newsp = regs->orig_gpr2 & 0x7fffffffUL;
  698. parent_tidptr = compat_ptr(regs->gprs[4]);
  699. child_tidptr = compat_ptr(regs->gprs[5]);
  700. if (!newsp)
  701. newsp = regs->gprs[15];
  702. return do_fork(clone_flags, newsp, regs, 0,
  703. parent_tidptr, child_tidptr);
  704. }
  705. /*
  706. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  707. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  708. * because the 31 bit values differ from the 64 bit values.
  709. */
  710. asmlinkage long
  711. sys32_fadvise64(int fd, loff_t offset, size_t len, int advise)
  712. {
  713. if (advise == 4)
  714. advise = POSIX_FADV_DONTNEED;
  715. else if (advise == 5)
  716. advise = POSIX_FADV_NOREUSE;
  717. return sys_fadvise64(fd, offset, len, advise);
  718. }
  719. struct fadvise64_64_args {
  720. int fd;
  721. long long offset;
  722. long long len;
  723. int advice;
  724. };
  725. asmlinkage long
  726. sys32_fadvise64_64(struct fadvise64_64_args __user *args)
  727. {
  728. struct fadvise64_64_args a;
  729. if ( copy_from_user(&a, args, sizeof(a)) )
  730. return -EFAULT;
  731. if (a.advice == 4)
  732. a.advice = POSIX_FADV_DONTNEED;
  733. else if (a.advice == 5)
  734. a.advice = POSIX_FADV_NOREUSE;
  735. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  736. }