compat_linux.c 26 KB

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