sys_sh.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <linux/fs.h>
  24. #include <linux/ipc.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/syscalls.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/unistd.h>
  29. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  30. EXPORT_SYMBOL(shm_align_mask);
  31. #ifdef CONFIG_MMU
  32. /*
  33. * To avoid cache aliases, we map the shared page with same color.
  34. */
  35. #define COLOUR_ALIGN(addr, pgoff) \
  36. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  37. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  38. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  39. unsigned long len, unsigned long pgoff, unsigned long flags)
  40. {
  41. struct mm_struct *mm = current->mm;
  42. struct vm_area_struct *vma;
  43. unsigned long start_addr;
  44. int do_colour_align;
  45. if (flags & MAP_FIXED) {
  46. /* We do not accept a shared mapping if it would violate
  47. * cache aliasing constraints.
  48. */
  49. if ((flags & MAP_SHARED) && (addr & shm_align_mask))
  50. return -EINVAL;
  51. return addr;
  52. }
  53. if (unlikely(len > TASK_SIZE))
  54. return -ENOMEM;
  55. do_colour_align = 0;
  56. if (filp || (flags & MAP_SHARED))
  57. do_colour_align = 1;
  58. if (addr) {
  59. if (do_colour_align)
  60. addr = COLOUR_ALIGN(addr, pgoff);
  61. else
  62. addr = PAGE_ALIGN(addr);
  63. vma = find_vma(mm, addr);
  64. if (TASK_SIZE - len >= addr &&
  65. (!vma || addr + len <= vma->vm_start))
  66. return addr;
  67. }
  68. if (len > mm->cached_hole_size) {
  69. start_addr = addr = mm->free_area_cache;
  70. } else {
  71. mm->cached_hole_size = 0;
  72. start_addr = addr = TASK_UNMAPPED_BASE;
  73. }
  74. full_search:
  75. if (do_colour_align)
  76. addr = COLOUR_ALIGN(addr, pgoff);
  77. else
  78. addr = PAGE_ALIGN(mm->free_area_cache);
  79. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  80. /* At this point: (!vma || addr < vma->vm_end). */
  81. if (unlikely(TASK_SIZE - len < addr)) {
  82. /*
  83. * Start a new search - just in case we missed
  84. * some holes.
  85. */
  86. if (start_addr != TASK_UNMAPPED_BASE) {
  87. start_addr = addr = TASK_UNMAPPED_BASE;
  88. mm->cached_hole_size = 0;
  89. goto full_search;
  90. }
  91. return -ENOMEM;
  92. }
  93. if (likely(!vma || addr + len <= vma->vm_start)) {
  94. /*
  95. * Remember the place where we stopped the search:
  96. */
  97. mm->free_area_cache = addr + len;
  98. return addr;
  99. }
  100. if (addr + mm->cached_hole_size < vma->vm_start)
  101. mm->cached_hole_size = vma->vm_start - addr;
  102. addr = vma->vm_end;
  103. if (do_colour_align)
  104. addr = COLOUR_ALIGN(addr, pgoff);
  105. }
  106. }
  107. #endif /* CONFIG_MMU */
  108. static inline long
  109. do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  110. unsigned long flags, int fd, unsigned long pgoff)
  111. {
  112. int error = -EBADF;
  113. struct file *file = NULL;
  114. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  115. if (!(flags & MAP_ANONYMOUS)) {
  116. file = fget(fd);
  117. if (!file)
  118. goto out;
  119. }
  120. down_write(&current->mm->mmap_sem);
  121. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  122. up_write(&current->mm->mmap_sem);
  123. if (file)
  124. fput(file);
  125. out:
  126. return error;
  127. }
  128. asmlinkage int old_mmap(unsigned long addr, unsigned long len,
  129. unsigned long prot, unsigned long flags,
  130. int fd, unsigned long off)
  131. {
  132. if (off & ~PAGE_MASK)
  133. return -EINVAL;
  134. return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
  135. }
  136. asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
  137. unsigned long prot, unsigned long flags,
  138. unsigned long fd, unsigned long pgoff)
  139. {
  140. return do_mmap2(addr, len, prot, flags, fd, pgoff);
  141. }
  142. /*
  143. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  144. *
  145. * This is really horribly ugly.
  146. */
  147. asmlinkage int sys_ipc(uint call, int first, int second,
  148. int third, void __user *ptr, long fifth)
  149. {
  150. int version, ret;
  151. version = call >> 16; /* hack for backward compatibility */
  152. call &= 0xffff;
  153. trace_mark(kernel_arch_ipc_call, "call %u first %d", call, first);
  154. if (call <= SEMTIMEDOP)
  155. switch (call) {
  156. case SEMOP:
  157. return sys_semtimedop(first,
  158. (struct sembuf __user *)ptr,
  159. second, NULL);
  160. case SEMTIMEDOP:
  161. return sys_semtimedop(first,
  162. (struct sembuf __user *)ptr, second,
  163. (const struct timespec __user *)fifth);
  164. case SEMGET:
  165. return sys_semget (first, second, third);
  166. case SEMCTL: {
  167. union semun fourth;
  168. if (!ptr)
  169. return -EINVAL;
  170. if (get_user(fourth.__pad, (void __user * __user *) ptr))
  171. return -EFAULT;
  172. return sys_semctl (first, second, third, fourth);
  173. }
  174. default:
  175. return -EINVAL;
  176. }
  177. if (call <= MSGCTL)
  178. switch (call) {
  179. case MSGSND:
  180. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  181. second, third);
  182. case MSGRCV:
  183. switch (version) {
  184. case 0:
  185. {
  186. struct ipc_kludge tmp;
  187. if (!ptr)
  188. return -EINVAL;
  189. if (copy_from_user(&tmp,
  190. (struct ipc_kludge __user *) ptr,
  191. sizeof (tmp)))
  192. return -EFAULT;
  193. return sys_msgrcv (first, tmp.msgp, second,
  194. tmp.msgtyp, third);
  195. }
  196. default:
  197. return sys_msgrcv (first,
  198. (struct msgbuf __user *) ptr,
  199. second, fifth, third);
  200. }
  201. case MSGGET:
  202. return sys_msgget ((key_t) first, second);
  203. case MSGCTL:
  204. return sys_msgctl (first, second,
  205. (struct msqid_ds __user *) ptr);
  206. default:
  207. return -EINVAL;
  208. }
  209. if (call <= SHMCTL)
  210. switch (call) {
  211. case SHMAT:
  212. switch (version) {
  213. default: {
  214. ulong raddr;
  215. ret = do_shmat (first, (char __user *) ptr,
  216. second, &raddr);
  217. if (ret)
  218. return ret;
  219. return put_user (raddr, (ulong __user *) third);
  220. }
  221. case 1: /* iBCS2 emulator entry point */
  222. if (!segment_eq(get_fs(), get_ds()))
  223. return -EINVAL;
  224. return do_shmat (first, (char __user *) ptr,
  225. second, (ulong *) third);
  226. }
  227. case SHMDT:
  228. return sys_shmdt ((char __user *)ptr);
  229. case SHMGET:
  230. return sys_shmget (first, second, third);
  231. case SHMCTL:
  232. return sys_shmctl (first, second,
  233. (struct shmid_ds __user *) ptr);
  234. default:
  235. return -EINVAL;
  236. }
  237. return -EINVAL;
  238. }
  239. asmlinkage int sys_uname(struct old_utsname __user *name)
  240. {
  241. int err;
  242. if (!name)
  243. return -EFAULT;
  244. down_read(&uts_sem);
  245. err = copy_to_user(name, utsname(), sizeof(*name));
  246. up_read(&uts_sem);
  247. return err?-EFAULT:0;
  248. }