clone.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. mov #-12, r2
  50. shad r2, r1
  51. not r1, r1 // r1=0 means r0 = -1 to -4095
  52. tst r1, r1 // i.e. error in linux
  53. bf .Lclone_end
  54. .Lsyscall_error:
  55. SYSCALL_ERROR_HANDLER
  56. .Lclone_end:
  57. tst r0, r0
  58. bt 2f
  59. .Lpseudo_end:
  60. rts
  61. nop
  62. 2:
  63. /* terminate the stack frame */
  64. mov #0, r14
  65. #ifdef RESET_PID
  66. mov r4, r0
  67. shlr16 r0
  68. tst #1, r0 // CLONE_THREAD = (1 << 16)
  69. bf/s 4f
  70. mov r4, r0
  71. /* new pid */
  72. shlr8 r0
  73. tst #1, r0 // CLONE_VM = (1 << 8)
  74. bf/s 3f
  75. mov #-1, r0
  76. mov #+SYS_ify(getpid), r3
  77. trapa #0x15
  78. 3:
  79. stc gbr, r1
  80. mov.w .Lpidoff, r2
  81. add r1, r2
  82. mov.l r0, @r2
  83. mov.w .Ltidoff, r2
  84. add r1, r2
  85. mov.l r0, @r2
  86. 4:
  87. #endif
  88. /* thread starts */
  89. mov.l @r15, r1
  90. jsr @r1
  91. mov.l @(4,r15), r4
  92. /* we are done, passing the return value through r0 */
  93. mov.l .L3, r1
  94. #ifdef SHARED
  95. mov.l r12, @-r15
  96. sts.l pr, @-r15
  97. mov r0, r4
  98. mova .LG, r0
  99. mov.l .LG, r12
  100. add r0, r12
  101. mova .L3, r0
  102. add r0, r1
  103. jsr @r1
  104. nop
  105. lds.l @r15+, pr
  106. rts
  107. mov.l @r15+, r12
  108. #else
  109. jmp @r1
  110. mov r0, r4
  111. #endif
  112. .align 2
  113. .LG:
  114. .long _GLOBAL_OFFSET_TABLE_
  115. .L3:
  116. .long PLTJMP(C_SYMBOL_NAME(_exit))
  117. #ifdef RESET_PID
  118. .Lpidoff:
  119. .word PID - TLS_PRE_TCB_SIZE
  120. .Ltidoff:
  121. .word TID - TLS_PRE_TCB_SIZE
  122. #endif
  123. PSEUDO_END (__clone)
  124. weak_alias (__clone, clone)