clone.S 3.3 KB

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