compat_linux.c 24 KB

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