compat_linux.c 24 KB

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