简体   繁体   English

上下文保存–​​如何读取C中的段寄存器和指令指针?

[英]Context saving – how to read segment registers in C and instruction pointer?

Hell'o. 你好。 I want to create a program which saves register set – process context. 我想创建一个程序来保存寄存器集-进程上下文。 When I try to read CS register, my compiler and IDE tell me, that register "CS" doesn't exist. 当我尝试读取CS寄存器时,我的编译器和IDE告诉我,该寄存器“ CS”不存在。

    register int *cs asm ("cs");

Am I doing it right? 我做对了吗? Saving process context means saving every register – especially Code Segment (and other segments as well) – those registers are required to restore process image and start executing where last execution stopped. 保存过程上下文意味着保存每个寄存器,尤其是代码段(以及其他段),这些寄存器是恢复过程映像并在上次执行停止的地方开始执行所必需的。

Saving process context means saving every register 保存过程上下文意味着保存每个寄存器

Of course you can save cs and eip . 当然,您可以保存cseip However ... 但是...

  • ... these two registers contain the location (address) of the instruction currently being executed. ...这两个寄存器包含当前正在执行的指令的位置(地址)。

    This means that these registers will always contain the address of the code that saves the context registers. 这意味着这些寄存器将始终包含保存上下文寄存器的代码的地址。

    Why do you want to store this information when these registers always contain the same value? 当这些寄存器始终包含相同的值时,为什么要存储此信息?

  • ... what do you think what happens when you restore these registers? ...您认为还原这些寄存器时会发生什么?

    Because these two registers contain the address of the instruction currently being executed, the only way to "restore" these two registers is to jump to the instruction that saved the values of the register. 因为这两个寄存器包含当前正在执行的指令的地址,所以“恢复”这两个寄存器的唯一方法是跳转到保存该寄存器值的指令。

    You'll run into an endless loop... 您将陷入无尽的循环...

    Is this really what you want to do? 这真的是您想要做的吗?

I want to create a program which saves register set – process context. 我想创建一个程序来保存寄存器集-进程上下文。

 register int *cs asm ("cs"); 

Now it's getting very scary: 现在变得非常恐怖:

If you really want to write some code that saves the CPU context (eg for manually implementing a try - catch mechanism in "low-level" C), you should write the complete function in assembler. 如果您确实想编写一些代码来保存CPU上下文(例如,在“低级” C中手动实现try - catch机制),则应在汇编器中编写完整的函数。

Don't try to write this using inline assembly within C functions! 不要试图在C函数中使用内联汇编来编写代码!

Otherwise you don't know how the "compiled" C code will interact with the assembler code; 否则,您将不知道“已编译”的C代码将如何与汇编代码进行交互。 it's highly probable that your program won't work. 您的程序很有可能无法运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM