For x86_64 Architecture:
1.
Linux启动阶段用的分配器是 boot allocator, 在start_kernel()中调用mm_init(), 然后把boot allocator管理的物理内存 交给buddy allocator, 以__free_pages()的方式把pages放到buddy的数据结构free_areas中。
mm_init() > mem_init() > free_all_bootmem() > free_all_bootmem_core() > __free_pages()
2.
boot allocator, kernel首先调用initmem_init()把管理的所有页都标记为已经分配,然后再选择空闲的页标记为0,表示可以分配。然后,调用
void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn,
int acpi, int k8)
{
#ifndef CONFIG_NO_BOOTMEM
unsigned long bootmap_size, bootmap;
bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
PAGE_SIZE);
if (bootmap == -1L)
panic("Cannot find bootmem map of size %ld\n", bootmap_size);
reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
/* don't touch min_low_pfn */
bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
0, end_pfn);
e820_register_active_regions(0, start_pfn, end_pfn);
free_bootmem_with_active_regions(0, end_pfn);
#else
e820_register_active_regions(0, start_pfn, end_pfn);
#endif
}
#endif
void __init paging_init(void)
{
unsigned long max_zone_pfns[MAX_NR_ZONES];
memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
max_zone_pfns[ZONE_NORMAL] = max_pfn;
sparse_memory_present_with_active_regions(MAX_NUMNODES);
sparse_init();
/*
* clear the default setting with node 0
* note: don't use nodes_clear here, that is really clearing when
* numa support is not compiled in, and later node_set_state
* will not set it back.
*/
node_clear_state(0, N_NORMAL_MEMORY);
free_area_init_nodes(max_zone_pfns);
}
|
然后,调用early_res_to_bootmem(),
dma32_reserve_bootmem()
保留一部分页,不能交给boot allocator分配。为什么要这样做呢?前面free的页包括内核代码和数据占用的页,如果这些页也拿给boot
allocator的话,系统可能会崩溃,所以,要把这些页标记为保留。 x86_64_start_reservations()中调用reserve_early(__pa_symbol(&_text), __pa_symbol(&__bss_stop), "TEXT DATA BSS"), 通过这种方式把要保留的地址段记录在early_res[]数组中,而early_res_to_bootmem()则根据early_res[]数组中记录的要保留的地址信息进行保留页操作。
void __init early_res_to_bootmem(u64 start, u64 end)
{
int i, count;
u64 final_start, final_end;
int idx = 0;
count = 0;
for (i = 0; i < max_early_res && early_res[i].end; i++)
count++;
/* need to skip first one ?*/
if (early_res != early_res_x)
idx = 1;
printk(KERN_INFO "(%d/%d early reservations) ==> bootmem [%010llx - %010llx]\n",
count - idx, max_early_res, start, end);
for (i = idx; i < count; i++) {
struct early_res *r = &early_res[i];
printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
r->start, r->end, r->name);
final_start = max(start, r->start);
final_end = min(end, r->end);
if (final_start >= final_end) {
printk(KERN_CONT "\n");
continue;
}
printk(KERN_CONT " ==> [%010llx - %010llx]\n",
final_start, final_end);
reserve_bootmem_generic(final_start, final_end - final_start%2
|
阅读(1858) | 评论(1) | 转发(0) |