ARM Linux内核启动过程

Linux  Kernel  Start   ARM  
ARM Linux内核启动过程.
Updated  Feb 24, 2011 by  [email protected]

注: 本文转自ChinaUnix 作者为XPL.

本文针对arm linux, 从kernel的第一条指令开始分析,一直分析到进入 start_kernel()函数. 我们当前以linux-2.6.19内核版本作为范例来分析,本文中所有的代码,前面都会加上行号以便于和源码进行对照, 例: 在文件init/main.c中: 00478: asmlinkage voidinit start_kernel(void) 前面的"00478:" 表示478行,冒号后面的内容就是源码了.

在分析代码的过程中,我们使用缩进来表示各个代码的调用层次.

由于启动部分有一些代码是平台特定的,虽然大部分的平台所实现的功能都比较类似,但是为了更好的对code进行说明,对于平台相关的代码,我们选择 at91(ARM926EJS)平台进行分析.

另外,本文是以uncompressed kernel开始讲解的.对于内核解压缩部分的code,在 arch/arm/boot/compressed中,本文不做讨论.

一. 启动条件

通常从系统上电到执行到linux kenel这部分的任务是由boot loader来完成. 关于boot loader的内容,本文就不做过多介绍. 这里只讨论进入到linux kernel的时候的一些限制条件,这一般是boot loader在最后跳转到kernel之前要完成的:
  • 1. CPU必须处于SVC(supervisor)模式,并且IRQ和FIQ中断都是禁止的;
  • 2. MMU(内存管理单元)必须是关闭的, 此时虚拟地址对物理地址;
  • 3. 数据cache(Data cache)必须是关闭的
  • 4. 指令cache(Instruction cache)可以是打开的,也可以是关闭的,这个没有强制要求;
  • 5. CPU 通用寄存器0 (r0)必须是 0;
  • 6. CPU 通用寄存器1 (r1)必须是 ARM Linux machine type (关于machine type, 我们后面会有讲解)
  • 7. CPU 通用寄存器2 (r2) 必须是 kernel parameter list 的物理地址(parameter list 是由boot loader传递给kernel,用来描述设备信息属性的列表,详细内容可参考"Booting ARM Linux"文档).

二. starting kernel

首先,我们先对几个重要的宏进行说明(我们针对有MMU的情况):

位置 默认值 说明
KERNEL_RAM_ADDR arch/arm/kernel/head.S +26 0xc0008000 kernel在RAM中的的虚拟地址
PAGE_OFFSET include/asm-arm/memeory.h +50 0xc0000000 内核空间的起始虚拟地址
TEXT_OFFSET arch/arm/Makefile +137 0x00008000 内核相对于存储空间的偏移
TEXTADDR arch/arm/kernel/head.S +49 0xc0008000 kernel的起始虚拟地址
PHYS_OFFSET include/asm-arm/arch-xxx/memory.h 平台相关 RAM的起始物理地址

内核的入口是stext,这是在arch/arm/kernel/vmlinux.lds.S中定义的:
<span class="pln">        </span><span class="lit">00011</span><span class="pun">:</span><span class="pln"> ENTRY</span><span class="pun">(</span><span class="pln">stext</span><span class="pun">)</span>
对于vmlinux.lds.S,这是ld script文件,此文件的格式和汇编及C程序都不同,本文不对ld script作过多的介绍,只对内核中用到的内容进行讲解,关于ld的详细内容可以参考ld.info 这里的ENTRY(stext) 表示程序的入口是在符号stext. 而符号stext是在arch/arm/kernel/head.S中定义的: 下面我们将arm linux boot的主要代码列出来进行一个概括的介绍,然后,我们会逐个的进行详细的讲解.

在arch/arm/kernel/head.S中 72 - 94 行,是arm linux boot的主代码:

<span class="lit">00072</span><span class="pun">:</span><span class="pln"> ENTRY</span><span class="pun">(</span><span class="pln">stext</span><span class="pun">)</span><span class="pln">                                                         
</span><span class="lit">00073</span><span class="pun">:</span><span class="pln">         msr        cpsr_c</span><span class="pun">,</span><span class="pln"> </span><span class="com">#PSR_F_BIT | PSR_I_BIT | SVC_MODE @ ensure svc mode</span><span class="pln"> 
</span><span class="lit">00074</span><span class="pun">:</span><span class="pln">                                                 </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">and</span><span class="pln"> irqs disabled         
</span><span class="lit">00075</span><span class="pun">:</span><span class="pln">         mrc        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r9</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> c0                </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">get</span><span class="pln"> processor id          
</span><span class="lit">00076</span><span class="pun">:</span><span class="pln">         bl        __lookup_processor_type                </span><span class="pun">@</span><span class="pln"> r5</span><span class="pun">=</span><span class="pln">procinfo r9</span><span class="pun">=</span><span class="pln">cpuid      
</span><span class="lit">00077</span><span class="pun">:</span><span class="pln">         movs        r10</span><span class="pun">,</span><span class="pln"> r5                                </span><span class="pun">@</span><span class="pln"> invalid processor </span><span class="pun">(</span><span class="pln">r5</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)?</span><span class="pln"> 
</span><span class="lit">00078</span><span class="pun">:</span><span class="pln">         beq        __error_p                        </span><span class="pun">@</span><span class="pln"> yes</span><span class="pun">,</span><span class="pln"> error </span><span class="str">'p'</span><span class="pln">            
</span><span class="lit">00079</span><span class="pun">:</span><span class="pln">         bl        __lookup_machine_type                </span><span class="pun">@</span><span class="pln"> r5</span><span class="pun">=</span><span class="pln">machinfo               
</span><span class="lit">00080</span><span class="pun">:</span><span class="pln">         movs        r8</span><span class="pun">,</span><span class="pln"> r5                                </span><span class="pun">@</span><span class="pln"> invalid machine </span><span class="pun">(</span><span class="pln">r5</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)?</span><span class="pln">   
</span><span class="lit">00081</span><span class="pun">:</span><span class="pln">         beq        __error_a                        </span><span class="pun">@</span><span class="pln"> yes</span><span class="pun">,</span><span class="pln"> error </span><span class="str">'a'</span><span class="pln">            
</span><span class="lit">00082</span><span class="pun">:</span><span class="pln">         bl        __create_page_tables                                        
</span><span class="lit">00083</span><span class="pun">:</span><span class="pln">                                                                      
</span><span class="lit">00084</span><span class="pun">:</span><span class="pln">         </span><span class="com">/*                                                                  
00085:          * The following calls CPU specific code in a position independent 
00086:          * manner.  See arch/arm/mm/proc-*.S for details.  r10 = base of    
00087:          * xxx_proc_info structure selected by __lookup_machine_type       
00088:          * above.  On return, the CPU will be ready for the MMU to be       
00089:          * turned on, and r0 will hold the CPU control register value.      
00090:          */</span><span class="pln">                                                                
</span><span class="lit">00091</span><span class="pun">:</span><span class="pln">         ldr        r13</span><span class="pun">,</span><span class="pln"> __switch_data                </span><span class="pun">@</span><span class="pln"> address to jump to after 
</span><span class="lit">00092</span><span class="pun">:</span><span class="pln">                                                 </span><span class="pun">@</span><span class="pln"> mmu has been enabled      
</span><span class="lit">00093</span><span class="pun">:</span><span class="pln">         adr        lr</span><span class="pun">,</span><span class="pln"> __enable_mmu                </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">(</span><span class="pln">PIC</span><span class="pun">)</span><span class="pln"> address      
</span><span class="lit">00094</span><span class="pun">:</span><span class="pln">         add        pc</span><span class="pun">,</span><span class="pln"> r10</span><span class="pun">,</span><span class="pln"> </span><span class="com">#PROCINFO_INITFUNC                                </span>

其中,73行是确保kernel运行在SVC模式下,并且IRQ和FIRQ中断已经关闭,这样做是很谨慎的.

arm linux boot的主线可以概括为以下几个步骤:

  • 1. 确定 processor type (75 - 78行)
  • 2. 确定 machine type (79 - 81行)
  • 3. 创建页表 (82行)
  • 4. 调用平台特定的cpu_flush函数 (在struct proc_info_list中) (94 行)
  • 5. 开启mmu (93行)
  • 6. 切换数据 (91行)
最终跳转到start_kernel (在 switch_data的结束的时候,调用了 b start_kernel)

下面,我们按照这个主线,逐步的分析Code.

1. 确定 processor type

arch/arm/kernel/head.S中:
<span class="lit">00075</span><span class="pun">:</span><span class="pln">         mrc        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r9</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> c0                </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">get</span><span class="pln"> processor id          
</span><span class="lit">00076</span><span class="pun">:</span><span class="pln">         bl        __lookup_processor_type                </span><span class="pun">@</span><span class="pln"> r5</span><span class="pun">=</span><span class="pln">procinfo r9</span><span class="pun">=</span><span class="pln">cpuid      
</span><span class="lit">00077</span><span class="pun">:</span><span class="pln">         movs        r10</span><span class="pun">,</span><span class="pln"> r5                                </span><span class="pun">@</span><span class="pln"> invalid processor </span><span class="pun">(</span><span class="pln">r5</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)?</span><span class="pln"> 
</span><span class="lit">00078</span><span class="pun">:</span><span class="pln">         beq        __error_p                        </span><span class="pun">@</span><span class="pln"> yes</span><span class="pun">,</span><span class="pln"> error </span><span class="str">'p'</span><span class="pln">           </span>

75行: 通过cp15协处理器的c0寄存器来获得processor id的指令. 关于cp15的详细内容可参考相关的arm手册

76行: 跳转到lookup_processor_type.在lookup_processor_type中,会把processor type 存储在r5中

77,78行: 判断r5中的processor type是否是0,如果是0,说明是无效的processor type,跳转到error_p(出错)

lookup_processor_type 函数主要是根据从cpu中获得的processor id和系统中的proc_info进行匹配,将匹配到的proc_info_list的基地址存到r5中, 0表示没有找到对应的processor type.

下面我们分析lookup_processor_type函数

arch/arm/kernel/head-common.S中:

