Linux, kernel source, kernel compilation and configuration, kernel module development, kernel boot process

Source: Internet
Author: User

How is Linux composed?
A: Linux is made up of user space and kernel space
Why divide user space and kernel space?
A: For CPU architectures, each processor can have multiple modes, and Linux is a partition that takes into account the system's
Security, such as X86 can have 4 modes ring0~ring3 RING0 privileged mode to the Linux kernel space RING3 to the user space
How is the Linux kernel composed?
A: The Linux kernel is called by the SCI (System call Interface) systems interface, PM (process Management) process management, MM (memory Management) RAM management, Arch,
VFS (virtual file systerm) virtualized file system, NS (network stack) networking protocol stack, DD (device Drivers) appliance driver


Linux Kernel source code

How does the Linux kernel source code compose or directory structure?
A: The arc directory holds some CPU-architecture-related code where the first CPU subdirectory is decomposed into subdirectories such as Boot,mm,kerner
Block Directory part blocks device driver code
Crypto directory encryption, compression, CRC check algorithm
Documentation Kernel Documentation
Drivers device Driver
FS holds implementation code for various file systems
The header file required by the include kernel. Platform-Independent header files are in the Include/linux subdirectory, and the platform-related header files are placed in the appropriate subdirectories
Init kernel initialization code
Implementation code for IPC interprocess communication
Kernel Linux Most key core features are implemented in this directory (program scheduling, Process Control, modularity)
LIB Library file code
MM Platform-independent memory management, platform-related implementation code in the corresponding ARCH/CPU directory net various network protocols, note rather than drive
Example of samples kernel programming
Scripts configuring the Kernel script
Security SELinux Module
Drivers for sound Audio devices
USR CPIP Command Implementation program
Virt Kernel virtual machine


Kernel Configuration and compilation
First, clear
Make clean deletes the compiled file but retains the configuration file
Make mrproper Delete all compiled files and configuration files
Make Distclean Delete compiled files, configuration files including backup backups and patch patches
Second, the kernel configuration mode
Make config interactive configuration based on text mode
Make menuconfig menu configuration based on text mode
Make Oldconfig uses an existing configuration file (. config), but is configured to ask for new configuration options
Make Xconfig graphical configuration

Third, make menuconfig some instructions or tips
Press "Y" in parentheses to compile into the kernel, press "M" to compile as module, press "n" not select, or press SPACEBAR to select
Note: When the kernel compiles, the "Y" compiled into the kernel, and the "M" compiled into the module are compiled in step

Iv. Configuring the kernel configuration of the appropriate architecture quickly
We can copy the corresponding processor model configuration file to the kernel source directory under the arch/$cpu/configs directory to replace the. config file

Five, compile the kernel
1.
————————————————————————————
Make Zimage Note: Zimage can only compile kernels that are less than 512k
Make Bzimage
Also we can compile the compile information, you can use the
Make Zimage V=1
Make Bzimage V=1
The compiled kernel is located in the arch/$cpu/boot/directory
————————————————————————————


The above is compiled kernel make menuconfig when the "m" option is compiled next to the compilation "Y" module, that is, the compilation module
2.
make modules compile kernel modules
Make Modules_install install kernel module------> This option is to copy the compiled kernel module from the kernel source code directory to/lib/modules

VI. Making init RAMDisk
MKINITRD initrd-$version $version
/**** mkinitrd initrd-$ (can be changed) version $version (cannot be changed, because this version is to look for/lib/modules/under the corresponding directory to make) ****/

VII. Kernel installation
Copy the kernel to the relevant directory and make grub boot.
1.CP arch/$cpu/boot/bzimage/boot/vmlinux-$version
2.CP $INITRD/boot/
3. Modify the bootloader/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 ___________________
First, required module functions
1. Load function Module_init (hello_init); By Module_init macro to specify
2. Unload function Module_exit (hello_exit); By MODULE_EXIT macro to specify

Compile modules use makefile more

Second, optional module function
1.module_license ("*******"); License Statement
2.module_author ("********"); Author's statement
3.modele_description ("* * *"); Module description
4.module_version ("V1.0"); Module version
5.module_alias ("*********"); Module aliases

Three, module parameters
Specify module parameters via macro Module_param, module parameters for passing parameter modules when loading modules
Module_param (Neme,type,perm);
Name is the module parameter name
Type is a common value for parameter types type: boot, int, charp (string type)
Perm is the parameter access permission perm common values: S_irugo, S_IWUSR
S_irugo: Any user has read access to the parameters that appear in the Sys/module
S_IWUSR: Allows root user to modify parameters that appear 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 M-value multi-use obj-(config_**) instead

Else

Kdir: =/lib/modules/$version/build
All
Make-c $ (Kdir) m=$ (PWD) modules
Clean
Rm-f *.ko *.o *.mod.o *.mod.c *.symyers
endif

/***** can extend multiple files makefile more obj-m***********end***************/here


/****** Module Parameter *****/
#include <linux/init.h>
#include <linux/module.h>
Module_license ("GPL");

static char *name = "Junroc Jinx";
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 an export?
A: The export is the module depends on the symbol into the kernel, in order to supply other modules call
Why export?
A: You cannot resolve a dependency without exporting it, and the import fails

Symbol Export Usage Instructions:
Export_symbol (symbol name)
EXPORT_SYMBOL_GPL (symbol name)
Where EXPORT_SYMBOL_GPL can only be used for modules that contain GPL licenses

Resolution of the module version mismatch problem:
1, using modprobe--force-modversion forcibly inserted
2. When you compile kernel modules, the kernel code version you rely on is equivalent to the kernel that is currently running UNAME-R
----------------------------------------------------------------------
PRINTK Kernel Print:
PRINTK allows the message to be categorized by attaching different "priorities" depending on the severity
There are 8 levels of records defined in <linux/kernel.h>. Decreasing by priority is:
Kern_emerg "<0>" for urgent messages, often crashes before the message
Kern_alert "<1>" need immediate action message
Kern_crit "<2>" serious situation
Kern_err "<3>" error condition
Kern_warning "<4>" a problematic warning
Kern_notice "<5>" normal, but still worth noting
Kern_info "<6>" informational message
Kern_debug "<7>" for debugging messages

PRINTK with no priority specified is used by default
Default_message_loglevel priority It is an integer defined in the KERNEL/PRINTK.C

Control the configuration of the priority level:
/PROC/SYS/KERNEL/PRINTK (can be viewed or modified)

/******* symbol symbols Each module relies on the example *****/

--------/********hello.c*********/----
#include <linux/module.h>
#include <linux/init.h>
Module_license ("GPL");
Module_author ("Junroc Jinx");
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 (n);
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****/

Linux, kernel source, kernel compilation and configuration, kernel module development, kernel boot process (RPM)

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.