clone.S 2.9 KB

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