1 启动代码分析(for MIPS):
  2 packages/hal/mips/arch/v2_0/src/vector.S
  3 FUNC_START(reset_vector) //reset向量,地址为0xbfc00000,内核由此地址启动
  4 
  5 #ifndef CYG_HAL_STARTUP_RAM
  6 #  if defined(CYGPKG_HAL_RESET_VECTOR_FIRST_CODE)
  7     hal_reset_vector_first_code
  8 #  endif
  9 #  if defined(CYGPKG_HAL_EARLY_INIT)
 10     hal_early_init
 11 #  endif
 12     # Decide whether this is an NMI, cold or warm boot.
 13 
 14     mfc0    k0,status        # get status reg //处理NMI位,MIPS R3000中不涉及NMI操作
 15     lui    k1,0x0008        # isolate NMI bit
 16     and    k1,k1,k0
 17     beqz    k1,1f            # skip if zero
 18     nop
 19 
 20     lar    k1,__nmi_entry        # jump to ROM nmi code
 21     jalr    k1
 22     nop
 23 1:
 24     lui    k1,0x0010        # isolate soft reset bit
 25     and    k1,k1,k0
 26     beqz    k1,2f            # skip if zero
 27     nop
 28 
 29     lar    k1,__warm_start        # jump to ROM warm_start code //__warm_start代码用于调试,可选
 30     jr    k1
 31     nop
 32 2:
 33     la    k0,INITIAL_CONFIG0    # Set up config0 register //初始化config0寄存器,MIPS R3000没有这个寄存器
 34     mtc0     k0,config0        # to disable cache //禁用cache
 35 #endif
 36     lar    v0,_start        # jump to start //加载_start标号地址(此处的lar指令MIPS R3000不支持)
 37 #ifdef CYGARC_START_FUNC_UNCACHED
 38     CYGARC_ADDRESS_REG_UNCACHED(v0)
 39 #endif
 40 
 41     jr    v0 //跳转到_start处
 42     nop                # (delay slot)
 43 
 44 FUNC_END(reset_vector)
 45 
 46 FUNC_START(_start) //_start函数,做大部分初始化工作,具体代码在_start之后分析
 47 
 48     # Initialize hardware
 49     hal_cpu_init //packages/hal/mips/arch/v2_0/include/arch.inc 初始化watchdog寄存器,cause寄存器清零初始化status寄存器初始化config0寄存器(MIPS R3000没有config0寄存器)
 50     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
 51     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
 52     hal_fpu_init //packages/hal/mips/arch/v2_0/include/arch.inc,初始化fpu
 53     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中实现
 54     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中实现
 55     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
 56     hal_timer_init //初始化timer,如果未定义CYGPKG_HAL_MIPS_TIMER_DEFINED,则表示该platform没有timer,则在packages/hal/mips/arch/v2_0/include/arch.inc中定义一个空宏,如果定义了则实现依赖于特定platform
 57 
 58 #ifdef CYGARC_START_FUNC_UNCACHED
 59     # switch to cached execution address if necessary
 60     # assumption is that hal_cache_init makes this safe
 61     lar    v0,1f
 62     jr    v0
 63     nop
 64    1:
 65 #endif
 66 
 67     # Load Global Pointer register.
 68     la    gp,_gp //加载gp寄存器
 69 
 70     # load initial stack pointer //设置stack指针,给c代码使用,通常interrupt stack用于此目的
 71     la    a0,__interrupt_stack //定义于vector.S,定义如下:
 72   .balign 16
 73     .global cyg_interrupt_stack
 74 cyg_interrupt_stack:
 75 __interrupt_stack:
 76 .long    0,0,0,0,0,0,0,0
 77     move    sp,a0 //加载stack指针
 78 
 79     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()
 80 #ifdef CYG_HAL_STARTUP_ROM //如果eCos从ROM启动,则拷贝data段到RAM中
 81     # Copy data from ROM to RAM
 82 
 83     .extern    hal_copy_data
 84     jal    hal_copy_data //packages/hal/mips/arch/v2_0/src/hal_misc.c
 85     nop
 86 
 87 #endif
 88 
 89     # Zero BSS //bss段清零
 90 
 91     .extern hal_zero_bss
 92     jal    hal_zero_bss //packages/hal/mips/arch/v2_0/src/vectors.S
 93     nop
 94 
 95     # Call variant and platform HAL
 96     # initialization routines.
 97 
 98     .extern    hal_variant_init
 99     jal    hal_variant_init //variant初始化,packages/hal/mips/tx39/v2_0/src/var_misc.c,大部分为空函数,也可在此处进一步初始化中断控制器,PCI桥,IO设备,启用cache等
100     nop
101 
102     .extern    hal_platform_init
103     jal    hal_platform_init //platform初始化,packages/hal/mips/vrc437x/v2_0/src/plf_misc.c,可以不实现,也可在此处进一步初始化中断控制器,PCI桥,IO设备,启用cache等
104     nop
105 
106     # Call constructors
107     .extern cyg_hal_invoke_constructors
108     jal     cyg_hal_invoke_constructors //建立构造函数表,运行静态构造函数(packages/hal/mips/arch/v2_0/src/hal_misc.c)
109     nop
110 
111 #if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS) //如果实现了GDB stub,则初始化
112         .extern initialize_stub
113         jal     initialize_stub //初始化GDB stub,packages/hal/common/v2_0/src/generic-stub.c
114         nop
115 #endif
116 #if defined(CYGDBG_HAL_MIPS_DEBUG_GDB_CTRLC_SUPPORT) //如果GDB需要ctrl-c支持,则初始化
117         .extern hal_ctrlc_isr_init
118         jal     hal_ctrlc_isr_init //初始化GDB ctrl-c支持,实现依赖于platform,如packages/hal/mips/vrc437x/v2_0/src/plf_misc.c
119         nop
120 #endif
121     # Call cyg_start
122     .extern    cyg_start
123     j    cyg_start //调用cyg_start()进入主循环,kernel开始运行,永不返回,cyg_start()由应用程序自己实现,可在此处调用_bsp_init()初始化设备驱动,一般实现如下:
124                 void
125                 cyg_start(void)
126                 {
127                      cyg_thread_create(10,                   // Priority - just a number
128                           (cyg_thread_entry_t*)console_test,         // entry
129                           0,                    // 
130                           "console_thread",     // Name
131                           &stack[0],            // Stack
132                           STACK_SIZE,           // Size
133                           &thread_handle,       // Handle
134                           &thread_data          // Thread data structure
135                      );
136                      cyg_thread_resume(thread_handle);
137                      cyg_scheduler_start();
138                 }
139     lui     ra,0
140 
141 FUNC_END(_start    )
View Code