单片机
返回首页

ARM Linux内核启动2

2016-06-16 来源:eefocus

上一篇ARM Linux内核启动(1)的衔接。

接着上一篇说,看下面源码:

/*
 * Setup the initial page tables.  We only setup the barest
 * amount which are required to get the kernel running, which
 * generally means mapping in the kernel code.只创建内核代码的映射
 *
 * r5 = physical address of start of RAM
 * r6 = physical IO address
 * r7 = byte offset into page tables for IO
 * r8 = page table flags
 */
__create_page_tables:
pgtbl r4, r5@ page table address页表地址


/*
* Clear the 16K level 1 swapper page table
*/
mov r0, r4
mov r3, #0
add r2, r0, #0x4000
1: str r3, [r0], #4
str r3, [r0], #4
str r3, [r0], #4
str r3, [r0], #4
teq r0, r2
bne 1b

 

/*
* Create identity mapping for first MB of kernel to
* cater for the MMU enable.  This identity mapping
* will be removed by paging_init().  We use our current program
* counter to determine corresponding section base address.
*/现在只创建开始1M的映射,其他外设寄存器空间的映射由paging_init()创建
mov r2, pc, lsr #20@ start of kernel section
add r3, r8, r2, lsl #20@ flags + kernel base
str r3, [r4, r2, lsl #2]@ identity mapping


/*
* Now setup the pagetables for our kernel direct
* mapped region.  We round TEXTADDR down to the
* nearest megabyte boundary.  It is assumed that
* the kernel fits within 4 contigous 1MB sections.
*/现在为内核直接映射区建立页表。我们大概将TEXTADDR降到最近的M区域
add r0, r4,  #(TEXTADDR & 0xff000000) >> 18@ start of kernel
str r3, [r0, #(TEXTADDR & 0x00f00000) >> 18]!
add r3, r3, #1 << 20
str r3, [r0, #4]!@ KERNEL + 1MB
add r3, r3, #1 << 20
str r3, [r0, #4]!@ KERNEL + 2MB
add r3, r3, #1 << 20
str r3, [r0, #4]@ KERNEL + 3MB

/*
* Then map first 1MB of ram in case it contains our boot params.
*/
add r0, r4, #VIRT_OFFSET >> 18
add r2, r5, r8
str r2, [r0]

linux内核中3GB以上的地址空间为内核空间,所以需要把内核所在的物理空间地址映射到3GB以上。这里只映射了4MB。注意第一节进行了两次映射,一个和物理地址相同映射,另一个映射到3GB以上。

......这中间还有一段代码,就不分析了,都是有关调试的。

/*
 * Read processor ID register (CP#15, CR0), and look up in the linker-built
 * supported processor list.  Note that we can't use the absolute addresses
 * for the __proc_info lists since we aren't running with the MMU on
 * (and therefore, we are not in the correct address space).  We have to
 * calculate the offset.
 *
 * Returns:
 * r5, r6, r7 corrupted
 * r8  = page table flags
 * r9  = processor ID
 * r10 = pointer to processor structure
 */
__lookup_processor_type:
adr r5, 2f
ldmia r5, {r7, r9, r10}
sub r5, r5, r10@ convert addresses
add r7, r7, r5@ to our address space
add r10, r9, r5
mrc p15, 0, r9, c0, c0@ get processor id
1: ldmia r10, {r5, r6, r8} @ value, mask, mmuflags
and r6, r6, r9@ mask wanted bits
teq r5, r6
moveq pc, lr
add r10, r10, #PROC_INFO_SZ@ sizeof(proc_info_list)
cmp r10, r7
blt 1b
mov r10, #0@ unknown processor
mov pc, lr


/*
 * Look in include/asm-arm/procinfo.h and arch/arm/kernel/arch.[ch] for
 * more information about the __proc_info and __arch_info structures.
 */

内核中定义的处理器信息和平台信息,在连接文件vmlinux.lds.S (arch\arm\kernel)中有如下定义:

vmlinux.lds.S (arch\arm\kernel)

                __proc_info_begin = .;
*(.proc.info)
__proc_info_end = .;
__arch_info_begin = .;
*(.arch.info)
__arch_info_end = .;


2: .long __proc_info_end
.long __proc_info_begin
.long 2b
.long __arch_info_begin
.long __arch_info_end
这段代码的开头标志,看起来是不是很熟悉,这个就是在第一篇中看到的的,不知道的话,可以回过去查看。这段代码主要是有关处理器的查找。

/*
 * Lookup machine architecture in the linker-build list of architectures.
 * Note that we can't use the absolute addresses for the __arch_info
 * lists since we aren't running with the MMU on (and therefore, we are
 * not in the correct address space).  We have to calculate the offset.
 *不能使用绝对地址
 *  r1 = machine architecture number
 * Returns:
 *  r2, r3, r4 corrupted
 *  r5 = physical start address of RAM
 *  r6 = physical address of IO
 *  r7 = byte offset into page tables for IO
 */
__lookup_architecture_type:
adr r4, 2b
ldmia r4, {r2, r3, r5, r6, r7}@ throw away r2, r3
sub r5, r4, r5@ convert addresses
add r4, r6, r5@ to our address space
add r7, r7, r5
1: ldr r5, [r4] @ get machine type
teq r5, r1@ matches loader number?
beq 2f@ found
add r4, r4, #SIZEOF_MACHINE_DESC@ next machine_desc
cmp r4, r7
blt 1b
mov r7, #0@ unknown architecture
mov pc, lr
2: ldmib r4, {r5, r6, r7} @ found, get results
mov pc, lr

这段代码也和上面的一样。这段完成的工作主要是判断内核对这个平台的支持。那平台信息在那里定义呢?

MACHINE_START (KEV7A400, 'Sharp KEV7a400')
MAINTAINER ('Marc Singer')
BOOT_MEM (0xc0000000, 0x80000000, io_p2v (0x80000000))
BOOT_PARAMS (0xc0000100)
MAPIO (kev7a400_map_io)
INITIRQ (lh7a400_init_irq)
.timer = &lh7a40x_timer,
MACHINE_END

主要是通过MACHINE_START宏,

/*
 * Set of macros to define architecture features.  This is built into
 * a table by the linker.
 */
#define MACHINE_START(_type,_name) \
const struct machine_desc __mach_desc_##_type \
 __attribute__((__section__('.arch.info'))) = { \
.nr = MACH_TYPE_##_type,\
.name = _name,

当想要添加新的平台是,需修改Mach-types (arch\arm\tools)这个文件,因为内核在编译时Makefile脚本会根据

Mach-types (arch\arm\tools)文件生成Mach-types.h (include\asm-arm\)文件。

 
进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • 家用电源无载自动断电装置的设计与制作

  • 用数字电路CD4069制作的万能遥控轻触开关

  • 使用ESP8266从NTP服务器获取时间并在OLED显示器上显示

  • 开关电源的基本组成及工作原理

  • 用NE555制作定时器

  • 带有短路保护系统的5V直流稳压电源电路图

    相关电子头条文章