調用 start_kernel

步驟 1

關閉中斷、進入 SVC 模式linux

ENTRY(stext)
	 THUMB(	adr	r9, BSYM(1f)	)	@ Kernel is always entered in ARM.
	 THUMB(	bx	r9		)	@ If this is a Thumb-2 kernel,
	 THUMB(	.thumb			)	@ switch to Thumb now.
	 THUMB(1:			)
		setmode	PSR_F_BIT | PSR_I_BIT | SVC_MODE, r9 @ 關中斷、進入 SVC 模式

步驟 2

查找指定處理器類型的 proc_infoapp

mrc	p15, 0, r9, c0, c0		@ 取出處理器 ID 放入寄存器 r9 中
	bl	__lookup_processor_type		@ 查找處理器類型 r5=procinfo r9=cpuid
	|
	|-->/* 找到匹配 proc_info 則返回,不然將 r5 清零 */
	|		__CPUINIT
	|	__lookup_processor_type:
	|		adr	r3, __lookup_processor_type_data
	|			|
	|			|-->.align	2
	|			|		.type	__lookup_processor_type_data, %object
	|			|	__lookup_processor_type_data:
	|			|		.long	.
	|			|		.long	__proc_info_begin
	|			|		.long	__proc_info_end
	|			|		.size	__lookup_processor_type_data, . - __lookup_processor_type_data
	|		ldmia	r3, {r4 - r6}	@ r4=當前數據地址、r5=處理器數據起始地址、r6=結束地址
	|		sub	r3, r3, r4			@ 計算出運行地址和連接地址間的偏移
	|		add	r5, r5, r3			@ 修正 r5
	|		add	r6, r6, r3			@ 修正 r6
	|	1:	ldmia	r5, {r3, r4}
	|		and	r4, r4, r9
	|		teq	r3, r4
	|		beq	2f					@ 若是相等則匹配成功
	|		add	r5, r5, #PROC_INFO_SZ	@ 開始指向下一個處理器數據
	|		cmp	r5, r6
	|		blo	1b					@ 若是還有數據則循環查找
	|		mov	r5, #0				@ 未找到時將 r5 清零
	|	2:	mov	pc, lr				@ 返回
	|	ENDPROC(__lookup_processor_type)
	
	movs	r10, r5			@ 使用 r5 改變標誌位
	THUMB( it	eq )
	beq	__error_p			@ 若是相等則沒找到
	
#ifndef CONFIG_XIP_KERNEL
	adr	r3, 2f				@ r3=運行地址
	ldmia	r3, {r4, r8}	@ r4=連接地址(虛擬地址)、r8=頁偏移
	sub	r4, r3, r4			@ 運行地址與連接地址間的差值
	/*
	 * 內核被解壓到 物理地址+text_offset 處,即 0x40008000,也是當前的運行地址
	 * 而內核在編譯時被連接到 page_offset+text_offset 處,即 0xc0008000
	 * 所以 r4=r3-r4 記錄的是內核實際存放的物理地址和運行時的虛擬地址間的偏移
	 * 即 r4=phys-page_offset
	 * 因此 r8 = r4+r8 = phys-page_offset+page_offset = phys,即物理地址的起始地址
	 */
	add	r8, r8, r4			@ 物理地址的起始地址
#else
	ldr	r8, =PHYS_OFFSET	@ always constant in this case
#endif

#ifndef CONFIG_XIP_KERNEL
2:	.long	.
	.long	PAGE_OFFSET
#endif

步驟 3

檢查 bootloader 傳遞的啓動參數是否有效函數

/*
 * r1 = machine no, r2 = atags or dtb,
 * r8 = phys_offset, r9 = cpuid, r10 = procinfo
 */
