init.c

来自「底层驱动开发」· C语言 代码 · 共 677 行 · 第 1/2 页

C
677
字号
/* * Initialise the bootmem allocator for all nodes.  This is called * early during the architecture specific initialisation. */static void __init bootmem_init(struct meminfo *mi){	struct node_info node_info[MAX_NUMNODES], *np = node_info;	unsigned int bootmap_pages, bootmap_pfn, map_pg;	int node, initrd_node;	bootmap_pages = find_memend_and_nodes(mi, np);	bootmap_pfn   = find_bootmap_pfn(0, mi, bootmap_pages);	initrd_node   = check_initrd(mi);	map_pg = bootmap_pfn;	/*	 * Initialise the bootmem nodes.	 *	 * What we really want to do is:	 *	 *   unmap_all_regions_except_kernel();	 *   for_each_node_in_reverse_order(node) {	 *     map_node(node);	 *     allocate_bootmem_map(node);	 *     init_bootmem_node(node);	 *     free_bootmem_node(node);	 *   }	 *	 * but this is a 2.5-type change.  For now, we just set	 * the nodes up in reverse order.	 *	 * (we could also do with rolling bootmem_init and paging_init	 * into one generic "memory_init" type function).	 */	np += num_online_nodes() - 1;	for (node = num_online_nodes() - 1; node >= 0; node--, np--) {		/*		 * If there are no pages in this node, ignore it.		 * Note that node 0 must always have some pages.		 */		if (np->end == 0 || !node_online(node)) {			if (node == 0)				BUG();			continue;		}		/*		 * Initialise the bootmem allocator.		 */		init_bootmem_node(NODE_DATA(node), map_pg, np->start, np->end);		free_bootmem_node_bank(node, mi);		map_pg += np->bootmap_pages;		/*		 * If this is node 0, we need to reserve some areas ASAP -		 * we may use bootmem on node 0 to setup the other nodes.		 */		if (node == 0)			reserve_node_zero(bootmap_pfn, bootmap_pages);	}#ifdef CONFIG_BLK_DEV_INITRD	if (phys_initrd_size && initrd_node >= 0) {		reserve_bootmem_node(NODE_DATA(initrd_node), phys_initrd_start,				     phys_initrd_size);		initrd_start = __phys_to_virt(phys_initrd_start);		initrd_end = initrd_start + phys_initrd_size;	}#endif	BUG_ON(map_pg != bootmap_pfn + bootmap_pages);}/* * paging_init() sets up the page tables, initialises the zone memory * maps, and sets up the zero page, bad page and bad page tables. */void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc){	void *zero_page;	int node;	bootmem_init(mi);	memcpy(&meminfo, mi, sizeof(meminfo));	/*	 * allocate the zero page.  Note that we count on this going ok.	 */	zero_page = alloc_bootmem_low_pages(PAGE_SIZE);	/*	 * initialise the page tables.	 */	memtable_init(mi);	if (mdesc->map_io)		mdesc->map_io();	local_flush_tlb_all();	/*	 * initialise the zones within each node	 */	for_each_online_node(node) {		unsigned long zone_size[MAX_NR_ZONES];		unsigned long zhole_size[MAX_NR_ZONES];		struct bootmem_data *bdata;		pg_data_t *pgdat;		int i;		/*		 * Initialise the zone size information.		 */		for (i = 0; i < MAX_NR_ZONES; i++) {			zone_size[i]  = 0;			zhole_size[i] = 0;		}		pgdat = NODE_DATA(node);		bdata = pgdat->bdata;		/*		 * The size of this node has already been determined.		 * If we need to do anything fancy with the allocation		 * of this memory to the zones, now is the time to do		 * it.		 */		zone_size[0] = bdata->node_low_pfn -				(bdata->node_boot_start >> PAGE_SHIFT);		/*		 * If this zone has zero size, skip it.		 */		if (!zone_size[0])			continue;		/*		 * For each bank in this node, calculate the size of the		 * holes.  holes = node_size - sum(bank_sizes_in_node)		 */		zhole_size[0] = zone_size[0];		for (i = 0; i < mi->nr_banks; i++) {			if (mi->bank[i].node != node)				continue;			zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;		}		/*		 * Adjust the sizes according to any special		 * requirements for this machine type.		 */		arch_adjust_zones(node, zone_size, zhole_size);		free_area_init_node(node, pgdat, zone_size,				bdata->node_boot_start >> PAGE_SHIFT, zhole_size);	}	/*	 * finish off the bad pages once	 * the mem_map is initialised	 */	memzero(zero_page, PAGE_SIZE);	empty_zero_page = virt_to_page(zero_page);	flush_dcache_page(empty_zero_page);}static inline void free_area(unsigned long addr, unsigned long end, char *s){	unsigned int size = (end - addr) >> 10;	for (; addr < end; addr += PAGE_SIZE) {		struct page *page = virt_to_page(addr);		ClearPageReserved(page);		set_page_count(page, 1);		free_page(addr);		totalram_pages++;	}	if (size && s)		printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);}static inline voidfree_memmap(int node, unsigned long start_pfn, unsigned long end_pfn){	struct page *start_pg, *end_pg;	unsigned long pg, pgend;	/*	 * Convert start_pfn/end_pfn to a struct page pointer.	 */	start_pg = pfn_to_page(start_pfn);	end_pg = pfn_to_page(end_pfn);	/*	 * Convert to physical addresses, and	 * round start upwards and end downwards.	 */	pg = PAGE_ALIGN(__pa(start_pg));	pgend = __pa(end_pg) & PAGE_MASK;	/*	 * If there are free pages between these,	 * free the section of the memmap array.	 */	if (pg < pgend)		free_bootmem_node(NODE_DATA(node), pg, pgend - pg);}/* * The mem_map array can get very big.  Free the unused area of the memory map. */static void __init free_unused_memmap_node(int node, struct meminfo *mi){	unsigned long bank_start, prev_bank_end = 0;	unsigned int i;	/*	 * [FIXME] This relies on each bank being in address order.  This	 * may not be the case, especially if the user has provided the	 * information on the command line.	 */	for (i = 0; i < mi->nr_banks; i++) {		if (mi->bank[i].size == 0 || mi->bank[i].node != node)			continue;		bank_start = mi->bank[i].start >> PAGE_SHIFT;		if (bank_start < prev_bank_end) {			printk(KERN_ERR "MEM: unordered memory banks.  "				"Not freeing memmap.\n");			break;		}		/*		 * If we had a previous bank, and there is a space		 * between the current bank and the previous, free it.		 */		if (prev_bank_end && prev_bank_end != bank_start)			free_memmap(node, prev_bank_end, bank_start);		prev_bank_end = (mi->bank[i].start +				 mi->bank[i].size) >> PAGE_SHIFT;	}}/* * mem_init() marks the free areas in the mem_map and tells us how much * memory is free.  This is done after various parts of the system have * claimed their memory after the kernel image. */void __init mem_init(void){	unsigned int codepages, datapages, initpages;	int i, node;	codepages = &_etext - &_text;	datapages = &_end - &__data_start;	initpages = &__init_end - &__init_begin;#ifndef CONFIG_DISCONTIGMEM	max_mapnr   = virt_to_page(high_memory) - mem_map;#endif	/* this will put all unused low memory onto the freelists */	for_each_online_node(node) {		pg_data_t *pgdat = NODE_DATA(node);		free_unused_memmap_node(node, &meminfo);		if (pgdat->node_spanned_pages != 0)			totalram_pages += free_all_bootmem_node(pgdat);	}#ifdef CONFIG_SA1111	/* now that our DMA memory is actually so designated, we can free it */	free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);#endif	/*	 * Since our memory may not be contiguous, calculate the	 * real number of pages we have in this system	 */	printk(KERN_INFO "Memory:");	num_physpages = 0;	for (i = 0; i < meminfo.nr_banks; i++) {		num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;		printk(" %ldMB", meminfo.bank[i].size >> 20);	}	printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));	printk(KERN_NOTICE "Memory: %luKB available (%dK code, "		"%dK data, %dK init)\n",		(unsigned long) nr_free_pages() << (PAGE_SHIFT-10),		codepages >> 10, datapages >> 10, initpages >> 10);	if (PAGE_SIZE >= 16384 && num_physpages <= 128) {		extern int sysctl_overcommit_memory;		/*		 * On a machine this small we won't get		 * anywhere without overcommit, so turn		 * it on by default.		 */		sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;	}}void free_initmem(void){	if (!machine_is_integrator() && !machine_is_cintegrator()) {		free_area((unsigned long)(&__init_begin),			  (unsigned long)(&__init_end),			  "init");	}}#ifdef CONFIG_BLK_DEV_INITRDstatic int keep_initrd;void free_initrd_mem(unsigned long start, unsigned long end){	if (!keep_initrd)		free_area(start, end, "initrd");}static int __init keepinitrd_setup(char *__unused){	keep_initrd = 1;	return 1;}__setup("keepinitrd", keepinitrd_setup);#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?