clone.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <sys/syscall.h>
  19. #define _ERRNO_H
  20. #include <bits/errno.h>
  21. #include <bits/sysnum.h>
  22. #ifdef __HAVE_SHARED__
  23. #define PLTJMP(_x) _x@PLT
  24. #else
  25. #define PLTJMP(_x) _x
  26. #endif
  27. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  28. .text
  29. .text
  30. .align 4
  31. .type clone,@function
  32. .globl clone;
  33. clone:
  34. /* sanity check arguments. */
  35. tst r4, r4
  36. bt 0f
  37. tst r5, r5
  38. bf/s 1f
  39. mov #+__NR_clone, r3
  40. 0:
  41. bra __syscall_error
  42. mov #-EINVAL, r4
  43. 1:
  44. /* insert the args onto the new stack */
  45. mov.l r7, @-r5
  46. /* save the function pointer as the 0th element */
  47. mov.l r4, @-r5
  48. /* do the system call */
  49. mov r6, r4
  50. trapa #(__SH_SYSCALL_TRAP_BASE + 2)
  51. mov r0, r1
  52. #ifdef __CONFIG_SH2__
  53. /* 12 arithmetic shifts for the crappy sh2, because shad doesn't exist! */
  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. shar r1
  66. #else
  67. mov #-12, r2
  68. shad r2, r1
  69. #endif
  70. not r1, r1 /* r1=0 means r0 = -1 to -4095 */
  71. tst r1, r1 /* i.e. error in linux */
  72. bf/s 2f
  73. tst r0, r0
  74. bra __syscall_error
  75. mov r0, r4
  76. 2:
  77. bt 3f
  78. rts
  79. nop
  80. 3:
  81. /* thread starts */
  82. mov.l @r15, r1
  83. jsr @r1
  84. mov.l @(4,r15), r4
  85. /* we are done, passing the return value through r0 */
  86. mov.l .L1, r1
  87. #ifdef __HAVE_SHARED__
  88. mov.l r12, @-r15
  89. sts.l pr, @-r15
  90. mov r0, r4
  91. mova .LG, r0 /* .LG from syscall_error.S */
  92. mov.l .LG, r12
  93. add r0, r12
  94. mova .L1, r0
  95. add r0, r1
  96. jsr @r1
  97. nop
  98. lds.l @r15+, pr
  99. rts
  100. mov.l @r15+, r12
  101. #else
  102. jmp @r1
  103. mov r0, r4
  104. #endif
  105. .align 2
  106. .L1:
  107. .long PLTJMP( HIDDEN_JUMPTARGET(_exit))
  108. .size clone,.-clone;
  109. #include "syscall_error.S"