clone.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #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. .globl __thread_start;
  70. .align 2;
  71. .ent __thread_start, 0;
  72. __thread_start:
  73. /* cp is already loaded. */
  74. .cprestore 16
  75. /* The stackframe has been created on entry of clone(). */
  76. /* Restore the arg for user's function. */
  77. lw t9,0(sp) /* Function pointer. */
  78. lw a0,4(sp) /* Argument pointer. */
  79. /* Call the user's function. */
  80. jalr t9
  81. /* Call _exit rather than doing it inline for breakpoint purposes. */
  82. move a0,v0
  83. #ifdef __PIC__
  84. la t9,_exit
  85. jalr t9
  86. #else
  87. jal _exit
  88. #endif
  89. .end __thread_start
  90. .weak clone;
  91. clone = __clone