syscall.c 9.6 KB

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