sys_sh.c 6.3 KB

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