RISCV Linux kernel 启动代码分析之四:setup_vm之后的页表

文摘   2024-11-16 08:30   湖南  

一. 前言

前文分析了setup_vm函数,主要是几个配置页表的函数的实现。这一篇就进一步来看看setup_vm完之后,配置的页表到底将那些虚拟地址映射到了哪里。我们通过手算和GDB查看对照的方式来加深印象。

二. 分析过程

在如下位置打断点运行到该处,即setup_vm执行完后,relocate执行前。

hb *0x80201000

c

然后打印出各个页表的内容,和手动计算对比。

从源码可以看出构建了两个根页表,

一条是

trampoline_pg_dir->trampoline_pmd

该条是relocateva-pa切换运行时临时使用,只映射了链接虚拟地址开始2MB到加载运行物理地址开始的2MB

一条是

early_pg_dir->fixmap_pmd->fixmap_pte

->early_pmd

->early_dtb_pmd->

reloacte通过trampoline_pg_dir切换到虚拟地址运行后,再切换到页表

2.1临时页表early_pg_dir

2.1.1trampoline_pg_dir

(gdb) p &trampoline_pg_dir$14 = (pgd_t (*)[512]) 0xffffffe001b7d000 <trampoline_pg_dir>(gdb)
(gdb) p /x *(pgd_t (*)[512])0x81d7d000$15 = {{pgd = 0x0} <repeats 384 times>, {pgd = 0x2075ec01}, {pgd = 0x0} <repeats 127 times>}(gdb)

由以下代码配置

  /* Setup trampoline PGD and PMD */    create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,               (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);

即配置pgd页表trampoline_pg_dir

Va=PAGE_OFFSET=0xffffffe0 00000000

Pa=trampoline_pmd=0x81d7b000

Sz=PGDIR_SIZE=1GB

Prot=PAGE_TABLE=1

先计算pgd_idx=(va>>30)&511=(0xffffffe0 00000000>>30)&511=0x180=384

trampoline_pg_dir中一个条目对应1GB,这里需要对应到第384个条目去(右移30位,即按照1GB的颗粒度),并且表只有一页即512个条目,所以按照512取余。

所以对应条目为trampoline_pg_dir[384]

然后计算条目的内容

pfn_pgd(PFN_DOWN(pa), prot);

其中pa0x81d7b000prot1

所以PFN_DOWN(pa)0x81d7b000>>12

所以值为

((0x81d7b000>>12 ) <<10)| 1=0x2075EC01

和前面的打印信息对应

(gdb) p /x *(pgd_t (*)[512])0x81d7d000$15 = {{pgd = 0x0} <repeats 384 times>, {pgd = 0x2075ec01}, {pgd = 0x0} <repeats 127 times>}(gdb)

2.1.2trampoline_pmd

(gdb) p &trampoline_pmd

$16 = (pmd_t (*)[512]) 0xffffffe001b7b000 <trampoline_pmd>

(gdb)

(gdb) p /x *(pmd_t (*)[512])0x81d7b000

$17 = {{pmd = 0x200800ef}, {pmd = 0x0} <repeats 511 times>}

(gdb)

由以下代码配置

    create_pmd_mapping(trampoline_pmd, PAGE_OFFSET,               load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);              

配置PMD页表 trampoline_pmd

Va=PAGE_OFFSET=0xffffffe0 00000000

Pa=load_pa=0x80200000

Sz=PMD_SIZE=2MB

Prot=PAGE_KERNEL_EXEC=0xEF

arch/riscv/include/asm/pgtable-bits.h

include/linux/pgtable.h

定义了PAGE_KERNEL_EXEC相关位

计算

pmd_idx = pmd_index(va); =( va>>21)&511=(0xffffffe0 00000000>>21)&511=0

trampoline_pmd中一个条目对应2MB虚拟地址,这里需要对应到第0个条目去(右移21位,即按照2MB的颗粒度),并且表只有一页即512个条目,所以按照512取余。

再计算

pfn_pmd(PFN_DOWN(pa), prot); =

pfn_pmd(PFN_DOWN(0x80200000), 0xEF); =

