C语言 来自 Linux 内核模块的停止指令不起作用



我写了一个简单的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);

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

我错过了什么吗?

HLT

不会停止您的计算机,只会使该核心休眠(在 C1 中空闲(,直到下一次中断。

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

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

最新更新