linux上电初始化内存,Linux内存初始化

本文深入探讨了Linux内核在启动过程中如何初始化页表,特别是早期页表的构建,包括identity mapping的设置,以及如何通过startup_64函数逐步调整和检查内存地址。内容涉及PAE、PGE模式的开启,GDT和段寄存器的初始化,最终跳转到C代码执行。
摘要由CSDN通过智能技术生成

Linux内存初始化

大小:0.4 MB 人气: 2017-10-12 需要积分:1

TIon of 1 to 1 mapping pmd entries */

define PMDS(START, PERM, COUNT) \

i = 0 ; \

.rept (COUNT) ; \

.quad (START) + (i 《《 PMD_SHIFT) + (PERM) ; \

i = i + 1 ; \

.endr

我们就可以很轻易地画出下面这张图:

early page table

后面的初始化过程,就是建立在这个早期的页表结构中的。

正式进入startup_64

我们一段段来分析:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

startup_64:

/*

* Compute the delta between the address I am compiled to run at and the

* address I am actually running at.

*/

leaq _text(%rip), %rbp

subq $_text - __START_KERNEL_map, %rbp

/* Is the address not 2M aligned? */

movq %rbp, %rax

andl $~PMD_PAGE_MASK, %eax

testl %eax, %eax

jnz bad_address

/*

* Is the address too large?

*/

leaq _text(%rip), %rax

shrq $MAX_PHYSMEM_BITS, %rax

jnz bad_address

这里的这段代码非常奇怪:

1

2

leaq _text(%rip), %rbp

subq $_text - __START_KERNEL_map, %rbp

我想了好久,现在终于在Liangpig的指导下有了点眉目。(不确定的)解释如下:

首先leaq _text(%rip), %rbp是一个相对寻址的指令,其并不是直接将_text的地址和当前%rip的值相加,而是%rip加上一个_text和它的相对地址,其实就是$-7(因为该地址的长度为7,而当前的%rip就是_text地址加上7),这个相对值是在link的时候计算出来的,可以参看这个问题和这个问题。

这里另外需要注意的一点是,在当前这个时候,计算机还是通过实模式进行寻址的,所以内核的代码应该是被load到了一个低地址(而不是大于0xffffffff8000000的地址),因此,%rbp存储的也是一个低地址,表示的是内核的代码段被实际装载到内存到的地址,让我们假设是0x3000000。

那么$_text - __START_KERNEL_map是什么呢?我们来看下面的定义:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

define __START_KERNEL_map _AC(0xffffffff80000000, UL)

define __PHYSICAL_START ALIGN(CONFIG_PHYSICAL_START, \

CONFIG_PHYSICAL_ALIGN)

define __START_KERNEL (__START_KERNEL_map + __PHYSICAL_START)

SECTIONS

{

。 = __START_KERNEL;

.text : AT(ADDR(.text) - LOAD_OFFSET) {

_text = 。;

}

}

define

首先,__START_KERNEL_map是0xffffffff80000000,即内核代码和数据段在64位的虚拟地址空间中的最低地址段(0xffffffff80000000到0xffffffffa0000000这512MB的虚拟机之空间映射了内核段)。而_text表示的是__START_KERNEL_map加上了一段编译过程中指定的地址,在我机器内核的.config文件中为0x1000000。也就是说,如果__START_KERNEL_map映射的是物理地址为0的内存的话,那么在编译中我们期望的真正的物理地址就为0x1000000,也就是说,_text - __START_KERNEL_map表示的是我们在编译过程中期望的内核段被装载到内存的起始地址,因此subq_text - __START_KERNEL_map, %rbp表示将当前内核段真实被装载到内存中的地址和编译过程中期望被装载到内存中的地址的差值赋值给%rbx,在我们的例子中即为0x2000000(0x3000000 - 0x1000000)。

之后我们就对这个真实被装载到内存中的地址做一些检查,包括是否2M对齐,以及有没有超过最大大小等等,这里就不详述了。

然后做的一件事就是调整初始化页表中的物理地址映射:

1

2

3

4

5

6

7

8

9

/*

* Fixup the physical addresses in the page table

*/

addq %rbp, early_level4_pgt + (L4_START_KERNEL*8)(%rip)

