Hello World for linux Kernel Module

This commit is contained in:
root
2025-02-16 12:30:21 +02:00
commit 4e4b515f78
17 changed files with 1969 additions and 0 deletions

21
01_hello/Makefile Normal file
View File

@@ -0,0 +1,21 @@
#This variable is used to specify all the object file
#which will be loaded and compile as kernel module for
#usage. And the compilation for obj file from .c file
#is done automatically.
obj-m += hello.o
# M variable used here tells the "make" to look in
# current path to build bootable kernel module from
# .c files in specified current path.
# modules: It is the target that is used to build the kernel
# modules.
# clean: It is used to clean the build targets, compile and
# generated files which would be created.
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

58
01_hello/hello.c Normal file
View File

@@ -0,0 +1,58 @@
#include <linux/module.h>
#include <linux/init.h>
/*
* insmod: It's the command to insert the
* loadable kernel module to the
* kernel.
*
* rmmod: It's the command to remove the
* loadable kernel module from the
* kernel.
*/
/*
* There are two imp. events for linux driver
* The loading of driver on kernel and unloading
* of driver from kernel.
*/
/*
* Function to use when this is
* module is loaded to the kernel.
*/
int
my_init(void){
/*
* We don't use printf for linux driver
* because printf writes to standard IO
* and there is no standard IO for kernel
* There is only kernel's log. Kernel's log
* is buffer which stores whatever is written
* onto that buffer. It can me printed using
* command: dmesg
*/
printk("hello - Hello, Kernel!\n");
return 0;
}
/*
* Function to execute when module
* is unloaded from the kernel
*/
void
my_exit(void){
printk("hello - Goodbye, kernel!\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");