syscall.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/fs.h>
  16. #include <linux/smp.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 <linux/ipc.h>
  32. #include <asm/branch.h>
  33. #include <asm/cachectl.h>
  34. #include <asm/cacheflush.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 (len > task_size)
  68. return -ENOMEM;
  69. if (flags & MAP_FIXED) {
  70. /* Even MAP_FIXED mappings must reside within task_size. */
  71. if (task_size - len < addr)
  72. return -EINVAL;
  73. /*
  74. * We do not accept a shared mapping if it would violate
  75. * cache aliasing constraints.
  76. */
  77. if ((flags & MAP_SHARED) && (addr & shm_align_mask))
  78. return -EINVAL;
  79. return addr;
  80. }
  81. do_color_align = 0;
  82. if (filp || (flags & MAP_SHARED))
  83. do_color_align = 1;
  84. if (addr) {
  85. if (do_color_align)
  86. addr = COLOUR_ALIGN(addr, pgoff);
  87. else
  88. addr = PAGE_ALIGN(addr);
  89. vmm = find_vma(current->mm, addr);
  90. if (task_size - len >= addr &&
  91. (!vmm || addr + len <= vmm->vm_start))
  92. return addr;
  93. }
  94. addr = TASK_UNMAPPED_BASE;
  95. if (do_color_align)
  96. addr = COLOUR_ALIGN(addr, pgoff);
  97. else
  98. addr = PAGE_ALIGN(addr);
  99. for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
  100. /* At this point: (!vmm || addr < vmm->vm_end). */
  101. if (task_size - len < addr)
  102. return -ENOMEM;
  103. if (!vmm || addr + len <= vmm->vm_start)
  104. return addr;
  105. addr = vmm->vm_end;
  106. if (do_color_align)
  107. addr = COLOUR_ALIGN(addr, pgoff);
  108. }
  109. }
  110. /* common code for old and new mmaps */
  111. static inline unsigned long
  112. do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  113. unsigned long flags, unsigned long fd, unsigned long pgoff)
  114. {
  115. unsigned long error = -EBADF;
  116. struct file * file = NULL;
  117. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  118. if (!(flags & MAP_ANONYMOUS)) {
  119. file = fget(fd);
  120. if (!file)
  121. goto out;
  122. }
  123. down_write(&current->mm->mmap_sem);
  124. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  125. up_write(&current->mm->mmap_sem);
  126. if (file)
  127. fput(file);
  128. out:
  129. return error;
  130. }
  131. asmlinkage unsigned long
  132. old_mmap(unsigned long addr, unsigned long len, int prot,
  133. int flags, int fd, off_t offset)
  134. {
  135. unsigned long result;
  136. result = -EINVAL;
  137. if (offset & ~PAGE_MASK)
  138. goto out;
  139. result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  140. out:
  141. return result;
  142. }
  143. asmlinkage unsigned long
  144. sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  145. unsigned long flags, unsigned long fd, unsigned long pgoff)
  146. {
  147. if (pgoff & (~PAGE_MASK >> 12))
  148. return -EINVAL;
  149. return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
  150. }
  151. save_static_function(sys_fork);
  152. static int __used noinline
  153. _sys_fork(nabi_no_regargs struct pt_regs regs)
  154. {
  155. return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
  156. }
  157. save_static_function(sys_clone);
  158. static int __used noinline
  159. _sys_clone(nabi_no_regargs struct pt_regs regs)
  160. {
  161. unsigned long clone_flags;
  162. unsigned long newsp;
  163. int __user *parent_tidptr, *child_tidptr;
  164. clone_flags = regs.regs[4];
  165. newsp = regs.regs[5];
  166. if (!newsp)
  167. newsp = regs.regs[29];
  168. parent_tidptr = (int __user *) regs.regs[6];
  169. #ifdef CONFIG_32BIT
  170. /* We need to fetch the fifth argument off the stack. */
  171. child_tidptr = NULL;
  172. if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
  173. int __user *__user *usp = (int __user *__user *) regs.regs[29];
  174. if (regs.regs[2] == __NR_syscall) {
  175. if (get_user (child_tidptr, &usp[5]))
  176. return -EFAULT;
  177. }
  178. else if (get_user (child_tidptr, &usp[4]))
  179. return -EFAULT;
  180. }
  181. #else
  182. child_tidptr = (int __user *) regs.regs[8];
  183. #endif
  184. return do_fork(clone_flags, newsp, &regs, 0,
  185. parent_tidptr, child_tidptr);
  186. }
  187. /*
  188. * sys_execve() executes a new program.
  189. */
  190. asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
  191. {
  192. int error;
  193. char * filename;
  194. filename = getname((char __user *) (long)regs.regs[4]);
  195. error = PTR_ERR(filename);
  196. if (IS_ERR(filename))
  197. goto out;
  198. error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
  199. (char __user *__user *) (long)regs.regs[6], &regs);
  200. putname(filename);
  201. out:
  202. return error;
  203. }
  204. /*
  205. * Compacrapability ...
  206. */
  207. asmlinkage int sys_uname(struct old_utsname __user * name)
  208. {
  209. if (name && !copy_to_user(name, utsname(), sizeof (*name)))
  210. return 0;
  211. return -EFAULT;
  212. }
  213. /*
  214. * Compacrapability ...
  215. */
  216. asmlinkage int sys_olduname(struct oldold_utsname __user * name)
  217. {
  218. int error;
  219. if (!name)
  220. return -EFAULT;
  221. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  222. return -EFAULT;
  223. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  224. __OLD_UTS_LEN);
  225. error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
  226. error -= __copy_to_user(&name->nodename, &utsname()->nodename,
  227. __OLD_UTS_LEN);
  228. error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
  229. error -= __copy_to_user(&name->release, &utsname()->release,
  230. __OLD_UTS_LEN);
  231. error -= __put_user(0, name->release + __OLD_UTS_LEN);
  232. error -= __copy_to_user(&name->version, &utsname()->version,
  233. __OLD_UTS_LEN);
  234. error -= __put_user(0, name->version + __OLD_UTS_LEN);
  235. error -= __copy_to_user(&name->machine, &utsname()->machine,
  236. __OLD_UTS_LEN);
  237. error = __put_user(0, name->machine + __OLD_UTS_LEN);
  238. error = error ? -EFAULT : 0;
  239. return error;
  240. }
  241. asmlinkage int sys_set_thread_area(unsigned long addr)
  242. {
  243. struct thread_info *ti = task_thread_info(current);
  244. ti->tp_value = addr;
  245. if (cpu_has_userlocal)
  246. write_c0_userlocal(addr);
  247. return 0;
  248. }
  249. asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
  250. {
  251. switch (cmd) {
  252. case MIPS_ATOMIC_SET:
  253. printk(KERN_CRIT "How did I get here?\n");
  254. return -EINVAL;
  255. case MIPS_FIXADE:
  256. if (arg1 & ~3)
  257. return -EINVAL;
  258. if (arg1 & 1)
  259. set_thread_flag(TIF_FIXADE);
  260. else
  261. clear_thread_flag(TIF_FIXADE);
  262. if (arg1 & 2)
  263. set_thread_flag(TIF_LOGADE);
  264. else
  265. clear_thread_flag(TIF_FIXADE);
  266. return 0;
  267. case FLUSH_CACHE:
  268. __flush_cache_all();
  269. return 0;
  270. }
  271. return -EINVAL;
  272. }
  273. /*
  274. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  275. *
  276. * This is really horribly ugly.
  277. */
  278. asmlinkage int sys_ipc(unsigned int call, int first, int second,
  279. unsigned long third, void __user *ptr, long fifth)
  280. {
  281. int version, ret;
  282. version = call >> 16; /* hack for backward compatibility */
  283. call &= 0xffff;
  284. switch (call) {
  285. case SEMOP:
  286. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  287. second, NULL);
  288. case SEMTIMEDOP:
  289. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  290. 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 __user *__user *) ptr))
  299. return -EFAULT;
  300. return sys_semctl(first, second, third, fourth);
  301. }
  302. case MSGSND:
  303. return sys_msgsnd(first, (struct msgbuf __user *) 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 __user *) 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 __user *) 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,
  327. (struct msqid_ds __user *) ptr);
  328. case SHMAT:
  329. switch (version) {
  330. default: {
  331. unsigned long raddr;
  332. ret = do_shmat(first, (char __user *) ptr, second,
  333. &raddr);
  334. if (ret)
  335. return ret;
  336. return put_user(raddr, (unsigned long __user *) third);
  337. }
  338. case 1: /* iBCS2 emulator entry point */
  339. if (!segment_eq(get_fs(), get_ds()))
  340. return -EINVAL;
  341. return do_shmat(first, (char __user *) ptr, second,
  342. (unsigned long *) third);
  343. }
  344. case SHMDT:
  345. return sys_shmdt((char __user *)ptr);
  346. case SHMGET:
  347. return sys_shmget(first, second, third);
  348. case SHMCTL:
  349. return sys_shmctl(first, second,
  350. (struct shmid_ds __user *) ptr);
  351. default:
  352. return -ENOSYS;
  353. }
  354. }
  355. /*
  356. * No implemented yet ...
  357. */
  358. asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
  359. {
  360. return -ENOSYS;
  361. }
  362. /*
  363. * If we ever come here the user sp is bad. Zap the process right away.
  364. * Due to the bad stack signaling wouldn't work.
  365. */
  366. asmlinkage void bad_stack(void)
  367. {
  368. do_exit(SIGSEGV);
  369. }
  370. /*
  371. * Do a system call from kernel instead of calling sys_execve so we
  372. * end up with proper pt_regs.
  373. */
  374. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  375. {
  376. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  377. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  378. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  379. register unsigned long __a3 asm("$7");
  380. unsigned long __v0;
  381. __asm__ volatile (" \n"
  382. " .set noreorder \n"
  383. " li $2, %5 # __NR_execve \n"
  384. " syscall \n"
  385. " move %0, $2 \n"
  386. " .set reorder \n"
  387. : "=&r" (__v0), "=r" (__a3)
  388. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  389. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  390. "memory");
  391. if (__a3 == 0)
  392. return __v0;
  393. return -__v0;
  394. }