From: http://hi.baidu.com/zkheartboy/blog/item/6331d5cec964f60093457e0d.html
A script for programming a Linux kernel module, which has not been written and is constantly updated...
1. Related commands
0. view the kernel modules mounted by the system.
Lsmod modulename
1. Load the kernel module
Insmod modulename
2. Uninstall the kernel module
Rmmod modulename
3. Create a device file
Mknod filename dev_type major_number minor_number
Description: The name of the device file created by the filename parameter.
Dev_type: the type of the device file,
C character device (character device)
B block Device)
The main difference between the two file types is that block device has Buffer
Major_number the primary number is used to mark which driver can use this device (each driver has a unique ID)
Minor_number Image number the driver to distinguish between the various hardware it controls (inaccurate translation)
---------------
Example:
Ls-L/dev/hda [1-3]
BRW-RW -- 1 root disk 3, 1 Jul 5 2000/dev/hda1
BRW-RW -- 1 root disk 3, 2 Jul 5 2000/dev/hda2
BRW-RW -- 1 root disk 3, 3 Jul 5 2000/dev/hda3
B Indicates the device type.
3 indicates major number
1, 2, 3 indicates minor number
----------------
4. view the Program Execution Process (generally used to check which systems are called by the Program)
Strace./APP
5. automatically load the module (after debugging and reuse, or the system is easy to mount)
Depmod-A (check whether the kernel dependency is correct) to generate the/lib/modules/version/modules. Dep File
Modprobe is mounted before insmod (the kernel module also has dependency ~~) You can edit/etc/modules. conf to enable the system to load important information, such as the driver.
Modprobe-A msdos (loaded to all modules with dependency in msdos)
Ii. Related Documents
1/proc/modules.
Stores information about the loaded kernel. Lsmod is the Read File.
2/etc/modules. conf
Let the system load important information at startup, such as the driver
3/lib/modules/version/modules. Dep
Kernel dependency (guessed, not viewed)
4/var/log/message
I used the turbo 10 desktop version for testing, so the printk () function output cannot be displayed on my KDE terminal.
This problem has been depressing for 20 minutes. Later, I checked the message file and found that all printk output information was recorded here. I am also stupid :(
You can write your own printx function after you know how to create and use the "character Device File. I also know why it cannot be displayed on the kede terminal :)
5/proc/devices
The data structure in the used device file is major number device name.
3 user space vs kernel space | System Call (interesting)
The kernel can access all resources. The same is true for user programs. However, they must be accessed through drivers.
The kernel needs to manage all resources. It is a bridge between user programs and hardware. When a user runs the program, he transfers data to the kernel space through system calls, then the kernel processes the request, and then returns the result to the user space. The kernel can capture all this information, and you can replace it with the original system call.
Open files cannot be opened. You can make mkdir unable to create directories !!
Three damn makefiles !!!
Almost all the articles I read at the beginning are based on version 2.4, as shown in figure 2. In version 4, makefile is written, but my kernel is 2.6, so I tried it for a while. After compilation, I couldn't load it. After a long time, I almost failed to replace the kernel. Later I found 2. 6.
2.6 kernel module makefile is compiled into a. Ko File
--------------------
Ifneq ($ (kernelrelease ),)
OBJ-M: = test. O # Module name
Else
Kdir: =/lib/modules/$ (shell uname-R)/build # kernel path
PWD: = $ (shell PWD)
Default:
$ (Make)-C $ (kdir) subdirs = $ (PWD) Modules
Endif
--------------------
2.4 kernel module makefile is compiled into a. o file
--------------------
Arget = test
Warn =-w-wall-wstrict-prototypes-wmissing-prototypes
Include =-isystem/lib/modules/2.6.0-1/build/include # kernel path
Cflag =-O2-dmodule-d1_kernel _ $ {warn }$ {include}
Cc = gcc
$ {Target}. O :$ {target}. c
$ {CC }$ {cflag}-C $ {target}. c
. Phony: clean
Clean:
Rm-RF $ {target}. o
--------------------
Program structure of four kernel modules
-----------------
/*
* Hello-1.c-simple hello the World Program from the manual
*/
# Include <Linux/module. h>/* required for all kernel modules */
# Include <Linux/kernel. h>/* needed for kern_alert */
// Initialize the program and execute it when loading the module
Int init_module (void)
{
Printk ("<1> Hello World 1.n"); // output information of this function, which is equivalent to the user space printf
/*
* A non-0 value is returned, indicating that the kernel cannot be loaded due to an error.
*/
Return 0;
}
// Clear the function, which is executed when the module is detached
Void cleanup_module (void)
{
Printk (kern_alert "Goodbye World 1.n ");
}
------------------
Version 2.6 has more writing methods, such as adding loading parameters and kernel instructions. For more information, see the relevant documentation.
Http://www.tldp.org/LDP/lkmpg/2.6/html/index.html
Five-character Device File
5.1 Data Structure
1 file_operations defined in <Linux/fs. h>
Note: This struct contains function pointers for device operations. You can use its configuration and operation functions to perform device operations.
Set the entry you do not use to null.
------------------------------
Struct file_operations {
Struct module * owner;
Loff_t (* llseek) (struct file *, loff_t, INT );
Ssize_t (* read) (struct file *, char _ User *, size_t, loff_t *);
Ssize_t (* aio_read) (struct kiocb *, char _ User *, size_t, loff_t );
Ssize_t (* write) (struct file *, const char _ User *, size_t, loff_t *);
Ssize_t (* aio_write) (struct kiocb *, const char _ User *, size_t, loff_t );
INT (* readdir) (struct file *, void *, filldir_t );
Unsigned int (* poll) (struct file *, struct poll_table_struct *);
INT (* IOCTL) (struct inode *, struct file *, unsigned int, unsigned long );
INT (* MMAP) (struct file *, struct vm_area_struct *);
INT (* open) (struct inode *, struct file *);
INT (* flush) (struct file *);
INT (* release) (struct inode *, struct file *);
INT (* fsync) (struct file *, struct dentry *, int datasync );
INT (* aio_fsync) (struct kiocb *, int datasync );
INT (* fasync) (INT, struct file *, INT );
INT (* Lock) (struct file *, Int, struct file_lock *);
Ssize_t (* readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
Ssize_t (* writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
Ssize_t (* sendfile) (struct file *, loff_t *, size_t, read_actor_t, void _ User *);
Ssize_t (* sendpage) (struct file *, struct page *, Int, size_t, loff_t *, INT );
Unsigned long (* get_unmapped_area) (struct file *, unsigned long, unsigned long,
Unsigned long, unsigned long );
};
------------------------------
This guy is too hard to remember. I once saw this three times and I went to the Forum to fill the water. Today, he was an idiot after reading him!
In the new driver, you may see a simpler and more friendly way to assign values to file_operations. refer to the following structure. The manual says this is
The GCC extension (gnu c) means that, anyway, I understand that when GCC compiles it, it first translates the following things into standard C, then compile. Sorry, poor C standard! However, it is useless except for Linux.
--------------
Struct file_operations fops = {
Read: device_read, // corresponding function
Write: device_write,
Open: device_open,
Release: device_release
};
--------------
In the manual, the following is the c99 feature! (Isn't BSD enough? Why is it so messy !) Note: For lower GCC versions
May not compile properly! Because it exceeds the gnu c standard
Http://www-900.ibm.com/developerWorks/cn/linux/l-c99/index.shtml c99
Standard Reference)
--------------
Struct file_operations fops = {
. Read = device_read, // corresponding function
. Write = device_write,
. Open = device_open,
. Release = device_release
};
--------------
I chose the first method!
The 2 * file * structure is defined in <Linux/fs. h>.
Note:
* It is different from file (defined in glibc)
* It is a kernel-level struct.
* It indicates the abstract 'file'
* It is not the file on the disk.
* The driver assigns a value to this object using a struct *******
* The pointer type pointing to it is filp. You can also directly write it as a struct file.
5.2 related functions
1. register the device function
<Linux/fs. h>
Int register_chrdev (unsigned int major, const char * Name, struct file_operations * FoPs );
Remember the command used to create a device file,
Parameter description: Major: it is the only driver corresponding to major number. Remember!
Name: filename can be found in/proc/devices (better device name)
FoPs: file_operations struct, which can be understood as the kernel entry of the operating device. You can fill it with the two methods mentioned above
Function return: 1 major is 0: dynamically assigned major number value
2 major:
3 major is a special value:
Note: because a driver can only have one major value, there are three ways to create a driver:
1. Create a device file first. You can assign a fixed major value.
2. dynamically allocate the major value, read the/proc/devices file, find the major value, and create it using a script (create a device file in the user space)
3. dynamically allocate the major value and use the system call function to directly create a device file (executed in the kernel space)
1st methods are not desirable, because you cannot predict whether to use this major number in the future
2nd methods, which must be created in the user space
The best way to do this is to dynamically create and destroy device files.
2. Cancel device registration function
About: Counter