syscall.c 11 KB

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