Linux kernel device driver Linux kernel module loading mechanism notes collation

  • 2020-12-22 17:50:51
  • OfStack

#include <linux/moduleparam.h>

1. Module parameters

Define variables in the driver


static int num = 0; // When the load module is not specified num Is the value of 0
module_param(variable name, type, permission); Types: byte, int, uint, short, ushort, long, ulong, bool, charp, permissions cannot have write permissions insmod test.ko variable name 1= value 1 variable name 2= value 2

The call relationship of module_param is as follows:


#define module_param(name, type, perm) \
module_param_named(name, name, type, perm)
#define module_param_named(name, value, type, perm)  \
param_check_##type(name, &(value));  \
module_param_call(name, param_set_##type, param_get_##type, &value, perm); \
__MODULE_PARM_TYPE(name, #type)
#define module_param_call(name, set, get, arg, perm)   \
__module_param_call(MODULE_PARAM_PREFIX,   \
  name, set, get, arg,   \
  __same_type(*(arg), bool), perm)
#define __module_param_call(prefix, name, set, get, arg, isbool, perm) \
static int __param_perm_check_##name __attribute__((unused)) = \
BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \
+ BUILD_BUG_ON_ZERO(sizeof(""prefix) > MAX_PARAM_PREFIX_LEN); \
static const char __param_str_##name[] = prefix #name; \
static struct kernel_param __moduleparam_const __param_##name \
__used \
  __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
= { __param_str_##name, perm, isbool ? KPARAM_ISBOOL : 0, \
  set, get, { arg } }

Multiple c files are compiled into a module, which can be realized by using the instruction in xxx-objs, as follows:


test-objs := a.o b.o // by a.c, b.c  woven test.ko,  Be careful not to have .o Documents and Objectives ko A file with the same name 
obj-m += test.o

You can view the information of the module in the system under /sys/module/ module name /

1. View the elf file for information

readelf test.ko -a

ko file composition

1. elf header 2. text data ... 3. sections table 4. symbol table

EXPORT_SYMBOL(function name/variable address) // exports the address of the function/or variable to the kernel symbol table


EXPORT_SYMBOL_GPL( The function name )
///////////

/proc/kallsyms view the symbol table for the current system

conclusion


Related articles: