clone.S 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Richard Henderson <rth@tamu.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. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg) */
  22. #define a0 $16
  23. #define a1 $17
  24. #define a2 $18
  25. #define a3 $19
  26. #define v0 $0
  27. #define fp $15
  28. #define gp $29
  29. #define ra $26
  30. #define pv $27
  31. #define zero $31
  32. .text
  33. .globl __clone;
  34. .align 3;
  35. .ent __clone , 0;
  36. __clone:
  37. .frame $30 , 0, $26
  38. .prologue 0
  39. /* Sanity check arguments. */
  40. ldiq v0,EINVAL
  41. beq a0,$error /* no NULL function pointers */
  42. beq a1,$error /* no NULL stack pointers */
  43. /* Save the fn ptr and arg on the new stack. */
  44. subq a1,16,a1
  45. stq a0,0(a1)
  46. stq a3,8(a1)
  47. /* Do the system call */
  48. mov a2,a0
  49. ldiq v0,__NR_clone
  50. call_pal 131
  51. bne a3,$error
  52. beq v0,thread_start
  53. /* Successful return from the parent */
  54. ret
  55. /* Something bad happened -- no child created */
  56. $error:
  57. br gp,1f
  58. 1: ldgp gp,0(gp)
  59. jmp zero,__syscall_error
  60. .end __clone
  61. /* Load up the arguments to the function. Put this block of code in
  62. its own function so that we can terminate the stack trace with our
  63. debug info. */
  64. .ent thread_start
  65. thread_start:
  66. .frame fp,0,zero,0
  67. mov zero,fp
  68. .prologue 0
  69. /* Load up the arguments. */
  70. ldq pv,0($30)
  71. ldq a0,8($30)
  72. addq $30,16,$30
  73. /* Call the user's function */
  74. jsr ra,(pv)
  75. ldgp gp,0(ra)
  76. /* Call _exit rather than doing it inline for breakpoint purposes */
  77. mov v0,a0
  78. jsr ra,_exit
  79. /* Die horribly. */
  80. halt
  81. .end thread_start
  82. .weak clone;
  83. clone = __clone