clone.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifdef PROF
  39. ldgp gp,0(pv)
  40. .set noat
  41. lda AT, _mcount
  42. jsr AT, (AT), _mcount
  43. .set at
  44. .prologue 1
  45. #else
  46. .prologue 0
  47. #endif
  48. /* Sanity check arguments. */
  49. ldiq v0,EINVAL
  50. beq a0,$error /* no NULL function pointers */
  51. beq a1,$error /* no NULL stack pointers */
  52. /* Save the fn ptr and arg on the new stack. */
  53. subq a1,16,a1
  54. stq a0,0(a1)
  55. stq a3,8(a1)
  56. /* Do the system call */
  57. mov a2,a0
  58. ldiq v0,__NR_clone
  59. call_pal 131
  60. bne a3,$error
  61. beq v0,thread_start
  62. /* Successful return from the parent */
  63. ret
  64. /* Something bad happened -- no child created */
  65. $error:
  66. #ifndef PROF
  67. br gp,1f
  68. 1: ldgp gp,0(gp)
  69. #endif
  70. jmp zero,__syscall_error
  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. .ent thread_start
  76. thread_start:
  77. .frame fp,0,zero,0
  78. mov zero,fp
  79. .prologue 0
  80. /* Load up the arguments. */
  81. ldq pv,0($30)
  82. ldq a0,8($30)
  83. addq $30,16,$30
  84. /* Call the user's function */
  85. jsr ra,(pv)
  86. ldgp gp,0(ra)
  87. /* Call _exit rather than doing it inline for breakpoint purposes */
  88. mov v0,a0
  89. jsr ra,_exit
  90. /* Die horribly. */
  91. halt
  92. .end thread_start
  93. .weak clone;
  94. clone = __clone