compat_linux.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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/timex.h>
  28. #include <linux/smp.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/sem.h>
  31. #include <linux/msg.h>
  32. #include <linux/shm.h>
  33. #include <linux/slab.h>
  34. #include <linux/uio.h>
  35. #include <linux/nfs_fs.h>
  36. #include <linux/quota.h>
  37. #include <linux/module.h>
  38. #include <linux/sunrpc/svc.h>
  39. #include <linux/nfsd/nfsd.h>
  40. #include <linux/nfsd/cache.h>
  41. #include <linux/nfsd/xdr.h>
  42. #include <linux/nfsd/syscall.h>
  43. #include <linux/poll.h>
  44. #include <linux/personality.h>
  45. #include <linux/stat.h>
  46. #include <linux/filter.h>
  47. #include <linux/highmem.h>
  48. #include <linux/highuid.h>
  49. #include <linux/mman.h>
  50. #include <linux/ipv6.h>
  51. #include <linux/in.h>
  52. #include <linux/icmpv6.h>
  53. #include <linux/syscalls.h>
  54. #include <linux/sysctl.h>
  55. #include <linux/binfmts.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 * filename, u16 user, u16 group)
  93. {
  94. return sys_chown(filename, low2highuid(user), low2highgid(group));
  95. }
  96. asmlinkage long sys32_lchown16(const char * 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 *ruid, u16 *euid, u16 *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 *rgid, u16 *egid, u16 *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 *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 *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 *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 *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 *i)
  235. {
  236. return (!access_ok(VERIFY_READ, tv32, sizeof(*tv32)) ||
  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 *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 * 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 *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 *tv, struct timezone *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 *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 *tv, struct timezone *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 *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 *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 *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 *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. /* Handle adjtimex compatibility. */
  608. struct timex32 {
  609. u32 modes;
  610. s32 offset, freq, maxerror, esterror;
  611. s32 status, constant, precision, tolerance;
  612. struct compat_timeval time;
  613. s32 tick;
  614. s32 ppsfreq, jitter, shift, stabil;
  615. s32 jitcnt, calcnt, errcnt, stbcnt;
  616. s32 :32; s32 :32; s32 :32; s32 :32;
  617. s32 :32; s32 :32; s32 :32; s32 :32;
  618. s32 :32; s32 :32; s32 :32; s32 :32;
  619. };
  620. extern int do_adjtimex(struct timex *);
  621. asmlinkage long sys32_adjtimex(struct timex32 *utp)
  622. {
  623. struct timex txc;
  624. int ret;
  625. memset(&txc, 0, sizeof(struct timex));
  626. if(get_user(txc.modes, &utp->modes) ||
  627. __get_user(txc.offset, &utp->offset) ||
  628. __get_user(txc.freq, &utp->freq) ||
  629. __get_user(txc.maxerror, &utp->maxerror) ||
  630. __get_user(txc.esterror, &utp->esterror) ||
  631. __get_user(txc.status, &utp->status) ||
  632. __get_user(txc.constant, &utp->constant) ||
  633. __get_user(txc.precision, &utp->precision) ||
  634. __get_user(txc.tolerance, &utp->tolerance) ||
  635. __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
  636. __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
  637. __get_user(txc.tick, &utp->tick) ||
  638. __get_user(txc.ppsfreq, &utp->ppsfreq) ||
  639. __get_user(txc.jitter, &utp->jitter) ||
  640. __get_user(txc.shift, &utp->shift) ||
  641. __get_user(txc.stabil, &utp->stabil) ||
  642. __get_user(txc.jitcnt, &utp->jitcnt) ||
  643. __get_user(txc.calcnt, &utp->calcnt) ||
  644. __get_user(txc.errcnt, &utp->errcnt) ||
  645. __get_user(txc.stbcnt, &utp->stbcnt))
  646. return -EFAULT;
  647. ret = do_adjtimex(&txc);
  648. if(put_user(txc.modes, &utp->modes) ||
  649. __put_user(txc.offset, &utp->offset) ||
  650. __put_user(txc.freq, &utp->freq) ||
  651. __put_user(txc.maxerror, &utp->maxerror) ||
  652. __put_user(txc.esterror, &utp->esterror) ||
  653. __put_user(txc.status, &utp->status) ||
  654. __put_user(txc.constant, &utp->constant) ||
  655. __put_user(txc.precision, &utp->precision) ||
  656. __put_user(txc.tolerance, &utp->tolerance) ||
  657. __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
  658. __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
  659. __put_user(txc.tick, &utp->tick) ||
  660. __put_user(txc.ppsfreq, &utp->ppsfreq) ||
  661. __put_user(txc.jitter, &utp->jitter) ||
  662. __put_user(txc.shift, &utp->shift) ||
  663. __put_user(txc.stabil, &utp->stabil) ||
  664. __put_user(txc.jitcnt, &utp->jitcnt) ||
  665. __put_user(txc.calcnt, &utp->calcnt) ||
  666. __put_user(txc.errcnt, &utp->errcnt) ||
  667. __put_user(txc.stbcnt, &utp->stbcnt))
  668. ret = -EFAULT;
  669. return ret;
  670. }
  671. #ifdef CONFIG_SYSCTL
  672. struct __sysctl_args32 {
  673. u32 name;
  674. int nlen;
  675. u32 oldval;
  676. u32 oldlenp;
  677. u32 newval;
  678. u32 newlen;
  679. u32 __unused[4];
  680. };
  681. asmlinkage long sys32_sysctl(struct __sysctl_args32 *args)
  682. {
  683. struct __sysctl_args32 tmp;
  684. int error;
  685. size_t oldlen, *oldlenp = NULL;
  686. unsigned long addr = (((long)&args->__unused[0]) + 7) & ~7;
  687. if (copy_from_user(&tmp, args, sizeof(tmp)))
  688. return -EFAULT;
  689. if (tmp.oldval && tmp.oldlenp) {
  690. /* Duh, this is ugly and might not work if sysctl_args
  691. is in read-only memory, but do_sysctl does indirectly
  692. a lot of uaccess in both directions and we'd have to
  693. basically copy the whole sysctl.c here, and
  694. glibc's __sysctl uses rw memory for the structure
  695. anyway. */
  696. if (get_user(oldlen, (u32 *)A(tmp.oldlenp)) ||
  697. put_user(oldlen, (size_t *)addr))
  698. return -EFAULT;
  699. oldlenp = (size_t *)addr;
  700. }
  701. lock_kernel();
  702. error = do_sysctl((int *)A(tmp.name), tmp.nlen, (void *)A(tmp.oldval),
  703. oldlenp, (void *)A(tmp.newval), tmp.newlen);
  704. unlock_kernel();
  705. if (oldlenp) {
  706. if (!error) {
  707. if (get_user(oldlen, (size_t *)addr) ||
  708. put_user(oldlen, (u32 *)A(tmp.oldlenp)))
  709. error = -EFAULT;
  710. }
  711. copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
  712. }
  713. return error;
  714. }
  715. #endif
  716. struct stat64_emu31 {
  717. unsigned long long st_dev;
  718. unsigned int __pad1;
  719. #define STAT64_HAS_BROKEN_ST_INO 1
  720. u32 __st_ino;
  721. unsigned int st_mode;
  722. unsigned int st_nlink;
  723. u32 st_uid;
  724. u32 st_gid;
  725. unsigned long long st_rdev;
  726. unsigned int __pad3;
  727. long st_size;
  728. u32 st_blksize;
  729. unsigned char __pad4[4];
  730. u32 __pad5; /* future possible st_blocks high bits */
  731. u32 st_blocks; /* Number 512-byte blocks allocated. */
  732. u32 st_atime;
  733. u32 __pad6;
  734. u32 st_mtime;
  735. u32 __pad7;
  736. u32 st_ctime;
  737. u32 __pad8; /* will be high 32 bits of ctime someday */
  738. unsigned long st_ino;
  739. };
  740. static int cp_stat64(struct stat64_emu31 *ubuf, struct kstat *stat)
  741. {
  742. struct stat64_emu31 tmp;
  743. memset(&tmp, 0, sizeof(tmp));
  744. tmp.st_dev = huge_encode_dev(stat->dev);
  745. tmp.st_ino = stat->ino;
  746. tmp.__st_ino = (u32)stat->ino;
  747. tmp.st_mode = stat->mode;
  748. tmp.st_nlink = (unsigned int)stat->nlink;
  749. tmp.st_uid = stat->uid;
  750. tmp.st_gid = stat->gid;
  751. tmp.st_rdev = huge_encode_dev(stat->rdev);
  752. tmp.st_size = stat->size;
  753. tmp.st_blksize = (u32)stat->blksize;
  754. tmp.st_blocks = (u32)stat->blocks;
  755. tmp.st_atime = (u32)stat->atime.tv_sec;
  756. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  757. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  758. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  759. }
  760. asmlinkage long sys32_stat64(char * filename, struct stat64_emu31 * statbuf)
  761. {
  762. struct kstat stat;
  763. int ret = vfs_stat(filename, &stat);
  764. if (!ret)
  765. ret = cp_stat64(statbuf, &stat);
  766. return ret;
  767. }
  768. asmlinkage long sys32_lstat64(char * filename, struct stat64_emu31 * statbuf)
  769. {
  770. struct kstat stat;
  771. int ret = vfs_lstat(filename, &stat);
  772. if (!ret)
  773. ret = cp_stat64(statbuf, &stat);
  774. return ret;
  775. }
  776. asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 * statbuf)
  777. {
  778. struct kstat stat;
  779. int ret = vfs_fstat(fd, &stat);
  780. if (!ret)
  781. ret = cp_stat64(statbuf, &stat);
  782. return ret;
  783. }
  784. /*
  785. * Linux/i386 didn't use to be able to handle more than
  786. * 4 system call parameters, so these system calls used a memory
  787. * block for parameter passing..
  788. */
  789. struct mmap_arg_struct_emu31 {
  790. u32 addr;
  791. u32 len;
  792. u32 prot;
  793. u32 flags;
  794. u32 fd;
  795. u32 offset;
  796. };
  797. /* common code for old and new mmaps */
  798. static inline long do_mmap2(
  799. unsigned long addr, unsigned long len,
  800. unsigned long prot, unsigned long flags,
  801. unsigned long fd, unsigned long pgoff)
  802. {
  803. struct file * file = NULL;
  804. unsigned long error = -EBADF;
  805. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  806. if (!(flags & MAP_ANONYMOUS)) {
  807. file = fget(fd);
  808. if (!file)
  809. goto out;
  810. }
  811. down_write(&current->mm->mmap_sem);
  812. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  813. if (!IS_ERR((void *) error) && error + len >= 0x80000000ULL) {
  814. /* Result is out of bounds. */
  815. do_munmap(current->mm, addr, len);
  816. error = -ENOMEM;
  817. }
  818. up_write(&current->mm->mmap_sem);
  819. if (file)
  820. fput(file);
  821. out:
  822. return error;
  823. }
  824. asmlinkage unsigned long
  825. old32_mmap(struct mmap_arg_struct_emu31 *arg)
  826. {
  827. struct mmap_arg_struct_emu31 a;
  828. int error = -EFAULT;
  829. if (copy_from_user(&a, arg, sizeof(a)))
  830. goto out;
  831. error = -EINVAL;
  832. if (a.offset & ~PAGE_MASK)
  833. goto out;
  834. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
  835. out:
  836. return error;
  837. }
  838. asmlinkage long
  839. sys32_mmap2(struct mmap_arg_struct_emu31 *arg)
  840. {
  841. struct mmap_arg_struct_emu31 a;
  842. int error = -EFAULT;
  843. if (copy_from_user(&a, arg, sizeof(a)))
  844. goto out;
  845. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  846. out:
  847. return error;
  848. }
  849. asmlinkage long sys32_read(unsigned int fd, char * buf, size_t count)
  850. {
  851. if ((compat_ssize_t) count < 0)
  852. return -EINVAL;
  853. return sys_read(fd, buf, count);
  854. }
  855. asmlinkage long sys32_write(unsigned int fd, char * buf, size_t count)
  856. {
  857. if ((compat_ssize_t) count < 0)
  858. return -EINVAL;
  859. return sys_write(fd, buf, count);
  860. }
  861. asmlinkage long sys32_clone(struct pt_regs regs)
  862. {
  863. unsigned long clone_flags;
  864. unsigned long newsp;
  865. int *parent_tidptr, *child_tidptr;
  866. clone_flags = regs.gprs[3] & 0xffffffffUL;
  867. newsp = regs.orig_gpr2 & 0x7fffffffUL;
  868. parent_tidptr = (int *) (regs.gprs[4] & 0x7fffffffUL);
  869. child_tidptr = (int *) (regs.gprs[5] & 0x7fffffffUL);
  870. if (!newsp)
  871. newsp = regs.gprs[15];
  872. return do_fork(clone_flags, newsp, &regs, 0,
  873. parent_tidptr, child_tidptr);
  874. }
  875. /*
  876. * Wrapper function for sys_timer_create.
  877. */
  878. extern asmlinkage long
  879. sys_timer_create(clockid_t, struct sigevent *, timer_t *);
  880. asmlinkage long
  881. sys32_timer_create(clockid_t which_clock, struct compat_sigevent *se32,
  882. timer_t *timer_id)
  883. {
  884. struct sigevent se;
  885. timer_t ktimer_id;
  886. mm_segment_t old_fs;
  887. long ret;
  888. if (se32 == NULL)
  889. return sys_timer_create(which_clock, NULL, timer_id);
  890. if (get_compat_sigevent(&se, se32))
  891. return -EFAULT;
  892. old_fs = get_fs();
  893. set_fs(KERNEL_DS);
  894. ret = sys_timer_create(which_clock, &se, &ktimer_id);
  895. set_fs(old_fs);
  896. if (!ret)
  897. ret = put_user (ktimer_id, timer_id);
  898. return ret;
  899. }
  900. /*
  901. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  902. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  903. * because the 31 bit values differ from the 64 bit values.
  904. */
  905. asmlinkage long
  906. sys32_fadvise64(int fd, loff_t offset, size_t len, int advise)
  907. {
  908. if (advise == 4)
  909. advise = POSIX_FADV_DONTNEED;
  910. else if (advise == 5)
  911. advise = POSIX_FADV_NOREUSE;
  912. return sys_fadvise64(fd, offset, len, advise);
  913. }
  914. struct fadvise64_64_args {
  915. int fd;
  916. long long offset;
  917. long long len;
  918. int advice;
  919. };
  920. asmlinkage long
  921. sys32_fadvise64_64(struct fadvise64_64_args __user *args)
  922. {
  923. struct fadvise64_64_args a;
  924. if ( copy_from_user(&a, args, sizeof(a)) )
  925. return -EFAULT;
  926. if (a.advice == 4)
  927. a.advice = POSIX_FADV_DONTNEED;
  928. else if (a.advice == 5)
  929. a.advice = POSIX_FADV_NOREUSE;
  930. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  931. }