sys_arm.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * linux/arch/arm/kernel/sys_arm.c
  3. *
  4. * Copyright (C) People who wrote linux/arch/i386/kernel/sys_i386.c
  5. * Copyright (C) 1995, 1996 Russell King.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This file contains various random system calls that
  12. * have a non-standard calling sequence on the Linux/arm
  13. * platform.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/mm.h>
  20. #include <linux/sem.h>
  21. #include <linux/msg.h>
  22. #include <linux/shm.h>
  23. #include <linux/stat.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/mman.h>
  26. #include <linux/fs.h>
  27. #include <linux/file.h>
  28. #include <linux/utsname.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/ipc.h>
  31. extern unsigned long do_mremap(unsigned long addr, unsigned long old_len,
  32. unsigned long new_len, unsigned long flags,
  33. unsigned long new_addr);
  34. /*
  35. * sys_pipe() is the normal C calling standard for creating
  36. * a pipe. It's not the way unix traditionally does this, though.
  37. */
  38. asmlinkage int sys_pipe(unsigned long __user *fildes)
  39. {
  40. int fd[2];
  41. int error;
  42. error = do_pipe(fd);
  43. if (!error) {
  44. if (copy_to_user(fildes, fd, 2*sizeof(int)))
  45. error = -EFAULT;
  46. }
  47. return error;
  48. }
  49. /*
  50. * This is the lowest virtual address we can permit any user space
  51. * mapping to be mapped at. This is particularly important for
  52. * non-high vector CPUs.
  53. */
  54. #define MIN_MAP_ADDR (PAGE_SIZE)
  55. /* common code for old and new mmaps */
  56. inline long do_mmap2(
  57. unsigned long addr, unsigned long len,
  58. unsigned long prot, unsigned long flags,
  59. unsigned long fd, unsigned long pgoff)
  60. {
  61. int error = -EINVAL;
  62. struct file * file = NULL;
  63. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  64. if (flags & MAP_FIXED && addr < MIN_MAP_ADDR)
  65. goto out;
  66. error = -EBADF;
  67. if (!(flags & MAP_ANONYMOUS)) {
  68. file = fget(fd);
  69. if (!file)
  70. goto out;
  71. }
  72. down_write(&current->mm->mmap_sem);
  73. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  74. up_write(&current->mm->mmap_sem);
  75. if (file)
  76. fput(file);
  77. out:
  78. return error;
  79. }
  80. struct mmap_arg_struct {
  81. unsigned long addr;
  82. unsigned long len;
  83. unsigned long prot;
  84. unsigned long flags;
  85. unsigned long fd;
  86. unsigned long offset;
  87. };
  88. asmlinkage int old_mmap(struct mmap_arg_struct __user *arg)
  89. {
  90. int error = -EFAULT;
  91. struct mmap_arg_struct a;
  92. if (copy_from_user(&a, arg, sizeof(a)))
  93. goto out;
  94. error = -EINVAL;
  95. if (a.offset & ~PAGE_MASK)
  96. goto out;
  97. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
  98. out:
  99. return error;
  100. }
  101. asmlinkage unsigned long
  102. sys_arm_mremap(unsigned long addr, unsigned long old_len,
  103. unsigned long new_len, unsigned long flags,
  104. unsigned long new_addr)
  105. {
  106. unsigned long ret = -EINVAL;
  107. if (flags & MREMAP_FIXED && new_addr < MIN_MAP_ADDR)
  108. goto out;
  109. down_write(&current->mm->mmap_sem);
  110. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  111. up_write(&current->mm->mmap_sem);
  112. out:
  113. return ret;
  114. }
  115. /*
  116. * Perform the select(nd, in, out, ex, tv) and mmap() system
  117. * calls.
  118. */
  119. struct sel_arg_struct {
  120. unsigned long n;
  121. fd_set __user *inp, *outp, *exp;
  122. struct timeval __user *tvp;
  123. };
  124. asmlinkage int old_select(struct sel_arg_struct __user *arg)
  125. {
  126. struct sel_arg_struct a;
  127. if (copy_from_user(&a, arg, sizeof(a)))
  128. return -EFAULT;
  129. /* sys_select() does the appropriate kernel locking */
  130. return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  131. }
  132. /*
  133. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  134. *
  135. * This is really horribly ugly.
  136. */
  137. asmlinkage int sys_ipc(uint call, int first, int second, int third,
  138. void __user *ptr, long fifth)
  139. {
  140. int version, ret;
  141. version = call >> 16; /* hack for backward compatibility */
  142. call &= 0xffff;
  143. switch (call) {
  144. case SEMOP:
  145. return sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL);
  146. case SEMTIMEDOP:
  147. return sys_semtimedop(first, (struct sembuf __user *)ptr, second,
  148. (const struct timespec __user *)fifth);
  149. case SEMGET:
  150. return sys_semget (first, second, third);
  151. case SEMCTL: {
  152. union semun fourth;
  153. if (!ptr)
  154. return -EINVAL;
  155. if (get_user(fourth.__pad, (void __user * __user *) ptr))
  156. return -EFAULT;
  157. return sys_semctl (first, second, third, fourth);
  158. }
  159. case MSGSND:
  160. return sys_msgsnd(first, (struct msgbuf __user *) ptr,
  161. second, third);
  162. case MSGRCV:
  163. switch (version) {
  164. case 0: {
  165. struct ipc_kludge tmp;
  166. if (!ptr)
  167. return -EINVAL;
  168. if (copy_from_user(&tmp,(struct ipc_kludge __user *)ptr,
  169. sizeof (tmp)))
  170. return -EFAULT;
  171. return sys_msgrcv (first, tmp.msgp, second,
  172. tmp.msgtyp, third);
  173. }
  174. default:
  175. return sys_msgrcv (first,
  176. (struct msgbuf __user *) ptr,
  177. second, fifth, third);
  178. }
  179. case MSGGET:
  180. return sys_msgget ((key_t) first, second);
  181. case MSGCTL:
  182. return sys_msgctl(first, second, (struct msqid_ds __user *)ptr);
  183. case SHMAT:
  184. switch (version) {
  185. default: {
  186. ulong raddr;
  187. ret = do_shmat(first, (char __user *)ptr, second, &raddr);
  188. if (ret)
  189. return ret;
  190. return put_user(raddr, (ulong __user *)third);
  191. }
  192. case 1: /* Of course, we don't support iBCS2! */
  193. return -EINVAL;
  194. }
  195. case SHMDT:
  196. return sys_shmdt ((char __user *)ptr);
  197. case SHMGET:
  198. return sys_shmget (first, second, third);
  199. case SHMCTL:
  200. return sys_shmctl (first, second,
  201. (struct shmid_ds __user *) ptr);
  202. default:
  203. return -ENOSYS;
  204. }
  205. }
  206. asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg,
  207. unsigned long __user *addr)
  208. {
  209. unsigned long ret;
  210. long err;
  211. err = do_shmat(shmid, shmaddr, shmflg, &ret);
  212. if (err == 0)
  213. err = put_user(ret, addr);
  214. return err;
  215. }
  216. /* Fork a new task - this creates a new program thread.
  217. * This is called indirectly via a small wrapper
  218. */
  219. asmlinkage int sys_fork(struct pt_regs *regs)
  220. {
  221. return do_fork(SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
  222. }
  223. /* Clone a task - this clones the calling program thread.
  224. * This is called indirectly via a small wrapper
  225. */
  226. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  227. int __user *parent_tidptr, int tls_val,
  228. int __user *child_tidptr, struct pt_regs *regs)
  229. {
  230. if (!newsp)
  231. newsp = regs->ARM_sp;
  232. return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr);
  233. }
  234. asmlinkage int sys_vfork(struct pt_regs *regs)
  235. {
  236. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
  237. }
  238. /* sys_execve() executes a new program.
  239. * This is called indirectly via a small wrapper
  240. */
  241. asmlinkage int sys_execve(char __user *filenamei, char __user * __user *argv,
  242. char __user * __user *envp, struct pt_regs *regs)
  243. {
  244. int error;
  245. char * filename;
  246. filename = getname(filenamei);
  247. error = PTR_ERR(filename);
  248. if (IS_ERR(filename))
  249. goto out;
  250. error = do_execve(filename, argv, envp, regs);
  251. putname(filename);
  252. out:
  253. return error;
  254. }
  255. long execve(const char *filename, char **argv, char **envp)
  256. {
  257. struct pt_regs regs;
  258. int ret;
  259. memset(&regs, 0, sizeof(struct pt_regs));
  260. ret = do_execve((char *)filename, (char __user * __user *)argv,
  261. (char __user * __user *)envp, &regs);
  262. if (ret < 0)
  263. goto out;
  264. /*
  265. * Save argc to the register structure for userspace.
  266. */
  267. regs.ARM_r0 = ret;
  268. /*
  269. * We were successful. We won't be returning to our caller, but
  270. * instead to user space by manipulating the kernel stack.
  271. */
  272. asm( "add r0, %0, %1\n\t"
  273. "mov r1, %2\n\t"
  274. "mov r2, %3\n\t"
  275. "bl memmove\n\t" /* copy regs to top of stack */
  276. "mov r8, #0\n\t" /* not a syscall */
  277. "mov r9, %0\n\t" /* thread structure */
  278. "mov sp, r0\n\t" /* reposition stack pointer */
  279. "b ret_to_user"
  280. :
  281. : "r" (current_thread_info()),
  282. "Ir" (THREAD_SIZE - 8 - sizeof(regs)),
  283. "r" (&regs),
  284. "Ir" (sizeof(regs))
  285. : "r0", "r1", "r2", "r3", "ip", "memory");
  286. out:
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(execve);