Linux Kernel Startup Process -- zimage self-extracting

Source: Internet
Author: User

 

Linux Kernel Startup Process -- self-Extracting Based on S3C2410 (1) zimage

Reprinted from: http://blog.csdn.net/y296144646q/article/details/5683004

Linux Kernel startup process-based on S3C2410
(1) zimage self-extracting

This paper takes the popular Samsung S3C2410, mini2440 platform and linux-2.6.29 as an example to introduce how to explore the Linux kernel Startup Process in the zix embedded development environment.

Linux Kernel boot is usually guided by external bootloader. You can also embed a loader in the kernel header. In actual applications, these two methods are often used. Therefore, to understand the initial process of kernel startup, you must be familiar with how bootloader guides the kernel. Next we will analyze the code for loading the Linux kernel from U-boot (for the Startup Process of U-boot, referU-boot
Startup Process-Based on S3C2410
).

1. Code loading for the processor Kernel

In the U-bootDo_bootm_linux FunctionsLinux Kernel Code related to the processor architecture, especially the tags transfer.

 

In the (u-boot-1.6) lib_arm/armlinux. c90 rowsCall getenv to save the bootargs environment variable in CommandLine

Char * CommandLine =Getenv
("Bootargs ");

Parse the uimage file header, and break down and load the uimage according to the definition in the header. Therefore, the running of this part of Code depends on how the uimage file is generated. This article does not describe much about it. You can refer to another article to learn about the usage of U-boot. Next, set the tags and call

  • Setup_start_tag ()
  • Setup_memory_tag ()
  • Setup_commandline_tag ()
  • Setup_initrd_tag ()
  • Setup_end_tag ()

Then, perform invalid operations on TLB and cache.268 rowsCallCleanup_before_linux ()(CPU/ARM920T/108), and then you can jump to the kernel image or zimage entry decomposed from uimage.

Cleanup_before_linux ();

Thekernel (0, machid, BD-> bi_boot_params );

/* Does not return */

Return;

On the S3C2410 platform, Thekernel is generally a physical address of 0x30008000. If we use zimage to decompress the kernel image, the corresponding code is exactly the self-resolving pressure head, the position in the kernel source code linux-2.6.29 ARCH/ARM/boot/compressed/head. s 114th line start symbol

Start:

. Type start, # Function

. Rept 8

MoV r0, R0

. Endr

 

B 1f

. Word 0x016f2818 @ magic numbers to help the loader

. Word start @ absolute load/run zimage address

. Word _ edata @ zimage end address

1: mov R7, R1 @ save architecture ID

MoV R8, R2 @ save atags pointer

This also marksU-boot has completely handed over the system to the OS, and the bootloader is terminated.. Then the Code reads CPSR In the 133 line and determines whether the processor is in supervisor mode. The system enters the kernel mode from U-boot, and the system is in svc32 mode, two additional commands are required. Then, confirm that the interrupt is disabled again and complete CPSR writing.

Mrs R2, CPSR @ get Current Mode

Tst R2, #3 @ not user?

BNE not_angel

MoV r0, #0x17 @ angel_swireason_entersvc

SWI 0x123456 @ angel_swi_arm

Not_angel:

Mrs R2, CPSR @ turn off interrupts

ORR R2, R2, #0xc0 @ prevent angel from running

MSR cpsr_c, r2

Then import the segment information at lc0 address (row 157) to registers such as r0-r6, IP, SP, and check if the code is running at the same destination address (row 162) as the link ), to determine whether to process the data. Because few people do not use loader or tags, zimage is directly written to Rom from 0x0, therefore, this processing is required (but the zimage header now retains the ability to start without loader ). In the ARM architecture, the self-resolving pressure head is generally linked to the 0x0 address and loaded to 0 x for operation. Therefore, you need to modify this change. Involved

  • Zimage base address stored in R5 register
  • Got (Global Offset Table) stored in R6 and R12 (IP register)
  • Start and end addresses of BSS segments stored in R2 and r3
  • SP Stack pointer address

Simply put, these registers are all added with an offset address 0x30008000 that you can guess. This address is related to S3C2410. For other arm processors, see the following table.

  • Pxa2xx is 0xa0008000
  • Ixp2x00 and ixp4xx are 0x00008000
  • Freescale I. mx31/37 is 0x80008000
  • Ti DaVinci dm64xx is 0x80008000
  • The Ti OMAP series are 0x80008000
  • At91rm/sam92xx series is 0x20008000
  • Cirrus ep93xx is 0x00008000

These operations occur at the beginning of line 172, and only part of them is pasted below.

Add R5, R5, R0

Add R6, R6, R0

Add IP, IP, R0

The following code clears BSS segments in Row 3.

Not_relocated: mov r0, #0

1: Str r0, [R2], #4 @ clear BSS

STR r0, [R2], #4

STR r0, [R2], #4

STR r0, [R2], #4

CMP R2, R3

BlO 1b

Then, in Row 3, open the cache and decompress the package to set a 64kb temporary malloc space.

BL cache_on

 

MoV R1, Sp @ malloc space abve Stack

Add R2, SP, #0x10000 @ 64 K max

Next, check Row 3 to determine whether the destination image address after kernel decompression will overwrite the zimage header. If so, transfer the zimage header to the back of the extracted kernel.

CMP R4, r2

BHS wont_overwrite

Sub R3, SP, R5 @> compressed kernel size

Add r0, R4, R3, LSL #2 @ allow for 4x Expansion

CMP r0, R5

BLS wont_overwrite

 

MoV R5, R2 @ decompress after malloc Space

MoV r0, R5

MoV R3, r7

BL decompress_kernel

Real situation-in most applications, kernel compilation links compressed zimage and non-compressed image to the same address. On the S3C2410 platform, this is 0x30008000. The advantage of doing so is that people don't have to worry about whether the kernel is an image or zimage, and they can execute it at this location. Therefore, after decompression, the zimage header must make way for the real kernel.

After decompression is completed in Row 3, the kernel length return value is stored in the R0 register. 128 bytes of stack space is reserved at the end of the kernel, and the length is 128 bytes aligned.

Add r0, R0, #127 + 128 @ alignment + Stack

Bic r0, R0, #127 @ align the kernel Length

Calculate the parameters of the Code to be moved: the address at the end of the computing kernel is stored in the R1 register. You need to move the original code address to R2 and the length of the Code to be moved to R3. Then execute the migration and set the SP pointer to point to the new stack (the original stack will also be overwritten by the kernel)

Add R1, R5, R0 @ end of decompressed Kernel

ADR R2, reloc_start

LDR R3, LC1

Add R3, R2, R3

1: ldmia R2 !, {R9-R14} @ copy relocation code

Stmia R1 !, {R9-R14}

Ldmia R2 !, {R9-R14}

Stmia R1 !, {R9-R14}

 

Note: For uncompressed kernel, it is based on ARCH/ARM/boot/head. s. for the code of the kernel decompression part, go to arch/ARM/boot/compressed/head. in S, the zimage is self-decompressed and then the kernel is extracted from arch/ARM/boot/head. started in S, which is not discussed in this article.

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.