syscall.c 9.4 KB

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