2017-2018-1 20179202 "Linux kernel Fundamentals and Analysis" Tenth week assignment

Source: Internet
Author: User

I. Equipment and modules 1. Type of equipment
    • Block device: Random access to content in the device, accessed through block device nodes, usually mounted as file system
    • Character device: Non-addressable, provides streaming access to data only, through character device node access, applications interact with character devices by direct access to device nodes
    • Network devices: Access via socket API

In addition to physical devices, some of the drive devices are virtual (pseudo-devices), providing only access to kernel functionality. such as kernel random number generator, empty device, 0 device, full device, memory device.

2. Module

(1) The Linux kernel is modular, allowing the kernel to dynamically insert or remove code from it at runtime. The code is combined in a separate binary image to load the kernel modules. A module is a standalone program that can be compiled separately, but not run independently. It is linked to the kernel at run time as part of the kernel running in kernel space. The dynamic loading of kernel modules allows the kernel image to be kept to a minimum size, with maximum flexibility, and facilitates the verification of new kernel code without recompiling the kernel and rebooting.

#include<linux/module.h>#include<linux/init.h>#include<linux/kernel.h>static int hello_init(void){    printk(KERN_ALERT"Hi module!\n");    return 0;}static void hello_exit(void){    printk(KERN_ALERT"Bye module!\n");}module_init(hello_init);//hello_init模块入口函数,通过module_init例程注册到系统中,在模块加载时被调用module_exit(hello_exit);//hello_exit模块入口函数,通过module_exit例程注册到系统中,在模块从内存卸载时被调用

The Module_license () macro is used to specify the version of the module, the Module_author () macro specifies the code author, and Module_description () is a brief description of the module.

(2) The module can be placed in the kernel source tree. The device driver is placed in a subdirectory of the/drivers, such as a character device placed under/drivers/char/, and the device block is placed under/drivers/block/. Makefile added in the corresponding directory, the obj-m += ···.o module can also be placed outside the kernel source code tree, in its own source code directory to establish makefile, through the make command to find the source code and makefile. If there are multiple source files, use two lines enough:

obj-m := fishing.ofishing-objs :=fishing-main.o fishing-line.o

(3) After the compiled module is loaded into the/lib/modules/version/kernel/, make modules_install install the compiled module into the appropriate directory.

    • Name: is both a user-visible parameter name and a variable name that holds module parameters in the module
    • Type: Types of parameters (byte, short, int, uint, long, ulong, CHARP, bool ...) The byte type is stored in the char variable, and the bool type is stored in the INT variable
    • Perm: Specifies the file permissions of the module in the Sysfs file system
insmod module.ko //载入模块rmmod module.ko  //卸载模块modprobe module [参数] //加载依赖模块modprobe -r modules    //卸载依赖模块

module_param(name, type, perm)Defines a module parameter that the user can specify when the system boots or when the module is loaded.

(4) When the module is loaded, it is dynamically connected to the kernel. The exported kernel functions can be called by the module, but not exported. Exporting a kernel function requires a special directive: EXPORT_ SYMBOL() (after the exported function) EXPORT_SYMBOL_GPL() (as with Export_symbol, the difference is that only the modules that are marked as GPL protocol are visible)
The exported kernel symbol table is considered an exported kernel interface, called the Kernel API.

3. Device model

(1) Kobject is the core part of the device model, which is represented by struct kobject structure, which is usually embedded in other data structures to play a role. Such as:

struct cdev {    struct kobject kobj;    //嵌在 cdev 中的kobject     struct module *owner;       const struct file_operations *ops;    struct list_head list;    dev_t dev;    unsigned int count;};

Once Kobject is embedded in the Cdev, it is possible to establish a hierarchical relationship between Cdev through Cdev->kboj.parent, and to obtain all associated cdev->kobj.entry devices through Cdev.

In short, after embedding the Kobject, there is a tree structure between the cdev devices, and there is a hierarchical relationship between the Cdev device and other devices.

(2) Kobject is associated to the Ktype, with the kobj_type structure, that is, a family of kobject properties of a unified definition, to avoid each kobject defined separately.

(3) Kset is a collection of Kobject objects that can be placed in a kset by all relevant kobject. Kset lets the embedded kobj be authored as the base class for the Kobject group, and sets the associated kobject together.

(4) Kobject provides a unified reference counting system, which is implemented by the KREF structure. After initialization, the reference count for Kobject is set to 1. The reference count is nonzero and the object continues to remain in memory. The reference count falls to 0 o'clock, the object can be revoked, and the associated memory is freed.

4.sysfs

(1) Sysfs is an in-memory virtual file system that provides a view of the Kobject object hierarchy. Kobject is a directory that is mapped into SYSFS, and the file in Sysfs is the attribute of Kobject.

int sysfs_create_file(struct kobject *kobj, const struct attribute *attr);//添加新属性void sysfs_remove_file (struct kobject *kobj, const struct attribute *attr);//删除属性int sysfs_create_link(struct kobject *kobj, struct kobject *target, char *name);//在sysfs中创建一个符号连接void sysfsremovelink(struct kobject *kobj , *char name);//删除由sysfs_ creat_ link()创建的符号连接可通过:

(2) In order to keep the Sysfs clean and intuitive, SYSFS convention:

