clone.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  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 <features.h>
  18. #include <asm/unistd.h>
  19. #include <sysdep.h>
  20. #define _ERRNO_H 1
  21. #include <bits/errno.h>
  22. #ifdef RESET_PID
  23. #include <tcb-offsets.h>
  24. #endif
  25. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  26. pid_t *ptid, void *tls, pid_t *ctid); */
  27. .text
  28. ENTRY(__clone)
  29. /* sanity check arguments. */
  30. tst r4, r4
  31. bt/s 0f
  32. tst r5, r5
  33. bf 1f
  34. 0:
  35. bra .Lsyscall_error
  36. mov #-EINVAL,r0
  37. 1:
  38. /* insert the args onto the new stack */
  39. mov.l r7, @-r5
  40. /* save the function pointer as the 0th element */
  41. mov.l r4, @-r5
  42. /* do the system call */
  43. mov r6, r4
  44. mov.l @r15, r6
  45. mov.l @(8,r15), r7
  46. mov.l @(4,r15), r0
  47. mov #+SYS_ify(clone), r3
  48. trapa #0x15
  49. mov r0, r1
  50. mov #-12, r2
  51. shad r2, r1
  52. not r1, r1 // r1=0 means r0 = -1 to -4095
  53. tst r1, r1 // i.e. error in linux
  54. bf .Lclone_end
  55. .Lsyscall_error:
  56. SYSCALL_ERROR_HANDLER
  57. .Lclone_end:
  58. tst r0, r0
  59. bt 2f
  60. .Lpseudo_end:
  61. rts
  62. nop
  63. 2:
  64. /* terminate the stack frame */
  65. mov #0, r14
  66. #ifdef RESET_PID
  67. mov r4, r0
  68. shlr16 r0
  69. tst #1, r0 // CLONE_THREAD = (1 << 16)
  70. bf/s 4f
  71. mov r4, r0
  72. /* new pid */
  73. shlr8 r0
  74. tst #1, r0 // CLONE_VM = (1 << 8)
  75. bf/s 3f
  76. mov #-1, r0
  77. mov #+SYS_ify(getpid), r3
  78. trapa #0x15
  79. 3:
  80. stc gbr, r1
  81. mov.w .Lpidoff, r2
  82. add r1, r2
  83. mov.l r0, @r2
  84. mov.w .Ltidoff, r2
  85. add r1, r2
  86. mov.l r0, @r2
  87. 4:
  88. #endif
  89. /* thread starts */
  90. mov.l @r15, r1
  91. jsr @r1
  92. mov.l @(4,r15), r4
  93. /* we are done, passing the return value through r0 */
  94. mov.l .L3, r1
  95. #ifdef SHARED
  96. mov.l r12, @-r15
  97. sts.l pr, @-r15
  98. mov r0, r4
  99. mova .LG, r0
  100. mov.l .LG, r12
  101. add r0, r12
  102. mova .L3, r0
  103. add r0, r1
  104. jsr @r1
  105. nop
  106. lds.l @r15+, pr
  107. rts
  108. mov.l @r15+, r12
  109. #else
  110. jmp @r1
  111. mov r0, r4
  112. #endif
  113. .align 2
  114. .LG:
  115. .long _GLOBAL_OFFSET_TABLE_
  116. .L3:
  117. .long PLTJMP(C_SYMBOL_NAME(_exit))
  118. #ifdef RESET_PID
  119. .Lpidoff:
  120. .word PID - TLS_PRE_TCB_SIZE
  121. .Ltidoff:
  122. .word TID - TLS_PRE_TCB_SIZE
  123. #endif
  124. PSEUDO_END (__clone)
  125. weak_alias (__clone, clone)