1.前言
本文基于高通8996平台,kernel版本为3.18.31。
本文主要介绍head.S执行的整体流程
2. head.S执行前
bootloader在跳转到kernel前,需要确保如下设置:
MMU = off, D-cache = off, I-cache = on or off
x0 = physical address to the FDT blob
kernel的入口在arch\arm64\kernel\head.S中。
b stext // 跳转到stext
3. stext执行总体流程
mov x21, x0 // x21=FDT
将x0的值(device tree的地址)暂存在x21寄存器中
bl el2_setup // Drop to EL1, w20=cpu_boot_mode
Drop to EL1, w20=cpu_boot_mode //从EL2或者non-secure EL1回退到EL1;
判断当前的CPU异常模式,保存到w20中,如果为EL1则设置完CPU的大小端模式,就可以返回了;如果当前CPU异常模式为EL2, 则还要设置EL2下的虚拟化相关的一些配置再返回到EL1模式;
bl __calc_phys_offset // x24=PHYS_OFFSET, x28=PHYS_OFFSET-PAGE_OFFSET
x24=PHYS_OFFSET即kernel image的起始物理地址,
x28=物理地址和虚拟地址的偏移
计算出kernel image起始物理地址并保存在x24中
bl set_cpu_boot_mode_flag
将cpu启动的模式保存到全局变量__boot_cpu_mode中
CPU的启动模式在el2_setup时保存到了w20中
mrs x22, midr_el1 // x22=cpuid
Move from State register to Register,x22=cpuid
获取当前cpu id,保存在x22
mov x0, x22
bl lookup_processor_type
将当前cpuid作为参数传递给lookup_processor_type
根据上述保存在x22中的cpu id,查找cpu_table,并将cpu_table的对应表项的地址保存到X0中
mov x23, x0 // x23=current cpu_table
在lookup_processor_type中会将cpu_table对应表项的物理地址保存到x0中,此处会将x23=current cpu_table(物理地址)
cbz x23, __error_p // invalid processor (x23=0)?
1:
bl __vet_fdt
x23非0,跳转到标号1处
检查device tree的合法性
bl __create_page_tables // x25=TTBR0, x26=TTBR1
创建临时页表
/*
* The following calls CPU specific code in a position independent
* manner. See arch/arm64/mm/proc.S for details. x23 = base of
* cpu_info structure selected by lookup_processor_type above.
* On return, the CPU will be ready for the MMU to be turned on and
* the TCR will have been set.
*/
ldr x27, __switch_data // address to jump to after
// MMU has been enabled
由函数__enable_mmu中调用
adr lr, __enable_mmu // return (PIC) address
adr伪指令用于将一个地址加载到寄存器,取到的是相对于PC寄存器的地址,由于此刻PC寄存器中值是物理地址,所以lr中取到的即是标号__enable_mmu处的物理页基地址(页对齐)。
add lr, lr, #:lo12:__enable_mmu
加上__enable_mmu的页内偏移,lo12保存了__enable_mmu的页内偏移
ldr x12, [x23, #CPU_INFO_SETUP]
x23保存的是cpu_table,
CPU_INFO_SETUP = offsetof(struct cpu_info, cpu_setup),
那么x12就等于__cpu_setup
add x12, x12, x28 // __virt_to_phys
__cpu_setup 转化成物理地址
br x12 // initialise processor
跳转到__cpu_setup继续执行,由于lr等于__enable_mmu,
因此从__cpu_setup返回时,会跳转到__enable_mmu继续执行
补充知识:
ARMv8-a 划分了4 个Exception level,EL0归属于non-privilege level,EL1/2/3属于privilege level。Application位于特权等级最低的EL0,Guest OS(Linux kernel、window等)位于EL1,提供虚拟化支持的Hypervisor位于EL2(可选),提供Security支持的Seurity Monitor位于EL3(可选)。EL0,EL1,EL2,El3之间的切换,分别通过指令svc,hvc,smc
参考文档
1.https://blog.youkuaiyun.com/xichangbao/article/details/51568782
2.https://www.cnblogs.com/smartjourneys/diary/2017/04/27/6774121.html
3.http://www.wowotech.net/armv8a_arch/arm64_initialize_1.html ARM64的启动过程之(一):内核第一个脚印
3.http://www.wowotech.net/armv8a_arch/create_page_tables.html ARM64的启动过程之(二):创建启动阶段的页表
4.http://www.wowotech.net/armv8a_arch/__cpu_setup.html ARM64的启动过程之(三):为打开MMU而进行的CPU初始化
5.http://www.wowotech.net/armv8a_arch/turn-on-mmu.html ARM64的启动过程之(四):打开MMU
6.https://blog.youkuaiyun.com/xichangbao/article/details/51568782 Kernel启动流程源码解析 1 head.S
7.https://blog.youkuaiyun.com/xichangbao/article/details/51605462 Kernel启动流程源码解析 2 head.S