clone.S 3.5 KB

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