1. 核心啟動地址
1.1. 名詞解釋
ZTEXTADDR
解壓程式碼執行的開始地址。沒有實體地址和虛擬地址之分,因為此時MMU處於關閉狀態。這個地址不一定時RAM的地址,可以是支援讀寫定址的flash等儲存中介。
Start address of decompressor. here's no point in talking about virtual or physical addresses here, since the MMU will be off at the time when you call the decompressor code. You normally call the kernel at this address to start it booting. This doesn't have to be located in RAM, it can be in flash or other read-only or read-write addressable medium.
ZRELADDR
核心啟動在RAM中的地址。壓縮的核心映像被解壓到這個地址,然後執行。
This is the address where the decompressed kernel will be written, and eventually executed. The following constraint must be valid:
__virt_to_phys(TEXTADDR) == ZRELADDR
The initial part of the kernel is carefully coded to be position independent.
TEXTADDR
核心啟動的虛擬地址,與ZRELADDR相對應。一般核心啟動的虛擬地址為RAM的第一個bank地址加上0x8000。
TEXTADDR = PAGE_OFFSET + TEXTOFFST
Virtual start address of kernel, normally PAGE_OFFSET + 0x8000.This is where the kernel image ends up. With the latest kernels, it must be located at 32768 bytes into a 128MB region. Previous kernels placed a restriction of 256MB here.
TEXTOFFSET
核心偏移地址。在arch/arm/makefile中設定。
PHYS_OFFSET
RAM第一個bank的物理起始地址。
Physical start address of the first bank of RAM.
PAGE_OFFSET
RAM第一個bank的虛擬起始地址。
Virtual start address of the first bank of RAM. During the kernel
boot phase, virtual address PAGE_OFFSET will be mapped to physical
address PHYS_OFFSET, along with any other mappings you supply.
This should be the same value as TASK_SIZE.
1.2. 核心啟動地址確定
核心啟動引導地址由bootp.lds決定。 Bootp.lds : arch/arm/bootp
OUTPUT_ARCH(arm)
ENTRY(_start)
SECTIONS
{
. = 0;
.text : {
_stext = .;
*(.start)
*(.text)
initrd_size = initrd_end - initrd_start;
_etext = .;
}
}
由上 .= 0可以確定解壓程式碼執行的開始地址在0x0的位置。ZTEXTADDR的值決定了這個值得選取。
Makefile : arch/arm/boot/compressed
如果設定核心從ROM中啟動的話,可以在make menuconfig 的配置介面中設定解壓程式碼的起始地址,否則解壓程式碼的起始地址為0x0。實際上,預設從ROM啟動時,解壓程式碼的起始地址也是0x0。
feq ($(CONFIG_ZBOOT_ROM),y)
ZTEXTADDR := $(CONFIG_ZBOOT_ROM_TEXT)
ZBSSADDR := $(CONFIG_ZBOOT_ROM_BSS)
else
ZTEXTADDR :=0 ZBSSADDR := ALIGN(4)
endif
SEDFLAGS = s/TEXT_START/$(ZTEXTADDR)/;s/BSS_START/$(ZBSSADDR)/
……
$(obj)/vmlinux.lds: $(obj)/vmlinux.lds.in arch/arm/mach-s3c2410/Makefile .config
@sed "$(SEDFLAGS)" < $< > $@
@sed "$(SEDFLAGS)" < $< > $@ 規則將TEXT_START設定為ZTEXTADDR。TEXT_START在arch/arm/boot/compressed/vmlinux.lds.in 中被用來設定解壓程式碼的起始地址。
OUTPUT_ARCH(arm)
ENTRY(_start)
SECTIONS
{
. = TEXT_START;
_text = .;
.text : {
_start = .;
*(.start)
*(.text)
*(.text.*)
……
}
}
核心的編譯依靠vmlinux.lds,vmlinux.lds由vmlinux.lds.s 生成。從下面程式碼可以看出核心啟動的虛擬地址被設定為PAGE_OFFSET + TEXT_OFFSET,而核心啟動的實體地址ZRELADDR在arch/arm/boot/Makefile中設定。
OUTPUT_ARCH(arm)
ENTRY(stext)
SECTIONS
{
#ifdef CONFIG_XIP_KERNEL
. = XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR);
#else
. = PAGE_OFFSET + TEXT_OFFSET;
#endif
.init : { /* Init code and data */
_stext = .;
_sinittext = .;
*(.init.text)
_einittext = .;
……
}
}
# arch/arm/boot/Makefile
# Note: the following conditions must always be true:
# ZRELADDR == virt_to_phys(PAGE_OFFSET + TEXT_OFFSET)
# PARAMS_PHYS must be within 4MB of ZRELADDR
# INITRD_PHYS must be in RAM
ZRELADDR := $(zreladdr-y)
#---> zrealaddr-y is specified with 0x30008000 in arch/arm/boot/makefile.boot
PARAMS_PHYS := $(params_phys-y)
INITRD_PHYS := $(initrd_phys-y)
export ZRELADDR INITRD_PHYS PARAMS_PHYS
通過下面的命令編譯核心映像,由引數-a, -e設定其入口地址為ZRELADDR,此值在上面ZRELADDR := $(zreladdr-y)指定。
quiet_cmd_uimage= UIMAGE $@
cmd_uimage = $(CONFIG_SHELL) $(MKIMAGE) -A arm -O linux -T kernel \
-C none -a $(ZRELADDR) -e $(ZRELADDR) \
-n 'Linux-$(KERNELRELEASE)' -d $< $@
1.3. 小結
從上面分析可知道,linux核心被bootloader拷貝到RAM後,解壓程式碼從ZTEXTADDR開始執行(這段程式碼是與位置無關的PIC)。核心被解壓縮到ZREALADDR處,也就是核心啟動的實體地址處。相應地,核心啟動的虛擬地址被設定為TEXTADDR,滿足如下條件:
TEXTADDR = PAGE_OFFSET + TEXT_OFFSET
核心啟動的實體地址和虛擬地址滿足入下條件:
ZRELADDR == virt_to_phys(PAGE_OFFSET + TEXT_OFFSET)= virt_to_phys(TEXTADDR)
假定開發板為smdk2410,則有:
核心啟動的虛擬地址
TEXTADDR = 0xC0008000
核心啟動的實體地址
ZRELADDR = 0x30008000
如果直接從flash中啟動還需要設定ZTEXTADDR地址。
2. 核心啟動過程分析
核心啟動過程經過大體可以分為兩個階段:核心映像的自引導;linux核心子模組的初始化。
start
Decompress_kernel()
Call_kernel
Stext:
Prepare_namespace
Do_basic_setup
init
Rest_init
Setup_arch ……
Start_kernel
_enable_mmu
Execve(“/sbin/init”))
核心啟動流程圖
2.1. 核心映像的自引導
這階段的主要工作是實現壓縮核心的解壓和進入核心程式碼的入口。
Bootloader完成系統引導後,核心映像被調入記憶體指定的實體地址ZTEXTADDR。典型的核心映像由自載入程式和壓縮的VMlinux組成。因此在啟動核心之前需要先把核心解壓縮。核心映像的入口的第一條程式碼就是自載入程式。它在arch/arm/boot/compressed/head.S檔案中。
Head.S檔案主要功能是實現壓縮核心的解壓和跳轉到核心vmlinux核心的入口。Decompress_kernel(): arch/arm/boot/compressed/misc.c 和call_kernel這兩個函式實現了上述功能。在呼叫decompress_kernel()解壓核心之前,需要確保解壓後的核心程式碼不會覆蓋掉原來的核心映像。以及設定核心程式碼的入口地址ZREALADDR。
.text
adr r0, LC0
ldmia r0, {r1, r2, r3, r4, r5, r6, ip, sp}
.type LC0, #object
LC0: .word LC0 @ r1
.word __bss_start @ r2
.word _end @ r3
.word zreladdr @ r4
.word _start @ r5
.word _got_start @ r6
.word _got_end @ ip
.word user_stack+4096 @ sp
上面這段程式碼得到核心程式碼的入口地址,儲存在r4中。
/*
* Check to see if we will overwrite ourselves.
* r4 = final kernel address
* r5 = start of this image
* r2 = end of malloc space (and therefore this image)
* We basically want:
* r4 >= r2 -> OK
* r4 + image length <= r5 -> OK
*/
cmp r4, r2
bhs wont_overwrite
add r0, r4, #4096*1024 @ 4MB largest kernel size
cmp r0, r5
bls wont_overwrite
mov r5, r2 @ decompress after malloc space
mov r0, r5
mov r3, r7
bl decompress_kernel
b call_kernel
上面程式碼判斷解壓後的核心程式碼會不會覆蓋原來的核心映像,然後呼叫核心解壓縮函式decompress_kernel()。
ulg
decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
int arch_id)
{
output_data = (uch *)output_start; /* 指定核心執行地址,儲存在r4中*/
free_mem_ptr = free_mem_ptr_p;
free_mem_ptr_end = free_mem_ptr_end_p;
__machine_arch_type = arch_id;
arch_decomp_setup(); /*解壓縮前的初始化和設定,包括串列埠波特率設定等*/
makecrc(); /*CRC校驗*/
putstr("Uncompressing Linux...");
gunzip(); /*呼叫解壓縮函式*/
putstr(" done, booting the kernel.\n");
return output_ptr;
}