addq %rbp, level3_kernel_pgt + (510*8)(%rip)

addq %rbp, level3_kernel_pgt + (511*8)(%rip)

addq %rbp, level2_fixmap_pgt + (506*8)(%rip)

这又是一段相对寻址,由于页表处于数据段,所以需要根据其和%rip中的相对地址来定位到页表,然后将页表中的表项加上之前计算的相对偏移量。当然这里只处理了early_level4_pgt、level3_kernel_pgt和level2_fixmap_pgt,而真正映射内核段的level2_kernel_pgt会在之后进行fixup。

之后又进入了一段诡异的代码,来建立idenTIty mapping for the switchover,我也不懂这里的switchover是什么,我们先来看下这段代码做了什么吧:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

/*

* Set up the idenTIty mapping for the switchover. These

* entries should NOThave the global bit set! This also

* creates a bunch of nonsense entries but that is fine –

* it avoids problems around wraparound.

*/

leaq _text(%rip), %rdi

leaq early_level4_pgt(%rip), %rbx

movq %rdi, %rax

shrq $PGDIR_SHIFT, %rax

leaq (4096 + _KERNPG_TABLE)(%rbx), %rdx

movq %rdx, 0(%rbx,%rax,8)

movq %rdx, 8(%rbx,%rax,8)

addq 4096,movqshrqPUD_SHIFT, %rax

andl (PTRSPERPUD−1),movqinclandl(PTRS_PER_PUD-1), %eax

movq %rdx, 4096(%rbx,%rax,8)

addq 8192,movqshrqPMD_SHIFT, %rdi

addq (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL), %rax

leaq (_end - 1)(%rip), %rcx

shrqPMD_SHIFT, %rcx

subq %rdi, %rcx

incl %ecx

1:

andq (PTRSPERPMD−1),movqincqaddqPMD_SIZE, %rax

decl %ecx

jnz 1b

我们可以稍微进行一个计算,首先%rdi保存了当前内核代码段的首地址,%rbx保存了early_level4_pgt的地址,%rax是内核代码首地址对于level4页表的index,在当前即为0。所以leaq (4096 + _KERNPG_TABLE)(%rbx), %rdx表示的是将early_level4_pgt所在的地址加上一个页的地址,作为第3级页表页,再加上相应的权限位,保存在%rdx中,然后通过movq %rdx, 0(%rbx,%rax,8)和movq %rdx, 8(%rbx,%rax,8)指令把%rdx作为一个表项,存在early_level4_pgt的第0和第1项中。

然后将%rdx再加上一个页的大小,作为第2级页表页,找到内核代码段对于level3页表的index,然后将第2级页表页加上对应的权限作为一个页表项存在刚刚建立的level3页表的第0项和第1项。

然后将%rbx加上两个页的大小,即第2级页表的位置,找到从_text到_end所有内核代码段对于level2页表的索引,然后将对应的地址+权限作为页表项逐个填到这个第2级页表中。

我们可以在arch/x86/kernel/head_64.S文件中找到这几个新添加的页表页的定义:

1

2

3

4

5

6

7

__INITDATA

NEXT_PAGE(early_level4_pgt)

.fill 511,8,0

.quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE

NEXT_PAGE(early_dynamic_pgts)

.fill 512*EARLY_DYNAMIC_PAGE_TABLES,8,0

即紧接着early_level4_pgt,被称为early_dynamic_pgts。这个就是所谓的identity mapping for the switchover,表示在之后的一小段页表转换过程中会被用到的identity mapping。因为在页表中虚拟地址从低地址到高地址转换的过程中不可避免的会通过低位的虚拟地址进行索引,所以需要预先做个identity mapping的准备。

至此,页表变成了这个样子。

early page table 2

startup_64最后一步就是fixup内核段真正的物理页对应的页表项了,代码如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/*

* Fixup the kernel text+data virtual addresses. Note that

* we might write invalid pmds, when the kernel is relocated

* cleanup_highmap() fixes this up along with the mappings

* beyond _end.

*/

leaq level2_kernel_pgt(%rip), %rdi

leaq 4096(%rdi), %r8

/* See if it is a valid page table entry */

