clone.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /* int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
  25. _syscall2(int, clone, int, flags, void *, child_stack) */
  26. ENTRY(__clone)
  27. #ifdef __NDS32_ABI_2FP_PLUS__
  28. lwi $r4, [$sp]
  29. lwi $r5, [$sp+4]
  30. #endif
  31. #ifdef PIC
  32. /* set GP register to parent only, cause child's $SP will be $r1. */
  33. pushm $fp, $gp
  34. cfi_adjust_cfa_offset(8)
  35. cfi_rel_offset(fp, 0)
  36. cfi_rel_offset(gp, 4)
  37. mfusr $r15, $pc
  38. sethi $gp, hi20(_GLOBAL_OFFSET_TABLE_+4)
  39. ori $gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_+8)
  40. add $gp, $gp, $r15
  41. #endif /* PIC */
  42. /* sanity check arguments. */
  43. beqz $r0, 1f
  44. bnez $r1, 2f
  45. 1:
  46. movi $r0, -EINVAL
  47. 5:
  48. #ifdef PIC
  49. /* restore GP register, only in parent's stack */
  50. la $r15, C_SYMBOL_NAME(__syscall_error@PLT)
  51. push $lp
  52. cfi_adjust_cfa_offset(4)
  53. cfi_rel_offset(lp, 0)
  54. addi $sp, $sp, -4
  55. cfi_adjust_cfa_offset(4)
  56. jral $r15
  57. addi $sp, $sp, 4
  58. cfi_adjust_cfa_offset(-4)
  59. pop $lp
  60. cfi_adjust_cfa_offset(-4)
  61. cfi_restore(lp)
  62. popm $fp, $gp
  63. cfi_adjust_cfa_offset(-8)
  64. cfi_restore(fp)
  65. cfi_restore(gp)
  66. ret
  67. #else /* ! PIC */
  68. la $r15, C_SYMBOL_NAME(__syscall_error)
  69. jr $r15
  70. #endif /* ! PIC */
  71. 2:
  72. /* Child's $sp will be $r1, make $sp 8-byte alignment */
  73. bitci $r1, $r1, 7
  74. /* push to child's stack only. */
  75. addi $r1, $r1, -4
  76. swi.p $r3, [$r1], -4 ! arg
  77. swi $r0, [$r1] ! fn
  78. /* do the system call */
  79. or $r0, $r2, $r2 ! move $r0, $r2
  80. move $r3, $r5
  81. move $r5, $r2 ! Use $r5 to backup $r2
  82. ! The pt_regs is placed in $r5 in kerenl (sys_clone_wrapper)
  83. move $r2, $r4
  84. #ifdef __NDS32_ABI_2FP_PLUS__
  85. # ifdef PIC
  86. lwi $r4, [$sp+#0x10]
  87. # else
  88. lwi $r4, [$sp+#0x8]
  89. # endif
  90. #else
  91. # ifdef PIC
  92. lwi $r4, [$sp+#0x8]
  93. # else
  94. lwi $r4, [$sp]
  95. # endif
  96. #endif
  97. __do_syscall(clone)
  98. beqz $r0, 4f
  99. bltz $r0, 5b
  100. 10:
  101. #ifdef PIC
  102. /* restore GP register, only in parent's stack */
  103. popm $fp, $gp
  104. cfi_adjust_cfa_offset(-8)
  105. cfi_restore(gp)
  106. cfi_restore(fp)
  107. #endif /* PIC */
  108. ret
  109. 4:
  110. /* Only in child's stack. */
  111. pop $r1 ! fn
  112. pop $r0 ! arg
  113. #if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
  114. addi $sp, $sp, -24
  115. #endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__) */
  116. ! use $r15 in case _exit is PIC
  117. bral $r1
  118. #if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
  119. addi $sp, $sp, 24
  120. #endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__) */
  121. ! use $r15 in case _exit is PIC
  122. #ifdef PIC
  123. la $r15, C_SYMBOL_NAME(_exit@PLT)
  124. #else /* ! PIC */
  125. la $r15, C_SYMBOL_NAME(_exit)
  126. #endif /* ! PIC */
  127. jr $r15
  128. PSEUDO_END (__clone)
  129. weak_alias (__clone, clone)