syscall.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 <asm/branch.h>
  32. #include <asm/cachectl.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/asm-offsets.h>
  35. #include <asm/signal.h>
  36. #include <asm/sim.h>
  37. #include <asm/shmparam.h>
  38. #include <asm/sysmips.h>
  39. #include <asm/uaccess.h>
  40. /*
  41. * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
  42. * convention. It returns results in registers $v0 / $v1 which means there
  43. * is no need for it to do verify the validity of a userspace pointer
  44. * argument. Historically that used to be expensive in Linux. These days
  45. * the performance advantage is negligible.
  46. */
  47. asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
  48. {
  49. int fd[2];
  50. int error, res;
  51. error = do_pipe_flags(fd, 0);
  52. if (error) {
  53. res = error;
  54. goto out;
  55. }
  56. regs.regs[3] = fd[1];
  57. res = fd[0];
  58. out:
  59. return res;
  60. }
  61. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  62. EXPORT_SYMBOL(shm_align_mask);
  63. #define COLOUR_ALIGN(addr,pgoff) \
  64. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  65. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  66. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  67. unsigned long len, unsigned long pgoff, unsigned long flags)
  68. {
  69. struct vm_area_struct * vmm;
  70. int do_color_align;
  71. unsigned long task_size;
  72. task_size = STACK_TOP;
  73. if (len > task_size)
  74. return -ENOMEM;
  75. if (flags & MAP_FIXED) {
  76. /* Even MAP_FIXED mappings must reside within task_size. */
  77. if (task_size - len < addr)
  78. return -EINVAL;
  79. /*
  80. * We do not accept a shared mapping if it would violate
  81. * cache aliasing constraints.
  82. */
  83. if ((flags & MAP_SHARED) && (addr & shm_align_mask))
  84. return -EINVAL;
  85. return addr;
  86. }
  87. do_color_align = 0;
  88. if (filp || (flags & MAP_SHARED))
  89. do_color_align = 1;
  90. if (addr) {
  91. if (do_color_align)
  92. addr = COLOUR_ALIGN(addr, pgoff);
  93. else
  94. addr = PAGE_ALIGN(addr);
  95. vmm = find_vma(current->mm, addr);
  96. if (task_size - len >= addr &&
  97. (!vmm || addr + len <= vmm->vm_start))
  98. return addr;
  99. }
  100. addr = TASK_UNMAPPED_BASE;
  101. if (do_color_align)
  102. addr = COLOUR_ALIGN(addr, pgoff);
  103. else
  104. addr = PAGE_ALIGN(addr);
  105. for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
  106. /* At this point: (!vmm || addr < vmm->vm_end). */
  107. if (task_size - len < addr)
  108. return -ENOMEM;
  109. if (!vmm || addr + len <= vmm->vm_start)
  110. return addr;
  111. addr = vmm->vm_end;
  112. if (do_color_align)
  113. addr = COLOUR_ALIGN(addr, pgoff);
  114. }
  115. }
  116. /* common code for old and new mmaps */
  117. static inline unsigned long
  118. do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  119. unsigned long flags, unsigned long fd, unsigned long pgoff)
  120. {
  121. unsigned long error = -EBADF;
  122. struct file * file = NULL;
  123. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  124. if (!(flags & MAP_ANONYMOUS)) {
  125. file = fget(fd);
  126. if (!file)
  127. goto out;
  128. }
  129. down_write(&current->mm->mmap_sem);
  130. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  131. up_write(&current->mm->mmap_sem);
  132. if (file)
  133. fput(file);
  134. out:
  135. return error;
  136. }
  137. SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
  138. unsigned long, prot, unsigned long, flags, unsigned long,
  139. fd, off_t, offset)
  140. {
  141. unsigned long result;
  142. result = -EINVAL;
  143. if (offset & ~PAGE_MASK)
  144. goto out;
  145. result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  146. out:
  147. return result;
  148. }
  149. SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
  150. unsigned long, prot, unsigned long, flags, unsigned long, fd,
  151. unsigned long, pgoff)
  152. {
  153. if (pgoff & (~PAGE_MASK >> 12))
  154. return -EINVAL;
  155. return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  156. }
  157. save_static_function(sys_fork);
  158. static int __used noinline
  159. _sys_fork(nabi_no_regargs struct pt_regs regs)
  160. {
  161. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  162. }
  163. save_static_function(sys_clone);
  164. static int __used noinline
  165. _sys_clone(nabi_no_regargs struct pt_regs regs)
  166. {
  167. unsigned long clone_flags;
  168. unsigned long newsp;
  169. int __user *parent_tidptr, *child_tidptr;
  170. clone_flags = regs.regs[4];
  171. newsp = regs.regs[5];
  172. if (!newsp)
  173. newsp = regs.regs[29];
  174. parent_tidptr = (int __user *) regs.regs[6];
  175. #ifdef CONFIG_32BIT
  176. /* We need to fetch the fifth argument off the stack. */
  177. child_tidptr = NULL;
  178. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  179. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  180. if (regs.regs[2] == __NR_syscall) {
  181. if (get_user (child_tidptr, &usp[5]))
  182. return -EFAULT;
  183. }
  184. else if (get_user (child_tidptr, &usp[4]))
  185. return -EFAULT;
  186. }
  187. #else
  188. child_tidptr = (int __user *) regs.regs[8];
  189. #endif
  190. return do_fork(clone_flags, newsp, &regs, 0,
  191. parent_tidptr, child_tidptr);
  192. }
  193. /*
  194. * sys_execve() executes a new program.
  195. */
  196. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  197. {
  198. int error;
  199. char * filename;
  200. filename = getname((char __user *) (long)regs.regs[4]);
  201. error = PTR_ERR(filename);
  202. if (IS_ERR(filename))
  203. goto out;
  204. error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
  205. (char __user *__user *) (long)regs.regs[6], &regs);
  206. putname(filename);
  207. out:
  208. return error;
  209. }
  210. /*
  211. * Compacrapability ...
  212. */
  213. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  214. {
  215. if (name && !copy_to_user(name, utsname(), sizeof (*name)))
  216. return 0;
  217. return -EFAULT;
  218. }
  219. /*
  220. * Compacrapability ...
  221. */
  222. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  223. {
  224. int error;
  225. if (!name)
  226. return -EFAULT;
  227. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  228. return -EFAULT;
  229. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  230. __OLD_UTS_LEN);
  231. error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
  232. error -= __copy_to_user(&name->nodename, &utsname()->nodename,
  233. __OLD_UTS_LEN);
  234. error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
  235. error -= __copy_to_user(&name->release, &utsname()->release,
  236. __OLD_UTS_LEN);
  237. error -= __put_user(0, name->release + __OLD_UTS_LEN);
  238. error -= __copy_to_user(&name->version, &utsname()->version,
  239. __OLD_UTS_LEN);
  240. error -= __put_user(0, name->version + __OLD_UTS_LEN);
  241. error -= __copy_to_user(&name->machine, &utsname()->machine,
  242. __OLD_UTS_LEN);
  243. error = __put_user(0, name->machine + __OLD_UTS_LEN);
  244. error = error ? -EFAULT : 0;
  245. return error;
  246. }
  247. SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
  248. {
  249. struct thread_info *ti = task_thread_info(current);
  250. ti->tp_value = addr;
  251. if (cpu_has_userlocal)
  252. write_c0_userlocal(addr);
  253. return 0;
  254. }
  255. asmlinkage int _sys_sysmips(long cmd, long arg1, long arg2, long arg3)
  256. {
  257. switch (cmd) {
  258. case MIPS_ATOMIC_SET:
  259. printk(KERN_CRIT "How did I get here?\n");
  260. return -EINVAL;
  261. case MIPS_FIXADE:
  262. if (arg1 & ~3)
  263. return -EINVAL;
  264. if (arg1 & 1)
  265. set_thread_flag(TIF_FIXADE);
  266. else
  267. clear_thread_flag(TIF_FIXADE);
  268. if (arg1 & 2)
  269. set_thread_flag(TIF_LOGADE);
  270. else
  271. clear_thread_flag(TIF_FIXADE);
  272. return 0;
  273. case FLUSH_CACHE:
  274. __flush_cache_all();
  275. return 0;
  276. }
  277. return -EINVAL;
  278. }
  279. /*
  280. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  281. *
  282. * This is really horribly ugly.
  283. */
  284. SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, int, second,
  285. unsigned long, third, void __user *, ptr, long, fifth)
  286. {
  287. int version, ret;
  288. version = call >> 16; /* hack for backward compatibility */
  289. call &= 0xffff;
  290. switch (call) {
  291. case SEMOP:
  292. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  293. second, NULL);
  294. case SEMTIMEDOP:
  295. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  296. second,
  297. (const struct timespec __user *)fifth);
  298. case SEMGET:
  299. return sys_semget(first, second, third);
  300. case SEMCTL: {
  301. union semun fourth;
  302. if (!ptr)
  303. return -EINVAL;
  304. if (get_user(fourth.__pad, (void __user *__user *) ptr))
  305. return -EFAULT;
  306. return sys_semctl(first, second, third, fourth);
  307. }
  308. case MSGSND:
  309. return sys_msgsnd(first, (struct msgbuf __user *) ptr,
  310. second, third);
  311. case MSGRCV:
  312. switch (version) {
  313. case 0: {
  314. struct ipc_kludge tmp;
  315. if (!ptr)
  316. return -EINVAL;
  317. if (copy_from_user(&tmp,
  318. (struct ipc_kludge __user *) ptr,
  319. sizeof(tmp)))
  320. return -EFAULT;
  321. return sys_msgrcv(first, tmp.msgp, second,
  322. tmp.msgtyp, third);
  323. }
  324. default:
  325. return sys_msgrcv(first,
  326. (struct msgbuf __user *) ptr,
  327. second, fifth, third);
  328. }
  329. case MSGGET:
  330. return sys_msgget((key_t) first, second);
  331. case MSGCTL:
  332. return sys_msgctl(first, second,
  333. (struct msqid_ds __user *) ptr);
  334. case SHMAT:
  335. switch (version) {
  336. default: {
  337. unsigned long raddr;
  338. ret = do_shmat(first, (char __user *) ptr, second,
  339. &raddr);
  340. if (ret)
  341. return ret;
  342. return put_user(raddr, (unsigned long __user *) third);
  343. }
  344. case 1: /* iBCS2 emulator entry point */
  345. if (!segment_eq(get_fs(), get_ds()))
  346. return -EINVAL;
  347. return do_shmat(first, (char __user *) ptr, second,
  348. (unsigned long *) third);
  349. }
  350. case SHMDT:
  351. return sys_shmdt((char __user *)ptr);
  352. case SHMGET:
  353. return sys_shmget(first, second, third);
  354. case SHMCTL:
  355. return sys_shmctl(first, second,
  356. (struct shmid_ds __user *) ptr);
  357. default:
  358. return -ENOSYS;
  359. }
  360. }
  361. /*
  362. * No implemented yet ...
  363. */
  364. SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
  365. {
  366. return -ENOSYS;
  367. }
  368. /*
  369. * If we ever come here the user sp is bad. Zap the process right away.
  370. * Due to the bad stack signaling wouldn't work.
  371. */
  372. asmlinkage void bad_stack(void)
  373. {
  374. do_exit(SIGSEGV);
  375. }
  376. /*
  377. * Do a system call from kernel instead of calling sys_execve so we
  378. * end up with proper pt_regs.
  379. */
  380. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  381. {
  382. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  383. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  384. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  385. register unsigned long __a3 asm("$7");
  386. unsigned long __v0;
  387. __asm__ volatile (" \n"
  388. " .set noreorder \n"
  389. " li $2, %5 # __NR_execve \n"
  390. " syscall \n"
  391. " move %0, $2 \n"
  392. " .set reorder \n"
  393. : "=&r" (__v0), "=r" (__a3)
  394. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  395. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  396. "memory");
  397. if (__a3 == 0)
  398. return __v0;
  399. return -__v0;
  400. }