clone.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /* Do the system call */
  69. pushl %ebx
  70. pushl %esi
  71. pushl %edi
  72. movl TLS+12(%esp),%esi
  73. movl PTID+12(%esp),%edx
  74. movl FLAGS+12(%esp),%ebx
  75. movl CTID+12(%esp),%edi
  76. movl $__NR_clone,%eax
  77. int $0x80
  78. popl %edi
  79. popl %esi
  80. popl %ebx
  81. test %eax,%eax
  82. jl __error
  83. jz .Lthread_start
  84. ret
  85. .Lthread_start:
  86. /* Note: %esi is zero. */
  87. movl %esi,%ebp /* terminate the stack frame */
  88. call *%ebx
  89. #ifdef __PIC__
  90. call .Lhere
  91. .Lhere:
  92. popl %ebx
  93. addl $_GLOBAL_OFFSET_TABLE_+[.-.Lhere], %ebx
  94. #endif
  95. movl %eax, %ebx
  96. movl $__NR_exit, %eax
  97. int $0x80
  98. /* Need to indirect jump to syscall error
  99. * or we end up with TEXTREL's
  100. */
  101. __error:
  102. jmp __syscall_error
  103. .size clone,.-clone
  104. weak_alias(clone, __clone)