compat_linux.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S390 version
  4. * Copyright IBM Corp. 2000
  5. * Author(s): Martin Schwidefsky ([email protected]),
  6. * Gerhard Tonn ([email protected])
  7. * Thomas Spatzier ([email protected])
  8. *
  9. * Conversion between 31bit and 64bit native syscalls.
  10. *
  11. * Heavily inspired by the 32-bit Sparc compat code which is
  12. * Copyright (C) 1997,1998 Jakub Jelinek ([email protected])
  13. * Copyright (C) 1997 David S. Miller ([email protected])
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/fs.h>
  19. #include <linux/mm.h>
  20. #include <linux/file.h>
  21. #include <linux/signal.h>
  22. #include <linux/resource.h>
  23. #include <linux/times.h>
  24. #include <linux/smp.h>
  25. #include <linux/sem.h>
  26. #include <linux/msg.h>
  27. #include <linux/shm.h>
  28. #include <linux/uio.h>
  29. #include <linux/quota.h>
  30. #include <linux/poll.h>
  31. #include <linux/personality.h>
  32. #include <linux/stat.h>
  33. #include <linux/filter.h>
  34. #include <linux/highmem.h>
  35. #include <linux/mman.h>
  36. #include <linux/ipv6.h>
  37. #include <linux/in.h>
  38. #include <linux/icmpv6.h>
  39. #include <linux/syscalls.h>
  40. #include <linux/sysctl.h>
  41. #include <linux/binfmts.h>
  42. #include <linux/capability.h>
  43. #include <linux/compat.h>
  44. #include <linux/vfs.h>
  45. #include <linux/ptrace.h>
  46. #include <linux/fadvise.h>
  47. #include <linux/ipc.h>
  48. #include <linux/slab.h>
  49. #include <asm/types.h>
  50. #include <linux/uaccess.h>
  51. #include <net/scm.h>
  52. #include <net/sock.h>
  53. #include "compat_linux.h"
  54. #ifdef CONFIG_SYSVIPC
  55. COMPAT_SYSCALL_DEFINE5(s390_ipc, uint, call, int, first, compat_ulong_t, second,
  56. compat_ulong_t, third, compat_uptr_t, ptr)
  57. {
  58. if (call >> 16) /* hack for backward compatibility */
  59. return -EINVAL;
  60. return compat_ksys_ipc(call, first, second, third, ptr, third);
  61. }
  62. #endif
  63. COMPAT_SYSCALL_DEFINE3(s390_truncate64, const char __user *, path, u32, high, u32, low)
  64. {
  65. return ksys_truncate(path, (unsigned long)high << 32 | low);
  66. }
  67. COMPAT_SYSCALL_DEFINE3(s390_ftruncate64, unsigned int, fd, u32, high, u32, low)
  68. {
  69. return ksys_ftruncate(fd, (unsigned long)high << 32 | low);
  70. }
  71. COMPAT_SYSCALL_DEFINE5(s390_pread64, unsigned int, fd, char __user *, ubuf,
  72. compat_size_t, count, u32, high, u32, low)
  73. {
  74. if ((compat_ssize_t) count < 0)
  75. return -EINVAL;
  76. return ksys_pread64(fd, ubuf, count, (unsigned long)high << 32 | low);
  77. }
  78. COMPAT_SYSCALL_DEFINE5(s390_pwrite64, unsigned int, fd, const char __user *, ubuf,
  79. compat_size_t, count, u32, high, u32, low)
  80. {
  81. if ((compat_ssize_t) count < 0)
  82. return -EINVAL;
  83. return ksys_pwrite64(fd, ubuf, count, (unsigned long)high << 32 | low);
  84. }
  85. COMPAT_SYSCALL_DEFINE4(s390_readahead, int, fd, u32, high, u32, low, s32, count)
  86. {
  87. return ksys_readahead(fd, (unsigned long)high << 32 | low, count);
  88. }
  89. struct stat64_emu31 {
  90. unsigned long long st_dev;
  91. unsigned int __pad1;
  92. #define STAT64_HAS_BROKEN_ST_INO 1
  93. u32 __st_ino;
  94. unsigned int st_mode;
  95. unsigned int st_nlink;
  96. u32 st_uid;
  97. u32 st_gid;
  98. unsigned long long st_rdev;
  99. unsigned int __pad3;
  100. long st_size;
  101. u32 st_blksize;
  102. unsigned char __pad4[4];
  103. u32 __pad5; /* future possible st_blocks high bits */
  104. u32 st_blocks; /* Number 512-byte blocks allocated. */
  105. u32 st_atime;
  106. u32 __pad6;
  107. u32 st_mtime;
  108. u32 __pad7;
  109. u32 st_ctime;
  110. u32 __pad8; /* will be high 32 bits of ctime someday */
  111. unsigned long st_ino;
  112. };
  113. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  114. {
  115. struct stat64_emu31 tmp;
  116. memset(&tmp, 0, sizeof(tmp));
  117. tmp.st_dev = huge_encode_dev(stat->dev);
  118. tmp.st_ino = stat->ino;
  119. tmp.__st_ino = (u32)stat->ino;
  120. tmp.st_mode = stat->mode;
  121. tmp.st_nlink = (unsigned int)stat->nlink;
  122. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  123. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  124. tmp.st_rdev = huge_encode_dev(stat->rdev);
  125. tmp.st_size = stat->size;
  126. tmp.st_blksize = (u32)stat->blksize;
  127. tmp.st_blocks = (u32)stat->blocks;
  128. tmp.st_atime = (u32)stat->atime.tv_sec;
  129. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  130. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  131. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  132. }
  133. COMPAT_SYSCALL_DEFINE2(s390_stat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
  134. {
  135. struct kstat stat;
  136. int ret = vfs_stat(filename, &stat);
  137. if (!ret)
  138. ret = cp_stat64(statbuf, &stat);
  139. return ret;
  140. }
  141. COMPAT_SYSCALL_DEFINE2(s390_lstat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
  142. {
  143. struct kstat stat;
  144. int ret = vfs_lstat(filename, &stat);
  145. if (!ret)
  146. ret = cp_stat64(statbuf, &stat);
  147. return ret;
  148. }
  149. COMPAT_SYSCALL_DEFINE2(s390_fstat64, unsigned int, fd, struct stat64_emu31 __user *, statbuf)
  150. {
  151. struct kstat stat;
  152. int ret = vfs_fstat(fd, &stat);
  153. if (!ret)
  154. ret = cp_stat64(statbuf, &stat);
  155. return ret;
  156. }
  157. COMPAT_SYSCALL_DEFINE4(s390_fstatat64, unsigned int, dfd, const char __user *, filename,
  158. struct stat64_emu31 __user *, statbuf, int, flag)
  159. {
  160. struct kstat stat;
  161. int error;
  162. error = vfs_fstatat(dfd, filename, &stat, flag);
  163. if (error)
  164. return error;
  165. return cp_stat64(statbuf, &stat);
  166. }
  167. /*
  168. * Linux/i386 didn't use to be able to handle more than
  169. * 4 system call parameters, so these system calls used a memory
  170. * block for parameter passing..
  171. */
  172. struct mmap_arg_struct_emu31 {
  173. compat_ulong_t addr;
  174. compat_ulong_t len;
  175. compat_ulong_t prot;
  176. compat_ulong_t flags;
  177. compat_ulong_t fd;
  178. compat_ulong_t offset;
  179. };
  180. COMPAT_SYSCALL_DEFINE1(s390_old_mmap, struct mmap_arg_struct_emu31 __user *, arg)
  181. {
  182. struct mmap_arg_struct_emu31 a;
  183. if (copy_from_user(&a, arg, sizeof(a)))
  184. return -EFAULT;
  185. if (a.offset & ~PAGE_MASK)
  186. return -EINVAL;
  187. return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  188. a.offset >> PAGE_SHIFT);
  189. }
  190. COMPAT_SYSCALL_DEFINE1(s390_mmap2, struct mmap_arg_struct_emu31 __user *, arg)
  191. {
  192. struct mmap_arg_struct_emu31 a;
  193. if (copy_from_user(&a, arg, sizeof(a)))
  194. return -EFAULT;
  195. return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  196. }
  197. COMPAT_SYSCALL_DEFINE3(s390_read, unsigned int, fd, char __user *, buf, compat_size_t, count)
  198. {
  199. if ((compat_ssize_t) count < 0)
  200. return -EINVAL;
  201. return ksys_read(fd, buf, count);
  202. }
  203. COMPAT_SYSCALL_DEFINE3(s390_write, unsigned int, fd, const char __user *, buf, compat_size_t, count)
  204. {
  205. if ((compat_ssize_t) count < 0)
  206. return -EINVAL;
  207. return ksys_write(fd, buf, count);
  208. }
  209. /*
  210. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  211. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  212. * because the 31 bit values differ from the 64 bit values.
  213. */
  214. COMPAT_SYSCALL_DEFINE5(s390_fadvise64, int, fd, u32, high, u32, low, compat_size_t, len, int, advise)
  215. {
  216. if (advise == 4)
  217. advise = POSIX_FADV_DONTNEED;
  218. else if (advise == 5)
  219. advise = POSIX_FADV_NOREUSE;
  220. return ksys_fadvise64_64(fd, (unsigned long)high << 32 | low, len,
  221. advise);
  222. }
  223. struct fadvise64_64_args {
  224. int fd;
  225. long long offset;
  226. long long len;
  227. int advice;
  228. };
  229. COMPAT_SYSCALL_DEFINE1(s390_fadvise64_64, struct fadvise64_64_args __user *, args)
  230. {
  231. struct fadvise64_64_args a;
  232. if ( copy_from_user(&a, args, sizeof(a)) )
  233. return -EFAULT;
  234. if (a.advice == 4)
  235. a.advice = POSIX_FADV_DONTNEED;
  236. else if (a.advice == 5)
  237. a.advice = POSIX_FADV_NOREUSE;
  238. return ksys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  239. }
  240. COMPAT_SYSCALL_DEFINE6(s390_sync_file_range, int, fd, u32, offhigh, u32, offlow,
  241. u32, nhigh, u32, nlow, unsigned int, flags)
  242. {
  243. return ksys_sync_file_range(fd, ((loff_t)offhigh << 32) + offlow,
  244. ((u64)nhigh << 32) + nlow, flags);
  245. }
  246. COMPAT_SYSCALL_DEFINE6(s390_fallocate, int, fd, int, mode, u32, offhigh, u32, offlow,
  247. u32, lenhigh, u32, lenlow)
  248. {
  249. return ksys_fallocate(fd, mode, ((loff_t)offhigh << 32) + offlow,
  250. ((u64)lenhigh << 32) + lenlow);
  251. }