1: testq 1,0(jz2faddq/∗Gotothenextpage∗/2:addq8, %rdi

cmp %r8, %rdi

jne 1b

/* Fixup phys_base */

addq %rbp, phys_base(%rip)

movq $(early_level4_pgt - __START_KERNEL_map), %rax

jmp 1f

这个过程的前半部分就是将level2_kernel_pgt中的表项进行一个个的检查,如果不是0(即为一个可能存在的页表项),则将其加上之前计算的真实地址和被期待地址的偏移量(%rbp)。

当这个fixup结束之后,将%rbp保存在phys_base这个地址中,然后再将early_level4_pgt - __START_KERNEL_map保存在%rax中。

接下来就进入secondary_startup_64。

secondary_startup_64

这部分代码的主要功能是一些模式的开启,以及相关数据结构的加载,我们同样逐段进行分析:

1

2

3

4

5

6

7

8

ENTRY(secondary_startup_64)

/* Enable PAE mode and PGE */

movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx

movq %rcx, %cr4

/* Setup early boot stage 4 level pagetables. */

addq phys_base(%rip), %rax

movq %rax, %cr3

这里开启了PAE和PGE模式,并将其写到%cr4中,同时将初始页表的第四级页表地址写入了%cr3。至此,分页模式开启!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

/* Ensure I am executing from virtual addresses */

movq $1f, %rax

jmp *%rax

1:

/* Check if nx is implemented */

movl $0x80000001, %eax

cpuid

movl %edx,%edi

/* Setup EFER (Extended Feature Enable Register) */

movl MSREFER,rdmsrbtsl_EFER_SCE, %eax /* Enable System Call */

btl 20,jnc1fbtsl_EFER_NX, %eax

btsq $_PAGE_BIT_NX,early_pmd_flags(%rip)

1: wrmsr /* Make changes effective */

/* Setup cr0 */

define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \

X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \ X86_CR0_PG)

movl $CR0_STATE, %eax

/* Make changes effective */

movq %rax, %cr0

/* Setup a boot time stack */

movq stack_start(%rip), %rsp

/* zero EFLAGS after setting rsp */

pushq $0

popfq

上面的代码进行了一系列的初始化,包括检查nx(non-execution)是否开启,创建EFER,创建cr0,以及设置一个启动时会用到的栈,并且将所有eflags清零。这里就不细讲了。

然后是加载早期的GDT:

1

2

3

4

5

6

7

/*

* We must switch to a new deor in kernel space for the GDT

* because soon the kernel won’t have access anymore to the userspace

* addresses where we’re currently running on. We have to do that here

* because in 32bit we couldn’t load a 64bit linear address.

*/

lgdt early_gdt_descr(%rip)

初始化段寄存器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/* set up data segments */

xorl %eax,%eax

movl %eax,%ds

movl %eax,%ss

movl %eax,%es

movl %eax,%fs

movl %eax,%gs

/* Set up %gs.

*

* The base of %gs always points to the bottom of the irqstack

* union. If the stack protector canary is enabled, it is

* located at %gs:40. Note that, on SMP, the boot cpu uses

* init data section till per cpu areas are set up.

*/

movl $MSR_GS_BASE,%ecx

movl initial_gs(%rip),%eax

movl initial_gs+4(%rip),%edx

wrmsr

这里需要注意的是%gs的建立,它和per cpu变量相关,是一个比较关键的段寄存器。不过由于这个系列主要是和内存相关,所以这里就不详述了。

最后就是一个通过far jump的跳转:

1

2

3

4

5

6

7

8

9

10

11

/* Finally jump to run C code and to be on real kernel address

* Since we are running on identity-mapped space we have to jump

* to the full 64bit address, this is only possible as indirect

* jump. In addition we need to ensure %cs is set so we make this

* a far return.

*/

movq initial_code(%rip),%rax

pushq 0 # fake return address to stop unwinder

pushq__KERNEL_CS # set correct cs

pushq %rax # target address in negative space

lretq

其中initial_code定义为:

1

2

GLOBAL(initial_code)

.quad x86_64_start_kernel

因此,最后进入了x86_64_start_kernel函数,这是一个C语言写的函数,所以,会在下一篇博客中进行介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值