compat_linux.c 26 KB

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