compat_linux.c 24 KB

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