<span class="lit">00145</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __lookup_processor_type</span><span class="pun">,</span><span class="pln"> </span><span class="pun">%</span><span class="kwd">function</span><span class="pln"> 
</span><span class="lit">00146</span><span class="pun">:</span><span class="pln"> __lookup_processor_type</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00147</span><span class="pun">:</span><span class="pln">         adr        r3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3f</span><span class="pln"> 
</span><span class="lit">00148</span><span class="pun">:</span><span class="pln">         ldmda        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r5 </span><span class="pun">-</span><span class="pln"> r7</span><span class="pun">}</span><span class="pln"> 
</span><span class="lit">00149</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">sub</span><span class="pln">        r3</span><span class="pun">,</span><span class="pln"> r3</span><span class="pun">,</span><span class="pln"> r7                        </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">get</span><span class="pln"> offset between virt</span><span class="pun">&</span><span class="pln">phys 
</span><span class="lit">00150</span><span class="pun">:</span><span class="pln">         add        r5</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> r3                        </span><span class="pun">@</span><span class="pln"> convert virt addresses to 
</span><span class="lit">00151</span><span class="pun">:</span><span class="pln">         add        r6</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> r3                        </span><span class="pun">@</span><span class="pln"> physical address space 
</span><span class="lit">00152</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">:</span><span class="pln">        ldmia        r5</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r3</span><span class="pun">,</span><span class="pln"> r4</span><span class="pun">}</span><span class="pln">                        </span><span class="pun">@</span><span class="pln"> value</span><span class="pun">,</span><span class="pln"> mask 
</span><span class="lit">00153</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">and</span><span class="pln">        r4</span><span class="pun">,</span><span class="pln"> r4</span><span class="pun">,</span><span class="pln"> r9                        </span><span class="pun">@</span><span class="pln"> mask wanted bits 
</span><span class="lit">00154</span><span class="pun">:</span><span class="pln">         teq        r3</span><span class="pun">,</span><span class="pln"> r4 
</span><span class="lit">00155</span><span class="pun">:</span><span class="pln">         beq        </span><span class="lit">2f</span><span class="pln"> 
</span><span class="lit">00156</span><span class="pun">:</span><span class="pln">         add        r5</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> </span><span class="com">#PROC_INFO_SZ                @ sizeof(proc_info_list)</span><span class="pln"> 
</span><span class="lit">00157</span><span class="pun">:</span><span class="pln">         cmp        r5</span><span class="pun">,</span><span class="pln"> r6 
</span><span class="lit">00158</span><span class="pun">:</span><span class="pln">         blo        </span><span class="lit">1b</span><span class="pln"> 
</span><span class="lit">00159</span><span class="pun">:</span><span class="pln">         mov        r5</span><span class="pun">,</span><span class="pln"> </span><span class="com">#0                                @ unknown processor</span><span class="pln"> 
</span><span class="lit">00160</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span><span class="pun">:</span><span class="pln">        mov        pc</span><span class="pun">,</span><span class="pln"> lr 
</span><span class="lit">00161</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00162</span><span class="pun">:</span><span class="pln"> </span><span class="com">/* 
00163:  * This provides a C-API version of the above function. 
00164:  */</span><span class="pln"> 
</span><span class="lit">00165</span><span class="pun">:</span><span class="pln"> ENTRY</span><span class="pun">(</span><span class="pln">lookup_processor_type</span><span class="pun">)</span><span class="pln"> 
</span><span class="lit">00166</span><span class="pun">:</span><span class="pln">         stmfd        sp</span><span class="pun">!,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r4 </span><span class="pun">-</span><span class="pln"> r7</span><span class="pun">,</span><span class="pln"> r9</span><span class="pun">,</span><span class="pln"> lr</span><span class="pun">}</span><span class="pln"> 
</span><span class="lit">00167</span><span class="pun">:</span><span class="pln">         mov        r9</span><span class="pun">,</span><span class="pln"> r0 
</span><span class="lit">00168</span><span class="pun">:</span><span class="pln">         bl        __lookup_processor_type 
</span><span class="lit">00169</span><span class="pun">:</span><span class="pln">         mov        r0</span><span class="pun">,</span><span class="pln"> r5 
</span><span class="lit">00170</span><span class="pun">:</span><span class="pln">         ldmfd        sp</span><span class="pun">!,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r4 </span><span class="pun">-</span><span class="pln"> r7</span><span class="pun">,</span><span class="pln"> r9</span><span class="pun">,</span><span class="pln"> pc</span><span class="pun">}</span><span class="pln"> 
</span><span class="lit">00171</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00172</span><span class="pun">:</span><span class="pln"> </span><span class="com">/* 
00173:  * Look in include/asm-arm/procinfo.h and arch/arm/kernel/arch.[ch] for 
00174:  * more information about the __proc_info and __arch_info structures. 
00175:  */</span><span class="pln"> 
</span><span class="lit">00176</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __proc_info_begin 
</span><span class="lit">00177</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __proc_info_end 
</span><span class="lit">00178</span><span class="pun">:</span><span class="pln"> </span><span class="lit">3</span><span class="pun">:</span><span class="pln">        </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        </span><span class="pun">.</span><span class="pln"> 
</span><span class="lit">00179</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __arch_info_begin 
</span><span class="lit">00180</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __arch_info_end</span>

145, 146行是函数定义

147行: 取地址指令,这里的3f是向前symbol名称是3的位置,即第178行,将该地址存入r3.

这里需要注意的是,adr指令取址,获得的是基于pc的一个地址,要格外注意,这个地址是3f处的"运行时地址",由于此时MMU还没有打开,也可以理解成物理地址(实地址).(详细内容可参考arm指令手册)
148行: 因为r3中的地址是178行的位置的地址,因而执行完后:
r5存的是176行符号  proc_info_begin的地址; r6存的是177行符号  proc_info_end的地址; r7存的是3f处的地址. 这里需要注意链接地址和运行时地址的区别. r3存储的是运行时地址(物理地址),而r7中存储的是链接地址(虚拟地址).
proc_info_begin和 proc_info_end是在arch/arm/kernel/vmlinux.lds.S中:

