Linux Kernel Driver-Kernel

Source: Internet
Author: User

1. Why should we learn the kernel?

Some people want to learn the kernel, while others do not. If you want to engage in system development or driver development in the future, you must learn the kernel.

I just got started with the kernel and mainly learned kernel interface functions. Do not read the kernel in depth, because you do not understand it, and the kernel code is huge and cannot be controlled like a beast.

The Learning kernel is mainly used to master the hierarchical learning method, that is, learning from the beginning is closely linked.

Four-step kernel learning method: 1. Core theory learning-concept and function prototype 2. Sample program analysis 3. Mind Map Design 4. write code in person

II. Introduction to Linux Kernel

1. linux architecture

There are two parts: user space and kernel space


2. Why is linux divided into user space and kernel space?

Modern CPUs usually implement different working modes. Taking ARM as an example, the 7-in-7 Working Mode is realized. X86 achieves different levels in 4: Ring0-Ring3. Ring0 can execute privileged commands, can access IO devices, etc., there are many restrictions in Ring3. The linux system uses the CPU feature to run the linux kernel and applications at two levels, so that the operating system itself is fully protected. For example, if X86 is used, the user code runs in Ring3, and the kernel code runs in Ring0. the kernel space and the user space are two different states of program execution, the transfer from user space to kernel space can be completed through system calls and hardware interruptions.

3. linux kernel architecture


System Call Interface

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

Process Management (PM) is used to create, stop, and control the communication between processes. Process Management also includes controlling how active processes share the CPU, that is, process scheduling.

The main function of Memory Management (MM) is to control multiple processes to securely share memory areas.

Network Stack provides a variety of Network protocols for linux.

The Virtual File System (VFS) hides the details 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 included in the device driver, which controls specific hardware devices.

Iii. Linux kernel source code structure

1. Download source code www.kernel.org

2. the source code of the Linux kernel is organized in a tree structure. The function-related files are reasonably stored in the same subdirectory, making the program more readable.


Arch Directory: arch is short for architecture. Each CPU system supported by the kernel has corresponding subdirectories under this directory. Sub-directories of each cpu are further divided into sub-directories such as boot, mm, and kernel, including control system boot, memory management, and system calling. /* X86 Intel cpu is compatible with the sub-directory of the Architecture: boot program compressed kernel decompress tools to generate the program kernel-related kernel feature implementation method, such as signal processing and clock processing lib hardware-related tools and functions */

Documentation kernel documentation

Drivers Device Driver documentation

Include the header file required by the kernel. Platform-independent header files are in the include/linux subdirectory, and platform-related header files are placed in the corresponding subdirectory.

The fs directory stores the implementation code of various file systems. Each subdirectory corresponds to the implementation of a file system, and the public source program is used to implement the Virtual File System vfs

"| -- Devpts is the/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 cd-rom of the iso660 optical disk.

Net is the implementation code of the network protocol.

| -- 802 802 core support code of the wireless communication protocol

| -- Appletalk protocol connected to the Apple System

| -- Ax25 AX25 wireless INTERNET Protocol

| -- Bridge bridging device

| -- Ipv4 IP protocol V4 32-bit addressing mode

| -- Ipv6 IP protocol family V6

Iv. Linux Kernel configuration and compilation

How is the code converted into a system image file that is burned or installed on the hardware platform?

1. Why should I configure the kernel?

Select the desired ones. Remove unnecessary ones! 1. Hardware requirements 2. software requirements

After downloading the kernel, decompress it in linux. Do not decompress it in windows because windows is case-insensitive, while linux is case-sensitive.

Go to the kernel file:

2. Kernel configuration:

Make config: Interactive configuration based on text mode

Make menuconfig: text-based menu Configuration


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

<M> kernel module. The. o file is installed on the hard disk after compilation.

<> This function is not selected.

The configuration result file is a hidden file. You can use ls-a to view the. config version in the kernel file.

The above is a method, but as a beginner, we often get a new configuration file by modifying an existing configuration file, the Linux Kernel provides a series of Kernel configuration files for reference, located in Arch/cpu/configs.

Next, we will 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 the kernel (compile the kernel, compile the kernel module, and create ramdisk)

3.1 compile the kernel

Make zImage can only compile kernels smaller than KB

Make bzImage we usually use this method to compile the kernel

To obtain 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 compile the kernel module

Make modules compile the kernel module

Make modules_install: copy the compiled kernel module from the kernel source code directory to/lib/modules ** to prepare for packaging.

After the first step of compilation, the. ko files scattered under each file are memory modules. You need to move to/lib/modules in a centralized manner. This is done by make modules_install.

3.3 create init ramdisk

Method: mkinitrd initrd-$ version

Example: mkinitrd initrd-2.6.32 2.6.32

Get the running kernel version through uname-r

* $ Version can be obtained by querying the/lib/modules directory.

4. Install the kernel

1. cp arch/x86/boot/bzImage

/Boot/vmlinuz-$ version

2. cp initrd-$ version/boot/

3. Modify the last four lines of/etc/grub. conf.

5. Clean up the kernel

Make clean: Clear the. o files produced by the compiling Kernel

Make distclean: Clear the. o files and. config $ version generated by the compiling kernel.

Several memory commands

Rpm-qa | grep kernel find kernel package 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.