syscalls.c 6.8 KB

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