clone.S 3.6 KB

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