What actually happens when I run "cli; hlt" on my Linux system?

16

5

So I recently figured out that there's a HLT opcode for halting the CPU. Cool, let's see what happens!

user@box:~$ cat > test.c
int main(void)
{
    __asm__("HLT");
    return 0;
}
user@box:~$ gcc -o test test.c
user@box:~$ ./test
Segmentation fault (core dumped)
user@box:~$

Duh! How boring.

Turns out HLT is a privileged instruction, so let's try something else.

user@box:~$ mkdir test; cd test
user@box:~/test$ cat > test.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

int init_module(void)
{
    __asm__("hlt");
    return 0;
}

void cleanup_module(void)
{
}
user@box:~/test$ echo obj-m += test.o > Makefile
user@box:~/test$ make -C /lib/modules/$(uname -r)/build modules M=$(pwd)
[...]
user@box:~/test$ sudo insmod test.ko
user@box:~/test$

Nothing happens! Boring!

As it turns out, HLT halts the CPU... until the next interrupt. Cool, so let's try disabling interrupts. CLI sounds like it'll do what we want.

user@box:~/test$ sudo rmmod test
user@box:~/test$ sed -i 's/hlt/cli; hlt/' test.c
user@box:~/test$ make -C /lib/modules/$(uname -r)/build modules M=$(pwd)
[...]
user@box:~/test$ sudo insmod test.ko

...and at this point, the OS stopped responding to my input. I couldn't move the cursor, or type anything using my keyboard. Pretty much frozen.

Except it wasn't. The clock in my GUI's panel kept on running. Hell, even the music kept on playing. It was as if only my mouse and my keyboard had stopped working. I realized that my (USB) keyboard didn't have power any more, not even my caps lock LED would work.

So, what happened here? Why does a pair of instructions that I feel like should "hang up" the system only shut down my USB devices? Why does everything else keep on running? As a bonus: What do I have to do to actually make the system freeze?

secretpow

Posted 2016-02-26T01:13:59.990

Reputation: 171

3What kind of system is this? CLI only applies to the CPU it's running on, so if you have multiple CPUs you would have to run it on each one. Anything not relying on the CLI+HLT CPU would be free to continue on its merry way – Eric Renouf – 2016-02-26T19:13:59.153

2I tested it on my normal desktop machine running on one CPU with multiple cores. I know that each core is also one "logical" CPU; is that what you're referring to? – secretpow – 2016-02-26T20:15:21.780

Answers

2

Halting the CPU does not completely halt the processor. It's usually executed by the operating system when there is no more work to be done. The CPU then enters an IDLE state from which it can wake-up anytime for exmample by an interrupt, but also by ACPI - so you might want to try to stop that as well: In your BIOS or as a boot argument:

acpi=off

The reason for USB devices to not work anymore was due to the disabled interrupts although according to this discussion USB is not interrupt driven by design.

For reference: https://en.wikipedia.org/wiki/X86_instruction_listings

Phlogi

Posted 2016-02-26T01:13:59.990

Reputation: 173