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?