clone.S 3.2 KB

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