sys_oabi-compat.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * arch/arm/kernel/sys_oabi-compat.c
  3. *
  4. * Compatibility wrappers for syscalls that are used from
  5. * old ABI user space binaries with an EABI kernel.
  6. *
  7. * Author: Nicolas Pitre
  8. * Created: Oct 7, 2005
  9. * Copyright: MontaVista Software, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. /*
  16. * The legacy ABI and the new ARM EABI have different rules making some
  17. * syscalls incompatible especially with structure arguments.
  18. * Most notably, Eabi says 64-bit members should be 64-bit aligned instead of
  19. * simply word aligned. EABI also pads structures to the size of the largest
  20. * member it contains instead of the invariant 32-bit.
  21. *
  22. * The following syscalls are affected:
  23. *
  24. * sys_stat64:
  25. * sys_lstat64:
  26. * sys_fstat64:
  27. *
  28. * struct stat64 has different sizes and some members are shifted
  29. * Compatibility wrappers are needed for them and provided below.
  30. *
  31. * sys_fcntl64:
  32. *
  33. * struct flock64 has different sizes and some members are shifted
  34. * A compatibility wrapper is needed and provided below.
  35. *
  36. * sys_statfs64:
  37. * sys_fstatfs64:
  38. *
  39. * struct statfs64 has extra padding with EABI growing its size from
  40. * 84 to 88. This struct is now __attribute__((packed,aligned(4)))
  41. * with a small assembly wrapper to force the sz argument to 84 if it is 88
  42. * to avoid copying the extra padding over user space unexpecting it.
  43. *
  44. * sys_newuname:
  45. *
  46. * struct new_utsname has no padding with EABI. No problem there.
  47. *
  48. * sys_epoll_ctl:
  49. * sys_epoll_wait:
  50. *
  51. * struct epoll_event has its second member shifted also affecting the
  52. * structure size. Compatibility wrappers are needed and provided below.
  53. *
  54. * sys_ipc:
  55. * sys_semop:
  56. * sys_semtimedop:
  57. *
  58. * struct sembuf loses its padding with EABI. Since arrays of them are
  59. * used they have to be copyed to remove the padding. Compatibility wrappers
  60. * provided below.
  61. */
  62. #include <linux/syscalls.h>
  63. #include <linux/errno.h>
  64. #include <linux/fs.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/eventpoll.h>
  67. #include <linux/sem.h>
  68. #include <asm/ipc.h>
  69. #include <asm/uaccess.h>
  70. struct oldabi_stat64 {
  71. unsigned long long st_dev;
  72. unsigned int __pad1;
  73. unsigned long __st_ino;
  74. unsigned int st_mode;
  75. unsigned int st_nlink;
  76. unsigned long st_uid;
  77. unsigned long st_gid;
  78. unsigned long long st_rdev;
  79. unsigned int __pad2;
  80. long long st_size;
  81. unsigned long st_blksize;
  82. unsigned long long st_blocks;
  83. unsigned long st_atime;
  84. unsigned long st_atime_nsec;
  85. unsigned long st_mtime;
  86. unsigned long st_mtime_nsec;
  87. unsigned long st_ctime;
  88. unsigned long st_ctime_nsec;
  89. unsigned long long st_ino;
  90. } __attribute__ ((packed,aligned(4)));
  91. static long cp_oldabi_stat64(struct kstat *stat,
  92. struct oldabi_stat64 __user *statbuf)
  93. {
  94. struct oldabi_stat64 tmp;
  95. tmp.st_dev = huge_encode_dev(stat->dev);
  96. tmp.__pad1 = 0;
  97. tmp.__st_ino = stat->ino;
  98. tmp.st_mode = stat->mode;
  99. tmp.st_nlink = stat->nlink;
  100. tmp.st_uid = stat->uid;
  101. tmp.st_gid = stat->gid;
  102. tmp.st_rdev = huge_encode_dev(stat->rdev);
  103. tmp.st_size = stat->size;
  104. tmp.st_blocks = stat->blocks;
  105. tmp.__pad2 = 0;
  106. tmp.st_blksize = stat->blksize;
  107. tmp.st_atime = stat->atime.tv_sec;
  108. tmp.st_atime_nsec = stat->atime.tv_nsec;
  109. tmp.st_mtime = stat->mtime.tv_sec;
  110. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  111. tmp.st_ctime = stat->ctime.tv_sec;
  112. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  113. tmp.st_ino = stat->ino;
  114. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  115. }
  116. asmlinkage long sys_oabi_stat64(char __user * filename,
  117. struct oldabi_stat64 __user * statbuf)
  118. {
  119. struct kstat stat;
  120. int error = vfs_stat(filename, &stat);
  121. if (!error)
  122. error = cp_oldabi_stat64(&stat, statbuf);
  123. return error;
  124. }
  125. asmlinkage long sys_oabi_lstat64(char __user * filename,
  126. struct oldabi_stat64 __user * statbuf)
  127. {
  128. struct kstat stat;
  129. int error = vfs_lstat(filename, &stat);
  130. if (!error)
  131. error = cp_oldabi_stat64(&stat, statbuf);
  132. return error;
  133. }
  134. asmlinkage long sys_oabi_fstat64(unsigned long fd,
  135. struct oldabi_stat64 __user * statbuf)
  136. {
  137. struct kstat stat;
  138. int error = vfs_fstat(fd, &stat);
  139. if (!error)
  140. error = cp_oldabi_stat64(&stat, statbuf);
  141. return error;
  142. }
  143. struct oabi_flock64 {
  144. short l_type;
  145. short l_whence;
  146. loff_t l_start;
  147. loff_t l_len;
  148. pid_t l_pid;
  149. } __attribute__ ((packed,aligned(4)));
  150. asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
  151. unsigned long arg)
  152. {
  153. struct oabi_flock64 user;
  154. struct flock64 kernel;
  155. mm_segment_t fs = USER_DS; /* initialized to kill a warning */
  156. unsigned long local_arg = arg;
  157. int ret;
  158. switch (cmd) {
  159. case F_GETLK64:
  160. case F_SETLK64:
  161. case F_SETLKW64:
  162. if (copy_from_user(&user, (struct oabi_flock64 __user *)arg,
  163. sizeof(user)))
  164. return -EFAULT;
  165. kernel.l_type = user.l_type;
  166. kernel.l_whence = user.l_whence;
  167. kernel.l_start = user.l_start;
  168. kernel.l_len = user.l_len;
  169. kernel.l_pid = user.l_pid;
  170. local_arg = (unsigned long)&kernel;
  171. fs = get_fs();
  172. set_fs(KERNEL_DS);
  173. }
  174. ret = sys_fcntl64(fd, cmd, local_arg);
  175. switch (cmd) {
  176. case F_GETLK64:
  177. if (!ret) {
  178. user.l_type = kernel.l_type;
  179. user.l_whence = kernel.l_whence;
  180. user.l_start = kernel.l_start;
  181. user.l_len = kernel.l_len;
  182. user.l_pid = kernel.l_pid;
  183. if (copy_to_user((struct oabi_flock64 __user *)arg,
  184. &user, sizeof(user)))
  185. ret = -EFAULT;
  186. }
  187. case F_SETLK64:
  188. case F_SETLKW64:
  189. set_fs(fs);
  190. }
  191. return ret;
  192. }
  193. struct oabi_epoll_event {
  194. __u32 events;
  195. __u64 data;
  196. } __attribute__ ((packed,aligned(4)));
  197. asmlinkage long sys_oabi_epoll_ctl(int epfd, int op, int fd,
  198. struct oabi_epoll_event __user *event)
  199. {
  200. struct oabi_epoll_event user;
  201. struct epoll_event kernel;
  202. mm_segment_t fs;
  203. long ret;
  204. if (op == EPOLL_CTL_DEL)
  205. return sys_epoll_ctl(epfd, op, fd, NULL);
  206. if (copy_from_user(&user, event, sizeof(user)))
  207. return -EFAULT;
  208. kernel.events = user.events;
  209. kernel.data = user.data;
  210. fs = get_fs();
  211. set_fs(KERNEL_DS);
  212. ret = sys_epoll_ctl(epfd, op, fd, &kernel);
  213. set_fs(fs);
  214. return ret;
  215. }
  216. asmlinkage long sys_oabi_epoll_wait(int epfd,
  217. struct oabi_epoll_event __user *events,
  218. int maxevents, int timeout)
  219. {
  220. struct epoll_event *kbuf;
  221. mm_segment_t fs;
  222. long ret, err, i;
  223. if (maxevents <= 0 || maxevents > (INT_MAX/sizeof(struct epoll_event)))
  224. return -EINVAL;
  225. kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
  226. if (!kbuf)
  227. return -ENOMEM;
  228. fs = get_fs();
  229. set_fs(KERNEL_DS);
  230. ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
  231. set_fs(fs);
  232. err = 0;
  233. for (i = 0; i < ret; i++) {
  234. __put_user_error(kbuf[i].events, &events->events, err);
  235. __put_user_error(kbuf[i].data, &events->data, err);
  236. events++;
  237. }
  238. kfree(kbuf);
  239. return err ? -EFAULT : ret;
  240. }
  241. struct oabi_sembuf {
  242. unsigned short sem_num;
  243. short sem_op;
  244. short sem_flg;
  245. unsigned short __pad;
  246. };
  247. asmlinkage long sys_oabi_semtimedop(int semid,
  248. struct oabi_sembuf __user *tsops,
  249. unsigned nsops,
  250. const struct timespec __user *timeout)
  251. {
  252. struct sembuf *sops;
  253. struct timespec local_timeout;
  254. long err;
  255. int i;
  256. if (nsops < 1)
  257. return -EINVAL;
  258. sops = kmalloc(sizeof(*sops) * nsops, GFP_KERNEL);
  259. if (!sops)
  260. return -ENOMEM;
  261. err = 0;
  262. for (i = 0; i < nsops; i++) {
  263. __get_user_error(sops[i].sem_num, &tsops->sem_num, err);
  264. __get_user_error(sops[i].sem_op, &tsops->sem_op, err);
  265. __get_user_error(sops[i].sem_flg, &tsops->sem_flg, err);
  266. tsops++;
  267. }
  268. if (timeout) {
  269. /* copy this as well before changing domain protection */
  270. err |= copy_from_user(&local_timeout, timeout, sizeof(*timeout));
  271. timeout = &local_timeout;
  272. }
  273. if (err) {
  274. err = -EFAULT;
  275. } else {
  276. mm_segment_t fs = get_fs();
  277. set_fs(KERNEL_DS);
  278. err = sys_semtimedop(semid, sops, nsops, timeout);
  279. set_fs(fs);
  280. }
  281. kfree(sops);
  282. return err;
  283. }
  284. asmlinkage long sys_oabi_semop(int semid, struct oabi_sembuf __user *tsops,
  285. unsigned nsops)
  286. {
  287. return sys_oabi_semtimedop(semid, tsops, nsops, NULL);
  288. }
  289. extern asmlinkage int sys_ipc(uint call, int first, int second, int third,
  290. void __user *ptr, long fifth);
  291. asmlinkage int sys_oabi_ipc(uint call, int first, int second, int third,
  292. void __user *ptr, long fifth)
  293. {
  294. switch (call & 0xffff) {
  295. case SEMOP:
  296. return sys_oabi_semtimedop(first,
  297. (struct oabi_sembuf __user *)ptr,
  298. second, NULL);
  299. case SEMTIMEDOP:
  300. return sys_oabi_semtimedop(first,
  301. (struct oabi_sembuf __user *)ptr,
  302. second,
  303. (const struct timespec __user *)fifth);
  304. default:
  305. return sys_ipc(call, first, second, third, ptr, fifth);
  306. }
  307. }