clone.S 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. /* clone() is even more special than fork() as it mucks with stacks
  16. and invokes a function in the right context after its all over. */
  17. #include <features.h>
  18. #include <asm/unistd.h>
  19. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. #ifdef __HAVE_SHARED__
  22. #define PLTJMP(_x) _x##@PLT
  23. #else
  24. #define PLTJMP(_x) _x
  25. #endif
  26. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  27. .text
  28. .text
  29. .align 4
  30. .type clone,@function
  31. .globl clone;
  32. clone:
  33. /* sanity check arguments. */
  34. tst r4, r4
  35. bt 0f
  36. tst r5, r5
  37. bf/s 1f
  38. mov #+__NR_clone, r3
  39. 0:
  40. bra __syscall_error
  41. mov #-EINVAL, r4
  42. 1:
  43. /* insert the args onto the new stack */
  44. mov.l r7, @-r5
  45. /* save the function pointer as the 0th element */
  46. mov.l r4, @-r5
  47. /* do the system call */
  48. mov r6, r4
  49. trapa #0x12
  50. mov r0, r1
  51. #ifdef __sh2__
  52. // 12 arithmetic shifts for the crappy sh2, because shad doesn't exist!
  53. shar r1
  54. shar r1
  55. shar r1
  56. shar r1
  57. shar r1
  58. shar r1
  59. shar r1
  60. shar r1
  61. shar r1
  62. shar r1
  63. shar r1
  64. shar r1
  65. #else
  66. mov #-12, r2
  67. shad r2, r1
  68. #endif
  69. not r1, r1 // r1=0 means r0 = -1 to -4095
  70. tst r1, r1 // i.e. error in linux
  71. bf/s 2f
  72. tst r0, r0
  73. bra __syscall_error
  74. mov r0, r4
  75. 2:
  76. bt 3f
  77. rts
  78. nop
  79. 3:
  80. /* thread starts */
  81. mov.l @r15, r1
  82. jsr @r1
  83. mov.l @(4,r15), r4
  84. /* we are done, passing the return value through r0 */
  85. mov.l .L1, r1
  86. #ifdef __HAVE_SHARED__
  87. mov.l r12, @-r15
  88. sts.l pr, @-r15
  89. mov r0, r4
  90. mova .LG, r0 /* .LG from syscall_error.S */
  91. mov.l .LG, r12
  92. add r0, r12
  93. mova .L1, r0
  94. add r0, r1
  95. jsr @r1
  96. nop
  97. lds.l @r15+, pr
  98. rts
  99. mov.l @r15+, r12
  100. #else
  101. jmp @r1
  102. mov r0, r4
  103. #endif
  104. .align 2
  105. .L1:
  106. .long PLTJMP( HIDDEN_JUMPTARGET(_exit))
  107. .size clone,.-clone;
  108. #include "syscall_error.S"