syscalls.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Implementation of various system calls for Linux/PowerPC
  3. *
  4. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  5. *
  6. * Derived from "arch/i386/kernel/sys_i386.c"
  7. * Adapted from the i386 version by Gary Thomas
  8. * Modified by Cort Dougan (cort@cs.nmt.edu)
  9. * and Paul Mackerras (paulus@cs.anu.edu.au).
  10. *
  11. * This file contains various random system calls that
  12. * have a non-standard calling sequence on the Linux/PPC
  13. * platform.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/sched.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/mm.h>
  25. #include <linux/fs.h>
  26. #include <linux/smp.h>
  27. #include <linux/sem.h>
  28. #include <linux/msg.h>
  29. #include <linux/shm.h>
  30. #include <linux/stat.h>
  31. #include <linux/mman.h>
  32. #include <linux/sys.h>
  33. #include <linux/ipc.h>
  34. #include <linux/utsname.h>
  35. #include <linux/file.h>
  36. #include <linux/init.h>
  37. #include <linux/personality.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/syscalls.h>
  40. #include <asm/time.h>
  41. #include <asm/unistd.h>
  42. /*
  43. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  44. *
  45. * This is really horribly ugly.
  46. */
  47. int sys_ipc(uint call, int first, unsigned long second, long third,
  48. void __user *ptr, long fifth)
  49. {
  50. int version, ret;
  51. version = call >> 16; /* hack for backward compatibility */
  52. call &= 0xffff;
  53. ret = -ENOSYS;
  54. switch (call) {
  55. case SEMOP:
  56. ret = sys_semtimedop(first, (struct sembuf __user *)ptr,
  57. (unsigned)second, NULL);
  58. break;
  59. case SEMTIMEDOP:
  60. ret = sys_semtimedop(first, (struct sembuf __user *)ptr,
  61. (unsigned)second,
  62. (const struct timespec __user *) fifth);
  63. break;
  64. case SEMGET:
  65. ret = sys_semget (first, (int)second, third);
  66. break;
  67. case SEMCTL: {
  68. union semun fourth;
  69. ret = -EINVAL;
  70. if (!ptr)
  71. break;
  72. if ((ret = get_user(fourth.__pad, (void __user * __user *)ptr)))
  73. break;
  74. ret = sys_semctl(first, (int)second, third, fourth);
  75. break;
  76. }
  77. case MSGSND:
  78. ret = sys_msgsnd(first, (struct msgbuf __user *)ptr,
  79. (size_t)second, third);
  80. break;
  81. case MSGRCV:
  82. switch (version) {
  83. case 0: {
  84. struct ipc_kludge tmp;
  85. ret = -EINVAL;
  86. if (!ptr)
  87. break;
  88. if ((ret = copy_from_user(&tmp,
  89. (struct ipc_kludge __user *) ptr,
  90. sizeof (tmp)) ? -EFAULT : 0))
  91. break;
  92. ret = sys_msgrcv(first, tmp.msgp, (size_t) second,
  93. tmp.msgtyp, third);
  94. break;
  95. }
  96. default:
  97. ret = sys_msgrcv (first, (struct msgbuf __user *) ptr,
  98. (size_t)second, fifth, third);
  99. break;
  100. }
  101. break;
  102. case MSGGET:
  103. ret = sys_msgget((key_t)first, (int)second);
  104. break;
  105. case MSGCTL:
  106. ret = sys_msgctl(first, (int)second,
  107. (struct msqid_ds __user *)ptr);
  108. break;
  109. case SHMAT: {
  110. ulong raddr;
  111. ret = do_shmat(first, (char __user *)ptr, (int)second, &raddr);
  112. if (ret)
  113. break;
  114. ret = put_user(raddr, (ulong __user *) third);
  115. break;
  116. }
  117. case SHMDT:
  118. ret = sys_shmdt((char __user *)ptr);
  119. break;
  120. case SHMGET:
  121. ret = sys_shmget(first, (size_t)second, third);
  122. break;
  123. case SHMCTL:
  124. ret = sys_shmctl(first, (int)second,
  125. (struct shmid_ds __user *)ptr);
  126. break;
  127. }
  128. return ret;
  129. }
  130. static inline unsigned long do_mmap2(unsigned long addr, size_t len,
  131. unsigned long prot, unsigned long flags,
  132. unsigned long fd, unsigned long off, int shift)
  133. {
  134. struct file * file = NULL;
  135. unsigned long ret = -EINVAL;
  136. if (!arch_validate_prot(prot))
  137. goto out;
  138. if (shift) {
  139. if (off & ((1 << shift) - 1))
  140. goto out;
  141. off >>= shift;
  142. }
  143. ret = -EBADF;
  144. if (!(flags & MAP_ANONYMOUS)) {
  145. if (!(file = fget(fd)))
  146. goto out;
  147. }
  148. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  149. down_write(&current->mm->mmap_sem);
  150. ret = do_mmap_pgoff(file, addr, len, prot, flags, off);
  151. up_write(&current->mm->mmap_sem);
  152. if (file)
  153. fput(file);
  154. out:
  155. return ret;
  156. }
  157. unsigned long sys_mmap2(unsigned long addr, size_t len,
  158. unsigned long prot, unsigned long flags,
  159. unsigned long fd, unsigned long pgoff)
  160. {
  161. return do_mmap2(addr, len, prot, flags, fd, pgoff, PAGE_SHIFT-12);
  162. }
  163. unsigned long sys_mmap(unsigned long addr, size_t len,
  164. unsigned long prot, unsigned long flags,
  165. unsigned long fd, off_t offset)
  166. {
  167. return do_mmap2(addr, len, prot, flags, fd, offset, PAGE_SHIFT);
  168. }
  169. #ifdef CONFIG_PPC32
  170. /*
  171. * Due to some executables calling the wrong select we sometimes
  172. * get wrong args. This determines how the args are being passed
  173. * (a single ptr to them all args passed) then calls
  174. * sys_select() with the appropriate args. -- Cort
  175. */
  176. int
  177. ppc_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
  178. {
  179. if ( (unsigned long)n >= 4096 )
  180. {
  181. unsigned long __user *buffer = (unsigned long __user *)n;
  182. if (!access_ok(VERIFY_READ, buffer, 5*sizeof(unsigned long))
  183. || __get_user(n, buffer)
  184. || __get_user(inp, ((fd_set __user * __user *)(buffer+1)))
  185. || __get_user(outp, ((fd_set __user * __user *)(buffer+2)))
  186. || __get_user(exp, ((fd_set __user * __user *)(buffer+3)))
  187. || __get_user(tvp, ((struct timeval __user * __user *)(buffer+4))))
  188. return -EFAULT;
  189. }
  190. return sys_select(n, inp, outp, exp, tvp);
  191. }
  192. #endif
  193. #ifdef CONFIG_PPC64
  194. long ppc64_personality(unsigned long personality)
  195. {
  196. long ret;
  197. if (personality(current->personality) == PER_LINUX32
  198. && personality == PER_LINUX)
  199. personality = PER_LINUX32;
  200. ret = sys_personality(personality);
  201. if (ret == PER_LINUX32)
  202. ret = PER_LINUX;
  203. return ret;
  204. }
  205. #endif
  206. #ifdef CONFIG_PPC64
  207. #define OVERRIDE_MACHINE (personality(current->personality) == PER_LINUX32)
  208. #else
  209. #define OVERRIDE_MACHINE 0
  210. #endif
  211. static inline int override_machine(char __user *mach)
  212. {
  213. if (OVERRIDE_MACHINE) {
  214. /* change ppc64 to ppc */
  215. if (__put_user(0, mach+3) || __put_user(0, mach+4))
  216. return -EFAULT;
  217. }
  218. return 0;
  219. }
  220. long ppc_newuname(struct new_utsname __user * name)
  221. {
  222. int err = 0;
  223. down_read(&uts_sem);
  224. if (copy_to_user(name, utsname(), sizeof(*name)))
  225. err = -EFAULT;
  226. up_read(&uts_sem);
  227. if (!err)
  228. err = override_machine(name->machine);
  229. return err;
  230. }
  231. int sys_uname(struct old_utsname __user *name)
  232. {
  233. int err = 0;
  234. down_read(&uts_sem);
  235. if (copy_to_user(name, utsname(), sizeof(*name)))
  236. err = -EFAULT;
  237. up_read(&uts_sem);
  238. if (!err)
  239. err = override_machine(name->machine);
  240. return err;
  241. }
  242. int sys_olduname(struct oldold_utsname __user *name)
  243. {
  244. int error;
  245. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  246. return -EFAULT;
  247. down_read(&uts_sem);
  248. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  249. __OLD_UTS_LEN);
  250. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  251. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  252. __OLD_UTS_LEN);
  253. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  254. error |= __copy_to_user(&name->release, &utsname()->release,
  255. __OLD_UTS_LEN);
  256. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  257. error |= __copy_to_user(&name->version, &utsname()->version,
  258. __OLD_UTS_LEN);
  259. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  260. error |= __copy_to_user(&name->machine, &utsname()->machine,
  261. __OLD_UTS_LEN);
  262. error |= override_machine(name->machine);
  263. up_read(&uts_sem);
  264. return error? -EFAULT: 0;
  265. }
  266. long ppc_fadvise64_64(int fd, int advice, u32 offset_high, u32 offset_low,
  267. u32 len_high, u32 len_low)
  268. {
  269. return sys_fadvise64(fd, (u64)offset_high << 32 | offset_low,
  270. (u64)len_high << 32 | len_low, advice);
  271. }
  272. void do_show_syscall(unsigned long r3, unsigned long r4, unsigned long r5,
  273. unsigned long r6, unsigned long r7, unsigned long r8,
  274. struct pt_regs *regs)
  275. {
  276. printk("syscall %ld(%lx, %lx, %lx, %lx, %lx, %lx) regs=%p current=%p"
  277. " cpu=%d\n", regs->gpr[0], r3, r4, r5, r6, r7, r8, regs,
  278. current, smp_processor_id());
  279. }
  280. void do_show_syscall_exit(unsigned long r3)
  281. {
  282. printk(" -> %lx, current=%p cpu=%d\n", r3, current, smp_processor_id());
  283. }