syscall.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/config.h>
  11. #include <linux/a.out.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. return do_mmap2(addr, len, prot, flags, fd, pgoff);
  145. }
  146. save_static_function(sys_fork);
  147. __attribute_used__ noinline static int
  148. _sys_fork(nabi_no_regargs struct pt_regs regs)
  149. {
  150. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  151. }
  152. save_static_function(sys_clone);
  153. __attribute_used__ noinline static int
  154. _sys_clone(nabi_no_regargs struct pt_regs regs)
  155. {
  156. unsigned long clone_flags;
  157. unsigned long newsp;
  158. int __user *parent_tidptr, *child_tidptr;
  159. clone_flags = regs.regs[4];
  160. newsp = regs.regs[5];
  161. if (!newsp)
  162. newsp = regs.regs[29];
  163. parent_tidptr = (int __user *) regs.regs[6];
  164. #ifdef CONFIG_32BIT
  165. /* We need to fetch the fifth argument off the stack. */
  166. child_tidptr = NULL;
  167. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  168. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  169. if (regs.regs[2] == __NR_syscall) {
  170. if (get_user (child_tidptr, &usp[5]))
  171. return -EFAULT;
  172. }
  173. else if (get_user (child_tidptr, &usp[4]))
  174. return -EFAULT;
  175. }
  176. #else
  177. child_tidptr = (int __user *) regs.regs[8];
  178. #endif
  179. return do_fork(clone_flags, newsp, &regs, 0,
  180. parent_tidptr, child_tidptr);
  181. }
  182. /*
  183. * sys_execve() executes a new program.
  184. */
  185. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  186. {
  187. int error;
  188. char * filename;
  189. filename = getname((char *) (long)regs.regs[4]);
  190. error = PTR_ERR(filename);
  191. if (IS_ERR(filename))
  192. goto out;
  193. error = do_execve(filename, (char **) (long)regs.regs[5],
  194. (char **) (long)regs.regs[6], &regs);
  195. putname(filename);
  196. out:
  197. return error;
  198. }
  199. /*
  200. * Compacrapability ...
  201. */
  202. asmlinkage int sys_uname(struct old_utsname * name)
  203. {
  204. if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
  205. return 0;
  206. return -EFAULT;
  207. }
  208. /*
  209. * Compacrapability ...
  210. */
  211. asmlinkage int sys_olduname(struct oldold_utsname * name)
  212. {
  213. int error;
  214. if (!name)
  215. return -EFAULT;
  216. if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
  217. return -EFAULT;
  218. error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
  219. error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
  220. error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
  221. error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
  222. error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
  223. error -= __put_user(0,name->release+__OLD_UTS_LEN);
  224. error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
  225. error -= __put_user(0,name->version+__OLD_UTS_LEN);
  226. error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
  227. error = __put_user(0,name->machine+__OLD_UTS_LEN);
  228. error = error ? -EFAULT : 0;
  229. return error;
  230. }
  231. void sys_set_thread_area(unsigned long addr)
  232. {
  233. struct thread_info *ti = current->thread_info;
  234. ti->tp_value = addr;
  235. /* If some future MIPS implementation has this register in hardware,
  236. * we will need to update it here (and in context switches). */
  237. }
  238. asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
  239. {
  240. int tmp, len;
  241. char *name;
  242. switch(cmd) {
  243. case SETNAME: {
  244. char nodename[__NEW_UTS_LEN + 1];
  245. if (!capable(CAP_SYS_ADMIN))
  246. return -EPERM;
  247. name = (char *) arg1;
  248. len = strncpy_from_user(nodename, name, __NEW_UTS_LEN);
  249. if (len < 0)
  250. return -EFAULT;
  251. down_write(&uts_sem);
  252. strncpy(system_utsname.nodename, nodename, len);
  253. nodename[__NEW_UTS_LEN] = '\0';
  254. strlcpy(system_utsname.nodename, nodename,
  255. sizeof(system_utsname.nodename));
  256. up_write(&uts_sem);
  257. return 0;
  258. }
  259. case MIPS_ATOMIC_SET:
  260. printk(KERN_CRIT "How did I get here?\n");
  261. return -EINVAL;
  262. case MIPS_FIXADE:
  263. tmp = current->thread.mflags & ~3;
  264. current->thread.mflags = tmp | (arg1 & 3);
  265. return 0;
  266. case FLUSH_CACHE:
  267. __flush_cache_all();
  268. return 0;
  269. case MIPS_RDNVRAM:
  270. return -EIO;
  271. }
  272. return -EINVAL;
  273. }
  274. /*
  275. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  276. *
  277. * This is really horribly ugly.
  278. */
  279. asmlinkage int sys_ipc (uint call, int first, int second,
  280. unsigned long third, void *ptr, long fifth)
  281. {
  282. int version, ret;
  283. version = call >> 16; /* hack for backward compatibility */
  284. call &= 0xffff;
  285. switch (call) {
  286. case SEMOP:
  287. return sys_semtimedop (first, (struct sembuf *)ptr, second,
  288. NULL);
  289. case SEMTIMEDOP:
  290. return sys_semtimedop (first, (struct sembuf *)ptr, second,
  291. (const struct timespec __user *)fifth);
  292. case SEMGET:
  293. return sys_semget (first, second, third);
  294. case SEMCTL: {
  295. union semun fourth;
  296. if (!ptr)
  297. return -EINVAL;
  298. if (get_user(fourth.__pad, (void **) ptr))
  299. return -EFAULT;
  300. return sys_semctl (first, second, third, fourth);
  301. }
  302. case MSGSND:
  303. return sys_msgsnd (first, (struct msgbuf *) ptr,
  304. second, third);
  305. case MSGRCV:
  306. switch (version) {
  307. case 0: {
  308. struct ipc_kludge tmp;
  309. if (!ptr)
  310. return -EINVAL;
  311. if (copy_from_user(&tmp,
  312. (struct ipc_kludge *) ptr,
  313. sizeof (tmp)))
  314. return -EFAULT;
  315. return sys_msgrcv (first, tmp.msgp, second,
  316. tmp.msgtyp, third);
  317. }
  318. default:
  319. return sys_msgrcv (first,
  320. (struct msgbuf *) ptr,
  321. second, fifth, third);
  322. }
  323. case MSGGET:
  324. return sys_msgget ((key_t) first, second);
  325. case MSGCTL:
  326. return sys_msgctl (first, second, (struct msqid_ds *) ptr);
  327. case SHMAT:
  328. switch (version) {
  329. default: {
  330. ulong raddr;
  331. ret = do_shmat (first, (char *) ptr, second, &raddr);
  332. if (ret)
  333. return ret;
  334. return put_user (raddr, (ulong *) third);
  335. }
  336. case 1: /* iBCS2 emulator entry point */
  337. if (!segment_eq(get_fs(), get_ds()))
  338. return -EINVAL;
  339. return do_shmat (first, (char *) ptr, second, (ulong *) third);
  340. }
  341. case SHMDT:
  342. return sys_shmdt ((char *)ptr);
  343. case SHMGET:
  344. return sys_shmget (first, second, third);
  345. case SHMCTL:
  346. return sys_shmctl (first, second,
  347. (struct shmid_ds *) ptr);
  348. default:
  349. return -ENOSYS;
  350. }
  351. }
  352. /*
  353. * No implemented yet ...
  354. */
  355. asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
  356. {
  357. return -ENOSYS;
  358. }
  359. /*
  360. * If we ever come here the user sp is bad. Zap the process right away.
  361. * Due to the bad stack signaling wouldn't work.
  362. */
  363. asmlinkage void bad_stack(void)
  364. {
  365. do_exit(SIGSEGV);
  366. }