c - 来自 Linux 内核模块的暂停指令不起作用

标签 c linux assembly linux-kernel x86

我写了一个简单的 Linux 内核模块来发出 hlt 指令

#include <linux/kernel.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");
static int __init test_hello_init(void)
{
    asm("hlt");
    return 0;
}

static void __exit test_hello_exit(void)
{
}

module_init(test_hello_init);
module_exit(test_hello_exit);

在我的虚拟机上加载此模块时,我没有看到我的虚拟机已停止。

我错过了什么吗?

最佳答案

HLT 不会停止您的机器,只会让该核心休眠(在 C1 空闲状态下)直到下一个中​​断。

您可以尝试在hlt 之前添加cli 指令,这样只有NMI 才能唤醒CPU 并使函数返回。

static int __init test_hello_init(void) {
    asm("cli");
    asm("hlt");
    return 0;
}

关于c - 来自 Linux 内核模块的暂停指令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59561259/

相关文章:

linux - 使用 x64 linux 系统调用(汇编)从键盘读取输入

c - netinet/tcp.h 中的有效负载偏移值?

java - 临时文件夹被创建为临时文件

linux - Linux 中的 $PATH 是什么以及如何修改它

linux - 选择哪个 yocto 发布标签

c++ - 机器码定义

c - C 和 Mathematica 中的并行计算

c++ - 在 Win32 (c++) 的另一个进程中写入文本框

linux - 我如何在 ls 命令后搜索特定字符串

c - 使用 C/Intel 程序集寻求最大位图(又名位数组)性能