clone.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  23. void *parent_tidptr, void *tls, void *child_tidptr) */
  24. .text
  25. #if _MIPS_SIM == _ABIO32
  26. # define EXTRA_LOCALS 1
  27. #else
  28. # define EXTRA_LOCALS 0
  29. #endif
  30. LOCALSZ= 4
  31. FRAMESZ= (((NARGSAVE+LOCALSZ)*SZREG)+ALSZ)&ALMASK
  32. GPOFF= FRAMESZ-(1*SZREG)
  33. NESTED(clone,4*SZREG,sp)
  34. #ifdef __PIC__
  35. SETUP_GP
  36. #endif
  37. PTR_SUBU sp, FRAMESZ
  38. SETUP_GP64 (GPOFF, clone)
  39. #ifdef __PIC__
  40. SAVE_GP (GPOFF)
  41. #endif
  42. /* Sanity check arguments. */
  43. li v0,EINVAL
  44. beqz a0,L(error) /* No NULL function pointers. */
  45. beqz a1,L(error) /* No NULL stack pointers. */
  46. PTR_SUBU a1,32 /* Reserve argument save space. */
  47. PTR_S a0,0(a1) /* Save function pointer. */
  48. PTR_S a3,PTRSIZE(a1) /* Save argument pointer. */
  49. move a0,a2
  50. /* Shuffle in the last three arguments - arguments 5, 6, and 7 to
  51. this function, but arguments 3, 4, and 5 to the syscall. */
  52. #if _MIPS_SIM == _ABIO32
  53. PTR_L a2,(FRAMESZ+PTRSIZE+PTRSIZE+16)(sp)
  54. PTR_S a2,16(sp)
  55. PTR_L a2,(FRAMESZ+16)(sp)
  56. PTR_L a3,(FRAMESZ+PTRSIZE+16)(sp)
  57. #else
  58. move a2,a4
  59. move a3,a5
  60. move a4,a6
  61. #endif
  62. /* Do the system call */
  63. li v0,__NR_clone
  64. syscall
  65. bnez a3,L(error)
  66. beqz v0,L(thread_start)
  67. /* Successful return from the parent */
  68. RESTORE_GP64
  69. PTR_ADDU sp, FRAMESZ
  70. j $31 ; nop
  71. /* Something bad happened -- no child created */
  72. L(error):
  73. #ifdef __PIC__
  74. PTR_LA t9,__syscall_error
  75. RESTORE_GP64
  76. PTR_ADDU sp, FRAMESZ
  77. /* uClibc change -- start */
  78. move a0,v0 /* Pass return val to C function. */
  79. /* uClibc change -- stop */
  80. jr t9
  81. #else
  82. RESTORE_GP64
  83. PTR_ADDU sp, FRAMESZ
  84. /* uClibc change -- start */
  85. move a0,v0 /* Pass return val to C function. */
  86. /* uClibc change -- stop */
  87. j __syscall_error
  88. #endif
  89. END(clone)
  90. /* Load up the arguments to the function. Put this block of code in
  91. its own function so that we can terminate the stack trace with our
  92. debug info. */
  93. ENTRY(__thread_start)
  94. L(thread_start):
  95. /* cp is already loaded. */
  96. SAVE_GP (GPOFF)
  97. /* The stackframe has been created on entry of clone(). */
  98. /* Restore the arg for user's function. */
  99. PTR_L t9,0(sp) /* Function pointer. */
  100. PTR_L a0,PTRSIZE(sp) /* Argument pointer. */
  101. /* Call the user's function. */
  102. jal t9
  103. /* Call _exit rather than doing it inline for breakpoint purposes. */
  104. move a0,v0
  105. #ifdef __PIC__
  106. PTR_LA t9,_exit
  107. jalr t9
  108. #else
  109. jal _exit
  110. #endif
  111. END(__thread_start)
  112. weak_alias(clone, __clone)