Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Halt instruction from Linux kernel module is not working

I wrote a simple Linux kernel module to issue hlt instruction

#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);

Loading this module on my Virtual Machine, I don't see my VM is halted.

Am I missing something?

like image 971
md.jamal Avatar asked Oct 20 '25 12:10

md.jamal


1 Answers

HLT doesn't stop your machine, only make that core sleep (in C1 idle) until the next interrupt.

You can try adding cli instruction before hlt, so only an NMI can wake that CPU up and make the function return.

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