clone.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 1996, 1997, 2000, 2003, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ralf Baechle <ralf@linux-mips.org>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  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 <sys/asm.h>
  19. #include <sysdep.h>
  20. #define _ERRNO_H 1
  21. #include <bits/errno.h>
  22. #ifdef RESET_PID
  23. #include <tls.h>
  24. #endif
  25. #define CLONE_VM 0x00000100
  26. #define CLONE_THREAD 0x00010000
  27. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  28. void *parent_tidptr, void *tls, void *child_tidptr) */
  29. .text
  30. #if _MIPS_SIM == _ABIO32
  31. # define EXTRA_LOCALS 1
  32. #else
  33. # define EXTRA_LOCALS 0
  34. #endif
  35. LOCALSZ= 4
  36. FRAMESZ= (((NARGSAVE+LOCALSZ)*SZREG)+ALSZ)&ALMASK
  37. GPOFF= FRAMESZ-(1*SZREG)
  38. NESTED(clone,4*SZREG,sp)
  39. #ifdef __PIC__
  40. SETUP_GP
  41. #endif
  42. PTR_SUBU sp, FRAMESZ
  43. SETUP_GP64 (GPOFF, clone)
  44. #ifdef __PIC__
  45. SAVE_GP (GPOFF)
  46. #endif
  47. #ifdef PROF
  48. .set noat
  49. move $1,ra
  50. jal _mcount
  51. .set at
  52. #endif
  53. /* Sanity check arguments. */
  54. li v0,EINVAL
  55. beqz a0,L(error) /* No NULL function pointers. */
  56. beqz a1,L(error) /* No NULL stack pointers. */
  57. PTR_SUBU a1,32 /* Reserve argument save space. */
  58. PTR_S a0,0(a1) /* Save function pointer. */
  59. PTR_S a3,PTRSIZE(a1) /* Save argument pointer. */
  60. #ifdef RESET_PID
  61. LONG_S a2,(PTRSIZE*2)(a1) /* Save clone flags. */
  62. #endif
  63. move a0,a2
  64. /* Shuffle in the last three arguments - arguments 5, 6, and 7 to
  65. this function, but arguments 3, 4, and 5 to the syscall. */
  66. #if _MIPS_SIM == _ABIO32
  67. PTR_L a2,(FRAMESZ+PTRSIZE+PTRSIZE+16)(sp)
  68. PTR_S a2,16(sp)
  69. PTR_L a2,(FRAMESZ+16)(sp)
  70. PTR_L a3,(FRAMESZ+PTRSIZE+16)(sp)
  71. #else
  72. move a2,a4
  73. move a3,a5
  74. move a4,a6
  75. #endif
  76. /* Do the system call */
  77. li v0,__NR_clone
  78. syscall
  79. bnez a3,L(error)
  80. beqz v0,L(thread_start)
  81. /* Successful return from the parent */
  82. RESTORE_GP64
  83. PTR_ADDU sp, FRAMESZ
  84. j $31 ; nop
  85. /* Something bad happened -- no child created */
  86. L(error):
  87. #ifdef __PIC__
  88. PTR_LA t9,__syscall_error
  89. RESTORE_GP64
  90. PTR_ADDU sp, FRAMESZ
  91. /* uClibc change -- start */
  92. move a0,v0 /* Pass return val to C function. */
  93. /* uClibc change -- stop */
  94. jr t9
  95. #else
  96. RESTORE_GP64
  97. PTR_ADDU sp, FRAMESZ
  98. /* uClibc change -- start */
  99. move a0,v0 /* Pass return val to C function. */
  100. /* uClibc change -- stop */
  101. j __syscall_error
  102. #endif
  103. END(clone)
  104. /* Load up the arguments to the function. Put this block of code in
  105. its own function so that we can terminate the stack trace with our
  106. debug info. */
  107. ENTRY(__thread_start)
  108. L(thread_start):
  109. /* cp is already loaded. */
  110. SAVE_GP (GPOFF)
  111. /* The stackframe has been created on entry of clone(). */
  112. #ifdef RESET_PID
  113. /* Check and see if we need to reset the PID. */
  114. LONG_L a0,(PTRSIZE*2)(sp)
  115. and a1,a0,CLONE_THREAD
  116. beqz a1,L(restore_pid)
  117. L(donepid):
  118. #endif
  119. /* Restore the arg for user's function. */
  120. PTR_L t9,0(sp) /* Function pointer. */
  121. PTR_L a0,PTRSIZE(sp) /* Argument pointer. */
  122. /* Call the user's function. */
  123. jal t9
  124. /* Call _exit rather than doing it inline for breakpoint purposes. */
  125. move a0,v0
  126. #ifdef __PIC__
  127. PTR_LA t9,_exit
  128. jalr t9
  129. #else
  130. jal _exit
  131. #endif
  132. #ifdef RESET_PID
  133. L(restore_pid):
  134. and a1,a0,CLONE_VM
  135. li v0,-1
  136. bnez a1,L(gotpid)
  137. li v0,__NR_getpid
  138. syscall
  139. L(gotpid):
  140. READ_THREAD_POINTER(v1)
  141. INT_S v0,PID_OFFSET(v1)
  142. INT_S v0,TID_OFFSET(v1)
  143. b L(donepid)
  144. #endif
  145. END(__thread_start)
  146. weak_alias(clone, __clone)