Linux Device Driver programming Lecture 19- LKM entry point registration and other macros

  • Post author:
  • Post category:Blog

 

LKM entry point registration and other macros

 

 

In this article, let’s explore about the module entry points registration.

Module Entry Point Registration:

  • In kernel programming, you need to register module initialization and cleanup functions with the kernel.
  • This registration is done using macros provided by the kernel: module_init and module_exit.

Below are macros used to register your module’s init function and clean-up function with the kernel. 

/* This is registration of above entry points with kernel */
 module_init(my_kernel_module_init);
 module_exit(my_kernel_module_exit);

Macros used to register your module’s

module_init and module_exit Macros:

  • Here module_init and module_exit is not a function, but a macro defined in linux/module.h. 
  • For example, module_init() will add its parameter to the init entry point database of the kernel. 
  • And module_exit() will add parameter to exit entry point database of the kernel.
  • Basically, you just have to give your entry point name as an argument to these macros.

 

Module Metadata:

The next part is module description(show in Figure 1).

Figure 2. Module description
Figure 1. Module description

These are some meta data which we are going to include in your kernel module. You can include MODULE_LICENSE, MODULE_AUTHOR, and MODULE_DESCRIPTION. For that, these macros can be used.

Out of these macro’s MODULE_LICENSE macro is very much important where you have to mention license type of the a kernel module what you are going to write.

MODULE_LICENSE is a macro used by the kernel module to announce its license type.

If you load a module whose license parameter is non-GPL, then the kernel triggers a warning of being tainted.  Way of kernel letting the users and developers know it’s a non-free license-based modules. The developer community may ignore the bug reports you submit after loading the proprietary licensed module. The declared module license is also used to decide whether a given module can have access to the small number of “GPL-only” symbols in the kernel.

Go to linux/module.h(as shown in Figure 2) to find out what are the allowed parameters which can be used with this macro to load the module without tainting the kernel.

So, with this macro, you have to use a string value. This string value indicates a license type. It could be GPL or non-GPL. 

LKM entry point registration and other macros
Figure 2. Module License macro

 

And here, you see the module license macro (in Figure 3), and these are the options(in Figure 3) you can use with a module license macro.

LKM entry point registration and other macros
Figure 3. Options

It could be GPL, or GPL v2,  GPL BSD, MIT, all these string values you can use with that macro. If your module is proprietary, then you can use the string proprietary. That’s why, you have to use an appropriate value according to your license for which you are module Adhest to.

When a non-GPL module is loaded, the kernel throws an error saying a proprietary module has been loaded, and the kernel will get tainted. There is nothing wrong in using proprietary as an option here. You can use that. That depends on you.

And if you want to understand more about this licensing schemes, then you can read the kernel documentation, or you can just a google these a license terms you will get more information.

And by using MODULE_AUTHOR, a macro, you can mention the author of the kernel module, and by using MODULE_DESCRIPTION macro, you can write a small descriptive message which explains your kernel module.

 

Custom Information with MODULE_INFO:

You can use MODULE_INFO to add custom information to your module.

LKM entry point registration and other macros
Figure 4. MODULE_INFO

For example, if I want to include a message or a data such as a board is equal to beagle bone, then I would use module info. Here, the board is a ‘key’, and beagle bone is a ‘value’. When we do exercises, we are going to use all these macros. It will get clear when you see one example.

 

Extracting Module Information:

  • You can use tools like objdump or modinfo to extract metadata from kernel modules.
  • These tools are useful for analyzing ELF object files and obtaining module information.
  • This information can be especially valuable when building and working with kernel modules.

We will see all these things when we build our kernel module and generate a .ko files, that is kernel object file.

In the following article, let’s implement  hello world linux kernel modules, and after that, we’ll explore how to build a kernel module. 

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1 

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.