bl	__vet_atags
	|
	+-->/* Returns:
	|	 *  r2 either valid atags pointer, valid dtb pointer, or zero
	|	 *  r5, r6 corrupted
	|	 */
	|	__vet_atags:
	|		tst	r2, #0x3			@ 判斷 atags 是否 4 字節對齊
	|		bne	1f
	|
	|		ldr	r5, [r2, #0]
	|	#ifdef CONFIG_OF_FLATTREE	@ 配置此項時支持設備樹
	|		ldr	r6, =OF_DT_MAGIC	@ 判斷是不是 DTB 數據
	|		cmp	r5, r6
	|		beq	2f
	|	#endif
	|		cmp	r5, #ATAG_CORE_SIZE		@ 判斷第一個 atags 參數的大小是不是與 ATAG_CORE 相同
	|		cmpne	r5, #ATAG_CORE_SIZE_EMPTY
	|		bne	1f
	|		ldr	r5, [r2, #4]
	|		ldr	r6, =ATAG_CORE			@ 再判斷該參數是否是 ATAG_CORE 節點
	|		cmp	r5, r6
	|		bne	1f
	|
	|	2:	mov	pc, lr				@ 所傳遞參數合法,正常返回
	|
	|	1:	mov	r2, #0
	|		mov	pc, lr
	|	ENDPROC(__vet_atags)

步驟 4

當前內核鏡像在內存中的佈局佈局

// 物理內存中的佈局
       _____________________________________________
      |      |          |                          |
      |      |          |                          |
      |      | 段描述符 |       kernel image       |
      |      |          |                          |
      |______|__________|__________________________|
0x4000_0000      0x4000_8000

// 虛擬內存中的佈局
       _____________________________________________
      |      |          |                          |
      |      |          |                          |
      |      | 段描述符 |       kernel image       |
      |      |          |                          |
      |______|__________|__________________________|
0xc000_0000      0xc000_8000

內核創建內核空間臨時的線性映射,採用一級映射,也就是 section 模式,每一個section 爲 1MB.ui

#ifdef CONFIG_SMP_ON_UP
	bl	__fixup_smp			@ 自旋鎖在 SMP 和 UP 上的相關修正
							@ arch/arm/include/asm::ALT_SMP
#endif
#ifdef CONFIG_ARM_PATCH_PHYS_VIRT
	bl	__fixup_pv_table	@ 物理地址和虛擬地址間的偏移修正等
							@ arch/arm/include/asm::pv_stub
#endif
	bl	__create_page_tables
	|	
	+-->/* r8 = phys_offset, r9 = cpuid, r10 = procinfo
	|	 *
	|	 * Returns:
	|	 *  r0, r3, r5-r7 corrupted
	|	 *  r4 = physical page table address
	|	 */
	|	__create_page_tables:
	|		pgtbl	r4, r8				@ 將頁表起始物理地址放入 r4 中
	|			|
	|			+-->.macro	pgtbl, rd, phys
	|			|	add	\rd, \phys, #TEXT_OFFSET - PG_DIR_SIZE
	|			|	.endm
	|
	|		@ 對頁表區域進行清零
	|		mov	r0, r4
	|		mov	r3, #0
	|		add	r6, r0, #PG_DIR_SIZE
	|	1:	str	r3, [r0], #4
	|		str	r3, [r0], #4
	|		str	r3, [r0], #4
	|		str	r3, [r0], #4
	|		teq	r0, r6
	|		bne	1b
	|
	|		ldr	r7, [r10, #PROCINFO_MM_MMUFLAGS] @ mm_mmuflags
	|
	|		@ 建立臨時的線性映射
	|		@ 頁表項格式:一級頁表入口值[31:20]  MMUFLAGS[19:0]
	|		adr	r0, __turn_mmu_on_loc
	|		ldmia	r0, {r3, r5, r6}@ 獲得函數的物理地址
	|		sub	r0, r0, r3			@ virt->phys offset
	|		add	r5, r5, r0			@ phys __turn_mmu_on
	|		add	r6, r6, r0			@ phys __turn_mmu_on_end
	|		mov	r5, r5, lsr #SECTION_SHIFT @ 獲得一級頁表入口值
	|		mov	r6, r6, lsr #SECTION_SHIFT
	|
	|	1:	orr	r3, r7, r5, lsl #SECTION_SHIFT	@ 一級段描述符
	|		str	r3, [r4, r5, lsl #PMD_ORDER]	@ 將 r3 中存放的段描述符放入對應的物理地址中
	|		cmp	r5, r6
	|		addlo	r5, r5, #1			@ 下一個段描述符
	|		blo	1b
	|
	|		@ 設置映射頁表
	|		mov	r3, pc
	|		mov	r3, r3, lsr #SECTION_SHIFT @ 獲得當前執行程序的段描述符編號
	|		orr	r3, r7, r3, lsl #SECTION_SHIFT @ 合成段描述符
	|		@ kernel_start=0xc000_8000, section_shift=20, pmd_order=2
	|		@ 如下兩行實際上是在計算段描述符的入口地址
	|		@ 由於要回寫到 r0 中,所以拆分來寫的
	|		add	r0, r4,  #(KERNEL_START & 0xff000000) >> (SECTION_SHIFT - PMD_ORDER)
	|		str	r3, [r0, #((KERNEL_START & 0x00f00000) >> SECTION_SHIFT) << PMD_ORDER]!
	|		ldr	r6, =(KERNEL_END - 1) @ 內核(包括數據段)的最後一個字節位置
	|		add	r0, r0, #1 << PMD_ORDER @ 下一個段描述符存放的物理地址
	|		add	r6, r4, r6, lsr #(SECTION_SHIFT - PMD_ORDER) @ 內核須要的最後一個段描述符存放的物理地址
	|	1:	cmp	r0, r6
	|		@ 內核對自身進行了線性映射,將自身物理內存所在段直接放入頁表中
	|		add	r3, r3, #1 << SECTION_SHIFT @ 下一個段描述符,只須要增長段基址便可
	|		strls	r3, [r0], #1 << PMD_ORDER @ 寫入到物理內存對應的頁表中
	|		bls	1b
	|
	|		@ 將 atags 所在段寫到頁表中
	|		mov	r0, r2, lsr #SECTION_SHIFT @ atags 段編號
	|		movs	r0, r0, lsl #SECTION_SHIFT @ 若是 r0 爲零則賦值爲 r8,即沒有指定 atags 的狀況
	|		moveq	r0, r8
	|		sub	r3, r0, r8 @ 段內偏移量
	|		add	r3, r3, #PAGE_OFFSET @ 轉化成虛擬地址
	|		add	r3, r4, r3, lsr #(SECTION_SHIFT - PMD_ORDER) @ 獲得該段描述符存放的物理地址
	|		orr	r6, r7, r0 @ 合成段描述
	|		str	r6, [r3]   @ 寫入物理內存中
	|
	|		mov	pc, lr
	|		ENDPROC(__create_page_tables)
	
	/*
	 * r10 = base of xxx_proc_info structure selected by __lookup_processor_type
	 * On return, the CPU will be ready for the MMU to be turned on, 
	 * r0 = CPU control register value.
	 */
	 /*
	  * 如下代碼流程 
	  * 1. 設置v7核心,主要涉及SMP,準備MMU硬件配置,I/D cache,TLB,涉及協處理的配置
	  * --> arch/arm/mm/proc-v7.S::__v7_setup
	  * 2. 配置MMU,設置內存訪問權限,並激活MMU
	  * --> arch/arm/kernel/head.S::__enable_mmu
	  * 3. 將數據段複製到內存中,清理bss段,將processor ID,machine ID,atags 指針保存到指定變量中
	  * --> arch/arm/kernel/head-common.S::__mmap_switched
	  * 4. __mmap_switched 最後進入C語言函數start_kernel,至此終於走出了彙編代碼,進入C語言的天堂
	  * --> init/main.c::start_kernel
	  */
	@ 由於跳轉到該函數時,MMU已激活,故這裏使用的是虛擬地址,而不是物理地址
	ldr	r13, =__mmap_switched	@ address to jump to after
								@ mmu has been enabled
	adr	lr, BSYM(1f)			@ return (PIC) address
	mov	r8, r4					@ set TTBR1 to swapper_pg_dir
	ARM(	add	pc, r10, #PROCINFO_INITFUNC	)
	THUMB(	add	r12, r10, #PROCINFO_INITFUNC	)
	THUMB(	mov	pc, r12				)
1:	b	__enable_mmu

關鍵宏定義

::arch/arm/kernel/vmlinux.ld.S
. = PAGE_OFFSET + TEXT_OFFSET
::arcm/arm/kernel/head.S
/*
 * swapper_pg_dir is the virtual address of the initial page table.
 * We place the page tables 16K below KERNEL_RAM_VADDR.  Therefore, we must
 * make sure that KERNEL_RAM_VADDR is correctly set.  Currently, we expect
 * the least significant 16 bits to be 0x8000, but we could probably
 * relax this restriction to KERNEL_RAM_VADDR >= PAGE_OFFSET + 0x4000.
 */
#define KERNEL_RAM_VADDR	(PAGE_OFFSET + TEXT_OFFSET)
#if (KERNEL_RAM_VADDR & 0xffff) != 0x8000
#error KERNEL_RAM_VADDR must start at 0xXXXX8000
#endif

#ifdef CONFIG_ARM_LPAE
	/* LPAE requires an additional page for the PGD */
#define PG_DIR_SIZE	0x5000
#define PMD_ORDER	3
#else
#define PG_DIR_SIZE	0x4000
#define PMD_ORDER	2
#endif

	.globl	swapper_pg_dir
	.equ	swapper_pg_dir, KERNEL_RAM_VADDR - PG_DIR_SIZE

	.macro	pgtbl, rd, phys
	add	\rd, \phys, #TEXT_OFFSET - PG_DIR_SIZE
	.endm

#ifdef CONFIG_XIP_KERNEL
#define KERNEL_START	XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)
#define KERNEL_END	_edata_loc
#else
#define KERNEL_START	KERNEL_RAM_VADDR
#define KERNEL_END	_end
#endif
相關文章
相關標籤/搜索