A simple example of a Linux kernel module

Source: Internet
Author: User

1. The overall structure of the Linux kernel is very large and contains many components, using these components in two ways:

① directly into the kernel file, either Zimage or bzimage (problem: excessive memory consumption)

② Dynamic Additions

* The module itself is not compiled into the kernel file

* Dynamically install or uninstall during kernel runtime, as required

2. Dynamic installation and uninstallation of kernel modules

① installation Insmod

Example: Insmod/home/dnw_usb.ko

② Uninstalling Rmmod

Example: Rmmod dnw_usb

③ View Lsmod

Example: Lsmod

3. Module declaration

①module_license ()

②module_author ()

③module_description ()

④module_version ()

4. Module parameters

The ① module parameter is used to pass parameters to the module when the module is loaded

② specifying variables to save module parameters via macro Module_param

Module_param (name, type, perm)

* Name: Names of variables

* Type: Variable type. BOOL: boolean, int: integer, Charp: String

* Perm is access rights. S_irugo: Read permission, s_iwusr: Write permission

③ Simple Example

int 3  int, sirugo);

Command line: insmod Xxx.ko a = 10

5. Symbol Export

① if a function or global variable in a kernel module wants to be used by another module, it must be exported

② kernel symbol export using macros

Export_symbol (symbol name)

EXPORT_SYMBOL_GPL (symbol name)

Note: Where EXPORT_SYMBOL_GPL can only be used for modules that contain GPL licenses

6. Kernel Module Simple Example

① Module Code:

#include <linux/init.h><linux/module.h>staticint  hello_init () {    PRINTK (kern_warning"Hello World initlizing\n");     return 0 ;    } Static void Hello_exit () {    printk (kern_warning"Hello World exiting\n");    } Module_init (Hello_init); Module_exit (hello_exit);

②makefile:

Obj-m: = helloworld.okdir:=/home/linux/kernal/linux-2.6.  in All :     -C $ (Kdir) m=$ (PWD) modules cross_compile=arm-linux-arch=arm clean    :    -F *.o *.ko *.order *. symvers    

A simple example of a Linux kernel module

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.