Linux, kernel source code, kernel compilation and configuration, kernel module development, and kernel Startup Process

Source: Internet
Author: User

How is Linux made up?
A: Linux is composed of user space and kernel space.
Why divide user space and kernel space?
A: For the CPU architecture, each processor can have multiple modes, while the Linux Division takes into account the system's
Security. For example, x86 can have four modes: ring0 ~ Ring3 ring0 privileged mode to Linux kernel space ring3 to user space
How is the Linux kernel made up?
A: The Linux kernel is managed by the SCI (System Call Interface) System Call interface, the PM (Process Management) process management, the mm (memory management) memory management, the arch,
VFS (Virtual File systerm) Virtual File System, NS (network stack) network protocol stack, DD (Device Drivers) Device Driver

Linux KernelSource code

Linux kernel source Code How is the structure or directory structured?
A: The Arc directory stores some Code related to the CPU architecture. The first CPU subdirectory is used to break down the subdirectories such as boot, mm, and Kerner.
Block directory part block device driver code
Crypto Directory encryption, compression, CRC verification Algorithm
Documentation kernel documentation
Drivers Device Driver
FS stores the implementation code of various file systems
Include the header file required by the kernel. Platform-independent header files are included in the include/Linux subdirectory, and platform-related header files are placed in the corresponding subdirectory.
Init kernel initialization code
Implementation Code of IPC inter-process communication
Most of the key functions of kernel Linux are implemented in this directory ( Program Scheduling, process control, modular)
Lib library file code
MM memory management unrelated to the platform. The platform-related implementation code is stored in the corresponding ARCH/CPU directory.
Sample sample of samples kernel Programming
Script for configuring the kernel in scripts
Security SELinux Module
Sound audio device driver
USR cpip command Implementation Program
Virt kernel Virtual Machine

Kernel configuration and compilation
1. Clear
Make clean: Delete the compilation file but keep the configuration file
Make mrproper Delete All compilation files and configuration files
Make distclean: delete compilation files, configuration files including backup and patch Patches
Ii. Kernel configuration
Make config text-based interactive Configuration
Make menuconfig text-based menu Configuration
Make oldconfig uses the existing configuration file (. config), but the new configuration options are asked during configuration.
Make xconfig graphical Configuration

3. Make menuconfig instructions or tips
Press "Y" in the brackets to compile the program into the kernel. Press "M" to compile the program as a module. Press "N" to select a module. You can also press the Space key to select a module.
Note: during kernel compilation, the "Y" compiled into the kernel and the "M" compiled into the module are compiled step by step.

4. Quickly configure the kernel configurations of the corresponding Architecture
We can copy the configuration file of the corresponding Processor Model to the kernel source directory to replace the. config file under the arch/$ CPU/configs directory.

5. Compile the kernel
1.
----------------------------
Make zimage Note: zimage can only compile kernels smaller than KB
Make bzimage
We can also obtain the compilation information during compilation.
Make zimage v = 1
Make bzimage v = 1
The compiled kernel is located in the arch/$ CPU/boot/directory.
----------------------------

The above is the compilation of the "M" option before the kernel make menuconfig, and then to the compilation of the "y" module, that is, the compilation module.
2.
Make modules compile the kernel module
Make modules_install install kernel module ------> this option is used to copy the compiled kernel module from the kernel source code directory to/lib/modules.

6. Create init ramdisk
Mkinitrd initrd-$ version
/***** Mkinitrd initrd-$ (Changeable) version $ version (unchangeable, because the version is created in the corresponding directory under/lib/modules) ****/

7. kernel Installation
Copy the kernel to the relevant directory and then perform GRUB boot.
1. cp arch/$ CPU/boot/bzimage/boot/vmlinux-$ version
2. CP $ initrd/boot/
3. Modify the boot loader/etc/grub. conf (Lio. conf) to boot correctly.

# Incldue <Linux/init. h>
# Include <Linux/module. h>

Static int hello_init (void)
{
Printk (kern_warning "Hello, world! \ N ");
Return 0;
}

Static void hello_exit (void)
{
Printk (kern_info "Good, world! \ N ");
}

Module_init (hello_init );
Module_exit (hello_exit );

___________ Hello, world! Example ___________________
I. Required module functions
1. The module_init (hello_init) function is loaded. The module_init macro is used to specify
2. Uninstall the module_exit (hello_exit) function. Use the module_exit macro to specify

Makefile is often used in the compilation module.

