clone.S 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <asm/unistd.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. .text
  24. NESTED(__clone,4*SZREG,sp)
  25. #ifdef __PIC__
  26. .set noreorder
  27. .cpload $25
  28. .set reorder
  29. subu sp,32
  30. .cprestore 16
  31. #else
  32. subu sp,32
  33. #endif
  34. #ifdef PROF
  35. .set noat
  36. move $1,ra
  37. jal _mcount
  38. .set at
  39. #endif
  40. /* Sanity check arguments. */
  41. li v0,EINVAL
  42. beqz a0,error /* No NULL function pointers. */
  43. beqz a1,error /* No NULL stack pointers. */
  44. subu a1,32 /* Reserve argument save space. */
  45. sw a0,0(a1) /* Save function pointer. */
  46. sw a3,4(a1) /* Save argument pointer. */
  47. /* Do the system call */
  48. move a0,a2
  49. li v0,__NR_clone
  50. syscall
  51. bnez a3,error
  52. beqz v0,__thread_start
  53. /* Successful return from the parent */
  54. addiu sp,32
  55. ret
  56. /* Something bad happened -- no child created */
  57. error:
  58. addiu sp,32
  59. #ifdef __PIC__
  60. la t9,__syscall_error
  61. jr t9
  62. #else
  63. j __syscall_error
  64. #endif
  65. END(__clone)
  66. /* Load up the arguments to the function. Put this block of code in
  67. its own function so that we can terminate the stack trace with our
  68. debug info. */
  69. ENTRY(__thread_start)
  70. /* cp is already loaded. */
  71. .cprestore 16
  72. /* The stackframe has been created on entry of clone(). */
  73. /* Restore the arg for user's function. */
  74. lw t9,0(sp) /* Function pointer. */
  75. lw a0,4(sp) /* Argument pointer. */
  76. /* Call the user's function. */
  77. jalr t9
  78. /* Call _exit rather than doing it inline for breakpoint purposes. */
  79. move a0,v0
  80. #ifdef __PIC__
  81. la t9,_exit
  82. jalr t9
  83. #else
  84. jal _exit
  85. #endif
  86. END(__thread_start)
  87. weak_alias(__clone, clone)