sys_score.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * arch/score/kernel/syscall.c
  3. *
  4. * Score Processor version.
  5. *
  6. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  7. * Chen Liqin <liqin.chen@sunplusct.com>
  8. * Lennox Wu <lennox.wu@sunplusct.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see the file COPYING, or write
  22. * to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/file.h>
  26. #include <linux/fs.h>
  27. #include <linux/mm.h>
  28. #include <linux/mman.h>
  29. #include <linux/module.h>
  30. #include <linux/unistd.h>
  31. #include <linux/syscalls.h>
  32. #include <asm/syscalls.h>
  33. asmlinkage long
  34. sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
  35. unsigned long flags, unsigned long fd, unsigned long pgoff)
  36. {
  37. int error = -EBADF;
  38. struct file *file = NULL;
  39. if (pgoff & (~PAGE_MASK >> 12))
  40. return -EINVAL;
  41. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  42. if (!(flags & MAP_ANONYMOUS)) {
  43. file = fget(fd);
  44. if (!file)
  45. return error;
  46. }
  47. down_write(&current->mm->mmap_sem);
  48. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  49. up_write(&current->mm->mmap_sem);
  50. if (file)
  51. fput(file);
  52. return error;
  53. }
  54. asmlinkage long
  55. sys_mmap(unsigned long addr, unsigned long len, unsigned long prot,
  56. unsigned long flags, unsigned long fd, off_t pgoff)
  57. {
  58. return sys_mmap2(addr, len, prot, flags, fd, pgoff >> PAGE_SHIFT);
  59. }
  60. asmlinkage long
  61. score_fork(struct pt_regs *regs)
  62. {
  63. return do_fork(SIGCHLD, regs->regs[0], regs, 0, NULL, NULL);
  64. }
  65. /*
  66. * Clone a task - this clones the calling program thread.
  67. * This is called indirectly via a small wrapper
  68. */
  69. asmlinkage long
  70. score_clone(struct pt_regs *regs)
  71. {
  72. unsigned long clone_flags;
  73. unsigned long newsp;
  74. int __user *parent_tidptr, *child_tidptr;
  75. clone_flags = regs->regs[4];
  76. newsp = regs->regs[5];
  77. if (!newsp)
  78. newsp = regs->regs[0];
  79. parent_tidptr = (int __user *)regs->regs[6];
  80. child_tidptr = (int __user *)regs->regs[8];
  81. return do_fork(clone_flags, newsp, regs, 0,
  82. parent_tidptr, child_tidptr);
  83. }
  84. asmlinkage long
  85. score_vfork(struct pt_regs *regs)
  86. {
  87. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD,
  88. regs->regs[0], regs, 0, NULL, NULL);
  89. }
  90. /*
  91. * sys_execve() executes a new program.
  92. * This is called indirectly via a small wrapper
  93. */
  94. asmlinkage long
  95. score_execve(struct pt_regs *regs)
  96. {
  97. int error;
  98. char *filename;
  99. filename = getname((char __user*)regs->regs[4]);
  100. error = PTR_ERR(filename);
  101. if (IS_ERR(filename))
  102. return error;
  103. error = do_execve(filename, (char __user *__user*)regs->regs[5],
  104. (char __user *__user *) regs->regs[6], regs);
  105. putname(filename);
  106. return error;
  107. }
  108. /*
  109. * Do a system call from kernel instead of calling sys_execve so we
  110. * end up with proper pt_regs.
  111. */
  112. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  113. {
  114. register unsigned long __r4 asm("r4") = (unsigned long) filename;
  115. register unsigned long __r5 asm("r5") = (unsigned long) argv;
  116. register unsigned long __r6 asm("r6") = (unsigned long) envp;
  117. register unsigned long __r7 asm("r7");
  118. __asm__ __volatile__ (" \n"
  119. "ldi r27, %5 \n"
  120. "syscall \n"
  121. "mv %0, r4 \n"
  122. "mv %1, r7 \n"
  123. : "=&r" (__r4), "=r" (__r7)
  124. : "r" (__r4), "r" (__r5), "r" (__r6), "i" (__NR_execve)
  125. : "r8", "r9", "r10", "r11", "r22", "r23", "r24", "r25",
  126. "r26", "r27", "memory");
  127. if (__r7 == 0)
  128. return __r4;
  129. return -__r4;
  130. }