syscall.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/smp.h>
  16. #include <linux/smp_lock.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 <asm/branch.h>
  32. #include <asm/cachectl.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/ipc.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. asmlinkage int sys_pipe(nabi_no_regargs volatile struct pt_regs regs)
  42. {
  43. int fd[2];
  44. int error, res;
  45. error = do_pipe(fd);
  46. if (error) {
  47. res = error;
  48. goto out;
  49. }
  50. regs.regs[3] = fd[1];
  51. res = fd[0];
  52. out:
  53. return res;
  54. }
  55. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  56. EXPORT_SYMBOL(shm_align_mask);
  57. #define COLOUR_ALIGN(addr,pgoff) \
  58. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  59. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  60. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  61. unsigned long len, unsigned long pgoff, unsigned long flags)
  62. {
  63. struct vm_area_struct * vmm;
  64. int do_color_align;
  65. unsigned long task_size;
  66. task_size = STACK_TOP;
  67. if (flags & MAP_FIXED) {
  68. /*
  69. * We do not accept a shared mapping if it would violate
  70. * cache aliasing constraints.
  71. */
  72. if ((flags & MAP_SHARED) && (addr & shm_align_mask))
  73. return -EINVAL;
  74. return addr;
  75. }
  76. if (len > task_size)
  77. return -ENOMEM;
  78. do_color_align = 0;
  79. if (filp || (flags & MAP_SHARED))
  80. do_color_align = 1;
  81. if (addr) {
  82. if (do_color_align)
  83. addr = COLOUR_ALIGN(addr, pgoff);
  84. else
  85. addr = PAGE_ALIGN(addr);
  86. vmm = find_vma(current->mm, addr);
  87. if (task_size - len >= addr &&
  88. (!vmm || addr + len <= vmm->vm_start))
  89. return addr;
  90. }
  91. addr = TASK_UNMAPPED_BASE;
  92. if (do_color_align)
  93. addr = COLOUR_ALIGN(addr, pgoff);
  94. else
  95. addr = PAGE_ALIGN(addr);
  96. for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
  97. /* At this point: (!vmm || addr < vmm->vm_end). */
  98. if (task_size - len < addr)
  99. return -ENOMEM;
  100. if (!vmm || addr + len <= vmm->vm_start)
  101. return addr;
  102. addr = vmm->vm_end;
  103. if (do_color_align)
  104. addr = COLOUR_ALIGN(addr, pgoff);
  105. }
  106. }
  107. /* common code for old and new mmaps */
  108. static inline unsigned long
  109. do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  110. unsigned long flags, unsigned long fd, unsigned long pgoff)
  111. {
  112. unsigned long error = -EBADF;
  113. struct file * file = NULL;
  114. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  115. if (!(flags & MAP_ANONYMOUS)) {
  116. file = fget(fd);
  117. if (!file)
  118. goto out;
  119. }
  120. down_write(&current->mm->mmap_sem);
  121. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  122. up_write(&current->mm->mmap_sem);
  123. if (file)
  124. fput(file);
  125. out:
  126. return error;
  127. }
  128. asmlinkage unsigned long
  129. old_mmap(unsigned long addr, unsigned long len, int prot,
  130. int flags, int fd, off_t offset)
  131. {
  132. unsigned long result;
  133. result = -EINVAL;
  134. if (offset & ~PAGE_MASK)
  135. goto out;
  136. result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  137. out:
  138. return result;
  139. }
  140. asmlinkage unsigned long
  141. sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  142. unsigned long flags, unsigned long fd, unsigned long pgoff)
  143. {
  144. if (pgoff & (~PAGE_MASK >> 12))
  145. return -EINVAL;
  146. return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  147. }
  148. save_static_function(sys_fork);
  149. __attribute_used__ noinline static int
  150. _sys_fork(nabi_no_regargs struct pt_regs regs)
  151. {
  152. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  153. }
  154. save_static_function(sys_clone);
  155. __attribute_used__ noinline static int
  156. _sys_clone(nabi_no_regargs struct pt_regs regs)
  157. {
  158. unsigned long clone_flags;
  159. unsigned long newsp;
  160. int __user *parent_tidptr, *child_tidptr;
  161. clone_flags = regs.regs[4];
  162. newsp = regs.regs[5];
  163. if (!newsp)
  164. newsp = regs.regs[29];
  165. parent_tidptr = (int __user *) regs.regs[6];
  166. #ifdef CONFIG_32BIT
  167. /* We need to fetch the fifth argument off the stack. */
  168. child_tidptr = NULL;
  169. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  170. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  171. if (regs.regs[2] == __NR_syscall) {
  172. if (get_user (child_tidptr, &usp[5]))
  173. return -EFAULT;
  174. }
  175. else if (get_user (child_tidptr, &usp[4]))
  176. return -EFAULT;
  177. }
  178. #else
  179. child_tidptr = (int __user *) regs.regs[8];
  180. #endif
  181. return do_fork(clone_flags, newsp, &regs, 0,
  182. parent_tidptr, child_tidptr);
  183. }
  184. /*
  185. * sys_execve() executes a new program.
  186. */
  187. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  188. {
  189. int error;
  190. char * filename;
  191. filename = getname((char __user *) (long)regs.regs[4]);
  192. error = PTR_ERR(filename);
  193. if (IS_ERR(filename))
  194. goto out;
  195. error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
  196. (char __user *__user *) (long)regs.regs[6], &regs);
  197. putname(filename);
  198. out:
  199. return error;
  200. }
  201. /*
  202. * Compacrapability ...
  203. */
  204. asmlinkage int sys_uname(struct old_utsname __user * name)
  205. {
  206. if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
  207. return 0;
  208. return -EFAULT;
  209. }
  210. /*
  211. * Compacrapability ...
  212. */
  213. asmlinkage int sys_olduname(struct oldold_utsname __user * name)
  214. {
  215. int error;
  216. if (!name)
  217. return -EFAULT;
  218. if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
  219. return -EFAULT;
  220. error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
  221. error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
  222. error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
  223. error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
  224. error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
  225. error -= __put_user(0,name->release+__OLD_UTS_LEN);
  226. error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
  227. error -= __put_user(0,name->version+__OLD_UTS_LEN);
  228. error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
  229. error = __put_user(0,name->machine+__OLD_UTS_LEN);
  230. error = error ? -EFAULT : 0;
  231. return error;
  232. }
  233. void sys_set_thread_area(unsigned long addr)
  234. {
  235. struct thread_info *ti = task_thread_info(current);
  236. ti->tp_value = addr;
  237. /* If some future MIPS implementation has this register in hardware,
  238. * we will need to update it here (and in context switches). */
  239. }
  240. asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
  241. {
  242. int tmp;
  243. switch(cmd) {
  244. case MIPS_ATOMIC_SET:
  245. printk(KERN_CRIT "How did I get here?\n");
  246. return -EINVAL;
  247. case MIPS_FIXADE:
  248. tmp = current->thread.mflags & ~3;
  249. current->thread.mflags = tmp | (arg1 & 3);
  250. return 0;
  251. case FLUSH_CACHE:
  252. __flush_cache_all();
  253. return 0;
  254. }
  255. return -EINVAL;
  256. }
  257. /*
  258. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  259. *
  260. * This is really horribly ugly.
  261. */
  262. asmlinkage int sys_ipc (unsigned int call, int first, int second,
  263. unsigned long third, void __user *ptr, long fifth)
  264. {
  265. int version, ret;
  266. version = call >> 16; /* hack for backward compatibility */
  267. call &= 0xffff;
  268. switch (call) {
  269. case SEMOP:
  270. return sys_semtimedop (first, (struct sembuf __user *)ptr,
  271. second, NULL);
  272. case SEMTIMEDOP:
  273. return sys_semtimedop (first, (struct sembuf __user *)ptr,
  274. second,
  275. (const struct timespec __user *)fifth);
  276. case SEMGET:
  277. return sys_semget (first, second, third);
  278. case SEMCTL: {
  279. union semun fourth;
  280. if (!ptr)
  281. return -EINVAL;
  282. if (get_user(fourth.__pad, (void __user *__user *) ptr))
  283. return -EFAULT;
  284. return sys_semctl (first, second, third, fourth);
  285. }
  286. case MSGSND:
  287. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  288. second, third);
  289. case MSGRCV:
  290. switch (version) {
  291. case 0: {
  292. struct ipc_kludge tmp;
  293. if (!ptr)
  294. return -EINVAL;
  295. if (copy_from_user(&tmp,
  296. (struct ipc_kludge __user *) ptr,
  297. sizeof (tmp)))
  298. return -EFAULT;
  299. return sys_msgrcv (first, tmp.msgp, second,
  300. tmp.msgtyp, third);
  301. }
  302. default:
  303. return sys_msgrcv (first,
  304. (struct msgbuf __user *) ptr,
  305. second, fifth, third);
  306. }
  307. case MSGGET:
  308. return sys_msgget ((key_t) first, second);
  309. case MSGCTL:
  310. return sys_msgctl (first, second,
  311. (struct msqid_ds __user *) ptr);
  312. case SHMAT:
  313. switch (version) {
  314. default: {
  315. unsigned long raddr;
  316. ret = do_shmat (first, (char __user *) ptr, second,
  317. &raddr);
  318. if (ret)
  319. return ret;
  320. return put_user (raddr, (unsigned long __user *) third);
  321. }
  322. case 1: /* iBCS2 emulator entry point */
  323. if (!segment_eq(get_fs(), get_ds()))
  324. return -EINVAL;
  325. return do_shmat (first, (char __user *) ptr, second,
  326. (unsigned long *) third);
  327. }
  328. case SHMDT:
  329. return sys_shmdt ((char __user *)ptr);
  330. case SHMGET:
  331. return sys_shmget (first, second, third);
  332. case SHMCTL:
  333. return sys_shmctl (first, second,
  334. (struct shmid_ds __user *) ptr);
  335. default:
  336. return -ENOSYS;
  337. }
  338. }
  339. /*
  340. * No implemented yet ...
  341. */
  342. asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
  343. {
  344. return -ENOSYS;
  345. }
  346. /*
  347. * If we ever come here the user sp is bad. Zap the process right away.
  348. * Due to the bad stack signaling wouldn't work.
  349. */
  350. asmlinkage void bad_stack(void)
  351. {
  352. do_exit(SIGSEGV);
  353. }