clone.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (C) 1996-2000,02,03,04,2005 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. #ifdef __PIC__
  44. jecxz __error
  45. #else
  46. testl %ecx,%ecx
  47. jz __error
  48. #endif
  49. /* no NULL stack pointers */
  50. movl STACK(%esp),%ecx
  51. #ifdef __PIC__
  52. jecxz __error
  53. #else
  54. testl %ecx,%ecx
  55. jz __error
  56. #endif
  57. /* Insert the argument onto the new stack. Make sure the new
  58. thread is started with an alignment of (mod 16). */
  59. andl $0xfffffff0, %ecx
  60. subl $28,%ecx
  61. movl ARG(%esp),%eax /* no negative argument counts */
  62. movl %eax,12(%ecx)
  63. /* Save the function pointer as the zeroth argument.
  64. It will be popped off in the child in the ebx frobbing below. */
  65. movl FUNC(%esp),%eax
  66. movl %eax,8(%ecx)
  67. /* Don't leak any information. */
  68. movl $0,4(%ecx)
  69. #ifndef RESET_PID
  70. movl $0,(%ecx)
  71. #endif
  72. /* Do the system call */
  73. pushl %ebx
  74. pushl %esi
  75. pushl %edi
  76. movl TLS+12(%esp),%esi
  77. movl PTID+12(%esp),%edx
  78. movl FLAGS+12(%esp),%ebx
  79. movl CTID+12(%esp),%edi
  80. movl $__NR_clone,%eax
  81. #ifdef RESET_PID
  82. /* Remember the flag value. */
  83. movl %ebx, (%ecx)
  84. #endif
  85. int $0x80
  86. popl %edi
  87. popl %esi
  88. popl %ebx
  89. test %eax,%eax
  90. jl __error
  91. jz .Lthread_start
  92. ret
  93. .Lthread_start:
  94. /* Note: %esi is zero. */
  95. movl %esi,%ebp /* terminate the stack frame */
  96. call *%ebx
  97. #ifdef __PIC__
  98. call .Lhere
  99. .Lhere:
  100. popl %ebx
  101. addl $_GLOBAL_OFFSET_TABLE_+[.-.Lhere], %ebx
  102. #endif
  103. movl %eax, %ebx
  104. movl $__NR_exit, %eax
  105. int $0x80
  106. /* Need to indirect jump to syscall error
  107. * or we end up with TEXTREL's
  108. */
  109. __error:
  110. jmp __syscall_error
  111. .size clone,.-clone
  112. weak_alias(clone, __clone)