Linux to write kernel modules and file read and write __linux

Source: Internet
Author: User
Tags data structures
What's Sysfs?

SYSFS is a memory-based file system that is used to provide kernel information to a user program in a file format. The directory hierarchy of the file system is organized in strict accordance with the data structure of the kernel. In addition to binaries (used only on special occasions), the contents of the Sysfs file are saved in ASCII format, and one file holds only one data, and a file cannot be larger than one page of memory (typically 4096 bytes). The project requires that the char array be saved to the property file, and the following describes how to do it. What is the Linux kernel module?

The kernel module is a socket provided externally by the Linux kernel, which is known as the dynamic loadable kernel module (Loadablekernel module,lkm), and is referred to as a module. The Linux kernel provides a modular mechanism because it itself is a single kernel (monolithic kernel). One of the biggest advantages of a single kernel is the high efficiency, because all the content is integrated, but its disadvantage is that scalability and maintainability is relatively poor, the modular mechanism is to make up for this flaw.

A module is a stand-alone program that can be compiled separately, but not run independently. It is linked to the kernel as part of the kernel running in the kernel space at runtime, which is different from the process running in user space. A module is usually composed of a set of functions and data structures to implement a file system, a driver, or other functions on the top of the kernel.

In short, a module is a code block that provides functionality for the kernel (in a sense, the kernel is also a module) or other kernel modules.

Linux kernel module writing

First, you need to include the header files required for kernel module programming, which typically includes the following three types of requirements:

Linux/module.h, the header file function is to dynamically load the module into the kernel

Linux/kernel.h, the header file contains the common kernel functions

Linux/init.h, the header file contains macros _init and _exit that allow memory to be freed from the kernel

Next is the module load function and the module unload function.

The load function needs to add a _init identity, such as

static int __init ***_init (void)

A return of 0 means that the load succeeds, and the failure returns some error codes, referring to <linux/errno.h>, which is typically used to initialize hardware in the load function, and to request resources.

The Unload function needs to add a _exit identity, such as

static void __exit ***_exit (void)

Generally in this function to complete the following few things,

If the module loading function registers the * * *, it should be cancelled in the module unload function;

If the memory is dynamically applied in the module load function, it also needs to be released here.

If some hardware resources (such as IRQ, DMA channel,i/o, memory) are requested in the module loading, they need to be released here.

The Module_init (***_init) function is then used to tell the kernel where the module from which to begin execution, the parameter is the function name of the registry, and the Module_exit (***_exit) function tells the kernel where the module is to be launched, and the parameter is the function name of the Unload function.

GCC needs to be compiled with the modules parameter, which is compiled as a kernel module, as shown in the following figure


The following describes how to read the contents of a file from user space and write to the kernel module's properties file at initialization time.

kernel module Read and write files

First, a simple understanding of the Linux file read and write workflow

So, to open a file in the kernel, you need to pass the Filp_open function, which has the same parameters as the open function.

Strcut file* filp_open (const char* filename, int open_mode, intmode);

It then obtains the current setting through GET_FS () and changes the way the kernel handles the memory address check by SET_FS (), and its prototype is as follows:

void Set_fs (mm_segment_t FS);

You can then read and write files using the Vfs_read and Vfs_write functions. The prototype is as follows

ssize_t vfs_read (struct file* filp, char __user* buffer, size_t len,loff_t* POS);

ssize_t vfs_write (struct file* filp, const char __user* buffer,size_t len, loff_t* POS);

Finally, the file is closed by Filp_close.

The specific implementation process is as follows:

Kernel Module Create property file

First call the Kobject_create_and_add function, dynamically create a kobject structure and register to SYSFS, the function is as follows

struct Kobject *kobject_create_and_add (const char *name, Structkobject *parent)

The property file is then created through a function sysfs_create_file. This also establishes the connection and correspondence between the file and the operation. The prototype function is as follows

int Sysfs_create_file (helloworld_kobj, &hello_value_attribute)

After the previous call is completed. Call Kobject_put so that the Kobject structure will be released dynamically when it is no longer in use. The prototype function is as follows

void Kobject_put (struct kobject *kobj)

The specific implementation process is as follows


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.