For Linux device drivers, we can use only two languages: Assembler and C. Assembler implements the main parts of the Linux kernel, while C implements the architecture-dependent parts. Uploaded kernel modules are often referred to as kernel modules or modules, but those are misleading names because there are so many types of modules in the world, and the various pieces built into the base kernel can easily be called modules. We use the term kernel module or LKM for certain types of modules that HOWTO refers to. Some people think of LKM as outside the kernel. They talk about LKMs that connect to the kernel. This is a mistake; LKM (if loaded) is a large part of the kernel. The correct term for the part of the kernel tied to the image you are launching, i.e. the whole kernel except LKMs, is "base kernel." LKM connects to the base kernel. Let's start writing a Loadable Kernel Module for Linux with the evergreen start program - The " Hello -World ".
Sample File: Learn. c
The my_init function is the driver initialization entry point and is called during system startup (if the driver is statically compiled into the kernel) or when the module is inserted into the kernel. The my_exit function is the driver exit point. It’s called when unloading a module from the Linux kernel. This function has no effect if the driver is statically compiled into the kernel. These functions are declared in the linux/module.h header file.
In modern kernel versions, the Makefile takes care of most of the building for developers. It starts the kernel build system and provides a list of components required to build the module. We are Giving the build path of the kernel version and it generates .ko and other files. This Kernel Object (KO) file generated will not be loaded into the Kernel.
ubuntu@ubuntu:~/Desktop/LKM$ sudo insmod learn.ko
ubuntu@ubuntu:~/Desktop/LKM$ dmesg | tail -10
[ 1179.315828] audit: type=1400 audit(1645410095.127:35): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="snap.snap-store.hook.configure" pid=4520 comm="apparmor_parser"
[ 1181.044041] audit: type=1400 audit(1645410096.856:36): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="snap.snap-store.snap-store" pid=4521 comm="apparmor_parser"
[ 1182.803700] audit: type=1400 audit(1645410098.616:37): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="snap.snap-store.ubuntu-software" pid=4522 comm="apparmor_parser"
[ 1184.677654] audit: type=1400 audit(1645410100.492:38): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="snap.snap-store.ubuntu-software-local-file" pid=4526 comm="apparmor_parser"
[ 1282.734538] rfkill: input handler disabled
[ 5315.486562] learn: module verification failed: signature and/or required key missing - tainting kernel
[ 5315.486700] Loading module....
[ 5315.486702] Hello World....
ubuntu@ubuntu:~/Desktop/LKM$ sudo rmmod learn
ubuntu@ubuntu:~/Desktop/LKM$ dmesg | tail -10
[ 1181.044041] audit: type=1400 audit(1645410096.856:36): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="snap.snap-store.snap-store" pid=4521 comm="apparmor_parser"
[ 1182.803700] audit: type=1400 audit(1645410098.616:37): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="snap.snap-store.ubuntu-software" pid=4522 comm="apparmor_parser"
[ 1184.677654] audit: type=1400 audit(1645410100.492:38): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="snap.snap-store.ubuntu-software-local-file" pid=4526 comm="apparmor_parser"
[ 1282.734538] rfkill: input handler disabled [ 5315.486562] learn: module verification failed: signature and/or required key missing - tainting kernel
[ 5315.486700] Loading module....
[ 5315.486702] Hello World....
[ 5340.195459] Bye World....