<span class="pln">        </span><span class="lit">00031</span><span class="pun">:</span><span class="pln">                __proc_info_begin </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln"> 
        </span><span class="lit">00032</span><span class="pun">:</span><span class="pln">                        </span><span class="pun">*(.</span><span class="pln">proc</span><span class="pun">.</span><span class="pln">info</span><span class="pun">.</span><span class="pln">init</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00033</span><span class="pun">:</span><span class="pln">                __proc_info_end </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span>
这里是声明了两个变量: proc_info_begin 和  proc_info_end,其中等号后面的"."是location counter(详细内容请参考ld.info) 这三行的意思是: proc_info_begin 的位置上,放置所有文件中的 ".proc.info.init" 段的内容,然后紧接着是  proc_info_end 的位置.
kernel 使用struct proc_info_list来描述processor type.
在 include/asm-arm/procinfo.h 中:
<span class="pln">        </span><span class="lit">00029</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">struct</span><span class="pln"> proc_info_list </span><span class="pun">{</span><span class="pln"> 
        </span><span class="lit">00030</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                cpu_val</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00031</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                cpu_mask</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00032</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln">                __cpu_mm_mmu_flags</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* used by head.S */</span><span class="pln"> 
        </span><span class="lit">00033</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln">                __cpu_io_mmu_flags</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* used by head.S */</span><span class="pln"> 
        </span><span class="lit">00034</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln">                __cpu_flush</span><span class="pun">;</span><span class="pln">                </span><span class="com">/* used by head.S */</span><span class="pln"> 
        </span><span class="lit">00035</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">char</span><span class="pln">                </span><span class="pun">*</span><span class="pln">arch_name</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00036</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">char</span><span class="pln">                </span><span class="pun">*</span><span class="pln">elf_name</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00037</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                elf_hwcap</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00038</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">char</span><span class="pln">                </span><span class="pun">*</span><span class="pln">cpu_name</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00039</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">struct</span><span class="pln"> processor        </span><span class="pun">*</span><span class="pln">proc</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00040</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">struct</span><span class="pln"> cpu_tlb_fns        </span><span class="pun">*</span><span class="pln">tlb</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00041</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">struct</span><span class="pln"> cpu_user_fns        </span><span class="pun">*</span><span class="pln">user</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00042</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">struct</span><span class="pln"> cpu_cache_fns        </span><span class="pun">*</span><span class="pln">cache</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00043</span><span class="pun">:</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> 
         
        </span><span class="pun">我们当前以</span><span class="pln">at91</span><span class="pun">为例,其</span><span class="pln">processor</span><span class="pun">是</span><span class="lit">926</span><span class="pun">的.</span><span class="pln"> 
                </span><span class="pun">在</span><span class="pln">arch</span><span class="pun">/</span><span class="pln">arm</span><span class="pun">/</span><span class="pln">mm</span><span class="pun">/</span><span class="pln">proc</span><span class="pun">-</span><span class="pln">arm926</span><span class="pun">.</span><span class="pln">S </span><span class="pun">中:</span><span class="pln"> 
        </span><span class="lit">00464</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">section </span><span class="str">".proc.info.init"</span><span class="pun">,</span><span class="pln"> </span><span class="com">#alloc, #execinstr</span><span class="pln"> 
        </span><span class="lit">00465</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00466</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __arm926_proc_info</span><span class="pun">,</span><span class="com">#object</span><span class="pln"> 
        </span><span class="lit">00467</span><span class="pun">:</span><span class="pln"> __arm926_proc_info</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00468</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        </span><span class="lit">0x41069260</span><span class="pln">                        </span><span class="pun">@</span><span class="pln"> ARM926EJ</span><span class="pun">-</span><span class="pln">S </span><span class="pun">(</span><span class="pln">v5TEJ</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00469</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        </span><span class="lit">0xff0ffff0</span><span class="pln"> 
        </span><span class="lit">00470</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">   PMD_TYPE_SECT </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00471</span><span class="pun">:</span><span class="pln">                 PMD_SECT_BUFFERABLE </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00472</span><span class="pun">:</span><span class="pln">                 PMD_SECT_CACHEABLE </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00473</span><span class="pun">:</span><span class="pln">                 PMD_BIT4 </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00474</span><span class="pun">:</span><span class="pln">                 PMD_SECT_AP_WRITE </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00475</span><span class="pun">:</span><span class="pln">                 PMD_SECT_AP_READ 
        </span><span class="lit">00476</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">   PMD_TYPE_SECT </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00477</span><span class="pun">:</span><span class="pln">                 PMD_BIT4 </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00478</span><span class="pun">:</span><span class="pln">                 PMD_SECT_AP_WRITE </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00479</span><span class="pun">:</span><span class="pln">                 PMD_SECT_AP_READ 
        </span><span class="lit">00480</span><span class="pun">:</span><span class="pln">         b        __arm926_setup 
        </span><span class="lit">00481</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        cpu_arch_name 
        </span><span class="lit">00482</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        cpu_elf_name 
        </span><span class="lit">00483</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        HWCAP_SWP</span><span class="pun">|</span><span class="pln">HWCAP_HALF</span><span class="pun">|</span><span class="pln">HWCAP_THUMB</span><span class="pun">|</span><span class="pln">HWCAP_FAST_MULT</span><span class="pun">|</span><span class="pln">HWCAP_VFP</span><span class="pun">|</span><span class="pln">HWCAP_EDSP</span><span class="pun">|</span><span class="pln">HWCAP_JAVA 
        </span><span class="lit">00484</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        cpu_arm926_name 
        </span><span class="lit">00485</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        arm926_processor_functions 
        </span><span class="lit">00486</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        v4wbi_tlb_fns 
        </span><span class="lit">00487</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        v4wb_user_fns 
        </span><span class="lit">00488</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        arm926_cache_fns 
        </span><span class="lit">00489</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">size        __arm926_proc_info</span><span class="pun">,</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> __arm926_proc_info</span>

从464行,我们可以看到  arm926_proc_info 被放到了".proc.info.init"段中. 对照struct proc_info_list,我们可以看到 cpu_flush的定义是在480行,即 arm926_setup.(我们将在"4. 调用平台特定的 cpu_flush函数"一节中详细分析这部分的内容.)
从以上的内容我们可以看出: r5中的 proc_info_begin是proc_info_list的起始地址, r6中的 proc_info_end是proc_info_list的结束地址.

149行: 从上面的分析我们可以知道r3中存储的是3f处的物理地址,而r7存储的是3f处的虚拟地址,这一行是计算当前程序运行的物理地址和虚拟地址的差值,将其保存到r3中.

150行: 将r5存储的虚拟地址(proc_info_begin)转换成物理地址

151行: 将r6存储的虚拟地址(proc_info_end)转换成物理地址

152行: 对照struct proc_info_list,可以得知,这句是将当前proc_info的cpu_val和cpu_mask分别存r3, r4中

153行: r9中存储了processor id(arch/arm/kernel/head.S中的75行),与r4的cpu_mask进行逻辑与操作,得到我们需要的值

154行: 将153行中得到的值与r3中的cpu_val进行比较

155行: 如果相等,说明我们找到了对应的processor type,跳到160行,返回

156行: (如果不相等) , 将r5指向下一个proc_info,

157行: 和r6比较,检查是否到了proc_info_end.

158行: 如果没有到proc_info_end,表明还有proc_info配置,返回152行继续查找

159行: 执行到这里,说明所有的proc_info都匹配过了,但是没有找到匹配的,将r5设置成0(unknown processor)

160行: 返回

2. 确定 machine type

arch/arm/kernel/head.S中:
<span class="lit">00079</span><span class="pun">:</span><span class="pln">         bl        __lookup_machine_type                </span><span class="pun">@</span><span class="pln"> r5</span><span class="pun">=</span><span class="pln">machinfo               
</span><span class="lit">00080</span><span class="pun">:</span><span class="pln">         movs        r8</span><span class="pun">,</span><span class="pln"> r5                                </span><span class="pun">@</span><span class="pln"> invalid machine </span><span class="pun">(</span><span class="pln">r5</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)?</span><span class="pln">   
</span><span class="lit">00081</span><span class="pun">:</span><span class="pln">         beq        __error_a                        </span><span class="pun">@</span><span class="pln"> yes</span><span class="pun">,</span><span class="pln"> error </span><span class="str">'a'</span><span class="pln">  </span>

79行: 跳转到lookup_machine_type函数,在lookup_machine_type中,会把struct machine_desc的基地址(machine type)存储在r5中 80,81行: 将r5中的 machine_desc的基地址存储到r8中,并判断r5是否是0,如果是0,说明是无效的machine type,跳转到error_a(出错)

lookup_machine_type 函数 下面我们分析lookup_machine_type 函数:

arch/arm/kernel/head-common.S中:
<span class="lit">00176</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __proc_info_begin 
</span><span class="lit">00177</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __proc_info_end 
</span><span class="lit">00178</span><span class="pun">:</span><span class="pln"> </span><span class="lit">3</span><span class="pun">:</span><span class="pln">        </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        </span><span class="pun">.</span><span class="pln"> 
</span><span class="lit">00179</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __arch_info_begin 
</span><span class="lit">00180</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __arch_info_end 
</span><span class="lit">00181</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00182</span><span class="pun">:</span><span class="pln"> </span><span class="com">/* 
00183:  * Lookup machine architecture in the linker-build list of architectures. 
00184:  * Note that we can't use the absolute addresses for the __arch_info 
00185:  * lists since we aren't running with the MMU on (and therefore, we are 
00186:  * not in the correct address space).  We have to calculate the offset. 
00187:  * 
00188:  *  r1 = machine architecture number 
00189:  * Returns: 
00190:  *  r3, r4, r6 corrupted 
00191:  *  r5 = mach_info pointer in physical address space 
00192:  */</span><span class="pln">        
</span><span class="lit">00193</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __lookup_machine_type</span><span class="pun">,</span><span class="pln"> </span><span class="pun">%</span><span class="kwd">function</span><span class="pln"> 
</span><span class="lit">00194</span><span class="pun">:</span><span class="pln"> __lookup_machine_type</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00195</span><span class="pun">:</span><span class="pln">         adr        r3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3b</span><span class="pln"> 
</span><span class="lit">00196</span><span class="pun">:</span><span class="pln">         ldmia        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r4</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">}</span><span class="pln"> 
</span><span class="lit">00197</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">sub</span><span class="pln">        r3</span><span class="pun">,</span><span class="pln"> r3</span><span class="pun">,</span><span class="pln"> r4                        </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">get</span><span class="pln"> offset between virt</span><span class="pun">&</span><span class="pln">phys 
</span><span class="lit">00198</span><span class="pun">:</span><span class="pln">         add        r5</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> r3                        </span><span class="pun">@</span><span class="pln"> convert virt addresses to 
</span><span class="lit">00199</span><span class="pun">:</span><span class="pln">         add        r6</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> r3                        </span><span class="pun">@</span><span class="pln"> physical address space 
</span><span class="lit">00200</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">:</span><span class="pln">        ldr        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r5</span><span class="pun">,</span><span class="pln"> </span><span class="com">#MACHINFO_TYPE]        @ get machine type</span><span class="pln"> 
</span><span class="lit">00201</span><span class="pun">:</span><span class="pln">         teq        r3</span><span class="pun">,</span><span class="pln"> r1                                </span><span class="pun">@</span><span class="pln"> matches loader number</span><span class="pun">?</span><span class="pln"> 
</span><span class="lit">00202</span><span class="pun">:</span><span class="pln">         beq        </span><span class="lit">2f</span><span class="pln">                                </span><span class="pun">@</span><span class="pln"> found 
</span><span class="lit">00203</span><span class="pun">:</span><span class="pln">         add        r5</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> </span><span class="com">#SIZEOF_MACHINE_DESC        @ next machine_desc</span><span class="pln"> 
</span><span class="lit">00204</span><span class="pun">:</span><span class="pln">         cmp        r5</span><span class="pun">,</span><span class="pln"> r6 
</span><span class="lit">00205</span><span class="pun">:</span><span class="pln">         blo        </span><span class="lit">1b</span><span class="pln"> 
</span><span class="lit">00206</span><span class="pun">:</span><span class="pln">         mov        r5</span><span class="pun">,</span><span class="pln"> </span><span class="com">#0                                @ unknown machine</span><span class="pln"> 
</span><span class="lit">00207</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span><span class="pun">:</span><span class="pln">        mov        pc</span><span class="pun">,</span><span class="pln"> lr</span>

193, 194行: 函数声明

195行: 取地址指令,这里的3b是向后symbol名称是3的位置,即第178行,将该地址存入r3.

和上面我们对 lookup_processor_type 函数的分析相同,r3中存放的是3b处物理地址.

196行:

  • r3是3b处的地址,因而执行完后:
  • r4存的是 3b处的地址
  • r5存的是arch_info_begin 的地址
  • r6存的是arch_info_end 的地址

arch_info_begin 和  arch_info_end是在 arch/arm/kernel/vmlinux.lds.S中:
<span class="pln">        </span><span class="lit">00034</span><span class="pun">:</span><span class="pln">                __arch_info_begin </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln"> 
        </span><span class="lit">00035</span><span class="pun">:</span><span class="pln">                        </span><span class="pun">*(.</span><span class="pln">arch</span><span class="pun">.</span><span class="pln">info</span><span class="pun">.</span><span class="pln">init</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00036</span><span class="pun">:</span><span class="pln">                __arch_info_end </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span>

这里是声明了两个变量: arch_info_begin 和  arch_info_end,其中等号后面的"."是location counter(详细内容请参考ld.info) 这三行的意思是: arch_info_begin 的位置上,放置所有文件中的 ".arch.info.init" 段的内容,然后紧接着是  arch_info_end 的位置.
kernel 使用struct machine_desc 来描述 machine type. 在 include/asm-arm/mach/arch.h 中:
<span class="pln">        </span><span class="lit">00017</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">struct</span><span class="pln"> machine_desc </span><span class="pun">{</span><span class="pln"> 
        </span><span class="lit">00018</span><span class="pun">:</span><span class="pln">         </span><span class="com">/* 
        00019:          * Note! The first four elements are used 
        00020:          * by assembler code in head-armv.S 
        00021:          */</span><span class="pln"> 
        </span><span class="lit">00022</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                nr</span><span class="pun">;</span><span class="pln">                </span><span class="com">/* architecture number        */</span><span class="pln"> 
        </span><span class="lit">00023</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                phys_io</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* start of physical io        */</span><span class="pln"> 
        </span><span class="lit">00024</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                io_pg_offst</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* byte offset for io 
        00025:                                                  * page tabe entry        */</span><span class="pln"> 
        </span><span class="lit">00026</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00027</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">char</span><span class="pln">                </span><span class="pun">*</span><span class="pln">name</span><span class="pun">;</span><span class="pln">                </span><span class="com">/* architecture name        */</span><span class="pln"> 
        </span><span class="lit">00028</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln">                boot_params</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* tagged list                */</span><span class="pln"> 
        </span><span class="lit">00029</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00030</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                video_start</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* start of video RAM        */</span><span class="pln"> 
        </span><span class="lit">00031</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                video_end</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* end of video RAM        */</span><span class="pln"> 
        </span><span class="lit">00032</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00033</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                reserve_lp0 </span><span class="pun">:</span><span class="lit">1</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* never has lp0        */</span><span class="pln"> 
        </span><span class="lit">00034</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                reserve_lp1 </span><span class="pun">:</span><span class="lit">1</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* never has lp1        */</span><span class="pln"> 
        </span><span class="lit">00035</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                reserve_lp2 </span><span class="pun">:</span><span class="lit">1</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* never has lp2        */</span><span class="pln"> 
        </span><span class="lit">00036</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln">                soft_reboot </span><span class="pun">:</span><span class="lit">1</span><span class="pun">;</span><span class="pln">        </span><span class="com">/* soft reboot                */</span><span class="pln"> 
        </span><span class="lit">00037</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">void</span><span class="pln">                        </span><span class="pun">(*</span><span class="pln">fixup</span><span class="pun">)(</span><span class="kwd">struct</span><span class="pln"> machine_desc </span><span class="pun">*,</span><span class="pln"> 
        </span><span class="lit">00038</span><span class="pun">:</span><span class="pln">                                          </span><span class="kwd">struct</span><span class="pln"> tag </span><span class="pun">*,</span><span class="pln"> </span><span class="kwd">char</span><span class="pln"> </span><span class="pun">**,</span><span class="pln"> 
        </span><span class="lit">00039</span><span class="pun">:</span><span class="pln">                                          </span><span class="kwd">struct</span><span class="pln"> meminfo </span><span class="pun">*);</span><span class="pln"> 
        </span><span class="lit">00040</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">void</span><span class="pln">                        </span><span class="pun">(*</span><span class="pln">map_io</span><span class="pun">)(</span><span class="kwd">void</span><span class="pun">);</span><span class="com">/* IO mapping function        */</span><span class="pln"> 
        </span><span class="lit">00041</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">void</span><span class="pln">                        </span><span class="pun">(*</span><span class="pln">init_irq</span><span class="pun">)(</span><span class="kwd">void</span><span class="pun">);</span><span class="pln"> 
        </span><span class="lit">00042</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">struct</span><span class="pln"> sys_timer        </span><span class="pun">*</span><span class="pln">timer</span><span class="pun">;</span><span class="pln">                </span><span class="com">/* system tick timer        */</span><span class="pln"> 
        </span><span class="lit">00043</span><span class="pun">:</span><span class="pln">         </span><span class="kwd">void</span><span class="pln">                        </span><span class="pun">(*</span><span class="pln">init_machine</span><span class="pun">)(</span><span class="kwd">void</span><span class="pun">);</span><span class="pln"> 
        </span><span class="lit">00044</span><span class="pun">:</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> 
        </span><span class="lit">00045</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00046</span><span class="pun">:</span><span class="pln"> </span><span class="com">/* 
        00047:  * Set of macros to define architecture features.  This is built into 
        00048:  * a table by the linker. 
        00049:  */</span><span class="pln"> 
        </span><span class="lit">00050</span><span class="pun">:</span><span class="pln"> </span><span class="com">#define</span><span class="pln"> MACHINE_START</span><span class="pun">(</span><span class="pln">_type</span><span class="pun">,</span><span class="pln">_name</span><span class="pun">)</span><span class="pln">                        </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00051</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">struct</span><span class="pln"> machine_desc __mach_desc_</span><span class="com">##_type        \</span><span class="pln"> 
        </span><span class="lit">00052</span><span class="pun">:</span><span class="pln">  __attribute_used__                                        </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00053</span><span class="pun">:</span><span class="pln">  __attribute__</span><span class="pun">((</span><span class="pln">__section__</span><span class="pun">(</span><span class="str">".arch.info.init"</span><span class="pun">))</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">        </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00054</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">nr                </span><span class="pun">=</span><span class="pln"> MACH_TYPE_</span><span class="com">##_type,                \</span><span class="pln"> 
        </span><span class="lit">00055</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">name                </span><span class="pun">=</span><span class="pln"> _name</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00056</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00057</span><span class="pun">:</span><span class="pln"> </span><span class="com">#define</span><span class="pln"> MACHINE_END                                </span><span class="pun">\</span><span class="pln"> 
        </span><span class="lit">00058</span><span class="pun">:</span><span class="pln"> </span><span class="pun">};</span><span class="pln">         
         
        </span><span class="pun">内核中,一般使用宏</span><span class="pln">MACHINE_START</span><span class="pun">来定义</span><span class="pln">machine type</span><span class="pun">.</span><span class="pln"> 
        </span><span class="pun">对于</span><span class="pln">at91</span><span class="pun">,</span><span class="pln"> </span><span class="pun">在</span><span class="pln"> arch</span><span class="pun">/</span><span class="pln">arm</span><span class="pun">/</span><span class="pln">mach</span><span class="pun">-</span><span class="pln">at91rm9200</span><span class="pun">/</span><span class="pln">board</span><span class="pun">-</span><span class="pln">ek</span><span class="pun">.</span><span class="pln">c </span><span class="pun">中:</span><span class="pln"> 
        </span><span class="lit">00137</span><span class="pun">:</span><span class="pln"> MACHINE_START</span><span class="pun">(</span><span class="pln">AT91RM9200EK</span><span class="pun">,</span><span class="pln"> </span><span class="str">"Atmel AT91RM9200-EK"</span><span class="pln"> 
        </span><span class="lit">00138</span><span class="pun">:</span><span class="pln">         </span><span class="com">/* Maintainer: SAN People/Atmel */</span><span class="pln"> 
        </span><span class="lit">00139</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">phys_io        </span><span class="pun">=</span><span class="pln"> AT91_BASE_SYS</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00140</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">io_pg_offst        </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">AT91_VA_BASE_SYS </span><span class="pun">>></span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">&</span><span class="pln"> </span><span class="lit">0xfffc</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00141</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">boot_params        </span><span class="pun">=</span><span class="pln"> AT91_SDRAM_BASE </span><span class="pun">+</span><span class="pln"> </span><span class="lit">0x100</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00142</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">timer                </span><span class="pun">=</span><span class="pln"> </span><span class="pun">&</span><span class="pln">at91rm9200_timer</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00143</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">map_io                </span><span class="pun">=</span><span class="pln"> ek_map_io</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00144</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">init_irq        </span><span class="pun">=</span><span class="pln"> ek_init_irq</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00145</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">init_machine        </span><span class="pun">=</span><span class="pln"> ek_board_init</span><span class="pun">,</span><span class="pln"> 
        </span><span class="lit">00146</span><span class="pun">:</span><span class="pln"> MACHINE_END</span>

197行: r3中存储的是3b处的物理地址,而r4中存储的是3b处的虚拟地址,这里计算处物理地址和虚拟地址的差值,保存到r3中

198行: 将r5存储的虚拟地址(arch_info_begin)转换成物理地址 199行: 将r6存储的虚拟地址(arch_info_end)转换成物理地址 200行: MACHINFO_TYPE 在 arch/arm/kernel/asm-offset.c 101行定义, 这里是取 struct machine_desc中的nr(architecture number) 到r3中

201行: 将r3中取到的machine type 和 r1中的 machine type(见前面的"启动条件"进行比较

202行: 如果相同,说明找到了对应的machine type,跳转到207行的2f处,此时r5中存储了对应的struct machine_desc的基地址

203行: (不相同), 取下一个machine_desc的地址

204行: 和r6进行比较,检查是否到了arch_info_end.

205行: 如果不相同,说明还有machine_desc,返回200行继续查找.

206行: 执行到这里,说明所有的machind_desc都查找完了,并且没有找到匹配的, 将r5设置成0(unknown machine).

207行: 返回

3. 创建页表

通过前面的两步,我们已经确定了processor type 和 machine type. 此时,一些特定寄存器的值如下所示:

<span class="pln">r8 </span><span class="pun">=</span><span class="pln"> machine info       </span><span class="pun">(</span><span class="kwd">struct</span><span class="pln"> machine_desc</span><span class="pun">的基地址)</span><span class="pln"> 
r9 </span><span class="pun">=</span><span class="pln"> cpu id             </span><span class="pun">(通过</span><span class="pln">cp15</span><span class="pun">协处理器获得的</span><span class="pln">cpu id</span><span class="pun">)</span><span class="pln"> 
r10 </span><span class="pun">=</span><span class="pln"> procinfo          </span><span class="pun">(</span><span class="kwd">struct</span><span class="pln"> proc_info_list</span><span class="pun">的基地址)</span>

创建页表是通过函数 create_page_tables 来实现的.

这里,我们使用的是arm的L1主页表,L1主页表也称为段页表(section page table) L1 主页表将4 GB 的地址空间分成若干个1 MB的段(section),因此L1页表包含4096个页表项(section entry). 每个页表项是32 bits(4 bytes) 因而L1主页表占用 40964 = 16k的内存空间.

对于ARM926,其L1 section entry的格式为可参考arm926EJS TRM):

下面我们来分析 create_page_tables 函数:

在 arch/arm/kernel/head.S 中:
<span class="lit">00206</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __create_page_tables</span><span class="pun">,</span><span class="pln"> </span><span class="pun">%</span><span class="kwd">function</span><span class="pln"> 
</span><span class="lit">00207</span><span class="pun">:</span><span class="pln"> __create_page_tables</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00208</span><span class="pun">:</span><span class="pln">         pgtbl        r4                                </span><span class="pun">@</span><span class="pln"> page table address 
</span><span class="lit">00209</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00210</span><span class="pun">:</span><span class="pln">         </span><span class="com">/* 
00211:          * Clear the 16K level 1 swapper page table 
00212:          */</span><span class="pln"> 
</span><span class="lit">00213</span><span class="pun">:</span><span class="pln">         mov        r0</span><span class="pun">,</span><span class="pln"> r4 
</span><span class="lit">00214</span><span class="pun">:</span><span class="pln">         mov        r3</span><span class="pun">,</span><span class="pln"> </span><span class="com">#0</span><span class="pln"> 
</span><span class="lit">00215</span><span class="pun">:</span><span class="pln">         add        r6</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#0x4000</span><span class="pln"> 
</span><span class="lit">00216</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">:</span><span class="pln">        str        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r0</span><span class="pun">],</span><span class="pln"> </span><span class="com">#4</span><span class="pln"> 
</span><span class="lit">00217</span><span class="pun">:</span><span class="pln">         str        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r0</span><span class="pun">],</span><span class="pln"> </span><span class="com">#4</span><span class="pln"> 
</span><span class="lit">00218</span><span class="pun">:</span><span class="pln">         str        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r0</span><span class="pun">],</span><span class="pln"> </span><span class="com">#4</span><span class="pln"> 
</span><span class="lit">00219</span><span class="pun">:</span><span class="pln">         str        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r0</span><span class="pun">],</span><span class="pln"> </span><span class="com">#4</span><span class="pln"> 
</span><span class="lit">00220</span><span class="pun">:</span><span class="pln">         teq        r0</span><span class="pun">,</span><span class="pln"> r6 
</span><span class="lit">00221</span><span class="pun">:</span><span class="pln">         bne        </span><span class="lit">1b</span><span class="pln"> 
</span><span class="lit">00222</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00223</span><span class="pun">:</span><span class="pln">         ldr        r7</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r10</span><span class="pun">,</span><span class="pln"> </span><span class="com">#PROCINFO_MM_MMUFLAGS] @ mm_mmuflags</span><span class="pln"> 
</span><span class="lit">00224</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00225</span><span class="pun">:</span><span class="pln">         </span><span class="com">/* 
00226:          * Create identity mapping for first MB of kernel to 
00227:          * cater for the MMU enable.  This identity mapping 
00228:          * will be removed by paging_init().  We use our current program 
00229:          * counter to determine corresponding section base address. 
00230:          */</span><span class="pln"> 
</span><span class="lit">00231</span><span class="pun">:</span><span class="pln">         mov        r6</span><span class="pun">,</span><span class="pln"> pc</span><span class="pun">,</span><span class="pln"> lsr </span><span class="com">#20                        @ start of kernel section</span><span class="pln"> 
</span><span class="lit">00232</span><span class="pun">:</span><span class="pln">         orr        r3</span><span class="pun">,</span><span class="pln"> r7</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> lsl </span><span class="com">#20                @ flags + kernel base</span><span class="pln"> 
</span><span class="lit">00233</span><span class="pun">:</span><span class="pln">         str        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r4</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> lsl </span><span class="com">#2]                @ identity mapping</span><span class="pln"> 
</span><span class="lit">00234</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00235</span><span class="pun">:</span><span class="pln">         </span><span class="com">/* 
00236:          * Now setup the pagetables for our kernel direct 
00237:          * mapped region. 
00238:          */</span><span class="pln"> 
</span><span class="lit">00239</span><span class="pun">:</span><span class="pln">         add        r0</span><span class="pun">,</span><span class="pln"> r4</span><span class="pun">,</span><span class="pln">  </span><span class="com">#(TEXTADDR & 0xff000000) >> 18        @ start of kernel</span><span class="pln"> 
</span><span class="lit">00240</span><span class="pun">:</span><span class="pln">         str        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#(TEXTADDR & 0x00f00000) >> 18]!</span><span class="pln"> 
</span><span class="lit">00241</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00242</span><span class="pun">:</span><span class="pln">         ldr        r6</span><span class="pun">,</span><span class="pln"> </span><span class="pun">=(</span><span class="pln">_end </span><span class="pun">-</span><span class="pln"> PAGE_OFFSET </span><span class="pun">-</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln">        </span><span class="pun">@</span><span class="pln"> r6 </span><span class="pun">=</span><span class="pln"> number of sections 
</span><span class="lit">00243</span><span class="pun">:</span><span class="pln">         mov        r6</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> lsr </span><span class="com">#20                        @ needed for kernel minus 1</span><span class="pln"> 
</span><span class="lit">00244</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00245</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">:</span><span class="pln">        add        r3</span><span class="pun">,</span><span class="pln"> r3</span><span class="pun">,</span><span class="pln"> </span><span class="com">#1 << 20</span><span class="pln"> 
</span><span class="lit">00246</span><span class="pun">:</span><span class="pln">         str        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#4]!</span><span class="pln"> 
</span><span class="lit">00247</span><span class="pun">:</span><span class="pln">         subs        r6</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> </span><span class="com">#1</span><span class="pln"> 
</span><span class="lit">00248</span><span class="pun">:</span><span class="pln">         bgt        </span><span class="lit">1b</span><span class="pln"> 
</span><span class="lit">00249</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00250</span><span class="pun">:</span><span class="pln">         </span><span class="com">/* 
00251:          * Then map first 1MB of ram in case it contains our boot params. 
00252:          */</span><span class="pln"> 
</span><span class="lit">00253</span><span class="pun">:</span><span class="pln">         add        r0</span><span class="pun">,</span><span class="pln"> r4</span><span class="pun">,</span><span class="pln"> </span><span class="com">#PAGE_OFFSET >> 18</span><span class="pln"> 
</span><span class="lit">00254</span><span class="pun">:</span><span class="pln">         orr        r6</span><span class="pun">,</span><span class="pln"> r7</span><span class="pun">,</span><span class="pln"> </span><span class="com">#PHYS_OFFSET</span><span class="pln"> 
</span><span class="lit">00255</span><span class="pun">:</span><span class="pln">         str        r6</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r0</span><span class="pun">]</span><span class="pln"> 
         
        </span><span class="pun">...</span><span class="pln"> 
         
