启动代码分析(for MIPS):
packages/hal/mips/arch/v2_0/src/vector.S
FUNC_START(reset_vector) //reset向量,地址为0xbfc00000,内核由此地址启动
#ifndef CYG_HAL_STARTUP_RAM
# if defined(CYGPKG_HAL_RESET_VECTOR_FIRST_CODE)
hal_reset_vector_first_code
# endif
# if defined(CYGPKG_HAL_EARLY_INIT)
hal_early_init
# endif
# Decide whether this is an NMI, cold or warm boot.
mfc0 k0,status # get status reg //处理NMI位,MIPS R3000中不涉及NMI操作
lui k1,0x0008 # isolate NMI bit
and k1,k1,k0
beqz k1,1f # skip if zero
nop
lar k1,__nmi_entry # jump to ROM nmi code
jalr k1
nop
1:
lui k1,0x0010 # isolate soft reset bit
and k1,k1,k0
beqz k1,2f # skip if zero
nop
lar k1,__warm_start # jump to ROM warm_start code //__warm_start代码用于调试,可选
jr k1
nop
2:
la k0,INITIAL_CONFIG0 # Set up config0 register //初始化config0寄存器,MIPS R3000没有这个寄存器
mtc0 k0,config0 # to disable cache //禁用cache
#endif
lar v0,_start # jump to start //加载_start标号地址(此处的lar指令MIPS R3000不支持)
#ifdef CYGARC_START_FUNC_UNCACHED
CYGARC_ADDRESS_REG_UNCACHED(v0)
#endif
jr v0 //跳转到_start处
nop # (delay slot)
FUNC_END(reset_vector)
FUNC_START(_start) //_start函数,做大部分初始化工作,具体代码在_start之后分析
# Initialize hardware
hal_cpu_init //packages/hal/mips/arch/v2_0/include/arch.inc 初始化watchdog寄存器,cause寄存器清零初始化status寄存器初始化config0寄存器(MIPS R3000没有config0寄存器)
hal_diag_init //诊断支持初始化,如果未定义CYGPKG_HAL_MIPS_DIAG_DEFINED,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义一个空宏,如果定义了则实现依赖于特定平台,如packages/hal/mips/tx39/v2_0/src/hal_duag.c
hal_mmu_init //初始化mmu,如果未定义CYGPKG_HAL_MIPS_MMU_DEFINED,则表示该variant不支持mmu,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义一个空宏,如果定义了则实现依赖于特定平台,如packages/hal/mips/vrc437x/v2_0/include/platform.inc
hal_fpu_init //packages/hal/mips/arch/v2_0/include/arch.inc,初始化fpu
hal_memc_init //初始化memory控制器,如果未定义CYGPKG_HAL_MIPS_MEMC_DEFINED,则表示该platform没有memory控制器,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义一个空宏,如果定义了则实现依赖于特定平台,如packages/hal/mips/tx39/v2_0/include/variant.inc,该宏也可能在platform.inc中实现
hal_intc_init //初始化中断控制器,如果未定义CYGPKG_HAL_MIPS_INTC_INIT_DEFINED,则表示该platform没有中断控制器,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义该宏,在status寄存器中禁用所有中断,如果定义了则实现依赖于特定平台,如packages/hal/mips/tx39/v2_0/include/variant.inc,该宏也可能在platform.inc中实现
hal_cache_init //初始化cache,如果未定义CYGPKG_HAL_MIPS_CACHE_DEFINED,则表示该variant的cache是标准cache,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义该宏,使用config寄存器初始化cache,如果定义了则实现依赖于特定variant,如packages/hal/mips/tx49/v2_0/include/variant.inc
hal_timer_init //初始化timer,如果未定义CYGPKG_HAL_MIPS_TIMER_DEFINED,则表示该platform没有timer,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义一个空宏,如果定义了则实现依赖于特定platform
#ifdef CYGARC_START_FUNC_UNCACHED
# switch to cached execution address if necessary
# assumption is that hal_cache_init makes this safe
lar v0,1f
jr v0
nop
1:
#endif
# Load Global Pointer register.
la gp,_gp //加载gp寄存器
# load initial stack pointer //设置stack指针,给c代码使用,通常interrupt stack用于此目的
la a0,__interrupt_stack //定义于vector.S,定义如下:
.balign 16
.global cyg_interrupt_stack
cyg_interrupt_stack:
__interrupt_stack:
.long 0,0,0,0,0,0,0,0
move sp,a0 //加载stack指针
hal_mon_init //初始化monitor,用于安装中断向量,如果未定义CYGPKG_HAL_MIPS_MON_DEFINED,则表示该platform未实现monitor,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义一个空宏,如果定义了则实现依赖于特定platform,如packages/hal/mips/rm7000/v2_0/include/platform.inc,一般会调用hal_vectors_init()
#ifdef CYG_HAL_STARTUP_ROM //如果eCos从ROM启动,则拷贝data段到RAM中
# Copy data from ROM to RAM
.extern hal_copy_data
jal hal_copy_data //packages/hal/mips/arch/v2_0/src/hal_misc.c
nop
#endif
# Zero BSS //bss段清零
.extern hal_zero_bss
jal hal_zero_bss //packages/hal/mips/arch/v2_0/src/vectors.S
nop
# Call variant and platform HAL
# initialization routines.
.extern hal_variant_init
jal hal_variant_init //variant初始化,packages/hal/mips/tx39/v2_0/src/var_misc.c,大部分为空函数,也可在此处进一步初始化中断控制器,PCI桥,IO设备,启用cache等
nop
.extern hal_platform_init
jal hal_platform_init //platform初始化,packages/hal/mips/vrc437x/v2_0/src/plf_misc.c,可以不实现,也可在此处进一步初始化中断控制器,PCI桥,IO设备,启用cache等
nop
# Call constructors
.extern cyg_hal_invoke_constructors
jal cyg_hal_invoke_constructors //建立构造函数表,运行静态构造函数(packages/hal/mips/arch/v2_0/src/hal_misc.c)
nop
#if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS) //如果实现了GDB stub,则初始化
.extern initialize_stub
jal initialize_stub //初始化GDB stub,packages/hal/common/v2_0/src/generic-stub.c
nop
#endif
#if defined(CYGDBG_HAL_MIPS_DEBUG_GDB_CTRLC_SUPPORT) //如果GDB需要ctrl-c支持,则初始化
.extern hal_ctrlc_isr_init
jal hal_ctrlc_isr_init //初始化GDB ctrl-c支持,实现依赖于platform,如packages/hal/mips/vrc437x/v2_0/src/plf_misc.c
nop
#endif
# Call cyg_start
.extern cyg_start
j cyg_start //调用cyg_start()进入主循环,kernel开始运行,永不返回,cyg_start()由应用程序自己实现,可在此处调用_bsp_init()初始化设备驱动,一般实现如下:
void
cyg_start(void)
{
cyg_thread_create(10, // Priority - just a number
(cyg_thread_entry_t*)console_test, // entry
0, //
"console_thread", // Name
&stack[0], // Stack
STACK_SIZE, // Size
&thread_handle, // Handle
&thread_data // Thread data structure
);
cyg_thread_resume(thread_handle);
cyg_scheduler_start();
}
lui ra,0
FUNC_END(_start )