clone.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2001, 2005, 2007 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 it's all over. */
  15. #include "sysdep.h"
  16. #include <sys/syscall.h>
  17. #define _ERRNO_H 1
  18. #include <bits/errno.h>
  19. /* int clone (a2 = int (*fn)(void *arg),
  20. a3 = void *child_stack,
  21. a4 = int flags,
  22. a5 = void *arg,
  23. a6 = pid_t *ptid,
  24. a7 = struct user_desc *tls,
  25. 16(sp) = pid_t *ctid) */
  26. .text
  27. ENTRY (__clone)
  28. /* Sanity check arguments. */
  29. beqz a2, .Leinval /* no NULL function pointers */
  30. beqz a3, .Leinval /* no NULL stack pointers */
  31. /* a2 and a3 are candidates for destruction by system-call return
  32. parameters. We don't need the stack pointer after the system
  33. call. We trust that the kernel will preserve a7, a9, and a6. */
  34. mov a9, a5 /* save function argument */
  35. mov a5, a7
  36. mov a7, a2 /* save function pointer */
  37. mov a8, a6 /* use a8 as a temp */
  38. mov a6, a4
  39. mov a4, a8
  40. l32i a8, a1, 16 /* child_tid */
  41. movi a2, SYS_ify (clone)
  42. /* syscall (a2 = NR_clone,
  43. a6 = clone_flags,
  44. a3 = usp,
  45. a4 = parent_tid,
  46. a5 = child_tls,
  47. a8 = child_tid) */
  48. syscall
  49. bltz a2, SYSCALL_ERROR_LABEL
  50. beqz a2, .Lthread_start
  51. /* Fall through for parent. */
  52. .Lpseudo_end:
  53. retw
  54. .Leinval:
  55. movi a2, -EINVAL
  56. j SYSCALL_ERROR_LABEL
  57. .Lthread_start:
  58. /* Start child thread. */
  59. movi a0, 0 /* terminate the stack frame */
  60. #ifdef RESET_PID
  61. /* Check and see if we need to reset the PID. */
  62. bbsi.l a6, 16, 1f /* CLONE_THREAD = 0x00010000 */
  63. movi a2, -1
  64. bbsi.l a6, 8, 2f /* CLONE_VM = 0x00000100 */
  65. movi a2, SYS_ify (getpid)
  66. syscall
  67. 2: rur a3, THREADPTR
  68. movi a4, PID_OFFSET
  69. add a4, a4, a3
  70. s32i a2, a4, 0
  71. movi a4, TID_OFFSET
  72. add a4, a4, a3
  73. s32i a2, a3, 0
  74. 1:
  75. #endif /* RESET_PID */
  76. mov a6, a9 /* load up the 'arg' parameter */
  77. callx4 a7 /* call the user's function */
  78. /* Call _exit. Note that any return parameter from the user's
  79. function in a6 is seen as inputs to _exit. */
  80. movi a2, JUMPTARGET(_exit)
  81. callx4 a2
  82. PSEUDO_END (__clone)
  83. weak_alias (__clone, clone)