syscall.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/mman.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/file.h>
  22. #include <linux/slab.h>
  23. #include <linux/utsname.h>
  24. #include <linux/unistd.h>
  25. #include <linux/sem.h>
  26. #include <linux/msg.h>
  27. #include <linux/shm.h>
  28. #include <linux/compiler.h>
  29. #include <linux/module.h>
  30. #include <asm/branch.h>
  31. #include <asm/cachectl.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/ipc.h>
  34. #include <asm/asm-offsets.h>
  35. #include <asm/signal.h>
  36. #include <asm/sim.h>
  37. #include <asm/shmparam.h>
  38. #include <asm/sysmips.h>
  39. #include <asm/uaccess.h>
  40. asmlinkage int sys_pipe(nabi_no_regargs volatile struct pt_regs regs)
  41. {
  42. int fd[2];
  43. int error, res;
  44. error = do_pipe(fd);
  45. if (error) {
  46. res = error;
  47. goto out;
  48. }
  49. regs.regs[3] = fd[1];
  50. res = fd[0];
  51. out:
  52. return res;
  53. }
  54. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  55. EXPORT_SYMBOL(shm_align_mask);
  56. #define COLOUR_ALIGN(addr,pgoff) \
  57. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  58. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  59. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  60. unsigned long len, unsigned long pgoff, unsigned long flags)
  61. {
  62. struct vm_area_struct * vmm;
  63. int do_color_align;
  64. unsigned long task_size;
  65. task_size = STACK_TOP;
  66. if (flags & MAP_FIXED) {
  67. /*
  68. * We do not accept a shared mapping if it would violate
  69. * cache aliasing constraints.
  70. */
  71. if ((flags & MAP_SHARED) && (addr & shm_align_mask))
  72. return -EINVAL;
  73. return addr;
  74. }
  75. if (len > task_size)
  76. return -ENOMEM;
  77. do_color_align = 0;
  78. if (filp || (flags & MAP_SHARED))
  79. do_color_align = 1;
  80. if (addr) {
  81. if (do_color_align)
  82. addr = COLOUR_ALIGN(addr, pgoff);
  83. else
  84. addr = PAGE_ALIGN(addr);
  85. vmm = find_vma(current->mm, addr);
  86. if (task_size - len >= addr &&
  87. (!vmm || addr + len <= vmm->vm_start))
  88. return addr;
  89. }
  90. addr = TASK_UNMAPPED_BASE;
  91. if (do_color_align)
  92. addr = COLOUR_ALIGN(addr, pgoff);
  93. else
  94. addr = PAGE_ALIGN(addr);
  95. for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
  96. /* At this point: (!vmm || addr < vmm->vm_end). */
  97. if (task_size - len < addr)
  98. return -ENOMEM;
  99. if (!vmm || addr + len <= vmm->vm_start)
  100. return addr;
  101. addr = vmm->vm_end;
  102. if (do_color_align)
  103. addr = COLOUR_ALIGN(addr, pgoff);
  104. }
  105. }
  106. /* common code for old and new mmaps */
  107. static inline unsigned long
  108. do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  109. unsigned long flags, unsigned long fd, unsigned long pgoff)
  110. {
  111. unsigned long error = -EBADF;
  112. struct file * file = NULL;
  113. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  114. if (!(flags & MAP_ANONYMOUS)) {
  115. file = fget(fd);
  116. if (!file)
  117. goto out;
  118. }
  119. down_write(&current->mm->mmap_sem);
  120. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  121. up_write(&current->mm->mmap_sem);
  122. if (file)
  123. fput(file);
  124. out:
  125. return error;
  126. }
  127. asmlinkage unsigned long
  128. old_mmap(unsigned long addr, unsigned long len, int prot,
  129. int flags, int fd, off_t offset)
  130. {
  131. unsigned long result;
  132. result = -EINVAL;
  133. if (offset & ~PAGE_MASK)
  134. goto out;
  135. result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  136. out:
  137. return result;
  138. }
  139. asmlinkage unsigned long
  140. sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  141. unsigned long flags, unsigned long fd, unsigned long pgoff)
  142. {
  143. if (pgoff & (~PAGE_MASK >> 12))
  144. return -EINVAL;
  145. return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  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 __user *) (long)regs.regs[4]);
  191. error = PTR_ERR(filename);
  192. if (IS_ERR(filename))
  193. goto out;
  194. error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
  195. (char __user *__user *) (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 __user * name)
  204. {
  205. if (name && !copy_to_user(name, utsname(), sizeof (*name)))
  206. return 0;
  207. return -EFAULT;
  208. }
  209. /*
  210. * Compacrapability ...
  211. */
  212. asmlinkage int sys_olduname(struct oldold_utsname __user * 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, &utsname()->sysname,
  220. __OLD_UTS_LEN);
  221. error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
  222. error -= __copy_to_user(&name->nodename, &utsname()->nodename,
  223. __OLD_UTS_LEN);
  224. error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
  225. error -= __copy_to_user(&name->release, &utsname()->release,
  226. __OLD_UTS_LEN);
  227. error -= __put_user(0, name->release + __OLD_UTS_LEN);
  228. error -= __copy_to_user(&name->version, &utsname()->version,
  229. __OLD_UTS_LEN);
  230. error -= __put_user(0, name->version + __OLD_UTS_LEN);
  231. error -= __copy_to_user(&name->machine, &utsname()->machine,
  232. __OLD_UTS_LEN);
  233. error = __put_user(0, name->machine + __OLD_UTS_LEN);
  234. error = error ? -EFAULT : 0;
  235. return error;
  236. }
  237. asmlinkage int sys_set_thread_area(unsigned long addr)
  238. {
  239. struct thread_info *ti = task_thread_info(current);
  240. ti->tp_value = addr;
  241. /* If some future MIPS implementation has this register in hardware,
  242. * we will need to update it here (and in context switches). */
  243. return 0;
  244. }
  245. asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
  246. {
  247. int tmp;
  248. switch(cmd) {
  249. case MIPS_ATOMIC_SET:
  250. printk(KERN_CRIT "How did I get here?\n");
  251. return -EINVAL;
  252. case MIPS_FIXADE:
  253. tmp = current->thread.mflags & ~3;
  254. current->thread.mflags = tmp | (arg1 & 3);
  255. return 0;
  256. case FLUSH_CACHE:
  257. __flush_cache_all();
  258. return 0;
  259. }
  260. return -EINVAL;
  261. }
  262. /*
  263. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  264. *
  265. * This is really horribly ugly.
  266. */
  267. asmlinkage int sys_ipc (unsigned int call, int first, int second,
  268. unsigned long third, void __user *ptr, long fifth)
  269. {
  270. int version, ret;
  271. version = call >> 16; /* hack for backward compatibility */
  272. call &= 0xffff;
  273. switch (call) {
  274. case SEMOP:
  275. return sys_semtimedop (first, (struct sembuf __user *)ptr,
  276. second, NULL);
  277. case SEMTIMEDOP:
  278. return sys_semtimedop (first, (struct sembuf __user *)ptr,
  279. second,
  280. (const struct timespec __user *)fifth);
  281. case SEMGET:
  282. return sys_semget (first, second, third);
  283. case SEMCTL: {
  284. union semun fourth;
  285. if (!ptr)
  286. return -EINVAL;
  287. if (get_user(fourth.__pad, (void __user *__user *) ptr))
  288. return -EFAULT;
  289. return sys_semctl (first, second, third, fourth);
  290. }
  291. case MSGSND:
  292. return sys_msgsnd (first, (struct msgbuf __user *) ptr,
  293. second, third);
  294. case MSGRCV:
  295. switch (version) {
  296. case 0: {
  297. struct ipc_kludge tmp;
  298. if (!ptr)
  299. return -EINVAL;
  300. if (copy_from_user(&tmp,
  301. (struct ipc_kludge __user *) ptr,
  302. sizeof (tmp)))
  303. return -EFAULT;
  304. return sys_msgrcv (first, tmp.msgp, second,
  305. tmp.msgtyp, third);
  306. }
  307. default:
  308. return sys_msgrcv (first,
  309. (struct msgbuf __user *) ptr,
  310. second, fifth, third);
  311. }
  312. case MSGGET:
  313. return sys_msgget ((key_t) first, second);
  314. case MSGCTL:
  315. return sys_msgctl (first, second,
  316. (struct msqid_ds __user *) ptr);
  317. case SHMAT:
  318. switch (version) {
  319. default: {
  320. unsigned long raddr;
  321. ret = do_shmat (first, (char __user *) ptr, second,
  322. &raddr);
  323. if (ret)
  324. return ret;
  325. return put_user (raddr, (unsigned long __user *) third);
  326. }
  327. case 1: /* iBCS2 emulator entry point */
  328. if (!segment_eq(get_fs(), get_ds()))
  329. return -EINVAL;
  330. return do_shmat (first, (char __user *) ptr, second,
  331. (unsigned long *) third);
  332. }
  333. case SHMDT:
  334. return sys_shmdt ((char __user *)ptr);
  335. case SHMGET:
  336. return sys_shmget (first, second, third);
  337. case SHMCTL:
  338. return sys_shmctl (first, second,
  339. (struct shmid_ds __user *) ptr);
  340. default:
  341. return -ENOSYS;
  342. }
  343. }
  344. /*
  345. * No implemented yet ...
  346. */
  347. asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
  348. {
  349. return -ENOSYS;
  350. }
  351. /*
  352. * If we ever come here the user sp is bad. Zap the process right away.
  353. * Due to the bad stack signaling wouldn't work.
  354. */
  355. asmlinkage void bad_stack(void)
  356. {
  357. do_exit(SIGSEGV);
  358. }
  359. /*
  360. * Do a system call from kernel instead of calling sys_execve so we
  361. * end up with proper pt_regs.
  362. */
  363. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  364. {
  365. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  366. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  367. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  368. register unsigned long __a3 asm("$7");
  369. unsigned long __v0;
  370. __asm__ volatile (" \n"
  371. " .set noreorder \n"
  372. " li $2, %5 # __NR_execve \n"
  373. " syscall \n"
  374. " move %0, $2 \n"
  375. " .set reorder \n"
  376. : "=&r" (__v0), "=r" (__a3)
  377. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  378. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  379. "memory");
  380. if (__a3 == 0)
  381. return __v0;
  382. return -__v0;
  383. }