Hello world example for lkm (loadable kernel module)
$ make
$ sudo insmod hello-lkm.ko
$ sudo rmmod hello-lkm$ gcc -o main_user main_user.c
$ ./main_userIf you get Segmentation fault (core dumped) after running the main_user, it's probably because the rmdpc is not allowed. It might due to the protection principles done by kernal 4.x, please see more information in the github issue.
For simply remove this protection, do this:
$ sudo sh -c "echo '2' > /sys/bus/event_source/devices/cpu/rdpmc"If you get
insmod: ERROR: could not insert module simple-lkm.ko: Operation not permitedwhen you try sudo insmod simple-lkm.ko after you build the module, it is probably because your BIOS setting.
In my case, when I use dmesg | tail -5 to see what actually happened, I get:
Lockdown: insmod: Loading of unsigned module is restricted; see man kernel_lockdoen.7Then I check the configuration of my BIOS setting, I found my PC's Secure Boot is enabled, so I turned it off.
After I restart the PC, I installed the module successfully.