clone.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. /* clone() is even more special than fork() as it mucks with stacks
  17. and invokes a function in the right context after its all over. */
  18. #include <features.h>
  19. #include <sys/asm.h>
  20. #include <sysdep.h>
  21. #define _ERRNO_H 1
  22. #include <bits/errno.h>
  23. #ifdef RESET_PID
  24. #include <tls.h>
  25. #endif
  26. #define CLONE_VM 0x00000100
  27. #define CLONE_THREAD 0x00010000
  28. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  29. void *parent_tidptr, void *tls, void *child_tidptr) */
  30. .text
  31. #if _MIPS_SIM == _ABIO32
  32. # define EXTRA_LOCALS 1
  33. #else
  34. # define EXTRA_LOCALS 0
  35. #endif
  36. LOCALSZ= 4
  37. FRAMESZ= (((NARGSAVE+LOCALSZ)*SZREG)+ALSZ)&ALMASK
  38. GPOFF= FRAMESZ-(1*SZREG)
  39. NESTED(clone,4*SZREG,sp)
  40. #ifdef __PIC__
  41. SETUP_GP
  42. #endif
  43. PTR_SUBU sp, FRAMESZ
  44. SETUP_GP64 (GPOFF, clone)
  45. #ifdef __PIC__
  46. SAVE_GP (GPOFF)
  47. #endif
  48. #ifdef PROF
  49. .set noat
  50. move $1,ra
  51. jal _mcount
  52. .set at
  53. #endif
  54. /* Sanity check arguments. */
  55. li v0,EINVAL
  56. beqz a0,L(error) /* No NULL function pointers. */
  57. beqz a1,L(error) /* No NULL stack pointers. */
  58. PTR_SUBU a1,32 /* Reserve argument save space. */
  59. PTR_S a0,0(a1) /* Save function pointer. */
  60. PTR_S a3,PTRSIZE(a1) /* Save argument pointer. */
  61. #ifdef RESET_PID
  62. LONG_S a2,(PTRSIZE*2)(a1) /* Save clone flags. */
  63. #endif
  64. move a0,a2
  65. /* Shuffle in the last three arguments - arguments 5, 6, and 7 to
  66. this function, but arguments 3, 4, and 5 to the syscall. */
  67. #if _MIPS_SIM == _ABIO32
  68. PTR_L a2,(FRAMESZ+PTRSIZE+PTRSIZE+16)(sp)
  69. PTR_S a2,16(sp)
  70. PTR_L a2,(FRAMESZ+16)(sp)
  71. PTR_L a3,(FRAMESZ+PTRSIZE+16)(sp)
  72. #else
  73. move a2,a4
  74. move a3,a5
  75. move a4,a6
  76. #endif
  77. /* Do the system call */
  78. li v0,__NR_clone
  79. syscall
  80. bnez a3,L(error)
  81. beqz v0,L(thread_start)
  82. /* Successful return from the parent */
  83. RESTORE_GP64
  84. PTR_ADDU sp, FRAMESZ
  85. j $31 ; nop
  86. /* Something bad happened -- no child created */
  87. L(error):
  88. #ifdef __PIC__
  89. PTR_LA t9,__syscall_error
  90. RESTORE_GP64
  91. PTR_ADDU sp, FRAMESZ
  92. /* uClibc change -- start */
  93. move a0,v0 /* Pass return val to C function. */
  94. /* uClibc change -- stop */
  95. jr t9
  96. #else
  97. RESTORE_GP64
  98. PTR_ADDU sp, FRAMESZ
  99. /* uClibc change -- start */
  100. move a0,v0 /* Pass return val to C function. */
  101. /* uClibc change -- stop */
  102. j __syscall_error
  103. #endif
  104. END(clone)
  105. /* Load up the arguments to the function. Put this block of code in
  106. its own function so that we can terminate the stack trace with our
  107. debug info. */
  108. ENTRY(__thread_start)
  109. L(thread_start):
  110. /* cp is already loaded. */
  111. SAVE_GP (GPOFF)
  112. /* The stackframe has been created on entry of clone(). */
  113. #ifdef RESET_PID
  114. /* Check and see if we need to reset the PID. */
  115. LONG_L a0,(PTRSIZE*2)(sp)
  116. and a1,a0,CLONE_THREAD
  117. beqz a1,L(restore_pid)
  118. L(donepid):
  119. #endif
  120. /* Restore the arg for user's function. */
  121. PTR_L t9,0(sp) /* Function pointer. */
  122. PTR_L a0,PTRSIZE(sp) /* Argument pointer. */
  123. /* Call the user's function. */
  124. jal t9
  125. /* Call _exit rather than doing it inline for breakpoint purposes. */
  126. move a0,v0
  127. #ifdef __PIC__
  128. PTR_LA t9,_exit
  129. jalr t9
  130. #else
  131. jal _exit
  132. #endif
  133. #ifdef RESET_PID
  134. L(restore_pid):
  135. and a1,a0,CLONE_VM
  136. li v0,-1
  137. bnez a1,L(gotpid)
  138. li v0,__NR_getpid
  139. syscall
  140. L(gotpid):
  141. READ_THREAD_POINTER(v1)
  142. INT_S v0,PID_OFFSET(v1)
  143. INT_S v0,TID_OFFSET(v1)
  144. b L(donepid)
  145. #endif
  146. END(__thread_start)
  147. weak_alias(clone, __clone)