syscall.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 (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. static int __used noinline
  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. static int __used noinline
  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 (cpu_has_userlocal)
  243. write_c0_userlocal(addr);
  244. return 0;
  245. }
  246. asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
  247. {
  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. if (arg1 & ~3)
  254. return -EINVAL;
  255. if (arg1 & 1)
  256. set_thread_flag(TIF_FIXADE);
  257. else
  258. clear_thread_flag(TIF_FIXADE);
  259. if (arg1 & 2)
  260. set_thread_flag(TIF_LOGADE);
  261. else
  262. clear_thread_flag(TIF_FIXADE);
  263. return 0;
  264. case FLUSH_CACHE:
  265. __flush_cache_all();
  266. return 0;
  267. }
  268. return -EINVAL;
  269. }
  270. /*
  271. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  272. *
  273. * This is really horribly ugly.
  274. */
  275. asmlinkage int sys_ipc(unsigned int call, int first, int second,
  276. unsigned long third, void __user *ptr, long fifth)
  277. {
  278. int version, ret;
  279. version = call >> 16; /* hack for backward compatibility */
  280. call &= 0xffff;
  281. switch (call) {
  282. case SEMOP:
  283. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  284. second, NULL);
  285. case SEMTIMEDOP:
  286. return sys_semtimedop(first, (struct sembuf __user *)ptr,
  287. second,
  288. (const struct timespec __user *)fifth);
  289. case SEMGET:
  290. return sys_semget(first, second, third);
  291. case SEMCTL: {
  292. union semun fourth;
  293. if (!ptr)
  294. return -EINVAL;
  295. if (get_user(fourth.__pad, (void __user *__user *) ptr))
  296. return -EFAULT;
  297. return sys_semctl(first, second, third, fourth);
  298. }
  299. case MSGSND:
  300. return sys_msgsnd(first, (struct msgbuf __user *) ptr,
  301. second, third);
  302. case MSGRCV:
  303. switch (version) {
  304. case 0: {
  305. struct ipc_kludge tmp;
  306. if (!ptr)
  307. return -EINVAL;
  308. if (copy_from_user(&tmp,
  309. (struct ipc_kludge __user *) ptr,
  310. sizeof(tmp)))
  311. return -EFAULT;
  312. return sys_msgrcv(first, tmp.msgp, second,
  313. tmp.msgtyp, third);
  314. }
  315. default:
  316. return sys_msgrcv(first,
  317. (struct msgbuf __user *) ptr,
  318. second, fifth, third);
  319. }
  320. case MSGGET:
  321. return sys_msgget((key_t) first, second);
  322. case MSGCTL:
  323. return sys_msgctl(first, second,
  324. (struct msqid_ds __user *) ptr);
  325. case SHMAT:
  326. switch (version) {
  327. default: {
  328. unsigned long raddr;
  329. ret = do_shmat(first, (char __user *) ptr, second,
  330. &raddr);
  331. if (ret)
  332. return ret;
  333. return put_user(raddr, (unsigned long __user *) third);
  334. }
  335. case 1: /* iBCS2 emulator entry point */
  336. if (!segment_eq(get_fs(), get_ds()))
  337. return -EINVAL;
  338. return do_shmat(first, (char __user *) ptr, second,
  339. (unsigned long *) third);
  340. }
  341. case SHMDT:
  342. return sys_shmdt((char __user *)ptr);
  343. case SHMGET:
  344. return sys_shmget(first, second, third);
  345. case SHMCTL:
  346. return sys_shmctl(first, second,
  347. (struct shmid_ds __user *) ptr);
  348. default:
  349. return -ENOSYS;
  350. }
  351. }
  352. /*
  353. * No implemented yet ...
  354. */
  355. asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
  356. {
  357. return -ENOSYS;
  358. }
  359. /*
  360. * If we ever come here the user sp is bad. Zap the process right away.
  361. * Due to the bad stack signaling wouldn't work.
  362. */
  363. asmlinkage void bad_stack(void)
  364. {
  365. do_exit(SIGSEGV);
  366. }
  367. /*
  368. * Do a system call from kernel instead of calling sys_execve so we
  369. * end up with proper pt_regs.
  370. */
  371. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  372. {
  373. register unsigned long __a0 asm("$4") = (unsigned long) filename;
  374. register unsigned long __a1 asm("$5") = (unsigned long) argv;
  375. register unsigned long __a2 asm("$6") = (unsigned long) envp;
  376. register unsigned long __a3 asm("$7");
  377. unsigned long __v0;
  378. __asm__ volatile (" \n"
  379. " .set noreorder \n"
  380. " li $2, %5 # __NR_execve \n"
  381. " syscall \n"
  382. " move %0, $2 \n"
  383. " .set reorder \n"
  384. : "=&r" (__v0), "=r" (__a3)
  385. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
  386. : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
  387. "memory");
  388. if (__a3 == 0)
  389. return __v0;
  390. return -__v0;
  391. }