clone.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <bits/arm_bx.h>
  23. #if defined(__NR_clone)
  24. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  25. .text
  26. .global __clone
  27. .type __clone,%function
  28. .align 2
  29. #if defined(THUMB1_ONLY)
  30. .thumb_func
  31. __clone:
  32. @ sanity check args
  33. cmp r0, #0
  34. beq __einval
  35. cmp r1, #0
  36. beq __einval
  37. @ insert the args onto the new stack
  38. sub r1, r1, #8
  39. str r3, [r1, #4]
  40. @ save the function pointer as the 0th element
  41. str r0, [r1]
  42. @ do the system call
  43. @ get flags
  44. mov r0, r2
  45. @ new sp is already in r1
  46. @ load remaining arguments off the stack
  47. stmfd sp!, {r4}
  48. ldr r2, [sp, #4]
  49. ldr r3, [sp, #8]
  50. ldr r4, [sp, #12]
  51. DO_CALL (clone)
  52. movs a1, a1
  53. blt __error
  54. ldmnefd sp!, {r4}
  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. ldr r1, [sp]
  61. bl 2f @ blx r1
  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. @ sanity check args
  78. cmp r0, #0
  79. IT(te, ne)
  80. cmpne r1, #0
  81. moveq r0, #-EINVAL
  82. beq __error
  83. @ insert the args onto the new stack
  84. sub r1, r1, #8
  85. str r3, [r1, #4]
  86. @ save the function pointer as the 0th element
  87. str r0, [r1]
  88. @ do the system call
  89. @ get flags
  90. mov r0, r2
  91. @ new sp is already in r1
  92. @ load remaining arguments off the stack
  93. stmfd sp!, {r4}
  94. ldr r2, [sp, #4]
  95. ldr r3, [sp, #8]
  96. ldr r4, [sp, #12]
  97. DO_CALL (clone)
  98. movs a1, a1
  99. IT(t, ne)
  100. ldmnefd sp!, {r4}
  101. blt __error
  102. IT(t, ne)
  103. BXC(ne, lr)
  104. @ pick the function arg and call address off the stack and execute
  105. ldr r0, [sp, #4]
  106. mov lr, pc
  107. ldr pc, [sp]
  108. @ and we are done, passing the return value through r0
  109. b HIDDEN_JUMPTARGET(_exit)
  110. __error:
  111. b __syscall_error
  112. #endif
  113. .size __clone,.-__clone
  114. weak_alias(__clone, clone)
  115. #endif