For a simple driver module, the following is the classic composition of the Makefile:
//------------ Makefile---------------------- obj-m := hello.o KERNELDIR := /lib/modules/$(shell uname -r)/build PWD : = $(shell pwd) modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules #note the front must be tab modules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install #Note that you must analyze each statement one by one for tab
:
obj-m := hello.o
$:ls /lib/modules/
< Br>2.6.38.2 2.6.38-8-generic
ps
In the ldd3 P29 page, when you talk about Makefile, there is an example like this:
//Makefile is very simple obj-m := hello.o
But when compiling the module, use the following command:
$:make -C ~/kernel-2.6 M='pwd' modules
where" ~/kernel-2.6" is the kernel source tree directory, depending on where you place it, so the command for the native environment is:
$:make -C /usr/src/linux-source-2.6. 38 M=$PWD modules
The final result is exactly the same as the first method!
Now, let's compare these two methods to know, in fact, the only difference between them is that the source directory is not the same, respectively, "/lib/modules/$(shell uname -r)/build" And "/usr/src/linux-source-2.6.38/", but if you compile the kernel, you will know that the source code in the usr directory is generally decompressed after we download it, and the lib directory is Automatically copy the past at compile time, the file structure of the two is exactly the same, so it is not surprising that the make effect is exactly the same.
Hello, kernel module example
When learning C language, we will learn hello, world program in the first chapter, I believe everyone thinks it is so simple that I repeat the following program, everyone I think it's superfluous:
C++ code#include <stdio.h>
Please ask the students to think about two questions: Why do we have to write a main() function? Does the kernel C program need main?
here #include <stdio.h> is for us to use printf(), in fact they are all functions of the C language library, can they be used in the kernel program?
We first answer these two questions. The C language application must have a main() function because it is the entry point of the application. As for why it is such an entry, we only have one answer: , mandatory. C applications have application specifications, as kernel modules have kernel modules, so when we write the kernel module framework, remember that this is the rule.
As for the second question, it is more important: the application can call the function of the C standard library, and the kernel program is absolutely impossible. If you remember that we said fopen, it is dependent on the open system call. And if the system call is exported by the kernel, then if we can use the standard function library in the kernel program, then we will go to the "circle of the chicken" or the egg chicken.
The following program is the standard framework for the Linux kernel module (please see how the teacher wrote this code when I first learned).
C++ code#include <linux/module.h>
This is a hello Kernel module framework, if we want to print out hello, kernel, we only need to modify hello_init to: static int __init hello_init(void){ printk(“hello,kernel/n”); return 0;} module framework Contains the following four parts:
(1) The module_init(function) that the module needs to execute when it is loaded, and the module_init The function specified in (), module_exit(function) executed when the module is unloaded, and the function defined in module_exit(). If module_exit() is declared, the module will not have dynamic unloading.
(2) Need to define the initialization function called by module_init() and the cleanup function used in module_exit(). Only when the initialization function returns a non-negative value (because in the kernel, a negative value indicates that the operation failed), the kernel module can be loaded correctly, otherwise the module fails to load. The cleanup function returns a void type. In general, the initialization function is used to apply for resources when the module is loaded, and the cleanup function is used to release resources when the module is unloaded, which is similar to the constructor and deconstructor in C++.
(3) Header files, for kernel modules, you must use <linux/module.h> and <linux/init.h>. It's important to note that <> is used to include header files, but it's obvious that neither of these header files will be the header of a standard function, because, as mentioned earlier, kernel modules cannot reference standard libraries. The function. The header file here actually comes from the $(KERNELSRC)/include directory in the Linux kernel source path.
(4) Related content represented by MODULE_XXX, these are descriptions of the current kernel module, although not required, but in general, please fill in a few items, especially the license problem of the module. . Of course, it also gives you the opportunity to make a name for yourself, and you should also be responsible. Your more detailed description of the module will help you debug errors later. Modinfo allows you to identify modules faster. If necessary, please refer to the LDD (<<<Linux Device Drivers>>, hereinafter referred to as LDD) macro description for more MODULE_XXX.
1. Log in to Linux with telnet: Need to install telnet related packages for Linux Configure /etc/Xin
This tutorial gives examples of how to provide messages/dialogs in Bash Shell scripts using tools l
In the various Linux distributions now, the enterprise version of red hat is relatively safe. For t
System Requirements: Unlimited release, requires PC platform (x86 architecture CPU), Glibc version
KVM Basic Principles and Architecture II - Basic Principles
Debian installation chrome scim
KVM Fundamentals and Architecture - Concepts and Terminology
Linux system information directory
Linux Server Network Development Model
Step by step to teach the Linux system to maintain the four great tricks
20 years of glory, forensic growth of SUSE and Linux
Centos6.4 Configuring LVS + Piranha High Availability Load Balancing
Linux operating system network card installation guide (on)
Linux memory view command (free -m) using tutorial
The difference between Linux synchronous/asynchronous and blocking/non-blocking
Group Policy Set Win8 Account Login Error Limit and Lock Time
Win10 switch screen keyboard and touch keyboard method
How to remove win10 search web and windows? Win10 search web and windows to remove the tutorial
How to modify the name of the C drive Win8 Win8 modify the name of the C drive tutorial
How to troubleshoot missing SYSVOL and NETLOG on a Windows Server 2003 domain controller
Win8 search super button can not find all the reasons and solutions
How to use the virtual desktop function in the official version of Win10 th2?
How to tell if the hard disk 4K is aligned?