syscall.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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, 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, &utsname()->sysname,
  221. __OLD_UTS_LEN);
  222. error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
  223. error -= __copy_to_user(&name->nodename, &utsname()->nodename,
  224. __OLD_UTS_LEN);
  225. error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
  226. error -= __copy_to_user(&name->release, &utsname()->release,
  227. __OLD_UTS_LEN);
  228. error -= __put_user(0, name->release + __OLD_UTS_LEN);
  229. error -= __copy_to_user(&name->version, &utsname()->version,
  230. __OLD_UTS_LEN);
  231. error -= __put_user(0, name->version + __OLD_UTS_LEN);
  232. error -= __copy_to_user(&name->machine, &utsname()->machine,
  233. __OLD_UTS_LEN);
  234. error = __put_user(0, name->machine + __OLD_UTS_LEN);
  235. error = error ? -EFAULT : 0;
  236. return error;
  237. }
  238. asmlinkage int sys_set_thread_area(unsigned long addr)
  239. {
  240. struct thread_info *ti = task_thread_info(current);
  241. ti->tp_value = addr;
  242. /* If some future MIPS implementation has this register in hardware,
  243. * we will need to update it here (and in context switches). */
  244. return 0;
  245. }
  246. asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
  247. {
  248. int tmp;
  249. switch(cmd) {
  250. case MIPS_ATOMIC_SET:
  251. printk(KERN_CRIT "How did I get here?\n");
  252. return -EINVAL;
  253. case MIPS_FIXADE:
  254. tmp = current->thread.mflags & ~3;
  255. current->thread.mflags = tmp | (arg1 & 3);
  256. return 0;
  257. case FLUSH_CACHE:
  258. __flush_cache_all();
  259. return 0;
  260. }
  261. return -EINVAL;
  262. }
  263. /*
  264. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  265. *
  266. * This is really horribly ugly.
  267. */
  268. asmlinkage int sys_ipc (unsigned int call, int first, int second,
  269. unsigned long third, void __user *ptr, long fifth)
  270. {
  271. int version, ret;
  272. version = call >> 16; /* hack for backward compatibility */
  273. call &= 0xffff;
  274. switch (call) {
  275. case SEMOP:
  276. return sys_semtimedop (first, (struct sembuf __user *)ptr,
  277. second, NULL);
  278. case SEMTIMEDOP:
  279. return sys_semtimedop (first, (struct sembuf __user *)ptr,
  280. second,
  281. (const struct timespec __user *)fifth);
  282. case SEMGET:
  283. return sys_semget (first, second, third);
  284. case SEMCTL: {
  285. union semun fourth;
  286. if (!ptr)
  287. return -EINVAL;
  288. if (get_user(fourth.__pad, (void __user *__user *) ptr))
  289. return -EFAULT;
  290. return sys_semctl (first, second, third, fourth);
  291. }
  292. case MSGSND:
  293. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  294. second, third);
  295. case MSGRCV:
  296. switch (version) {
  297. case 0: {
  298. struct ipc_kludge tmp;
  299. if (!ptr)
  300. return -EINVAL;
  301. if (copy_from_user(&tmp,
  302. (struct ipc_kludge __user *) ptr,
  303. sizeof (tmp)))
  304. return -EFAULT;
  305. return sys_msgrcv (first, tmp.msgp, second,
  306. tmp.msgtyp, third);
  307. }
  308. default:
  309. return sys_msgrcv (first,
  310. (struct msgbuf __user *) ptr,
  311. second, fifth, third);
  312. }
  313. case MSGGET:
  314. return sys_msgget ((key_t) first, second);
  315. case MSGCTL:
  316. return sys_msgctl (first, second,
  317. (struct msqid_ds __user *) ptr);
  318. case SHMAT:
  319. switch (version) {
  320. default: {
  321. unsigned long raddr;
  322. ret = do_shmat (first, (char __user *) ptr, second,
  323. &raddr);
  324. if (ret)
  325. return ret;
  326. return put_user (raddr, (unsigned long __user *) third);
  327. }
  328. case 1: /* iBCS2 emulator entry point */
  329. if (!segment_eq(get_fs(), get_ds()))
  330. return -EINVAL;
  331. return do_shmat (first, (char __user *) ptr, second,
  332. (unsigned long *) third);
  333. }
  334. case SHMDT:
  335. return sys_shmdt ((char __user *)ptr);
  336. case SHMGET:
  337. return sys_shmget (first, second, third);
  338. case SHMCTL:
  339. return sys_shmctl (first, second,
  340. (struct shmid_ds __user *) ptr);
  341. default:
  342. return -ENOSYS;
  343. }
  344. }
  345. /*
  346. * No implemented yet ...
  347. */
  348. asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
  349. {
  350. return -ENOSYS;
  351. }
  352. /*
  353. * If we ever come here the user sp is bad. Zap the process right away.
  354. * Due to the bad stack signaling wouldn't work.
  355. */
  356. asmlinkage void bad_stack(void)
  357. {
  358. do_exit(SIGSEGV);
  359. }
  360. /*
  361. * Do a system call from kernel instead of calling sys_execve so we
  362. * end up with proper pt_regs.
  363. */
  364. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  365. {
  366. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  367. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  368. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  369. register unsigned long __a3 asm("$7");
  370. unsigned long __v0;
  371. __asm__ volatile (" \n"
  372. " .set noreorder \n"
  373. " li $2, %5 # __NR_execve \n"
  374. " syscall \n"
  375. " move %0, $2 \n"
  376. " .set reorder \n"
  377. : "=&r" (__v0), "=r" (__a3)
  378. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  379. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  380. "memory");
  381. if (__a3 == 0)
  382. return __v0;
  383. return -__v0;
  384. }