linux页表机制的建立分为两个阶段,第一个阶段在启动初始化时,调用汇编代码实现临时的分页;第二阶段在内核初始化时建立完整的完整的页表机制。对于物理地址扩展(PAE)分页机制中,intel通过在她得处理器上把管脚数从32增加到36已经满足了这些需求,寻址能力可以达到64GB。不过,只有引入一种新的分页机制把32位线性地址转换为36位物理地址才能使用所增加的物理地址。linux为对多种体系的支持,选择了一套简单的通用实现机制。在这里,只分析x86 32位下的实现。
下图为一个linux分页机制框架图:
一、临时内核页表初始化
在setup_32.S中
[cpp]
- page_pde_offset = (__PAGE_OFFSET >> 20);
- movl $pa(__brk_base), %edi
- movl $pa(swapper_pg_dir), %edx
- movl $PTE_IDENT_ATTR, %eax
- 10:
- leal PDE_IDENT_ATTR(%edi),%ecx /* Create PDE entry */
- movl %ecx,(%edx) /* Store identity PDE entry */
- movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
- addl $4,%edx
- movl $1024, %ecx
- 11:
- stosl
- addl $0x1000,%eax
- loop 11b
- /*
- * End condition: we must map up to the end + MAPPING_BEYOND_END.
- */
- movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %ebp
- cmpl %ebp,%eax
- jb 10b
- addl $__PAGE_OFFSET, %edi
- movl %edi, pa(_brk_end)
- shrl $12, %eax
- movl %eax, pa(max_pfn_mapped)
- /* Do early initialization of the fixmap area */
- movl $pa(swapper_pg_fixmap)+PDE_IDENT_ATTR,%eax
- movl %eax,pa(swapper_pg_dir+0xffc)
对于页表初始化的原理如果可以参考intel手册。
其中常量PTE_IDENT_ATTR定义在patable_types.h中。
[cpp]
- #define PTE_IDENT_ATTR 0x003 /* PRESENT+RW */
二、启动分页机制
实现代码在head_32.S中。
[cpp]
- movl $pa(swapper_pg_dir),%eax
- movl %eax,%cr3 /* set the page table pointer.. */
- movl %cr0,%eax
- orl $X86_CR0_PG,%eax
- movl %eax,%cr0 /* ..and set paging (PG) bit */
三、低端内存和高端内存固定映射区页表机制初始化
在start_kernle()->setup_arch()->init_memory_mapping()中实现具体的页表映射机制的建立。
[cpp]
- void __init setup_arch(char **cmdline_p)
- {
- ……
- /* max_pfn_mapped is updated here */
- /*建立分页机制,包括页表的内存分配,映射的建立包括低端内存的映射建立,高端内 存中固定映射区域的映射建立返回的值为低端内存的总共映射的内存大小,页面为单位 */
- max_low_pfn_mapped = init_memory_mapping(0, max_low_pfn<<PAGE_SHIFT);
- max_pfn_mapped = max_low_pfn_mapped;//将其保存到变量
- ……
- }
[cpp]
- /*
- * Setup the direct mapping of the physical memory at PAGE_OFFSET.
- * This runs before bootmem is initialized and gets pages directly from
- * the physical memory. To access them they are temporarily mapped.
- */
- /*建立页表映射。对于高端内存的固定映射区没有映射到实际的页面,只是这种
- 机制的初步建立,页表存储的空间保留*/
- unsigned long __init_refok init_memory_mapping(unsigned long start,
- unsigned long end)
- {
- unsigned long page_size_mask = 0;
- unsigned long start_pfn, end_pfn;
- unsigned long ret = 0;
- unsigned long pos;
- struct map_range mr[NR_RANGE_MR];
- int nr_range, i;
- int use_pse, use_gbpages;
- printk(KERN_INFO “init_memory_mapping: %016lx-%016lx\n”, start, end);
- #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
- /*
- * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
- * This will simplify cpa(), which otherwise needs to support splitting
- * large pages into small in interrupt context, etc.
- */
- use_pse = use_gbpages = 0;
- #else
- use_pse = cpu_has_pse;
- use_gbpages = direct_gbpages;
- #endif
- //定义了X86_PAE模式后进行调用;
- set_nx();
- if (nx_enabled)
- printk(KERN_INFO “NX (Execute Disable) protection: active\n”);
- /* Enable PSE if available */
- if (cpu_has_pse)
- set_in_cr4(X86_CR4_PSE);
- /* Enable PGE if available */
- if (cpu_has_pge) {
- set_in_cr4(X86_CR4_PGE);
- __supported_pte_mask |= _PAGE_GLOBAL;
- }
- /*page_size_mask在这里更新,在后面对设置页表时用到*/
- if (use_gbpages)
- page_size_mask |= 1 << PG_LEVEL_1G;
- if (use_pse)
- page_size_mask |= 1 << PG_LEVEL_2M;
- memset(mr, 0, sizeof(mr));
- nr_range = 0;
- /* head if not big page alignment ? */
- start_pfn = start >> PAGE_SHIFT;/*在setup函数中调用时,这里为0*/
- pos = start_pfn << PAGE_SHIFT;/*pos为0*/
- #ifdef CONFIG_X86_32
- /*
- * Don’t use a large page for the first 2/4MB of memory
- * because there are often fixed size MTRRs in there
- * and overlapping MTRRs into large pages can cause
- * slowdowns.
- */
- if (pos == 0)/*end_pfn的大小为1k,也就是4M大小的内存*/
- end_pfn = 1<<(PMD_SHIFT – PAGE_SHIFT);
- else
- end_pfn = ((pos + (PMD_SIZE – 1))>>PMD_SHIFT)
- << (PMD_SHIFT – PAGE_SHIFT);
- #else /* CONFIG_X86_64 */
- end_pfn = ((pos + (PMD_SIZE – 1)) >> PMD_SHIFT)
- << (PMD_SHIFT – PAGE_SHIFT);
- #endif
- if (end_pfn > (end >> PAGE_SHIFT))
- end_pfn = end >> PAGE_SHIFT;
- if (start_pfn < end_pfn) {/*4M空间将这个区间存放在mr数组中;*/
- nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
- pos = end_pfn << PAGE_SHIFT;
- }
- /* big page (2M) range */
- /*对齐到PMD,换算成页面的多少*/
- start_pfn = ((pos + (PMD_SIZE – 1))>>PMD_SHIFT)
- << (PMD_SHIFT – PAGE_SHIFT);
- #ifdef CONFIG_X86_32
- /*这里的结束地址设置为调用的结束位页面数,也就是
- 所有的物理页面数*/
- end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT – PAGE_SHIFT);
- #else /* CONFIG_X86_64 */
- end_pfn = ((pos + (PUD_SIZE – 1))>>PUD_SHIFT)
- << (PUD_SHIFT – PAGE_SHIFT);
- if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT – PAGE_SHIFT)))
- end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT – PAGE_SHIFT));
- #endif
- if (start_pfn < end_pfn) {
- /*将这段内存放入mr中,保存后面用到*/
- nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
- page_size_mask & (1<<PG_LEVEL_2M));/*这里保证了运用PSE时为2M页面而不是PSE时,
- 仍然为4K页面(上面的按位或和这里的按位与)*/
- pos = end_pfn << PAGE_SHIFT;/*更新pos*/
- }
- #ifdef CONFIG_X86_64
- /* big page (1G) range */
- start_pfn = ((pos + (PUD_SIZE – 1))>>PUD_SHIFT)
- << (PUD_SHIFT – PAGE_SHIFT);
- end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT – PAGE_SHIFT);
- if (start_pfn < end_pfn) {
- nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
- page_size_mask &
- ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
- pos = end_pfn << PAGE_SHIFT;
- }
- /* tail is not big page (1G) alignment */
- start_pfn = ((pos + (PMD_SIZE – 1))>>PMD_SHIFT)
- << (PMD_SHIFT – PAGE_SHIFT);
- end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT – PAGE_SHIFT);
- if (start_pfn < end_pfn) {
- nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
- page_size_mask & (1<<PG_LEVEL_2M));
- pos = end_pfn << PAGE_SHIFT;
- }
- #endif
- /* tail is not big page (2M) alignment */
- start_pfn = pos>>PAGE_SHIFT;
- end_pfn = end>>PAGE_SHIFT;
- nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
- /* try to merge same page size and continuous */
- /*合并相同页面大小的连续的页面*/
- for (i = 0; nr_range > 1 && i < nr_range – 1; i++) {
- unsigned long old_start;
- if (mr[i].end != mr[i+1].start ||
- mr[i].page_size_mask != mr[i+1].page_size_mask)
- continue;
- /* move it */
- old_start = mr[i].start;
- memmove(&mr[i], &mr[i+1],
- (nr_range – 1 – i) * sizeof(struct map_range));
- mr[i–].start = old_start;
- nr_range–;
- }
- /*打印相关的信息*/
- for (i = 0; i < nr_range; i++)
- printk(KERN_DEBUG ” %010lx – %010lx page %s\n”,
- mr[i].start, mr[i].end,
- (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?“1G”🙁
- (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?“2M”:“4k”));
- /*
- * Find space for the kernel direct mapping tables.
- *
- * Later we should allocate these tables in the local node of the
- * memory mapped. Unfortunately this is done currently before the
- * nodes are discovered.
- */
- if (!after_bootmem)/*如果内存启动分配器没有建立,
- 这里是直接总e820.map中找到合适的连续内存*/
- /*找到存放页表机制中页表的空间
- 首地址为e820_table_start*/
- find_early_table_space(end, use_pse, use_gbpages);
- #ifdef CONFIG_X86_32
- /*设置页表映射,其中变量page_size_mask的值,
- 传入代换成了是否运用PSE来进行页表映射,
- 也就是说,当不用pse时,全部的物理页面都用
- 4K的形式映射,而运用PSE时,开始的4M用4K形式一个页面
- 后面的大块用2M形式作为一个页面映射;*/
- for (i = 0; i < nr_range; i++)/*对每个保存的区域*/
- kernel_physical_mapping_init(mr[i].start, mr[i].end,
- mr[i].page_size_mask);
- ret = end;
- #else /* CONFIG_X86_64 */
- for (i = 0; i < nr_range; i++)
- ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
- mr[i].page_size_mask);
- #endif
- #ifdef CONFIG_X86_32
- /*对高端内存区域的固定区域进行映射而建立了页表等,一般
- 高端固定内存区域为一个页表,具体的页面映射的建立需要set_fix函数*/
- early_ioremap_page_table_range_init();
- /*放入CR3寄存器*/
- load_cr3(swapper_pg_dir);
- #endif
- #ifdef CONFIG_X86_64
- if (!after_bootmem && !start) {
- pud_t *pud;
- pmd_t *pmd;
- mmu_cr4_features = read_cr4();
- /*
- * _brk_end cannot change anymore, but it and _end may be
- * located on different 2M pages. cleanup_highmap(), however,
- * can only consider _end when it runs, so destroy any
- * mappings beyond _brk_end here.
- */
- pud = pud_offset(pgd_offset_k(_brk_end), _brk_end);
- pmd = pmd_offset(pud, _brk_end – 1);
- while (++pmd <= pmd_offset(pud, (unsigned long)_end – 1))
- pmd_clear(pmd);
- }
- #endif
- /*因为将基址放到了CR3寄存器中,所以这里要对其寄存器
- 刷新,以表示将内容放到内存中*/
- __flush_tlb_all();
- /*将分配给建立页表机制的内存空间保留*/
- if (!after_bootmem && e820_table_end > e820_table_start)
- reserve_early(e820_table_start << PAGE_SHIFT,
- e820_table_end << PAGE_SHIFT, “PGTABLE”);
- /*下面的函数需要设置相关的宏没有设置*/
- if (!after_bootmem)
- early_memtest(start, end);
- return ret >> PAGE_SHIFT;
- }
[cpp]
- static int __meminit save_mr(struct map_range *mr, int nr_range,
- unsigned long start_pfn, unsigned long end_pfn,
- unsigned long page_size_mask)
- {
- if (start_pfn < end_pfn) {
- if (nr_range >= NR_RANGE_MR)
- panic(“run out of range for init_memory_mapping\n”);
- mr[nr_range].start = start_pfn<<PAGE_SHIFT;
- mr[nr_range].end = end_pfn<<PAGE_SHIFT;
- mr[nr_range].page_size_mask = page_size_mask;
- nr_range++;
- }
- return nr_range;
- }
[cpp]
- /*查找页表需要的空间*/
- static void __init find_early_table_space(unsigned long end, int use_pse,
- int use_gbpages)
- {
- unsigned long puds, pmds, ptes, tables, start;
- /*计算需要用到多少pud,当没有pud存在的情况下pud=pgd*/
- puds = (end + PUD_SIZE – 1) >> PUD_SHIFT;
- tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
- if (use_gbpages) {
- unsigned long extra;
- extra = end – ((end>>PUD_SHIFT) << PUD_SHIFT);
- pmds = (extra + PMD_SIZE – 1) >> PMD_SHIFT;
- } else
- pmds = (end + PMD_SIZE – 1) >> PMD_SHIFT;
- /*计算映射所有内存所要求的所有pmd的个数*/
- tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
- if (use_pse) {
- unsigned long extra;
- extra = end – ((end>>PMD_SHIFT) << PMD_SHIFT);
- #ifdef CONFIG_X86_32
- extra += PMD_SIZE;
- #endif
- ptes = (extra + PAGE_SIZE – 1) >> PAGE_SHIFT;
- } else/*计算所有需要的pte个数*/
- ptes = (end + PAGE_SIZE – 1) >> PAGE_SHIFT;
- tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
- #ifdef CONFIG_X86_32
- /* for fixmap */
- /*加上固定内存映射区的页表数量*/
- tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
- #endif
- /*
- * RED-PEN putting page tables only on node 0 could
- * cause a hotspot and fill up ZONE_DMA. The page tables
- * need roughly 0.5KB per GB.
- */
- #ifdef CONFIG_X86_32
- start = 0x7000;/*页表存放的开始地址,这里为什么从这里开始???*/
- #else
- start = 0x8000;
- #endif
- /*从e820.map中找到连续的足够大小的内存来存放用于映射的页表
- 返回起始地址*/
- e820_table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
- tables, PAGE_SIZE);
- if (e820_table_start == -1UL)
- panic(“Cannot find space for the kernel page tables”);
- /*将页表的起始地址的物理页面帧号保存到相关的全局变量中*/
- e820_table_start >>= PAGE_SHIFT;
- e820_table_end = e820_table_start;
- e820_table_top = e820_table_start + (tables >> PAGE_SHIFT);
- printk(KERN_DEBUG “kernel direct mapping tables up to %lx @ %lx-%lx\n”,
- end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT);
- }
[cpp]
- /*
- * This maps the physical memory to kernel virtual address space, a total
- * of max_low_pfn pages, by creating page tables starting from address
- * PAGE_OFFSET:
- */
- /*代码的作者已经写的很详细了,自己添加了一些
- 额外的注释*/
- unsigned long __init
- kernel_physical_mapping_init(unsigned long start,
- unsigned long end,
- unsigned long page_size_mask)
- {
- int use_pse = page_size_mask == (1<<PG_LEVEL_2M);
- unsigned long start_pfn, end_pfn;
- pgd_t *pgd_base = swapper_pg_dir;
- int pgd_idx, pmd_idx, pte_ofs;
- unsigned long pfn;
- pgd_t *pgd;
- pmd_t *pmd;
- pte_t *pte;
- unsigned pages_2m, pages_4k;
- int mapping_iter;
- /*换成页面起始*/
- start_pfn = start >> PAGE_SHIFT;
- end_pfn = end >> PAGE_SHIFT;
- /*
- * First iteration will setup identity mapping using large/small pages
- * based on use_pse, with other attributes same as set by
- * the early code in head_32.S
- *
- * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
- * as desired for the kernel identity mapping.
- *
- * This two pass mechanism conforms to the TLB app note which says:
- *
- * “Software should not write to a paging-structure entry in a way
- * that would change, for any linear address, both the page size
- * and either the page frame or attributes.”
- */
- mapping_iter = 1;
- if (!cpu_has_pse)
- use_pse = 0;
- repeat:
- pages_2m = pages_4k = 0;
- pfn = start_pfn;
- /*返回页面在PGD中的索引*/
- pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
- pgd = pgd_base + pgd_idx;
- for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
- pmd = one_md_table_init(pgd);/*pgd对应的第一个pmd*/
- if (pfn >= end_pfn)
- continue;
- #ifdef CONFIG_X86_PAE
- pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
- pmd += pmd_idx;
- #else
- pmd_idx = 0;
- #endif
- for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
- pmd++, pmd_idx++) {
- unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
- /*
- * Map with big pages if possible, otherwise
- * create normal page tables:
- */
- if (use_pse) {
- unsigned int addr2;
- pgprot_t prot = PAGE_KERNEL_LARGE;
- /*
- * first pass will use the same initial
- * identity mapping attribute + _PAGE_PSE.
- */
- pgprot_t init_prot =
- __pgprot(PTE_IDENT_ATTR |
- _PAGE_PSE);
- addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
- PAGE_OFFSET + PAGE_SIZE-1;
- if (is_kernel_text(addr) ||
- is_kernel_text(addr2))
- prot = PAGE_KERNEL_LARGE_EXEC;
- pages_2m++;
- if (mapping_iter == 1)
- set_pmd(pmd, pfn_pmd(pfn, init_prot));
- else
- set_pmd(pmd, pfn_pmd(pfn, prot));
- pfn += PTRS_PER_PTE;
- continue;
- }
- /*返回PMD中第一个PTE*/
- pte = one_page_table_init(pmd);
- /*PTE的索引*/
- pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
- pte += pte_ofs;/*定位带具体的pte*/
- for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
- pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
- pgprot_t prot = PAGE_KERNEL;
- /*
- * first pass will use the same initial
- * identity mapping attribute.
- */
- pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
- if (is_kernel_text(addr))/*是否为内核镜像中的地址空间*/
- prot = PAGE_KERNEL_EXEC;
- pages_4k++;/*没有PSE*/
- /*设置页表,根据MAPPING_ITER变量的不同
- 对表设置不同的属性,*/
- if (mapping_iter == 1)/*第一次迭代,属性设置都一样*/
- set_pte(pte, pfn_pte(pfn, init_prot));
- else/*设置为具体的属性*/
- set_pte(pte, pfn_pte(pfn, prot));
- }
- }
- }
- if (mapping_iter == 1) {
- /*
- * update direct mapping page count only in the first
- * iteration.
- */
- update_page_count(PG_LEVEL_2M, pages_2m);
- update_page_count(PG_LEVEL_4K, pages_4k);
- /*
- * local global flush tlb, which will flush the previous
- * mappings present in both small and large page TLB’s.
- */
- __flush_tlb_all();
- /*
- * Second iteration will set the actual desired PTE attributes.
- */
- mapping_iter = 2;
- goto repeat;
- }
- return 0;/*迭代两次后返回*/
- }
[cpp]
- /*
- * Build a proper pagetable for the kernel mappings. Up until this
- * point, we’ve been running on some set of pagetables constructed by
- * the boot process.
- *
- * If we’re booting on native hardware, this will be a pagetable
- * constructed in arch/x86/kernel/head_32.S. The root of the
- * pagetable will be swapper_pg_dir.
- *
- * If we’re booting paravirtualized under a hypervisor, then there are
- * more options: we may already be running PAE, and the pagetable may
- * or may not be based in swapper_pg_dir. In any case,
- * paravirt_pagetable_setup_start() will set up swapper_pg_dir
- * appropriately for the rest of the initialization to work.
- *
- * In general, pagetable_init() assumes that the pagetable may already
- * be partially populated, and so it avoids stomping on any existing
- * mappings.
- */
- void __init early_ioremap_page_table_range_init(void)
- {
- pgd_t *pgd_base = swapper_pg_dir;
- unsigned long vaddr, end;
- /*
- * Fixed mappings, only the page table structure has to be
- * created – mappings will be set by set_fixmap():
- */
- vaddr = __fix_to_virt(__end_of_fixed_addresses – 1) & PMD_MASK;
- end = (FIXADDR_TOP + PMD_SIZE – 1) & PMD_MASK;
- /*这里是对临时映射区域进行映射而为页表等分配了空间,
- 但是没有建立实际的映射*/
- page_table_range_init(vaddr, end, pgd_base);
- /*置变量after_paging_init为1,表示启动了分业制度*/
- early_ioremap_reset();
- }
[cpp]
- /*
- * This function initializes a certain range of kernel virtual memory
- * with new bootmem page tables, everywhere page tables are missing in
- * the given range.
- *
- * NOTE: The pagetables are allocated contiguous on the physical space
- * so we can cache the place of the first one and move around without
- * checking the pgd every time.
- */
- static void __init
- page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
- {
- int pgd_idx, pmd_idx;
- unsigned long vaddr;
- pgd_t *pgd;
- pmd_t *pmd;
- pte_t *pte = NULL;
- vaddr = start;
- pgd_idx = pgd_index(vaddr);
- pmd_idx = pmd_index(vaddr);
- pgd = pgd_base + pgd_idx;
- for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
- pmd = one_md_table_init(pgd);
- pmd = pmd + pmd_index(vaddr);
- for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
- pmd++, pmd_idx++) {
- /*early fixmap可能对临时映射区中的页表项已经分配了页表
- 为使页表分配的空间连续,需要对临时映射区的页表从
- 指定区间重新分配*/
- pte = page_table_kmap_check(one_page_table_init(pmd),/*在这里已经对pte进行了分配和初始化*/
- pmd, vaddr, pte);
- vaddr += PMD_SIZE;
- }
- pmd_idx = 0;
- }
- }
[cpp]
- /*
- * Create a page table and place a pointer to it in a middle page
- * directory entry:
- */
- static pte_t * __init one_page_table_init(pmd_t *pmd)
- {
- if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
- pte_t *page_table = NULL;
- if (after_bootmem) {
- #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
- page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
- #endif
- if (!page_table)
- page_table =
- (pte_t *)alloc_bootmem_pages(PAGE_SIZE);
- } else/*如果启动分配制度还没有建立,那么
- 从刚才分配建立的表中分配空间*/
- page_table = (pte_t *)alloc_low_page();
- paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
- /*设置PMD,将对应的PMD项设置为页表*/
- set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
- BUG_ON(page_table != pte_offset_kernel(pmd, 0));
- }
- return pte_offset_kernel(pmd, 0);
- }
start_kernel()->setup_arch()->paging()->pagetable_init()建立对高端永久内存的初始化
此时,启动内存分配器已经建立,所有不用从上面找出的页面中分配了。况且,上面建立的连续内存块只够低端内存和高端固定内存映射大小。
最终由下面函数实现
[cpp]
- static void __init permanent_kmaps_init(pgd_t *pgd_base)
- {
- unsigned long vaddr;
- pgd_t *pgd;
- pud_t *pud;
- pmd_t *pmd;
- pte_t *pte;
- vaddr = PKMAP_BASE;
- /*该阶段,也就是永久内存映射区的内存的页表初始化*/
- page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
- pgd = swapper_pg_dir + pgd_index(vaddr);
- pud = pud_offset(pgd, vaddr);
- pmd = pmd_offset(pud, vaddr);
- pte = pte_offset_kernel(pmd, vaddr);
- pkmap_page_table = pte;/*将永久映射区间映射的第一个页表保存到pkmap_page_table中*/
- }
start_kernel()->setup_arch()->paging()->kmap_init()
[cpp]
- static void __init kmap_init(void)
- {
- unsigned long kmap_vstart;
- /*
- * Cache the first kmap pte:
- */
- /*得到高端固定内存映射区域的起始内存的页表,将这个页表
- 放到kmap_pte变量中确切的说应该是固定内存中的临时内存映射区域*/
- kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
- kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
- kmap_prot = PAGE_KERNEL;
- }