clone.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  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. #include <features.h>
  18. #define _ERRNO_H 1
  19. #include <bits/errno.h>
  20. #include <sys/syscall.h>
  21. #define _SCHED_H
  22. #include <bits/sched.h>
  23. /* The userland implementation is:
  24. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  25. the kernel entry is:
  26. int clone (long flags, void *child_stack).
  27. The parameters are passed in register and on the stack from userland:
  28. rdi: fn
  29. rsi: child_stack
  30. rdx: flags
  31. rcx: arg
  32. r8d: TID field in parent
  33. r9d: thread pointer
  34. %esp+8: TID field in child
  35. The kernel expects:
  36. rax: system call number
  37. rdi: flags
  38. rsi: child_stack
  39. rdx: TID field in parent
  40. r10: TID field in child
  41. r8: thread pointer */
  42. .text
  43. .global clone
  44. .type clone,%function
  45. .align 4
  46. clone:
  47. /* Sanity check arguments. */
  48. movq $-EINVAL,%rax
  49. testq %rdi,%rdi /* no NULL function pointers */
  50. jz __syscall_error
  51. testq %rsi,%rsi /* no NULL stack pointers */
  52. jz __syscall_error
  53. /* Insert the argument onto the new stack. */
  54. subq $16,%rsi
  55. movq %rcx,8(%rsi)
  56. /* Save the function pointer. It will be popped off in the
  57. child in the ebx frobbing below. */
  58. movq %rdi,0(%rsi)
  59. /* Do the system call. */
  60. movq %rdx, %rdi
  61. movq %r8, %rdx
  62. movq %r9, %r8
  63. movq 8(%rsp), %r10
  64. movl $__NR_clone,%eax
  65. syscall
  66. testq %rax,%rax
  67. jl __syscall_error
  68. jz .Lthread_start
  69. .Lpseudo_end:
  70. ret
  71. .Lthread_start:
  72. /* Clear the frame pointer. The ABI suggests this be done, to mark
  73. the outermost frame obviously. */
  74. xorl %ebp, %ebp
  75. #ifdef RESET_PID
  76. testq $CLONE_THREAD, %rdi
  77. jne 1f
  78. testq $CLONE_VM, %rdi
  79. movl $-1, %eax
  80. jne 2f
  81. movl $__NR_getpid, %eax
  82. syscall
  83. 2: movl %eax, %fs:PID
  84. movl %eax, %fs:TID
  85. 1:
  86. #endif
  87. /* Set up arguments for the function call. */
  88. popq %rax /* Function to call. */
  89. popq %rdi /* Argument. */
  90. call *%rax
  91. /* Call exit with return value from function call. */
  92. movq %rax, %rdi
  93. call HIDDEN_JUMPTARGET(_exit)
  94. .size clone,.-clone