Linux Module Programming Guide __HTML5

Source: Internet
Author: User
Linux Module Programming 1.1 Module Learning what

1. Know what a module is. What does it have to do with the drive I'm learning?

2. Familiar with the installation of the module, uninstall, view

3. Familiar with the basic framework of the module

4. Familiar with module programming method 1.2 kernel module Overview

The overall structure of the Linux kernel is huge and contains a lot of components. How do we incorporate the required parts into the kernel? One way to do this is to compile all the required functionality into the Linux kernel. This leads to two problems, one is that the generated kernel is very large, and the second is that if we want to add or remove functionality from the existing kernel, we will have to recompile the kernel.

Is there a mechanism where the compiled kernel itself does not need to contain all the functionality, and the corresponding code can be dynamically loaded into the kernel when the functionality needs to be used?

Linux provides such a mechanism, known as a module, to achieve these effects. The module has the following characteristics.

1. The module itself is not compiled into the kernel image, which controls the size of the kernel.

2. Once the module is loaded, it is exactly the same as the other parts of the kernel. 1.3 Realization Module

When we configure the kernel, we see an option to choose <M> <*> < > in the Configuration menu.

In which,<M> is compiled into a module, that is, modules, this easy to compile the option of the code is not compiled into the kernel zimage, but compiled into a separate file, typically suffix. ko (more than 2.6 kernel versions are. o) files before the. ko,2.6 kernel, then we can choose to install and uninstall these. Ko module files like software.

<*> to compile into the kernel, which is to compile this section of the module code into the zimage image file, the kernel when the start of the automatic installation of the implementation of our module code, this is similar to some of the installation system when the driver, It was installed when the system was installed.

< > says there is no choice but to do anything.

So, the module we are talking about is the. ko files that are under Linux . module test procedure provided by 1.3.1 tiny4412

In our study of the Tiny4412 provided by the Linux3.5 source code has a module test options, as follows:

Tiny4412module Sample.

Symbol:tiny4412_hello_module [=m]

Type:tristate

Prompt:Tiny4412 Module Sample

Definedat drivers/char/kconfig:49

DependsOn: arch_exynos4 [=y]

Location:

-> devicedrivers

->Character Devices

Note: Because there are dependencies, its dependencies must be selected, that is, Arch_exynos4 [=y].

CONFIG_ARCH_EXYNOS4:

Samsung EXYNOS4 SoCs based Systems

Symbol: arch_exynos4[=y]

Type:boolean

Prompt: SAMSUNGEXYNOS4

Defined at Arch/arm/mach-exynos/kconfig:14

Depends On:arch_exynos [=y]

Location:

-> SystemType

-> Samsungexynos SoCs Support

SELECTS:HAVE_SMP [=y] && might_have_cache_l2x0 [=y]

This test file is tiny4412_hello_moduls.c in the drivers/char/directory . when it is selected as <M>, compilation will generate the. ko file in the same level directory.

compile command: Make modules

In a Linux system, almost all drivers can compile the form of a module.

In short, we can install the module code after the system is started (the Zimage file does not contain the corresponding C code for this option). You can also compile to the kernel, as in the Kernel configuration menu, select Y, and the corresponding C code will be compiled into the Zimage file.

Here's a word: Linux is notnecessarily a driver, but the driver is a module .

 

Example: Compiling into a module

1 in the Linux kernel kernel configuration menu makememuconfig the module test option to M, and then exit save.

2) compiled into modules, as follows:

[Root@localhost linux-3.5]# make modules

Scripts/kconfig/conf--silentoldconfig Kconfig

CHK Include/linux/version.h

CHK include/generated/utsrelease.h

MAKE[1]: "Include/generated/mach-types.h" is up to date.

Call scripts/checksyscalls.sh

CC [M] crypto/ansi_cprng.o

CC [M] DRIVERS/CHAR/TINY4412_HELLO_MODULE.O

CC [M] drivers/scsi/scsi_wait_scan.o

Building modules, Stage 2.

Modpost 3 Modules

CC CRYPTO/ANSI_CPRNG.MOD.O

LD [M] Crypto/ansi_cprng.ko

