When you start writing device driver you will definitely look into this book. I will share some of Kernel functions, Macros, Variables and /proc files needed for Kernel Module building and running.

insmod

User space utility that, inserts module into active Kernel.

modprobe

User space utility same as insmod but it extends its support by insmoding the dependent modules too, by referring at /lib/modules/…. directories.

rmmod

User space utility, to remove module from active Kernel without need of reboot.

#include <linux/init.h>

module_init(init_function);

module_exit(exit_function);

Macros for initialisation and cleanup functions along with respective header file.

__init / __exit

Markers for functions only used at module initialisation / cleanup time.

__initdata / __exitdata

Markers for datas

Items marked are only considered while initialisation / cleanup time, and discarded afterwards respectively. These may work due to placing relevant objects at special ELF section in the executable files.

#include <linux/sched.h>

Header file, contains most important kernel API, functions and variables.

Usage:

struct task_struct *current;

Denotes current process.

current->pid

current->comm

The process ID and command name of the current process.

obj-m

Makefile symbol, used by kernel build system to determine which module to be built inside the current directory.

i) /sys/module

ii) /proc/module

i) sysfs hierarchy which contains informations about currently loaded modules, contains module name, amount of memory occupied, and usage count. Extra strings appended at end of each line denotes flags currently available for the module.

ii) Older single file version of that, (my laptop doesn’t contain this file)

vermagic.o

It is an object file lies inside kernel source directory, describe the environment a module built for.

#include <linux/module.h>

Necessary for all kernel module source code.

#include <linux/version.h>

Holds information of kernel version being built.

EXPORT_SYMBOL(symbol);

EXPORT_SYMBOL_GPL(symbol);

Exports symbol to kernel, first uses with licensing issues, second, restrict itself for only GPL licensed modules.

MODULE_AUTHOR(“S MOHAMMED AUDHIL”);

MODULE_LICENSE(“GPL”);

MODULE_VERSION(version_string); 

MODULE_DEVICE_TABLE(table_info); 

MODULE_ALIAS(alternate_name);

Description to reader + place documentation on module in object file.

#include <linux/moduleparam.h>

module_param(variable,type,perm);

Macro for module parameter, along with respective header file. It can be modified by user during insmoding. Type can be bool, charp, int, invbool, long, short, ushort, uint, ulong, or intarray.

#include <linux/kernel.h>

int printk(const char * fmt, …);

Same as normal printf.

Enjoy…!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s