sys_sh64.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * arch/sh64/kernel/sys_sh64.c
  7. *
  8. * Copyright (C) 2000, 2001 Paolo Alberelli
  9. *
  10. * This file contains various random system calls that
  11. * have a non-standard calling sequence on the Linux/SH5
  12. * platform.
  13. *
  14. * Mostly taken from i386 version.
  15. *
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/rwsem.h>
  19. #include <linux/sched.h>
  20. #include <linux/mm.h>
  21. #include <linux/smp.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/sem.h>
  24. #include <linux/msg.h>
  25. #include <linux/shm.h>
  26. #include <linux/stat.h>
  27. #include <linux/mman.h>
  28. #include <linux/file.h>
  29. #include <linux/utsname.h>
  30. #include <linux/syscalls.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/ipc.h>
  33. #include <asm/ptrace.h>
  34. #include <asm/unistd.h>
  35. #define REG_3 3
  36. /*
  37. * sys_pipe() is the normal C calling standard for creating
  38. * a pipe. It's not the way Unix traditionally does this, though.
  39. */
  40. #ifdef NEW_PIPE_IMPLEMENTATION
  41. asmlinkage int sys_pipe(unsigned long * fildes,
  42. unsigned long dummy_r3,
  43. unsigned long dummy_r4,
  44. unsigned long dummy_r5,
  45. unsigned long dummy_r6,
  46. unsigned long dummy_r7,
  47. struct pt_regs * regs) /* r8 = pt_regs forced by entry.S */
  48. {
  49. int fd[2];
  50. int ret;
  51. ret = do_pipe(fd);
  52. if (ret == 0)
  53. /*
  54. ***********************************************************************
  55. * To avoid the copy_to_user we prefer to break the ABIs convention, *
  56. * packing the valid pair of file IDs into a single register (r3); *
  57. * while r2 is the return code as defined by the sh5-ABIs. *
  58. * BE CAREFUL: pipe stub, into glibc, must be aware of this solution *
  59. ***********************************************************************
  60. #ifdef __LITTLE_ENDIAN__
  61. regs->regs[REG_3] = (((unsigned long long) fd[1]) << 32) | ((unsigned long long) fd[0]);
  62. #else
  63. regs->regs[REG_3] = (((unsigned long long) fd[0]) << 32) | ((unsigned long long) fd[1]);
  64. #endif
  65. */
  66. /* although not very clever this is endianess independent */
  67. regs->regs[REG_3] = (unsigned long long) *((unsigned long long *) fd);
  68. return ret;
  69. }
  70. #else
  71. asmlinkage int sys_pipe(unsigned long * fildes)
  72. {
  73. int fd[2];
  74. int error;
  75. error = do_pipe(fd);
  76. if (!error) {
  77. if (copy_to_user(fildes, fd, 2*sizeof(int)))
  78. error = -EFAULT;
  79. }
  80. return error;
  81. }
  82. #endif
  83. /*
  84. * To avoid cache alias, we map the shard page with same color.
  85. */
  86. #define COLOUR_ALIGN(addr) (((addr)+SHMLBA-1)&~(SHMLBA-1))
  87. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  88. unsigned long len, unsigned long pgoff, unsigned long flags)
  89. {
  90. struct vm_area_struct *vma;
  91. if (flags & MAP_FIXED) {
  92. /* We do not accept a shared mapping if it would violate
  93. * cache aliasing constraints.
  94. */
  95. if ((flags & MAP_SHARED) && (addr & (SHMLBA - 1)))
  96. return -EINVAL;
  97. return addr;
  98. }
  99. if (len > TASK_SIZE)
  100. return -ENOMEM;
  101. if (!addr)
  102. addr = TASK_UNMAPPED_BASE;
  103. if (flags & MAP_PRIVATE)
  104. addr = PAGE_ALIGN(addr);
  105. else
  106. addr = COLOUR_ALIGN(addr);
  107. for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
  108. /* At this point: (!vma || addr < vma->vm_end). */
  109. if (TASK_SIZE - len < addr)
  110. return -ENOMEM;
  111. if (!vma || addr + len <= vma->vm_start)
  112. return addr;
  113. addr = vma->vm_end;
  114. if (!(flags & MAP_PRIVATE))
  115. addr = COLOUR_ALIGN(addr);
  116. }
  117. }
  118. /* common code for old and new mmaps */
  119. static inline long do_mmap2(
  120. unsigned long addr, unsigned long len,
  121. unsigned long prot, unsigned long flags,
  122. unsigned long fd, unsigned long pgoff)
  123. {
  124. int error = -EBADF;
  125. struct file * file = NULL;
  126. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  127. if (!(flags & MAP_ANONYMOUS)) {
  128. file = fget(fd);
  129. if (!file)
  130. goto out;
  131. }
  132. down_write(&current->mm->mmap_sem);
  133. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  134. up_write(&current->mm->mmap_sem);
  135. if (file)
  136. fput(file);
  137. out:
  138. return error;
  139. }
  140. asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
  141. unsigned long prot, unsigned long flags,
  142. unsigned long fd, unsigned long pgoff)
  143. {
  144. return do_mmap2(addr, len, prot, flags, fd, pgoff);
  145. }
  146. asmlinkage int old_mmap(unsigned long addr, unsigned long len,
  147. unsigned long prot, unsigned long flags,
  148. int fd, unsigned long off)
  149. {
  150. if (off & ~PAGE_MASK)
  151. return -EINVAL;
  152. return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
  153. }
  154. /*
  155. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  156. *
  157. * This is really horribly ugly.
  158. */
  159. asmlinkage int sys_ipc(uint call, int first, int second,
  160. int third, void __user *ptr, long fifth)
  161. {
  162. int version, ret;
  163. version = call >> 16; /* hack for backward compatibility */
  164. call &= 0xffff;
  165. if (call <= SEMCTL)
  166. switch (call) {
  167. case SEMOP:
  168. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  169. second, NULL);
  170. case SEMTIMEDOP:
  171. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  172. second,
  173. (const struct timespec __user *)fifth);
  174. case SEMGET:
  175. return sys_semget (first, second, third);
  176. case SEMCTL: {
  177. union semun fourth;
  178. if (!ptr)
  179. return -EINVAL;
  180. if (get_user(fourth.__pad, (void * __user *) ptr))
  181. return -EFAULT;
  182. return sys_semctl (first, second, third, fourth);
  183. }
  184. default:
  185. return -EINVAL;
  186. }
  187. if (call <= MSGCTL)
  188. switch (call) {
  189. case MSGSND:
  190. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  191. second, third);
  192. case MSGRCV:
  193. switch (version) {
  194. case 0: {
  195. struct ipc_kludge tmp;
  196. if (!ptr)
  197. return -EINVAL;
  198. if (copy_from_user(&tmp,
  199. (struct ipc_kludge __user *) ptr,
  200. sizeof (tmp)))
  201. return -EFAULT;
  202. return sys_msgrcv (first, tmp.msgp, second,
  203. tmp.msgtyp, third);
  204. }
  205. default:
  206. return sys_msgrcv (first,
  207. (struct msgbuf __user *) ptr,
  208. second, fifth, third);
  209. }
  210. case MSGGET:
  211. return sys_msgget ((key_t) first, second);
  212. case MSGCTL:
  213. return sys_msgctl (first, second,
  214. (struct msqid_ds __user *) ptr);
  215. default:
  216. return -EINVAL;
  217. }
  218. if (call <= SHMCTL)
  219. switch (call) {
  220. case SHMAT:
  221. switch (version) {
  222. default: {
  223. ulong raddr;
  224. ret = do_shmat (first, (char __user *) ptr,
  225. second, &raddr);
  226. if (ret)
  227. return ret;
  228. return put_user (raddr, (ulong __user *) third);
  229. }
  230. case 1: /* iBCS2 emulator entry point */
  231. if (!segment_eq(get_fs(), get_ds()))
  232. return -EINVAL;
  233. return do_shmat (first, (char __user *) ptr,
  234. second, (ulong *) third);
  235. }
  236. case SHMDT:
  237. return sys_shmdt ((char __user *)ptr);
  238. case SHMGET:
  239. return sys_shmget (first, second, third);
  240. case SHMCTL:
  241. return sys_shmctl (first, second,
  242. (struct shmid_ds __user *) ptr);
  243. default:
  244. return -EINVAL;
  245. }
  246. return -EINVAL;
  247. }
  248. asmlinkage int sys_uname(struct old_utsname * name)
  249. {
  250. int err;
  251. if (!name)
  252. return -EFAULT;
  253. down_read(&uts_sem);
  254. err = copy_to_user(name, utsname(), sizeof (*name));
  255. up_read(&uts_sem);
  256. return err?-EFAULT:0;
  257. }
  258. /*
  259. * Do a system call from kernel instead of calling sys_execve so we
  260. * end up with proper pt_regs.
  261. */
  262. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  263. {
  264. register unsigned long __sc0 __asm__ ("r9") = ((0x13 << 16) | __NR_execve);
  265. register unsigned long __sc2 __asm__ ("r2") = (unsigned long) filename;
  266. register unsigned long __sc3 __asm__ ("r3") = (unsigned long) argv;
  267. register unsigned long __sc4 __asm__ ("r4") = (unsigned long) envp;
  268. __asm__ __volatile__ ("trapa %1 !\t\t\t execve(%2,%3,%4)"
  269. : "=r" (__sc0)
  270. : "r" (__sc0), "r" (__sc2), "r" (__sc3), "r" (__sc4) );
  271. __asm__ __volatile__ ("!dummy %0 %1 %2 %3"
  272. : : "r" (__sc0), "r" (__sc2), "r" (__sc3), "r" (__sc4) : "memory");
  273. return __sc0;
  274. }