clone.S 2.7 KB

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