clone.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include <sysdep-cancel.h>
  25. #define CLONE_VM 0x00000100
  26. #define CLONE_THREAD 0x00010000
  27. #if defined(__NR_clone)
  28. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  29. .text
  30. .global __clone
  31. .type __clone,%function
  32. .align 2
  33. #if defined(THUMB1_ONLY)
  34. .thumb_func
  35. __clone:
  36. @ sanity check args
  37. cmp r0, #0
  38. beq __einval
  39. cmp r1, #0
  40. beq __einval
  41. @ insert the args onto the new stack
  42. sub r1, r1, #8
  43. str r3, [r1, #4]
  44. @ save the function pointer as the 0th element
  45. str r0, [r1]
  46. @ do the system call
  47. @ get flags
  48. mov r0, r2
  49. @ new sp is already in r1
  50. @ load remaining arguments off the stack
  51. stmfd sp!, {r4}
  52. ldr r2, [sp, #4]
  53. ldr r3, [sp, #8]
  54. ldr r4, [sp, #12]
  55. DO_CALL (clone)
  56. movs a1, a1
  57. blt __error
  58. ldmnefd sp!, {r4}
  59. beq 1f
  60. bx lr
  61. 1:
  62. @ pick the function arg and call address off the stack and execute
  63. ldr r0, [sp, #4]
  64. ldr r1, [sp]
  65. bl 2f @ blx r1
  66. @ and we are done, passing the return value through r0
  67. bl HIDDEN_JUMPTARGET(_exit)
  68. @ Should never return
  69. b .
  70. 2:
  71. bx r1
  72. __einval:
  73. ldr r0, =-EINVAL
  74. __error:
  75. push {r3, lr}
  76. bl __syscall_error
  77. POP_RET
  78. .pool
  79. #else
  80. __clone:
  81. .fnstart
  82. .cantunwind
  83. @ sanity check args
  84. cmp r0, #0
  85. IT(te, ne)
  86. cmpne r1, #0
  87. moveq r0, #-EINVAL
  88. beq __error
  89. @ insert the args onto the new stack
  90. str r3, [r1, #-4]!
  91. str r0, [r1, #-4]!
  92. @ do the system call
  93. @ get flags
  94. mov r0, r2
  95. #ifdef RESET_PID
  96. mov ip, r2
  97. #endif
  98. @ new sp is already in r1
  99. push {r4, r7}
  100. cfi_adjust_cfa_offset (8)
  101. cfi_rel_offset (r4, 0)
  102. cfi_rel_offset (r7, 4)
  103. ldr r2, [sp, #8]
  104. ldr r3, [sp, #12]
  105. ldr r4, [sp, #16]
  106. ldr r7, =SYS_ify(clone)
  107. swi 0x0
  108. cfi_endproc
  109. cmp r0, #0
  110. beq 1f
  111. pop {r4, r7}
  112. blt __error
  113. #if defined(__USE_BX__)
  114. bxne lr
  115. #else
  116. movne pc, lr
  117. #endif
  118. cfi_startproc
  119. .fnend
  120. PSEUDO_END (__clone)
  121. 1:
  122. .fnstart
  123. .cantunwind
  124. #ifdef RESET_PID
  125. tst ip, #CLONE_THREAD
  126. bne 3f
  127. GET_TLS (lr)
  128. mov r1, r0
  129. tst ip, #CLONE_VM
  130. ldr r7, =SYS_ify(getpid)
  131. ite ne
  132. movne r0, #-1
  133. swieq 0x0
  134. NEGOFF_ADJ_BASE (r1, TID_OFFSET)
  135. str r0, NEGOFF_OFF1 (r1, TID_OFFSET)
  136. str r0, NEGOFF_OFF2 (r1, PID_OFFSET, TID_OFFSET)
  137. 3:
  138. #endif
  139. @ pick the function arg and call address off the stack and execute
  140. ldr r0, [sp, #4]
  141. mov lr, pc
  142. ldr pc, [sp]
  143. @ and we are done, passing the return value through r0
  144. b HIDDEN_JUMPTARGET(_exit)
  145. __error:
  146. b __syscall_error
  147. #endif
  148. .size __clone,.-__clone
  149. weak_alias(__clone, clone)
  150. #endif