clone.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2016-2017 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* Copyright (C) 2010-2014 Free Software Foundation, Inc.
  6. Contributed by Pat Beirne <patb@corelcomputer.com>
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, write to the Free
  17. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA. */
  19. /* clone() is even more special than fork() as it mucks with stacks
  20. and invokes a function in the right context after its all over. */
  21. #include <sysdep.h>
  22. #define _ERRNO_H 1
  23. #include <bits/errno.h>
  24. #define CLONE_VM 0x00000100
  25. #define CLONE_THREAD 0x00010000
  26. /* int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
  27. _syscall2(int, clone, int, flags, void *, child_stack) */
  28. ENTRY(__clone)
  29. #ifdef __NDS32_ABI_2FP_PLUS__
  30. lwi $r4, [$sp]
  31. lwi $r5, [$sp+4]
  32. #endif
  33. #ifdef PIC
  34. /* set GP register to parent only, cause child's $SP will be $r1. */
  35. pushm $fp, $gp
  36. cfi_adjust_cfa_offset(8)
  37. cfi_rel_offset(fp, 0)
  38. cfi_rel_offset(gp, 4)
  39. mfusr $r15, $pc
  40. sethi $gp, hi20(_GLOBAL_OFFSET_TABLE_+4)
  41. ori $gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_+8)
  42. add $gp, $gp, $r15
  43. #endif /* PIC */
  44. /* sanity check arguments. */
  45. beqz $r0, 1f
  46. bnez $r1, 2f
  47. 1:
  48. movi $r0, -EINVAL
  49. 5:
  50. #ifdef PIC
  51. /* restore GP register, only in parent's stack */
  52. la $r15, C_SYMBOL_NAME(__syscall_error@PLT)
  53. push $lp
  54. cfi_adjust_cfa_offset(4)
  55. cfi_rel_offset(lp, 0)
  56. addi $sp, $sp, -4
  57. cfi_adjust_cfa_offset(4)
  58. jral $r15
  59. addi $sp, $sp, 4
  60. cfi_adjust_cfa_offset(-4)
  61. pop $lp
  62. cfi_adjust_cfa_offset(-4)
  63. cfi_restore(lp)
  64. popm $fp, $gp
  65. cfi_adjust_cfa_offset(-8)
  66. cfi_restore(fp)
  67. cfi_restore(gp)
  68. ret
  69. #else /* ! PIC */
  70. la $r15, C_SYMBOL_NAME(__syscall_error)
  71. jr $r15
  72. #endif /* ! PIC */
  73. 2:
  74. /* Child's $sp will be $r1, make $sp 8-byte alignment */
  75. bitci $r1, $r1, 7
  76. /* push to child's stack only. */
  77. addi $r1, $r1, -4
  78. swi.p $r3, [$r1], -4 ! arg
  79. swi $r0, [$r1] ! fn
  80. /* do the system call */
  81. or $r0, $r2, $r2 ! move $r0, $r2
  82. move $r3, $r5
  83. move $r5, $r2 ! Use $r5 to backup $r2
  84. ! The pt_regs is placed in $r5 in kerenl (sys_clone_wrapper)
  85. move $r2, $r4
  86. #ifdef __NDS32_ABI_2FP_PLUS__
  87. # ifdef PIC
  88. lwi $r4, [$sp+#0x10]
  89. # else
  90. lwi $r4, [$sp+#0x8]
  91. # endif
  92. #else
  93. # ifdef PIC
  94. lwi $r4, [$sp+#0x8]
  95. # else
  96. lwi $r4, [$sp]
  97. # endif
  98. #endif
  99. __do_syscall(clone)
  100. beqz $r0, 4f
  101. bltz $r0, 5b
  102. 10:
  103. #ifdef PIC
  104. /* restore GP register, only in parent's stack */
  105. popm $fp, $gp
  106. cfi_adjust_cfa_offset(-8)
  107. cfi_restore(gp)
  108. cfi_restore(fp)
  109. #endif /* PIC */
  110. ret
  111. 4:
  112. /* Only in child's stack. */
  113. pop $r1 ! fn
  114. pop $r0 ! arg
  115. #if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
  116. addi $sp, $sp, -24
  117. #endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__) */
  118. ! use $r15 in case _exit is PIC
  119. bral $r1
  120. #if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
  121. addi $sp, $sp, 24
  122. #endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__) */
  123. ! use $r15 in case _exit is PIC
  124. #ifdef PIC
  125. la $r15, C_SYMBOL_NAME(_exit@PLT)
  126. #else /* ! PIC */
  127. la $r15, C_SYMBOL_NAME(_exit)
  128. #endif /* ! PIC */
  129. jr $r15
  130. PSEUDO_END (__clone)
  131. weak_alias (__clone, clone)