clone.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* Sanity check arguments. */
  48. li v0,EINVAL
  49. beqz a0,L(error) /* No NULL function pointers. */
  50. beqz a1,L(error) /* No NULL stack pointers. */
  51. PTR_SUBU a1,32 /* Reserve argument save space. */
  52. PTR_S a0,0(a1) /* Save function pointer. */
  53. PTR_S a3,PTRSIZE(a1) /* Save argument pointer. */
  54. #ifdef RESET_PID
  55. LONG_S a2,(PTRSIZE*2)(a1) /* Save clone flags. */
  56. #endif
  57. move a0,a2
  58. /* Shuffle in the last three arguments - arguments 5, 6, and 7 to
  59. this function, but arguments 3, 4, and 5 to the syscall. */
  60. #if _MIPS_SIM == _ABIO32
  61. PTR_L a2,(FRAMESZ+PTRSIZE+PTRSIZE+16)(sp)
  62. PTR_S a2,16(sp)
  63. PTR_L a2,(FRAMESZ+16)(sp)
  64. PTR_L a3,(FRAMESZ+PTRSIZE+16)(sp)
  65. #else
  66. move a2,a4
  67. move a3,a5
  68. move a4,a6
  69. #endif
  70. /* Do the system call */
  71. li v0,__NR_clone
  72. syscall
  73. bnez a3,L(error)
  74. beqz v0,L(thread_start)
  75. /* Successful return from the parent */
  76. RESTORE_GP64
  77. PTR_ADDU sp, FRAMESZ
  78. j $31 ; nop
  79. /* Something bad happened -- no child created */
  80. L(error):
  81. #ifdef __PIC__
  82. PTR_LA t9,__syscall_error
  83. RESTORE_GP64
  84. PTR_ADDU sp, FRAMESZ
  85. /* uClibc change -- start */
  86. move a0,v0 /* Pass return val to C function. */
  87. /* uClibc change -- stop */
  88. jr t9
  89. #else
  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. j __syscall_error
  96. #endif
  97. END(clone)
  98. /* Load up the arguments to the function. Put this block of code in
  99. its own function so that we can terminate the stack trace with our
  100. debug info. */
  101. ENTRY(__thread_start)
  102. L(thread_start):
  103. /* cp is already loaded. */
  104. SAVE_GP (GPOFF)
  105. /* The stackframe has been created on entry of clone(). */
  106. #ifdef RESET_PID
  107. /* Check and see if we need to reset the PID. */
  108. LONG_L a0,(PTRSIZE*2)(sp)
  109. and a1,a0,CLONE_THREAD
  110. beqz a1,L(restore_pid)
  111. L(donepid):
  112. #endif
  113. /* Restore the arg for user's function. */
  114. PTR_L t9,0(sp) /* Function pointer. */
  115. PTR_L a0,PTRSIZE(sp) /* Argument pointer. */
  116. /* Call the user's function. */
  117. jal t9
  118. /* Call _exit rather than doing it inline for breakpoint purposes. */
  119. move a0,v0
  120. #ifdef __PIC__
  121. PTR_LA t9,_exit
  122. jalr t9
  123. #else
  124. jal _exit
  125. #endif
  126. #ifdef RESET_PID
  127. L(restore_pid):
  128. and a1,a0,CLONE_VM
  129. li v0,-1
  130. bnez a1,L(gotpid)
  131. li v0,__NR_getpid
  132. syscall
  133. L(gotpid):
  134. READ_THREAD_POINTER(v1)
  135. INT_S v0,PID_OFFSET(v1)
  136. INT_S v0,TID_OFFSET(v1)
  137. b L(donepid)
  138. #endif
  139. END(__thread_start)
  140. weak_alias(clone, __clone)