Detailed analysis of kernel module development under Linux operating system

  
 

1. modprobe load kernel module

a) Find alias in /etc/module.conf

b) Determine if there is a dependency in lib/modules/version/module.dep The module needs to be loaded ahead of time (the file is created by depmod-a)

2. The 2.4 kernel does not have to use init_module and cleanup_module to do the start and end functions, but need to use module_init and

Module_exit declaration.

3. Macros __init and __exit can cause functions to automatically reclaim memory after running (limited to modules), __initdata for variables,

Example:

# Include //need to include the header file

static int ntest __initdata = 3;

static int __init test_init(void) {...}

static void __exit test_exit( Void) {...}

module_init(test_init); //Declare after the implementation function

module_exit(test_exit);

4. Some module descriptions are relevant Macro, you can use objdump to view related information.

MODULE_LICENSE() Description Code License Type

MODULE_DESCRIPTION() Module Description

MODULE_AUTHOR() Author

MODULE_SUPPORTED_DEVICE() Module Supported Devices

5. Parameter passing uses the macro MODULE_PARM (variable name, type).

Supported types are "b"bit"h"short"i"integer"l"long "s"string

static int myint = 9;

static char *mystr = "test"; //Note that you need to pass the default value of the parameter

MODULE_PARM(myint, "i");

MODULE_PARM(mystr , "s");

6. Compiling multiple files into one kernel module:

a) Need to add

#define __NO_VERSION__

#include

b) Compile with a normal single kernel file

c) Connection: ld -m elf_i386 -r -o <1st src file.o> <2nd Src file.o>

7. strace Check the system call used by a program

8. About the file_operations structure is defined in the linux/fs.h file.

Usage:

struct file_operations fops = {read: device_read,write: device_write,open: device_open,release: device_release}C99 usage: struct file_operations fops = {.read = device_read ,.write = device_write,.open = device_open,.release = device_release}

Copyright © Windows knowledge All Rights Reserved