</span><span class="lit">00314</span><span class="pun">:</span><span class="pln">        mov        pc</span><span class="pun">,</span><span class="pln"> lr 
</span><span class="lit">00315</span><span class="pun">:</span><span class="pln">        </span><span class="pun">.</span><span class="pln">ltorg</span>

206, 207行: 函数声明

208行: 通过宏 pgtbl 将r4设置成页表的基地址(物理地址)

宏pgtbl 在 arch/arm/kernel/head.S 中:

<span class="pln">        </span><span class="lit">00042</span><span class="pun">:</span><span class="pln">        </span><span class="pun">.</span><span class="pln">macro        pgtbl</span><span class="pun">,</span><span class="pln"> rd 
        </span><span class="lit">00043</span><span class="pun">:</span><span class="pln">        ldr        </span><span class="pun">\</span><span class="pln">rd</span><span class="pun">,</span><span class="pln"> </span><span class="pun">=(</span><span class="pln">__virt_to_phys</span><span class="pun">(</span><span class="pln">KERNEL_RAM_ADDR </span><span class="pun">-</span><span class="pln"> </span><span class="lit">0x4000</span><span class="pun">))</span><span class="pln"> 
        </span><span class="lit">00044</span><span class="pun">:</span><span class="pln">        </span><span class="pun">.</span><span class="pln">endm 
 
        </span><span class="pun">可以看到,页表是位于</span><span class="pln"> KERNEL_RAM_ADDR </span><span class="pun">下面</span><span class="pln"> </span><span class="lit">16k</span><span class="pln"> </span><span class="pun">的位置</span><span class="pln"> 
        </span><span class="pun">宏</span><span class="pln"> __virt_to_phys </span><span class="pun">是在</span><span class="pln">incude</span><span class="pun">/</span><span class="kwd">asm</span><span class="pun">-</span><span class="pln">arm</span><span class="pun">/</span><span class="pln">memory</span><span class="pun">.</span><span class="pln">h </span><span class="pun">中:</span><span class="pln"> 
         
        </span><span class="lit">00125</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifndef</span><span class="pln"> __virt_to_phys 
        </span><span class="lit">00126</span><span class="pun">:</span><span class="pln"> </span><span class="com">#define</span><span class="pln"> __virt_to_phys</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span><span class="pln">        </span><span class="pun">((</span><span class="pln">x</span><span class="pun">)</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> PAGE_OFFSET </span><span class="pun">+</span><span class="pln"> PHYS_OFFSET</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00127</span><span class="pun">:</span><span class="pln"> </span><span class="com">#define</span><span class="pln"> __phys_to_virt</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span><span class="pln">        </span><span class="pun">((</span><span class="pln">x</span><span class="pun">)</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> PHYS_OFFSET </span><span class="pun">+</span><span class="pln"> PAGE_OFFSET</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00128</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln">   </span>

