clone.S 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2016-2017 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* Copyright (C) 2010-2014 Free Software Foundation, Inc.
  6. Contributed by Pat Beirne <patb@corelcomputer.com>
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, write to the Free
  17. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA. */
  19. /* clone() is even more special than fork() as it mucks with stacks
  20. and invokes a function in the right context after its all over. */
  21. #include <sysdep.h>
  22. #define _ERRNO_H 1
  23. #include <bits/errno.h>
  24. #ifdef RESET_PID
  25. #include <tcb-offsets.h>
  26. #endif
  27. #define CLONE_VM 0x00000100
  28. #define CLONE_THREAD 0x00010000
  29. /* int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
  30. _syscall2(int, clone, int, flags, void *, child_stack) */
  31. ENTRY(__clone)
  32. #ifdef __NDS32_ABI_2FP_PLUS__
  33. lwi $r4, [$sp]
  34. lwi $r5, [$sp+4]
  35. #endif
  36. #ifdef PIC
  37. /* set GP register to parent only, cause child's $SP will be $r1. */
  38. pushm $fp, $gp
  39. cfi_adjust_cfa_offset(8)
  40. cfi_rel_offset(fp, 0)
  41. cfi_rel_offset(gp, 4)
  42. mfusr $r15, $pc
  43. sethi $gp, hi20(_GLOBAL_OFFSET_TABLE_+4)
  44. ori $gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_+8)
  45. add $gp, $gp, $r15
  46. #endif /* PIC */
  47. /* sanity check arguments. */
  48. beqz $r0, 1f
  49. bnez $r1, 2f
  50. 1:
  51. movi $r0, -EINVAL
  52. 5:
  53. #ifdef PIC
  54. /* restore GP register, only in parent's stack */
  55. la $r15, C_SYMBOL_NAME(__syscall_error@PLT)
  56. push $lp
  57. cfi_adjust_cfa_offset(4)
  58. cfi_rel_offset(lp, 0)
  59. addi $sp, $sp, -4
  60. cfi_adjust_cfa_offset(4)
  61. jral $r15
  62. addi $sp, $sp, 4
  63. cfi_adjust_cfa_offset(-4)
  64. pop $lp
  65. cfi_adjust_cfa_offset(-4)
  66. cfi_restore(lp)
  67. popm $fp, $gp
  68. cfi_adjust_cfa_offset(-8)
  69. cfi_restore(fp)
  70. cfi_restore(gp)
  71. ret
  72. #else /* ! PIC */
  73. la $r15, C_SYMBOL_NAME(__syscall_error)
  74. jr $r15
  75. #endif /* ! PIC */
  76. 2:
  77. /* Child's $sp will be $r1, make $sp 8-byte alignment */
  78. bitci $r1, $r1, 7
  79. /* push to child's stack only. */
  80. addi $r1, $r1, -4
  81. swi.p $r3, [$r1], -4 ! arg
  82. swi $r0, [$r1] ! fn
  83. /* do the system call */
  84. or $r0, $r2, $r2 ! move $r0, $r2
  85. move $r3, $r5
  86. move $r5, $r2 ! Use $r5 to backup $r2
  87. ! The pt_regs is placed in $r5 in kerenl (sys_clone_wrapper)
  88. move $r2, $r4
  89. #ifdef __NDS32_ABI_2FP_PLUS__
  90. # ifdef PIC
  91. lwi $r4, [$sp+#0x10]
  92. # else
  93. lwi $r4, [$sp+#0x8]
  94. # endif
  95. #else
  96. # ifdef PIC
  97. lwi $r4, [$sp+#0x8]
  98. # else
  99. lwi $r4, [$sp]
  100. # endif
  101. #endif
  102. __do_syscall(clone)
  103. beqz $r0, 4f
  104. bltz $r0, 5b
  105. 10:
  106. #ifdef PIC
  107. /* restore GP register, only in parent's stack */
  108. popm $fp, $gp
  109. cfi_adjust_cfa_offset(-8)
  110. cfi_restore(gp)
  111. cfi_restore(fp)
  112. #endif /* PIC */
  113. ret
  114. #ifdef RESET_PID
  115. 4:
  116. cfi_undefined(lp)
  117. movi $r0, CLONE_THREAD ! Skip when CLONE_THREAD is set.
  118. and $r0, $r5, $r0
  119. bnez $r0, 8f
  120. movi $r0, CLONE_VM ! Value = -1 when CLONE_VM is set.
  121. and $r0, $r5, $r0
  122. beqz $r0, 6f
  123. movi $r0, -1
  124. j 7f
  125. 6:
  126. __do_syscall(getpid) ! __do_syscall(gettid) ! __do_syscall(getpid)
  127. 7:
  128. swi $r0, [$r25 + PID_OFFSET]
  129. swi $r0, [$r25 + TID_OFFSET]
  130. 8:
  131. #else
  132. 4:
  133. #endif
  134. /* Only in child's stack. */
  135. pop $r1 ! fn
  136. pop $r0 ! arg
  137. #if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
  138. addi $sp, $sp, -24
  139. #endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__) */
  140. ! use $r15 in case _exit is PIC
  141. bral $r1
  142. #if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
  143. addi $sp, $sp, 24
  144. #endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__) */
  145. ! use $r15 in case _exit is PIC
  146. #ifdef PIC
  147. la $r15, C_SYMBOL_NAME(_exit@PLT)
  148. #else /* ! PIC */
  149. la $r15, C_SYMBOL_NAME(_exit)
  150. #endif /* ! PIC */
  151. jr $r15
  152. PSEUDO_END (__clone)
  153. weak_alias (__clone, clone)