clone.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #define _ERRNO_H
  18. #include <features.h>
  19. #include <bits/errno.h>
  20. #include <sys/syscall.h>
  21. #include <bits/arm_asm.h>
  22. #if defined(__NR_clone)
  23. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  24. .text
  25. .global __clone
  26. .type __clone,%function
  27. .align 2
  28. #if defined(THUMB1_ONLY)
  29. .thumb_func
  30. __clone:
  31. @ sanity check args
  32. cmp r0, #0
  33. beq __einval
  34. cmp r1, #0
  35. beq __einval
  36. @ insert the args onto the new stack
  37. sub r1, r1, #8
  38. str r3, [r1, #4]
  39. @ save the function pointer as the 0th element
  40. str r0, [r1]
  41. @ do the system call
  42. @ get flags
  43. mov r0, r2
  44. @ new sp is already in r1
  45. @ load remaining arguments off the stack
  46. stmfd sp!, {r4}
  47. ldr r2, [sp, #4]
  48. ldr r3, [sp, #8]
  49. ldr r4, [sp, #12]
  50. DO_CALL (clone)
  51. movs a1, a1
  52. blt __error
  53. ldmnefd sp!, {r4}
  54. beq 1f
  55. bx lr
  56. 1:
  57. @ pick the function arg and call address off the stack and execute
  58. ldr r0, [sp, #4]
  59. ldr r1, [sp]
  60. bl 2f @ blx r1
  61. @ and we are done, passing the return value through r0
  62. bl HIDDEN_JUMPTARGET(_exit)
  63. @ Should never return
  64. b .
  65. 2:
  66. bx r1
  67. __einval:
  68. ldr r0, =-EINVAL
  69. __error:
  70. push {r3, lr}
  71. bl __syscall_error
  72. POP_RET
  73. .pool
  74. #else
  75. __clone:
  76. @ sanity check args
  77. cmp r0, #0
  78. IT(te, ne)
  79. cmpne r1, #0
  80. moveq r0, #-EINVAL
  81. beq __error
  82. @ insert the args onto the new stack
  83. sub r1, r1, #8
  84. str r3, [r1, #4]
  85. @ save the function pointer as the 0th element
  86. str r0, [r1]
  87. @ do the system call
  88. @ get flags
  89. mov r0, r2
  90. @ new sp is already in r1
  91. @ load remaining arguments off the stack
  92. stmfd sp!, {r4}
  93. ldr r2, [sp, #4]
  94. ldr r3, [sp, #8]
  95. ldr r4, [sp, #12]
  96. DO_CALL (clone)
  97. movs a1, a1
  98. ldmnefd sp!, {r4}
  99. blt __error
  100. IT(t, ne)
  101. #if defined(__USE_BX__)
  102. bxne lr
  103. #else
  104. movne pc, lr
  105. #endif
  106. @ pick the function arg and call address off the stack and execute
  107. ldr r0, [sp, #4]
  108. mov lr, pc
  109. ldr pc, [sp]
  110. @ and we are done, passing the return value through r0
  111. b HIDDEN_JUMPTARGET(_exit)
  112. __error:
  113. b __syscall_error
  114. #endif
  115. .size __clone,.-__clone
  116. weak_alias(__clone, clone)
  117. #endif