【问题标题】:iret error:general protection fault fffciret 错误:一般保护故障 fffc
【发布时间】:2012-08-02 08:33:23
【问题描述】:

我有一个在内核(2.6.30 x86_64)模式(r0)下运行的示例代码,试图模拟 iret。我在英特尔手册的指南下推送变量。但它在 iret 指令的 pos 中出现运行时错误:

一般保护故障:fffc[#] SMP

asm volatile(
    "mov %%ss,%%ax \n\t"
    "push %%rax \n\t"/*ss*/
    "push %%rsp \n\t"/*rsp*/
    "pushfq \n\t"/*rflags*/
    "mov %%cs,%%ax \n\t"
    "push %%rax \n\t"/*cs*/
    "mov $._restart_code,%%rax \n\t"
    "push %%rax \n\t"/*rip*/
    "iret \n\t"/*here is the fault rip!!!!!!*/
    "._restart_code:"
    "nop" :);

【问题讨论】:

    标签: linux assembly linux-kernel


    【解决方案1】:

    我不确定它是否能解决您的问题,但这里有一些注意事项:

    "mov %%cs,%%ax \n\t"
    "push %%rax \n\t"/*cs*/
    

    您正在更新 EAX 的低位字节,因此 RAX 的高位字节可能不同于 0(不应该)。

    "mov $._restart_code,%%rax \n\t"
    

    同样,您应该断言$._restart_code 是规范形式。

    "mov %%ss,%%ax \n\t"
    "push %%rax \n\t"/*ss*/
    "push %%rsp \n\t"/*rsp*/
    

    没用,因为你回到了相同的权限级别。

    【讨论】:

      【解决方案2】:

      bug是rsp在push之后改变了。

      asm volatile(
          "mov %%ss,%%ax \n\t"
          "push %%rax \n\t"/*ss*/
          "push %%rsp \n\t"/*rsp ##########error here!!!!!!! */ 
          "pushfq \n\t"/*rflags*/
          "mov %%cs,%%ax \n\t"
          "push %%rax \n\t"/*cs*/
          "mov $._restart_code,%%rax \n\t"
          "push %%rax \n\t"/*rip*/
          "iret \n\t"/*###iretq should be used under 64bit mode*/
          "._restart_code:"
          "nop" :);
      

      所以,在所有推送指令之前保存 rsp。正确的代码是:

      asm volatile(
              "mov %%rsp,%%rbx \n\t"
              "mov %%ss,%%ax \n\t"
              "push %%rax \n\t"/*ss*/
              "push %%rbx \n\t"/*rsp*/ 
              "pushfq \n\t"/*rflags*/
              "mov %%cs,%%ax \n\t"
              "push %%rax \n\t"/*cs*/
              "mov $._restart_code,%%rax \n\t"
              "push %%rax \n\t"/*rip*/
              "iretq \n\t"
              "._restart_code:"
              "nop" :);
      

      谢谢!!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-14
        • 2012-05-08
        • 2013-04-11
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多