Comments (5)
from state-threads.
from state-threads.
The asm code:
__st_md_cxt_save:
/* Save SP */
leaq 8(%rsp), %rdx /* Save *(int64_t*)(rsp+8) to rdx, https://my.oschina.net/guonaihong/blog/508907 */
movq %rdx, (JB_RSP*8)(%rdi) /* Save rdx(rsp) to env[6], *(int64_t*)(rdi+6)=rdx */
which save RSP, the stack address to rdx, then save rdx to jmpbuf[JB_RSP].
from state-threads.
The asm code:
__st_md_cxt_save:
/* Save PC we are returning to */
movq (%rsp), %rax /* Save PC(parent function address) %(rsp) to rax */
movq %rax, (JB_PC*8)(%rdi) /* Save rax(PC) to env[7], *(int64_t*)(rdi+7)=rax */
which save the content of RSP, that is the previous function address(PC), to rax, then save rax to jmpbuf[JB_PC].
from state-threads.
寄存器布局,osx和linux是一样的,参考:https://en.wikipedia.org/wiki/X86_calling_conventions
- 函数参数:RDI, RSI, RDX, RCX, R8, R9
- 返回值:RAX
由于save和restore最多只有两个参数:
extern int _st_md_cxt_save(jmp_buf env);
extern void _st_md_cxt_restore(jmp_buf env, int val);
因此,我们可以将其他的参数寄存器当作临时变量用,比如存储RSP和PC的内容:
- rdx,在save时保存RSP的值到jmpbuf。现在改成了寄存器r8。
- rax,在save时保存PC的值到jmpbuf。虽然rax是返回值,但是临时用下也可以。现在改成了寄存器r9。
__st_md_cxt_save:
/* Save SP */
leaq 8(%rsp), %r8 /* Save *(int64_t*)(rsp+8) to r8, https://github.com/ossrs/state-threads/issues/11#issuecomment-888709759 */
movq %r8, (JB_RSP*8)(%rdi) /* Save r8(rsp) to env[6], *(int64_t*)(rdi+6)=r8 */
/* Save PC we are returning to */
movq (%rsp), %r9 /* Save PC(parent function address) %(rsp) to r9 */
movq %r9, (JB_PC*8)(%rdi) /* Save r9(PC) to env[7], *(int64_t*)(rdi+7)=r9 */
- rdx,在restore时从jmpbuf恢复PC寄存器。现在改成了寄存器r8。
__st_md_cxt_restore:
/* Restore PC and RSP */
movq (JB_PC*8)(%rdi), %r8 /* Load r8(PC) from env[7] */
movq (JB_RSP*8)(%rdi), %rsp /* Load rsp from env[6] */
/* Jump to saved PC */
jmpq *%r8 /* Jump to r8(PC) */
Note: PC寄存器就是函数执行的地址。RSP是堆栈地址。
from state-threads.
Related Issues (20)
- Support MSG_ZEROCOPY for streaming server. HOT 9
- UDP接收中文乱码 HOT 1
- Guide: Notes about code of state-threads. ST代码分析. HOT 4
- Performance improvement for st_usleep.
- 支持iOS和安卓吗? HOT 4
- Support Multiple Threads for Linux and Darwin only. HOT 4
- Cygwin: ST Support Windows 64bits HOT 2
- Support MIPS for OpenWRT HOT 1
- How to porting ST to other OS/CPU? 如何移植ST到其他系统或CPU?
- Support daemon(fork twice) for Darwin/OSX HOT 1
- Support Loongson CPU arch
- aosp中编译srs-server及libst报错 HOT 2
- 如何使st库支持多线程? HOT 1
- AppleM1: Support Apple Silicon M1(aarch64). HOT 1
- Add utest for threads and codes.
- About How to Support EPOLLET Issue in ST
- Plan: Migrate to C++98/MIT. 计划迁移到C++98/MIT.
- Support backtrace and backtrace_symbols
- 如果协程释放将内存也同时释放,为什么会崩溃 HOT 2
- 增加可以将协程内存释放的功能 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from state-threads.