下面从213行 - 221行, 是将这16k 的页表清0.

213行: r0 =r4, 将页表基地址存在r0中

214行: 将 r3 置成0

215行: r6 = 页表基地址 + 16k, 可以看到这是页表的尾地址

216 - 221 行: 循环,从 r0 到 r6 将这16k页表用0填充. 223行: 获得proc_info_list的cpu_mm_mmu_flags的值,并存储到 r7中. (宏PROCINFO_MM_MMUFLAGS是在arch/arm/kernel/asm-offset.c中定义)

231行: 通过pc值的高12位(右移20位),得到kernel的section,并存储到r6中.因为当前是通过运行时地址得到的kernel的 section,因而是物理地址.

232行: r3 =r7 | (r6 << 20); flags + kernel base,得到页表中需要设置的值.

233行: 设置页表: mem+ r6 * 4 = r3

这里,因为页表的每一项是32 bits(4 bytes),所以要乘以4(<<2).

上面这三行,设置了kernel的第一个section(物理地址所在的page entry)的页表项

239, 240行: TEXTADDR是内核的起始虚拟地址(0xc0008000), 这两行是设置kernel起始虚拟地址的页表项(注意,这里设置的页表项和上面的231 - 233行设置的页表项是不同的 )

执行完后,r0指向kernel的第2个section的虚拟地址所在的页表项.

/  TODO: 这两行的code很奇怪,为什么要先取TEXTADDR的高8位(Bit31:24)0xff000000,然后再取后面的8位 (Bit23:20)0x00f00000/

242行: 这一行计算kernel镜像的大小(bytes).

end 是在vmlinux.lds.S中162行定义的,标记kernel的结束位置(虚拟地址):

