syscall.c 11 KB

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