syscall.c 7.8 KB

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