<span class="pln">        </span><span class="lit">00158</span><span class="pln">                </span><span class="pun">.</span><span class="pln">bss </span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> 
        </span><span class="lit">00159</span><span class="pln">                __bss_start </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln">        </span><span class="com">/* BSS                                */</span><span class="pln"> 
        </span><span class="lit">00160</span><span class="pln">                </span><span class="pun">*(.</span><span class="pln">bss</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00161</span><span class="pln">                </span><span class="pun">*(</span><span class="pln">COMMON</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00162</span><span class="pln">                _end </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln"> 
        </span><span class="lit">00163</span><span class="pln">        </span><span class="pun">}</span>

kernel的size =  end - PAGE_OFFSET -1, 这里 减1的原因是因为 end 是 location counter,它的地址是kernel镜像后面的一个byte的地址.

243行: 地址右移20位,计算出kernel有多少sections,并将结果存到r6中

245 - 248行: 这几行用来填充kernel所有section虚拟地址对应的页表项.

253行: 将r0设置为RAM第一兆虚拟地址的页表项地址(page entry)

254行: r7中存储的是mmu flags, 逻辑或上RAM的起始物理地址,得到RAM第一个MB页表项的值.

255行: 设置RAM的第一个MB虚拟地址的页表.

上面这三行是用来设置RAM中第一兆虚拟地址的页表. 之所以要设置这个页表项的原因是RAM的第一兆内存中可能存储着boot params.

这样,kernel所需要的基本的页表我们都设置完了, 如下图所示

4. 调用平台特定的 cpu_flush 函数

当 create_page_tables 返回之后

此时,一些特定寄存器的值如下所示: r4 = pgtbl (page table 的物理基地址)r8 = machine info (struct machine_desc的基地址)r9 = cpu id (通过cp15协处理器获得的cpu id)r10 = procinfo (struct proc_info_list的基地址)

在我们需要在开启mmu之前,做一些必须的工作:清除ICache, 清除 DCache, 清除 Writebuffer, 清除TLB等.

这些一般是通过cp15协处理器来实现的,并且是平台相关的. 这就是 cpu_flush 需要做的工作.

在 arch/arm/kernel/head.S中

<span class="lit">00091</span><span class="pun">:</span><span class="pln">         ldr        r13</span><span class="pun">,</span><span class="pln"> __switch_data                </span><span class="pun">@</span><span class="pln"> address to jump to after 
</span><span class="lit">00092</span><span class="pun">:</span><span class="pln">                                                 </span><span class="pun">@</span><span class="pln"> mmu has been enabled      
</span><span class="lit">00093</span><span class="pun">:</span><span class="pln">         adr        lr</span><span class="pun">,</span><span class="pln"> __enable_mmu                </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">(</span><span class="pln">PIC</span><span class="pun">)</span><span class="pln"> address      
</span><span class="lit">00094</span><span class="pun">:</span><span class="pln">         add        pc</span><span class="pun">,</span><span class="pln"> r10</span><span class="pun">,</span><span class="pln"> </span><span class="com">#PROCINFO_INITFUNC            </span>

第91行: 将r13设置为 switch_data 的地址

第92行: 将lr设置为 enable_mmu 的地址

第93行: r10存储的是procinfo的基地址, PROCINFO_INITFUNC是在 arch/arm/kernel/asm-offset.c 中107行定义.

则该行将pc设为 proc_info_list的  cpu_flush 函数的地址, 即下面跳转到该函数. 在分析 lookup_processor_type 的时候,我们已经知道,对于 ARM926EJS 来说,其 cpu_flush指向的是函数 arm926_setup

下面我们来分析函数  arm926_setup
在 arch/arm/mm/proc-arm926.S 中:

<span class="lit">00391</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __arm926_setup</span><span class="pun">,</span><span class="pln"> </span><span class="com">#function</span><span class="pln"> 
</span><span class="lit">00392</span><span class="pun">:</span><span class="pln"> __arm926_setup</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00393</span><span class="pun">:</span><span class="pln">         mov        r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#0</span><span class="pln"> 
</span><span class="lit">00394</span><span class="pun">:</span><span class="pln">         mcr        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> c7</span><span class="pun">,</span><span class="pln"> c7                </span><span class="pun">@</span><span class="pln"> invalidate I</span><span class="pun">,</span><span class="pln">D caches on v4 
</span><span class="lit">00395</span><span class="pun">:</span><span class="pln">         mcr        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> c7</span><span class="pun">,</span><span class="pln"> c10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span class="pln">                </span><span class="pun">@</span><span class="pln"> drain write buffer on v4 
</span><span class="lit">00396</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_MMU 
</span><span class="lit">00397</span><span class="pun">:</span><span class="pln">         mcr        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> c8</span><span class="pun">,</span><span class="pln"> c7                </span><span class="pun">@</span><span class="pln"> invalidate I</span><span class="pun">,</span><span class="pln">D </span><span class="typ">TLBs</span><span class="pln"> on v4 
</span><span class="lit">00398</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
</span><span class="lit">00399</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00400</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00401</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_CPU_DCACHE_WRITETHROUGH 
</span><span class="lit">00402</span><span class="pun">:</span><span class="pln">         mov        r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#4                                @ disable write-back on caches explicitly</span><span class="pln"> 
</span><span class="lit">00403</span><span class="pun">:</span><span class="pln">         mcr        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> c15</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> 
</span><span class="lit">00404</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
</span><span class="lit">00405</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00406</span><span class="pun">:</span><span class="pln">         adr        r5</span><span class="pun">,</span><span class="pln"> arm926_crval 
</span><span class="lit">00407</span><span class="pun">:</span><span class="pln">         ldmia        r5</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r5</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">}</span><span class="pln"> 
</span><span class="lit">00408</span><span class="pun">:</span><span class="pln">         mrc        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> c1</span><span class="pun">,</span><span class="pln"> c0                </span><span class="pun">@</span><span class="pln"> </span><span class="kwd">get</span><span class="pln"> control </span><span class="kwd">register</span><span class="pln"> v4 
</span><span class="lit">00409</span><span class="pun">:</span><span class="pln">         bic        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> r5 
</span><span class="lit">00410</span><span class="pun">:</span><span class="pln">         orr        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> r6 
</span><span class="lit">00411</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_CPU_CACHE_ROUND_ROBIN 
</span><span class="lit">00412</span><span class="pun">:</span><span class="pln">         orr        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#0x4000                        @ .1.. .... .... ....</span><span class="pln"> 
</span><span class="lit">00413</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
</span><span class="lit">00414</span><span class="pun">:</span><span class="pln">         mov        pc</span><span class="pun">,</span><span class="pln"> lr         
</span><span class="lit">00415</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">size        __arm926_setup</span><span class="pun">,</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> __arm926_setup 
</span><span class="lit">00416</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00417</span><span class="pun">:</span><span class="pln">         </span><span class="com">/* 
00418:          *  R 
00419:          * .RVI ZFRS BLDP WCAM 
00420:          * .011 0001 ..11 0101 
00421:          * 
00422:          */</span><span class="pln"> 
</span><span class="lit">00423</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        arm926_crval</span><span class="pun">,</span><span class="pln"> </span><span class="com">#object</span><span class="pln"> 
</span><span class="lit">00424</span><span class="pun">:</span><span class="pln"> arm926_crval</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00425</span><span class="pun">:</span><span class="pln">         crval        clear</span><span class="pun">=</span><span class="lit">0x00007f3f</span><span class="pun">,</span><span class="pln"> mmuset</span><span class="pun">=</span><span class="lit">0x00003135</span><span class="pun">,</span><span class="pln"> ucset</span><span class="pun">=</span><span class="lit">0x00001134</span>

第391, 392行: 是函数声明

第393行: 将r0设置为0

第394行: 清除(invalidate)Instruction Cache 和 Data Cache.

第395行: 清除(drain) Write Buffer.

第396 - 398行: 如果有配置了MMU,则需要清除(invalidate)Instruction TLB 和Data TLB

接下来,是对控制寄存器c1进行配置,请参考 ARM926 TRM.

第401 - 404行: 如果配置了Data Cache使用writethrough方式, 需要关掉write-back. 第406行: 取arm926_crval的地址到r5中, arm926_crval 在第424行 第407行: 这里我们需要看一下424和425行,其中用到了宏crval,crval是在 arch/arm/mm/proc-macro.S 中:

<span class="pln">        </span><span class="lit">00053</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">macro        crval</span><span class="pun">,</span><span class="pln"> clear</span><span class="pun">,</span><span class="pln"> mmuset</span><span class="pun">,</span><span class="pln"> ucset 
        </span><span class="lit">00054</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_MMU 
        </span><span class="lit">00055</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">word        </span><span class="pun">\</span><span class="pln">clear 
        </span><span class="lit">00056</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">word        </span><span class="pun">\</span><span class="pln">mmuset 
        </span><span class="lit">00057</span><span class="pun">:</span><span class="pln"> </span><span class="com">#else</span><span class="pln"> 
        </span><span class="lit">00058</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">word        </span><span class="pun">\</span><span class="pln">clear 
        </span><span class="lit">00059</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">word        </span><span class="pun">\</span><span class="pln">ucset 
        </span><span class="lit">00060</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
        </span><span class="lit">00061</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">endm</span>
配合425行,我们可以看出,首先在arm926_crval的地址处存放了clear的值,然后接下来的地址存放了mmuset的值(对于配置了 MMU的情况)

所以,在407行中,我们将clear和mmuset的值分别存到了r5, r6中

第408行: 获得控制寄存器c1的值

第409行: 将r0中的 clear (r5) 对应的位都清除掉

第410行: 设置r0中 mmuset (r6) 对应的位

第411 - 413行: 如果配置了使用 round robin方式,需要设置控制寄存器c1的 Bit16

第412行: 取lr的值到pc中. 而lr中的值存放的是 enable_mmu 的地址(arch/arm/kernel/head.S 93行),所以,接下来就是跳转到函数enable_mmu

5. 开启mmu

开启mmu是又函数  enable_mmu 实现的.

在进入  enable_mmu 的时候, r0中已经存放了控制寄存器c1的一些配置(在上一步中进行的设置), 但是并没有真正的打开mmu,

在  enable_mmu 中,我们将打开mmu.

此时,一些特定寄存器的值如下所示:

r0 = c1 parameters (用来配置控制寄存器的参数)r4 = pgtbl (page table 的物理基地址)r8 = machine info (struct machine_desc的基地址)r9 = cpu id (通过cp15协处理器获得的cpu id)r10 = procinfo (struct proc_info_list的基地址)

在 arch/arm/kernel/head.S 中:

<span class="lit">00146</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __enable_mmu</span><span class="pun">,</span><span class="pln"> </span><span class="pun">%</span><span class="kwd">function</span><span class="pln"> 
</span><span class="lit">00147</span><span class="pun">:</span><span class="pln"> __enable_mmu</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00148</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_ALIGNMENT_TRAP 
</span><span class="lit">00149</span><span class="pun">:</span><span class="pln">         orr        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#CR_A</span><span class="pln"> 
</span><span class="lit">00150</span><span class="pun">:</span><span class="pln"> </span><span class="com">#else</span><span class="pln"> 
</span><span class="lit">00151</span><span class="pun">:</span><span class="pln">         bic        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#CR_A</span><span class="pln"> 
</span><span class="lit">00152</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
</span><span class="lit">00153</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_CPU_DCACHE_DISABLE 
</span><span class="lit">00154</span><span class="pun">:</span><span class="pln">         bic        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#CR_C</span><span class="pln"> 
</span><span class="lit">00155</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
</span><span class="lit">00156</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_CPU_BPREDICT_DISABLE 
</span><span class="lit">00157</span><span class="pun">:</span><span class="pln">         bic        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#CR_Z</span><span class="pln"> 
</span><span class="lit">00158</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
</span><span class="lit">00159</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_CPU_ICACHE_DISABLE 
</span><span class="lit">00160</span><span class="pun">:</span><span class="pln">         bic        r0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#CR_I</span><span class="pln"> 
</span><span class="lit">00161</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
</span><span class="lit">00162</span><span class="pun">:</span><span class="pln">         mov        r5</span><span class="pun">,</span><span class="pln"> </span><span class="com">#(domain_val(DOMAIN_USER, DOMAIN_MANAGER) | \</span><span class="pln"> 
</span><span class="lit">00163</span><span class="pun">:</span><span class="pln">                       domain_val</span><span class="pun">(</span><span class="pln">DOMAIN_KERNEL</span><span class="pun">,</span><span class="pln"> DOMAIN_MANAGER</span><span class="pun">)</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
</span><span class="lit">00164</span><span class="pun">:</span><span class="pln">                       domain_val</span><span class="pun">(</span><span class="pln">DOMAIN_TABLE</span><span class="pun">,</span><span class="pln"> DOMAIN_MANAGER</span><span class="pun">)</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="pun">\</span><span class="pln"> 
</span><span class="lit">00165</span><span class="pun">:</span><span class="pln">                       domain_val</span><span class="pun">(</span><span class="pln">DOMAIN_IO</span><span class="pun">,</span><span class="pln"> DOMAIN_CLIENT</span><span class="pun">))</span><span class="pln"> 
</span><span class="lit">00166</span><span class="pun">:</span><span class="pln">         mcr        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> c3</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pln">                </span><span class="pun">@</span><span class="pln"> load domain access </span><span class="kwd">register</span><span class="pln"> 
</span><span class="lit">00167</span><span class="pun">:</span><span class="pln">         mcr        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r4</span><span class="pun">,</span><span class="pln"> c2</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pln">                </span><span class="pun">@</span><span class="pln"> load page table pointer 
</span><span class="lit">00168</span><span class="pun">:</span><span class="pln">         b        __turn_mmu_on 
</span><span class="lit">00169</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00170</span><span class="pun">:</span><span class="pln"> </span><span class="com">/* 
00171:  * Enable the MMU.  This completely changes the structure of the visible 
00172:  * memory space.  You will not be able to trace execution through this. 
00173:  * If you have an enquiry about this, *please* check the linux-arm-kernel 
00174:  * mailing list archives BEFORE sending another post to the list. 
00175:  * 
00176:  *  r0  = cp#15 control register 
00177:  *  r13 = *virtual* address to jump to upon completion 
00178:  * 
00179:  * other registers depend on the function called upon completion 
00180:  */</span><span class="pln"> 
</span><span class="lit">00181</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">align        </span><span class="lit">5</span><span class="pln"> 
</span><span class="lit">00182</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __turn_mmu_on</span><span class="pun">,</span><span class="pln"> </span><span class="pun">%</span><span class="kwd">function</span><span class="pln"> 
</span><span class="lit">00183</span><span class="pun">:</span><span class="pln"> __turn_mmu_on</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00184</span><span class="pun">:</span><span class="pln">         mov        r0</span><span class="pun">,</span><span class="pln"> r0 
</span><span class="lit">00185</span><span class="pun">:</span><span class="pln">         mcr        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> c1</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pln">                </span><span class="pun">@</span><span class="pln"> write control reg 
</span><span class="lit">00186</span><span class="pun">:</span><span class="pln">         mrc        p15</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> r3</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> c0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pln">                </span><span class="pun">@</span><span class="pln"> read id reg 
</span><span class="lit">00187</span><span class="pun">:</span><span class="pln">         mov        r3</span><span class="pun">,</span><span class="pln"> r3 
</span><span class="lit">00188</span><span class="pun">:</span><span class="pln">         mov        r3</span><span class="pun">,</span><span class="pln"> r3 
</span><span class="lit">00189</span><span class="pun">:</span><span class="pln">         mov        pc</span><span class="pun">,</span><span class="pln"> r13</span>

