clone.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* clone() is even more special than fork() as it mucks with stacks
  16. and invokes a function in the right context after its all over.
  17. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  18. */
  19. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. #include <sys/syscall.h>
  22. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  23. pid_t *ptid, struct user_desc *tls, pid_t *ctid); */
  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. .global clone
  36. .type clone,%function
  37. clone:
  38. /* Sanity check arguments. */
  39. movl $-EINVAL,%eax
  40. /* no NULL function pointers */
  41. movl FUNC(%esp),%ecx
  42. #ifdef __PIC__
  43. jecxz __error
  44. #else
  45. testl %ecx,%ecx
  46. jz __error
  47. #endif
  48. /* no NULL stack pointers */
  49. movl STACK(%esp),%ecx
  50. #ifdef __PIC__
  51. jecxz __error
  52. #else
  53. testl %ecx,%ecx
  54. jz __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 $28,%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. #ifndef RESET_PID
  69. movl $0,(%ecx)
  70. #endif
  71. /* Do the system call */
  72. pushl %ebx
  73. pushl %esi
  74. pushl %edi
  75. movl TLS+12(%esp),%esi
  76. movl PTID+12(%esp),%edx
  77. movl FLAGS+12(%esp),%ebx
  78. movl CTID+12(%esp),%edi
  79. movl $__NR_clone,%eax
  80. #ifdef RESET_PID
  81. /* Remember the flag value. */
  82. movl %ebx, (%ecx)
  83. #endif
  84. int $0x80
  85. popl %edi
  86. popl %esi
  87. popl %ebx
  88. test %eax,%eax
  89. jl __error
  90. jz .Lthread_start
  91. ret
  92. .Lthread_start:
  93. /* Note: %esi is zero. */
  94. movl %esi,%ebp /* terminate the stack frame */
  95. call *%ebx
  96. #ifdef __PIC__
  97. call .Lhere
  98. .Lhere:
  99. popl %ebx
  100. addl $_GLOBAL_OFFSET_TABLE_+[.-.Lhere], %ebx
  101. #endif
  102. movl %eax, %ebx
  103. movl $__NR_exit, %eax
  104. int $0x80
  105. /* Need to indirect jump to syscall error
  106. * or we end up with TEXTREL's
  107. */
  108. __error:
  109. jmp __syscall_error
  110. .size clone,.-clone
  111. weak_alias(clone, __clone)