Linux Kernel configuration

Source: Internet
Author: User

With the wide application of Linux operating systems, especially the development of Linux in the embedded field, more and more people are devoted to Linux kernel-level development. Faced with the ever-growing Linux kernel source code, developers will face the same problem after completing their own kernel code, that is, how to integrate the source code into the Linux kernel, add the corresponding Linux configuration options and finally compile them into the Linux kernel. Therefore, you need to understand the Linux Kernel configuration system.
  
As we all know, the Linux kernel is developed by Linux fans all over the world. The Linux kernel is subject to many new changes every day. However, the organization of the Linux kernel is not messy, but concise and scalable. developers can easily add new content to the Linux kernel. One of the reasons is that linux adopts a modular Kernel configuration system to ensure kernel scalability.
  
This article first analyzes the Configuration System Structure in the Linux kernel, then explains the formats of makefile and configuration files, as well as the meaning of the configuration statement, and finally, through a simple example-test driver, describes how to add self-developed code to the Linux kernel. In the following article, it is not possible to explain all the functions and commands. You can only explain the commonly used functions. For those that have not been discussed, please refer to the references below.
  
  1. Configure the basic structure of the system
The Linux Kernel configuration system consists of three parts:
  
Makefile: makefile distributed in the source code of the Linux kernel, which defines the compiling rules of the Linux kernel;
Config. In: provides you with the configuration selection function;
Configuration tool: includes the configuration command interpreter (explains the configuration commands used in the configuration script) and configure the user interface (the user configuration interface based on the Character interface, the ncurses-based GUI, And the xwindows-based GUI, corresponding to make config, make menuconfig, and make xconfig ).
These configuration tools are all written in scripting languages, such as Tcl/TK and Perl (including some code written in C ). This document does not analyze the Configuration System, but describes how to use the configuration system. Therefore, unless it is the maintainer of the Configuration System, General kernel developers do not need to understand their principles. They only need to know how to compile makefile and configuration file. Therefore, in this article, we will only discuss makefile and configuration file. In addition, we take arm as an example for all the content related to the specific CPU architecture. In this way, we can not only clarify the problems discussed, but also have no impact on the content itself.
  
  2. makefile
2.1 makefile Overview
  
Makefile is used to construct a list of source files to be compiled based on the configuration, compile them separately, and link the target code together to form a Linux kernel binary file.
  
Since the Linux kernel source code is organized in a tree structure, makefile is also distributed in the directory tree. Makefile in Linux kernel and files directly related to makefile include:
  
Makefile: the top-level makefile, which is the overall control file for Kernel configuration and compilation.
. Config: The Kernel configuration file, including the configuration options selected by the user, used to store the Kernel configuration results (such as make config ).
ARCH/*/makefile: makefile located in various CPU system directories, such as ARCH/ARM/makefile, is a makefile for a specific platform.
Makefile under each subdirectory: for example, drivers/makefile, responsible for managing the source code under the subdirectory.
Rules. Make: Rule file, used by all makefiles.
After you use make config to configure, A. config is generated. Select the configuration for reading the top-level makefile into. config. The top-level makefile has two main tasks: generate the vmlinux file and the kernel module ). To achieve this, the top-level makefile recursively enters each subdirectory of the kernel and calls makefile in these subdirectories respectively. The subdirectories are determined by the Kernel configuration. In the top-level makefile, include ARCH/$ (ARCH)/makefile, which contains makefile under the specific CPU architecture. This makefile contains platform-related information.
  
Makefile in each subdirectory is also based on. the configuration information provided by config constructs the list of source files required for the current configuration, and includes $ (topdir)/rules at the end of the file. make.
  
The rules. Make file plays an important role in defining the compilation rules shared by all makefiles. For example, if you need to compile all C Programs in the current directory into assembly code, you need to have the following compilation rules in makefile:
  
%. S: %. c
$ (CC) $ (cflags)-S $ <-o $ @
    
Many Sub-directories have the same requirements, so you need to include this compilation rule in their makefiles, Which is troublesome. In Linux, such compilation rules are uniformly placed in rules. make, and include the rules in their makefiles. make (include rules. make) to avoid repeating the same rules in multiple makefiles. For the above example, the rule in rules. Make is:
  
%. S: %. c
$ (CC) $ (cflags) $ (extra_cflags) $ (cflags _ $ (* F) $ (cflags _ $ @)-S $ <-o $ @
    
2.2 variables in makefile
  
The top-level makefile defines and outputs many variables to the environment, and transmits some information to makefiles in each subdirectory. Some variables, such as subdirs, are defined in the top-level makefile and assigned the initial values. They are also expanded in arch/*/makefile.
  