pfn_pmd(0x80200000>>12, 0xEF);=

((0x80200000>>12)<<10)|0xEF=0x200800EF

GDB打印信息对应

(gdb) p /x *(pmd_t (*)[512])0x81d7b000

$17 = {{pmd = 0x200800ef}, {pmd = 0x0} <repeats 511 times>}

(gdb)

2.1.3 映射效果

所以以上两级页表,将链接虚拟地址0xffffffe000000000开始的2MB映射到了运行物理地址0x80200000开始的2MB。这样relocate时先将satp设置到trampoline_pg_dir时,访问这片链接地址实际就是访问到对应的物理地址实现无缝切换。这里仅仅作为中转,所以只映射了前面2MB即可,也就是relocate处的代码要在该范围内。

2.2 页表early_pg_dir

2.2.1 一级early_pg_dir

(gdb) p &early_pg_dir

$8 = (pgd_t (*)[512]) 0xffffffe00087b000 <early_pg_dir>

(gdb)

(gdb) p /x *(pgd_t (*)[512])0x80a7b000

$9 = {{pgd = 0x0}, {pgd = 0x2029e401}, {pgd = 0x0} <repeats 313 times>, {pgd = 0x2075e801}, {pgd = 0x0} <repeats 68 times>, {

pgd = 0x2029e801}, {pgd = 0x0} <repeats 127 times>}

(gdb)

可以看到页表 early_pg_dir有三个条目,分贝对应以下语句实现

一一来看

fixmap_pmd

create_pgd_mapping(early_pg_dir, FIXADDR_START,

   (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);

即配置pgd页表early_pg_dir

Va=FIXADDR_START=0xffffffcefee00000(这个地址上一篇文章已经分析过了)

Pa=fixmap_pgd_nextfixmap_pmd = 0x81d7a000

Sz=PGDIR_SIZE=1GB

Prot=PAGE_TABLE=1 下一级还是页表

先计算pgd_idx=(va>>30)&511=(0xffffffcefee00000>>30)&511=0x13b=315

early_pg_dir中一个条目对应1GB,这里需要对应到第315个条目去(右移30位,即按照1GB的颗粒度),并且表只有一页即512个条目,所以按照512取余。

所以对应条目为early_pg_dir[315]

然后计算条目的内容

pfn_pgd(PFN_DOWN(pa), prot);

其中pa0x81d7a000prot1

所以PFN_DOWN(pa)0x81d7a000>>12

所以值为

((0x81d7a000>>12 ) <<10)| 1=0x2075E801

GDB打印的如下信息对应

(gdb) p /x *(pgd_t (*)[512])0x80a7b000$9 = {{pgd = 0x0}, {pgd = 0x2029e401}, {pgd = 0x0} <repeats 313 times>, {pgd = 0x2075e801}, {pgd = 0x0} <repeats 68 times>, {    pgd = 0x2029e801}, {pgd = 0x0} <repeats 127 times>}(gdb)

early_dtb_pmd

create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,

   (uintptr_t)early_dtb_pmd, PGDIR_SIZE, PAGE_TABLE);

即配置pgd页表early_pg_dir