    • Ensure that only one value is exported per file, which is in text form and can be mapped to a simple C type
    • Organize your data at a clear level
    • Provides kernel-to-user space services
5. Thinking

Kset,ktype are related to Kobject, a collection, a unified definition Kobject attribute, both of which seem to be the classification method, then what is the difference?

Refer to "Linux kernel design and implementation" Reading notes (17)-Devices and modules

Kset and Ktype are designed to classify kobject in order to centralize processing of common processing, thus reducing the amount of code and also increasing maintainability. From the entire kernel code, the number of kset is more than the number of Ktype, the same ktype kobject can be in different kset.

Bloggers made a metaphor: if the kobject compared to a person, kset equivalent to a country, Ktype is the equivalent of race (such as yellow, white, etc.). There are only a few types of races, but there are many countries, and the aim of the race is to describe the common attributes of a group of people, and the country's purpose is to manage a group of people. That is, Ktype focuses on description, and Kset focuses on management.

Second, portability

1. When the word length is involved in kernel encoding, keep the following guidelines in mind:

    • The ANSI C standard stipulates that the length of a char must be one byte (8 bits)
    • In the architecture currently supported by Linux, the int type is 32-bit
    • Linux currently supports architectures where the short type is 16-bit
    • In the architectures currently supported by Linux, the length of pointers and long types is variable, and changes in 32-bit and 64-bit cannot assume that sizeof (int) = = sizeof (long), similar to the length of the pointer and int type is not assumed

2. Opaque data types hide their internal formatting or structure. Using typedef to declare a type, which is called an opaque type, is a package on the standard type of C language.

When using these opaque types, the following guidelines need to be noted:

    • Do not assume the length of the type (which is afraid to see its C language type through the source code), these types may change in length in different architectures
    • Do not convert these opaque types to C standard types to use
    • Code is not affected when programming to ensure that the actual storage space of an opaque type or when the format is changed

3. Length-definite types can only be used in kernel space and user space is not available. The user space has a corresponding variable type, with 2 more underscores before the name. These two names can be used arbitrarily in the kernel, but the user-visible type must use an underscore-prefixed version.

4.char types are in different architectures and sometimes are signed by default, sometimes without symbols. Therefore, when assigning a value to a char type, it is explicitly signed.

5.1027 corresponding binary for 00000000 00000000 00000100 00000011, high priority byte order and status precedence byte order

6. Data alignment:

    • Do not convert types with different lengths when converting types by pointer
    • For arrays, the installation base data type is aligned on the line. (the array elements are kept in memory consecutively, the first is aligned, and the back is automatically aligned)
    • For a consortium, the data with the largest length can be aligned
    • For structs, ensure that each element in the structure is properly aligned
    • 7. You should use Page_size to represent the page length in bytes, using Page_shift to indicate how many bits are masked from the far right to get the page number of the page that corresponds to that address.
Iii. patches, development and community
    • The indentation style is to indent 8 character lengths (not 8 spaces) at a Time with tabs (tab), each indented by a tab, with 8 character lengths per tab stop.
    • The case tag of the switch statement should be indented to align with the switch declaration, which will help reduce the typographic indentation that is brought by the TAB key of 8 characters.
    • The opening parenthesis is immediately at the end of the statement, on the same line as the statement. The closing parenthesis is a new line, as the first character of the line. Note that the next identifier is part of the same statement block, and the curly braces do not take on a single line, but are on the same line as that identifier.
    • Camel spelling, Studly caps, or other mixed uppercase and lowercase characters are not allowed in the name. Global variables and functions should select names that contain descriptive content, use lowercase letters, and underline words when necessary.
    • In the source code less use TypeDef, IFDEF.
diff -urN linux-old/ linux-new/ > my-patch //通过源码和加进了所修改部分的源代码两份代码创建补丁diff -u linux-old/some/file linux-new/some/file > my-patch  //对单独的文件进行diffdiffstat -p1 my-patch  //列出补丁所引起的变更的统计(加入或移去的代码行)git commit -a   //提交所有修改的代码git commit linux-src/some/file.c //提交某个修改的代码git add linux-src/some/new-file.c   // 把新增的文件加入版本库git commit -a       //Git并不立即提交新文件,直到把它们添加到版本库中才提交git format-patch -N  //生成最后N次提交产生的补丁

2017-2018-1 20179202 "Linux kernel Fundamentals and Analysis" Tenth week assignment

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.