clone.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 1999, 2000, 2003, 2004, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /* clone() is even more special than fork() as it mucks with stacks
  15. and invokes a function in the right context after its all over. */
  16. #include <features.h>
  17. #include <asm/unistd.h>
  18. #include <sysdep.h>
  19. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. #ifdef RESET_PID
  22. #include <tcb-offsets.h>
  23. #endif
  24. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  25. pid_t *ptid, void *tls, pid_t *ctid); */
  26. .text
  27. ENTRY(__clone)
  28. /* sanity check arguments. */
  29. tst r4, r4
  30. bt/s 0f
  31. tst r5, r5
  32. bf 1f
  33. 0:
  34. bra .Lsyscall_error
  35. mov #-EINVAL,r0
  36. 1:
  37. /* insert the args onto the new stack */
  38. mov.l r7, @-r5
  39. /* save the function pointer as the 0th element */
  40. mov.l r4, @-r5
  41. /* do the system call */
  42. mov r6, r4
  43. mov.l @r15, r6
  44. mov.l @(8,r15), r7
  45. mov.l @(4,r15), r0
  46. mov #+SYS_ify(clone), r3
  47. trapa #0x15
  48. mov r0, r1
  49. #ifdef __sh2__
  50. /* 12 arithmetic shifts for the sh2, because shad doesn't exist! */
  51. shar r1
  52. shar r1
  53. shar r1
  54. shar r1
  55. shar r1
  56. shar r1
  57. shar r1
  58. shar r1
  59. shar r1
  60. shar r1
  61. shar r1
  62. shar r1
  63. #else
  64. mov #-12, r2
  65. shad r2, r1
  66. #endif
  67. not r1, r1 // r1=0 means r0 = -1 to -4095
  68. tst r1, r1 // i.e. error in linux
  69. bf .Lclone_end
  70. .Lsyscall_error:
  71. SYSCALL_ERROR_HANDLER
  72. .Lclone_end:
  73. tst r0, r0
  74. bt 2f
  75. .Lpseudo_end:
  76. rts
  77. nop
  78. 2:
  79. /* terminate the stack frame */
  80. mov #0, r14
  81. #ifdef RESET_PID
  82. mov r4, r0
  83. shlr16 r0
  84. tst #1, r0 // CLONE_THREAD = (1 << 16)
  85. bf/s 4f
  86. mov r4, r0
  87. /* new pid */
  88. shlr8 r0
  89. tst #1, r0 // CLONE_VM = (1 << 8)
  90. bf/s 3f
  91. mov #-1, r0
  92. mov #+SYS_ify(getpid), r3
  93. trapa #0x15
  94. 3:
  95. stc gbr, r1
  96. mov.w .Lpidoff, r2
  97. add r1, r2
  98. mov.l r0, @r2
  99. mov.w .Ltidoff, r2
  100. add r1, r2
  101. mov.l r0, @r2
  102. 4:
  103. #endif
  104. /* thread starts */
  105. mov.l @r15, r1
  106. jsr @r1
  107. mov.l @(4,r15), r4
  108. /* we are done, passing the return value through r0 */
  109. mov.l .L3, r1
  110. #ifdef SHARED
  111. mov.l r12, @-r15
  112. sts.l pr, @-r15
  113. mov r0, r4
  114. mova .LG, r0
  115. mov.l .LG, r12
  116. add r0, r12
  117. mova .L3, r0
  118. add r0, r1
  119. jsr @r1
  120. nop
  121. lds.l @r15+, pr
  122. rts
  123. mov.l @r15+, r12
  124. #else
  125. jmp @r1
  126. mov r0, r4
  127. #endif
  128. .align 2
  129. .LG:
  130. .long _GLOBAL_OFFSET_TABLE_
  131. .L3:
  132. .long PLTJMP(C_SYMBOL_NAME(_exit))
  133. #ifdef RESET_PID
  134. .Lpidoff:
  135. .word PID - TLS_PRE_TCB_SIZE
  136. .Ltidoff:
  137. .word TID - TLS_PRE_TCB_SIZE
  138. #endif
  139. PSEUDO_END (__clone)
  140. weak_alias (__clone, clone)