Va=DTB_EARLY_BASE_VA=0x40000000(#define DTB_EARLY_BASE_VA      PGDIR_SIZE)

Pa=early_dtb_pmd= 0x80a79000

Sz=PGDIR_SIZE=1GB

Prot=PAGE_TABLE=1 下一级还是页表

先计算pgd_idx=(va>>30)&511=(0x40000000>>30)&511=1

early_pg_dir中一个条目对应1GB,这里需要对应到第1个条目去(右移30位,即按照1GB的颗粒度),并且表只有一页即512个条目,所以按照512取余。

所以对应条目为early_pg_dir[1]

然后计算条目的内容

pfn_pgd(PFN_DOWN(pa), prot);

其中pa0x80a79000prot1

所以PFN_DOWN(pa)0x80a79000>>12

所以值为

((0x80a79000>>12 ) <<10)| 1=0x2029E401

GDB打印的如下信息对应

(gdb) p /x *(pgd_t (*)[512])0x80a7b000$9 = {{pgd = 0x0}, {pgd = 0x2029e401}, {pgd = 0x0} <repeats 313 times>, {pgd = 0x2075e801}, {pgd = 0x0} <repeats 68 times>, {    pgd = 0x2029e801}, {pgd = 0x0} <repeats 127 times>}(gdb)

early_pmd

  /*   * Setup early PGD covering entire kernel which will allows   * us to reach paging_init(). We map all memory banks later   * in setup_vm_final() below.   */  end_va = PAGE_OFFSET + load_sz;  for (va = PAGE_OFFSET; va < end_va; va += map_size)    create_pgd_mapping(early_pg_dir, va,           load_pa + (va - PAGE_OFFSET),           map_size, PAGE_KERNEL_EXEC);

即配置pgd页表early_pg_dir

Va=PAGE_OFFSET开始按照map_size(前一篇文章分析的大小是2MB)为单位递进

Pa=load_pa 开始按照2MB递进

Sz=2MB

Prot=PAGE_KERNEL_EXEC 下一级叶子pte

这里希望直接映射

PAGE_OFFSET开始的虚拟地址到load_pa处,以2MB为单位映射整个镜像。

我们看create_pgd_mapping的实现

这里sz不为PGDIR_SIZE,且之前只映射了

DTB_EARLY_BASE_VA 0x40000000

FIXADDR_START 0xffffffcefee00000

所以此时映射0xffffffe0 00000000

按照1GB的颗粒度early_pg_dir中肯定是没有这个条目的,

所以会走下面红色框中代码,先在early_pg_dir中创建一个条目,而其

下一级是alloc_pgd_next

pt_ops.alloc_pmd(__va)

pt_ops.alloc_pmd = alloc_pmd_early;

&early_pmd[pmd_num * PTRS_PER_PMD];

MAX_EARLY_MAPPING_SIZE 小于PGDIR_SIZE的话

early_pmd只有一个页大小

这里MAX_EARLY_MAPPING_SIZE128M小于PGDIR_SIZE

所以

        next_phys = alloc_pgd_next(va);        pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);        nextp = get_pgd_next_virt(next_phys);        memset(nextp, 0, PAGE_SIZE);

执行后

next_phys=&pmd_t early_pmd = 0x80A7A000

Pgd_idx=(va>>30)&511=(0xffffffe000000000>>30)&511=0x180=384

pfn_pgd(PFN_DOWN(next_phys), prot);

其中pa0x80a7a000protPAGE_TABLE=1

所以PFN_DOWN(pa)0x80a7a000>>12

所以值为

((0x80a7a000>>12 ) <<10)| 1=0x2029E801

所以

early_pg_dir[384]=0x2029E801

和如下打印对应

(gdb) p /x *(pgd_t (*)[512])0x80a7b000$9 = {{pgd = 0x0}, {pgd = 0x2029e401}, {pgd = 0x0} <repeats 313 times>, {pgd = 0x2075e801}, {pgd = 0x0} <repeats 68 times>, {    pgd = 0x2029e801}, {pgd = 0x0} <repeats 127 times>}(gdb)

一级PGD效果

至此上面early_pg_dir下的3个条目,映射了3GB块。

此时还只映射到下一级PMD

对应如下黄色部分的3块虚拟地址起点

后续还要继续构建下一级PMD到物理地址

2.2.2 二级pmd

在上述三个pmd页表下继续映射到最终的物理块。

分别对应以下语句

fixmap_pmd

(gdb) p &fixmap_pmd$10 = (pmd_t (*)[512]) 0xffffffe001b7a000 <fixmap_pmd>(gdb)
(gdb) p /x *(pmd_t (*)[512])0x81d7a000$11 = {{pmd = 0x0} <repeats 503 times>, {pmd = 0x2075f001}, {pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}, { pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}}(gdb)

