clone.S 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by David Huggins-Daines <dhd@debian.org>, 2000.
  4. Based on the Alpha version by Richard Henderson <rth@tamu.edu>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* clone() is even more special than fork() as it mucks with stacks
  18. and invokes a function in the right context after its all over. */
  19. #include <asm/unistd.h>
  20. #define _ERRNO_H 1
  21. #include <bits/errno.h>
  22. #include <sys/syscall.h>
  23. /* Non-thread code calls __clone with the following parameters:
  24. int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg)
  25. NPTL Code will call __clone with the following parameters:
  26. int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  27. int *parent_tidptr, struct user_desc *newtls, int *child_pidptr)
  28. The code should not mangle the extra input registers.
  29. Syscall expects: Input to __clone:
  30. 4(r25) - function pointer (r26, arg0)
  31. 0(r25) - argument (r23, arg3)
  32. r26 - clone flags. (r24, arg2)
  33. r25+64 - user stack pointer. (r25, arg1)
  34. r24 - parent tid pointer. (stack - 52)
  35. r23 - struct user_desc newtls pointer. (stack - 56)
  36. r22 - child tid pointer. (stack - 60)
  37. r20 - clone syscall number (constant)
  38. */
  39. .text
  40. .global __clone
  41. .type __clone,%function
  42. __clone:
  43. /* Sanity check arguments. */
  44. ldi -EINVAL,%ret0
  45. comib,=,n 0,%arg0,.Lerror /* no NULL function pointers */
  46. comib,=,n 0,%arg1,.Lerror /* no NULL stack pointers */
  47. /* Save the fn ptr and arg on the new stack. */
  48. stwm %r26,64(%r25)
  49. stw %r23,-60(%r25)
  50. /* Clone arguments are (int flags, void * child_stack) */
  51. copy %r24,%r26 /* flags are first */
  52. /* User stack pointer is in the correct register already */
  53. /* Load args from stack... */
  54. ldw -52(%sp), %r24 /* Load parent_tidptr */
  55. ldw -56(%sp), %r23 /* Load newtls */
  56. ldw -60(%sp), %r22 /* Load child_tidptr */
  57. /* Create frame to get r3 free */
  58. copy %sp, %r21
  59. stwm %r3, 64(%sp)
  60. stw %r21, -4(%sp)
  61. /* Save the PIC register. */
  62. #ifdef __PIC__
  63. copy %r19, %r3 /* parent */
  64. #endif
  65. /* Do the system call */
  66. ble 0x100(%sr2,%r0)
  67. ldi __NR_clone,%r20
  68. ldi -4096,%r1
  69. comclr,>>= %r1,%ret0,%r0 /* Note: unsigned compare. */
  70. b,n .LerrorRest
  71. comib,=,n 0,%ret0,thread_start
  72. /* Successful return from the parent
  73. No need to restore the PIC register,
  74. since we return immediately. */
  75. bv %r0(%rp)
  76. ldwm -64(%sp), %r3
  77. .LerrorRest:
  78. /* Restore the PIC register on error */
  79. #ifdef __PIC__
  80. copy %r3, %r19 /* parent */
  81. #endif
  82. /* Something bad happened -- no child created */
  83. .Lerror:
  84. /* Set errno, save ret0 so we return with that value. */
  85. copy %ret0, %r3
  86. b __syscall_error
  87. sub %r0,%ret0,%arg0
  88. copy %r3, %ret0
  89. /* Return after setting errno, and restoring ret0 */
  90. bv %r0(%rp)
  91. ldwm -64(%sp), %r3
  92. thread_start:
  93. /* Load up the arguments. */
  94. ldw -60(%sr0, %sp),%arg0
  95. ldw -64(%sr0, %sp),%r22
  96. /* $$dyncall fixes childs PIC register */
  97. /* Call the user's function */
  98. bl $$dyncall,%r31
  99. copy %r31,%rp
  100. bl HIDDEN_JUMPTARGET(_exit),%rp
  101. copy %ret0,%arg0
  102. /* Die horribly. */
  103. iitlbp %r0,(%sr0,%r0)
  104. .size clone,.-clone
  105. weak_alias (__clone, clone)