syscalls.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * arch/xtensa/kernel/syscall.c
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. * Copyright (C) 2000 Silicon Graphics, Inc.
  10. * Copyright (C) 1995 - 2000 by Ralf Baechle
  11. *
  12. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  13. * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
  14. * Chris Zankel <chris@zankel.net>
  15. * Kevin Chea
  16. *
  17. */
  18. #define DEBUG 0
  19. #include <linux/config.h>
  20. #include <linux/linkage.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/mman.h>
  25. #include <linux/sched.h>
  26. #include <linux/file.h>
  27. #include <linux/slab.h>
  28. #include <linux/utsname.h>
  29. #include <linux/unistd.h>
  30. #include <linux/stringify.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/sem.h>
  33. #include <linux/msg.h>
  34. #include <linux/shm.h>
  35. #include <linux/errno.h>
  36. #include <asm/ptrace.h>
  37. #include <asm/signal.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/hardirq.h>
  40. #include <asm/mman.h>
  41. #include <asm/shmparam.h>
  42. #include <asm/page.h>
  43. #include <asm/ipc.h>
  44. extern void do_syscall_trace(void);
  45. typedef int (*syscall_t)(void *a0,...);
  46. extern int (*do_syscalls)(struct pt_regs *regs, syscall_t fun,
  47. int narg);
  48. extern syscall_t sys_call_table[];
  49. extern unsigned char sys_narg_table[];
  50. /*
  51. * sys_pipe() is the normal C calling standard for creating a pipe. It's not
  52. * the way unix traditional does this, though.
  53. */
  54. int sys_pipe(int __user *userfds)
  55. {
  56. int fd[2];
  57. int error;
  58. error = do_pipe(fd);
  59. if (!error) {
  60. if (copy_to_user(userfds, fd, 2 * sizeof(int)))
  61. error = -EFAULT;
  62. }
  63. return error;
  64. }
  65. /*
  66. * Common code for old and new mmaps.
  67. */
  68. static inline long do_mmap2(unsigned long addr, unsigned long len,
  69. unsigned long prot, unsigned long flags,
  70. unsigned long fd, unsigned long pgoff)
  71. {
  72. int error = -EBADF;
  73. struct file * file = NULL;
  74. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  75. if (!(flags & MAP_ANONYMOUS)) {
  76. file = fget(fd);
  77. if (!file)
  78. goto out;
  79. }
  80. down_write(&current->mm->mmap_sem);
  81. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  82. up_write(&current->mm->mmap_sem);
  83. if (file)
  84. fput(file);
  85. out:
  86. return error;
  87. }
  88. unsigned long old_mmap(unsigned long addr, size_t len, int prot,
  89. int flags, int fd, off_t offset)
  90. {
  91. return do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
  92. }
  93. long sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  94. unsigned long flags, unsigned long fd, unsigned long pgoff)
  95. {
  96. return do_mmap2(addr, len, prot, flags, fd, pgoff);
  97. }
  98. int sys_fork(struct pt_regs *regs)
  99. {
  100. return do_fork(SIGCHLD, regs->areg[1], regs, 0, NULL, NULL);
  101. }
  102. int sys_vfork(struct pt_regs *regs)
  103. {
  104. return do_fork(CLONE_VFORK|CLONE_VM|SIGCHLD, regs->areg[1],
  105. regs, 0, NULL, NULL);
  106. }
  107. int sys_clone(struct pt_regs *regs)
  108. {
  109. unsigned long clone_flags;
  110. unsigned long newsp;
  111. int __user *parent_tidptr, *child_tidptr;
  112. clone_flags = regs->areg[4];
  113. newsp = regs->areg[3];
  114. parent_tidptr = (int __user *)regs->areg[5];
  115. child_tidptr = (int __user *)regs->areg[6];
  116. if (!newsp)
  117. newsp = regs->areg[1];
  118. return do_fork(clone_flags,newsp,regs,0,parent_tidptr,child_tidptr);
  119. }
  120. /*
  121. * sys_execve() executes a new program.
  122. */
  123. int sys_execve(struct pt_regs *regs)
  124. {
  125. int error;
  126. char * filename;
  127. filename = getname((char *) (long)regs->areg[5]);
  128. error = PTR_ERR(filename);
  129. if (IS_ERR(filename))
  130. goto out;
  131. error = do_execve(filename, (char **) (long)regs->areg[3],
  132. (char **) (long)regs->areg[4], regs);
  133. putname(filename);
  134. out:
  135. return error;
  136. }
  137. int sys_uname(struct old_utsname * name)
  138. {
  139. if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
  140. return 0;
  141. return -EFAULT;
  142. }
  143. int sys_olduname(struct oldold_utsname * name)
  144. {
  145. int error;
  146. if (!name)
  147. return -EFAULT;
  148. if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
  149. return -EFAULT;
  150. error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
  151. error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
  152. error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
  153. error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
  154. error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
  155. error -= __put_user(0,name->release+__OLD_UTS_LEN);
  156. error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
  157. error -= __put_user(0,name->version+__OLD_UTS_LEN);
  158. error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
  159. error -= __put_user(0,name->machine+__OLD_UTS_LEN);
  160. return error ? -EFAULT : 0;
  161. }
  162. /*
  163. * Build the string table for the builtin "poor man's strace".
  164. */
  165. #if DEBUG
  166. #define SYSCALL(fun, narg) #fun,
  167. static char *sfnames[] = {
  168. #include "syscalls.h"
  169. };
  170. #undef SYS
  171. #endif
  172. void system_call (struct pt_regs *regs)
  173. {
  174. syscall_t syscall;
  175. unsigned long parm0, parm1, parm2, parm3, parm4, parm5;
  176. int nargs, res;
  177. unsigned int syscallnr;
  178. int ps;
  179. #if DEBUG
  180. int i;
  181. unsigned long parms[6];
  182. char *sysname;
  183. #endif
  184. regs->syscall = regs->areg[2];
  185. do_syscall_trace();
  186. /* Have to load after syscall_trace because strace
  187. * sometimes changes regs->syscall.
  188. */
  189. syscallnr = regs->syscall;
  190. parm0 = parm1 = parm2 = parm3 = parm4 = parm5 = 0;
  191. /* Restore interrupt level to syscall invoker's.
  192. * If this were in assembly, we wouldn't disable
  193. * interrupts in the first place:
  194. */
  195. local_save_flags (ps);
  196. local_irq_restore((ps & ~XCHAL_PS_INTLEVEL_MASK) |
  197. (regs->ps & XCHAL_PS_INTLEVEL_MASK) );
  198. if (syscallnr > __NR_Linux_syscalls) {
  199. regs->areg[2] = -ENOSYS;
  200. return;
  201. }
  202. syscall = sys_call_table[syscallnr];
  203. nargs = sys_narg_table[syscallnr];
  204. if (syscall == NULL) {
  205. regs->areg[2] = -ENOSYS;
  206. return;
  207. }
  208. /* There shouldn't be more than six arguments in the table! */
  209. if (nargs > 6)
  210. panic("Internal error - too many syscall arguments (%d)!\n",
  211. nargs);
  212. /* Linux takes system-call arguments in registers. The ABI
  213. * and Xtensa software conventions require the system-call
  214. * number in a2. If an argument exists in a2, we move it to
  215. * the next available register. Note that for improved
  216. * efficiency, we do NOT shift all parameters down one
  217. * register to maintain the original order.
  218. *
  219. * At best case (zero arguments), we just write the syscall
  220. * number to a2. At worst case (1 to 6 arguments), we move
  221. * the argument in a2 to the next available register, then
  222. * write the syscall number to a2.
  223. *
  224. * For clarity, the following truth table enumerates all
  225. * possibilities.
  226. *
  227. * arguments syscall number arg0, arg1, arg2, arg3, arg4, arg5
  228. * --------- -------------- ----------------------------------
  229. * 0 a2
  230. * 1 a2 a3
  231. * 2 a2 a4, a3
  232. * 3 a2 a5, a3, a4
  233. * 4 a2 a6, a3, a4, a5
  234. * 5 a2 a7, a3, a4, a5, a6
  235. * 6 a2 a8, a3, a4, a5, a6, a7
  236. */
  237. if (nargs) {
  238. parm0 = regs->areg[nargs+2];
  239. parm1 = regs->areg[3];
  240. parm2 = regs->areg[4];
  241. parm3 = regs->areg[5];
  242. parm4 = regs->areg[6];
  243. parm5 = regs->areg[7];
  244. } else /* nargs == 0 */
  245. parm0 = (unsigned long) regs;
  246. #if DEBUG
  247. parms[0] = parm0;
  248. parms[1] = parm1;
  249. parms[2] = parm2;
  250. parms[3] = parm3;
  251. parms[4] = parm4;
  252. parms[5] = parm5;
  253. sysname = sfnames[syscallnr];
  254. if (strncmp(sysname, "sys_", 4) == 0)
  255. sysname = sysname + 4;
  256. printk("\017SYSCALL:I:%x:%d:%s %s(", regs->pc, current->pid,
  257. current->comm, sysname);
  258. for (i = 0; i < nargs; i++)
  259. printk((i>0) ? ", %#lx" : "%#lx", parms[i]);
  260. printk(")\n");
  261. #endif
  262. res = syscall((void *)parm0, parm1, parm2, parm3, parm4, parm5);
  263. #if DEBUG
  264. printk("\017SYSCALL:O:%d:%s %s(",current->pid, current->comm, sysname);
  265. for (i = 0; i < nargs; i++)
  266. printk((i>0) ? ", %#lx" : "%#lx", parms[i]);
  267. if (res < 4096)
  268. printk(") = %d\n", res);
  269. else
  270. printk(") = %#x\n", res);
  271. #endif /* DEBUG */
  272. regs->areg[2] = res;
  273. do_syscall_trace();
  274. }
  275. /*
  276. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  277. *
  278. * This is really horribly ugly.
  279. */
  280. int sys_ipc (uint call, int first, int second,
  281. int third, void __user *ptr, long fifth)
  282. {
  283. int version, ret;
  284. version = call >> 16; /* hack for backward compatibility */
  285. call &= 0xffff;
  286. ret = -ENOSYS;
  287. switch (call) {
  288. case SEMOP:
  289. ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
  290. second, NULL);
  291. break;
  292. case SEMTIMEDOP:
  293. ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
  294. second, (const struct timespec *) fifth);
  295. break;
  296. case SEMGET:
  297. ret = sys_semget (first, second, third);
  298. break;
  299. case SEMCTL: {
  300. union semun fourth;
  301. if (ptr && !get_user(fourth.__pad, (void *__user *) ptr))
  302. ret = sys_semctl (first, second, third, fourth);
  303. break;
  304. }
  305. case MSGSND:
  306. ret = sys_msgsnd (first, (struct msgbuf __user*) ptr,
  307. second, third);
  308. break;
  309. case MSGRCV:
  310. switch (version) {
  311. case 0: {
  312. struct ipc_kludge tmp;
  313. if (ptr && !copy_from_user(&tmp,
  314. (struct ipc_kludge *) ptr,
  315. sizeof (tmp)))
  316. ret = sys_msgrcv (first, tmp.msgp, second,
  317. tmp.msgtyp, third);
  318. break;
  319. }
  320. default:
  321. ret = sys_msgrcv (first, (struct msgbuf __user *) ptr,
  322. second, 0, third);
  323. break;
  324. }
  325. break;
  326. case MSGGET:
  327. ret = sys_msgget ((key_t) first, second);
  328. break;
  329. case MSGCTL:
  330. ret = sys_msgctl (first, second, (struct msqid_ds __user*) ptr);
  331. break;
  332. case SHMAT: {
  333. ulong raddr;
  334. ret = do_shmat (first, (char __user *) ptr, second, &raddr);
  335. if (!ret)
  336. ret = put_user (raddr, (ulong __user *) third);
  337. break;
  338. }
  339. case SHMDT:
  340. ret = sys_shmdt ((char __user *)ptr);
  341. break;
  342. case SHMGET:
  343. ret = sys_shmget (first, second, third);
  344. break;
  345. case SHMCTL:
  346. ret = sys_shmctl (first, second, (struct shmid_ds __user*) ptr);
  347. break;
  348. }
  349. return ret;
  350. }