clone.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. @ new sp is already in r1
  97. push {r4, r7}
  98. cfi_adjust_cfa_offset (8)
  99. cfi_rel_offset (r4, 0)
  100. cfi_rel_offset (r7, 4)
  101. ldr r2, [sp, #8]
  102. ldr r3, [sp, #12]
  103. ldr r4, [sp, #16]
  104. ldr r7, =SYS_ify(clone)
  105. swi 0x0
  106. cfi_endproc
  107. cmp r0, #0
  108. beq 1f
  109. pop {r4, r7}
  110. blt __error
  111. IT(t, ne)
  112. BXC(ne, lr)
  113. cfi_startproc
  114. .fnend
  115. PSEUDO_END (__clone)
  116. 1:
  117. .fnstart
  118. .cantunwind
  119. @ pick the function arg and call address off the stack and execute
  120. ldr r0, [sp, #4]
  121. mov lr, pc
  122. ldr pc, [sp]
  123. @ and we are done, passing the return value through r0
  124. b HIDDEN_JUMPTARGET(_exit)
  125. __error:
  126. b __syscall_error
  127. #endif
  128. .size __clone,.-__clone
  129. weak_alias(__clone, clone)
  130. #endif