clone.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 1999, 2000 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. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. #if defined __HAVE_ELF__ && defined __HAVE_SHARED__
  22. #define PLTJMP(_x) _x##@PLT
  23. #else
  24. #define PLTJMP(_x) _x
  25. #endif
  26. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  27. .text
  28. .extern __syscall_error
  29. .text
  30. .align 4
  31. .type __clone,@function
  32. .globl __clone;
  33. __clone:
  34. /* sanity check arguments. */
  35. tst r4, r4
  36. bf/s 1f
  37. tst r5, r5
  38. bf/s 1f
  39. mov.l .L1, r1
  40. #ifdef __HAVE_SHARED__
  41. mov.l r12, @-r15
  42. sts.l pr, @-r15
  43. mov.l .LG, r12
  44. mova .LG, r0
  45. add r0, r12
  46. mova .L1, r0
  47. add r0, r1
  48. jsr @r1
  49. mov #-EINVAL, r4
  50. lds.l @r15+, pr
  51. rts
  52. mov.l @r15+, r12
  53. #else
  54. jmp @r1
  55. mov #-EINVAL, r4
  56. #endif
  57. .align 2
  58. .L1:
  59. .long PLTJMP(__syscall_error)
  60. 1:
  61. /* insert the args onto the new stack */
  62. mov.l r7, @-r5
  63. /* save the function pointer as the 0th element */
  64. mov.l r4, @-r5
  65. /* do the system call */
  66. mov r6, r4
  67. mov #+__NR_clone, r3
  68. trapa #0x12
  69. mov r0, r1
  70. mov #-12, r2
  71. shad r2, r1
  72. not r1, r1 // r1=0 means r0 = -1 to -4095
  73. tst r1, r1 // i.e. error in linux
  74. bf 2f
  75. mov.l .L2, r1
  76. #ifdef __HAVE_SHARED__
  77. mov r0, r4
  78. mov.l r12, @-r15
  79. sts.l pr, @-r15
  80. mov.l .LG, r12
  81. mova .LG, r0
  82. add r0, r12
  83. mova .L2, r0
  84. add r0, r1
  85. jsr @r1
  86. nop
  87. lds.l @r15+, pr
  88. rts
  89. mov.l @r15+, r12
  90. #else
  91. jmp @r1
  92. mov r0, r4
  93. #endif
  94. .align 2
  95. .L2:
  96. .long PLTJMP(__syscall_error)
  97. 2:
  98. tst r0, r0
  99. bt 3f
  100. rts
  101. nop
  102. 3:
  103. /* thread starts */
  104. mov.l @r15, r1
  105. jsr @r1
  106. mov.l @(4,r15), r4
  107. /* we are done, passing the return value through r0 */
  108. mov.l .L3, r1
  109. #ifdef __HAVE_SHARED__
  110. mov.l r12, @-r15
  111. sts.l pr, @-r15
  112. mov r0, r4
  113. mova .LG, r0
  114. mov.l .LG, r12
  115. add r0, r12
  116. mova .L3, r0
  117. add r0, r1
  118. jsr @r1
  119. nop
  120. lds.l @r15+, pr
  121. rts
  122. mov.l @r15+, r12
  123. #else
  124. jmp @r1
  125. mov r0, r4
  126. #endif
  127. .align 2
  128. .LG:
  129. .long _GLOBAL_OFFSET_TABLE_
  130. .L3:
  131. .long PLTJMP(_exit)
  132. .size __clone,.-__clone;
  133. .globl clone;
  134. clone = __clone