当前位置:网站首页>The kernel of the decompression process steps
The kernel of the decompression process steps
2022-08-01 01:19:00 【The snail is taking off】
1、Differences between various kernel images
2、Why does the kernel need to decompress
(1)Decompression is required because compression was done when making the kernel image,The compressed image cannot be directly executed,内核镜像 = uncompressed header + 压缩的内核;
(2)The main function of the uncompressed header is to decompress the compressed kernel image and place it at a suitable address in memory,Then call the decompressed kernel;
(3)The reason for compressing the kernel is that the uncompressed kernel is too large,Compression saves storage space.
(4)The cost of saving space is that the kernel boot process is more complicated,Decompression also causes the kernel to take longer to boot.In today's more complex embedded devices,flashThere are dozens of them at every turnG,The few megabytes of space saved by compressing the kernel don't really matter,
So personally I think the compression kernel is rightflashSmall equipment is cost-effective,对flashLarge equipment is not cost-effective,但是为了统一,All are compressed.
3、ubootPreliminary work to start the kernel
(1)Relocate the kernel image into memory,Parse the header of the image,Find the entry address of the kernel image;
(2)Get the machine code ready,tagparameter address ordtb数据的地址;
(3)call kernel entry,Pass the data prepared in the previous step to the kernel;
4、Unzip the files involved
(1)以armTake the chip of the architecture as an example:"arch/arm/boot/comperssed/"目录;
(2)Analyze the program entry of the mirror and the composition structure of the mirror,查看vmlinux.lds;
(3)The entire mirror program entry is inhead.S文件中;
5、Confirm the storage address of the decompressed kernel
5.1、汇编代码
#ifdef CONFIG_AUTO_ZRELADDR
mov r4, pc //当前PC的值保存到r4寄存器
and r4, r4, #0xf8000000 //只保留高5位
/* Determine final kernel image address. */
add r4, r4, #TEXT_OFFSET //加上偏移量#TEXT_OFFSET
#else
ldr r4, =zreladdr //Specify the unpacked kernel address in the configuration file
#endif
5.2、CONFIG_AUTO_ZRELADDR宏
(1)The function of the above assembly code is to calculate the storage address of the decompressed kernel,然后保存到r4寄存器中;
(2)定义了CONFIG_AUTO_ZRELADDRThe macro representation is based on the currentPCThe value of , calculates the address where the kernel will be stored after decompression,然后存放到r4寄存器;
(3)如果没有定义CONFIG_AUTO_ZRELADDR宏,则把zreladdr变量的值赋值给r4寄存器;
5.3、TEXT_OFFSET宏
(1)The offset where the kernel code is stored relative to the memory start,通常设为 32k (0x8000);
(2)TEXT_OFFSET宏分析"arch/arm/boot/comperssed/Makefile"和"arch/arm/Makefile";
5.4、zreladdr变量
//arch/arm/boot/comperssed/Makefile
ifneq ($(CONFIG_AUTO_ZRELADDR),y)
LDFLAGS_vmlinux += --defsym zreladdr=$(ZRELADDR)
endif
//arch/arm/boot/Makefile
ZRELADDR := $(zreladdr-y)
//arch/arm/Mach-xxx/ Makefile.boot
在这里指定zreladdr-y的值
The address stored by the kernel after decompression,It is also the starting address of the kernel after the final decompression,可以在配置文件中指定;
6、Kernel decompression function
6.1、汇编代码部分
* The C runtime environment should now be setup sufficiently.
* Set up some pointers, and start decompressing.
* r4 = kernel execution address
* r7 = architecture ID
* r8 = atags pointer
*/
mov r0, r4 //内核解压后存放的地址
mov r1, sp @ malloc space above stack
add r2, sp, #0x10000 @ 64k max
mov r3, r7 //architecture ID:机器码
bl decompress_kernel //执行解压zImage为elf格式的可执行kernel镜像
bl cache_clean_flush
bl cache_off
mov r1, r7 @ restore architecture number
mov r2, r8 @ restore atags pointer
//函数原型
void decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p, \
unsigned long free_mem_ptr_end_p,
int arch_id)
decompress_kernel函数传参 | 含义 |
---|---|
output_start | The starting memory address where the data is stored after decompression by the kernel |
free_mem_ptr_p | Available memory starting address |
free_mem_ptr_end_p | The address of the end of available memory |
arch_id | 机器码 |
(1)r4Before this, the memory address stored by the kernel after decompression has been stored,这里将r4寄存器的值赋值给r0;
(2)sp是栈寄存器,r1The value of the register is the current stack address,r2The value of the register is the current stack address+64KB,The effect is to allocate on the stack64KBmemory space to pass todecompress_kernel()函数;
(3)r3registers are assignedr7寄存器的值,是机器码;
(4)decompress_kernel()函数的4Each parameter corresponds respectivelyr0-r3四个寄存器;
6.2、decompress_kernel()函数源码分析
//arch/arm/boot/compressed/piggy.S
.section .piggydata,#alloc
.globl input_data
input_data:
.incbin "arch/arm/boot/compressed/piggy_data"
.globl input_data_end
input_data_end:
void decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
unsigned long free_mem_ptr_end_p, int arch_id)
{
int ret;
__stack_chk_guard_setup();
//Assign values to some global variables
output_data = (unsigned char *)output_start; //The memory address stored by the kernel after decompression
free_mem_ptr = free_mem_ptr_p; //The starting address of available memory
free_mem_end_ptr = free_mem_ptr_end_p; //可用内存的结束地址
__machine_arch_type = arch_id; //机器码
//Decompression module initialization
arch_decomp_setup();
putstr("Uncompressing Linux...");
//(The memory address where the compressed kernel image is located, Compressed kernel image size, The storage address of the decompressed image, Error handling function pointer)
ret = do_decompress(input_data, input_data_end - input_data,
output_data, error);
if (ret)
error("decompressor returned an error");
else
putstr(" done, booting the kernel.\n");
}
(1)do_decompress()The function is the function that actually does the decompression,Only the address and length of the compressed kernel image are required、Decompress the kernel storage address and other information and pass it in,Inside the function, the function corresponding to the decompression method will be called according to the configuration item,The principle is to use the corresponding decompression method for whatever compression method is used;
(2)The start and end addresses where the compressed kernel image is located are stored in input_data、input_data_end变量中,这在piggy.S文件中指定,Specifically, the link script can be analyzed;
(3)piggy_dataIt is the compressed kernel image,The general process of generation:vmlinux->Image->piggy_data->zImage->zImage-dtb;
7、Invoke the unpacked kernel
b __enter_kernel
__enter_kernel:
mov r0, #0 @ must be 0
ARM( mov pc, r4 ) @ call kernel Call the decompressed kernel image
M_CLASS( add r4, r4, #1 ) @ enter in Thumb mode for M class
THUMB( bx r4 ) @ entry point is always ARM for A/R classes
During the kernel decompression stage,The decompressed kernel has been put inr4The memory address where the register executes,这里将r4寄存器的值赋值给PC寄存器,下面就是执行r4寄存器指向的内存地址,That is, the first sentence of the kernel code.
边栏推荐
- Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
- You need to know the TCP wave four times
- Inheritance and friend, static member relationship
- STK8321 I2C(昇佳-加速度传感器)示例
- MYSQL索引解析
- Introduction to the five data types of Redis
- Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
- 500 miles
- RTL8762DK RTC (5)
- RTL8762DK uses DebugAnalyzer (four)
猜你喜欢
ECCV2022 Workshop | 复杂环境中的多目标跟踪和分割
Basic implementation of vector
从零造键盘的键盘超级喜欢,IT人最爱
现代企业架构框架1
Blueprint: Yang Hui's Triangular Arrangement
ROS2系列知识(4): 理解【服务】的概念
Google engineer fired for claiming AI awareness: breach of nondisclosure agreement
MYSQL master-slave replication
leetcode:1648. 销售价值减少的颜色球【二分找边界】
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
随机推荐
device node结构体转换成platform_device结构体
You need to know the TCP wave four times
Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
VPGNet
七月集训(第31天) —— 状态压缩
修改Postman安装路径
设备树的树形结构到底是怎样体现的?
MYSQL关键字Explain解析
数据中台建设(七):数据资产管理
VPGNet
RTL8762DK UART (two)
MYSQL Classic Interview Questions
MYSQL索引解析
彻底关闭Chrome浏览器更新及右上角的更新提示
Cmake introductory study notes
MVCC总结
Euler system (euleros): upgrade Mysql
Unity3D学习笔记10——纹理数组
手写二叉查找树及测试
北京突然宣布,元宇宙重大消息