Linux kernel driver-kernel initial involvement

Source: Internet
Author: User
Tags documentation function prototype

One, why to learn the kernel?

Some people have to learn the kernel, while others can not learn it. If you want to engage in system development or drive development in the future, you should learn the kernel.

Just contact the kernel, mainly learn the interface function of the kernel. Don't read the kernel in depth, because you can't read or read, and the kernel code is as big as an animal.

Learning the kernel to master the level of learning, that is, learn from the beginning, a ring close to one ring.

The four-step learning method of kernel Learning: 1. Core theory Learning-concept and function prototype 2, case program analysis 3, mind map design 4, write your own code

Ii. Introduction to Linux kernel

1. Linux Architecture

Divided into two parts: User space and kernel space

2, Linux Why should be divided into user space and kernel space

The modern CPU usually realizes the different working mode, taking arm as an example, realizes the 7 working mode. X86 implements the different levels in 4: Ring0-ring3. RING0 can execute privileged commands, access IO devices, etc., and there are many limitations in Ring3. The Linux system utilizes this feature of the CPU to run the Linux kernel and the application in two different levels, allowing the operating system itself to be adequately protected. For example, if you use X86, the user code runs in Ring3, and the kernel code runs in RING0. Kernel space and user space are two different states of program execution, and can be transferred from user space to kernel space through system calls and hardware interrupts.

3.linux Kernel Architecture

System Call Interface

The SCI layer provides a set of standard system call functions for user space to access the Linux kernel and build up a bridge between user space and kernel space.

Process Management (PM) is the process of creating processes, stopping processes, and controlling communication between them. Process management also includes controlling how the active process shares the CPU, that is, process scheduling.

The primary role of memory management (MM) is to control the safe sharing of memory areas by multiple processes.

The Network protocol stack (network stack) provides a rich network protocol implementation for Linux.

The virtual file system (VFS) hides the specifics of each file system and provides a unified interface for file operations.

Device driver (DD): A large amount of code in the Linux kernel is in device drivers that control specific hardware devices.

Three, Linux kernel source code structure

1, download the source code address www.kernel.org

2, the Linux kernel source code is organized by tree structure, it is very reasonable to put the function-related files in the same subdirectory, making the program more readable.

Arch Directory: Arch is the abbreviation of architecture. Each of the CPU systems supported by the kernel has a subdirectory in that directory. The subdirectories of each CPU are further decomposed into subdirectories such as Boot,mm,kernel, including control system guidance, memory management, system call, and so on. /* X86 Intel CPU and compatible architecture: Boot Bootstrapper compressed kernel decompression tools generate compressed kernel image program Kernel related kernel features, such as signal processing, clock processing LIB hardware-related tool functions * *

Documentation Kernel Document

Drivers Device Driver Documentation

The header files required by the include kernel. Platform-Independent header files in the Include/linux subdirectory, the platform-related header files are placed in the corresponding subdirectories.

The FS directory holds the implementation code for various file systems. Each subdirectory corresponds to the implementation of a file system, and the common source program is used to implement the virtual file system VFS

"|--devpts is/dev/pts virtual file system

|| --EXT2 is the second extended file system

|| --FAT is the FAT32 file system of MS

|| --isofs is the file system on the IsO9660 CD-ROM

NET is the implementation code of Network protocol

|| --802 802 Wireless Communication protocol core Support code

|| --appletalk and Apple System networking protocol

|| --AX25 AX25 Wireless Internet Protocol

|| --bridge Bridge Equipment

|| --ipv4 IP Protocol Family V4 version 32-bit addressing mode

|| --ipv6 IP Protocol Family V6 Edition

Iv. configuration and compilation of Linux kernel

How does the code translate to a system image file that is burned or installed into a hardware platform?

1, why to configure the kernel

Choose what you need, and get rid of it! 1, hardware requirements 2, software requirements

After downloading the kernel to unzip in Linux, do not unzip under Windows because Windows is case-insensitive, and the Linux operating system is case-sensitive.

Enter the kernel file below:

2, the kernel configuration:

Make config: Interactive configuration based on text mode

Make Menuconfig: A text-mode-based menu configuration

<*> files are compiled from. c files to. o files, and the last link is compressed into a kernel image, which is stored in memory.

   The kernel module, which is compiled to install the. o file to the hard drive.

< > Indicates that the feature is not selected

The configuration result file is a hidden file that can be viewed with ls-a under the kernel file. config version

The above is a method, but as a beginner we are often based on an existing configuration file, by modifying the new configuration file, the Linux kernel provides a series of reference to the kernel configuration file, located in the Arch/cpu/configs

Next, we use the Linux system configuration file on the virtual machine to create our own upgraded kernel and run the kernel on the virtual machine.

Figure:

3. Compile kernel (compile kernel, compile kernel module, make RAMDisk)

3.1. Compile the kernel

Make zimage can only compile a kernel less than 512K

Make bzimage we typically compile the kernel using this method

For detailed compilation information, you can use:

Make Zimage V=1

Make Bzimage V=1

The compiled kernel is located in the arch/cpu/boot/directory * * *

3.2 Compiling kernel modules

Make modules compile kernel module

Make Modules_install will compile a good kernel module, copy from the kernel source directory to/lib/modules * *, ready for packing

After the first step is compiled, the. ko file that is scattered under each file is a memory module. Need to focus on moving to/lib/modules this is done by make Modules_install

3.3 Making Init RAMDisk

Methods: MKINITRD initrd-$version $version

For example: MKINITRD initrd-2.6.32 2.6.32

Get the running kernel version through UNAME-R

* $version can be obtained by querying the directory under/lib/modules

4, install the kernel

1, CP Arch/x86/boot/bzimage

/boot/vmlinuz-$version

2, CP initrd-$version/boot/

3, modify the/etc/grub.conf of the following four lines

5, clean the core

Make clean cleans and compiles the. o files produced by the kernel

Make Distclean cleans up the. o files and. config $version produced by the compiled kernel

Remember a few commands.

Rpm-qa | grep kernel Find Kernel pack information

RPM-E kernel-Kernel Package name

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.