clone.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2001, 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 Library General Public License as
  4. published by the Free Software Foundation; either version 2 of the
  5. 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. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with the GNU C Library; see the file COPYING.LIB. If not,
  12. see <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. #include <sysdep.h>
  17. #define _ERRNO_H 1
  18. #include <bits/errno.h>
  19. #ifdef RESET_PID
  20. #include <tls.h>
  21. #endif
  22. #define __ASSEMBLY__
  23. #include <linux/sched.h>
  24. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  25. a2 a3 a4 a5
  26. pid_t *ptid, struct user_desc *tls, pid_t *ctid)
  27. a6 a7 16(sp)
  28. */
  29. .text
  30. ENTRY (__clone)
  31. /* Sanity check arguments. */
  32. beqz a2, .Leinval /* no NULL function pointers */
  33. beqz a3, .Leinval /* no NULL stack pointers */
  34. /* a2 and a3 are candidates for destruction by system-call return
  35. parameters. We don't need the stack pointer after the system
  36. call. We trust that the kernel will preserve a6, a7 and a9. */
  37. mov a9, a5 /* save function argument */
  38. mov a5, a7
  39. mov a7, a2 /* save function pointer */
  40. mov a8, a6 /* use a8 as a temp */
  41. mov a6, a4
  42. mov a4, a8
  43. l32i a8, a1, FRAMESIZE /* child_tid */
  44. movi a2, SYS_ify(clone)
  45. /* syscall(NR_clone,clone_flags, usp, parent_tid, child_tls, child_tid)
  46. a2 a6 a3 a4 a5 a8
  47. */
  48. syscall
  49. bltz a2, SYSCALL_ERROR_LABEL
  50. beqz a2, .Lthread_start
  51. /* fall through for parent */
  52. .Lpseudo_end:
  53. abi_ret
  54. .Leinval:
  55. movi a2, -EINVAL
  56. j SYSCALL_ERROR_LABEL
  57. .Lthread_start:
  58. #if CLONE_THREAD != 0x00010000 || CLONE_VM != 0x00000100
  59. # error invalid values for CLONE_THREAD or CLONE_VM
  60. #endif
  61. #ifdef RESET_PID
  62. bbsi.l a6, 16, .Lskip_restore_pid /* CLONE_THREAD = 0x00010000 */
  63. movi a2, -1
  64. bbsi a6, 8, .Lgotpid /* CLONE_VM = 0x00000100 */
  65. movi a2, SYS_ify(getpid)
  66. syscall
  67. .Lgotpid:
  68. rur a3, threadptr
  69. movi a0, TLS_PRE_TCB_SIZE
  70. sub a3, a3, a0
  71. s32i a2, a3, PID
  72. s32i a2, a3, TID
  73. .Lskip_restore_pid:
  74. #endif
  75. /* start child thread */
  76. movi a0, 0 /* terminate the stack frame */
  77. #if defined(__XTENSA_WINDOWED_ABI__)
  78. mov a6, a9 /* load up the 'arg' parameter */
  79. callx4 a7 /* call the user's function */
  80. /* Call _exit. Note that any return parameter from the user's
  81. function in a6 is seen as inputs to _exit. */
  82. movi a2, JUMPTARGET(_exit)
  83. callx4 a2
  84. #elif defined(__XTENSA_CALL0_ABI__)
  85. mov a2, a9 /* load up the 'arg' parameter */
  86. callx0 a7 /* call the user's function */
  87. /* Call _exit. Note that any return parameter from the user's
  88. function in a2 is seen as inputs to _exit. */
  89. movi a0, JUMPTARGET(_exit)
  90. callx0 a0
  91. #else
  92. #error Unsupported Xtensa ABI
  93. #endif
  94. PSEUDO_END (__clone)
  95. weak_alias (__clone, clone)