clone.S 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* clone() is even more special than fork() as it mucks with stacks
  16. and invokes a function in the right context after its all over. */
  17. #include <features.h>
  18. #define _ERRNO_H 1
  19. #include <bits/errno.h>
  20. #include <sys/syscall.h>
  21. #include <sys/regdef.h>
  22. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg) */
  23. .text
  24. .globl clone;
  25. .align 3;
  26. .ent clone , 0;
  27. clone:
  28. .frame $30 , 0, $26
  29. .prologue 0
  30. /* Sanity check arguments. */
  31. ldiq v0,EINVAL
  32. beq a0,$error /* no NULL function pointers */
  33. beq a1,$error /* no NULL stack pointers */
  34. /* Save the fn ptr and arg on the new stack. */
  35. subq a1,16,a1
  36. stq a0,0(a1)
  37. stq a3,8(a1)
  38. /* Do the system call */
  39. mov a2,a0
  40. ldiq v0,__NR_clone
  41. call_pal 131
  42. bne a3,$error
  43. beq v0,thread_start
  44. /* Successful return from the parent */
  45. ret
  46. /* Something bad happened -- no child created */
  47. $error:
  48. br gp,1f
  49. 1: ldgp gp,0(gp)
  50. jmp zero,__syscall_error
  51. .end clone
  52. /* Load up the arguments to the function. Put this block of code in
  53. its own function so that we can terminate the stack trace with our
  54. debug info. */
  55. .ent thread_start
  56. thread_start:
  57. .frame fp,0,zero,0
  58. mov zero,fp
  59. .prologue 0
  60. /* Load up the arguments. */
  61. ldq pv,0($30)
  62. ldq a0,8($30)
  63. addq $30,16,$30
  64. /* Call the user's function */
  65. jsr ra,(pv)
  66. ldgp gp,0(ra)
  67. /* Call _exit rather than doing it inline for breakpoint purposes */
  68. mov v0,a0
  69. jsr ra,HIDDEN_JUMPTARGET(_exit)
  70. /* Die horribly. */
  71. halt
  72. .end thread_start