syscalls.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. extern void do_syscall_trace(void);
  44. typedef int (*syscall_t)(void *a0,...);
  45. extern syscall_t sys_call_table[];
  46. extern unsigned char sys_narg_table[];
  47. /*
  48. * sys_pipe() is the normal C calling standard for creating a pipe. It's not
  49. * the way unix traditional does this, though.
  50. */
  51. int sys_pipe(int __user *userfds)
  52. {
  53. int fd[2];
  54. int error;
  55. error = do_pipe(fd);
  56. if (!error) {
  57. if (copy_to_user(userfds, fd, 2 * sizeof(int)))
  58. error = -EFAULT;
  59. }
  60. return error;
  61. }
  62. /*
  63. * Common code for old and new mmaps.
  64. */
  65. long sys_mmap(unsigned long addr, unsigned long len, unsigned long prot,
  66. unsigned long flags, unsigned long fd, unsigned long pgoff)
  67. {
  68. int error = -EBADF;
  69. struct file * file = NULL;
  70. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  71. if (!(flags & MAP_ANONYMOUS)) {
  72. file = fget(fd);
  73. if (!file)
  74. goto out;
  75. }
  76. down_write(&current->mm->mmap_sem);
  77. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  78. up_write(&current->mm->mmap_sem);
  79. if (file)
  80. fput(file);
  81. out:
  82. return error;
  83. }
  84. int sys_clone(struct pt_regs *regs)
  85. {
  86. unsigned long clone_flags;
  87. unsigned long newsp;
  88. int __user *parent_tidptr, *child_tidptr;
  89. clone_flags = regs->areg[4];
  90. newsp = regs->areg[3];
  91. parent_tidptr = (int __user *)regs->areg[5];
  92. child_tidptr = (int __user *)regs->areg[6];
  93. if (!newsp)
  94. newsp = regs->areg[1];
  95. return do_fork(clone_flags,newsp,regs,0,parent_tidptr,child_tidptr);
  96. }
  97. /*
  98. * sys_execve() executes a new program.
  99. */
  100. int sys_execve(struct pt_regs *regs)
  101. {
  102. int error;
  103. char * filename;
  104. filename = getname((char *) (long)regs->areg[5]);
  105. error = PTR_ERR(filename);
  106. if (IS_ERR(filename))
  107. goto out;
  108. error = do_execve(filename, (char **) (long)regs->areg[3],
  109. (char **) (long)regs->areg[4], regs);
  110. putname(filename);
  111. out:
  112. return error;
  113. }
  114. int sys_uname(struct old_utsname * name)
  115. {
  116. if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
  117. return 0;
  118. return -EFAULT;
  119. }
  120. /*
  121. * Build the string table for the builtin "poor man's strace".
  122. */
  123. #if DEBUG
  124. #define SYSCALL(fun, narg) #fun,
  125. static char *sfnames[] = {
  126. #include "syscalls.h"
  127. };
  128. #undef SYS
  129. #endif
  130. void system_call (struct pt_regs *regs)
  131. {
  132. syscall_t syscall;
  133. unsigned long parm0, parm1, parm2, parm3, parm4, parm5;
  134. int nargs, res;
  135. unsigned int syscallnr;
  136. int ps;
  137. #if DEBUG
  138. int i;
  139. unsigned long parms[6];
  140. char *sysname;
  141. #endif
  142. regs->syscall = regs->areg[2];
  143. do_syscall_trace();
  144. /* Have to load after syscall_trace because strace
  145. * sometimes changes regs->syscall.
  146. */
  147. syscallnr = regs->syscall;
  148. parm0 = parm1 = parm2 = parm3 = parm4 = parm5 = 0;
  149. /* Restore interrupt level to syscall invoker's.
  150. * If this were in assembly, we wouldn't disable
  151. * interrupts in the first place:
  152. */
  153. local_save_flags (ps);
  154. local_irq_restore((ps & ~XCHAL_PS_INTLEVEL_MASK) |
  155. (regs->ps & XCHAL_PS_INTLEVEL_MASK) );
  156. if (syscallnr > __NR_Linux_syscalls) {
  157. regs->areg[2] = -ENOSYS;
  158. return;
  159. }
  160. syscall = sys_call_table[syscallnr];
  161. nargs = sys_narg_table[syscallnr];
  162. if (syscall == NULL) {
  163. regs->areg[2] = -ENOSYS;
  164. return;
  165. }
  166. /* There shouldn't be more than six arguments in the table! */
  167. if (nargs > 6)
  168. panic("Internal error - too many syscall arguments (%d)!\n",
  169. nargs);
  170. /* Linux takes system-call arguments in registers. The ABI
  171. * and Xtensa software conventions require the system-call
  172. * number in a2. If an argument exists in a2, we move it to
  173. * the next available register. Note that for improved
  174. * efficiency, we do NOT shift all parameters down one
  175. * register to maintain the original order.
  176. *
  177. * At best case (zero arguments), we just write the syscall
  178. * number to a2. At worst case (1 to 6 arguments), we move
  179. * the argument in a2 to the next available register, then
  180. * write the syscall number to a2.
  181. *
  182. * For clarity, the following truth table enumerates all
  183. * possibilities.
  184. *
  185. * arguments syscall number arg0, arg1, arg2, arg3, arg4, arg5
  186. * --------- -------------- ----------------------------------
  187. * 0 a2
  188. * 1 a2 a3
  189. * 2 a2 a4, a3
  190. * 3 a2 a5, a3, a4
  191. * 4 a2 a6, a3, a4, a5
  192. * 5 a2 a7, a3, a4, a5, a6
  193. * 6 a2 a8, a3, a4, a5, a6, a7
  194. */
  195. if (nargs) {
  196. parm0 = regs->areg[nargs+2];
  197. parm1 = regs->areg[3];
  198. parm2 = regs->areg[4];
  199. parm3 = regs->areg[5];
  200. parm4 = regs->areg[6];
  201. parm5 = regs->areg[7];
  202. } else /* nargs == 0 */
  203. parm0 = (unsigned long) regs;
  204. #if DEBUG
  205. parms[0] = parm0;
  206. parms[1] = parm1;
  207. parms[2] = parm2;
  208. parms[3] = parm3;
  209. parms[4] = parm4;
  210. parms[5] = parm5;
  211. sysname = sfnames[syscallnr];
  212. if (strncmp(sysname, "sys_", 4) == 0)
  213. sysname = sysname + 4;
  214. printk("\017SYSCALL:I:%x:%d:%s %s(", regs->pc, current->pid,
  215. current->comm, sysname);
  216. for (i = 0; i < nargs; i++)
  217. printk((i>0) ? ", %#lx" : "%#lx", parms[i]);
  218. printk(")\n");
  219. #endif
  220. res = syscall((void *)parm0, parm1, parm2, parm3, parm4, parm5);
  221. #if DEBUG
  222. printk("\017SYSCALL:O:%d:%s %s(",current->pid, current->comm, sysname);
  223. for (i = 0; i < nargs; i++)
  224. printk((i>0) ? ", %#lx" : "%#lx", parms[i]);
  225. if (res < 4096)
  226. printk(") = %d\n", res);
  227. else
  228. printk(") = %#x\n", res);
  229. #endif /* DEBUG */
  230. regs->areg[2] = res;
  231. do_syscall_trace();
  232. }