CC DRIVERS/CHAR/TINY4412_HELLO_MODULE.MOD.O

LD [M] Drivers/char/tiny4412_hello_module.ko

CC DRIVERS/SCSI/SCSI_WAIT_SCAN.MOD.O

LD [M] Drivers/scsi/scsi_wait_scan.ko

[Root@localhost linux-3.5]#

The above will generate the Tiny4412_hello_module.ko file in drivers/char/.

Then we can look at its presence:

[Root@localhost linux-3.5]# CD drivers/char/

[root@localhost char]# ls tiny4412_hello_module.ko-l

-rw-r--r--. 1root root 29062 September 11:26 Tiny4412_hello_module.ko

[Root@localhost char]#

 

with the. ko file, we can load it into the kernel as externally independent. And how to unload it.

The Linux system provides professional commands to do such things, and here are some of these commands. 1.3.2 Linux Module installation, uninstall, view

The Linux module provides commands for installing, uninstalling, and viewing the modules and viewing module information. 1.3.1, module-related commands

Insmod <file name.ko>

Install the module into the kernel

Rmmod <file name>

Uninstall the specified module

Lsmod

To see which modules are currently installed on the system

Modinfo <file name>

View module Information

1.insmod--Installation module

Example :

[root@junjia/home]# insmod Tiny4412_hello_module.ko

[140.030000] Hello, Tiny4412 module is installed!

[root@junjia/home]# 2.lsmod--Lists the dependencies between modules and modules that are already installed on the current system (module code that does not contain the compiled kernel)

Example :

[root@junjia/home]# lsmod

Tiny4412_hello_module 773 0-live 0xbf000000

1th Column: module name

2nd Column: module size

3rd column: How many times is referenced (the code of this module is called by how many modules)

4th column: Which module is referenced by

Description :

The lsmod command actually reads and analyzes the/proc/modules file.

We may encounter this problem when installing the module, the module version and the Linux to install The problem occurs when the kernel version is different.

For example, I modified the kernel version number of the compilation module and then compiled the View module information:

[root@localhostlinux-3.5]# modinfo Drivers/char/tiny4412_hello_module.ko

Filename:drivers/char/tiny4412_hello_module.ko

License:gpl

Depends:

Intree:y

Vermagic: 3.5.0-friendlyarm_junjiaSMP preempt mod_unload ARMv7 p2v8

[root@localhostlinux-3.5]#

The following problems may occur with the installation:

[root@junjia/home]# insmod Tiny4412_hello_module.ko

[3212.730000] tiny4412_hello_module:version Magic '3.5.0-friendlyarm_junjiaSMP preempt mod_unload ARMv7 p2v8 ' should be '3.5.0-friendlyarmSMP preempt mod_unload ARMv7 p2v8 '

Insmod:can ' t insert ' Tiny4412_hello_module.ko ': Invalid module format

[root@junjia/home]#

The error is that the module version number is 3.5.0-friendlyarm_junjia, and the kernel version number is 3.5.0-friendlyarm and does not match and cannot be loaded. Then I can only

Conclusion: Unlike the version of the module and the kernel version, the module cannot be installed.

3.rmmod-Unload module (the module code compiled in the kernel cannot be removed)

Example:

[root@junjia/home]# Rmmodtiny4412_hello_module.ko

[root@junjia/home]# lsmod

Tiny4412_hello_module 773 0-live 0xbf000000 (O) //found no uninstall off

[root@junjia/home]# rmmodtiny4412_hello_module//no suffix

[231.660000] Good-bye, Tiny4412 module was removed!//print unload function information

rmmod:module ' tiny4412_hello_module ' not found//this hint is a bug that exists Busybos

[root@junjia/home]#

Note: The Rmmod command for the root file system made with respect to some busybox cannot be carried with an extension and cannot be unloaded, but X86 can be brought or not. Now the new version of the root file system making tool BusyBox fixes this feature , with. Ko can also uninstall.

sometimes we run into an error unloading the module . and so on, here is a common problem.

If the BusyBox makemenuconfig configuration is as follows:

Symbol:modprobe_small [=y]

Prompt:simplified modutils

Definedat modutils/config.in:15

Location:

                            ->linux Module utilities 

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.