syscall.c 9.8 KB

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