clone.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, 16 /* 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. retw
  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. 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. #ifdef PIC
  83. movi a2, _exit@PLT
  84. #else
  85. movi a2, _exit
  86. #endif
  87. callx4 a2
  88. PSEUDO_END (__clone)
  89. weak_alias (__clone, clone)