clone.S 2.9 KB

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