clone.S 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp> */
  2. /* clone is even more special than fork as it mucks with stacks
  3. and invokes a function in the right context after its all over. */
  4. #define _ERRNO_H
  5. #include <bits/errno.h>
  6. #include <sys/syscall.h>
  7. /* int _clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg
  8. void *parent_tidptr, void *tls, void *child_tidptr) */
  9. #ifdef __H8300H__
  10. .h8300h
  11. #endif
  12. #ifdef __H8300S__
  13. .h8300s
  14. #endif
  15. .text
  16. .globl clone
  17. clone:
  18. /* Sanity check arguments. */
  19. mov.l #-EINVAL,er3
  20. mov.l er0,er0 /* no NULL function pointers */
  21. beq __syscall_error
  22. mov.l er1,er1 /* no NULL stack pointers */
  23. beq __syscall_error
  24. /* Allocate space and copy the argument onto the new stack. */
  25. mov.l @(4:16,sp),er3
  26. mov.l er3,@-er1
  27. /* setup argument */
  28. mov.l er0,er3 /* er3 = child entry */
  29. sub.l #20,sp
  30. mov.l er2,@sp /* flags */
  31. mov.l er1,@(4,sp) /* new sp */
  32. mov.l sp,er1
  33. mov.l @(20+8,sp),er0
  34. mov.l er0,@er1 /* parent tid */
  35. adds #4,er1
  36. mov.l @(20+16,sp),er0
  37. mov.l er0,@er1 /* child tid */
  38. adds #4,er1
  39. mov.l @(20+12,sp),er0
  40. mov.l er0,@er1 /* tls */
  41. /* do the system call */
  42. mov.l sp,er1
  43. mov.l #__NR_clone,er0
  44. trapa #0
  45. add.l #20,sp
  46. mov.l er0,er0
  47. bmi __syscall_error
  48. beq thread_start
  49. rts
  50. __syscall_error:
  51. neg.l er0
  52. mov.l er0,@-sp
  53. #if !defined(__PIC__)
  54. jsr @__errno_location
  55. #else
  56. mov.l @(__errno_location@GOTOFF,er5),er1
  57. jsr @er1
  58. #endif
  59. mov.l @sp,er1
  60. mov.l er1,@er0
  61. sub.l er0,er0
  62. dec.l #1,er0
  63. rts
  64. thread_start:
  65. mov.l @sp+,er0 /* restore args */
  66. jsr @er3
  67. mov.l er0,er1
  68. mov.l #__NR_exit,er0
  69. trapa #0
  70. .end