中止是否在0环中运行?

Yun*_*ncy 2 c c++ x86

这些天我在C中找到了一个博客提到的中止功能.

以下是中止功能的源代码:http: //cristi.indefero.net/p/uClibc-cristi/source/tree/0_9_14/libc/stdlib/abort.c

我发现它使用hlt指令(我的电脑是x86).

但似乎hlt必须在第0环中运行.(请参阅wiki http://en.wikipedia.org/wiki/HLT)

似乎中止正在用户空间中运行.因此hlt,中止使用该指令似乎是非法的.

顺便说一下,我尝试hlt在linux和windows中运行.但我遇到了一个错误.

在linux中:

#include <iostream>
using namespace std;

#define HLT_INST asm("hlt")

int main(){
  cout<<"whill run halt"<<endl;

  HLT_INST; //result in SIGSEGV error
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在Windows中:

cout<<"will run hlg"<<endl;
/*Unhandled exception at 0x0040101d in test_learn.exe: 0xC0000096: Privileged instruction.
*/
__asm{
    hlt;
}
Run Code Online (Sandbox Code Playgroud)

sla*_*ppy 6

abort功能仅hlt在发送SIGABRT失败后使用该指令.如果您阅读源代码,该函数首先尝试:

raise(SIGABRT);
Run Code Online (Sandbox Code Playgroud)

然后调用无效指令:

/* Still here?  Try to suicide with an illegal instruction */
if (been_there_done_that == 2) {
    been_there_done_that++;
    ABORT_INSTRUCTION;
}
Run Code Online (Sandbox Code Playgroud)

所以你是对的,hlt需要获得0特权.这正是使它成为无效指令的原因.执行它会调用一个无效的指令处理程序,在你的情况下(我想)SIGSEGV.