syscall.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. 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 = current->mm->mmap_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. void arch_pick_mmap_layout(struct mm_struct *mm)
  120. {
  121. unsigned long random_factor = 0UL;
  122. if (current->flags & PF_RANDOMIZE) {
  123. random_factor = get_random_int();
  124. random_factor = random_factor << PAGE_SHIFT;
  125. if (TASK_IS_32BIT_ADDR)
  126. random_factor &= 0xfffffful;
  127. else
  128. random_factor &= 0xffffffful;
  129. }
  130. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  131. mm->get_unmapped_area = arch_get_unmapped_area;
  132. mm->unmap_area = arch_unmap_area;
  133. }
  134. static inline unsigned long brk_rnd(void)
  135. {
  136. unsigned long rnd = get_random_int();
  137. rnd = rnd << PAGE_SHIFT;
  138. /* 8MB for 32bit, 256MB for 64bit */
  139. if (TASK_IS_32BIT_ADDR)
  140. rnd = rnd & 0x7ffffful;
  141. else
  142. rnd = rnd & 0xffffffful;
  143. return rnd;
  144. }
  145. unsigned long arch_randomize_brk(struct mm_struct *mm)
  146. {
  147. unsigned long base = mm->brk;
  148. unsigned long ret;
  149. ret = PAGE_ALIGN(base + brk_rnd());
  150. if (ret < mm->brk)
  151. return mm->brk;
  152. return ret;
  153. }
  154. SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
  155. unsigned long, prot, unsigned long, flags, unsigned long,
  156. fd, off_t, offset)
  157. {
  158. unsigned long result;
  159. result = -EINVAL;
  160. if (offset & ~PAGE_MASK)
  161. goto out;
  162. result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  163. out:
  164. return result;
  165. }
  166. SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
  167. unsigned long, prot, unsigned long, flags, unsigned long, fd,
  168. unsigned long, pgoff)
  169. {
  170. if (pgoff & (~PAGE_MASK >> 12))
  171. return -EINVAL;
  172. return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  173. }
  174. save_static_function(sys_fork);
  175. static int __used noinline
  176. _sys_fork(nabi_no_regargs struct pt_regs regs)
  177. {
  178. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  179. }
  180. save_static_function(sys_clone);
  181. static int __used noinline
  182. _sys_clone(nabi_no_regargs struct pt_regs regs)
  183. {
  184. unsigned long clone_flags;
  185. unsigned long newsp;
  186. int __user *parent_tidptr, *child_tidptr;
  187. clone_flags = regs.regs[4];
  188. newsp = regs.regs[5];
  189. if (!newsp)
  190. newsp = regs.regs[29];
  191. parent_tidptr = (int __user *) regs.regs[6];
  192. #ifdef CONFIG_32BIT
  193. /* We need to fetch the fifth argument off the stack. */
  194. child_tidptr = NULL;
  195. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  196. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  197. if (regs.regs[2] == __NR_syscall) {
  198. if (get_user (child_tidptr, &usp[5]))
  199. return -EFAULT;
  200. }
  201. else if (get_user (child_tidptr, &usp[4]))
  202. return -EFAULT;
  203. }
  204. #else
  205. child_tidptr = (int __user *) regs.regs[8];
  206. #endif
  207. return do_fork(clone_flags, newsp, &regs, 0,
  208. parent_tidptr, child_tidptr);
  209. }
  210. /*
  211. * sys_execve() executes a new program.
  212. */
  213. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  214. {
  215. int error;
  216. char * filename;
  217. filename = getname((const char __user *) (long)regs.regs[4]);
  218. error = PTR_ERR(filename);
  219. if (IS_ERR(filename))
  220. goto out;
  221. error = do_execve(filename,
  222. (const char __user *const __user *) (long)regs.regs[5],
  223. (const char __user *const __user *) (long)regs.regs[6],
  224. &regs);
  225. putname(filename);
  226. out:
  227. return error;
  228. }
  229. SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
  230. {
  231. struct thread_info *ti = task_thread_info(current);
  232. ti->tp_value = addr;
  233. if (cpu_has_userlocal)
  234. write_c0_userlocal(addr);
  235. return 0;
  236. }
  237. static inline int mips_atomic_set(struct pt_regs *regs,
  238. unsigned long addr, unsigned long new)
  239. {
  240. unsigned long old, tmp;
  241. unsigned int err;
  242. if (unlikely(addr & 3))
  243. return -EINVAL;
  244. if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
  245. return -EINVAL;
  246. if (cpu_has_llsc && R10000_LLSC_WAR) {
  247. __asm__ __volatile__ (
  248. " .set mips3 \n"
  249. " li %[err], 0 \n"
  250. "1: ll %[old], (%[addr]) \n"
  251. " move %[tmp], %[new] \n"
  252. "2: sc %[tmp], (%[addr]) \n"
  253. " beqzl %[tmp], 1b \n"
  254. "3: \n"
  255. " .section .fixup,\"ax\" \n"
  256. "4: li %[err], %[efault] \n"
  257. " j 3b \n"
  258. " .previous \n"
  259. " .section __ex_table,\"a\" \n"
  260. " "STR(PTR)" 1b, 4b \n"
  261. " "STR(PTR)" 2b, 4b \n"
  262. " .previous \n"
  263. " .set mips0 \n"
  264. : [old] "=&r" (old),
  265. [err] "=&r" (err),
  266. [tmp] "=&r" (tmp)
  267. : [addr] "r" (addr),
  268. [new] "r" (new),
  269. [efault] "i" (-EFAULT)
  270. : "memory");
  271. } else if (cpu_has_llsc) {
  272. __asm__ __volatile__ (
  273. " .set mips3 \n"
  274. " li %[err], 0 \n"
  275. "1: ll %[old], (%[addr]) \n"
  276. " move %[tmp], %[new] \n"
  277. "2: sc %[tmp], (%[addr]) \n"
  278. " bnez %[tmp], 4f \n"
  279. "3: \n"
  280. " .subsection 2 \n"
  281. "4: b 1b \n"
  282. " .previous \n"
  283. " \n"
  284. " .section .fixup,\"ax\" \n"
  285. "5: li %[err], %[efault] \n"
  286. " j 3b \n"
  287. " .previous \n"
  288. " .section __ex_table,\"a\" \n"
  289. " "STR(PTR)" 1b, 5b \n"
  290. " "STR(PTR)" 2b, 5b \n"
  291. " .previous \n"
  292. " .set mips0 \n"
  293. : [old] "=&r" (old),
  294. [err] "=&r" (err),
  295. [tmp] "=&r" (tmp)
  296. : [addr] "r" (addr),
  297. [new] "r" (new),
  298. [efault] "i" (-EFAULT)
  299. : "memory");
  300. } else {
  301. do {
  302. preempt_disable();
  303. ll_bit = 1;
  304. ll_task = current;
  305. preempt_enable();
  306. err = __get_user(old, (unsigned int *) addr);
  307. err |= __put_user(new, (unsigned int *) addr);
  308. if (err)
  309. break;
  310. rmb();
  311. } while (!ll_bit);
  312. }
  313. if (unlikely(err))
  314. return err;
  315. regs->regs[2] = old;
  316. regs->regs[7] = 0; /* No error */
  317. /*
  318. * Don't let your children do this ...
  319. */
  320. __asm__ __volatile__(
  321. " move $29, %0 \n"
  322. " j syscall_exit \n"
  323. : /* no outputs */
  324. : "r" (regs));
  325. /* unreached. Honestly. */
  326. while (1);
  327. }
  328. save_static_function(sys_sysmips);
  329. static int __used noinline
  330. _sys_sysmips(nabi_no_regargs struct pt_regs regs)
  331. {
  332. long cmd, arg1, arg2;
  333. cmd = regs.regs[4];
  334. arg1 = regs.regs[5];
  335. arg2 = regs.regs[6];
  336. switch (cmd) {
  337. case MIPS_ATOMIC_SET:
  338. return mips_atomic_set(&regs, arg1, arg2);
  339. case MIPS_FIXADE:
  340. if (arg1 & ~3)
  341. return -EINVAL;
  342. if (arg1 & 1)
  343. set_thread_flag(TIF_FIXADE);
  344. else
  345. clear_thread_flag(TIF_FIXADE);
  346. if (arg1 & 2)
  347. set_thread_flag(TIF_LOGADE);
  348. else
  349. clear_thread_flag(TIF_LOGADE);
  350. return 0;
  351. case FLUSH_CACHE:
  352. __flush_cache_all();
  353. return 0;
  354. }
  355. return -EINVAL;
  356. }
  357. /*
  358. * No implemented yet ...
  359. */
  360. SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
  361. {
  362. return -ENOSYS;
  363. }
  364. /*
  365. * If we ever come here the user sp is bad. Zap the process right away.
  366. * Due to the bad stack signaling wouldn't work.
  367. */
  368. asmlinkage void bad_stack(void)
  369. {
  370. do_exit(SIGSEGV);
  371. }
  372. /*
  373. * Do a system call from kernel instead of calling sys_execve so we
  374. * end up with proper pt_regs.
  375. */
  376. int kernel_execve(const char *filename,
  377. const char *const argv[],
  378. const char *const envp[])
  379. {
  380. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  381. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  382. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  383. register unsigned long __a3 asm("$7");
  384. unsigned long __v0;
  385. __asm__ volatile (" \n"
  386. " .set noreorder \n"
  387. " li $2, %5 # __NR_execve \n"
  388. " syscall \n"
  389. " move %0, $2 \n"
  390. " .set reorder \n"
  391. : "=&r" (__v0), "=r" (__a3)
  392. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  393. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  394. "memory");
  395. if (__a3 == 0)
  396. return __v0;
  397. return -__v0;
  398. }