入口:
ENTRY(stext)
msrcpsr_c, #PSR_F_BIT | PSR_I_BIT | SVC_MODE
@ 管理模式
@ 禁止中断
mrc
p15, 0, r9, c0, c0
@ 获得cpu的ID
bl
__lookup_processor_type
@ r5=procinfo r9=cpuid
movsr10, r5 @ invalid processor (r5=0)?
beq__error_p @ yes, error 'p'
bl
__lookup_machine_type
@ r5=machinfo,获取机器ID,见注释1
movsr8, r5 @ invalid machine (r5=0)?
beq__error_a @ yes, error 'a'
bl__create_page_tables
@创建页表
adrlr, __enable_mmu
@ 启动MMU
start_kernel
printk(linux_banner);
//打印内核版本信息
setup_arch(&command_line);
//这两个函数用于处理u-boot传递进来的启动参数,见注释2
setup_command_line(command_line);
console_init();
//初始化控制台
rest_init();
kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
kernel_init
prepare_namespace();
mount_root();
//挂接根文件系统
init_post
sys_open((const char __user *) "/dev/console", O_RDWR, 0)//打开控制端
sys_dup(0)
run_init_process("/sbin/init");
run_init_process("/etc/init");
run_init_process("/bin/init");
run_init_process("/bin/sh");
注释1:
3:
.long
.
.long
__arch_info_begin
.long
__arch_info_end
...................................................................................................
.type
__lookup_machine_type, %function
__lookup_machine_type:
adr
r3, 3b
@r3=标号3处的物理地址
ldmia
r3, {r4, r5, r6}
@r4=".",即标号3处的虚拟地址
@r5=__arch_info_begin
@r6=__arch_info_end
sub
r3, r3, r4
@ get offset between virt&phys
add
r5, r5, r3
@ convert virt addresses to
add
r6, r6, r3
@ physical address space
1:
ldr
r3, [r5, #MACHINFO_TYPE]
teq
r3, r1
@bootloader传参的时候r1里面存放的机器ID
beq
2f
add
r5, r5, #SIZEOF_MACHINE_DESC
cmp
r5, r6
blo
1b
mov
r5, #0
2:
mov
pc, lr
通过这个循环我们知道,会在某个特殊定义的段里面找到各个ID跟传进来的ID相比较,有匹配项才可以!
这个段在链接脚本里面定义:
__arch_info_begin = .;
*(.arch.info.init)
__arch_info_end = .;
那么这个段里面到底都定义了什么东西呢?我们发现:
#define MACHINE_START(_type,_name)
static const struct machine_desc __mach_desc_##_type
__used
__attribute__((__section__(".arch.info.init"))) = {
.nr
= MACH_TYPE_##_type,
.name
= _name,
#define MACHINE_END
};
又有:
MACHINE_START(S3C2440, "SMDK2440")
/* Maintainer: Ben Dooks <ben@fluff.org> */
.phys_io
= S3C2410_PA_UART,
.io_pg_offst
= (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
.boot_params
= S3C2410_SDRAM_PA + 0x100,
//这就是我们定义的存放传参的地址
.init_irq
= s3c24xx_init_irq,
.map_io
= smdk2440_map_io,
.init_machine
= smdk2440_machine_init,
.timer
= &s3c24xx_timer,
MACHINE_END
所以:
static const struct machine_desc __mach_desc_
S3C2440
__used
__attribute__((__section__(".arch.info.init"))) = {
.nr
= MACH_TYPE_
S3C2440 ,
.name
=
"SMDK2440" ,
/* Maintainer: Ben Dooks <ben@fluff.org> */
.phys_io
= S3C2410_PA_UART,
.io_pg_offst
= (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
.boot_params
= S3C2410_SDRAM_PA + 0x100,
//这就是我们定义的存放传参的地址
.init_irq
= s3c24xx_init_irq,
.map_io
= smdk2440_map_io,
.init_machine
= smdk2440_machine_init,
.timer
= &s3c24xx_timer,
};
这就很清楚了,内核定义了一个
machine_desc
结构体,里面存放了内核所支持的机器ID,以及一些初始化函数。这个结构体被强制存放在
.arch.info.init段里面!
我们可以看看这个段的内容:
struct machine_desc {
unsigned int
nr;
/* 机器ID*/
unsigned int
phys_io;
/* start of physical io
*/
unsigned int
io_pg_offst;
/* byte offset for io
* page tabe entry
*/
const char
*name;
/* architecture name
*/
unsigned long
boot_params;
/* u-boot传参地址 */
unsigned int
video_start;
/* start of video RAM
*/
unsigned int
video_end;
/* end of video RAM
*/
unsigned int
reserve_lp0 :1;
/* never has lp0
*/
unsigned int
reserve_lp1 :1;
/* never has lp1
*/
unsigned int
reserve_lp2 :1;
/* never has lp2
*/
unsigned int
soft_reboot :1;
/* soft reboot
*/
void
(*fixup)(struct machine_desc *,
struct tag *, char **,
struct meminfo *);
void
(*map_io)(void);/* IO mapping function
*/
void
(*init_irq)(void);
struct sys_timer
*timer;
/* system tick timer
*/
void
(*init_machine)(void);
};
总结一下就是:内核支持多少中单板就会定义多少个
machine_desc结构体,这些结构体存放在
.arch.info.init段里面,启动内核的时候,内核会根据u-boot传进来的机器id去遍历匹配
.arch.info.init段里面的ID,
来判断该内核是否支持该单板!
注释2:
setup_arch
mdesc = setup_machine(machine_arch_type);//根据机器id找到其对应的
machine_desc结构体
machine_desc结构体里面存放着传参地址,取出传参,根据传参进行相关设置!
parse_cmdline//
将命令行放到
.early_param.init段里面
我们来看一下这个段的内容:
#define __early_param(name,fn)
static struct early_params __early_##fn __used
__attribute__((__section__(".early_param.init"))) = { name, fn }
对于下面这个命令我们来展开:
__early_param("initrd=", early_initrd);
static struct early_params __early_
early_initrd __used
__attribute__((__section__(".early_param.init"))) = {
"initrd=" ,
early_initrd }
它的意思就是将各个命令对应的结构体放在
.early_param.init段里面,真正执行命的时候,会根据命令的名字找到其对应结构体,然后调用对应的函数!