compat_linux.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * arch/s390x/kernel/linux32.c
  3. *
  4. * S390 version
  5. * Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7. * Gerhard Tonn (ton@de.ibm.com)
  8. * Thomas Spatzier (tspat@de.ibm.com)
  9. *
  10. * Conversion between 31bit and 64bit native syscalls.
  11. *
  12. * Heavily inspired by the 32-bit Sparc compat code which is
  13. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  14. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/file.h>
  22. #include <linux/signal.h>
  23. #include <linux/resource.h>
  24. #include <linux/times.h>
  25. #include <linux/utsname.h>
  26. #include <linux/smp.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/sem.h>
  29. #include <linux/msg.h>
  30. #include <linux/shm.h>
  31. #include <linux/slab.h>
  32. #include <linux/uio.h>
  33. #include <linux/nfs_fs.h>
  34. #include <linux/quota.h>
  35. #include <linux/module.h>
  36. #include <linux/sunrpc/svc.h>
  37. #include <linux/nfsd/nfsd.h>
  38. #include <linux/nfsd/cache.h>
  39. #include <linux/nfsd/xdr.h>
  40. #include <linux/nfsd/syscall.h>
  41. #include <linux/poll.h>
  42. #include <linux/personality.h>
  43. #include <linux/stat.h>
  44. #include <linux/filter.h>
  45. #include <linux/highmem.h>
  46. #include <linux/highuid.h>
  47. #include <linux/mman.h>
  48. #include <linux/ipv6.h>
  49. #include <linux/in.h>
  50. #include <linux/icmpv6.h>
  51. #include <linux/syscalls.h>
  52. #include <linux/sysctl.h>
  53. #include <linux/binfmts.h>
  54. #include <linux/capability.h>
  55. #include <linux/compat.h>
  56. #include <linux/vfs.h>
  57. #include <linux/ptrace.h>
  58. #include <linux/fadvise.h>
  59. #include <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 __user * filename, u16 user, u16 group)
  92. {
  93. return sys_chown(filename, low2highuid(user), low2highgid(group));
  94. }
  95. asmlinkage long sys32_lchown16(const char __user * 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 __user *ruid, u16 __user *euid, u16 __user *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 __user *rgid, u16 __user *egid, u16 __user *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 __user *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 __user *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 __user *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 __user *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 __user *i)
  234. {
  235. return (!access_ok(VERIFY_READ, o, sizeof(*o)) ||
  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 __user *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. #ifdef CONFIG_SYSVIPC
  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. #endif
  292. asmlinkage long sys32_truncate64(const char __user * path, unsigned long high, unsigned long low)
  293. {
  294. if ((int)high < 0)
  295. return -EINVAL;
  296. else
  297. return sys_truncate(path, (high << 32) | low);
  298. }
  299. asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
  300. {
  301. if ((int)high < 0)
  302. return -EINVAL;
  303. else
  304. return sys_ftruncate(fd, (high << 32) | low);
  305. }
  306. int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
  307. {
  308. compat_ino_t ino;
  309. int err;
  310. if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
  311. return -EOVERFLOW;
  312. ino = stat->ino;
  313. if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
  314. return -EOVERFLOW;
  315. err = put_user(old_encode_dev(stat->dev), &statbuf->st_dev);
  316. err |= put_user(stat->ino, &statbuf->st_ino);
  317. err |= put_user(stat->mode, &statbuf->st_mode);
  318. err |= put_user(stat->nlink, &statbuf->st_nlink);
  319. err |= put_user(high2lowuid(stat->uid), &statbuf->st_uid);
  320. err |= put_user(high2lowgid(stat->gid), &statbuf->st_gid);
  321. err |= put_user(old_encode_dev(stat->rdev), &statbuf->st_rdev);
  322. err |= put_user(stat->size, &statbuf->st_size);
  323. err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
  324. err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
  325. err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
  326. err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
  327. err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
  328. err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
  329. err |= put_user(stat->blksize, &statbuf->st_blksize);
  330. err |= put_user(stat->blocks, &statbuf->st_blocks);
  331. /* fixme
  332. err |= put_user(0, &statbuf->__unused4[0]);
  333. err |= put_user(0, &statbuf->__unused4[1]);
  334. */
  335. return err;
  336. }
  337. struct sysinfo32 {
  338. s32 uptime;
  339. u32 loads[3];
  340. u32 totalram;
  341. u32 freeram;
  342. u32 sharedram;
  343. u32 bufferram;
  344. u32 totalswap;
  345. u32 freeswap;
  346. unsigned short procs;
  347. unsigned short pads;
  348. u32 totalhigh;
  349. u32 freehigh;
  350. unsigned int mem_unit;
  351. char _f[8];
  352. };
  353. asmlinkage long sys32_sysinfo(struct sysinfo32 __user *info)
  354. {
  355. struct sysinfo s;
  356. int ret, err;
  357. mm_segment_t old_fs = get_fs ();
  358. set_fs (KERNEL_DS);
  359. ret = sys_sysinfo((struct sysinfo __user *) &s);
  360. set_fs (old_fs);
  361. err = put_user (s.uptime, &info->uptime);
  362. err |= __put_user (s.loads[0], &info->loads[0]);
  363. err |= __put_user (s.loads[1], &info->loads[1]);
  364. err |= __put_user (s.loads[2], &info->loads[2]);
  365. err |= __put_user (s.totalram, &info->totalram);
  366. err |= __put_user (s.freeram, &info->freeram);
  367. err |= __put_user (s.sharedram, &info->sharedram);
  368. err |= __put_user (s.bufferram, &info->bufferram);
  369. err |= __put_user (s.totalswap, &info->totalswap);
  370. err |= __put_user (s.freeswap, &info->freeswap);
  371. err |= __put_user (s.procs, &info->procs);
  372. err |= __put_user (s.totalhigh, &info->totalhigh);
  373. err |= __put_user (s.freehigh, &info->freehigh);
  374. err |= __put_user (s.mem_unit, &info->mem_unit);
  375. if (err)
  376. return -EFAULT;
  377. return ret;
  378. }
  379. asmlinkage long sys32_sched_rr_get_interval(compat_pid_t pid,
  380. struct compat_timespec __user *interval)
  381. {
  382. struct timespec t;
  383. int ret;
  384. mm_segment_t old_fs = get_fs ();
  385. set_fs (KERNEL_DS);
  386. ret = sys_sched_rr_get_interval(pid, (struct timespec __user *) &t);
  387. set_fs (old_fs);
  388. if (put_compat_timespec(&t, interval))
  389. return -EFAULT;
  390. return ret;
  391. }
  392. asmlinkage long sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
  393. compat_sigset_t __user *oset, size_t sigsetsize)
  394. {
  395. sigset_t s;
  396. compat_sigset_t s32;
  397. int ret;
  398. mm_segment_t old_fs = get_fs();
  399. if (set) {
  400. if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
  401. return -EFAULT;
  402. switch (_NSIG_WORDS) {
  403. case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
  404. case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
  405. case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
  406. case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  407. }
  408. }
  409. set_fs (KERNEL_DS);
  410. ret = sys_rt_sigprocmask(how,
  411. set ? (sigset_t __user *) &s : NULL,
  412. oset ? (sigset_t __user *) &s : NULL,
  413. sigsetsize);
  414. set_fs (old_fs);
  415. if (ret) return ret;
  416. if (oset) {
  417. switch (_NSIG_WORDS) {
  418. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  419. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  420. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  421. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  422. }
  423. if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
  424. return -EFAULT;
  425. }
  426. return 0;
  427. }
  428. asmlinkage long sys32_rt_sigpending(compat_sigset_t __user *set,
  429. size_t sigsetsize)
  430. {
  431. sigset_t s;
  432. compat_sigset_t s32;
  433. int ret;
  434. mm_segment_t old_fs = get_fs();
  435. set_fs (KERNEL_DS);
  436. ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
  437. set_fs (old_fs);
  438. if (!ret) {
  439. switch (_NSIG_WORDS) {
  440. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  441. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  442. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  443. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  444. }
  445. if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
  446. return -EFAULT;
  447. }
  448. return ret;
  449. }
  450. asmlinkage long
  451. sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
  452. {
  453. siginfo_t info;
  454. int ret;
  455. mm_segment_t old_fs = get_fs();
  456. if (copy_siginfo_from_user32(&info, uinfo))
  457. return -EFAULT;
  458. set_fs (KERNEL_DS);
  459. ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *) &info);
  460. set_fs (old_fs);
  461. return ret;
  462. }
  463. /*
  464. * sys32_execve() executes a new program after the asm stub has set
  465. * things up for us. This should basically do what I want it to.
  466. */
  467. asmlinkage long
  468. sys32_execve(struct pt_regs regs)
  469. {
  470. int error;
  471. char * filename;
  472. filename = getname(compat_ptr(regs.orig_gpr2));
  473. error = PTR_ERR(filename);
  474. if (IS_ERR(filename))
  475. goto out;
  476. error = compat_do_execve(filename, compat_ptr(regs.gprs[3]),
  477. compat_ptr(regs.gprs[4]), &regs);
  478. if (error == 0)
  479. {
  480. task_lock(current);
  481. current->ptrace &= ~PT_DTRACE;
  482. task_unlock(current);
  483. current->thread.fp_regs.fpc=0;
  484. asm volatile("sfpc %0,0" : : "d" (0));
  485. }
  486. putname(filename);
  487. out:
  488. return error;
  489. }
  490. #ifdef CONFIG_MODULES
  491. asmlinkage long
  492. sys32_init_module(void __user *umod, unsigned long len,
  493. const char __user *uargs)
  494. {
  495. return sys_init_module(umod, len, uargs);
  496. }
  497. asmlinkage long
  498. sys32_delete_module(const char __user *name_user, unsigned int flags)
  499. {
  500. return sys_delete_module(name_user, flags);
  501. }
  502. #else /* CONFIG_MODULES */
  503. asmlinkage long
  504. sys32_init_module(void __user *umod, unsigned long len,
  505. const char __user *uargs)
  506. {
  507. return -ENOSYS;
  508. }
  509. asmlinkage long
  510. sys32_delete_module(const char __user *name_user, unsigned int flags)
  511. {
  512. return -ENOSYS;
  513. }
  514. #endif /* CONFIG_MODULES */
  515. /* Translations due to time_t size differences. Which affects all
  516. sorts of things, like timeval and itimerval. */
  517. extern struct timezone sys_tz;
  518. asmlinkage long sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  519. {
  520. if (tv) {
  521. struct timeval ktv;
  522. do_gettimeofday(&ktv);
  523. if (put_tv32(tv, &ktv))
  524. return -EFAULT;
  525. }
  526. if (tz) {
  527. if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  528. return -EFAULT;
  529. }
  530. return 0;
  531. }
  532. static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
  533. {
  534. long usec;
  535. if (!access_ok(VERIFY_READ, i, sizeof(*i)))
  536. return -EFAULT;
  537. if (__get_user(o->tv_sec, &i->tv_sec))
  538. return -EFAULT;
  539. if (__get_user(usec, &i->tv_usec))
  540. return -EFAULT;
  541. o->tv_nsec = usec * 1000;
  542. return 0;
  543. }
  544. asmlinkage long sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  545. {
  546. struct timespec kts;
  547. struct timezone ktz;
  548. if (tv) {
  549. if (get_ts32(&kts, tv))
  550. return -EFAULT;
  551. }
  552. if (tz) {
  553. if (copy_from_user(&ktz, tz, sizeof(ktz)))
  554. return -EFAULT;
  555. }
  556. return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
  557. }
  558. /* These are here just in case some old sparc32 binary calls it. */
  559. asmlinkage long sys32_pause(void)
  560. {
  561. current->state = TASK_INTERRUPTIBLE;
  562. schedule();
  563. return -ERESTARTNOHAND;
  564. }
  565. asmlinkage long sys32_pread64(unsigned int fd, char __user *ubuf,
  566. size_t count, u32 poshi, u32 poslo)
  567. {
  568. if ((compat_ssize_t) count < 0)
  569. return -EINVAL;
  570. return sys_pread64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  571. }
  572. asmlinkage long sys32_pwrite64(unsigned int fd, const char __user *ubuf,
  573. size_t count, u32 poshi, u32 poslo)
  574. {
  575. if ((compat_ssize_t) count < 0)
  576. return -EINVAL;
  577. return sys_pwrite64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  578. }
  579. asmlinkage compat_ssize_t sys32_readahead(int fd, u32 offhi, u32 offlo, s32 count)
  580. {
  581. return sys_readahead(fd, ((loff_t)AA(offhi) << 32) | AA(offlo), count);
  582. }
  583. asmlinkage long sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, size_t count)
  584. {
  585. mm_segment_t old_fs = get_fs();
  586. int ret;
  587. off_t of;
  588. if (offset && get_user(of, offset))
  589. return -EFAULT;
  590. set_fs(KERNEL_DS);
  591. ret = sys_sendfile(out_fd, in_fd,
  592. offset ? (off_t __user *) &of : NULL, count);
  593. set_fs(old_fs);
  594. if (offset && put_user(of, offset))
  595. return -EFAULT;
  596. return ret;
  597. }
  598. asmlinkage long sys32_sendfile64(int out_fd, int in_fd,
  599. compat_loff_t __user *offset, s32 count)
  600. {
  601. mm_segment_t old_fs = get_fs();
  602. int ret;
  603. loff_t lof;
  604. if (offset && get_user(lof, offset))
  605. return -EFAULT;
  606. set_fs(KERNEL_DS);
  607. ret = sys_sendfile64(out_fd, in_fd,
  608. offset ? (loff_t __user *) &lof : NULL, count);
  609. set_fs(old_fs);
  610. if (offset && put_user(lof, offset))
  611. return -EFAULT;
  612. return ret;
  613. }
  614. #ifdef CONFIG_SYSCTL_SYSCALL
  615. struct __sysctl_args32 {
  616. u32 name;
  617. int nlen;
  618. u32 oldval;
  619. u32 oldlenp;
  620. u32 newval;
  621. u32 newlen;
  622. u32 __unused[4];
  623. };
  624. asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
  625. {
  626. struct __sysctl_args32 tmp;
  627. int error;
  628. size_t oldlen;
  629. size_t __user *oldlenp = NULL;
  630. unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
  631. if (copy_from_user(&tmp, args, sizeof(tmp)))
  632. return -EFAULT;
  633. if (tmp.oldval && tmp.oldlenp) {
  634. /* Duh, this is ugly and might not work if sysctl_args
  635. is in read-only memory, but do_sysctl does indirectly
  636. a lot of uaccess in both directions and we'd have to
  637. basically copy the whole sysctl.c here, and
  638. glibc's __sysctl uses rw memory for the structure
  639. anyway. */
  640. if (get_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)) ||
  641. put_user(oldlen, (size_t __user *)addr))
  642. return -EFAULT;
  643. oldlenp = (size_t __user *)addr;
  644. }
  645. lock_kernel();
  646. error = do_sysctl(compat_ptr(tmp.name), tmp.nlen, compat_ptr(tmp.oldval),
  647. oldlenp, compat_ptr(tmp.newval), tmp.newlen);
  648. unlock_kernel();
  649. if (oldlenp) {
  650. if (!error) {
  651. if (get_user(oldlen, (size_t __user *)addr) ||
  652. put_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)))
  653. error = -EFAULT;
  654. }
  655. copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
  656. }
  657. return error;
  658. }
  659. #endif
  660. struct stat64_emu31 {
  661. unsigned long long st_dev;
  662. unsigned int __pad1;
  663. #define STAT64_HAS_BROKEN_ST_INO 1
  664. u32 __st_ino;
  665. unsigned int st_mode;
  666. unsigned int st_nlink;
  667. u32 st_uid;
  668. u32 st_gid;
  669. unsigned long long st_rdev;
  670. unsigned int __pad3;
  671. long st_size;
  672. u32 st_blksize;
  673. unsigned char __pad4[4];
  674. u32 __pad5; /* future possible st_blocks high bits */
  675. u32 st_blocks; /* Number 512-byte blocks allocated. */
  676. u32 st_atime;
  677. u32 __pad6;
  678. u32 st_mtime;
  679. u32 __pad7;
  680. u32 st_ctime;
  681. u32 __pad8; /* will be high 32 bits of ctime someday */
  682. unsigned long st_ino;
  683. };
  684. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  685. {
  686. struct stat64_emu31 tmp;
  687. memset(&tmp, 0, sizeof(tmp));
  688. tmp.st_dev = huge_encode_dev(stat->dev);
  689. tmp.st_ino = stat->ino;
  690. tmp.__st_ino = (u32)stat->ino;
  691. tmp.st_mode = stat->mode;
  692. tmp.st_nlink = (unsigned int)stat->nlink;
  693. tmp.st_uid = stat->uid;
  694. tmp.st_gid = stat->gid;
  695. tmp.st_rdev = huge_encode_dev(stat->rdev);
  696. tmp.st_size = stat->size;
  697. tmp.st_blksize = (u32)stat->blksize;
  698. tmp.st_blocks = (u32)stat->blocks;
  699. tmp.st_atime = (u32)stat->atime.tv_sec;
  700. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  701. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  702. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  703. }
  704. asmlinkage long sys32_stat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  705. {
  706. struct kstat stat;
  707. int ret = vfs_stat(filename, &stat);
  708. if (!ret)
  709. ret = cp_stat64(statbuf, &stat);
  710. return ret;
  711. }
  712. asmlinkage long sys32_lstat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  713. {
  714. struct kstat stat;
  715. int ret = vfs_lstat(filename, &stat);
  716. if (!ret)
  717. ret = cp_stat64(statbuf, &stat);
  718. return ret;
  719. }
  720. asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 __user * statbuf)
  721. {
  722. struct kstat stat;
  723. int ret = vfs_fstat(fd, &stat);
  724. if (!ret)
  725. ret = cp_stat64(statbuf, &stat);
  726. return ret;
  727. }
  728. asmlinkage long sys32_fstatat64(unsigned int dfd, char __user *filename,
  729. struct stat64_emu31 __user* statbuf, int flag)
  730. {
  731. struct kstat stat;
  732. int error = -EINVAL;
  733. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  734. goto out;
  735. if (flag & AT_SYMLINK_NOFOLLOW)
  736. error = vfs_lstat_fd(dfd, filename, &stat);
  737. else
  738. error = vfs_stat_fd(dfd, filename, &stat);
  739. if (!error)
  740. error = cp_stat64(statbuf, &stat);
  741. out:
  742. return error;
  743. }
  744. /*
  745. * Linux/i386 didn't use to be able to handle more than
  746. * 4 system call parameters, so these system calls used a memory
  747. * block for parameter passing..
  748. */
  749. struct mmap_arg_struct_emu31 {
  750. u32 addr;
  751. u32 len;
  752. u32 prot;
  753. u32 flags;
  754. u32 fd;
  755. u32 offset;
  756. };
  757. /* common code for old and new mmaps */
  758. static inline long do_mmap2(
  759. unsigned long addr, unsigned long len,
  760. unsigned long prot, unsigned long flags,
  761. unsigned long fd, unsigned long pgoff)
  762. {
  763. struct file * file = NULL;
  764. unsigned long error = -EBADF;
  765. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  766. if (!(flags & MAP_ANONYMOUS)) {
  767. file = fget(fd);
  768. if (!file)
  769. goto out;
  770. }
  771. down_write(&current->mm->mmap_sem);
  772. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  773. if (!IS_ERR((void *) error) && error + len >= 0x80000000ULL) {
  774. /* Result is out of bounds. */
  775. do_munmap(current->mm, addr, len);
  776. error = -ENOMEM;
  777. }
  778. up_write(&current->mm->mmap_sem);
  779. if (file)
  780. fput(file);
  781. out:
  782. return error;
  783. }
  784. asmlinkage unsigned long
  785. old32_mmap(struct mmap_arg_struct_emu31 __user *arg)
  786. {
  787. struct mmap_arg_struct_emu31 a;
  788. int error = -EFAULT;
  789. if (copy_from_user(&a, arg, sizeof(a)))
  790. goto out;
  791. error = -EINVAL;
  792. if (a.offset & ~PAGE_MASK)
  793. goto out;
  794. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
  795. out:
  796. return error;
  797. }
  798. asmlinkage long
  799. sys32_mmap2(struct mmap_arg_struct_emu31 __user *arg)
  800. {
  801. struct mmap_arg_struct_emu31 a;
  802. int error = -EFAULT;
  803. if (copy_from_user(&a, arg, sizeof(a)))
  804. goto out;
  805. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  806. out:
  807. return error;
  808. }
  809. asmlinkage long sys32_read(unsigned int fd, char __user * buf, size_t count)
  810. {
  811. if ((compat_ssize_t) count < 0)
  812. return -EINVAL;
  813. return sys_read(fd, buf, count);
  814. }
  815. asmlinkage long sys32_write(unsigned int fd, char __user * buf, size_t count)
  816. {
  817. if ((compat_ssize_t) count < 0)
  818. return -EINVAL;
  819. return sys_write(fd, buf, count);
  820. }
  821. asmlinkage long sys32_clone(struct pt_regs regs)
  822. {
  823. unsigned long clone_flags;
  824. unsigned long newsp;
  825. int __user *parent_tidptr, *child_tidptr;
  826. clone_flags = regs.gprs[3] & 0xffffffffUL;
  827. newsp = regs.orig_gpr2 & 0x7fffffffUL;
  828. parent_tidptr = compat_ptr(regs.gprs[4]);
  829. child_tidptr = compat_ptr(regs.gprs[5]);
  830. if (!newsp)
  831. newsp = regs.gprs[15];
  832. return do_fork(clone_flags, newsp, &regs, 0,
  833. parent_tidptr, child_tidptr);
  834. }
  835. /*
  836. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  837. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  838. * because the 31 bit values differ from the 64 bit values.
  839. */
  840. asmlinkage long
  841. sys32_fadvise64(int fd, loff_t offset, size_t len, int advise)
  842. {
  843. if (advise == 4)
  844. advise = POSIX_FADV_DONTNEED;
  845. else if (advise == 5)
  846. advise = POSIX_FADV_NOREUSE;
  847. return sys_fadvise64(fd, offset, len, advise);
  848. }
  849. struct fadvise64_64_args {
  850. int fd;
  851. long long offset;
  852. long long len;
  853. int advice;
  854. };
  855. asmlinkage long
  856. sys32_fadvise64_64(struct fadvise64_64_args __user *args)
  857. {
  858. struct fadvise64_64_args a;
  859. if ( copy_from_user(&a, args, sizeof(a)) )
  860. return -EFAULT;
  861. if (a.advice == 4)
  862. a.advice = POSIX_FADV_DONTNEED;
  863. else if (a.advice == 5)
  864. a.advice = POSIX_FADV_NOREUSE;
  865. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  866. }