syscalls.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * arch/ppc/kernel/sys_ppc.c
  3. *
  4. * PowerPC version
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. *
  7. * Derived from "arch/i386/kernel/sys_i386.c"
  8. * Adapted from the i386 version by Gary Thomas
  9. * Modified by Cort Dougan (cort@cs.nmt.edu)
  10. * and Paul Mackerras (paulus@cs.anu.edu.au).
  11. *
  12. * This file contains various random system calls that
  13. * have a non-standard calling sequence on the Linux/PPC
  14. * platform.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/sched.h>
  24. #include <linux/mm.h>
  25. #include <linux/smp.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/sem.h>
  28. #include <linux/msg.h>
  29. #include <linux/shm.h>
  30. #include <linux/stat.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/mman.h>
  33. #include <linux/sys.h>
  34. #include <linux/ipc.h>
  35. #include <linux/utsname.h>
  36. #include <linux/file.h>
  37. #include <linux/unistd.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/ipc.h>
  40. #include <asm/semaphore.h>
  41. /*
  42. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  43. *
  44. * This is really horribly ugly.
  45. */
  46. int
  47. sys_ipc (uint call, int first, int second, int third, void __user *ptr, long fifth)
  48. {
  49. int version, ret;
  50. version = call >> 16; /* hack for backward compatibility */
  51. call &= 0xffff;
  52. ret = -ENOSYS;
  53. switch (call) {
  54. case SEMOP:
  55. ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
  56. second, NULL);
  57. break;
  58. case SEMTIMEDOP:
  59. ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
  60. second, (const struct timespec __user *) fifth);
  61. break;
  62. case SEMGET:
  63. ret = sys_semget (first, second, third);
  64. break;
  65. case SEMCTL: {
  66. union semun fourth;
  67. if (!ptr)
  68. break;
  69. if ((ret = access_ok(VERIFY_READ, ptr, sizeof(long)) ? 0 : -EFAULT)
  70. || (ret = get_user(fourth.__pad, (void __user *__user *)ptr)))
  71. break;
  72. ret = sys_semctl (first, second, third, fourth);
  73. break;
  74. }
  75. case MSGSND:
  76. ret = sys_msgsnd (first, (struct msgbuf __user *) ptr, second, third);
  77. break;
  78. case MSGRCV:
  79. switch (version) {
  80. case 0: {
  81. struct ipc_kludge tmp;
  82. if (!ptr)
  83. break;
  84. if ((ret = access_ok(VERIFY_READ, ptr, sizeof(tmp)) ? 0 : -EFAULT)
  85. || (ret = copy_from_user(&tmp,
  86. (struct ipc_kludge __user *) ptr,
  87. sizeof (tmp)) ? -EFAULT : 0))
  88. break;
  89. ret = sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp,
  90. third);
  91. break;
  92. }
  93. default:
  94. ret = sys_msgrcv (first, (struct msgbuf __user *) ptr,
  95. second, fifth, third);
  96. break;
  97. }
  98. break;
  99. case MSGGET:
  100. ret = sys_msgget ((key_t) first, second);
  101. break;
  102. case MSGCTL:
  103. ret = sys_msgctl (first, second, (struct msqid_ds __user *) ptr);
  104. break;
  105. case SHMAT: {
  106. ulong raddr;
  107. if ((ret = access_ok(VERIFY_WRITE, (ulong __user *) third,
  108. sizeof(ulong)) ? 0 : -EFAULT))
  109. break;
  110. ret = do_shmat (first, (char __user *) ptr, second, &raddr);
  111. if (ret)
  112. break;
  113. ret = put_user (raddr, (ulong __user *) third);
  114. break;
  115. }
  116. case SHMDT:
  117. ret = sys_shmdt ((char __user *)ptr);
  118. break;
  119. case SHMGET:
  120. ret = sys_shmget (first, second, third);
  121. break;
  122. case SHMCTL:
  123. ret = sys_shmctl (first, second, (struct shmid_ds __user *) ptr);
  124. break;
  125. }
  126. return ret;
  127. }
  128. /*
  129. * sys_pipe() is the normal C calling standard for creating
  130. * a pipe. It's not the way unix traditionally does this, though.
  131. */
  132. int sys_pipe(int __user *fildes)
  133. {
  134. int fd[2];
  135. int error;
  136. error = do_pipe(fd);
  137. if (!error) {
  138. if (copy_to_user(fildes, fd, 2*sizeof(int)))
  139. error = -EFAULT;
  140. }
  141. return error;
  142. }
  143. static inline unsigned long
  144. do_mmap2(unsigned long addr, size_t len,
  145. unsigned long prot, unsigned long flags,
  146. unsigned long fd, unsigned long pgoff)
  147. {
  148. struct file * file = NULL;
  149. int ret = -EBADF;
  150. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  151. if (!(flags & MAP_ANONYMOUS)) {
  152. if (!(file = fget(fd)))
  153. goto out;
  154. }
  155. down_write(&current->mm->mmap_sem);
  156. ret = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  157. up_write(&current->mm->mmap_sem);
  158. if (file)
  159. fput(file);
  160. out:
  161. return ret;
  162. }
  163. unsigned long sys_mmap2(unsigned long addr, size_t len,
  164. unsigned long prot, unsigned long flags,
  165. unsigned long fd, unsigned long pgoff)
  166. {
  167. return do_mmap2(addr, len, prot, flags, fd, pgoff);
  168. }
  169. unsigned long sys_mmap(unsigned long addr, size_t len,
  170. unsigned long prot, unsigned long flags,
  171. unsigned long fd, off_t offset)
  172. {
  173. int err = -EINVAL;
  174. if (offset & ~PAGE_MASK)
  175. goto out;
  176. err = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  177. out:
  178. return err;
  179. }
  180. /*
  181. * Due to some executables calling the wrong select we sometimes
  182. * get wrong args. This determines how the args are being passed
  183. * (a single ptr to them all args passed) then calls
  184. * sys_select() with the appropriate args. -- Cort
  185. */
  186. int
  187. ppc_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
  188. {
  189. if ( (unsigned long)n >= 4096 )
  190. {
  191. unsigned long __user *buffer = (unsigned long __user *)n;
  192. if (!access_ok(VERIFY_READ, buffer, 5*sizeof(unsigned long))
  193. || __get_user(n, buffer)
  194. || __get_user(inp, ((fd_set __user * __user *)(buffer+1)))
  195. || __get_user(outp, ((fd_set __user * __user *)(buffer+2)))
  196. || __get_user(exp, ((fd_set __user * __user *)(buffer+3)))
  197. || __get_user(tvp, ((struct timeval __user * __user *)(buffer+4))))
  198. return -EFAULT;
  199. }
  200. return sys_select(n, inp, outp, exp, tvp);
  201. }
  202. int sys_uname(struct old_utsname __user * name)
  203. {
  204. int err = -EFAULT;
  205. down_read(&uts_sem);
  206. if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
  207. err = 0;
  208. up_read(&uts_sem);
  209. return err;
  210. }
  211. int sys_olduname(struct oldold_utsname __user * name)
  212. {
  213. int error;
  214. if (!name)
  215. return -EFAULT;
  216. if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
  217. return -EFAULT;
  218. down_read(&uts_sem);
  219. error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
  220. error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
  221. error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
  222. error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
  223. error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
  224. error -= __put_user(0,name->release+__OLD_UTS_LEN);
  225. error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
  226. error -= __put_user(0,name->version+__OLD_UTS_LEN);
  227. error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
  228. error = __put_user(0,name->machine+__OLD_UTS_LEN);
  229. up_read(&uts_sem);
  230. error = error ? -EFAULT : 0;
  231. return error;
  232. }
  233. /*
  234. * We put the arguments in a different order so we only use 6
  235. * registers for arguments, rather than 7 as sys_fadvise64_64 needs
  236. * (because `offset' goes in r5/r6).
  237. */
  238. long ppc_fadvise64_64(int fd, int advice, loff_t offset, loff_t len)
  239. {
  240. return sys_fadvise64_64(fd, offset, len, advice);
  241. }