clone.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 1996,1997,98,99,2000,02,03 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Richard Henderson (rth@tamu.edu)
  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. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  19. */
  20. #define _ERRNO_H 1
  21. #include <features.h>
  22. #include <bits/errno.h>
  23. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  24. pid_t *ptid, struct user_desc *tls, pid_t *ctid); */
  25. #define LINKAGE 4
  26. #define PTR_SIZE 4
  27. #define PARMS LINKAGE /* no space for saved regs */
  28. #define FUNC PARMS
  29. #define STACK FUNC+4
  30. #define FLAGS STACK+PTR_SIZE
  31. #define ARG FLAGS+4
  32. #define PTID ARG+PTR_SIZE
  33. #define TLS PTID+PTR_SIZE
  34. #define CTID TLS+PTR_SIZE
  35. #define __NR_clone 120
  36. #define __NR_exit 1
  37. .text
  38. .type clone,@function;
  39. .weak clone ; clone = __clone
  40. .type __clone,@function;
  41. .globl __clone;
  42. __clone:
  43. /* Sanity check arguments. */
  44. movl $-EINVAL,%eax
  45. /* no NULL function pointers */
  46. movl FUNC(%esp),%ecx
  47. jecxz .Lclone_error
  48. /* no NULL stack pointers */
  49. movl STACK(%esp),%ecx
  50. jecxz .Lclone_error
  51. /* Insert the argument onto the new stack. Make sure the new
  52. thread is started with an alignment of (mod 16). */
  53. andl $0xfffffff0, %ecx
  54. subl $24,%ecx
  55. movl ARG(%esp),%eax /* no negative argument counts */
  56. movl %eax,12(%ecx)
  57. /* Save the function pointer as the zeroth argument.
  58. It will be popped off in the child in the ebx frobbing below. */
  59. movl FUNC(%esp),%eax
  60. movl %eax,8(%ecx)
  61. /* Don't leak any information. */
  62. movl $0,4(%ecx)
  63. movl $0,(%ecx)
  64. /* Do the system call */
  65. pushl %ebx
  66. pushl %esi
  67. pushl %edi
  68. movl TLS+12(%esp),%esi
  69. movl PTID+12(%esp),%edx
  70. movl FLAGS+12(%esp),%ebx
  71. movl CTID+12(%esp),%edi
  72. movl $__NR_clone,%eax
  73. int $0x80
  74. popl %edi
  75. popl %esi
  76. popl %ebx
  77. test %eax,%eax
  78. jl .Lclone_error
  79. jz .Lthread_start
  80. .Lpseudo_end:
  81. ret
  82. .Lthread_start:
  83. subl %ebp,%ebp /* terminate the stack frame */
  84. call *%ebx
  85. #ifdef __PIC__
  86. call .Lhere
  87. .Lhere:
  88. popl %ebx
  89. addl $_GLOBAL_OFFSET_TABLE_+[.-.Lhere], %ebx
  90. #endif
  91. movl %eax, %ebx
  92. movl $__NR_exit, %eax
  93. int $0x80
  94. #ifdef __PIC__
  95. .Lthere:
  96. movl (%esp), %ebx
  97. ret
  98. .Lclone_error:
  99. pushl %ebx
  100. call .Lthere
  101. addl $_GLOBAL_OFFSET_TABLE_, %ebx
  102. xorl %edx, %edx
  103. subl %eax, %edx
  104. pushl %edx
  105. call __errno_location@PLT
  106. popl %ecx
  107. popl %ebx
  108. movl %ecx, (%eax)
  109. orl $-1, %eax
  110. jmp .Lpseudo_end
  111. #else /* __PIC__ */
  112. .Lclone_error:
  113. negl %eax
  114. pushl %eax
  115. call __errno_location
  116. popl %ecx
  117. movl %ecx, (%eax)
  118. xorl %eax, %eax
  119. decl %eax
  120. #endif
  121. .size __clone,.-__clone