clone.S 1.6 KB

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