Ii. Optional module functions
1. module_license ("********"); license statement
2. module_author ("*********");
3. modele_description ("***"); Module description
4. module_version ("V1.0"); Module version
5. module_alias ("**********"); Module alias

Iii. Module Parameters
Specify the module parameters through macro module_param. The module parameters are used to pass the parameter module when loading the module.
Module_param (Neme, type, Perm );
Name is the module parameter name.
Type is a common parameter type value: boot, Int, CHARP (string type)
Perm is a common value of parameter access permission perm: s_irugo, s_iwusr
S_irugo: any user has read permission on the parameters in sys/module.
S_iwusr: allows the root user to modify the parameters in/sys/module.
/***** ------ Example --------*******/
Int A = 3;
Char * st;
Module_param (A, Int, s_irugo );
Module_param (St, CHARP, s_irugo );

/********* ---- End ------**********/

/********** ---- Makefile example ----*************/

Ifneq ($ (kernelrelfase ),)

OBJ-M: = Hello. O // here, the m value is replaced by obj-(config _ **).

Else

Kdir: =/lib/modules/$ version/build
ALL:
Make-C $ (kdir) M = $ (PWD) Modules
Clean:
Rm-f *. Ko *. O *. Mod. O *. Mod. C *. symyers
Endif

/***** Multiple obj-M ************** end *********** ****/

/****** Module parameters *****/
# Include <Linux/init. h>
# Include <Linux/module. h>
Module_license ("GPL ");

Static char * name = "junroc July ";
Static int age = 30;

Module_param (ARG, Int, s_irugo );
Module_param (name, CHARP, s_irugo );

Static int Hello Init (void)
{
Printk (kern_emerg "Name: % s \ n", name );
Printk (kern_emerg "Age: % d \ n", age );
Return 0;
}

Static void hello_exit (void)
{
Printk (kern_infa "module exit \ n ");
}

Modulej_init (hello_init );
Module_exit (hello_exit );
/****************/

----------------------------------------------------------------------------
The/proc/kallsyms document records the names and addresses of all exported symbols in the kernel.
What is export?
A: Exporting refers to importing the module-dependent symbols into the kernel for other modules to call.
Why export?
A: If the dependency is not exported, it cannot be solved and the Import fails.

Symbol export instructions:
Export_symbol (symbol name)
Export_symbol_gpl (symbol name)
Export_symbol_gpl can only be used for modules that contain the GPL license.

Solution to module version mismatch:
1. Use modprobe -- force-modversion to forcibly insert data
2. Ensure that the kernel code version on which the kernel module is compiled is equivalent to the currently running kernel uname-R.
----------------------------------------------------------------------
Print the printk kernel:
Printk allows message classification by attaching different "Priority" based on severity
In <Linux/kernel. h>, eight record levels are defined. Descending by priority:
Kern_emerg "<0>" is used for emergency messages, often messages before a crash
Kern_alert "<1>" message requiring immediate action
Kern_crit "<2>" severe condition
Kern_err "<3>" error
Kern_warning "<4>" problematic warnings
Kern_notice "<5>" normal, but still worth noting
Kern_info "<6>" information message
Kern_debug "<7>" used to debug messages

Printk with no specified priority is used by default.
Default_message_loglevel priority. It is an integer defined in kernel/printk. C.

Configuration of control priority:
/Proc/sys/kernel/printk (which can be viewed or modified)

/* Example of the dependencies between symbol modules *****/

--------/********* Hello. c *********/----
# Include <Linux/module. h>
# Include <Linux/init. h>
Module_license ("GPL ");
Module_author ("junroc July ");
Module_description ("Hello, world module! ");
Module_alias ("A Simple Modle Test ");

Extern int add_integar (int A, int B );
Extern int sub_integar (int A, int B );

Static int _ init hello_init ()
{
Int res = add_integar (1, 2 );
Return 0;
}

Static void _ exit hello_exit ()
{
Int res = sub_integar (2, 1 );
}

Module_init (hello_init );
Module_exit (hello_exit );

/****** Hello. c ***** end **********/

/******** Start ****** calculate. c ******/
# Include <Linux/init. h>
# Include <Linux/module. h>

Module_license ("GPL ");

Int add_integar (int A, int B)
{
Return A + B;
}

Int sub_integar (int A, int B)
{
Return A-B;
}

Static int _ init sym_init ()
{
Return 0;
}
Static void _ exit sym_exit ()
{

}

Module_init (sym_init );
Module_exit (sym_exit );

// Export_symbol (add_integar );
// Export_symbol (sub_integar );

/*********** End ****** calculte. c ****/

Read the full text

Category:View comments by default

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.