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