clone.S 3.1 KB

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