Common variables include the following types:
  
1) version information
Version information includes: version, patchlevel, sublevel, extraversion, and kernelrelease. Version Information defines the current kernel version, such as version = 2, patchlevel = 4, sublevel = 18, exataversion =-rmk7, which together constitute the kernel release version kernelrelease: 2.4.18-rmk7
  
2) CPU architecture: arch
At the beginning of the makefile on the top layer, arch is used to define the architecture of the target CPU, such as arch: = arm. In makefile of many sub-directories, You must select the list of source files to be compiled according to the arch definition.
  
3) path information: topdir, subdirs
Topdir defines the root directory of the Linux kernel source code. For example, makefile in each subdirectory can locate rules. Make by using $ (topdir)/rules. Make.
Subdirs defines a directory list. When compiling a kernel or module, the top-level makefile determines which subdirectories to enter based on subdirs. The value of subdirs depends on the Kernel configuration. In the top-level makefile, the value of subdirs is kernel drivers mm FS net IPC Lib. According to the Kernel configuration, the value of subdirs is expanded in arch/*/makefile. See the example in 4.
  
4) kernel composition information: Head, core_files, networks, drivers, Libs
The vmlinux Kernel File is generated by the following rules:
  
  
Vmlinux: $ (configuration) init/Main. O init/version. O linuxsubdirs
$ (LD) $ (linkflags) $ (head) init/main. O init/version. O -- start-group $ (core_files) $ (drivers) $ (networks) $ (libs) -- end-group-O vmlinux
  
We can see that vmlinux is composed of head, Main. O, version. O, core_files, drivers, networks, and libs. These variables (such as head) are used to define the target file and library file list generated by the connection in vmlinux. The head is defined in arch/*/makefile to determine the list of files first linked to vmlinux. For example, for the cpu Of the ARM Series, the head is defined:
Head: = ARCH/ARM/kernel/head-$ (processor). o arch/ARM/kernel/init_task.o
  
It indicates that head-$ (processor). O and init_task.o must be first linked to vmlinux. Processor is armv or Armo, depending on the target CPU. Core_files, network, drivers, and libs are defined in the top-level makefile, and are expanded by arch/*/makefile as needed. Core_files corresponds to the core file of the kernel, including kernel/kernel. o, mm/mm. o, FS/Fs. o, IPC/IPC. o, we can see that these are the most important files in the kernel. In addition, arch/ARM/makefile expands core_files:
  
# Arch/ARM/makefile
  
# If we have a machine-specific directory, then include it in the build.
Machdir: = ARCH/ARM/Mach-$ (machine)
Ifeq ($ (machdir), $ (wildcard $ (machdir )))
Subdirs + = $ (machdir)
Core_files: = $ (machdir)/$ (MACHINE). o $ (core_files)
Endif
  
Head: = ARCH/ARM/kernel/head-$ (processor). o arch/ARM/kernel/init_task.o
Subdirs + = ARCH/ARM/kernel ARCH/ARM/mm ARCH/ARM/lib ARCH/ARM/nwfpe
Core_files: = ARCH/ARM/kernel. o arch/ARM/MM/mm. o $ (core_files)
Libs: = ARCH/ARM/lib. A $ (libs)
  
  
5) Compilation information: CPP, CC, As, LD, AR, cflags, linkflags
In rules. Make, general compilation rules are defined. In specific scenarios, the compilation environment must be clearly defined. The compilation environment is defined in the preceding variables. Cross_compile is defined for cross-compilation requirements. For example:
  
  
Cross_compile = arm-Linux-
Cc = $ (cross_compile) GCC
LD = $ (cross_compile) LD
......
  
Cross_compile defines the prefix of the Cross-compiler Arm-Linux-, indicating that all the cross-compiler tools start with arm-Linux-. Therefore, before each cross-compiler tool, $ (cross_compile) is added to form a complete cross-compilation tool file name, such as arm-Linux-GCC.
Cflags defines the parameters passed to the C compiler.
Linkflags is the parameter used by the linker when the link is used to generate vmlinux. Linkflags are defined in ARM/*/makefile, for example:
  
# Arch/ARM/makefile
  
Linkflags: =-p-X-t arch/ARM/vmlinux. LDS
  
  
6) configure the variable config _*
The. config file contains many configuration variable equations to describe user configuration.

Turn from http://www.linuxmine.com, 3x!

 

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.