syscall.c 10 KB

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