x86的hlt指令不可用
我在虚拟机里面编译一个c文件(基于x86 gcc),里面有一条据说是x86的停机指令hlt,如下:
- int main(...)
- {
- ......
- __asm volatile
- (
- "hlt"
- );
- return 0;
- }
复制代码代码编译正常,但运行时提示"Segmentation fault",莫非这是一条特权指令?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Since issuing the HLT instruction requires ring 0 access, it can only be run by privileged system software, such as the kernel.
这些涉及到硬件特权的指令,当然不可能如此随便
搜了一下为什么编译出来的可执行文件有hlt,却要在_exit里退出了
http://stackoverflow.com/questions/5213466/why-does-gcc-place-a-halt-instruction-in-programs-after-the-call-to-main
查下手册不就知道了
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.
其实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.