x86的hlt指令不可用

发布于 2022-10-15 05:45:40 字数 545 浏览 30 评论 0

我在虚拟机里面编译一个c文件(基于x86 gcc),里面有一条据说是x86的停机指令hlt,如下:

  1. int main(...)
  2. {
  3.     ......
  4.     __asm volatile
  5.     (
  6.         "hlt"
  7.     );
  8.     return 0;
  9. }

复制代码代码编译正常,但运行时提示"Segmentation fault",莫非这是一条特权指令?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怀里藏娇 2022-10-22 05:45:40

Since issuing the HLT instruction requires ring 0 access, it can only be run by privileged system software, such as the kernel.

谜兔 2022-10-22 05:45:40

这些涉及到硬件特权的指令,当然不可能如此随便

瑾兮 2022-10-22 05:45:40

搜了一下为什么编译出来的可执行文件有hlt,却要在_exit里退出了

http://stackoverflow.com/questions/5213466/why-does-gcc-place-a-halt-instruction-in-programs-after-the-call-to-main

After main returns, exit will be called. The hlt is there in case the system's version of exit doesn't stop execution of the process immediately. In user mode, it will cause a protection fault, which will kill the process. If the process is for some reason running in ring 0, it will just stop the processor until the next interrupt, which will hopefully trigger the OS to remove the process. In processes designed to run in ring 0, there is often a jmp instruction after the hlt which will cause the hlt to be performed over and over until the process is terminated.

红ご颜醉 2022-10-22 05:45:40

查下手册不就知道了
The HLT instruction is a privileged instruction. When the processor is running in
protected or virtual-8086 mode, the privilege level of a program or procedure must
be 0 to execute the HLT instruction.

若能看破又如何 2022-10-22 05:45:40

其实HLT的行为就代表不能在用户态执行
An enabled interrupt (including NMI and SMI), a debug exception, the BINIT# signal, the INIT#
signal, or the RESET# signal will resume execution. If an interrupt (including NMI) is
used to resume execution after a HLT instruction, the saved instruction pointer
(CS:EIP) points to the instruction following the HLT instruction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文