sys_sh.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * linux/arch/sh/kernel/sys_sh.c
  3. *
  4. * This file contains various random system calls that
  5. * have a non-standard calling sequence on the Linux/SuperH
  6. * platform.
  7. *
  8. * Taken from i386 version.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/sem.h>
  15. #include <linux/msg.h>
  16. #include <linux/shm.h>
  17. #include <linux/stat.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/mman.h>
  20. #include <linux/file.h>
  21. #include <linux/utsname.h>
  22. #include <linux/module.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/ipc.h>
  26. #include <asm/unistd.h>
  27. /*
  28. * sys_pipe() is the normal C calling standard for creating
  29. * a pipe. It's not the way Unix traditionally does this, though.
  30. */
  31. asmlinkage int sys_pipe(unsigned long r4, unsigned long r5,
  32. unsigned long r6, unsigned long r7,
  33. struct pt_regs __regs)
  34. {
  35. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  36. int fd[2];
  37. int error;
  38. error = do_pipe(fd);
  39. if (!error) {
  40. regs->regs[1] = fd[1];
  41. return fd[0];
  42. }
  43. return error;
  44. }
  45. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  46. EXPORT_SYMBOL(shm_align_mask);
  47. #ifdef CONFIG_MMU
  48. /*
  49. * To avoid cache aliases, we map the shared page with same color.
  50. */
  51. #define COLOUR_ALIGN(addr, pgoff) \
  52. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  53. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  54. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  55. unsigned long len, unsigned long pgoff, unsigned long flags)
  56. {
  57. struct mm_struct *mm = current->mm;
  58. struct vm_area_struct *vma;
  59. unsigned long start_addr;
  60. int do_colour_align;
  61. if (flags & MAP_FIXED) {
  62. /* We do not accept a shared mapping if it would violate
  63. * cache aliasing constraints.
  64. */
  65. if ((flags & MAP_SHARED) && (addr & shm_align_mask))
  66. return -EINVAL;
  67. return addr;
  68. }
  69. if (unlikely(len > TASK_SIZE))
  70. return -ENOMEM;
  71. do_colour_align = 0;
  72. if (filp || (flags & MAP_SHARED))
  73. do_colour_align = 1;
  74. if (addr) {
  75. if (do_colour_align)
  76. addr = COLOUR_ALIGN(addr, pgoff);
  77. else
  78. addr = PAGE_ALIGN(addr);
  79. vma = find_vma(mm, addr);
  80. if (TASK_SIZE - len >= addr &&
  81. (!vma || addr + len <= vma->vm_start))
  82. return addr;
  83. }
  84. if (len > mm->cached_hole_size) {
  85. start_addr = addr = mm->free_area_cache;
  86. } else {
  87. mm->cached_hole_size = 0;
  88. start_addr = addr = TASK_UNMAPPED_BASE;
  89. }
  90. full_search:
  91. if (do_colour_align)
  92. addr = COLOUR_ALIGN(addr, pgoff);
  93. else
  94. addr = PAGE_ALIGN(mm->free_area_cache);
  95. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  96. /* At this point: (!vma || addr < vma->vm_end). */
  97. if (unlikely(TASK_SIZE - len < addr)) {
  98. /*
  99. * Start a new search - just in case we missed
  100. * some holes.
  101. */
  102. if (start_addr != TASK_UNMAPPED_BASE) {
  103. start_addr = addr = TASK_UNMAPPED_BASE;
  104. mm->cached_hole_size = 0;
  105. goto full_search;
  106. }
  107. return -ENOMEM;
  108. }
  109. if (likely(!vma || addr + len <= vma->vm_start)) {
  110. /*
  111. * Remember the place where we stopped the search:
  112. */
  113. mm->free_area_cache = addr + len;
  114. return addr;
  115. }
  116. if (addr + mm->cached_hole_size < vma->vm_start)
  117. mm->cached_hole_size = vma->vm_start - addr;
  118. addr = vma->vm_end;
  119. if (do_colour_align)
  120. addr = COLOUR_ALIGN(addr, pgoff);
  121. }
  122. }
  123. #endif /* CONFIG_MMU */
  124. static inline long
  125. do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  126. unsigned long flags, int fd, unsigned long pgoff)
  127. {
  128. int error = -EBADF;
  129. struct file *file = NULL;
  130. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  131. if (!(flags & MAP_ANONYMOUS)) {
  132. file = fget(fd);
  133. if (!file)
  134. goto out;
  135. }
  136. down_write(&current->mm->mmap_sem);
  137. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  138. up_write(&current->mm->mmap_sem);
  139. if (file)
  140. fput(file);
  141. out:
  142. return error;
  143. }
  144. asmlinkage int old_mmap(unsigned long addr, unsigned long len,
  145. unsigned long prot, unsigned long flags,
  146. int fd, unsigned long off)
  147. {
  148. if (off & ~PAGE_MASK)
  149. return -EINVAL;
  150. return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
  151. }
  152. asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
  153. unsigned long prot, unsigned long flags,
  154. unsigned long fd, unsigned long pgoff)
  155. {
  156. return do_mmap2(addr, len, prot, flags, fd, pgoff);
  157. }
  158. /*
  159. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  160. *
  161. * This is really horribly ugly.
  162. */
  163. asmlinkage int sys_ipc(uint call, int first, int second,
  164. int third, void __user *ptr, long fifth)
  165. {
  166. int version, ret;
  167. version = call >> 16; /* hack for backward compatibility */
  168. call &= 0xffff;
  169. if (call <= SEMCTL)
  170. switch (call) {
  171. case SEMOP:
  172. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  173. second, NULL);
  174. case SEMTIMEDOP:
  175. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  176. second,
  177. (const struct timespec __user *)fifth);
  178. case SEMGET:
  179. return sys_semget (first, second, third);
  180. case SEMCTL: {
  181. union semun fourth;
  182. if (!ptr)
  183. return -EINVAL;
  184. if (get_user(fourth.__pad, (void * __user *) ptr))
  185. return -EFAULT;
  186. return sys_semctl (first, second, third, fourth);
  187. }
  188. default:
  189. return -EINVAL;
  190. }
  191. if (call <= MSGCTL)
  192. switch (call) {
  193. case MSGSND:
  194. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  195. second, third);
  196. case MSGRCV:
  197. switch (version) {
  198. case 0: {
  199. struct ipc_kludge tmp;
  200. if (!ptr)
  201. return -EINVAL;
  202. if (copy_from_user(&tmp,
  203. (struct ipc_kludge __user *) ptr,
  204. sizeof (tmp)))
  205. return -EFAULT;
  206. return sys_msgrcv (first, tmp.msgp, second,
  207. tmp.msgtyp, third);
  208. }
  209. default:
  210. return sys_msgrcv (first,
  211. (struct msgbuf __user *) ptr,
  212. second, fifth, third);
  213. }
  214. case MSGGET:
  215. return sys_msgget ((key_t) first, second);
  216. case MSGCTL:
  217. return sys_msgctl (first, second,
  218. (struct msqid_ds __user *) ptr);
  219. default:
  220. return -EINVAL;
  221. }
  222. if (call <= SHMCTL)
  223. switch (call) {
  224. case SHMAT:
  225. switch (version) {
  226. default: {
  227. ulong raddr;
  228. ret = do_shmat (first, (char __user *) ptr,
  229. second, &raddr);
  230. if (ret)
  231. return ret;
  232. return put_user (raddr, (ulong __user *) third);
  233. }
  234. case 1: /* iBCS2 emulator entry point */
  235. if (!segment_eq(get_fs(), get_ds()))
  236. return -EINVAL;
  237. return do_shmat (first, (char __user *) ptr,
  238. second, (ulong *) third);
  239. }
  240. case SHMDT:
  241. return sys_shmdt ((char __user *)ptr);
  242. case SHMGET:
  243. return sys_shmget (first, second, third);
  244. case SHMCTL:
  245. return sys_shmctl (first, second,
  246. (struct shmid_ds __user *) ptr);
  247. default:
  248. return -EINVAL;
  249. }
  250. return -EINVAL;
  251. }
  252. asmlinkage int sys_uname(struct old_utsname * name)
  253. {
  254. int err;
  255. if (!name)
  256. return -EFAULT;
  257. down_read(&uts_sem);
  258. err = copy_to_user(name, utsname(), sizeof (*name));
  259. up_read(&uts_sem);
  260. return err?-EFAULT:0;
  261. }
  262. asmlinkage ssize_t sys_pread_wrapper(unsigned int fd, char * buf,
  263. size_t count, long dummy, loff_t pos)
  264. {
  265. return sys_pread64(fd, buf, count, pos);
  266. }
  267. asmlinkage ssize_t sys_pwrite_wrapper(unsigned int fd, const char * buf,
  268. size_t count, long dummy, loff_t pos)
  269. {
  270. return sys_pwrite64(fd, buf, count, pos);
  271. }
  272. asmlinkage int sys_fadvise64_64_wrapper(int fd, u32 offset0, u32 offset1,
  273. u32 len0, u32 len1, int advice)
  274. {
  275. #ifdef __LITTLE_ENDIAN__
  276. return sys_fadvise64_64(fd, (u64)offset1 << 32 | offset0,
  277. (u64)len1 << 32 | len0, advice);
  278. #else
  279. return sys_fadvise64_64(fd, (u64)offset0 << 32 | offset1,
  280. (u64)len0 << 32 | len1, advice);
  281. #endif
  282. }
  283. #if defined(CONFIG_CPU_SH2) || defined(CONFIG_CPU_SH2A)
  284. #define SYSCALL_ARG3 "trapa #0x23"
  285. #else
  286. #define SYSCALL_ARG3 "trapa #0x13"
  287. #endif
  288. /*
  289. * Do a system call from kernel instead of calling sys_execve so we
  290. * end up with proper pt_regs.
  291. */
  292. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  293. {
  294. register long __sc0 __asm__ ("r3") = __NR_execve;
  295. register long __sc4 __asm__ ("r4") = (long) filename;
  296. register long __sc5 __asm__ ("r5") = (long) argv;
  297. register long __sc6 __asm__ ("r6") = (long) envp;
  298. __asm__ __volatile__ (SYSCALL_ARG3 : "=z" (__sc0)
  299. : "0" (__sc0), "r" (__sc4), "r" (__sc5), "r" (__sc6)
  300. : "memory");
  301. return __sc0;
  302. }