第146, 147行: 函数声明

第148 - 161行: 根据相应的配置,设置r0中的相应的Bit. (r0 将用来配置控制寄存器c1)

第162 - 165行: 设置 domain 参数r5.(r5 将用来配置domain)

第166行: 配置 domain (详细信息清参考arm相关手册)

第167行: 配置页表在存储器中的位置(set ttb).这里页表的基地址是r4, 通过写cp15的c2寄存器来设置页表基地址.

第168行: 跳转到 turn_mmu_on. 从名称我们可以猜到,下面是要真正打开mmu了.

(继续向下看,我们会发现, turn_mmu_on就下当前代码的下方,为什么要跳转一下呢? 这是有原因的. go on)

第169 - 180行: 空行和注释. 这里的注释我们可以看到, r0是cp15控制寄存器的内容, r13存储了完成后需要跳转的虚拟地址(因为完成后mmu已经打开了,都是虚拟地址了).

第181行: .algin 5 这句是cache line对齐. 我们可以看到下面一行就是 turn_mmu_on, 之所以

第182 - 183行: turn_mmu_on 的函数声明. 这里我们可以看到, turn_mmu_on 是紧接着上面第168行的跳转指令的,只是中间在第181行多了一个cache line对齐.

这么做的原因是: 下面我们要进行真正的打开mmu操作了, 我们要把打开mmu的操作放到一个单独的cache line上. 而在之前的"启动条件"一节我们说了,I Cache是可以打开也可以关闭的,这里这么做的原因是要保证在I Cache打开的时候,打开mmu的操作也能正常执行.

第184行: 这是一个空操作,相当于nop. 在arm中,nop操作经常用指令 mov rd, rd 来实现.

注意: 为什么这里要有一个nop,我思考了很长时间,这里是我的猜测,可能不是正确的: 因为之前设置了页表基地址(set ttb),到下一行(185行)打开mmu操作,中间的指令序列是这样的:

  • set ttb(第167行)
  • branch(第168行)
  • nop(第184行)
  • enable mmu(第185行)
对于arm的五级流水线: fetch - decode - execute - memory - write

他们执行的情况如下图所示:

这里需要说明的是,branch操作会在3个cycle中完成,并且会导致重新取指.

从这个图我们可以看出来,在enable mmu操作取指的时候, set ttb操作刚好完成.
第185行: 写cp15的控制寄存器c1, 这里是打开mmu的操作,同时会打开cache等(根据r0相应的配置)

第186行: 读取id寄存器.

第187 - 188行: 两个nop.

第189行: 取r13到pc中,我们前面已经看到了, r13中存储的是 switch_data (在 arch/arm/kernel/head.S 91行),下面会跳到switch_data.

第187,188行的两个nop是非常重要的,因为在185行打开mmu操作之后,要等到3个cycle之后才会生效,这和arm的流水线有关系. 因而,在打开mmu操作之后的加了两个nop操作.

6. 切换数据