对应代码如下

    /* Setup fixmap PMD */    create_pmd_mapping(fixmap_pmd, FIXADDR_START,               (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);

Va= FIXADDR_START=0xffffffcefee00000

Pa=fixmap_pte=0x81d7c000

Sz=2MB

Prot=PAGE_TABLE = 1表示下一级还是页表。

pmd_idx=(0xffffffcefee00000>>21)&511=0x1F7=503

pfn_pmd(PFN_DOWN(pa), prot);=((0x81d7c000>>12)<<10) | 1=2075F001

所以

fixmap_pmd[503]=0x2075F001

和打印以下对应


(gdb) p /x *(pmd_t (*)[512])0x81d7a000$11 = {{pmd = 0x0} <repeats 503 times>, {pmd = 0x2075f001}, {pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}, { pmd = 0x0}, {pmd = 0x0}, {pmd = 0x0}}(gdb)

fixmap_pte

此时fixmap_pte还没有映射叶子pte物理地址。

(gdb) p &fixmap_pte$12 = (pte_t (*)[512]) 0xffffffe001b7c000 <fixmap_pte>(gdb)
(gdb) p /x *(pte_t (*)[512])0x81d7c000$13 = {{pte = 0x0} <repeats 512 times>}(gdb)

页表对应如下

这一部分还未映射物理地址

early_dtb_pmd

(gdb) p &early_dtb_pmd$20 = (pmd_t (*)[512]) 0xffffffe000879000 <early_dtb_pmd>(gdb)
(gdb) p /x *(pmd_t (*)[512])0x80a79000$23 = {{pmd = 0x20f800e7}, {pmd = 0x210000e7}, {pmd = 0x0} <repeats 510 times>}(gdb)

该部分映射22MB的设备树块。

对应代码如下

其中dtb_pa由参数传入,kernel启动时由寄存器r1传入

我这里是

gdbinit.txt中设置

(gdb) p /x $dtb_addr

$25 = 0x83f00000

(gdb)

这里先按2MB对齐

pa = dtb_pa & ~(PMD_SIZE - 1);

所以pa=0x83e00000

后面映射了两个连续的2MB的块,所以分析一个即可。

    create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA,               pa, PMD_SIZE, PAGE_KERNEL);

Va= DTB_EARLY_BASE_VA=0x40000000

Pa=0x83e00000

Sz=2MB

Prot=PAGE_KERNEL = 0xE7表示没有下一级,直接索引物理地址了。

先计算

pmd_idx = pmd_index(va);=(va>>21)&511,  即按照2MB块分配,索引对512取余。

pmd_idx=(0x40000000>>21)&511=0

pfn_pmd(PFN_DOWN(pa), prot);=((0x83e00000>>12)<<10) | 0xE7=0x20F800E7

所以

early_dtb_pmd[0]=0x20F800E7

和打印以下对应

(gdb) p /x *(pmd_t (*)[512])0x80a79000$23 = {{pmd = 0x20f800e7}, {pmd = 0x210000e7}, {pmd = 0x0} <repeats 510 times>}(gdb)

再来看

  create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE,               pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);

Va= DTB_EARLY_BASE_VA=0x40200000

Pa=0x84000000

Sz=2MB

Prot=PAGE_KERNEL = 0xE7表示没有下一级,直接索引物理地址了。

pmd_idx=(0x40200000>>21)&511=1

pfn_pmd(PFN_DOWN(pa), prot);=((0x84000000>>12)<<10) | 0xE7=0x210000E7

所以

early_dtb_pmd[1]=0x210000E7

和打印以下对应

(gdb) p /x *(pmd_t (*)[512])0x80a79000$23 = {{pmd = 0x20f800e7}, {pmd = 0x210000e7}, {pmd = 0x0} <repeats 510 times>}(gdb)

对应的映射如下

即将0x40000000开始的2个连续的2MB映射到了0x83e00000开始的两个连续的2MB

采用大页2MB为单位映射。

early_pmd

(gdb) p &early_pmd$2 = (pmd_t (*)[512]) 0xffffffe00087a000 <early_pmd>
(gdb) p /x * (pmd_t (*)[512])0x80A7A000$5 = {{pmd = 0x200800ef}, {pmd = 0x201000ef}, {pmd = 0x201800ef}, {pmd = 0x202000ef}, {pmd = 0x202800ef}, {pmd = 0x203000ef}, { pmd = 0x203800ef}, {pmd = 0x204000ef}, {pmd = 0x204800ef}, {pmd = 0x205000ef}, {pmd = 0x205800ef}, {pmd = 0x206000ef}, { pmd = 0x206800ef}, {pmd = 0x207000ef}, {pmd = 0x0} <repeats 498 times>}(gdb)

