syscall.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1995, 1996, 1997, 2000, 2001, 05 by Ralf Baechle
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Copyright (C) 2001 MIPS Technologies, Inc.
  9. */
  10. #include <linux/capability.h>
  11. #include <linux/errno.h>
  12. #include <linux/linkage.h>
  13. #include <linux/fs.h>
  14. #include <linux/smp.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/string.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/file.h>
  19. #include <linux/utsname.h>
  20. #include <linux/unistd.h>
  21. #include <linux/sem.h>
  22. #include <linux/msg.h>
  23. #include <linux/shm.h>
  24. #include <linux/compiler.h>
  25. #include <linux/ipc.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/slab.h>
  28. #include <linux/elf.h>
  29. #include <asm/asm.h>
  30. #include <asm/branch.h>
  31. #include <asm/cachectl.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/asm-offsets.h>
  34. #include <asm/signal.h>
  35. #include <asm/sim.h>
  36. #include <asm/shmparam.h>
  37. #include <asm/sysmips.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/switch_to.h>
  40. /*
  41. * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
  42. * convention. It returns results in registers $v0 / $v1 which means there
  43. * is no need for it to do verify the validity of a userspace pointer
  44. * argument. Historically that used to be expensive in Linux. These days
  45. * the performance advantage is negligible.
  46. */
  47. asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
  48. {
  49. int fd[2];
  50. int error, res;
  51. error = do_pipe_flags(fd, 0);
  52. if (error) {
  53. res = error;
  54. goto out;
  55. }
  56. regs.regs[3] = fd[1];
  57. res = fd[0];
  58. out:
  59. return res;
  60. }
  61. SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
  62. unsigned long, prot, unsigned long, flags, unsigned long,
  63. fd, off_t, offset)
  64. {
  65. unsigned long result;
  66. result = -EINVAL;
  67. if (offset & ~PAGE_MASK)
  68. goto out;
  69. result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  70. out:
  71. return result;
  72. }
  73. SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
  74. unsigned long, prot, unsigned long, flags, unsigned long, fd,
  75. unsigned long, pgoff)
  76. {
  77. if (pgoff & (~PAGE_MASK >> 12))
  78. return -EINVAL;
  79. return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  80. }
  81. save_static_function(sys_fork);
  82. static int __used noinline
  83. _sys_fork(nabi_no_regargs struct pt_regs regs)
  84. {
  85. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  86. }
  87. save_static_function(sys_clone);
  88. static int __used noinline
  89. _sys_clone(nabi_no_regargs struct pt_regs regs)
  90. {
  91. unsigned long clone_flags;
  92. unsigned long newsp;
  93. int __user *parent_tidptr, *child_tidptr;
  94. clone_flags = regs.regs[4];
  95. newsp = regs.regs[5];
  96. if (!newsp)
  97. newsp = regs.regs[29];
  98. parent_tidptr = (int __user *) regs.regs[6];
  99. #ifdef CONFIG_32BIT
  100. /* We need to fetch the fifth argument off the stack. */
  101. child_tidptr = NULL;
  102. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  103. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  104. if (regs.regs[2] == __NR_syscall) {
  105. if (get_user (child_tidptr, &usp[5]))
  106. return -EFAULT;
  107. }
  108. else if (get_user (child_tidptr, &usp[4]))
  109. return -EFAULT;
  110. }
  111. #else
  112. child_tidptr = (int __user *) regs.regs[8];
  113. #endif
  114. return do_fork(clone_flags, newsp, &regs, 0,
  115. parent_tidptr, child_tidptr);
  116. }
  117. /*
  118. * sys_execve() executes a new program.
  119. */
  120. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  121. {
  122. int error;
  123. char * filename;
  124. filename = getname((const char __user *) (long)regs.regs[4]);
  125. error = PTR_ERR(filename);
  126. if (IS_ERR(filename))
  127. goto out;
  128. error = do_execve(filename,
  129. (const char __user *const __user *) (long)regs.regs[5],
  130. (const char __user *const __user *) (long)regs.regs[6],
  131. &regs);
  132. putname(filename);
  133. out:
  134. return error;
  135. }
  136. SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
  137. {
  138. struct thread_info *ti = task_thread_info(current);
  139. ti->tp_value = addr;
  140. if (cpu_has_userlocal)
  141. write_c0_userlocal(addr);
  142. return 0;
  143. }
  144. static inline int mips_atomic_set(struct pt_regs *regs,
  145. unsigned long addr, unsigned long new)
  146. {
  147. unsigned long old, tmp;
  148. unsigned int err;
  149. if (unlikely(addr & 3))
  150. return -EINVAL;
  151. if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
  152. return -EINVAL;
  153. if (cpu_has_llsc && R10000_LLSC_WAR) {
  154. __asm__ __volatile__ (
  155. " .set mips3 \n"
  156. " li %[err], 0 \n"
  157. "1: ll %[old], (%[addr]) \n"
  158. " move %[tmp], %[new] \n"
  159. "2: sc %[tmp], (%[addr]) \n"
  160. " beqzl %[tmp], 1b \n"
  161. "3: \n"
  162. " .section .fixup,\"ax\" \n"
  163. "4: li %[err], %[efault] \n"
  164. " j 3b \n"
  165. " .previous \n"
  166. " .section __ex_table,\"a\" \n"
  167. " "STR(PTR)" 1b, 4b \n"
  168. " "STR(PTR)" 2b, 4b \n"
  169. " .previous \n"
  170. " .set mips0 \n"
  171. : [old] "=&r" (old),
  172. [err] "=&r" (err),
  173. [tmp] "=&r" (tmp)
  174. : [addr] "r" (addr),
  175. [new] "r" (new),
  176. [efault] "i" (-EFAULT)
  177. : "memory");
  178. } else if (cpu_has_llsc) {
  179. __asm__ __volatile__ (
  180. " .set mips3 \n"
  181. " li %[err], 0 \n"
  182. "1: ll %[old], (%[addr]) \n"
  183. " move %[tmp], %[new] \n"
  184. "2: sc %[tmp], (%[addr]) \n"
  185. " bnez %[tmp], 4f \n"
  186. "3: \n"
  187. " .subsection 2 \n"
  188. "4: b 1b \n"
  189. " .previous \n"
  190. " \n"
  191. " .section .fixup,\"ax\" \n"
  192. "5: li %[err], %[efault] \n"
  193. " j 3b \n"
  194. " .previous \n"
  195. " .section __ex_table,\"a\" \n"
  196. " "STR(PTR)" 1b, 5b \n"
  197. " "STR(PTR)" 2b, 5b \n"
  198. " .previous \n"
  199. " .set mips0 \n"
  200. : [old] "=&r" (old),
  201. [err] "=&r" (err),
  202. [tmp] "=&r" (tmp)
  203. : [addr] "r" (addr),
  204. [new] "r" (new),
  205. [efault] "i" (-EFAULT)
  206. : "memory");
  207. } else {
  208. do {
  209. preempt_disable();
  210. ll_bit = 1;
  211. ll_task = current;
  212. preempt_enable();
  213. err = __get_user(old, (unsigned int *) addr);
  214. err |= __put_user(new, (unsigned int *) addr);
  215. if (err)
  216. break;
  217. rmb();
  218. } while (!ll_bit);
  219. }
  220. if (unlikely(err))
  221. return err;
  222. regs->regs[2] = old;
  223. regs->regs[7] = 0; /* No error */
  224. /*
  225. * Don't let your children do this ...
  226. */
  227. __asm__ __volatile__(
  228. " move $29, %0 \n"
  229. " j syscall_exit \n"
  230. : /* no outputs */
  231. : "r" (regs));
  232. /* unreached. Honestly. */
  233. while (1);
  234. }
  235. save_static_function(sys_sysmips);
  236. static int __used noinline
  237. _sys_sysmips(nabi_no_regargs struct pt_regs regs)
  238. {
  239. long cmd, arg1, arg2;
  240. cmd = regs.regs[4];
  241. arg1 = regs.regs[5];
  242. arg2 = regs.regs[6];
  243. switch (cmd) {
  244. case MIPS_ATOMIC_SET:
  245. return mips_atomic_set(&regs, arg1, arg2);
  246. case MIPS_FIXADE:
  247. if (arg1 & ~3)
  248. return -EINVAL;
  249. if (arg1 & 1)
  250. set_thread_flag(TIF_FIXADE);
  251. else
  252. clear_thread_flag(TIF_FIXADE);
  253. if (arg1 & 2)
  254. set_thread_flag(TIF_LOGADE);
  255. else
  256. clear_thread_flag(TIF_LOGADE);
  257. return 0;
  258. case FLUSH_CACHE:
  259. __flush_cache_all();
  260. return 0;
  261. }
  262. return -EINVAL;
  263. }
  264. /*
  265. * No implemented yet ...
  266. */
  267. SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
  268. {
  269. return -ENOSYS;
  270. }
  271. /*
  272. * If we ever come here the user sp is bad. Zap the process right away.
  273. * Due to the bad stack signaling wouldn't work.
  274. */
  275. asmlinkage void bad_stack(void)
  276. {
  277. do_exit(SIGSEGV);
  278. }
  279. /*
  280. * Do a system call from kernel instead of calling sys_execve so we
  281. * end up with proper pt_regs.
  282. */
  283. int kernel_execve(const char *filename,
  284. const char *const argv[],
  285. const char *const envp[])
  286. {
  287. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  288. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  289. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  290. register unsigned long __a3 asm("$7");
  291. unsigned long __v0;
  292. __asm__ volatile (" \n"
  293. " .set noreorder \n"
  294. " li $2, %5 # __NR_execve \n"
  295. " syscall \n"
  296. " move %0, $2 \n"
  297. " .set reorder \n"
  298. : "=&r" (__v0), "=r" (__a3)
  299. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  300. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  301. "memory");
  302. if (__a3 == 0)
  303. return __v0;
  304. return -__v0;
  305. }