sys_ia32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
  3. * sys_sparc32
  4. *
  5. * Copyright (C) 2000 VA Linux Co
  6. * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
  7. * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
  8. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  9. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  10. * Copyright (C) 2000 Hewlett-Packard Co.
  11. * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
  12. * Copyright (C) 2000,2001,2002 Andi Kleen, SuSE Labs (x86-64 port)
  13. *
  14. * These routines maintain argument size conversion between 32bit and 64bit
  15. * environment. In 2.5 most of this should be moved to a generic directory.
  16. *
  17. * This file assumes that there is a hole at the end of user address space.
  18. *
  19. * Some of the functions are LE specific currently. These are
  20. * hopefully all marked. This should be fixed.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/fs.h>
  25. #include <linux/file.h>
  26. #include <linux/signal.h>
  27. #include <linux/syscalls.h>
  28. #include <linux/times.h>
  29. #include <linux/utsname.h>
  30. #include <linux/mm.h>
  31. #include <linux/uio.h>
  32. #include <linux/poll.h>
  33. #include <linux/personality.h>
  34. #include <linux/stat.h>
  35. #include <linux/rwsem.h>
  36. #include <linux/compat.h>
  37. #include <linux/vfs.h>
  38. #include <linux/ptrace.h>
  39. #include <linux/highuid.h>
  40. #include <linux/sysctl.h>
  41. #include <linux/slab.h>
  42. #include <asm/mman.h>
  43. #include <asm/types.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/atomic.h>
  46. #include <asm/vgtod.h>
  47. #include <asm/sys_ia32.h>
  48. #define AA(__x) ((unsigned long)(__x))
  49. asmlinkage long sys32_truncate64(const char __user *filename,
  50. unsigned long offset_low,
  51. unsigned long offset_high)
  52. {
  53. return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
  54. }
  55. asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long offset_low,
  56. unsigned long offset_high)
  57. {
  58. return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
  59. }
  60. /*
  61. * Another set for IA32/LFS -- x86_64 struct stat is different due to
  62. * support for 64bit inode numbers.
  63. */
  64. static int cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
  65. {
  66. typeof(ubuf->st_uid) uid = 0;
  67. typeof(ubuf->st_gid) gid = 0;
  68. SET_UID(uid, stat->uid);
  69. SET_GID(gid, stat->gid);
  70. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
  71. __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
  72. __put_user(stat->ino, &ubuf->__st_ino) ||
  73. __put_user(stat->ino, &ubuf->st_ino) ||
  74. __put_user(stat->mode, &ubuf->st_mode) ||
  75. __put_user(stat->nlink, &ubuf->st_nlink) ||
  76. __put_user(uid, &ubuf->st_uid) ||
  77. __put_user(gid, &ubuf->st_gid) ||
  78. __put_user(huge_encode_dev(stat->rdev), &ubuf->st_rdev) ||
  79. __put_user(stat->size, &ubuf->st_size) ||
  80. __put_user(stat->atime.tv_sec, &ubuf->st_atime) ||
  81. __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec) ||
  82. __put_user(stat->mtime.tv_sec, &ubuf->st_mtime) ||
  83. __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
  84. __put_user(stat->ctime.tv_sec, &ubuf->st_ctime) ||
  85. __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
  86. __put_user(stat->blksize, &ubuf->st_blksize) ||
  87. __put_user(stat->blocks, &ubuf->st_blocks))
  88. return -EFAULT;
  89. return 0;
  90. }
  91. asmlinkage long sys32_stat64(const char __user *filename,
  92. struct stat64 __user *statbuf)
  93. {
  94. struct kstat stat;
  95. int ret = vfs_stat(filename, &stat);
  96. if (!ret)
  97. ret = cp_stat64(statbuf, &stat);
  98. return ret;
  99. }
  100. asmlinkage long sys32_lstat64(const char __user *filename,
  101. struct stat64 __user *statbuf)
  102. {
  103. struct kstat stat;
  104. int ret = vfs_lstat(filename, &stat);
  105. if (!ret)
  106. ret = cp_stat64(statbuf, &stat);
  107. return ret;
  108. }
  109. asmlinkage long sys32_fstat64(unsigned int fd, struct stat64 __user *statbuf)
  110. {
  111. struct kstat stat;
  112. int ret = vfs_fstat(fd, &stat);
  113. if (!ret)
  114. ret = cp_stat64(statbuf, &stat);
  115. return ret;
  116. }
  117. asmlinkage long sys32_fstatat(unsigned int dfd, const char __user *filename,
  118. struct stat64 __user *statbuf, int flag)
  119. {
  120. struct kstat stat;
  121. int error;
  122. error = vfs_fstatat(dfd, filename, &stat, flag);
  123. if (error)
  124. return error;
  125. return cp_stat64(statbuf, &stat);
  126. }
  127. /*
  128. * Linux/i386 didn't use to be able to handle more than
  129. * 4 system call parameters, so these system calls used a memory
  130. * block for parameter passing..
  131. */
  132. struct mmap_arg_struct32 {
  133. unsigned int addr;
  134. unsigned int len;
  135. unsigned int prot;
  136. unsigned int flags;
  137. unsigned int fd;
  138. unsigned int offset;
  139. };
  140. asmlinkage long sys32_mmap(struct mmap_arg_struct32 __user *arg)
  141. {
  142. struct mmap_arg_struct32 a;
  143. if (copy_from_user(&a, arg, sizeof(a)))
  144. return -EFAULT;
  145. if (a.offset & ~PAGE_MASK)
  146. return -EINVAL;
  147. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  148. a.offset>>PAGE_SHIFT);
  149. }
  150. asmlinkage long sys32_mprotect(unsigned long start, size_t len,
  151. unsigned long prot)
  152. {
  153. return sys_mprotect(start, len, prot);
  154. }
  155. asmlinkage long sys32_rt_sigaction(int sig, struct sigaction32 __user *act,
  156. struct sigaction32 __user *oact,
  157. unsigned int sigsetsize)
  158. {
  159. struct k_sigaction new_ka, old_ka;
  160. int ret;
  161. compat_sigset_t set32;
  162. /* XXX: Don't preclude handling different sized sigset_t's. */
  163. if (sigsetsize != sizeof(compat_sigset_t))
  164. return -EINVAL;
  165. if (act) {
  166. compat_uptr_t handler, restorer;
  167. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  168. __get_user(handler, &act->sa_handler) ||
  169. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  170. __get_user(restorer, &act->sa_restorer) ||
  171. __copy_from_user(&set32, &act->sa_mask,
  172. sizeof(compat_sigset_t)))
  173. return -EFAULT;
  174. new_ka.sa.sa_handler = compat_ptr(handler);
  175. new_ka.sa.sa_restorer = compat_ptr(restorer);
  176. /*
  177. * FIXME: here we rely on _COMPAT_NSIG_WORS to be >=
  178. * than _NSIG_WORDS << 1
  179. */
  180. switch (_NSIG_WORDS) {
  181. case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
  182. | (((long)set32.sig[7]) << 32);
  183. case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
  184. | (((long)set32.sig[5]) << 32);
  185. case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
  186. | (((long)set32.sig[3]) << 32);
  187. case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
  188. | (((long)set32.sig[1]) << 32);
  189. }
  190. }
  191. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  192. if (!ret && oact) {
  193. /*
  194. * FIXME: here we rely on _COMPAT_NSIG_WORS to be >=
  195. * than _NSIG_WORDS << 1
  196. */
  197. switch (_NSIG_WORDS) {
  198. case 4:
  199. set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
  200. set32.sig[6] = old_ka.sa.sa_mask.sig[3];
  201. case 3:
  202. set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
  203. set32.sig[4] = old_ka.sa.sa_mask.sig[2];
  204. case 2:
  205. set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
  206. set32.sig[2] = old_ka.sa.sa_mask.sig[1];
  207. case 1:
  208. set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
  209. set32.sig[0] = old_ka.sa.sa_mask.sig[0];
  210. }
  211. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  212. __put_user(ptr_to_compat(old_ka.sa.sa_handler),
  213. &oact->sa_handler) ||
  214. __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
  215. &oact->sa_restorer) ||
  216. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  217. __copy_to_user(&oact->sa_mask, &set32,
  218. sizeof(compat_sigset_t)))
  219. return -EFAULT;
  220. }
  221. return ret;
  222. }
  223. asmlinkage long sys32_sigaction(int sig, struct old_sigaction32 __user *act,
  224. struct old_sigaction32 __user *oact)
  225. {
  226. struct k_sigaction new_ka, old_ka;
  227. int ret;
  228. if (act) {
  229. compat_old_sigset_t mask;
  230. compat_uptr_t handler, restorer;
  231. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  232. __get_user(handler, &act->sa_handler) ||
  233. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  234. __get_user(restorer, &act->sa_restorer) ||
  235. __get_user(mask, &act->sa_mask))
  236. return -EFAULT;
  237. new_ka.sa.sa_handler = compat_ptr(handler);
  238. new_ka.sa.sa_restorer = compat_ptr(restorer);
  239. siginitset(&new_ka.sa.sa_mask, mask);
  240. }
  241. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  242. if (!ret && oact) {
  243. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  244. __put_user(ptr_to_compat(old_ka.sa.sa_handler),
  245. &oact->sa_handler) ||
  246. __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
  247. &oact->sa_restorer) ||
  248. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  249. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  250. return -EFAULT;
  251. }
  252. return ret;
  253. }
  254. asmlinkage long sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
  255. compat_sigset_t __user *oset,
  256. unsigned int sigsetsize)
  257. {
  258. sigset_t s;
  259. compat_sigset_t s32;
  260. int ret;
  261. mm_segment_t old_fs = get_fs();
  262. if (set) {
  263. if (copy_from_user(&s32, set, sizeof(compat_sigset_t)))
  264. return -EFAULT;
  265. switch (_NSIG_WORDS) {
  266. case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
  267. case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
  268. case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
  269. case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  270. }
  271. }
  272. set_fs(KERNEL_DS);
  273. ret = sys_rt_sigprocmask(how,
  274. set ? (sigset_t __user *)&s : NULL,
  275. oset ? (sigset_t __user *)&s : NULL,
  276. sigsetsize);
  277. set_fs(old_fs);
  278. if (ret)
  279. return ret;
  280. if (oset) {
  281. switch (_NSIG_WORDS) {
  282. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  283. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  284. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  285. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  286. }
  287. if (copy_to_user(oset, &s32, sizeof(compat_sigset_t)))
  288. return -EFAULT;
  289. }
  290. return 0;
  291. }
  292. asmlinkage long sys32_alarm(unsigned int seconds)
  293. {
  294. return alarm_setitimer(seconds);
  295. }
  296. asmlinkage long sys32_waitpid(compat_pid_t pid, unsigned int *stat_addr,
  297. int options)
  298. {
  299. return compat_sys_wait4(pid, stat_addr, options, NULL);
  300. }
  301. /* 32-bit timeval and related flotsam. */
  302. asmlinkage long sys32_sysfs(int option, u32 arg1, u32 arg2)
  303. {
  304. return sys_sysfs(option, arg1, arg2);
  305. }
  306. asmlinkage long sys32_sched_rr_get_interval(compat_pid_t pid,
  307. struct compat_timespec __user *interval)
  308. {
  309. struct timespec t;
  310. int ret;
  311. mm_segment_t old_fs = get_fs();
  312. set_fs(KERNEL_DS);
  313. ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
  314. set_fs(old_fs);
  315. if (put_compat_timespec(&t, interval))
  316. return -EFAULT;
  317. return ret;
  318. }
  319. asmlinkage long sys32_rt_sigpending(compat_sigset_t __user *set,
  320. compat_size_t sigsetsize)
  321. {
  322. sigset_t s;
  323. compat_sigset_t s32;
  324. int ret;
  325. mm_segment_t old_fs = get_fs();
  326. set_fs(KERNEL_DS);
  327. ret = sys_rt_sigpending((sigset_t __user *)&s, sigsetsize);
  328. set_fs(old_fs);
  329. if (!ret) {
  330. switch (_NSIG_WORDS) {
  331. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  332. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  333. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  334. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  335. }
  336. if (copy_to_user(set, &s32, sizeof(compat_sigset_t)))
  337. return -EFAULT;
  338. }
  339. return ret;
  340. }
  341. asmlinkage long sys32_rt_sigqueueinfo(int pid, int sig,
  342. compat_siginfo_t __user *uinfo)
  343. {
  344. siginfo_t info;
  345. int ret;
  346. mm_segment_t old_fs = get_fs();
  347. if (copy_siginfo_from_user32(&info, uinfo))
  348. return -EFAULT;
  349. set_fs(KERNEL_DS);
  350. ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
  351. set_fs(old_fs);
  352. return ret;
  353. }
  354. /* warning: next two assume little endian */
  355. asmlinkage long sys32_pread(unsigned int fd, char __user *ubuf, u32 count,
  356. u32 poslo, u32 poshi)
  357. {
  358. return sys_pread64(fd, ubuf, count,
  359. ((loff_t)AA(poshi) << 32) | AA(poslo));
  360. }
  361. asmlinkage long sys32_pwrite(unsigned int fd, const char __user *ubuf,
  362. u32 count, u32 poslo, u32 poshi)
  363. {
  364. return sys_pwrite64(fd, ubuf, count,
  365. ((loff_t)AA(poshi) << 32) | AA(poslo));
  366. }
  367. asmlinkage long sys32_personality(unsigned long personality)
  368. {
  369. int ret;
  370. if (personality(current->personality) == PER_LINUX32 &&
  371. personality == PER_LINUX)
  372. personality = PER_LINUX32;
  373. ret = sys_personality(personality);
  374. if (ret == PER_LINUX32)
  375. ret = PER_LINUX;
  376. return ret;
  377. }
  378. asmlinkage long sys32_sendfile(int out_fd, int in_fd,
  379. compat_off_t __user *offset, s32 count)
  380. {
  381. mm_segment_t old_fs = get_fs();
  382. int ret;
  383. off_t of;
  384. if (offset && get_user(of, offset))
  385. return -EFAULT;
  386. set_fs(KERNEL_DS);
  387. ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL,
  388. count);
  389. set_fs(old_fs);
  390. if (offset && put_user(of, offset))
  391. return -EFAULT;
  392. return ret;
  393. }
  394. asmlinkage long sys32_execve(const char __user *name, compat_uptr_t __user *argv,
  395. compat_uptr_t __user *envp, struct pt_regs *regs)
  396. {
  397. long error;
  398. char *filename;
  399. filename = getname(name);
  400. error = PTR_ERR(filename);
  401. if (IS_ERR(filename))
  402. return error;
  403. error = compat_do_execve(filename, argv, envp, regs);
  404. putname(filename);
  405. return error;
  406. }
  407. asmlinkage long sys32_clone(unsigned int clone_flags, unsigned int newsp,
  408. struct pt_regs *regs)
  409. {
  410. void __user *parent_tid = (void __user *)regs->dx;
  411. void __user *child_tid = (void __user *)regs->di;
  412. if (!newsp)
  413. newsp = regs->sp;
  414. return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
  415. }
  416. /*
  417. * Some system calls that need sign extended arguments. This could be
  418. * done by a generic wrapper.
  419. */
  420. long sys32_lseek(unsigned int fd, int offset, unsigned int whence)
  421. {
  422. return sys_lseek(fd, offset, whence);
  423. }
  424. long sys32_kill(int pid, int sig)
  425. {
  426. return sys_kill(pid, sig);
  427. }
  428. long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
  429. __u32 len_low, __u32 len_high, int advice)
  430. {
  431. return sys_fadvise64_64(fd,
  432. (((u64)offset_high)<<32) | offset_low,
  433. (((u64)len_high)<<32) | len_low,
  434. advice);
  435. }
  436. long sys32_vm86_warning(void)
  437. {
  438. struct task_struct *me = current;
  439. static char lastcomm[sizeof(me->comm)];
  440. if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
  441. compat_printk(KERN_INFO
  442. "%s: vm86 mode not supported on 64 bit kernel\n",
  443. me->comm);
  444. strncpy(lastcomm, me->comm, sizeof(lastcomm));
  445. }
  446. return -ENOSYS;
  447. }
  448. long sys32_lookup_dcookie(u32 addr_low, u32 addr_high,
  449. char __user *buf, size_t len)
  450. {
  451. return sys_lookup_dcookie(((u64)addr_high << 32) | addr_low, buf, len);
  452. }
  453. asmlinkage ssize_t sys32_readahead(int fd, unsigned off_lo, unsigned off_hi,
  454. size_t count)
  455. {
  456. return sys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
  457. }
  458. asmlinkage long sys32_sync_file_range(int fd, unsigned off_low, unsigned off_hi,
  459. unsigned n_low, unsigned n_hi, int flags)
  460. {
  461. return sys_sync_file_range(fd,
  462. ((u64)off_hi << 32) | off_low,
  463. ((u64)n_hi << 32) | n_low, flags);
  464. }
  465. asmlinkage long sys32_fadvise64(int fd, unsigned offset_lo, unsigned offset_hi,
  466. size_t len, int advice)
  467. {
  468. return sys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
  469. len, advice);
  470. }
  471. asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_lo,
  472. unsigned offset_hi, unsigned len_lo,
  473. unsigned len_hi)
  474. {
  475. return sys_fallocate(fd, mode, ((u64)offset_hi << 32) | offset_lo,
  476. ((u64)len_hi << 32) | len_lo);
  477. }
  478. asmlinkage long sys32_fanotify_mark(int fanotify_fd, unsigned int flags,
  479. u32 mask_lo, u32 mask_hi,
  480. int fd, const char __user *pathname)
  481. {
  482. return sys_fanotify_mark(fanotify_fd, flags,
  483. ((u64)mask_hi << 32) | mask_lo,
  484. fd, pathname);
  485. }