这里对应代码如下

    /*     * Setup early PGD covering entire kernel which will allows     * us to reach paging_init(). We map all memory banks later     * in setup_vm_final() below.     */    end_va = PAGE_OFFSET + load_sz;    for (va = PAGE_OFFSET; va < end_va; va += map_size)        create_pgd_mapping(early_pg_dir, va,                   load_pa + (va - PAGE_OFFSET),                   map_size, PAGE_KERNEL_EXEC);

这里是对kernel镜像大小,从

PAGE_OFFSET0xffffffe000000000开始,按照2MB进行映射到load_pa0x80200000处。

我们前面一篇分析了load_sz大小为29151232=27.8M,所以按照2MB为单位,要创建14个条目,从上面gdb的打印p /x * (pmd_t (*)[512])0x80A7A000也可看到下有14个条目。

我们以第一个为例分析,后面的类似。

这里create_pgd_mapping由于传入sz2MB不是1GB

且之前已经创建了early_pg_dir的条目。所以这里会走如下红色框部分,即调用create_pmd_mapping

Va= PAGE_OFFSET=0xffffffe000000000

Pa=0x80200000

Sz=2MB

Prot=PAGE_KERNEL_EXEC = 0xEF表示没有下一级,直接索引物理地址了。

pmd_idx=(0xffffffe000000000>>21)&511=0

pfn_pmd(PFN_DOWN(pa), prot);=((0x80200000>>12)<<10) | 0xEF=200800EF

所以

early_pmd[0]=200800EF

和打印以下对应

(gdb) p /x * (pmd_t (*)[512])0x80A7A000$5 = {{pmd = 0x200800ef}, {pmd = 0x201000ef}, {pmd = 0x201800ef}, {pmd = 0x202000ef}, {pmd = 0x202800ef}, {pmd = 0x203000ef}, {    pmd = 0x203800ef}, {pmd = 0x204000ef}, {pmd = 0x204800ef}, {pmd = 0x205000ef}, {pmd = 0x205800ef}, {pmd = 0x206000ef}, {    pmd = 0x206800ef}, {pmd = 0x207000ef}, {pmd = 0x0} <repeats 498 times>}(gdb)

如果对应最后一个则是

Va= PAGE_OFFSET=0xffffffe000000000+13*2MB=0xffffffe001A00000

Pa=0x80200000+13x2MB=0x81C00000

Sz=2MB

Prot=PAGE_KERNEL_EXEC = 0xEF表示没有下一级,直接索引物理地址了。

pmd_idx=(0xffffffe001A00000>>21)&511=13

pfn_pmd(PFN_DOWN(pa), prot);=((0x81C00000>>12)<<10) | 0xEF=0x207000EF

所以

early_pmd[13]=0x207000EF

和打印以下对应

(gdb) p /x * (pmd_t (*)[512])0x80A7A000$5 = {{pmd = 0x200800ef}, {pmd = 0x201000ef}, {pmd = 0x201800ef}, {pmd = 0x202000ef}, {pmd = 0x202800ef}, {pmd = 0x203000ef}, {    pmd = 0x203800ef}, {pmd = 0x204000ef}, {pmd = 0x204800ef}, {pmd = 0x205000ef}, {pmd = 0x205800ef}, {pmd = 0x206000ef}, {    pmd = 0x206800ef}, {pmd = 0x207000ef}, {pmd = 0x0} <repeats 498 times>}(gdb)

映射完后对应如下

2.3 swapper_pg_dir

(gdb) p &swapper_pg_dir$18 = (pgd_t (*)[512]) 0xffffffe001b7e000 <swapper_pg_dir>(gdb)
(gdb) p /x * (pgd_t (*)[512]) 0x81D7E000$19 = {{pgd = 0x0} <repeats 512 times>}(gdb)

