clone.S 2.9 KB

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