Getting Started with Linux Kernel Modules HelloWold

Introduction

This article mainly introduces how to write the simplest Linux kernel module "Hello World".

hello.c

Go directly to the code, the first is the source code file hello.c of the module :

/**
 * hello.c
 * Simple hello world driver module with module_init, module_exit
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE ("GPL");

static int __init hello_2_init (void)
{
    
    
    printk (KERN_INFO "Hello world\n");
    return 0;
}

static int __exit hello_2_exit (void)
{
    
    
    printk (KERN_INFO "Goodbye world\n");
    return 0;
}

module_init (hello_2_init);
module_exit (hello_2_exit);

The content of this hello.c is relatively simple: when the module is loaded, output "Hello world" in the kernel log, and when the module is unloaded, output "Goodbye world" in the kernel log.

Makefile

Next is the make description file Makefile of the module :

ifeq ($(KERNELRELEASE),)

#KERNELDIR ?= /home/kernel2.6/linux-2.6.14

KERNELDIR ?= /lib/modules/$(shell uname -r)/build M=$(PWD) modules
PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
	obj-m := hello.o
endif

The first ifeq ($(KERNELRELEASE),) is currently useless. Its origin means that when the Makefile in the root directory of the Linux source code compiles the kernel, the KERNELRELEASE macro will be defined, so if the make starting from the root directory of the source code will Compile the hello.o module into the kernel.

KERNELDIR ?= /lib/modules/ ( s h e l l u n a m e − r ) / b u i l d M = (shell uname -r)/build M= (shellunamer)/buildM= (PWD) modulesis toKERNELDIR, this variable is used later to refer to the kernel source code directory.

PWD := $(shell pwd) is to assign a value to the PWD variable. The function is to assign the return result of $(shell pwd), which is the path of the current directory, to PWD . This variable refers to the location of the driver to be compiled. Location.

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

The above sentence is the rule of Makefile: $(MAKE) here is equivalent to make , and the function of the " -C " option is to transfer the current working directory to the specified location. The function of the " M= " option is that when the user needs to compile an external module based on a certain kernel, he needs to add " M=dir " to the make modules command, and the program will automatically search for the module source code in the specified dir , and the It compiles and generates KO files.

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

The above command is the installation of the module

.PHONY: modules modules_install clean

The function of the above sentence is to ensure that the three commands modules , modules_install , and clean can be completed normally. .PHONY This is a special target name, there are others, such as: .SUFFIXES, .DEFAULT, .PRECIOUS, .INTERMEDIATE, .SECONDARY, .SECONDEXPANSION, .DELETE_ON_ERROR, .IGNORE .LOW_RESOLUTION_TIME .SILENT .EXPORT_ALL_VARIABLES .NOTPARALLEL .

For their specific usage, please refer to the Special Built-in Target Names chapter in the GNU manual.

The specific meaning of the .PHONY target is that iffilesnamed modules , modules_install , clean , etc. in the working directory of the Makefile , the command will make an error. It's the way to prevent this from going wrong.

Module compilation, loading and deletion

(1) Copy the above two files to the specified folder in the Linux environment, such as /home/linux/test/hello-world.
(2) Switch root user:

# sudo su

(3) Enter our module source code folder

# cd /home/linux/test/hello-world

(4) Compile the module

# make

(5) Add the module to the kernel through the insmod command:

# insmod hello.ko

(6) Check the kernel module through lsmod:

# lsmod | grep hello

(7) Delete the modules in the kernel by rmmod:

# rmmod hello.ko

(8) If you want to view the kernel log we printed, you can use the dmesg command:

# dmesg

Guess you like

Origin blog.csdn.net/hanshiying007/article/details/129284279