Linux的初始页表
LOWMEM,是相对于HIGHMEM而言,其实就是内核态直接映射的物理内存空间的大小,一般为896MB。
这里只是粗略地估计了一下,取1GB。
LOWMEM_PAGES代表了LOWMEM区域对应了多少内存页,1GB / 4KB = 256K,即需要256K个页表项来描述内核空间
1: /* Number of possible pages in the lowmem region */
2: LOWMEM_PAGES = (((1<<32) - __PAGE_OFFSET) >> PAGE_SHIFT)
计算一下,需要多少内存页来存放LOWMEM区域对应的内存页表项,有256个页表,每个页表占1页内存,需要256页,即256 * 4KB = 1MB来存放256个页表。
1: /* Enough space to fit pagetables for the low memory linear map */
2: MAPPING_BEYOND_END = PAGE_TABLE_SIZE(LOWMEM_PAGES) << PAGE_SHIFT
其中,PAGE_TABLE_SIZE用来计算需要多少个页表来容纳这么多的页, 256K / 1K = 256,需要256个页表
1: #if PTRS_PER_PMD > 1
2: #define PAGE_TABLE_SIZE(pages) (((pages) / PTRS_PER_PMD) + PTRS_PER_PGD)
3: #else
4: #define PAGE_TABLE_SIZE(pages) ((pages) / PTRS_PER_PGD)
5: #endif
下面的计算与上面是相同的, INIT_MAP_SIZE也是1MB
1: /*
2: * Worst-case size of the kernel mapping we need to make:
3: * a relocatable kernel can live anywhere in lowmem, so we need to be able
4: * to map all of lowmem.
5: */
6: KERNEL_PAGES = LOWMEM_PAGES
7:
8: INIT_MAP_SIZE = PAGE_TABLE_SIZE(KERNEL_PAGES) * PAGE_SIZE
9: RESERVE_BRK(pagetables, INIT_MAP_SIZE)
RESERVE_BRK的作用是在.brk区域预留1MB大小的空间,并且设置pagetables为该区域的起始位置的标号名称。
1: /*
2: * Reserve space in the brk section. The name must be unique within
3: * the file, and somewhat descriptive. The size is in bytes. Must be
4: * used at file scope.
5: *
6: * (This uses a temp function to wrap the asm so we can pass it the
7: * size parameter; otherwise we wouldn't be able to. We can't use a
8: * "section" attribute on a normal variable because it always ends up
9: * being @progbits, which ends up allocating space in the vmlinux
10: * executable.)
11: */
12: #define RESERVE_BRK(name,sz) \
13: static void __section(.discard.text) __used notrace \
14: __brk_reservation_fn_##name##__(void) { \
15: asm volatile ( \
16: ".pushsection .brk_reservation,\"aw\",@nobits;" \
17: ".brk." #name ":" \
18: " 1:.skip %c0;" \
19: " .size .brk." #name ", . - 1b;" \
20: " .popsection" \
21: : : "i" (sz)); \
22: }