clone.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>, 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 <asm/unistd.h>
  20. #include <sys/regdef.h>
  21. #define _ERRNO_H 1
  22. #include <bits/errno.h>
  23. #include <sys/asm.h>
  24. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg) */
  25. .text
  26. .globl clone ;
  27. .align 2;
  28. .type clone,@function;
  29. .ent clone, 0;
  30. clone:
  31. .frame sp, 4*SZREG, sp
  32. #ifdef __PIC__
  33. #if _MIPS_SIM == _MIPS_SIM_ABI32
  34. .set noreorder
  35. .cpload $25
  36. .set reorder
  37. subu sp,32
  38. .cprestore 16
  39. #else /* N32 */
  40. PTR_SUBU sp,32 /* fn, arg, gp, pad */
  41. .cpsetup $25, 16, clone
  42. #endif /* N32 */
  43. #else
  44. subu sp,32
  45. #endif
  46. /* Sanity check arguments. */
  47. li v0,EINVAL
  48. beqz a0,L(error) /* No NULL function pointers. */
  49. beqz a1,L(error) /* No NULL stack pointers. */
  50. #if _MIPS_SIM != _MIPS_SIM_ABI32
  51. and a1,~(16-1) /* force alignment */
  52. #endif
  53. PTR_SUBU a1,32 /* Reserve argument save space. */
  54. PTR_S a0,0(a1) /* Save function pointer. */
  55. PTR_S a3,PTRSIZE(a1) /* Save argument pointer. */
  56. /* Do the system call */
  57. move a0,a2
  58. li v0,__NR_clone
  59. syscall
  60. bnez a3,L(error)
  61. beqz v0,L(__thread_start)
  62. /* Successful return from the parent */
  63. #if _MIPS_SIM != _MIPS_SIM_ABI32
  64. .cpreturn
  65. #endif
  66. PTR_ADDU sp,32
  67. j $31 ; nop
  68. /* Something bad happened -- no child created */
  69. L(error):
  70. #if _MIPS_SIM != _MIPS_SIM_ABI32
  71. .cpreturn
  72. #endif
  73. PTR_ADDU sp,32
  74. /* uClibc change -- start */
  75. move a0,v0 /* Pass return val to C function. */
  76. /* uClibc change -- stop */
  77. #ifdef __PIC__
  78. PTR_LA t9,__syscall_error
  79. jr t9
  80. #else
  81. j __syscall_error
  82. #endif
  83. .end clone
  84. /* Load up the arguments to the function. Put this block of code in
  85. its own function so that we can terminate the stack trace with our
  86. debug info. */
  87. .globl __thread_start;
  88. .align 2;
  89. .ent __thread_start, 0;
  90. __thread_start:
  91. L(__thread_start):
  92. #if _MIPS_SIM == _MIPS_SIM_ABI32
  93. .frame sp, 24, sp
  94. /* cp is already loaded. */
  95. .cprestore 16
  96. #endif
  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