clone.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, see
  15. <http://www.gnu.org/licenses/>. */
  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 <asm/unistd.h>
  19. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. #include <sys/syscall.h>
  22. /* Non-thread code calls __clone with the following parameters:
  23. int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg)
  24. NPTL Code will call __clone with the following parameters:
  25. int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  26. int *parent_tidptr, struct user_desc *newtls, int *child_pidptr)
  27. The code should not mangle the extra input registers.
  28. Syscall expects: Input to __clone:
  29. 4(r25) - function pointer (r26, arg0)
  30. 0(r25) - argument (r23, arg3)
  31. r26 - clone flags. (r24, arg2)
  32. r25+64 - user stack pointer. (r25, arg1)
  33. r24 - parent tid pointer. (stack - 52)
  34. r23 - struct user_desc newtls pointer. (stack - 56)
  35. r22 - child tid pointer. (stack - 60)
  36. r20 - clone syscall number (constant)
  37. */
  38. .text
  39. .global __clone
  40. .type __clone,%function
  41. __clone:
  42. /* Sanity check arguments. */
  43. ldi -EINVAL,%ret0
  44. comib,=,n 0,%arg0,.Lerror /* no NULL function pointers */
  45. comib,=,n 0,%arg1,.Lerror /* no NULL stack pointers */
  46. /* Save the fn ptr and arg on the new stack. */
  47. stwm %r26,64(%r25)
  48. stw %r23,-60(%r25)
  49. /* Clone arguments are (int flags, void * child_stack) */
  50. copy %r24,%r26 /* flags are first */
  51. /* User stack pointer is in the correct register already */
  52. /* Load args from stack... */
  53. ldw -52(%sp), %r24 /* Load parent_tidptr */
  54. ldw -56(%sp), %r23 /* Load newtls */
  55. ldw -60(%sp), %r22 /* Load child_tidptr */
  56. /* Create frame to get r3 free */
  57. copy %sp, %r21
  58. stwm %r3, 64(%sp)
  59. stw %r21, -4(%sp)
  60. /* Save the PIC register. */
  61. #ifdef __PIC__
  62. copy %r19, %r3 /* parent */
  63. #endif
  64. /* Do the system call */
  65. ble 0x100(%sr2,%r0)
  66. ldi __NR_clone,%r20
  67. ldi -4096,%r1
  68. comclr,>>= %r1,%ret0,%r0 /* Note: unsigned compare. */
  69. b,n .LerrorRest
  70. comib,=,n 0,%ret0,thread_start
  71. /* Successful return from the parent
  72. No need to restore the PIC register,
  73. since we return immediately. */
  74. bv %r0(%rp)
  75. ldwm -64(%sp), %r3
  76. .LerrorRest:
  77. /* Restore the PIC register on error */
  78. #ifdef __PIC__
  79. copy %r3, %r19 /* parent */
  80. #endif
  81. /* Something bad happened -- no child created */
  82. .Lerror:
  83. /* Set errno, save ret0 so we return with that value. */
  84. copy %ret0, %r3
  85. b __syscall_error
  86. sub %r0,%ret0,%arg0
  87. copy %r3, %ret0
  88. /* Return after setting errno, and restoring ret0 */
  89. bv %r0(%rp)
  90. ldwm -64(%sp), %r3
  91. thread_start:
  92. /* Load up the arguments. */
  93. ldw -60(%sr0, %sp),%arg0
  94. ldw -64(%sr0, %sp),%r22
  95. /* $$dyncall fixes childs PIC register */
  96. /* Call the user's function */
  97. bl $$dyncall,%r31
  98. copy %r31,%rp
  99. bl HIDDEN_JUMPTARGET(_exit),%rp
  100. copy %ret0,%arg0
  101. /* Die horribly. */
  102. iitlbp %r0,(%sr0,%r0)
  103. .size clone,.-clone
  104. weak_alias (__clone, clone)