Construction of modules in Linux, parameters, and simple use of PRINTK functions

Source: Internet
Author: User
Tags dmesg

Static compilation, dynamic loading
The application wants to access the kernel through a system call

Driven:
1. Module (package, add kernel)
2. Kernel mechanism
3. Operating Hardware

When configuring Menuconfig in Kconfig, different types will display the unused configuration options at the end of the graphical interface:
BOOL Config_hello []hello_driver two states
TriState Config_hello <>hello_driver Three-state empty * M
String/int Config_hello () hello_driver equivalent macro replacement (dest) Hello_driver---> Finally the result in. CONFIG is Hello_driver = dest, This is the format of the string


The function is modularized and can be uninstalled:
Here is the function: hello_module.c

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>


Module_license ("GPL"); Module authentication, do not write this statement, the final kernel will complain

static int __init hello_init (void)//through the following module, the final kernel will interpret this statement as Init_module (), which is what the kernel is looking for when executing the function name.
{The function is preceded by a--init to optimize the content of the segment after one execution, which optimizes memory and saves space.
PRINTK ("Hello_init! \ n ");
return 0;
}


static void __exit hello_exit (void)//through the following module, the final kernel will interpret this statement as Cleanup_module (), which is what the kernel is looking for when executing the function name
{
PRINTK ("Hello_exit! \ n ");
}

/* Module_init:
* 1. Static Compile:set "Hello_init" to. Init section
* 2. Dynamic loadable Module:hello_init named Init_module
*/
Module_init (Hello_init);
Module_exit (Hello_exit);

Module_author ("[email protected]"); Annotate some information
Module_description ("Just for test!");

Corresponds to the writing of the above makefile:

Mod_name = Hello_module

Obj-m = $ (mod_name). O

#KERN_DIR =/home/linux/linux-2.6.35-farsight
Kern_dir =/lib/modules/$ (Shell uname-r)/build

All
Make-c $ (kern_dir) m=$ (shell pwd) modules


Clean
RM-RF *.o *.ko *.mod.c *.order *.symvers. *.cmd. *versions


Backup
Tar cvf. /$ (mod_name). tar.gz. /$ (Mod_name)
Cp.. /$ (mod_name). Tar.gz/mnt/hgfs/ubuntu_share


The above C file will generate a. ko file after compiling: Execute with the following statement:
1.make Module
2.sudo insmod Hello_module.ko automatically run driver after call
3.sudo dmesg-c The data inside the cache before emptying it, or the last printed result will continue to print, interfering with the interpretation of the code
4.DMESG viewing the results of the code run
5.rmmod Hello_module Note that there is no. ko
6.lsmod Viewing module

When you want to see Hello_module.ko details, you can use the following command:
Modinfo Hello_module.ko

Parameters in kernel functions: HELLO_PARAM.C

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>


Module_license ("GPL");

static int m; Variables that want to pass the argument
static int n;

Module_param (m, int, 0600); Here are some definitions of the parameter properties: int type, 0600 is permission, read and Write permission
Module_param (n, int, 0200);

static int __init hello_init (void)
{
PRINTK ("Hello_init! \ n ");
PRINTK ("m =%d:n =%d \ n", M, N);
return 0;
}


static void __exit hello_exit (void)
{
PRINTK ("Hello_exit! \ n ");
PRINTK ("m =%d:n =%d \ n", M, N);
}

/* Module_init:
* 1. Static Compile:set "Hello_init" to. Init section
* 2. Dynamic loadable Module:hello_init named Init_module
*/
Module_init (Hello_init);
Module_exit (Hello_exit);

Module_author ("[email protected]");
Module_description ("Just for test!");

Makefile written for the above function:
Mod_name = Hello_param

Obj-m = $ (mod_name). O

#KERN_DIR =/home/linux/linux-2.6.35-farsight
Kern_dir =/lib/modules/$ (Shell uname-r)/build

All
Make-c $ (kern_dir) m=$ (shell pwd) modules


Clean
RM-RF *.o *.ko *.mod.c *.order *.symvers. *.cmd. *versions


Backup
Tar cvf. /$ (mod_name). tar.gz. /$ (Mod_name)
Cp.. /$ (mod_name). Tar.gz/mnt/hgfs/ubuntu_share

After compiling the above code, the Pass command is as follows:
1.sudo insmod Hello_param.ko m=1234 n=4321
2.dmesg

To view the parameters of the incoming kernel, use the following command to find:
1.cd/sys/module/hello_param/parametes/
2.ls
3.sudo cat See the parameters you see first

Export of kernel functions:

