sys_sh.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/smp_lock.h>
  15. #include <linux/sem.h>
  16. #include <linux/msg.h>
  17. #include <linux/shm.h>
  18. #include <linux/stat.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/mman.h>
  21. #include <linux/file.h>
  22. #include <linux/utsname.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/ipc.h>
  25. /*
  26. * sys_pipe() is the normal C calling standard for creating
  27. * a pipe. It's not the way Unix traditionally does this, though.
  28. */
  29. asmlinkage int sys_pipe(unsigned long r4, unsigned long r5,
  30. unsigned long r6, unsigned long r7,
  31. struct pt_regs regs)
  32. {
  33. int fd[2];
  34. int error;
  35. error = do_pipe(fd);
  36. if (!error) {
  37. regs.regs[1] = fd[1];
  38. return fd[0];
  39. }
  40. return error;
  41. }
  42. #if defined(HAVE_ARCH_UNMAPPED_AREA)
  43. /*
  44. * To avoid cache alias, we map the shard page with same color.
  45. */
  46. #define COLOUR_ALIGN(addr) (((addr)+SHMLBA-1)&~(SHMLBA-1))
  47. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  48. unsigned long len, unsigned long pgoff, unsigned long flags)
  49. {
  50. struct mm_struct *mm = current->mm;
  51. struct vm_area_struct *vma;
  52. unsigned long start_addr;
  53. if (flags & MAP_FIXED) {
  54. /* We do not accept a shared mapping if it would violate
  55. * cache aliasing constraints.
  56. */
  57. if ((flags & MAP_SHARED) && (addr & (SHMLBA - 1)))
  58. return -EINVAL;
  59. return addr;
  60. }
  61. if (len > TASK_SIZE)
  62. return -ENOMEM;
  63. if (addr) {
  64. if (flags & MAP_PRIVATE)
  65. addr = PAGE_ALIGN(addr);
  66. else
  67. addr = COLOUR_ALIGN(addr);
  68. vma = find_vma(mm, addr);
  69. if (TASK_SIZE - len >= addr &&
  70. (!vma || addr + len <= vma->vm_start))
  71. return addr;
  72. }
  73. if (len <= mm->cached_hole_size) {
  74. mm->cached_hole_size = 0;
  75. mm->free_area_cache = TASK_UNMAPPED_BASE;
  76. }
  77. if (flags & MAP_PRIVATE)
  78. addr = PAGE_ALIGN(mm->free_area_cache);
  79. else
  80. addr = COLOUR_ALIGN(mm->free_area_cache);
  81. start_addr = addr;
  82. full_search:
  83. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  84. /* At this point: (!vma || addr < vma->vm_end). */
  85. if (TASK_SIZE - len < addr) {
  86. /*
  87. * Start a new search - just in case we missed
  88. * some holes.
  89. */
  90. if (start_addr != TASK_UNMAPPED_BASE) {
  91. start_addr = addr = TASK_UNMAPPED_BASE;
  92. mm->cached_hole_size = 0;
  93. goto full_search;
  94. }
  95. return -ENOMEM;
  96. }
  97. if (!vma || addr + len <= vma->vm_start) {
  98. /*
  99. * Remember the place where we stopped the search:
  100. */
  101. mm->free_area_cache = addr + len;
  102. return addr;
  103. }
  104. if (addr + mm->cached_hole_size < vma->vm_start)
  105. mm->cached_hole_size = vma->vm_start - addr;
  106. addr = vma->vm_end;
  107. if (!(flags & MAP_PRIVATE))
  108. addr = COLOUR_ALIGN(addr);
  109. }
  110. }
  111. #endif
  112. static inline long
  113. do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  114. unsigned long flags, int fd, unsigned long pgoff)
  115. {
  116. int error = -EBADF;
  117. struct file *file = NULL;
  118. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  119. if (!(flags & MAP_ANONYMOUS)) {
  120. file = fget(fd);
  121. if (!file)
  122. goto out;
  123. }
  124. down_write(&current->mm->mmap_sem);
  125. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  126. up_write(&current->mm->mmap_sem);
  127. if (file)
  128. fput(file);
  129. out:
  130. return error;
  131. }
  132. asmlinkage int old_mmap(unsigned long addr, unsigned long len,
  133. unsigned long prot, unsigned long flags,
  134. int fd, unsigned long off)
  135. {
  136. if (off & ~PAGE_MASK)
  137. return -EINVAL;
  138. return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
  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. /*
  147. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  148. *
  149. * This is really horribly ugly.
  150. */
  151. asmlinkage int sys_ipc(uint call, int first, int second,
  152. int third, void __user *ptr, long fifth)
  153. {
  154. int version, ret;
  155. version = call >> 16; /* hack for backward compatibility */
  156. call &= 0xffff;
  157. if (call <= SEMCTL)
  158. switch (call) {
  159. case SEMOP:
  160. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  161. second, NULL);
  162. case SEMTIMEDOP:
  163. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  164. second,
  165. (const struct timespec __user *)fifth);
  166. case SEMGET:
  167. return sys_semget (first, second, third);
  168. case SEMCTL: {
  169. union semun fourth;
  170. if (!ptr)
  171. return -EINVAL;
  172. if (get_user(fourth.__pad, (void * __user *) ptr))
  173. return -EFAULT;
  174. return sys_semctl (first, second, third, fourth);
  175. }
  176. default:
  177. return -EINVAL;
  178. }
  179. if (call <= MSGCTL)
  180. switch (call) {
  181. case MSGSND:
  182. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  183. second, third);
  184. case MSGRCV:
  185. switch (version) {
  186. case 0: {
  187. struct ipc_kludge tmp;
  188. if (!ptr)
  189. return -EINVAL;
  190. if (copy_from_user(&tmp,
  191. (struct ipc_kludge __user *) ptr,
  192. sizeof (tmp)))
  193. return -EFAULT;
  194. return sys_msgrcv (first, tmp.msgp, second,
  195. tmp.msgtyp, third);
  196. }
  197. default:
  198. return sys_msgrcv (first,
  199. (struct msgbuf __user *) ptr,
  200. second, fifth, third);
  201. }
  202. case MSGGET:
  203. return sys_msgget ((key_t) first, second);
  204. case MSGCTL:
  205. return sys_msgctl (first, second,
  206. (struct msqid_ds __user *) ptr);
  207. default:
  208. return -EINVAL;
  209. }
  210. if (call <= SHMCTL)
  211. switch (call) {
  212. case SHMAT:
  213. switch (version) {
  214. default: {
  215. ulong raddr;
  216. ret = do_shmat (first, (char __user *) ptr,
  217. second, &raddr);
  218. if (ret)
  219. return ret;
  220. return put_user (raddr, (ulong __user *) third);
  221. }
  222. case 1: /* iBCS2 emulator entry point */
  223. if (!segment_eq(get_fs(), get_ds()))
  224. return -EINVAL;
  225. return do_shmat (first, (char __user *) ptr,
  226. second, (ulong *) third);
  227. }
  228. case SHMDT:
  229. return sys_shmdt ((char __user *)ptr);
  230. case SHMGET:
  231. return sys_shmget (first, second, third);
  232. case SHMCTL:
  233. return sys_shmctl (first, second,
  234. (struct shmid_ds __user *) ptr);
  235. default:
  236. return -EINVAL;
  237. }
  238. return -EINVAL;
  239. }
  240. asmlinkage int sys_uname(struct old_utsname * name)
  241. {
  242. int err;
  243. if (!name)
  244. return -EFAULT;
  245. down_read(&uts_sem);
  246. err=copy_to_user(name, &system_utsname, sizeof (*name));
  247. up_read(&uts_sem);
  248. return err?-EFAULT:0;
  249. }
  250. asmlinkage ssize_t sys_pread_wrapper(unsigned int fd, char * buf,
  251. size_t count, long dummy, loff_t pos)
  252. {
  253. return sys_pread64(fd, buf, count, pos);
  254. }
  255. asmlinkage ssize_t sys_pwrite_wrapper(unsigned int fd, const char * buf,
  256. size_t count, long dummy, loff_t pos)
  257. {
  258. return sys_pwrite64(fd, buf, count, pos);
  259. }
  260. asmlinkage int sys_fadvise64_64_wrapper(int fd, u32 offset0, u32 offset1,
  261. u32 len0, u32 len1, int advice)
  262. {
  263. #ifdef __LITTLE_ENDIAN__
  264. return sys_fadvise64_64(fd, (u64)offset1 << 32 | offset0,
  265. (u64)len1 << 32 | len0, advice);
  266. #else
  267. return sys_fadvise64_64(fd, (u64)offset0 << 32 | offset1,
  268. (u64)len0 << 32 | len1, advice);
  269. #endif
  270. }