LINUX module stack technology and multi-ko Compilation

Source: Internet
Author: User
Tags dmesg

The lsmod command in the driver actually reads the/proc/modules file.
That is, the result corresponding to the lsmod command is cat/proc/modules.

Information about the loaded modules in the kernel exists in the/sys/Module Directory.

The modprobe command is more powerful than the insmod command. When a module is loaded, it loads other modules that the module depends on at the same time.
Use modprobe-r filename to uninstall the modules that are dependent on at the same time.
Modinfo Module name command to obtain module information
Modinfo hello. Ko

Filename: Hello. Ko
Alias: a simplest Module
Description: a simple hello World module
Author: Jimmy
License: Dual BSD/GPL
Srcversion: fc20e540c350c6f733c7546
Depends:
Vermagic: 2.6.28-15-genericsmp mod_unload modversions 586

Driver Module parameters, module_parm_desc can be annotated with Parameters
For example:
Static int num = 4000;
Module_param (Num, Int, s_irugo );
Module_parm_desc (myshort, "a integer ");

# Include <Linux/init. h>
# Include <Linux/module. h>

Module_license ("dual BSD/GPL ");

Static char * book_name = "dissecting Linux Device Driver ";
Static int num = 4000;
Static int Param [8] = {1, 2, 3, 4, 5, 6, 7, 8 };
Static int param_len = 8;

Static int book_init (void)
{
Int I;
Printk (kern_alert "bookname: % s/n", book_name );
Printk (kern_alert "booknum: % d/N", num );
For (I = 0; I <8; I ++)
{
Printk (kern_alert "Param [% d] = % d/N", I, Param [I]);
}

Return 0;
}

Static void book_exit (void)
{
Printk (kern_alert "bookmodule exit/N ");
}

Module_init (book_init );
Module_exit (book_exit );
Module_param (Num, Int, s_irugo );
Module_param (book_name, CHARP, s_irugo );
Module_param_array (Param, Int, & param_len, s_irugo );

Module_author ("Jimmy, fightingjimmy@gmail.com ");
Module_description ("A simple module for testing module Params ");
Module_version ("V1.0 ");

Sudo insmod book. Ko
Dmesg | tail-10
[2, 14047.901352] book name: Dissecting Linux Device Driver
[14047.901366] Book num: 4000
[14047.901377] Param [0] = 1
[2, 14047.901386] Param [1] = 2
[2, 14047.901388] Param [2] = 3
[2, 14047.901389] Param [3] = 4
[1, 14047.901390] Param [4] = 5
[1, 14047.901391] Param [5] = 6
[2, 14047.901392] Param [6] = 7
[2, 14047.901393] Param [7] = 8

Sudo rmmod book. Ko
Sudo insmod book. Ko book_name = 'hello' num = 1000 Param =, 6, 5
[2, 14298.942521] book name: Hello
[14298.942535] Book num: 1000
[1, 14298.942547] Param [0] = 8
[1, 14298.942558] Param [1] = 7
[1, 14298.942559] Param [2] = 6
[2, 14298.942560] Param [3] = 5
[1, 14298.942561] Param [4] = 5
[1, 14298.942562] Param [5] = 6
[2, 14298.942564] Param [6] = 7
[2, 14298.942565] Param [7] = 8

Sudo rmmod book. Ko
Sudo insmod book. Ko book_name = "Hello World" num = 1000 Param = 8, 7, 6, 5
Insmod: Error inserting 'book. ko':-1 unknown symbol in Module

Dmesg | tail-10
[2, 15097.587159] Book: Unknown Parameter 'World'
Conjecture: it may be because the input string parameters contain spaces (please do not hesitate to inform us if you encounter similar situations)

The/proc/kallsyms file of linux2.6 corresponds to the kernel symbol table, which records and the memory address of the symbol.
Export_symbol (symbol name );
Exprot_symbol_gpl (symbol name );

Module/
Include/
Print. h
Print/
Print. c
Makefile
Symbol/
Test. c
Makefile

/*************************************** *
* Print. H *
**************************************** */
# Ifndef print_h
# Define print_h

Void add_integer (int A, int B );
Void sub_integer (int A, int B );

# Endif

/*************************************** *
* Print. C *
**************************************** */
# Include <Linux/init. h>
# Include <Linux/module. h>

# Include "print. H"

Module_license ("dual BSD/GPL ");

Void add_integer (int A, int B)
{
Printk (kern_alert "sum: % d/N", A + B );
}

Void sub_integer (int A, int B)
{
Printk (kern_alert "Sub: % d/N", A-B );
}

Export_symbol (add_integer );
Export_symbol (sub_integer );

/*************************************** *
* Test. C *
**************************************** */
# Include <Linux/init. h>
# Include <Linux/module. h>

# Include "print. H"

Module_license ("GPL ");

Static int symbol_init (void)
{
Printk (kern_alert "symboltest init/N ");
Add_integer (10, 6 );
Sub_integer (10, 6 );
Return 0;
}

Static void symbol_exit (void)
{
Printk (kern_alert "symboltest exit/N ");
}

Module_init (symbol_init );
Module_exit (symbol_exit );

Makefile in the print directory:
OBJ-M: = print. o
Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Print_inc = $ (OBJ)/../include # contains the print. h header file
Extra_cflags + =-I $ (print_inc)

Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules

Modules_install:
$ (Make)-C $ (kerneldir) M = $ (PWD) modules_install

Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

. Phony: modules modules_install clean

Makefile in the symbol directory:
OBJ-M: = test. o
Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Symbol_inc = $ (OBJ)/../include
Extra_cflags + =-I $ (symbol_inc)
Kbuild_extra_symbols = $ (OBJ)/../print/module. symvers

Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules

Modules_install:
$ (Make)-C $ (kerneldir) M = $ (PWD) modules_install

Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

. Phony: modules modules_install clean

Note: If the MAKEFILE file does not contain kbuild_extra_symbols = $ (OBJ)/../print/module. symvers
Copy module. symvers under the print directory to the symbol directory.
Kernel Versions later than 2.6.26 bug (For details, please refer to the http://bugzilla.kernel.org/show_bug.cgi? Id = 12446)


Compile insmod separately.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.