syscalls.c 6.3 KB

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