void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn) { #ifdef CONFIG_HIGHMEM highstart_pfn = highend_pfn = max_pfn; if (max_pfn > max_low_pfn) highstart_pfn = max_low_pfn; /*將活動內存放到early_node_map中,前面已經分析過了*/ e820_register_active_regions(0, 0, highend_pfn); /*設置上面變數中的內存為當前,在這裡沒有 設置相關的宏*/ sparse_memory_present_with_active_regions(0); printk(KERN_NOTICE "%ldMB HIGHMEM available.\n", pages_to_mb(highend_pfn - highstart_pfn)); num_physpages = highend_pfn; /*高端內存開始地址物理*/ high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1; #else e820_register_active_regions(0, 0, max_low_pfn); sparse_memory_present_with_active_regions(0); num_physpages = max_low_pfn; high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1; #endif #ifdef CONFIG_FLATMEM max_mapnr = num_physpages; #endif __vmalloc_start_set = true; printk(KERN_NOTICE "%ldMB LOWMEM available.\n", pages_to_mb(max_low_pfn)); /*安裝bootmem分配器,此分配器在夥伴系統起來之前 用來進行承擔內存的分配等管理*/ setup_bootmem_allocator(); }void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn) { #ifdef CONFIG_HIGHMEM highstart_pfn = highend_pfn = max_pfn; if (max_pfn > max_low_pfn) highstart_pfn = max_low_pfn; /*將活動內存放到early_node_map中,前面已經分析過了*/ e820_register_active_regions(0, 0, highend_pfn); /*設置上面變數中的內存為當前,在這裡沒有 設置相關的宏*/ sparse_memory_present_with_active_regions(0); printk(KERN_NOTICE "%ldMB HIGHMEM available.\n", pages_to_mb(highend_pfn - highstart_pfn)); num_physpages = highend_pfn; /*高端內存開始地址物理*/ high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1; #else e820_register_active_regions(0, 0, max_low_pfn); sparse_memory_present_with_active_regions(0); num_physpages = max_low_pfn; high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1; #endif #ifdef CONFIG_FLATMEM max_mapnr = num_physpages; #endif __vmalloc_start_set = true; printk(KERN_NOTICE "%ldMB LOWMEM available.\n", pages_to_mb(max_low_pfn)); /*安裝bootmem分配器,此分配器在夥伴系統起來之前 用來進行承擔內存的分配等管理*/ setup_bootmem_allocator(); } view plaincopy to clipboardprint?void __init setup_bootmem_allocator(void) { int nodeid; unsigned long bootmap_size, bootmap; /* * Initialize the boot-time allocator (with low memory only): */ /*計算所需要的映射頁面大小一個位元組一位, 所以需要對總的頁面大小除以8*/ bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT; /*直接中e820中找到一個大小合適的內存塊,返回基址*/ bootmap = find_e820_area(0, max_pfn_mapped<<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"); printk(KERN_INFO " mapped low ram: 0 - %08lx\n", max_pfn_mapped<<PAGE_SHIFT); printk(KERN_INFO " low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT); /*對每一個在線的node*/ for_each_online_node(nodeid) { unsigned long start_pfn, end_pfn; #ifdef CONFIG_NEED_MULTIPLE_NODES/*not set*/ start_pfn = node_start_pfn; end_pfn = node_end_pfn; if (start_pfn > max_low_pfn) continue; if (end_pfn > max_low_pfn) end_pfn = max_low_pfn; #else start_pfn = 0; end_pfn = max_low_pfn; #endif /*對指定節點安裝啟動分配器*/ bootmap = setup_node_bootmem(nodeid, start_pfn, end_pfn, bootmap); } /*bootmem的分配製度到這裡就已經建立完成,把after_bootmem 變數置成1,標識*/ after_bootmem = 1; } void __init setup_bootmem_allocator(void) { int nodeid; unsigned long bootmap_size, bootmap; /* * Initialize the boot-time allocator (with low memory only): */ /*計算所需要的映射頁面大小一個位元組一位, 所以需要對總的頁面大小除以8*/ bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT; /*直接中e820中找到一個大小合適的內存塊,返回基址*/ bootmap = find_e820_area(0, max_pfn_mapped<<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"); printk(KERN_INFO " mapped low ram: 0 - %08lx\n", max_pfn_mapped<<PAGE_SHIFT); printk(KERN_INFO " low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT); /*對每一個在線的node*/ for_each_online_node(nodeid) { unsigned long start_pfn, end_pfn; #ifdef CONFIG_NEED_MULTIPLE_NODES/*not set*/ start_pfn = node_start_pfn; end_pfn = node_end_pfn; if (start_pfn > max_low_pfn) continue; if (end_pfn > max_low_pfn) end_pfn = max_low_pfn; #else start_pfn = 0; end_pfn = max_low_pfn; #endif /*對指定節點安裝啟動分配器*/ bootmap = setup_node_bootmem(nodeid, start_pfn, end_pfn, bootmap); } /*bootmem的分配製度到這裡就已經建立完成,把after_bootmem 變數置成1,標識*/ after_bootmem = 1; }view plaincopy to clipboardprint?static unsigned long __init setup_node_bootmem(int nodeid, unsigned long start_pfn, unsigned long end_pfn, unsigned long bootmap) { unsigned long bootmap_size; /* don't touch min_low_pfn */ /*初始化映射點陣圖,將點陣圖中的所有位置1*/ bootmap_size = init_bootmem_node(NODE_DATA(nodeid), bootmap >> PAGE_SHIFT, start_pfn, end_pfn); printk(KERN_INFO " node %d low ram: %08lx - %08lx\n", nodeid, start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT); printk(KERN_INFO " node %d bootmap %08lx - %08lx\n", nodeid, bootmap, bootmap + bootmap_size); /*將活動內存區對應點陣圖相關位置0,表示可被分配的*/ free_bootmem_with_active_regions(nodeid, end_pfn); /*對置保留位的相關頁面對應的點陣圖設置為1,表示已經分配 或者不可用(不能被分配)*/ early_res_to_bootmem(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT); /*返回映射頁面的最後地址,下次映射即可以從這裡開始*/ return bootmap + bootmap_size; } static unsigned long __init setup_node_bootmem(int nodeid, unsigned long start_pfn, unsigned long end_pfn, unsigned long bootmap) { unsigned long bootmap_size; /* don't touch min_low_pfn */ /*初始化映射點陣圖,將點陣圖中的所有位置1*/ bootmap_size = init_bootmem_node(NODE_DATA(nodeid), bootmap >> PAGE_SHIFT, start_pfn, end_pfn); printk(KERN_INFO " node %d low ram: %08lx - %08lx\n", nodeid, start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT); printk(KERN_INFO " node %d bootmap %08lx - %08lx\n", nodeid, bootmap, bootmap + bootmap_size); /*將活動內存區對應點陣圖相關位置0,表示可被分配的*/ free_bootmem_with_active_regions(nodeid, end_pfn); /*對置保留位的相關頁面對應的點陣圖設置為1,表示已經分配 或者不可用(不能被分配)*/ early_res_to_bootmem(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT); /*返回映射頁面的最後地址,下次映射即可以從這裡開始*/ return bootmap + bootmap_size; } 對於初始化映射點陣圖,最終調用init_bootmem_core() view plaincopy to clipboardprint?/* * Called once to set up the allocator itself. */ static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, unsigned long mapstart, unsigned long start, unsigned long end) { unsigned long mapsize; mminit_validate_memmodel_limits(&start, &end); bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); bdata->node_min_pfn = start; bdata->node_low_pfn = end; /*添加bdata變數到鏈表中*/ link_bootmem(bdata); /* * Initially all pages are reserved - setup_arch() has to * register free RAM areas explicitly. */ /*計算本bdata的mapsize,也就是內存頁面大小的1/8*/ mapsize = bootmap_bytes(end - start); /*將所有map置1*/ memset(bdata->node_bootmem_map, 0xff, mapsize); bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n", bdata - bootmem_node_data, start, mapstart, end, mapsize); return mapsize; } /* * Called once to set up the allocator itself. */ static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, unsigned long mapstart, unsigned long start, unsigned long end) { unsigned long mapsize; mminit_validate_memmodel_limits(&start, &end); bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); bdata->node_min_pfn = start; bdata->node_low_pfn = end; /*添加bdata變數到鏈表中*/ link_bootmem(bdata); /* * Initially all pages are reserved - setup_arch() has to * register free RAM areas explicitly. */ /*計算本bdata的mapsize,也就是內存頁面大小的1/8*/ mapsize = bootmap_bytes(end - start); /*將所有map置1*/ memset(bdata->node_bootmem_map, 0xff, mapsize); bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n", bdata - bootmem_node_data, start, mapstart, end, mapsize); return mapsize; } view plaincopy to clipboardprint?/* * link bdata in order */ /*添加到鏈表,由添加的代碼可知 鏈表中的數據開始位置為遞增的*/《解決方案》謝謝分享
[火星人 ] linux啟動內存分配器已經有860次圍觀