kernel的启动过程,head.S到main.c

kernel的启动过程,head.S到main.c:

head.S:

head.S主要做的事情是处理u-boot传入的参数,具体如下:

  1. 判断是否支持这个CPU
  2. 判断是否支持这个单板(对比机器ID
  3. 建立一级页表
  4. 使能MMU
  5. 跳到start_kernel函数

下面具体分析这个流程:

ENTRY(stext)
	setmode	PSR_F_BIT | PSR_I_BIT | SVC_MODE, r9 @ ensure svc mode
						@ and irqs disabled
	mrc	p15, 0, r9, c0, c0		@ get processor id
	bl	__lookup_processor_type		@ r5=procinfo r9=cpuid
	movs	r10, r5				@ invalid processor (r5=0)?
 THUMB( it	eq )		@ force fixup-able long branch encoding
	beq	__error_p			@ yes, error 'p'
	bl	__lookup_machine_type		@ r5=machinfo
	movs	r8, r5				@ invalid machine (r5=0)?
 THUMB( it	eq )		@ force fixup-able long branch encoding
	beq	__error_a			@ yes, error 'a'

	/*
	 * r1 = machine no, r2 = atags,
	 * r8 = machinfo, r9 = cpuid, r10 = procinfo
	 */
	bl	__vet_atags
#ifdef CONFIG_SMP_ON_UP
	bl	__fixup_smp
#endif
	bl	__create_page_tables

	/*
	 * The following calls CPU specific code in a position independent
	 * manner.  See arch/arm/mm/proc-*.S for details.  r10 = base of
	 * xxx_proc_info structure selected by __lookup_machine_type
	 * above.  On return, the CPU will be ready for the MMU to be
	 * turned on, and r0 will hold the CPU control register value.
	 */
	ldr	r13, =__mmap_switched		@ address to jump to after
						@ mmu has been enabled
	adr	lr, BSYM(1f)			@ return (PIC) address
 ARM(	add	pc, r10, #PROCINFO_INITFUNC	)
 THUMB(	add	r12, r10, #PROCINFO_INITFUNC	)
 THUMB(	mov	pc, r12				)
1:	b	__enable_mmu
ENDPROC(stext)

这是在2.6.38内核文件下arch\arm\kernel目录下的head.S文件,也是内核启动的第一个文件.

通过设置CPSR寄存器来确保处理器进入管理模式,并且禁止中断。

通过读取CP15寄存器C0获得CPUid 

调用__lookup_processor_type这个函数确认内核是否支持当前CPU,如果支持,r5寄存器会返回一个用来描述这个处理器的结构体的地址,否则r5=0

先把这个r5的值保存到r10中

调用__lookup_machine_type这个函数确认内核是否支持当前开发板,如果支持,r5寄存器会返回一个用来描述这个开发板的结构体的地址,否则r5=0

__lookup_processor_type和__lookup_machine_type这两个判断任何一个出现错误,内核都会停止启动,并且跳转到显示错误的函数如54、87

而一旦两个判断都通过,那么就会执行88行,生成页表。

 

 

ENTRY(stext)
	bl	preserve_boot_args
	bl	el2_setup			// Drop to EL1, w20=cpu_boot_mode
	adrp	x24, __PHYS_OFFSET
	bl	set_cpu_boot_mode_flag
	bl	__create_page_tables		// x25=TTBR0, x26=TTBR1
	/*
	 * The following calls CPU setup code, see arch/arm64/mm/proc.S for
	 * details.
	 * On return, the CPU will be ready for the MMU to be turned on and
	 * the TCR will have been set.
	 */
	ldr	x27, =__mmap_switched		// address to jump to after
						// MMU has been enabled
	adr_l	lr, __enable_mmu		// return (PIC) address
	b	__cpu_setup			// initialise processor
ENDPROC(stext)

在4.4.142内核文件下arch\arm64\kernel目录下的head.S文件

 

 

 

你可能感兴趣的:(kernel,start)