在 arch/arm/kernel/head-common.S 中:
<span class="lit">00014</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __switch_data</span><span class="pun">,</span><span class="pln"> </span><span class="pun">%</span><span class="kwd">object</span><span class="pln"> 
</span><span class="lit">00015</span><span class="pun">:</span><span class="pln"> __switch_data</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00016</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __mmap_switched 
</span><span class="lit">00017</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __data_loc                        </span><span class="pun">@</span><span class="pln"> r4 
</span><span class="lit">00018</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __data_start                        </span><span class="pun">@</span><span class="pln"> r5 
</span><span class="lit">00019</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __bss_start                        </span><span class="pun">@</span><span class="pln"> r6 
</span><span class="lit">00020</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        _end                                </span><span class="pun">@</span><span class="pln"> r7 
</span><span class="lit">00021</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        processor_id                        </span><span class="pun">@</span><span class="pln"> r4 
</span><span class="lit">00022</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        __machine_arch_type                </span><span class="pun">@</span><span class="pln"> r5 
</span><span class="lit">00023</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        cr_alignment                        </span><span class="pun">@</span><span class="pln"> r6 
</span><span class="lit">00024</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="kwd">long</span><span class="pln">        init_thread_union </span><span class="pun">+</span><span class="pln"> THREAD_START_SP </span><span class="pun">@</span><span class="pln"> sp 
</span><span class="lit">00025</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00026</span><span class="pun">:</span><span class="pln"> </span><span class="com">/* 
00027:  * The following fragment of code is executed with the MMU on in MMU mode, 
00028:  * and uses absolute addresses; this is not position independent. 
00029:  * 
00030:  *  r0  = cp#15 control register 
00031:  *  r1  = machine ID 
00032:  *  r9  = processor ID 
00033:  */</span><span class="pln"> 
</span><span class="lit">00034</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">type        __mmap_switched</span><span class="pun">,</span><span class="pln"> </span><span class="pun">%</span><span class="kwd">function</span><span class="pln"> 
</span><span class="lit">00035</span><span class="pun">:</span><span class="pln"> __mmap_switched</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00036</span><span class="pun">:</span><span class="pln">         adr        r3</span><span class="pun">,</span><span class="pln"> __switch_data </span><span class="pun">+</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> 
</span><span class="lit">00037</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00038</span><span class="pun">:</span><span class="pln">         ldmia        r3</span><span class="pun">!,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r4</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> r7</span><span class="pun">}</span><span class="pln"> 
</span><span class="lit">00039</span><span class="pun">:</span><span class="pln">         cmp        r4</span><span class="pun">,</span><span class="pln"> r5                                </span><span class="pun">@</span><span class="pln"> </span><span class="typ">Copy</span><span class="pln"> data segment </span><span class="kwd">if</span><span class="pln"> needed 
</span><span class="lit">00040</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">:</span><span class="pln">        cmpne        r5</span><span class="pun">,</span><span class="pln"> r6 
</span><span class="lit">00041</span><span class="pun">:</span><span class="pln">         ldrne        fp</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r4</span><span class="pun">],</span><span class="pln"> </span><span class="com">#4</span><span class="pln"> 
</span><span class="lit">00042</span><span class="pun">:</span><span class="pln">         strne        fp</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r5</span><span class="pun">],</span><span class="pln"> </span><span class="com">#4</span><span class="pln"> 
</span><span class="lit">00043</span><span class="pun">:</span><span class="pln">         bne        </span><span class="lit">1b</span><span class="pln"> 
</span><span class="lit">00044</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00045</span><span class="pun">:</span><span class="pln">         mov        fp</span><span class="pun">,</span><span class="pln"> </span><span class="com">#0                                @ Clear BSS (and zero fp)</span><span class="pln"> 
</span><span class="lit">00046</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">:</span><span class="pln">        cmp        r6</span><span class="pun">,</span><span class="pln"> r7 
</span><span class="lit">00047</span><span class="pun">:</span><span class="pln">         strcc        fp</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r6</span><span class="pun">],</span><span class="com">#4</span><span class="pln"> 
</span><span class="lit">00048</span><span class="pun">:</span><span class="pln">         bcc        </span><span class="lit">1b</span><span class="pln"> 
</span><span class="lit">00049</span><span class="pun">:</span><span class="pln"> 
</span><span class="lit">00050</span><span class="pun">:</span><span class="pln">         ldmia        r3</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r4</span><span class="pun">,</span><span class="pln"> r5</span><span class="pun">,</span><span class="pln"> r6</span><span class="pun">,</span><span class="pln"> sp</span><span class="pun">}</span><span class="pln"> 
</span><span class="lit">00051</span><span class="pun">:</span><span class="pln">         str        r9</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r4</span><span class="pun">]</span><span class="pln">                        </span><span class="pun">@</span><span class="pln"> </span><span class="typ">Save</span><span class="pln"> processor ID 
</span><span class="lit">00052</span><span class="pun">:</span><span class="pln">         str        r1</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln">r5</span><span class="pun">]</span><span class="pln">                        </span><span class="pun">@</span><span class="pln"> </span><span class="typ">Save</span><span class="pln"> machine type 
</span><span class="lit">00053</span><span class="pun">:</span><span class="pln">         bic        r4</span><span class="pun">,</span><span class="pln"> r0</span><span class="pun">,</span><span class="pln"> </span><span class="com">#CR_A                        @ Clear 'A' bit</span><span class="pln"> 
</span><span class="lit">00054</span><span class="pun">:</span><span class="pln">         stmia        r6</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">r0</span><span class="pun">,</span><span class="pln"> r4</span><span class="pun">}</span><span class="pln">                        </span><span class="pun">@</span><span class="pln"> </span><span class="typ">Save</span><span class="pln"> control </span><span class="kwd">register</span><span class="pln"> values 
</span><span class="lit">00055</span><span class="pun">:</span><span class="pln">         b        start_kernel</span>

第14, 15行: 函数声明

第16 - 24行: 定义了一些地址,例如第16行存储的是 mmap_switched 的地址, 第17行存储的是 data_loc 的地址 ......

第34, 35行: 函数 mmap_switched

第36行: 取 switch_data + 4的地址到r3. 从上文可以看到这个地址就是第17行的地址.

第37行: 依次取出从第17行到第20行的地址,存储到r4,r5, r6, r7 中. 并且累加r3的值.当执行完后, r3指向了第21行的位置.

对照上文,我们可以得知:

  • r4 - data_loc
  • r5 - data_start
  • r6 - bss_start
  • r7 - end
这几个符号都是在 arch/arm/kernel/vmlinux.lds.S 中定义的变量:
<span class="pln">        </span><span class="lit">00102</span><span class="pun">:</span><span class="pln"> </span><span class="com">#ifdef</span><span class="pln"> CONFIG_XIP_KERNEL 
        </span><span class="lit">00103</span><span class="pun">:</span><span class="pln">         __data_loc </span><span class="pun">=</span><span class="pln"> ALIGN</span><span class="pun">(</span><span class="lit">4</span><span class="pun">);</span><span class="pln">                </span><span class="com">/* location in binary */</span><span class="pln"> 
        </span><span class="lit">00104</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> PAGE_OFFSET </span><span class="pun">+</span><span class="pln"> TEXT_OFFSET</span><span class="pun">;</span><span class="pln"> 
        </span><span class="lit">00105</span><span class="pun">:</span><span class="pln"> </span><span class="com">#else</span><span class="pln"> 
        </span><span class="lit">00106</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> ALIGN</span><span class="pun">(</span><span class="pln">THREAD_SIZE</span><span class="pun">);</span><span class="pln"> 
        </span><span class="lit">00107</span><span class="pun">:</span><span class="pln">         __data_loc </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln"> 
        </span><span class="lit">00108</span><span class="pun">:</span><span class="pln"> </span><span class="com">#endif</span><span class="pln"> 
        </span><span class="lit">00109</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00110</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">data </span><span class="pun">:</span><span class="pln"> AT</span><span class="pun">(</span><span class="pln">__data_loc</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> 
        </span><span class="lit">00111</span><span class="pun">:</span><span class="pln">                 __data_start </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln">        </span><span class="com">/* address in memory */</span><span class="pln"> 
        </span><span class="lit">00112</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00113</span><span class="pun">:</span><span class="pln">                 </span><span class="com">/* 
        00114:                  * first, the init task union, aligned 
        00115:                  * to an 8192 byte boundary. 
        00116:                  */</span><span class="pln"> 
        </span><span class="lit">00117</span><span class="pun">:</span><span class="pln">                 </span><span class="pun">*(.</span><span class="pln">init</span><span class="pun">.</span><span class="pln">task</span><span class="pun">)</span><span class="pln"> 
         
                </span><span class="pun">......</span><span class="pln"> 
                
        </span><span class="lit">00158</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">bss </span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> 
        </span><span class="lit">00159</span><span class="pun">:</span><span class="pln">                 __bss_start </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln">        </span><span class="com">/* BSS                                */</span><span class="pln"> 
        </span><span class="lit">00160</span><span class="pun">:</span><span class="pln">                 </span><span class="pun">*(.</span><span class="pln">bss</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00161</span><span class="pun">:</span><span class="pln">                 </span><span class="pun">*(</span><span class="pln">COMMON</span><span class="pun">)</span><span class="pln"> 
        </span><span class="lit">00162</span><span class="pun">:</span><span class="pln">                 _end </span><span class="pun">=</span><span class="pln"> </span><span class="pun">.;</span><span class="pln"> 
        </span><span class="lit">00163</span><span class="pun">:</span><span class="pln">         </span><span class="pun">}</span>

对于这四个变量,我们简单的介绍一下:
  • data_loc 是数据存放的位置
  • data_start 是数据开始的位置
  • bss_start 是bss开始的位置
  • end 是bss结束的位置, 也是内核结束的位置
其中对第110行的指令讲解一下: 这里定义了.data 段,后面的AT( data_loc) 的意思是这部分的内容是在 data_loc中存储的(要注意,储存的位置和链接的位置是可以不相同的). 关于 AT 详细的信息请参考 ld.info
第38行: 比较  data_loc 和  data_start

第39 - 43行: 这几行是判断数据存储的位置和数据的开始的位置是否相等,如果不相等,则需要搬运数据,从 data_loc 将数据搬到 data_start.

其中  bss_start 是bss的开始的位置,也标志了 data 结束的位置,因而用其作为判断数据是否搬运完成.

第45 - 48行: 是清除 bss 段的内容,将其都置成0. 这里使用 end 来判断 bss 的结束位置.

第50行: 因为在第38行的时候,r3被更新到指向第21行的位置.因而这里取得r4,r5, r6, sp的值分别是:

  • r4 - processor_id
  • r5 - machine_arch_type
  • r6 - cr_alignment
  • sp - init_thread_union + THREAD_START_SP

processor_id 和  machine_arch_type 这两个变量是在 arch/arm/kernel/setup.c 中 第62, 63行中定义的. cr_alignment 是在 arch/arm/kernel/entry-armv.S 中定义的:
<span class="pln">        </span><span class="lit">00182</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">globl        cr_alignment 
        </span><span class="lit">00183</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">globl        cr_no_alignment 
        </span><span class="lit">00184</span><span class="pun">:</span><span class="pln"> cr_alignment</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00185</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">space        </span><span class="lit">4</span><span class="pln"> 
        </span><span class="lit">00186</span><span class="pun">:</span><span class="pln"> cr_no_alignment</span><span class="pun">:</span><span class="pln"> 
        </span><span class="lit">00187</span><span class="pun">:</span><span class="pln">         </span><span class="pun">.</span><span class="pln">space        </span><span class="lit">4</span>

init_thread_union 是 init进程的基地址. 在 arch/arm/kernel/init_task.c 中:

<span class="pln">        </span><span class="lit">00033</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">union</span><span class="pln"> thread_union init_thread_union 
        </span><span class="lit">00034</span><span class="pun">:</span><span class="pln">         __attribute__</span><span class="pun">((</span><span class="pln">__section__</span><span class="pun">(</span><span class="str">".init.task"</span><span class="pun">)))</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> 
        </span><span class="lit">00035</span><span class="pun">:</span><span class="pln">                 </span><span class="pun">{</span><span class="pln"> INIT_THREAD_INFO</span><span class="pun">(</span><span class="pln">init_task</span><span class="pun">)</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> </span>
对照 vmlnux.lds.S 中的 的117行,我们可以知道init task是存放在 .data 段的开始8k, 并且是THREAD_SIZE(8k)对齐的

第51行: 将r9中存放的 processor id (在arch/arm/kernel/head.S 75行) 赋值给变量 processor_id

第52行: 将r1中存放的 machine id (见"启动条件"一节)赋值给变量 machine_arch_type

第53行: 清除r0中的 CR_A 位并将值存到r4中. CR_A 是在 include/asm-arm/system.h 21行定义, 是cp15控制寄存器c1的Bit1(alignment fault enable/disable)

第54行: 这一行是存储控制寄存器的值.

从上面 arch/arm/kernel/entry-armv.S 的代码我们可以得知. 这一句是将r0存储到了 cr_alignment 中,将r4存储到了 cr_no_alignment 中.

第55行: 最终跳转到start_kernel

 

你可能感兴趣的:(ARM,linux内核)