clone.S 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  2. The GNU C Library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public
  4. License as published by the Free Software Foundation; either
  5. version 2.1 of the License, or (at your option) any later version.
  6. The GNU C Library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public
  11. License along with the GNU C Library; if not, see
  12. <http://www.gnu.org/licenses/>. */
  13. /* clone() is even more special than fork() as it mucks with stacks
  14. and invokes a function in the right context after its all over. */
  15. #include <features.h>
  16. #define _ERRNO_H 1
  17. #include <bits/errno.h>
  18. #include <sys/syscall.h>
  19. /* The userland implementation is:
  20. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  21. the kernel entry is:
  22. int clone (long flags, void *child_stack).
  23. The parameters are passed in register and on the stack from userland:
  24. rdi: fn
  25. rsi: child_stack
  26. rdx: flags
  27. rcx: arg
  28. r8d: TID field in parent
  29. r9d: thread pointer
  30. %esp+8: TID field in child
  31. The kernel expects:
  32. rax: system call number
  33. rdi: flags
  34. rsi: child_stack
  35. rdx: TID field in parent
  36. r10: TID field in child
  37. r8: thread pointer */
  38. .text
  39. .global clone
  40. .type clone,%function
  41. clone:
  42. /* Sanity check arguments. */
  43. movq $-EINVAL,%rax
  44. testq %rdi,%rdi /* no NULL function pointers */
  45. jz __syscall_error
  46. testq %rsi,%rsi /* no NULL stack pointers */
  47. jz __syscall_error
  48. /* Insert the argument onto the new stack. */
  49. subq $16,%rsi
  50. movq %rcx,8(%rsi)
  51. /* Save the function pointer. It will be popped off in the
  52. child in the ebx frobbing below. */
  53. movq %rdi,0(%rsi)
  54. /* Do the system call. */
  55. movq %rdx, %rdi
  56. movq %r8, %rdx
  57. movq %r9, %r8
  58. movq 8(%rsp), %r10
  59. movl $__NR_clone,%eax
  60. syscall
  61. testq %rax,%rax
  62. jl __syscall_error
  63. jz .Lthread_start
  64. .Lpseudo_end:
  65. ret
  66. .Lthread_start:
  67. /* Clear the frame pointer. The ABI suggests this be done, to mark
  68. the outermost frame obviously. */
  69. xorl %ebp, %ebp
  70. /* Set up arguments for the function call. */
  71. popq %rax /* Function to call. */
  72. popq %rdi /* Argument. */
  73. call *%rax
  74. /* Call exit with return value from function call. */
  75. movq %rax, %rdi
  76. movl $__NR_exit, %eax
  77. syscall
  78. .size clone,.-clone
  79. weak_alias(clone, __clone)