Fix #11, Support OSX for Apple Darwin, macOS · ossrs/state-threads@91d530e
1+2+/* If user disable the ASM, such as avoiding bugs in ASM, donot compile it. */
3+#if !defined(MD_ST_NO_ASM)
4+5+#if defined(__amd64__) || defined(__x86_64__)
6+7+ /****************************************************************/
8+9+ /*
10+ * Internal __jmp_buf layout
11+ */
12+ #define JB_RBX 0
13+ #define JB_RBP 1
14+ #define JB_R12 2 /* Backup IP, https://www.cnblogs.com/Five100Miles/p/8458561.html */
15+ #define JB_R13 3 /* Backup SP, https://www.cnblogs.com/Five100Miles/p/8458561.html */
16+ #define JB_R14 4 /* Backup LR, https://www.cnblogs.com/Five100Miles/p/8458561.html */
17+ #define JB_R15 5 /* Backup PC, https://www.cnblogs.com/Five100Miles/p/8458561.html */
18+ #define JB_RSP 6
19+ #define JB_PC 7
20+21+.file "md_darwin.S"
22+.text
23+24+ /* _st_md_cxt_save(__jmp_buf env) */ /* The env is rdi, http://blog.chinaunix.net/uid-20157960-id-1974354.html */
25+.globl __st_md_cxt_save
26+.align 16
27+__st_md_cxt_save:
28+ /*
29+ * Save registers.
30+ */
31+movq %rbx, (JB_RBX*8)(%rdi) /* Save rbx to env[0], *(int64_t*)(rdi+0)=rbx */
32+movq %rbp, (JB_RBP*8)(%rdi) /* Save rbp to env[1], *(int64_t*)(rdi+1)=rbp */
33+movq %r12, (JB_R12*8)(%rdi) /* Save r12 to env[2], *(int64_t*)(rdi+2)=r12 */
34+movq %r13, (JB_R13*8)(%rdi) /* Save r13 to env[3], *(int64_t*)(rdi+3)=r13 */
35+movq %r14, (JB_R14*8)(%rdi) /* Save r14 to env[4], *(int64_t*)(rdi+4)=r14 */
36+movq %r15, (JB_R15*8)(%rdi) /* Save r15 to env[5], *(int64_t*)(rdi+5)=r15 */
37+ /* Save SP */
38+leaq 8(%rsp), %rdx /* Save *(int64_t*)(rsp+8) to rdx, https://my.oschina.net/guonaihong/blog/508907 */
39+movq %rdx, (JB_RSP*8)(%rdi) /* Save rdx(rsp) to env[6], *(int64_t*)(rdi+6)=rdx */
40+ /* Save PC we are returning to */
41+movq (%rsp), %rax /* Save PC(parent function address) %(rsp) to rax */
42+movq %rax, (JB_PC*8)(%rdi) /* Save rax(PC) to env[7], *(int64_t*)(rdi+7)=rax */
43+xorq %rax, %rax /* Reset rax to 0 */
44+ret
45+46+47+ /****************************************************************/
48+49+ /* _st_md_cxt_restore(__jmp_buf env, int val) */ /* The env is rdi, val is esi/rsi, http://blog.chinaunix.net/uid-20157960-id-1974354.html */
50+.globl __st_md_cxt_restore
51+.align 16
52+__st_md_cxt_restore:
53+ /*
54+ * Restore registers.
55+ */
56+movq (JB_RBX*8)(%rdi), %rbx /* Load rbx from env[0] */
57+movq (JB_RBP*8)(%rdi), %rbp /* Load rbp from env[1] */
58+movq (JB_R12*8)(%rdi), %r12 /* Load r12 from env[2] */
59+movq (JB_R13*8)(%rdi), %r13 /* Load r13 from env[3] */
60+movq (JB_R14*8)(%rdi), %r14 /* Load r14 from env[4] */
61+movq (JB_R15*8)(%rdi), %r15 /* Load r15 from env[5] */
62+ /* Set return value */ /* The esi is param1 val, the eax is return value */
63+test %esi, %esi /* if (!val) { */
64+mov $01, %eax /* val=1; */
65+cmove %eax, %esi /* } */
66+mov %esi, %eax /* return val; */
67+movq (JB_PC*8)(%rdi), %rdx /* Load rdx(PC) from env[7] */
68+movq (JB_RSP*8)(%rdi), %rsp /* Load rsp from env[6] */
69+ /* Jump to saved PC */
70+jmpq *%rdx /* Jump to rdx(PC) */
71+72+ /****************************************************************/
73+74+#endif
75+76+#endif