clone.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Pat Beirne <patb@corelcomputer.com>
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* clone() is even more special than fork() as it mucks with stacks
  16. and invokes a function in the right context after its all over. */
  17. #include <sysdep.h>
  18. #define _ERRNO_H
  19. #include <features.h>
  20. #include <bits/errno.h>
  21. #include <sys/syscall.h>
  22. #include <bits/arm_asm.h>
  23. #include <bits/arm_bx.h>
  24. #if defined __UCLIBC_HAS_THREADS__ && !defined __UCLIBC_HAS_LINUXTHREADS__
  25. #include <sysdep-cancel.h>
  26. #endif
  27. #define CLONE_VM 0x00000100
  28. #define CLONE_THREAD 0x00010000
  29. #if defined(__NR_clone)
  30. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  31. .text
  32. .global __clone
  33. .type __clone,%function
  34. .align 2
  35. #if defined(THUMB1_ONLY)
  36. .thumb_func
  37. __clone:
  38. @ sanity check args
  39. cmp r0, #0
  40. beq __einval
  41. cmp r1, #0
  42. beq __einval
  43. @ insert the args onto the new stack
  44. sub r1, r1, #8
  45. str r3, [r1, #4]
  46. @ save the function pointer as the 0th element
  47. str r0, [r1]
  48. @ do the system call
  49. @ get flags
  50. mov r0, r2
  51. @ new sp is already in r1
  52. DO_CALL (clone)
  53. movs a1, a1
  54. blt __error
  55. beq 1f
  56. bx lr
  57. 1:
  58. @ pick the function arg and call address off the stack and execute
  59. ldr r0, [sp, #4]
  60. #if defined(ARCH_HAS_BX)
  61. ldr r1, [sp]
  62. bl 2f @ blx r1
  63. #else
  64. mov lr, pc
  65. ldr pc, [sp]
  66. #endif
  67. @ and we are done, passing the return value through r0
  68. bl HIDDEN_JUMPTARGET(_exit)
  69. @ Should never return
  70. b .
  71. 2:
  72. bx r1
  73. __einval:
  74. ldr r0, =-EINVAL
  75. __error:
  76. push {r3, lr}
  77. bl __syscall_error
  78. POP_RET
  79. .pool
  80. #else
  81. __clone:
  82. .fnstart
  83. .cantunwind
  84. @ sanity check args
  85. cmp r0, #0
  86. IT(te, ne)
  87. cmpne r1, #0
  88. moveq r0, #-EINVAL
  89. beq __error
  90. @ insert the args onto the new stack
  91. str r3, [r1, #-4]!
  92. str r0, [r1, #-4]!
  93. @ do the system call
  94. @ get flags
  95. mov r0, r2
  96. #ifdef RESET_PID
  97. mov ip, r2
  98. #endif
  99. @ new sp is already in r1
  100. push {r4, r7}
  101. cfi_adjust_cfa_offset (8)
  102. cfi_rel_offset (r4, 0)
  103. cfi_rel_offset (r7, 4)
  104. ldr r2, [sp, #8]
  105. ldr r3, [sp, #12]
  106. ldr r4, [sp, #16]
  107. ldr r7, =SYS_ify(clone)
  108. swi 0x0
  109. cfi_endproc
  110. cmp r0, #0
  111. beq 1f
  112. pop {r4, r7}
  113. blt __error
  114. IT(t, ne)
  115. BXC(ne, lr)
  116. cfi_startproc
  117. .fnend
  118. PSEUDO_END (__clone)
  119. 1:
  120. .fnstart
  121. .cantunwind
  122. #ifdef RESET_PID
  123. tst ip, #CLONE_THREAD
  124. bne 3f
  125. GET_TLS (lr)
  126. mov r1, r0
  127. tst ip, #CLONE_VM
  128. ldr r7, =SYS_ify(getpid)
  129. ite ne
  130. movne r0, #-1
  131. swieq 0x0
  132. NEGOFF_ADJ_BASE (r1, TID_OFFSET)
  133. str r0, NEGOFF_OFF1 (r1, TID_OFFSET)
  134. str r0, NEGOFF_OFF2 (r1, PID_OFFSET, TID_OFFSET)
  135. 3:
  136. #endif
  137. @ pick the function arg and call address off the stack and execute
  138. ldr r0, [sp, #4]
  139. mov lr, pc
  140. ldr pc, [sp]
  141. @ and we are done, passing the return value through r0
  142. b HIDDEN_JUMPTARGET(_exit)
  143. __error:
  144. b __syscall_error
  145. #endif
  146. .size __clone,.-__clone
  147. weak_alias(__clone, clone)
  148. #endif