clone.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /* 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. j $31 ; nop
  56. /* Something bad happened -- no child created */
  57. error:
  58. addiu sp,32
  59. /* uClibc change -- start */
  60. move a0,v0 /* Pass return val to C function. */
  61. /* uClibc change -- stop */
  62. #ifdef __PIC__
  63. la t9,__syscall_error
  64. jr t9
  65. #else
  66. j __syscall_error
  67. #endif
  68. .end __clone
  69. /* Load up the arguments to the function. Put this block of code in
  70. its own function so that we can terminate the stack trace with our
  71. debug info. */
  72. .globl __thread_start;
  73. .align 2;
  74. .ent __thread_start, 0;
  75. __thread_start:
  76. /* cp is already loaded. */
  77. .cprestore 16
  78. /* The stackframe has been created on entry of clone(). */
  79. /* Restore the arg for user's function. */
  80. lw t9,0(sp) /* Function pointer. */
  81. lw a0,4(sp) /* Argument pointer. */
  82. /* Call the user's function. */
  83. jal t9
  84. /* Call _exit rather than doing it inline for breakpoint purposes. */
  85. move a0,v0
  86. #ifdef __PIC__
  87. la t9,_exit
  88. jalr t9
  89. #else
  90. jal _exit
  91. #endif
  92. .end __thread_start
  93. .weak clone;
  94. clone = __clone