swapper_pg_dirsetup_vm_final中设置,此时还未设置。

swapper_pg_dir下一篇再分析。

2.4 .fixmap_pmd的检查

setup_vm函数分析时,还有如下部分未分析,我们来继续分析下

/*     * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap     * range can not span multiple pmds.     */    BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)             != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
#ifndef __PAGETABLE_PMD_FOLDED    /*     * Early ioremap fixmap is already created as it lies within first 2MB     * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END     * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn     * the user if not.     */    fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];    fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];    if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {        WARN_ON(1);        pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",            pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));        pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",            fix_to_virt(FIX_BTMAP_BEGIN));        pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",            fix_to_virt(FIX_BTMAP_END));
        pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);        pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);    }#endif

首先

arch/riscv/include/asm/fixmap.h

计算

__fix_to_virt(FIX_BTMAP_BEGIN)__fix_to_virt(FIX_BTMAP_END)要在一个2MB内。

其中

FIX_BTMAP_BEGIN(FIX_BTMAP_END计算如下

/* * Here we define all the compile-time 'special' virtual addresses. * The point is to have a constant address at compile time, but to * set the physical address only in the boot process. * * These 'compile-time allocated' memory buffers are page-sized. Use * set_fixmap(idx,phys) to associate physical memory with fixmap indices. */enum fixed_addresses {    FIX_HOLE,    FIX_PTE,    FIX_PMD,    FIX_TEXT_POKE1,    FIX_TEXT_POKE0,    FIX_EARLYCON_MEM_BASE,
    __end_of_permanent_fixed_addresses,    /*     * Temporary boot-time mappings, used by early_ioremap(),     * before ioremap() is functional.     */#define NR_FIX_BTMAPS       (SZ_256K / PAGE_SIZE)#define FIX_BTMAPS_SLOTS    7#define TOTAL_FIX_BTMAPS    (NR_FIX_BTMAPS * FIX_BTMAPS_SLOTS)
    FIX_BTMAP_END = __end_of_permanent_fixed_addresses,    FIX_BTMAP_BEGIN = FIX_BTMAP_END + TOTAL_FIX_BTMAPS - 1,
    __end_of_fixed_addresses};

得到FIX_BTMAP_END=6

FIX_BTMAP_END_BEGIN=6+7*64-1=453

__fix_to_virt

include/asm-generic/fixmap.h

#define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT))

#define __virt_to_fix(x) ((FIXADDR_TOP - ((x)&PAGE_MASK)) >> PAGE_SHIFT)

即先<<PAGE_SHIFT即成衣4K

然后计算FIXADDR_TOP减去该值,得到BEGINEND处的虚拟地址。

FIXADDR_TOParch/riscv/include/asm/pgtable.h中定义

对应如下空间,即如下7*644k空间要在同一个2MB

然后pmd_index虚拟地址转pmd_idx

    fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];    fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];

找到该idx对应的fixmap_pmd条目

对应我们前面分析的这个索引503

然后判断对应的pmd条目内容要一样,我们这里是在同一个2MB内是一样的。

  if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {        WARN_ON(1);        pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",            pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));        pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",            fix_to_virt(FIX_BTMAP_BEGIN));        pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",            fix_to_virt(FIX_BTMAP_END));
        pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);        pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);    }

三. 总结

setup_vm后,总的页表映射关系如下

两个根页表在relocate中使用,其中先临时使用trampoline_pg_dir跳转下,再使用

early_pg_dir 这两个页表都映射了PAGE_OFFSET:0xffffffe000000000load_pa:0x80200000,都包括了relocate处的代码,所以才能无缝切换。

前者只映射了2MB,后者映射了整个Image的大小,都是按照大页2MB为单位映射。

此时的虚拟地址和物理地址对应如下

配置了

CONFIG_MMUCONFIG_DEBUG_VM

函数

print_vm_layout可打印上述信息
























嵌入式Lee
嵌入式软硬件技术:RTOS,GUI,FS,协议栈,ARM,总线,嵌入式C,开发环境 and blablaba....多年经验分享,非硬货不发,带你扒开每一个技术背后的根本原理。
 最新文章