A function inside a C file is called in another function:
The following is a C file script, refer to:

Function provider: hello_export.c
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>


Module_license ("GPL"); Authentication followed by the kernel

void Common_func (void) The export function we used
{
PRINTK ("common_func! \ n ");
}
EXPORT_SYMBOL_GPL (Common_func); Export_symbol here to export the function, preceded by the GPL means that only the GPL-certified modules can use this function

static int __init hello_init (void) Note that the return value of this function is of type int
{
PRINTK ("Hello_init! \ n ");
return 0;
}


static void __exit hello_exit (void) The return value of this function must be of type void
{
PRINTK ("Hello_exit! \ n ");
}

Module_init (Hello_init);
Module_exit (Hello_exit);

Module_author ("[email protected]");
Module_description ("Just for test!");


function User: Hello_import.c

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>


Module_license ("GPL");

extern void Common_func (void); Users must first make a statement

static int __init hello_init (void)
{
Common_func (); This function is called here.
PRINTK ("Hello_init! \ n ");
return 0;
}


static void __exit hello_exit (void) Note that the previous--exit is the function of optimization, which is usually added
{
PRINTK ("Hello_exit! \ n ");
}

Module_init (Hello_init);
Module_exit (Hello_exit);

Module_author ("[email protected]"); Indicate the author's information
Module_description ("Just for test!"); Description of the module

Makefile's writing:

Mod_name = Hello_export

Obj-m = $ (mod_name). O
Obj-m + = HELLO_IMPORT.O

#KERN_DIR =/home/linux/linux-2.6.35-farsight
Kern_dir =/lib/modules/$ (Shell uname-r)/build Note This path is written, because the last obtained. ko file needs to rely on the compiled kernel version, here to solve different versions of the compilation run problem, it is worth learning

All
Make-c $ (kern_dir) m=$ (shell pwd) modules jump to the corresponding directory execution makefile


Clean
RM-RF *.o *.ko *.mod.c *.order *.symvers. *.cmd. *versions


Backup
Tar cvf. /$ (mod_name). tar.gz. /$ (Mod_name)
Cp.. /$ (mod_name). Tar.gz/mnt/hgfs/ubuntu_share

When the function executes: Load the provider first, then run the user

sudo insmod Hello_export.ko
sudo insmod Hello_import.ko

Then run DMESG to see the results

Check success after export: cat/proc/kallsyms | grep common_func//See the exported function below
Lsmod can also see the information of the function


Uninstall: Uninstall the user first, then uninstall the provider
sudo rmmod hello_import
sudo rmmod hello_export


Kernel print function printk (), HELLO_PRINTK.C

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>


Module_license ("GPL");

static int __init hello_init (void)
{
PRINTK (kern_info "Hello_info! \ n "); Kern_info here is the print priority setting
PRINTK (kern_notice "Hello NOTICE! \ n "); These permissions can be viewed inside the kernel include/linux/kerner.h
PRINTK (kern_err "Hello_err! \ n ");
PRINTK (kern_alert "Hello_alert! \ n ");
PRINTK (kern_warning "Hello_warning! \ n ");
return 0;
}


static void __exit hello_exit (void)
{
PRINTK ("Hello_exit! \ n ");
}

/* Module_init:
* 1. Static Compile:set "Hello_init" to. Init section
* 2. Dynamic loadable Module:hello_init named Init_module
*/
Module_init (Hello_init);
Module_exit (Hello_exit);

Module_author ("[email protected]");
Module_description ("Just for test!");

The makefile of the above C file:

Mod_name = HELLO_PRINTK

Obj-m = $ (mod_name). O

#KERN_DIR =/home/linux/linux-2.6.35-farsight
Kern_dir =/lib/modules/$ (Shell uname-r)/build

All
Make-c $ (kern_dir) m=$ (shell pwd) modules


Clean
RM-RF *.o *.ko *.mod.c *.order *.symvers. *.cmd. *versions


Backup
Tar cvf. /$ (mod_name). tar.gz. /$ (Mod_name)
Cp.. /$ (mod_name). Tar.gz/mnt/hgfs/ubuntu_share


Test execution of the above program, you need to test under the real terminal: CTRL+ALT+F2 into the terminal, ALT+F7 exit terminal
You can change the priority: columns such as:
Su
Echo 8 4 1 7 >/PROC/SYS/KERNEL/PRINTK//So the permission to print in the object set by the first array can be printed in less than 8
Insmod Hello_printk.ko

*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************

Construction of modules in Linux, parameters, and simple use of PRINTK functions

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.